@cognipeer/agent-sdk 0.2.4 → 0.2.5
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 +89 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +89 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1387,7 +1387,95 @@ function createAgentCoreNode(opts) {
|
|
|
1387
1387
|
};
|
|
1388
1388
|
let response;
|
|
1389
1389
|
try {
|
|
1390
|
-
const
|
|
1390
|
+
const ensureUniqueToolCallIds = (input) => {
|
|
1391
|
+
const msgs = Array.isArray(input) ? input.map((m) => ({ ...m })) : [];
|
|
1392
|
+
const usedIds = /* @__PURE__ */ new Set();
|
|
1393
|
+
let counter = 0;
|
|
1394
|
+
for (let i = 0; i < msgs.length; i++) {
|
|
1395
|
+
const m = msgs[i];
|
|
1396
|
+
if (m?.role !== "assistant") continue;
|
|
1397
|
+
const hasToolCalls = Array.isArray(m.tool_calls) && m.tool_calls.length > 0;
|
|
1398
|
+
const contentArr = Array.isArray(m.content) ? m.content : [];
|
|
1399
|
+
const hasContentToolUse = contentArr.some((c) => c?.type === "tool_use" && c?.id);
|
|
1400
|
+
const hasAdditionalToolCalls = Array.isArray(m.additional_kwargs?.tool_calls) && m.additional_kwargs.tool_calls.length > 0;
|
|
1401
|
+
if (!hasToolCalls && !hasContentToolUse && !hasAdditionalToolCalls) continue;
|
|
1402
|
+
if (hasToolCalls) {
|
|
1403
|
+
m.tool_calls = m.tool_calls.map((tc) => ({ ...tc }));
|
|
1404
|
+
}
|
|
1405
|
+
if (hasContentToolUse) {
|
|
1406
|
+
m.content = contentArr.map(
|
|
1407
|
+
(c) => c && typeof c === "object" ? { ...c } : c
|
|
1408
|
+
);
|
|
1409
|
+
}
|
|
1410
|
+
if (hasAdditionalToolCalls) {
|
|
1411
|
+
m.additional_kwargs = { ...m.additional_kwargs };
|
|
1412
|
+
m.additional_kwargs.tool_calls = m.additional_kwargs.tool_calls.map((tc) => ({ ...tc }));
|
|
1413
|
+
}
|
|
1414
|
+
const idsInMessage = /* @__PURE__ */ new Set();
|
|
1415
|
+
if (hasToolCalls) {
|
|
1416
|
+
for (const tc of m.tool_calls) {
|
|
1417
|
+
if (tc?.id) idsInMessage.add(tc.id);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
if (hasContentToolUse) {
|
|
1421
|
+
for (const c of m.content) {
|
|
1422
|
+
if (c?.type === "tool_use" && c?.id) idsInMessage.add(c.id);
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
if (hasAdditionalToolCalls) {
|
|
1426
|
+
for (const tc of m.additional_kwargs.tool_calls) {
|
|
1427
|
+
if (tc?.id) idsInMessage.add(tc.id);
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
for (const id of idsInMessage) {
|
|
1431
|
+
if (!usedIds.has(id)) {
|
|
1432
|
+
usedIds.add(id);
|
|
1433
|
+
continue;
|
|
1434
|
+
}
|
|
1435
|
+
const newId = `${id}_${Date.now()}_${counter++}`;
|
|
1436
|
+
if (hasToolCalls) {
|
|
1437
|
+
for (const tc of m.tool_calls) {
|
|
1438
|
+
if (tc.id === id) tc.id = newId;
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
if (hasContentToolUse && Array.isArray(m.content)) {
|
|
1442
|
+
for (const c of m.content) {
|
|
1443
|
+
if (c?.type === "tool_use" && c.id === id) c.id = newId;
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
if (hasAdditionalToolCalls) {
|
|
1447
|
+
for (const tc of m.additional_kwargs.tool_calls) {
|
|
1448
|
+
if (tc.id === id) tc.id = newId;
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
for (let j = i + 1; j < msgs.length; j++) {
|
|
1452
|
+
const next = msgs[j];
|
|
1453
|
+
if (next?.role === "assistant") break;
|
|
1454
|
+
if (next?.role === "tool" && next.tool_call_id === id) {
|
|
1455
|
+
next.tool_call_id = newId;
|
|
1456
|
+
}
|
|
1457
|
+
if (Array.isArray(next?.content)) {
|
|
1458
|
+
let contentCloned = false;
|
|
1459
|
+
for (let k = 0; k < next.content.length; k++) {
|
|
1460
|
+
const block = next.content[k];
|
|
1461
|
+
if (block?.type === "tool_result" && block.tool_use_id === id) {
|
|
1462
|
+
if (!contentCloned) {
|
|
1463
|
+
next.content = next.content.map((x) => x && typeof x === "object" ? { ...x } : x);
|
|
1464
|
+
contentCloned = true;
|
|
1465
|
+
}
|
|
1466
|
+
next.content[k].tool_use_id = newId;
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
usedIds.add(newId);
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
return msgs;
|
|
1475
|
+
};
|
|
1476
|
+
const normalizedMessages = ensureUniqueToolCallIds(
|
|
1477
|
+
normalizeBedrockToolPairing([...state.messages])
|
|
1478
|
+
);
|
|
1391
1479
|
debugToolPairing(normalizedMessages);
|
|
1392
1480
|
if (streamingEnabled && typeof modelWithTools.stream === "function") {
|
|
1393
1481
|
let streamedText = "";
|