mixdog 1.4.3 → 1.4.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/package.json +1 -1
- package/src/defaults/shipped-history.json +4 -0
- package/src/defaults/skills/computer-use/SKILL.md +14 -3
- package/src/defaults/skills/memory-management/SKILL.md +10 -5
- package/src/defaults/skills/setup/references/actions.md +1 -1
- package/src/rules/agent-mode/MIXY.md +5 -3
- package/src/runtime/agent/orchestrator/providers/admission-scheduler.mjs +2 -1
- package/src/runtime/agent/orchestrator/providers/anthropic-api-request.mjs +11 -1
- package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-events/content-block-start.mjs +4 -0
- package/src/runtime/agent/orchestrator/providers/anthropic-sse-turn/turn-outcome.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/anthropic-turn-result.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/anthropic.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/custom-provider-config.mjs +121 -9
- package/src/runtime/agent/orchestrator/providers/custom-provider.mjs +4 -1
- package/src/runtime/agent/orchestrator/providers/lib/generation-window.mjs +9 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-chat-send.mjs +9 -1
- package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-result.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-chat-stream/stream-state.mjs +5 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-options.mjs +3 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-response-normalization.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/openai-compat-responses.mjs +10 -1
- package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-events.mjs +3 -0
- package/src/runtime/agent/orchestrator/providers/openai-http-sse-response-state/response-outcome.mjs +2 -0
- package/src/runtime/agent/orchestrator/providers/openai-ws-response-state.mjs +9 -2
- package/src/runtime/agent/orchestrator/providers/openrouter-provider-routing.mjs +28 -0
- package/src/runtime/agent/orchestrator/providers/openrouter.mjs +4 -1
- package/src/runtime/agent/orchestrator/providers/registry.mjs +30 -25
- package/src/runtime/agent/orchestrator/runtime-core/ask-user-result.mjs +66 -0
- package/src/runtime/agent/orchestrator/session/manager/runtime-liveness.mjs +8 -1
- package/src/runtime/agent/orchestrator/session/store.mjs +12 -6
- package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-output-silence.mjs +64 -0
- package/src/runtime/agent/orchestrator/tools/builtin/lib/shell-task-output.mjs +20 -6
- package/src/runtime/agent/orchestrator/tools/builtin/task-tool.mjs +12 -1
- package/src/runtime/channels/lib/routines-db.mjs +35 -0
- package/src/runtime/channels/lib/schedules-db.mjs +4 -0
- package/src/runtime/channels/lib/webhooks-db.mjs +4 -0
- package/src/runtime/computer-bridge/action-schema.mjs +2 -2
- package/src/runtime/computer-bridge/core-actions.mjs +16 -2
- package/src/runtime/computer-bridge/result-canonical.mjs +6 -0
- package/src/runtime/memory/tool-defs.mjs +1 -1
- package/src/runtime/shared/llm/usage-accounting.mjs +110 -19
- package/src/runtime/shared/llm/usage-context.mjs +1 -1
- package/src/runtime/shared/llm/usage-ledger-quota.mjs +2 -0
- package/src/runtime/shared/llm/usage-ledger-requests.mjs +183 -0
- package/src/runtime/shared/llm/usage-ledger-rollup.mjs +7 -3
- package/src/runtime/shared/llm/usage-ledger-worker.mjs +2 -0
- package/src/runtime/shared/llm/usage-ledger.mjs +57 -1
- package/src/runtime/shared/llm/usage-request-status.mjs +48 -0
- package/src/runtime/shared/tool-result-summary.mjs +1 -1
- package/src/session-runtime/agent-home-delivery.mjs +76 -8
- package/src/session-runtime/agent-task-sessions.mjs +63 -22
- package/src/session-runtime/boot/apis.mjs +15 -0
- package/src/session-runtime/boot/tools.mjs +8 -2
- package/src/session-runtime/internal-tool-executor/ask-user-tool.mjs +3 -58
- package/src/session-runtime/model-route/route-persist.mjs +12 -4
- package/src/session-runtime/provider-models/row-hydration.mjs +2 -0
- package/src/session-runtime/routine-owner.mjs +9 -3
- package/src/session-runtime/services/agent-task-status.mjs +29 -1
- package/src/session-runtime/services/agent-tool/job-views/session-progress.mjs +3 -0
- package/src/session-runtime/services/agent-tool/render.mjs +6 -1
- package/src/session-runtime/services/channel-admin.mjs +44 -3
- package/src/session-runtime/services/plugin-admin.mjs +46 -7
- package/src/session-runtime/services/provider-admin.mjs +5 -0
- package/src/session-runtime/setup-tool/agent-settings-actions.mjs +1 -1
- package/src/session-runtime/setup-tool/executor.mjs +1 -1
- package/src/session-runtime/setup-tool/extended-actions.mjs +1 -1
- package/src/session-runtime/setup-tool/settings-contract.mjs +2 -2
- package/src/session-runtime/usage-stats-api.mjs +117 -1
- package/src/session-runtime/workflow-agents-api/agent-connectors.mjs +36 -29
- package/src/session-runtime/workflow-agents-api/agent-editor.mjs +183 -23
- package/src/standalone/session-protocol.mjs +1 -0
- package/src/standalone/session-service/retention/busy.mjs +3 -2
- package/src/tui/app/model-picker/model-footer.mjs +1 -1
- package/src/tui/app/model-picker/provider-models-list.mjs +7 -3
- package/src/tui/app/model-picker/route-selection/fast-parameters.mjs +23 -3
- package/src/tui/app/model-picker/route-selection.mjs +1 -0
- package/src/tui/dist/index.mjs +41 -7
- package/src/tui/session/session-api/integrations.mjs +3 -0
- package/src/tui/session/tool-approval-settle.mjs +24 -4
- package/src/tui/session/tool-approval.mjs +35 -10
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { getUsageLedger, makeUsageRecord } from './usage-ledger.mjs';
|
|
3
|
-
import { isNotedUsage, withUsageContext } from './usage-context.mjs';
|
|
3
|
+
import { currentUsageContext, isNotedUsage, withUsageContext } from './usage-context.mjs';
|
|
4
|
+
import { servedModelSubstituted, usageRequestStatus } from './usage-request-status.mjs';
|
|
4
5
|
import { ACCOUNT_PROVIDERS } from '../provider-accounts.mjs';
|
|
5
6
|
import { currentProviderAccountId } from '../provider-auth-binding.mjs';
|
|
6
7
|
|
|
@@ -8,7 +9,31 @@ import { currentProviderAccountId } from '../provider-auth-binding.mjs';
|
|
|
8
9
|
// fallback, catalog retry) runs its own accounting and its result or error
|
|
9
10
|
// then returns through the enclosing send, which must not write it again.
|
|
10
11
|
const recorded = new WeakSet();
|
|
12
|
+
// The accounting identity of the send whose options carry it; object spread
|
|
13
|
+
// keeps it on every option set a provider derives for its own re-send.
|
|
14
|
+
const SEND_IDENTITY = Symbol('usageSendIdentity');
|
|
15
|
+
// Providers whose answering stream's generation window (`generationMs`,
|
|
16
|
+
// providers/lib/generation-window.mjs) starts when generation does, read
|
|
17
|
+
// from the stream code: Anthropic opens every content block, a thinking
|
|
18
|
+
// block included even when its text is omitted; OpenAI's Responses streams
|
|
19
|
+
// add every output item, an encrypted reasoning item included, as it starts;
|
|
20
|
+
// the local llama.cpp server streams every generated token, reasoning
|
|
21
|
+
// included. Any other provider may reason before its first streamed token
|
|
22
|
+
// (hidden reasoning on chat-compat gateways, Gemini thought summaries,
|
|
23
|
+
// Cursor, Devin, compat Responses gateways), so a speed from it could be
|
|
24
|
+
// inflated: it records no first-output time.
|
|
25
|
+
const TIMED_PROVIDERS = new Set(['anthropic', 'anthropic-oauth', 'openai', 'openai-oauth', 'mixdog-local']);
|
|
26
|
+
|
|
27
|
+
/** The answering stream's own first-output time within the request, when its
|
|
28
|
+
* provider is timed: never an abandoned attempt's or a retry backoff's. */
|
|
29
|
+
function firstOutputMs(provider, result, streamMs) {
|
|
30
|
+
const generationMs = Number(result?.generationMs);
|
|
31
|
+
if (!TIMED_PROVIDERS.has(provider) || !Number.isFinite(generationMs)) return null;
|
|
32
|
+
if (generationMs < 0 || generationMs > streamMs) return null;
|
|
33
|
+
return streamMs - generationMs;
|
|
34
|
+
}
|
|
11
35
|
const UNMEASURED_USAGE = Object.freeze({ inputTokens: null, inputTokensKnown: false });
|
|
36
|
+
const FAILED_USAGE = Object.freeze({ inputTokens: 0, outputTokens: 0, costUsd: 0 });
|
|
12
37
|
const hasTokens = (usage) =>
|
|
13
38
|
['inputTokens', 'outputTokens', 'cachedTokens', 'cacheWriteTokens'].some((key) => Number(usage?.[key]) > 0);
|
|
14
39
|
// One rule for every reported usage object: tokens, or a provider-reported
|
|
@@ -41,17 +66,24 @@ export async function accountProviderSend(provider, instance, send, model, opts
|
|
|
41
66
|
} catch (error) {
|
|
42
67
|
openingError = error;
|
|
43
68
|
}
|
|
69
|
+
// A provider's own re-send on another model (an overload or retired-model
|
|
70
|
+
// fallback) still answers the model the outer send was asked for. Only a
|
|
71
|
+
// re-send built from the outer send's own options is one: any other send
|
|
72
|
+
// that merely runs inside that async context (a tool or agent started
|
|
73
|
+
// while the outer stream was live) asks for its own model.
|
|
74
|
+
const outer = currentUsageContext();
|
|
44
75
|
const identity = {
|
|
45
76
|
provider,
|
|
46
|
-
requestedModel: model,
|
|
77
|
+
requestedModel: (outer && opts[SEND_IDENTITY] === outer && outer.requestedModel) || model,
|
|
47
78
|
sessionId,
|
|
48
79
|
sourceType,
|
|
49
80
|
inputTokensInclusive,
|
|
50
81
|
};
|
|
82
|
+
const sendOpts = { ...opts, [SEND_IDENTITY]: identity };
|
|
51
83
|
// `timed`: only a send's completed result owns the request's wall time. An
|
|
52
84
|
// abandoned attempt or a failure shares that clock with the final attempt,
|
|
53
85
|
// so timing it too would count the same seconds twice and understate speed.
|
|
54
|
-
const record = async (result, id, owner, timed) => {
|
|
86
|
+
const record = async (result, id, owner, timed, outcome = null) => {
|
|
55
87
|
// A completed call that reported no usage is still a call: it is recorded
|
|
56
88
|
// as unmeasured (unknown input, unpriced) instead of vanishing from totals.
|
|
57
89
|
if (!result?.usage && !(timed && result && typeof result === 'object')) return;
|
|
@@ -64,6 +96,21 @@ export async function accountProviderSend(provider, instance, send, model, opts
|
|
|
64
96
|
recorded.add(owner);
|
|
65
97
|
const usage = result.usage || UNMEASURED_USAGE;
|
|
66
98
|
const durationMs = timed ? Date.now() - startedAt : 0;
|
|
99
|
+
const servedModel = result.model || model;
|
|
100
|
+
const attempts = outcome ? outcome.attempts : 1;
|
|
101
|
+
let status = 'completed';
|
|
102
|
+
if (outcome?.cancelled) status = 'cancelled';
|
|
103
|
+
else if (outcome)
|
|
104
|
+
status = usageRequestStatus({
|
|
105
|
+
failed: outcome.failed,
|
|
106
|
+
attempts,
|
|
107
|
+
substituted: servedModelSubstituted({
|
|
108
|
+
provider,
|
|
109
|
+
requestedModel: identity.requestedModel,
|
|
110
|
+
servedModel,
|
|
111
|
+
fallback: Boolean(result.providerMetadata?.anthropicFallbacks?.length),
|
|
112
|
+
}),
|
|
113
|
+
});
|
|
67
114
|
// The one measured request time: the loop's speed figures read it, so
|
|
68
115
|
// the composer and the ledger show the same span.
|
|
69
116
|
if (timed && result && typeof result === 'object') {
|
|
@@ -73,8 +120,15 @@ export async function accountProviderSend(provider, instance, send, model, opts
|
|
|
73
120
|
id: result.responseId ? undefined : id,
|
|
74
121
|
ts: Date.now(),
|
|
75
122
|
provider,
|
|
76
|
-
model:
|
|
77
|
-
requestedModel: model,
|
|
123
|
+
model: servedModel,
|
|
124
|
+
requestedModel: outcome ? identity.requestedModel : model,
|
|
125
|
+
status,
|
|
126
|
+
attempts,
|
|
127
|
+
// Measured to the moment the stream returned, not to this write: the
|
|
128
|
+
// abandoned attempts' writes awaited before it are not first-output wait.
|
|
129
|
+
ttftMs: timed ? firstOutputMs(provider, result, streamEndedAt - startedAt) : null,
|
|
130
|
+
// The request log shows how long a failure took; statistics never time it.
|
|
131
|
+
elapsedMs: outcome?.failed ? Date.now() - startedAt : 0,
|
|
78
132
|
pricingModel: result.pricingModel,
|
|
79
133
|
sessionId,
|
|
80
134
|
// The session this one runs for (an agent's Lead), kept with its spend.
|
|
@@ -101,34 +155,71 @@ export async function accountProviderSend(provider, instance, send, model, opts
|
|
|
101
155
|
// SQLite write no longer runs on the event loop.
|
|
102
156
|
await ledger.recordQueued(row);
|
|
103
157
|
};
|
|
104
|
-
const save = async (result, id = requestId, owner = result, timed = false) => {
|
|
158
|
+
const save = async (result, id = requestId, owner = result, timed = false, outcome = null) => {
|
|
105
159
|
try {
|
|
106
|
-
await record(result, id, owner, timed);
|
|
160
|
+
await record(result, id, owner, timed, outcome);
|
|
107
161
|
} catch (error) {
|
|
108
162
|
result.usageAccountingError = String(error?.message || error);
|
|
109
163
|
process.stderr.write(`[usage-ledger] RECORD NOT SAVED: ${result.usageAccountingError}\n`);
|
|
110
164
|
}
|
|
111
165
|
};
|
|
112
|
-
const saveAbandoned = async () => {
|
|
166
|
+
const saveAbandoned = async (skip = -1) => {
|
|
113
167
|
for (const [index, attempt] of (identity.abandonedUsage || []).entries()) {
|
|
114
|
-
if (recordable(attempt.usage)) await save(attempt, `${requestId}:abandoned:${index}`);
|
|
168
|
+
if (index !== skip && recordable(attempt.usage)) await save(attempt, `${requestId}:abandoned:${index}`);
|
|
115
169
|
}
|
|
116
170
|
};
|
|
117
171
|
let result;
|
|
172
|
+
let streamEndedAt;
|
|
118
173
|
try {
|
|
119
|
-
result = await withUsageContext(identity, send);
|
|
174
|
+
result = await withUsageContext(identity, () => send(sendOpts));
|
|
175
|
+
streamEndedAt = Date.now();
|
|
120
176
|
} catch (error) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
177
|
+
const abandoned = identity.abandonedUsage || [];
|
|
178
|
+
const cancelled = opts.signal?.aborted === true || error?.name === 'AbortError';
|
|
179
|
+
// Only provider-reported usage is recordable; never invent tokens for a
|
|
180
|
+
// failed request or reinterpret an error as a success.
|
|
181
|
+
const reported = error?.usage || (recordable(error?.partialUsage) ? error.partialUsage : null);
|
|
182
|
+
if (cancelled) {
|
|
183
|
+
// A user's cancel is not a request outcome: it records exactly what it
|
|
184
|
+
// always did, under the same ids (noted attempts as abandoned rows, the
|
|
185
|
+
// cancel's own unnoted usage as the request's row, response-keyed when
|
|
186
|
+
// it names a response), and that row is marked `cancelled` so the
|
|
187
|
+
// request log leaves it out.
|
|
188
|
+
await saveAbandoned();
|
|
189
|
+
const outcome = { cancelled: true, attempts: abandoned.length + 1 };
|
|
190
|
+
if (error?.usage) {
|
|
191
|
+
if (!isNotedUsage(error.usage)) await save(error, requestId, error, false, outcome);
|
|
192
|
+
} else if (reported && !isNotedUsage(reported)) {
|
|
193
|
+
await save({ usage: reported, model: error.partialModel }, requestId, error, false, outcome);
|
|
194
|
+
}
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
// The failing attempt's usage may already be noted as abandoned (the
|
|
198
|
+
// error carries that same usage): that attempt is the request's own row,
|
|
199
|
+
// not an abandoned one. Any other failure without usage is an attempt of
|
|
200
|
+
// its own.
|
|
201
|
+
const last = reported
|
|
202
|
+
? abandoned.findLastIndex((attempt) => attempt.source === reported || attempt.usage === reported)
|
|
203
|
+
: -1;
|
|
204
|
+
await saveAbandoned(last);
|
|
205
|
+
const failure = { failed: true, attempts: last >= 0 ? abandoned.length : abandoned.length + 1 };
|
|
206
|
+
const owner = error && typeof error === 'object' ? error : {};
|
|
207
|
+
if (last >= 0) {
|
|
208
|
+
const attempt = abandoned[last];
|
|
209
|
+
// Owned by the attempt, as when it was stored as abandoned: one error
|
|
210
|
+
// may end several concurrent sends (a shared cancel reason).
|
|
211
|
+
await save({ usage: attempt.usage, model: attempt.model || error?.model }, requestId, attempt, false, failure);
|
|
212
|
+
} else if (reported) {
|
|
213
|
+
// Usage noted by a nested send was recorded there.
|
|
214
|
+
const failed = error.usage ? error : { usage: reported, model: error.partialModel };
|
|
215
|
+
if (!isNotedUsage(reported)) await save(failed, requestId, owner, false, failure);
|
|
216
|
+
} else {
|
|
217
|
+
// A failure that reported nothing is still a request: zero tokens.
|
|
218
|
+
await save({ usage: FAILED_USAGE, model: error?.model }, requestId, owner, false, failure);
|
|
219
|
+
}
|
|
129
220
|
throw error;
|
|
130
221
|
}
|
|
131
222
|
await saveAbandoned();
|
|
132
|
-
await save(result, requestId, result, true);
|
|
223
|
+
await save(result, requestId, result, true, { failed: false, attempts: (identity.abandonedUsage?.length || 0) + 1 });
|
|
133
224
|
return result;
|
|
134
225
|
}
|
|
@@ -25,5 +25,5 @@ export const noteAbandonedUsage = (usage, model) => {
|
|
|
25
25
|
notedUsage.add(usage);
|
|
26
26
|
notedUsage.add(stored);
|
|
27
27
|
identity.abandonedUsage ||= [];
|
|
28
|
-
identity.abandonedUsage.push({ usage: stored, model });
|
|
28
|
+
identity.abandonedUsage.push({ usage: stored, model, source: usage });
|
|
29
29
|
};
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* is an estimate: providers do not publish what a request costs the meter.
|
|
14
14
|
*/
|
|
15
15
|
import { normalizeUsageMeasurement } from './usage-measurement.mjs';
|
|
16
|
+
import { NO_USAGE_COST_SOURCE } from './usage-ledger-requests.mjs';
|
|
16
17
|
import { FAST_KEY, fastRouteSql, sortFastBesideModel, usageRollupDayKey } from './usage-rollup.mjs';
|
|
17
18
|
import { estimateOutsideQuota, estimateQuotaValue, outsideSpans } from './quota-value-estimate.mjs';
|
|
18
19
|
|
|
@@ -401,6 +402,7 @@ function readQuotaEvents(db, { provider, account, label = '', fromMs, toMs }) {
|
|
|
401
402
|
${fastRouteSql('signature')} AS fast
|
|
402
403
|
FROM usage_routes
|
|
403
404
|
WHERE json_extract(signature,'$[0]')=? AND COALESCE(json_extract(signature,'$[8]'),'') IN (?,?)
|
|
405
|
+
AND json_extract(signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'
|
|
404
406
|
)
|
|
405
407
|
SELECT e.ts,e.day,r.model,r.rank,COUNT(*) AS turns,
|
|
406
408
|
SUM(e.input) AS input,SUM(e.output) AS output,SUM(e.cache_read) AS cacheRead,
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The request log: individual requests, newest first, a page at a time. The
|
|
3
|
+
* ledger keeps every record forever, so each page is read through an index
|
|
4
|
+
* ordered by time (all requests, one status, or one route) and stops after
|
|
5
|
+
* the page: a page of "all history" costs what a page of today does.
|
|
6
|
+
*
|
|
7
|
+
* The request facts (requested model, status, attempts) sit in
|
|
8
|
+
* `usage_requests` and the first-output time in `usage_request_first_output`,
|
|
9
|
+
* beside `usage_events` and keyed by the record id, so the record table keeps
|
|
10
|
+
* the exact shape every earlier build writes: an older app still opens and
|
|
11
|
+
* writes a ledger this one touched. A record without a request row is a
|
|
12
|
+
* completed single attempt (every record from before).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const REQUEST_STATUSES = ['completed', 'substituted', 'retried', 'failed'];
|
|
16
|
+
// `cancelled`: usage of a user's cancel, kept under its own record id (and so
|
|
17
|
+
// response-deduplicated) but never listed: a cancel is not a request outcome.
|
|
18
|
+
export const STORED_REQUEST_STATUSES = [...REQUEST_STATUSES, 'cancelled'];
|
|
19
|
+
|
|
20
|
+
// Almost every request completes: the status index holds only the others,
|
|
21
|
+
// and "completed" is read off the time index, where nearly every row matches.
|
|
22
|
+
// The model index lets a model filter find its routes without a scan.
|
|
23
|
+
export const REQUEST_LOG_SCHEMA = `
|
|
24
|
+
CREATE TABLE IF NOT EXISTS usage_requests (
|
|
25
|
+
id BLOB PRIMARY KEY, ts INTEGER NOT NULL, requested_model TEXT, status TEXT NOT NULL,
|
|
26
|
+
attempts INTEGER NOT NULL, ttft_ms REAL, elapsed_ms REAL
|
|
27
|
+
) WITHOUT ROWID;
|
|
28
|
+
CREATE TABLE IF NOT EXISTS usage_request_first_output (
|
|
29
|
+
id BLOB PRIMARY KEY, ttft_ms REAL NOT NULL
|
|
30
|
+
) WITHOUT ROWID;
|
|
31
|
+
CREATE INDEX IF NOT EXISTS usage_requests_status_time ON usage_requests(status, ts) WHERE status<>'completed';
|
|
32
|
+
CREATE INDEX IF NOT EXISTS usage_events_route_time ON usage_events(route, ts);
|
|
33
|
+
CREATE INDEX IF NOT EXISTS usage_routes_model ON usage_routes(json_extract(signature,'$[1]'));
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
// The cost source of a failure that reported no usage: the request log lists
|
|
37
|
+
// it, but statistics and quota totals add no turn for it.
|
|
38
|
+
export const NO_USAGE_COST_SOURCE = 'none';
|
|
39
|
+
|
|
40
|
+
const DEFAULT_PAGE = 50;
|
|
41
|
+
const MAX_PAGE = 200;
|
|
42
|
+
// A provider-local retry's abandoned attempts are stored as
|
|
43
|
+
// `<requestId>:abandoned:N` (UTF-8 ids carry tag 0x01). They are part of
|
|
44
|
+
// their request, counted in its attempts, never a request of their own.
|
|
45
|
+
const NOT_ABANDONED = `NOT (substr(e.id,1,1)=X'01' AND instr(e.id, CAST(':abandoned:' AS BLOB))>0)`;
|
|
46
|
+
// The first-output time is read only from usage_request_first_output, which
|
|
47
|
+
// only a build that times the answering stream's first output of any kind
|
|
48
|
+
// writes. `usage_requests.ttft_ms` is never read: the build that added it
|
|
49
|
+
// (e544d62a5) timed visible text only, so a thinking or tool turn read as
|
|
50
|
+
// generated in a few milliseconds, and an older process may still write it.
|
|
51
|
+
const COLUMNS = `e.id,e.ts,e.route,e.session,e.input,e.output,e.cache_read,e.cache_write,e.cost_usd,e.duration_ms,
|
|
52
|
+
r.requested_model,r.status,r.attempts,r.elapsed_ms,
|
|
53
|
+
(SELECT f.ttft_ms FROM usage_request_first_output f WHERE f.id=e.id) AS first_output_ms`;
|
|
54
|
+
const COMPLETED = `(r.status IS NULL OR r.status='completed')`;
|
|
55
|
+
// Checked on the row the time-ordered read already joined: no other index.
|
|
56
|
+
const LISTED = `(r.status IS NULL OR r.status<>'cancelled')`;
|
|
57
|
+
|
|
58
|
+
function decodeCursor(cursor) {
|
|
59
|
+
const match = typeof cursor === 'string' ? /^(\d+):([0-9a-f]+)$/.exec(cursor) : null;
|
|
60
|
+
return match ? { ts: Number(match[1]), id: Buffer.from(match[2], 'hex') } : null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const encodeCursor = (row) => `${row.ts}:${Buffer.from(row.id).toString('hex')}`;
|
|
64
|
+
|
|
65
|
+
const newestFirst = (a, b) => b.ts - a.ts || Buffer.compare(Buffer.from(b.id), Buffer.from(a.id));
|
|
66
|
+
|
|
67
|
+
/** The SQL of one page; `source` is the table whose index orders the read. */
|
|
68
|
+
export function requestPageQuery({ source, cursor = false, completed = false, routes = 0 }) {
|
|
69
|
+
const alias = source === 'status' ? 'r' : 'e';
|
|
70
|
+
const from =
|
|
71
|
+
source === 'status'
|
|
72
|
+
? 'usage_requests r INDEXED BY usage_requests_status_time CROSS JOIN usage_events e ON e.id=r.id'
|
|
73
|
+
: `usage_events e INDEXED BY ${source === 'route' ? 'usage_events_route_time' : 'usage_events_time'}
|
|
74
|
+
LEFT JOIN usage_requests r ON r.id=e.id`;
|
|
75
|
+
const where = [
|
|
76
|
+
...(source === 'status' ? [`r.status=? AND r.status<>'completed'`] : []),
|
|
77
|
+
...(source === 'route' ? ['e.route=?'] : []),
|
|
78
|
+
`${alias}.ts>=?`,
|
|
79
|
+
`${alias}.ts<=?`,
|
|
80
|
+
...(cursor ? [`(${alias}.ts<? OR ${alias}.id<?)`] : []),
|
|
81
|
+
NOT_ABANDONED,
|
|
82
|
+
...(source !== 'status' ? [completed ? COMPLETED : LISTED] : []),
|
|
83
|
+
...(routes ? [`e.route IN (${Array(routes).fill('?').join(',')})`] : []),
|
|
84
|
+
];
|
|
85
|
+
return `SELECT ${COLUMNS} FROM ${from} WHERE ${where.join(' AND ')}
|
|
86
|
+
ORDER BY ${alias}.ts DESC, ${alias}.id DESC LIMIT ?`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* `{ fromMs, toMs, status?, model?, cursor?, limit? }` → `{ requests,
|
|
91
|
+
* nextCursor, models }`. `model` is the model that answered (the route's).
|
|
92
|
+
* `models`, every model that answered within the period, comes with the
|
|
93
|
+
* first page only (no cursor): it is the one read that visits every route.
|
|
94
|
+
*/
|
|
95
|
+
export function readUsageRequests(db, options = {}) {
|
|
96
|
+
const fromMs = Number(options.fromMs) || 0;
|
|
97
|
+
const toMs = Number.isFinite(Number(options.toMs)) ? Number(options.toMs) : Date.now();
|
|
98
|
+
const requested = Math.floor(Number(options.limit));
|
|
99
|
+
const limit = requested > 0 ? Math.min(requested, MAX_PAGE) : DEFAULT_PAGE;
|
|
100
|
+
const status = REQUEST_STATUSES.includes(options.status) ? options.status : '';
|
|
101
|
+
const cursor = decodeCursor(options.cursor);
|
|
102
|
+
const range = [fromMs, cursor ? Math.min(toMs, cursor.ts) : toMs, ...(cursor ? [cursor.ts, cursor.id] : [])];
|
|
103
|
+
const modelRoutes = options.model
|
|
104
|
+
? db
|
|
105
|
+
.prepare(`SELECT id FROM usage_routes WHERE json_extract(signature,'$[1]')=?`)
|
|
106
|
+
.all(String(options.model))
|
|
107
|
+
.map((route) => route.id)
|
|
108
|
+
: null;
|
|
109
|
+
const run = (shape, params) =>
|
|
110
|
+
db.prepare(requestPageQuery({ cursor: Boolean(cursor), ...shape })).all(...params, limit + 1);
|
|
111
|
+
let rows;
|
|
112
|
+
if (modelRoutes && !modelRoutes.length) rows = [];
|
|
113
|
+
else if (status && status !== 'completed') {
|
|
114
|
+
// Rare statuses: their own index, the model's routes filtered within it.
|
|
115
|
+
rows = run({ source: 'status', routes: modelRoutes?.length || 0 }, [status, ...range, ...(modelRoutes || [])]);
|
|
116
|
+
} else if (modelRoutes) {
|
|
117
|
+
// One ordered read per route of that model, merged: never every record of it.
|
|
118
|
+
rows = modelRoutes
|
|
119
|
+
.flatMap((route) => run({ source: 'route', completed: Boolean(status) }, [route, ...range]))
|
|
120
|
+
.sort(newestFirst)
|
|
121
|
+
.slice(0, limit + 1);
|
|
122
|
+
} else {
|
|
123
|
+
rows = run({ source: 'time', completed: Boolean(status) }, range);
|
|
124
|
+
}
|
|
125
|
+
const more = rows.length > limit;
|
|
126
|
+
rows = rows.slice(0, limit);
|
|
127
|
+
|
|
128
|
+
const routeOf = db.prepare(
|
|
129
|
+
`SELECT json_extract(signature,'$[0]') AS provider,json_extract(signature,'$[1]') AS model,
|
|
130
|
+
json_extract(signature,'$[2]') AS kind,json_extract(signature,'$[3]') AS source_type,
|
|
131
|
+
json_extract(signature,'$[4]') AS cost_source
|
|
132
|
+
FROM usage_routes WHERE id=?`
|
|
133
|
+
);
|
|
134
|
+
const sessionOf = db.prepare('SELECT value FROM usage_sessions WHERE id=?');
|
|
135
|
+
const ownerOf = db.prepare(
|
|
136
|
+
'SELECT s.value FROM usage_session_owners o JOIN usage_sessions s ON s.id=o.owner WHERE o.session=?'
|
|
137
|
+
);
|
|
138
|
+
const routes = new Map();
|
|
139
|
+
const requests = rows.map((row) => {
|
|
140
|
+
if (!routes.has(row.route)) routes.set(row.route, routeOf.get(row.route) || {});
|
|
141
|
+
const route = routes.get(row.route);
|
|
142
|
+
const rowStatus = row.status || 'completed';
|
|
143
|
+
const durationMs = row.duration_ms || row.elapsed_ms || 0;
|
|
144
|
+
const ttftMs = row.first_output_ms;
|
|
145
|
+
return {
|
|
146
|
+
id: Buffer.from(row.id).toString('hex'),
|
|
147
|
+
ts: row.ts,
|
|
148
|
+
provider: route.provider || '',
|
|
149
|
+
model: route.model || '',
|
|
150
|
+
requestedModel: row.requested_model || route.model || '',
|
|
151
|
+
kind: route.kind || '',
|
|
152
|
+
sourceType: route.source_type || '',
|
|
153
|
+
costSource: route.cost_source || '',
|
|
154
|
+
sessionId: sessionOf.get(row.session)?.value || '',
|
|
155
|
+
ownerSessionId: ownerOf.get(row.session)?.value || '',
|
|
156
|
+
input: row.input,
|
|
157
|
+
output: row.output,
|
|
158
|
+
cacheRead: row.cache_read,
|
|
159
|
+
cacheWrite: row.cache_write,
|
|
160
|
+
tokens: row.input + row.output + row.cache_read + row.cache_write,
|
|
161
|
+
costUsd: row.cost_usd,
|
|
162
|
+
durationMs,
|
|
163
|
+
ttftMs: ttftMs ?? null,
|
|
164
|
+
attempts: row.attempts || 1,
|
|
165
|
+
status: rowStatus,
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
let models = null;
|
|
170
|
+
if (!cursor) {
|
|
171
|
+
// One indexed probe per route: does any request of it fall in the period?
|
|
172
|
+
const probe = db.prepare(
|
|
173
|
+
`SELECT 1 FROM usage_events e INDEXED BY usage_events_route_time LEFT JOIN usage_requests r ON r.id=e.id
|
|
174
|
+
WHERE e.route=? AND e.ts>=? AND e.ts<=? AND ${NOT_ABANDONED} AND ${LISTED} LIMIT 1`
|
|
175
|
+
);
|
|
176
|
+
const found = new Set();
|
|
177
|
+
for (const route of db.prepare(`SELECT id,json_extract(signature,'$[1]') AS model FROM usage_routes`).all()) {
|
|
178
|
+
if (route.model && !found.has(route.model) && probe.get(route.id, fromMs, toMs)) found.add(route.model);
|
|
179
|
+
}
|
|
180
|
+
models = [...found].sort((a, b) => a.localeCompare(b));
|
|
181
|
+
}
|
|
182
|
+
return { requests, nextCursor: more && rows.length ? encodeCursor(rows.at(-1)) : null, models };
|
|
183
|
+
}
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
usageRollupDayKey,
|
|
24
24
|
} from './usage-rollup.mjs';
|
|
25
25
|
import { normalizeUsageMeasurement, normalizeLegacyUsageDay } from './usage-measurement.mjs';
|
|
26
|
+
import { NO_USAGE_COST_SOURCE } from './usage-ledger-requests.mjs';
|
|
26
27
|
|
|
27
28
|
// A detail and its terminal summary can overlap on the SAME route.
|
|
28
29
|
// Another provider/model on that day is independent and must survive.
|
|
@@ -97,7 +98,7 @@ function readHourlyRows(db, { hourlyDay, fromDay, toDay, fromMs, toMs, fromTs, t
|
|
|
97
98
|
.prepare(`
|
|
98
99
|
SELECT e.ts,e.provider,e.source_type,e.input,e.output,e.cache_read,e.cache_write,e.cost_usd
|
|
99
100
|
FROM events e JOIN (${best}) b USING(day,provider,model,rank)
|
|
100
|
-
WHERE e.ts>=? AND e.ts<? ORDER BY e.ts
|
|
101
|
+
WHERE e.ts>=? AND e.ts<? AND e.cost_source<>'${NO_USAGE_COST_SOURCE}' ORDER BY e.ts
|
|
101
102
|
`)
|
|
102
103
|
.all(fromDay, toDay, fromMs == null ? start.getTime() : fromTs, toMs == null ? end.getTime() : toTs)
|
|
103
104
|
.map((row) => {
|
|
@@ -132,7 +133,7 @@ function readDailyAmounts(db, { fromDay, toDay, fromMs, fromTs, toTs }) {
|
|
|
132
133
|
SUM(e.cost_usd) AS cost_usd,SUM(e.duration_ms) AS duration_ms,
|
|
133
134
|
MAX(e.origin!='live') AS imported
|
|
134
135
|
FROM events e JOIN (${best}) b USING(day,provider,model,rank)
|
|
135
|
-
WHERE e.ts>=? AND e.ts<?
|
|
136
|
+
WHERE e.ts>=? AND e.ts<? AND e.cost_source<>'${NO_USAGE_COST_SOURCE}'
|
|
136
137
|
GROUP BY e.day,e.rank,e.provider,e.model,e.kind,e.cost_source,e.source_type
|
|
137
138
|
`)
|
|
138
139
|
.all(fromDay, toDay, fromTs, toTs)
|
|
@@ -178,7 +179,8 @@ function readFastAmounts(db, { fromDay, toDay, fromTs, toTs }) {
|
|
|
178
179
|
json_extract(r.signature,'$[3]') AS source_type,
|
|
179
180
|
json_extract(r.signature,'$[4]') AS cost_source,
|
|
180
181
|
json_extract(r.signature,'$[7]') AS rank
|
|
181
|
-
FROM usage_routes r WHERE ${fastRouteSql('r.signature')}
|
|
182
|
+
FROM usage_routes r WHERE ${fastRouteSql('r.signature')}
|
|
183
|
+
AND json_extract(r.signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'`;
|
|
182
184
|
if (!db.prepare(`${fastRoutes} LIMIT 1`).get()) return [];
|
|
183
185
|
return db
|
|
184
186
|
.prepare(`
|
|
@@ -258,6 +260,7 @@ function readSessionAttribution(db, { fromDay, toDay, fromTs, toTs }) {
|
|
|
258
260
|
s.value AS session_id,e.input,e.output,e.cacheRead,e.cacheWrite
|
|
259
261
|
FROM grouped e JOIN usage_routes r ON r.id=e.route
|
|
260
262
|
JOIN usage_sessions s ON s.id=e.session
|
|
263
|
+
WHERE json_extract(r.signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'
|
|
261
264
|
)
|
|
262
265
|
SELECT e.* FROM attributed e JOIN (${best}) b USING(day,provider,model,rank)
|
|
263
266
|
`)
|
|
@@ -399,6 +402,7 @@ export function readSessionUsage(db, sessionIds, ownerIds = []) {
|
|
|
399
402
|
m.turns,m.input,m.output,m.cache_read,m.cache_write,m.cost_usd,m.duration_ms,
|
|
400
403
|
m.speed_output,m.speed_ms
|
|
401
404
|
FROM mine m JOIN usage_routes r ON r.id=m.route
|
|
405
|
+
WHERE json_extract(r.signature,'$[4]')<>'${NO_USAGE_COST_SOURCE}'
|
|
402
406
|
)
|
|
403
407
|
SELECT a.* FROM attributed a JOIN (${best}) b USING(day,provider,model,rank)
|
|
404
408
|
`)
|
|
@@ -9,6 +9,7 @@ import { serveWorkerRequests } from '../worker-requests.mjs';
|
|
|
9
9
|
import { UsageLedger } from './usage-ledger.mjs';
|
|
10
10
|
import { readSessionUsage, rollupDelta, rollupUsage } from './usage-ledger-rollup.mjs';
|
|
11
11
|
import { readQuotaHistory, readQuotaWindows } from './usage-ledger-quota.mjs';
|
|
12
|
+
import { readUsageRequests } from './usage-ledger-requests.mjs';
|
|
12
13
|
import { applyUsageRepairIfUnchanged, selectUsageRepairCandidates } from './usage-ledger-repair.mjs';
|
|
13
14
|
import { unpricedCountAfter, unpricedPendingCounts } from './usage-pricing-refresh.mjs';
|
|
14
15
|
|
|
@@ -61,6 +62,7 @@ const operations = {
|
|
|
61
62
|
rollup: ({ path, options }) => read(path, (db) => rollupUsage(db, options)),
|
|
62
63
|
rollupDelta: ({ path, options, known, now }) => read(path, (db) => rollupDelta(db, options, known, now)),
|
|
63
64
|
sessionUsage: ({ path, sessionIds, ownerIds }) => read(path, (db) => readSessionUsage(db, sessionIds, ownerIds)),
|
|
65
|
+
requests: ({ path, options }) => read(path, (db) => readUsageRequests(db, options)),
|
|
64
66
|
record: ({ path, rows }) => write(path, (ledger) => recordBatch(ledger, rows)),
|
|
65
67
|
recordQuota: ({ path, samples }) => write(path, (ledger) => ledger.recordQuota(samples)),
|
|
66
68
|
quotaHistory: ({ path, options }) => read(path, (db) => readQuotaHistory(db, options)),
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
rollupUsage,
|
|
21
21
|
} from './usage-ledger-rollup.mjs';
|
|
22
22
|
import { QUOTA_SCHEMA, readQuotaHistory, readQuotaWindows, recordQuotaSamples } from './usage-ledger-quota.mjs';
|
|
23
|
+
import { NO_USAGE_COST_SOURCE, REQUEST_LOG_SCHEMA, STORED_REQUEST_STATUSES, readUsageRequests } from './usage-ledger-requests.mjs';
|
|
23
24
|
import { SHARE_ENV } from 'node:worker_threads';
|
|
24
25
|
import { createWorkerRequestClient } from '../worker-requests.mjs';
|
|
25
26
|
|
|
@@ -116,6 +117,8 @@ function compactEventWriter(db) {
|
|
|
116
117
|
const insertSession = db.prepare('INSERT OR IGNORE INTO usage_sessions(value) VALUES (?)');
|
|
117
118
|
const findSession = db.prepare('SELECT id FROM usage_sessions WHERE value=?');
|
|
118
119
|
const insert = db.prepare('INSERT INTO usage_events VALUES (?,?,?,?,?,?,?,?,?,?,?)');
|
|
120
|
+
const insertRequest = db.prepare('INSERT INTO usage_requests VALUES (?,?,?,?,?,?,?)');
|
|
121
|
+
const insertFirstOutput = db.prepare('INSERT INTO usage_request_first_output VALUES (?,?)');
|
|
119
122
|
const insertOwner = db.prepare('INSERT OR IGNORE INTO usage_session_owners VALUES (?,?)');
|
|
120
123
|
return (row) => {
|
|
121
124
|
for (const key of ['id', 'provider', 'model', 'kind', 'source_type', 'cost_source', 'origin', 'session_id']) {
|
|
@@ -159,6 +162,16 @@ function compactEventWriter(db) {
|
|
|
159
162
|
row.cost_usd,
|
|
160
163
|
row.duration_ms
|
|
161
164
|
);
|
|
165
|
+
// Request-log facts live beside the record (usage-ledger-requests.mjs);
|
|
166
|
+
// a completed single attempt with nothing else to say needs no row.
|
|
167
|
+
const status = row.status || 'completed';
|
|
168
|
+
const attempts = row.attempts || 1;
|
|
169
|
+
if (status !== 'completed' || attempts > 1 || row.requested_model || row.elapsed_ms) {
|
|
170
|
+
insertRequest.run(id, row.ts, row.requested_model || null, status, attempts, null, row.elapsed_ms || null);
|
|
171
|
+
}
|
|
172
|
+
// The first-output time has its own table: only this build writes it, so
|
|
173
|
+
// a row there is this build's measurement (usage-ledger-requests.mjs).
|
|
174
|
+
if (row.ttft_ms != null) insertFirstOutput.run(id, row.ttft_ms);
|
|
162
175
|
return true;
|
|
163
176
|
};
|
|
164
177
|
}
|
|
@@ -187,6 +200,7 @@ function migrateUsageLedger(db, path) {
|
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
db.exec(COMPACT_SCHEMA);
|
|
203
|
+
db.exec(REQUEST_LOG_SCHEMA);
|
|
190
204
|
const insert = compactEventWriter(db);
|
|
191
205
|
for (const row of db.prepare('SELECT * FROM events').iterate()) insert(row);
|
|
192
206
|
db.exec(`CREATE VIEW usage_events_migration_check AS ${EVENT_SELECT}`);
|
|
@@ -251,6 +265,7 @@ export function usageCostSource({ kind, costUsd, subscription, reported = false
|
|
|
251
265
|
export function makeUsageRecord(args) {
|
|
252
266
|
const provider = text(args.provider);
|
|
253
267
|
const model = text(args.model);
|
|
268
|
+
const requestedModel = text(args.requestedModel);
|
|
254
269
|
if (!provider || !model) throw new Error('Usage requires a provider and model');
|
|
255
270
|
const ts = Number(args.ts ?? Date.now());
|
|
256
271
|
if (!Number.isSafeInteger(ts) || ts <= 0) throw new Error('Usage requires a valid timestamp');
|
|
@@ -269,6 +284,26 @@ export function makeUsageRecord(args) {
|
|
|
269
284
|
let costUsd = priced.costUsd;
|
|
270
285
|
if (kind === 'local') costUsd = 0;
|
|
271
286
|
else if (reported) costUsd = Number(args.costUsd);
|
|
287
|
+
// A request that measured no tokens at all (a failure that reported
|
|
288
|
+
// nothing) cost nothing, priced model or not: it must not read as unpriced.
|
|
289
|
+
else if (
|
|
290
|
+
costUsd === null &&
|
|
291
|
+
args.status === 'failed' &&
|
|
292
|
+
args.inputTokensKnown !== false &&
|
|
293
|
+
priced.input === 0 &&
|
|
294
|
+
!num(args.outputTokens) &&
|
|
295
|
+
!num(args.cacheReadTokens) &&
|
|
296
|
+
!num(args.cacheWriteTokens)
|
|
297
|
+
)
|
|
298
|
+
costUsd = 0;
|
|
299
|
+
const noUsage =
|
|
300
|
+
args.status === 'failed' &&
|
|
301
|
+
args.inputTokensKnown !== false &&
|
|
302
|
+
priced.input === 0 &&
|
|
303
|
+
!num(args.outputTokens) &&
|
|
304
|
+
!num(args.cacheReadTokens) &&
|
|
305
|
+
!num(args.cacheWriteTokens) &&
|
|
306
|
+
!costUsd;
|
|
272
307
|
let rates = priced.rates;
|
|
273
308
|
if (reported || kind === 'local') {
|
|
274
309
|
rates = {
|
|
@@ -295,12 +330,22 @@ export function makeUsageRecord(args) {
|
|
|
295
330
|
cacheRead: num(args.cacheReadTokens),
|
|
296
331
|
cacheWrite: num(args.cacheWriteTokens),
|
|
297
332
|
costUsd,
|
|
298
|
-
costSource: usageCostSource({ kind, costUsd, subscription, reported }),
|
|
333
|
+
costSource: noUsage ? NO_USAGE_COST_SOURCE : usageCostSource({ kind, costUsd, subscription, reported }),
|
|
299
334
|
rates,
|
|
300
335
|
responseId: text(args.responseId),
|
|
301
336
|
account: text(args.account),
|
|
302
337
|
origin: args.origin || 'live',
|
|
303
338
|
durationMs: num(args.durationMs),
|
|
339
|
+
// The request log (usage-ledger-requests.mjs): kept only when the
|
|
340
|
+
// answering model differs from the one asked for.
|
|
341
|
+
requestedModel: requestedModel !== model ? requestedModel : '',
|
|
342
|
+
status: STORED_REQUEST_STATUSES.includes(args.status) ? args.status : 'completed',
|
|
343
|
+
attempts: Math.max(1, Math.floor(num(args.attempts)) || 1),
|
|
344
|
+
ttftMs: args.ttftMs == null || !Number.isFinite(Number(args.ttftMs)) ? null : Math.max(0, Number(args.ttftMs)),
|
|
345
|
+
// A failed request's wall time, shown in the request log only: the
|
|
346
|
+
// record's own duration stays 0 so statistics speed reads completed
|
|
347
|
+
// requests alone, as before.
|
|
348
|
+
elapsedMs: num(args.elapsedMs),
|
|
304
349
|
};
|
|
305
350
|
row.id = args.id || (row.origin === 'live' && !row.responseId ? randomUUID() : usageRecordId(row));
|
|
306
351
|
return row;
|
|
@@ -369,6 +414,7 @@ export class UsageLedger {
|
|
|
369
414
|
CREATE TABLE IF NOT EXISTS legacy_days (day TEXT PRIMARY KEY, document TEXT NOT NULL);
|
|
370
415
|
${QUOTA_SCHEMA}
|
|
371
416
|
${DAY_REVISION_SCHEMA}
|
|
417
|
+
${REQUEST_LOG_SCHEMA}
|
|
372
418
|
PRAGMA user_version=2;
|
|
373
419
|
`);
|
|
374
420
|
this.prepareStatements();
|
|
@@ -417,6 +463,7 @@ export class UsageLedger {
|
|
|
417
463
|
}
|
|
418
464
|
|
|
419
465
|
indexRecord(row, rank) {
|
|
466
|
+
if (row.costSource === NO_USAGE_COST_SOURCE) return;
|
|
420
467
|
const conversation = isConversationUsageSource(row.sourceType);
|
|
421
468
|
this.daily.run(
|
|
422
469
|
row.day,
|
|
@@ -477,6 +524,9 @@ export class UsageLedger {
|
|
|
477
524
|
cost_source: row.costSource,
|
|
478
525
|
rates: row.rates ? JSON.stringify(row.rates) : null,
|
|
479
526
|
duration_ms: row.durationMs,
|
|
527
|
+
requested_model: row.requestedModel,
|
|
528
|
+
ttft_ms: row.ttftMs,
|
|
529
|
+
elapsed_ms: row.elapsedMs,
|
|
480
530
|
rank,
|
|
481
531
|
})
|
|
482
532
|
)
|
|
@@ -582,6 +632,12 @@ export class UsageLedger {
|
|
|
582
632
|
return this.workerRequest('sessionUsage', { sessionIds, ownerIds });
|
|
583
633
|
}
|
|
584
634
|
|
|
635
|
+
/** One page of the request log (usage-ledger-requests.mjs), read on the ledger worker. */
|
|
636
|
+
requestsAsync(options) {
|
|
637
|
+
if (this.path === ':memory:') return Promise.resolve().then(() => readUsageRequests(this.db, options));
|
|
638
|
+
return this.workerRequest('requests', { options });
|
|
639
|
+
}
|
|
640
|
+
|
|
585
641
|
preserveLegacyDays(days) {
|
|
586
642
|
const insert = this.db.prepare('INSERT OR IGNORE INTO legacy_days VALUES (?,?)');
|
|
587
643
|
this.db.exec('BEGIN IMMEDIATE');
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A request's one-word outcome in the request log. Precedence: failed >
|
|
3
|
+
* substituted > retried > completed.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// These providers echo the requested id instead of reporting the model that
|
|
7
|
+
// served, so a difference there would only be our own id mapping.
|
|
8
|
+
const NEVER_REPORTS_SERVED_MODEL = /^(?:gemini|vertex|antigravity)/;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The model id with only what provably names the same model removed: a
|
|
12
|
+
* `vendor/` route prefix (OpenRouter and gateways report `openai/gpt-5`), a
|
|
13
|
+
* `[…]` context-window tag (`[1m]`), a release-date snapshot and `-latest`.
|
|
14
|
+
* Anthropic reports the dated snapshot it served (`message_start.message.model`,
|
|
15
|
+
* anthropic-request-utils.mjs normalizeAnthropicNonStreamingResponse and the
|
|
16
|
+
* SSE turn), OpenAI and compatible providers the dated id in the response
|
|
17
|
+
* `model` (openai-compat-chat-stream/chunk-events.mjs). Dots, other digits
|
|
18
|
+
* and provider tags such as `:free` are kept: `gpt-5.1` is not `gpt-5-1`.
|
|
19
|
+
*/
|
|
20
|
+
export function normalizeServedModelId(model) {
|
|
21
|
+
let id = String(model || '').trim();
|
|
22
|
+
id = id.replace(/\[[^\]]*\]$/, '');
|
|
23
|
+
id = id.slice(id.lastIndexOf('/') + 1);
|
|
24
|
+
let previous;
|
|
25
|
+
do {
|
|
26
|
+
previous = id;
|
|
27
|
+
id = id.replace(/(?:-\d{8}|-\d{4}-\d{2}-\d{2}|@\d{8}|-latest)$/, '');
|
|
28
|
+
} while (id !== previous);
|
|
29
|
+
return id;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Whether the model that answered is not the model asked for. */
|
|
33
|
+
export function servedModelSubstituted({ provider, requestedModel, servedModel, fallback = false }) {
|
|
34
|
+
if (NEVER_REPORTS_SERVED_MODEL.test(String(provider || ''))) return false;
|
|
35
|
+
if (fallback) return true;
|
|
36
|
+
if (!requestedModel || !servedModel) return false;
|
|
37
|
+
const requested = normalizeServedModelId(requestedModel);
|
|
38
|
+
const served = normalizeServedModelId(servedModel);
|
|
39
|
+
// A router alias names no model: whatever it picks is what was asked for.
|
|
40
|
+
if (requested === 'auto' || served === requested) return false;
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function usageRequestStatus({ failed = false, substituted = false, attempts = 1 }) {
|
|
45
|
+
if (failed) return 'failed';
|
|
46
|
+
if (substituted) return 'substituted';
|
|
47
|
+
return attempts > 1 ? 'retried' : 'completed';
|
|
48
|
+
}
|