@coda-console/grok 1.3.2 → 1.4.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/agent/entry.mjs CHANGED
@@ -1,17 +1,22 @@
1
1
  // Grok 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. Coda's session
3
- // instructions go in front of a new session's first prompt (instructions.mjs).
3
+ // instructions go in front of a new session's first prompt (instructions.mjs). Grok's questions are asked of Coda as ACP forms
4
+ // (questions.mjs); Grok gives one up (its timeout, a cancelled turn) by ending the tool call alone, so that end withdraws the form.
4
5
  import { spawn } from 'node:child_process';
5
6
  import { grokBinary } from '../scripts/ask.mjs';
6
7
  import { SessionInstructions } from './instructions.mjs';
8
+ import { ASK, questionForm } from './questions.mjs';
7
9
 
8
- const modes = [{ id: 'auto', name: '自动审批' }, { id: 'always-approve', name: '全部放行' }];
10
+ const modes = [{ id: 'auto', name: '自动审批', _meta: { coda: { shortName: '自动审批' } } },
11
+ { id: 'always-approve', name: '全部放行', _meta: { coda: { shortName: '全部放行' } } }];
9
12
  const grok = spawn(grokBinary() ?? 'grok', ['agent', '--no-leader', 'stdio'], { stdio: ['pipe', 'pipe', 'inherit'] });
10
13
  const send = (stream, message) => stream.write(`${JSON.stringify(message)}\n`);
11
14
  const say = message => send(process.stdout, { jsonrpc: '2.0', ...message });
12
15
  const pending = new Map();
13
16
  const instructions = new SessionInstructions();
14
- let sessionId, mode = '', stopping = false;
17
+ // The forms asked of Coda, by their id: Grok's request they answer, its tool call, and how the answer is given back.
18
+ const questions = new Map();
19
+ let sessionId, mode = '', stopping = false, asked = 0;
15
20
 
16
21
  // Only LF separates JSON messages; readline would also split literal Unicode line separators inside JSON strings.
17
22
  function read(stream, receive) {
@@ -26,6 +31,11 @@ function read(stream, receive) {
26
31
  stream.setEncoding('utf8').on('data', take).on('end', () => take('\n'));
27
32
  }
28
33
  read(process.stdin, said => {
34
+ const question = !said.method && questions.get(said.id);
35
+ if (question) {
36
+ questions.delete(said.id);
37
+ send(grok.stdin, { jsonrpc: '2.0', id: question.id, result: question.answer(said.result) }); return;
38
+ }
29
39
  const { agent: message, coda } = instructions.fromCoda(said);
30
40
  if (coda) { say(coda); return; }
31
41
  if (message.method === 'session/set_mode' && 'id' in message) {
@@ -45,6 +55,17 @@ read(process.stdin, said => {
45
55
  send(grok.stdin, message);
46
56
  });
47
57
  read(grok.stdout, said => {
58
+ if (said.method === ASK && 'id' in said) {
59
+ const id = `grok-ask-${++asked}`, { params, answer } = questionForm(said.params ?? {});
60
+ questions.set(id, { id: said.id, toolCallId: params.toolCallId, answer });
61
+ say({ id, method: 'elicitation/create', params }); return;
62
+ }
63
+ const update = said.method === 'session/update' ? said.params?.update : null;
64
+ if (update?.sessionUpdate === 'tool_call_update' && ['completed', 'failed'].includes(update.status)) {
65
+ for (const [id, question] of questions) if (question.toolCallId === update.toolCallId) {
66
+ questions.delete(id); say({ method: '$/cancel_request', params: { requestId: id } });
67
+ }
68
+ }
48
69
  const message = instructions.fromAgent(said), request = !message.method && pending.get(message.id);
49
70
  if (request) {
50
71
  pending.delete(message.id);
@@ -0,0 +1,33 @@
1
+ // What Grok asks the user (its `ask_user_question` tool), which it asks of the client with a request of its own,
2
+ // `_x.ai/ask_user_question` { sessionId, toolCallId, questions: [{ question, options: [{ label, description }], multiSelect }] }:
3
+ // asked of Coda as an ACP form in plain JSON Schema, and answered as Grok takes it, `{ outcome: 'accepted', answers }` with
4
+ // each question's text naming its answer (a list for several), or `{ outcome: 'cancelled' }`.
5
+ export const ASK = '_x.ai/ask_user_question';
6
+
7
+ // The choice that says the answer is the one written beside it: ACP forms carry no rule that one of two fields be filled,
8
+ // so the question stays required and this is one of its choices.
9
+ export const OTHER = 'coda:other';
10
+
11
+ export function questionForm({ sessionId, toolCallId, questions }) {
12
+ questions = Array.isArray(questions) ? questions : [];
13
+ const properties = {};
14
+ questions.forEach((q, i) => {
15
+ const choices = [...(q.options ?? []).map(o => ({ const: o.label, title: o.label + (o.description ? ` — ${o.description}` : '') })),
16
+ { const: OTHER, title: '其他(写在下面)' }];
17
+ properties[`q${i}`] = { title: q.question, ...(q.multiSelect
18
+ ? { type: 'array', items: { anyOf: choices }, minItems: 1 } : { type: 'string', oneOf: choices }) };
19
+ properties[`q${i}_other`] = { type: 'string', title: '其他' };
20
+ });
21
+ const params = { sessionId, ...(toolCallId ? { toolCallId } : {}), mode: 'form', message: 'Grok 需要你的回答',
22
+ requestedSchema: { type: 'object', properties, required: questions.map((_, i) => `q${i}`) } };
23
+ const answer = response => {
24
+ if (response?.action !== 'accept') return { outcome: 'cancelled' };
25
+ const content = response.content ?? {};
26
+ return { outcome: 'accepted', answers: Object.fromEntries(questions.map((q, i) => {
27
+ const other = String(content[`q${i}_other`] ?? '').trim() || '其他', pick = value => value === OTHER ? other : value;
28
+ const value = content[`q${i}`];
29
+ return [q.question, Array.isArray(value) ? value.map(pick) : pick(value)];
30
+ })) };
31
+ };
32
+ return { params, answer };
33
+ }
package/package.json CHANGED
@@ -3,8 +3,10 @@
3
3
  "id": "grok",
4
4
  "displayName": "Grok",
5
5
  "description": "Grok Agent:适配运行机上 Grok CLI 的 ACP 与审批模式;资源页的「Grok」面板显示账号与额度,可从 Coda 安装和登录。",
6
- "version": "1.3.2",
7
- "categories": ["Agent"],
6
+ "version": "1.4.0",
7
+ "categories": [
8
+ "Agent"
9
+ ],
8
10
  "contributes": {
9
11
  "agents": [
10
12
  {