@cognipeer/agent-sdk 0.6.6 → 0.7.0
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/index.cjs +324 -166
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -1
- package/dist/index.d.ts +40 -1
- package/dist/index.mjs +324 -166
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -42,7 +42,7 @@ function getTokenCounter() {
|
|
|
42
42
|
function countApproxTokens(text) {
|
|
43
43
|
return activeTokenCounter(text || "");
|
|
44
44
|
}
|
|
45
|
-
function
|
|
45
|
+
function extractMessageText2(m) {
|
|
46
46
|
if (!m) return "";
|
|
47
47
|
const parts = [];
|
|
48
48
|
parts.push(m.role || "");
|
|
@@ -92,7 +92,7 @@ function extractMessageText(m) {
|
|
|
92
92
|
}
|
|
93
93
|
function countMessagesTokens(messages) {
|
|
94
94
|
if (!Array.isArray(messages) || messages.length === 0) return 0;
|
|
95
|
-
const allText = messages.map(
|
|
95
|
+
const allText = messages.map(extractMessageText2).join("\n");
|
|
96
96
|
return countApproxTokens(allText) + messages.length * 4;
|
|
97
97
|
}
|
|
98
98
|
var activeTokenCounter;
|
|
@@ -1262,6 +1262,7 @@ function eventToOtlpSpan(event) {
|
|
|
1262
1262
|
stringAttr("cognipeer.actor.role", event.actor?.role),
|
|
1263
1263
|
stringAttr("cognipeer.model", event.model),
|
|
1264
1264
|
stringAttr("cognipeer.provider", event.provider),
|
|
1265
|
+
stringAttr("cognipeer.tool.details", event.toolDetails ? JSON.stringify(event.toolDetails) : void 0),
|
|
1265
1266
|
intAttr("cognipeer.tokens.input", event.inputTokens),
|
|
1266
1267
|
intAttr("cognipeer.tokens.output", event.outputTokens),
|
|
1267
1268
|
intAttr("cognipeer.tokens.total", event.totalTokens),
|
|
@@ -1733,6 +1734,7 @@ function recordTraceEvent(session, event) {
|
|
|
1733
1734
|
responseBytes,
|
|
1734
1735
|
model: resolvedModel,
|
|
1735
1736
|
provider: resolvedProvider,
|
|
1737
|
+
toolDetails: session.resolvedConfig.logData ? sanitizeTracePayload(event.toolDetails) : void 0,
|
|
1736
1738
|
toolExecutionId: event.toolExecutionId,
|
|
1737
1739
|
retryOf: event.retryOf,
|
|
1738
1740
|
error: event.error ?? (status === "error" ? { message: "Unknown error" } : void 0) ?? void 0,
|
|
@@ -2183,16 +2185,99 @@ function createAgentCoreNode(opts) {
|
|
|
2183
2185
|
};
|
|
2184
2186
|
}
|
|
2185
2187
|
|
|
2188
|
+
// src/utils/content.ts
|
|
2189
|
+
function contentToString(content) {
|
|
2190
|
+
try {
|
|
2191
|
+
if (content == null) return "";
|
|
2192
|
+
if (typeof content === "string") return content;
|
|
2193
|
+
if (Array.isArray(content)) {
|
|
2194
|
+
return content.map((p) => {
|
|
2195
|
+
if (!p || typeof p !== "object") return String(p);
|
|
2196
|
+
if (p.type === "text") return String(p.text ?? "");
|
|
2197
|
+
if (p.type === "image_url") {
|
|
2198
|
+
const img = p.image_url;
|
|
2199
|
+
const url = typeof img === "string" ? img : img?.url;
|
|
2200
|
+
const detail = typeof img === "string" ? void 0 : img?.detail;
|
|
2201
|
+
return `[image:${url || "unknown"}${detail ? ` detail=${detail}` : ""}]`;
|
|
2202
|
+
}
|
|
2203
|
+
return JSON.stringify(p);
|
|
2204
|
+
}).join("\n");
|
|
2205
|
+
}
|
|
2206
|
+
return JSON.stringify(content);
|
|
2207
|
+
} catch {
|
|
2208
|
+
return String(content);
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
function mergeContentsToString(contents) {
|
|
2212
|
+
return contents.map((c) => contentToString(c)).join("\n");
|
|
2213
|
+
}
|
|
2214
|
+
function safeStringify(value, pretty = false) {
|
|
2215
|
+
if (value == null) return "";
|
|
2216
|
+
if (typeof value === "string") return value;
|
|
2217
|
+
try {
|
|
2218
|
+
return JSON.stringify(value, null, pretty ? 2 : void 0) ?? String(value);
|
|
2219
|
+
} catch {
|
|
2220
|
+
return String(value);
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
function extractMessageText(message) {
|
|
2224
|
+
if (!message) return "";
|
|
2225
|
+
const content = message.content;
|
|
2226
|
+
if (typeof content === "string") return content;
|
|
2227
|
+
if (Array.isArray(content)) {
|
|
2228
|
+
return content.map((part) => typeof part === "string" ? part : part?.text ?? part?.content ?? "").join("");
|
|
2229
|
+
}
|
|
2230
|
+
return "";
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
// src/utils/traceSections.ts
|
|
2234
|
+
function truncate(text, max = 500) {
|
|
2235
|
+
if (!text) return "";
|
|
2236
|
+
return text.length > max ? `${text.slice(0, max)}\u2026` : text;
|
|
2237
|
+
}
|
|
2238
|
+
function summarizeToolOutput(output) {
|
|
2239
|
+
if (output == null) return "";
|
|
2240
|
+
if (typeof output === "string") return truncate(output.trim());
|
|
2241
|
+
if (Array.isArray(output)) {
|
|
2242
|
+
const preview = output.slice(0, 3).map((item, index) => `${index + 1}. ${truncate(safeStringify(item))}`).join("\n");
|
|
2243
|
+
const suffix = output.length > 3 ? `
|
|
2244
|
+
\u2026 (+${output.length - 3} more)` : "";
|
|
2245
|
+
return `${preview}${suffix}`.trim();
|
|
2246
|
+
}
|
|
2247
|
+
if (typeof output === "object") {
|
|
2248
|
+
if (Array.isArray(output.items)) {
|
|
2249
|
+
return summarizeToolOutput(output.items);
|
|
2250
|
+
}
|
|
2251
|
+
return truncate(safeStringify(output));
|
|
2252
|
+
}
|
|
2253
|
+
return truncate(String(output));
|
|
2254
|
+
}
|
|
2255
|
+
function extractToolItems(output) {
|
|
2256
|
+
const source = Array.isArray(output) ? output : Array.isArray(output?.items) ? output.items : void 0;
|
|
2257
|
+
if (!Array.isArray(source) || source.length === 0) return void 0;
|
|
2258
|
+
return source.slice(0, 5).map((item) => {
|
|
2259
|
+
if (item && typeof item === "object") {
|
|
2260
|
+
const snippetSource = item.content ?? item.snippet ?? item.description ?? item.text ?? item.summary ?? item;
|
|
2261
|
+
return {
|
|
2262
|
+
title: typeof item.title === "string" ? item.title : void 0,
|
|
2263
|
+
url: typeof item.url === "string" ? item.url : void 0,
|
|
2264
|
+
snippet: truncate(safeStringify(snippetSource))
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
return { snippet: truncate(safeStringify(item)) };
|
|
2268
|
+
});
|
|
2269
|
+
}
|
|
2270
|
+
|
|
2186
2271
|
// src/smart/runtimeConfig.ts
|
|
2187
2272
|
var DEFAULT_CRITICAL_TOOLS = ["response", "manage_todo_list", "get_tool_response"];
|
|
2188
2273
|
var BASE_DEFAULTS = {
|
|
2189
|
-
limits: { maxToolCalls:
|
|
2274
|
+
limits: { maxToolCalls: 20, maxParallelTools: 5, maxContextTokens: 96e3, maxTotalOutputTokens: 0, maxCostUsd: 0, maxWallClockMs: 0 },
|
|
2190
2275
|
summarization: {
|
|
2191
2276
|
enable: true,
|
|
2192
|
-
maxTokens:
|
|
2193
|
-
summaryTriggerTokens:
|
|
2194
|
-
summaryPromptMaxTokens:
|
|
2195
|
-
summaryCompressionRatioTarget: 0.
|
|
2277
|
+
maxTokens: 96e3,
|
|
2278
|
+
summaryTriggerTokens: 72e3,
|
|
2279
|
+
summaryPromptMaxTokens: 12e3,
|
|
2280
|
+
summaryCompressionRatioTarget: 0.45,
|
|
2196
2281
|
summaryMode: "incremental",
|
|
2197
2282
|
promptTemplate: "",
|
|
2198
2283
|
toolFreeCall: true,
|
|
@@ -2200,13 +2285,13 @@ var BASE_DEFAULTS = {
|
|
|
2200
2285
|
},
|
|
2201
2286
|
context: {
|
|
2202
2287
|
policy: "hybrid",
|
|
2203
|
-
lastTurnsToKeep:
|
|
2288
|
+
lastTurnsToKeep: 16,
|
|
2204
2289
|
toolResponsePolicy: "summarize_archive",
|
|
2205
2290
|
budget: {
|
|
2206
|
-
systemReserveTokens:
|
|
2207
|
-
goalsReserveTokens:
|
|
2208
|
-
recentTurnsReserveTokens:
|
|
2209
|
-
toolResponseReserveTokens:
|
|
2291
|
+
systemReserveTokens: 2e3,
|
|
2292
|
+
goalsReserveTokens: 2e3,
|
|
2293
|
+
recentTurnsReserveTokens: 18e3,
|
|
2294
|
+
toolResponseReserveTokens: 9e3
|
|
2210
2295
|
}
|
|
2211
2296
|
},
|
|
2212
2297
|
planning: {
|
|
@@ -2223,14 +2308,14 @@ var BASE_DEFAULTS = {
|
|
|
2223
2308
|
delegation: {
|
|
2224
2309
|
mode: "role_based",
|
|
2225
2310
|
maxDelegationDepth: 2,
|
|
2226
|
-
maxChildCalls:
|
|
2227
|
-
maxParallelChild:
|
|
2311
|
+
maxChildCalls: 6,
|
|
2312
|
+
maxParallelChild: 3,
|
|
2228
2313
|
childContextPolicy: "scoped",
|
|
2229
2314
|
requireJsonOutputContract: true
|
|
2230
2315
|
},
|
|
2231
2316
|
toolResponses: {
|
|
2232
|
-
maxToolResponseChars:
|
|
2233
|
-
maxToolResponseTokens:
|
|
2317
|
+
maxToolResponseChars: 32e3,
|
|
2318
|
+
maxToolResponseTokens: 8e3,
|
|
2234
2319
|
defaultPolicy: "summarize_archive",
|
|
2235
2320
|
toolResponseRetentionByTool: {},
|
|
2236
2321
|
criticalTools: [...DEFAULT_CRITICAL_TOOLS],
|
|
@@ -2259,54 +2344,54 @@ function buildProfile(overrides) {
|
|
|
2259
2344
|
}
|
|
2260
2345
|
var DEFAULT_PROFILE_CONFIGS = {
|
|
2261
2346
|
fast: buildProfile({
|
|
2262
|
-
limits: { maxToolCalls:
|
|
2347
|
+
limits: { maxToolCalls: 8, maxParallelTools: 3, maxContextTokens: 32e3 },
|
|
2263
2348
|
summarization: {
|
|
2264
|
-
maxTokens:
|
|
2265
|
-
summaryTriggerTokens:
|
|
2266
|
-
summaryPromptMaxTokens:
|
|
2267
|
-
summaryCompressionRatioTarget: 0.
|
|
2349
|
+
maxTokens: 32e3,
|
|
2350
|
+
summaryTriggerTokens: 24e3,
|
|
2351
|
+
summaryPromptMaxTokens: 6e3,
|
|
2352
|
+
summaryCompressionRatioTarget: 0.5
|
|
2268
2353
|
},
|
|
2269
2354
|
context: {
|
|
2270
|
-
lastTurnsToKeep:
|
|
2355
|
+
lastTurnsToKeep: 8,
|
|
2271
2356
|
toolResponsePolicy: "keep_structured",
|
|
2272
|
-
budget: { systemReserveTokens:
|
|
2357
|
+
budget: { systemReserveTokens: 1500, goalsReserveTokens: 1200, recentTurnsReserveTokens: 6e3, toolResponseReserveTokens: 3500 }
|
|
2273
2358
|
},
|
|
2274
2359
|
memory: { readPolicy: "recent_only" },
|
|
2275
2360
|
delegation: { mode: "off", maxDelegationDepth: 1, maxChildCalls: 2, maxParallelChild: 1, childContextPolicy: "minimal" },
|
|
2276
|
-
toolResponses: { maxToolResponseChars:
|
|
2361
|
+
toolResponses: { maxToolResponseChars: 16e3, maxToolResponseTokens: 4e3 }
|
|
2277
2362
|
}),
|
|
2278
2363
|
balanced: buildProfile({}),
|
|
2279
2364
|
deep: buildProfile({
|
|
2280
|
-
limits: { maxToolCalls:
|
|
2365
|
+
limits: { maxToolCalls: 40, maxParallelTools: 8, maxContextTokens: 2e5 },
|
|
2281
2366
|
summarization: {
|
|
2282
|
-
maxTokens:
|
|
2283
|
-
summaryTriggerTokens:
|
|
2284
|
-
summaryPromptMaxTokens:
|
|
2285
|
-
summaryCompressionRatioTarget: 0.
|
|
2367
|
+
maxTokens: 2e5,
|
|
2368
|
+
summaryTriggerTokens: 15e4,
|
|
2369
|
+
summaryPromptMaxTokens: 16e3,
|
|
2370
|
+
summaryCompressionRatioTarget: 0.4
|
|
2286
2371
|
},
|
|
2287
2372
|
context: {
|
|
2288
|
-
lastTurnsToKeep:
|
|
2289
|
-
budget: { systemReserveTokens:
|
|
2373
|
+
lastTurnsToKeep: 30,
|
|
2374
|
+
budget: { systemReserveTokens: 2500, goalsReserveTokens: 2500, recentTurnsReserveTokens: 36e3, toolResponseReserveTokens: 18e3 }
|
|
2290
2375
|
},
|
|
2291
2376
|
memory: { scope: "workspace" },
|
|
2292
|
-
delegation: { maxDelegationDepth: 3, maxChildCalls:
|
|
2293
|
-
toolResponses: { maxToolResponseChars:
|
|
2377
|
+
delegation: { maxDelegationDepth: 3, maxChildCalls: 10, maxParallelChild: 4 },
|
|
2378
|
+
toolResponses: { maxToolResponseChars: 64e3, maxToolResponseTokens: 16e3 }
|
|
2294
2379
|
}),
|
|
2295
2380
|
research: buildProfile({
|
|
2296
|
-
limits: { maxToolCalls:
|
|
2381
|
+
limits: { maxToolCalls: 80, maxParallelTools: 10, maxContextTokens: 4e5 },
|
|
2297
2382
|
summarization: {
|
|
2298
|
-
maxTokens:
|
|
2299
|
-
summaryTriggerTokens:
|
|
2300
|
-
summaryPromptMaxTokens:
|
|
2301
|
-
summaryCompressionRatioTarget: 0.
|
|
2383
|
+
maxTokens: 4e5,
|
|
2384
|
+
summaryTriggerTokens: 3e5,
|
|
2385
|
+
summaryPromptMaxTokens: 24e3,
|
|
2386
|
+
summaryCompressionRatioTarget: 0.35
|
|
2302
2387
|
},
|
|
2303
2388
|
context: {
|
|
2304
|
-
lastTurnsToKeep:
|
|
2305
|
-
budget: { systemReserveTokens:
|
|
2389
|
+
lastTurnsToKeep: 60,
|
|
2390
|
+
budget: { systemReserveTokens: 3e3, goalsReserveTokens: 3e3, recentTurnsReserveTokens: 72e3, toolResponseReserveTokens: 32e3 }
|
|
2306
2391
|
},
|
|
2307
2392
|
memory: { scope: "workspace" },
|
|
2308
|
-
delegation: { mode: "automatic", maxDelegationDepth: 4, maxChildCalls:
|
|
2309
|
-
toolResponses: { maxToolResponseChars:
|
|
2393
|
+
delegation: { mode: "automatic", maxDelegationDepth: 4, maxChildCalls: 16, maxParallelChild: 6, childContextPolicy: "full" },
|
|
2394
|
+
toolResponses: { maxToolResponseChars: 96e3, maxToolResponseTokens: 24e3 }
|
|
2310
2395
|
})
|
|
2311
2396
|
};
|
|
2312
2397
|
function mergeLimits(base, override) {
|
|
@@ -2396,53 +2481,6 @@ function getResolvedSmartConfig(opts, runtime) {
|
|
|
2396
2481
|
|
|
2397
2482
|
// src/smart/toolResponses.ts
|
|
2398
2483
|
init_utilTokens();
|
|
2399
|
-
|
|
2400
|
-
// src/utils/content.ts
|
|
2401
|
-
function contentToString(content) {
|
|
2402
|
-
try {
|
|
2403
|
-
if (content == null) return "";
|
|
2404
|
-
if (typeof content === "string") return content;
|
|
2405
|
-
if (Array.isArray(content)) {
|
|
2406
|
-
return content.map((p) => {
|
|
2407
|
-
if (!p || typeof p !== "object") return String(p);
|
|
2408
|
-
if (p.type === "text") return String(p.text ?? "");
|
|
2409
|
-
if (p.type === "image_url") {
|
|
2410
|
-
const img = p.image_url;
|
|
2411
|
-
const url = typeof img === "string" ? img : img?.url;
|
|
2412
|
-
const detail = typeof img === "string" ? void 0 : img?.detail;
|
|
2413
|
-
return `[image:${url || "unknown"}${detail ? ` detail=${detail}` : ""}]`;
|
|
2414
|
-
}
|
|
2415
|
-
return JSON.stringify(p);
|
|
2416
|
-
}).join("\n");
|
|
2417
|
-
}
|
|
2418
|
-
return JSON.stringify(content);
|
|
2419
|
-
} catch {
|
|
2420
|
-
return String(content);
|
|
2421
|
-
}
|
|
2422
|
-
}
|
|
2423
|
-
function mergeContentsToString(contents) {
|
|
2424
|
-
return contents.map((c) => contentToString(c)).join("\n");
|
|
2425
|
-
}
|
|
2426
|
-
function safeStringify(value, pretty = false) {
|
|
2427
|
-
if (value == null) return "";
|
|
2428
|
-
if (typeof value === "string") return value;
|
|
2429
|
-
try {
|
|
2430
|
-
return JSON.stringify(value, null, pretty ? 2 : void 0) ?? String(value);
|
|
2431
|
-
} catch {
|
|
2432
|
-
return String(value);
|
|
2433
|
-
}
|
|
2434
|
-
}
|
|
2435
|
-
function extractMessageText2(message) {
|
|
2436
|
-
if (!message) return "";
|
|
2437
|
-
const content = message.content;
|
|
2438
|
-
if (typeof content === "string") return content;
|
|
2439
|
-
if (Array.isArray(content)) {
|
|
2440
|
-
return content.map((part) => typeof part === "string" ? part : part?.text ?? part?.content ?? "").join("");
|
|
2441
|
-
}
|
|
2442
|
-
return "";
|
|
2443
|
-
}
|
|
2444
|
-
|
|
2445
|
-
// src/smart/toolResponses.ts
|
|
2446
2484
|
function summarizeObject(value) {
|
|
2447
2485
|
if (value == null) return "null";
|
|
2448
2486
|
if (typeof value === "string") {
|
|
@@ -2557,6 +2595,97 @@ function normalizeToolCall(call) {
|
|
|
2557
2595
|
args: call.args ?? call.arguments ?? call.input ?? call.function?.arguments ?? call.function_call?.arguments
|
|
2558
2596
|
};
|
|
2559
2597
|
}
|
|
2598
|
+
function normalizeToolSchema(tool, toolName) {
|
|
2599
|
+
const schema = tool.schema;
|
|
2600
|
+
if (!schema) return void 0;
|
|
2601
|
+
if (typeof schema === "object" && schema._def) {
|
|
2602
|
+
try {
|
|
2603
|
+
return sanitizeTracePayload(zodToJsonSchema(schema, { name: toolName }));
|
|
2604
|
+
} catch {
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
if (schema && typeof schema === "object" && typeof schema.toJSON === "function") {
|
|
2608
|
+
try {
|
|
2609
|
+
return sanitizeTracePayload(schema.toJSON());
|
|
2610
|
+
} catch {
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
return sanitizeTracePayload(schema);
|
|
2614
|
+
}
|
|
2615
|
+
function buildToolDetails(tool, toolName) {
|
|
2616
|
+
if (!tool) return void 0;
|
|
2617
|
+
const details = { name: toolName };
|
|
2618
|
+
if (typeof tool.description === "string" && tool.description.trim().length > 0) {
|
|
2619
|
+
details.description = tool.description;
|
|
2620
|
+
}
|
|
2621
|
+
const inputSchema = normalizeToolSchema(tool, toolName);
|
|
2622
|
+
if (inputSchema !== void 0) details.inputSchema = inputSchema;
|
|
2623
|
+
const needsApproval = typeof tool.needsApproval === "boolean" ? tool.needsApproval : void 0;
|
|
2624
|
+
if (needsApproval !== void 0 || tool.approvalPrompt !== void 0 || tool.approvalDefaults !== void 0) {
|
|
2625
|
+
details.approval = {
|
|
2626
|
+
required: needsApproval,
|
|
2627
|
+
prompt: typeof tool.approvalPrompt === "string" ? tool.approvalPrompt : void 0,
|
|
2628
|
+
defaults: tool.approvalDefaults !== void 0 ? sanitizeTracePayload(tool.approvalDefaults) : void 0
|
|
2629
|
+
};
|
|
2630
|
+
}
|
|
2631
|
+
const cache = tool.cache;
|
|
2632
|
+
if (cache !== void 0) {
|
|
2633
|
+
details.cache = {
|
|
2634
|
+
enabled: Boolean(cache),
|
|
2635
|
+
scope: cache && typeof cache === "object" && typeof cache.scope === "string" ? cache.scope : void 0,
|
|
2636
|
+
ttlMs: cache && typeof cache === "object" && typeof cache.ttlMs === "number" ? cache.ttlMs : void 0,
|
|
2637
|
+
hasKeyFn: Boolean(cache && typeof cache === "object" && typeof cache.keyFn === "function")
|
|
2638
|
+
};
|
|
2639
|
+
}
|
|
2640
|
+
const retry = tool.retry;
|
|
2641
|
+
if (retry && typeof retry === "object") {
|
|
2642
|
+
details.retry = {
|
|
2643
|
+
maxRetries: typeof retry.maxRetries === "number" ? retry.maxRetries : void 0,
|
|
2644
|
+
backoffMs: typeof retry.backoffMs === "number" ? retry.backoffMs : void 0,
|
|
2645
|
+
circuitBreakerThreshold: typeof retry.circuitBreakerThreshold === "number" ? retry.circuitBreakerThreshold : void 0,
|
|
2646
|
+
hasShouldRetry: typeof retry.shouldRetry === "function"
|
|
2647
|
+
};
|
|
2648
|
+
}
|
|
2649
|
+
if (tool.maxExecutionsPerRun !== void 0) {
|
|
2650
|
+
details.limits = { maxExecutionsPerRun: tool.maxExecutionsPerRun };
|
|
2651
|
+
}
|
|
2652
|
+
if (typeof tool.__source === "string") {
|
|
2653
|
+
details.source = tool.__source;
|
|
2654
|
+
}
|
|
2655
|
+
return details;
|
|
2656
|
+
}
|
|
2657
|
+
function buildToolTraceSections(params) {
|
|
2658
|
+
const sanitizedArgs = sanitizeTracePayload(params.args ?? {});
|
|
2659
|
+
const sanitizedOutput = sanitizeTracePayload(params.output);
|
|
2660
|
+
const summary = params.summary ?? summarizeToolOutput(sanitizedOutput);
|
|
2661
|
+
const items = extractToolItems(sanitizedOutput);
|
|
2662
|
+
return [
|
|
2663
|
+
{
|
|
2664
|
+
kind: "tool_call",
|
|
2665
|
+
label: `Tool Call: ${params.toolName}`,
|
|
2666
|
+
tool: params.toolName,
|
|
2667
|
+
arguments: sanitizedArgs,
|
|
2668
|
+
toolDetails: params.toolDetails
|
|
2669
|
+
},
|
|
2670
|
+
{
|
|
2671
|
+
kind: "tool_result",
|
|
2672
|
+
label: `Tool Result: ${params.toolName}`,
|
|
2673
|
+
tool: params.toolName,
|
|
2674
|
+
summary,
|
|
2675
|
+
items,
|
|
2676
|
+
output: sanitizedOutput,
|
|
2677
|
+
toolDetails: params.toolDetails,
|
|
2678
|
+
execution: {
|
|
2679
|
+
id: params.executionId,
|
|
2680
|
+
status: params.status,
|
|
2681
|
+
durationMs: params.durationMs,
|
|
2682
|
+
fromCache: params.fromCache
|
|
2683
|
+
},
|
|
2684
|
+
classification: params.classification,
|
|
2685
|
+
retentionPolicy: params.retentionPolicy
|
|
2686
|
+
}
|
|
2687
|
+
];
|
|
2688
|
+
}
|
|
2560
2689
|
function createToolsNode(initialTools, opts) {
|
|
2561
2690
|
return async (state) => {
|
|
2562
2691
|
const runtime = state.agent || {
|
|
@@ -2665,6 +2794,7 @@ function createToolsNode(initialTools, opts) {
|
|
|
2665
2794
|
args = validatedArgs.value;
|
|
2666
2795
|
}
|
|
2667
2796
|
const toolName = tool.name || tc.name;
|
|
2797
|
+
const toolDetails = buildToolDetails(tool, toolName);
|
|
2668
2798
|
const toolStateRef = tool._stateRef && typeof tool._stateRef === "object" ? tool._stateRef : null;
|
|
2669
2799
|
if (toolStateRef) {
|
|
2670
2800
|
toolStateRef.pendingApprovals = pendingApprovals;
|
|
@@ -2685,21 +2815,17 @@ function createToolsNode(initialTools, opts) {
|
|
|
2685
2815
|
label: `Tool Skipped - ${toolName}`,
|
|
2686
2816
|
actor: { scope: "tool", name: toolName, role: "tool" },
|
|
2687
2817
|
status: "skipped",
|
|
2818
|
+
toolDetails,
|
|
2688
2819
|
toolExecutionId: tc.id,
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
function: { name: toolName, arguments: sanitizeTracePayload(args) }
|
|
2699
|
-
}
|
|
2700
|
-
]
|
|
2701
|
-
}
|
|
2702
|
-
]
|
|
2820
|
+
sections: buildToolTraceSections({
|
|
2821
|
+
args,
|
|
2822
|
+
executionId: tc.id,
|
|
2823
|
+
output: limitMessage,
|
|
2824
|
+
status: "skipped",
|
|
2825
|
+
summary: limitMessage,
|
|
2826
|
+
toolDetails,
|
|
2827
|
+
toolName
|
|
2828
|
+
})
|
|
2703
2829
|
});
|
|
2704
2830
|
toolCount += 1;
|
|
2705
2831
|
return { status: "error" };
|
|
@@ -2786,6 +2912,25 @@ function createToolsNode(initialTools, opts) {
|
|
|
2786
2912
|
push({ role: "tool", content: breakerMsg, tool_call_id: toolCallId, name: tc.name });
|
|
2787
2913
|
markToolFailure(breakerMsg, toolName, toolCallId);
|
|
2788
2914
|
onEvent?.({ type: "tool_call", phase: "error", name: toolName, id: tc.id, args, error: { message: breakerMsg } });
|
|
2915
|
+
recordTraceEvent(traceSession, {
|
|
2916
|
+
type: "tool_call",
|
|
2917
|
+
label: `Tool Error - ${toolName}`,
|
|
2918
|
+
actor: { scope: "tool", name: toolName, role: "tool" },
|
|
2919
|
+
status: "error",
|
|
2920
|
+
requestBytes: inputBytes,
|
|
2921
|
+
toolDetails,
|
|
2922
|
+
toolExecutionId: toolCallId,
|
|
2923
|
+
error: { message: breakerMsg },
|
|
2924
|
+
sections: buildToolTraceSections({
|
|
2925
|
+
args,
|
|
2926
|
+
executionId: toolCallId,
|
|
2927
|
+
output: { message: breakerMsg },
|
|
2928
|
+
status: "error",
|
|
2929
|
+
summary: breakerMsg,
|
|
2930
|
+
toolDetails,
|
|
2931
|
+
toolName
|
|
2932
|
+
})
|
|
2933
|
+
});
|
|
2789
2934
|
toolCount += 1;
|
|
2790
2935
|
return { status: "error" };
|
|
2791
2936
|
}
|
|
@@ -2810,6 +2955,38 @@ function createToolsNode(initialTools, opts) {
|
|
|
2810
2955
|
});
|
|
2811
2956
|
push({ role: "tool", content: responsePolicy2.content, tool_call_id: tc.id || `${tc.name}_${toolCount}`, name: tc.name });
|
|
2812
2957
|
onEvent?.({ type: "tool_call", phase: "success", name: toolName, id: tc.id, args, result: cachedHit, durationMs: 0 });
|
|
2958
|
+
const sanitizedCachedHit = sanitizeTracePayload(cachedHit);
|
|
2959
|
+
const outputBytes2 = traceSession?.resolvedConfig.logData ? estimatePayloadBytes(sanitizedCachedHit) : void 0;
|
|
2960
|
+
recordTraceEvent(traceSession, {
|
|
2961
|
+
type: "tool_call",
|
|
2962
|
+
label: `Tool Execution - ${toolName}`,
|
|
2963
|
+
actor: { scope: "tool", name: toolName, role: "tool" },
|
|
2964
|
+
durationMs: 0,
|
|
2965
|
+
requestBytes: inputBytes,
|
|
2966
|
+
responseBytes: outputBytes2,
|
|
2967
|
+
toolDetails,
|
|
2968
|
+
toolExecutionId: executionId2,
|
|
2969
|
+
sections: buildToolTraceSections({
|
|
2970
|
+
args,
|
|
2971
|
+
classification: responsePolicy2.classification,
|
|
2972
|
+
durationMs: 0,
|
|
2973
|
+
executionId: executionId2,
|
|
2974
|
+
fromCache: true,
|
|
2975
|
+
output: sanitizedCachedHit,
|
|
2976
|
+
retentionPolicy: "keep_full",
|
|
2977
|
+
status: "cached",
|
|
2978
|
+
summary: responsePolicy2.content,
|
|
2979
|
+
toolDetails,
|
|
2980
|
+
toolName
|
|
2981
|
+
}),
|
|
2982
|
+
debug: {
|
|
2983
|
+
classification: responsePolicy2.classification,
|
|
2984
|
+
retentionPolicy: "keep_full",
|
|
2985
|
+
originalTokenCount: responsePolicy2.tokenCount,
|
|
2986
|
+
truncated: responsePolicy2.truncated,
|
|
2987
|
+
fromCache: true
|
|
2988
|
+
}
|
|
2989
|
+
});
|
|
2813
2990
|
toolCount += 1;
|
|
2814
2991
|
return { status: "success" };
|
|
2815
2992
|
}
|
|
@@ -2930,28 +3107,6 @@ function createToolsNode(initialTools, opts) {
|
|
|
2930
3107
|
}
|
|
2931
3108
|
const sanitizedOutput = sanitizeTracePayload(output);
|
|
2932
3109
|
const outputBytes = traceSession?.resolvedConfig.logData ? estimatePayloadBytes(sanitizedOutput) : void 0;
|
|
2933
|
-
const messageList = [
|
|
2934
|
-
{
|
|
2935
|
-
role: "assistant",
|
|
2936
|
-
name: toolName,
|
|
2937
|
-
content: "",
|
|
2938
|
-
tool_calls: [
|
|
2939
|
-
{
|
|
2940
|
-
id: tc.id || executionId,
|
|
2941
|
-
type: "function",
|
|
2942
|
-
function: {
|
|
2943
|
-
name: toolName,
|
|
2944
|
-
arguments: sanitizedArgs
|
|
2945
|
-
}
|
|
2946
|
-
}
|
|
2947
|
-
]
|
|
2948
|
-
},
|
|
2949
|
-
{
|
|
2950
|
-
role: "tool",
|
|
2951
|
-
name: toolName,
|
|
2952
|
-
content: responsePolicy.content
|
|
2953
|
-
}
|
|
2954
|
-
];
|
|
2955
3110
|
recordTraceEvent(traceSession, {
|
|
2956
3111
|
type: "tool_call",
|
|
2957
3112
|
label: `Tool Execution - ${toolName}`,
|
|
@@ -2959,8 +3114,20 @@ function createToolsNode(initialTools, opts) {
|
|
|
2959
3114
|
durationMs,
|
|
2960
3115
|
requestBytes: inputBytes,
|
|
2961
3116
|
responseBytes: outputBytes,
|
|
3117
|
+
toolDetails,
|
|
2962
3118
|
toolExecutionId: executionId,
|
|
2963
|
-
|
|
3119
|
+
sections: buildToolTraceSections({
|
|
3120
|
+
args: sanitizedArgs,
|
|
3121
|
+
classification: responsePolicy.classification,
|
|
3122
|
+
durationMs,
|
|
3123
|
+
executionId,
|
|
3124
|
+
output: sanitizedOutput,
|
|
3125
|
+
retentionPolicy: "keep_full",
|
|
3126
|
+
status: "success",
|
|
3127
|
+
summary: responsePolicy.content,
|
|
3128
|
+
toolDetails,
|
|
3129
|
+
toolName
|
|
3130
|
+
}),
|
|
2964
3131
|
debug: {
|
|
2965
3132
|
classification: responsePolicy.classification,
|
|
2966
3133
|
retentionPolicy: "keep_full",
|
|
@@ -2979,6 +3146,7 @@ function createToolsNode(initialTools, opts) {
|
|
|
2979
3146
|
markToolFailure(message, toolName, toolCallId);
|
|
2980
3147
|
onEvent?.({ type: "tool_call", phase: "error", name: toolName, id: tc.id, args, error: { message } });
|
|
2981
3148
|
onProgress?.({ stage: "tools", message: `Tool ${tc.name} failed`, detail: { error: message } });
|
|
3149
|
+
const errorOutput = { message, stack: error?.stack };
|
|
2982
3150
|
recordTraceEvent(traceSession, {
|
|
2983
3151
|
type: "tool_call",
|
|
2984
3152
|
label: `Tool Error - ${toolName}`,
|
|
@@ -2986,27 +3154,20 @@ function createToolsNode(initialTools, opts) {
|
|
|
2986
3154
|
status: "error",
|
|
2987
3155
|
durationMs,
|
|
2988
3156
|
requestBytes: inputBytes,
|
|
3157
|
+
responseBytes: traceSession?.resolvedConfig.logData ? estimatePayloadBytes(errorOutput) : void 0,
|
|
3158
|
+
toolDetails,
|
|
2989
3159
|
toolExecutionId: executionId,
|
|
2990
3160
|
error: { message, stack: error?.stack },
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
}
|
|
3002
|
-
]
|
|
3003
|
-
},
|
|
3004
|
-
{
|
|
3005
|
-
role: "tool",
|
|
3006
|
-
name: toolName,
|
|
3007
|
-
content: `Error executing tool: ${message}`
|
|
3008
|
-
}
|
|
3009
|
-
]
|
|
3161
|
+
sections: buildToolTraceSections({
|
|
3162
|
+
args: sanitizedArgs,
|
|
3163
|
+
durationMs,
|
|
3164
|
+
executionId,
|
|
3165
|
+
output: errorOutput,
|
|
3166
|
+
status: "error",
|
|
3167
|
+
summary: `Error executing tool: ${message}`,
|
|
3168
|
+
toolDetails,
|
|
3169
|
+
toolName
|
|
3170
|
+
})
|
|
3010
3171
|
});
|
|
3011
3172
|
toolCount += 1;
|
|
3012
3173
|
return { status: "error", approval: approvalEntry };
|
|
@@ -3083,29 +3244,26 @@ function createToolsNode(initialTools, opts) {
|
|
|
3083
3244
|
}
|
|
3084
3245
|
for (const tc of skipped) {
|
|
3085
3246
|
onEvent?.({ type: "tool_call", phase: "skipped", name: tc.name, id: tc.id, args: tc.args });
|
|
3086
|
-
const
|
|
3247
|
+
const toolDetails = buildToolDetails(toolByName.get(tc.name), tc.name);
|
|
3248
|
+
const skipMessage = `Skipped tool due to tool-call limit: ${tc.name}`;
|
|
3087
3249
|
recordTraceEvent(traceSession, {
|
|
3088
3250
|
type: "tool_call",
|
|
3089
3251
|
label: `Tool Skipped - ${tc.name}`,
|
|
3090
3252
|
actor: { scope: "tool", name: tc.name, role: "tool" },
|
|
3091
3253
|
status: "skipped",
|
|
3254
|
+
toolDetails,
|
|
3092
3255
|
toolExecutionId: tc.id,
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
function: { name: tc.name, arguments: sanitizedArgs }
|
|
3103
|
-
}
|
|
3104
|
-
]
|
|
3105
|
-
}
|
|
3106
|
-
]
|
|
3256
|
+
sections: buildToolTraceSections({
|
|
3257
|
+
args: tc.args,
|
|
3258
|
+
executionId: tc.id,
|
|
3259
|
+
output: skipMessage,
|
|
3260
|
+
status: "skipped",
|
|
3261
|
+
summary: skipMessage,
|
|
3262
|
+
toolDetails,
|
|
3263
|
+
toolName: tc.name
|
|
3264
|
+
})
|
|
3107
3265
|
});
|
|
3108
|
-
appended.push({ role: "tool", content:
|
|
3266
|
+
appended.push({ role: "tool", content: skipMessage, tool_call_id: tc.id || `${tc.name}_${appended.length}`, name: tc.name });
|
|
3109
3267
|
toolCount += 1;
|
|
3110
3268
|
}
|
|
3111
3269
|
if (!awaitingApproval && state.ctx?.__awaitingApproval) {
|
|
@@ -4356,7 +4514,7 @@ function createAgent(opts) {
|
|
|
4356
4514
|
if (toolCalls.length === 0) {
|
|
4357
4515
|
if (soManager && !state.ctx?.__finalizedDueToStructuredOutput && !state.ctx?.__structuredOutputForceFinalize) {
|
|
4358
4516
|
if (soManager.strategy.kind === "native") {
|
|
4359
|
-
const assistantText =
|
|
4517
|
+
const assistantText = extractMessageText(lastMsg);
|
|
4360
4518
|
if (assistantText) {
|
|
4361
4519
|
const parsed = soManager.parseFromContent(assistantText);
|
|
4362
4520
|
if (parsed.success) {
|
|
@@ -4407,7 +4565,7 @@ function createAgent(opts) {
|
|
|
4407
4565
|
if (soManager && !state.ctx?.__finalizedDueToStructuredOutput) {
|
|
4408
4566
|
const lastForParse = state.messages[state.messages.length - 1];
|
|
4409
4567
|
if (lastForParse?.role === "assistant") {
|
|
4410
|
-
const assistantText =
|
|
4568
|
+
const assistantText = extractMessageText(lastForParse);
|
|
4411
4569
|
if (assistantText) {
|
|
4412
4570
|
const parsed = soManager.parseFromContent(assistantText);
|
|
4413
4571
|
if (parsed.success) {
|
|
@@ -4560,7 +4718,7 @@ function createAgent(opts) {
|
|
|
4560
4718
|
const isTransientSummarizationExit = !!res.ctx?.__needsSummarization;
|
|
4561
4719
|
const finalAssistantMsg = isTransientSummarizationExit ? void 0 : getLastAssistantMessage(res.messages);
|
|
4562
4720
|
const finalIsSyntheticSummary = !isTransientSummarizationExit && finalAssistantMsg && isSyntheticSummaryMessage(finalAssistantMsg);
|
|
4563
|
-
const content = isTransientSummarizationExit || finalIsSyntheticSummary ? "" :
|
|
4721
|
+
const content = isTransientSummarizationExit || finalIsSyntheticSummary ? "" : extractMessageText(finalAssistantMsg);
|
|
4564
4722
|
let parsed = void 0;
|
|
4565
4723
|
let outputError = void 0;
|
|
4566
4724
|
if (soManager) {
|
|
@@ -4868,7 +5026,7 @@ function renderMemoryBlock(facts) {
|
|
|
4868
5026
|
function clampToBudget(messages, maxContextTokens) {
|
|
4869
5027
|
let working = [...messages];
|
|
4870
5028
|
while (working.length > 2) {
|
|
4871
|
-
const tokenCount = countApproxTokens(working.map(
|
|
5029
|
+
const tokenCount = countApproxTokens(working.map(extractMessageText).join("\n"));
|
|
4872
5030
|
if (tokenCount <= maxContextTokens) return working;
|
|
4873
5031
|
const firstNonSystem = working.findIndex((message, index) => !(index === 0 && message.role === "system"));
|
|
4874
5032
|
if (firstNonSystem < 0) break;
|
|
@@ -5971,7 +6129,7 @@ function createSmartAgent(opts) {
|
|
|
5971
6129
|
].join("\n");
|
|
5972
6130
|
function latestUserPrompt(messages) {
|
|
5973
6131
|
const latest = [...messages].reverse().find((message) => message.role === "user");
|
|
5974
|
-
return latest ?
|
|
6132
|
+
return latest ? extractMessageText(latest) : "";
|
|
5975
6133
|
}
|
|
5976
6134
|
async function syncMemory(state) {
|
|
5977
6135
|
const query = latestUserPrompt(state.messages || []);
|