@kenkaiiii/gg-ai 5.13.2 → 5.14.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 +39 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +39 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1766,11 +1766,16 @@ function getEnvironment() {
|
|
|
1766
1766
|
// src/providers/openai.ts
|
|
1767
1767
|
function extractOpenAIUsage(usage) {
|
|
1768
1768
|
let cacheRead = 0;
|
|
1769
|
+
let cacheWrite = 0;
|
|
1769
1770
|
const details = usage.prompt_tokens_details;
|
|
1770
1771
|
if (details?.cached_tokens) {
|
|
1771
1772
|
cacheRead = details.cached_tokens;
|
|
1772
1773
|
}
|
|
1773
1774
|
const usageAny = usage;
|
|
1775
|
+
const detailsAny = details;
|
|
1776
|
+
if (typeof detailsAny?.cache_write_tokens === "number") {
|
|
1777
|
+
cacheWrite = detailsAny.cache_write_tokens;
|
|
1778
|
+
}
|
|
1774
1779
|
if (!cacheRead && typeof usageAny.cached_tokens === "number" && usageAny.cached_tokens > 0) {
|
|
1775
1780
|
cacheRead = usageAny.cached_tokens;
|
|
1776
1781
|
}
|
|
@@ -1778,9 +1783,10 @@ function extractOpenAIUsage(usage) {
|
|
|
1778
1783
|
cacheRead = usageAny.prompt_cache_hit_tokens;
|
|
1779
1784
|
}
|
|
1780
1785
|
return {
|
|
1781
|
-
inputTokens: usage.prompt_tokens - cacheRead,
|
|
1786
|
+
inputTokens: usage.prompt_tokens - cacheRead - cacheWrite,
|
|
1782
1787
|
outputTokens: usage.completion_tokens,
|
|
1783
|
-
cacheRead
|
|
1788
|
+
cacheRead,
|
|
1789
|
+
cacheWrite
|
|
1784
1790
|
};
|
|
1785
1791
|
}
|
|
1786
1792
|
var openaiClientCache = /* @__PURE__ */ new Map();
|
|
@@ -1845,8 +1851,9 @@ async function* runStream2(options) {
|
|
|
1845
1851
|
if (options.provider === "openai" || options.provider === "moonshot") {
|
|
1846
1852
|
const paramsAny = params;
|
|
1847
1853
|
paramsAny.prompt_cache_key = normalizePromptCacheKey(options.promptCacheKey ?? "ggcoder");
|
|
1848
|
-
|
|
1849
|
-
|
|
1854
|
+
if (options.provider === "openai" && options.model.startsWith("gpt-5.6")) {
|
|
1855
|
+
paramsAny.prompt_cache_options = { mode: "implicit", ttl: "30m" };
|
|
1856
|
+
} else if ((options.cacheRetention ?? "short") === "long") {
|
|
1850
1857
|
paramsAny.prompt_cache_retention = "24h";
|
|
1851
1858
|
}
|
|
1852
1859
|
}
|
|
@@ -1897,6 +1904,7 @@ async function* runStream2(options) {
|
|
|
1897
1904
|
let inputTokens = 0;
|
|
1898
1905
|
let outputTokens = 0;
|
|
1899
1906
|
let cacheRead = 0;
|
|
1907
|
+
let cacheWrite = 0;
|
|
1900
1908
|
let finishReason = null;
|
|
1901
1909
|
let receivedAnyChunk = false;
|
|
1902
1910
|
try {
|
|
@@ -1904,7 +1912,7 @@ async function* runStream2(options) {
|
|
|
1904
1912
|
receivedAnyChunk = true;
|
|
1905
1913
|
const choice = chunk.choices?.[0];
|
|
1906
1914
|
if (chunk.usage) {
|
|
1907
|
-
({ inputTokens, outputTokens, cacheRead } = extractOpenAIUsage(chunk.usage));
|
|
1915
|
+
({ inputTokens, outputTokens, cacheRead, cacheWrite } = extractOpenAIUsage(chunk.usage));
|
|
1908
1916
|
}
|
|
1909
1917
|
if (!choice) continue;
|
|
1910
1918
|
if (choice.finish_reason) {
|
|
@@ -1984,7 +1992,12 @@ async function* runStream2(options) {
|
|
|
1984
1992
|
content: contentParts.length > 0 ? contentParts : textAccum || ""
|
|
1985
1993
|
},
|
|
1986
1994
|
stopReason,
|
|
1987
|
-
usage: {
|
|
1995
|
+
usage: {
|
|
1996
|
+
inputTokens,
|
|
1997
|
+
outputTokens,
|
|
1998
|
+
...cacheRead > 0 && { cacheRead },
|
|
1999
|
+
...cacheWrite > 0 && { cacheWrite }
|
|
2000
|
+
}
|
|
1988
2001
|
};
|
|
1989
2002
|
yield { type: "done", stopReason };
|
|
1990
2003
|
return response;
|
|
@@ -2057,8 +2070,9 @@ function completionToResponse(completion) {
|
|
|
2057
2070
|
let inputTokens = 0;
|
|
2058
2071
|
let outputTokens = 0;
|
|
2059
2072
|
let cacheRead = 0;
|
|
2073
|
+
let cacheWrite = 0;
|
|
2060
2074
|
if (completion.usage) {
|
|
2061
|
-
({ inputTokens, outputTokens, cacheRead } = extractOpenAIUsage(completion.usage));
|
|
2075
|
+
({ inputTokens, outputTokens, cacheRead, cacheWrite } = extractOpenAIUsage(completion.usage));
|
|
2062
2076
|
}
|
|
2063
2077
|
const stopReason = normalizeOpenAIStopReason(choice?.finish_reason ?? null);
|
|
2064
2078
|
return {
|
|
@@ -2067,7 +2081,12 @@ function completionToResponse(completion) {
|
|
|
2067
2081
|
content: contentParts.length > 0 ? contentParts : textAccum
|
|
2068
2082
|
},
|
|
2069
2083
|
stopReason,
|
|
2070
|
-
usage: {
|
|
2084
|
+
usage: {
|
|
2085
|
+
inputTokens,
|
|
2086
|
+
outputTokens,
|
|
2087
|
+
...cacheRead > 0 && { cacheRead },
|
|
2088
|
+
...cacheWrite > 0 && { cacheWrite }
|
|
2089
|
+
}
|
|
2071
2090
|
};
|
|
2072
2091
|
}
|
|
2073
2092
|
function classifyOpenAICompatLimit(args) {
|
|
@@ -2247,10 +2266,9 @@ async function* runStream3(options) {
|
|
|
2247
2266
|
if (options.accountId) {
|
|
2248
2267
|
headers["chatgpt-account-id"] = options.accountId;
|
|
2249
2268
|
}
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
headers["
|
|
2253
|
-
headers["x-client-request-id"] = cacheScopeId;
|
|
2269
|
+
if (options.transportSessionId) {
|
|
2270
|
+
headers["session_id"] = options.transportSessionId;
|
|
2271
|
+
headers["x-client-request-id"] = options.transportSessionId;
|
|
2254
2272
|
}
|
|
2255
2273
|
const response = await fetch(url, {
|
|
2256
2274
|
method: "POST",
|
|
@@ -2294,6 +2312,7 @@ async function* runStream3(options) {
|
|
|
2294
2312
|
let inputTokens = 0;
|
|
2295
2313
|
let outputTokens = 0;
|
|
2296
2314
|
let cacheRead = 0;
|
|
2315
|
+
let cacheWrite = 0;
|
|
2297
2316
|
const diagStart = Date.now();
|
|
2298
2317
|
const diagSeen = /* @__PURE__ */ new Set();
|
|
2299
2318
|
for await (const event of parseSSE(response.body)) {
|
|
@@ -2470,7 +2489,8 @@ async function* runStream3(options) {
|
|
|
2470
2489
|
const usage = resp?.usage;
|
|
2471
2490
|
if (usage) {
|
|
2472
2491
|
cacheRead = usage.input_tokens_details?.cached_tokens ?? 0;
|
|
2473
|
-
|
|
2492
|
+
cacheWrite = usage.input_tokens_details?.cache_write_tokens ?? 0;
|
|
2493
|
+
inputTokens = (usage.input_tokens ?? 0) - cacheRead - cacheWrite;
|
|
2474
2494
|
outputTokens = usage.output_tokens ?? 0;
|
|
2475
2495
|
}
|
|
2476
2496
|
}
|
|
@@ -2518,7 +2538,12 @@ async function* runStream3(options) {
|
|
|
2518
2538
|
content: contentParts.length > 0 ? contentParts : textAccum || ""
|
|
2519
2539
|
},
|
|
2520
2540
|
stopReason,
|
|
2521
|
-
usage: {
|
|
2541
|
+
usage: {
|
|
2542
|
+
inputTokens,
|
|
2543
|
+
outputTokens,
|
|
2544
|
+
...cacheRead > 0 && { cacheRead },
|
|
2545
|
+
...cacheWrite > 0 && { cacheWrite }
|
|
2546
|
+
}
|
|
2522
2547
|
};
|
|
2523
2548
|
yield { type: "done", stopReason };
|
|
2524
2549
|
return streamResponse;
|