@coda-console/kimi 1.4.2 → 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/README.md CHANGED
@@ -1,3 +1,5 @@
1
1
  # Kimi Code
2
2
 
3
3
  Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发,读 Kimi 的会话文件报出后台命令与子 Agent;资源页的「Kimi」显示账号与额度,可从 Coda 安装和登录。
4
+
5
+ 不声明 `systemPrompt`;会话指令由 Coda 后端在新会话第一次非斜杠命令的用户输入前送入单独的文本块,恢复会话不再送,插件不用处理。
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 2.1.1 maps this quota response to ACP auth_required. Keep the provider's reason, but report an execution failure
4
- // so clients do not send the user to sign in again. A 403 or provider.auth_error alone does not establish quota exhaustion.
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
- if (!/^403 You've reached your 5-hour usage limit\.(?:\s|$)/.test(reason)) return message;
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: 'quota_exhausted' } } } } } };
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 whether Kimi itself classified it as quota
20
- // exhaustion (APIProviderQuotaExhaustedError, from the provider's own quota response). Null for a turn that did not fail,
21
- // or whose filtered response Kimi answers as `refusal`.
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
- return { message, quota: error?.name === 'APIProviderQuotaExhaustedError' };
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.quota ? { data: { _meta: { coda: { failure: { kind: 'quota_exhausted' } } } } } : {}) } });
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
@@ -7,13 +7,12 @@ import { kimiDisplayNames } from './display.mjs';
7
7
  // The turns Kimi's ACP does not tell, its goal's and those of prompts it queued behind them, are told from its files too
8
8
  // (turns.mjs): an answer Kimi gave at once to a prompt in which it said nothing waits for that look, and is held if Kimi
9
9
  // has not run the prompt yet.
10
- // Nothing waits on a look longer than `hold`. CODA_DISABLE_RUNTIME_PATCHES=kimi leaves a plain relay, but for Coda's session
11
- // instructions, which go in front of a new session's first prompt either way (instructions.mjs). A known quota error in
10
+ // Nothing waits on a look longer than `hold`. CODA_DISABLE_RUNTIME_PATCHES=kimi leaves a plain relay. A known quota error in
12
11
  // a prompt's answer is classified at this boundary (errors.mjs), without retrying or changing other errors; an `end_turn`
13
- // Kimi gave to a prompt whose turn its files say failed is passed on as that failure. A Kimi whose files are not as understood
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
14
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
15
15
  // it (`_coda/unrecognized`, docs/agent-protocol.md「没适配的东西」): the user is told what goes unseen.
16
- import { SessionInstructions } from './instructions.mjs';
17
16
  import { classifyKimiError, failedAnswer } from './errors.mjs';
18
17
  import { SUPPORTED, kimiSession } from './observe.mjs';
19
18
  import { KimiWaits } from './waits.mjs';
@@ -36,15 +35,12 @@ export class KimiRelay {
36
35
  this.asked = new Map(); this.prompts = new Set(); this.sent = new Map();
37
36
  // Whether Coda takes what is not understood, and what it was told, by session and name.
38
37
  this.taken = false; this.told = new Set();
39
- this.version = null; this.session = null; this.waits = null; this.turns = null; this.content = []; this.instructions = new SessionInstructions();
38
+ this.version = null; this.session = null; this.waits = null; this.turns = null; this.content = [];
40
39
  // What Kimi said, passed on in order behind any message that waits for a look.
41
40
  this.queue = Promise.resolve();
42
41
  this.looking = null; this.again = false; this.lookedAt = -Infinity; this.timer = null; this.watching = null; this.closed = false;
43
42
  }
