@coda-console/kimi 1.4.3 → 1.4.4
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/agent/errors.mjs +26 -9
- package/agent/relay.mjs +7 -3
- package/agent/turns.mjs +1 -1
- package/package.json +1 -1
package/agent/errors.mjs
CHANGED
|
@@ -1,29 +1,46 @@
|
|
|
1
1
|
const record = value => value && typeof value === 'object' && !Array.isArray(value);
|
|
2
2
|
|
|
3
|
-
// Kimi
|
|
4
|
-
//
|
|
3
|
+
// Kimi's own words for a quota used up, whatever status or error code carries them (a 403 provider.auth_error for the five-hour
|
|
4
|
+
// window, a 429 for the weekly one).
|
|
5
|
+
const QUOTA_TEXT = /^(?:\d{3} )?You've reached your (?:5-hour|weekly \(7-day\)) usage limit\.(?:\s|$)/;
|
|
6
|
+
|
|
7
|
+
// Kimi 2.1.1 maps the quota response to ACP -32000 (auth_required). Report it as the execution failure it is (-32603), with the
|
|
8
|
+
// category turnFailure gives that reason, so clients do not send the user to sign in again.
|
|
5
9
|
export function classifyKimiError(message) {
|
|
6
10
|
const error = message.error;
|
|
7
11
|
if (error?.code !== -32000 || typeof error.message !== 'string') return message;
|
|
8
12
|
const reason = error.message.replace(/^Authentication required:\s*/, '');
|
|
9
|
-
|
|
13
|
+
const { kind } = turnFailure('failed', { message: reason });
|
|
14
|
+
if (!kind) return message;
|
|
10
15
|
const data = record(error.data) ? error.data : error.data == null ? {} : { details: error.data };
|
|
11
16
|
const meta = record(data._meta) ? data._meta : {};
|
|
12
17
|
const coda = record(meta.coda) ? meta.coda : {};
|
|
13
18
|
return { ...message, error: { ...error, code: -32603,
|
|
14
19
|
message: reason,
|
|
15
|
-
data: { ...data, _meta: { ...meta, coda: { ...coda, failure: { kind
|
|
20
|
+
data: { ...data, _meta: { ...meta, coda: { ...coda, failure: { kind } } } } } };
|
|
16
21
|
}
|
|
17
22
|
|
|
18
23
|
// Kimi 2.1.1's ACP answers a prompt whose turn failed with `end_turn`, the error kept to the `turn.ended` of its main wire
|
|
19
|
-
// (observe.mjs). What failed so, read from that end: the provider's reason, and
|
|
20
|
-
//
|
|
21
|
-
// or
|
|
24
|
+
// (observe.mjs). What failed so, read from that end: the provider's reason, and the category Kimi's own error says: its
|
|
25
|
+
// APIProviderQuotaExhaustedError (the provider's quota response), or a fault that clears by itself (an upstream overload or 5xx
|
|
26
|
+
// status, a connection failure or timeout, a 429 that is not quota), which Kimi already retried before ending the turn. Kimi's own
|
|
27
|
+
// words for a quota used up (QUOTA_TEXT) count whatever its name, status or code say, so they are judged before any of those.
|
|
28
|
+
// Null for a turn that did not fail, or whose filtered response Kimi answers as `refusal`. Other failures (authentication,
|
|
29
|
+
// context, invalid request) carry no category, except that Kimi's own `auth.login_required` (its OAuth provider's token could
|
|
30
|
+
// not be refreshed: OAuthUnauthorizedError) is a login gone, `auth_required` (docs/agent-protocol.md「需要重新登录」). Its
|
|
31
|
+
// `provider.auth_error` is the provider rejecting the credentials, a key or a permission as well, which logging in again does not mend.
|
|
22
32
|
export function turnFailure(reason, error) {
|
|
23
33
|
if (reason !== 'failed' || error?.code === 'provider.filtered') return null;
|
|
24
34
|
const message = typeof error?.message === 'string' && error.message.trim() ? error.message : 'Kimi 这一轮执行失败';
|
|
25
|
-
|
|
35
|
+
const status = error?.details?.statusCode;
|
|
36
|
+
if (error?.code === 'auth.login_required') return { message, kind: 'auth_required' };
|
|
37
|
+
if (error?.name === 'APIProviderQuotaExhaustedError' || QUOTA_TEXT.test(message)) return { message, kind: 'quota_exhausted' };
|
|
38
|
+
if (error?.name === 'APIProviderOverloadedError' || error?.code === 'provider.connection_error' || status === 429 ||
|
|
39
|
+
(Number.isInteger(status) && status >= 500 && status <= 599))
|
|
40
|
+
return { message, kind: 'service_unavailable' };
|
|
41
|
+
return { message };
|
|
26
42
|
}
|
|
27
43
|
// The answer to a prompt whose turn failed so, as the execution failure it was (docs/agent-protocol.md「执行失败」).
|
|
28
44
|
export const failedAnswer = (answer, failure) => ({ jsonrpc: answer.jsonrpc ?? '2.0', id: answer.id, error: { code: -32603, message: failure.message,
|
|
29
|
-
...(failure.
|
|
45
|
+
...(failure.kind ? { data: { _meta: { coda: { failure: failure.kind === 'quota_exhausted' ? { kind: failure.kind } :
|
|
46
|
+
failure.kind === 'auth_required' ? { kind: failure.kind, login: 'Kimi Code OAuth' } : { kind: failure.kind, input: 'accepted' } } } } } : {}) } });
|
package/agent/relay.mjs
CHANGED
|
@@ -9,7 +9,8 @@ import { kimiDisplayNames } from './display.mjs';
|
|
|
9
9
|
// has not run the prompt yet.
|
|
10
10
|
// Nothing waits on a look longer than `hold`. CODA_DISABLE_RUNTIME_PATCHES=kimi leaves a plain relay. A known quota error in
|
|
11
11
|
// a prompt's answer is classified at this boundary (errors.mjs), without retrying or changing other errors; an `end_turn`
|
|
12
|
-
// Kimi gave to a prompt whose turn its files say failed is passed on as that failure
|
|
12
|
+
// Kimi gave to a prompt whose turn its files say failed is passed on as that failure, and so is a -32000 to a prompt whose turn its files
|
|
13
|
+
// say ended on a lost login (`auth.login_required`). A Kimi whose files are not as understood
|
|
13
14
|
// (another version, another layout) is relayed as it would be anyway, but said so once a session and way, to a Coda that takes
|
|
14
15
|
// it (`_coda/unrecognized`, docs/agent-protocol.md「没适配的东西」): the user is told what goes unseen.
|
|
15
16
|
import { classifyKimiError, failedAnswer } from './errors.mjs';
|
|
@@ -76,8 +77,11 @@ export class KimiRelay {
|
|
|
76
77
|
this.queue = this.queue.then(async () => {
|
|
77
78
|
if (waiting) await this.look({ fresh: !!asked?.prompt });
|
|
78
79
|
if (sent?.quiet && message.result?.stopReason === 'end_turn' && this.turns?.hold(sent.at, message)) { this.prompts.add(message.id); return; }
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
// Kimi's ACP also rejects a prompt whose turn ended on a lost login with -32000 (the handshake's "not signed in"), no code in it; the
|
|
81
|
+
// turn's own end says which it was, and mid-run it is an execution failure (-32603).
|
|
82
|
+
const failure = sent ? this.turns?.failure(sent.at) : null;
|
|
83
|
+
if (failure && (message.result?.stopReason === 'end_turn' || failure.kind === 'auth_required' && message.error?.code === -32000))
|
|
84
|
+
message = failedAnswer(message, failure);
|
|
81
85
|
this.say(message);
|
|
82
86
|
const sessionId = asked && !('error' in message) ? message.result?.sessionId ?? asked.sessionId : undefined;
|
|
83
87
|
if (typeof sessionId === 'string') this.begin(sessionId, asked.cwd);
|
package/agent/turns.mjs
CHANGED
|
@@ -103,7 +103,7 @@ export class KimiTurns {
|
|
|
103
103
|
finish(told) {
|
|
104
104
|
const failure = this.goal.failure;
|
|
105
105
|
if (!failure) { this.say(told, 'done'); return; }
|
|
106
|
-
this.say(told, 'failed', { ...(failure.
|
|
106
|
+
this.say(told, 'failed', { ...(failure.kind === 'quota_exhausted' ? { title: '额度不足' } : {}), detail: failure.message.slice(0, 4000) });
|
|
107
107
|
}
|
|
108
108
|
say(told, state, more = {}) {
|
|
109
109
|
this.goal.state = state;
|