{"version":3,"file":"as-tool.mjs","names":[],"sources":["../../../../../../../ai/src/workflow/as-tool.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { WorkflowInstance } from \"../contracts/workflow/workflow.contract\";\nimport { WorkflowError } from \"../errors\";\nimport { compositeAsTool, type ToolContract } from \"../tool/tool\";\n\n/**\n * Wrap a `WorkflowInstance` as a `ToolContract` so an agent can invoke\n * it inside its tool-call loop. Closes the agent-calls-workflow\n * composition gap.\n *\n * Behavior:\n * - Tool `name` mirrors `workflow.name` — workflows without a name throw\n *   `WorkflowError` at wrap time (the agent surface needs a stable id).\n * - Tool `input` is the supplied `inputSchema`; the validated value is\n *   forwarded straight to `workflow.execute(input)`.\n * - On `result.error`, the workflow error is wrapped in\n *   `ToolExecutionError` with `cause` set to the original\n *   `WorkflowError` subclass — the agent's tool-call loop sees a\n *   uniform error class regardless of which primitive failed.\n *\n * @example\n * const wf = workflow({ name: \"triage\", steps: [...] });\n * const triageTool = asTool(wf, {\n *   description: \"Run the support-ticket triage flow\",\n *   inputSchema: ticketSchema,\n * });\n * const a = ai.agent({ model, tools: [triageTool] });\n */\nexport function asTool<TInput, TOutput, TToolInput = TInput>(\n  workflowInstance: WorkflowInstance<TInput, TOutput>,\n  options: {\n    description?: string;\n    inputSchema: StandardSchemaV1<TToolInput>;\n  },\n): ToolContract<TToolInput, TOutput> {\n  if (!workflowInstance.name || typeof workflowInstance.name !== \"string\") {\n    throw new WorkflowError(\n      \"workflow.asTool(): workflow must have a `name` to be wrapped as a tool\",\n    );\n  }\n\n  return compositeAsTool<TToolInput, TOutput>({\n    name: workflowInstance.name,\n    description: options.description ?? `Invoke workflow \"${workflowInstance.name}\" as a tool.`,\n    input: options.inputSchema,\n    execute: async (input, ctx) => {\n      // Relay the outer agent's cancellation signal so cancelling the\n      // parent aborts this nested workflow run (C2).\n      const result = await workflowInstance.execute(\n        input as unknown as TInput,\n        ctx?.signal ? { signal: ctx.signal } : undefined,\n      );\n\n      if (result.error) {\n        // Throw the workflow error so the surrounding wrapper catches\n        // it and produces a `ToolExecutionError` with `cause` pointing\n        // back at the original `WorkflowError` subclass — keeps the\n        // agent's tool-call loop seeing one uniform error class\n        // regardless of which primitive failed.\n        throw result.error;\n      }\n\n      return {\n        data: result.data as TOutput,\n        usage: result.usage,\n        report: result.report,\n      };\n    },\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,OACd,kBACA,SAImC;CACnC,IAAI,CAAC,iBAAiB,QAAQ,OAAO,iBAAiB,SAAS,UAC7D,MAAM,IAAI,cACR,wEACF;CAGF,OAAO,gBAAqC;EAC1C,MAAM,iBAAiB;EACvB,aAAa,QAAQ,eAAe,oBAAoB,iBAAiB,KAAK;EAC9E,OAAO,QAAQ;EACf,SAAS,OAAO,OAAO,QAAQ;GAG7B,MAAM,SAAS,MAAM,iBAAiB,QACpC,OACA,KAAK,SAAS,EAAE,QAAQ,IAAI,OAAO,IAAI,MACzC;GAEA,IAAI,OAAO,OAMT,MAAM,OAAO;GAGf,OAAO;IACL,MAAM,OAAO;IACb,OAAO,OAAO;IACd,QAAQ,OAAO;GACjB;EACF;CACF,CAAC;AACH"}