@tanstack/openai-base 0.10.2 → 0.10.3

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.
@@ -184,6 +184,9 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
184
184
  timestamp: Date.now(),
185
185
  content: accumulatedReasoning
186
186
  };
187
+ reasoningMessageId = void 0;
188
+ stepId = void 0;
189
+ hasClosedReasoning = false;
187
190
  }
188
191
  }.bind(this);
189
192
  const openReasoning = function* () {
@@ -520,8 +523,83 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
520
523
  let model = options.model;
521
524
  let stepId = null;
522
525
  let hasEmittedTextMessageStart = false;
523
- let hasEmittedStepStarted = false;
526
+ let reasoningMessageId;
527
+ let hasClosedReasoning = false;
524
528
  let runFinishedEmitted = false;
529
+ const adapterName = this.name;
530
+ const emitModel = () => model || options.model;
531
+ const openReasoning = function* () {
532
+ if (reasoningMessageId) return;
533
+ reasoningMessageId = generateId(adapterName);
534
+ stepId = generateId(adapterName);
535
+ const timestamp = Date.now();
536
+ const currentModel = emitModel();
537
+ yield {
538
+ type: EventType.REASONING_START,
539
+ messageId: reasoningMessageId,
540
+ model: currentModel,
541
+ timestamp
542
+ };
543
+ yield {
544
+ type: EventType.REASONING_MESSAGE_START,
545
+ messageId: reasoningMessageId,
546
+ role: "reasoning",
547
+ model: currentModel,
548
+ timestamp
549
+ };
550
+ yield {
551
+ type: EventType.STEP_STARTED,
552
+ stepName: stepId,
553
+ stepId,
554
+ model: currentModel,
555
+ timestamp,
556
+ stepType: "thinking"
557
+ };
558
+ };
559
+ const closeReasoning = function* () {
560
+ if (!reasoningMessageId || hasClosedReasoning) return;
561
+ hasClosedReasoning = true;
562
+ const timestamp = Date.now();
563
+ const currentModel = emitModel();
564
+ yield {
565
+ type: EventType.REASONING_MESSAGE_END,
566
+ messageId: reasoningMessageId,
567
+ model: currentModel,
568
+ timestamp
569
+ };
570
+ yield {
571
+ type: EventType.REASONING_END,
572
+ messageId: reasoningMessageId,
573
+ model: currentModel,
574
+ timestamp
575
+ };
576
+ if (stepId) yield {
577
+ type: EventType.STEP_FINISHED,
578
+ stepName: stepId,
579
+ stepId,
580
+ model: currentModel,
581
+ timestamp,
582
+ content: accumulatedReasoning
583
+ };
584
+ reasoningMessageId = void 0;
585
+ stepId = null;
586
+ hasClosedReasoning = false;
587
+ accumulatedReasoning = "";
588
+ };
589
+ const emitReasoningDelta = function* (text) {
590
+ if (!text) return;
591
+ yield* openReasoning();
592
+ if (!reasoningMessageId) return;
593
+ accumulatedReasoning += text;
594
+ hasStreamedReasoningDeltas = true;
595
+ yield {
596
+ type: EventType.REASONING_MESSAGE_CONTENT,
597
+ messageId: reasoningMessageId,
598
+ delta: text,
599
+ model: emitModel(),
600
+ timestamp: Date.now()
601
+ };
602
+ };
525
603
  try {
526
604
  for await (const chunk of stream) {
527
605
  options.logger.provider(`provider=${this.name} type=${chunk.type}`, {
@@ -551,19 +629,6 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
551
629
  content: accumulatedContent
552
630
  };
553
631
  }
554
- if (contentPart.type === "reasoning_text") {
555
- accumulatedReasoning += contentPart.text || "";
556
- if (!stepId) stepId = generateId(this.name);
557
- return {
558
- type: EventType.STEP_FINISHED,
559
- stepName: stepId,
560
- stepId,
561
- model: model || options.model,
562
- timestamp: Date.now(),
563
- delta: contentPart.text || "",
564
- content: accumulatedReasoning
565
- };
566
- }
567
632
  const isRefusal = contentPart.type === "refusal";
568
633
  const message = isRefusal ? contentPart.refusal || "Refused without explanation" : `Unsupported response content_part type: ${contentPart.type}`;
569
634
  const code = isRefusal ? "refusal" : contentPart.type;
@@ -584,11 +649,14 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
584
649
  hasStreamedContentDeltas = false;
585
650
  hasStreamedReasoningDeltas = false;
586
651
  hasEmittedTextMessageStart = false;
587
- hasEmittedStepStarted = false;
652
+ reasoningMessageId = void 0;
653
+ hasClosedReasoning = false;
654
+ stepId = null;
588
655
  accumulatedContent = "";
589
656
  accumulatedReasoning = "";
590
657
  }
591
658
  if (chunk.type === "response.failed" || chunk.type === "response.incomplete") {
659
+ yield* closeReasoning();
592
660
  if (hasEmittedTextMessageStart) {
593
661
  yield {
594
662
  type: EventType.TEXT_MESSAGE_END,
@@ -617,6 +685,7 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
617
685
  if (chunk.type === "response.output_text.delta" && chunk.delta) {
618
686
  const textDelta = Array.isArray(chunk.delta) ? chunk.delta.join("") : typeof chunk.delta === "string" ? chunk.delta : "";
619
687
  if (textDelta) {
688
+ yield* closeReasoning();
620
689
  if (!hasEmittedTextMessageStart) {
621
690
  hasEmittedTextMessageStart = true;
622
691
  yield {
@@ -639,68 +708,17 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
639
708
  };
640
709
  }
641
710
  }
642
- if (chunk.type === "response.reasoning_text.delta" && chunk.delta) {
643
- const reasoningDelta = Array.isArray(chunk.delta) ? chunk.delta.join("") : typeof chunk.delta === "string" ? chunk.delta : "";
644
- if (reasoningDelta) {
645
- if (!hasEmittedStepStarted) {
646
- hasEmittedStepStarted = true;
647
- stepId = generateId(this.name);
648
- yield {
649
- type: EventType.STEP_STARTED,
650
- stepName: stepId,
651
- stepId,
652
- model: model || options.model,
653
- timestamp: Date.now(),
654
- stepType: "thinking"
655
- };
656
- }
657
- accumulatedReasoning += reasoningDelta;
658
- hasStreamedReasoningDeltas = true;
659
- const fallbackStepId = stepId || generateId(this.name);
660
- yield {
661
- type: EventType.STEP_FINISHED,
662
- stepName: fallbackStepId,
663
- stepId: fallbackStepId,
664
- model: model || options.model,
665
- timestamp: Date.now(),
666
- delta: reasoningDelta,
667
- content: accumulatedReasoning
668
- };
669
- }
670
- }
671
- if (chunk.type === "response.reasoning_summary_text.delta" && chunk.delta) {
672
- const summaryDelta = typeof chunk.delta === "string" ? chunk.delta : "";
673
- if (summaryDelta) {
674
- if (!hasEmittedStepStarted) {
675
- hasEmittedStepStarted = true;
676
- stepId = generateId(this.name);
677
- yield {
678
- type: EventType.STEP_STARTED,
679
- stepName: stepId,
680
- stepId,
681
- model: model || options.model,
682
- timestamp: Date.now(),
683
- stepType: "thinking"
684
- };
685
- }
686
- accumulatedReasoning += summaryDelta;
687
- hasStreamedReasoningDeltas = true;
688
- const fallbackStepId = stepId || generateId(this.name);
689
- yield {
690
- type: EventType.STEP_FINISHED,
691
- stepName: fallbackStepId,
692
- stepId: fallbackStepId,
693
- model: model || options.model,
694
- timestamp: Date.now(),
695
- delta: summaryDelta,
696
- content: accumulatedReasoning
697
- };
698
- }
699
- }
711
+ if (chunk.type === "response.reasoning_text.delta" && chunk.delta) yield* emitReasoningDelta(Array.isArray(chunk.delta) ? chunk.delta.join("") : typeof chunk.delta === "string" ? chunk.delta : "");
712
+ if (chunk.type === "response.reasoning_summary_text.delta" && chunk.delta) yield* emitReasoningDelta(typeof chunk.delta === "string" ? chunk.delta : "");
700
713
  if (chunk.type === "response.content_part.added") {
701
714
  const contentPart = chunk.part;
702
715
  if ((contentPart.type === "output_text" || contentPart.type === "reasoning_text") && !contentPart.text) continue;
716
+ if (contentPart.type === "reasoning_text") {
717
+ yield* emitReasoningDelta(contentPart.text || "");
718
+ continue;
719
+ }
703
720
  if (contentPart.type === "output_text" && !hasEmittedTextMessageStart) {
721
+ yield* closeReasoning();
704
722
  hasEmittedTextMessageStart = true;
705
723
  yield {
706
724
  type: EventType.TEXT_MESSAGE_START,
@@ -710,20 +728,7 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
710
728
  role: "assistant"
711
729
  };
712
730
  }
713
- if (contentPart.type === "reasoning_text" && !hasEmittedStepStarted) {
714
- hasEmittedStepStarted = true;
715
- stepId = generateId(this.name);
716
- yield {
717
- type: EventType.STEP_STARTED,
718
- stepName: stepId,
719
- stepId,
720
- model: model || options.model,
721
- timestamp: Date.now(),
722
- stepType: "thinking"
723
- };
724
- }
725
731
  if (contentPart.type === "output_text") hasStreamedContentDeltas = true;
726
- else if (contentPart.type === "reasoning_text") hasStreamedReasoningDeltas = true;
727
732
  const partChunk = handleContentPart(contentPart);
728
733
  yield partChunk;
729
734
  if (partChunk.type === "RUN_ERROR") {
@@ -735,7 +740,12 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
735
740
  const contentPart = chunk.part;
736
741
  if (contentPart.type === "output_text" && hasStreamedContentDeltas) continue;
737
742
  if (contentPart.type === "reasoning_text" && hasStreamedReasoningDeltas) continue;
743
+ if (contentPart.type === "reasoning_text") {
744
+ yield* emitReasoningDelta(contentPart.text || "");
745
+ continue;
746
+ }
738
747
  if (contentPart.type === "output_text" && !hasEmittedTextMessageStart) {
748
+ yield* closeReasoning();
739
749
  hasEmittedTextMessageStart = true;
740
750
  yield {
741
751
  type: EventType.TEXT_MESSAGE_START,
@@ -744,17 +754,6 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
744
754
  timestamp: Date.now(),
745
755
  role: "assistant"
746
756
  };
747
- } else if (contentPart.type === "reasoning_text" && !hasEmittedStepStarted) {
748
- hasEmittedStepStarted = true;
749
- stepId = generateId(this.name);
750
- yield {
751
- type: EventType.STEP_STARTED,
752
- stepName: stepId,
753
- stepId,
754
- model: model || options.model,
755
- timestamp: Date.now(),
756
- stepType: "thinking"
757
- };
758
757
  }
759
758
  const doneChunk = handleContentPart(contentPart);
760
759
  yield doneChunk;
@@ -996,6 +995,7 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
996
995
  metadata.pendingArguments = void 0;
997
996
  }
998
997
  }
998
+ yield* closeReasoning();
999
999
  if (hasEmittedTextMessageStart) {
1000
1000
  yield {
1001
1001
  type: EventType.TEXT_MESSAGE_END,
@@ -1037,6 +1037,7 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
1037
1037
  }
1038
1038
  }
1039
1039
  if (!runFinishedEmitted && aguiState.hasEmittedRunStarted) {
1040
+ yield* closeReasoning();
1040
1041
  if (hasEmittedTextMessageStart) yield {
1041
1042
  type: EventType.TEXT_MESSAGE_END,
1042
1043
  messageId: aguiState.messageId,
@@ -1235,7 +1236,7 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
1235
1236
  * Handles backward compatibility with string content.
1236
1237
  */
1237
1238
  normalizeContent(content) {
1238
- if (content === null) return [];
1239
+ if (content === null || content === void 0) return [];
1239
1240
  if (typeof content === "string") return [{
1240
1241
  type: "text",
1241
1242
  content
@@ -1246,7 +1247,7 @@ var OpenAIBaseResponsesTextAdapter = class extends BaseTextAdapter {
1246
1247
  * Extracts text content from a content value that may be string, null, or ContentPart array.
1247
1248
  */
1248
1249
  extractTextContent(content) {
1249
- if (content === null) return "";
1250
+ if (content === null || content === void 0) return "";
1250
1251
  if (typeof content === "string") return content;
1251
1252
  return content.filter((p) => p.type === "text").map((p) => p.content).join("");
1252
1253
  }