@schlessera/brain-ui-server 0.18.0 → 0.20.0
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 +5 -1
- package/data/model-prices.json +63 -0
- package/dist/activity/digest.d.ts +29 -0
- package/dist/activity/digest.d.ts.map +1 -0
- package/dist/activity/digest.js +89 -0
- package/dist/activity/digest.js.map +1 -0
- package/dist/activity/notify.d.ts +62 -0
- package/dist/activity/notify.d.ts.map +1 -0
- package/dist/activity/notify.js +193 -0
- package/dist/activity/notify.js.map +1 -0
- package/dist/activity/push-sender.d.ts +63 -0
- package/dist/activity/push-sender.d.ts.map +1 -0
- package/dist/activity/push-sender.js +131 -0
- package/dist/activity/push-sender.js.map +1 -0
- package/dist/activity/query.d.ts +16 -0
- package/dist/activity/query.d.ts.map +1 -0
- package/dist/activity/query.js +155 -0
- package/dist/activity/query.js.map +1 -0
- package/dist/activity/recorder.d.ts +53 -0
- package/dist/activity/recorder.d.ts.map +1 -0
- package/dist/activity/recorder.js +238 -0
- package/dist/activity/recorder.js.map +1 -0
- package/dist/activity/runtime.d.ts +37 -0
- package/dist/activity/runtime.d.ts.map +1 -0
- package/dist/activity/runtime.js +127 -0
- package/dist/activity/runtime.js.map +1 -0
- package/dist/activity/span-sink.d.ts +22 -0
- package/dist/activity/span-sink.d.ts.map +1 -0
- package/dist/activity/span-sink.js +138 -0
- package/dist/activity/span-sink.js.map +1 -0
- package/dist/activity/store.d.ts +256 -0
- package/dist/activity/store.d.ts.map +1 -0
- package/dist/activity/store.js +657 -0
- package/dist/activity/store.js.map +1 -0
- package/dist/activity/stream.d.ts +49 -0
- package/dist/activity/stream.d.ts.map +1 -0
- package/dist/activity/stream.js +353 -0
- package/dist/activity/stream.js.map +1 -0
- package/dist/agent/backend.d.ts +13 -4
- package/dist/agent/backend.d.ts.map +1 -1
- package/dist/agent/backend.js +68 -5
- package/dist/agent/backend.js.map +1 -1
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +33 -2
- package/dist/app.js.map +1 -1
- package/dist/config/env.d.ts +36 -0
- package/dist/config/env.d.ts.map +1 -1
- package/dist/config/env.js +66 -2
- package/dist/config/env.js.map +1 -1
- package/dist/cron/scheduler.d.ts.map +1 -1
- package/dist/cron/scheduler.js +20 -5
- package/dist/cron/scheduler.js.map +1 -1
- package/dist/db/client.d.ts.map +1 -1
- package/dist/db/client.js +32 -6
- package/dist/db/client.js.map +1 -1
- package/dist/db/settings.d.ts +15 -0
- package/dist/db/settings.d.ts.map +1 -1
- package/dist/db/settings.js +43 -0
- package/dist/db/settings.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/pricing/model-pricing.d.ts +61 -0
- package/dist/pricing/model-pricing.d.ts.map +1 -0
- package/dist/pricing/model-pricing.js +379 -0
- package/dist/pricing/model-pricing.js.map +1 -0
- package/dist/routes/activity.d.ts +20 -0
- package/dist/routes/activity.d.ts.map +1 -0
- package/dist/routes/activity.js +308 -0
- package/dist/routes/activity.js.map +1 -0
- package/dist/routes/models.d.ts +6 -0
- package/dist/routes/models.d.ts.map +1 -1
- package/dist/routes/models.js +38 -3
- package/dist/routes/models.js.map +1 -1
- package/dist/routes/push.d.ts +6 -0
- package/dist/routes/push.d.ts.map +1 -0
- package/dist/routes/push.js +73 -0
- package/dist/routes/push.js.map +1 -0
- package/dist/routes/sessions.d.ts.map +1 -1
- package/dist/routes/sessions.js +20 -4
- package/dist/routes/sessions.js.map +1 -1
- package/dist/ws/bridge.d.ts +2 -1
- package/dist/ws/bridge.d.ts.map +1 -1
- package/dist/ws/bridge.js +27 -2
- package/dist/ws/bridge.js.map +1 -1
- package/dist/ws/connection.d.ts.map +1 -1
- package/dist/ws/connection.js +9 -1
- package/dist/ws/connection.js.map +1 -1
- package/dist/ws/dispatch.d.ts.map +1 -1
- package/dist/ws/dispatch.js +11 -0
- package/dist/ws/dispatch.js.map +1 -1
- package/dist/ws/host.d.ts +16 -0
- package/dist/ws/host.d.ts.map +1 -1
- package/dist/ws/host.js +2 -0
- package/dist/ws/host.js.map +1 -1
- package/dist/ws/run-session.d.ts.map +1 -1
- package/dist/ws/run-session.js +55 -1
- package/dist/ws/run-session.js.map +1 -1
- package/migrations/007_activity.sql +114 -0
- package/migrations/008_push_subscriptions.sql +24 -0
- package/migrations/009_activity_followups.sql +22 -0
- package/migrations/010_effective_cost.sql +21 -0
- package/package.json +6 -3
- package/src/activity/digest.ts +120 -0
- package/src/activity/notify.ts +307 -0
- package/src/activity/push-sender.ts +215 -0
- package/src/activity/query.ts +187 -0
- package/src/activity/recorder.ts +313 -0
- package/src/activity/runtime.ts +169 -0
- package/src/activity/span-sink.ts +152 -0
- package/src/activity/store.ts +1069 -0
- package/src/activity/stream.ts +396 -0
- package/src/agent/backend.ts +91 -10
- package/src/app.ts +35 -2
- package/src/config/env.ts +90 -2
- package/src/cron/scheduler.ts +28 -8
- package/src/db/client.ts +31 -6
- package/src/db/settings.ts +56 -0
- package/src/index.ts +23 -0
- package/src/pricing/model-pricing.ts +497 -0
- package/src/routes/activity.ts +371 -0
- package/src/routes/models.ts +57 -6
- package/src/routes/push.ts +90 -0
- package/src/routes/sessions.ts +24 -4
- package/src/ws/bridge.ts +29 -2
- package/src/ws/connection.ts +9 -1
- package/src/ws/dispatch.ts +13 -0
- package/src/ws/host.ts +18 -0
- package/src/ws/run-session.ts +71 -2
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import type { Database } from "bun:sqlite";
|
|
3
|
+
import {
|
|
4
|
+
isFailureOutcome,
|
|
5
|
+
type ActivityAggregate,
|
|
6
|
+
type ActivityRollups,
|
|
7
|
+
type ActivityRunDetail,
|
|
8
|
+
type ActivityRunSummary,
|
|
9
|
+
} from "@schlessera/brain-ui-sdk/protocol";
|
|
10
|
+
|
|
11
|
+
import { rowToRunRollup, type ActivityStore } from "../activity/store.js";
|
|
12
|
+
import { toWireEvent, toWireRollup, toWireSpan } from "../activity/stream.js";
|
|
13
|
+
import type { ActivityNotifier } from "../activity/notify.js";
|
|
14
|
+
import {
|
|
15
|
+
dismissActivityDigest,
|
|
16
|
+
digestDismissedAt,
|
|
17
|
+
generateActivityDigest,
|
|
18
|
+
latestActivityDigest,
|
|
19
|
+
} from "../activity/digest.js";
|
|
20
|
+
import { getSetting, setSetting } from "../db/settings.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The activity record's read API. Auth-guarded like every /api route (the
|
|
24
|
+
* record leaks strictly more than /api/status, which is itself deliberately
|
|
25
|
+
* behind the guard).
|
|
26
|
+
*
|
|
27
|
+
* Resolution rules (origin R26): a run id that ever existed resolves — to
|
|
28
|
+
* the full span tree while detail is retained, to its rollup ("detail
|
|
29
|
+
* pruned") afterwards; only never-existed ids 404. Cost/token aggregates
|
|
30
|
+
* read per-run rollups, which carry ROOT-span accounting only.
|
|
31
|
+
*/
|
|
32
|
+
export function createActivityRoutes(deps: {
|
|
33
|
+
db: Database;
|
|
34
|
+
store: ActivityStore;
|
|
35
|
+
notifier?: ActivityNotifier;
|
|
36
|
+
}): Hono {
|
|
37
|
+
const { db, store, notifier } = deps;
|
|
38
|
+
|
|
39
|
+
return new Hono()
|
|
40
|
+
.get("/activity/digest", (c) => {
|
|
41
|
+
try {
|
|
42
|
+
// An authenticated fetch IS the app opening — the marker the next
|
|
43
|
+
// digest window's framing leans on.
|
|
44
|
+
setSetting(db, "activity.lastVisitAt", Date.now());
|
|
45
|
+
return c.json({
|
|
46
|
+
digest: latestActivityDigest(db),
|
|
47
|
+
dismissedAt: digestDismissedAt(db),
|
|
48
|
+
});
|
|
49
|
+
} catch (err) {
|
|
50
|
+
return c.json(
|
|
51
|
+
{ error: err instanceof Error ? err.message : "Failed to load digest" },
|
|
52
|
+
500
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
.post("/activity/digest/dismiss", (c) => {
|
|
58
|
+
try {
|
|
59
|
+
dismissActivityDigest(db);
|
|
60
|
+
return c.json({ ok: true });
|
|
61
|
+
} catch (err) {
|
|
62
|
+
return c.json(
|
|
63
|
+
{ error: err instanceof Error ? err.message : "Failed to dismiss" },
|
|
64
|
+
500
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
.post("/activity/digest/generate", (c) => {
|
|
70
|
+
// Manual trigger (the cron job calls generateActivityDigest directly
|
|
71
|
+
// through the script; this exists for dev and for a pull-to-refresh).
|
|
72
|
+
try {
|
|
73
|
+
return c.json({ digest: generateActivityDigest(db) });
|
|
74
|
+
} catch (err) {
|
|
75
|
+
return c.json(
|
|
76
|
+
{ error: err instanceof Error ? err.message : "Failed to generate" },
|
|
77
|
+
500
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
.get("/activity/inbox", (c) => {
|
|
83
|
+
try {
|
|
84
|
+
return c.json({ intents: notifier?.inbox() ?? [] });
|
|
85
|
+
} catch (err) {
|
|
86
|
+
return c.json(
|
|
87
|
+
{ error: err instanceof Error ? err.message : "Failed to list inbox" },
|
|
88
|
+
500
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
.post("/activity/inbox/ack-all", (c) => {
|
|
94
|
+
try {
|
|
95
|
+
return c.json({ acknowledged: notifier?.acknowledgeAll() ?? 0 });
|
|
96
|
+
} catch (err) {
|
|
97
|
+
return c.json(
|
|
98
|
+
{ error: err instanceof Error ? err.message : "Failed to acknowledge" },
|
|
99
|
+
500
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
.post("/activity/inbox/:id/ack", (c) => {
|
|
105
|
+
const id = Number(c.req.param("id"));
|
|
106
|
+
if (!Number.isInteger(id) || id < 1) return c.json({ error: "Bad intent id" }, 400);
|
|
107
|
+
try {
|
|
108
|
+
const ok = notifier?.acknowledge(id) ?? false;
|
|
109
|
+
return ok ? c.json({ ok: true }) : c.json({ error: "Unknown intent" }, 404);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
return c.json(
|
|
112
|
+
{ error: err instanceof Error ? err.message : "Failed to acknowledge" },
|
|
113
|
+
500
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
.get("/activity/runs", (c) => {
|
|
118
|
+
try {
|
|
119
|
+
const limit = Math.min(Math.max(Number(c.req.query("limit") ?? 50) || 50, 1), 200);
|
|
120
|
+
const before = Number(c.req.query("before")) || Date.now() + 1;
|
|
121
|
+
const origin = c.req.query("origin");
|
|
122
|
+
const job = c.req.query("job");
|
|
123
|
+
const session = c.req.query("session");
|
|
124
|
+
const status = c.req.query("status");
|
|
125
|
+
|
|
126
|
+
// Live first (open roots), then history from rollups. Rollups exist
|
|
127
|
+
// for every finished run (terminal writes upsert them), so one table
|
|
128
|
+
// serves the history list regardless of pruning state.
|
|
129
|
+
const live: ActivityRunSummary[] = store
|
|
130
|
+
.openRootSpans()
|
|
131
|
+
.filter((s) => !origin || s.origin === origin)
|
|
132
|
+
.filter((s) => !job || s.jobName === job)
|
|
133
|
+
.filter((s) => !session || s.sessionId === session)
|
|
134
|
+
.map((s) => ({
|
|
135
|
+
runId: s.runId,
|
|
136
|
+
origin: s.origin,
|
|
137
|
+
name: s.name,
|
|
138
|
+
sessionId: s.sessionId,
|
|
139
|
+
jobName: s.jobName,
|
|
140
|
+
startedAt: s.startedAt,
|
|
141
|
+
endedAt: null,
|
|
142
|
+
outcome: null,
|
|
143
|
+
running: true,
|
|
144
|
+
durationMs: null,
|
|
145
|
+
costUsd: s.usage.costUsd ?? null,
|
|
146
|
+
// No rollup yet — effective cost is unknown until the run prices.
|
|
147
|
+
effectiveCostUsd: null,
|
|
148
|
+
failureReason: null,
|
|
149
|
+
detailPruned: false,
|
|
150
|
+
}));
|
|
151
|
+
|
|
152
|
+
const filters: string[] = ["started_at < ?"];
|
|
153
|
+
const params: unknown[] = [before];
|
|
154
|
+
if (origin) {
|
|
155
|
+
filters.push("origin = ?");
|
|
156
|
+
params.push(origin);
|
|
157
|
+
}
|
|
158
|
+
if (job) {
|
|
159
|
+
filters.push("job_name = ?");
|
|
160
|
+
params.push(job);
|
|
161
|
+
}
|
|
162
|
+
if (session) {
|
|
163
|
+
filters.push("session_id = ?");
|
|
164
|
+
params.push(session);
|
|
165
|
+
}
|
|
166
|
+
if (status) {
|
|
167
|
+
filters.push("outcome = ?");
|
|
168
|
+
params.push(status);
|
|
169
|
+
}
|
|
170
|
+
if (live.length > 0) {
|
|
171
|
+
filters.push(`run_id NOT IN (${live.map(() => "?").join(", ")})`);
|
|
172
|
+
params.push(...live.map((r) => r.runId));
|
|
173
|
+
}
|
|
174
|
+
const history: ActivityRunSummary[] = (
|
|
175
|
+
db
|
|
176
|
+
.query(
|
|
177
|
+
`SELECT * FROM activity_run_rollups WHERE ${filters.join(" AND ")}
|
|
178
|
+
ORDER BY started_at DESC LIMIT ?`
|
|
179
|
+
)
|
|
180
|
+
.all(...(params as never[]), limit) as any[]
|
|
181
|
+
)
|
|
182
|
+
.map(rowToRunRollup)
|
|
183
|
+
// The wire mapping (explicit-null vs omit) lives in toWireRollup;
|
|
184
|
+
// a summary adds only its identity and liveness fields on top.
|
|
185
|
+
.map((r) => ({
|
|
186
|
+
...toWireRollup(r),
|
|
187
|
+
origin: r.origin,
|
|
188
|
+
runId: r.runId,
|
|
189
|
+
running: false,
|
|
190
|
+
detailPruned: r.detailPruned,
|
|
191
|
+
}));
|
|
192
|
+
|
|
193
|
+
return c.json({ live, history });
|
|
194
|
+
} catch (err) {
|
|
195
|
+
return c.json(
|
|
196
|
+
{ error: err instanceof Error ? err.message : "Failed to list activity" },
|
|
197
|
+
500
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
.get("/activity/runs/:runId", (c) => {
|
|
203
|
+
const runId = c.req.param("runId");
|
|
204
|
+
try {
|
|
205
|
+
const snapshot = store.snapshotRun(runId);
|
|
206
|
+
const row = db
|
|
207
|
+
.query("SELECT * FROM activity_run_rollups WHERE run_id = ?")
|
|
208
|
+
.get(runId) as any;
|
|
209
|
+
if (snapshot) {
|
|
210
|
+
// Tool payload events (up to 16 KB each) dominate a run's byte
|
|
211
|
+
// size, and the history list opens details eagerly — so the REST
|
|
212
|
+
// detail ships WITHOUT them unless the drill-in asks
|
|
213
|
+
// (?include=payloads). The WS snapshot path is untouched: a live
|
|
214
|
+
// subscription already committed to the full stream.
|
|
215
|
+
const includePayloads = c.req.query("include") === "payloads";
|
|
216
|
+
const events = includePayloads
|
|
217
|
+
? snapshot.events
|
|
218
|
+
: snapshot.events.filter(
|
|
219
|
+
(e) => e.eventType !== "tool_input" && e.eventType !== "tool_output"
|
|
220
|
+
);
|
|
221
|
+
// Through the SAME wire mappers the live stream uses: a raw
|
|
222
|
+
// SpanRow serializes null fields where the wire contract omits
|
|
223
|
+
// them, which broke the client's `outcome === undefined`
|
|
224
|
+
// liveness test on REST-loaded runs. The rollup rides along when
|
|
225
|
+
// it exists (finished runs) so the detail view can show cost facts
|
|
226
|
+
// without waiting for detail pruning to force the rollup-only path.
|
|
227
|
+
const detail: ActivityRunDetail = {
|
|
228
|
+
runId,
|
|
229
|
+
detailPruned: false,
|
|
230
|
+
spans: snapshot.spans.map(toWireSpan),
|
|
231
|
+
events: events.map(toWireEvent),
|
|
232
|
+
highWaterSeq: snapshot.highWaterSeq,
|
|
233
|
+
...(row ? { rollup: toWireRollup(rowToRunRollup(row)) } : {}),
|
|
234
|
+
};
|
|
235
|
+
return c.json(detail);
|
|
236
|
+
}
|
|
237
|
+
if (!row) return c.json({ error: "Unknown run" }, 404);
|
|
238
|
+
const r = rowToRunRollup(row);
|
|
239
|
+
const detail: ActivityRunDetail = { runId, detailPruned: true, rollup: toWireRollup(r) };
|
|
240
|
+
return c.json(detail);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
return c.json(
|
|
243
|
+
{ error: err instanceof Error ? err.message : "Failed to load run" },
|
|
244
|
+
500
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
.get("/activity/rollups", (c) => {
|
|
250
|
+
try {
|
|
251
|
+
const days = Math.min(Number(c.req.query("days") ?? 7) || 7, 90);
|
|
252
|
+
const since = Date.now() - days * 24 * 60 * 60 * 1000;
|
|
253
|
+
// The day boundary is the USER'S day, not UTC's — cron runs in UTC
|
|
254
|
+
// but nobody reviews spend in it. Configurable server-side.
|
|
255
|
+
const timeZone = getSetting<string>(db, "activity.timezone", "UTC");
|
|
256
|
+
const dayOf = makeDayFormatter(timeZone);
|
|
257
|
+
|
|
258
|
+
// Job/session aggregates group SQL-side; the per-day fold stays in
|
|
259
|
+
// JS because only Intl knows the configured timezone's day boundary.
|
|
260
|
+
const rows = db
|
|
261
|
+
.query(
|
|
262
|
+
`SELECT started_at, outcome, duration_ms, cost_usd, effective_cost_usd,
|
|
263
|
+
input_tokens, output_tokens, cache_read_tokens, cache_creation_tokens
|
|
264
|
+
FROM activity_run_rollups WHERE started_at >= ?`
|
|
265
|
+
)
|
|
266
|
+
.all(since) as any[];
|
|
267
|
+
const byDay = new Map<string, ActivityAggregate>();
|
|
268
|
+
for (const r of rows) {
|
|
269
|
+
add(byDay, dayOf(r.started_at), r);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const rollups: ActivityRollups = {
|
|
273
|
+
timeZone,
|
|
274
|
+
days: [...byDay.entries()]
|
|
275
|
+
.map(([key, a]) => ({ day: key, ...a }))
|
|
276
|
+
.sort((a, b) => (a.day < b.day ? 1 : -1)),
|
|
277
|
+
jobs: groupedAggregates(db, since, "job_name").map(({ key, ...a }) => ({
|
|
278
|
+
jobName: key,
|
|
279
|
+
...a,
|
|
280
|
+
})),
|
|
281
|
+
sessions: groupedAggregates(db, since, "session_id").map(({ key, ...a }) => ({
|
|
282
|
+
sessionId: key,
|
|
283
|
+
...a,
|
|
284
|
+
})),
|
|
285
|
+
};
|
|
286
|
+
return c.json(rollups);
|
|
287
|
+
} catch (err) {
|
|
288
|
+
return c.json(
|
|
289
|
+
{ error: err instanceof Error ? err.message : "Failed to aggregate" },
|
|
290
|
+
500
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* SQL-side aggregation per group key. The failure predicate mirrors the
|
|
298
|
+
* SDK's `isFailureOutcome` — it cannot be shared into SQL, so keep the two
|
|
299
|
+
* in sync.
|
|
300
|
+
*/
|
|
301
|
+
function groupedAggregates(
|
|
302
|
+
db: Database,
|
|
303
|
+
since: number,
|
|
304
|
+
column: "job_name" | "session_id"
|
|
305
|
+
): Array<ActivityAggregate & { key: string }> {
|
|
306
|
+
return db
|
|
307
|
+
.query(
|
|
308
|
+
`SELECT ${column} AS key,
|
|
309
|
+
COUNT(*) AS runs,
|
|
310
|
+
SUM(CASE WHEN outcome IN ('error', 'timeout', 'interrupted') THEN 1 ELSE 0 END) AS failures,
|
|
311
|
+
SUM(COALESCE(cost_usd, 0)) AS costUsd,
|
|
312
|
+
SUM(COALESCE(effective_cost_usd, 0)) AS effectiveCostUsd,
|
|
313
|
+
SUM(CASE WHEN effective_cost_usd IS NULL THEN 1 ELSE 0 END) AS unpricedRuns,
|
|
314
|
+
SUM(COALESCE(input_tokens, 0)) AS inputTokens,
|
|
315
|
+
SUM(COALESCE(output_tokens, 0)) AS outputTokens,
|
|
316
|
+
SUM(COALESCE(cache_read_tokens, 0)) AS cacheReadTokens,
|
|
317
|
+
SUM(COALESCE(cache_creation_tokens, 0)) AS cacheCreationTokens,
|
|
318
|
+
SUM(COALESCE(duration_ms, 0)) AS durationMs
|
|
319
|
+
FROM activity_run_rollups
|
|
320
|
+
WHERE started_at >= ? AND ${column} IS NOT NULL
|
|
321
|
+
GROUP BY ${column}`
|
|
322
|
+
)
|
|
323
|
+
.all(since) as Array<ActivityAggregate & { key: string }>;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function add(map: Map<string, ActivityAggregate>, key: string, r: any): void {
|
|
327
|
+
const a =
|
|
328
|
+
map.get(key) ??
|
|
329
|
+
({
|
|
330
|
+
runs: 0,
|
|
331
|
+
failures: 0,
|
|
332
|
+
costUsd: 0,
|
|
333
|
+
effectiveCostUsd: 0,
|
|
334
|
+
unpricedRuns: 0,
|
|
335
|
+
inputTokens: 0,
|
|
336
|
+
outputTokens: 0,
|
|
337
|
+
cacheReadTokens: 0,
|
|
338
|
+
cacheCreationTokens: 0,
|
|
339
|
+
durationMs: 0,
|
|
340
|
+
} satisfies ActivityAggregate);
|
|
341
|
+
a.runs += 1;
|
|
342
|
+
if (isFailureOutcome(r.outcome)) {
|
|
343
|
+
a.failures += 1;
|
|
344
|
+
}
|
|
345
|
+
// Both cost sums are sum-of-KNOWNS (a missing value contributes nothing);
|
|
346
|
+
// for effective cost the excluded rows are counted so no surface can pass
|
|
347
|
+
// an unknown off as $0 (AE3).
|
|
348
|
+
a.costUsd += r.cost_usd ?? 0;
|
|
349
|
+
if (r.effective_cost_usd == null) {
|
|
350
|
+
a.unpricedRuns = (a.unpricedRuns ?? 0) + 1;
|
|
351
|
+
} else {
|
|
352
|
+
a.effectiveCostUsd = (a.effectiveCostUsd ?? 0) + r.effective_cost_usd;
|
|
353
|
+
}
|
|
354
|
+
a.inputTokens += r.input_tokens ?? 0;
|
|
355
|
+
a.outputTokens += r.output_tokens ?? 0;
|
|
356
|
+
a.cacheReadTokens += r.cache_read_tokens ?? 0;
|
|
357
|
+
a.cacheCreationTokens += r.cache_creation_tokens ?? 0;
|
|
358
|
+
a.durationMs += r.duration_ms ?? 0;
|
|
359
|
+
map.set(key, a);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** YYYY-MM-DD in the given zone; a bad zone degrades loudly to UTC once. */
|
|
363
|
+
function makeDayFormatter(timeZone: string): (ms: number) => string {
|
|
364
|
+
let fmt: Intl.DateTimeFormat;
|
|
365
|
+
try {
|
|
366
|
+
fmt = new Intl.DateTimeFormat("en-CA", { timeZone, dateStyle: "short" });
|
|
367
|
+
} catch {
|
|
368
|
+
fmt = new Intl.DateTimeFormat("en-CA", { timeZone: "UTC", dateStyle: "short" });
|
|
369
|
+
}
|
|
370
|
+
return (ms) => fmt.format(new Date(ms));
|
|
371
|
+
}
|
package/src/routes/models.ts
CHANGED
|
@@ -8,27 +8,45 @@
|
|
|
8
8
|
import type { Logger } from "@opentelemetry/api-logs";
|
|
9
9
|
import { Hono } from "hono";
|
|
10
10
|
import type { Database } from "bun:sqlite";
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
import {
|
|
12
|
+
isBillingMode,
|
|
13
|
+
type BillingMode,
|
|
14
|
+
type ModelCatalogEntry,
|
|
15
|
+
type ModelCatalogResponse,
|
|
14
16
|
} from "@schlessera/brain-ui-sdk";
|
|
15
17
|
import type { BackendRegistry } from "../agent/backend.js";
|
|
16
|
-
import {
|
|
18
|
+
import type { ModelPricingState } from "../pricing/model-pricing.js";
|
|
19
|
+
import {
|
|
20
|
+
getBillingOverrides,
|
|
21
|
+
getHiddenModelIds,
|
|
22
|
+
setBillingOverrides,
|
|
23
|
+
setHiddenModelIds,
|
|
24
|
+
} from "../db/settings.js";
|
|
17
25
|
|
|
18
26
|
export function createModelRoutes(deps: {
|
|
19
27
|
registry: BackendRegistry;
|
|
20
28
|
db: Database;
|
|
29
|
+
/** The shared pricing instance — its state drives the client's staleness indicator. */
|
|
30
|
+
pricing?: { state(): ModelPricingState; ensureFresh(): Promise<void> };
|
|
21
31
|
/** Where refresh failures are reported. */
|
|
22
32
|
log?: Logger;
|
|
23
33
|
}): Hono {
|
|
24
|
-
const { registry, db } = deps;
|
|
34
|
+
const { registry, db, pricing } = deps;
|
|
25
35
|
|
|
26
36
|
async function buildCatalog(): Promise<ModelCatalogResponse> {
|
|
27
37
|
const source = await registry.getModelSource();
|
|
28
38
|
const hidden = new Set(getHiddenModelIds(db));
|
|
39
|
+
const overrides = getBillingOverrides(db);
|
|
40
|
+
// `billingMode` already rides each provider entry (the registry applies the
|
|
41
|
+
// override last); the catalog additionally tags WHICH rows carry an explicit
|
|
42
|
+
// override, so the settings screen can render auto vs forced.
|
|
29
43
|
const models: ModelCatalogEntry[] = (
|
|
30
44
|
await registry.listAllProviders({ includeHidden: true })
|
|
31
|
-
).map((profile) => ({
|
|
45
|
+
).map((profile) => ({
|
|
46
|
+
...profile,
|
|
47
|
+
hidden: hidden.has(profile.id),
|
|
48
|
+
...(overrides[profile.id] ? { billingOverride: overrides[profile.id] } : {}),
|
|
49
|
+
}));
|
|
32
50
|
|
|
33
51
|
const state = source?.state();
|
|
34
52
|
return {
|
|
@@ -51,6 +69,17 @@ export function createModelRoutes(deps: {
|
|
|
51
69
|
return c.json(await buildCatalog());
|
|
52
70
|
})
|
|
53
71
|
|
|
72
|
+
.get("/models/pricing", async (c) => {
|
|
73
|
+
// Absent instance (a host that opted out) → 404; the client hides the
|
|
74
|
+
// indicator on any rejection, so old and opted-out servers degrade alike.
|
|
75
|
+
if (!pricing) return c.json({ error: "pricing not available" }, 404);
|
|
76
|
+
// Kick a refresh behind the response when stale — reading the state is
|
|
77
|
+
// also the natural moment to heal it. Never blocks: ensureFresh awaits
|
|
78
|
+
// only on a true cold start.
|
|
79
|
+
void pricing.ensureFresh().catch(() => {});
|
|
80
|
+
return c.json(pricing.state());
|
|
81
|
+
})
|
|
82
|
+
|
|
54
83
|
.put("/models/hidden", async (c) => {
|
|
55
84
|
const body = (await c.req.json().catch(() => null)) as unknown;
|
|
56
85
|
const hidden = (body as { hidden?: unknown } | null)?.hidden;
|
|
@@ -67,6 +96,28 @@ export function createModelRoutes(deps: {
|
|
|
67
96
|
return c.json(await buildCatalog());
|
|
68
97
|
})
|
|
69
98
|
|
|
99
|
+
.put("/models/billing", async (c) => {
|
|
100
|
+
const body = (await c.req.json().catch(() => null)) as unknown;
|
|
101
|
+
const billing = (body as { billing?: unknown } | null)?.billing;
|
|
102
|
+
if (
|
|
103
|
+
typeof billing !== "object" ||
|
|
104
|
+
billing === null ||
|
|
105
|
+
Array.isArray(billing) ||
|
|
106
|
+
Object.values(billing).some((mode) => !isBillingMode(mode))
|
|
107
|
+
) {
|
|
108
|
+
return c.json(
|
|
109
|
+
{ error: 'billing must map profile ids to "subscription" or "api"' },
|
|
110
|
+
400
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
setBillingOverrides(db, billing as Record<string, BillingMode>);
|
|
115
|
+
// Same memo discipline as the hidden set: the next roster read (and the
|
|
116
|
+
// next run's classification) must see the override immediately.
|
|
117
|
+
registry.invalidateProfiles();
|
|
118
|
+
return c.json(await buildCatalog());
|
|
119
|
+
})
|
|
120
|
+
|
|
70
121
|
.post("/models/refresh", async (c) => {
|
|
71
122
|
const source = await registry.getModelSource();
|
|
72
123
|
if (!source) {
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import type { PushSender } from "../activity/push-sender.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Push subscription lifecycle. Behind the auth guard by mount position —
|
|
8
|
+
* the public key is per-deployment (not build-time), and subscribing is a
|
|
9
|
+
* write into the notification fan-out, which only the authenticated user
|
|
10
|
+
* may do. The VAPID PRIVATE key has no route, here or anywhere.
|
|
11
|
+
*/
|
|
12
|
+
const subscribeSchema = z.object({
|
|
13
|
+
subscription: z.object({
|
|
14
|
+
endpoint: z.url().max(2048),
|
|
15
|
+
keys: z.object({
|
|
16
|
+
p256dh: z.string().min(1).max(512),
|
|
17
|
+
auth: z.string().min(1).max(512),
|
|
18
|
+
}),
|
|
19
|
+
}),
|
|
20
|
+
label: z.string().max(120).optional(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const unsubscribeSchema = z.object({
|
|
24
|
+
endpoint: z.url().max(2048),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export function createPushRoutes(deps: { sender: PushSender }): Hono {
|
|
28
|
+
const { sender } = deps;
|
|
29
|
+
|
|
30
|
+
return new Hono()
|
|
31
|
+
.get("/push/public-key", (c) => {
|
|
32
|
+
try {
|
|
33
|
+
return c.json({ publicKey: sender.publicKey() });
|
|
34
|
+
} catch (err) {
|
|
35
|
+
return c.json(
|
|
36
|
+
{ error: err instanceof Error ? err.message : "Push unavailable" },
|
|
37
|
+
500
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
.get("/push/subscriptions", (c) => {
|
|
43
|
+
try {
|
|
44
|
+
// Endpoints are capability URLs — list only metadata.
|
|
45
|
+
return c.json({
|
|
46
|
+
subscriptions: sender.subscriptions().map((s) => ({
|
|
47
|
+
label: s.label,
|
|
48
|
+
createdAt: s.createdAt,
|
|
49
|
+
lastUsedAt: s.lastUsedAt,
|
|
50
|
+
// Enough for the client to recognize its own registration.
|
|
51
|
+
endpointHash: hashEndpoint(s.endpoint),
|
|
52
|
+
})),
|
|
53
|
+
});
|
|
54
|
+
} catch (err) {
|
|
55
|
+
return c.json(
|
|
56
|
+
{ error: err instanceof Error ? err.message : "Failed to list" },
|
|
57
|
+
500
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
.post("/push/subscribe", async (c) => {
|
|
63
|
+
try {
|
|
64
|
+
const body = subscribeSchema.parse(await c.req.json());
|
|
65
|
+
sender.subscribe(body.subscription, body.label);
|
|
66
|
+
return c.json({ ok: true });
|
|
67
|
+
} catch (err) {
|
|
68
|
+
return c.json(
|
|
69
|
+
{ error: err instanceof Error ? err.message : "Bad subscription" },
|
|
70
|
+
400
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
.post("/push/unsubscribe", async (c) => {
|
|
76
|
+
try {
|
|
77
|
+
const body = unsubscribeSchema.parse(await c.req.json());
|
|
78
|
+
return c.json({ removed: sender.unsubscribe(body.endpoint) });
|
|
79
|
+
} catch (err) {
|
|
80
|
+
return c.json(
|
|
81
|
+
{ error: err instanceof Error ? err.message : "Bad request" },
|
|
82
|
+
400
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function hashEndpoint(endpoint: string): string {
|
|
89
|
+
return new Bun.CryptoHasher("sha256").update(endpoint).digest("hex").slice(0, 16);
|
|
90
|
+
}
|
package/src/routes/sessions.ts
CHANGED
|
@@ -19,13 +19,33 @@ export function createSessionRoutes(deps: {
|
|
|
19
19
|
.get("/sessions", async (c) => {
|
|
20
20
|
try {
|
|
21
21
|
const backends = await registry.getBackends();
|
|
22
|
+
// The catalog's accounting (total_cost_usd, num_turns) was write-only
|
|
23
|
+
// for years on the default backend, whose listSessions hardcodes
|
|
24
|
+
// zeros — merge the stored numbers in so the drawer's cost renders
|
|
25
|
+
// from what the server actually recorded.
|
|
26
|
+
const accounting = new Map(
|
|
27
|
+
(
|
|
28
|
+
db
|
|
29
|
+
.query("SELECT id, total_cost_usd AS cost, num_turns AS turns FROM sessions")
|
|
30
|
+
.all() as Array<{ id: string; cost: number | null; turns: number | null }>
|
|
31
|
+
).map((r) => [r.id, r])
|
|
32
|
+
);
|
|
22
33
|
const sessions = (
|
|
23
34
|
await Promise.all(
|
|
24
35
|
backends.map(async (backend) =>
|
|
25
|
-
(await backend.listSessions()).map((session) =>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
(await backend.listSessions()).map((session) => {
|
|
37
|
+
const stored = accounting.get(session.id);
|
|
38
|
+
return {
|
|
39
|
+
...session,
|
|
40
|
+
...(stored?.cost && session.totalCostUsd === 0
|
|
41
|
+
? { totalCostUsd: stored.cost }
|
|
42
|
+
: {}),
|
|
43
|
+
...(stored?.turns && session.numTurns === 0
|
|
44
|
+
? { numTurns: stored.turns }
|
|
45
|
+
: {}),
|
|
46
|
+
backendId: backend.id,
|
|
47
|
+
};
|
|
48
|
+
})
|
|
29
49
|
)
|
|
30
50
|
)
|
|
31
51
|
)
|
package/src/ws/bridge.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
ActivityQuery,
|
|
2
3
|
BackendBridge,
|
|
3
4
|
PermissionDecision,
|
|
4
5
|
AskUserResult,
|
|
@@ -8,13 +9,15 @@ import { BackendBusyError, BackendRequestError } from "@schlessera/brain-ui-sdk/
|
|
|
8
9
|
import { withTurnScope } from "./frames.js";
|
|
9
10
|
import type { RunningTurn } from "./turns.js";
|
|
10
11
|
import type { WsHost } from "./host.js";
|
|
12
|
+
import type { TurnRecorder } from "../activity/recorder.js";
|
|
11
13
|
|
|
12
14
|
/** Build the per-turn bridge the backend drives. */
|
|
13
15
|
export function makeBridge(
|
|
14
16
|
host: WsHost,
|
|
15
17
|
turn: RunningTurn,
|
|
16
18
|
promptText: string,
|
|
17
|
-
backendId: string
|
|
19
|
+
backendId: string,
|
|
20
|
+
recorder?: TurnRecorder
|
|
18
21
|
): BackendBridge {
|
|
19
22
|
const { coordinator, catalog } = host;
|
|
20
23
|
// Capture the turn identity at construction: the slot's turnId is re-minted
|
|
@@ -22,6 +25,7 @@ export function makeBridge(
|
|
|
22
25
|
// through this bridge after its startTurn resolved. Stamping from the live
|
|
23
26
|
// field would attribute those to the NEXT turn.
|
|
24
27
|
const turnId = turn.turnId;
|
|
28
|
+
const queryActivity = host.activity?.query;
|
|
25
29
|
return {
|
|
26
30
|
emit: (message) => {
|
|
27
31
|
let msg = message;
|
|
@@ -36,6 +40,11 @@ export function makeBridge(
|
|
|
36
40
|
// later fails or is cancelled must not leave an unowned transcript.
|
|
37
41
|
catalog.persistSessionStub(msg.sessionId, promptText, turn.providerId, backendId);
|
|
38
42
|
}
|
|
43
|
+
// Persist-then-emit: the span write commits before the frame goes out,
|
|
44
|
+
// so a subscriber's snapshot can never be behind what it just saw live.
|
|
45
|
+
// Late frames through a previous turn's bridge stay un-recorded — the
|
|
46
|
+
// recorder belongs to ONE turn identity.
|
|
47
|
+
if (turn.turnId === turnId) recorder?.observeFrame(msg);
|
|
39
48
|
host.sendToClients(withTurnScope(msg, turn, turnId));
|
|
40
49
|
if (msg.type === "result") {
|
|
41
50
|
// Only the live turn's own result may set its disposition — a late
|
|
@@ -87,9 +96,27 @@ export function makeBridge(
|
|
|
87
96
|
resolve({ behavior: "deny", message: "Duplicate tool-approval id" });
|
|
88
97
|
return;
|
|
89
98
|
}
|
|
90
|
-
|
|
99
|
+
// The decision stamps the wait/execution boundary on the tool span
|
|
100
|
+
// (grant) or lands the denied outcome (deny) before the backend's
|
|
101
|
+
// own error tool_result can mislabel it — write-once protects it.
|
|
102
|
+
const recorded = (decision: PermissionDecision) => {
|
|
103
|
+
recorder?.onApprovalDecision(req.toolUseId, decision.behavior === "allow");
|
|
104
|
+
resolve(decision);
|
|
105
|
+
};
|
|
106
|
+
coordinator.pendingApprovals.set(req.toolUseId, {
|
|
107
|
+
turn,
|
|
108
|
+
turnId,
|
|
109
|
+
request: req,
|
|
110
|
+
resolve: recorded,
|
|
111
|
+
});
|
|
91
112
|
});
|
|
92
113
|
},
|
|
114
|
+
...(recorder
|
|
115
|
+
? { activity: (event: Parameters<NonNullable<BackendBridge["activity"]>>[0]) => recorder.observeActivity(event) }
|
|
116
|
+
: {}),
|
|
117
|
+
...(queryActivity
|
|
118
|
+
? { queryActivity: async (query: ActivityQuery) => queryActivity(query) }
|
|
119
|
+
: {}),
|
|
93
120
|
askUser: (requestId, questions) => {
|
|
94
121
|
host.sendToClients(
|
|
95
122
|
withTurnScope({ type: "ask_user_request", requestId, questions }, turn, turnId)
|
package/src/ws/connection.ts
CHANGED
|
@@ -75,7 +75,14 @@ export function createWsHandlers(host: WsHost) {
|
|
|
75
75
|
host.sendMessage(ws, {
|
|
76
76
|
type: "server_hello",
|
|
77
77
|
protocolRev: PROTOCOL_REV,
|
|
78
|
-
capabilities: {
|
|
78
|
+
capabilities: {
|
|
79
|
+
multiSession: true,
|
|
80
|
+
askUser: true,
|
|
81
|
+
location: true,
|
|
82
|
+
// Advertised only when this host records activity — a client on an
|
|
83
|
+
// activity-less host knows subscribing would be pointless.
|
|
84
|
+
...(host.activity ? { activity: true } : {}),
|
|
85
|
+
},
|
|
79
86
|
});
|
|
80
87
|
|
|
81
88
|
// Snapshot-on-connect only for the single-running-session case (backward
|
|
@@ -214,6 +221,7 @@ export function createWsHandlers(host: WsHost) {
|
|
|
214
221
|
host.reportAbnormalClose(code);
|
|
215
222
|
}
|
|
216
223
|
host.clients.remove(ws);
|
|
224
|
+
host.activity?.stream.dropConnection(ws);
|
|
217
225
|
// Turns keep running in the background. Once the LAST client leaves,
|
|
218
226
|
// reject only the requests that need a live client RIGHT NOW (location,
|
|
219
227
|
// mask). Approvals and ask-user cards survive the disconnect and are
|