44
43
  fromCoda(message) {
45
- const { agent, coda } = this.instructions.fromCoda(message);
46
- if (coda) { this.queue = this.queue.then(() => this.say(coda)); return; }
47
- message = agent;
48
44
  if (!this.plain && 'id' in message && typeof message.method === 'string') {
49
45
  if (message.method === 'initialize') {
50
46
  this.asked.set(message.id, { initialize: true });
@@ -62,7 +58,7 @@ export class KimiRelay {
62
58
  }
63
59
  fromAgent(message) {
64
60
  if (!record(message)) return;
65
- message = kimiDisplayNames(this.instructions.fromAgent(message));
61
+ message = kimiDisplayNames(message);
66
62
  const answer = 'id' in message && !('method' in message), asked = answer ? this.asked.get(message.id) : undefined;
67
63
  if (asked) this.asked.delete(message.id);
68
64
  if (asked?.initialize && record(message.result)) {
@@ -81,8 +77,11 @@ export class KimiRelay {
81
77
  this.queue = this.queue.then(async () => {
82
78
  if (waiting) await this.look({ fresh: !!asked?.prompt });
83
79
  if (sent?.quiet && message.result?.stopReason === 'end_turn' && this.turns?.hold(sent.at, message)) { this.prompts.add(message.id); return; }
84
- const failure = sent && message.result?.stopReason === 'end_turn' ? this.turns?.failure(sent.at) : null;
85
- if (failure) message = failedAnswer(message, failure);
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);
86
85
  this.say(message);
87
86
  const sessionId = asked && !('error' in message) ? message.result?.sessionId ?? asked.sessionId : undefined;
88
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.quota ? { title: '额度不足' } : {}), detail: failure.message.slice(0, 4000) });
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;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "kimi",
4
4
  "displayName": "Kimi Code",
5
5
  "description": "用 Kimi Code 跑会话",
6
- "version": "1.4.2",
6
+ "version": "1.4.4",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],
@@ -1,68 +0,0 @@
1
- // Coda's session instructions (docs/agent-protocol.md「会话指令」) for an agent whose own ACP does not take `systemPrompt`: the
2
- // entry declares that it does, keeps the parameter from the agent, and puts the text in front of the first prompt of a
3
- // session the agent opened new, as a part of its own; a session it loaded has had them. A slash command is the agent's to
4
- // read as one: the instructions wait for the next prompt. Kept the same in every plugin that relays a CLI's ACP.
5
- const record = value => value && typeof value === 'object' && !Array.isArray(value);
6
-
7
- // ACP's `systemPrompt`: its text, '' when there is none; throws on anything but text appended to the agent's own.
8
- export function sessionInstructions(systemPrompt) {
9
- if (systemPrompt === undefined || systemPrompt === null) return '';
10
- if (systemPrompt.mode !== 'append') throw new Error('会话指令只支持 append 模式');
11
- const { content } = systemPrompt;
12
- if (!Array.isArray(content) || content.some(block => block?.type !== 'text' || typeof block.text !== 'string')) throw new Error('会话指令只接受文本块');
13
- return content.map(block => block.text).join('\n\n');
14
- }
15
-
16
- // The part in front of the user's input: delimited, and said to be the client's, not the user's.
17
- const OPENING = '<coda-session-instructions>';
18
- export const instructionsBlock = text => ({ type: 'text', text: `${OPENING}\n`
19
- + 'Instructions from Coda, the client this session runs in, for the whole session. They are not part of the user\'s message.\n\n'
20
- + `${text}\n</coda-session-instructions>\n` });
21
-
22
- export class SessionInstructions {
23
- // Coda's requests whose answers are read here, by id; the instructions of a session yet to have its first prompt.
24
- constructor() { this.asked = new Map(); this.waiting = new Map(); }
25
- // What Coda says: `agent`, what the agent is told, or `coda`, Coda's answer given here instead.
26
- fromCoda(message) {
27
- const { id, method, params } = message;
28
- if (!record(params)) return { agent: message };
29
- if (method === 'initialize' && id !== undefined) this.asked.set(id, { initialize: true });
30
- if (method === 'session/new' || method === 'session/load') {
31
- let text;
32
- try { text = sessionInstructions(params.systemPrompt); }
33
- catch (error) { return { coda: { jsonrpc: '2.0', id, error: { code: -32602, message: error.message } } }; }
34
- const { systemPrompt, ...rest } = params;
35
- if (method === 'session/new' && text && id !== undefined) this.asked.set(id, { text });
36
- return { agent: { ...message, params: rest } };
37
- }
38
- if (method === 'session/prompt' && Array.isArray(params.prompt) && this.waiting.has(params.sessionId)) {
39
- const first = params.prompt.find(block => block?.type === 'text');
40
- if (typeof first?.text === 'string' && first.text.trimStart().startsWith('/')) return { agent: message };
41
- const text = this.waiting.get(params.sessionId);
42
- this.waiting.delete(params.sessionId);
43
- return { agent: { ...message, params: { ...params, prompt: [instructionsBlock(text), ...params.prompt] } } };
44
- }
45
- return { agent: message };
46
- }
47
- // What the agent says, as said to Coda. A title the agent took from the first prompt's text begins with the instructions,
48
- // not with what the user asked: it is left out, and the session keeps the title Coda gives it.
49
- fromAgent(message) {
50
- const update = message?.method === 'session/update' && record(message.params) ? message.params.update : null;
51
- if (record(update) && update.sessionUpdate === 'session_info_update' && typeof update.title === 'string' && update.title.trimStart().startsWith(OPENING)) {
52
- const { title, ...rest } = update;
53
- return { ...message, params: { ...message.params, update: rest } };
54
- }
55
- if (!record(message) || !('id' in message) || 'method' in message) return message;
56
- const asked = this.asked.get(message.id);
57
- if (!asked) return message;
58
- this.asked.delete(message.id);
59
- if (!record(message.result)) return message;
60
- if (asked.text) {
61
- if (typeof message.result.sessionId === 'string') this.waiting.set(message.result.sessionId, asked.text);
62
- return message;
63
- }
64
- const capabilities = record(message.result.agentCapabilities) ? message.result.agentCapabilities : {};
65
- const sessions = record(capabilities.sessionCapabilities) ? capabilities.sessionCapabilities : {};
66
- return { ...message, result: { ...message.result, agentCapabilities: { ...capabilities, sessionCapabilities: { ...sessions, systemPrompt: {} } } } };
67
- }
68
- }