assistant-stream 0.3.13 → 0.3.15

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.
Files changed (103) hide show
  1. package/README.md +39 -0
  2. package/dist/core/AssistantStream.d.ts +37 -0
  3. package/dist/core/AssistantStream.d.ts.map +1 -1
  4. package/dist/core/AssistantStream.js +22 -0
  5. package/dist/core/AssistantStream.js.map +1 -1
  6. package/dist/core/AssistantStreamChunk.d.ts +32 -0
  7. package/dist/core/AssistantStreamChunk.d.ts.map +1 -1
  8. package/dist/core/accumulators/assistant-message-accumulator.d.ts.map +1 -1
  9. package/dist/core/accumulators/assistant-message-accumulator.js +3 -0
  10. package/dist/core/accumulators/assistant-message-accumulator.js.map +1 -1
  11. package/dist/core/modules/assistant-stream.d.ts +68 -0
  12. package/dist/core/modules/assistant-stream.d.ts.map +1 -1
  13. package/dist/core/modules/assistant-stream.js +23 -0
  14. package/dist/core/modules/assistant-stream.js.map +1 -1
  15. package/dist/core/modules/tool-call.d.ts.map +1 -1
  16. package/dist/core/modules/tool-call.js +3 -0
  17. package/dist/core/modules/tool-call.js.map +1 -1
  18. package/dist/core/tool/ToolExecutionStream.d.ts.map +1 -1
  19. package/dist/core/tool/ToolExecutionStream.js +3 -0
  20. package/dist/core/tool/ToolExecutionStream.js.map +1 -1
  21. package/dist/core/tool/ToolResponse.d.ts +44 -0
  22. package/dist/core/tool/ToolResponse.d.ts.map +1 -1
  23. package/dist/core/tool/ToolResponse.js +27 -0
  24. package/dist/core/tool/ToolResponse.js.map +1 -1
  25. package/dist/core/tool/tool-types.d.ts +119 -2
  26. package/dist/core/tool/tool-types.d.ts.map +1 -1
  27. package/dist/core/tool/toolResultStream.d.ts +15 -0
  28. package/dist/core/tool/toolResultStream.d.ts.map +1 -1
  29. package/dist/core/tool/toolResultStream.js +39 -1
  30. package/dist/core/tool/toolResultStream.js.map +1 -1
  31. package/dist/core/utils/types.d.ts +4 -0
  32. package/dist/core/utils/types.d.ts.map +1 -1
  33. package/dist/index.d.ts +1 -1
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/resumable/ResumableStreamContext.d.ts +27 -0
  37. package/dist/resumable/ResumableStreamContext.d.ts.map +1 -0
  38. package/dist/resumable/ResumableStreamContext.js +121 -0
  39. package/dist/resumable/ResumableStreamContext.js.map +1 -0
  40. package/dist/resumable/constants.d.ts +2 -0
  41. package/dist/resumable/constants.d.ts.map +1 -0
  42. package/dist/resumable/constants.js +2 -0
  43. package/dist/resumable/constants.js.map +1 -0
  44. package/dist/resumable/createResumableAssistantStreamResponse.d.ts +24 -0
  45. package/dist/resumable/createResumableAssistantStreamResponse.d.ts.map +1 -0
  46. package/dist/resumable/createResumableAssistantStreamResponse.js +40 -0
  47. package/dist/resumable/createResumableAssistantStreamResponse.js.map +1 -0
  48. package/dist/resumable/errors.d.ts +7 -0
  49. package/dist/resumable/errors.d.ts.map +1 -0
  50. package/dist/resumable/errors.js +15 -0
  51. package/dist/resumable/errors.js.map +1 -0
  52. package/dist/resumable/index.d.ts +7 -0
  53. package/dist/resumable/index.d.ts.map +1 -0
  54. package/dist/resumable/index.js +5 -0
  55. package/dist/resumable/index.js.map +1 -0
  56. package/dist/resumable/stores/InMemoryResumableStreamStore.d.ts +13 -0
  57. package/dist/resumable/stores/InMemoryResumableStreamStore.d.ts.map +1 -0
  58. package/dist/resumable/stores/InMemoryResumableStreamStore.js +199 -0
  59. package/dist/resumable/stores/InMemoryResumableStreamStore.js.map +1 -0
  60. package/dist/resumable/stores/ioredis.d.ts +10 -0
  61. package/dist/resumable/stores/ioredis.d.ts.map +1 -0
  62. package/dist/resumable/stores/ioredis.js +95 -0
  63. package/dist/resumable/stores/ioredis.js.map +1 -0
  64. package/dist/resumable/stores/redis-impl.d.ts +60 -0
  65. package/dist/resumable/stores/redis-impl.d.ts.map +1 -0
  66. package/dist/resumable/stores/redis-impl.js +198 -0
  67. package/dist/resumable/stores/redis-impl.js.map +1 -0
  68. package/dist/resumable/stores/redis.d.ts +39 -0
  69. package/dist/resumable/stores/redis.d.ts.map +1 -0
  70. package/dist/resumable/stores/redis.js +113 -0
  71. package/dist/resumable/stores/redis.js.map +1 -0
  72. package/dist/resumable/types.d.ts +30 -0
  73. package/dist/resumable/types.d.ts.map +1 -0
  74. package/dist/resumable/types.js +2 -0
  75. package/dist/resumable/types.js.map +1 -0
  76. package/package.json +28 -2
  77. package/src/core/AssistantStream.ts +37 -0
  78. package/src/core/AssistantStreamChunk.ts +32 -0
  79. package/src/core/accumulators/assistant-message-accumulator.ts +3 -0
  80. package/src/core/modules/assistant-stream.ts +68 -0
  81. package/src/core/modules/tool-call.ts +3 -0
  82. package/src/core/tool/ToolExecutionStream.ts +3 -0
  83. package/src/core/tool/ToolResponse.ts +50 -0
  84. package/src/core/tool/tool-types.ts +125 -2
  85. package/src/core/tool/toolResultStream.test.ts +360 -2
  86. package/src/core/tool/toolResultStream.ts +45 -1
  87. package/src/core/utils/types.ts +4 -0
  88. package/src/index.ts +5 -1
  89. package/src/resumable/ResumableStreamContext.test.ts +274 -0
  90. package/src/resumable/ResumableStreamContext.ts +187 -0
  91. package/src/resumable/__tests__/integration.test.ts +159 -0
  92. package/src/resumable/constants.ts +1 -0
  93. package/src/resumable/createResumableAssistantStreamResponse.test.ts +243 -0
  94. package/src/resumable/createResumableAssistantStreamResponse.ts +80 -0
  95. package/src/resumable/errors.ts +26 -0
  96. package/src/resumable/index.ts +36 -0
  97. package/src/resumable/stores/InMemoryResumableStreamStore.test.ts +285 -0
  98. package/src/resumable/stores/InMemoryResumableStreamStore.ts +237 -0
  99. package/src/resumable/stores/ioredis.ts +123 -0
  100. package/src/resumable/stores/redis-impl.ts +304 -0
  101. package/src/resumable/stores/redis.test.ts +265 -0
  102. package/src/resumable/stores/redis.ts +171 -0
  103. package/src/resumable/types.ts +49 -0
