veryfront 0.1.138 → 0.1.139
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/esm/deno.js +1 -1
- package/esm/src/agent/runtime/ai-stream-handler.d.ts +10 -0
- package/esm/src/agent/runtime/ai-stream-handler.d.ts.map +1 -1
- package/esm/src/agent/runtime/ai-stream-handler.js +34 -0
- package/esm/src/agent/runtime/index.d.ts.map +1 -1
- package/esm/src/agent/runtime/index.js +46 -2
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +1 -1
- package/src/deno.js +1 -1
- package/src/src/agent/runtime/ai-stream-handler.ts +46 -0
- package/src/src/agent/runtime/index.ts +53 -2
- package/src/src/utils/version-constant.ts +1 -1
package/esm/deno.js
CHANGED
|
@@ -15,10 +15,20 @@ export interface StreamingToolCall {
|
|
|
15
15
|
providerExecuted?: boolean;
|
|
16
16
|
dynamic?: boolean;
|
|
17
17
|
}
|
|
18
|
+
export interface StreamingToolResult {
|
|
19
|
+
toolCallId: string;
|
|
20
|
+
toolName: string;
|
|
21
|
+
output?: unknown;
|
|
22
|
+
error?: unknown;
|
|
23
|
+
providerExecuted?: boolean;
|
|
24
|
+
dynamic?: boolean;
|
|
25
|
+
preliminary?: boolean;
|
|
26
|
+
}
|
|
18
27
|
export interface AIStreamState {
|
|
19
28
|
accumulatedText: string;
|
|
20
29
|
finishReason: string | null;
|
|
21
30
|
toolCalls: Map<string, StreamingToolCall>;
|
|
31
|
+
toolResults: StreamingToolResult[];
|
|
22
32
|
usage: {
|
|
23
33
|
promptTokens: number;
|
|
24
34
|
completionTokens: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-stream-handler.d.ts","sourceRoot":"","sources":["../../../../src/src/agent/runtime/ai-stream-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,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typeof output === "string" && output.length > 0) {
|
|
54
54
|
return output;
|
|
55
55
|
}
|
|
56
|
+
if (output instanceof Error && typeof output.message === "string" && output.message.length > 0) {
|
|
57
|
+
return output.message;
|
|
58
|
+
}
|
|
56
59
|
try {
|
|
57
60
|
return JSON.stringify(output);
|
|
58
61
|
}
|
|
@@ -65,6 +68,7 @@ export function createStreamState() {
|
|
|
65
68
|
accumulatedText: "",
|
|
66
69
|
finishReason: null,
|
|
67
70
|
toolCalls: new Map(),
|
|
71
|
+
toolResults: [],
|
|
68
72
|
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
|
69
73
|
};
|
|
70
74
|
}
|
|
@@ -155,6 +159,15 @@ export function processStream(result, state, controller, encoder, textPartId, ca
|
|
|
155
159
|
case "tool-result": {
|
|
156
160
|
const isError = "isError" in part && part.isError === true;
|
|
157
161
|
if (isError) {
|
|
162
|
+
state.toolResults.push({
|
|
163
|
+
toolCallId: part.toolCallId,
|
|
164
|
+
toolName: part.toolName,
|
|
165
|
+
error: "output" in part ? part.output : undefined,
|
|
166
|
+
...("providerExecuted" in part && part.providerExecuted !== undefined
|
|
167
|
+
? { providerExecuted: part.providerExecuted }
|
|
168
|
+
: {}),
|
|
169
|
+
...("dynamic" in part && part.dynamic ? { dynamic: true } : {}),
|
|
170
|
+
});
|
|
158
171
|
sendSSE(controller, encoder, {
|
|
159
172
|
type: "tool-output-error",
|
|
160
173
|
toolCallId: part.toolCallId,
|
|
@@ -166,6 +179,18 @@ export function processStream(result, state, controller, encoder, textPartId, ca
|
|
|
166
179
|
});
|
|
167
180
|
break;
|
|
168
181
|
}
|
|
182
|
+
state.toolResults.push({
|
|
183
|
+
toolCallId: part.toolCallId,
|
|
184
|
+
toolName: part.toolName,
|
|
185
|
+
output: part.output,
|
|
186
|
+
...("providerExecuted" in part && part.providerExecuted !== undefined
|
|
187
|
+
? { providerExecuted: part.providerExecuted }
|
|
188
|
+
: {}),
|
|
189
|
+
...("dynamic" in part && part.dynamic ? { dynamic: true } : {}),
|
|
190
|
+
...("preliminary" in part && part.preliminary !== undefined
|
|
191
|
+
? { preliminary: part.preliminary }
|
|
192
|
+
: {}),
|
|
193
|
+
});
|
|
169
194
|
sendSSE(controller, encoder, {
|
|
170
195
|
type: "tool-output-available",
|
|
171
196
|
toolCallId: part.toolCallId,
|
|
@@ -181,6 +206,15 @@ export function processStream(result, state, controller, encoder, textPartId, ca
|
|
|
181
206
|
break;
|
|
182
207
|
}
|
|
183
208
|
case "tool-error": {
|
|
209
|
+
state.toolResults.push({
|
|
210
|
+
toolCallId: part.toolCallId,
|
|
211
|
+
toolName: part.toolName,
|
|
212
|
+
error: part.error,
|
|
213
|
+
...("providerExecuted" in part && part.providerExecuted !== undefined
|
|
214
|
+
? { providerExecuted: part.providerExecuted }
|
|
215
|
+
: {}),
|
|
216
|
+
...("dynamic" in part && part.dynamic ? { dynamic: true } : {}),
|
|
217
|
+
});
|
|
184
218
|
sendSSE(controller, encoder, {
|
|
185
219
|
type: "tool-output-error",
|
|
186
220
|
toolCallId: part.toolCallId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/agent/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,KAAK,WAAW,EAEhB,KAAK,aAAa,EAGlB,KAAK,OAAO,EAEZ,KAAK,QAAQ,EACd,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAiB/D,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC5E,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,aAAa,EACb,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,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;OAIG;YACW,yBAAyB;IAyRvC;;OAEG;YACW,eAAe;IAqC7B;;OAEG;YACW,mBAAmB;IAOjC;;OAEG;IACH,OAAO,CAAC,eAAe;IAKvB,OAAO,CAAC,sBAAsB;IAY9B;;OAEG;IACH,SAAS,IAAI,MAAM,CAAC,OAAO,CAAC;IAI5B;;OAEG;IACG,cAAc,IAAI,OAAO,CAAC;QAC9B,aAAa,EAAE,MAAM,CAAC;QACtB,eAAe,EAAE,MAAM,CAAC;QACxB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IAIF;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;CAGnC"}
|
|
@@ -56,6 +56,20 @@ function isAbortError(error, abortSignal) {
|
|
|
56
56
|
}
|
|
57
57
|
return error instanceof DOMException && error.name === "AbortError";
|
|
58
58
|
}
|
|
59
|
+
function stringifyToolError(error) {
|
|
60
|
+
if (typeof error === "string" && error.length > 0) {
|
|
61
|
+
return error;
|
|
62
|
+
}
|
|
63
|
+
if (error instanceof Error && typeof error.message === "string" && error.message.length > 0) {
|
|
64
|
+
return error.message;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
return JSON.stringify(error);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return String(error);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
59
73
|
function getSkillActivationRequiredError(toolName) {
|
|
60
74
|
return `Tool "${toolName}" cannot run before load-skill succeeds in the same step. ` +
|
|
61
75
|
`Call "${LOAD_SKILL_TOOL_ID}" first to establish the active skill context.`;
|
|
@@ -491,6 +505,7 @@ export class AgentRuntime {
|
|
|
491
505
|
// Request-scoped skill policy (not class-level mutable state)
|
|
492
506
|
let activeSkillPolicy;
|
|
493
507
|
let finalFinishReason;
|
|
508
|
+
let latestAssistantText = "";
|
|
494
509
|
const allowedRemoteToolNames = getRuntimeAllowedRemoteTools(this.config);
|
|
495
510
|
for (let step = 0; step < maxSteps; step++) {
|
|
496
511
|
throwIfAborted(abortSignal);
|
|
@@ -547,8 +562,29 @@ export class AgentRuntime {
|
|
|
547
562
|
parts: streamParts,
|
|
548
563
|
timestamp: Date.now(),
|
|
549
564
|
};
|
|
565
|
+
latestAssistantText = getTextFromParts(assistantMessage.parts);
|
|
550
566
|
currentMessages.push(assistantMessage);
|
|
551
567
|
await this.memory.add(assistantMessage);
|
|
568
|
+
for (const tr of state.toolResults) {
|
|
569
|
+
if (tr.preliminary) {
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const toolResultMessage = {
|
|
573
|
+
id: `tool_${tr.toolCallId}`,
|
|
574
|
+
role: "tool",
|
|
575
|
+
parts: [
|
|
576
|
+
{
|
|
577
|
+
type: "tool-result",
|
|
578
|
+
toolCallId: tr.toolCallId,
|
|
579
|
+
toolName: tr.toolName,
|
|
580
|
+
result: tr.error === undefined ? tr.output : { error: stringifyToolError(tr.error) },
|
|
581
|
+
},
|
|
582
|
+
],
|
|
583
|
+
timestamp: Date.now(),
|
|
584
|
+
};
|
|
585
|
+
currentMessages.push(toolResultMessage);
|
|
586
|
+
await this.memory.add(toolResultMessage);
|
|
587
|
+
}
|
|
552
588
|
if (state.finishReason !== "tool-calls" || !state.toolCalls.size) {
|
|
553
589
|
sendSSE(controller, encoder, { type: "step-end" });
|
|
554
590
|
break;
|
|
@@ -563,6 +599,15 @@ export class AgentRuntime {
|
|
|
563
599
|
const { args, error: argError } = parseToolArgs(tc.arguments);
|
|
564
600
|
const toolCall = { id: tc.id, name: tc.name, args, status: "pending" };
|
|
565
601
|
if (tc.providerExecuted === true) {
|
|
602
|
+
const matchingResult = state.toolResults.find((result) => result.toolCallId === tc.id && result.preliminary !== true);
|
|
603
|
+
if (matchingResult) {
|
|
604
|
+
toolCall.status = matchingResult.error === undefined ? "completed" : "error";
|
|
605
|
+
toolCall.result = matchingResult.output;
|
|
606
|
+
toolCall.error = matchingResult.error === undefined
|
|
607
|
+
? undefined
|
|
608
|
+
: stringifyToolError(matchingResult.error);
|
|
609
|
+
toolCalls.push(toolCall);
|
|
610
|
+
}
|
|
566
611
|
continue;
|
|
567
612
|
}
|
|
568
613
|
if (argError) {
|
|
@@ -636,9 +681,8 @@ export class AgentRuntime {
|
|
|
636
681
|
sendSSE(controller, encoder, { type: "step-end" });
|
|
637
682
|
this.status = "thinking";
|
|
638
683
|
}
|
|
639
|
-
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
640
684
|
return {
|
|
641
|
-
text:
|
|
685
|
+
text: latestAssistantText,
|
|
642
686
|
messages: currentMessages,
|
|
643
687
|
toolCalls,
|
|
644
688
|
status: "completed",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.139";
|
|
2
2
|
//# sourceMappingURL=version-constant.d.ts.map
|
package/package.json
CHANGED
package/src/deno.js
CHANGED
|
@@ -24,10 +24,21 @@ export interface StreamingToolCall {
|
|
|
24
24
|
dynamic?: boolean;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
export interface StreamingToolResult {
|
|
28
|
+
toolCallId: string;
|
|
29
|
+
toolName: string;
|
|
30
|
+
output?: unknown;
|
|
31
|
+
error?: unknown;
|
|
32
|
+
providerExecuted?: boolean;
|
|
33
|
+
dynamic?: boolean;
|
|
34
|
+
preliminary?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
27
37
|
export interface AIStreamState {
|
|
28
38
|
accumulatedText: string;
|
|
29
39
|
finishReason: string | null;
|
|
30
40
|
toolCalls: Map<string, StreamingToolCall>;
|
|
41
|
+
toolResults: StreamingToolResult[];
|
|
31
42
|
usage: { promptTokens: number; completionTokens: number; totalTokens: number };
|
|
32
43
|
}
|
|
33
44
|
|
|
@@ -93,6 +104,10 @@ function stringifyToolError(output: unknown): string {
|
|
|
93
104
|
return output;
|
|
94
105
|
}
|
|
95
106
|
|
|
107
|
+
if (output instanceof Error && typeof output.message === "string" && output.message.length > 0) {
|
|
108
|
+
return output.message;
|
|
109
|
+
}
|
|
110
|
+
|
|
96
111
|
try {
|
|
97
112
|
return JSON.stringify(output);
|
|
98
113
|
} catch {
|
|
@@ -105,6 +120,7 @@ export function createStreamState(): AIStreamState {
|
|
|
105
120
|
accumulatedText: "",
|
|
106
121
|
finishReason: null,
|
|
107
122
|
toolCalls: new Map(),
|
|
123
|
+
toolResults: [],
|
|
108
124
|
usage: { promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
|
109
125
|
};
|
|
110
126
|
}
|
|
@@ -213,6 +229,15 @@ export function processStream(
|
|
|
213
229
|
case "tool-result": {
|
|
214
230
|
const isError = "isError" in part && part.isError === true;
|
|
215
231
|
if (isError) {
|
|
232
|
+
state.toolResults.push({
|
|
233
|
+
toolCallId: part.toolCallId,
|
|
234
|
+
toolName: part.toolName,
|
|
235
|
+
error: "output" in part ? part.output : undefined,
|
|
236
|
+
...("providerExecuted" in part && part.providerExecuted !== undefined
|
|
237
|
+
? { providerExecuted: part.providerExecuted }
|
|
238
|
+
: {}),
|
|
239
|
+
...("dynamic" in part && part.dynamic ? { dynamic: true } : {}),
|
|
240
|
+
});
|
|
216
241
|
sendSSE(controller, encoder, {
|
|
217
242
|
type: "tool-output-error",
|
|
218
243
|
toolCallId: part.toolCallId,
|
|
@@ -225,6 +250,18 @@ export function processStream(
|
|
|
225
250
|
break;
|
|
226
251
|
}
|
|
227
252
|
|
|
253
|
+
state.toolResults.push({
|
|
254
|
+
toolCallId: part.toolCallId,
|
|
255
|
+
toolName: part.toolName,
|
|
256
|
+
output: part.output,
|
|
257
|
+
...("providerExecuted" in part && part.providerExecuted !== undefined
|
|
258
|
+
? { providerExecuted: part.providerExecuted }
|
|
259
|
+
: {}),
|
|
260
|
+
...("dynamic" in part && part.dynamic ? { dynamic: true } : {}),
|
|
261
|
+
...("preliminary" in part && part.preliminary !== undefined
|
|
262
|
+
? { preliminary: part.preliminary }
|
|
263
|
+
: {}),
|
|
264
|
+
});
|
|
228
265
|
sendSSE(controller, encoder, {
|
|
229
266
|
type: "tool-output-available",
|
|
230
267
|
toolCallId: part.toolCallId,
|
|
@@ -241,6 +278,15 @@ export function processStream(
|
|
|
241
278
|
}
|
|
242
279
|
|
|
243
280
|
case "tool-error": {
|
|
281
|
+
state.toolResults.push({
|
|
282
|
+
toolCallId: part.toolCallId,
|
|
283
|
+
toolName: part.toolName,
|
|
284
|
+
error: part.error,
|
|
285
|
+
...("providerExecuted" in part && part.providerExecuted !== undefined
|
|
286
|
+
? { providerExecuted: part.providerExecuted }
|
|
287
|
+
: {}),
|
|
288
|
+
...("dynamic" in part && part.dynamic ? { dynamic: true } : {}),
|
|
289
|
+
});
|
|
244
290
|
sendSSE(controller, encoder, {
|
|
245
291
|
type: "tool-output-error",
|
|
246
292
|
toolCallId: part.toolCallId,
|
|
@@ -107,6 +107,22 @@ function isAbortError(error: unknown, abortSignal?: AbortSignal): boolean {
|
|
|
107
107
|
return error instanceof DOMException && error.name === "AbortError";
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
function stringifyToolError(error: unknown): string {
|
|
111
|
+
if (typeof error === "string" && error.length > 0) {
|
|
112
|
+
return error;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (error instanceof Error && typeof error.message === "string" && error.message.length > 0) {
|
|
116
|
+
return error.message;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
return JSON.stringify(error);
|
|
121
|
+
} catch {
|
|
122
|
+
return String(error);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
110
126
|
function getSkillActivationRequiredError(toolName: string): string {
|
|
111
127
|
return `Tool "${toolName}" cannot run before load-skill succeeds in the same step. ` +
|
|
112
128
|
`Call "${LOAD_SKILL_TOOL_ID}" first to establish the active skill context.`;
|
|
@@ -686,6 +702,7 @@ export class AgentRuntime {
|
|
|
686
702
|
// Request-scoped skill policy (not class-level mutable state)
|
|
687
703
|
let activeSkillPolicy: string[] | undefined;
|
|
688
704
|
let finalFinishReason: string | undefined;
|
|
705
|
+
let latestAssistantText = "";
|
|
689
706
|
const allowedRemoteToolNames = getRuntimeAllowedRemoteTools(this.config);
|
|
690
707
|
|
|
691
708
|
for (let step = 0; step < maxSteps; step++) {
|
|
@@ -749,9 +766,32 @@ export class AgentRuntime {
|
|
|
749
766
|
parts: streamParts,
|
|
750
767
|
timestamp: Date.now(),
|
|
751
768
|
};
|
|
769
|
+
latestAssistantText = getTextFromParts(assistantMessage.parts);
|
|
752
770
|
currentMessages.push(assistantMessage);
|
|
753
771
|
await this.memory.add(assistantMessage);
|
|
754
772
|
|
|
773
|
+
for (const tr of state.toolResults) {
|
|
774
|
+
if (tr.preliminary) {
|
|
775
|
+
continue;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const toolResultMessage: Message = {
|
|
779
|
+
id: `tool_${tr.toolCallId}`,
|
|
780
|
+
role: "tool",
|
|
781
|
+
parts: [
|
|
782
|
+
{
|
|
783
|
+
type: "tool-result",
|
|
784
|
+
toolCallId: tr.toolCallId,
|
|
785
|
+
toolName: tr.toolName,
|
|
786
|
+
result: tr.error === undefined ? tr.output : { error: stringifyToolError(tr.error) },
|
|
787
|
+
},
|
|
788
|
+
],
|
|
789
|
+
timestamp: Date.now(),
|
|
790
|
+
};
|
|
791
|
+
currentMessages.push(toolResultMessage);
|
|
792
|
+
await this.memory.add(toolResultMessage);
|
|
793
|
+
}
|
|
794
|
+
|
|
755
795
|
if (state.finishReason !== "tool-calls" || !state.toolCalls.size) {
|
|
756
796
|
sendSSE(controller, encoder, { type: "step-end" });
|
|
757
797
|
break;
|
|
@@ -769,6 +809,18 @@ export class AgentRuntime {
|
|
|
769
809
|
const toolCall: ToolCall = { id: tc.id, name: tc.name, args, status: "pending" };
|
|
770
810
|
|
|
771
811
|
if (tc.providerExecuted === true) {
|
|
812
|
+
const matchingResult = state.toolResults.find((result) =>
|
|
813
|
+
result.toolCallId === tc.id && result.preliminary !== true
|
|
814
|
+
);
|
|
815
|
+
|
|
816
|
+
if (matchingResult) {
|
|
817
|
+
toolCall.status = matchingResult.error === undefined ? "completed" : "error";
|
|
818
|
+
toolCall.result = matchingResult.output;
|
|
819
|
+
toolCall.error = matchingResult.error === undefined
|
|
820
|
+
? undefined
|
|
821
|
+
: stringifyToolError(matchingResult.error);
|
|
822
|
+
toolCalls.push(toolCall);
|
|
823
|
+
}
|
|
772
824
|
continue;
|
|
773
825
|
}
|
|
774
826
|
|
|
@@ -881,9 +933,8 @@ export class AgentRuntime {
|
|
|
881
933
|
this.status = "thinking";
|
|
882
934
|
}
|
|
883
935
|
|
|
884
|
-
const lastMessage = currentMessages[currentMessages.length - 1];
|
|
885
936
|
return {
|
|
886
|
-
text:
|
|
937
|
+
text: latestAssistantText,
|
|
887
938
|
messages: currentMessages,
|
|
888
939
|
toolCalls,
|
|
889
940
|
status: "completed",
|