assistant-stream 0.2.31 → 0.2.32

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.
@@ -54,9 +54,10 @@ export interface ToolCallReader<TArgs extends Record<string, unknown> = Record<s
54
54
  get: () => Promise<TResult>;
55
55
  };
56
56
  }
57
- type ToolExecutionContext = {
57
+ export type ToolExecutionContext = {
58
58
  toolCallId: string;
59
59
  abortSignal: AbortSignal;
60
+ interrupt: (payload: unknown) => Promise<unknown>;
60
61
  };
61
62
  export type ToolExecuteFunction<TArgs, TResult> = (args: TArgs, context: ToolExecutionContext) => TResult | Promise<TResult>;
62
63
  export type ToolStreamCallFunction<TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = (reader: ToolCallReader<TArgs, TResult>, context: ToolExecutionContext) => void;
@@ -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,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACvE;;;;;OAKG;IACH,GAAG,CAAC,KAAK,SAAS,QAAQ,CAAC,KAAK,CAAC,EAC/B,GAAG,SAAS,EAAE,KAAK,GAClB,OAAO,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAErC;;;;;OAKG;IACH,YAAY,CAAC,KAAK,SAAS,QAAQ,CAAC,KAA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CAAC,GAAG;IAC7B,IAAI,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAE3B,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,UAAU,EAAE,gBAAgB,CAAC,KAAK,CAAC,GAAG,WAAW,CAAC;IAClD,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,SAAS,CAAC;IACpB,oCAAoC,CAAC,EAAE,SAAS,CAAC;CAClD,CAAC;AAEF,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,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import { Tool } from "./tool-types";
2
2
  import { ToolExecutionStream } from "./ToolExecutionStream";
3
3
  import { AssistantMessage } from "../utils/types";
4
- export declare function unstable_runPendingTools(message: AssistantMessage, tools: Record<string, Tool> | undefined, abortSignal: AbortSignal): Promise<AssistantMessage>;
5
- export declare function toolResultStream(tools: Record<string, Tool> | (() => Record<string, Tool> | undefined) | undefined, abortSignal: AbortSignal | (() => AbortSignal)): ToolExecutionStream;
4
+ export declare function unstable_runPendingTools(message: AssistantMessage, tools: Record<string, Tool> | undefined, abortSignal: AbortSignal, interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>): Promise<AssistantMessage>;
5
+ export declare function toolResultStream(tools: Record<string, Tool> | (() => Record<string, Tool> | undefined) | undefined, abortSignal: AbortSignal | (() => AbortSignal), interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>): ToolExecutionStream;
6
6
  //# 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,EAAE,IAAI,EAAuC,MAAM,cAAc,CAAC;AAGzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAuElD,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,gBAAgB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,EACvC,WAAW,EAAE,WAAW,6BA+BzB;AAED,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,uBAW/C"}
1
+ {"version":3,"file":"toolResultStream.d.ts","sourceRoot":"","sources":["../../../src/core/tool/toolResultStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAuC,MAAM,cAAc,CAAC;AAGzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AA2ElD,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,gBAAgB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,EACvC,WAAW,EAAE,WAAW,EACxB,SAAS,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,6BAuCtE;AAED,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,SAAS,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,uBAiBtE"}
@@ -4,7 +4,7 @@ import { ToolExecutionStream } from "./ToolExecutionStream.js";
4
4
  var isStandardSchemaV1 = (schema) => {
5
5
  return typeof schema === "object" && schema !== null && "~standard" in schema && schema["~standard"].version === 1;
6
6
  };
7
- function getToolResponse(tools, abortSignal, toolCall) {
7
+ function getToolResponse(tools, abortSignal, toolCall, interrupt) {
8
8
  const tool = tools?.[toolCall.toolName];
9
9
  if (!tool || !tool.execute) return void 0;
10
10
  const getResult = async (toolExecute) => {
@@ -22,22 +22,31 @@ function getToolResponse(tools, abortSignal, toolCall) {
22
22
  }
23
23
  const result = await executeFn(toolCall.args, {
24
24
  toolCallId: toolCall.toolCallId,
25
- abortSignal
25
+ abortSignal,
26
+ interrupt: (payload) => interrupt(toolCall.toolCallId, payload)
26
27
  });
27
28
  return ToolResponse.toResponse(result);
28
29
  };
29
30
  return getResult(tool.execute);
30
31
  }
31
- function getToolStreamResponse(tools, abortSignal, reader, context) {
32
+ function getToolStreamResponse(tools, abortSignal, reader, context, interrupt) {
32
33
  tools?.[context.toolName]?.streamCall?.(reader, {
33
34
  toolCallId: context.toolCallId,
34
- abortSignal
35
+ abortSignal,
36
+ interrupt: (payload) => interrupt(context.toolCallId, payload)
35
37
  });
36
38
  }
37
- async function unstable_runPendingTools(message, tools, abortSignal) {
39
+ async function unstable_runPendingTools(message, tools, abortSignal, interrupt) {
38
40
  for (const part of message.parts) {
39
41
  if (part.type === "tool-call") {
40
- const promiseOrUndefined = getToolResponse(tools, abortSignal, part);
42
+ const promiseOrUndefined = getToolResponse(
43
+ tools,
44
+ abortSignal,
45
+ part,
46
+ interrupt ?? (async () => {
47
+ throw new Error("Tool interrupt is not supported in this context");
48
+ })
49
+ );
41
50
  if (promiseOrUndefined) {
42
51
  const result = await promiseOrUndefined;
43
52
  const updatedParts = message.parts.map((p) => {
@@ -62,12 +71,18 @@ async function unstable_runPendingTools(message, tools, abortSignal) {
62
71
  }
63
72
  return message;
64
73
  }
65
- function toolResultStream(tools, abortSignal) {
74
+ function toolResultStream(tools, abortSignal, interrupt) {
66
75
  const toolsFn = typeof tools === "function" ? tools : () => tools;
67
76
  const abortSignalFn = typeof abortSignal === "function" ? abortSignal : () => abortSignal;
68
77
  return new ToolExecutionStream({
69
- execute: (toolCall) => getToolResponse(toolsFn(), abortSignalFn(), toolCall),
70
- streamCall: ({ reader, ...context }) => getToolStreamResponse(toolsFn(), abortSignalFn(), reader, context)
78
+ execute: (toolCall) => getToolResponse(toolsFn(), abortSignalFn(), toolCall, interrupt),
79
+ streamCall: ({ reader, ...context }) => getToolStreamResponse(
80
+ toolsFn(),
81
+ abortSignalFn(),
82
+ reader,
83
+ context,
84
+ interrupt
85
+ )
71
86
  });
72
87
  }
73
88
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/core/tool/toolResultStream.ts"],"sourcesContent":["import { Tool, ToolCallReader, ToolExecuteFunction } from \"./tool-types\";\nimport { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { ToolResponse } from \"./ToolResponse\";\nimport { ToolExecutionStream } from \"./ToolExecutionStream\";\nimport { AssistantMessage } from \"../utils/types\";\nimport { ReadonlyJSONObject, ReadonlyJSONValue } from \"../../utils\";\n\nconst isStandardSchemaV1 = (\n schema: unknown,\n): schema is StandardSchemaV1<unknown> => {\n return (\n typeof schema === \"object\" &&\n schema !== null &&\n \"~standard\" in schema &&\n (schema as StandardSchemaV1<unknown>)[\"~standard\"].version === 1\n );\n};\n\nfunction getToolResponse(\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n toolCall: {\n toolCallId: string;\n toolName: string;\n args: ReadonlyJSONObject;\n },\n) {\n const tool = tools?.[toolCall.toolName];\n if (!tool || !tool.execute) return undefined;\n\n const getResult = async (\n toolExecute: ToolExecuteFunction<ReadonlyJSONObject, unknown>,\n ): Promise<ToolResponse<ReadonlyJSONValue>> => {\n let executeFn = toolExecute;\n\n if (isStandardSchemaV1(tool.parameters)) {\n let result = tool.parameters[\"~standard\"].validate(toolCall.args);\n if (result instanceof Promise) result = await result;\n\n if (result.issues) {\n executeFn =\n tool.experimental_onSchemaValidationError ??\n (() => {\n throw new Error(\n `Function parameter validation failed. ${JSON.stringify(result.issues)}`,\n );\n });\n }\n }\n\n const result = (await executeFn(toolCall.args, {\n toolCallId: toolCall.toolCallId,\n abortSignal,\n })) as unknown as ReadonlyJSONValue;\n return ToolResponse.toResponse(result);\n };\n\n return getResult(tool.execute);\n}\n\nfunction getToolStreamResponse(\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n reader: ToolCallReader<any, ReadonlyJSONValue>,\n context: {\n toolCallId: string;\n toolName: string;\n },\n) {\n tools?.[context.toolName]?.streamCall?.(reader, {\n toolCallId: context.toolCallId,\n abortSignal,\n });\n}\n\nexport async function unstable_runPendingTools(\n message: AssistantMessage,\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n) {\n // TODO parallel tool calling\n for (const part of message.parts) {\n if (part.type === \"tool-call\") {\n const promiseOrUndefined = getToolResponse(tools, abortSignal, part);\n if (promiseOrUndefined) {\n const result = await promiseOrUndefined;\n const updatedParts = message.parts.map((p) => {\n if (p.type === \"tool-call\" && p.toolCallId === part.toolCallId) {\n return {\n ...p,\n state: \"result\" as const,\n ...(result.artifact !== undefined\n ? { artifact: result.artifact }\n : {}),\n result: result.result as ReadonlyJSONValue,\n isError: result.isError,\n };\n }\n return p;\n });\n message = {\n ...message,\n parts: updatedParts,\n content: updatedParts,\n };\n }\n }\n }\n return message;\n}\n\nexport function toolResultStream(\n tools:\n | Record<string, Tool>\n | (() => Record<string, Tool> | undefined)\n | undefined,\n abortSignal: AbortSignal | (() => AbortSignal),\n) {\n const toolsFn = typeof tools === \"function\" ? tools : () => tools;\n const abortSignalFn =\n typeof abortSignal === \"function\" ? abortSignal : () => abortSignal;\n return new ToolExecutionStream({\n execute: (toolCall) =>\n getToolResponse(toolsFn(), abortSignalFn(), toolCall),\n streamCall: ({ reader, ...context }) =>\n getToolStreamResponse(toolsFn(), abortSignalFn(), reader, context),\n });\n}\n"],"mappings":";AAEA,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AAIpC,IAAM,qBAAqB,CACzB,WACwC;AACxC,SACE,OAAO,WAAW,YAClB,WAAW,QACX,eAAe,UACd,OAAqC,WAAW,EAAE,YAAY;AAEnE;AAEA,SAAS,gBACP,OACA,aACA,UAKA;AACA,QAAM,OAAO,QAAQ,SAAS,QAAQ;AACtC,MAAI,CAAC,QAAQ,CAAC,KAAK,QAAS,QAAO;AAEnC,QAAM,YAAY,OAChB,gBAC6C;AAC7C,QAAI,YAAY;AAEhB,QAAI,mBAAmB,KAAK,UAAU,GAAG;AACvC,UAAIA,UAAS,KAAK,WAAW,WAAW,EAAE,SAAS,SAAS,IAAI;AAChE,UAAIA,mBAAkB,QAAS,CAAAA,UAAS,MAAMA;AAE9C,UAAIA,QAAO,QAAQ;AACjB,oBACE,KAAK,yCACJ,MAAM;AACL,gBAAM,IAAI;AAAA,YACR,yCAAyC,KAAK,UAAUA,QAAO,MAAM,CAAC;AAAA,UACxE;AAAA,QACF;AAAA,MACJ;AAAA,IACF;AAEA,UAAM,SAAU,MAAM,UAAU,SAAS,MAAM;AAAA,MAC7C,YAAY,SAAS;AAAA,MACrB;AAAA,IACF,CAAC;AACD,WAAO,aAAa,WAAW,MAAM;AAAA,EACvC;AAEA,SAAO,UAAU,KAAK,OAAO;AAC/B;AAEA,SAAS,sBACP,OACA,aACA,QACA,SAIA;AACA,UAAQ,QAAQ,QAAQ,GAAG,aAAa,QAAQ;AAAA,IAC9C,YAAY,QAAQ;AAAA,IACpB;AAAA,EACF,CAAC;AACH;AAEA,eAAsB,yBACpB,SACA,OACA,aACA;AAEA,aAAW,QAAQ,QAAQ,OAAO;AAChC,QAAI,KAAK,SAAS,aAAa;AAC7B,YAAM,qBAAqB,gBAAgB,OAAO,aAAa,IAAI;AACnE,UAAI,oBAAoB;AACtB,cAAM,SAAS,MAAM;AACrB,cAAM,eAAe,QAAQ,MAAM,IAAI,CAAC,MAAM;AAC5C,cAAI,EAAE,SAAS,eAAe,EAAE,eAAe,KAAK,YAAY;AAC9D,mBAAO;AAAA,cACL,GAAG;AAAA,cACH,OAAO;AAAA,cACP,GAAI,OAAO,aAAa,SACpB,EAAE,UAAU,OAAO,SAAS,IAC5B,CAAC;AAAA,cACL,QAAQ,OAAO;AAAA,cACf,SAAS,OAAO;AAAA,YAClB;AAAA,UACF;AACA,iBAAO;AAAA,QACT,CAAC;AACD,kBAAU;AAAA,UACR,GAAG;AAAA,UACH,OAAO;AAAA,UACP,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,iBACd,OAIA,aACA;AACA,QAAM,UAAU,OAAO,UAAU,aAAa,QAAQ,MAAM;AAC5D,QAAM,gBACJ,OAAO,gBAAgB,aAAa,cAAc,MAAM;AAC1D,SAAO,IAAI,oBAAoB;AAAA,IAC7B,SAAS,CAAC,aACR,gBAAgB,QAAQ,GAAG,cAAc,GAAG,QAAQ;AAAA,IACtD,YAAY,CAAC,EAAE,QAAQ,GAAG,QAAQ,MAChC,sBAAsB,QAAQ,GAAG,cAAc,GAAG,QAAQ,OAAO;AAAA,EACrE,CAAC;AACH;","names":["result"]}
1
+ {"version":3,"sources":["../../../src/core/tool/toolResultStream.ts"],"sourcesContent":["import { Tool, ToolCallReader, ToolExecuteFunction } from \"./tool-types\";\nimport { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { ToolResponse } from \"./ToolResponse\";\nimport { ToolExecutionStream } from \"./ToolExecutionStream\";\nimport { AssistantMessage } from \"../utils/types\";\nimport { ReadonlyJSONObject, ReadonlyJSONValue } from \"../../utils\";\n\nconst isStandardSchemaV1 = (\n schema: unknown,\n): schema is StandardSchemaV1<unknown> => {\n return (\n typeof schema === \"object\" &&\n schema !== null &&\n \"~standard\" in schema &&\n (schema as StandardSchemaV1<unknown>)[\"~standard\"].version === 1\n );\n};\n\nfunction getToolResponse(\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n toolCall: {\n toolCallId: string;\n toolName: string;\n args: ReadonlyJSONObject;\n },\n interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,\n) {\n const tool = tools?.[toolCall.toolName];\n if (!tool || !tool.execute) return undefined;\n\n const getResult = async (\n toolExecute: ToolExecuteFunction<ReadonlyJSONObject, unknown>,\n ): Promise<ToolResponse<ReadonlyJSONValue>> => {\n let executeFn = toolExecute;\n\n if (isStandardSchemaV1(tool.parameters)) {\n let result = tool.parameters[\"~standard\"].validate(toolCall.args);\n if (result instanceof Promise) result = await result;\n\n if (result.issues) {\n executeFn =\n tool.experimental_onSchemaValidationError ??\n (() => {\n throw new Error(\n `Function parameter validation failed. ${JSON.stringify(result.issues)}`,\n );\n });\n }\n }\n\n const result = (await executeFn(toolCall.args, {\n toolCallId: toolCall.toolCallId,\n abortSignal,\n interrupt: (payload: unknown) => interrupt(toolCall.toolCallId, payload),\n })) as unknown as ReadonlyJSONValue;\n return ToolResponse.toResponse(result);\n };\n\n return getResult(tool.execute);\n}\n\nfunction getToolStreamResponse(\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n reader: ToolCallReader<any, ReadonlyJSONValue>,\n context: {\n toolCallId: string;\n toolName: string;\n },\n interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,\n) {\n tools?.[context.toolName]?.streamCall?.(reader, {\n toolCallId: context.toolCallId,\n abortSignal,\n interrupt: (payload: unknown) => interrupt(context.toolCallId, payload),\n });\n}\n\nexport async function unstable_runPendingTools(\n message: AssistantMessage,\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,\n) {\n // TODO parallel tool calling\n for (const part of message.parts) {\n if (part.type === \"tool-call\") {\n const promiseOrUndefined = getToolResponse(\n tools,\n abortSignal,\n part,\n interrupt ??\n (async () => {\n throw new Error(\"Tool interrupt is not supported in this context\");\n }),\n );\n if (promiseOrUndefined) {\n const result = await promiseOrUndefined;\n const updatedParts = message.parts.map((p) => {\n if (p.type === \"tool-call\" && p.toolCallId === part.toolCallId) {\n return {\n ...p,\n state: \"result\" as const,\n ...(result.artifact !== undefined\n ? { artifact: result.artifact }\n : {}),\n result: result.result as ReadonlyJSONValue,\n isError: result.isError,\n };\n }\n return p;\n });\n message = {\n ...message,\n parts: updatedParts,\n content: updatedParts,\n };\n }\n }\n }\n return message;\n}\n\nexport function toolResultStream(\n tools:\n | Record<string, Tool>\n | (() => Record<string, Tool> | undefined)\n | undefined,\n abortSignal: AbortSignal | (() => AbortSignal),\n interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,\n) {\n const toolsFn = typeof tools === \"function\" ? tools : () => tools;\n const abortSignalFn =\n typeof abortSignal === \"function\" ? abortSignal : () => abortSignal;\n return new ToolExecutionStream({\n execute: (toolCall) =>\n getToolResponse(toolsFn(), abortSignalFn(), toolCall, interrupt),\n streamCall: ({ reader, ...context }) =>\n getToolStreamResponse(\n toolsFn(),\n abortSignalFn(),\n reader,\n context,\n interrupt,\n ),\n });\n}\n"],"mappings":";AAEA,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AAIpC,IAAM,qBAAqB,CACzB,WACwC;AACxC,SACE,OAAO,WAAW,YAClB,WAAW,QACX,eAAe,UACd,OAAqC,WAAW,EAAE,YAAY;AAEnE;AAEA,SAAS,gBACP,OACA,aACA,UAKA,WACA;AACA,QAAM,OAAO,QAAQ,SAAS,QAAQ;AACtC,MAAI,CAAC,QAAQ,CAAC,KAAK,QAAS,QAAO;AAEnC,QAAM,YAAY,OAChB,gBAC6C;AAC7C,QAAI,YAAY;AAEhB,QAAI,mBAAmB,KAAK,UAAU,GAAG;AACvC,U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,OAAO;AAAA,cACf,SAAS,OAAO;AAAA,YAClB;AAAA,UACF;AACA,iBAAO;AAAA,QACT,CAAC;AACD,kBAAU;AAAA,UACR,GAAG;AAAA,UACH,OAAO;AAAA,UACP,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,iBACd,OAIA,aACA,WACA;AACA,QAAM,UAAU,OAAO,UAAU,aAAa,QAAQ,MAAM;AAC5D,QAAM,gBACJ,OAAO,gBAAgB,aAAa,cAAc,MAAM;AAC1D,SAAO,IAAI,oBAAoB;AAAA,IAC7B,SAAS,CAAC,aACR,gBAAgB,QAAQ,GAAG,cAAc,GAAG,UAAU,SAAS;AAAA,IACjE,YAAY,CAAC,EAAE,QAAQ,GAAG,QAAQ,MAChC;AAAA,MACE,QAAQ;AAAA,MACR,cAAc;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EACJ,CAAC;AACH;","names":["result"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assistant-stream",
3
- "version": "0.2.31",
3
+ "version": "0.2.32",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -77,9 +77,10 @@ export interface ToolCallReader<
77
77
  };
78
78
  }
79
79
 
80
- type ToolExecutionContext = {
80
+ export type ToolExecutionContext = {
81
81
  toolCallId: string;
82
82
  abortSignal: AbortSignal;
83
+ interrupt: (payload: unknown) => Promise<unknown>;
83
84
  };
84
85
 
85
86
  export type ToolExecuteFunction<TArgs, TResult> = (
@@ -24,6 +24,7 @@ function getToolResponse(
24
24
  toolName: string;
25
25
  args: ReadonlyJSONObject;
26
26
  },
27
+ interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,
27
28
  ) {
28
29
  const tool = tools?.[toolCall.toolName];
29
30
  if (!tool || !tool.execute) return undefined;
@@ -51,6 +52,7 @@ function getToolResponse(
51
52
  const result = (await executeFn(toolCall.args, {
52
53
  toolCallId: toolCall.toolCallId,
53
54
  abortSignal,
55
+ interrupt: (payload: unknown) => interrupt(toolCall.toolCallId, payload),
54
56
  })) as unknown as ReadonlyJSONValue;
55
57
  return ToolResponse.toResponse(result);
56
58
  };
@@ -66,10 +68,12 @@ function getToolStreamResponse(
66
68
  toolCallId: string;
67
69
  toolName: string;
68
70
  },
71
+ interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,
69
72
  ) {
70
73
  tools?.[context.toolName]?.streamCall?.(reader, {
71
74
  toolCallId: context.toolCallId,
72
75
  abortSignal,
76
+ interrupt: (payload: unknown) => interrupt(context.toolCallId, payload),
73
77
  });
74
78
  }
75
79
 
@@ -77,11 +81,20 @@ export async function unstable_runPendingTools(
77
81
  message: AssistantMessage,
78
82
  tools: Record<string, Tool> | undefined,
79
83
  abortSignal: AbortSignal,
84
+ interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,
80
85
  ) {
81
86
  // TODO parallel tool calling
82
87
  for (const part of message.parts) {
83
88
  if (part.type === "tool-call") {
84
- const promiseOrUndefined = getToolResponse(tools, abortSignal, part);
89
+ const promiseOrUndefined = getToolResponse(
90
+ tools,
91
+ abortSignal,
92
+ part,
93
+ interrupt ??
94
+ (async () => {
95
+ throw new Error("Tool interrupt is not supported in this context");
96
+ }),
97
+ );
85
98
  if (promiseOrUndefined) {
86
99
  const result = await promiseOrUndefined;
87
100
  const updatedParts = message.parts.map((p) => {
@@ -115,14 +128,21 @@ export function toolResultStream(
115
128
  | (() => Record<string, Tool> | undefined)
116
129
  | undefined,
117
130
  abortSignal: AbortSignal | (() => AbortSignal),
131
+ interrupt: (toolCallId: string, payload: unknown) => Promise<unknown>,
118
132
  ) {
119
133
  const toolsFn = typeof tools === "function" ? tools : () => tools;
120
134
  const abortSignalFn =
121
135
  typeof abortSignal === "function" ? abortSignal : () => abortSignal;
122
136
  return new ToolExecutionStream({
123
137
  execute: (toolCall) =>
124
- getToolResponse(toolsFn(), abortSignalFn(), toolCall),
138
+ getToolResponse(toolsFn(), abortSignalFn(), toolCall, interrupt),
125
139
  streamCall: ({ reader, ...context }) =>
126
- getToolStreamResponse(toolsFn(), abortSignalFn(), reader, context),
140
+ getToolStreamResponse(
141
+ toolsFn(),
142
+ abortSignalFn(),
143
+ reader,
144
+ context,
145
+ interrupt,
146
+ ),
127
147
  });
128
148
  }