@coda-console/grok 1.6.1 → 1.7.0
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/delete.mjs +27 -0
- package/agent/entry.mjs +25 -4
- package/package.json +1 -1
- package/scripts/setup.mjs +2 -1
package/README.md
CHANGED
|
@@ -3,3 +3,5 @@
|
|
|
3
3
|
Grok Agent:适配运行机上 Grok CLI 的 ACP 与审批模式;资源页的「Grok」显示账号与额度,可从 Coda 安装和登录。
|
|
4
4
|
|
|
5
5
|
不声明 `systemPrompt`;会话指令由 Coda 后端在新会话第一次非斜杠命令的用户输入前送入单独的文本块,恢复会话不再送,插件不用处理。
|
|
6
|
+
|
|
7
|
+
入口声明 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/delete.mjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// `grok sessions delete <id>`, which is how Grok's history loses a session: ACP has no session/delete for it. It exits 0
|
|
2
|
+
// whether or not the session existed, so what it prints says which. Resolves null when it reports `Deleted session`, else
|
|
3
|
+
// the JSON-RPC error to answer with: -32002 for `No session found`, -32603 for anything else. Checked on 1.0.77: a session still open in an agent is deleted for good, and closing it
|
|
4
|
+
// or ending that agent afterwards writes nothing back.
|
|
5
|
+
import { spawn } from 'node:child_process';
|
|
6
|
+
|
|
7
|
+
const firstLine = text => text.split('\n').map(line => line.trim()).find(Boolean);
|
|
8
|
+
|
|
9
|
+
export function deleteSession(binary, sessionId, timeoutMs = 15000) {
|
|
10
|
+
return new Promise(resolve => {
|
|
11
|
+
// `--` keeps an id that starts with a dash from being read as an option.
|
|
12
|
+
const child = spawn(binary, ['sessions', 'delete', '--', sessionId], { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
13
|
+
let stdout = '', stderr = '', done = false;
|
|
14
|
+
const finish = error => { if (done) return; done = true; clearTimeout(timer); resolve(error); };
|
|
15
|
+
const timer = setTimeout(() => { child.kill('SIGKILL'); finish({ code: -32603, message: '删除 Grok 会话超时' }); }, timeoutMs);
|
|
16
|
+
child.stdout.setEncoding('utf8').on('data', text => { stdout += text; });
|
|
17
|
+
child.stderr.setEncoding('utf8').on('data', text => { stderr += text; });
|
|
18
|
+
child.on('error', error => finish({ code: -32603, message: `无法运行 Grok CLI:${error.message}` }));
|
|
19
|
+
child.on('close', (code, signal) => {
|
|
20
|
+
if (code !== 0) return finish({ code: -32603, message: firstLine(stderr) ?? (code === null ? `grok sessions delete 被信号 ${signal} 结束` : `grok sessions delete 退出码 ${code}`) });
|
|
21
|
+
if (/^Deleted session /m.test(stdout)) return finish(null);
|
|
22
|
+
const missing = stdout.split('\n').find(line => line.startsWith('No session found with id'));
|
|
23
|
+
finish(missing ? { code: -32002, message: missing.trim() }
|
|
24
|
+
: { code: -32603, message: `grok sessions delete 没有确认删除:${firstLine(stdout) ?? '没有输出'}` });
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
package/agent/entry.mjs
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
|
-
// Grok 1.0.52 reports no ACP modes. Translate set_mode to the notification its own UI sends; everything else passes
|
|
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
4
|
// by ending the tool call alone, so that end withdraws the form.
|
|
5
|
+
// Grok's ACP has no session/delete, so the entry declares it and answers it with `grok sessions delete`
|
|
6
|
+
// (delete.mjs; checked on 1.0.77): Coda's probes (session/new, then gone) leave no empty sessions in Grok's history.
|
|
5
7
|
import { spawn } from 'node:child_process';
|
|
6
8
|
import { grokBinary } from '../scripts/ask.mjs';
|
|
9
|
+
import { deleteSession } from './delete.mjs';
|
|
7
10
|
import { ASK, questionForm } from './questions.mjs';
|
|
8
11
|
|
|
9
12
|
const modes = [{ id: 'auto', name: '自动审批', _meta: { coda: { shortName: '自动审批' } } },
|
|
10
13
|
{ id: 'always-approve', name: '全部放行', _meta: { coda: { shortName: '全部放行' } } }];
|
|
11
|
-
const
|
|
14
|
+
const binary = grokBinary() ?? 'grok';
|
|
15
|
+
const grok = spawn(binary, ['agent', '--no-leader', 'stdio'], { stdio: ['pipe', 'pipe', 'inherit'] });
|
|
12
16
|
const send = (stream, message) => stream.write(`${JSON.stringify(message)}\n`);
|
|
13
17
|
const say = message => send(process.stdout, { jsonrpc: '2.0', ...message });
|
|
14
18
|
const pending = new Map();
|
|
19
|
+
// The deletes under way, by session: Coda closes a probe's session once its delete has had its few seconds, and a close
|
|
20
|
+
// reaching Grok while `sessions delete` still runs could write the session back.
|
|
21
|
+
const deleting = new Map();
|
|
15
22
|
// The forms asked of Coda, by their id: Grok's request they answer, its tool call, and how the answer is given back.
|
|
16
23
|
const questions = new Map();
|
|
17
24
|
let sessionId, mode = '', stopping = false, asked = 0;
|
|
@@ -29,6 +36,17 @@ function read(stream, receive) {
|
|
|
29
36
|
stream.setEncoding('utf8').on('data', take).on('end', () => take('\n'));
|
|
30
37
|
}
|
|
31
38
|
read(process.stdin, message => {
|
|
39
|
+
if (message.method === 'session/delete' && 'id' in message) {
|
|
40
|
+
const id = message.params?.sessionId;
|
|
41
|
+
if (typeof id !== 'string' || !id) { say({ id: message.id, error: { code: -32602, message: '缺少 sessionId' } }); return; }
|
|
42
|
+
deleting.set(id, deleteSession(binary, id).then(error => {
|
|
43
|
+
deleting.delete(id); say(error ? { id: message.id, error } : { id: message.id, result: {} });
|
|
44
|
+
}));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (message.method === 'session/close' && deleting.has(message.params?.sessionId)) {
|
|
48
|
+
deleting.get(message.params.sessionId).then(() => send(grok.stdin, message)); return;
|
|
49
|
+
}
|
|
32
50
|
const question = !message.method && questions.get(message.id);
|
|
33
51
|
if (question) {
|
|
34
52
|
questions.delete(message.id);
|
|
@@ -47,7 +65,7 @@ read(process.stdin, message => {
|
|
|
47
65
|
say({ method: 'session/update', params: { sessionId, update: { sessionUpdate: 'current_mode_update', currentModeId: mode } } });
|
|
48
66
|
say({ id: message.id, result: {} }); return;
|
|
49
67
|
}
|
|
50
|
-
if ('id' in message && ['session/new', 'session/load', 'session/prompt'].includes(message.method)) pending.set(message.id, message);
|
|
68
|
+
if ('id' in message && ['initialize', 'session/new', 'session/load', 'session/prompt'].includes(message.method)) pending.set(message.id, message);
|
|
51
69
|
send(grok.stdin, message);
|
|
52
70
|
});
|
|
53
71
|
read(grok.stdout, message => {
|
|
@@ -65,7 +83,10 @@ read(grok.stdout, message => {
|
|
|
65
83
|
const request = !message.method && pending.get(message.id);
|
|
66
84
|
if (request) {
|
|
67
85
|
pending.delete(message.id);
|
|
68
|
-
if (message.result && request.method
|
|
86
|
+
if (message.result && request.method === 'initialize') {
|
|
87
|
+
const capabilities = message.result.agentCapabilities ??= {};
|
|
88
|
+
capabilities.sessionCapabilities = { ...capabilities.sessionCapabilities, delete: {} };
|
|
89
|
+
} else if (message.result && request.method !== 'session/prompt') {
|
|
69
90
|
sessionId = message.result.sessionId ?? request.params.sessionId;
|
|
70
91
|
mode = request.params?._meta?.yoloMode ? 'always-approve' : request.params?._meta?.autoMode ? 'auto' : '';
|
|
71
92
|
message.result.modes = { currentModeId: mode, availableModes: modes };
|
package/package.json
CHANGED
package/scripts/setup.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The plugin's setup (`contributes.setup`), which Coda runs on each machine when the user installs or updates the plugin: it
|
|
2
2
|
// ends well once the machine can run the agent. Without a Grok CLI that runs, or with one older than MINIMUM (the CLI whose
|
|
3
|
-
// `agent --no-leader stdio` and `_x.ai/yolo_mode_changed` the agent entry was checked against)
|
|
3
|
+
// `agent --no-leader stdio` and `_x.ai/yolo_mode_changed` the agent entry was checked against: 1.0.52). `sessions delete`
|
|
4
|
+
// was checked on 1.0.77; on an older CLI that does not have it, the entry answers session/delete with its error. Grok's own installer puts
|
|
4
5
|
// the latest in ~/.grok/bin, where the agent looks first, as the「Grok」type's install does; a newer one is kept. Why the
|
|
5
6
|
// machine is not ready is said in one line on standard error.
|
|
6
7
|
import { spawnSync } from 'node:child_process';
|