context-mode 1.0.165 → 1.0.167

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.
Files changed (56) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/.codex-plugin/plugin.json +1 -1
  4. package/.openclaw-plugin/openclaw.plugin.json +1 -1
  5. package/.openclaw-plugin/package.json +1 -1
  6. package/README.md +6 -4
  7. package/build/adapters/codex/usage.d.ts +107 -0
  8. package/build/adapters/codex/usage.js +227 -0
  9. package/build/adapters/gemini-cli/hooks.d.ts +7 -1
  10. package/build/adapters/gemini-cli/hooks.js +9 -1
  11. package/build/adapters/gemini-cli/index.js +11 -0
  12. package/build/adapters/kimi/paths.d.ts +20 -0
  13. package/build/adapters/kimi/paths.js +41 -1
  14. package/build/adapters/kimi/usage.d.ts +82 -0
  15. package/build/adapters/kimi/usage.js +217 -0
  16. package/build/adapters/omp/plugin.d.ts +6 -0
  17. package/build/adapters/omp/plugin.js +87 -2
  18. package/build/adapters/omp/usage.d.ts +49 -0
  19. package/build/adapters/omp/usage.js +110 -0
  20. package/build/adapters/openclaw/plugin.d.ts +10 -0
  21. package/build/adapters/openclaw/plugin.js +57 -0
  22. package/build/adapters/openclaw/usage.d.ts +34 -0
  23. package/build/adapters/openclaw/usage.js +52 -0
  24. package/build/adapters/opencode/plugin.d.ts +17 -0
  25. package/build/adapters/opencode/plugin.js +40 -1
  26. package/build/adapters/pi/extension.js +61 -10
  27. package/build/adapters/pi/mcp-bridge.d.ts +78 -1
  28. package/build/adapters/pi/mcp-bridge.js +105 -17
  29. package/build/adapters/qwen-code/index.js +23 -1
  30. package/build/adapters/qwen-code/usage.d.ts +90 -0
  31. package/build/adapters/qwen-code/usage.js +222 -0
  32. package/build/lifecycle.d.ts +10 -0
  33. package/build/lifecycle.js +16 -1
  34. package/build/session/db.d.ts +11 -0
  35. package/build/session/db.js +33 -0
  36. package/build/session/extract.d.ts +208 -0
  37. package/build/session/extract.js +670 -43
  38. package/build/session/model-prices.json +429 -0
  39. package/build/session/pricing.d.ts +64 -0
  40. package/build/session/pricing.js +151 -0
  41. package/cli.bundle.mjs +84 -84
  42. package/configs/antigravity-cli/plugin.json +1 -1
  43. package/configs/copilot-cli/.github/plugin/plugin.json +1 -1
  44. package/configs/gemini-cli/settings.json +11 -0
  45. package/hooks/codex/stop.mjs +91 -4
  46. package/hooks/gemini-cli/aftermodel.mjs +70 -0
  47. package/hooks/kimi/stop.mjs +74 -3
  48. package/hooks/qwen-code/platform.mjs +1 -0
  49. package/hooks/qwen-code/stop.mjs +168 -0
  50. package/hooks/session-db.bundle.mjs +7 -7
  51. package/hooks/session-extract.bundle.mjs +3 -2
  52. package/hooks/session-loaders.mjs +9 -1
  53. package/hooks/stop.mjs +35 -2
  54. package/openclaw.plugin.json +1 -1
  55. package/package.json +1 -1
  56. package/server.bundle.mjs +107 -107
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Kimi Code (kimi-code) per-turn token usage capture.
3
+ *
4
+ * Ground truth: context-mode-platform/docs/prds/2026-06-paid-observability/
5
+ * adapter-matrix/kimi.md (+ cited refs/platforms/kimi-code/...).
6
+ *
7
+ * Kimi Code emits REAL per-turn token usage + model, but ONLY on the
8
+ * `wire.jsonl` records stream — NOT through any hook stdin payload. Each usage
9
+ * line is an AgentRecord of `type: "usage.record"` carrying a normalized
10
+ * four-field Moonshot/OpenAI-compatible `TokenUsage` plus the model id:
11
+ *
12
+ * refs/platforms/kimi-code/packages/agent-core/src/agent/usage/index.ts:27-32
13
+ * — this.agent.records.logRecord({ type: 'usage.record', model, usage, usageScope })
14
+ * refs/platforms/kimi-code/packages/agent-core/src/agent/records/types.ts:59-63
15
+ * — record shape { model: string; usage: TokenUsage; usageScope?: UsageRecordScope }
16
+ * refs/platforms/kimi-code/packages/agent-core/src/agent/index.ts:142
17
+ * — new FileSystemAgentRecordPersistence(join(options.homedir, 'wire.jsonl'), ...)
18
+ * => the persisted file is <sessionDir>/wire.jsonl.
19
+ *
20
+ * Normalized TokenUsage (kosong/src/usage.ts:7-13; parsed by
21
+ * kosong/src/providers/openai-common.ts:213-241):
22
+ * { inputOther, output, inputCacheRead, inputCacheCreation }
23
+ *
24
+ * Mapping → buildAgentUsageEvent input shape:
25
+ * inputOther → input_tokens (prompt - cached)
26
+ * output → output_tokens
27
+ * inputCacheRead → cache_read_tokens
28
+ * inputCacheCreation → cache_creation_tokens
29
+ * record.model → model_id
30
+ *
31
+ * INCREMENTAL: usage.record lines are per-step deltas (summed via addUsage;
32
+ * usage/index.ts:34,37). The cumulative total exists only in-memory, never on
33
+ * disk — so cost capture sums the NEW delta lines per model since a cursor.
34
+ *
35
+ * Native cost: kimi-code's TokenUsage carries NO USD cost field (verified
36
+ * against the matrix doc field list — only token counts). So native_cost_usd
37
+ * is left null and buildAgentUsageEvent falls back to the pricing catalog.
38
+ *
39
+ * Pure, null-safe, algorithmic — NO regex.
40
+ */
41
+ import { buildAgentUsageEvent } from "../../session/extract.js";
42
+ /** Non-negative finite number, else 0. */
43
+ function num(v) {
44
+ return typeof v === "number" && Number.isFinite(v) && v > 0 ? v : 0;
45
+ }
46
+ /**
47
+ * Parse ONE kimi-code `usage.record` line object into the buildAgentUsageEvent
48
+ * input shape, or null when it is not a usage record / carries no usage /
49
+ * every token bucket is zero.
50
+ *
51
+ * Accepts the parsed AgentRecord object (NOT the raw JSONL string). Tolerant of
52
+ * the record being passed either as the full stamped record `{ type, model,
53
+ * usage, ... }` or a bare `{ model, usage }`.
54
+ */
55
+ export function parseKimiUsage(record) {
56
+ if (!record || typeof record !== "object")
57
+ return null;
58
+ const rec = record;
59
+ // When a `type` discriminator is present it MUST be the usage record kind.
60
+ // Absent type → tolerate (caller may have already narrowed), but a wrong
61
+ // explicit type is rejected so non-usage records never produce cost events.
62
+ if (typeof rec.type === "string" && rec.type !== "usage.record")
63
+ return null;
64
+ const usageRaw = rec.usage;
65
+ if (!usageRaw || typeof usageRaw !== "object")
66
+ return null;
67
+ const usage = usageRaw;
68
+ const input_tokens = num(usage.inputOther);
69
+ const output_tokens = num(usage.output);
70
+ const cache_read_tokens = num(usage.inputCacheRead);
71
+ const cache_creation_tokens = num(usage.inputCacheCreation);
72
+ // Zero-everything record → null (mirrors buildAgentUsageEvent's zero->null
73
+ // contract; keeps the DB free of no-op cost events).
74
+ if (input_tokens <= 0 &&
75
+ output_tokens <= 0 &&
76
+ cache_read_tokens <= 0 &&
77
+ cache_creation_tokens <= 0) {
78
+ return null;
79
+ }
80
+ const model_id = typeof rec.model === "string" ? rec.model : "";
81
+ return {
82
+ model_id,
83
+ input_tokens,
84
+ output_tokens,
85
+ cache_creation_tokens,
86
+ cache_read_tokens,
87
+ // kimi-code TokenUsage carries no native USD field — defer to the catalog.
88
+ native_cost_usd: null,
89
+ };
90
+ }
91
+ /**
92
+ * Cursor-aware wire.jsonl reader for the Stop / SessionEnd hook.
93
+ *
94
+ * `wire.jsonl` is an append-only records stream that grows every turn; the
95
+ * forward loop forwards ALL passed events unconditionally, so re-summing the
96
+ * whole file each hook fire would double-count every prior turn. This sums only
97
+ * the `usage.record` lines NEW since the last fire, keyed by a per-session
98
+ * high-water cursor (a 1-based count of usage.record lines consumed so far,
99
+ * serialized as a decimal string in session_meta.usage_cursor).
100
+ *
101
+ * - cursor null/empty/unparseable → process ALL usage.record lines.
102
+ * - cursor = N (>= total) → nothing new; no events, cursor unchanged.
103
+ * - cursor = N (< total) → process usage.record lines AFTER index N.
104
+ * - BOUNDED COMPACTION FALLBACK: if the file SHRANK below the cursor (the
105
+ * stream was truncated/rotated, so prior lines are gone), the cursor has
106
+ * fallen off the front — process ONLY the LAST usage.record line so we
107
+ * never re-emit the whole history. Mirrors extractTranscriptUsageSince.
108
+ *
109
+ * `cursor` returns the decimal string count of TOTAL usage.record lines seen,
110
+ * so the next fire resumes exactly past it.
111
+ *
112
+ * Per-model summation: lines are bucketed by model_id and each bucket emits one
113
+ * agent_usage event (incremental deltas are additive — addUsage semantics).
114
+ *
115
+ * Char-algorithmic JSONL parse (split on "\n", JSON.parse each line, skip
116
+ * blanks/unparseable). NO regex.
117
+ */
118
+ export function extractKimiUsageSince(wireJsonlText, cursor) {
119
+ const inputCursor = parseCursor(cursor);
120
+ if (typeof wireJsonlText !== "string" || wireJsonlText.length === 0) {
121
+ // Empty/missing wire file: nothing to process, cursor unchanged.
122
+ return { events: [], cursor: cursor ?? null };
123
+ }
124
+ // Pass 1: materialize the ordered usage.record parse results (one linear
125
+ // walk). We keep the AgentUsageCounts for each usage.record line so the
126
+ // cursor counts ONLY usage records (not unrelated wire lines), making the
127
+ // high-water mark stable against interleaved non-usage records.
128
+ const records = [];
129
+ const lines = wireJsonlText.split("\n");
130
+ for (const line of lines) {
131
+ const trimmed = line.trim();
132
+ if (trimmed.length === 0)
133
+ continue;
134
+ let obj;
135
+ try {
136
+ obj = JSON.parse(trimmed);
137
+ }
138
+ catch {
139
+ continue; // partial/corrupt trailing line — skip.
140
+ }
141
+ if (!obj || typeof obj !== "object")
142
+ continue;
143
+ if (obj.type !== "usage.record")
144
+ continue;
145
+ const parsed = parseKimiUsage(obj);
146
+ // A usage.record that sums to zero still ADVANCES the cursor (it was seen)
147
+ // but contributes no tokens. Push a zero-counts placeholder so the index
148
+ // accounting stays aligned with the on-disk usage.record ordinal.
149
+ records.push(parsed ?? {
150
+ model_id: "",
151
+ input_tokens: 0,
152
+ output_tokens: 0,
153
+ cache_creation_tokens: 0,
154
+ cache_read_tokens: 0,
155
+ native_cost_usd: null,
156
+ });
157
+ }
158
+ const total = records.length;
159
+ if (total === 0) {
160
+ // No usage records at all → nothing to emit, cursor unchanged.
161
+ return { events: [], cursor: cursor ?? null };
162
+ }
163
+ // Select the slice to sum.
164
+ let slice;
165
+ if (inputCursor === null || inputCursor <= 0) {
166
+ slice = records; // all usage records
167
+ }
168
+ else if (inputCursor >= total) {
169
+ if (inputCursor === total) {
170
+ // Caught up exactly — nothing new.
171
+ slice = [];
172
+ }
173
+ else {
174
+ // Cursor exceeds the on-disk count → the stream shrank (compaction /
175
+ // rotation). Bounded fallback: last usage record only.
176
+ slice = records.slice(total - 1);
177
+ }
178
+ }
179
+ else {
180
+ slice = records.slice(inputCursor); // strictly after the cursor index
181
+ }
182
+ // Sum the selected records per model and emit via the shared event builder.
183
+ const sums = new Map();
184
+ for (const r of slice) {
185
+ const cur = sums.get(r.model_id) ?? { input: 0, output: 0, cacheCreate: 0, cacheRead: 0 };
186
+ cur.input += r.input_tokens;
187
+ cur.output += r.output_tokens;
188
+ cur.cacheCreate += r.cache_creation_tokens;
189
+ cur.cacheRead += r.cache_read_tokens;
190
+ sums.set(r.model_id, cur);
191
+ }
192
+ const events = [];
193
+ for (const [model, s] of sums) {
194
+ const ev = buildAgentUsageEvent({
195
+ model_id: model,
196
+ input_tokens: s.input,
197
+ output_tokens: s.output,
198
+ cache_creation_tokens: s.cacheCreate,
199
+ cache_read_tokens: s.cacheRead,
200
+ });
201
+ if (ev)
202
+ events.push(ev);
203
+ }
204
+ // Cursor always advances to the total usage.record count seen, so the next
205
+ // fire resumes past it. Even when the slice produced no events (all-zero or
206
+ // already caught up), the high-water mark moves forward.
207
+ return { events, cursor: String(total) };
208
+ }
209
+ /** Decimal-string cursor → non-negative integer count, or null when absent/invalid. */
210
+ function parseCursor(cursor) {
211
+ if (typeof cursor !== "string" || cursor.length === 0)
212
+ return null;
213
+ const n = Number.parseInt(cursor, 10);
214
+ if (!Number.isFinite(n) || n < 0)
215
+ return null;
216
+ return n;
217
+ }
@@ -52,6 +52,11 @@ type ToolCallEventResult = {
52
52
  block?: boolean;
53
53
  reason?: string;
54
54
  };
