@theokit/sdk 2.23.0 → 2.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,7 +23,8 @@
23
23
  * @public
24
24
  */
25
25
  import type { ZodType } from "zod";
26
- import type { SDKAgent } from "./types/agent.js";
26
+ import { z } from "zod";
27
+ import type { CustomTool, SDKAgent } from "./types/agent.js";
27
28
  import type { MessageOrigin } from "./types/run.js";
28
29
  import type { AgentStep, BranchStep, DowhileStep, FnStep, RetryPolicy, Step, StepContext, WorkflowOptions, WorkflowResumeOptions, WorkflowRun, WorkflowRunOptions } from "./types/workflow.js";
29
30
  export declare class WorkflowBuilder<TInput = unknown, TOutput = unknown> {
@@ -97,3 +98,48 @@ export declare function agentStep(id: string, agent: SDKAgent, promptTemplate: s
97
98
  export { __resetSnapshotStoresForTests } from "./internal/workflow/snapshot-store.js";
98
99
  export type * from "./types/workflow.js";
99
100
  export { WorkflowAlreadyRunningError, WorkflowCompensateNotImplementedError, WorkflowDuplicateStepIdError, WorkflowMaxIterationsExceededError, WorkflowNotSerializableError, WorkflowParallelError, WorkflowResumeStepNotFoundError, WorkflowSnapshotNotFoundError, } from "./types/workflow.js";
101
+ /**
102
+ * Raised by a {@link workflowAsTool} tool when the wrapped workflow run does not
103
+ * reach `status: "completed"` (a step failed, the run was cancelled/suspended).
104
+ * The dispatch converts it to a `tool_result(isError)`.
105
+ *
106
+ * @public
107
+ */
108
+ export declare class WorkflowToolError extends Error {
109
+ readonly toolName: string;
110
+ readonly workflowStatus: string;
111
+ readonly workflowError?: {
112
+ name: string;
113
+ message: string;
114
+ } | undefined;
115
+ readonly code: "workflow_tool_failed";
116
+ constructor(toolName: string, workflowStatus: string, workflowError?: {
117
+ name: string;
118
+ message: string;
119
+ } | undefined);
120
+ }
121
+ /** Spec for {@link workflowAsTool}. `inputSchema` is the workflow's input shape (Zod). */
122
+ export interface WorkflowAsToolSpec<T extends ZodType> {
123
+ /** Tool name surfaced to the LLM. Same constraints as a `CustomTool.name`. */
124
+ name: string;
125
+ /** Description surfaced to the LLM — when the agent should trigger the workflow. */
126
+ description: string;
127
+ /** Zod schema for the workflow's input (a `Workflow` carries no top-level schema). */
128
+ inputSchema: T;
129
+ }
130
+ /**
131
+ * SE19 — expose a {@link Workflow} as an agent {@link CustomTool}, completing the
132
+ * "X as tools" trio (tools; agents-as-tools via `defineSubAgent`; workflows-as-tools).
133
+ * The handler validates the model's args against `spec.inputSchema`, runs the
134
+ * workflow, and returns its output (a string as-is, else JSON). A run that does not
135
+ * reach `status: "completed"` raises a typed {@link WorkflowToolError} (workflow
136
+ * step errors do NOT throw — they surface via `run.status === "failed"`).
137
+ *
138
+ * Accepts any `{ run }`-shaped workflow (structural), so it never imports the
139
+ * `Workflow` class directly.
140
+ *
141
+ * @public
142
+ */
143
+ export declare function workflowAsTool<T extends ZodType, TOutput = unknown>(workflow: {
144
+ run: (input: z.infer<T>) => Promise<WorkflowRun<TOutput>>;
145
+ }, spec: WorkflowAsToolSpec<T>): CustomTool;
@@ -23,7 +23,8 @@
23
23
  * @public
24
24
  */
25
25
  import type { ZodType } from "zod";
26
- import type { SDKAgent } from "./types/agent.js";
26
+ import { z } from "zod";
27
+ import type { CustomTool, SDKAgent } from "./types/agent.js";
27
28
  import type { MessageOrigin } from "./types/run.js";
28
29
  import type { AgentStep, BranchStep, DowhileStep, FnStep, RetryPolicy, Step, StepContext, WorkflowOptions, WorkflowResumeOptions, WorkflowRun, WorkflowRunOptions } from "./types/workflow.js";
29
30
  export declare class WorkflowBuilder<TInput = unknown, TOutput = unknown> {
@@ -97,3 +98,48 @@ export declare function agentStep(id: string, agent: SDKAgent, promptTemplate: s
97
98
  export { __resetSnapshotStoresForTests } from "./internal/workflow/snapshot-store.js";
98
99
  export type * from "./types/workflow.js";
99
100
  export { WorkflowAlreadyRunningError, WorkflowCompensateNotImplementedError, WorkflowDuplicateStepIdError, WorkflowMaxIterationsExceededError, WorkflowNotSerializableError, WorkflowParallelError, WorkflowResumeStepNotFoundError, WorkflowSnapshotNotFoundError, } from "./types/workflow.js";
101
+ /**
102
+ * Raised by a {@link workflowAsTool} tool when the wrapped workflow run does not
103
+ * reach `status: "completed"` (a step failed, the run was cancelled/suspended).
104
+ * The dispatch converts it to a `tool_result(isError)`.
105
+ *
106
+ * @public
107
+ */
108
+ export declare class WorkflowToolError extends Error {
109
+ readonly toolName: string;
110
+ readonly workflowStatus: string;
111
+ readonly workflowError?: {
112
+ name: string;
113
+ message: string;
114
+ } | undefined;
115
+ readonly code: "workflow_tool_failed";
116
+ constructor(toolName: string, workflowStatus: string, workflowError?: {
117
+ name: string;
118
+ message: string;
119
+ } | undefined);
120
+ }
121
+ /** Spec for {@link workflowAsTool}. `inputSchema` is the workflow's input shape (Zod). */
122
+ export interface WorkflowAsToolSpec<T extends ZodType> {
123
+ /** Tool name surfaced to the LLM. Same constraints as a `CustomTool.name`. */
124
+ name: string;
125
+ /** Description surfaced to the LLM — when the agent should trigger the workflow. */
126
+ description: string;
127
+ /** Zod schema for the workflow's input (a `Workflow` carries no top-level schema). */
128
+ inputSchema: T;
129
+ }
130
+ /**
131
+ * SE19 — expose a {@link Workflow} as an agent {@link CustomTool}, completing the
132
+ * "X as tools" trio (tools; agents-as-tools via `defineSubAgent`; workflows-as-tools).
133
+ * The handler validates the model's args against `spec.inputSchema`, runs the
134
+ * workflow, and returns its output (a string as-is, else JSON). A run that does not
135
+ * reach `status: "completed"` raises a typed {@link WorkflowToolError} (workflow
136
+ * step errors do NOT throw — they surface via `run.status === "failed"`).
137
+ *
138
+ * Accepts any `{ run }`-shaped workflow (structural), so it never imports the
139
+ * `Workflow` class directly.
140
+ *
141
+ * @public
142
+ */
143
+ export declare function workflowAsTool<T extends ZodType, TOutput = unknown>(workflow: {
144
+ run: (input: z.infer<T>) => Promise<WorkflowRun<TOutput>>;
145
+ }, spec: WorkflowAsToolSpec<T>): CustomTool;
package/dist/workflow.js CHANGED
@@ -4,7 +4,7 @@ import { join, dirname } from 'path';
4
4
  import { createRequire } from 'module';
5
5
  import { mkdirSync, readdirSync } from 'fs';
6
6
  import { AsyncLocalStorage } from 'async_hooks';
7
- import { z } from 'zod';
7
+ import { z, toJSONSchema } from 'zod';
8
8
 
9
9
  var __defProp = Object.defineProperty;
10
10
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -2241,6 +2241,9 @@ function sanitizeIdentifier(input, options) {
2241
2241
  }
2242
2242
  return input.toLowerCase();
2243
2243
  }
2244
+ function toJsonSchema(schema, options = { unrepresentable: "any" }) {
2245
+ return toJSONSchema(schema, options);
2246
+ }
2244
2247
 
2245
2248
  // src/workflow.ts
2246
2249
  init_workflow();
@@ -2467,6 +2470,38 @@ function agentStep(id, agent, promptTemplate, opts) {
2467
2470
  ...opts?.origin !== void 0 ? { origin: opts.origin } : {}
2468
2471
  };
2469
2472
  }
2473
+ var WorkflowToolError = class extends Error {
2474
+ constructor(toolName, workflowStatus, workflowError) {
2475
+ super(
2476
+ `workflowAsTool("${toolName}"): workflow ${workflowStatus}${workflowError ? `: ${workflowError.message}` : ""}`
2477
+ );
2478
+ this.toolName = toolName;
2479
+ this.workflowStatus = workflowStatus;
2480
+ this.workflowError = workflowError;
2481
+ this.name = "WorkflowToolError";
2482
+ }
2483
+ toolName;
2484
+ workflowStatus;
2485
+ workflowError;
2486
+ code = "workflow_tool_failed";
2487
+ };
2488
+ function workflowAsTool(workflow, spec) {
2489
+ const inputSchema = toJsonSchema(spec.inputSchema, { unrepresentable: "any" });
2490
+ return {
2491
+ name: spec.name,
2492
+ description: spec.description,
2493
+ inputSchema,
2494
+ handler: async (rawInput) => {
2495
+ const parsed = spec.inputSchema.parse(rawInput);
2496
+ const run = await workflow.run(parsed);
2497
+ if (run.status !== "completed") {
2498
+ throw new WorkflowToolError(spec.name, run.status, run.error);
2499
+ }
2500
+ const output = run.output;
2501
+ return typeof output === "string" ? output : JSON.stringify(output ?? null);
2502
+ }
2503
+ };
2504
+ }
2470
2505
  function validateStepId(id) {
2471
2506
  sanitizeIdentifier(id, { maxLen: 64 });
2472
2507
  }
@@ -2492,6 +2527,6 @@ function walkStepsValidating(steps, seen) {
2492
2527
  }
2493
2528
  }
2494
2529
 
2495
- export { Workflow, WorkflowAlreadyRunningError, WorkflowBuilder, WorkflowCompensateNotImplementedError, WorkflowDuplicateStepIdError, WorkflowMaxIterationsExceededError, WorkflowNotSerializableError, WorkflowParallelError, WorkflowResumeStepNotFoundError, WorkflowSnapshotNotFoundError, __resetSnapshotStoresForTests, agentStep, fn };
2530
+ export { Workflow, WorkflowAlreadyRunningError, WorkflowBuilder, WorkflowCompensateNotImplementedError, WorkflowDuplicateStepIdError, WorkflowMaxIterationsExceededError, WorkflowNotSerializableError, WorkflowParallelError, WorkflowResumeStepNotFoundError, WorkflowSnapshotNotFoundError, WorkflowToolError, __resetSnapshotStoresForTests, agentStep, fn, workflowAsTool };
2496
2531
  //# sourceMappingURL=workflow.js.map
2497
2532
  //# sourceMappingURL=workflow.js.map