pi-provider-kimi-code 0.3.1 → 0.3.2
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 +99 -51
- package/index.ts +41 -944
- package/package.json +10 -6
- package/src/constants.ts +48 -0
- package/src/device.ts +164 -0
- package/src/models.ts +129 -0
- package/src/oauth.ts +397 -0
- package/src/payload.ts +446 -0
- package/src/stream.ts +299 -0
package/index.ts
CHANGED
|
@@ -7,952 +7,37 @@
|
|
|
7
7
|
* Usage:
|
|
8
8
|
* pi -e ~/workshop/pi-provider-kimi-code
|
|
9
9
|
* # Then /login kimi-coding, or set KIMI_API_KEY=...
|
|
10
|
+
*
|
|
11
|
+
* Source layout:
|
|
12
|
+
* src/constants.ts — module-level consts + env-driven configuration
|
|
13
|
+
* src/device.ts — device id + kimi-cli-compatible request headers
|
|
14
|
+
* src/oauth.ts — device flow, token refresh, kimi-cli reuse,
|
|
15
|
+
* login/refresh handlers, stream-level auth refresh
|
|
16
|
+
* src/models.ts — /v1/models discovery + extras-merging helpers
|
|
17
|
+
* src/payload.ts — payload pipeline + file upload + transforms
|
|
18
|
+
* src/stream.ts — empty-response filter + streamSimpleKimi orchestrator
|
|
10
19
|
*/
|
|
11
20
|
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
15
|
-
import os from "node:os";
|
|
16
|
-
import { dirname, join } from "node:path";
|
|
17
|
-
import type {
|
|
18
|
-
OAuthCredentials,
|
|
19
|
-
OAuthLoginCallbacks,
|
|
20
|
-
AssistantMessageEvent,
|
|
21
|
-
CacheRetention,
|
|
22
|
-
Context,
|
|
23
|
-
Model,
|
|
24
|
-
SimpleStreamOptions,
|
|
25
|
-
ThinkingLevel,
|
|
26
|
-
} from "@mariozechner/pi-ai";
|
|
27
|
-
import {
|
|
28
|
-
streamSimpleAnthropic,
|
|
29
|
-
streamSimpleOpenAICompletions,
|
|
30
|
-
AssistantMessageEventStream,
|
|
31
|
-
} from "@mariozechner/pi-ai";
|
|
32
|
-
import type { ExtensionAPI, OAuthCredential } from "@mariozechner/pi-coding-agent";
|
|
33
|
-
import { AuthStorage } from "@mariozechner/pi-coding-agent";
|
|
34
|
-
|
|
35
|
-
// =============================================================================
|
|
36
|
-
// Constants
|
|
37
|
-
// =============================================================================
|
|
38
|
-
|
|
39
|
-
const CLIENT_ID = "17e5f671-d194-4dfb-9706-5516cb48c098";
|
|
40
|
-
const DEFAULT_OAUTH_HOST = "https://auth.kimi.com";
|
|
41
|
-
const PROTOCOL =
|
|
42
|
-
process.env.KIMI_CODE_PROTOCOL === "openai" ? "openai-completions" : "anthropic-messages";
|
|
43
|
-
// Use a custom api identifier so this provider never conflicts with the
|
|
44
|
-
// built-in "anthropic-messages" or "openai-completions" stream handlers.
|
|
45
|
-
const KIMI_API_TYPE = PROTOCOL === "openai-completions" ? "kimi-openai-completions" : "kimi-anthropic-messages";
|
|
46
|
-
const DEFAULT_BASE_URL =
|
|
47
|
-
PROTOCOL === "openai-completions"
|
|
48
|
-
? "https://api.kimi.com/coding/v1"
|
|
49
|
-
: "https://api.kimi.com/coding";
|
|
50
|
-
const KIMI_CLI_VERSION = "1.30.0";
|
|
51
|
-
const KIMI_CLI_USER_AGENT = `KimiCLI/${KIMI_CLI_VERSION}`;
|
|
52
|
-
const KIMI_PLATFORM = "kimi_cli";
|
|
53
|
-
const DEVICE_ID_PATH = join(os.homedir(), ".pi", "providers", "kimi-coding", "device_id");
|
|
54
|
-
|
|
55
|
-
// =============================================================================
|
|
56
|
-
// Device identification
|
|
57
|
-
// =============================================================================
|
|
58
|
-
|
|
59
|
-
function getOAuthHost(): string {
|
|
60
|
-
const value =
|
|
61
|
-
process.env.KIMI_CODE_OAUTH_HOST || process.env.KIMI_OAUTH_HOST || DEFAULT_OAUTH_HOST;
|
|
62
|
-
return value.trim() || DEFAULT_OAUTH_HOST;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function getBaseUrl(): string {
|
|
66
|
-
const value = process.env.KIMI_CODE_BASE_URL || DEFAULT_BASE_URL;
|
|
67
|
-
return value.trim() || DEFAULT_BASE_URL;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function createDeviceId(): string {
|
|
71
|
-
return randomBytes(16).toString("hex");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function ensurePrivateFile(path: string): void {
|
|
75
|
-
try {
|
|
76
|
-
chmodSync(path, 0o600);
|
|
77
|
-
} catch {
|
|
78
|
-
// Ignore chmod failures on platforms/filesystems that do not support it.
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function readPersistedDeviceId(): string | null {
|
|
83
|
-
try {
|
|
84
|
-
if (!existsSync(DEVICE_ID_PATH)) return null;
|
|
85
|
-
const deviceId = readFileSync(DEVICE_ID_PATH, "utf8").trim();
|
|
86
|
-
return deviceId || null;
|
|
87
|
-
} catch {
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function persistDeviceId(deviceId: string): void {
|
|
93
|
-
try {
|
|
94
|
-
mkdirSync(dirname(DEVICE_ID_PATH), { recursive: true });
|
|
95
|
-
writeFileSync(DEVICE_ID_PATH, deviceId, "utf8");
|
|
96
|
-
ensurePrivateFile(DEVICE_ID_PATH);
|
|
97
|
-
} catch {
|
|
98
|
-
// Ignore persistence failures and fall back to the in-memory device id.
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function getMacOSVersion(): string {
|
|
103
|
-
try {
|
|
104
|
-
return execSync("sw_vers -productVersion", { encoding: "utf8" }).trim();
|
|
105
|
-
} catch {
|
|
106
|
-
return os.release();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function getDeviceModel(): string {
|
|
111
|
-
const platform = process.platform;
|
|
112
|
-
const arch = os.machine() || process.arch;
|
|
113
|
-
if (platform === "darwin") {
|
|
114
|
-
const version = getMacOSVersion();
|
|
115
|
-
return version && arch ? `macOS ${version} ${arch}` : `macOS ${arch}`;
|
|
116
|
-
}
|
|
117
|
-
if (platform === "win32") {
|
|
118
|
-
// Only show the major release (e.g. "Windows 10", "Windows 11") to match
|
|
119
|
-
// the upstream kimi-cli behavior in `def _device_model() -> str:`.
|
|
120
|
-
// See: https://github.com/MoonshotAI/kimi-cli/blob/main/src/kimi_cli/auth/oauth.py
|
|
121
|
-
const parts = os.release().split(".");
|
|
122
|
-
let release = parts[0];
|
|
123
|
-
if (release === "10" && parts.length >= 3) {
|
|
124
|
-
const build = parseInt(parts[2], 10);
|
|
125
|
-
if (!isNaN(build) && build >= 22000) {
|
|
126
|
-
release = "11";
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return release && arch ? `Windows ${release} ${arch}` : `Windows ${arch}`;
|
|
130
|
-
}
|
|
131
|
-
// Normalize platform name to match Python `platform.system()` casing used by
|
|
132
|
-
// upstream kimi-cli (e.g. "Linux", "FreeBSD", "SunOS"), since Node's
|
|
133
|
-
// `process.platform` is always lower-case.
|
|
134
|
-
const SYSTEM_NAME: Record<string, string> = {
|
|
135
|
-
aix: "AIX",
|
|
136
|
-
freebsd: "FreeBSD",
|
|
137
|
-
linux: "Linux",
|
|
138
|
-
openbsd: "OpenBSD",
|
|
139
|
-
sunos: "SunOS",
|
|
140
|
-
};
|
|
141
|
-
const system = SYSTEM_NAME[platform] ?? platform;
|
|
142
|
-
const release = os.release();
|
|
143
|
-
return release && arch ? `${system} ${release} ${arch}` : `${system} ${arch}`;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function asciiHeaderValue(value: string, fallback = "unknown"): string {
|
|
147
|
-
const trimmed = value.trim();
|
|
148
|
-
/* oxlint-disable-next-line no-control-regex */
|
|
149
|
-
if (/^[\x00-\x7F]*$/.test(trimmed)) {
|
|
150
|
-
return trimmed;
|
|
151
|
-
}
|
|
152
|
-
/* oxlint-disable-next-line no-control-regex */
|
|
153
|
-
const sanitized = trimmed.replace(/[^\x00-\x7F]/g, "").trim();
|
|
154
|
-
return sanitized || fallback;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const DEVICE_MODEL = getDeviceModel();
|
|
158
|
-
let DEVICE_ID: string | null = null;
|
|
159
|
-
|
|
160
|
-
function getStableDeviceId(): string {
|
|
161
|
-
if (DEVICE_ID) {
|
|
162
|
-
return DEVICE_ID;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const persisted = readPersistedDeviceId();
|
|
166
|
-
if (persisted) {
|
|
167
|
-
DEVICE_ID = persisted;
|
|
168
|
-
return DEVICE_ID;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
DEVICE_ID = createDeviceId();
|
|
172
|
-
persistDeviceId(DEVICE_ID);
|
|
173
|
-
return DEVICE_ID;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function getCommonHeaders(): Record<string, string> {
|
|
177
|
-
const headers = {
|
|
178
|
-
"User-Agent": KIMI_CLI_USER_AGENT,
|
|
179
|
-
"X-Msh-Platform": KIMI_PLATFORM,
|
|
180
|
-
"X-Msh-Version": KIMI_CLI_VERSION,
|
|
181
|
-
"X-Msh-Device-Name": os.hostname(),
|
|
182
|
-
"X-Msh-Device-Model": DEVICE_MODEL,
|
|
183
|
-
"X-Msh-Os-Version": os.release(),
|
|
184
|
-
"X-Msh-Device-Id": getStableDeviceId(),
|
|
185
|
-
};
|
|
186
|
-
return Object.fromEntries(
|
|
187
|
-
Object.entries(headers).map(([key, value]) => [key, asciiHeaderValue(value)]),
|
|
188
|
-
) as Record<string, string>;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// =============================================================================
|
|
192
|
-
// OAuth Implementation
|
|
193
|
-
// =============================================================================
|
|
194
|
-
|
|
195
|
-
interface DeviceAuthorization {
|
|
196
|
-
user_code: string;
|
|
197
|
-
device_code: string;
|
|
198
|
-
verification_uri: string;
|
|
199
|
-
verification_uri_complete: string;
|
|
200
|
-
expires_in: number;
|
|
201
|
-
interval: number;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
interface TokenResponse {
|
|
205
|
-
access_token: string;
|
|
206
|
-
refresh_token: string;
|
|
207
|
-
expires_in: number;
|
|
208
|
-
scope: string;
|
|
209
|
-
token_type: string;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async function requestDeviceAuthorization(): Promise<DeviceAuthorization> {
|
|
213
|
-
const response = await fetch(`${getOAuthHost()}/api/oauth/device_authorization`, {
|
|
214
|
-
method: "POST",
|
|
215
|
-
headers: {
|
|
216
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
217
|
-
...getCommonHeaders(),
|
|
218
|
-
},
|
|
219
|
-
body: new URLSearchParams({
|
|
220
|
-
client_id: CLIENT_ID,
|
|
221
|
-
}),
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
if (!response.ok) {
|
|
225
|
-
const text = await response.text().catch(() => "");
|
|
226
|
-
throw new Error(`Device authorization failed: ${response.status} ${text}`);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const data = (await response.json()) as {
|
|
230
|
-
user_code?: string;
|
|
231
|
-
device_code?: string;
|
|
232
|
-
verification_uri?: string;
|
|
233
|
-
verification_uri_complete?: string;
|
|
234
|
-
expires_in?: number;
|
|
235
|
-
interval?: number;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
if (!data.user_code || !data.device_code || !data.verification_uri_complete) {
|
|
239
|
-
throw new Error("Invalid device authorization response");
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return {
|
|
243
|
-
user_code: data.user_code,
|
|
244
|
-
device_code: data.device_code,
|
|
245
|
-
verification_uri: data.verification_uri || "",
|
|
246
|
-
verification_uri_complete: data.verification_uri_complete,
|
|
247
|
-
expires_in: data.expires_in || 1800,
|
|
248
|
-
interval: data.interval || 5,
|
|
249
|
-
};
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
async function requestDeviceToken(auth: DeviceAuthorization): Promise<TokenResponse | null> {
|
|
253
|
-
const response = await fetch(`${getOAuthHost()}/api/oauth/token`, {
|
|
254
|
-
method: "POST",
|
|
255
|
-
headers: {
|
|
256
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
257
|
-
...getCommonHeaders(),
|
|
258
|
-
},
|
|
259
|
-
body: new URLSearchParams({
|
|
260
|
-
client_id: CLIENT_ID,
|
|
261
|
-
device_code: auth.device_code,
|
|
262
|
-
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
263
|
-
}),
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
if (response.status === 200) {
|
|
267
|
-
const data = (await response.json()) as TokenResponse;
|
|
268
|
-
if (data.access_token && data.refresh_token) {
|
|
269
|
-
return data;
|
|
270
|
-
}
|
|
271
|
-
throw new Error("Token response missing required fields");
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (response.status === 400) {
|
|
275
|
-
const data = (await response.json()) as { error?: string; error_description?: string };
|
|
276
|
-
if (data.error === "authorization_pending") {
|
|
277
|
-
return null;
|
|
278
|
-
}
|
|
279
|
-
if (data.error === "expired_token") {
|
|
280
|
-
throw new Error("expired_token");
|
|
281
|
-
}
|
|
282
|
-
throw new Error(`Token request failed: ${data.error_description || data.error || "unknown"}`);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const text = await response.text().catch(() => "");
|
|
286
|
-
throw new Error(`Token request failed: ${response.status} ${text}`);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
async function refreshAccessToken(refreshToken: string): Promise<TokenResponse> {
|
|
290
|
-
const response = await fetch(`${getOAuthHost()}/api/oauth/token`, {
|
|
291
|
-
method: "POST",
|
|
292
|
-
headers: {
|
|
293
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
294
|
-
...getCommonHeaders(),
|
|
295
|
-
},
|
|
296
|
-
body: new URLSearchParams({
|
|
297
|
-
client_id: CLIENT_ID,
|
|
298
|
-
grant_type: "refresh_token",
|
|
299
|
-
refresh_token: refreshToken,
|
|
300
|
-
}),
|
|
301
|
-
});
|
|
302
|
-
|
|
303
|
-
if (!response.ok) {
|
|
304
|
-
const text = await response.text().catch(() => "");
|
|
305
|
-
if (response.status === 401 || response.status === 403) {
|
|
306
|
-
throw new Error(`Token refresh unauthorized: ${text}`);
|
|
307
|
-
}
|
|
308
|
-
throw new Error(`Token refresh failed: ${response.status} ${text}`);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const data = (await response.json()) as TokenResponse;
|
|
312
|
-
if (!data.access_token || !data.refresh_token) {
|
|
313
|
-
throw new Error("Token refresh response missing required fields");
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return data;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// =============================================================================
|
|
320
|
-
// OAuth login / refresh for extension registration
|
|
321
|
-
// =============================================================================
|
|
322
|
-
|
|
323
|
-
async function loginKimiCode(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
|
324
|
-
// Keep trying until we get a token (handles expired device codes)
|
|
325
|
-
while (true) {
|
|
326
|
-
const auth = await requestDeviceAuthorization();
|
|
327
|
-
|
|
328
|
-
callbacks.onAuth({
|
|
329
|
-
url: auth.verification_uri_complete,
|
|
330
|
-
instructions: `Please visit the URL to authorize. Your code: ${auth.user_code}`,
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
const interval = Math.max(auth.interval, 1) * 1000;
|
|
334
|
-
const expiresAt = Date.now() + auth.expires_in * 1000;
|
|
335
|
-
|
|
336
|
-
let token: TokenResponse | null = null;
|
|
337
|
-
let printedWaiting = false;
|
|
338
|
-
|
|
339
|
-
while (Date.now() < expiresAt) {
|
|
340
|
-
try {
|
|
341
|
-
token = await requestDeviceToken(auth);
|
|
342
|
-
if (token) break;
|
|
343
|
-
} catch (error) {
|
|
344
|
-
if (error instanceof Error && error.message === "expired_token") {
|
|
345
|
-
// Device code expired, restart the flow
|
|
346
|
-
if (callbacks.onProgress) {
|
|
347
|
-
callbacks.onProgress("Device code expired, restarting...");
|
|
348
|
-
}
|
|
349
|
-
break;
|
|
350
|
-
}
|
|
351
|
-
throw error;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (!printedWaiting) {
|
|
355
|
-
if (callbacks.onProgress) {
|
|
356
|
-
callbacks.onProgress("Waiting for authorization...");
|
|
357
|
-
}
|
|
358
|
-
printedWaiting = true;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// Check for abort
|
|
362
|
-
if (callbacks.signal?.aborted) {
|
|
363
|
-
throw new Error("Authorization aborted");
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
if (token) {
|
|
370
|
-
return {
|
|
371
|
-
access: token.access_token,
|
|
372
|
-
refresh: token.refresh_token,
|
|
373
|
-
expires: Date.now() + token.expires_in * 1000,
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// If we get here without a token, the device code expired - loop will retry
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
async function refreshKimiCodeToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
|
382
|
-
const token = await refreshAccessToken(credentials.refresh);
|
|
383
|
-
return {
|
|
384
|
-
access: token.access_token,
|
|
385
|
-
refresh: token.refresh_token,
|
|
386
|
-
expires: Date.now() + token.expires_in * 1000,
|
|
387
|
-
};
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// =============================================================================
|
|
391
|
-
// Payload / stream helpers: types + pure utilities
|
|
392
|
-
// =============================================================================
|
|
393
|
-
|
|
394
|
-
const EMPTY_RESPONSE_PREFIX = "(Empty response:";
|
|
395
|
-
const DEFAULT_KIMI_INLINE_UPLOAD_THRESHOLD_BYTES = 1 * 1024 * 1024;
|
|
396
|
-
|
|
397
|
-
type JsonRecord = Record<string, unknown>;
|
|
398
|
-
type Uploader = (mimeType: string, data: string) => Promise<string | null>;
|
|
399
|
-
|
|
400
|
-
interface KimiEnvOverrides {
|
|
401
|
-
temperature?: number;
|
|
402
|
-
topP?: number;
|
|
403
|
-
maxTokens?: number;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
function resolveCacheRetention(value?: CacheRetention): CacheRetention {
|
|
407
|
-
if (value === "none" || value === "short" || value === "long") return value;
|
|
408
|
-
if (process.env.PI_CACHE_RETENTION === "long") return "long";
|
|
409
|
-
return "short";
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
interface KimiPayloadContext {
|
|
413
|
-
api: "anthropic-messages" | "openai-completions";
|
|
414
|
-
upload?: Uploader;
|
|
415
|
-
cacheKey?: string;
|
|
416
|
-
cacheRetention: CacheRetention;
|
|
417
|
-
reasoning?: ThinkingLevel;
|
|
418
|
-
envOverrides: KimiEnvOverrides;
|
|
419
|
-
}
|
|
21
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
22
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
420
23
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
const parsed = Number.parseInt(raw ?? "", 10);
|
|
436
|
-
return Number.isFinite(parsed) && parsed >= 0
|
|
437
|
-
? parsed
|
|
438
|
-
: DEFAULT_KIMI_INLINE_UPLOAD_THRESHOLD_BYTES;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
function deriveFilesBaseUrl(baseUrl: string): string {
|
|
442
|
-
const trimmed = baseUrl.replace(/\/$/, "");
|
|
443
|
-
return trimmed.endsWith("/v1") ? trimmed : `${trimmed}/v1`;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
function parseDataUrl(url: string): { mimeType: string; data: string } | null {
|
|
447
|
-
const match = url.match(/^data:([^;,]+);base64,([A-Za-z0-9+/=]+)$/);
|
|
448
|
-
return match ? { mimeType: match[1], data: match[2] } : null;
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
function getUploadFilename(mimeType: string): string {
|
|
452
|
-
const map: Record<string, string> = {
|
|
453
|
-
"image/jpeg": "upload.jpg",
|
|
454
|
-
"image/png": "upload.png",
|
|
455
|
-
"image/gif": "upload.gif",
|
|
456
|
-
"image/webp": "upload.webp",
|
|
457
|
-
"video/mp4": "upload.mp4",
|
|
458
|
-
"video/quicktime": "upload.mov",
|
|
459
|
-
};
|
|
460
|
-
return map[mimeType] ?? (mimeType.startsWith("video/") ? "upload.mp4" : "upload.bin");
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function readEnvOverrides(): KimiEnvOverrides {
|
|
464
|
-
const out: KimiEnvOverrides = {};
|
|
465
|
-
const temp = process.env.KIMI_MODEL_TEMPERATURE;
|
|
466
|
-
if (temp) out.temperature = parseFloat(temp);
|
|
467
|
-
const topP = process.env.KIMI_MODEL_TOP_P;
|
|
468
|
-
if (topP) out.topP = parseFloat(topP);
|
|
469
|
-
const maxTokens = process.env.KIMI_MODEL_MAX_TOKENS;
|
|
470
|
-
if (maxTokens) out.maxTokens = parseInt(maxTokens, 10);
|
|
471
|
-
return out;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
// =============================================================================
|
|
475
|
-
// File upload (I/O edge)
|
|
476
|
-
// =============================================================================
|
|
477
|
-
|
|
478
|
-
async function uploadKimiFile(
|
|
479
|
-
apiKey: string,
|
|
480
|
-
mimeType: string,
|
|
481
|
-
data: string,
|
|
482
|
-
): Promise<string | null> {
|
|
483
|
-
const buffer = Buffer.from(data, "base64");
|
|
484
|
-
const isVideo = mimeType.startsWith("video/");
|
|
485
|
-
const threshold = parseInlineUploadThreshold(process.env.KIMI_CODE_UPLOAD_THRESHOLD_BYTES);
|
|
486
|
-
if (!isVideo && buffer.length <= threshold) return null;
|
|
487
|
-
|
|
488
|
-
const filename = getUploadFilename(mimeType);
|
|
489
|
-
const formData = new FormData();
|
|
490
|
-
formData.append("file", new Blob([buffer], { type: mimeType }), filename);
|
|
491
|
-
formData.append("purpose", isVideo ? "video" : "image");
|
|
492
|
-
|
|
493
|
-
const baseUrl = process.env.KIMI_CODE_BASE_URL || DEFAULT_BASE_URL;
|
|
494
|
-
const uploadUrl = `${deriveFilesBaseUrl(baseUrl)}/files`;
|
|
495
|
-
const debug = process.env.KIMI_CODE_DEBUG === "1";
|
|
496
|
-
if (debug) {
|
|
497
|
-
console.log(
|
|
498
|
-
`\n[kimi-coding] Uploading ${filename} to ${uploadUrl} (${(buffer.length / 1024 / 1024).toFixed(2)} MB)`,
|
|
499
|
-
);
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
try {
|
|
503
|
-
const response = await fetch(uploadUrl, {
|
|
504
|
-
method: "POST",
|
|
505
|
-
headers: { Authorization: `Bearer ${apiKey}`, ...getCommonHeaders() },
|
|
506
|
-
body: formData,
|
|
507
|
-
});
|
|
508
|
-
if (!response.ok) throw new Error(`${response.status} ${await response.text()}`);
|
|
509
|
-
const fileObj = (await response.json()) as { id?: string };
|
|
510
|
-
if (!fileObj.id) throw new Error("missing file id");
|
|
511
|
-
const fileUrl = `ms://${fileObj.id}`;
|
|
512
|
-
if (debug) console.log(`[kimi-coding] Upload success: ${fileUrl}`);
|
|
513
|
-
return fileUrl;
|
|
514
|
-
} catch (err) {
|
|
515
|
-
console.error("[kimi-coding] Upload failed:", err);
|
|
516
|
-
return null;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
// =============================================================================
|
|
521
|
-
// Payload file transformers (pure given an Uploader)
|
|
522
|
-
// =============================================================================
|
|
523
|
-
// These walk the provider-specific payload shape and replace inline base64
|
|
524
|
-
// image/video blocks with ms:// references returned by the injected uploader.
|
|
525
|
-
// They take an Uploader rather than an apiKey so they can be unit-tested with
|
|
526
|
-
// a fake uploader; all network I/O stays behind that boundary.
|
|
527
|
-
|
|
528
|
-
async function transformOpenAIPayloadFiles(payload: JsonRecord, upload: Uploader): Promise<void> {
|
|
529
|
-
if (!Array.isArray(payload.messages)) return;
|
|
530
|
-
const cache = new Map<string, string>();
|
|
531
|
-
|
|
532
|
-
for (const message of payload.messages) {
|
|
533
|
-
if (!isRecord(message) || !Array.isArray(message.content)) continue;
|
|
534
|
-
|
|
535
|
-
for (const block of message.content) {
|
|
536
|
-
if (!isRecord(block)) continue;
|
|
537
|
-
const key =
|
|
538
|
-
block.type === "image_url" ? "image_url" : block.type === "video_url" ? "video_url" : null;
|
|
539
|
-
if (!key) continue;
|
|
540
|
-
|
|
541
|
-
const field = block[key];
|
|
542
|
-
const urlValue =
|
|
543
|
-
typeof field === "string"
|
|
544
|
-
? field
|
|
545
|
-
: isRecord(field) && typeof field.url === "string"
|
|
546
|
-
? field.url
|
|
547
|
-
: null;
|
|
548
|
-
if (!urlValue || urlValue.startsWith("ms://")) continue;
|
|
549
|
-
|
|
550
|
-
const parsed = parseDataUrl(urlValue);
|
|
551
|
-
if (!parsed) continue;
|
|
552
|
-
|
|
553
|
-
const uploaded = cache.get(urlValue) ?? (await upload(parsed.mimeType, parsed.data));
|
|
554
|
-
if (!uploaded) continue;
|
|
555
|
-
cache.set(urlValue, uploaded);
|
|
556
|
-
|
|
557
|
-
block[key] =
|
|
558
|
-
typeof field === "string" ? uploaded : { ...(field as JsonRecord), url: uploaded };
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
async function transformAnthropicPayloadFiles(
|
|
564
|
-
payload: JsonRecord,
|
|
565
|
-
upload: Uploader,
|
|
566
|
-
): Promise<void> {
|
|
567
|
-
if (!Array.isArray(payload.messages)) return;
|
|
568
|
-
const cache = new Map<string, string>();
|
|
569
|
-
|
|
570
|
-
const transformImageBlock = async (block: unknown): Promise<unknown> => {
|
|
571
|
-
if (!isRecord(block) || block.type !== "image") return block;
|
|
572
|
-
const source = block.source;
|
|
573
|
-
if (!isRecord(source) || source.type !== "base64") return block;
|
|
574
|
-
const mediaType = source.media_type;
|
|
575
|
-
const data = source.data;
|
|
576
|
-
if (typeof mediaType !== "string" || typeof data !== "string") return block;
|
|
577
|
-
|
|
578
|
-
const cacheKey = `${mediaType}:${data}`;
|
|
579
|
-
const uploaded = cache.get(cacheKey) ?? (await upload(mediaType, data));
|
|
580
|
-
if (!uploaded) return block;
|
|
581
|
-
cache.set(cacheKey, uploaded);
|
|
582
|
-
|
|
583
|
-
const next: JsonRecord = { type: "image", source: { type: "url", url: uploaded } };
|
|
584
|
-
if (block.cache_control !== undefined) next.cache_control = block.cache_control;
|
|
585
|
-
return next;
|
|
586
|
-
};
|
|
587
|
-
|
|
588
|
-
for (const message of payload.messages) {
|
|
589
|
-
if (!isRecord(message) || !Array.isArray(message.content)) continue;
|
|
590
|
-
|
|
591
|
-
for (let i = 0; i < message.content.length; i++) {
|
|
592
|
-
const block = message.content[i];
|
|
593
|
-
if (isRecord(block) && block.type === "tool_result" && Array.isArray(block.content)) {
|
|
594
|
-
for (let j = 0; j < block.content.length; j++) {
|
|
595
|
-
block.content[j] = await transformImageBlock(block.content[j]);
|
|
596
|
-
}
|
|
597
|
-
continue;
|
|
598
|
-
}
|
|
599
|
-
message.content[i] = await transformImageBlock(block);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// =============================================================================
|
|
605
|
-
// Payload mutation pipeline
|
|
606
|
-
// =============================================================================
|
|
607
|
-
// Applies all Kimi-specific mutations to a provider payload in place.
|
|
608
|
-
// Pure given its context: no process.env / fs / network access of its own —
|
|
609
|
-
// every side effect enters via ctx.upload or pre-read values in ctx.
|
|
610
|
-
// This makes the five steps below testable with fixture payloads.
|
|
611
|
-
|
|
612
|
-
async function applyKimiPayloadMutations(
|
|
613
|
-
payload: JsonRecord,
|
|
614
|
-
ctx: KimiPayloadContext,
|
|
615
|
-
): Promise<void> {
|
|
616
|
-
// 1. Map unsupported roles: Kimi does not recognize "developer" (OpenAI-specific).
|
|
617
|
-
if (Array.isArray(payload.messages)) {
|
|
618
|
-
payload.messages = payload.messages.map((msg) =>
|
|
619
|
-
isRecord(msg) && msg.role === "developer" ? { ...msg, role: "system" } : msg,
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
// 2. File upload dispatch (protocol-specific).
|
|
624
|
-
if (ctx.upload) {
|
|
625
|
-
if (ctx.api === "openai-completions") {
|
|
626
|
-
await transformOpenAIPayloadFiles(payload, ctx.upload);
|
|
627
|
-
} else if (ctx.api === "anthropic-messages") {
|
|
628
|
-
await transformAnthropicPayloadFiles(payload, ctx.upload);
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
// 3. prompt_cache_key injection. Respect any key already on the payload,
|
|
633
|
-
// otherwise fall back to the caller-provided cacheKey (sessionId or
|
|
634
|
-
// explicit options.prompt_cache_key override). Skipped entirely when
|
|
635
|
-
// cacheRetention is "none" (via options.cacheRetention or
|
|
636
|
-
// PI_CACHE_RETENTION) so callers can truly disable caching — otherwise
|
|
637
|
-
// Kimi's native session cache would still fire even if pi-ai's
|
|
638
|
-
// Anthropic-style cache_control markers are omitted.
|
|
639
|
-
if (ctx.cacheRetention !== "none") {
|
|
640
|
-
const existing = payload.prompt_cache_key;
|
|
641
|
-
const resolved = (typeof existing === "string" && existing) || ctx.cacheKey;
|
|
642
|
-
if (resolved) payload.prompt_cache_key = resolved;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
// 4. Env-level hyperparameter overrides (pre-parsed into numbers by caller).
|
|
646
|
-
const { temperature, topP, maxTokens } = ctx.envOverrides;
|
|
647
|
-
if (temperature !== undefined) payload.temperature = temperature;
|
|
648
|
-
if (topP !== undefined) payload.top_p = topP;
|
|
649
|
-
if (maxTokens !== undefined) payload.max_tokens = maxTokens;
|
|
650
|
-
|
|
651
|
-
// 5. Reasoning effort mapping.
|
|
652
|
-
if (ctx.reasoning) {
|
|
653
|
-
const mapped = mapThinkingLevel(ctx.reasoning);
|
|
654
|
-
if (mapped) {
|
|
655
|
-
payload.reasoning_effort = mapped.effort;
|
|
656
|
-
const extraBody = isRecord(payload.extra_body) ? payload.extra_body : {};
|
|
657
|
-
extraBody.thinking = { type: mapped.enabled ? "enabled" : "disabled" };
|
|
658
|
-
payload.extra_body = extraBody;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
// =============================================================================
|
|
664
|
-
// Event stream filter: suppress Kimi "(Empty response: ...)" text blocks
|
|
665
|
-
// =============================================================================
|
|
666
|
-
// The Kimi API wraps thinking-only responses (no text content) into a text
|
|
667
|
-
// block like: (Empty response: {'content': [{'type': 'thinking', ...}]}).
|
|
668
|
-
// This leaks internal state to the user. We buffer text_start/text_delta
|
|
669
|
-
// events and drop the whole block if text_end starts with the marker.
|
|
670
|
-
// Pure async generator — no closure dependencies, testable with synthetic
|
|
671
|
-
// event arrays.
|
|
672
|
-
|
|
673
|
-
async function* filterEmptyResponseStream(
|
|
674
|
-
upstream: AsyncIterable<AssistantMessageEvent>,
|
|
675
|
-
): AsyncIterable<AssistantMessageEvent> {
|
|
676
|
-
const suppressedIndices = new Set<number>();
|
|
677
|
-
let textBuffer: AssistantMessageEvent[] = [];
|
|
678
|
-
let bufferingIndex: number | null = null;
|
|
679
|
-
|
|
680
|
-
for await (const event of upstream) {
|
|
681
|
-
// Start buffering when a new text block begins.
|
|
682
|
-
if (event.type === "text_start") {
|
|
683
|
-
bufferingIndex = event.contentIndex;
|
|
684
|
-
textBuffer = [event];
|
|
685
|
-
continue;
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
// Accumulate text deltas + detect the empty-response marker on text_end.
|
|
689
|
-
if (
|
|
690
|
-
bufferingIndex !== null &&
|
|
691
|
-
"contentIndex" in event &&
|
|
692
|
-
event.contentIndex === bufferingIndex
|
|
693
|
-
) {
|
|
694
|
-
if (event.type === "text_delta") {
|
|
695
|
-
textBuffer.push(event);
|
|
696
|
-
continue;
|
|
697
|
-
}
|
|
698
|
-
if (event.type === "text_end") {
|
|
699
|
-
if (event.content.startsWith(EMPTY_RESPONSE_PREFIX)) {
|
|
700
|
-
// Suppress entire text block. Do NOT splice the message content
|
|
701
|
-
// array: it is a shared reference into session state, and mutating
|
|
702
|
-
// it would shift subsequent contentIndex values, corrupting the
|
|
703
|
-
// stream.
|
|
704
|
-
suppressedIndices.add(bufferingIndex);
|
|
705
|
-
} else {
|
|
706
|
-
// Legitimate text block — flush buffered events + end event.
|
|
707
|
-
for (const buffered of textBuffer) yield buffered;
|
|
708
|
-
yield event;
|
|
709
|
-
}
|
|
710
|
-
textBuffer = [];
|
|
711
|
-
bufferingIndex = null;
|
|
712
|
-
continue;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
// Skip any event targeting an already-suppressed content index.
|
|
717
|
-
if ("contentIndex" in event && suppressedIndices.has(event.contentIndex)) {
|
|
718
|
-
continue;
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
// Clean suppressed blocks out of the final message.
|
|
722
|
-
if (event.type === "done" && suppressedIndices.size > 0) {
|
|
723
|
-
event.message.content = event.message.content.filter(
|
|
724
|
-
(block) =>
|
|
725
|
-
!(
|
|
726
|
-
block.type === "text" &&
|
|
727
|
-
typeof block.text === "string" &&
|
|
728
|
-
block.text.startsWith(EMPTY_RESPONSE_PREFIX)
|
|
729
|
-
),
|
|
730
|
-
);
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
yield event;
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
// =============================================================================
|
|
738
|
-
// Auth refresh: recover from server-side token invalidation
|
|
739
|
-
// =============================================================================
|
|
740
|
-
// pi-coding-agent only refreshes an OAuth token when the locally cached
|
|
741
|
-
// `expires` is in the past. If the server rotates/revokes the access token
|
|
742
|
-
// before that (common with short-lived session tokens), every request keeps
|
|
743
|
-
// returning 401. We detect that situation by inspecting the first event of
|
|
744
|
-
// the upstream stream, force a refresh through AuthStorage (which persists
|
|
745
|
-
// the new credentials under a file lock), and retry once.
|
|
746
|
-
|
|
747
|
-
const PROVIDER_ID = "kimi-coding";
|
|
748
|
-
|
|
749
|
-
async function refreshKimiAuthToken(currentKey: string): Promise<string | null> {
|
|
750
|
-
try {
|
|
751
|
-
const storage = AuthStorage.create();
|
|
752
|
-
const cred = storage.get(PROVIDER_ID);
|
|
753
|
-
if (!cred || cred.type !== "oauth") {
|
|
754
|
-
console.error(
|
|
755
|
-
`[kimi-coding] auth refresh skipped: no OAuth credentials for ${PROVIDER_ID} on disk`,
|
|
756
|
-
);
|
|
757
|
-
return null;
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
// If disk already has a different valid token (e.g., another process or a
|
|
761
|
-
// previous retry refreshed it while the caller's in-memory cache went
|
|
762
|
-
// stale), reuse it without hitting the OAuth endpoint.
|
|
763
|
-
if (cred.access !== currentKey && Date.now() < cred.expires) {
|
|
764
|
-
console.error("[kimi-coding] auth refresh: reusing newer on-disk token");
|
|
765
|
-
return cred.access;
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
console.error("[kimi-coding] auth refresh: requesting new access token");
|
|
769
|
-
const refreshed = await refreshAccessToken(cred.refresh);
|
|
770
|
-
const newCred: OAuthCredential = {
|
|
771
|
-
type: "oauth",
|
|
772
|
-
access: refreshed.access_token,
|
|
773
|
-
refresh: refreshed.refresh_token,
|
|
774
|
-
expires: Date.now() + refreshed.expires_in * 1000,
|
|
775
|
-
};
|
|
776
|
-
storage.set(PROVIDER_ID, newCred);
|
|
777
|
-
console.error("[kimi-coding] auth refresh: new token persisted");
|
|
778
|
-
return newCred.access;
|
|
779
|
-
} catch (err) {
|
|
780
|
-
console.error("[kimi-coding] auth refresh failed:", err);
|
|
781
|
-
return null;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
// =============================================================================
|
|
786
|
-
// Stream wrapper: orchestrates payload mutation + event filter
|
|
787
|
-
// =============================================================================
|
|
788
|
-
// Reads every side-effect source (process.env, options, apiKey) at the top
|
|
789
|
-
// and hands a plain KimiPayloadContext to applyKimiPayloadMutations. The only
|
|
790
|
-
// thing this function itself "does" is wire SDK streaming + filter + error
|
|
791
|
-
// fallback; the actual logic lives in the pure units above.
|
|
792
|
-
|
|
793
|
-
function streamSimpleKimi(
|
|
794
|
-
model: Model<"anthropic-messages" | "openai-completions">,
|
|
795
|
-
context: Context,
|
|
796
|
-
options?: SimpleStreamOptions,
|
|
797
|
-
): AssistantMessageEventStream {
|
|
798
|
-
const filtered = new AssistantMessageEventStream();
|
|
799
|
-
const initialKey = options?.apiKey || process.env.KIMI_API_KEY || "";
|
|
800
|
-
|
|
801
|
-
const cacheKeyOverride = (
|
|
802
|
-
options as (SimpleStreamOptions & { prompt_cache_key?: unknown }) | undefined
|
|
803
|
-
)?.prompt_cache_key;
|
|
804
|
-
const cacheKey = (typeof cacheKeyOverride === "string" && cacheKeyOverride) || options?.sessionId;
|
|
805
|
-
const cacheRetention = resolveCacheRetention(options?.cacheRetention);
|
|
806
|
-
const envOverrides = readEnvOverrides();
|
|
807
|
-
const originalOnPayload = options?.onPayload;
|
|
808
|
-
|
|
809
|
-
const buildPatchedOptions = (apiKey: string): SimpleStreamOptions => {
|
|
810
|
-
const upload: Uploader | undefined = apiKey
|
|
811
|
-
? (mimeType, data) => uploadKimiFile(apiKey, mimeType, data)
|
|
812
|
-
: undefined;
|
|
813
|
-
// Only forward apiKey if we actually have one — never override the
|
|
814
|
-
// caller's credential (e.g. Claude Code OAuth token) with an empty string.
|
|
815
|
-
const apiKeyOverride = apiKey ? { apiKey } : {};
|
|
816
|
-
return {
|
|
817
|
-
...options,
|
|
818
|
-
...apiKeyOverride,
|
|
819
|
-
onPayload: async (payload, modelData) => {
|
|
820
|
-
let nextPayload: unknown = payload;
|
|
821
|
-
|
|
822
|
-
if (isRecord(nextPayload)) {
|
|
823
|
-
await applyKimiPayloadMutations(nextPayload, {
|
|
824
|
-
api: model.api,
|
|
825
|
-
upload,
|
|
826
|
-
cacheKey,
|
|
827
|
-
cacheRetention,
|
|
828
|
-
reasoning: options?.reasoning,
|
|
829
|
-
envOverrides,
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
if (originalOnPayload) {
|
|
834
|
-
const res = await originalOnPayload(nextPayload, modelData);
|
|
835
|
-
if (res !== undefined) nextPayload = res;
|
|
836
|
-
}
|
|
837
|
-
|
|
838
|
-
return nextPayload;
|
|
839
|
-
},
|
|
840
|
-
};
|
|
841
|
-
};
|
|
842
|
-
|
|
843
|
-
void (async () => {
|
|
844
|
-
let attempt = 0;
|
|
845
|
-
let currentKey = initialKey;
|
|
846
|
-
|
|
847
|
-
while (true) {
|
|
848
|
-
const patchedOptions = buildPatchedOptions(currentKey);
|
|
849
|
-
// Route by the module-level PROTOCOL, not model.api, since we register
|
|
850
|
-
// with a custom api type (kimi-openai-completions / kimi-anthropic-messages)
|
|
851
|
-
// to avoid overriding the built-in Anthropic/OpenAI stream handlers.
|
|
852
|
-
const upstream =
|
|
853
|
-
PROTOCOL === "openai-completions"
|
|
854
|
-
? streamSimpleOpenAICompletions(
|
|
855
|
-
model as Model<"openai-completions">,
|
|
856
|
-
context,
|
|
857
|
-
patchedOptions,
|
|
858
|
-
)
|
|
859
|
-
: streamSimpleAnthropic(model as Model<"anthropic-messages">, context, patchedOptions);
|
|
860
|
-
|
|
861
|
-
let shouldRetry = false;
|
|
862
|
-
let prefixBuffer: AssistantMessageEvent[] = [];
|
|
863
|
-
|
|
864
|
-
try {
|
|
865
|
-
for await (const event of filterEmptyResponseStream(upstream)) {
|
|
866
|
-
// streamAnthropic emits a synthetic "start" event synchronously,
|
|
867
|
-
// before the for-await loop begins iterating and therefore before
|
|
868
|
-
// the HTTP request is actually made. If the request 401s, the loop
|
|
869
|
-
// throws and the catch block emits "error". Without buffering, the
|
|
870
|
-
// "start" event (which carries an empty AssistantMessage) leaks into
|
|
871
|
-
// the session history and the TUI, leaving a phantom empty assistant
|
|
872
|
-
// bubble. We buffer "start" events and only flush them once we see
|
|
873
|
-
// a non-error event that proves the stream is alive.
|
|
874
|
-
if (event.type === "start") {
|
|
875
|
-
prefixBuffer.push(event);
|
|
876
|
-
continue;
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
// Speculative OAuth refresh on the first error. We retry once so
|
|
880
|
-
// that short-lived Kimi tokens invalidated by the server before the
|
|
881
|
-
// local expires timestamp lapses don't surface as raw 401s to the
|
|
882
|
-
// user. Non-auth errors still trigger one wasted refresh, but the
|
|
883
|
-
// retried request fails the same way and we forward it.
|
|
884
|
-
if (attempt === 0 && event.type === "error") {
|
|
885
|
-
console.error(
|
|
886
|
-
`[kimi-coding] upstream error on first event, attempting refresh: ${event.error?.errorMessage?.slice(0, 200)}`,
|
|
887
|
-
);
|
|
888
|
-
const refreshed = await refreshKimiAuthToken(currentKey);
|
|
889
|
-
if (refreshed && refreshed !== currentKey) {
|
|
890
|
-
console.error("[kimi-coding] retrying stream with refreshed token");
|
|
891
|
-
currentKey = refreshed;
|
|
892
|
-
shouldRetry = true;
|
|
893
|
-
break; // discard prefixBuffer — don't leak the stale start
|
|
894
|
-
}
|
|
895
|
-
console.error(
|
|
896
|
-
"[kimi-coding] refresh did not yield a new token, forwarding original error",
|
|
897
|
-
);
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
// First non-start, non-retry event: flush buffered prefix, then
|
|
901
|
-
// stream normally.
|
|
902
|
-
for (const e of prefixBuffer) filtered.push(e);
|
|
903
|
-
prefixBuffer = [];
|
|
904
|
-
filtered.push(event);
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
// Stream ended normally: flush any remaining buffered starts.
|
|
908
|
-
for (const e of prefixBuffer) filtered.push(e);
|
|
909
|
-
} catch (err) {
|
|
910
|
-
// Upstream threw rather than emitting a stream `error` event. This can
|
|
911
|
-
// be the same stale-token 401 surfaced as an exception (depending on
|
|
912
|
-
// the SDK path / network layer), so mirror the in-stream refresh
|
|
913
|
-
// branch: on attempt 0, try one OAuth refresh + retry. Either way,
|
|
914
|
-
// discard `prefixBuffer` — we never confirmed the stream actually
|
|
915
|
-
// started, and flushing the buffered `start` would resurrect the
|
|
916
|
-
// phantom empty assistant message this PR set out to fix.
|
|
917
|
-
console.error("[kimi-coding] stream error:", err);
|
|
918
|
-
if (attempt === 0) {
|
|
919
|
-
const refreshed = await refreshKimiAuthToken(currentKey);
|
|
920
|
-
if (refreshed && refreshed !== currentKey) {
|
|
921
|
-
console.error("[kimi-coding] retrying stream after thrown error with refreshed token");
|
|
922
|
-
currentKey = refreshed;
|
|
923
|
-
shouldRetry = true;
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
if (!shouldRetry) {
|
|
927
|
-
filtered.push({
|
|
928
|
-
type: "error",
|
|
929
|
-
reason: "error",
|
|
930
|
-
error: {
|
|
931
|
-
content: [],
|
|
932
|
-
stopReason: "error",
|
|
933
|
-
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 },
|
|
934
|
-
},
|
|
935
|
-
} as AssistantMessageEvent & { type: "error" });
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
if (shouldRetry) {
|
|
940
|
-
attempt++;
|
|
941
|
-
continue;
|
|
942
|
-
}
|
|
943
|
-
break;
|
|
944
|
-
}
|
|
945
|
-
})();
|
|
946
|
-
|
|
947
|
-
return filtered;
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
// =============================================================================
|
|
951
|
-
// Extension Entry Point
|
|
952
|
-
// =============================================================================
|
|
24
|
+
import {
|
|
25
|
+
DEFAULT_KIMI_MODEL_INPUT,
|
|
26
|
+
KIMI_API_TYPE,
|
|
27
|
+
PROVIDER_ID,
|
|
28
|
+
getBaseUrl,
|
|
29
|
+
} from "./src/constants.ts";
|
|
30
|
+
import { getCommonHeaders } from "./src/device.ts";
|
|
31
|
+
import {
|
|
32
|
+
type KimiOAuthCredentials,
|
|
33
|
+
applyKimiEnvOverridesToModel,
|
|
34
|
+
applyKimiOAuthExtrasToModel,
|
|
35
|
+
} from "./src/models.ts";
|
|
36
|
+
import { loginKimiCode, refreshKimiCodeToken } from "./src/oauth.ts";
|
|
37
|
+
import { streamSimpleKimi } from "./src/stream.ts";
|
|
953
38
|
|
|
954
39
|
export default function (pi: ExtensionAPI) {
|
|
955
|
-
pi.registerProvider(
|
|
40
|
+
pi.registerProvider(PROVIDER_ID, {
|
|
956
41
|
baseUrl: getBaseUrl(),
|
|
957
42
|
apiKey: "KIMI_API_KEY",
|
|
958
43
|
api: KIMI_API_TYPE,
|
|
@@ -961,15 +46,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
961
46
|
headers: getCommonHeaders(),
|
|
962
47
|
|
|
963
48
|
models: [
|
|
964
|
-
{
|
|
49
|
+
applyKimiEnvOverridesToModel({
|
|
965
50
|
id: "kimi-for-coding",
|
|
966
51
|
name: "Kimi for Coding",
|
|
967
52
|
reasoning: true,
|
|
968
|
-
input: ["text"
|
|
53
|
+
input: [...DEFAULT_KIMI_MODEL_INPUT] as unknown as ("text" | "image")[],
|
|
969
54
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
970
55
|
contextWindow: 262144,
|
|
971
56
|
maxTokens: 32000,
|
|
972
|
-
},
|
|
57
|
+
} as Model<Api>),
|
|
973
58
|
],
|
|
974
59
|
|
|
975
60
|
oauth: {
|
|
@@ -977,6 +62,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
977
62
|
login: loginKimiCode,
|
|
978
63
|
refreshToken: refreshKimiCodeToken,
|
|
979
64
|
getApiKey: (cred) => cred.access,
|
|
65
|
+
// Reflect server-side model identity on the registered model after login
|
|
66
|
+
// / refresh. We never rewrite the model id (pi-side `/model` selections
|
|
67
|
+
// and persisted sessions reference it); only the human-facing name, the
|
|
68
|
+
// context window, and an out-of-band `wireModelId` carried into the
|
|
69
|
+
// request payload by streamSimpleKimi.
|
|
70
|
+
modifyModels: (models, cred) => {
|
|
71
|
+
const extras = cred as KimiOAuthCredentials;
|
|
72
|
+
return models.map((model) => {
|
|
73
|
+
if (model.id !== "kimi-for-coding") return model;
|
|
74
|
+
return applyKimiEnvOverridesToModel(applyKimiOAuthExtrasToModel(model, extras));
|
|
75
|
+
});
|
|
76
|
+
},
|
|
980
77
|
},
|
|
981
78
|
});
|
|
982
79
|
}
|