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