@kenkaiiii/gg-ai 4.3.215 → 4.3.217

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
@@ -1570,6 +1570,9 @@ function parseToolArguments2(argsJson) {
1570
1570
  function outputTextKey(itemId, contentIndex) {
1571
1571
  return `${itemId ?? ""}:${contentIndex ?? 0}`;
1572
1572
  }
1573
+ function isVisibleOutputItem(itemType) {
1574
+ return itemType === "message";
1575
+ }
1573
1576
  function streamOpenAICodex(options) {
1574
1577
  return new StreamResult(runStream3(options));
1575
1578
  }
@@ -1653,6 +1656,7 @@ async function* runStream3(options) {
1653
1656
  const toolCalls = /* @__PURE__ */ new Map();
1654
1657
  const outputItemTypes = /* @__PURE__ */ new Map();
1655
1658
  const outputTextByPart = /* @__PURE__ */ new Map();
1659
+ const pendingOutputTextByPart = /* @__PURE__ */ new Map();
1656
1660
  let inputTokens = 0;
1657
1661
  let outputTokens = 0;
1658
1662
  let cacheRead = 0;
@@ -1689,11 +1693,19 @@ async function* runStream3(options) {
1689
1693
  const contentIndex = event.content_index;
1690
1694
  const key = outputTextKey(itemId, contentIndex);
1691
1695
  outputTextByPart.set(key, `${outputTextByPart.get(key) ?? ""}${delta}`);
1692
- if (itemId && outputItemTypes.get(itemId) === "reasoning") {
1693
- if (options.thinking) yield { type: "thinking_delta", text: delta };
1694
- } else {
1696
+ const itemType = itemId ? outputItemTypes.get(itemId) : void 0;
1697
+ if (itemId && isVisibleOutputItem(itemType)) {
1695
1698
  textAccum += delta;
1696
1699
  yield { type: "text_delta", text: delta };
1700
+ } else if (itemId && itemType == null) {
1701
+ const pending = pendingOutputTextByPart.get(key);
1702
+ pendingOutputTextByPart.set(key, {
1703
+ itemId,
1704
+ contentIndex: contentIndex ?? 0,
1705
+ text: `${pending?.text ?? ""}${delta}`
1706
+ });
1707
+ } else if (options.thinking) {
1708
+ yield { type: "thinking_delta", text: delta };
1697
1709
  }
1698
1710
  }
1699
1711
  if (type === "response.output_text.done") {
@@ -1706,11 +1718,19 @@ async function* runStream3(options) {
1706
1718
  const missingText = streamedText ? fullText.slice(streamedText.length) : fullText;
1707
1719
  outputTextByPart.set(key, fullText);
1708
1720
  if (missingText && fullText.startsWith(streamedText)) {
1709
- if (itemId && outputItemTypes.get(itemId) === "reasoning") {
1710
- if (options.thinking) yield { type: "thinking_delta", text: missingText };
1711
- } else {
1721
+ const itemType = itemId ? outputItemTypes.get(itemId) : void 0;
1722
+ if (itemId && isVisibleOutputItem(itemType)) {
1712
1723
  textAccum += missingText;
1713
1724
  yield { type: "text_delta", text: missingText };
1725
+ } else if (itemId && itemType == null) {
1726
+ const pending = pendingOutputTextByPart.get(key);
1727
+ pendingOutputTextByPart.set(key, {
1728
+ itemId,
1729
+ contentIndex: contentIndex ?? 0,
1730
+ text: `${pending?.text ?? ""}${missingText}`
1731
+ });
1732
+ } else if (options.thinking) {
1733
+ yield { type: "thinking_delta", text: missingText };
1714
1734
  }
1715
1735
  }
1716
1736
  }
@@ -1729,6 +1749,19 @@ async function* runStream3(options) {
1729
1749
  if (itemType === "reasoning" && options.thinking) {
1730
1750
  yield { type: "thinking_delta", text: "" };
1731
1751
  }
1752
+ if (itemId && itemType) {
1753
+ const pending = [...pendingOutputTextByPart.entries()].filter(([, pendingPart]) => pendingPart.itemId === itemId).sort(([, a], [, b]) => a.contentIndex - b.contentIndex);
1754
+ for (const [key, pendingPart] of pending) {
1755
+ pendingOutputTextByPart.delete(key);
1756
+ if (!pendingPart.text) continue;
1757
+ if (isVisibleOutputItem(itemType)) {
1758
+ textAccum += pendingPart.text;
1759
+ yield { type: "text_delta", text: pendingPart.text };
1760
+ } else if (options.thinking) {
1761
+ yield { type: "thinking_delta", text: pendingPart.text };
1762
+ }
1763
+ }
1764
+ }
1732
1765
  }
1733
1766
  if (type === "response.output_item.added") {
1734
1767
  const item = event.item;