@kenkaiiii/gg-ai 4.3.238 → 4.3.240

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;
@@ -1098,11 +1098,18 @@ async function* runStream(options) {
1098
1098
  args: tc.args
1099
1099
  };
1100
1100
  } else if (accum.type === "server_tool_use") {
1101
+ let input = accum.input;
1102
+ if (accum.argsJson) {
1103
+ try {
1104
+ input = JSON.parse(accum.argsJson);
1105
+ } catch {
1106
+ }
1107
+ }
1101
1108
  const stc = {
1102
1109
  type: "server_tool_call",
1103
1110
  id: accum.toolId,
1104
1111
  name: accum.toolName,
1105
- input: accum.input
1112
+ input
1106
1113
  };
1107
1114
  contentParts.push(stc);
1108
1115
  yield {
@@ -1847,6 +1854,7 @@ async function* runStream3(options) {
1847
1854
  const contentParts = [];
1848
1855
  let textAccum = "";
1849
1856
  const toolCalls = /* @__PURE__ */ new Map();
1857
+ const orderedItems = [];
1850
1858
  const outputItemTypes = /* @__PURE__ */ new Map();
1851
1859
  const outputTextByPart = /* @__PURE__ */ new Map();
1852
1860
  const pendingOutputTextByPart = /* @__PURE__ */ new Map();
@@ -1994,12 +2002,26 @@ async function* runStream3(options) {
1994
2002
  }
1995
2003
  if (type === "response.output_item.done") {
1996
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
+ }
1997
2018
  if (item?.type === "function_call") {
1998
2019
  const callId = item.call_id;
1999
2020
  const itemId = item.id;
2000
2021
  const id = `${callId}|${itemId}`;
2001
2022
  const tc = toolCalls.get(id);
2002
2023
  if (tc) {
2024
+ orderedItems.push({ kind: "tool", id });
2003
2025
  const args = parseToolArguments(tc.argsJson);
2004
2026
  yield {
2005
2027
  type: "toolcall_done",
@@ -2020,19 +2042,41 @@ async function* runStream3(options) {
2020
2042
  }
2021
2043
  }
2022
2044
  }
2023
- if (textAccum) {
2024
- contentParts.push({ type: "text", text: textAccum });
2025
- }
2026
- for (const [, tc] of toolCalls) {
2027
- 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);
2028
2059
  const toolCall = {
2029
2060
  type: "tool_call",
2030
2061
  id: tc.id,
2031
2062
  name: tc.name,
2032
- args
2063
+ args: parseToolArguments(tc.argsJson)
2033
2064
  };
2034
2065
  contentParts.push(toolCall);
2035
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
+ }
2036
2080
  const hasToolCalls = contentParts.some((p) => p.type === "tool_call");
2037
2081
  const stopReason = hasToolCalls ? "tool_use" : "end_turn";
2038
2082
  const streamResponse = {
@@ -2064,6 +2108,9 @@ function remapCodexId(id, idMap) {
2064
2108
  idMap.set(id, mapped);
2065
2109
  return mapped;
2066
2110
  }
2111
+ function isEncryptedReasoning(data) {
2112
+ return data.type === "reasoning" && typeof data.id === "string" && typeof data.encrypted_content === "string";
2113
+ }
2067
2114
  function toCodexInput(messages, options) {
2068
2115
  let system;
2069
2116
  const input = [];
@@ -2096,7 +2143,9 @@ function toCodexInput(messages, options) {
2096
2143
  continue;
2097
2144
  }
2098
2145
  for (const part of msg.content) {
2099
- if (part.type === "text") {
2146
+ if (part.type === "raw" && isEncryptedReasoning(part.data)) {
2147
+ input.push(part.data);
2148
+ } else if (part.type === "text") {
2100
2149
  input.push({
2101
2150
  type: "message",
2102
2151
  role: "assistant",