dsh-agent-sdk-bridge 1.1.2 → 1.1.4
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/lib/index.js +93 -20
- package/lib/types/codebuddy.d.ts +22 -1
- package/package.json +1 -1
- package/src/codebuddy.ts +126 -20
package/lib/index.js
CHANGED
|
@@ -4004,6 +4004,30 @@ var CodeBuddyAuth = class {
|
|
|
4004
4004
|
}
|
|
4005
4005
|
}
|
|
4006
4006
|
};
|
|
4007
|
+
/** A token count as a non-negative whole number; anything unusable becomes 0. */
|
|
4008
|
+
function countOf(value) {
|
|
4009
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.ceil(value) : 0;
|
|
4010
|
+
}
|
|
4011
|
+
/** Whether one usage frame actually measured a prompt. */
|
|
4012
|
+
function measuredPrompt(usage) {
|
|
4013
|
+
return usage !== void 0 && (countOf(usage.input_tokens) > 0 || countOf(usage.cache_read_input_tokens) > 0 || countOf(usage.cache_creation_input_tokens) > 0);
|
|
4014
|
+
}
|
|
4015
|
+
/**
|
|
4016
|
+
* Fold one `message_delta` usage into the turn's reading.
|
|
4017
|
+
*
|
|
4018
|
+
* The delta is cumulative for its own message and carries the final output
|
|
4019
|
+
* count. Not every transport repeats the prompt fields on it, so the last
|
|
4020
|
+
* measured values are kept rather than letting a later frame erase them.
|
|
4021
|
+
*/
|
|
4022
|
+
function mergeTurnUsage(previous, next) {
|
|
4023
|
+
const output = typeof next.output_tokens === "number" && Number.isFinite(next.output_tokens) ? next.output_tokens : previous?.output_tokens ?? 0;
|
|
4024
|
+
return {
|
|
4025
|
+
input_tokens: countOf(next.input_tokens) > 0 ? next.input_tokens : previous?.input_tokens ?? 0,
|
|
4026
|
+
output_tokens: output,
|
|
4027
|
+
cache_read_input_tokens: next.cache_read_input_tokens ?? previous?.cache_read_input_tokens ?? 0,
|
|
4028
|
+
cache_creation_input_tokens: next.cache_creation_input_tokens ?? previous?.cache_creation_input_tokens ?? 0
|
|
4029
|
+
};
|
|
4030
|
+
}
|
|
4007
4031
|
/**
|
|
4008
4032
|
* One warm CodeBuddy session for a host session id.
|
|
4009
4033
|
*
|
|
@@ -4031,7 +4055,26 @@ var CodeBuddySession = class {
|
|
|
4031
4055
|
callCounter = 0;
|
|
4032
4056
|
callNonce = randomUUID().slice(0, 8);
|
|
4033
4057
|
lastUsage;
|
|
4034
|
-
|
|
4058
|
+
/**
|
|
4059
|
+
* The last prompt-side buckets a request actually measured.
|
|
4060
|
+
*
|
|
4061
|
+
* Kept across turns: a turn can end without a usable usage frame (aborted
|
|
4062
|
+
* before the first delta, or a transport that only sends the cumulative
|
|
4063
|
+
* result). Reusing the last real reading keeps the host's context pressure
|
|
4064
|
+
* at the provider's actual prompt instead of collapsing it to the CLI's
|
|
4065
|
+
* startup overhead.
|
|
4066
|
+
*/
|
|
4067
|
+
lastPromptUsage;
|
|
4068
|
+
/**
|
|
4069
|
+
* Ids of assistant messages whose streaming events this session consumed.
|
|
4070
|
+
*
|
|
4071
|
+
* Kept across turns, not reset with the turn: the echo of a tool round is
|
|
4072
|
+
* delivered on the next stream(), so the previous turn's id is exactly the
|
|
4073
|
+
* one that has to be recognised. Only the last few ids matter, and bounding
|
|
4074
|
+
* the list keeps a long-lived session from growing without limit.
|
|
4075
|
+
*/
|
|
4076
|
+
streamedMessageIds = /* @__PURE__ */ new Set();
|
|
4077
|
+
streamedMessageOrder = [];
|
|
4035
4078
|
output = new TurnOutputAccounting();
|
|
4036
4079
|
inputChars = 0;
|
|
4037
4080
|
toolResultTimeoutMs;
|
|
@@ -4243,7 +4286,8 @@ var CodeBuddySession = class {
|
|
|
4243
4286
|
const event = message.event;
|
|
4244
4287
|
if (event.type === "message_start") {
|
|
4245
4288
|
const usage = event.message.usage;
|
|
4246
|
-
if (usage
|
|
4289
|
+
if (measuredPrompt(usage)) this.lastUsage = usage;
|
|
4290
|
+
this.rememberStreamedMessage(event.message.id);
|
|
4247
4291
|
return;
|
|
4248
4292
|
}
|
|
4249
4293
|
if (event.type === "content_block_start") {
|
|
@@ -4348,7 +4392,7 @@ var CodeBuddySession = class {
|
|
|
4348
4392
|
}
|
|
4349
4393
|
if (event.type === "message_delta") {
|
|
4350
4394
|
const usage = event.usage;
|
|
4351
|
-
if (usage !== void 0) this.lastUsage = usage;
|
|
4395
|
+
if (usage !== void 0) this.lastUsage = mergeTurnUsage(this.lastUsage, usage);
|
|
4352
4396
|
return;
|
|
4353
4397
|
}
|
|
4354
4398
|
if (event.type === "message_stop") {
|
|
@@ -4358,6 +4402,8 @@ var CodeBuddySession = class {
|
|
|
4358
4402
|
return;
|
|
4359
4403
|
}
|
|
4360
4404
|
if (message.type === "assistant") {
|
|
4405
|
+
const echoOf = message.message.id;
|
|
4406
|
+
if (typeof echoOf === "string" && this.streamedMessageIds.has(echoOf)) return;
|
|
4361
4407
|
if (this.textBlock === void 0 && this.tools.length === 0) {
|
|
4362
4408
|
for (const block of message.message.content) if (block.type === "tool_use") {
|
|
4363
4409
|
this.countOutput(block.name);
|
|
@@ -4385,7 +4431,6 @@ var CodeBuddySession = class {
|
|
|
4385
4431
|
return;
|
|
4386
4432
|
}
|
|
4387
4433
|
if (message.type === "result") {
|
|
4388
|
-
this.resultUsage = message.usage;
|
|
4389
4434
|
if (this.abortPending) {
|
|
4390
4435
|
this.endTurn({
|
|
4391
4436
|
kind: "aborted",
|
|
@@ -4424,6 +4469,16 @@ var CodeBuddySession = class {
|
|
|
4424
4469
|
this.endTurn({ kind: "stop" });
|
|
4425
4470
|
}
|
|
4426
4471
|
}
|
|
4472
|
+
/** Record that one assistant message reached the host as streaming events. */
|
|
4473
|
+
rememberStreamedMessage(id) {
|
|
4474
|
+
if (typeof id !== "string" || id.length === 0 || this.streamedMessageIds.has(id)) return;
|
|
4475
|
+
this.streamedMessageIds.add(id);
|
|
4476
|
+
this.streamedMessageOrder.push(id);
|
|
4477
|
+
if (this.streamedMessageOrder.length > 32) {
|
|
4478
|
+
const oldest = this.streamedMessageOrder.shift();
|
|
4479
|
+
if (oldest !== void 0) this.streamedMessageIds.delete(oldest);
|
|
4480
|
+
}
|
|
4481
|
+
}
|
|
4427
4482
|
resetTurnState() {
|
|
4428
4483
|
this.blockIndex = 0;
|
|
4429
4484
|
this.textBlock = void 0;
|
|
@@ -4433,7 +4488,6 @@ var CodeBuddySession = class {
|
|
|
4433
4488
|
this.inputChars = 0;
|
|
4434
4489
|
this.output.reset();
|
|
4435
4490
|
this.lastUsage = void 0;
|
|
4436
|
-
this.resultUsage = void 0;
|
|
4437
4491
|
this.turnEndKind = void 0;
|
|
4438
4492
|
}
|
|
4439
4493
|
emit(chunk) {
|
|
@@ -4446,29 +4500,48 @@ var CodeBuddySession = class {
|
|
|
4446
4500
|
this.output.addOutput(text);
|
|
4447
4501
|
}
|
|
4448
4502
|
usage() {
|
|
4449
|
-
const metered = this.lastUsage
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4503
|
+
const metered = this.lastUsage;
|
|
4504
|
+
const local = this.output.usage();
|
|
4505
|
+
const measuredOutput = countOf(metered?.output_tokens);
|
|
4506
|
+
const outputTokens = measuredOutput > 0 ? measuredOutput : Math.max(1, Math.ceil(local.outputTokens));
|
|
4507
|
+
const reasoning = local.reasoningTokens === void 0 ? {} : { reasoningTokens: Math.min(outputTokens, Math.max(1, Math.ceil(local.reasoningTokens))) };
|
|
4508
|
+
if (metered !== void 0 && countOf(metered.input_tokens) > 0) {
|
|
4509
|
+
const cacheRead = countOf(metered.cache_read_input_tokens);
|
|
4510
|
+
const cacheWrite = countOf(metered.cache_creation_input_tokens);
|
|
4511
|
+
const inputTokens = Math.max(0, countOf(metered.input_tokens) - cacheRead - cacheWrite);
|
|
4512
|
+
this.lastPromptUsage = {
|
|
4513
|
+
inputTokens,
|
|
4514
|
+
cacheReadTokens: cacheRead,
|
|
4515
|
+
cacheWriteTokens: cacheWrite
|
|
4516
|
+
};
|
|
4455
4517
|
return {
|
|
4456
|
-
inputTokens
|
|
4457
|
-
outputTokens
|
|
4458
|
-
...
|
|
4518
|
+
inputTokens,
|
|
4519
|
+
outputTokens,
|
|
4520
|
+
...reasoning,
|
|
4459
4521
|
cacheReadTokens: cacheRead,
|
|
4460
4522
|
cacheWriteTokens: cacheWrite,
|
|
4461
|
-
totalTokens:
|
|
4523
|
+
totalTokens: inputTokens + cacheRead + cacheWrite + outputTokens
|
|
4462
4524
|
};
|
|
4463
4525
|
}
|
|
4464
|
-
|
|
4465
|
-
|
|
4526
|
+
if (this.lastPromptUsage !== void 0) {
|
|
4527
|
+
const prompt = this.lastPromptUsage;
|
|
4528
|
+
return {
|
|
4529
|
+
inputTokens: prompt.inputTokens,
|
|
4530
|
+
outputTokens,
|
|
4531
|
+
...reasoning,
|
|
4532
|
+
cacheReadTokens: prompt.cacheReadTokens,
|
|
4533
|
+
cacheWriteTokens: prompt.cacheWriteTokens,
|
|
4534
|
+
totalTokens: prompt.inputTokens + prompt.cacheReadTokens + prompt.cacheWriteTokens + outputTokens
|
|
4535
|
+
};
|
|
4536
|
+
}
|
|
4537
|
+
const inputTokens = Math.max(1, Math.ceil(estimateTextTokens(this.inputChars, 0)));
|
|
4466
4538
|
return {
|
|
4467
|
-
inputTokens
|
|
4468
|
-
outputTokens
|
|
4539
|
+
inputTokens,
|
|
4540
|
+
outputTokens,
|
|
4541
|
+
...reasoning,
|
|
4469
4542
|
cacheReadTokens: 0,
|
|
4470
4543
|
cacheWriteTokens: 0,
|
|
4471
|
-
totalTokens:
|
|
4544
|
+
totalTokens: inputTokens + outputTokens
|
|
4472
4545
|
};
|
|
4473
4546
|
}
|
|
4474
4547
|
endTurn(reason) {
|
package/lib/types/codebuddy.d.ts
CHANGED
|
@@ -208,7 +208,26 @@ export declare class CodeBuddySession {
|
|
|
208
208
|
private callCounter;
|
|
209
209
|
private readonly callNonce;
|
|
210
210
|
private lastUsage;
|
|
211
|
-
|
|
211
|
+
/**
|
|
212
|
+
* The last prompt-side buckets a request actually measured.
|
|
213
|
+
*
|
|
214
|
+
* Kept across turns: a turn can end without a usable usage frame (aborted
|
|
215
|
+
* before the first delta, or a transport that only sends the cumulative
|
|
216
|
+
* result). Reusing the last real reading keeps the host's context pressure
|
|
217
|
+
* at the provider's actual prompt instead of collapsing it to the CLI's
|
|
218
|
+
* startup overhead.
|
|
219
|
+
*/
|
|
220
|
+
private lastPromptUsage;
|
|
221
|
+
/**
|
|
222
|
+
* Ids of assistant messages whose streaming events this session consumed.
|
|
223
|
+
*
|
|
224
|
+
* Kept across turns, not reset with the turn: the echo of a tool round is
|
|
225
|
+
* delivered on the next stream(), so the previous turn's id is exactly the
|
|
226
|
+
* one that has to be recognised. Only the last few ids matter, and bounding
|
|
227
|
+
* the list keeps a long-lived session from growing without limit.
|
|
228
|
+
*/
|
|
229
|
+
private readonly streamedMessageIds;
|
|
230
|
+
private readonly streamedMessageOrder;
|
|
212
231
|
private readonly output;
|
|
213
232
|
private inputChars;
|
|
214
233
|
private readonly toolResultTimeoutMs;
|
|
@@ -263,6 +282,8 @@ export declare class CodeBuddySession {
|
|
|
263
282
|
private canUseTool;
|
|
264
283
|
private consume;
|
|
265
284
|
private handle;
|
|
285
|
+
/** Record that one assistant message reached the host as streaming events. */
|
|
286
|
+
private rememberStreamedMessage;
|
|
266
287
|
private resetTurnState;
|
|
267
288
|
private emit;
|
|
268
289
|
private countOutput;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "dsh-agent-sdk-bridge",
|
|
3
3
|
"displayName": "Agent SDK Bridge",
|
|
4
4
|
"description": "DeepSeek Harness LLM adapter bridge for Qoder CN, Qoder International, Tencent CodeBuddy and MiniMax Code subscriptions. It reuses each provider's own SDK or official ACP server and CLI login, shows available quota, and exposes per-provider settings pages with independent international and mainland login regions.",
|
|
5
|
-
"version": "1.1.
|
|
5
|
+
"version": "1.1.4",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "k1-h2",
|
package/src/codebuddy.ts
CHANGED
|
@@ -824,6 +824,38 @@ interface PendingTool {
|
|
|
824
824
|
/** Per-turn output accounting shared with the Qoder session. */
|
|
825
825
|
type TurnUsage = { input_tokens: number, output_tokens: number, cache_read_input_tokens?: number | null, cache_creation_input_tokens?: number | null }
|
|
826
826
|
|
|
827
|
+
/** A token count as a non-negative whole number; anything unusable becomes 0. */
|
|
828
|
+
function countOf(value: unknown): number {
|
|
829
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.ceil(value) : 0
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/** Whether one usage frame actually measured a prompt. */
|
|
833
|
+
function measuredPrompt(usage: TurnUsage | undefined): usage is TurnUsage {
|
|
834
|
+
return usage !== undefined
|
|
835
|
+
&& (countOf(usage.input_tokens) > 0
|
|
836
|
+
|| countOf(usage.cache_read_input_tokens) > 0
|
|
837
|
+
|| countOf(usage.cache_creation_input_tokens) > 0)
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Fold one `message_delta` usage into the turn's reading.
|
|
842
|
+
*
|
|
843
|
+
* The delta is cumulative for its own message and carries the final output
|
|
844
|
+
* count. Not every transport repeats the prompt fields on it, so the last
|
|
845
|
+
* measured values are kept rather than letting a later frame erase them.
|
|
846
|
+
*/
|
|
847
|
+
function mergeTurnUsage(previous: TurnUsage | undefined, next: TurnUsage): TurnUsage {
|
|
848
|
+
const output = typeof next.output_tokens === 'number' && Number.isFinite(next.output_tokens)
|
|
849
|
+
? next.output_tokens
|
|
850
|
+
: previous?.output_tokens ?? 0
|
|
851
|
+
return {
|
|
852
|
+
input_tokens: countOf(next.input_tokens) > 0 ? next.input_tokens : previous?.input_tokens ?? 0,
|
|
853
|
+
output_tokens: output,
|
|
854
|
+
cache_read_input_tokens: next.cache_read_input_tokens ?? previous?.cache_read_input_tokens ?? 0,
|
|
855
|
+
cache_creation_input_tokens: next.cache_creation_input_tokens ?? previous?.cache_creation_input_tokens ?? 0,
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
827
859
|
/**
|
|
828
860
|
* One warm CodeBuddy session for a host session id.
|
|
829
861
|
*
|
|
@@ -850,7 +882,26 @@ export class CodeBuddySession {
|
|
|
850
882
|
private callCounter = 0
|
|
851
883
|
private readonly callNonce = randomUUID().slice(0, 8)
|
|
852
884
|
private lastUsage: TurnUsage | undefined
|
|
853
|
-
|
|
885
|
+
/**
|
|
886
|
+
* The last prompt-side buckets a request actually measured.
|
|
887
|
+
*
|
|
888
|
+
* Kept across turns: a turn can end without a usable usage frame (aborted
|
|
889
|
+
* before the first delta, or a transport that only sends the cumulative
|
|
890
|
+
* result). Reusing the last real reading keeps the host's context pressure
|
|
891
|
+
* at the provider's actual prompt instead of collapsing it to the CLI's
|
|
892
|
+
* startup overhead.
|
|
893
|
+
*/
|
|
894
|
+
private lastPromptUsage: { inputTokens: number, cacheReadTokens: number, cacheWriteTokens: number } | undefined
|
|
895
|
+
/**
|
|
896
|
+
* Ids of assistant messages whose streaming events this session consumed.
|
|
897
|
+
*
|
|
898
|
+
* Kept across turns, not reset with the turn: the echo of a tool round is
|
|
899
|
+
* delivered on the next stream(), so the previous turn's id is exactly the
|
|
900
|
+
* one that has to be recognised. Only the last few ids matter, and bounding
|
|
901
|
+
* the list keeps a long-lived session from growing without limit.
|
|
902
|
+
*/
|
|
903
|
+
private readonly streamedMessageIds = new Set<string>()
|
|
904
|
+
private readonly streamedMessageOrder: string[] = []
|
|
854
905
|
private readonly output = new TurnOutputAccounting()
|
|
855
906
|
private inputChars = 0
|
|
856
907
|
private readonly toolResultTimeoutMs: number
|
|
@@ -1056,7 +1107,16 @@ export class CodeBuddySession {
|
|
|
1056
1107
|
const event = message.event
|
|
1057
1108
|
if (event.type === 'message_start') {
|
|
1058
1109
|
const usage = (event.message as { usage?: TurnUsage }).usage
|
|
1059
|
-
|
|
1110
|
+
// CodeBuddy reports all-zero counts here; the real prompt arrives on
|
|
1111
|
+
// message_delta. Only a frame that measured something may replace the
|
|
1112
|
+
// turn's reading, or an interrupted turn would report a zero prompt.
|
|
1113
|
+
if (measuredPrompt(usage)) this.lastUsage = usage
|
|
1114
|
+
// Remember which message this stream belongs to. A tool round ends the
|
|
1115
|
+
// host turn at `message_stop` (waiting for `result` would deadlock:
|
|
1116
|
+
// the CLI only finishes after the host returns the tool result), so
|
|
1117
|
+
// the CLI's `assistant` echo of this message is still buffered and is
|
|
1118
|
+
// delivered on the NEXT stream() — where it must not be re-emitted.
|
|
1119
|
+
this.rememberStreamedMessage((event.message as { id?: unknown }).id)
|
|
1060
1120
|
return
|
|
1061
1121
|
}
|
|
1062
1122
|
if (event.type === 'content_block_start') {
|
|
@@ -1122,7 +1182,7 @@ export class CodeBuddySession {
|
|
|
1122
1182
|
}
|
|
1123
1183
|
if (event.type === 'message_delta') {
|
|
1124
1184
|
const usage = (event as { usage?: TurnUsage }).usage
|
|
1125
|
-
if (usage !== undefined) this.lastUsage = usage
|
|
1185
|
+
if (usage !== undefined) this.lastUsage = mergeTurnUsage(this.lastUsage, usage)
|
|
1126
1186
|
return
|
|
1127
1187
|
}
|
|
1128
1188
|
if (event.type === 'message_stop') {
|
|
@@ -1132,6 +1192,12 @@ export class CodeBuddySession {
|
|
|
1132
1192
|
return
|
|
1133
1193
|
}
|
|
1134
1194
|
if (message.type === 'assistant') {
|
|
1195
|
+
// The CLI sends the assembled message after its streaming events. When
|
|
1196
|
+
// those events already reached the host, this is an echo, not new
|
|
1197
|
+
// content; a replayed echo would print the same reply a second time
|
|
1198
|
+
// (and count its tokens twice).
|
|
1199
|
+
const echoOf = (message.message as { id?: unknown }).id
|
|
1200
|
+
if (typeof echoOf === 'string' && this.streamedMessageIds.has(echoOf)) return
|
|
1135
1201
|
if (this.textBlock === undefined && this.tools.length === 0) {
|
|
1136
1202
|
for (const block of message.message.content) {
|
|
1137
1203
|
if (block.type === 'tool_use') {
|
|
@@ -1150,7 +1216,9 @@ export class CodeBuddySession {
|
|
|
1150
1216
|
return
|
|
1151
1217
|
}
|
|
1152
1218
|
if (message.type === 'result') {
|
|
1153
|
-
|
|
1219
|
+
// `result.usage` is cumulative for the whole inner session (verified:
|
|
1220
|
+
// the inputs of every request so far are summed), so it is never the
|
|
1221
|
+
// current prompt. The per-request reading is the last message_delta's.
|
|
1154
1222
|
if (this.abortPending) {
|
|
1155
1223
|
this.endTurn({ kind: 'aborted', failure: { message: 'CodeBuddy turn aborted by host', code: 'ABORTED' } })
|
|
1156
1224
|
return
|
|
@@ -1172,6 +1240,17 @@ export class CodeBuddySession {
|
|
|
1172
1240
|
}
|
|
1173
1241
|
}
|
|
1174
1242
|
|
|
1243
|
+
/** Record that one assistant message reached the host as streaming events. */
|
|
1244
|
+
private rememberStreamedMessage(id: unknown): void {
|
|
1245
|
+
if (typeof id !== 'string' || id.length === 0 || this.streamedMessageIds.has(id)) return
|
|
1246
|
+
this.streamedMessageIds.add(id)
|
|
1247
|
+
this.streamedMessageOrder.push(id)
|
|
1248
|
+
if (this.streamedMessageOrder.length > 32) {
|
|
1249
|
+
const oldest = this.streamedMessageOrder.shift()
|
|
1250
|
+
if (oldest !== undefined) this.streamedMessageIds.delete(oldest)
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1175
1254
|
private resetTurnState(): void {
|
|
1176
1255
|
this.blockIndex = 0
|
|
1177
1256
|
this.textBlock = undefined
|
|
@@ -1181,7 +1260,6 @@ export class CodeBuddySession {
|
|
|
1181
1260
|
this.inputChars = 0
|
|
1182
1261
|
this.output.reset()
|
|
1183
1262
|
this.lastUsage = undefined
|
|
1184
|
-
this.resultUsage = undefined
|
|
1185
1263
|
this.turnEndKind = undefined
|
|
1186
1264
|
}
|
|
1187
1265
|
|
|
@@ -1194,29 +1272,57 @@ export class CodeBuddySession {
|
|
|
1194
1272
|
}
|
|
1195
1273
|
|
|
1196
1274
|
private usage(): TokenUsage {
|
|
1197
|
-
const metered = this.lastUsage
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1275
|
+
const metered = this.lastUsage
|
|
1276
|
+
const local = this.output.usage()
|
|
1277
|
+
// The provider counts output exactly. The character estimate is only for
|
|
1278
|
+
// turns whose stream carried no usage at all (older transports, aborts).
|
|
1279
|
+
const measuredOutput = countOf(metered?.output_tokens)
|
|
1280
|
+
const outputTokens = measuredOutput > 0 ? measuredOutput : Math.max(1, Math.ceil(local.outputTokens))
|
|
1281
|
+
const reasoning = local.reasoningTokens === undefined
|
|
1282
|
+
? {}
|
|
1283
|
+
: { reasoningTokens: Math.min(outputTokens, Math.max(1, Math.ceil(local.reasoningTokens))) }
|
|
1284
|
+
|
|
1285
|
+
if (metered !== undefined && countOf(metered.input_tokens) > 0) {
|
|
1286
|
+
const cacheRead = countOf(metered.cache_read_input_tokens)
|
|
1287
|
+
const cacheWrite = countOf(metered.cache_creation_input_tokens)
|
|
1288
|
+
// `input_tokens` includes cache traffic: CodeBuddy's own modelUsage
|
|
1289
|
+
// reports inputTokens = input_tokens - cache_read (verified live), so
|
|
1290
|
+
// the harness's disjoint buckets are the remainder plus the cache ones.
|
|
1291
|
+
const inputTokens = Math.max(0, countOf(metered.input_tokens) - cacheRead - cacheWrite)
|
|
1292
|
+
this.lastPromptUsage = { inputTokens, cacheReadTokens: cacheRead, cacheWriteTokens: cacheWrite }
|
|
1203
1293
|
return {
|
|
1204
|
-
inputTokens
|
|
1205
|
-
outputTokens
|
|
1206
|
-
...
|
|
1294
|
+
inputTokens,
|
|
1295
|
+
outputTokens,
|
|
1296
|
+
...reasoning,
|
|
1207
1297
|
cacheReadTokens: cacheRead,
|
|
1208
1298
|
cacheWriteTokens: cacheWrite,
|
|
1209
|
-
totalTokens:
|
|
1299
|
+
totalTokens: inputTokens + cacheRead + cacheWrite + outputTokens,
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
if (this.lastPromptUsage !== undefined) {
|
|
1304
|
+
// No usable usage frame this turn. Reuse the provider's last measured
|
|
1305
|
+
// prompt: reporting zero would collapse the context ring, and `result`
|
|
1306
|
+
// is session-cumulative, so it can never stand in for one request.
|
|
1307
|
+
const prompt = this.lastPromptUsage
|
|
1308
|
+
return {
|
|
1309
|
+
inputTokens: prompt.inputTokens,
|
|
1310
|
+
outputTokens,
|
|
1311
|
+
...reasoning,
|
|
1312
|
+
cacheReadTokens: prompt.cacheReadTokens,
|
|
1313
|
+
cacheWriteTokens: prompt.cacheWriteTokens,
|
|
1314
|
+
totalTokens: prompt.inputTokens + prompt.cacheReadTokens + prompt.cacheWriteTokens + outputTokens,
|
|
1210
1315
|
}
|
|
1211
1316
|
}
|
|
1212
|
-
|
|
1213
|
-
const
|
|
1317
|
+
|
|
1318
|
+
const inputTokens = Math.max(1, Math.ceil(estimateTextTokens(this.inputChars, 0)))
|
|
1214
1319
|
return {
|
|
1215
|
-
inputTokens
|
|
1216
|
-
outputTokens
|
|
1320
|
+
inputTokens,
|
|
1321
|
+
outputTokens,
|
|
1322
|
+
...reasoning,
|
|
1217
1323
|
cacheReadTokens: 0,
|
|
1218
1324
|
cacheWriteTokens: 0,
|
|
1219
|
-
totalTokens:
|
|
1325
|
+
totalTokens: inputTokens + outputTokens,
|
|
1220
1326
|
}
|
|
1221
1327
|
}
|
|
1222
1328
|
|