@@ -1,6 +1,13 @@
1
1
  import type { ReadonlyJSONValue } from "../utils/json/json-value";
2
2
  import type { ObjectStreamOperation } from "./object/types";
3
+ import type { ToolModelContentPart } from "./tool/tool-types";
3
4
 
5
+ /**
6
+ * Initial metadata for a stream part.
7
+ *
8
+ * A part starts with `part-start`, receives zero or more chunks at the same
9
+ * path, and ends with `part-finish`.
10
+ */
4
11
  export type PartInit =
5
12
  | {
6
13
  readonly type: "text" | "reasoning";
@@ -33,34 +40,49 @@ export type PartInit =
33
40
  readonly parentId?: string;
34
41
  };
35
42
 
43
+ /**
44
+ * Normalized assistant-ui streaming protocol chunk.
45
+ *
46
+ * `path` identifies the part or nested position the chunk belongs to. Encoders
47
+ * may translate these chunks into provider-specific wire formats, while
48
+ * accumulators consume them to build assistant messages.
49
+ */
36
50
  export type AssistantStreamChunk = { readonly path: readonly number[] } & (
37
51
  | {
52
+ /** Opens a new content part at `path`. */
38
53
  readonly type: "part-start";
39
54
  readonly part: PartInit;
40
55
  }
41
56
  | {
57
+ /** Closes the current part at `path`. */
42
58
  readonly type: "part-finish";
43
59
  }
44
60
  | {
61
+ /** Marks streamed tool-call argument text as complete. */
45
62
  readonly type: "tool-call-args-text-finish";
46
63
  }
47
64
  | {
65
+ /** Appends text to a text, reasoning, or tool-call argument part. */
48
66
  readonly type: "text-delta";
49
67
  readonly textDelta: string;
50
68
  }
51
69
  | {
70
+ /** Appends provider or application annotations to the current message. */
52
71
  readonly type: "annotations";
53
72
  readonly annotations: ReadonlyJSONValue[];
54
73
  }
55
74
  | {
75
+ /** Emits application data chunks associated with the current message. */
56
76
  readonly type: "data";
57
77
  readonly data: ReadonlyJSONValue[];
58
78
  }
59
79
  | {
80
+ /** Starts a model generation step. */
60
81
  readonly type: "step-start";
61
82
  readonly messageId: string;
62
83
  }
63
84
  | {
85
+ /** Finishes a model generation step and reports usage for that step. */
64
86
  readonly type: "step-finish";
65
87
  readonly finishReason:
66
88
  | "stop"
@@ -77,6 +99,7 @@ export type AssistantStreamChunk = { readonly path: readonly number[] } & (
77
99
  readonly isContinued: boolean;
78
100
  }
79
101
  | {
102
+ /** Finishes the assistant message and reports final usage. */
80
103
  readonly type: "message-finish";
81
104
  readonly finishReason:
82
105
  | "stop"
@@ -92,17 +115,26 @@ export type AssistantStreamChunk = { readonly path: readonly number[] } & (
92
115
  };
93
116
  }
94
117
  | {
118
+ /**
119
+ * Emits the result for a tool-call part.
120
+ *
121
+ * `artifact` is UI-visible metadata, while `modelContent` can override
122
+ * what is sent back to the model.
123
+ */
95
124
  readonly type: "result";
96
125
  readonly artifact?: ReadonlyJSONValue;
97
126
  readonly result: ReadonlyJSONValue;
98
127
  readonly isError: boolean;
128
+ readonly modelContent?: readonly ToolModelContentPart[];
99
129
  readonly messages?: ReadonlyJSONValue;
100
130
  }
101
131
  | {
132
+ /** Emits a stream-level error message. */
102
133
  readonly type: "error";
103
134
  readonly error: string;
104
135
  }
105
136
  | {
137
+ /** Applies object-stream operations to state carried by this stream. */
106
138
  readonly type: "update-state";
107
139
  readonly operations: ObjectStreamOperation[];
108
140
  }
@@ -216,6 +216,9 @@ const handleResult = (
216
216
  ...(chunk.artifact !== undefined ? { artifact: chunk.artifact } : {}),
217
217
  result: chunk.result,
218
218
  isError: chunk.isError ?? false,
219
+ ...(chunk.modelContent !== undefined
220
+ ? { modelContent: chunk.modelContent }
221
+ : {}),
219
222
  ...(chunk.messages !== undefined ? { messages: chunk.messages } : {}),
220
223
  status: { type: "complete", reason: "stop" },
221
224
  };
@@ -29,18 +29,63 @@ type ToolCallPartInit = {
29
29
  response?: ToolResponseLike<ReadonlyJSONValue>;
30
30
  };
31
31
 
32
+ /**
33
+ * Imperative writer for constructing an {@link AssistantStream}.
34
+ *
35
+ * The controller handles part boundaries for common streaming operations. Use
36
+ * `appendText` and `appendReasoning` for simple token streams, or open explicit
37
+ * parts with `addTextPart` and `addToolCallPart` when you need direct control.
38
+ */
32
39
  export type AssistantStreamController = {
40
+ /** Appends text to the current text part, opening one if needed. */
33
41
  appendText(textDelta: string): void;
42
+ /** Appends reasoning text to the current reasoning part, opening one if needed. */
34
43
  appendReasoning(reasoningDelta: string): void;
44
+ /** Appends a source citation part to the stream. */
35
45
  appendSource(options: SourcePart): void;
46
+ /** Appends a file part to the stream. */
36
47
  appendFile(options: FilePart): void;
48
+ /** Appends a named data part to the stream. */
37
49
  appendData(options: DataPart): void;
50
+ /**
51
+ * Opens a text part and returns its writer.
52
+ *
53
+ * Close the returned controller when the text part is complete. Opening a new
54
+ * part through this controller closes any implicit text or reasoning append
55
+ * part first.
56
+ */
38
57
  addTextPart(): TextStreamController;
58
+ /**
59
+ * Opens a tool-call part by tool name and returns its writer.
60
+ *
61
+ * A tool call id is generated automatically. Use the object overload when the
62
+ * caller already has an id, initial args, args text, or response.
63
+ */
39
64
  addToolCallPart(options: string): ToolCallStreamController;
65
+ /**
66
+ * Opens a tool-call part and returns its writer.
67
+ *
68
+ * Use this overload to provide a stable `toolCallId`, initial arguments,
69
+ * streamed argument text, or an immediate {@link ToolResponseLike}.
70
+ */
40
71
  addToolCallPart(options: ToolCallPartInit): ToolCallStreamController;
72
+ /** Enqueues a raw protocol chunk. Prefer higher-level helpers when possible. */
41
73
  enqueue(chunk: AssistantStreamChunk): void;
74
+ /**
75
+ * Merges another assistant stream into this stream.
76
+ *
77
+ * Paths from the merged stream are remapped so its parts appear at the next
78
+ * available position in this controller's output.
79
+ */
42
80
  merge(stream: AssistantStream): void;
81
+ /** Closes any active part and finishes the stream. */
43
82
  close(): void;
83
+ /**
84
+ * Returns a controller that writes child parts with `parentId` attached.
85
+ *
86
+ * Use this for nested or related parts that should be associated with an
87
+ * existing message or part in downstream renderers.
88
+ */
44
89
  withParentId(parentId: string): AssistantStreamController;
45
90
  };
46
91
 
@@ -226,6 +271,15 @@ class AssistantStreamControllerImpl implements AssistantStreamController {
226
271
  }
227
272
  }
228
273
 
274
+ /**
275
+ * Creates an {@link AssistantStream} and writes to it with an
276
+ * {@link AssistantStreamController}.
277
+ *
278
+ * The callback may write synchronously or asynchronously. If it throws, an
279
+ * `error` chunk is emitted before the error is rethrown; when the callback
280
+ * settles, the stream is closed automatically unless the controller was
281
+ * already closed.
282
+ */
229
283
  export function createAssistantStream(
230
284
  callback: (controller: AssistantStreamController) => PromiseLike<void> | void,
231
285
  ): AssistantStream {
@@ -254,6 +308,13 @@ export function createAssistantStream(
254
308
  return controller.__internal_getReadable();
255
309
  }
256
310
 
311
+ /**
312
+ * Creates an {@link AssistantStream} together with the controller used to
313
+ * write into it.
314
+ *
315
+ * Use this when the stream needs to be returned before all writers are known.
316
+ * Closing the returned controller finishes the paired stream.
317
+ */
257
318
  export function createAssistantStreamController() {
258
319
  const { resolve, promise } = promiseWithResolvers<void>();
259
320
  let controller!: AssistantStreamController;
@@ -269,6 +330,13 @@ export function createAssistantStreamController() {
269
330
  return [stream, controller] as const;
270
331
  }
271
332
 
333
+ /**
334
+ * Creates a `Response` whose body is an encoded {@link AssistantStream}.
335
+ *
336
+ * This is the HTTP-route convenience form of {@link createAssistantStream}; it
337
+ * uses {@link DataStreamEncoder} so the response can be consumed by matching
338
+ * assistant-ui data stream decoders.
339
+ */
272
340
  export function createAssistantStreamResponse(
273
341
  callback: (controller: AssistantStreamController) => PromiseLike<void> | void,
274
342
  ) {
@@ -76,6 +76,9 @@ class ToolCallStreamControllerImpl implements ToolCallStreamController {
76
76
  : {}),
77
77
  result: response.result,
78
78
  isError: response.isError ?? false,
79
+ ...(response.modelContent !== undefined
80
+ ? { modelContent: response.modelContent }
81
+ : {}),
79
82
  ...(response.messages !== undefined
80
83
  ? { messages: response.messages }
81
84
  : {}),
@@ -104,6 +104,7 @@ export class ToolExecutionStream extends PipeableTransformStream<
104
104
  result: chunk.result,
105
105
  artifact: chunk.artifact,
106
106
  isError: chunk.isError,
107
+ modelContent: chunk.modelContent,
107
108
  }),
108
109
  );
109
110
  break;
@@ -156,6 +157,8 @@ export class ToolExecutionStream extends PipeableTransformStream<
156
157
  artifact: c.artifact,
157
158
  result: c.result,
158
159
  isError: c.isError,
160
+ messages: c.messages,
161
+ modelContent: c.modelContent,
159
162
  });
