Nothing anywhere recorded what a model call cost: the harness used a provider's usage numbers only to decide whether a turn had finished, then dropped them. So a reply could not say what it took to make, one model could not be compared with another on price, and there was no way to ask what a day, a week or a month of the crew's work came to. - `token_usages` records one row per call: user, bot, workspace, session, job and message, the harness, the provider and model that actually answered, the prompt and completion counts, and the cost when the provider reports one. - `harness-usage-scope.ts` carries those ids in an AsyncLocalStorage scope that `run-bot-job.ts` enters once, so a run's turns, its session summary, its transcriptions, images and embeddings are all attributed without threading ids through every harness function. Nested runs merge rather than replace, and a null genuinely clears a field (a bot's own chat has no workspace). - Work with no run behind it is charged to its owner instead: `run-with-fallback-scope.ts` reaches the workspace's creator else the bot's, and deliberately does not override a real run, so an ingest a bot asked for is not misattributed to the bot's owner. - All six harnesses record through `withCandidates`'s `on_success`. Recording never throws: a model that answered must not lose its answer to a failed insert. - A job's calls land before its message exists, so `complete-bot-job.ts` back-fills `message_id` once the message is inserted. - The message's actions row shows what it cost, beside the timestamp and the play button, with the breakdown in its title. - `/admin/usage` totals the install's spending for 7, 30 or 90 days: the total against the previous period, a day-by-day chart, a split per kind of call, and a table per model with the price per thousand tokens worked out from cost and tokens, which is what makes two models comparable. Crew managers only, since it is the whole install's work rather than one user's conversation. It sits with Routines and Activity in the user popover rather than under Settings, because it is a record of what happened, not something to configure. Days are grouped in local time (`date(created_at / 1000, 'unixepoch', 'localtime')`), matching the local midnights the window builds and the `YYYY-MM-DD` buckets it looks up. Grouping by the bare UTC day put a late evening's calls under a name no bucket carried, so they vanished and the chart's total silently disagreed with the totals card; a test pins it. `canSeeRailItem` now decides who sees a popover or rail entry. The two each filtered with their own inline rule and only one understood `can_view`, so a gated page could be offered in the popover to everyone.
103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import type { HarnessCandidate } from "../types";
|
|
import buildUsageCall from "./build-usage-call";
|
|
|
|
function model(overrides: { row_id?: number } = {}) {
|
|
return {
|
|
slug: "deepseek/deepseek-chat",
|
|
name: "DeepSeek Chat",
|
|
input_modalities: ["text"],
|
|
output_modalities: ["text"],
|
|
supports_tools: true,
|
|
...overrides,
|
|
} as HarnessCandidate["model"];
|
|
}
|
|
|
|
function candidate(overrides: Partial<HarnessCandidate> = {}): HarnessCandidate {
|
|
return {
|
|
provider: {
|
|
id: 2,
|
|
name: "OpenRouter",
|
|
kind: "openrouter",
|
|
} as HarnessCandidate["provider"],
|
|
key: "openrouter:deepseek/deepseek-chat",
|
|
source: "auto",
|
|
model: model({ row_id: 41 }),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("buildUsageCall", () => {
|
|
test("names the candidate that really answered and what it spent", () => {
|
|
const call = buildUsageCall({
|
|
harness: "text",
|
|
candidate: candidate(),
|
|
usage: { prompt_tokens: 1200, completion_tokens: 340, cost: 0.0042 },
|
|
});
|
|
|
|
expect(call).toEqual({
|
|
harness: "text",
|
|
provider_id: 2,
|
|
provider_name: "OpenRouter",
|
|
model_id: 41,
|
|
model_slug: "deepseek/deepseek-chat",
|
|
prompt_tokens: 1200,
|
|
completion_tokens: 340,
|
|
cost: 0.0042,
|
|
});
|
|
});
|
|
|
|
test("a provider with no name reads as its kind", () => {
|
|
const call = buildUsageCall({
|
|
harness: "image",
|
|
candidate: candidate({
|
|
provider: { id: 4, name: "", kind: "openai" } as HarnessCandidate["provider"],
|
|
}),
|
|
usage: { prompt_tokens: 0, completion_tokens: 0 },
|
|
});
|
|
|
|
expect(call.provider_name).toBe("openai");
|
|
});
|
|
|
|
test("a provider that reports no usage still gets a row", () => {
|
|
const call = buildUsageCall({ harness: "transcription", candidate: candidate() });
|
|
|
|
expect(call.prompt_tokens).toBe(0);
|
|
expect(call.completion_tokens).toBe(0);
|
|
expect(call.cost).toBeUndefined();
|
|
});
|
|
|
|
test("a candidate with no catalog row has no model id to point at", () => {
|
|
const call = buildUsageCall({
|
|
harness: "text",
|
|
candidate: candidate({
|
|
model: model({ slug: "made-up-model" } as any),
|
|
}),
|
|
usage: { prompt_tokens: 10, completion_tokens: 5 },
|
|
});
|
|
|
|
expect(call.model_id).toBeUndefined();
|
|
expect(call.model_slug).toBe("made-up-model");
|
|
});
|
|
|
|
test("a cost the provider did not report is left out, not zeroed", () => {
|
|
const call = buildUsageCall({
|
|
harness: "text",
|
|
candidate: candidate(),
|
|
usage: { prompt_tokens: 10, completion_tokens: 5, cost: Number.NaN },
|
|
});
|
|
|
|
expect(call.cost).toBeUndefined();
|
|
});
|
|
|
|
test("a free provider's zero cost is kept as zero", () => {
|
|
const call = buildUsageCall({
|
|
harness: "text",
|
|
candidate: candidate(),
|
|
usage: { prompt_tokens: 10, completion_tokens: 5, cost: 0 },
|
|
});
|
|
|
|
expect(call.cost).toBe(0);
|
|
});
|
|
});
|