@kenkaiiii/gg-ai 4.3.239 → 4.3.241

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
@@ -414,8 +414,8 @@ function toAnthropicAssistantPart(part, idMap) {
414
414
  if (part.type === "raw") return part.data;
415
415
  return null;
416
416
  }
417
- function toAnthropicAssistantContent(content, isLatest, idMap) {
418
- if (!isLatest) {
417
+ function toAnthropicAssistantContent(content, preserveThinking, idMap) {
418
+ if (!preserveThinking) {
419
419
  return content.filter((part) => {
420
420
  if (part.type === "thinking" || isRawThinking(part)) return false;
421
421
  if (part.type === "text" && !part.text) return false;
@@ -532,8 +532,8 @@ function toAnthropicMessages(messages, cacheControl) {
532
532
  let systemText;
533
533
  const out = [];
534
534
  const idMap = /* @__PURE__ */ new Map();
535
- const lastAssistantIdx = messages.reduce(
536
- (last, m, i) => m.role === "assistant" ? i : last,
535
+ const trajectoryStartIdx = messages.reduce(
536
+ (last, m, i) => m.role === "user" ? i : last,
537
537
  -1
538
538
  );
539
539
  let msgIdx = -1;
@@ -571,7 +571,7 @@ function toAnthropicMessages(messages, cacheControl) {
571
571
  continue;
572
572
  }
573
573
  if (msg.role === "assistant") {
574
- const content = typeof msg.content === "string" ? msg.content : toAnthropicAssistantContent(msg.content, msgIdx === lastAssistantIdx, idMap);
574
+ const content = typeof msg.content === "string" ? msg.content : toAnthropicAssistantContent(msg.content, msgIdx > trajectoryStartIdx, idMap);
575
575
  if (Array.isArray(content) && content.length === 0) continue;
576
576
  out.push({ role: "assistant", content });
577
577
  continue;
@@ -1854,6 +1854,7 @@ async function* runStream3(options) {
1854
1854
  const contentParts = [];
1855
1855
  let textAccum = "";
1856
1856
  const toolCalls = /* @__PURE__ */ new Map();
1857
+ const orderedItems = [];
1857
1858
  const outputItemTypes = /* @__PURE__ */ new Map();
1858
1859
  const outputTextByPart = /* @__PURE__ */ new Map();
1859
1860
  const pendingOutputTextByPart = /* @__PURE__ */ new Map();
@@ -2001,12 +2002,26 @@ async function* runStream3(options) {
2001
2002
  }
2002
2003
  if (type === "response.output_item.done") {
2003
2004
  const item = event.item;
2005
+ if (item?.type === "reasoning") {
2006
+ const encrypted = item.encrypted_content;
2007
+ const reasoningId = item.id;
2008
+ if (encrypted && reasoningId) {
2009
+ orderedItems.push({
2010
+ kind: "reasoning",
2011
+ part: {
2012
+ type: "raw",
2013
+ data: { ...item, summary: Array.isArray(item.summary) ? item.summary : [] }
2014
+ }
2015
+ });
2016
+ }
2017
+ }
2004
2018
  if (item?.type === "function_call") {
2005
2019
  const callId = item.call_id;
2006
2020
  const itemId = item.id;
2007
2021
  const id = `${callId}|${itemId}`;
2008
2022
  const tc = toolCalls.get(id);
2009
2023
  if (tc) {
2024
+ orderedItems.push({ kind: "tool", id });
2010
2025
  const args = parseToolArguments(tc.argsJson);
2011
2026
  yield {
2012
2027
  type: "toolcall_done",
@@ -2027,19 +2042,41 @@ async function* runStream3(options) {
2027
2042
  }
2028
2043
  }
2029
2044
  }
2030
- if (textAccum) {
2031
- contentParts.push({ type: "text", text: textAccum });
2032
- }
2033
- for (const [, tc] of toolCalls) {
2034
- const args = parseToolArguments(tc.argsJson);
2045
+ const seenTool = /* @__PURE__ */ new Set();
2046
+ let textInserted = false;
2047
+ for (const entry of orderedItems) {
2048
+ if (entry.kind === "reasoning") {
2049
+ contentParts.push(entry.part);
2050
+ continue;
2051
+ }
2052
+ if (textAccum && !textInserted) {
2053
+ contentParts.push({ type: "text", text: textAccum });
2054
+ textInserted = true;
2055
+ }
2056
+ const tc = toolCalls.get(entry.id);
2057
+ if (!tc || seenTool.has(entry.id)) continue;
2058
+ seenTool.add(entry.id);
2035
2059
  const toolCall = {
2036
2060
  type: "tool_call",
2037
2061
  id: tc.id,
2038
2062
  name: tc.name,
2039
- args
2063
+ args: parseToolArguments(tc.argsJson)
2040
2064
  };
2041
2065
  contentParts.push(toolCall);
2042
2066
  }
2067
+ if (textAccum && !textInserted) {
2068
+ contentParts.push({ type: "text", text: textAccum });
2069
+ }
2070
+ for (const [id, tc] of toolCalls) {
2071
+ if (seenTool.has(id)) continue;
2072
+ seenTool.add(id);
2073
+ contentParts.push({
2074
+ type: "tool_call",
2075
+ id: tc.id,
2076
+ name: tc.name,
2077
+ args: parseToolArguments(tc.argsJson)
2078
+ });
2079
+ }
2043
2080
  const hasToolCalls = contentParts.some((p) => p.type === "tool_call");
2044
2081
  const stopReason = hasToolCalls ? "tool_use" : "end_turn";
2045
2082
  const streamResponse = {
@@ -2071,6 +2108,9 @@ function remapCodexId(id, idMap) {
2071
2108
  idMap.set(id, mapped);
2072
2109
  return mapped;
2073
2110
  }
2111
+ function isEncryptedReasoning(data) {
2112
+ return data.type === "reasoning" && typeof data.id === "string" && typeof data.encrypted_content === "string";
2113
+ }
2074
2114
  function toCodexInput(messages, options) {
2075
2115
  let system;
2076
2116
  const input = [];
@@ -2103,7 +2143,9 @@ function toCodexInput(messages, options) {
2103
2143
  continue;
2104
2144
  }
2105
2145
  for (const part of msg.content) {
2106
- if (part.type === "text") {
2146
+ if (part.type === "raw" && isEncryptedReasoning(part.data)) {
2147
+ input.push(part.data);
2148
+ } else if (part.type === "text") {
2107
2149
  input.push({
2108
2150
  type: "message",
2109
2151
  role: "assistant",