assistant-stream 0.3.14 → 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.
@@ -6,19 +6,56 @@ import type { ToolResponse } from "./ToolResponse";
6
6
 
7
7
  export type ToolModelContentPart =
8
8
  | {
9
+ /** A text content part returned to the model after a tool call. */
9
10
  readonly type: "text";
11
+ /** Text that should be included in the model-visible tool result. */
10
12
  readonly text: string;
11
13
  }
12
14
  | {
15
+ /** A file content part returned to the model after a tool call. */
13
16
  readonly type: "file";
17
+ /**
18
+ * File payload encoded as a provider-compatible string, commonly base64
19
+ * for binary data.
20
+ */
14
21
  readonly data: string;
22
+ /** MIME type for the file payload. */
15
23
  readonly mediaType: string;
24
+ /** Optional display filename for the file payload. */
16
25
  readonly filename?: string;
17
26
  };
18
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
+ */
19
53
  export type ToolModelOutputFunction<TArgs, TResult> = (options: {
54
+ /** Stable identifier for the tool call being converted. */
20
55
  toolCallId: string;
56
+ /** Arguments supplied by the model for this tool call. */
21
57
  input: TArgs;
58
+ /** Value returned by the tool's {@link ToolExecuteFunction}. */
22
59
  output: TResult;
23
60
  }) =>
24
61
  | readonly ToolModelContentPart[]
@@ -90,7 +127,8 @@ export interface ToolCallReader<
90
127
  response: ToolCallResponseReader<TResult>;
91
128
 
92
129
  /**
93
- * @deprecated Deprecated. Use `response.get().result` instead.
130
+ * @deprecated Use {@link ToolCallReader.response} and read
131
+ * `response.get().result` instead.
94
132
  */
95
133
  result: {
96
134
  get: () => Promise<TResult>;
@@ -98,11 +136,20 @@ export interface ToolCallReader<
98
136
  }
99
137
 
100
138
  export type ToolExecutionContext = {
139
+ /** Stable identifier for the tool call being executed. */
101
140
  toolCallId: string;
141
+ /** Signal that is aborted when the current run is cancelled. */
102
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
+ */
103
147
  human: (payload: unknown) => Promise<unknown>;
104
148
  };
105
149
 
150
+ /**
151
+ * Function called when assistant-ui executes a frontend tool.
152
+ */
106
153
  export type ToolExecuteFunction<TArgs, TResult> = (
107
154
  args: TArgs,
108
155
  context: ToolExecutionContext,
@@ -135,6 +182,7 @@ type BackendTool<
135
182
  TArgs extends Record<string, unknown> = Record<string, unknown>,
136
183
  TResult = unknown,
137
184
  > = ToolBase<TArgs, TResult> & {
185
+ /** Tool that is executed by the backend rather than in the browser. */
138
186
  type: "backend";
139
187
 
140
188
  description?: undefined;
@@ -149,13 +197,20 @@ type FrontendTool<
149
197
  TArgs extends Record<string, unknown> = Record<string, unknown>,
150
198
  TResult = unknown,
151
199
  > = ToolBase<TArgs, TResult> & {
200
+ /** Tool that is executed in the frontend runtime. */
152
201
  type: "frontend";
153
202
 
203
+ /** Natural-language description shown to the model when selecting tools. */
154
204
  description?: string | undefined;
205
+ /** Schema for the arguments the model must provide when calling the tool. */
155
206
  parameters: StandardSchemaV1<TArgs> | JSONSchema7;
207
+ /** Prevents the tool from being exposed to the model while true. */
156
208
  disabled?: boolean;
209
+ /** Executes the tool after the model provides valid arguments. */
157
210
  execute: ToolExecuteFunction<TArgs, TResult>;
211
+ /** Converts the execution result into model-visible output. */
158
212
  toModelOutput?: ToolModelOutputFunction<TArgs, TResult>;
213
+ /** Handles invalid tool arguments when schema validation fails. */
159
214
  experimental_onSchemaValidationError?: OnSchemaValidationErrorFunction<TResult>;
160
215
  };
161
216
 
@@ -163,16 +218,61 @@ type HumanTool<
163
218
  TArgs extends Record<string, unknown> = Record<string, unknown>,
164
219
  TResult = unknown,
165
220
  > = ToolBase<TArgs, TResult> & {
221
+ /** Tool that pauses the run until a user or UI supplies a result. */
166
222
  type: "human";
167
223
 
224
+ /** Natural-language description shown to the model when selecting tools. */
168
225
  description?: string | undefined;
226
+ /** Schema for the arguments the model must provide when requesting input. */
169
227
  parameters: StandardSchemaV1<TArgs> | JSONSchema7;
228
+ /** Prevents the tool from being exposed to the model while true. */
170
229
  disabled?: boolean;
171
230
  execute?: undefined;
172
231
  toModelOutput?: undefined;
173
232
  experimental_onSchemaValidationError?: undefined;
174
233
  };
175
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
+ */
176
276
  export type Tool<
177
277
  TArgs extends Record<string, unknown> = Record<string, unknown>,
178
278
  TResult = unknown,
@@ -183,7 +283,7 @@ export type Tool<
183
283
  | ToolWithoutType<TArgs, TResult>;
184
284
 
185
285
  /**
186
- * @deprecated Use `Tool` with an explicit `type` field instead.
286
+ * @deprecated Use {@link Tool} with an explicit `type` field instead.
187
287
  */
188
288
  export type ToolWithoutType<
189
289
  TArgs extends Record<string, unknown> = Record<string, unknown>,
@@ -214,10 +214,25 @@ export async function unstable_runPendingTools(
214
214
  }
215
215
 
216
216
  export type ToolResultStreamOptions = {
217
+ /** Called immediately before a frontend tool's `execute` function runs. */
217
218
  onExecutionStart?: (toolCallId: string, toolName: string) => void;
219
+ /** Called after frontend tool execution finishes or fails. */
218
220
  onExecutionEnd?: (toolCallId: string, toolName: string) => void;
219
221
  };
220
222
 
223
+ /**
224
+ * Transform stream that executes frontend tools and appends tool results.
225
+ *
226
+ * The transform watches streamed tool-call arguments, runs the matching
227
+ * frontend tool once its arguments are complete, and emits a result chunk for
228
+ * the tool call. Backend and human tools pass through according to their tool
229
+ * definition.
230
+ *
231
+ * @param tools Tool registry or function returning the current registry.
232
+ * @param abortSignal Signal, or signal getter, used for the current run.
233
+ * @param human Callback used to resolve human-tool requests from UI input.
234
+ * @param options Optional execution lifecycle callbacks.
235
+ */
221
236
  export function toolResultStream(
222
237
  tools:
223
238
  | Record<string, Tool>