@coda-console/claude 1.18.0 → 1.18.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/agent/session.mjs CHANGED
@@ -94,6 +94,10 @@ export class ClaudeSession {
94
94
  // The messages sent into the prompt's turn not yet taken (steer), the prompt whose own turn ended before they were, how
95
95
  // the prompt ends once it is ending before they are taken (dropRest), and whether a native turn is going.
96
96
  this.steering = new Set(); this.held = null; this.ending = null; this.turning = false;
97
+ // The model chosen that the turn going runs as (system init): what its usage says it asked for on Claude's own models.
98
+ this.turnAsked = null;
99
+ // The models the turn's main thread said it ran on (its init, its own messages; never a subagent's), for that usage.
100
+ this.turnModels = new Set();
97
101
  // The model services chosen in Coda (providers.mjs), and the service's model the query runs on: null on Claude's own.
98
102
  this.services = chosenServices(env); this.serviceModel = null;
99
103
  this.models = []; this.commands = []; this.terminal = null; this.current = { mode: 'default', model: undefined, effort: null, ultracode: null };
@@ -515,6 +519,9 @@ export class ClaudeSession {
515
519
  // Its effort is the one applied, the model's own included: what was chosen stays what it was. A query on the
516
520
  // service's model reports it by that service's name for it.
517
521
  this.current = { ...this.current, model: this.serviceModel ?? reportedModel(this.models, this.current.model, m.model), mode: m.permissionMode ?? this.current.mode };
522
+ // The model this turn runs as chosen: a model chosen while it goes is the next turn's.
523
+ this.turnAsked = this.current.model;
524
+ this.turnModels = new Set([m.model]);
518
525
  if (!this.title) {
519
526
  const sessionId = this.sessionId;
520
527
  this.title = new SessionTitle({ sdk: this.sdk, query: this.query, cwd: this.cwd, sessionId, alive: () => ['starting', 'running'].includes(this.phase),
@@ -632,6 +639,7 @@ export class ClaudeSession {
632
639
  if (!parent) {
633
640
  this.turnError = m.error ? { error: m.error, message: text(m.message?.content) } : null;
634
641
  if (m.error) return;
642
+ this.turnModels.add(m.message?.model);
635
643
  // One not streamed was a request too.
636
644
  if (!blocks) this.cacheUse.requested();
637
645
  this.cacheUse.used(m.message?.usage);
@@ -671,7 +679,8 @@ export class ClaudeSession {
671
679
  }
672
680
  result(m) {
673
681
  // What this turn used; the base moves with every result, whose turn it is told as (below) or not.
674
- const spent = this.usage.measure(m);
682
+ const spent = this.usage.measure(m, this.turnAsked ?? this.current.model, [...this.turnModels]);
683
+ this.turnModels = new Set();
675
684
  const windows = Object.values(m.modelUsage ?? {});
676
685
  const ids = takenIn(m);
677
686
  // The compaction Coda asked for (compact) is the turn that names its input. Any other is the session's own, whose cache runs
package/agent/usage.mjs CHANGED
@@ -2,7 +2,11 @@
2
2
  // the uncached input (the cached reads and writes not in it), the cache reads and writes beside it, and `breakdown` by model
3
3
  // where the models are known (when it is there, it is the whole of what Coda counts; the top level is its sum). A model is told as
4
4
  // `served` when the SDK names the one that ran (`canonicalModel`) and else by the SDK's own name as the unlayered `model` label; on a
5
- // model service (providers.mjs) the usage says its `service` and `asked` instead (Coda's docs/plugins.md「Coda 的用量记录」).
5
+ // model service (providers.mjs) the usage says its `service` and `asked` instead (Coda's docs/plugins.md「Coda 的用量记录」). On
6
+ // Claude's own models the row of the model the turn asked for (the session's chosen one) also says `asked`, so that its turns and
7
+ // tokens are one record; a subagent's model is never told as asked, nor is a model told by `model` (that label stands alone). That
8
+ // row is the one named as asked (by its key or canonical name) or one the turn's main thread said it ran on (`confirmed`: the
9
+ // models of its init and of its own messages): a lone model that is neither, a subagent's say, is never taken for the main one.
6
10
  //
7
11
  // The SDK's result carries `modelUsage`, per model and cumulative over the query() call: main loop, Task subagents and all. This
8
12
  // keeps what the last result said (`base`) and answers each result with the difference, so that a turn says what it used.
@@ -78,8 +82,9 @@ export class PromptUsage {
78
82
  // A query begins: a new session's from nothing, a resumed one's with its history (its first result says it). `requested`: the
79
83
  // `{ service, asked }` of the model service it runs on (`{}` for a service whose address gives no reference), null on Claude's own models.
80
84
  restarted(resumed, requested = null) { this.base = resumed ? null : new Map(); this.held = new Spent(); this.requested = requested; }
81
- // What this result's turn used (never throws: the result is handled whatever this makes of it).
82
- measure(m) {
85
+ // What this result's turn used (never throws: the result is handled whatever this makes of it). `asked`: the model the turn ran as
86
+ // chosen, on Claude's own models; `confirmed`: the models its main thread said it ran on (never a subagent's).
87
+ measure(m, asked = null, confirmed = []) {
83
88
  const spent = new Spent();
84
89
  try {
85
90
  const entries = Object.entries(m.modelUsage ?? {}).map(([key, usage]) => ({ key, usage, counts: fromModelUsage(usage) })).filter(entry => entry.counts);
@@ -90,11 +95,20 @@ export class PromptUsage {
90
95
  if (counts) spent.addLoose(counts);
91
96
  return spent;
92
97
  }
93
- for (const { key, usage, counts } of entries) {
98
+ const named = entry => !this.requested && typeof entry.usage.canonicalModel === 'string' && entry.usage.canonicalModel;
99
+ // What each model used this turn (the results hold the whole query's totals, those of models idle now included).
100
+ const turn = entries.map(entry => ({ ...entry, used: increase(entry.counts, this.base.get(entry.key)) }));
101
+ for (const { key, counts } of turn) this.base.set(key, counts);
102
+ const active = turn.filter(entry => !none(entry.used));
103
+ // The main model's row is the one asked for, or else the one its main thread said ran, whatever name the SDK gives it.
104
+ const bare = name => typeof name === 'string' ? name.replace(/\[[^\]]+\]$/, '') : name;
105
+ const is = names => entry => named(entry) && [entry.key, named(entry)].some(name => names.map(bare).includes(bare(name)));
106
+ const main = !this.requested && typeof asked === 'string' && asked ? active.find(is([asked])) ?? active.find(is([...confirmed])) : undefined;
107
+ for (const entry of active) {
108
+ const { key, used } = entry, canonical = named(entry);
94
109
  // A service's model is told by the name the SDK has it asked as, which is no model that ran; Claude's own by the canonical one.
95
- const canonical = !this.requested && typeof usage.canonicalModel === 'string' && usage.canonicalModel;
96
- spent.addModel(canonical ? { served: canonical } : { model: key }, increase(counts, this.base.get(key)));
97
- this.base.set(key, counts);
110
+ const label = !canonical ? { model: key } : main && canonical === named(main) ? { asked, served: canonical } : { served: canonical };
111
+ spent.addModel(label, used);
98
112
  }
99
113
  } catch {}
100
114
  return spent;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "claude",
4
4
  "displayName": "Claude",
5
5
  "description": "用 Claude Agent SDK 跑会话",
6
- "version": "1.18.0",
6
+ "version": "1.18.1",
7
7
  "engines": {
8
8
  "coda": 21
9
9
  },