@coda-console/kimi 1.4.3 → 1.5.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/README.md CHANGED
@@ -3,3 +3,5 @@
3
3
  Kimi Code Agent:Coda 的入口在运行机的 kimi acp 与 Coda 之间转发,读 Kimi 的会话文件报出后台命令与子 Agent;资源页的「Kimi」显示账号与额度,可从 Coda 安装和登录。
4
4
 
5
5
  不声明 `systemPrompt`;会话指令由 Coda 后端在新会话第一次非斜杠命令的用户输入前送入单独的文本块,恢复会话不再送,插件不用处理。
6
+
7
+ 压缩上下文(自动,或用户的 `/compact`):Kimi 的 ACP 只用一段文字说,所以读主 Agent 的 wire(`agents/main/wire.jsonl`)里的 `full_compaction.begin`、`full_compaction.complete`、`full_compaction.cancel`,按 ACP `compaction_update` 报给 Coda:开始 `in_progress`,完成 `completed`,取消 `failed`,同一次压缩用同一个 `compactionId`。和其余会话文件观察一样,只认核对过的 Kimi 版本;本进程启动前就开始的压缩只报结束。子 Agent 的压缩不报。
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/observe.mjs CHANGED
@@ -76,7 +76,7 @@ export function kimiSession({ sessionId, cwd, env = {}, version, startedAt = 0,
76
76
  // the schedule config that count uses (`schedule`), and what its subagents said since the last look, to `emit` as their
77
77
  // own sessions' updates. `main` is what the main wire added since the last look, in its order, for turns.mjs: the goal
78
78
  // as it changes, each main turn as it begins and ends (with the error a failed one ended with), and what is said in a
79
- // goal's or Coda's prompt's turn (as ACP updates); it opens with the goal and the turns still going as of the last look. A cron's prompt and a goal's objective
79
+ // goal's or Coda's prompt's turn (as ACP updates), and each compaction of its context as it begins and ends; it opens with the goal and the turns still going as of the last look. A cron's prompt and a goal's objective
80
80
  // do not leave here, only a short title cut from the first line. Null when the files are not this session's, or not laid
81
81
  // out as understood; a read that fails (a file being written) throws. A cron or goal event that is not the shape
82
82
  // understood is skipped, so one of them cannot hide the tasks.
@@ -175,6 +175,8 @@ export function kimiSession({ sessionId, cwd, env = {}, version, startedAt = 0,
175
175
  for (const cron of crons.values()) if (cron.fireTurn === event.turnId) cron.fireEnded = true;
176
176
  if (span.turn?.turnId === event.turnId) span.turn = null;
177
177
  if (fresh) main.push({ type: 'ended', turnId: event.turnId, reason: event.reason, ...(object(event.error) ? { error: event.error } : {}) });
178
+ } else if (COMPACTION[event.type]) {
179
+ if (fresh && event.agentId === 'main') main.push({ type: 'compaction', phase: COMPACTION[event.type], time: event.time });
178
180
  } else if (event.type === 'goal.create' || event.type === 'goal.update' || event.type === 'goal.clear') {
179
181
  const next = noteGoal(goal, event);
180
182
  if (next === goal) continue;
@@ -196,6 +198,8 @@ export function kimiSession({ sessionId, cwd, env = {}, version, startedAt = 0,
196
198
  } };
197
199
  }
198
200
 
201
+ // How the main wire tells its context being compacted (`full_compaction.begin` of an auto or a manual one, then one of the others).
202
+ const COMPACTION = { 'full_compaction.begin': 'begin', 'full_compaction.complete': 'complete', 'full_compaction.cancel': 'cancel' };
199
203
  // Who began a main turn: Coda's prompt (`user`), the goal (`goal`), or something else Kimi took up.
200
204
  function turnKind(origin) {
201
205
  if (origin?.kind === 'user') return 'user';
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. 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
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
- const failure = sent && message.result?.stopReason === 'end_turn' ? this.turns?.failure(sent.at) : null;
80
- 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);
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
@@ -19,6 +19,8 @@ export class KimiTurns {
19
19
  // The goal as told ({ id, active, state, turn, failure }) and as the wire last left it. Main turns going, those whose content is
20
20
  // told, the last Coda prompt's turn seen, and the prompts held.
21
21
  this.goal = null; this.latest = null; this.going = new Set(); this.telling = new Set(); this.user = null; this.held = [];
22
+ // The id of the compaction going, told as `compaction_update` (docs/agent-protocol.md): Kimi's ACP says it in text only.
23
+ this.compaction = null;
22
24
  }
23
25
  // Holds `answer`, Kimi's answer to the prompt sent at `sentAt`, when Kimi did not run that prompt yet: no turn of Coda's
24
26
  // prompts began since, or the one that did is still going, while some turn is. False when it is to be passed on now.
@@ -36,10 +38,19 @@ export class KimiTurns {
36
38
  if (entry.type === 'goal') this.noteGoal(entry, told);
37
39
  else if (entry.type === 'turn') this.begin(entry, told);
38
40
  else if (entry.type === 'ended') this.end(entry, told);
41
+ else if (entry.type === 'compaction') this.compact(entry, told);
39
42
  else if (entry.type === 'update' && this.telling.has(entry.turnId)) told.push(update(this.sessionId, entry.update));
40
43
  }
41
44
  return told;
42
45
  }
46
+ // The main agent's context is being compacted, auto or at Coda's `/compact`: told when it begins and when it ends, under one
47
+ // id. One that began before this process is told only at its end.
48
+ compact({ phase, time }, told) {
49
+ this.compaction ??= `${this.sessionId}:${time}`;
50
+ told.push(update(this.sessionId, { sessionUpdate: 'compaction_update', compactionId: this.compaction,
51
+ status: { begin: 'in_progress', complete: 'completed', cancel: 'failed' }[phase], error: phase === 'cancel' ? '压缩已取消' : null }));
52
+ if (phase !== 'begin') this.compaction = null;
53
+ }
43
54
  // The goal as the wire now leaves it. The one told goes on while it is this one and active; otherwise it ends, once its
44
55
  // turn did, and this one is told if active.
45
56
  noteGoal(entry, told) {
@@ -103,7 +114,7 @@ export class KimiTurns {
103
114
  finish(told) {
104
115
  const failure = this.goal.failure;
105
116
  if (!failure) { this.say(told, 'done'); return; }
106
- this.say(told, 'failed', { ...(failure.quota ? { title: '额度不足' } : {}), detail: failure.message.slice(0, 4000) });
117
+ this.say(told, 'failed', { ...(failure.kind === 'quota_exhausted' ? { title: '额度不足' } : {}), detail: failure.message.slice(0, 4000) });
107
118
  }
108
119
  say(told, state, more = {}) {
109
120
  this.goal.state = state;
@@ -118,6 +129,7 @@ export class KimiTurns {
118
129
  // The files no longer read as understood: the goal ends, and held answers are passed on as Kimi gave them.
119
130
  lost() {
120
131
  const told = [];
132
+ if (this.compaction) this.compact({ phase: 'cancel' }, told);
121
133
  if (open(this.goal)) { this.goal.turn = undefined; this.say(told, 'failed'); }
122
134
  this.going.clear(); this.telling.clear(); this.user = null;
123
135
  return [...told, ...this.held.splice(0).map(held => held.answer)];
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.3",
6
+ "version": "1.5.0",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],