@coda-console/grok 1.8.0 → 1.8.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 +15 -2
- package/agent/exit.mjs +12 -0
- package/package.json +1 -1
package/agent/entry.mjs
CHANGED
|
@@ -9,12 +9,15 @@ import { spawn } from 'node:child_process';
|
|
|
9
9
|
import { randomUUID } from 'node:crypto';
|
|
10
10
|
import { grokBinary } from '../scripts/ask.mjs';
|
|
11
11
|
import { deleteSession } from './delete.mjs';
|
|
12
|
+
import { describeExit, endedError } from './exit.mjs';
|
|
12
13
|
import { ASK, questionForm } from './questions.mjs';
|
|
13
14
|
|
|
14
15
|
const modes = [{ id: 'auto', name: '自动审批', _meta: { coda: { shortName: '自动审批' } } },
|
|
15
16
|
{ id: 'always-approve', name: '全部放行', _meta: { coda: { shortName: '全部放行' } } }];
|
|
16
17
|
const binary = grokBinary() ?? 'grok';
|
|
17
|
-
const grok = spawn(binary, ['agent', '--no-leader', 'stdio'], { stdio: ['pipe', 'pipe', '
|
|
18
|
+
const grok = spawn(binary, ['agent', '--no-leader', 'stdio'], { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
19
|
+
let stderr = '';
|
|
20
|
+
grok.stderr.on('data', chunk => { process.stderr.write(chunk); stderr = (stderr + chunk).slice(-2048); });
|
|
18
21
|
const send = (stream, message) => stream.write(`${JSON.stringify(message)}\n`);
|
|
19
22
|
const say = message => send(process.stdout, { jsonrpc: '2.0', ...message });
|
|
20
23
|
const pending = new Map();
|
|
@@ -112,6 +115,16 @@ const stop = () => {
|
|
|
112
115
|
};
|
|
113
116
|
grok.stdin.on('error', () => {});
|
|
114
117
|
grok.on('error', () => process.exit(1));
|
|
115
|
-
|
|
118
|
+
// Grok dying on its own: the log and the prompts still waiting say how (exit.mjs).
|
|
119
|
+
grok.on('close', (code, signal) => {
|
|
120
|
+
const waiting = [...pending].filter(([, request]) => request.method === 'session/prompt');
|
|
121
|
+
if (!stopping && (code !== 0 || waiting.length)) {
|
|
122
|
+
const message = describeExit({ code, signal, stderr });
|
|
123
|
+
process.stderr.write(`${message}\n`);
|
|
124
|
+
for (const [id] of waiting) say({ id, error: endedError(message) });
|
|
125
|
+
}
|
|
126
|
+
// What was said reaches Coda before this process ends.
|
|
127
|
+
process.stdout.write('', () => process.exit(stopping ? 0 : code ?? 1));
|
|
128
|
+
});
|
|
116
129
|
process.stdin.on('end', stop);
|
|
117
130
|
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP']) process.once(signal, stop);
|
package/agent/exit.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// How `grok agent` 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 `Grok 进程意外退出(${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' } } } } });
|