@kenkaiiii/gg-ai 5.23.2 → 5.24.0

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.d.cts CHANGED
@@ -507,6 +507,8 @@ declare function toOpenAIMessages(messages: Message[], options?: {
507
507
  provider?: string;
508
508
  thinking?: boolean;
509
509
  supportsImages?: boolean;
510
+ /** Wire name for reasoning on assistant messages. Defaults to `reasoning_content`. */
511
+ reasoningField?: string;
510
512
  }): OpenAI.ChatCompletionMessageParam[];
511
513
 
512
514
  /**
package/dist/index.d.ts CHANGED
@@ -507,6 +507,8 @@ declare function toOpenAIMessages(messages: Message[], options?: {
507
507
  provider?: string;
508
508
  thinking?: boolean;
509
509
  supportsImages?: boolean;
510
+ /** Wire name for reasoning on assistant messages. Defaults to `reasoning_content`. */
511
+ reasoningField?: string;
510
512
  }): OpenAI.ChatCompletionMessageParam[];
511
513
 
512
514
  /**
package/dist/index.js CHANGED
@@ -501,6 +501,39 @@ function normalizeRootForAnthropic(schema) {
501
501
  return out;
502
502
  }
503
503
 
504
+ // src/providers/reasoning-field.ts
505
+ var REASONING_FIELD_ALIASES = [
506
+ "reasoning_content",
507
+ "reasoning",
508
+ "reasoning_text"
509
+ ];
510
+ var DEFAULT_REASONING_FIELD = REASONING_FIELD_ALIASES[0];
511
+ function readReasoning(obj) {
512
+ if (!obj) return void 0;
513
+ for (const field of REASONING_FIELD_ALIASES) {
514
+ const value = obj[field];
515
+ if (typeof value === "string" && value) return { field, text: value };
516
+ }
517
+ return void 0;
518
+ }
519
+ function reasoningFieldKey(provider, baseUrl, model) {
520
+ return `${provider}|${baseUrl ?? ""}|${model}`;
521
+ }
522
+ var MAX_REMEMBERED_ENDPOINTS = 64;
523
+ var detectedFields = /* @__PURE__ */ new Map();
524
+ function rememberReasoningField(key, field) {
525
+ if (detectedFields.get(key) === field) return;
526
+ detectedFields.set(key, field);
527
+ while (detectedFields.size > MAX_REMEMBERED_ENDPOINTS) {
528
+ const oldest = detectedFields.keys().next();
529
+ if (oldest.done) break;
530
+ detectedFields.delete(oldest.value);
531
+ }
532
+ }
533
+ function getReasoningField(key) {
534
+ return detectedFields.get(key) ?? DEFAULT_REASONING_FIELD;
535
+ }
536
+
504
537
  // src/providers/transform.ts
