@coda-console/grok 1.8.1 → 1.8.2

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
@@ -3,6 +3,7 @@
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. Its context compactions, auto or a /compact of the user's, are
5
5
  // told as `_x.ai/session_notification` updates, which are also told to Coda as `compaction_update` (docs/agent-protocol.md).
6
+ // The prompt's usage Grok tells in `_meta.usage` is also told as the response's `usage` (usage.mjs).
6
7
  // Grok's ACP has no session/delete, so the entry declares it and answers it with `grok sessions delete`
7
8
  // (delete.mjs; checked on 1.0.77): Coda's probes (session/new, then gone) leave no empty sessions in Grok's history.
8
9
  import { spawn } from 'node:child_process';
@@ -11,6 +12,7 @@ import { grokBinary } from '../scripts/ask.mjs';
11
12
  import { deleteSession } from './delete.mjs';
12
13
  import { describeExit, endedError } from './exit.mjs';
13
14
  import { ASK, questionForm } from './questions.mjs';
15
+ import { promptUsage } from './usage.mjs';
14
16
 
15
17
  const modes = [{ id: 'auto', name: '自动审批', _meta: { coda: { shortName: '自动审批' } } },
16
18
  { id: 'always-approve', name: '全部放行', _meta: { coda: { shortName: '全部放行' } } }];
@@ -94,6 +96,9 @@ read(grok.stdout, message => {
94
96
  if (message.result && request.method === 'initialize') {
95
97
  const capabilities = message.result.agentCapabilities ??= {};
96
98
  capabilities.sessionCapabilities = { ...capabilities.sessionCapabilities, delete: {} };
99
+ } else if (message.result && request.method === 'session/prompt') {
100
+ const usage = promptUsage(message.result);
101
+ if (usage) message.result.usage = usage;
97
102
  } else if (message.result && request.method !== 'session/prompt') {
98
103
  sessionId = message.result.sessionId ?? request.params.sessionId;
99
104
  mode = request.params?._meta?.yoloMode ? 'always-approve' : request.params?._meta?.autoMode ? 'auto' : '';
@@ -0,0 +1,24 @@
1
+ // What a prompt used, as `PromptResponse.usage` says it (Coda's per-turn token collection): this turn's increment, `inputTokens` the
2
+ // uncached input, the cache reads and writes beside it. Grok (ACP, checked on 1.0.77) says it in the response's `_meta.usage` instead,
3
+ // per turn, with its `inputTokens` counting the cached tokens too: those are taken out of it. `_meta` stays as Grok sent it (Coda
4
+ // does not read it; other ACP clients may), and a response that already has a `usage` of its own keeps it.
5
+ const count = value => Number.isSafeInteger(value) && value >= 0 ? value : null;
6
+
7
+ // The `usage` to add to a successful prompt response `result`, or null where there is none to add.
8
+ export function promptUsage(result) {
9
+ if (!result || typeof result !== 'object' || Array.isArray(result) || (result.usage !== undefined && result.usage !== null)) return null;
10
+ const raw = result._meta?.usage;
11
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
12
+ const input = count(raw.inputTokens), output = count(raw.outputTokens), read = count(raw.cachedReadTokens);
13
+ const write = count(raw.cachedWriteTokens) ?? count(raw.cacheCreationTokens);
14
+ if (input === null && output === null && read === null && write === null) return null;
15
+ const uncached = input === null ? null : Math.max(0, input - (read ?? 0) - (write ?? 0));
16
+ return {
17
+ // The sum ACP's own Usage asks for, where the input and output are both there to make it.
18
+ ...(uncached !== null && output !== null ? { totalTokens: uncached + output + (read ?? 0) + (write ?? 0) } : {}),
19
+ ...(uncached === null ? {} : { inputTokens: uncached }),
20
+ ...(output === null ? {} : { outputTokens: output }),
21
+ ...(read === null ? {} : { cachedReadTokens: read }),
22
+ ...(write === null ? {} : { cachedWriteTokens: write }),
23
+ };
24
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "grok",
4
4
  "displayName": "Grok",
5
5
  "description": "用 Grok CLI 跑会话",
6
- "version": "1.8.1",
6
+ "version": "1.8.2",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],