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.
- package/dist/core/AssistantStream.d.ts +37 -0
- package/dist/core/AssistantStream.d.ts.map +1 -1
- package/dist/core/AssistantStream.js +22 -0
- package/dist/core/AssistantStream.js.map +1 -1
- package/dist/core/AssistantStreamChunk.d.ts +30 -0
- package/dist/core/AssistantStreamChunk.d.ts.map +1 -1
- package/dist/core/modules/assistant-stream.d.ts +68 -0
- package/dist/core/modules/assistant-stream.d.ts.map +1 -1
- package/dist/core/modules/assistant-stream.js +23 -0
- package/dist/core/modules/assistant-stream.js.map +1 -1
- package/dist/core/tool/ToolResponse.d.ts +41 -0
- package/dist/core/tool/ToolResponse.d.ts.map +1 -1
- package/dist/core/tool/ToolResponse.js +23 -0
- package/dist/core/tool/ToolResponse.js.map +1 -1
- package/dist/core/tool/tool-types.d.ts +102 -2
- package/dist/core/tool/tool-types.d.ts.map +1 -1
- package/dist/core/tool/toolResultStream.d.ts +15 -0
- package/dist/core/tool/toolResultStream.d.ts.map +1 -1
- package/dist/core/tool/toolResultStream.js +13 -0
- package/dist/core/tool/toolResultStream.js.map +1 -1
- package/package.json +4 -3
- package/src/core/AssistantStream.ts +37 -0
- package/src/core/AssistantStreamChunk.ts +30 -0
- package/src/core/modules/assistant-stream.ts +68 -0
- package/src/core/tool/ToolResponse.ts +45 -1
- package/src/core/tool/tool-types.ts +102 -2
- package/src/core/tool/toolResultStream.ts +15 -0
|
@@ -4,17 +4,54 @@ import type { AsyncIterableStream } from "../../utils.js";
|
|
|
4
4
|
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
5
5
|
import type { ToolResponse } from "./ToolResponse.js";
|
|
6
6
|
export type ToolModelContentPart = {
|
|
7
|
+
/** A text content part returned to the model after a tool call. */
|
|
7
8
|
readonly type: "text";
|
|
9
|
+
/** Text that should be included in the model-visible tool result. */
|
|
8
10
|
readonly text: string;
|
|
9
11
|
} | {
|
|
12
|
+
/** A file content part returned to the model after a tool call. */
|
|
10
13
|
readonly type: "file";
|
|
14
|
+
/**
|
|
15
|
+
* File payload encoded as a provider-compatible string, commonly base64
|
|
16
|
+
* for binary data.
|
|
17
|
+
*/
|
|
11
18
|
readonly data: string;
|
|
19
|
+
/** MIME type for the file payload. */
|
|
12
20
|
readonly mediaType: string;
|
|
21
|
+
/** Optional display filename for the file payload. */
|
|
13
22
|
readonly filename?: string;
|
|
14
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* Converts a tool's runtime result into content that is sent back to the
|
|
26
|
+
* model.
|
|
27
|
+
*
|
|
28
|
+
* Return this when the value shown in the UI or stored as the tool result
|
|
29
|
+
* should differ from the model-visible response. When omitted, the successful
|
|
30
|
+
* tool result is sent back to the model as-is. If a tool returns a
|
|
31
|
+
* `ToolResponse` with `modelContent`, that explicit content is used instead
|
|
32
|
+
* of calling this function.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const toModelOutput: ToolModelOutputFunction<
|
|
37
|
+
* { documentId: string },
|
|
38
|
+
* { summary: string; pdfBase64: string }
|
|
39
|
+
* > = ({ output }) => [
|
|
40
|
+
* { type: "text", text: output.summary },
|
|
41
|
+
* {
|
|
42
|
+
* type: "file",
|
|
43
|
+
* data: output.pdfBase64,
|
|
44
|
+
* mediaType: "application/pdf",
|
|
45
|
+
* },
|
|
46
|
+
* ];
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
15
49
|
export type ToolModelOutputFunction<TArgs, TResult> = (options: {
|
|
50
|
+
/** Stable identifier for the tool call being converted. */
|
|
16
51
|
toolCallId: string;
|
|
52
|
+
/** Arguments supplied by the model for this tool call. */
|
|
17
53
|
input: TArgs;
|
|
54
|
+
/** Value returned by the tool's {@link ToolExecuteFunction}. */
|
|
18
55
|
output: TResult;
|
|
19
56
|
}) => readonly ToolModelContentPart[] | Promise<readonly ToolModelContentPart[]>;
|
|
20
57
|
/**
|
|
@@ -62,17 +99,27 @@ export interface ToolCallReader<TArgs extends Record<string, unknown> = Record<s
|
|
|
62
99
|
args: ToolCallArgsReader<TArgs>;
|
|
63
100
|
response: ToolCallResponseReader<TResult>;
|
|
64
101
|
/**
|
|
65
|
-
* @deprecated
|
|
102
|
+
* @deprecated Use {@link ToolCallReader.response} and read
|
|
103
|
+
* `response.get().result` instead.
|
|
66
104
|
*/
|
|
67
105
|
result: {
|
|
68
106
|
get: () => Promise<TResult>;
|
|
69
107
|
};
|
|
70
108
|
}
|
|
71
109
|
export type ToolExecutionContext = {
|
|
110
|
+
/** Stable identifier for the tool call being executed. */
|
|
72
111
|
toolCallId: string;
|
|
112
|
+
/** Signal that is aborted when the current run is cancelled. */
|
|
73
113
|
abortSignal: AbortSignal;
|
|
114
|
+
/**
|
|
115
|
+
* From inside a frontend tool's execute function, request human input from
|
|
116
|
+
* the UI. Resolves with the payload the UI supplies.
|
|
117
|
+
*/
|
|
74
118
|
human: (payload: unknown) => Promise<unknown>;
|
|
75
119
|
};
|
|
120
|
+
/**
|
|
121
|
+
* Function called when assistant-ui executes a frontend tool.
|
|
122
|
+
*/
|
|
76
123
|
export type ToolExecuteFunction<TArgs, TResult> = (args: TArgs, context: ToolExecutionContext) => TResult | Promise<TResult>;
|
|
77
124
|
export type ToolStreamCallFunction<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = (reader: ToolCallReader<TArgs, TResult>, context: ToolExecutionContext) => void;
|
|
78
125
|
type OnSchemaValidationErrorFunction<TResult> = ToolExecuteFunction<unknown, TResult>;
|
|
@@ -83,6 +130,7 @@ type ToolBase<TArgs extends Record<string, unknown> = Record<string, unknown>, T
|
|
|
83
130
|
streamCall?: ToolStreamCallFunction<TArgs, TResult>;
|
|
84
131
|
};
|
|
85
132
|
type BackendTool<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = ToolBase<TArgs, TResult> & {
|
|
133
|
+
/** Tool that is executed by the backend rather than in the browser. */
|
|
86
134
|
type: "backend";
|
|
87
135
|
description?: undefined;
|
|
88
136
|
parameters?: undefined;
|
|
@@ -92,26 +140,78 @@ type BackendTool<TArgs extends Record<string, unknown> = Record<string, unknown>
|
|
|
92
140
|
experimental_onSchemaValidationError?: undefined;
|
|
93
141
|
};
|
|
94
142
|
type FrontendTool<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = ToolBase<TArgs, TResult> & {
|
|
143
|
+
/** Tool that is executed in the frontend runtime. */
|
|
95
144
|
type: "frontend";
|
|
145
|
+
/** Natural-language description shown to the model when selecting tools. */
|
|
96
146
|
description?: string | undefined;
|
|
147
|
+
/** Schema for the arguments the model must provide when calling the tool. */
|
|
97
148
|
parameters: StandardSchemaV1<TArgs> | JSONSchema7;
|
|
149
|
+
/** Prevents the tool from being exposed to the model while true. */
|
|
98
150
|
disabled?: boolean;
|
|
151
|
+
/** Executes the tool after the model provides valid arguments. */
|
|
99
152
|
execute: ToolExecuteFunction<TArgs, TResult>;
|
|
153
|
+
/** Converts the execution result into model-visible output. */
|
|
100
154
|
toModelOutput?: ToolModelOutputFunction<TArgs, TResult>;
|
|
155
|
+
/** Handles invalid tool arguments when schema validation fails. */
|
|
101
156
|
experimental_onSchemaValidationError?: OnSchemaValidationErrorFunction<TResult>;
|
|
102
157
|
};
|
|
103
158
|
type HumanTool<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = ToolBase<TArgs, TResult> & {
|
|
159
|
+
/** Tool that pauses the run until a user or UI supplies a result. */
|
|
104
160
|
type: "human";
|
|
161
|
+
/** Natural-language description shown to the model when selecting tools. */
|
|
105
162
|
description?: string | undefined;
|
|
163
|
+
/** Schema for the arguments the model must provide when requesting input. */
|
|
106
164
|
parameters: StandardSchemaV1<TArgs> | JSONSchema7;
|
|
165
|
+
/** Prevents the tool from being exposed to the model while true. */
|
|
107
166
|
disabled?: boolean;
|
|
108
167
|
execute?: undefined;
|
|
109
168
|
toModelOutput?: undefined;
|
|
110
169
|
experimental_onSchemaValidationError?: undefined;
|
|
111
170
|
};
|
|
171
|
+
/**
|
|
172
|
+
* Definition for a tool that can be exposed to the assistant model.
|
|
173
|
+
*
|
|
174
|
+
* Use `type: "frontend"` for tools executed in the browser, `type: "backend"`
|
|
175
|
+
* for tools handled by your server, and `type: "human"` for flows that pause
|
|
176
|
+
* until the UI supplies a result.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const frontendTool: Tool<{ city: string }, string> = {
|
|
181
|
+
* type: "frontend",
|
|
182
|
+
* description: "Get the weather for a city.",
|
|
183
|
+
* parameters: {
|
|
184
|
+
* type: "object",
|
|
185
|
+
* properties: { city: { type: "string" } },
|
|
186
|
+
* required: ["city"],
|
|
187
|
+
* },
|
|
188
|
+
* execute: async ({ city }) => `Sunny in ${city}`,
|
|
189
|
+
* };
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* const backendTool: Tool = {
|
|
195
|
+
* type: "backend",
|
|
196
|
+
* };
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const humanTool: Tool<{ question: string }, string> = {
|
|
202
|
+
* type: "human",
|
|
203
|
+
* description: "Ask the user a follow-up question.",
|
|
204
|
+
* parameters: {
|
|
205
|
+
* type: "object",
|
|
206
|
+
* properties: { question: { type: "string" } },
|
|
207
|
+
* required: ["question"],
|
|
208
|
+
* },
|
|
209
|
+
* };
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
112
212
|
export type Tool<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = FrontendTool<TArgs, TResult> | BackendTool<TArgs, TResult> | HumanTool<TArgs, TResult> | ToolWithoutType<TArgs, TResult>;
|
|
113
213
|
/**
|
|
114
|
-
* @deprecated Use
|
|
214
|
+
* @deprecated Use {@link Tool} with an explicit `type` field instead.
|
|
115
215
|
*/
|
|
116
216
|
export type ToolWithoutType<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = (Omit<FrontendTool<TArgs, TResult>, "type"> | Omit<BackendTool<TArgs, TResult>, "type"> | Omit<HumanTool<TArgs, TResult>, "type">) & {
|
|
117
217
|
type?: undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-types.d.ts","sourceRoot":"","sources":["../../../src/core/tool/tool-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,6BAA0B;AAC3E,OAAO,KAAK,EAAE,mBAAmB,EAAE,uBAAoB;AACvD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,0BAAuB;AAEnD,MAAM,MAAM,oBAAoB,GAC5B;IACE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB,GACD;IACE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEN,MAAM,MAAM,uBAAuB,CAAC,KAAK,EAAE,OAAO,IAAI,CAAC,OAAO,EAAE;IAC9D,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,KAAK,CAAC;IACb,MAAM,EAAE,OAAO,CAAC;CACjB,KACG,SAAS,oBAAoB,EAAE,GAC/B,OAAO,CAAC,SAAS,oBAAoB,EAAE,CAAC,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAA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AC,GAAG,WAAW,CAAC;IAClD,oEAAoE;IACpE,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,SAAS,CAAC;IACpB,aAAa,CAAC,EAAE,SAAS,CAAC;IAC1B,oCAAoC,CAAC,EAAE,SAAS,CAAC;CAClD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,MAAM,IAAI,CACd,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/D,OAAO,GAAG,OAAO,IAEf,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,GAC5B,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,GAC3B,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,GACzB,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,MAAM,eAAe,CACzB,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/D,OAAO,GAAG,OAAO,IACf,CACA,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,GAC1C,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,GACzC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,CAC1C,GAAG;IAAE,IAAI,CAAC,EAAE,SAAS,CAAA;CAAE,CAAC"}
|
|
@@ -3,8 +3,23 @@ import { ToolExecutionStream } from "./ToolExecutionStream.js";
|
|
|
3
3
|
import type { AssistantMessage } from "../utils/types.js";
|
|
4
4
|
export declare function unstable_runPendingTools(message: AssistantMessage, tools: Record<string, Tool> | undefined, abortSignal: AbortSignal, human: (toolCallId: string, payload: unknown) => Promise<unknown>): Promise<AssistantMessage>;
|
|
5
5
|
export type ToolResultStreamOptions = {
|
|
6
|
+
/** Called immediately before a frontend tool's `execute` function runs. */
|
|
6
7
|
onExecutionStart?: (toolCallId: string, toolName: string) => void;
|
|
8
|
+
/** Called after frontend tool execution finishes or fails. */
|
|
7
9
|
onExecutionEnd?: (toolCallId: string, toolName: string) => void;
|
|
8
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Transform stream that executes frontend tools and appends tool results.
|
|
13
|
+
*
|
|
14
|
+
* The transform watches streamed tool-call arguments, runs the matching
|
|
15
|
+
* frontend tool once its arguments are complete, and emits a result chunk for
|
|
16
|
+
* the tool call. Backend and human tools pass through according to their tool
|
|
17
|
+
* definition.
|
|
18
|
+
*
|
|
19
|
+
* @param tools Tool registry or function returning the current registry.
|
|
20
|
+
* @param abortSignal Signal, or signal getter, used for the current run.
|
|
21
|
+
* @param human Callback used to resolve human-tool requests from UI input.
|
|
22
|
+
* @param options Optional execution lifecycle callbacks.
|
|
23
|
+
*/
|
|
9
24
|
export declare function toolResultStream(tools: Record<string, Tool> | (() => Record<string, Tool> | undefined) | undefined, abortSignal: AbortSignal | (() => AbortSignal), human: (toolCallId: string, payload: unknown) => Promise<unknown>, options?: ToolResultStreamOptions): ToolExecutionStream;
|
|
10
25
|
//# sourceMappingURL=toolResultStream.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toolResultStream.d.ts","sourceRoot":"","sources":["../../../src/core/tool/toolResultStream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAuC,wBAAqB;AAG9E,OAAO,EAAE,mBAAmB,EAAE,iCAA8B;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,0BAAuB;AAyIvD,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,gBAAgB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,EACvC,WAAW,EAAE,WAAW,EACxB,KAA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,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IAClE,8DAA8D;IAC9D,cAAc,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;CACjE,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EACD,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GACpB,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC,GACxC,SAAS,EACb,WAAW,EAAE,WAAW,GAAG,CAAC,MAAM,WAAW,CAAC,EAC9C,KAAK,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,EACjE,OAAO,CAAC,EAAE,uBAAuB,uBAalC"}
|
|
@@ -143,6 +143,19 @@ export async function unstable_runPendingTools(message, tools, abortSignal, huma
|
|
|
143
143
|
content: updatedParts,
|
|
144
144
|
};
|
|
145
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Transform stream that executes frontend tools and appends tool results.
|
|
148
|
+
*
|
|
149
|
+
* The transform watches streamed tool-call arguments, runs the matching
|
|
150
|
+
* frontend tool once its arguments are complete, and emits a result chunk for
|
|
151
|
+
* the tool call. Backend and human tools pass through according to their tool
|
|
152
|
+
* definition.
|
|
153
|
+
*
|
|
154
|
+
* @param tools Tool registry or function returning the current registry.
|
|
155
|
+
* @param abortSignal Signal, or signal getter, used for the current run.
|
|
156
|
+
* @param human Callback used to resolve human-tool requests from UI input.
|
|
157
|
+
* @param options Optional execution lifecycle callbacks.
|
|
158
|
+
*/
|
|
146
159
|
export function toolResultStream(tools, abortSignal, human, options) {
|
|
147
160
|
const toolsFn = typeof tools === "function" ? tools : () => tools;
|
|
148
161
|
const abortSignalFn = typeof abortSignal === "function" ? abortSignal : () => abortSignal;
|
|
@@ -1 +1 @@
|
|
|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,YAAY;KACtB,CAAC;AACJ,CAAC;AASD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAGa,EACb,WAA8C,EAC9C,KAAiE,EACjE,OAAiC;IAEjC,MAAM,OAAO,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC;IAClE,MAAM,aAAa,GACjB,OAAO,WAAW,KAAK,UAAU,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC;IACtE,OAAO,IAAI,mBAAmB,CAAC;QAC7B,OAAO,EAAE,CAAC,QAAQ,EAAE,EAAE,CACpB,eAAe,CAAC,OAAO,EAAE,EAAE,aAAa,EAAE,EAAE,QAAQ,EAAE,KAAK,CAAC;QAC9D,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,EAAE,EAAE,CACrC,qBAAqB,CAAC,OAAO,EAAE,EAAE,aAAa,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC;QAC3E,gBAAgB,EAAE,OAAO,EAAE,gBAAgB;QAC3C,cAAc,EAAE,OAAO,EAAE,cAAc;KACxC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assistant-stream",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.15",
|
|
4
4
|
"description": "Streaming utilities for AI assistants",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -67,10 +67,11 @@
|
|
|
67
67
|
"ioredis": "^5.10.1",
|
|
68
68
|
"redis": "^5.12.1",
|
|
69
69
|
"vitest": "^4.1.5",
|
|
70
|
-
"@assistant-ui/x-buildutils": "0.0.
|
|
70
|
+
"@assistant-ui/x-buildutils": "0.0.8"
|
|
71
71
|
},
|
|
72
72
|
"publishConfig": {
|
|
73
|
-
"access": "public"
|
|
73
|
+
"access": "public",
|
|
74
|
+
"provenance": true
|
|
74
75
|
},
|
|
75
76
|
"homepage": "https://www.assistant-ui.com/",
|
|
76
77
|
"repository": {
|
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import type { AssistantStreamChunk } from "./AssistantStreamChunk";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Stream of assistant-ui protocol chunks.
|
|
5
|
+
*
|
|
6
|
+
* `AssistantStream` is the normalized internal stream format used by
|
|
7
|
+
* encoders, decoders, accumulators, and tool execution transforms. Use an
|
|
8
|
+
* encoder such as `DataStreamEncoder` when returning it from an HTTP route,
|
|
9
|
+
* and the matching decoder when reading it back from a response.
|
|
10
|
+
*/
|
|
3
11
|
export type AssistantStream = ReadableStream<AssistantStreamChunk>;
|
|
4
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Encoder that converts an {@link AssistantStream} into bytes suitable for an
|
|
15
|
+
* HTTP response body.
|
|
16
|
+
*
|
|
17
|
+
* Encoders may expose response headers, such as content type or protocol
|
|
18
|
+
* markers, through `headers`.
|
|
19
|
+
*/
|
|
5
20
|
export type AssistantStreamEncoder = ReadableWritablePair<
|
|
6
21
|
Uint8Array<ArrayBuffer>,
|
|
7
22
|
AssistantStreamChunk
|
|
@@ -10,12 +25,26 @@ export type AssistantStreamEncoder = ReadableWritablePair<
|
|
|
10
25
|
};
|
|
11
26
|
|
|
12
27
|
export const AssistantStream = {
|
|
28
|
+
/**
|
|
29
|
+
* Converts an {@link AssistantStream} into a `Response` using the supplied
|
|
30
|
+
* encoder.
|
|
31
|
+
*
|
|
32
|
+
* The encoder's `headers` are copied onto the response. Pair this with the
|
|
33
|
+
* decoder for the same wire format when consuming the response.
|
|
34
|
+
*/
|
|
13
35
|
toResponse(stream: AssistantStream, transformer: AssistantStreamEncoder) {
|
|
14
36
|
return new Response(AssistantStream.toByteStream(stream, transformer), {
|
|
15
37
|
headers: transformer.headers ?? {},
|
|
16
38
|
});
|
|
17
39
|
},
|
|
18
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Reads an assistant stream from a `Response` body using the supplied
|
|
43
|
+
* decoder.
|
|
44
|
+
*
|
|
45
|
+
* The response body must be present and encoded with the matching assistant
|
|
46
|
+
* stream wire format.
|
|
47
|
+
*/
|
|
19
48
|
fromResponse(
|
|
20
49
|
response: Response,
|
|
21
50
|
transformer: ReadableWritablePair<
|
|
@@ -26,6 +55,10 @@ export const AssistantStream = {
|
|
|
26
55
|
return AssistantStream.fromByteStream(response.body!, transformer);
|
|
27
56
|
},
|
|
28
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Pipes an {@link AssistantStream} through an encoder and returns the
|
|
60
|
+
* resulting byte stream.
|
|
61
|
+
*/
|
|
29
62
|
toByteStream(
|
|
30
63
|
stream: AssistantStream,
|
|
31
64
|
transformer: ReadableWritablePair<
|
|
@@ -36,6 +69,10 @@ export const AssistantStream = {
|
|
|
36
69
|
return stream.pipeThrough(transformer);
|
|
37
70
|
},
|
|
38
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Pipes a byte stream through a decoder and returns normalized
|
|
74
|
+
* {@link AssistantStreamChunk} values.
|
|
75
|
+
*/
|
|
39
76
|
fromByteStream(
|
|
40
77
|
readable: ReadableStream<Uint8Array<ArrayBuffer>>,
|
|
41
78
|
transformer: ReadableWritablePair<
|
|
@@ -2,6 +2,12 @@ import type { ReadonlyJSONValue } from "../utils/json/json-value";
|
|
|
2
2
|
import type { ObjectStreamOperation } from "./object/types";
|
|
3
3
|
import type { ToolModelContentPart } from "./tool/tool-types";
|
|
4
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
|
+
*/
|
|
5
11
|
export type PartInit =
|
|
6
12
|
| {
|
|
7
13
|
readonly type: "text" | "reasoning";
|
|
@@ -34,34 +40,49 @@ export type PartInit =
|
|
|
34
40
|
readonly parentId?: string;
|
|
35
41
|
};
|
|
36
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
|
+
*/
|
|
37
50
|
export type AssistantStreamChunk = { readonly path: readonly number[] } & (
|
|
38
51
|
| {
|
|
52
|
+
/** Opens a new content part at `path`. */
|
|
39
53
|
readonly type: "part-start";
|
|
40
54
|
readonly part: PartInit;
|
|
41
55
|
}
|
|
42
56
|
| {
|
|
57
|
+
/** Closes the current part at `path`. */
|
|
43
58
|
readonly type: "part-finish";
|
|
44
59
|
}
|
|
45
60
|
| {
|
|
61
|
+
/** Marks streamed tool-call argument text as complete. */
|
|
46
62
|
readonly type: "tool-call-args-text-finish";
|
|
47
63
|
}
|
|
48
64
|
| {
|
|
65
|
+
/** Appends text to a text, reasoning, or tool-call argument part. */
|
|
49
66
|
readonly type: "text-delta";
|
|
50
67
|
readonly textDelta: string;
|
|
51
68
|
}
|
|
52
69
|
| {
|
|
70
|
+
/** Appends provider or application annotations to the current message. */
|
|
53
71
|
readonly type: "annotations";
|
|
54
72
|
readonly annotations: ReadonlyJSONValue[];
|
|
55
73
|
}
|
|
56
74
|
| {
|
|
75
|
+
/** Emits application data chunks associated with the current message. */
|
|
57
76
|
readonly type: "data";
|
|
58
77
|
readonly data: ReadonlyJSONValue[];
|
|
59
78
|
}
|
|
60
79
|
| {
|
|
80
|
+
/** Starts a model generation step. */
|
|
61
81
|
readonly type: "step-start";
|
|
62
82
|
readonly messageId: string;
|
|
63
83
|
}
|
|
64
84
|
| {
|
|
85
|
+
/** Finishes a model generation step and reports usage for that step. */
|
|
65
86
|
readonly type: "step-finish";
|
|
66
87
|
readonly finishReason:
|
|
67
88
|
| "stop"
|
|
@@ -78,6 +99,7 @@ export type AssistantStreamChunk = { readonly path: readonly number[] } & (
|
|
|
78
99
|
readonly isContinued: boolean;
|
|
79
100
|
}
|
|
80
101
|
| {
|
|
102
|
+
/** Finishes the assistant message and reports final usage. */
|
|
81
103
|
readonly type: "message-finish";
|
|
82
104
|
readonly finishReason:
|
|
83
105
|
| "stop"
|
|
@@ -93,6 +115,12 @@ export type AssistantStreamChunk = { readonly path: readonly number[] } & (
|
|
|
93
115
|
};
|
|
94
116
|
}
|
|
95
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
|
+
*/
|
|
96
124
|
readonly type: "result";
|
|
97
125
|
readonly artifact?: ReadonlyJSONValue;
|
|
98
126
|
readonly result: ReadonlyJSONValue;
|
|
@@ -101,10 +129,12 @@ export type AssistantStreamChunk = { readonly path: readonly number[] } & (
|
|
|
101
129
|
readonly messages?: ReadonlyJSONValue;
|
|
102
130
|
}
|
|
103
131
|
| {
|
|
132
|
+
/** Emits a stream-level error message. */
|
|
104
133
|
readonly type: "error";
|
|
105
134
|
readonly error: string;
|
|
106
135
|
}
|
|
107
136
|
| {
|
|
137
|
+
/** Applies object-stream operations to state carried by this stream. */
|
|
108
138
|
readonly type: "update-state";
|
|
109
139
|
readonly operations: ObjectStreamOperation[];
|
|
110
140
|
}
|
|
@@ -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
|
) {
|
|
@@ -1,16 +1,53 @@
|
|
|
1
1
|
import type { ReadonlyJSONValue } from "../../utils/json/json-value";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ToolModelContentPart,
|
|
4
|
+
ToolModelOutputFunction,
|
|
5
|
+
} from "./tool-types";
|
|
3
6
|
|
|
4
7
|
const TOOL_RESPONSE_SYMBOL = Symbol.for("aui.tool-response");
|
|
5
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Shape accepted anywhere a {@link ToolResponse} can be returned.
|
|
11
|
+
*/
|
|
6
12
|
export type ToolResponseLike<TResult> = {
|
|
13
|
+
/** UI-visible tool result value. */
|
|
7
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
|
+
*/
|
|
8
21
|
artifact?: ReadonlyJSONValue | undefined;
|
|
22
|
+
/** Marks the tool result as an error result. */
|
|
9
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
|
+
*/
|
|
10
30
|
modelContent?: readonly ToolModelContentPart[] | undefined;
|
|
31
|
+
/** Optional provider-specific message payload associated with the tool result. */
|
|
11
32
|
messages?: ReadonlyJSONValue | undefined;
|
|
12
33
|
};
|
|
13
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
|
+
*/
|
|
14
51
|
export class ToolResponse<TResult> {
|
|
15
52
|
get [TOOL_RESPONSE_SYMBOL]() {
|
|
16
53
|
return true;
|
|
@@ -44,6 +81,13 @@ export class ToolResponse<TResult> {
|
|
|
44
81
|
);
|
|
45
82
|
}
|
|
46
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
|
+
*/
|
|
47
91
|
static toResponse(result: any | ToolResponse<any>): ToolResponse<any> {
|
|
48
92
|
if (result instanceof ToolResponse) {
|
|
49
93
|
return result;
|