160
163
  streamController.setResponse(result);
161
164
  controller.enqueue({
@@ -1,14 +1,53 @@
1
1
  import type { ReadonlyJSONValue } from "../../utils/json/json-value";
2
+ import type {
3
+ ToolModelContentPart,
4
+ ToolModelOutputFunction,
5
+ } from "./tool-types";
2
6
 
3
7
  const TOOL_RESPONSE_SYMBOL = Symbol.for("aui.tool-response");
4
8
 
9
+ /**
10
+ * Shape accepted anywhere a {@link ToolResponse} can be returned.
11
+ */
5
12
  export type ToolResponseLike<TResult> = {
13
+ /** UI-visible tool result value. */
6
14
  result: TResult;
15
+ /**
16
+ * Optional UI-only artifact associated with the result.
17
+ *
18
+ * Artifacts are useful for large or structured data that should be available
19
+ * to renderers without necessarily being sent back to the model.
20
+ */
7
21
  artifact?: ReadonlyJSONValue | undefined;
22
+ /** Marks the tool result as an error result. */
8
23
  isError?: boolean | undefined;
24
+ /**
25
+ * Explicit model-visible content to send back after the tool call.
26
+ *
27
+ * When omitted, assistant-ui derives model output from `result` or a tool's
28
+ * {@link ToolModelOutputFunction}.
29
+ */
30
+ modelContent?: readonly ToolModelContentPart[] | undefined;
31
+ /** Optional provider-specific message payload associated with the tool result. */
9
32
  messages?: ReadonlyJSONValue | undefined;
10
33
  };
11
34
 
35
+ /**
36
+ * Tool result wrapper for separating UI-visible output from model-visible
37
+ * output.
38
+ *
39
+ * Return `ToolResponse` from a tool when you need to attach an artifact, mark
40
+ * the result as an error, or control the content sent back to the model.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * return new ToolResponse({
45
+ * result: { title: "Report ready" },
46
+ * artifact: { reportId },
47
+ * modelContent: [{ type: "text", text: "The report is ready." }],
48
+ * });
49
+ * ```
50
+ */
12
51
  export class ToolResponse<TResult> {
13
52
  get [TOOL_RESPONSE_SYMBOL]() {
14
53
  return true;
@@ -17,6 +56,7 @@ export class ToolResponse<TResult> {
17
56
  readonly artifact?: ReadonlyJSONValue;
18
57
  readonly result: TResult;
19
58
  readonly isError: boolean;
59
+ readonly modelContent?: readonly ToolModelContentPart[];
20
60
  readonly messages?: ReadonlyJSONValue;
21
61
 
22
62
  constructor(options: ToolResponseLike<TResult>) {
@@ -25,6 +65,9 @@ export class ToolResponse<TResult> {
25
65
  }
26
66
  this.result = options.result;
27
67
  this.isError = options.isError ?? false;
68
+ if (options.modelContent !== undefined) {
69
+ this.modelContent = options.modelContent;
70
+ }
28
71
  if (options.messages !== undefined) {
29
72
  this.messages = options.messages;
30
73
  }
@@ -38,6 +81,13 @@ export class ToolResponse<TResult> {
38
81
  );
39
82
  }
40
83
 
84
+ /**
85
+ * Converts a plain tool return value into a {@link ToolResponse}.
86
+ *
87
+ * Existing `ToolResponse` instances are returned unchanged. `undefined`
88
+ * becomes the string `"<no result>"` so downstream protocol chunks always
89
+ * carry a concrete result.
90
+ */
41
91
  static toResponse(result: any | ToolResponse<any>): ToolResponse<any> {
42
92
  if (result instanceof ToolResponse) {
43
93
  return result;
@@ -4,6 +4,63 @@ import type { AsyncIterableStream } from "../../utils";
4
4
  import type { StandardSchemaV1 } from "@standard-schema/spec";
5
5
  import type { ToolResponse } from "./ToolResponse";
6
6
 
7
+ export type ToolModelContentPart =
8
+ | {
9
+ /** A text content part returned to the model after a tool call. */
10
+ readonly type: "text";
11
+ /** Text that should be included in the model-visible tool result. */
12
+ readonly text: string;
13
+ }
14
+ | {
15
+ /** A file content part returned to the model after a tool call. */
16
+ readonly type: "file";
17
+ /**
18
+ * File payload encoded as a provider-compatible string, commonly base64
19
+ * for binary data.
20
+ */
21
+ readonly data: string;
22
+ /** MIME type for the file payload. */
23
+ readonly mediaType: string;
24
+ /** Optional display filename for the file payload. */
25
+ readonly filename?: string;
26
+ };
27
+
28
+ /**
29
+ * Converts a tool's runtime result into content that is sent back to the
30
+ * model.
31
+ *
32
+ * Return this when the value shown in the UI or stored as the tool result
33
+ * should differ from the model-visible response. When omitted, the successful
34
+ * tool result is sent back to the model as-is. If a tool returns a
35
+ * `ToolResponse` with `modelContent`, that explicit content is used instead
36
+ * of calling this function.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const toModelOutput: ToolModelOutputFunction<
41
+ * { documentId: string },
42
+ * { summary: string; pdfBase64: string }
43
+ * > = ({ output }) => [
44
+ * { type: "text", text: output.summary },
45
+ * {
46
+ * type: "file",
47
+ * data: output.pdfBase64,
48
+ * mediaType: "application/pdf",
49
+ * },
50
+ * ];
51
+ * ```
52
+ */
53
+ export type ToolModelOutputFunction<TArgs, TResult> = (options: {
54
+ /** Stable identifier for the tool call being converted. */
55
+ toolCallId: string;
56
+ /** Arguments supplied by the model for this tool call. */
57
+ input: TArgs;
58
+ /** Value returned by the tool's {@link ToolExecuteFunction}. */
59
+ output: TResult;
60
+ }) =>
61
+ | readonly ToolModelContentPart[]
62
+ | Promise<readonly ToolModelContentPart[]>;
63
+
7
64
  /**
8
65
  * Interface for reading tool call arguments from a stream, which are
9
66
  * generated by a language learning model (LLM). Provides methods to
@@ -70,7 +127,8 @@ export interface ToolCallReader<
70
127
  response: ToolCallResponseReader<TResult>;
71
128
 
72
129
  /**
73
- * @deprecated Deprecated. Use `response.get().result` instead.
130
+ * @deprecated Use {@link ToolCallReader.response} and read
131
+ * `response.get().result` instead.
74
132
  */
75
133
  result: {
76
134
  get: () => Promise<TResult>;
@@ -78,11 +136,20 @@ export interface ToolCallReader<
78
136
  }
79
137
 
80
138
  export type ToolExecutionContext = {
139
+ /** Stable identifier for the tool call being executed. */
81
140
  toolCallId: string;
141
+ /** Signal that is aborted when the current run is cancelled. */
82
142
  abortSignal: AbortSignal;
143
+ /**
144
+ * From inside a frontend tool's execute function, request human input from
145
+ * the UI. Resolves with the payload the UI supplies.
146
+ */
83
147
  human: (payload: unknown) => Promise<unknown>;
84
148
  };
85
149
 
150
+ /**
151
+ * Function called when assistant-ui executes a frontend tool.
152
+ */
86
153
  export type ToolExecuteFunction<TArgs, TResult> = (
87
154
  args: TArgs,
88
155
  context: ToolExecutionContext,
@@ -115,12 +182,14 @@ type BackendTool<
115
182
  TArgs extends Record<string, unknown> = Record<string, unknown>,
116
183
  TResult = unknown,
117
184
  > = ToolBase<TArgs, TResult> & {
185
+ /** Tool that is executed by the backend rather than in the browser. */
118
186
  type: "backend";
119
187
 
120
188
  description?: undefined;
121
189
  parameters?: undefined;
122
190
  disabled?: undefined;
123
191
  execute?: undefined;
192
+ toModelOutput?: undefined;
124
193
  experimental_onSchemaValidationError?: undefined;
125
194
  };
126
195
 
@@ -128,12 +197,20 @@ type FrontendTool<
128
197
  TArgs extends Record<string, unknown> = Record<string, unknown>,
129
198
  TResult = unknown,
130
199
  > = ToolBase<TArgs, TResult> & {
200
+ /** Tool that is executed in the frontend runtime. */
131
201
  type: "frontend";
132
202
 
203
+ /** Natural-language description shown to the model when selecting tools. */
133
204
  description?: string | undefined;
205
+ /** Schema for the arguments the model must provide when calling the tool. */
134
206
  parameters: StandardSchemaV1<TArgs> | JSONSchema7;
207
+ /** Prevents the tool from being exposed to the model while true. */
135
208
  disabled?: boolean;
209
+ /** Executes the tool after the model provides valid arguments. */
136
210
  execute: ToolExecuteFunction<TArgs, TResult>;
211
+ /** Converts the execution result into model-visible output. */
212
+ toModelOutput?: ToolModelOutputFunction<TArgs, TResult>;
213
+ /** Handles invalid tool arguments when schema validation fails. */
137
214
  experimental_onSchemaValidationError?: OnSchemaValidationErrorFunction<TResult>;
138
215
  };
139
216
 
@@ -141,15 +218,61 @@ type HumanTool<
141
218
  TArgs extends Record<string, unknown> = Record<string, unknown>,
142
219
  TResult = unknown,
143
220
  > = ToolBase<TArgs, TResult> & {
221
+ /** Tool that pauses the run until a user or UI supplies a result. */
144
222
  type: "human";
145
223
 
224
+ /** Natural-language description shown to the model when selecting tools. */
146
225
  description?: string | undefined;
226
+ /** Schema for the arguments the model must provide when requesting input. */
147
227
  parameters: StandardSchemaV1<TArgs> | JSONSchema7;
228
+ /** Prevents the tool from being exposed to the model while true. */
148
229
  disabled?: boolean;
149
230
  execute?: undefined;
231
+ toModelOutput?: undefined;
150
232
  experimental_onSchemaValidationError?: undefined;
151
233
  };
152
234
 
235
+ /**
236
+ * Definition for a tool that can be exposed to the assistant model.
237
+ *
238
+ * Use `type: "frontend"` for tools executed in the browser, `type: "backend"`
239
+ * for tools handled by your server, and `type: "human"` for flows that pause
240
+ * until the UI supplies a result.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * const frontendTool: Tool<{ city: string }, string> = {
245
+ * type: "frontend",
246
+ * description: "Get the weather for a city.",
247
+ * parameters: {
248
+ * type: "object",
249
+ * properties: { city: { type: "string" } },
250
+ * required: ["city"],
251
+ * },
252
+ * execute: async ({ city }) => `Sunny in ${city}`,
253
+ * };
254
+ * ```
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * const backendTool: Tool = {
259
+ * type: "backend",
260
+ * };
261
+ * ```
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * const humanTool: Tool<{ question: string }, string> = {
266
+ * type: "human",
267
+ * description: "Ask the user a follow-up question.",
268
+ * parameters: {
269
+ * type: "object",
270
+ * properties: { question: { type: "string" } },
271
+ * required: ["question"],
272
+ * },
273
+ * };
274
+ * ```
275
+ */
153
276
  export type Tool<
154
277
  TArgs extends Record<string, unknown> = Record<string, unknown>,
155
278
  TResult = unknown,
@@ -160,7 +283,7 @@ export type Tool<
160
283
  | ToolWithoutType<TArgs, TResult>;
161
284
 
162
285
  /**
163
- * @deprecated Use `Tool` with an explicit `type` field instead.
286
+ * @deprecated Use {@link Tool} with an explicit `type` field instead.
164
287
  */
165
288
  export type ToolWithoutType<
166
289
  TArgs extends Record<string, unknown> = Record<string, unknown>,