@molecule/api-ai-moonshot 1.2.0 → 1.3.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/provider.d.ts.map +1 -1
- package/dist/provider.js +71 -15
- package/package.json +2 -2
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EACV,UAAU,EAQX,MAAM,kBAAkB,CAAA;AASzB,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAizBhD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,CAAC,EAAE,cAAc,GAAG,UAAU,CAElE"}
|
package/dist/provider.js
CHANGED
|
@@ -46,6 +46,35 @@ async function waitForRetry(delayMs, signal) {
|
|
|
46
46
|
onAbort();
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
|
+
/** Moonshot's error type for an account whose balance is exhausted; it is answered with HTTP 429. */
|
|
50
|
+
const QUOTA_EXHAUSTED_TYPE = 'exceeded_current_quota_error';
|
|
51
|
+
/**
|
|
52
|
+
* Read the upstream's explanation from a rejected response body: its error
|
|
53
|
+
* `type` (or `code`) and `message`, the message bounded to 500 characters.
|
|
54
|
+
*
|
|
55
|
+
* @param text - The rejected response's body text.
|
|
56
|
+
* @returns The upstream error, or `undefined` when the body carried neither field.
|
|
57
|
+
*/
|
|
58
|
+
function parseUpstreamError(text) {
|
|
59
|
+
let error;
|
|
60
|
+
try {
|
|
61
|
+
error = JSON.parse(text)?.error;
|
|
62
|
+
}
|
|
63
|
+
catch (_error) {
|
|
64
|
+
// Not JSON (a proxy's error page, plain text): a short body is the message itself.
|
|
65
|
+
const trimmed = text.trim();
|
|
66
|
+
return trimmed && trimmed.length <= 500 ? { message: trimmed } : undefined;
|
|
67
|
+
}
|
|
68
|
+
const type = typeof error?.type === 'string'
|
|
69
|
+
? error.type
|
|
70
|
+
: typeof error?.code === 'string'
|
|
71
|
+
? error.code
|
|
72
|
+
: undefined;
|
|
73
|
+
const message = typeof error?.message === 'string' ? error.message.slice(0, 500) : undefined;
|
|
74
|
+
if (!type && !message)
|
|
75
|
+
return undefined;
|
|
76
|
+
return { ...(type ? { type } : {}), ...(message ? { message } : {}) };
|
|
77
|
+
}
|
|
49
78
|
/**
|
|
50
79
|
* Moonshot (Kimi) AI provider that implements the `AIProvider` interface
|
|
51
80
|
* using Moonshot's OpenAI-compatible Chat Completions API with support
|
|
@@ -204,8 +233,10 @@ class MoonshotAIProvider {
|
|
|
204
233
|
// backoff for rate limits (429) and overloaded (503) responses.
|
|
205
234
|
const serializedBody = JSON.stringify(body);
|
|
206
235
|
let response = null;
|
|
236
|
+
let rejectedBody;
|
|
207
237
|
for (let attempt = 0; this.rateLimitRetries === 'until-aborted' || attempt <= this.rateLimitRetries; attempt++) {
|
|
208
238
|
signal.throwIfAborted();
|
|
239
|
+
rejectedBody = undefined;
|
|
209
240
|
response = await fetch(`${this.baseUrl}${this.completionsPath}`, {
|
|
210
241
|
method: 'POST',
|
|
211
242
|
headers,
|
|
@@ -213,7 +244,25 @@ class MoonshotAIProvider {
|
|
|
213
244
|
signal,
|
|
214
245
|
});
|
|
215
246
|
if (response.status === 429 || response.status === 503) {
|
|
216
|
-
|
|
247
|
+
// Read the body once: an unread body pins its connection, and only the
|
|
248
|
+
// body says whether a 429 is a rate limit or an exhausted balance.
|
|
249
|
+
try {
|
|
250
|
+
rejectedBody = await response.text();
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
// Caller cancellation still ends the request; any other read failure only
|
|
254
|
+
// loses the reason, so the body counts as read and the retry policy goes on.
|
|
255
|
+
signal.throwIfAborted();
|
|
256
|
+
logger.debug('Moonshot rejected response body could not be read', { error });
|
|
257
|
+
rejectedBody = '';
|
|
258
|
+
}
|
|
259
|
+
const upstreamError = parseUpstreamError(rejectedBody);
|
|
260
|
+
// An exhausted balance does not recover by waiting: a bounded retry
|
|
261
|
+
// budget fails at once, while 'until-aborted' keeps the same request
|
|
262
|
+
// pending so a long-running caller resumes when the account is recharged.
|
|
263
|
+
const quotaExhausted = upstreamError?.type === QUOTA_EXHAUSTED_TYPE;
|
|
264
|
+
const willRetry = this.rateLimitRetries === 'until-aborted' ||
|
|
265
|
+
(!quotaExhausted && attempt < this.rateLimitRetries);
|
|
217
266
|
// Retry-After is equally-valid as delta-seconds or an HTTP-date;
|
|
218
267
|
// parseInt on the date form yields NaN, degrading the backoff to a
|
|
219
268
|
// ~0ms retry against an already rate-limiting API. Guard it.
|
|
@@ -236,6 +285,7 @@ class MoonshotAIProvider {
|
|
|
236
285
|
willRetry,
|
|
237
286
|
retryInMs: delayMs,
|
|
238
287
|
...(hasRetryAfter ? { retryAfterSeconds: parsedRetryAfterSeconds } : {}),
|
|
288
|
+
...(upstreamError ? { upstreamError } : {}),
|
|
239
289
|
});
|
|
240
290
|
}
|
|
241
291
|
catch (error) {
|
|
@@ -243,10 +293,13 @@ class MoonshotAIProvider {
|
|
|
243
293
|
logger.warn('onRateLimit callback threw', { error });
|
|
244
294
|
}
|
|
245
295
|
if (willRetry) {
|
|
246
|
-
logger.warn(
|
|
296
|
+
logger.warn(quotaExhausted
|
|
297
|
+
? 'Moonshot account balance exhausted, retrying until recharged'
|
|
298
|
+
: 'Moonshot API rate limited, retrying', {
|
|
247
299
|
status: response.status,
|
|
248
300
|
attempt: attempt + 1,
|
|
249
301
|
delayMs,
|
|
302
|
+
...(upstreamError ? { upstreamError } : {}),
|
|
250
303
|
});
|
|
251
304
|
await waitForRetry(delayMs, signal);
|
|
252
305
|
continue;
|
|
@@ -255,7 +308,8 @@ class MoonshotAIProvider {
|
|
|
255
308
|
break;
|
|
256
309
|
}
|
|
257
310
|
if (!response.ok) {
|
|
258
|
-
|
|
311
|
+
// A 429/503 body was already read by the retry loop; a body reads only once.
|
|
312
|
+
const errorBody = rejectedBody ?? (await response.text());
|
|
259
313
|
let detail = `HTTP ${response.status}`;
|
|
260
314
|
try {
|
|
261
315
|
const parsed = JSON.parse(errorBody);
|
|
@@ -268,18 +322,20 @@ class MoonshotAIProvider {
|
|
|
268
322
|
detail = errorBody;
|
|
269
323
|
}
|
|
270
324
|
logger.error('Moonshot API error', { status: response.status, detail, errorBody });
|
|
271
|
-
const clientMessage = response.status === 429
|
|
272
|
-
? 'AI
|
|
273
|
-
: response.status ===
|
|
274
|
-
? 'AI
|
|
275
|
-
: response.status ===
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
? '
|
|
280
|
-
: response.status ===
|
|
281
|
-
? 'AI
|
|
282
|
-
:
|
|
325
|
+
const clientMessage = response.status === 429 && parseUpstreamError(errorBody)?.type === QUOTA_EXHAUSTED_TYPE
|
|
326
|
+
? 'AI service is unavailable right now. Please try again later.'
|
|
327
|
+
: response.status === 429
|
|
328
|
+
? 'AI rate limit exceeded. Please try again shortly.'
|
|
329
|
+
: response.status === 401
|
|
330
|
+
? 'AI service configuration error.'
|
|
331
|
+
: response.status === 400 &&
|
|
332
|
+
/prompt is too long|too many tokens|token.*limit|context.*length/i.test(detail)
|
|
333
|
+
? "Conversation too long for the model's context window. Use /compact to free space, or start a new conversation."
|
|
334
|
+
: response.status === 400
|
|
335
|
+
? 'AI request was invalid — check the model and request parameters.'
|
|
336
|
+
: response.status === 503
|
|
337
|
+
? 'AI service is temporarily overloaded. Please try again in a moment.'
|
|
338
|
+
: 'AI service error. Please try again.';
|
|
283
339
|
yield { type: 'error', message: clientMessage, errorKey: 'ai.error.apiError' };
|
|
284
340
|
return;
|
|
285
341
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-moonshot",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Moonshot (Kimi) AI provider for molecule.dev",
|
|
5
5
|
"homepage": "https://www.molecule.dev/packages/api-ai-moonshot",
|
|
6
6
|
"type": "module",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"directory": "packages/api/bonds/ai/moonshot"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@molecule/api-ai": "1.
|
|
37
|
+
"@molecule/api-ai": "1.5.0",
|
|
38
38
|
"@molecule/api-bond": "1.0.4",
|
|
39
39
|
"@types/node": "26.6.5",
|
|
40
40
|
"typescript": "6.0.3",
|