55
+ type TurnEndEvent = {
56
+ type?: string;
57
+ message?: unknown;
58
+ messages?: unknown;
59
+ };
55
60
  type HookEventCtx = Record<string, unknown> | undefined;
56
61
  type HookHandler<E, R = void> = (event: E, ctx: HookEventCtx) => R | undefined | Promise<R | undefined>;
57
62
  export interface MinimalHookAPI {
@@ -63,6 +68,7 @@ export interface MinimalHookAPI {
63
68
  }>): void;
64
69
  on(event: "tool_call", handler: HookHandler<ToolCallEvent, ToolCallEventResult>): void;
65
70
  on(event: "tool_result", handler: HookHandler<ToolResultEvent>): void;
71
+ on(event: "turn_end", handler: HookHandler<TurnEndEvent>): void;
66
72
  on(event: string, handler: (...args: unknown[]) => unknown): void;
67
73
  }
68
74
  /**
@@ -24,11 +24,14 @@
24
24
  * under OMP and is intentionally omitted here.
25
25
  */
26
26
  import { createHash } from "node:crypto";
27
- import { mkdirSync } from "node:fs";
27
+ import { existsSync, mkdirSync } from "node:fs";
28
+ import { dirname, resolve } from "node:path";
29
+ import { fileURLToPath } from "node:url";
28
30
  import { resolveSessionDbPath, SessionDB } from "../../session/db.js";
