{"version":3,"file":"AIUITools.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/GenerativeUI/AIUITools.ts"],"sourcesContent":["/**\n * One of the app's tools a reply's UI can run, such as to load data on this device, described\n * as the agent's model reads it. It is apart from the tools the agent itself calls. The\n * counterpart of `AIClientTool` in StreamChatAI.\n */\nexport type AIUIClientToolDefinition = {\n  name: string;\n  /** What it does, for the agent's model. */\n  description?: string;\n  /** The JSON Schema of its arguments, for the agent's model. */\n  inputSchema?: Record<string, unknown>;\n  /**\n   * Whether it only reads, so the UI may run it whenever it asks, such as to show its output.\n   * One that changes something runs only from something the person did, such as a tap.\n   */\n  readOnly?: boolean;\n};\n\nexport type AIUIClientTool = AIUIClientToolDefinition & {\n  /** Runs the tool. What it returns, as JSON, is the output the UI gets. */\n  run: (args: Record<string, unknown>) => unknown;\n};\n\n/** Why one of the app's tools didn't give a reply's UI its output. */\nexport class AIUIToolError extends Error {\n  readonly toolName: string;\n\n  constructor(toolName: string, message: string, options?: { cause?: unknown }) {\n    super(message, options);\n    this.name = 'AIUIToolError';\n    this.toolName = toolName;\n  }\n}\n\n/** A tool as the agent's backend reads it, in the shape of an MCP tool. */\nexport const aiUIClientToolJSON = ({\n  description,\n  inputSchema,\n  name,\n  readOnly,\n}: AIUIClientToolDefinition) => ({\n  annotations: { readOnlyHint: readOnly === true },\n  ...(description ? { description } : {}),\n  inputSchema: inputSchema ?? { type: 'object' },\n  name,\n});\n\n/**\n * Whether the person is acting on the page right now, such as having just tapped, as the\n * browser tells it. The browser's activation is page-wide and lasts a few seconds, so it can't\n * tell a tap on a reply's UI from sending a message: the renderers ask whether the person\n * pressed a control in the reply itself instead.\n */\nexport const isPersonActing = () => {\n  if (typeof navigator === 'undefined') return false;\n  const activation = (navigator as { userActivation?: { isActive?: boolean } })\n    .userActivation;\n  return activation?.isActive === true;\n};\n\n// Whether a tool's output can go to the UI as JSON, as it would from the agent.\nexport const isJSONValue = (value: unknown): boolean => {\n  if (value === undefined) return true;\n  try {\n    return JSON.stringify(value) !== undefined;\n  } catch {\n    return false;\n  }\n};\n"],"mappings":";;AAwBA,IAAa,gBAAb,cAAmC,MAAM;CACvC;CAEA,YAAY,UAAkB,SAAiB,SAA+B;EAC5E,MAAM,SAAS,OAAO;EACtB,KAAK,OAAO;EACZ,KAAK,WAAW;CAClB;AACF;;AAGA,IAAa,sBAAsB,EACjC,aACA,aACA,MACA,gBAC+B;CAC/B,aAAa,EAAE,cAAc,aAAa,KAAK;CAC/C,GAAI,cAAc,EAAE,YAAY,IAAI,CAAC;CACrC,aAAa,eAAe,EAAE,MAAM,SAAS;CAC7C;AACF;;;;;;;AAQA,IAAa,uBAAuB;CAClC,IAAI,OAAO,cAAc,aAAa,OAAO;CAG7C,OAFoB,UACjB,gBACgB,aAAa;AAClC;AAGA,IAAa,eAAe,UAA4B;CACtD,IAAI,UAAU,KAAA,GAAW,OAAO;CAChC,IAAI;EACF,OAAO,KAAK,UAAU,KAAK,MAAM,KAAA;CACnC,QAAQ;EACN,OAAO;CACT;AACF"}