@schlessera/brain-ui-server 0.33.1 → 0.34.1
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 +11 -0
- package/dist/activity/sql.d.ts +34 -0
- package/dist/activity/sql.d.ts.map +1 -0
- package/dist/activity/sql.js +112 -0
- package/dist/activity/sql.js.map +1 -0
- package/dist/activity/store.d.ts.map +1 -1
- package/dist/activity/store.js +35 -112
- package/dist/activity/store.js.map +1 -1
- package/dist/agent/backend.d.ts +17 -219
- package/dist/agent/backend.d.ts.map +1 -1
- package/dist/agent/backend.js +212 -471
- package/dist/agent/backend.js.map +1 -1
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +2 -1
- package/dist/app.js.map +1 -1
- package/dist/config/env.d.ts +4 -4
- package/dist/config/env.d.ts.map +1 -1
- package/dist/routes/pi-auth.d.ts.map +1 -1
- package/dist/routes/pi-auth.js +14 -6
- package/dist/routes/pi-auth.js.map +1 -1
- package/dist/routes/web-search.d.ts.map +1 -1
- package/dist/routes/web-search.js +12 -8
- package/dist/routes/web-search.js.map +1 -1
- package/package.json +3 -3
- package/src/activity/sql.ts +120 -0
- package/src/activity/store.ts +41 -154
- package/src/agent/backend.ts +281 -793
- package/src/app.ts +2 -1
- package/src/config/env.ts +4 -4
- package/src/routes/pi-auth.ts +13 -8
- package/src/routes/web-search.ts +12 -10
package/README.md
CHANGED
|
@@ -156,6 +156,17 @@ brain repository.
|
|
|
156
156
|
server-side hidden set (`PUT /api/models/hidden`) and a manual
|
|
157
157
|
`POST /api/models/refresh`. `/api/providers` serves the same roster minus the
|
|
158
158
|
hidden entries; hidden profiles still resolve for sessions pinned to them.
|
|
159
|
+
- **Backend registry** — iterates `BackendModule` descriptors
|
|
160
|
+
(`@schlessera/brain-ui-sdk/server`) rather than knowing any backend by name.
|
|
161
|
+
Each backend owns its own profile parsing, billing rule, settings hooks and
|
|
162
|
+
optional model source. `AGENT_BACKEND` selects among the FIRST-PARTY ids only
|
|
163
|
+
(`claude`, `pi`) — it is not a package specifier, and there is no runtime
|
|
164
|
+
discovery. A third-party backend is imported and passed by value:
|
|
165
|
+
`createApp({ registry: createStaticBackendRegistry({ ... }) })`, which keeps
|
|
166
|
+
the descriptor's hooks and model source. See
|
|
167
|
+
[docs/extending/agent-backends.md](../../docs/extending/agent-backends.md).
|
|
168
|
+
A session whose stored `backend_id` names a backend this deployment does not
|
|
169
|
+
have fails explicitly instead of silently running on the default.
|
|
159
170
|
|
|
160
171
|
## Environment
|
|
161
172
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** SQL owned by the activity store, kept together so the store reads as operations. */
|
|
2
|
+
export declare const ACTIVITY_SQL: {
|
|
3
|
+
readonly nextSeq: "SELECT COALESCE(MAX(seq), 0) AS hi FROM activity_changes WHERE run_id = ?";
|
|
4
|
+
readonly insertChange: "INSERT INTO activity_changes (run_id, seq, span_id, event_index) VALUES (?, ?, ?, ?)";
|
|
5
|
+
readonly spanById: "SELECT * FROM activity_spans WHERE span_id = ?";
|
|
6
|
+
readonly spanStateById: "SELECT run_id, outcome, attrs FROM activity_spans WHERE span_id = ?";
|
|
7
|
+
readonly endSpan: "UPDATE activity_spans SET outcome = ?, outcome_reason = ?, ended_at = ?, attrs = ?,\n input_tokens = COALESCE(?, input_tokens),\n output_tokens = COALESCE(?, output_tokens),\n cache_read_tokens = COALESCE(?, cache_read_tokens),\n cache_creation_tokens = COALESCE(?, cache_creation_tokens),\n cost_usd = COALESCE(?, cost_usd),\n model = COALESCE(?, model)\n WHERE span_id = ?";
|
|
8
|
+
readonly openSpansByRun: "SELECT span_id FROM activity_spans WHERE run_id = ? AND outcome IS NULL ORDER BY started_at DESC";
|
|
9
|
+
readonly spansByRun: "SELECT * FROM activity_spans WHERE run_id = ? ORDER BY started_at";
|
|
10
|
+
readonly upsertRollup: "INSERT INTO activity_run_rollups\n (run_id, origin, name, session_id, job_name, started_at, ended_at, outcome,\n duration_ms, span_count, input_tokens, output_tokens, cache_read_tokens,\n cache_creation_tokens, cost_usd, effective_cost_usd, billing_mode,\n pricing_estimate, failure_reason, detail_pruned)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)\n ON CONFLICT(run_id) DO UPDATE SET\n ended_at = excluded.ended_at, outcome = excluded.outcome,\n duration_ms = excluded.duration_ms, span_count = excluded.span_count,\n input_tokens = excluded.input_tokens, output_tokens = excluded.output_tokens,\n cache_read_tokens = excluded.cache_read_tokens,\n cache_creation_tokens = excluded.cache_creation_tokens,\n cost_usd = COALESCE(activity_run_rollups.cost_usd, excluded.cost_usd),\n effective_cost_usd = CASE\n WHEN activity_run_rollups.effective_cost_usd IS NOT NULL\n THEN activity_run_rollups.effective_cost_usd\n WHEN activity_run_rollups.billing_mode IS NULL\n OR activity_run_rollups.billing_mode = excluded.billing_mode\n THEN excluded.effective_cost_usd\n ELSE NULL\n END,\n billing_mode = COALESCE(activity_run_rollups.billing_mode, excluded.billing_mode),\n pricing_estimate = CASE\n WHEN activity_run_rollups.pricing_estimate IS NOT NULL\n THEN activity_run_rollups.pricing_estimate\n WHEN activity_run_rollups.billing_mode IS NULL\n OR activity_run_rollups.billing_mode = excluded.billing_mode\n THEN excluded.pricing_estimate\n ELSE NULL\n END,\n failure_reason = excluded.failure_reason";
|
|
11
|
+
readonly insertSpan: "INSERT INTO activity_spans\n (span_id, run_id, parent_span_id, name, kind, origin, session_id, job_name,\n attrs, started_at, writer, last_heartbeat_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
|
12
|
+
readonly patchSpan: "UPDATE activity_spans SET attrs = ?, wait_until = COALESCE(?, wait_until),\n input_tokens = COALESCE(?, input_tokens),\n output_tokens = COALESCE(?, output_tokens),\n cache_read_tokens = COALESCE(?, cache_read_tokens),\n cache_creation_tokens = COALESCE(?, cache_creation_tokens),\n cost_usd = COALESCE(?, cost_usd),\n model = COALESCE(?, model)\n WHERE span_id = ?";
|
|
13
|
+
readonly spanRunById: "SELECT run_id FROM activity_spans WHERE span_id = ?";
|
|
14
|
+
readonly nextEventIndex: "SELECT COALESCE(MAX(event_index), -1) + 1 AS idx FROM activity_events WHERE span_id = ?";
|
|
15
|
+
readonly insertEvent: "INSERT INTO activity_events (span_id, event_index, ts, event_type, payload) VALUES (?, ?, ?, ?, ?)";
|
|
16
|
+
readonly heartbeat: "UPDATE activity_spans SET last_heartbeat_at = ? WHERE span_id = ? AND outcome IS NULL";
|
|
17
|
+
readonly openSessionRuns: "SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL AND origin = 'session'";
|
|
18
|
+
readonly staleRoots: "SELECT span_id, run_id FROM activity_spans\n WHERE outcome IS NULL AND parent_span_id IS NULL\n AND writer != ? AND COALESCE(last_heartbeat_at, started_at) < ?";
|
|
19
|
+
readonly stuckRoots: "SELECT * FROM activity_spans WHERE outcome IS NULL AND parent_span_id IS NULL AND started_at < ?";
|
|
20
|
+
readonly spanChanges: "SELECT c.change_id, c.run_id, c.seq, s.*\n FROM activity_changes c\n JOIN activity_spans s ON s.span_id = c.span_id\n WHERE c.change_id > ? AND c.event_index IS NULL\n ORDER BY c.change_id LIMIT ?";
|
|
21
|
+
readonly eventChanges: "SELECT c.change_id, c.run_id, c.seq, e.*\n FROM activity_changes c\n JOIN activity_events e\n ON e.span_id = c.span_id AND e.event_index = c.event_index\n WHERE c.change_id > ? AND c.event_index IS NOT NULL\n ORDER BY c.change_id LIMIT ?";
|
|
22
|
+
readonly terminalRootChanges: "SELECT MAX(c.change_id) AS change_id, s.*\n FROM activity_changes c\n JOIN activity_spans s ON s.span_id = c.span_id\n WHERE c.change_id > ? AND c.event_index IS NULL\n AND s.parent_span_id IS NULL AND s.outcome IS NOT NULL\n GROUP BY s.span_id\n ORDER BY change_id LIMIT ?";
|
|
23
|
+
readonly latestChangeCursor: "SELECT COALESCE(MAX(change_id), 0) AS hi FROM activity_changes";
|
|
24
|
+
readonly eventsByRun: "SELECT e.* FROM activity_events e\n JOIN activity_spans s ON s.span_id = e.span_id\n WHERE s.run_id = ? ORDER BY e.ts, e.event_index";
|
|
25
|
+
readonly openRootSpans: "SELECT * FROM activity_spans WHERE outcome IS NULL AND parent_span_id IS NULL ORDER BY started_at DESC";
|
|
26
|
+
readonly pruneCandidates: "SELECT run_id, ended_at AS ended FROM activity_run_rollups r\n WHERE detail_pruned = 0 AND ended_at IS NOT NULL\n AND ((ended_at < ? AND ended_at < ?) OR ended_at < ?)\n AND NOT EXISTS (\n SELECT 1 FROM activity_spans s\n WHERE s.run_id = r.run_id AND s.outcome IS NULL\n )\n LIMIT ?";
|
|
27
|
+
readonly markDetailPruned: "UPDATE activity_run_rollups SET detail_pruned = 1 WHERE run_id = ?";
|
|
28
|
+
readonly markDigestCoverageGap: "UPDATE activity_run_rollups SET failure_reason = COALESCE(failure_reason, 'digest coverage gap') WHERE run_id = ?";
|
|
29
|
+
readonly deleteEventsByRun: "DELETE FROM activity_events WHERE span_id IN\n (SELECT span_id FROM activity_spans WHERE run_id = ?)";
|
|
30
|
+
readonly deleteSpansByRun: "DELETE FROM activity_spans WHERE run_id = ?";
|
|
31
|
+
readonly deleteChangesByRun: "DELETE FROM activity_changes WHERE run_id = ?";
|
|
32
|
+
readonly compactChanges: "DELETE FROM activity_changes WHERE change_id < (\n SELECT COALESCE(MAX(change_id), 0) FROM activity_changes\n ) - 100000 AND run_id NOT IN (\n SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL\n )";
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql.d.ts","sourceRoot":"","sources":["../../src/activity/sql.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsHf,CAAC"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/** SQL owned by the activity store, kept together so the store reads as operations. */
|
|
2
|
+
export const ACTIVITY_SQL = {
|
|
3
|
+
nextSeq: "SELECT COALESCE(MAX(seq), 0) AS hi FROM activity_changes WHERE run_id = ?",
|
|
4
|
+
insertChange: "INSERT INTO activity_changes (run_id, seq, span_id, event_index) VALUES (?, ?, ?, ?)",
|
|
5
|
+
spanById: "SELECT * FROM activity_spans WHERE span_id = ?",
|
|
6
|
+
spanStateById: "SELECT run_id, outcome, attrs FROM activity_spans WHERE span_id = ?",
|
|
7
|
+
endSpan: `UPDATE activity_spans SET outcome = ?, outcome_reason = ?, ended_at = ?, attrs = ?,
|
|
8
|
+
input_tokens = COALESCE(?, input_tokens),
|
|
9
|
+
output_tokens = COALESCE(?, output_tokens),
|
|
10
|
+
cache_read_tokens = COALESCE(?, cache_read_tokens),
|
|
11
|
+
cache_creation_tokens = COALESCE(?, cache_creation_tokens),
|
|
12
|
+
cost_usd = COALESCE(?, cost_usd),
|
|
13
|
+
model = COALESCE(?, model)
|
|
14
|
+
WHERE span_id = ?`,
|
|
15
|
+
openSpansByRun: "SELECT span_id FROM activity_spans WHERE run_id = ? AND outcome IS NULL ORDER BY started_at DESC",
|
|
16
|
+
spansByRun: "SELECT * FROM activity_spans WHERE run_id = ? ORDER BY started_at",
|
|
17
|
+
upsertRollup: `INSERT INTO activity_run_rollups
|
|
18
|
+
(run_id, origin, name, session_id, job_name, started_at, ended_at, outcome,
|
|
19
|
+
duration_ms, span_count, input_tokens, output_tokens, cache_read_tokens,
|
|
20
|
+
cache_creation_tokens, cost_usd, effective_cost_usd, billing_mode,
|
|
21
|
+
pricing_estimate, failure_reason, detail_pruned)
|
|
22
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
|
|
23
|
+
ON CONFLICT(run_id) DO UPDATE SET
|
|
24
|
+
ended_at = excluded.ended_at, outcome = excluded.outcome,
|
|
25
|
+
duration_ms = excluded.duration_ms, span_count = excluded.span_count,
|
|
26
|
+
input_tokens = excluded.input_tokens, output_tokens = excluded.output_tokens,
|
|
27
|
+
cache_read_tokens = excluded.cache_read_tokens,
|
|
28
|
+
cache_creation_tokens = excluded.cache_creation_tokens,
|
|
29
|
+
cost_usd = COALESCE(activity_run_rollups.cost_usd, excluded.cost_usd),
|
|
30
|
+
effective_cost_usd = CASE
|
|
31
|
+
WHEN activity_run_rollups.effective_cost_usd IS NOT NULL
|
|
32
|
+
THEN activity_run_rollups.effective_cost_usd
|
|
33
|
+
WHEN activity_run_rollups.billing_mode IS NULL
|
|
34
|
+
OR activity_run_rollups.billing_mode = excluded.billing_mode
|
|
35
|
+
THEN excluded.effective_cost_usd
|
|
36
|
+
ELSE NULL
|
|
37
|
+
END,
|
|
38
|
+
billing_mode = COALESCE(activity_run_rollups.billing_mode, excluded.billing_mode),
|
|
39
|
+
pricing_estimate = CASE
|
|
40
|
+
WHEN activity_run_rollups.pricing_estimate IS NOT NULL
|
|
41
|
+
THEN activity_run_rollups.pricing_estimate
|
|
42
|
+
WHEN activity_run_rollups.billing_mode IS NULL
|
|
43
|
+
OR activity_run_rollups.billing_mode = excluded.billing_mode
|
|
44
|
+
THEN excluded.pricing_estimate
|
|
45
|
+
ELSE NULL
|
|
46
|
+
END,
|
|
47
|
+
failure_reason = excluded.failure_reason`,
|
|
48
|
+
insertSpan: `INSERT INTO activity_spans
|
|
49
|
+
(span_id, run_id, parent_span_id, name, kind, origin, session_id, job_name,
|
|
50
|
+
attrs, started_at, writer, last_heartbeat_at)
|
|
51
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
52
|
+
patchSpan: `UPDATE activity_spans SET attrs = ?, wait_until = COALESCE(?, wait_until),
|
|
53
|
+
input_tokens = COALESCE(?, input_tokens),
|
|
54
|
+
output_tokens = COALESCE(?, output_tokens),
|
|
55
|
+
cache_read_tokens = COALESCE(?, cache_read_tokens),
|
|
56
|
+
cache_creation_tokens = COALESCE(?, cache_creation_tokens),
|
|
57
|
+
cost_usd = COALESCE(?, cost_usd),
|
|
58
|
+
model = COALESCE(?, model)
|
|
59
|
+
WHERE span_id = ?`,
|
|
60
|
+
spanRunById: "SELECT run_id FROM activity_spans WHERE span_id = ?",
|
|
61
|
+
nextEventIndex: "SELECT COALESCE(MAX(event_index), -1) + 1 AS idx FROM activity_events WHERE span_id = ?",
|
|
62
|
+
insertEvent: "INSERT INTO activity_events (span_id, event_index, ts, event_type, payload) VALUES (?, ?, ?, ?, ?)",
|
|
63
|
+
heartbeat: "UPDATE activity_spans SET last_heartbeat_at = ? WHERE span_id = ? AND outcome IS NULL",
|
|
64
|
+
openSessionRuns: "SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL AND origin = 'session'",
|
|
65
|
+
staleRoots: `SELECT span_id, run_id FROM activity_spans
|
|
66
|
+
WHERE outcome IS NULL AND parent_span_id IS NULL
|
|
67
|
+
AND writer != ? AND COALESCE(last_heartbeat_at, started_at) < ?`,
|
|
68
|
+
stuckRoots: "SELECT * FROM activity_spans WHERE outcome IS NULL AND parent_span_id IS NULL AND started_at < ?",
|
|
69
|
+
spanChanges: `SELECT c.change_id, c.run_id, c.seq, s.*
|
|
70
|
+
FROM activity_changes c
|
|
71
|
+
JOIN activity_spans s ON s.span_id = c.span_id
|
|
72
|
+
WHERE c.change_id > ? AND c.event_index IS NULL
|
|
73
|
+
ORDER BY c.change_id LIMIT ?`,
|
|
74
|
+
eventChanges: `SELECT c.change_id, c.run_id, c.seq, e.*
|
|
75
|
+
FROM activity_changes c
|
|
76
|
+
JOIN activity_events e
|
|
77
|
+
ON e.span_id = c.span_id AND e.event_index = c.event_index
|
|
78
|
+
WHERE c.change_id > ? AND c.event_index IS NOT NULL
|
|
79
|
+
ORDER BY c.change_id LIMIT ?`,
|
|
80
|
+
terminalRootChanges: `SELECT MAX(c.change_id) AS change_id, s.*
|
|
81
|
+
FROM activity_changes c
|
|
82
|
+
JOIN activity_spans s ON s.span_id = c.span_id
|
|
83
|
+
WHERE c.change_id > ? AND c.event_index IS NULL
|
|
84
|
+
AND s.parent_span_id IS NULL AND s.outcome IS NOT NULL
|
|
85
|
+
GROUP BY s.span_id
|
|
86
|
+
ORDER BY change_id LIMIT ?`,
|
|
87
|
+
latestChangeCursor: "SELECT COALESCE(MAX(change_id), 0) AS hi FROM activity_changes",
|
|
88
|
+
eventsByRun: `SELECT e.* FROM activity_events e
|
|
89
|
+
JOIN activity_spans s ON s.span_id = e.span_id
|
|
90
|
+
WHERE s.run_id = ? ORDER BY e.ts, e.event_index`,
|
|
91
|
+
openRootSpans: "SELECT * FROM activity_spans WHERE outcome IS NULL AND parent_span_id IS NULL ORDER BY started_at DESC",
|
|
92
|
+
pruneCandidates: `SELECT run_id, ended_at AS ended FROM activity_run_rollups r
|
|
93
|
+
WHERE detail_pruned = 0 AND ended_at IS NOT NULL
|
|
94
|
+
AND ((ended_at < ? AND ended_at < ?) OR ended_at < ?)
|
|
95
|
+
AND NOT EXISTS (
|
|
96
|
+
SELECT 1 FROM activity_spans s
|
|
97
|
+
WHERE s.run_id = r.run_id AND s.outcome IS NULL
|
|
98
|
+
)
|
|
99
|
+
LIMIT ?`,
|
|
100
|
+
markDetailPruned: "UPDATE activity_run_rollups SET detail_pruned = 1 WHERE run_id = ?",
|
|
101
|
+
markDigestCoverageGap: "UPDATE activity_run_rollups SET failure_reason = COALESCE(failure_reason, 'digest coverage gap') WHERE run_id = ?",
|
|
102
|
+
deleteEventsByRun: `DELETE FROM activity_events WHERE span_id IN
|
|
103
|
+
(SELECT span_id FROM activity_spans WHERE run_id = ?)`,
|
|
104
|
+
deleteSpansByRun: "DELETE FROM activity_spans WHERE run_id = ?",
|
|
105
|
+
deleteChangesByRun: "DELETE FROM activity_changes WHERE run_id = ?",
|
|
106
|
+
compactChanges: `DELETE FROM activity_changes WHERE change_id < (
|
|
107
|
+
SELECT COALESCE(MAX(change_id), 0) FROM activity_changes
|
|
108
|
+
) - 100000 AND run_id NOT IN (
|
|
109
|
+
SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL
|
|
110
|
+
)`,
|
|
111
|
+
};
|
|
112
|
+
//# sourceMappingURL=sql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql.js","sourceRoot":"","sources":["../../src/activity/sql.ts"],"names":[],"mappings":"AAAA,uFAAuF;AACvF,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,OAAO,EAAE,2EAA2E;IACpF,YAAY,EACV,sFAAsF;IACxF,QAAQ,EAAE,gDAAgD;IAC1D,aAAa,EAAE,qEAAqE;IACpF,OAAO,EAAE;;;;;;;yBAOc;IACvB,cAAc,EACZ,kGAAkG;IACpG,UAAU,EAAE,mEAAmE;IAC/E,YAAY,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kDA8BkC;IAChD,UAAU,EAAE;;;uDAGyC;IACrD,SAAS,EAAE;;;;;;;6BAOgB;IAC3B,WAAW,EAAE,qDAAqD;IAClE,cAAc,EACZ,yFAAyF;IAC3F,WAAW,EACT,oGAAoG;IACtG,SAAS,EACP,uFAAuF;IACzF,eAAe,EACb,yFAAyF;IAC3F,UAAU,EAAE;;6EAE+D;IAC3E,UAAU,EACR,kGAAkG;IACpG,WAAW,EAAE;;;;wCAIyB;IACtC,YAAY,EAAE;;;;;wCAKwB;IACtC,mBAAmB,EAAE;;;;;;sCAMe;IACpC,kBAAkB,EAAE,gEAAgE;IACpF,WAAW,EAAE;;+DAEgD;IAC7D,aAAa,EACX,wGAAwG;IAC1G,eAAe,EAAE;;;;;;;qBAOE;IACnB,gBAAgB,EAAE,oEAAoE;IACtF,qBAAqB,EACnB,mHAAmH;IACrH,iBAAiB,EAAE;qEACgD;IACnE,gBAAgB,EAAE,6CAA6C;IAC/D,kBAAkB,EAAE,+CAA+C;IACnE,cAAc,EAAE;;;;aAIL;CACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":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vC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;IACxC,aAAa,IAAI,OAAO,EAAE,CAAC;IAC3B,8EAA8E;IAC9E,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,KAAK,CAAC,OAAO,EAAE,YAAY,GAAG;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;CAC5E;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAAC;CAC/C;AAED,MAAM,WAAW,0BAA0B;IACzC,gEAAgE;IAChE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;;OAQG;IACH,OAAO,CAAC,EAAE,aAAa,CAAC;CACzB;AAED,wBAAgB,mBAAmB,CACjC,EAAE,EAAE,QAAQ,EACZ,OAAO,GAAE,0BAA+B,GACvC,aAAa,CA6ff"}
|
package/dist/activity/store.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isBillingMode } from "@schlessera/brain-ui-sdk/protocol";
|
|
2
2
|
import { resolveAmbientBillingMode, resolveStandalonePricingConfig, } from "../config/env.js";
|
|
3
3
|
import { createModelPricing } from "../pricing/model-pricing.js";
|
|
4
|
+
import { ACTIVITY_SQL } from "./sql.js";
|
|
4
5
|
export const SPAN_OUTCOMES = [
|
|
5
6
|
"success",
|
|
6
7
|
"error",
|
|
@@ -69,12 +70,12 @@ export function createActivityStore(db, options = {}) {
|
|
|
69
70
|
const inWrite = (fn) => db.transaction(fn).immediate();
|
|
70
71
|
function nextSeq(runId) {
|
|
71
72
|
const row = db
|
|
72
|
-
.query(
|
|
73
|
+
.query(ACTIVITY_SQL.nextSeq)
|
|
73
74
|
.get(runId);
|
|
74
75
|
return row.hi + 1;
|
|
75
76
|
}
|
|
76
77
|
function logChange(runId, seq, spanId, eventIndex) {
|
|
77
|
-
db.query(
|
|
78
|
+
db.query(ACTIVITY_SQL.insertChange).run(runId, seq, spanId, eventIndex ?? null);
|
|
78
79
|
}
|
|
79
80
|
function rowToSpan(r) {
|
|
80
81
|
return {
|
|
@@ -116,18 +117,18 @@ export function createActivityStore(db, options = {}) {
|
|
|
116
117
|
};
|
|
117
118
|
}
|
|
118
119
|
function getSpanRaw(spanId) {
|
|
119
|
-
const r = db.query(
|
|
120
|
+
const r = db.query(ACTIVITY_SQL.spanById).get(spanId);
|
|
120
121
|
return r ? rowToSpan(r) : null;
|
|
121
122
|
}
|
|
122
123
|
function runHighWaterSeqRaw(runId) {
|
|
123
124
|
const row = db
|
|
124
|
-
.query(
|
|
125
|
+
.query(ACTIVITY_SQL.nextSeq)
|
|
125
126
|
.get(runId);
|
|
126
127
|
return row.hi;
|
|
127
128
|
}
|
|
128
129
|
function endSpanInTx(spanId, input) {
|
|
129
130
|
const existing = db
|
|
130
|
-
.query(
|
|
131
|
+
.query(ACTIVITY_SQL.spanStateById)
|
|
131
132
|
.get(spanId);
|
|
132
133
|
if (!existing || existing.outcome !== null)
|
|
133
134
|
return false;
|
|
@@ -136,21 +137,14 @@ export function createActivityStore(db, options = {}) {
|
|
|
136
137
|
? JSON.stringify({ ...(safeParse(existing.attrs) ?? {}), ...input.attrs })
|
|
137
138
|
: existing.attrs;
|
|
138
139
|
const u = input.usage ?? {};
|
|
139
|
-
db.query(
|
|
140
|
-
input_tokens = COALESCE(?, input_tokens),
|
|
141
|
-
output_tokens = COALESCE(?, output_tokens),
|
|
142
|
-
cache_read_tokens = COALESCE(?, cache_read_tokens),
|
|
143
|
-
cache_creation_tokens = COALESCE(?, cache_creation_tokens),
|
|
144
|
-
cost_usd = COALESCE(?, cost_usd),
|
|
145
|
-
model = COALESCE(?, model)
|
|
146
|
-
WHERE span_id = ?`).run(input.outcome, input.reason ?? null, endedAt, attrs, u.inputTokens ?? null, u.outputTokens ?? null, u.cacheReadTokens ?? null, u.cacheCreationTokens ?? null, u.costUsd ?? null, u.model ?? null, spanId);
|
|
140
|
+
db.query(ACTIVITY_SQL.endSpan).run(input.outcome, input.reason ?? null, endedAt, attrs, u.inputTokens ?? null, u.outputTokens ?? null, u.cacheReadTokens ?? null, u.cacheCreationTokens ?? null, u.costUsd ?? null, u.model ?? null, spanId);
|
|
147
141
|
logChange(existing.run_id, nextSeq(existing.run_id), spanId);
|
|
148
142
|
return true;
|
|
149
143
|
}
|
|
150
144
|
/** Close every open span of the run (children first) and roll it up. */
|
|
151
145
|
function closeRunInTx(runId, outcome, reason) {
|
|
152
146
|
const open = db
|
|
153
|
-
.query(
|
|
147
|
+
.query(ACTIVITY_SQL.openSpansByRun)
|
|
154
148
|
.all(runId);
|
|
155
149
|
let closed = 0;
|
|
156
150
|
for (const { span_id } of open) {
|
|
@@ -163,7 +157,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
163
157
|
}
|
|
164
158
|
function rollupRunInTx(runId) {
|
|
165
159
|
const spans = db
|
|
166
|
-
.query(
|
|
160
|
+
.query(ACTIVITY_SQL.spansByRun)
|
|
167
161
|
.all(runId)
|
|
168
162
|
.map(rowToSpan);
|
|
169
163
|
if (spans.length === 0)
|
|
@@ -214,37 +208,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
214
208
|
? (priced.estimate ? 1 : 0)
|
|
215
209
|
: null;
|
|
216
210
|
const costUsd = root.usage.costUsd ?? priced?.costUsd ?? null;
|
|
217
|
-
db.query(
|
|
218
|
-
(run_id, origin, name, session_id, job_name, started_at, ended_at, outcome,
|
|
219
|
-
duration_ms, span_count, input_tokens, output_tokens, cache_read_tokens,
|
|
220
|
-
cache_creation_tokens, cost_usd, effective_cost_usd, billing_mode,
|
|
221
|
-
pricing_estimate, failure_reason, detail_pruned)
|
|
222
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)
|
|
223
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
224
|
-
ended_at = excluded.ended_at, outcome = excluded.outcome,
|
|
225
|
-
duration_ms = excluded.duration_ms, span_count = excluded.span_count,
|
|
226
|
-
input_tokens = excluded.input_tokens, output_tokens = excluded.output_tokens,
|
|
227
|
-
cache_read_tokens = excluded.cache_read_tokens,
|
|
228
|
-
cache_creation_tokens = excluded.cache_creation_tokens,
|
|
229
|
-
cost_usd = COALESCE(activity_run_rollups.cost_usd, excluded.cost_usd),
|
|
230
|
-
effective_cost_usd = CASE
|
|
231
|
-
WHEN activity_run_rollups.effective_cost_usd IS NOT NULL
|
|
232
|
-
THEN activity_run_rollups.effective_cost_usd
|
|
233
|
-
WHEN activity_run_rollups.billing_mode IS NULL
|
|
234
|
-
OR activity_run_rollups.billing_mode = excluded.billing_mode
|
|
235
|
-
THEN excluded.effective_cost_usd
|
|
236
|
-
ELSE NULL
|
|
237
|
-
END,
|
|
238
|
-
billing_mode = COALESCE(activity_run_rollups.billing_mode, excluded.billing_mode),
|
|
239
|
-
pricing_estimate = CASE
|
|
240
|
-
WHEN activity_run_rollups.pricing_estimate IS NOT NULL
|
|
241
|
-
THEN activity_run_rollups.pricing_estimate
|
|
242
|
-
WHEN activity_run_rollups.billing_mode IS NULL
|
|
243
|
-
OR activity_run_rollups.billing_mode = excluded.billing_mode
|
|
244
|
-
THEN excluded.pricing_estimate
|
|
245
|
-
ELSE NULL
|
|
246
|
-
END,
|
|
247
|
-
failure_reason = excluded.failure_reason`
|
|
211
|
+
db.query(ACTIVITY_SQL.upsertRollup
|
|
248
212
|
// Every cost column is FROZEN at first non-NULL write (AE5): a re-rollup
|
|
249
213
|
// after a pricing refresh must not silently reprice history — only a
|
|
250
214
|
// still-NULL slot may be filled by a later computation. cost_usd gets
|
|
@@ -262,10 +226,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
262
226
|
startSpan(input) {
|
|
263
227
|
return inWrite(() => {
|
|
264
228
|
const startedAt = input.startedAt ?? Date.now();
|
|
265
|
-
db.query(
|
|
266
|
-
(span_id, run_id, parent_span_id, name, kind, origin, session_id, job_name,
|
|
267
|
-
attrs, started_at, writer, last_heartbeat_at)
|
|
268
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(input.spanId, input.runId, input.parentSpanId ?? null, input.name, input.kind, input.origin, input.sessionId ?? null, input.jobName ?? null, input.attrs ? JSON.stringify(input.attrs) : null, startedAt, writer, input.parentSpanId ? null : startedAt);
|
|
229
|
+
db.query(ACTIVITY_SQL.insertSpan).run(input.spanId, input.runId, input.parentSpanId ?? null, input.name, input.kind, input.origin, input.sessionId ?? null, input.jobName ?? null, input.attrs ? JSON.stringify(input.attrs) : null, startedAt, writer, input.parentSpanId ? null : startedAt);
|
|
269
230
|
logChange(input.runId, nextSeq(input.runId), input.spanId);
|
|
270
231
|
return getSpanRaw(input.spanId);
|
|
271
232
|
});
|
|
@@ -276,7 +237,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
276
237
|
patchSpan(spanId, patch) {
|
|
277
238
|
return inWrite(() => {
|
|
278
239
|
const existing = db
|
|
279
|
-
.query(
|
|
240
|
+
.query(ACTIVITY_SQL.spanStateById)
|
|
280
241
|
.get(spanId);
|
|
281
242
|
if (!existing || existing.outcome !== null)
|
|
282
243
|
return false;
|
|
@@ -284,14 +245,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
284
245
|
? JSON.stringify({ ...(safeParse(existing.attrs) ?? {}), ...patch.attrs })
|
|
285
246
|
: existing.attrs;
|
|
286
247
|
const u = patch.usage ?? {};
|
|
287
|
-
db.query(
|
|
288
|
-
input_tokens = COALESCE(?, input_tokens),
|
|
289
|
-
output_tokens = COALESCE(?, output_tokens),
|
|
290
|
-
cache_read_tokens = COALESCE(?, cache_read_tokens),
|
|
291
|
-
cache_creation_tokens = COALESCE(?, cache_creation_tokens),
|
|
292
|
-
cost_usd = COALESCE(?, cost_usd),
|
|
293
|
-
model = COALESCE(?, model)
|
|
294
|
-
WHERE span_id = ?`).run(attrs, patch.waitUntil ?? null, u.inputTokens ?? null, u.outputTokens ?? null, u.cacheReadTokens ?? null, u.cacheCreationTokens ?? null, u.costUsd ?? null, u.model ?? null, spanId);
|
|
248
|
+
db.query(ACTIVITY_SQL.patchSpan).run(attrs, patch.waitUntil ?? null, u.inputTokens ?? null, u.outputTokens ?? null, u.cacheReadTokens ?? null, u.cacheCreationTokens ?? null, u.costUsd ?? null, u.model ?? null, spanId);
|
|
295
249
|
logChange(existing.run_id, nextSeq(existing.run_id), spanId);
|
|
296
250
|
return true;
|
|
297
251
|
});
|
|
@@ -299,16 +253,16 @@ export function createActivityStore(db, options = {}) {
|
|
|
299
253
|
appendEvent(spanId, eventType, payload, ts, cap) {
|
|
300
254
|
return inWrite(() => {
|
|
301
255
|
const span = db
|
|
302
|
-
.query(
|
|
256
|
+
.query(ACTIVITY_SQL.spanRunById)
|
|
303
257
|
.get(spanId);
|
|
304
258
|
if (!span)
|
|
305
259
|
return null;
|
|
306
260
|
const next = db
|
|
307
|
-
.query(
|
|
261
|
+
.query(ACTIVITY_SQL.nextEventIndex)
|
|
308
262
|
.get(spanId);
|
|
309
263
|
const stored = capPayload(payload, cap);
|
|
310
264
|
const at = ts ?? Date.now();
|
|
311
|
-
db.query(
|
|
265
|
+
db.query(ACTIVITY_SQL.insertEvent).run(spanId, next.idx, at, eventType, JSON.stringify(stored));
|
|
312
266
|
logChange(span.run_id, nextSeq(span.run_id), spanId, next.idx);
|
|
313
267
|
// Built from the values just written — no read-back needed.
|
|
314
268
|
return {
|
|
@@ -325,7 +279,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
325
279
|
inWrite(() => {
|
|
326
280
|
// Deliberately NOT change-logged: a heartbeat is liveness metadata,
|
|
327
281
|
// not activity the client needs a delta for.
|
|
328
|
-
db.query(
|
|
282
|
+
db.query(ACTIVITY_SQL.heartbeat).run(at ?? Date.now(), spanId);
|
|
329
283
|
});
|
|
330
284
|
},
|
|
331
285
|
cascadeClose(runId, outcome, reason) {
|
|
@@ -346,7 +300,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
346
300
|
// loop re-checks open-ness inside the transaction (endSpanInTx is
|
|
347
301
|
// write-once), so a race just skips.
|
|
348
302
|
const open = db
|
|
349
|
-
.query(
|
|
303
|
+
.query(ACTIVITY_SQL.openSessionRuns)
|
|
350
304
|
.all();
|
|
351
305
|
if (open.length === 0)
|
|
352
306
|
return 0;
|
|
@@ -365,9 +319,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
365
319
|
// legitimately long, quiet run with a live writer is never killed.
|
|
366
320
|
// Candidate read outside the write transaction, same as sweepOwnOrphans.
|
|
367
321
|
const staleRoots = db
|
|
368
|
-
.query(
|
|
369
|
-
WHERE outcome IS NULL AND parent_span_id IS NULL
|
|
370
|
-
AND writer != ? AND COALESCE(last_heartbeat_at, started_at) < ?`)
|
|
322
|
+
.query(ACTIVITY_SQL.staleRoots)
|
|
371
323
|
.all(writer, cutoff);
|
|
372
324
|
if (staleRoots.length === 0)
|
|
373
325
|
return 0;
|
|
@@ -383,7 +335,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
383
335
|
findStuck(thresholdMs, now) {
|
|
384
336
|
const cutoff = (now ?? Date.now()) - thresholdMs;
|
|
385
337
|
return db
|
|
386
|
-
.query(
|
|
338
|
+
.query(ACTIVITY_SQL.stuckRoots)
|
|
387
339
|
.all(cutoff).map(rowToSpan);
|
|
388
340
|
},
|
|
389
341
|
changesSince(changeCursor, limit = 500) {
|
|
@@ -391,19 +343,10 @@ export function createActivityStore(db, options = {}) {
|
|
|
391
343
|
// change whose span/event row was pruned drops out via the join, same
|
|
392
344
|
// as the old per-row miss.
|
|
393
345
|
const spanRows = db
|
|
394
|
-
.query(
|
|
395
|
-
FROM activity_changes c
|
|
396
|
-
JOIN activity_spans s ON s.span_id = c.span_id
|
|
397
|
-
WHERE c.change_id > ? AND c.event_index IS NULL
|
|
398
|
-
ORDER BY c.change_id LIMIT ?`)
|
|
346
|
+
.query(ACTIVITY_SQL.spanChanges)
|
|
399
347
|
.all(changeCursor, limit);
|
|
400
348
|
const eventRows = db
|
|
401
|
-
.query(
|
|
402
|
-
FROM activity_changes c
|
|
403
|
-
JOIN activity_events e
|
|
404
|
-
ON e.span_id = c.span_id AND e.event_index = c.event_index
|
|
405
|
-
WHERE c.change_id > ? AND c.event_index IS NOT NULL
|
|
406
|
-
ORDER BY c.change_id LIMIT ?`)
|
|
349
|
+
.query(ACTIVITY_SQL.eventChanges)
|
|
407
350
|
.all(changeCursor, limit);
|
|
408
351
|
const changes = [
|
|
409
352
|
...spanRows.map((r) => ({
|
|
@@ -426,19 +369,13 @@ export function createActivityStore(db, options = {}) {
|
|
|
426
369
|
},
|
|
427
370
|
terminalRootChangesSince(changeCursor, limit = 500) {
|
|
428
371
|
const rows = db
|
|
429
|
-
.query(
|
|
430
|
-
FROM activity_changes c
|
|
431
|
-
JOIN activity_spans s ON s.span_id = c.span_id
|
|
432
|
-
WHERE c.change_id > ? AND c.event_index IS NULL
|
|
433
|
-
AND s.parent_span_id IS NULL AND s.outcome IS NOT NULL
|
|
434
|
-
GROUP BY s.span_id
|
|
435
|
-
ORDER BY change_id LIMIT ?`)
|
|
372
|
+
.query(ACTIVITY_SQL.terminalRootChanges)
|
|
436
373
|
.all(changeCursor, limit);
|
|
437
374
|
return rows.map((r) => ({ changeId: r.change_id, span: rowToSpan(r) }));
|
|
438
375
|
},
|
|
439
376
|
latestChangeCursor() {
|
|
440
377
|
const row = db
|
|
441
|
-
.query(
|
|
378
|
+
.query(ACTIVITY_SQL.latestChangeCursor)
|
|
442
379
|
.get();
|
|
443
380
|
return row.hi;
|
|
444
381
|
},
|
|
@@ -448,17 +385,15 @@ export function createActivityStore(db, options = {}) {
|
|
|
448
385
|
// exactly this moment (no gap between snapshot and first delta).
|
|
449
386
|
return db.transaction(() => {
|
|
450
387
|
const spans = db
|
|
451
|
-
.query(
|
|
388
|
+
.query(ACTIVITY_SQL.spansByRun)
|
|
452
389
|
.all(runId).map(rowToSpan);
|
|
453
390
|
if (spans.length === 0)
|
|
454
391
|
return null;
|
|
455
392
|
const events = db
|
|
456
|
-
.query(
|
|
457
|
-
JOIN activity_spans s ON s.span_id = e.span_id
|
|
458
|
-
WHERE s.run_id = ? ORDER BY e.ts, e.event_index`)
|
|
393
|
+
.query(ACTIVITY_SQL.eventsByRun)
|
|
459
394
|
.all(runId).map(rowToEvent);
|
|
460
395
|
const cursor = db
|
|
461
|
-
.query(
|
|
396
|
+
.query(ACTIVITY_SQL.latestChangeCursor)
|
|
462
397
|
.get();
|
|
463
398
|
return {
|
|
464
399
|
runId,
|
|
@@ -473,7 +408,7 @@ export function createActivityStore(db, options = {}) {
|
|
|
473
408
|
getSpan: getSpanRaw,
|
|
474
409
|
openRootSpans() {
|
|
475
410
|
return db
|
|
476
|
-
.query(
|
|
411
|
+
.query(ACTIVITY_SQL.openRootSpans)
|
|
477
412
|
.all().map(rowToSpan);
|
|
478
413
|
},
|
|
479
414
|
rollupRun(runId) {
|
|
@@ -500,36 +435,24 @@ export function createActivityStore(db, options = {}) {
|
|
|
500
435
|
// run — that terminal write is what made the run prunable), guarded
|
|
501
436
|
// against any run that still has an open span.
|
|
502
437
|
const candidates = db
|
|
503
|
-
.query(
|
|
504
|
-
WHERE detail_pruned = 0 AND ended_at IS NOT NULL
|
|
505
|
-
AND ((ended_at < ? AND ended_at < ?) OR ended_at < ?)
|
|
506
|
-
AND NOT EXISTS (
|
|
507
|
-
SELECT 1 FROM activity_spans s
|
|
508
|
-
WHERE s.run_id = r.run_id AND s.outcome IS NULL
|
|
509
|
-
)
|
|
510
|
-
LIMIT ?`)
|
|
438
|
+
.query(ACTIVITY_SQL.pruneCandidates)
|
|
511
439
|
.all(floor, retentionCutoff, ceiling, batch);
|
|
512
440
|
let spansDeleted = 0;
|
|
513
441
|
for (const { run_id, ended } of candidates) {
|
|
514
|
-
db.query(
|
|
442
|
+
db.query(ACTIVITY_SQL.markDetailPruned).run(run_id);
|
|
515
443
|
if (ended >= floor) {
|
|
516
444
|
// Pruned by the hard ceiling alone — the digest never covered
|
|
517
445
|
// this run; make the coverage gap visible on the rollup.
|
|
518
|
-
db.query(
|
|
446
|
+
db.query(ACTIVITY_SQL.markDigestCoverageGap).run(run_id);
|
|
519
447
|
}
|
|
520
|
-
db.query(
|
|
521
|
-
|
|
522
|
-
const res = db.query("DELETE FROM activity_spans WHERE run_id = ?").run(run_id);
|
|
448
|
+
db.query(ACTIVITY_SQL.deleteEventsByRun).run(run_id);
|
|
449
|
+
const res = db.query(ACTIVITY_SQL.deleteSpansByRun).run(run_id);
|
|
523
450
|
spansDeleted += res.changes;
|
|
524
|
-
db.query(
|
|
451
|
+
db.query(ACTIVITY_SQL.deleteChangesByRun).run(run_id);
|
|
525
452
|
}
|
|
526
453
|
// The change log only serves live polling; rows this deep in the past
|
|
527
454
|
// are unreachable by any live cursor. Keep runs with open spans.
|
|
528
|
-
db.query(
|
|
529
|
-
SELECT COALESCE(MAX(change_id), 0) FROM activity_changes
|
|
530
|
-
) - 100000 AND run_id NOT IN (
|
|
531
|
-
SELECT DISTINCT run_id FROM activity_spans WHERE outcome IS NULL
|
|
532
|
-
)`).run();
|
|
455
|
+
db.query(ACTIVITY_SQL.compactChanges).run();
|
|
533
456
|
return { runsPruned: candidates.length, spansDeleted };
|
|
534
457
|
});
|
|
535
458
|
},
|