@codehz/ai 0.2.1 → 0.2.2

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.mjs CHANGED
@@ -483,7 +483,9 @@ function createAggregatorState() {
483
483
  toolCalls: [],
484
484
  started: false,
485
485
  completed: false,
486
- activeItems: /* @__PURE__ */ new Map()
486
+ activeItems: /* @__PURE__ */ new Map(),
487
+ itemOrder: [],
488
+ completedItems: /* @__PURE__ */ new Map()
487
489
  };
488
490
  }
489
491
  function getActiveItem(state, itemId, expectedType) {
@@ -546,6 +548,7 @@ function handleMessageStarted(state, event) {
546
548
  role: event.item.role,
547
549
  content: []
548
550
  });
551
+ state.itemOrder.push(id);
549
552
  }
550
553
  function handleMessageDelta(state, event) {
551
554
  getActiveItem(state, event.itemId, "message").content.push(event.delta);
@@ -554,7 +557,7 @@ function handleMessageCompleted(state, event) {
554
557
  const active = getActiveItem(state, event.itemId, "message");
555
558
  state.activeItems.delete(event.itemId);
556
559
  const item = finalizeMessage(active);
557
- state.output.push(item);
560
+ state.completedItems.set(event.itemId, item);
558
561
  pushMessageText(state, item);
559
562
  }
560
563
  function handleReasoningStarted(state, event) {
@@ -566,6 +569,7 @@ function handleReasoningStarted(state, event) {
566
569
  visibility: event.item.visibility,
567
570
  content: []
568
571
  });
572
+ state.itemOrder.push(id);
569
573
  }
570
574
  function handleReasoningDelta(state, event) {
571
575
  getActiveItem(state, event.itemId, "reasoning").content.push(event.delta);
@@ -573,7 +577,7 @@ function handleReasoningDelta(state, event) {
573
577
  function handleReasoningCompleted(state, event) {
574
578
  const active = getActiveItem(state, event.itemId, "reasoning");
575
579
  state.activeItems.delete(event.itemId);
576
- state.output.push(finalizeReasoning(active));
580
+ state.completedItems.set(event.itemId, finalizeReasoning(active));
577
581
  }
578
582
  function handleToolCallStarted(state, event) {
579
583
  const id = event.item.id;
@@ -584,6 +588,7 @@ function handleToolCallStarted(state, event) {
584
588
  name: event.item.name,
585
589
  argumentsText: ""
586
590
  });
591
+ state.itemOrder.push(id);
587
592
  }
588
593
  function handleToolCallDelta(state, event) {
589
594
  const active = getActiveItem(state, event.itemId, "tool_call");
@@ -593,7 +598,7 @@ function handleToolCallCompleted(state, event) {
593
598
  const active = getActiveItem(state, event.itemId, "tool_call");
594
599
  state.activeItems.delete(event.itemId);
595
600
  const item = finalizeToolCall(active);
596
- state.output.push(item);
601
+ state.completedItems.set(event.itemId, item);
597
602
  state.toolCalls.push(item);
598
603
  }
599
604
  function handleResponseCompleted(state, event) {
@@ -624,9 +629,14 @@ function buildResponse(state) {
624
629
  metadataSources: backendFromCompleted?.metadataSources,
625
630
  warnings: backendFromCompleted?.warnings
626
631
  };
632
+ const orderedOutput = state.itemOrder.map((id) => {
633
+ const item = state.completedItems.get(id);
634
+ if (!item) throw streamProtocolError(`Item ${id} was started but not completed`);
635
+ return item;
636
+ });
627
637
  return {
628
638
  id: state.responseId,
629
- output: state.output,
639
+ output: [...orderedOutput, ...state.output],
630
640
  replay: state.replayFromAdapter ?? [],
631
641
  text: state.textParts.join(""),
632
642
  toolCalls: state.toolCalls,
@@ -2675,7 +2685,7 @@ var ChatCompletionsAdapter = class extends AdapterBase {
2675
2685
  if (hasMessageStarted) {
2676
2686
  const message = messageItem([textBlock(accumulatedContent)], { id: currentMessageId });
2677
2687
  events.push(factory.messageCompleted(currentMessageId));
2678
- if (accumulatedContent) output.push(message);
2688
+ output.push(message);
2679
2689
  }
2680
2690
  for (const pending of finalizedToolCalls) {
2681
2691
  const toolCall = toolCallItem(pending.id, pending.name, pending.args);
@@ -2770,10 +2780,6 @@ var ChatCompletionsAdapter = class extends AdapterBase {
2770
2780
  hasMessageStarted = true;
2771
2781
  accumulatedContent = "";
2772
2782
  };
2773
- if (delta.role === "assistant" && !hasMessageStarted) {
2774
- ensureMessageStarted();
2775
- yield factory.messageStarted(currentMessageId);
2776
- }
2777
2783
  if (reasoningDeltas.length > 0) {
2778
2784
  if (!hasReasoningStarted) {
2779
2785
  currentReasoningId = `reason-${chunk.id}`;