29
- import { extractEvents } from "../../session/extract.js";
31
+ import { extractEvents, buildAgentUsageEvent } from "../../session/extract.js";
30
32
  import { buildResumeSnapshot } from "../../session/snapshot.js";
31
33
  import { OMPAdapter } from "./index.js";
34
+ import { parseOmpUsage } from "./usage.js";
32
35
  // ── Tool-name normalization ─────────────────────────────
33
36
  // OMP uses lowercase tool names (refs/.../hooks/types.ts:451 example
34
37
  // `toolName: "bash"`). Shared event extractors expect PascalCase
@@ -64,6 +67,57 @@ let _db = null;
64
67
  let _dbPath = "";
65
68
  let _sessionId = "";
66
69
  const _ompAdapter = new OMPAdapter();
70
+ // ── MCP self-registration (issue #677) ───────────────────
71
+ // The `omp plugin install context-mode` path wires THIS extension factory
72
+ // (so routing hooks fire), but never creates the MCP config — so the 11
73
+ // `ctx_*` tools stay unreachable even though curl/wget are blocked. Register
74
+ // the server ourselves on plugin load, ONLY when absent (never clobber a
75
+ // user's existing entry). Takes effect on the next OMP restart, same as the
76
+ // manual mcp.json workaround the issue documents.
77
+ const MCP_SERVER_NAME = "context-mode";
78
+ // plugin.js ships at <pkg>/build/adapters/omp/plugin.js; the MCP server
79
+ // bundle sits at the package root (<pkg>/server.bundle.mjs) — three up.
80
+ const SERVER_BUNDLE_RELATIVE = "../../../server.bundle.mjs";
81
+ function resolveServerBundle() {
82
+ try {
83
+ const here = dirname(fileURLToPath(import.meta.url));
84
+ const bundle = resolve(here, SERVER_BUNDLE_RELATIVE);
85
+ return existsSync(bundle) ? bundle : null;
86
+ }
87
+ catch {
88
+ return null;
89
+ }
90
+ }
91
+ /**
92
+ * Ensure `~/.omp/agent/mcp.json` registers the context-mode MCP server.
93
+ *
94
+ * Uses `node <abs>/server.bundle.mjs` rather than the `context-mode` bin:
95
+ * under the plugin install the package lives in `~/.omp/plugins/node_modules`
96
+ * and its bin is NOT on PATH, so the bare command would fail to spawn (the
97
+ * exact symptom reported on issue #677). Best effort — never throws, never
98
+ * breaks plugin load.
99
+ */
100
+ function ensureMcpServerRegistered() {
101
+ try {
102
+ const bundle = resolveServerBundle();
103
+ if (!bundle)
104
+ return; // bundle missing → nothing safe to register
105
+ const settings = _ompAdapter.readSettings() ?? {};
106
+ const mcpServers = settings.mcpServers ?? {};
107
+ if (MCP_SERVER_NAME in mcpServers)
108
+ return; // already present — don't clobber
109
+ mcpServers[MCP_SERVER_NAME] = {
110
+ type: "stdio",
111
+ command: "node",
112
+ args: [bundle],
113
+ };
114
+ settings.mcpServers = mcpServers;
115
+ _ompAdapter.writeSettings(settings);
116
+ }
117
+ catch {
118
+ // best effort — a registration failure must never break plugin load
119
+ }
120
+ }
67
121
  function getSessionDir() {
68
122
  const dir = _ompAdapter.getSessionDir();
69
123
  mkdirSync(dir, { recursive: true });
@@ -155,6 +209,9 @@ export default function ompPlugin(pi) {
155
209
  // earlier `OMP_PROJECT_DIR` read was an EM mistake — no upstream code
156
210
  // ever sets it. Drop it; fall through PI_PROJECT_DIR → cwd().
157
211
  const projectDir = process.env.PI_PROJECT_DIR || process.cwd();
212
+ // Self-register the MCP server so `ctx_*` tools are reachable under the
213
+ // plugin install path, not just the manual MCP-only path (issue #677).
214
+ ensureMcpServerRegistered();
158
215
  const db = getOrCreateDB(projectDir);
159
216
  // ── 1. session_start — initialize session row ─────────
160
217
  pi.on("session_start", (_event, ctx) => {
@@ -243,4 +300,32 @@ export default function ompPlugin(pi) {
243
300
  }
244
301
  return undefined;
245
302
  });
303
+ // ── 5. turn_end — per-turn token + provider cost capture ──
304
+ // OMP exposes REAL per-turn tokens AND a provider-computed USD cost on the
305
+ // completed turn's AssistantMessage (`event.message.usage` / `.model`),
306
+ // delivered INCREMENTALLY per turn (matrix §2,§5). parseOmpUsage maps that to
307
+ // the buildAgentUsageEvent counts (Usage.cacheWrite→cache_creation,
308
+ // cacheRead→cache_read, cost.total→native_cost_usd). buildAgentUsageEvent
309
+ // prefers the native cost over the local price table and returns null on an
310
+ // all-zero turn. We persist via db.insertEvent — the SessionDB-backed forward
311
+ // path used everywhere in this in-process plugin runtime (the .mjs
312
+ // attributeAndInsertEvents helper is the Claude-hook analogue, not reachable
313
+ // here). Best-effort: a usage parse must never break the turn.
314
+ pi.on("turn_end", (event) => {
315
+ try {
316
+ if (!_sessionId)
317
+ return undefined;
318
+ const counts = parseOmpUsage(event);
319
+ if (counts === null)
320
+ return undefined;
321
+ const usageEvent = buildAgentUsageEvent(counts);
322
+ if (usageEvent === null)
323
+ return undefined;
324
+ db.insertEvent(_sessionId, usageEvent, "PostToolUse");
325
+ }
326
+ catch {
327
+ // best effort — never break the turn on cost capture
328
+ }
329
+ return undefined;
330
+ });
246
331
  }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * adapters/omp/usage — pure parse of an OMP `turn_end` / `agent_end` payload
3
+ * into the {@link buildAgentUsageEvent} counts shape.
4
+ *
5
+ * Ground truth (docs/prds/2026-06-paid-observability/adapter-matrix/omp.md §2-4):
6
+ * - Per-turn usage rides on `AssistantMessage.usage`
7
+ * (`refs/platforms/omp/packages/ai/src/types.ts:505-541`).
8
+ * - The canonical `Usage` shape lives in pi-catalog
9
+ * (`refs/platforms/omp/packages/catalog/src/types.ts:100-145`): fields
10
+ * `input`, `output`, `cacheRead`, `cacheWrite`, `totalTokens`, plus a
11
+ * fully-resolved `cost: { input, output, cacheRead, cacheWrite, total }`
12
+ * (:138-144) — provider-computed, so NO client price table is needed.
13
+ * - `model: string` is `provider/model` on the `AssistantMessage` (:510).
14
+ * - `turn_end` carries `message: AssistantMessage`
15
+ * (`refs/.../extensibility/shared-events.ts:204-208`); `agent_end` carries
16
+ * `messages: AssistantMessage[]` (:191-194).
17
+ *
18
+ * Field mapping (matrix §3 → buildAgentUsageEvent input):
19
+ * Usage.input → input_tokens
20
+ * Usage.output → output_tokens
21
+ * Usage.cacheWrite → cache_creation_tokens (cache CREATION on the provider)
22
+ * Usage.cacheRead → cache_read_tokens
23
+ * message.model → model_id (the `provider/model` string)
24
+ * Usage.cost.total → native_cost_usd (provider-computed, preferred)
25
+ *
26
+ * Algorithmic + null-safe. NO regex. Returns `null` whenever there is no
27
+ * usable signal (no message / no usage / every token bucket absent-or-zero),
28
+ * so a malformed or empty turn emits nothing.
29
+ */
30
+ /**
31
+ * The counts object accepted by {@link buildAgentUsageEvent}. Re-declared here
32
+ * (structurally identical) so this module stays a leaf with no import cycle
33
+ * into the heavy `session/extract` module — the handler in `plugin.ts` passes
34
+ * the result straight through to `buildAgentUsageEvent`.
35
+ */
36
+ export interface OmpUsageCounts {
37
+ model_id: string;
38
+ input_tokens: number;
39
+ output_tokens: number;
40
+ cache_creation_tokens: number;
41
+ cache_read_tokens: number;
42
+ native_cost_usd: number | null;
43
+ }
44
+ /**
45
+ * Parse an OMP `turn_end` / `agent_end` event payload into the
46
+ * {@link buildAgentUsageEvent} counts shape, or `null` when no usable usage is
47
+ * present. Pure and side-effect free.
48
+ */
49
+ export declare function parseOmpUsage(payload: unknown): OmpUsageCounts | null;
@@ -0,0 +1,110 @@
1
+ /**
2
+ * adapters/omp/usage — pure parse of an OMP `turn_end` / `agent_end` payload
3
+ * into the {@link buildAgentUsageEvent} counts shape.
4
+ *
5
+ * Ground truth (docs/prds/2026-06-paid-observability/adapter-matrix/omp.md §2-4):
6
+ * - Per-turn usage rides on `AssistantMessage.usage`
7
+ * (`refs/platforms/omp/packages/ai/src/types.ts:505-541`).
8
+ * - The canonical `Usage` shape lives in pi-catalog
9
+ * (`refs/platforms/omp/packages/catalog/src/types.ts:100-145`): fields
10
+ * `input`, `output`, `cacheRead`, `cacheWrite`, `totalTokens`, plus a
11
+ * fully-resolved `cost: { input, output, cacheRead, cacheWrite, total }`
12
+ * (:138-144) — provider-computed, so NO client price table is needed.
13
+ * - `model: string` is `provider/model` on the `AssistantMessage` (:510).
14
+ * - `turn_end` carries `message: AssistantMessage`
15
+ * (`refs/.../extensibility/shared-events.ts:204-208`); `agent_end` carries
16
+ * `messages: AssistantMessage[]` (:191-194).
17
+ *
18
+ * Field mapping (matrix §3 → buildAgentUsageEvent input):
19
+ * Usage.input → input_tokens
20
+ * Usage.output → output_tokens
21
+ * Usage.cacheWrite → cache_creation_tokens (cache CREATION on the provider)
22
+ * Usage.cacheRead → cache_read_tokens
23
+ * message.model → model_id (the `provider/model` string)
24
+ * Usage.cost.total → native_cost_usd (provider-computed, preferred)
25
+ *
26
+ * Algorithmic + null-safe. NO regex. Returns `null` whenever there is no
27
+ * usable signal (no message / no usage / every token bucket absent-or-zero),
28
+ * so a malformed or empty turn emits nothing.
29
+ */
30
+ /** Coerce an unknown to a finite non-negative integer token count, else 0. */
31
+ function toTokenCount(value) {
32
+ if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
33
+ return 0;
34
+ }
35
+ // Token counts are integers upstream; floor defensively against floats.
36
+ return Math.floor(value);
37
+ }
38
+ /** Coerce an unknown to a finite USD cost, else null (so the catalog can fill in). */
39
+ function toCostUsd(value) {
40
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
41
+ return null;
42
+ }
43
+ return value;
44
+ }
45
+ /** Narrow an unknown to a plain (non-null, non-array) object. */
46
+ function asRecord(value) {
47
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
48
+ return null;
49
+ }
50
+ return value;
51
+ }
52
+ /**
53
+ * Resolve the per-turn `AssistantMessage` from a `turn_end` (`.message`) or
54
+ * `agent_end` (`.messages[]`) payload. For `agent_end` we take the LAST
55
+ * assistant message — that is the turn that just completed and whose usage is
56
+ * the new delta (OMP usage is INCREMENTAL per turn, matrix §5).
57
+ */
58
+ function resolveMessage(payload) {
59
+ const single = asRecord(payload.message);
60
+ if (single !== null)
61
+ return single;
62
+ const list = payload.messages;
63
+ if (Array.isArray(list)) {
64
+ for (let i = list.length - 1; i >= 0; i--) {
65
+ const m = asRecord(list[i]);
66
+ if (m !== null)
67
+ return m;
68
+ }
69
+ }
70
+ return null;
71
+ }
72
+ /**
73
+ * Parse an OMP `turn_end` / `agent_end` event payload into the
74
+ * {@link buildAgentUsageEvent} counts shape, or `null` when no usable usage is
75
+ * present. Pure and side-effect free.
76
+ */
77
+ export function parseOmpUsage(payload) {
78
+ const root = asRecord(payload);
79
+ if (root === null)
80
+ return null;
81
+ const message = resolveMessage(root);
82
+ if (message === null)
83
+ return null;
84
+ const usage = asRecord(message.usage);
85
+ if (usage === null)
86
+ return null;
87
+ const input_tokens = toTokenCount(usage.input);
88
+ const output_tokens = toTokenCount(usage.output);
89
+ const cache_creation_tokens = toTokenCount(usage.cacheWrite);
90
+ const cache_read_tokens = toTokenCount(usage.cacheRead);
91
+ // No usable token signal → no event (mirrors buildAgentUsageEvent's own
92
+ // all-zero guard, but lets the handler short-circuit before building).
93
+ if (input_tokens <= 0 &&
94
+ output_tokens <= 0 &&
95
+ cache_creation_tokens <= 0 &&
96
+ cache_read_tokens <= 0) {
97
+ return null;
98
+ }
99
+ const model_id = typeof message.model === "string" ? message.model : "";
100
+ const cost = asRecord(usage.cost);
101
+ const native_cost_usd = cost !== null ? toCostUsd(cost.total) : null;
102
+ return {
103
+ model_id,
104
+ input_tokens,
105
+ output_tokens,
106
+ cache_creation_tokens,
107
+ cache_read_tokens,
108
+ native_cost_usd,
109
+ };
110
+ }
@@ -78,6 +78,16 @@ interface OpenClawPluginApi {
78
78
  registerTool?(tool: OpenClawToolDef, opts?: {
79
79
  optional?: boolean;
80
80
  }): void;
81
+ /**
82
+ * Subscribe to the openclaw diagnostic-event bus (`model.usage` carries
83
+ * per-turn token usage + pre-computed costUsd — diagnostic-events.ts:1156).
84
+ * Some hosts surface `onDiagnosticEvent` directly on the activation `api`;
85
+ * others expose it only as a module export from
86
+ * `openclaw/plugin-sdk/diagnostic-runtime`. Typed loosely + optional so the
87
+ * plugin compiles and runs whether or not the host provides it (the SDK is
88
+ * not a dependency of this repo). Returns an unsubscribe function.
89
+ */
90
+ onDiagnosticEvent?(listener: (evt: unknown) => void): (() => void) | void;
81
91
  logger?: {
82
92
  info: (...args: unknown[]) => void;
83
93
  error: (...args: unknown[]) => void;
@@ -39,6 +39,7 @@ import { OpenClawSessionDB } from "./session-db.js";
39
39
  import { extractEvents, extractUserEvents } from "../../session/extract.js";
40
40
  import { buildResumeSnapshot } from "../../session/snapshot.js";
41
41
  import { WorkspaceRouter } from "./workspace-router.js";
42
+ import { handleOpenclawUsageEvent } from "./usage.js";
42
43
  import { buildNodeCommand } from "../types.js";
43
44
  import { OPENCLAW_TOOL_DEFS } from "./mcp-tools.js";
44
45
  // ── System-reminder filter (CCv2 — SLICE OClaw-3) ─────────
@@ -384,6 +385,62 @@ export default {
384
385
  // Silent — session capture must never break the tool call
385
386
  }
386
387
  });
