@coda-console/kimi 1.4.0 → 1.4.1
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 +13 -0
- package/agent/observe.mjs +3 -3
- package/agent/relay.mjs +5 -2
- package/agent/turns.mjs +31 -12
- package/package.json +1 -1
package/agent/errors.mjs
CHANGED
|
@@ -14,3 +14,16 @@ export function classifyKimiError(message) {
|
|
|
14
14
|
message: reason,
|
|
15
15
|
data: { ...data, _meta: { ...meta, coda: { ...coda, failure: { kind: 'quota_exhausted' } } } } } };
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
// 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`.
|
|
22
|
+
export function turnFailure(reason, error) {
|
|
23
|
+
if (reason !== 'failed' || error?.code === 'provider.filtered') return null;
|
|
24
|
+
const message = typeof error?.message === 'string' && error.message.trim() ? error.message : 'Kimi 这一轮执行失败';
|
|
25
|
+
return { message, quota: error?.name === 'APIProviderQuotaExhaustedError' };
|
|
26
|
+
}
|
|
27
|
+
// The answer to a prompt whose turn failed so, as the execution failure it was (docs/agent-protocol.md「执行失败」).
|
|
28
|
+
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' } } } } } : {}) } });
|
package/agent/observe.mjs
CHANGED
|
@@ -74,8 +74,8 @@ export function kimiSession({ sessionId, cwd, env = {}, version, startedAt = 0 }
|
|
|
74
74
|
// `waiting`, `handling`, `done` or `cancelled`, with the created time and fire cursor its next fire is counted from) and
|
|
75
75
|
// the schedule config that count uses (`schedule`), and what its subagents said since the last look, to `emit` as their
|
|
76
76
|
// own sessions' updates. `main` is what the main wire added since the last look, in its order, for turns.mjs: the goal
|
|
77
|
-
// as it changes, each main turn as it begins and ends, and what is said in a
|
|
78
|
-
// 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
|
|
77
|
+
// 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
|
|
78
|
+
// 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
79
|
// 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
|
|
80
80
|
// out as understood; a read that fails (a file being written) throws. A cron or goal event that is not the shape
|
|
81
81
|
// understood is skipped, so one of them cannot hide the tasks.
|
|
@@ -171,7 +171,7 @@ export function kimiSession({ sessionId, cwd, env = {}, version, startedAt = 0 }
|
|
|
171
171
|
turnEndedAt = event.time;
|
|
172
172
|
for (const cron of crons.values()) if (cron.fireTurn === event.turnId) cron.fireEnded = true;
|
|
173
173
|
if (span.turn?.turnId === event.turnId) span.turn = null;
|
|
174
|
-
if (fresh) main.push({ type: 'ended', turnId: event.turnId, reason: event.reason });
|
|
174
|
+
if (fresh) main.push({ type: 'ended', turnId: event.turnId, reason: event.reason, ...(object(event.error) ? { error: event.error } : {}) });
|
|
175
175
|
} else if (event.type === 'goal.create' || event.type === 'goal.update' || event.type === 'goal.clear') {
|
|
176
176
|
const next = noteGoal(goal, event);
|
|
177
177
|
if (next === goal) continue;
|
package/agent/relay.mjs
CHANGED
|
@@ -9,9 +9,10 @@ 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, but for Coda's session
|
|
11
11
|
// instructions, which go in front of a new session's first prompt either way (instructions.mjs). A known quota error in
|
|
12
|
-
// a prompt's answer is classified at this boundary (errors.mjs), without retrying or changing other errors
|
|
12
|
+
// 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.
|
|
13
14
|
import { SessionInstructions } from './instructions.mjs';
|
|
14
|
-
import { classifyKimiError } from './errors.mjs';
|
|
15
|
+
import { classifyKimiError, failedAnswer } from './errors.mjs';
|
|
15
16
|
import { SUPPORTED, kimiSession } from './observe.mjs';
|
|
16
17
|
import { KimiWaits } from './waits.mjs';
|
|
17
18
|
import { KimiTurns } from './turns.mjs';
|
|
@@ -71,6 +72,8 @@ export class KimiRelay {
|
|
|
71
72
|
this.queue = this.queue.then(async () => {
|
|
72
73
|
if (waiting) await this.look({ fresh: !!asked?.prompt });
|
|
73
74
|
if (sent?.quiet && message.result?.stopReason === 'end_turn' && this.turns?.hold(sent.at, message)) { this.prompts.add(message.id); return; }
|
|
75
|
+
const failure = sent && message.result?.stopReason === 'end_turn' ? this.turns?.failure(sent.at) : null;
|
|
76
|
+
if (failure) message = failedAnswer(message, failure);
|
|
74
77
|
this.say(message);
|
|
75
78
|
const sessionId = asked && !('error' in message) ? message.result?.sessionId ?? asked.sessionId : undefined;
|
|
76
79
|
if (typeof sessionId === 'string') this.begin(sessionId, asked.cwd);
|
package/agent/turns.mjs
CHANGED
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
// reaches it while such a turn is going is queued, and its ACP answers it at once though it runs later.
|
|
4
4
|
// So the goal is told as a timer that fires again after each turn (docs/agent-protocol.md「_coda/wait」): `handling` through
|
|
5
5
|
// each of its turns, which Coda takes up as turns of their own, `waiting` between them, `done` once the goal is no longer
|
|
6
|
-
// active. A Coda prompt Kimi answered at once without running it is held: its answer waits for the turn that runs it, the
|
|
7
|
-
// first of Coda's prompts' turns that began after it was sent.
|
|
6
|
+
// active, `failed` with the reason when the last main turn before that failed (Kimi pauses its goal after a turn fails). A Coda prompt Kimi answered at once without running it is held: its answer waits for the turn that runs it, the
|
|
7
|
+
// first of Coda's prompts' turns that began after it was sent. A prompt whose turn failed is answered with that failure
|
|
8
|
+
// (errors.mjs), which Kimi's ACP answers as `end_turn`. What is said in a goal's turn or a held prompt's is told as
|
|
8
9
|
// this session's updates; a turn from before this agent process (`startedAt`) is another process's and is not told.
|
|
10
|
+
import { failedAnswer, turnFailure } from './errors.mjs';
|
|
11
|
+
|
|
9
12
|
const CONTINUES = '目标未完成前,每轮结束后自动继续';
|
|
10
13
|
// How Kimi's ACP answers a turn that ended so.
|
|
11
14
|
const STOPPED = { cancelled: 'cancelled', blocked: 'refusal' };
|
|
@@ -13,7 +16,7 @@ const STOPPED = { cancelled: 'cancelled', blocked: 'refusal' };
|
|
|
13
16
|
export class KimiTurns {
|
|
14
17
|
constructor({ sessionId, startedAt = 0 }) {
|
|
15
18
|
this.sessionId = sessionId; this.startedAt = startedAt;
|
|
16
|
-
// The goal as told ({ id, active, state, turn }) and as the wire last left it. Main turns going, those whose content is
|
|
19
|
+
// The goal as told ({ id, active, state, turn, failure }) and as the wire last left it. Main turns going, those whose content is
|
|
17
20
|
// told, the last Coda prompt's turn seen, and the prompts held.
|
|
18
21
|
this.goal = null; this.latest = null; this.going = new Set(); this.telling = new Set(); this.user = null; this.held = [];
|
|
19
22
|
}
|
|
@@ -45,14 +48,14 @@ export class KimiTurns {
|
|
|
45
48
|
if (open(goal)) {
|
|
46
49
|
goal.active = goal.id === entry.id && entry.active;
|
|
47
50
|
if (goal.active || goal.turn !== undefined) return;
|
|
48
|
-
this.
|
|
51
|
+
this.finish(told);
|
|
49
52
|
}
|
|
50
53
|
this.start(told);
|
|
51
54
|
}
|
|
52
55
|
start(told) {
|
|
53
56
|
const latest = this.latest;
|
|
54
57
|
if (!latest?.active || this.goal?.id === latest.id) return;
|
|
55
|
-
this.goal = { id: latest.id, active: true, state: 'waiting', turn: undefined };
|
|
58
|
+
this.goal = { id: latest.id, active: true, state: 'waiting', turn: undefined, failure: null };
|
|
56
59
|
told.push(wait({ sessionId: this.sessionId, waitId: latest.id, state: 'waiting', kind: 'timer', title: latest.title || 'Kimi 目标',
|
|
57
60
|
detail: CONTINUES, recurring: true, stoppable: false }));
|
|
58
61
|
}
|
|
@@ -67,28 +70,44 @@ export class KimiTurns {
|
|
|
67
70
|
this.telling.add(turnId);
|
|
68
71
|
this.say(told, 'handling');
|
|
69
72
|
} else if (kind === 'user') {
|
|
70
|
-
this.user = { turnId, time, ended: false, claimed: false };
|
|
73
|
+
this.user = { turnId, time, ended: false, claimed: false, failure: null };
|
|
71
74
|
const held = this.held.find(h => h.turnId === undefined && h.sentAt <= time);
|
|
72
75
|
if (held) { held.turnId = turnId; this.user.claimed = true; this.telling.add(turnId); }
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
|
-
end({ turnId, reason }, told) {
|
|
78
|
+
end({ turnId, reason, error }, told) {
|
|
76
79
|
if (!this.going.delete(turnId)) return;
|
|
77
80
|
this.telling.delete(turnId);
|
|
78
|
-
|
|
81
|
+
const failure = turnFailure(reason, error);
|
|
82
|
+
if (this.user?.turnId === turnId) { this.user.ended = true; this.user.failure = failure; }
|
|
83
|
+
if (open(this.goal)) this.goal.failure = failure;
|
|
79
84
|
if (this.goal?.turn === turnId) this.settle(told);
|
|
80
85
|
const i = this.held.findIndex(h => h.turnId === turnId);
|
|
81
|
-
if (i >= 0)
|
|
86
|
+
if (i >= 0) {
|
|
87
|
+
const { answer } = this.held.splice(i, 1)[0];
|
|
88
|
+
told.push(failure ? failedAnswer(answer, failure) : answered(answer, STOPPED[reason] ?? 'end_turn'));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// How the turn of the prompt sent at `sentAt` failed, once it ended so; null otherwise. For an answer not held.
|
|
92
|
+
failure(sentAt) {
|
|
93
|
+
const user = this.user;
|
|
94
|
+
return user && !user.claimed && user.ended && user.time >= sentAt ? user.failure : null;
|
|
82
95
|
}
|
|
83
96
|
// The goal's turn is over: it waits for the next, or ends, and the goal that replaced it is told.
|
|
84
97
|
settle(told) {
|
|
85
98
|
this.goal.turn = undefined;
|
|
86
|
-
|
|
99
|
+
if (this.goal.active) this.say(told, 'waiting'); else this.finish(told);
|
|
87
100
|
this.start(told);
|
|
88
101
|
}
|
|
89
|
-
|
|
102
|
+
// The goal is no longer active: done, or failed as the last main turn did.
|
|
103
|
+
finish(told) {
|
|
104
|
+
const failure = this.goal.failure;
|
|
105
|
+
if (!failure) { this.say(told, 'done'); return; }
|
|
106
|
+
this.say(told, 'failed', { ...(failure.quota ? { title: '额度不足' } : {}), detail: failure.message.slice(0, 4000) });
|
|
107
|
+
}
|
|
108
|
+
say(told, state, more = {}) {
|
|
90
109
|
this.goal.state = state;
|
|
91
|
-
told.push(wait({ sessionId: this.sessionId, waitId: this.goal.id, state }));
|
|
110
|
+
told.push(wait({ sessionId: this.sessionId, waitId: this.goal.id, state, ...more }));
|
|
92
111
|
}
|
|
93
112
|
// Coda stopped: Kimi's ACP cannot stop a turn it does not drive, so a held prompt is answered as stopped now, and what its
|
|
94
113
|
// turn says if it still runs is not told.
|