@coda-console/kimi 1.5.0 → 1.5.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/entry.mjs +26 -4
- package/agent/exit.mjs +12 -0
- package/agent/relay.mjs +2 -0
- package/package.json +1 -1
package/agent/entry.mjs
CHANGED
|
@@ -1,20 +1,42 @@
|
|
|
1
1
|
// The Kimi agent as Coda starts it (the plugin's `contributes.agents`): `kimi acp` as a child in this process's group, with a relay
|
|
2
|
-
// between it and Coda that speaks Coda's protocol for it (relay.mjs, docs/agent-protocol.md). It ends as Kimi ends
|
|
2
|
+
// between it and Coda that speaks Coda's protocol for it (relay.mjs, docs/agent-protocol.md). It ends as Kimi ends; when Kimi ends on
|
|
3
|
+
// its own (not as Coda's input closing or a signal to this process stops it) with a non-zero exit code, a signal or a prompt
|
|
4
|
+
// unanswered, the log and the prompts still waiting say how (exit.mjs).
|
|
3
5
|
import { spawn } from 'node:child_process';
|
|
4
6
|
import { readLines } from './lines.mjs';
|
|
5
7
|
import { kimiBinary } from './binary.mjs';
|
|
6
8
|
import { KimiRelay } from './relay.mjs';
|
|
9
|
+
import { describeExit, endedError } from './exit.mjs';
|
|
7
10
|
|
|
8
11
|
const startedAt = Date.now();
|
|
9
|
-
const kimi = spawn(kimiBinary() ?? 'kimi', ['acp'], { stdio: ['pipe', 'pipe', '
|
|
12
|
+
const kimi = spawn(kimiBinary() ?? 'kimi', ['acp'], { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
13
|
+
let stderr = '';
|
|
14
|
+
kimi.stderr.on('data', chunk => { process.stderr.write(chunk); stderr = (stderr + chunk).slice(-2048); });
|
|
10
15
|
kimi.stdin.on('error', () => {});
|
|
16
|
+
let stopping = false;
|
|
17
|
+
const stop = () => {
|
|
18
|
+
if (stopping) return;
|
|
19
|
+
stopping = true; kimi.stdin.end(); kimi.kill();
|
|
20
|
+
setTimeout(() => kimi.kill('SIGKILL'), 800).unref();
|
|
21
|
+
};
|
|
11
22
|
const parse = line => { try { const value = JSON.parse(line); return value && typeof value === 'object' && !Array.isArray(value) ? value : null; } catch { return null; } };
|
|
12
23
|
const relay = new KimiRelay({ startedAt, say: message => process.stdout.write(`${JSON.stringify(message)}\n`),
|
|
13
24
|
tell: message => kimi.stdin.write(`${JSON.stringify(message)}\n`) });
|
|
14
|
-
readLines(process.stdin).on('line', line => { const message = parse(line); if (message) relay.fromCoda(message); }).on('close', () => kimi.stdin.end());
|
|
25
|
+
readLines(process.stdin).on('line', line => { const message = parse(line); if (message) relay.fromCoda(message); }).on('close', () => { stopping = true; kimi.stdin.end(); });
|
|
15
26
|
readLines(kimi.stdout).on('line', line => { const message = parse(line); if (message) relay.fromAgent(message); });
|
|
16
27
|
// What Kimi said before it ended reaches Coda before this process ends.
|
|
17
28
|
let ended = false;
|
|
18
29
|
const end = code => { if (ended) return; ended = true; relay.close(); void relay.drained().then(() => process.stdout.write('', () => process.exit(code))); };
|
|
19
|
-
kimi.on('close', code =>
|
|
30
|
+
kimi.on('close', (code, signal) => {
|
|
31
|
+
// Kimi's last words pass first; the prompts it leaves unanswered fail with how it ended.
|
|
32
|
+
void relay.drained().then(() => {
|
|
33
|
+
if (!stopping && (code !== 0 || relay.prompts.size)) {
|
|
34
|
+
const message = describeExit({ code, signal, stderr });
|
|
35
|
+
process.stderr.write(`${message}\n`);
|
|
36
|
+
for (const id of relay.abandon()) relay.say({ jsonrpc: '2.0', id, error: endedError(message) });
|
|
37
|
+
}
|
|
38
|
+
end(code ?? (stopping ? 0 : 1));
|
|
39
|
+
});
|
|
40
|
+
});
|
|
20
41
|
kimi.on('error', () => end(1));
|
|
42
|
+
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP']) process.once(signal, stop);
|
package/agent/exit.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// How `kimi acp` ended, as the plugin's log and the turns it leaves waiting say it: its exit code or the signal that killed it,
|
|
2
|
+
// and the last lines it wrote to stderr. Told when the process ends on its own with a prompt unanswered, or with a non-zero exit code or a signal.
|
|
3
|
+
export function describeExit({ code, signal, stderr }) {
|
|
4
|
+
const tail = stderr.trim().split('\n').slice(-5).join('\n').slice(-1000);
|
|
5
|
+
return `Kimi 进程意外退出(${signal ? `被信号 ${signal} 终止` : `退出码 ${code}`})${tail ? `,stderr 末尾:\n${tail}` : ''}`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// The error that ends a prompt as the process ends (docs/agent-protocol.md「原生会话已结束」): the category tells Coda the native
|
|
9
|
+
// session is over, not that the service failed. `input` is not written: nothing the
|
|
10
|
+
// process said proves this prompt was read, and Coda then resumes by checking first, with the whole original input.
|
|
11
|
+
export const endedError = message => ({ code: -32603, message,
|
|
12
|
+
data: { _meta: { coda: { failure: { kind: 'session_ended' } } } } });
|
package/agent/relay.mjs
CHANGED
|
@@ -145,5 +145,7 @@ export class KimiRelay {
|
|
|
145
145
|
}
|
|
146
146
|
// Everything Kimi said has been passed on.
|
|
147
147
|
drained() { return this.queue; }
|
|
148
|
+
// Kimi has ended: the prompts it never answered, no longer waited for here.
|
|
149
|
+
abandon() { const ids = [...this.prompts]; this.prompts.clear(); this.sent.clear(); return ids; }
|
|
148
150
|
close() { this.closed = true; this.unfollow(); clearTimeout(this.timer); this.timer = null; }
|
|
149
151
|
}
|