@tangle-network/agent-runtime 0.84.0 → 0.84.1

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.js CHANGED
@@ -483,10 +483,9 @@ function* parseStreamChunk(chunk, context, usage, toolCalls) {
483
483
  for (const tc of deltaToolCalls) {
484
484
  if (!tc || typeof tc !== "object") continue;
485
485
  const rec = tc;
486
- const idx = numberValue(rec.index) ?? 0;
487
- const key = `openai:${idx}`;
488
- const acc = toolCalls.get(key) ?? { argsRaw: "", source: "openai", finalized: false };
489
486
  const id = stringValue(rec.id);
487
+ const key = deltaToolCallKey(rec, id, toolCalls);
488
+ const acc = toolCalls.get(key) ?? { argsRaw: "", source: "openai", finalized: false };
490
489
  if (id) acc.id = id;
491
490
  const fn = rec.function;
492
491
  const name = stringValue(fn?.name);
@@ -599,6 +598,16 @@ function* flushPendingToolCalls(toolCalls, context) {
599
598
  yield buildToolCallEvent(acc, context);
600
599
  }
601
600
  }
601
+ function deltaToolCallKey(rec, id, toolCalls) {
602
+ const idx = numberValue(rec.index);
603
+ if (idx !== void 0) return `openai:${idx}`;
604
+ if (id) return `openai:id:${id}`;
605
+ let last;
606
+ for (const [key, acc] of toolCalls) {
607
+ if (acc.source === "openai" && !acc.finalized) last = key;
608
+ }
609
+ return last ?? "openai:0";
610
+ }
602
611
  function buildToolCallEvent(acc, context) {
603
612
  let args = acc.argsRaw;
604
613
  if (acc.argsRaw.length > 0) {