@coda-console/grok 1.7.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/README.md +2 -0
- package/agent/entry.mjs +27 -3
- package/agent/exit.mjs +12 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,4 +4,6 @@ Grok Agent:适配运行机上 Grok CLI 的 ACP 与审批模式;资源页的
|
|
|
4
4
|
|
|
5
5
|
不声明 `systemPrompt`;会话指令由 Coda 后端在新会话第一次非斜杠命令的用户输入前送入单独的文本块,恢复会话不再送,插件不用处理。
|
|
6
6
|
|
|
7
|
+
压缩上下文(自动,或用户的 `/compact`):入口把 Grok 的 `_x.ai/session_notification` 里的 `auto_compact_started`、`auto_compact_completed`、`auto_compact_failed`、`auto_compact_cancelled`(只取主会话的)原样转发后,另按 ACP `compaction_update` 报给 Coda:开始 `in_progress`,完成 `completed`,同一次压缩用同一个 `compactionId`;`/compact` 不报开始,只报完成。失败、取消两条在 Grok 1.0.77 上只在二进制里见到名字,没有实测,按 `failed` 报。
|
|
8
|
+
|
|
7
9
|
入口声明 ACP 的 `session/delete`:Grok 原生没有,入口自己拦下,改跑官方命令 `grok sessions delete <id>`(15 秒超时)。命令对不存在的会话也退出 0,所以以它的输出为准:`Deleted session` 回 `{}`,`No session found` 回 -32002,其他回 -32603 并带原因。会话还开在 Grok 里时可以直接删,之后 `session/close` 和结束进程都不会把它写回来(1.0.77 实测)。这样 Coda 探测模型和模式留下的空会话能被清掉;`sessions delete` 核对于 1.0.77,更旧的 CLI 没有它时回错误,不抬高最低版本(审批模式通知只核对过 1.0.52)。
|
package/agent/entry.mjs
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
// Grok (checked on 1.0.52) reports no ACP modes. Translate set_mode to the notification its own UI sends; everything else passes
|
|
2
2
|
// through. No selected mode (the empty currentModeId) leaves the machine's configuration in charge, as before.
|
|
3
3
|
// Grok's questions are asked of Coda as ACP forms (questions.mjs). Grok gives one up (its timeout, a cancelled turn)
|
|
4
|
-
// by ending the tool call alone, so that end withdraws the form.
|
|
4
|
+
// by ending the tool call alone, so that end withdraws the form. Its context compactions, auto or a /compact of the user's, are
|
|
5
|
+
// told as `_x.ai/session_notification` updates, which are also told to Coda as `compaction_update` (docs/agent-protocol.md).
|
|
5
6
|
// Grok's ACP has no session/delete, so the entry declares it and answers it with `grok sessions delete`
|
|
6
7
|
// (delete.mjs; checked on 1.0.77): Coda's probes (session/new, then gone) leave no empty sessions in Grok's history.
|
|
7
8
|
import { spawn } from 'node:child_process';
|
|
9
|
+
import { randomUUID } from 'node:crypto';
|
|
8
10
|
import { grokBinary } from '../scripts/ask.mjs';
|
|
9
11
|
import { deleteSession } from './delete.mjs';
|
|
12
|
+
import { describeExit, endedError } from './exit.mjs';
|
|
10
13
|
import { ASK, questionForm } from './questions.mjs';
|
|
11
14
|
|
|
12
15
|
const modes = [{ id: 'auto', name: '自动审批', _meta: { coda: { shortName: '自动审批' } } },
|
|
13
16
|
{ id: 'always-approve', name: '全部放行', _meta: { coda: { shortName: '全部放行' } } }];
|
|
14
17
|
const binary = grokBinary() ?? 'grok';
|
|
15
|
-
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); });
|
|
16
21
|
const send = (stream, message) => stream.write(`${JSON.stringify(message)}\n`);
|
|
17
22
|
const say = message => send(process.stdout, { jsonrpc: '2.0', ...message });
|
|
18
23
|
const pending = new Map();
|
|
24
|
+
// What Grok says of a compaction of the main session, as Coda takes it. A /compact says its end only.
|
|
25
|
+
const COMPACTION = { auto_compact_started: 'in_progress', auto_compact_completed: 'completed', auto_compact_failed: 'failed', auto_compact_cancelled: 'failed' };
|
|
26
|
+
let compaction = null;
|
|
19
27
|
// The deletes under way, by session: Coda closes a probe's session once its delete has had its few seconds, and a close
|
|
20
28
|
// reaching Grok while `sessions delete` still runs could write the session back.
|
|
21
29
|
const deleting = new Map();
|
|
@@ -93,6 +101,12 @@ read(grok.stdout, message => {
|
|
|
93
101
|
}
|
|
94
102
|
}
|
|
95
103
|
say(message);
|
|
104
|
+
const status = message.method === '_x.ai/session_notification' && message.params?.sessionId === sessionId ? COMPACTION[message.params.update?.sessionUpdate] : null;
|
|
105
|
+
if (status) {
|
|
106
|
+
compaction ??= randomUUID();
|
|
107
|
+
say({ method: 'session/update', params: { sessionId, update: { sessionUpdate: 'compaction_update', compactionId: compaction, status, error: null } } });
|
|
108
|
+
if (status !== 'in_progress') compaction = null;
|
|
109
|
+
}
|
|
96
110
|
});
|
|
97
111
|
const stop = () => {
|
|
98
112
|
if (stopping) return;
|
|
@@ -101,6 +115,16 @@ const stop = () => {
|
|
|
101
115
|
};
|
|
102
116
|
grok.stdin.on('error', () => {});
|
|
103
117
|
grok.on('error', () => process.exit(1));
|
|
104
|
-
|
|
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
|
+
});
|
|
105
129
|
process.stdin.on('end', stop);
|
|
106
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' } } } } });
|