pi-provider-kimi-code 0.6.8 → 0.6.10
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/README.md +48 -112
- package/index.ts +195 -25
- package/package.json +9 -5
- package/src/constants.ts +6 -2
- package/src/device.ts +59 -17
- package/src/models.ts +61 -6
- package/src/oauth.ts +7 -1
- package/src/payload.ts +154 -40
- package/src/stream.ts +32 -4
- package/src/tools/datasource.ts +22 -1
- package/src/usage.ts +98 -8
package/src/constants.ts
CHANGED
|
@@ -41,12 +41,12 @@ export function getDefaultBaseUrl(protocol: KimiWireProtocol): string {
|
|
|
41
41
|
|
|
42
42
|
export const DEFAULT_BASE_URL = getDefaultBaseUrl(ENV_KIMI_CODE_PROTOCOL);
|
|
43
43
|
|
|
44
|
-
export const PROVIDER_VERSION = "0.6.
|
|
44
|
+
export const PROVIDER_VERSION = "0.6.10";
|
|
45
45
|
|
|
46
46
|
// Upstream kimi-code CLI version — used in User-Agent and X-Msh-Version
|
|
47
47
|
// headers to match the official client's identity. Update this when
|
|
48
48
|
// syncing with https://github.com/MoonshotAI/kimi-code.
|
|
49
|
-
export const KIMI_UPSTREAM_VERSION = "0.
|
|
49
|
+
export const KIMI_UPSTREAM_VERSION = "0.34.0";
|
|
50
50
|
export const KIMI_CODE_USER_AGENT = `kimi-code-cli/${KIMI_UPSTREAM_VERSION}`;
|
|
51
51
|
export const KIMI_PLATFORM = "kimi_code_cli";
|
|
52
52
|
|
|
@@ -63,6 +63,10 @@ export const RETRYABLE_REFRESH_STATUSES = new Set([429, 500, 502, 503, 504]);
|
|
|
63
63
|
|
|
64
64
|
export const PROVIDER_ID = "kimi-coding";
|
|
65
65
|
|
|
66
|
+
// Owner tag for the entries this extension adds to pi-ai's global api-provider
|
|
67
|
+
// registry, so they can be identified and removed as a group.
|
|
68
|
+
export const KIMI_GLOBAL_FALLBACK_SOURCE_ID = "pi-provider-kimi-code-global-fallback";
|
|
69
|
+
|
|
66
70
|
export function getOAuthHost(): string {
|
|
67
71
|
const value =
|
|
68
72
|
process.env.KIMI_CODE_OAUTH_HOST || process.env.KIMI_OAUTH_HOST || DEFAULT_OAUTH_HOST;
|
package/src/device.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Device identification: device-id persistence, Kimi Code-compatible request
|
|
2
2
|
// headers, and the cross-platform device-model string. Pure helpers live near
|
|
3
|
-
// the side-effecting one-shots (file IO
|
|
3
|
+
// the side-effecting one-shots (file IO) they support.
|
|
4
4
|
|
|
5
|
-
import { execFileSync } from "node:child_process";
|
|
6
5
|
import { randomBytes } from "node:crypto";
|
|
7
6
|
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
8
|
import os from "node:os";
|
|
9
9
|
import { dirname } from "node:path";
|
|
10
10
|
|
|
@@ -97,25 +97,68 @@ export function computeDeviceModel(input: DeviceModelInput): string {
|
|
|
97
97
|
return `${system} ${arch}`.trim();
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
100
|
+
// Where macOS keeps its product version. Upstream Kimi Code reads it through
|
|
101
|
+
// Python's platform.mac_ver(), which parses this same file, so it is the same
|
|
102
|
+
// source of truth as `sw_vers -productVersion` without the process spawn: a
|
|
103
|
+
// spawn costs ~10ms of blocked event loop, and pi pays it again on every
|
|
104
|
+
// /reload because it re-evaluates extension modules.
|
|
105
|
+
const MAC_PRODUCT_VERSION_PATH = "/System/Library/CoreServices/SystemVersion.plist";
|
|
106
|
+
|
|
107
|
+
// Exposed for tests. The plist is a flat XML property list, so the version sits
|
|
108
|
+
// in the <string> immediately after the ProductVersion key.
|
|
109
|
+
export function parseMacProductVersion(plist: string): string | undefined {
|
|
110
|
+
const match = /<key>ProductVersion<\/key>\s*<string>([^<]*)<\/string>/.exec(plist);
|
|
111
|
+
return match?.[1].trim() || undefined;
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
let DEVICE_MODEL: string | undefined;
|
|
115
|
+
|
|
116
|
+
function setDeviceModel(macVersion: string | undefined): string {
|
|
117
|
+
DEVICE_MODEL ??= computeDeviceModel({
|
|
114
118
|
platform: process.platform,
|
|
115
119
|
release: os.release(),
|
|
116
120
|
arch: os.machine() || process.arch,
|
|
117
|
-
macVersion
|
|
121
|
+
macVersion,
|
|
118
122
|
});
|
|
123
|
+
return DEVICE_MODEL;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function loadMacDeviceModel(): Promise<void> {
|
|
127
|
+
try {
|
|
128
|
+
setDeviceModel(parseMacProductVersion(await readFile(MAC_PRODUCT_VERSION_PATH, "utf-8")));
|
|
129
|
+
} catch {
|
|
130
|
+
setDeviceModel(undefined);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Settles once the module-load lookup has produced a device model. Nothing in
|
|
136
|
+
* the provider waits for it — headers stay synchronous — but tests use it to
|
|
137
|
+
* exercise the async path without racing the fallback below.
|
|
138
|
+
*/
|
|
139
|
+
export const deviceModelReady: Promise<void> = (() => {
|
|
140
|
+
if (process.platform !== "darwin") {
|
|
141
|
+
// Nothing to read: every other platform derives the model from os.release().
|
|
142
|
+
setDeviceModel(undefined);
|
|
143
|
+
return Promise.resolve();
|
|
144
|
+
}
|
|
145
|
+
// Read the version off the event loop at module load so the value is usually
|
|
146
|
+
// in place before anything asks for a header.
|
|
147
|
+
return loadMacDeviceModel();
|
|
148
|
+
})();
|
|
149
|
+
|
|
150
|
+
function getDeviceModel(): string {
|
|
151
|
+
if (DEVICE_MODEL !== undefined) return DEVICE_MODEL;
|
|
152
|
+
// A header was needed before the async read landed. Read the same file
|
|
153
|
+
// synchronously rather than reporting the kernel release, which would put a
|
|
154
|
+
// different device model on the wire than every later request. This costs
|
|
155
|
+
// ~0.02ms against the ~10ms a `sw_vers` subprocess used to cost, and both
|
|
156
|
+
// paths memoize into the same slot.
|
|
157
|
+
try {
|
|
158
|
+
return setDeviceModel(parseMacProductVersion(readFileSync(MAC_PRODUCT_VERSION_PATH, "utf-8")));
|
|
159
|
+
} catch {
|
|
160
|
+
return setDeviceModel(undefined);
|
|
161
|
+
}
|
|
119
162
|
}
|
|
120
163
|
|
|
121
164
|
export function getOsVersion(): string {
|
|
@@ -133,7 +176,6 @@ export function asciiHeaderValue(value: string, fallback = "unknown"): string {
|
|
|
133
176
|
return sanitized || fallback;
|
|
134
177
|
}
|
|
135
178
|
|
|
136
|
-
const DEVICE_MODEL = getDeviceModel();
|
|
137
179
|
let DEVICE_ID: string | null = null;
|
|
138
180
|
|
|
139
181
|
function getStableDeviceId(): string {
|
|
@@ -174,7 +216,7 @@ export function getCommonHeaders(): Record<string, string> {
|
|
|
174
216
|
"X-Msh-Platform": KIMI_PLATFORM,
|
|
175
217
|
"X-Msh-Version": KIMI_UPSTREAM_VERSION,
|
|
176
218
|
"X-Msh-Device-Name": os.hostname(),
|
|
177
|
-
"X-Msh-Device-Model":
|
|
219
|
+
"X-Msh-Device-Model": getDeviceModel(),
|
|
178
220
|
"X-Msh-Os-Version": getOsVersion(),
|
|
179
221
|
"X-Msh-Device-Id": getStableDeviceId(),
|
|
180
222
|
};
|
package/src/models.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
ModelConfig,
|
|
9
9
|
ModelReasoningMap,
|
|
10
10
|
} from "./config.ts";
|
|
11
|
+
import { DEFAULT_KIMI_CODE_CONFIG } from "./config.ts";
|
|
11
12
|
|
|
12
13
|
import { type KimiWireProtocol, getBaseUrl } from "./constants.ts";
|
|
13
14
|
import { getKimiProviderHeaders } from "./device.ts";
|
|
@@ -36,6 +37,10 @@ const DEFAULT_DISCOVERY_TIMEOUT_MS = 2500;
|
|
|
36
37
|
|
|
37
38
|
export interface DiscoverKimiModelMetadataOptions {
|
|
38
39
|
timeoutMs?: number;
|
|
40
|
+
// 401 recovery for runtime callers (extension startup, settings menu).
|
|
41
|
+
// oauth.ts's login/refresh flows must NOT pass this: they call discovery
|
|
42
|
+
// while holding the OAuth lock, and refreshKimiAuthToken would re-enter it.
|
|
43
|
+
refreshAccessToken?: (currentToken: string) => Promise<string | null>;
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
function mergeInputModalities(
|
|
@@ -84,6 +89,36 @@ function resolveModelCost(
|
|
|
84
89
|
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
85
90
|
}
|
|
86
91
|
|
|
92
|
+
// Policy: a model's output cap tracks its context window unless the user
|
|
93
|
+
// explicitly configured model.maxTokens. pi-ai clamps the request cap to
|
|
94
|
+
// contextWindow - used - 4096 (clampMaxTokensToContext in
|
|
95
|
+
// @earendil-works/pi-ai), so setting maxTokens to the window yields the official
|
|
96
|
+
// kimi-code behavior (computeCompletionBudgetCap: maxCtx, clamped to
|
|
97
|
+
// maxCtx - usedContext) instead of a fixed small cap that truncates long
|
|
98
|
+
// max-effort reasoning (reasoning_content counts toward the cap). A deliberate
|
|
99
|
+
// per-request cap still wins (options.maxTokens ?? model.maxTokens). Reconcile
|
|
100
|
+
// wherever contextWindow is authored so the two stay in lockstep.
|
|
101
|
+
//
|
|
102
|
+
// "Explicitly configured" means any value other than the built-in default:
|
|
103
|
+
// ensureKimiCodeConfig materializes the full default config (including
|
|
104
|
+
// maxTokens) into the home config file, so config-source tracking cannot
|
|
105
|
+
// distinguish a deliberate 32000 from a materialized default. A user who
|
|
106
|
+
// explicitly sets the default value gets the window cap — indistinguishable
|
|
107
|
+
// from the materialized-default case, and the intended new behavior anyway.
|
|
108
|
+
const DEFAULT_MODEL_MAX_TOKENS = DEFAULT_KIMI_CODE_CONFIG.model.maxTokens;
|
|
109
|
+
|
|
110
|
+
// True while the cap is "unset": still the built-in default, or already
|
|
111
|
+
// tracking the context window (set by an earlier withWindowCappedMaxTokens
|
|
112
|
+
// call, before discovery grew the window).
|
|
113
|
+
function isWindowTrackedMaxTokens(model: Model<Api>): boolean {
|
|
114
|
+
return model.maxTokens === DEFAULT_MODEL_MAX_TOKENS || model.maxTokens === model.contextWindow;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function withWindowCappedMaxTokens<M extends Model<Api>>(model: M): M {
|
|
118
|
+
if (!isWindowTrackedMaxTokens(model)) return model;
|
|
119
|
+
return { ...model, maxTokens: model.contextWindow };
|
|
120
|
+
}
|
|
121
|
+
|
|
87
122
|
export function buildKimiModelFromConfig(
|
|
88
123
|
config: ModelConfig,
|
|
89
124
|
modelId = KIMI_CODING_MODEL_ID,
|
|
@@ -97,7 +132,7 @@ export function buildKimiModelFromConfig(
|
|
|
97
132
|
: modelId === KIMI_CODING_MODEL_ID
|
|
98
133
|
? "Kimi for Coding"
|
|
99
134
|
: modelId;
|
|
100
|
-
return {
|
|
135
|
+
return withWindowCappedMaxTokens({
|
|
101
136
|
id: modelId,
|
|
102
137
|
name,
|
|
103
138
|
reasoning: config.reasoning,
|
|
@@ -105,7 +140,7 @@ export function buildKimiModelFromConfig(
|
|
|
105
140
|
cost: { ...resolveModelCost(modelId) },
|
|
106
141
|
contextWindow: config.contextWindow,
|
|
107
142
|
maxTokens: config.maxTokens,
|
|
108
|
-
} as Model<Api
|
|
143
|
+
} as Model<Api>);
|
|
109
144
|
}
|
|
110
145
|
|
|
111
146
|
export function resolveKimiModelConfig(
|
|
@@ -225,15 +260,30 @@ export async function discoverKimiModelMetadata(
|
|
|
225
260
|
const controller = new AbortController();
|
|
226
261
|
const timeout =
|
|
227
262
|
timeoutMs > 0 ? setTimeout(() => controller.abort(), timeoutMs).unref() : undefined;
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
signal
|
|
263
|
+
const requestModels = (token: string, signal: AbortSignal) =>
|
|
264
|
+
fetch(modelsUrl, {
|
|
265
|
+
signal,
|
|
231
266
|
headers: {
|
|
232
267
|
...getKimiProviderHeaders(),
|
|
233
|
-
Authorization: `Bearer ${
|
|
268
|
+
Authorization: `Bearer ${token}`,
|
|
234
269
|
Accept: "application/json",
|
|
235
270
|
},
|
|
236
271
|
});
|
|
272
|
+
try {
|
|
273
|
+
let response = await requestModels(accessToken, controller.signal);
|
|
274
|
+
if (response.status === 401 && options.refreshAccessToken) {
|
|
275
|
+
const refreshed = await options.refreshAccessToken(accessToken);
|
|
276
|
+
if (refreshed && refreshed !== accessToken) {
|
|
277
|
+
const retryController = new AbortController();
|
|
278
|
+
const retryTimeout =
|
|
279
|
+
timeoutMs > 0 ? setTimeout(() => retryController.abort(), timeoutMs).unref() : undefined;
|
|
280
|
+
try {
|
|
281
|
+
response = await requestModels(refreshed, retryController.signal);
|
|
282
|
+
} finally {
|
|
283
|
+
if (retryTimeout) clearTimeout(retryTimeout);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
237
287
|
if (!response.ok) return {};
|
|
238
288
|
const json = (await response.json()) as { data?: unknown };
|
|
239
289
|
const list = Array.isArray(json.data) ? (json.data as KimiServerModel[]) : [];
|
|
@@ -306,6 +356,9 @@ export function applyKimiOAuthExtrasToModel(
|
|
|
306
356
|
extras: KimiModelMetadata,
|
|
307
357
|
reasoningMap?: ModelReasoningMap,
|
|
308
358
|
): Model<Api> {
|
|
359
|
+
// Capture before extras grow the window: a window-tracked cap must follow
|
|
360
|
+
// the new window, an explicit cap must survive unchanged.
|
|
361
|
+
const windowTracked = isWindowTrackedMaxTokens(model);
|
|
309
362
|
const next: Model<Api> & {
|
|
310
363
|
wireModelId?: string;
|
|
311
364
|
supportsThinkingType?: "only" | "no" | "both";
|
|
@@ -347,5 +400,7 @@ export function applyKimiOAuthExtrasToModel(
|
|
|
347
400
|
if (thinkingLevelMap) next.thinkingLevelMap = thinkingLevelMap;
|
|
348
401
|
else delete next.thinkingLevelMap;
|
|
349
402
|
}
|
|
403
|
+
// Reconcile the output cap with the (possibly discovery-updated) window.
|
|
404
|
+
if (windowTracked) next.maxTokens = next.contextWindow;
|
|
350
405
|
return next;
|
|
351
406
|
}
|
package/src/oauth.ts
CHANGED
|
@@ -658,7 +658,13 @@ export function isKimiAuthErrorMessage(message: unknown): boolean {
|
|
|
658
658
|
/\b401\b/.test(text) ||
|
|
659
659
|
text.includes("unauthorized") ||
|
|
660
660
|
text.includes("incorrect api key") ||
|
|
661
|
-
text.includes("invalid api key")
|
|
661
|
+
text.includes("invalid api key") ||
|
|
662
|
+
// Kimi's production 401 body: "The API Key appears to be invalid or may
|
|
663
|
+
// have expired..." with type "invalid_authentication_error". Neither
|
|
664
|
+
// contains the substrings above, so without these the digit fallback is
|
|
665
|
+
// the only match and vanishes when a transport strips the status prefix.
|
|
666
|
+
text.includes("api key appears to be invalid") ||
|
|
667
|
+
text.includes("invalid_authentication_error")
|
|
662
668
|
);
|
|
663
669
|
}
|
|
664
670
|
|
package/src/payload.ts
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
// per-protocol message transforms, OpenAI tool-call / tool-schema normalizers,
|
|
3
3
|
// and the top-level applyKimiPayloadMutations that orchestrates them.
|
|
4
4
|
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
|
|
5
7
|
import type { CacheRetention, ThinkingLevel } from "@earendil-works/pi-ai";
|
|
6
8
|
import type { KimiResolvedModelConfig, ModelReasoningEntry } from "./config.ts";
|
|
7
9
|
|
|
8
10
|
import { getBaseUrl } from "./constants.ts";
|
|
9
11
|
import { getKimiProviderHeaders } from "./device.ts";
|
|
12
|
+
import { refreshKimiAuthToken } from "./oauth.ts";
|
|
10
13
|
import { optimizeToolSchemas } from "./schema-dedup.ts";
|
|
11
14
|
|
|
12
15
|
// =============================================================================
|
|
@@ -30,6 +33,7 @@ export function resolveCacheRetention(value?: CacheRetention): CacheRetention {
|
|
|
30
33
|
export interface KimiPayloadContext {
|
|
31
34
|
api: "anthropic-messages" | "openai-completions";
|
|
32
35
|
upload?: Uploader;
|
|
36
|
+
uploadCacheScope?: string;
|
|
33
37
|
cacheKey?: string;
|
|
34
38
|
cacheRetention: CacheRetention;
|
|
35
39
|
reasoning?: ThinkingLevel;
|
|
@@ -80,6 +84,16 @@ function getUploadFilename(mimeType: string): string {
|
|
|
80
84
|
"image/png": "upload.png",
|
|
81
85
|
"image/gif": "upload.gif",
|
|
82
86
|
"image/webp": "upload.webp",
|
|
87
|
+
// Video extensions mirror upstream kimi-code's MIME_TO_EXT
|
|
88
|
+
// (packages/kosong/src/providers/kimi-files.ts).
|
|
89
|
+
"video/mp4": "upload.mp4",
|
|
90
|
+
"video/mpeg": "upload.mpeg",
|
|
91
|
+
"video/quicktime": "upload.mov",
|
|
92
|
+
"video/webm": "upload.webm",
|
|
93
|
+
"video/x-matroska": "upload.mkv",
|
|
94
|
+
"video/x-msvideo": "upload.avi",
|
|
95
|
+
"video/x-flv": "upload.flv",
|
|
96
|
+
"video/3gpp": "upload.3gp",
|
|
83
97
|
};
|
|
84
98
|
return map[mimeType] ?? "upload.bin";
|
|
85
99
|
}
|
|
@@ -88,22 +102,34 @@ function getUploadFilename(mimeType: string): string {
|
|
|
88
102
|
// File upload (I/O edge)
|
|
89
103
|
// =============================================================================
|
|
90
104
|
|
|
105
|
+
export interface UploadKimiFileDeps {
|
|
106
|
+
fetch?: typeof fetch;
|
|
107
|
+
refreshAccessToken?: (currentToken: string) => Promise<string | null>;
|
|
108
|
+
}
|
|
109
|
+
|
|
91
110
|
export async function uploadKimiFile(
|
|
92
111
|
apiKey: string,
|
|
93
112
|
mimeType: string,
|
|
94
113
|
data: string,
|
|
95
114
|
thresholdBytes?: number,
|
|
115
|
+
deps?: UploadKimiFileDeps,
|
|
96
116
|
): Promise<string | null> {
|
|
117
|
+
const fetchImpl = deps?.fetch ?? fetch;
|
|
118
|
+
const refreshAccessToken = deps?.refreshAccessToken ?? refreshKimiAuthToken;
|
|
97
119
|
const buffer = Buffer.from(data, "base64");
|
|
98
|
-
|
|
120
|
+
const isVideo = mimeType.startsWith("video/");
|
|
121
|
+
if (!mimeType.startsWith("image/") && !isVideo) return null;
|
|
99
122
|
const threshold =
|
|
100
123
|
thresholdBytes ?? parseInlineUploadThreshold(process.env.KIMI_CODE_UPLOAD_THRESHOLD_BYTES);
|
|
101
|
-
|
|
124
|
+
// The inline threshold applies to images only: the Kimi API has no inline
|
|
125
|
+
// video path (upstream kimi-code uploads every video via /files), so videos
|
|
126
|
+
// always upload.
|
|
127
|
+
if (!isVideo && buffer.length <= threshold) return null;
|
|
102
128
|
|
|
103
129
|
const filename = getUploadFilename(mimeType);
|
|
104
130
|
const formData = new FormData();
|
|
105
131
|
formData.append("file", new Blob([buffer], { type: mimeType }), filename);
|
|
106
|
-
formData.append("purpose", "image");
|
|
132
|
+
formData.append("purpose", isVideo ? "video" : "image");
|
|
107
133
|
|
|
108
134
|
const uploadUrl = `${deriveFilesBaseUrl(getBaseUrl())}/files`;
|
|
109
135
|
const debug = process.env.KIMI_CODE_DEBUG === "1";
|
|
@@ -113,13 +139,31 @@ export async function uploadKimiFile(
|
|
|
113
139
|
);
|
|
114
140
|
}
|
|
115
141
|
|
|
116
|
-
|
|
117
|
-
|
|
142
|
+
const postUpload = (token: string) =>
|
|
143
|
+
fetchImpl(uploadUrl, {
|
|
118
144
|
method: "POST",
|
|
119
|
-
headers: { ...getKimiProviderHeaders(), Authorization: `Bearer ${
|
|
145
|
+
headers: { ...getKimiProviderHeaders(), Authorization: `Bearer ${token}` },
|
|
120
146
|
body: formData,
|
|
121
147
|
});
|
|
122
|
-
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
let response = await postUpload(apiKey);
|
|
151
|
+
// Kimi access tokens are short-lived and invalidated server-side as soon
|
|
152
|
+
// as any peer process rotates them, so a 401 here usually means our key
|
|
153
|
+
// snapshot went stale mid-session, not that login is broken. Mirror the
|
|
154
|
+
// chat-stream recovery: force one refresh and retry once.
|
|
155
|
+
let responseText: string | undefined;
|
|
156
|
+
if (response.status === 401) {
|
|
157
|
+
responseText = await response.text();
|
|
158
|
+
const refreshed = await refreshAccessToken(apiKey);
|
|
159
|
+
if (refreshed && refreshed !== apiKey) {
|
|
160
|
+
console.error("[kimi-coding] upload got 401, retrying with refreshed token");
|
|
161
|
+
response = await postUpload(refreshed);
|
|
162
|
+
responseText = undefined;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (!response.ok)
|
|
166
|
+
throw new Error(`${response.status} ${responseText ?? (await response.text())}`);
|
|
123
167
|
const fileObj = (await response.json()) as { id?: string };
|
|
124
168
|
if (!fileObj.id) throw new Error("missing file id");
|
|
125
169
|
const fileUrl = `ms://${fileObj.id}`;
|
|
@@ -132,24 +176,69 @@ export async function uploadKimiFile(
|
|
|
132
176
|
}
|
|
133
177
|
|
|
134
178
|
// =============================================================================
|
|
135
|
-
// Payload file transformers
|
|
179
|
+
// Payload file transformers
|
|
136
180
|
//
|
|
137
181
|
// These walk the provider-specific payload shape and replace inline base64
|
|
138
182
|
// image blocks with ms:// references returned by the injected uploader. They
|
|
139
183
|
// take an Uploader rather than an apiKey so they can be unit-tested with a
|
|
140
184
|
// fake uploader; all network I/O stays behind that boundary.
|
|
185
|
+
//
|
|
186
|
+
// Successful uploads are remembered in a module-level cache shared across
|
|
187
|
+
// requests: payloads are rebuilt from session context every request, so
|
|
188
|
+
// without it a conversation's images would re-upload on every turn. Keys are
|
|
189
|
+
// content hashes rather than the data URLs themselves so the cache does not
|
|
190
|
+
// retain every image's base64 payload in memory. Failures are not cached and
|
|
191
|
+
// retry on the next request.
|
|
141
192
|
// =============================================================================
|
|
142
193
|
|
|
143
|
-
|
|
194
|
+
const MAX_UPLOADED_FILE_CACHE_ENTRIES = 512;
|
|
195
|
+
const uploadedFileCache = new Map<string, string>();
|
|
196
|
+
|
|
197
|
+
function uploadedFileCacheKey(cacheScope: string, mimeType: string, data: string): string {
|
|
198
|
+
return createHash("sha256")
|
|
199
|
+
.update(cacheScope)
|
|
200
|
+
.update("\0")
|
|
201
|
+
.update(mimeType)
|
|
202
|
+
.update("\0")
|
|
203
|
+
.update(data)
|
|
204
|
+
.digest("hex");
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function rememberUploadedFile(
|
|
208
|
+
uploadCache: Map<string, string>,
|
|
209
|
+
cacheKey: string,
|
|
210
|
+
url: string,
|
|
211
|
+
): void {
|
|
212
|
+
if (
|
|
213
|
+
uploadCache === uploadedFileCache &&
|
|
214
|
+
!uploadCache.has(cacheKey) &&
|
|
215
|
+
uploadCache.size >= MAX_UPLOADED_FILE_CACHE_ENTRIES
|
|
216
|
+
) {
|
|
217
|
+
const oldest = uploadCache.keys().next().value;
|
|
218
|
+
if (oldest !== undefined) uploadCache.delete(oldest);
|
|
219
|
+
}
|
|
220
|
+
uploadCache.set(cacheKey, url);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function clearKimiUploadedFileCache(): void {
|
|
224
|
+
uploadedFileCache.clear();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function transformOpenAIPayloadFiles(
|
|
228
|
+
payload: JsonRecord,
|
|
229
|
+
upload: Uploader,
|
|
230
|
+
uploadCache: Map<string, string>,
|
|
231
|
+
uploadCacheScope: string,
|
|
232
|
+
): Promise<void> {
|
|
144
233
|
if (!Array.isArray(payload.messages)) return;
|
|
145
|
-
const cache = new Map<string, string>();
|
|
146
234
|
|
|
147
235
|
for (const message of payload.messages) {
|
|
148
236
|
if (!isRecord(message) || !Array.isArray(message.content)) continue;
|
|
149
237
|
|
|
150
238
|
for (const block of message.content) {
|
|
151
239
|
if (!isRecord(block)) continue;
|
|
152
|
-
const key =
|
|
240
|
+
const key =
|
|
241
|
+
block.type === "image_url" ? "image_url" : block.type === "video_url" ? "video_url" : null;
|
|
153
242
|
if (!key) continue;
|
|
154
243
|
|
|
155
244
|
const field = block[key];
|
|
@@ -164,9 +253,10 @@ async function transformOpenAIPayloadFiles(payload: JsonRecord, upload: Uploader
|
|
|
164
253
|
const parsed = parseDataUrl(urlValue);
|
|
165
254
|
if (!parsed) continue;
|
|
166
255
|
|
|
167
|
-
const
|
|
256
|
+
const cacheKey = uploadedFileCacheKey(uploadCacheScope, parsed.mimeType, parsed.data);
|
|
257
|
+
const uploaded = uploadCache.get(cacheKey) ?? (await upload(parsed.mimeType, parsed.data));
|
|
168
258
|
if (!uploaded) continue;
|
|
169
|
-
|
|
259
|
+
rememberUploadedFile(uploadCache, cacheKey, uploaded);
|
|
170
260
|
|
|
171
261
|
block[key] =
|
|
172
262
|
typeof field === "string" ? uploaded : { ...(field as JsonRecord), url: uploaded };
|
|
@@ -330,9 +420,10 @@ function normalizeOpenAIToolSchemas(payload: JsonRecord): void {
|
|
|
330
420
|
async function transformAnthropicPayloadFiles(
|
|
331
421
|
payload: JsonRecord,
|
|
332
422
|
upload: Uploader,
|
|
423
|
+
uploadCache: Map<string, string>,
|
|
424
|
+
uploadCacheScope: string,
|
|
333
425
|
): Promise<void> {
|
|
334
426
|
if (!Array.isArray(payload.messages)) return;
|
|
335
|
-
const cache = new Map<string, string>();
|
|
336
427
|
|
|
337
428
|
const transformImageBlock = async (block: unknown): Promise<unknown> => {
|
|
338
429
|
if (!isRecord(block) || block.type !== "image") return block;
|
|
@@ -342,10 +433,10 @@ async function transformAnthropicPayloadFiles(
|
|
|
342
433
|
const data = source.data;
|
|
343
434
|
if (typeof mediaType !== "string" || typeof data !== "string") return block;
|
|
344
435
|
|
|
345
|
-
const cacheKey =
|
|
346
|
-
const uploaded =
|
|
436
|
+
const cacheKey = uploadedFileCacheKey(uploadCacheScope, mediaType, data);
|
|
437
|
+
const uploaded = uploadCache.get(cacheKey) ?? (await upload(mediaType, data));
|
|
347
438
|
if (!uploaded) return block;
|
|
348
|
-
|
|
439
|
+
rememberUploadedFile(uploadCache, cacheKey, uploaded);
|
|
349
440
|
|
|
350
441
|
const next: JsonRecord = { type: "image", source: { type: "url", url: uploaded } };
|
|
351
442
|
if (block.cache_control !== undefined) next.cache_control = block.cache_control;
|
|
@@ -390,10 +481,12 @@ export async function applyKimiPayloadMutations(
|
|
|
390
481
|
|
|
391
482
|
// 2. File upload dispatch (protocol-specific).
|
|
392
483
|
if (ctx.upload) {
|
|
484
|
+
const uploadCache = ctx.uploadCacheScope ? uploadedFileCache : new Map<string, string>();
|
|
485
|
+
const uploadCacheScope = ctx.uploadCacheScope ?? "request";
|
|
393
486
|
if (ctx.api === "openai-completions") {
|
|
394
|
-
await transformOpenAIPayloadFiles(payload, ctx.upload);
|
|
487
|
+
await transformOpenAIPayloadFiles(payload, ctx.upload, uploadCache, uploadCacheScope);
|
|
395
488
|
} else if (ctx.api === "anthropic-messages") {
|
|
396
|
-
await transformAnthropicPayloadFiles(payload, ctx.upload);
|
|
489
|
+
await transformAnthropicPayloadFiles(payload, ctx.upload, uploadCache, uploadCacheScope);
|
|
397
490
|
}
|
|
398
491
|
}
|
|
399
492
|
if (ctx.api === "openai-completions") {
|
|
@@ -447,8 +540,17 @@ export async function applyKimiPayloadMutations(
|
|
|
447
540
|
}
|
|
448
541
|
|
|
449
542
|
const generation = ctx.modelConfig.generation;
|
|
543
|
+
// Official kimi-code sends temperature/top_p only when explicitly configured
|
|
544
|
+
// (env KIMI_MODEL_TEMPERATURE / KIMI_MODEL_TOP_P → generation.*); otherwise it
|
|
545
|
+
// omits them and lets the server apply its own defaults. Mirror that exactly:
|
|
546
|
+
// drop any value pi-ai seeded by default when the user hasn't configured one
|
|
547
|
+
// (the official client does not pin these to 1.0 / 0.95).
|
|
450
548
|
if (generation.temperature !== undefined) payload.temperature = generation.temperature;
|
|
549
|
+
else delete payload.temperature;
|
|
451
550
|
if (generation.topP !== undefined) payload.top_p = generation.topP;
|
|
551
|
+
else delete payload.top_p;
|
|
552
|
+
// Output cap: an explicit cap (KIMI_MODEL_MAX_COMPLETION_TOKENS / config →
|
|
553
|
+
// generation.maxCompletionTokens) is honored as a hardCap on the request value.
|
|
452
554
|
if (generation.maxCompletionTokens !== undefined) {
|
|
453
555
|
const maxTokensKey = ctx.api === "anthropic-messages" ? "max_tokens" : "max_completion_tokens";
|
|
454
556
|
const currentMaxTokens = payload[maxTokensKey];
|
|
@@ -469,22 +571,39 @@ export async function applyKimiPayloadMutations(
|
|
|
469
571
|
const mapped = resolveReasoningForLevel(resolvedReasoning, ctx.modelConfig);
|
|
470
572
|
if (mapped) {
|
|
471
573
|
const oldThinking = isRecord(payload.thinking) ? payload.thinking : {};
|
|
472
|
-
const thinking: JsonRecord = {
|
|
473
|
-
...oldThinking,
|
|
474
|
-
type: mapped.enabled ? "enabled" : "disabled",
|
|
475
|
-
};
|
|
476
|
-
delete thinking.effort;
|
|
477
|
-
if (!mapped.enabled) delete thinking.keep;
|
|
478
574
|
const effort = ctx.reasoning
|
|
479
575
|
? mapped.effort
|
|
480
576
|
: (ctx.modelConfig.defaultEffort ?? mapped.effort);
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
577
|
+
const effortSupported =
|
|
578
|
+
effort !== null && ctx.modelConfig.supportEfforts?.includes(effort) === true;
|
|
579
|
+
if (ctx.api === "anthropic-messages" && oldThinking.type === "adaptive") {
|
|
580
|
+
// pi-ai >=0.82 builds adaptive thinking for models carrying
|
|
581
|
+
// compat.forceAdaptiveThinking (streamSimpleKimi sets it on the
|
|
582
|
+
// anthropic runtime model). Keep the adaptive shape — effort lives in
|
|
583
|
+
// top-level output_config there, not inside thinking — and only
|
|
584
|
+
// replace it with an explicit disable when the level maps to off.
|
|
585
|
+
if (mapped.enabled) {
|
|
586
|
+
if (effortSupported) payload.output_config = { effort };
|
|
587
|
+
else delete payload.output_config;
|
|
588
|
+
} else {
|
|
589
|
+
payload.thinking = { type: "disabled" };
|
|
590
|
+
delete payload.output_config;
|
|
591
|
+
}
|
|
592
|
+
} else {
|
|
593
|
+
const thinking: JsonRecord = {
|
|
594
|
+
...oldThinking,
|
|
595
|
+
type: mapped.enabled ? "enabled" : "disabled",
|
|
596
|
+
};
|
|
597
|
+
delete thinking.effort;
|
|
598
|
+
if (!mapped.enabled) delete thinking.keep;
|
|
599
|
+
if (mapped.enabled && effortSupported) {
|
|
600
|
+
thinking.effort = effort;
|
|
601
|
+
}
|
|
602
|
+
if (mapped.enabled && ctx.modelConfig.thinkingKeep) {
|
|
603
|
+
thinking.keep = ctx.modelConfig.thinkingKeep;
|
|
604
|
+
}
|
|
605
|
+
payload.thinking = thinking;
|
|
486
606
|
}
|
|
487
|
-
payload.thinking = thinking;
|
|
488
607
|
}
|
|
489
608
|
}
|
|
490
609
|
|
|
@@ -510,15 +629,10 @@ export async function applyKimiPayloadMutations(
|
|
|
510
629
|
}
|
|
511
630
|
}
|
|
512
631
|
|
|
513
|
-
// 8. K2.7 Code API constraints: the server rejects
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
|
|
517
|
-
delete payload.temperature;
|
|
518
|
-
}
|
|
519
|
-
if (payload.top_p !== undefined && payload.top_p !== 0.95) {
|
|
520
|
-
delete payload.top_p;
|
|
521
|
-
}
|
|
632
|
+
// 8. K2.7 Code API constraints: the server rejects tool_choice "required" /
|
|
633
|
+
// function-specific when thinking is enabled (always-on). temperature/top_p
|
|
634
|
+
// are handled in step 6 — omitted unless explicitly configured, matching the
|
|
635
|
+
// official kimi-code client rather than pinned to 1.0/0.95.
|
|
522
636
|
if (payload.tool_choice !== undefined) {
|
|
523
637
|
const tc = payload.tool_choice;
|
|
524
638
|
const isAllowed =
|