Skip to content
77 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { ModelUpstream } from "@g1t/contracts";
5
Merge platform pause and the hourly usage watcher: staff can pause compute, schedules, indexing or renders for everyone, the watcher emails on a breach and is never blind quietly, and the models proxy holds each run to its cap (billing 0051, integrations 0006)6import { isAnswer, runMayCall, tokenReport } from "./report.ts";
Mission control shows model usage, yours and the workspace's: tokens, cost, active days, cache share, each day, and the mix7import { NO_TOKENS, measure } from "./usage.ts";
8
9const upstream: ModelUpstream = {
10 route: "g1t",
11 api: "anthropic",
12 model: null,
13 official: false,
14 provider: "g1t",
15 workspace: "acme",
16 repo: "acme/web",
17 number: 7,
18 task: "implement",
19 session: "ms_abc",
20 tier: "large",
21 requestedBy: "ada",
22 baseUrl: null,
23 apiKey: null,
24 authHeader: null,
25};
26
27test("only messages are answers; counting tokens is not", () => {
28 assert.equal(isAnswer("/v1/messages"), true);
29 assert.equal(isAnswer("/v1/messages?beta=true"), true);
30 assert.equal(isAnswer("/v1/messages/count_tokens?beta=true"), false);
31 assert.equal(isAnswer("/v1/models"), false);
32});
33
34test("an answer's tokens are reported under its run and person", () => {
35 const report = tokenReport(upstream, "claude-opus-4", { input: 3, output: 9, cacheRead: 100, cacheWrite: 0 });
36 assert.deepEqual(report, {
37 workspace: "acme",
38 session: "ms_abc",
39 person: "ada",
40 model: "claude-opus-4",
41 tier: "large",
42 input: 3,
43 output: 9,
44 cacheRead: 100,
45 cacheWrite: 0,
46 });
47 // A route that names a model counts under it.
48 assert.equal(tokenReport({ ...upstream, model: "gpt-x", tier: null }, "other", { ...NO_TOKENS, output: 1 })?.model, "gpt-x");
49});
50
51test("nothing used, or no session to count it under, is not reported", () => {
52 assert.equal(tokenReport(upstream, null, { ...NO_TOKENS }), null);
53 assert.equal(tokenReport({ ...upstream, session: "" }, null, { ...NO_TOKENS, input: 1 }), null);
54});
55
56test("the model that answered is read from the answer", async () => {
57 const body = `data: ${JSON.stringify({ type: "message_start", message: { model: "claude-x", usage: { input_tokens: 1 } } })}\n\n`;
58 const streamed = measure(new Response(body, { headers: { "content-type": "text/event-stream" } }));
59 await streamed.response.text();
60 assert.equal(await streamed.model, "claude-x");
61 const whole = measure(Response.json({ model: "claude-y", usage: { output_tokens: 2 } }));
62 await whole.response.text();
63 assert.equal(await whole.model, "claude-y");
64});
Merge platform pause and the hourly usage watcher: staff can pause compute, schedules, indexing or renders for everyone, the watcher emails on a breach and is never blind quietly, and the models proxy holds each run to its cap (billing 0051, integrations 0006)65
66test("a run's token reaches answers, token counts and the model list, and nothing else", () => {
67 assert.ok(runMayCall("/v1/messages", "POST"));
68 assert.ok(runMayCall("/v1/messages?beta=true", "POST"));
69 assert.ok(runMayCall("/v1/messages/count_tokens", "POST"));
70 assert.ok(runMayCall("/v1/models", "GET"));
71 assert.ok(runMayCall("/v1/models/claude-sonnet-5-5", "GET"));
72 assert.equal(runMayCall("/v1/messages/batches", "POST"), false);
73 assert.equal(runMayCall("/v1//messages", "POST"), false);
74 assert.equal(runMayCall("/v1/complete", "POST"), false);
75 assert.equal(runMayCall("/v1/models", "POST"), false);
76 assert.equal(runMayCall("/v1/files", "GET"), false);
77});

This file's history is long; its oldest lines are credited to the oldest commit read.