@coda-console/claude 1.16.0 → 1.17.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/README.md +6 -0
- package/agent/providers.mjs +9 -0
- package/agent/session.mjs +7 -4
- package/agent/usage.mjs +27 -16
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -128,4 +128,10 @@ ACP 2.6.0 只使用默认 stable v1。逐项经过真实连接的解析测试:
|
|
|
128
128
|
PKCE、令牌交换、凭据保存和续期都由 Claude CLI 负责,沿用服务账号的 `CLAUDE_CONFIG_DIR`。配置了 token 文件、OAuth token、API key 或云服务商时它们仍然优先,添加窗口里写明。
|
|
129
129
|
SDK 0.3.296 的登录输出与原生二进制包布局是这里的版本边界,升级时复核。
|
|
130
130
|
|
|
131
|
+
### 用量上报的模型身份
|
|
132
|
+
|
|
133
|
+
回执 `usage` 的模型身份按 Coda 的 `coda/usage@1` 写(Coda 的 docs/plugins.md「Coda 的用量记录」)。
|
|
134
|
+
- 这一轮用的是 Coda 注入的模型服务时:顶层带 `service`(由注入地址 `…/resources/<接口种类>/<UUID><路径>` 得到的 `coda://<接口种类>/<UUID>`)与 `asked`(发给服务的裸模型 ID,不带服务前缀);地址不是这个形状就两者都不带。SDK 的键只是请求名,不是实际型号,不报 `served`;只用了一个模型就不带分项,用了多个模型时请求的那一项带 `service`、`asked`,其余项按 SDK 的名字作未分层的 `model` 标签。
|
|
135
|
+
- 用 Claude 自己的模型时不带 `service`,分项里 SDK 给了 `canonicalModel` 的报 `served`,只有 SDK 的键的仍是未分层的 `model` 标签。
|
|
136
|
+
|
|
131
137
|
「模型服务」设置的行为见仓库根 README 的「模型服务」。
|
package/agent/providers.mjs
CHANGED
|
@@ -11,6 +11,15 @@ const log = text => process.stderr.write(`[claude-agent] ${text}\n`);
|
|
|
11
11
|
const parse = text => { try { return JSON.parse(text || '{}'); } catch { return {}; } };
|
|
12
12
|
const LIST_TIMEOUT = 8000;
|
|
13
13
|
|
|
14
|
+
// The resource reference (`coda://<接口种类>/<UUID>`) of the model service an address Coda injected is, from its
|
|
15
|
+
// `…/resources/<接口种类>/<UUID><路径>` (Coda's docs/plugins.md「模型服务」); null for an address of another shape.
|
|
16
|
+
export function serviceReference(url) {
|
|
17
|
+
let path;
|
|
18
|
+
try { path = new URL(url).pathname; } catch { return null; }
|
|
19
|
+
const found = /^\/resources\/(openai-completions|openai-responses|anthropic-messages|openai-speech)\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:\/|$)/i.exec(path);
|
|
20
|
+
return found ? `coda://${found[1]}/${found[2].toLowerCase()}` : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
14
23
|
// The services chosen, in the order chosen: each its address, title and key (read from the file Coda keeps it in). One not
|
|
15
24
|
// on this machine, or speaking another API, is left out.
|
|
16
25
|
export function chosenServices(env = process.env) {
|
package/agent/session.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { agent, PROTOCOL_VERSION, RequestError } from '@agentclientprotocol/sdk'
|
|
|
15
15
|
import { ClaudeSessionLimits, accountHash } from './limits.mjs';
|
|
16
16
|
import { claudeFailure } from './errors.mjs';
|
|
17
17
|
import { sdkEnvironment } from './auth.mjs';
|
|
18
|
-
import { chosenServices, listedModels, serviceSettings } from './providers.mjs';
|
|
18
|
+
import { chosenServices, listedModels, serviceReference, serviceSettings } from './providers.mjs';
|
|
19
19
|
import { SessionTitle } from './titles.mjs';
|
|
20
20
|
import { MODEL_EFFORT, MODES, claudeModels, configOptions, reportedModel } from './models.mjs';
|
|
21
21
|
import { permissionRequest } from './permissions.mjs';
|
|
@@ -229,11 +229,14 @@ export class ClaudeSession {
|
|
|
229
229
|
// opened goes on with the model, mode and effort chosen.
|
|
230
230
|
start(at, model) {
|
|
231
231
|
this.input = new InputStream();
|
|
232
|
-
const entry = this.served(model);
|
|
232
|
+
const entry = this.served(model), service = entry && this.services.find(service => service.ref === entry.service);
|
|
233
233
|
const settings = { ...this.launch.settings, ...(this.current.effort ? { effortLevel: this.current.effort } : {}),
|
|
234
234
|
...(this.current.ultracode === true ? { ultracode: true } : {}),
|
|
235
|
-
...(entry ? serviceSettings(
|
|
236
|
-
|
|
235
|
+
...(entry ? serviceSettings(service, entry.model) : {}) };
|
|
236
|
+
// On a service its usage says the service and the model asked, when its address gives a reference (providers.mjs).
|
|
237
|
+
const reference = service && serviceReference(service.url);
|
|
238
|
+
this.serviceModel = entry ? model : null; this.cacheUse.restarted();
|
|
239
|
+
this.usage.restarted(!!at.resume, service ? reference ? { service: reference, asked: entry.model } : {} : null);
|
|
237
240
|
// A compaction in the query moved off ends with it.
|
|
238
241
|
this.compacting?.answer({ compacted: false, reason: '会话换到另一个模型,压缩没有做完' }); this.compacting = null;
|
|
239
242
|
this.query = this.sdk.query({ prompt: this.input, options: { ...this.launch, ...at, settings,
|
package/agent/usage.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// What a prompt used, as `PromptResponse.usage` says it (Coda's per-turn token collection): this turn's increment only, `inputTokens`
|
|
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
|
-
// where the models are known (when it is there, it is the whole of what Coda counts; the top level is its sum).
|
|
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
|
+
// `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 的用量记录」).
|
|
4
6
|
//
|
|
5
7
|
// The SDK's result carries `modelUsage`, per model and cumulative over the query() call: main loop, Task subagents and all. This
|
|
6
8
|
// keeps what the last result said (`base`) and answers each result with the difference, so that a turn says what it used.
|
|
@@ -41,34 +43,41 @@ function fromApiUsage(usage) {
|
|
|
41
43
|
const increase = (now, before) => !before || FIELDS.some(field => now[field] < before[field]) ? now
|
|
42
44
|
: Object.fromEntries(FIELDS.map(field => [field, now[field] - before[field]]));
|
|
43
45
|
|
|
44
|
-
// What was used, by model name, and what no model is told of.
|
|
46
|
+
// What was used, by model (its `label`: `served` for the model the SDK says ran, else the SDK's own name as `model`), and what no model is told of.
|
|
45
47
|
class Spent {
|
|
46
48
|
models = new Map(); loose = null;
|
|
47
|
-
addModel(
|
|
49
|
+
addModel(label, counts) {
|
|
50
|
+
const key = JSON.stringify(label), before = this.models.get(key);
|
|
51
|
+
this.models.set(key, { label, counts: before ? plus(before.counts, counts) : counts });
|
|
52
|
+
}
|
|
48
53
|
addLoose(counts) { this.loose = this.loose ? plus(this.loose, counts) : counts; }
|
|
49
54
|
merge(other) {
|
|
50
|
-
for (const
|
|
55
|
+
for (const { label, counts } of other.models.values()) this.addModel(label, counts);
|
|
51
56
|
if (other.loose) this.addLoose(other.loose);
|
|
52
57
|
}
|
|
53
|
-
// `usage` for the response, or nothing where no token was used.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
58
|
+
// `usage` for the response, or nothing where no token was used. `requested`: the `{ service, asked }` of the model service the
|
|
59
|
+
// query ran on (`{}` for a service without a reference, null on Claude's own), told at the top level.
|
|
60
|
+
report(requested) {
|
|
61
|
+
const models = [...this.models.values()].filter(({ counts }) => !none(counts));
|
|
62
|
+
const all = [...models.map(({ counts }) => counts), ...(this.loose && !none(this.loose) ? [this.loose] : [])];
|
|
57
63
|
if (!all.length) return {};
|
|
58
64
|
const sum = all.reduce(plus);
|
|
59
65
|
const usage = { totalTokens: total(sum), inputTokens: sum.inputTokens, outputTokens: sum.outputTokens,
|
|
60
66
|
...(sum.cachedReadTokens === null ? {} : { cachedReadTokens: sum.cachedReadTokens }),
|
|
61
|
-
...(sum.cachedWriteTokens === null ? {} : { cachedWriteTokens: sum.cachedWriteTokens }) };
|
|
62
|
-
// The breakdown is all that Coda counts when it is there: one that leaves out a part is not told.
|
|
63
|
-
|
|
67
|
+
...(sum.cachedWriteTokens === null ? {} : { cachedWriteTokens: sum.cachedWriteTokens }), ...requested };
|
|
68
|
+
// The breakdown is all that Coda counts when it is there: one that leaves out a part is not told. A service's one model is no
|
|
69
|
+
// breakdown (the SDK's name for it is only the name asked, not the model that ran); of several, the one asked is told as that.
|
|
70
|
+
if ((!this.loose || none(this.loose)) && (!requested || models.length > 1))
|
|
71
|
+
usage.breakdown = models.map(({ label, counts }) => ({ ...(requested && label.model === requested.asked ? requested : label), ...counts }));
|
|
64
72
|
return { usage };
|
|
65
73
|
}
|
|
66
74
|
}
|
|
67
75
|
|
|
68
76
|
export class PromptUsage {
|
|
69
|
-
base = new Map(); held = new Spent();
|
|
70
|
-
// A query begins: a new session's from nothing, a resumed one's with its history (its first result says it).
|
|
71
|
-
|
|
77
|
+
base = new Map(); held = new Spent(); requested = null;
|
|
78
|
+
// A query begins: a new session's from nothing, a resumed one's with its history (its first result says it). `requested`: the
|
|
79
|
+
// `{ service, asked }` of the model service it runs on (`{}` for a service whose address gives no reference), null on Claude's own models.
|
|
80
|
+
restarted(resumed, requested = null) { this.base = resumed ? null : new Map(); this.held = new Spent(); this.requested = requested; }
|
|
72
81
|
// What this result's turn used (never throws: the result is handled whatever this makes of it).
|
|
73
82
|
measure(m) {
|
|
74
83
|
const spent = new Spent();
|
|
@@ -82,7 +91,9 @@ export class PromptUsage {
|
|
|
82
91
|
return spent;
|
|
83
92
|
}
|
|
84
93
|
for (const { key, usage, counts } of entries) {
|
|
85
|
-
|
|
94
|
+
// 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)));
|
|
86
97
|
this.base.set(key, counts);
|
|
87
98
|
}
|
|
88
99
|
} catch {}
|
|
@@ -91,6 +102,6 @@ export class PromptUsage {
|
|
|
91
102
|
// The turn of a prompt of Coda's used this; told when the prompt is answered.
|
|
92
103
|
hold(spent) { this.held.merge(spent); }
|
|
93
104
|
// The prompt is answered with what it used (`usage` of the response, or nothing), or failed: it reports none.
|
|
94
|
-
take() { const usage = this.held.report(); this.held = new Spent(); return usage; }
|
|
105
|
+
take() { const usage = this.held.report(this.requested); this.held = new Spent(); return usage; }
|
|
95
106
|
drop() { this.held = new Spent(); }
|
|
96
107
|
}
|