veryfront 0.1.625 → 0.1.626
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/current-run-tool-state.d.ts +36 -0
- package/esm/src/agent/runtime/current-run-tool-state.d.ts.map +1 -0
- package/esm/src/agent/runtime/current-run-tool-state.js +246 -0
- package/esm/src/agent/runtime/index.d.ts.map +1 -1
- package/esm/src/agent/runtime/index.js +65 -9
- package/esm/src/integrations/_data.js +7 -7
- package/esm/src/integrations/_tool_summaries.d.ts.map +1 -1
- package/esm/src/integrations/_tool_summaries.js +788 -0
- package/esm/src/integrations/schema.d.ts +4 -4
- 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/esm/deno.js
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
type ToolStatus = "success" | "empty" | "error";
|
|
2
|
+
export type CurrentRunToolStateCall = {
|
|
3
|
+
toolCallIds: string[];
|
|
4
|
+
input: unknown;
|
|
5
|
+
status: ToolStatus;
|
|
6
|
+
summary: unknown;
|
|
7
|
+
updatedAt: string;
|
|
8
|
+
};
|
|
9
|
+
export type CurrentRunToolState = Record<string, {
|
|
10
|
+
calls: Record<string, CurrentRunToolStateCall>;
|
|
11
|
+
}>;
|
|
12
|
+
export type RecordCurrentRunToolResultInput = {
|
|
13
|
+
toolCallId: string;
|
|
14
|
+
toolName: string;
|
|
15
|
+
input: unknown;
|
|
16
|
+
result: unknown;
|
|
17
|
+
now?: Date;
|
|
18
|
+
};
|
|
19
|
+
export declare function createToolInputFingerprint(input: unknown): string;
|
|
20
|
+
export declare function summarizeToolResultForCurrentRunState(toolName: string, result: unknown): {
|
|
21
|
+
summary: unknown;
|
|
22
|
+
status: ToolStatus;
|
|
23
|
+
};
|
|
24
|
+
export declare function createCurrentRunToolState(): CurrentRunToolState;
|
|
25
|
+
export declare function recordCurrentRunToolResult(state: CurrentRunToolState, input: RecordCurrentRunToolResultInput): void;
|
|
26
|
+
export declare function hasCurrentRunToolState(state: CurrentRunToolState): boolean;
|
|
27
|
+
type PromptToolState = Record<string, {
|
|
28
|
+
calls: Record<string, {
|
|
29
|
+
status: ToolStatus;
|
|
30
|
+
summary: unknown;
|
|
31
|
+
}>;
|
|
32
|
+
}>;
|
|
33
|
+
export declare function projectCurrentRunToolStateForPrompt(state: CurrentRunToolState): PromptToolState;
|
|
34
|
+
export declare function appendCurrentRunToolStateToSystemPrompt(systemPrompt: string, state: CurrentRunToolState): string;
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=current-run-tool-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"current-run-tool-state.d.ts","sourceRoot":"","sources":["../../../../src/src/agent/runtime/current-run-tool-state.ts"],"names":[],"mappings":"AAIA,KAAK,UAAU,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,CAAC;AAEhD,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,UAAU,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG,MAAM,CACtC,MAAM,EACN;IAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,uBAAuB,CAAC,CAAA;CAAE,CACnD,CAAC;AAEF,MAAM,MAAM,+BAA+B,GAAG;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ,CAAC;AAyBF,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAEjE;AAmMD,wBAAgB,qCAAqC,CACnD,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,OAAO,GACd;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,UAAU,CAAA;CAAE,CAY1C;AAED,wBAAgB,yBAAyB,IAAI,mBAAmB,CAE/D;AAED,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,mBAAmB,EAC1B,KAAK,EAAE,+BAA+B,GACrC,IAAI,CAgBN;AAED,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,mBAAmB,GAAG,OAAO,CAI1E;AAED,KAAK,eAAe,GAAG,MAAM,CAC3B,MAAM,EACN;IAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,UAAU,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAA;CAAE,CACpE,CAAC;AAEF,wBAAgB,mCAAmC,CACjD,KAAK,EAAE,mBAAmB,GACzB,eAAe,CAejB;AAED,wBAAgB,uCAAuC,CACrD,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,mBAAmB,GACzB,MAAM,CAOR"}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { historicalToolSummaries } from "../../integrations/_tool_summaries.js";
|
|
2
|
+
const MAX_FALLBACK_ITEMS = 5;
|
|
3
|
+
const MAX_FALLBACK_STRING_LENGTH = 300;
|
|
4
|
+
const MAX_OBJECT_ARRAY_ITEMS = 5;
|
|
5
|
+
function isRecord(value) {
|
|
6
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
function truncate(value, maxLength) {
|
|
9
|
+
return value.length > maxLength ? `${value.slice(0, maxLength)}…` : value;
|
|
10
|
+
}
|
|
11
|
+
function normalizeForFingerprint(value) {
|
|
12
|
+
if (Array.isArray(value))
|
|
13
|
+
return value.map(normalizeForFingerprint);
|
|
14
|
+
if (!isRecord(value))
|
|
15
|
+
return value;
|
|
16
|
+
const normalized = {};
|
|
17
|
+
for (const key of Object.keys(value).sort()) {
|
|
18
|
+
normalized[key] = normalizeForFingerprint(value[key]);
|
|
19
|
+
}
|
|
20
|
+
return normalized;
|
|
21
|
+
}
|
|
22
|
+
export function createToolInputFingerprint(input) {
|
|
23
|
+
return JSON.stringify(normalizeForFingerprint(input ?? {}));
|
|
24
|
+
}
|
|
25
|
+
function compactContactValue(value) {
|
|
26
|
+
if (typeof value === "string")
|
|
27
|
+
return value;
|
|
28
|
+
if (!isRecord(value))
|
|
29
|
+
return null;
|
|
30
|
+
const compact = {};
|
|
31
|
+
const emailAddress = value.emailAddress;
|
|
32
|
+
if (isRecord(emailAddress)) {
|
|
33
|
+
if (typeof emailAddress.name === "string")
|
|
34
|
+
compact.name = emailAddress.name;
|
|
35
|
+
if (typeof emailAddress.address === "string")
|
|
36
|
+
compact.address = emailAddress.address;
|
|
37
|
+
}
|
|
38
|
+
for (const field of ["login", "name", "address", "email", "id"]) {
|
|
39
|
+
if (typeof value[field] === "string" || typeof value[field] === "number") {
|
|
40
|
+
compact[field] = value[field];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return Object.keys(compact).length > 0 ? compact : null;
|
|
44
|
+
}
|
|
45
|
+
function compactObjectValue(value, depth = 2) {
|
|
46
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
47
|
+
return typeof value === "string" ? truncate(value, MAX_FALLBACK_STRING_LENGTH) : value;
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(value)) {
|
|
50
|
+
const compacted = value.slice(0, MAX_OBJECT_ARRAY_ITEMS)
|
|
51
|
+
.map((item) => compactObjectValue(item, depth - 1))
|
|
52
|
+
.filter((item) => item !== null);
|
|
53
|
+
return compacted.length > 0 ? compacted : null;
|
|
54
|
+
}
|
|
55
|
+
if (!isRecord(value) || depth < 0)
|
|
56
|
+
return null;
|
|
57
|
+
const compact = {};
|
|
58
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
59
|
+
const compacted = compactObjectValue(entry, depth - 1);
|
|
60
|
+
if (compacted !== null)
|
|
61
|
+
compact[key] = compacted;
|
|
62
|
+
}
|
|
63
|
+
return Object.keys(compact).length > 0 ? compact : null;
|
|
64
|
+
}
|
|
65
|
+
function compactField(field, value) {
|
|
66
|
+
if (field.kind === "contact")
|
|
67
|
+
return compactContactValue(value);
|
|
68
|
+
if (field.kind === "contact-array") {
|
|
69
|
+
if (!Array.isArray(value))
|
|
70
|
+
return null;
|
|
71
|
+
const contacts = value
|
|
72
|
+
.map((item) => compactContactValue(item))
|
|
73
|
+
.filter((item) => item !== null);
|
|
74
|
+
return contacts.length > 0 ? contacts : null;
|
|
75
|
+
}
|
|
76
|
+
if (field.kind === "string-array") {
|
|
77
|
+
if (!Array.isArray(value))
|
|
78
|
+
return null;
|
|
79
|
+
const strings = value.filter((item) => typeof item === "string");
|
|
80
|
+
return strings.length > 0 ? strings : null;
|
|
81
|
+
}
|
|
82
|
+
if (field.kind === "object") {
|
|
83
|
+
return compactObjectValue(value);
|
|
84
|
+
}
|
|
85
|
+
if (typeof value === "string") {
|
|
86
|
+
return field.maxLength ? truncate(value, field.maxLength) : value;
|
|
87
|
+
}
|
|
88
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
89
|
+
return value;
|
|
90
|
+
if (Array.isArray(value)) {
|
|
91
|
+
const strings = value.filter((item) => typeof item === "string");
|
|
92
|
+
return strings.length > 0 ? strings : null;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
function compactItem(value, fields) {
|
|
97
|
+
if (!isRecord(value))
|
|
98
|
+
return null;
|
|
99
|
+
const compact = {};
|
|
100
|
+
for (const field of fields) {
|
|
101
|
+
const fieldValue = compactField(field, value[field.name]);
|
|
102
|
+
if (fieldValue !== null)
|
|
103
|
+
compact[field.name] = fieldValue;
|
|
104
|
+
}
|
|
105
|
+
return Object.keys(compact).length > 0 ? compact : null;
|
|
106
|
+
}
|
|
107
|
+
function getSummaryItems(result, contract) {
|
|
108
|
+
if (Array.isArray(result))
|
|
109
|
+
return result;
|
|
110
|
+
if (!isRecord(result))
|
|
111
|
+
return null;
|
|
112
|
+
for (const key of contract.collectionKeys) {
|
|
113
|
+
const value = result[key];
|
|
114
|
+
if (Array.isArray(value))
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
if (contract.singleItem)
|
|
118
|
+
return [result];
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
function summarizeWithContract(result, contract) {
|
|
122
|
+
const sourceItems = getSummaryItems(result, contract);
|
|
123
|
+
if (!sourceItems)
|
|
124
|
+
return null;
|
|
125
|
+
const items = sourceItems
|
|
126
|
+
.map((item) => compactItem(item, contract.itemFields))
|
|
127
|
+
.filter((item) => item !== null);
|
|
128
|
+
const summary = {
|
|
129
|
+
[`${contract.collectionName}Count`]: sourceItems.length,
|
|
130
|
+
[contract.collectionName]: items,
|
|
131
|
+
omitted: contract.omitted,
|
|
132
|
+
};
|
|
133
|
+
if (isRecord(result) && contract.outputFields) {
|
|
134
|
+
for (const field of contract.outputFields) {
|
|
135
|
+
const fieldValue = compactField(field, result[field.name]);
|
|
136
|
+
if (fieldValue !== null)
|
|
137
|
+
summary[field.name] = fieldValue;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
summary,
|
|
142
|
+
status: sourceItems.length === 0 ? "empty" : "success",
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function summarizeFallbackRecord(value) {
|
|
146
|
+
const summary = {};
|
|
147
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
148
|
+
if (typeof entry === "string")
|
|
149
|
+
summary[key] = truncate(entry, MAX_FALLBACK_STRING_LENGTH);
|
|
150
|
+
else if (typeof entry === "number" || typeof entry === "boolean")
|
|
151
|
+
summary[key] = entry;
|
|
152
|
+
}
|
|
153
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
154
|
+
if (Array.isArray(entry)) {
|
|
155
|
+
summary[`${key}Count`] = entry.length;
|
|
156
|
+
summary[key] = entry.slice(0, MAX_FALLBACK_ITEMS).map((item) => isRecord(item) ? summarizeFallbackRecord(item) : item);
|
|
157
|
+
if (entry.length > MAX_FALLBACK_ITEMS) {
|
|
158
|
+
summary.omitted = `Only first ${MAX_FALLBACK_ITEMS} ${key} included`;
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return Object.keys(summary).length > 0 ? summary : { keys: Object.keys(value).slice(0, 20) };
|
|
164
|
+
}
|
|
165
|
+
function summarizeFallback(result) {
|
|
166
|
+
if (Array.isArray(result)) {
|
|
167
|
+
return {
|
|
168
|
+
status: result.length === 0 ? "empty" : "success",
|
|
169
|
+
summary: {
|
|
170
|
+
itemsCount: result.length,
|
|
171
|
+
items: result.slice(0, MAX_FALLBACK_ITEMS).map((item) => isRecord(item) ? summarizeFallbackRecord(item) : item),
|
|
172
|
+
...(result.length > MAX_FALLBACK_ITEMS
|
|
173
|
+
? { omitted: `Only first ${MAX_FALLBACK_ITEMS} items included` }
|
|
174
|
+
: {}),
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (isRecord(result)) {
|
|
179
|
+
if ("error" in result) {
|
|
180
|
+
return { status: "error", summary: summarizeFallbackRecord(result) };
|
|
181
|
+
}
|
|
182
|
+
return { status: "success", summary: summarizeFallbackRecord(result) };
|
|
183
|
+
}
|
|
184
|
+
if (typeof result === "string") {
|
|
185
|
+
return {
|
|
186
|
+
status: result.length === 0 ? "empty" : "success",
|
|
187
|
+
summary: truncate(result, MAX_FALLBACK_STRING_LENGTH),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return { status: result == null ? "empty" : "success", summary: result };
|
|
191
|
+
}
|
|
192
|
+
export function summarizeToolResultForCurrentRunState(toolName, result) {
|
|
193
|
+
if (isRecord(result) && "error" in result) {
|
|
194
|
+
return { status: "error", summary: summarizeFallbackRecord(result) };
|
|
195
|
+
}
|
|
196
|
+
const contract = historicalToolSummaries[toolName];
|
|
197
|
+
if (contract) {
|
|
198
|
+
const contracted = summarizeWithContract(result, contract);
|
|
199
|
+
if (contracted)
|
|
200
|
+
return contracted;
|
|
201
|
+
}
|
|
202
|
+
return summarizeFallback(result);
|
|
203
|
+
}
|
|
204
|
+
export function createCurrentRunToolState() {
|
|
205
|
+
return {};
|
|
206
|
+
}
|
|
207
|
+
export function recordCurrentRunToolResult(state, input) {
|
|
208
|
+
const fingerprint = createToolInputFingerprint(input.input);
|
|
209
|
+
const toolBucket = state[input.toolName] ?? { calls: {} };
|
|
210
|
+
const existingCall = toolBucket.calls[fingerprint];
|
|
211
|
+
const { summary, status } = summarizeToolResultForCurrentRunState(input.toolName, input.result);
|
|
212
|
+
toolBucket.calls[fingerprint] = {
|
|
213
|
+
toolCallIds: existingCall?.toolCallIds.includes(input.toolCallId)
|
|
214
|
+
? existingCall.toolCallIds
|
|
215
|
+
: [...(existingCall?.toolCallIds ?? []), input.toolCallId],
|
|
216
|
+
input: input.input ?? {},
|
|
217
|
+
status,
|
|
218
|
+
summary,
|
|
219
|
+
updatedAt: (input.now ?? new Date()).toISOString(),
|
|
220
|
+
};
|
|
221
|
+
state[input.toolName] = toolBucket;
|
|
222
|
+
}
|
|
223
|
+
export function hasCurrentRunToolState(state) {
|
|
224
|
+
return Object.keys(state).some((toolName) => Object.keys(state[toolName]?.calls ?? {}).length > 0);
|
|
225
|
+
}
|
|
226
|
+
export function projectCurrentRunToolStateForPrompt(state) {
|
|
227
|
+
const projected = {};
|
|
228
|
+
for (const [toolName, bucket] of Object.entries(state)) {
|
|
229
|
+
const calls = {};
|
|
230
|
+
for (const [fingerprint, call] of Object.entries(bucket.calls)) {
|
|
231
|
+
calls[fingerprint] = {
|
|
232
|
+
status: call.status,
|
|
233
|
+
summary: call.summary,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
if (Object.keys(calls).length > 0)
|
|
237
|
+
projected[toolName] = { calls };
|
|
238
|
+
}
|
|
239
|
+
return projected;
|
|
240
|
+
}
|
|
241
|
+
export function appendCurrentRunToolStateToSystemPrompt(systemPrompt, state) {
|
|
242
|
+
if (!hasCurrentRunToolState(state))
|
|
243
|
+
return systemPrompt;
|
|
244
|
+
const promptState = projectCurrentRunToolStateForPrompt(state);
|
|
245
|
+
return `${systemPrompt}\n\n<tool_state current_run=\"true\">\n${JSON.stringify(promptState)}\n</tool_state>`;
|
|
246
|
+
}
|
|
@@ -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,EACZ,KAAK,WAAW,EAEhB,KAAK,QAAQ,EAEb,KAAK,cAAc,EACpB,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAU/D,OAAO,EACL,KAAK,eAAe,EAGpB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACzB,MAAM,0BAA0B,CAAC;AAUlC,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC5E,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EACV,8BAA8B,EAC9B,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAC7B,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,EACL,sBAAsB,EACtB,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAC/B,KA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AAI,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"}
|
|
@@ -42,6 +42,7 @@ import { accumulateUsage, getMaxSteps, normalizeInput } from "./input-utils.js";
|
|
|
42
42
|
import { filterToolsForSkill, isToolAllowedBySkill, validateAllowedToolPatterns, } from "../../skill/allowed-tools.js";
|
|
43
43
|
import { resolveConfiguredAgentModel, resolveRuntimeModel } from "./model-resolution.js";
|
|
44
44
|
import { stringifyToolError, throwIfAborted } from "./error-utils.js";
|
|
45
|
+
import { appendCurrentRunToolStateToSystemPrompt, createCurrentRunToolState, recordCurrentRunToolResult, } from "./current-run-tool-state.js";
|
|
45
46
|
const logger = serverLogger.component("agent");
|
|
46
47
|
const LOAD_SKILL_TOOL_ID = "load-skill";
|
|
47
48
|
function isAbortError(error, abortSignal) {
|
|
@@ -504,6 +505,7 @@ export class AgentRuntime {
|
|
|
504
505
|
const languageModel = resolvedModel ?? resolveModel(effectiveModel);
|
|
505
506
|
const toolCalls = [];
|
|
506
507
|
const currentMessages = [...messages];
|
|
508
|
+
const currentRunToolState = createCurrentRunToolState();
|
|
507
509
|
const totalUsage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
508
510
|
// Local models can't reliably do function calling — skip tools gracefully.
|
|
509
511
|
const isLocal = isLocalModelRuntime(languageModel);
|
|
@@ -534,6 +536,7 @@ export class AgentRuntime {
|
|
|
534
536
|
if (activeSkillPolicy) {
|
|
535
537
|
tools = filterToolsForSkill(tools, activeSkillPolicy);
|
|
536
538
|
}
|
|
539
|
+
const modelSystemPrompt = appendCurrentRunToolStateToSystemPrompt(currentSystemPrompt, currentRunToolState);
|
|
537
540
|
const response = await withSpan("agent.generate_text", async (span) => {
|
|
538
541
|
setSpanAttributes(span, {
|
|
539
542
|
"model.id": effectiveModel,
|
|
@@ -541,7 +544,7 @@ export class AgentRuntime {
|
|
|
541
544
|
});
|
|
542
545
|
return generateText({
|
|
543
546
|
model: languageModel,
|
|
544
|
-
system:
|
|
547
|
+
system: modelSystemPrompt,
|
|
545
548
|
messages: convertToTextGenerationRuntimeMessages(currentMessages),
|
|
546
549
|
tools: convertToolsToRuntimeTools(tools, {
|
|
547
550
|
model: effectiveModel,
|
|
@@ -584,12 +587,20 @@ export class AgentRuntime {
|
|
|
584
587
|
currentMessages.push(assistantMessage);
|
|
585
588
|
await this.memory.add(assistantMessage);
|
|
586
589
|
const generatedToolResults = collectGeneratedToolResults(response.toolResults);
|
|
587
|
-
const persistGeneratedToolResult = async (generatedToolResult) => {
|
|
590
|
+
const persistGeneratedToolResult = async (generatedToolResult, input = {}) => {
|
|
588
591
|
const toolResultMessage = createToolResultMessage(generatedToolResult.toolCallId, generatedToolResult.toolName, generatedToolResult.isError === true
|
|
589
592
|
? { error: stringifyToolError(generatedToolResult.result) }
|
|
590
593
|
: generatedToolResult.result);
|
|
591
594
|
currentMessages.push(toolResultMessage);
|
|
592
595
|
await this.memory.add(toolResultMessage);
|
|
596
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
597
|
+
toolCallId: generatedToolResult.toolCallId,
|
|
598
|
+
toolName: generatedToolResult.toolName,
|
|
599
|
+
input,
|
|
600
|
+
result: generatedToolResult.isError === true
|
|
601
|
+
? { error: stringifyToolError(generatedToolResult.result) }
|
|
602
|
+
: generatedToolResult.result,
|
|
603
|
+
});
|
|
593
604
|
};
|
|
594
605
|
if (!response.toolCalls?.length) {
|
|
595
606
|
for (const generatedToolResult of generatedToolResults.values()) {
|
|
@@ -621,7 +632,7 @@ export class AgentRuntime {
|
|
|
621
632
|
await withSpan("agent.tool_execute", async (toolSpan) => {
|
|
622
633
|
setSpanAttributes(toolSpan, { "tool.name": tc.toolName, "tool.id": tc.toolCallId });
|
|
623
634
|
if (generatedToolResult) {
|
|
624
|
-
await persistGeneratedToolResult(generatedToolResult);
|
|
635
|
+
await persistGeneratedToolResult(generatedToolResult, toolCall.args);
|
|
625
636
|
toolCall.status = generatedToolResult.isError === true ? "error" : "completed";
|
|
626
637
|
toolCall.result = generatedToolResult.result;
|
|
627
638
|
toolCall.error = generatedToolResult.isError === true
|
|
@@ -647,6 +658,12 @@ export class AgentRuntime {
|
|
|
647
658
|
};
|
|
648
659
|
currentMessages.push(errorMessage);
|
|
649
660
|
await this.memory.add(errorMessage);
|
|
661
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
662
|
+
toolCallId: tc.toolCallId,
|
|
663
|
+
toolName: tc.toolName,
|
|
664
|
+
input: toolCall.args,
|
|
665
|
+
result: { error: policyCheck.error },
|
|
666
|
+
});
|
|
650
667
|
toolCalls.push(toolCall);
|
|
651
668
|
return;
|
|
652
669
|
}
|
|
@@ -668,6 +685,12 @@ export class AgentRuntime {
|
|
|
668
685
|
result,
|
|
669
686
|
context: executionContext,
|
|
670
687
|
});
|
|
688
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
689
|
+
toolCallId: tc.toolCallId,
|
|
690
|
+
toolName: tc.toolName,
|
|
691
|
+
input: toolCall.args,
|
|
692
|
+
result,
|
|
693
|
+
});
|
|
671
694
|
toolCall.status = "completed";
|
|
672
695
|
toolCall.result = result;
|
|
673
696
|
toolCall.executionTime = Date.now() - startTime;
|
|
@@ -687,6 +710,12 @@ export class AgentRuntime {
|
|
|
687
710
|
const errorMessage = createToolErrorMessage(tc.toolCallId, tc.toolName, toolCall.error);
|
|
688
711
|
currentMessages.push(errorMessage);
|
|
689
712
|
await this.memory.add(errorMessage);
|
|
713
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
714
|
+
toolCallId: tc.toolCallId,
|
|
715
|
+
toolName: tc.toolName,
|
|
716
|
+
input: toolCall.args,
|
|
717
|
+
result: { error: toolCall.error },
|
|
718
|
+
});
|
|
690
719
|
}
|
|
691
720
|
toolCalls.push(toolCall);
|
|
692
721
|
});
|
|
@@ -717,6 +746,7 @@ export class AgentRuntime {
|
|
|
717
746
|
const languageModel = resolvedModel ?? resolveModel(effectiveModel);
|
|
718
747
|
const toolCalls = [];
|
|
719
748
|
const currentMessages = [...messages];
|
|
749
|
+
const currentRunToolState = createCurrentRunToolState();
|
|
720
750
|
const totalUsage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
|
721
751
|
// Local models can't reliably do function calling — skip tools gracefully.
|
|
722
752
|
const isLocalStreaming = isLocalModelRuntime(languageModel);
|
|
@@ -750,9 +780,10 @@ export class AgentRuntime {
|
|
|
750
780
|
if (activeSkillPolicy) {
|
|
751
781
|
tools = filterToolsForSkill(tools, activeSkillPolicy);
|
|
752
782
|
}
|
|
783
|
+
const modelSystemPrompt = appendCurrentRunToolStateToSystemPrompt(currentSystemPrompt, currentRunToolState);
|
|
753
784
|
const result = streamText({
|
|
754
785
|
model: languageModel,
|
|
755
|
-
system:
|
|
786
|
+
system: modelSystemPrompt,
|
|
756
787
|
messages: convertToTextGenerationRuntimeMessages(currentMessages),
|
|
757
788
|
tools: convertToolsToRuntimeTools(tools, {
|
|
758
789
|
model: effectiveModel,
|
|
@@ -826,6 +857,17 @@ export class AgentRuntime {
|
|
|
826
857
|
currentMessages.push(toolResultMessage);
|
|
827
858
|
await this.memory.add(toolResultMessage);
|
|
828
859
|
currentStepToolResults.set(toolResult.toolCallId, toolResultMessage.parts[0]);
|
|
860
|
+
const toolCallInput = state.toolCalls.get(toolResult.toolCallId);
|
|
861
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
862
|
+
toolCallId: toolResult.toolCallId,
|
|
863
|
+
toolName: toolResult.toolName,
|
|
864
|
+
input: toolCallInput?.inputAvailable === true
|
|
865
|
+
? captureStreamedToolCallInput(toolCallInput).args
|
|
866
|
+
: {},
|
|
867
|
+
result: toolResult.error === undefined
|
|
868
|
+
? toolResult.output
|
|
869
|
+
: { error: stringifyToolError(toolResult.error) },
|
|
870
|
+
});
|
|
829
871
|
};
|
|
830
872
|
if (!shouldContinueAfterStreamStep(state)) {
|
|
831
873
|
for (const toolResult of finalToolResults.values()) {
|
|
@@ -855,7 +897,7 @@ export class AgentRuntime {
|
|
|
855
897
|
status: "pending",
|
|
856
898
|
};
|
|
857
899
|
await this.recordToolError(incompleteToolCall, `Stream terminated before tool-call event fired for "${tc.name}". ` +
|
|
858
|
-
`Received ${tc.arguments.length} chars of partial tool-input deltas.`, controller, encoder, currentMessages, toolCalls);
|
|
900
|
+
`Received ${tc.arguments.length} chars of partial tool-input deltas.`, controller, encoder, currentMessages, toolCalls, currentRunToolState);
|
|
859
901
|
continue;
|
|
860
902
|
}
|
|
861
903
|
const capturedInput = captureStreamedToolCallInput(tc);
|
|
@@ -898,12 +940,12 @@ export class AgentRuntime {
|
|
|
898
940
|
errorText: `Invalid tool arguments: ${capturedInput.parseError}`,
|
|
899
941
|
...(dynamic ? { dynamic: true } : {}),
|
|
900
942
|
});
|
|
901
|
-
await this.recordToolError(toolCall, `Invalid tool arguments: ${capturedInput.parseError}`, controller, encoder, currentMessages, toolCalls);
|
|
943
|
+
await this.recordToolError(toolCall, `Invalid tool arguments: ${capturedInput.parseError}`, controller, encoder, currentMessages, toolCalls, currentRunToolState);
|
|
902
944
|
continue;
|
|
903
945
|
}
|
|
904
946
|
const policyCheck = enforceSkillPolicy(tc.name, activeSkillPolicy, mustLoadSkillFirst);
|
|
905
947
|
if (!policyCheck.allowed) {
|
|
906
|
-
await this.recordToolError(toolCall, policyCheck.error, controller, encoder, currentMessages, toolCalls);
|
|
948
|
+
await this.recordToolError(toolCall, policyCheck.error, controller, encoder, currentMessages, toolCalls, currentRunToolState);
|
|
907
949
|
continue;
|
|
908
950
|
}
|
|
909
951
|
try {
|
|
@@ -924,6 +966,12 @@ export class AgentRuntime {
|
|
|
924
966
|
result,
|
|
925
967
|
context: executionContext,
|
|
926
968
|
});
|
|
969
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
970
|
+
toolCallId: tc.id,
|
|
971
|
+
toolName: tc.name,
|
|
972
|
+
input: toolCall.args,
|
|
973
|
+
result,
|
|
974
|
+
});
|
|
927
975
|
toolCall.status = "completed";
|
|
928
976
|
toolCall.result = result;
|
|
929
977
|
toolCall.executionTime = Date.now() - startTime;
|
|
@@ -949,7 +997,7 @@ export class AgentRuntime {
|
|
|
949
997
|
}
|
|
950
998
|
catch (error) {
|
|
951
999
|
const errorStr = error instanceof Error ? error.message : String(error);
|
|
952
|
-
await this.recordToolError(toolCall, errorStr, controller, encoder, currentMessages, toolCalls);
|
|
1000
|
+
await this.recordToolError(toolCall, errorStr, controller, encoder, currentMessages, toolCalls, currentRunToolState);
|
|
953
1001
|
}
|
|
954
1002
|
}
|
|
955
1003
|
for (const toolResult of finalToolResults.values()) {
|
|
@@ -971,7 +1019,7 @@ export class AgentRuntime {
|
|
|
971
1019
|
/**
|
|
972
1020
|
* Record a tool error and send SSE event.
|
|
973
1021
|
*/
|
|
974
|
-
async recordToolError(toolCall, errorStr, controller, encoder, currentMessages, toolCalls) {
|
|
1022
|
+
async recordToolError(toolCall, errorStr, controller, encoder, currentMessages, toolCalls, currentRunToolState) {
|
|
975
1023
|
toolCall.status = "error";
|
|
976
1024
|
toolCall.error = errorStr;
|
|
977
1025
|
toolCalls.push(toolCall);
|
|
@@ -985,6 +1033,14 @@ export class AgentRuntime {
|
|
|
985
1033
|
const errorMessage = createToolErrorMessage(toolCall.id, toolCall.name, errorStr);
|
|
986
1034
|
currentMessages.push(errorMessage);
|
|
987
1035
|
await this.memory.add(errorMessage);
|
|
1036
|
+
if (currentRunToolState) {
|
|
1037
|
+
recordCurrentRunToolResult(currentRunToolState, {
|
|
1038
|
+
toolCallId: toolCall.id,
|
|
1039
|
+
toolName: toolCall.name,
|
|
1040
|
+
input: toolCall.args,
|
|
1041
|
+
result: { error: errorStr },
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
988
1044
|
}
|
|
989
1045
|
/**
|
|
990
1046
|
* Resolve system prompt (handle string or function)
|