assistant-stream 0.2.32 → 0.2.34
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/tool/tool-types.d.ts +1 -1
- package/dist/core/tool/tool-types.d.ts.map +1 -1
- package/dist/core/tool/toolResultStream.d.ts +2 -2
- package/dist/core/tool/toolResultStream.d.ts.map +1 -1
- package/dist/core/tool/toolResultStream.js +12 -16
- package/dist/core/tool/toolResultStream.js.map +1 -1
- package/package.json +2 -2
- package/src/core/tool/tool-types.ts +1 -1
- package/src/core/tool/toolResultStream.ts +12 -16
|
@@ -57,7 +57,7 @@ export interface ToolCallReader<TArgs extends Record<string, unknown> = Record<s
|
|
|
57
57
|
export type ToolExecutionContext = {
|
|
58
58
|
toolCallId: string;
|
|
59
59
|
abortSignal: AbortSignal;
|
|
60
|
-
|
|
60
|
+
human: (payload: unknown) => Promise<unknown>;
|
|
61
61
|
};
|
|
62
62
|
export type ToolExecuteFunction<TArgs, TResult> = (args: TArgs, context: ToolExecutionContext) => TResult | Promise<TResult>;
|
|
63
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,C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;IACnB,OAAO,CAAC,EAAE,mBAAmB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC9C,oCAAoC,CAAC,EAAE,+BAA+B,CAAC,OAAO,CAAC,CAAC;CACjF,CAAC;AAEF,KAAK,SAAS,CACZ,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/D,OAAO,GAAG,OAAO,IACf,QAAQ,CAAC,KAAK,EAAE,OAAO,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,
|
|
5
|
-
export declare function toolResultStream(tools: Record<string, Tool> | (() => Record<string, Tool> | undefined) | undefined, abortSignal: AbortSignal | (() => AbortSignal),
|
|
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
|
+
export declare function toolResultStream(tools: Record<string, Tool> | (() => Record<string, Tool> | undefined) | undefined, abortSignal: AbortSignal | (() => AbortSignal), human: (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;AA2ElD,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,gBAAgB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAAS,EACvC,WAAW,EAAE,WAAW,EACxB,
|
|
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,KAAK,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,6BAyClE;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,KAAK,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,uBAWlE"}
|
|
@@ -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, human) {
|
|
8
8
|
const tool = tools?.[toolCall.toolName];
|
|
9
9
|
if (!tool || !tool.execute) return void 0;
|
|
10
10
|
const getResult = async (toolExecute) => {
|
|
@@ -23,28 +23,30 @@ function getToolResponse(tools, abortSignal, toolCall, interrupt) {
|
|
|
23
23
|
const result = await executeFn(toolCall.args, {
|
|
24
24
|
toolCallId: toolCall.toolCallId,
|
|
25
25
|
abortSignal,
|
|
26
|
-
|
|
26
|
+
human: (payload) => human(toolCall.toolCallId, payload)
|
|
27
27
|
});
|
|
28
28
|
return ToolResponse.toResponse(result);
|
|
29
29
|
};
|
|
30
30
|
return getResult(tool.execute);
|
|
31
31
|
}
|
|
32
|
-
function getToolStreamResponse(tools, abortSignal, reader, context,
|
|
32
|
+
function getToolStreamResponse(tools, abortSignal, reader, context, human) {
|
|
33
33
|
tools?.[context.toolName]?.streamCall?.(reader, {
|
|
34
34
|
toolCallId: context.toolCallId,
|
|
35
35
|
abortSignal,
|
|
36
|
-
|
|
36
|
+
human: (payload) => human(context.toolCallId, payload)
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
|
-
async function unstable_runPendingTools(message, tools, abortSignal,
|
|
39
|
+
async function unstable_runPendingTools(message, tools, abortSignal, human) {
|
|
40
40
|
for (const part of message.parts) {
|
|
41
41
|
if (part.type === "tool-call") {
|
|
42
42
|
const promiseOrUndefined = getToolResponse(
|
|
43
43
|
tools,
|
|
44
44
|
abortSignal,
|
|
45
45
|
part,
|
|
46
|
-
|
|
47
|
-
throw new Error(
|
|
46
|
+
human ?? (async () => {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"Tool human input is not supported in this context"
|
|
49
|
+
);
|
|
48
50
|
})
|
|
49
51
|
);
|
|
50
52
|
if (promiseOrUndefined) {
|
|
@@ -71,18 +73,12 @@ async function unstable_runPendingTools(message, tools, abortSignal, interrupt)
|
|
|
71
73
|
}
|
|
72
74
|
return message;
|
|
73
75
|
}
|
|
74
|
-
function toolResultStream(tools, abortSignal,
|
|
76
|
+
function toolResultStream(tools, abortSignal, human) {
|
|
75
77
|
const toolsFn = typeof tools === "function" ? tools : () => tools;
|
|
76
78
|
const abortSignalFn = typeof abortSignal === "function" ? abortSignal : () => abortSignal;
|
|
77
79
|
return new ToolExecutionStream({
|
|
78
|
-
execute: (toolCall) => getToolResponse(toolsFn(), abortSignalFn(), toolCall,
|
|
79
|
-
streamCall: ({ reader, ...context }) => getToolStreamResponse(
|
|
80
|
-
toolsFn(),
|
|
81
|
-
abortSignalFn(),
|
|
82
|
-
reader,
|
|
83
|
-
context,
|
|
84
|
-
interrupt
|
|
85
|
-
)
|
|
80
|
+
execute: (toolCall) => getToolResponse(toolsFn(), abortSignalFn(), toolCall, human),
|
|
81
|
+
streamCall: ({ reader, ...context }) => getToolStreamResponse(toolsFn(), abortSignalFn(), reader, context, human)
|
|
86
82
|
});
|
|
87
83
|
}
|
|
88
84
|
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
|
|
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 human: (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 human: (payload: unknown) => human(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 human: (toolCallId: string, payload: unknown) => Promise<unknown>,\n) {\n tools?.[context.toolName]?.streamCall?.(reader, {\n toolCallId: context.toolCallId,\n abortSignal,\n human: (payload: unknown) => human(context.toolCallId, payload),\n });\n}\n\nexport async function unstable_runPendingTools(\n message: AssistantMessage,\n tools: Record<string, Tool> | undefined,\n abortSignal: AbortSignal,\n human: (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 human ??\n (async () => {\n throw new Error(\n \"Tool human input is not supported in this context\",\n );\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 human: (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, human),\n streamCall: ({ reader, ...context }) =>\n getToolStreamResponse(toolsFn(), abortSignalFn(), reader, context, human),\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,OACA;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,MACA,OAAO,CAAC,YAAqB,MAAM,SAAS,YAAY,OAAO;AAAA,IACjE,CAAC;AACD,WAAO,aAAa,WAAW,MAAM;AAAA,EACvC;AAEA,SAAO,UAAU,KAAK,OAAO;AAC/B;AAEA,SAAS,sBACP,OACA,aACA,QACA,SAIA,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,SAAS,KAAK;AAAA,EAC5E,CAAC;AACH;","names":["result"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assistant-stream",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.34",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@standard-schema/spec": "^1.0.0",
|
|
28
|
-
"@types/node": "^24.
|
|
28
|
+
"@types/node": "^24.6.2",
|
|
29
29
|
"eslint": "^9",
|
|
30
30
|
"eslint-config-next": "15.5.4",
|
|
31
31
|
"tsx": "^4.20.6",
|
|
@@ -80,7 +80,7 @@ export interface ToolCallReader<
|
|
|
80
80
|
export type ToolExecutionContext = {
|
|
81
81
|
toolCallId: string;
|
|
82
82
|
abortSignal: AbortSignal;
|
|
83
|
-
|
|
83
|
+
human: (payload: unknown) => Promise<unknown>;
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
export type ToolExecuteFunction<TArgs, TResult> = (
|
|
@@ -24,7 +24,7 @@ function getToolResponse(
|
|
|
24
24
|
toolName: string;
|
|
25
25
|
args: ReadonlyJSONObject;
|
|
26
26
|
},
|
|
27
|
-
|
|
27
|
+
human: (toolCallId: string, payload: unknown) => Promise<unknown>,
|
|
28
28
|
) {
|
|
29
29
|
const tool = tools?.[toolCall.toolName];
|
|
30
30
|
if (!tool || !tool.execute) return undefined;
|
|
@@ -52,7 +52,7 @@ function getToolResponse(
|
|
|
52
52
|
const result = (await executeFn(toolCall.args, {
|
|
53
53
|
toolCallId: toolCall.toolCallId,
|
|
54
54
|
abortSignal,
|
|
55
|
-
|
|
55
|
+
human: (payload: unknown) => human(toolCall.toolCallId, payload),
|
|
56
56
|
})) as unknown as ReadonlyJSONValue;
|
|
57
57
|
return ToolResponse.toResponse(result);
|
|
58
58
|
};
|
|
@@ -68,12 +68,12 @@ function getToolStreamResponse(
|
|
|
68
68
|
toolCallId: string;
|
|
69
69
|
toolName: string;
|
|
70
70
|
},
|
|
71
|
-
|
|
71
|
+
human: (toolCallId: string, payload: unknown) => Promise<unknown>,
|
|
72
72
|
) {
|
|
73
73
|
tools?.[context.toolName]?.streamCall?.(reader, {
|
|
74
74
|
toolCallId: context.toolCallId,
|
|
75
75
|
abortSignal,
|
|
76
|
-
|
|
76
|
+
human: (payload: unknown) => human(context.toolCallId, payload),
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -81,7 +81,7 @@ export async function unstable_runPendingTools(
|
|
|
81
81
|
message: AssistantMessage,
|
|
82
82
|
tools: Record<string, Tool> | undefined,
|
|
83
83
|
abortSignal: AbortSignal,
|
|
84
|
-
|
|
84
|
+
human: (toolCallId: string, payload: unknown) => Promise<unknown>,
|
|
85
85
|
) {
|
|
86
86
|
// TODO parallel tool calling
|
|
87
87
|
for (const part of message.parts) {
|
|
@@ -90,9 +90,11 @@ export async function unstable_runPendingTools(
|
|
|
90
90
|
tools,
|
|
91
91
|
abortSignal,
|
|
92
92
|
part,
|
|
93
|
-
|
|
93
|
+
human ??
|
|
94
94
|
(async () => {
|
|
95
|
-
throw new Error(
|
|
95
|
+
throw new Error(
|
|
96
|
+
"Tool human input is not supported in this context",
|
|
97
|
+
);
|
|
96
98
|
}),
|
|
97
99
|
);
|
|
98
100
|
if (promiseOrUndefined) {
|
|
@@ -128,21 +130,15 @@ export function toolResultStream(
|
|
|
128
130
|
| (() => Record<string, Tool> | undefined)
|
|
129
131
|
| undefined,
|
|
130
132
|
abortSignal: AbortSignal | (() => AbortSignal),
|
|
131
|
-
|
|
133
|
+
human: (toolCallId: string, payload: unknown) => Promise<unknown>,
|
|
132
134
|
) {
|
|
133
135
|
const toolsFn = typeof tools === "function" ? tools : () => tools;
|
|
134
136
|
const abortSignalFn =
|
|
135
137
|
typeof abortSignal === "function" ? abortSignal : () => abortSignal;
|
|
136
138
|
return new ToolExecutionStream({
|
|
137
139
|
execute: (toolCall) =>
|
|
138
|
-
getToolResponse(toolsFn(), abortSignalFn(), toolCall,
|
|
140
|
+
getToolResponse(toolsFn(), abortSignalFn(), toolCall, human),
|
|
139
141
|
streamCall: ({ reader, ...context }) =>
|
|
140
|
-
getToolStreamResponse(
|
|
141
|
-
toolsFn(),
|
|
142
|
-
abortSignalFn(),
|
|
143
|
-
reader,
|
|
144
|
-
context,
|
|
145
|
-
interrupt,
|
|
146
|
-
),
|
|
142
|
+
getToolStreamResponse(toolsFn(), abortSignalFn(), reader, context, human),
|
|
147
143
|
});
|
|
148
144
|
}
|