388
+ // ── 2b. model.usage — Per-turn token + cost capture ──────
389
+ // openclaw emits a first-class `model.usage` diagnostic event once per turn
390
+ // carrying the full usage breakdown + a pre-computed costUsd. We subscribe
391
+ // to the diagnostic-event bus (NOT the tool-call hook — before/after_tool_call
392
+ // carry approval/policy data only, no token usage). The handler
393
+ // (handleOpenclawUsageEvent) is decoupled + unit-tested; it parses → builds →
394
+ // inserts and never throws.
395
+ //
396
+ // tsc-safe SDK access: `onDiagnosticEvent` comes from the openclaw plugin SDK
397
+ // (`openclaw/plugin-sdk/diagnostic-runtime`), which is NOT in this repo's
398
+ // node_modules — a static import would break the build. We resolve it at
399
+ // runtime two ways, both best-effort:
400
+ // 1. directly off the activation `api` if the host surfaces it there;
401
+ // 2. otherwise a guarded dynamic import via a COMPUTED specifier string
402
+ // (TS treats it as Promise<any> and skips module resolution, mirroring
403
+ // the pathToFileURL(...).href dynamic imports above). Missing SDK → no-op.
404
+ const subscribeDiagnostics = (onDiag) => {
405
+ try {
406
+ onDiag((evt) => {
407
+ try {
408
+ const sid = sessionId; // snapshot to avoid race with session_start re-key
409
+ handleOpenclawUsageEvent(evt, (e) => db.insertEvent(sid, e, "Diagnostic"));
410
+ }
411
+ catch {
412
+ // Usage capture must never break the agent turn.
413
+ }
414
+ });
415
+ log.debug("model.usage diagnostic subscription registered");
416
+ }
417
+ catch (err) {
418
+ log.warn?.("model.usage diagnostic subscription failed", err);
419
+ }
420
+ };
421
+ if (typeof api.onDiagnosticEvent === "function") {
422
+ subscribeDiagnostics(api.onDiagnosticEvent.bind(api));
423
+ }
424
+ else {
425
+ // Computed specifier so NodeNext does not try to resolve the (absent) SDK
426
+ // module at build time — the dynamic import is typed as Promise<any>.
427
+ const diagnosticRuntimeSpecifier = ["openclaw", "plugin-sdk", "diagnostic-runtime"].join("/");
428
+ void (async () => {
429
+ try {
430
+ const mod = (await import(diagnosticRuntimeSpecifier));
431
+ if (typeof mod?.onDiagnosticEvent === "function") {
432
+ subscribeDiagnostics(mod.onDiagnosticEvent);
433
+ }
434
+ else {
435
+ log.debug("diagnostic-runtime loaded but onDiagnosticEvent missing");
436
+ }
437
+ }
438
+ catch {
439
+ // SDK not installed (dev/test) — usage capture silently inert.
440
+ log.debug("openclaw plugin-sdk/diagnostic-runtime unavailable — usage capture inert");
441
+ }
442
+ })();
443
+ }
387
444
  // ── 3. command:new — Session initialization ────────────
