@mergeline/agent 0.5.2 → 0.5.3

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 (3) hide show
  1. package/README.md +3 -1
  2. package/dist/watch.js +25 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -140,7 +140,9 @@ board watch --follow --interval 60 -- <command> # keep waiting for n
140
140
 
141
141
  With `--usage-from claude-json` (and `claude -p --output-format json`), the
142
142
  watcher reads each run's tokens and cost from Claude Code's final JSON and
143
- reports them on the ticket the run worked on; `board usage <ticket> --usage
143
+ reports them on the ticket the run worked on. The tokens are the whole
144
+ run's, subagents included (`modelUsage`, summed over models; `usage` alone
145
+ has only the last query loop), and the cost is `total_cost_usd`; `board usage <ticket> --usage
144
146
  <json>` does the same by hand. The board stores usage only for agents whose
145
147
  "Report usage" is on.
146
148
 
package/dist/watch.js CHANGED
@@ -29,18 +29,33 @@ export function usageFromClaudeJson(output) {
29
29
  catch {
30
30
  continue;
31
31
  }
32
- if (!run || typeof run !== "object" || (run.total_cost_usd === undefined && !run.usage))
32
+ if (!run || typeof run !== "object" || (run.total_cost_usd === undefined && !run.usage && !run.modelUsage))
33
33
  continue;
34
- const u = run.usage ?? {};
35
34
  const usage = { source: "claude-code" };
36
- if (Number.isInteger(u.input_tokens))
37
- usage.input_tokens = u.input_tokens;
38
- if (Number.isInteger(u.output_tokens))
39
- usage.output_tokens = u.output_tokens;
40
- if (Number.isInteger(u.cache_read_input_tokens))
41
- usage.cache_read_tokens = u.cache_read_input_tokens;
42
- if (Number.isInteger(u.cache_creation_input_tokens))
43
- usage.cache_write_tokens = u.cache_creation_input_tokens;
35
+ // `usage` has only the main thread's last query loop; `modelUsage` has the
36
+ // whole run, subagents and later loops included, like `total_cost_usd`.
37
+ const models = run.modelUsage && typeof run.modelUsage === "object"
38
+ ? Object.values(run.modelUsage).filter((m) => !!m && typeof m === "object")
39
+ : [];
40
+ const fields = models.length
41
+ ? [
42
+ ["input_tokens", "inputTokens"],
43
+ ["output_tokens", "outputTokens"],
44
+ ["cache_read_tokens", "cacheReadInputTokens"],
45
+ ["cache_write_tokens", "cacheCreationInputTokens"],
46
+ ]
47
+ : [
48
+ ["input_tokens", "input_tokens"],
49
+ ["output_tokens", "output_tokens"],
50
+ ["cache_read_tokens", "cache_read_input_tokens"],
51
+ ["cache_write_tokens", "cache_creation_input_tokens"],
52
+ ];
53
+ const sources = models.length ? models : [run.usage ?? {}];
54
+ for (const [to, from] of fields) {
55
+ const counts = sources.map((m) => m[from]).filter((n) => Number.isInteger(n));
56
+ if (counts.length)
57
+ usage[to] = counts.reduce((a, b) => a + b, 0);
58
+ }
44
59
  if (typeof run.total_cost_usd === "number") {
45
60
  usage.cost = Math.round(run.total_cost_usd * 10000) / 10000;
46
61
  usage.currency = "USD";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mergeline/agent",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "The agent's side of a Mergeline board: an MCP server and the `board` CLI. Works with any agent tool that runs MCP servers or shell commands.",
5
5
  "license": "MIT",
6
6
  "author": "frontender-ua",