@coda-console/kimi 1.3.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/README.md +1 -1
- package/agent/binary.mjs +1 -1
- 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 +3 -3
- package/scripts/login.sh +1 -1
- package/scripts/quota.mjs +11 -10
- package/scripts/{panel.mjs → resources.mjs} +5 -5
package/README.md
CHANGED
package/agent/binary.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Which Kimi CLI runs: the one its installer puts in ~/.kimi-code/bin, else the first `kimi` on the PATH; the same the
|
|
2
|
-
// 「Kimi」
|
|
2
|
+
// 「Kimi」resource type signs in and reads the quota with (scripts/).
|
|
3
3
|
import { existsSync } from 'node:fs';
|
|
4
4
|
import { homedir } from 'node:os';
|
|
5
5
|
import { join } from 'node:path';
|
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.
|
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.
|
|
6
|
+
"version": "1.4.1",
|
|
7
7
|
"categories": [
|
|
8
8
|
"Agent"
|
|
9
9
|
],
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"setupHint": "请在「资源」里为运行机添加 Kimi。"
|
|
31
31
|
}
|
|
32
32
|
],
|
|
33
|
-
"
|
|
33
|
+
"resources": [
|
|
34
34
|
{
|
|
35
35
|
"id": "account",
|
|
36
36
|
"title": "Kimi",
|
|
37
|
-
"command": "node \"$CODA_PLUGIN_PATH/scripts/
|
|
37
|
+
"command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
|
|
38
38
|
"maxAge": "PT5M",
|
|
39
39
|
"signIn": {
|
|
40
40
|
"command": "exec sh \"$CODA_PLUGIN_PATH/scripts/login.sh\"",
|
package/scripts/login.sh
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
|
-
# The「Kimi」
|
|
2
|
+
# The「Kimi」resource type's login (`signIn`): `kimi login`, the device-code login of the CLI the agent runs. `kimi login` opens the
|
|
3
3
|
# link with `open` (`xdg-open` on Linux) whatever BROWSER says: on a machine the user is not at, that would be a page left
|
|
4
4
|
# open for no one, so it finds do-nothing ones first on its PATH, in this plugin's own state.
|
|
5
5
|
stubs="$CODA_PLUGIN_STATE/no-browser"
|
package/scripts/quota.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// What the「Kimi」
|
|
1
|
+
// What the「Kimi」resource type shows (docs/plugins.md「内置插件:Kimi Code」): the signed-in account and its quota, from what the
|
|
2
2
|
// Kimi CLI's local server says (`/oauth/userinfo`, `/oauth/usage`, the same getManagedUsage its /status uses).
|
|
3
3
|
const line = (value, max = 80) => typeof value === 'string' ? [...value.replace(/\s+/g, ' ').trim()].slice(0, max).join('') : '';
|
|
4
4
|
const instant = value => typeof value === 'string' && Number.isFinite(Date.parse(value)) ? new Date(value).toISOString() : null;
|
|
@@ -11,8 +11,8 @@ export function account(data) {
|
|
|
11
11
|
const id = typeof data.userInfo.userId === 'string' || typeof data.userInfo.userId === 'number' ? line(String(data.userInfo.userId), 120) : '';
|
|
12
12
|
return { name: line(data.userInfo.nickname), plan: line(data.userInfo.userLevelName), ...(id ? { id } : {}) };
|
|
13
13
|
}
|
|
14
|
-
// The windows the plan has (5 hours, 7 days, monthly; those it has not are skipped), then the booster balance
|
|
15
|
-
// is left; a used ratio unknown
|
|
14
|
+
// The windows the plan has (5 hours, 7 days, monthly; those it has not are skipped), then the booster balance: values of
|
|
15
|
+
// what is left, with when it resets; a used ratio unknown leaves its window out.
|
|
16
16
|
export function windows(data) {
|
|
17
17
|
if (data?.kind !== 'ok' || !data.quota) return null;
|
|
18
18
|
const usages = data.quota.usages && typeof data.quota.usages === 'object' ? data.quota.usages : {};
|
|
@@ -21,19 +21,20 @@ export function windows(data) {
|
|
|
21
21
|
const used = ratio(usages[key]?.usedRatio);
|
|
22
22
|
if (used === null) return [];
|
|
23
23
|
const left = Math.round(100 - Math.min(100, used * 100)), resets = instant(usages[key].resetAt);
|
|
24
|
-
return [{
|
|
24
|
+
return [{ label, value: `剩余 ${left}%`, fraction: left / 100, ...(resets ? { time: resets } : {}) }];
|
|
25
25
|
});
|
|
26
26
|
const wallet = data.quota.extraUsage;
|
|
27
27
|
const [total, left] = [wallet?.totalCents, wallet?.balanceCents].map(v => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : null);
|
|
28
28
|
if (wallet && total !== null && left !== null) {
|
|
29
29
|
const share = total > 0 ? Math.round(Math.min(1, left / total) * 100) : 0;
|
|
30
|
-
meters.push({
|
|
30
|
+
meters.push({ label: 'Extra Usage 余额', value: `剩余 ${share}%`, fraction: share / 100 });
|
|
31
31
|
}
|
|
32
32
|
return meters;
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
// The login as a resource: its nickname, its quota, or why it has none.
|
|
35
|
+
export function resource(who, usage) {
|
|
36
|
+
const values = usage.error ? [] : windows(usage.data) ?? [];
|
|
37
|
+
const error = usage.error ? `暂时无法获取额度:${usage.error}` : values.length ? null : '暂时无法获取额度:Kimi 没有给出额度';
|
|
38
|
+
// The account, compared across machines by Coda's group view: its user ID, which the row does not show.
|
|
39
|
+
return { name: who.name || 'Kimi', ...(who.id ? { identity: `kimi:${who.id}` } : {}), values, ...(error ? { error: line(error) } : {}) };
|
|
39
40
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The「Kimi」
|
|
1
|
+
// The「Kimi」resources, printed once as `{ resources }`. No login file here shows nothing (the type is then offered under
|
|
2
2
|
// 「添加资源」). Who is signed in is asked once for each state of the login file, which the CLI rewrites as it signs in and
|
|
3
3
|
// renews, and kept in this plugin's state: Kimi's own upstream lookup takes seconds and gives up after 8. The quota is
|
|
4
4
|
// asked each reading, of the same `kimi web`.
|
|
@@ -6,16 +6,16 @@ import { readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
|
6
6
|
import { homedir } from 'node:os';
|
|
7
7
|
import { join } from 'node:path';
|
|
8
8
|
import { kimiBinary } from '../agent/binary.mjs';
|
|
9
|
-
import { account,
|
|
9
|
+
import { account, resource } from './quota.mjs';
|
|
10
10
|
import { kimiWeb } from './web.mjs';
|
|
11
11
|
|
|
12
12
|
const home = process.env.KIMI_CODE_HOME || join(homedir(), '.kimi-code');
|
|
13
13
|
const print = value => console.log(JSON.stringify(value));
|
|
14
14
|
let changed;
|
|
15
15
|
try { changed = statSync(join(home, 'credentials/kimi-code.json')).mtimeMs; }
|
|
16
|
-
catch (error) { if (error.code === 'ENOENT') { print({
|
|
16
|
+
catch (error) { if (error.code === 'ENOENT') { print({ resources: [] }); process.exit(0); } console.error(`读不了 Kimi 的登录:${error.code}`); process.exit(1); }
|
|
17
17
|
const binary = kimiBinary();
|
|
18
|
-
if (!binary) { print({
|
|
18
|
+
if (!binary) { print({ resources: [{ name: 'Kimi', error: '这台机器上找不到 Kimi CLI,用「···」里的「重新登录」安装' }] }); process.exit(0); }
|
|
19
19
|
|
|
20
20
|
const seenFile = process.env.CODA_PLUGIN_STATE && join(process.env.CODA_PLUGIN_STATE, 'account.json');
|
|
21
21
|
let seen = null;
|
|
@@ -33,5 +33,5 @@ try {
|
|
|
33
33
|
if (seenFile) { writeFileSync(`${seenFile}.tmp`, JSON.stringify({ changed, account: who })); renameSync(`${seenFile}.tmp`, seenFile); }
|
|
34
34
|
} catch { /* Not told now: the account seen last, or the kind's own name, and asked again next time. */ }
|
|
35
35
|
}
|
|
36
|
-
print({
|
|
36
|
+
print({ resources: who ? [resource(who, usage)] : [] });
|
|
37
37
|
} catch (error) { console.error(error.message); process.exit(1); }
|