505
538
  function hasValidThinkingSignature(part) {
506
539
  return typeof part.signature === "string" && part.signature.trim().length > 0;
@@ -921,6 +954,7 @@ function remapToolCallId(id, idMap) {
921
954
  return mapped;
922
955
  }
923
956
  function toOpenAIMessages(messages, options) {
957
+ const reasoningField = options?.reasoningField || DEFAULT_REASONING_FIELD;
924
958
  const out = [];
925
959
  const idMap = /* @__PURE__ */ new Map();
926
960
  const mergeToolResultText = options?.provider === "glm";
@@ -987,9 +1021,9 @@ function toOpenAIMessages(messages, options) {
987
1021
  ...hasToolCalls ? { tool_calls: toolCalls } : {}
988
1022
  };
989
1023
  if (thinkingParts) {
990
- assistantMsg.reasoning_content = thinkingParts;
1024
+ assistantMsg[reasoningField] = thinkingParts;
991
1025
  } else if (options?.thinking && hasToolCalls && options.provider !== "glm") {
992
- assistantMsg.reasoning_content = " ";
1026
+ assistantMsg[reasoningField] = " ";
993
1027
  }
994
1028
  out.push(assistantMsg);
995
1029
  continue;
@@ -1862,6 +1896,7 @@ function streamOpenAI(options) {
1862
1896
  async function* runStream2(options) {
1863
1897
  const providerName = options.provider ?? "openai";
1864
1898
  const useStreaming = options.streaming !== false;
1899
+ const endpointKey = reasoningFieldKey(providerName, options.baseUrl, options.model);
1865
1900
  const client = createClient2(options);
1866
1901
  const isKimiK3 = options.provider === "moonshot" && options.model === "kimi-k3";
1867
1902
  const isManagedKimiK3 = isKimiK3 && options.baseUrl?.replace(/\/+$/, "").endsWith("/coding/v1") === true;
@@ -1885,7 +1920,8 @@ async function* runStream2(options) {
1885
1920
  // disabled K3 must NOT carry placeholder reasoning_content (mirrors the
1886
1921
  // official CLI: reasoning is preserved only while thinking is enabled).
1887
1922
  thinking: isKimiK27 || !!options.thinking,
1888
- supportsImages: options.supportsImages
1923
+ supportsImages: options.supportsImages,
1924
+ reasoningField: getReasoningField(endpointKey)
1889
1925
  });
1890
1926
  const defaultTemp = options.provider === "glm" ? 0.6 : void 0;
1891
1927
  const effectiveTemp = options.temperature ?? defaultTemp;
@@ -1947,8 +1983,8 @@ async function* runStream2(options) {
1947
1983
  const completion = await client.chat.completions.create(params, {
1948
1984
  signal: options.signal ?? void 0
1949
1985
  });
1950
- yield* synthesizeEventsFromCompletion(completion, !!options.thinking);
1951
- return completionToResponse(completion);
1986
+ yield* synthesizeEventsFromCompletion(completion, !!options.thinking, endpointKey);
1987
+ return completionToResponse(completion, endpointKey);
1952
1988
  } catch (err) {
1953
1989
  throw toError2(err, providerName);
1954
1990
  }
@@ -1983,11 +2019,12 @@ async function* runStream2(options) {
1983
2019
  finishReason = choice.finish_reason;
1984
2020
  }
1985
2021
  const delta = choice.delta;
1986
- const reasoningContent = delta.reasoning_content;
1987
- if (typeof reasoningContent === "string" && reasoningContent) {
1988
- thinkingAccum += reasoningContent;
2022
+ const reasoning = readReasoning(delta);
2023
+ if (reasoning) {
2024
+ rememberReasoningField(endpointKey, reasoning.field);
2025
+ thinkingAccum += reasoning.text;
1989
2026
  if (options.thinking) {
1990
- yield { type: "thinking_delta", text: reasoningContent };
2027
+ yield { type: "thinking_delta", text: reasoning.text };
1991
2028
  }
1992
2029
  }
1993
2030
  if (delta.content) {
@@ -2072,16 +2109,17 @@ async function* runStream2(options) {
2072
2109
  yield { type: "done", stopReason };
2073
2110
  return response;
2074
2111
  }
2075
- function* synthesizeEventsFromCompletion(completion, thinkingEnabled) {
2112
+ function* synthesizeEventsFromCompletion(completion, thinkingEnabled, endpointKey) {
2076
2113
  const choice = completion.choices?.[0];
2077
2114
  if (!choice) {
2078
2115
  yield { type: "done", stopReason: normalizeOpenAIStopReason(null) };
2079
2116
  return;
2080
2117
  }
2081
2118
  const msg = choice.message;
2082
- const reasoning = msg.reasoning_content;
2083
- if (typeof reasoning === "string" && reasoning && thinkingEnabled) {
2084
- yield { type: "thinking_delta", text: reasoning };
2119
+ const reasoning = readReasoning(msg);
2120
+ if (reasoning) {
2121
+ rememberReasoningField(endpointKey, reasoning.field);
2122
+ if (thinkingEnabled) yield { type: "thinking_delta", text: reasoning.text };
2085
2123
  }
2086
2124
  if (typeof msg.content === "string" && msg.content) {
2087
2125
  yield { type: "text_delta", text: msg.content };
@@ -2109,15 +2147,16 @@ function* synthesizeEventsFromCompletion(completion, thinkingEnabled) {
2109
2147
  }
2110
2148
  yield { type: "done", stopReason: normalizeOpenAIStopReason(choice.finish_reason ?? null) };
2111
2149
  }
2112
- function completionToResponse(completion) {
2150
+ function completionToResponse(completion, endpointKey) {
2113
2151
  const choice = completion.choices?.[0];
2114
2152
  const contentParts = [];
2115
2153
  let textAccum = "";
2116
2154
  if (choice) {
2117
2155
  const msg = choice.message;
2118
- const reasoning = msg.reasoning_content;
2119
- if (typeof reasoning === "string" && reasoning) {
2120
- contentParts.push({ type: "thinking", text: reasoning });
2156
+ const reasoning = readReasoning(msg);
2157
+ if (reasoning) {
2158
+ rememberReasoningField(endpointKey, reasoning.field);
2159
+ contentParts.push({ type: "thinking", text: reasoning.text });
2121
2160
  }
2122
2161
  if (typeof msg.content === "string" && msg.content) {
2123
2162
  textAccum = msg.content;