@theokit/sdk 2.22.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.
@@ -1,13 +1,21 @@
1
1
  import type { z as ZodNamespace, ZodType } from "zod";
2
2
  import type { SanitizeOptions } from "./sanitize/types.js";
3
3
  import type { CustomTool } from "./types/agent.js";
4
+ import type { ToolResultContentBlock } from "./types/content-blocks.js";
4
5
  /**
5
6
  * Spec accepted by {@link defineTool}. `inputSchema` is a Zod schema; the
6
7
  * `handler` argument type is inferred via `z.infer<T>` — no `as` casts.
7
8
  *
8
9
  * @public
9
10
  */
10
- export interface DefineToolSpec<T extends ZodType> {
11
+ /**
12
+ * SE16 — the handler's return type. With no `outputSchema` the tool returns a
13
+ * plain `string` (pre-SE16 shape). With an `outputSchema` the handler returns the
14
+ * STRUCTURED output inferred from it (validated + serialized to the tool result).
15
+ * The `[O]` tuple wrap prevents distribution so `never` maps cleanly to `string`.
16
+ */
17
+ type ToolHandlerReturn<O extends ZodType> = [O] extends [never] ? string : ZodNamespace.infer<O>;
18
+ export interface DefineToolSpec<T extends ZodType, O extends ZodType = never> {
11
19
  /** Tool name surfaced to the LLM. Same constraints as {@link CustomTool.name}. */
12
20
  name: string;
13
21
  /** Description surfaced to the LLM. */
@@ -15,7 +23,16 @@ export interface DefineToolSpec<T extends ZodType> {
15
23
  /** Zod schema describing the input. Must be `z.object(...)` at the root for the LLM tool contract. */
16
24
  inputSchema: T;
17
25
  /**
18
- * Handler invoked with the parsed input. Type is inferred via `z.infer<T>`.
26
+ * SE16 optional Zod schema describing the OUTPUT. When set, the handler
27
+ * returns the structured value inferred from it; the value is validated against
28
+ * this schema and serialized to the tool result (a string stays as-is, an object
29
+ * is JSON-stringified). A validation failure raises `ZodError`, converted to a
30
+ * `tool_result(isError)`. Absent ⇒ the handler returns a plain string (unchanged).
31
+ */
32
+ outputSchema?: O;
33
+ /**
34
+ * Handler invoked with the parsed input. Type is inferred via `z.infer<T>`; the
35
+ * return type is `z.infer<O>` when `outputSchema` is set, else `string`.
19
36
  * #65 — an optional 2nd `ToolContext` argument carries the run's `AbortSignal`,
20
37
  * so a cooperative handler can stop early when the run is cancelled. Existing
21
38
  * single-argument handlers are unaffected.
@@ -23,7 +40,18 @@ export interface DefineToolSpec<T extends ZodType> {
23
40
  handler: (input: ZodNamespace.infer<T>, ctx?: {
24
41
  signal?: AbortSignal;
25
42
  context?: unknown;
26
- }) => string | Promise<string>;
43
+ }) => ToolHandlerReturn<O> | Promise<ToolHandlerReturn<O>>;
44
+ /**
45
+ * SE17 — map the handler's (validated) output to the compact / multimodal
46
+ * representation the MODEL sees in the tool_result. The handler keeps returning
47
+ * the FULL result (validated by `outputSchema`); `toModelOutput` shapes only what
48
+ * reaches the model, so app-facing detail is not forced into model context.
49
+ * Returns a string OR SE7 `ToolResultContentBlock[]` (text + image). Absent ⇒
50
+ * the tool_result is the serialized handler output (SE16 / pre-SE17 behavior).
51
+ * Note: observability (`onToolEnd`) sees the model-facing result this returns,
52
+ * not the raw handler output — the full result lives in the handler's own scope.
53
+ */
54
+ toModelOutput?: (output: ToolHandlerReturn<O>) => string | ToolResultContentBlock[];
27
55
  /**
28
56
  * Sanitize the raw model-emitted args BEFORE schema validation (`@theokit/sdk/sanitize`).
29
57
  * `true` trims whitespace; an object opts into coercion / JSON-repair. Coercion is schema-aware
@@ -32,20 +60,5 @@ export interface DefineToolSpec<T extends ZodType> {
32
60
  */
33
61
  sanitize?: boolean | SanitizeOptions;
34
62
  }
35
- /**
36
- * Type-safe builder for {@link CustomTool}. Converts a Zod schema to JSON
37
- * Schema (for the LLM-facing `inputSchema` field), wraps the handler with a
38
- * runtime `schema.parse` step, and preserves type inference.
39
- *
40
- * Behaviour (ADR D24):
41
- * - JSON Schema conversion uses Zod 4's native `z.toJSONSchema` with
42
- * `unrepresentable: "any"` so transforms/refinements round-trip.
43
- * - Runtime parse failures throw `ZodError`; the SDK's tool-dispatch converts
44
- * them to `tool_result(isError)` with the Zod message.
45
- * - Handler signature is `(input: z.infer<T>)`, not `Record<string, unknown>`.
46
- * - `zod` loads lazily via `createRequire` — consumers who don't call
47
- * `defineTool` don't need `zod` installed.
48
- *
49
- * @public
50
- */
51
- export declare function defineTool<T extends ZodType>(spec: DefineToolSpec<T>): CustomTool;
63
+ export declare function defineTool<T extends ZodType, O extends ZodType = never>(spec: DefineToolSpec<T, O>): CustomTool;
64
+ export {};
@@ -1,4 +1,4 @@
1
- import { v as RunOperation } from './run-CrIulPF7.cjs';
1
+ import { v as RunOperation } from './run-Cr0C6cOM.cjs';
2
2
 
3
3
  /**
4
4
  * Public type contract for the Budget enforcement primitive
@@ -1,4 +1,4 @@
1
- import { v as RunOperation } from './run-CrIulPF7.js';
1
+ import { v as RunOperation } from './run-Cr0C6cOM.js';
2
2
 
3
3
  /**
4
4
  * Public type contract for the Budget enforcement primitive
package/dist/errors.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export { A as AgentDisposedError, c as AgentRunError, d as AgentRunErrorCode, e as AuthenticationError, g as BudgetExceededError, C as ConfigurationError, u as CredentialPoolExhaustedError, m as ErrorCode, E as ErrorMetadata, I as IntegrationNotConnectedError, n as InvalidTaskIdError, K as KnownAgentRunErrorCode, M as MemoryAdapterError, o as MemoryAdapterErrorCode, N as NetworkError, R as RateLimitError, p as TaskNotFoundError, T as TheokitAgentError, U as UnknownAgentError, q as UnsupportedBudgetOperationError, r as UnsupportedRunOperationError, s as UnsupportedTaskOperationError, t as isTransientError } from './errors-CkCaIqVP.cjs';
2
- import './run-CrIulPF7.cjs';
1
+ export { A as AgentDisposedError, c as AgentRunError, d as AgentRunErrorCode, e as AuthenticationError, g as BudgetExceededError, C as ConfigurationError, u as CredentialPoolExhaustedError, m as ErrorCode, E as ErrorMetadata, I as IntegrationNotConnectedError, n as InvalidTaskIdError, K as KnownAgentRunErrorCode, M as MemoryAdapterError, o as MemoryAdapterErrorCode, N as NetworkError, R as RateLimitError, p as TaskNotFoundError, T as TheokitAgentError, U as UnknownAgentError, q as UnsupportedBudgetOperationError, r as UnsupportedRunOperationError, s as UnsupportedTaskOperationError, t as isTransientError } from './errors-DIKBXffg.cjs';
2
+ import './run-Cr0C6cOM.cjs';
3
3
  import 'zod';
package/dist/eval.cjs CHANGED
@@ -13302,6 +13302,7 @@ function levenshtein(a, b) {
13302
13302
  }
13303
13303
 
13304
13304
  // src/internal/runtime/local-agent/real-local-run.ts
13305
+ init_async_local_storage();
13305
13306
  function createRealLocalRun(options) {
13306
13307
  const { userText, id, startTime } = prepareRunContext(options.message);
13307
13308
  const supported = /* @__PURE__ */ new Set(["stream", "wait", "cancel", "conversation"]);
@@ -13413,6 +13414,7 @@ function buildLoopInputs(options, runId, userText) {
13413
13414
  ...options.onStep !== void 0 ? { onStep: options.onStep } : {},
13414
13415
  ...options.onDelta !== void 0 ? { onDelta: options.onDelta } : {},
13415
13416
  ...options.sendOptions.toolChoice !== void 0 ? { toolChoice: options.sendOptions.toolChoice } : {},
13417
+ ...options.sendOptions.activeTools !== void 0 ? { activeTools: options.sendOptions.activeTools } : {},
13416
13418
  ...options.priorMessages !== void 0 ? { priorMessages: options.priorMessages } : {},
13417
13419
  ...options.memoryTools !== void 0 && options.memoryTools.length > 0 ? { memoryTools: options.memoryTools } : {},
13418
13420
  ...buildCustomToolsInput(
@@ -13543,7 +13545,7 @@ var RealLocalRun = class extends FixtureRunBase {
13543
13545
  }
13544
13546
  async executeAgentLoop(inputs) {
13545
13547
  try {
13546
- const output = await runAgentLoop(inputs);
13548
+ const output = inputs.activeTools !== void 0 ? await withToolWhitelist(new Set(inputs.activeTools), () => runAgentLoop(inputs)) : await runAgentLoop(inputs);
13547
13549
  this.applyAgentLoopOutput(output);
13548
13550
  this.transitionTo(output.finalStatus);
13549
13551
  } catch (cause) {