@coda-console/grok 1.6.0 → 1.6.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 +13 -17
- package/package.json +1 -1
- package/agent/instructions.mjs +0 -68
package/README.md
CHANGED
package/agent/entry.mjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// Grok 1.0.52 reports no ACP modes. Translate set_mode to the notification its own UI sends; everything else passes
|
|
2
|
-
// through. No selected mode (the empty currentModeId) leaves the machine's configuration in charge, as before.
|
|
3
|
-
//
|
|
4
|
-
//
|
|
2
|
+
// through. No selected mode (the empty currentModeId) leaves the machine's configuration in charge, as before.
|
|
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.
|
|
5
5
|
import { spawn } from 'node:child_process';
|
|
6
6
|
import { grokBinary } from '../scripts/ask.mjs';
|
|
7
|
-
import { SessionInstructions } from './instructions.mjs';
|
|
8
7
|
import { ASK, questionForm } from './questions.mjs';
|
|
9
8
|
|
|
10
9
|
const modes = [{ id: 'auto', name: '自动审批', _meta: { coda: { shortName: '自动审批' } } },
|
|
@@ -13,7 +12,6 @@ const grok = spawn(grokBinary() ?? 'grok', ['agent', '--no-leader', 'stdio'], {
|
|
|
13
12
|
const send = (stream, message) => stream.write(`${JSON.stringify(message)}\n`);
|
|
14
13
|
const say = message => send(process.stdout, { jsonrpc: '2.0', ...message });
|
|
15
14
|
const pending = new Map();
|
|
16
|
-
const instructions = new SessionInstructions();
|
|
17
15
|
// The forms asked of Coda, by their id: Grok's request they answer, its tool call, and how the answer is given back.
|
|
18
16
|
const questions = new Map();
|
|
19
17
|
let sessionId, mode = '', stopping = false, asked = 0;
|
|
@@ -30,14 +28,12 @@ function read(stream, receive) {
|
|
|
30
28
|
};
|
|
31
29
|
stream.setEncoding('utf8').on('data', take).on('end', () => take('\n'));
|
|
32
30
|
}
|
|
33
|
-
read(process.stdin,
|
|
34
|
-
const question = !
|
|
31
|
+
read(process.stdin, message => {
|
|
32
|
+
const question = !message.method && questions.get(message.id);
|
|
35
33
|
if (question) {
|
|
36
|
-
questions.delete(
|
|
37
|
-
send(grok.stdin, { jsonrpc: '2.0', id: question.id, result: question.answer(
|
|
34
|
+
questions.delete(message.id);
|
|
35
|
+
send(grok.stdin, { jsonrpc: '2.0', id: question.id, result: question.answer(message.result) }); return;
|
|
38
36
|
}
|
|
39
|
-
const { agent: message, coda } = instructions.fromCoda(said);
|
|
40
|
-
if (coda) { say(coda); return; }
|
|
41
37
|
if (message.method === 'session/set_mode' && 'id' in message) {
|
|
42
38
|
const chosen = message.params?.modeId;
|
|
43
39
|
const error = !sessionId || message.params?.sessionId !== sessionId ? 'Grok 会话不存在'
|
|
@@ -54,19 +50,19 @@ read(process.stdin, said => {
|
|
|
54
50
|
if ('id' in message && ['session/new', 'session/load', 'session/prompt'].includes(message.method)) pending.set(message.id, message);
|
|
55
51
|
send(grok.stdin, message);
|
|
56
52
|
});
|
|
57
|
-
read(grok.stdout,
|
|
58
|
-
if (
|
|
59
|
-
const id = `grok-ask-${++asked}`, { params, answer } = questionForm(
|
|
60
|
-
questions.set(id, { id:
|
|
53
|
+
read(grok.stdout, message => {
|
|
54
|
+
if (message.method === ASK && 'id' in message) {
|
|
55
|
+
const id = `grok-ask-${++asked}`, { params, answer } = questionForm(message.params ?? {});
|
|
56
|
+
questions.set(id, { id: message.id, toolCallId: params.toolCallId, answer });
|
|
61
57
|
say({ id, method: 'elicitation/create', params }); return;
|
|
62
58
|
}
|
|
63
|
-
const update =
|
|
59
|
+
const update = message.method === 'session/update' ? message.params?.update : null;
|
|
64
60
|
if (update?.sessionUpdate === 'tool_call_update' && ['completed', 'failed'].includes(update.status)) {
|
|
65
61
|
for (const [id, question] of questions) if (question.toolCallId === update.toolCallId) {
|
|
66
62
|
questions.delete(id); say({ method: '$/cancel_request', params: { requestId: id } });
|
|
67
63
|
}
|
|
68
64
|
}
|
|
69
|
-
const
|
|
65
|
+
const request = !message.method && pending.get(message.id);
|
|
70
66
|
if (request) {
|
|
71
67
|
pending.delete(message.id);
|
|
72
68
|
if (message.result && request.method !== 'session/prompt') {
|
package/package.json
CHANGED
package/agent/instructions.mjs
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
// Coda's session instructions (docs/agent-protocol.md「会话指令」) for an agent whose own ACP does not take `systemPrompt`: the
|
|
2
|
-
// entry declares that it does, keeps the parameter from the agent, and puts the text in front of the first prompt of a
|
|
3
|
-
// session the agent opened new, as a part of its own; a session it loaded has had them. A slash command is the agent's to
|
|
4
|
-
// read as one: the instructions wait for the next prompt. Kept the same in every plugin that relays a CLI's ACP.
|
|
5
|
-
const record = value => value && typeof value === 'object' && !Array.isArray(value);
|
|
6
|
-
|
|
7
|
-
// ACP's `systemPrompt`: its text, '' when there is none; throws on anything but text appended to the agent's own.
|
|
8
|
-
export function sessionInstructions(systemPrompt) {
|
|
9
|
-
if (systemPrompt === undefined || systemPrompt === null) return '';
|
|
10
|
-
if (systemPrompt.mode !== 'append') throw new Error('会话指令只支持 append 模式');
|
|
11
|
-
const { content } = systemPrompt;
|
|
12
|
-
if (!Array.isArray(content) || content.some(block => block?.type !== 'text' || typeof block.text !== 'string')) throw new Error('会话指令只接受文本块');
|
|
13
|
-
return content.map(block => block.text).join('\n\n');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// The part in front of the user's input: delimited, and said to be the client's, not the user's.
|
|
17
|
-
const OPENING = '<coda-session-instructions>';
|
|
18
|
-
export const instructionsBlock = text => ({ type: 'text', text: `${OPENING}\n`
|
|
19
|
-
+ 'Instructions from Coda, the client this session runs in, for the whole session. They are not part of the user\'s message.\n\n'
|
|
20
|
-
+ `${text}\n</coda-session-instructions>\n` });
|
|
21
|
-
|
|
22
|
-
export class SessionInstructions {
|
|
23
|
-
// Coda's requests whose answers are read here, by id; the instructions of a session yet to have its first prompt.
|
|
24
|
-
constructor() { this.asked = new Map(); this.waiting = new Map(); }
|
|
25
|
-
// What Coda says: `agent`, what the agent is told, or `coda`, Coda's answer given here instead.
|
|
26
|
-
fromCoda(message) {
|
|
27
|
-
const { id, method, params } = message;
|
|
28
|
-
if (!record(params)) return { agent: message };
|
|
29
|
-
if (method === 'initialize' && id !== undefined) this.asked.set(id, { initialize: true });
|
|
30
|
-
if (method === 'session/new' || method === 'session/load') {
|
|
31
|
-
let text;
|
|
32
|
-
try { text = sessionInstructions(params.systemPrompt); }
|
|
33
|
-
catch (error) { return { coda: { jsonrpc: '2.0', id, error: { code: -32602, message: error.message } } }; }
|
|
34
|
-
const { systemPrompt, ...rest } = params;
|
|
35
|
-
if (method === 'session/new' && text && id !== undefined) this.asked.set(id, { text });
|
|
36
|
-
return { agent: { ...message, params: rest } };
|
|
37
|
-
}
|
|
38
|
-
if (method === 'session/prompt' && Array.isArray(params.prompt) && this.waiting.has(params.sessionId)) {
|
|
39
|
-
const first = params.prompt.find(block => block?.type === 'text');
|
|
40
|
-
if (typeof first?.text === 'string' && first.text.trimStart().startsWith('/')) return { agent: message };
|
|
41
|
-
const text = this.waiting.get(params.sessionId);
|
|
42
|
-
this.waiting.delete(params.sessionId);
|
|
43
|
-
return { agent: { ...message, params: { ...params, prompt: [instructionsBlock(text), ...params.prompt] } } };
|
|
44
|
-
}
|
|
45
|
-
return { agent: message };
|
|
46
|
-
}
|
|
47
|
-
// What the agent says, as said to Coda. A title the agent took from the first prompt's text begins with the instructions,
|
|
48
|
-
// not with what the user asked: it is left out, and the session keeps the title Coda gives it.
|
|
49
|
-
fromAgent(message) {
|
|
50
|
-
const update = message?.method === 'session/update' && record(message.params) ? message.params.update : null;
|
|
51
|
-
if (record(update) && update.sessionUpdate === 'session_info_update' && typeof update.title === 'string' && update.title.trimStart().startsWith(OPENING)) {
|
|
52
|
-
const { title, ...rest } = update;
|
|
53
|
-
return { ...message, params: { ...message.params, update: rest } };
|
|
54
|
-
}
|
|
55
|
-
if (!record(message) || !('id' in message) || 'method' in message) return message;
|
|
56
|
-
const asked = this.asked.get(message.id);
|
|
57
|
-
if (!asked) return message;
|
|
58
|
-
this.asked.delete(message.id);
|
|
59
|
-
if (!record(message.result)) return message;
|
|
60
|
-
if (asked.text) {
|
|
61
|
-
if (typeof message.result.sessionId === 'string') this.waiting.set(message.result.sessionId, asked.text);
|
|
62
|
-
return message;
|
|
63
|
-
}
|
|
64
|
-
const capabilities = record(message.result.agentCapabilities) ? message.result.agentCapabilities : {};
|
|
65
|
-
const sessions = record(capabilities.sessionCapabilities) ? capabilities.sessionCapabilities : {};
|
|
66
|
-
return { ...message, result: { ...message.result, agentCapabilities: { ...capabilities, sessionCapabilities: { ...sessions, systemPrompt: {} } } } };
|
|
67
|
-
}
|
|
68
|
-
}
|