388
445
  registerCommandHook("command:new", async () => {
389
446
  try {
@@ -0,0 +1,34 @@
1
+ /**
2
+ * adapters/openclaw/usage — per-turn token + cost capture handler.
3
+ *
4
+ * openclaw emits a first-class `model.usage` diagnostic event once per turn
5
+ * (`DiagnosticUsageEvent`, refs/platforms/openclaw/src/infra/diagnostic-events.ts:18-47),
6
+ * carrying the full usage breakdown {input, output, cacheRead, cacheWrite} plus
7
+ * a PRE-COMPUTED `costUsd` (estimateUsageCost, agent-runner.ts:1995). Consumers
8
+ * subscribe via `onDiagnosticEvent(listener)` (diagnostic-events.ts:1156) — the
9
+ * exact bus the first-party diagnostics-otel / diagnostics-prometheus extensions
10
+ * read.
11
+ *
12
+ * This module is the parse→build→insert handler the plugin's diagnostic-event
13
+ * listener invokes. It is deliberately decoupled from the openclaw plugin SDK so
14
+ * it stays unit-testable: the caller passes the raw payload and an `insert`
15
+ * callback (the plugin hands it `db.insertEvent`-bound-to-sessionId). The handler
16
+ * never throws — a usage-capture failure must never break the agent turn.
17
+ *
18
+ * Capture surface: the diagnostic-event bus, NOT the tool-call hook. The native
19
+ * before_tool_call / after_tool_call relay carries only approval/policy data and
20
+ * NO token usage (matrix §4) — so usage cannot be captured from after_tool_call.
21
+ */
22
+ import type { SessionEvent } from "../../session/extract.js";
23
+ /** Minimal event-insert surface the handler needs (satisfied by SessionDB.insertEvent bound to a sessionId). */
24
+ export type OpenClawUsageInsert = (event: SessionEvent) => void;
25
+ /**
26
+ * Handle one openclaw `model.usage` diagnostic payload: parse the per-turn usage
27
+ * (NOT lastCallUsage), build the structured `agent_usage` event with openclaw's
28
+ * native `costUsd` (preferred over the pricing catalog), and insert it.
29
+ *
30
+ * Returns the inserted event (for tests / callers that want to forward) or null
31
+ * when the payload is not a usage event, carries no usage, or sums to zero.
32
+ * Best-effort: swallows any insert failure.
33
+ */
34
+ export declare function handleOpenclawUsageEvent(payload: unknown, insert: OpenClawUsageInsert): SessionEvent | null;