@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.cjs +56 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +56 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -561,6 +561,39 @@ function normalizeRootForAnthropic(schema) {
|
|
|
561
561
|
return out;
|
|
562
562
|
}
|
|
563
563
|
|
|
564
|
+
// src/providers/reasoning-field.ts
|
|
565
|
+
var REASONING_FIELD_ALIASES = [
|
|
566
|
+
"reasoning_content",
|
|
567
|
+
"reasoning",
|
|
568
|
+
"reasoning_text"
|
|
569
|
+
];
|
|
570
|
+
var DEFAULT_REASONING_FIELD = REASONING_FIELD_ALIASES[0];
|
|
571
|
+
function readReasoning(obj) {
|
|
572
|
+
if (!obj) return void 0;
|
|
573
|
+
for (const field of REASONING_FIELD_ALIASES) {
|
|
574
|
+
const value = obj[field];
|
|
575
|
+
if (typeof value === "string" && value) return { field, text: value };
|
|
576
|
+
}
|
|
577
|
+
return void 0;
|
|
578
|
+
}
|
|
579
|
+
function reasoningFieldKey(provider, baseUrl, model) {
|
|
580
|
+
return `${provider}|${baseUrl ?? ""}|${model}`;
|
|
581
|
+
}
|
|
582
|
+
var MAX_REMEMBERED_ENDPOINTS = 64;
|
|
583
|
+
var detectedFields = /* @__PURE__ */ new Map();
|
|
584
|
+
function rememberReasoningField(key, field) {
|
|
585
|
+
if (detectedFields.get(key) === field) return;
|
|
586
|
+
detectedFields.set(key, field);
|
|
587
|
+
while (detectedFields.size > MAX_REMEMBERED_ENDPOINTS) {
|
|
588
|
+
const oldest = detectedFields.keys().next();
|
|
589
|
+
if (oldest.done) break;
|
|
590
|
+
detectedFields.delete(oldest.value);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
function getReasoningField(key) {
|
|
594
|
+
return detectedFields.get(key) ?? DEFAULT_REASONING_FIELD;
|
|
595
|
+
}
|
|
596
|
+
|
|
564
597
|
// src/providers/transform.ts
|
|
565
598
|
function hasValidThinkingSignature(part) {
|
|
566
599
|
return typeof part.signature === "string" && part.signature.trim().length > 0;
|
|
@@ -981,6 +1014,7 @@ function remapToolCallId(id, idMap) {
|
|
|
981
1014
|
return mapped;
|
|
982
1015
|
}
|
|
983
1016
|
function toOpenAIMessages(messages, options) {
|
|
1017
|
+
const reasoningField = options?.reasoningField || DEFAULT_REASONING_FIELD;
|
|
984
1018
|
const out = [];
|
|
985
1019
|
const idMap = /* @__PURE__ */ new Map();
|
|
986
1020
|
const mergeToolResultText = options?.provider === "glm";
|
|
@@ -1047,9 +1081,9 @@ function toOpenAIMessages(messages, options) {
|
|
|
1047
1081
|
...hasToolCalls ? { tool_calls: toolCalls } : {}
|
|
1048
1082
|
};
|
|
1049
1083
|
if (thinkingParts) {
|
|
1050
|
-
assistantMsg
|
|
1084
|
+
assistantMsg[reasoningField] = thinkingParts;
|
|
1051
1085
|
} else if (options?.thinking && hasToolCalls && options.provider !== "glm") {
|
|
1052
|
-
assistantMsg
|
|
1086
|
+
assistantMsg[reasoningField] = " ";
|
|
1053
1087
|
}
|
|
1054
1088
|
out.push(assistantMsg);
|
|
1055
1089
|
continue;
|
|
@@ -1922,6 +1956,7 @@ function streamOpenAI(options) {
|
|
|
1922
1956
|
async function* runStream2(options) {
|
|
1923
1957
|
const providerName = options.provider ?? "openai";
|
|
1924
1958
|
const useStreaming = options.streaming !== false;
|
|
1959
|
+
const endpointKey = reasoningFieldKey(providerName, options.baseUrl, options.model);
|
|
1925
1960
|
const client = createClient2(options);
|
|
1926
1961
|
const isKimiK3 = options.provider === "moonshot" && options.model === "kimi-k3";
|
|
1927
1962
|
const isManagedKimiK3 = isKimiK3 && options.baseUrl?.replace(/\/+$/, "").endsWith("/coding/v1") === true;
|
|
@@ -1945,7 +1980,8 @@ async function* runStream2(options) {
|
|
|
1945
1980
|
// disabled K3 must NOT carry placeholder reasoning_content (mirrors the
|
|
1946
1981
|
// official CLI: reasoning is preserved only while thinking is enabled).
|
|
1947
1982
|
thinking: isKimiK27 || !!options.thinking,
|
|
1948
|
-
supportsImages: options.supportsImages
|
|
1983
|
+
supportsImages: options.supportsImages,
|
|
1984
|
+
reasoningField: getReasoningField(endpointKey)
|
|
1949
1985
|
});
|
|
1950
1986
|
const defaultTemp = options.provider === "glm" ? 0.6 : void 0;
|
|
1951
1987
|
const effectiveTemp = options.temperature ?? defaultTemp;
|
|
@@ -2007,8 +2043,8 @@ async function* runStream2(options) {
|
|
|
2007
2043
|
const completion = await client.chat.completions.create(params, {
|
|
2008
2044
|
signal: options.signal ?? void 0
|
|
2009
2045
|
});
|
|
2010
|
-
yield* synthesizeEventsFromCompletion(completion, !!options.thinking);
|
|
2011
|
-
return completionToResponse(completion);
|
|
2046
|
+
yield* synthesizeEventsFromCompletion(completion, !!options.thinking, endpointKey);
|
|
2047
|
+
return completionToResponse(completion, endpointKey);
|
|
2012
2048
|
} catch (err) {
|
|
2013
2049
|
throw toError2(err, providerName);
|
|
2014
2050
|
}
|
|
@@ -2043,11 +2079,12 @@ async function* runStream2(options) {
|
|
|
2043
2079
|
finishReason = choice.finish_reason;
|
|
2044
2080
|
}
|
|
2045
2081
|
const delta = choice.delta;
|
|
2046
|
-
const
|
|
2047
|
-
if (
|
|
2048
|
-
|
|
2082
|
+
const reasoning = readReasoning(delta);
|
|
2083
|
+
if (reasoning) {
|
|
2084
|
+
rememberReasoningField(endpointKey, reasoning.field);
|
|
2085
|
+
thinkingAccum += reasoning.text;
|
|
2049
2086
|
if (options.thinking) {
|
|
2050
|
-
yield { type: "thinking_delta", text:
|
|
2087
|
+
yield { type: "thinking_delta", text: reasoning.text };
|
|
2051
2088
|
}
|
|
2052
2089
|
}
|
|
2053
2090
|
if (delta.content) {
|
|
@@ -2132,16 +2169,17 @@ async function* runStream2(options) {
|
|
|
2132
2169
|
yield { type: "done", stopReason };
|
|
2133
2170
|
return response;
|
|
2134
2171
|
}
|
|
2135
|
-
function* synthesizeEventsFromCompletion(completion, thinkingEnabled) {
|
|
2172
|
+
function* synthesizeEventsFromCompletion(completion, thinkingEnabled, endpointKey) {
|
|
2136
2173
|
const choice = completion.choices?.[0];
|
|
2137
2174
|
if (!choice) {
|
|
2138
2175
|
yield { type: "done", stopReason: normalizeOpenAIStopReason(null) };
|
|
2139
2176
|
return;
|
|
2140
2177
|
}
|
|
2141
2178
|
const msg = choice.message;
|
|
2142
|
-
const reasoning = msg
|
|
2143
|
-
if (
|
|
2144
|
-
|
|
2179
|
+
const reasoning = readReasoning(msg);
|
|
2180
|
+
if (reasoning) {
|
|
2181
|
+
rememberReasoningField(endpointKey, reasoning.field);
|
|
2182
|
+
if (thinkingEnabled) yield { type: "thinking_delta", text: reasoning.text };
|
|
2145
2183
|
}
|
|
2146
2184
|
if (typeof msg.content === "string" && msg.content) {
|
|
2147
2185
|
yield { type: "text_delta", text: msg.content };
|
|
@@ -2169,15 +2207,16 @@ function* synthesizeEventsFromCompletion(completion, thinkingEnabled) {
|
|
|
2169
2207
|
}
|
|
2170
2208
|
yield { type: "done", stopReason: normalizeOpenAIStopReason(choice.finish_reason ?? null) };
|
|
2171
2209
|
}
|
|
2172
|
-
function completionToResponse(completion) {
|
|
2210
|
+
function completionToResponse(completion, endpointKey) {
|
|
2173
2211
|
const choice = completion.choices?.[0];
|
|
2174
2212
|
const contentParts = [];
|
|
2175
2213
|
let textAccum = "";
|
|
2176
2214
|
if (choice) {
|
|
2177
2215
|
const msg = choice.message;
|
|
2178
|
-
const reasoning = msg
|
|
2179
|
-
if (
|
|
2180
|
-
|
|
2216
|
+
const reasoning = readReasoning(msg);
|
|
2217
|
+
if (reasoning) {
|
|
2218
|
+
rememberReasoningField(endpointKey, reasoning.field);
|
|
2219
|
+
contentParts.push({ type: "thinking", text: reasoning.text });
|
|
2181
2220
|
}
|
|
2182
2221
|
if (typeof msg.content === "string" && msg.content) {
|
|
2183
2222
|
textAccum = msg.content;
|