memhouse 0.18.12 → 0.20.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/AGENTS.md +22 -151
- package/CHANGELOG.md +263 -29
- package/LICENSE +202 -22
- package/LICENSES/agentlytics-MIT.txt +21 -0
- package/LICENSES/memhouse-MIT-until-0.18.11.txt +22 -0
- package/LICENSES/models.dev-MIT.txt +22 -0
- package/NOTICE +28 -0
- package/README.md +42 -20
- package/SECURITY.md +21 -8
- package/bin/memhouse.js +2223 -311
- package/docs/README.md +11 -9
- package/docs/guides/admin-password.md +37 -10
- package/docs/guides/configuration.md +13 -6
- package/docs/guides/daemons.md +64 -3
- package/docs/guides/import-claude-ai.md +204 -0
- package/docs/guides/install.md +11 -6
- package/docs/guides/invite.md +21 -7
- package/docs/guides/operate.md +114 -7
- package/docs/guides/search-and-resume.md +72 -3
- package/docs/guides/share.md +91 -52
- package/docs/guides/storage.md +44 -4
- package/docs/guides/troubleshooting.md +121 -14
- package/docs/guides/uninstall.md +4 -3
- package/docs/guides/update.md +16 -0
- package/docs/guides/upgrade-to-0.20.md +188 -0
- package/docs/releases/README.md +2 -1
- package/docs/releases/v0.18.10.md +5 -5
- package/docs/releases/v0.18.11.md +3 -3
- package/docs/releases/v0.20.0.md +627 -0
- package/docs/releases/v0.20.1.md +114 -0
- package/docs/tutorials/01-first-house.md +10 -7
- package/docs/tutorials/02-join-a-house.md +24 -15
- package/docs/tutorials/03-share-your-memory.md +40 -31
- package/editors/antigravity.js +1 -80
- package/editors/base.js +463 -9
- package/editors/bridge.js +119 -0
- package/editors/claude.js +273 -105
- package/editors/codex.js +278 -52
- package/editors/copilot.js +1 -70
- package/editors/cursor.js +1 -75
- package/editors/derive-fingerprints.json +18 -0
- package/editors/harness-paths.js +69 -0
- package/editors/index.js +1 -20
- package/editors/normalise/agy.js +910 -0
- package/editors/normalise/copilot-cli.js +321 -0
- package/editors/normalise/cursor-agent.js +852 -0
- package/editors/normalise/cursor.js +474 -0
- package/editors/normalise/devin.js +575 -0
- package/editors/normalise/gemini-cli.js +602 -0
- package/editors/normalise/goose.js +673 -0
- package/editors/normalise/opencode.js +633 -0
- package/editors/normalise/vscode.js +549 -0
- package/editors/normalise/zed.js +473 -0
- package/editors/opencode-v4.js +119 -0
- package/editors/scope.js +14 -3
- package/editors/sessions-log.js +349 -0
- package/editors/sqlite.js +28 -1
- package/editors/vscode.js +1 -72
- package/editors/windsurf.js +1 -143
- package/examples/01-local-one-machine/README.md +24 -19
- package/examples/01-local-one-machine/run.sh +1 -1
- package/examples/02-existing-clickhouse/README.md +30 -22
- package/examples/02-existing-clickhouse/run.sh +5 -4
- package/examples/03-team-house/README.md +44 -37
- package/examples/03-team-house/run.sh +35 -18
- package/examples/04-self-hosted-team/README.md +29 -14
- package/examples/04-self-hosted-team/compose.yml +5 -2
- package/examples/04-self-hosted-team/smoke.sh +26 -12
- package/examples/README.md +8 -4
- package/examples/fixtures/README.md +4 -1
- package/examples/fixtures/make-sessions.js +8 -5
- package/memhouse/admin-secret.js +89 -24
- package/memhouse/argv-secrets.js +64 -0
- package/memhouse/channel.js +49 -9
- package/memhouse/codex-plugin.js +194 -0
- package/memhouse/delivery/AGENT-INSTALL-PROMPT.md +2 -1
- package/memhouse/delivery/AGENT-INSTALL.md +22 -11
- package/memhouse/delivery/INVITATION.md.tpl +44 -45
- package/memhouse/delivery/PROMPT.md +53 -5
- package/memhouse/delivery/UPGRADE.md.tpl +1 -1
- package/memhouse/delivery/codex/AGENTS-BLOCK.md +11 -0
- package/memhouse/delivery/codex/SKILL.md +50 -0
- package/memhouse/delivery/kernel-install.md +17 -9
- package/memhouse/delivery/plugin/.claude-plugin/plugin.json +2 -2
- package/memhouse/delivery/plugin/reference/HOUSE.md +168 -41
- package/memhouse/delivery/plugin/skills/access/SKILL.md +13 -5
- package/memhouse/delivery/plugin/skills/admin/SKILL.md +14 -6
- package/memhouse/delivery/plugin/skills/house/SKILL.md +25 -8
- package/memhouse/delivery/plugin/skills/recall/SKILL.md +66 -8
- package/memhouse/delivery/plugin/skills/sql/SKILL.md +66 -7
- package/memhouse/deploy.js +1 -1
- package/memhouse/flags.js +35 -18
- package/memhouse/help.js +37 -0
- package/memhouse/host.js +82 -2
- package/memhouse/house/HOUSE.md +2 -2
- package/memhouse/house/convert-profile.js +638 -0
- package/memhouse/house/convert-v3.js +742 -0
- package/memhouse/house/doctor-v3.js +324 -0
- package/memhouse/house/host-alias.js +85 -0
- package/memhouse/house/house.js +3 -3
- package/memhouse/house/migrate.js +2 -2
- package/memhouse/house/profile-dedupe.js +183 -0
- package/memhouse/house/profile-edges.js +554 -0
- package/memhouse/house/profile-house.js +430 -0
- package/memhouse/house/profile-rollup.js +190 -0
- package/memhouse/house/q-cli.js +863 -0
- package/memhouse/house/queries-v3.js +1013 -0
- package/memhouse/house/reconcile-v3.js +192 -0
- package/memhouse/house/records-to-profile.js +174 -0
- package/memhouse/house/records-to-rows.js +169 -0
- package/memhouse/house/relocate.js +2 -1
- package/memhouse/house/schema-choice.js +121 -0
- package/memhouse/house/schema-profile.js +290 -0
- package/memhouse/house/schema-profile.sql.tpl +398 -0
- package/memhouse/house/schema-v3.js +267 -0
- package/memhouse/house/schema-v3.sql.tpl +213 -0
- package/memhouse/import/claude-ai.js +420 -0
- package/memhouse/import/import.js +372 -0
- package/memhouse/import/pyjson.js +206 -0
- package/memhouse/import/records.js +64 -0
- package/memhouse/import/writer-profile.js +192 -0
- package/memhouse/import/writer-v3.js +240 -0
- package/memhouse/import/zip.js +112 -0
- package/memhouse/invitation.js +102 -0
- package/memhouse/invitefile.js +5 -2
- package/memhouse/plugin-copies.js +176 -0
- package/memhouse/provision.js +7 -2
- package/memhouse/release-hint.js +24 -0
- package/memhouse/resume.js +4 -4
- package/memhouse/server/api-v3.js +632 -0
- package/memhouse/server/dashboard-auth.js +181 -0
- package/memhouse/server/dashboard-port.js +91 -0
- package/memhouse/server/queries.js +30 -0
- package/memhouse/server/server.js +118 -20
- package/memhouse/service.js +1 -1
- package/memhouse/share.js +687 -1
- package/memhouse/shipper/adapter-capture.js +756 -0
- package/memhouse/shipper/capture.js +2571 -0
- package/memhouse/shipper/dedupe-profile.js +237 -0
- package/memhouse/shipper/dedupe-v3.js +1027 -0
- package/memhouse/shipper/forks.js +233 -0
- package/memhouse/shipper/log-adapter.js +219 -0
- package/memhouse/shipper/maintenance.js +49 -0
- package/memhouse/shipper/normalise-run.js +148 -0
- package/memhouse/shipper/passlog.js +68 -5
- package/memhouse/shipper/profile-capture.js +903 -0
- package/memhouse/shipper/ship.js +91 -8
- package/memhouse/shipper/snapshots.js +193 -0
- package/memhouse/shipper/source-roots.js +111 -0
- package/memhouse/version-notice.js +66 -0
- package/package.json +14 -7
- package/public/THIRD_PARTY_NOTICES.txt +2 -2
- package/public/assets/index-CcNnhfM3.js +78 -0
- package/public/index.html +1 -1
- package/ui/package-lock.json +56 -37
- package/ui/package.json +1 -0
- package/ui/src/App.jsx +29 -14
- package/ui/src/components/ChatSidebar.jsx +69 -12
- package/ui/src/lib/api.js +76 -0
- package/ui/src/lib/dashboard-token.js +41 -0
- package/ui/src/main.jsx +22 -5
- package/ui/src/pages/Dashboard.jsx +22 -36
- package/ui/third-party-notices.js +119 -0
- package/ui/vite.config.js +5 -1
- package/docs/releases/v0.18.12.md +0 -59
- package/memhouse/COMPETITION.md +0 -221
- package/memhouse/DESIGN.md +0 -222
- package/memhouse/RESUME-AND-UPDATE.md +0 -182
- package/public/assets/index-CnWzrjF3.js +0 -78
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
// api-v3.js — the dashboard's read layer on the schema-v3 tables (MEMHOUSE_SCHEMA=3).
|
|
2
|
+
//
|
|
3
|
+
// Same exports and the same response shapes as ./queries.js (the agentlytics contract the
|
|
4
|
+
// SPA renders), computed from the named queries in ../house/queries-v3.js instead of the
|
|
5
|
+
// 0.18 rooms. Where the UI depends on a field it keeps its 0.18 name (`source`, `id`,
|
|
6
|
+
// `bubbleCount`); everything NEW is harness-agnostic (`harness`, `session_id`, `message_id`).
|
|
7
|
+
//
|
|
8
|
+
// Mapping, v3 -> the contract:
|
|
9
|
+
// harness -> source / editor (the UI's `editor` param filters it)
|
|
10
|
+
// `${harness}:${session_id}` -> id (the 0.18 prefixed session id, so old links still parse)
|
|
11
|
+
// sessions.messages -> bubbleCount / total_msgs (kind = 'text' rows, as 0.18 counted)
|
|
12
|
+
// sessions.ended -> lastUpdatedAt (the list's sort key)
|
|
13
|
+
// created_at ?? started -> createdAt
|
|
14
|
+
// member -> user (one member per room; user_id is in no key)
|
|
15
|
+
//
|
|
16
|
+
// What changed underneath, on purpose:
|
|
17
|
+
// * No `final=1` session setting. Each named query says where it reads FINAL.
|
|
18
|
+
// * The landing page is ONE request (`landing`) of 11 small reads of `sessions FINAL`,
|
|
19
|
+
// instead of 33–34 statements across five endpoints.
|
|
20
|
+
// * Every session is priced from its own `usage` (sumMap per model), not by the five
|
|
21
|
+
// chunked P1–P5 queries; an entry with no model is priced at the session's dominant model.
|
|
22
|
+
// * A date window keeps a session when it was ACTIVE in it (ended >= from AND started <= to);
|
|
23
|
+
// token, model and cost figures count only the usage hours inside the window.
|
|
24
|
+
// * Refetch never ships; Live polls `houseVersion`.
|
|
25
|
+
|
|
26
|
+
'use strict';
|
|
27
|
+
|
|
28
|
+
const { calculateCost, normalizeModelName } = require('../../pricing');
|
|
29
|
+
const Q = require('../house/queries-v3');
|
|
30
|
+
|
|
31
|
+
const CHARS_PER_TOKEN = 4;
|
|
32
|
+
const RAW_MAX_ROWS = 10000;
|
|
33
|
+
const RAW_MAX_BYTES = 64 * 1024 * 1024;
|
|
34
|
+
const TZ = Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC';
|
|
35
|
+
const EXCLUDED = new Set(Q.EXCLUDED_MODELS);
|
|
36
|
+
|
|
37
|
+
const topN = (freq, n) => Object.entries(freq)
|
|
38
|
+
.sort((a, b) => (b[1] - a[1]) || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)).slice(0, n)
|
|
39
|
+
.map(([name, count]) => ({ name, count }));
|
|
40
|
+
const num = (v) => Number(v) || 0;
|
|
41
|
+
const nmodel = (m) => normalizeModelName(m) || m;
|
|
42
|
+
|
|
43
|
+
/** Legacy options (editor, folder, dateFrom, …) -> the queries-v3 session filter. */
|
|
44
|
+
function toFilters(opts = {}, { editorLike = false, folderLike = false } = {}) {
|
|
45
|
+
const f = {};
|
|
46
|
+
if (opts.hiddenFolders && opts.hiddenFolders.length) f.hidden = opts.hiddenFolders;
|
|
47
|
+
const harness = opts.harness || opts.editor;
|
|
48
|
+
if (harness) { if (editorLike && !opts.harness) f.harnessLike = harness; else f.harness = harness; }
|
|
49
|
+
if (opts.folder) { if (folderLike) f.folderLike = opts.folder; else f.folder = opts.folder; }
|
|
50
|
+
if (opts.chatId) f.session = Q.parseSessionRef(opts.chatId);
|
|
51
|
+
if (opts.dateFrom != null) f.from = opts.dateFrom;
|
|
52
|
+
if (opts.dateTo != null) f.to = opts.dateTo;
|
|
53
|
+
return f;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* One session's cost, from its own usage. Entries with no model ('' / '<synthetic>') are
|
|
58
|
+
* priced at the session's dominant model; a session with tokens but no model at all, or a
|
|
59
|
+
* model with no price, is reported partial rather than silently cheaper.
|
|
60
|
+
*/
|
|
61
|
+
function priceSession(s) {
|
|
62
|
+
const tokenMap = {};
|
|
63
|
+
let hasUnpriced = false;
|
|
64
|
+
const add = (model, u) => {
|
|
65
|
+
const k = nmodel(model);
|
|
66
|
+
const t = tokenMap[k] || (tokenMap[k] = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
|
67
|
+
t.input += u.input; t.output += u.output; t.cacheRead += u.cache_read; t.cacheWrite += u.cache_write;
|
|
68
|
+
};
|
|
69
|
+
const usage = Q.usageByModel(s.usage_by_model);
|
|
70
|
+
let tokens = 0;
|
|
71
|
+
for (const u of usage) {
|
|
72
|
+
const any = u.input + u.output + u.cache_read + u.cache_write;
|
|
73
|
+
tokens += any;
|
|
74
|
+
if (!any) continue;
|
|
75
|
+
if (!EXCLUDED.has(u.model)) add(u.model, u);
|
|
76
|
+
else if (s.dominant_model) add(s.dominant_model, u);
|
|
77
|
+
else hasUnpriced = true;
|
|
78
|
+
}
|
|
79
|
+
// No tokens recorded but text exists (legacy imports): estimate, as 0.18 did.
|
|
80
|
+
if (!tokens && s.dominant_model && (num(s.user_chars) || num(s.assistant_chars))) {
|
|
81
|
+
add(s.dominant_model, { input: Math.round(num(s.user_chars) / CHARS_PER_TOKEN), output: Math.round(num(s.assistant_chars) / CHARS_PER_TOKEN), cache_read: 0, cache_write: 0 });
|
|
82
|
+
}
|
|
83
|
+
let totalCost = 0; const byModel = [];
|
|
84
|
+
for (const [model, t] of Object.entries(tokenMap)) {
|
|
85
|
+
const c = calculateCost(model, t.input, t.output, t.cacheRead, t.cacheWrite);
|
|
86
|
+
if (c === null) { hasUnpriced = true; continue; }
|
|
87
|
+
totalCost += c; byModel.push({ model, cost: c });
|
|
88
|
+
}
|
|
89
|
+
byModel.sort((a, b) => (b.cost - a.cost) || (a.model < b.model ? -1 : 1));
|
|
90
|
+
return { totalCost, byModel, hasUnpriced };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A sessions row -> the /api/chats list item. */
|
|
94
|
+
function chatItem(s, member) {
|
|
95
|
+
const priced = s.usage_by_model ? priceSession(s) : null;
|
|
96
|
+
return {
|
|
97
|
+
id: Q.sessionRef(s), user: member, source: s.harness, name: s.name, mode: s.mode, folder: s.folder,
|
|
98
|
+
createdAt: s.created_ms != null ? num(s.created_ms) : num(s.started_ms),
|
|
99
|
+
lastUpdatedAt: num(s.ended_ms),
|
|
100
|
+
encrypted: false, bubbleCount: num(s.messages),
|
|
101
|
+
topModel: (priced && priced.byModel[0] && priced.byModel[0].model) || s.dominant_model || null,
|
|
102
|
+
cost: priced ? priced.totalCost : 0, costPartial: !!(priced && priced.hasUnpriced),
|
|
103
|
+
// harness-agnostic fields, new in v3
|
|
104
|
+
harness: s.harness, session_id: s.session_id, project: s.project, host: s.host,
|
|
105
|
+
startedAt: num(s.started_ms), endedAt: num(s.ended_ms), version: s.rev,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* A tool part as the marker the sidebar renders (ui MessageRenderer parseContent, the 0.18 form):
|
|
111
|
+
* a call `[tool-call: Name(arg, keys)]`, a result `[tool-result: Name] output`. A result names its
|
|
112
|
+
* tool through its call (`names`: call_id -> tool name, from the page's calls and call parts).
|
|
113
|
+
*/
|
|
114
|
+
function toolMarker(t, names) {
|
|
115
|
+
const clean = (x) => String(x || 'tool').replace(/[\]\n]/g, ' ');
|
|
116
|
+
if (t.kind === 'tool_call') {
|
|
117
|
+
let keys = '';
|
|
118
|
+
try { const a = JSON.parse(t.args || '{}'); keys = a && typeof a === 'object' && !Array.isArray(a) ? Object.keys(a).join(', ') : ''; } catch { /* args not JSON */ }
|
|
119
|
+
return `[tool-call: ${clean(t.tool_name)}(${keys})]`;
|
|
120
|
+
}
|
|
121
|
+
return `[tool-result: ${clean((names && names.get(t.call_id)) || t.tool_name)}] ${t.output || ''}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A part row -> the 0.18 message shape the sidebar renders, plus the part's identity. Tool parts
|
|
126
|
+
* render as their markers (the profile keeps a call's name and arguments and a result's output in
|
|
127
|
+
* their own columns); the kind is sessions-model's (reasoning, tool_call, tool_result, ...).
|
|
128
|
+
*/
|
|
129
|
+
function messageItem(t, names) {
|
|
130
|
+
const content = t.kind === 'tool_call' || t.kind === 'tool_result' ? toolMarker(t, names) : t.kind === 'error' ? Q.partText(t) : t.text;
|
|
131
|
+
return {
|
|
132
|
+
role: t.role, content, model: t.model || null,
|
|
133
|
+
inputTokens: num(t.input_tokens), outputTokens: num(t.output_tokens),
|
|
134
|
+
message_id: t.message_id, part: num(t.part), agent_id: t.agent_id, kind: t.kind, ts: t.ts_ms == null ? null : num(t.ts_ms),
|
|
135
|
+
...(t.call_id ? { callId: t.call_id } : {}), ...(t.tool_name ? { toolName: t.tool_name } : {}),
|
|
136
|
+
...(t.kind === 'tool_result' && t.is_error != null ? { isError: !!t.is_error } : {}),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function safeJson(s) { try { return JSON.parse(s); } catch { return {}; } }
|
|
140
|
+
|
|
141
|
+
function streaks(days) {
|
|
142
|
+
let currentStreak = 0, longestStreak = 0, temp = 1;
|
|
143
|
+
const today = new Date().toISOString().split('T')[0];
|
|
144
|
+
for (let i = 1; i < days.length; i++) {
|
|
145
|
+
const diff = (new Date(days[i]) - new Date(days[i - 1])) / 86400000;
|
|
146
|
+
if (diff === 1) temp++; else { if (temp > longestStreak) longestStreak = temp; temp = 1; }
|
|
147
|
+
}
|
|
148
|
+
if (temp > longestStreak) longestStreak = temp;
|
|
149
|
+
if (days.length > 0 && (new Date(today) - new Date(days[days.length - 1])) / 86400000 <= 1) {
|
|
150
|
+
currentStreak = 1;
|
|
151
|
+
for (let i = days.length - 2; i >= 0; i--) {
|
|
152
|
+
if ((new Date(days[i + 1]) - new Date(days[i])) / 86400000 === 1) currentStreak++; else break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!days.length) longestStreak = 0;
|
|
156
|
+
return { current: currentStreak, longest: longestStreak, totalDays: days.length };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function createApi({ exec: injectedExec = null, member: injectedMember = null } = {}) {
|
|
160
|
+
const config = {
|
|
161
|
+
url: process.env.MEMHOUSE_URL || 'http://localhost:8123',
|
|
162
|
+
username: process.env.MEMHOUSE_USER || 'memhouse_root',
|
|
163
|
+
// MEMHOUSE_PASSWORD_FILE: a root/owner-only file, referenced by path (the CTs keep their
|
|
164
|
+
// credentials in /etc/memhouse/*.pass) — the value never enters an env file or argv.
|
|
165
|
+
password: process.env.MEMHOUSE_PASSWORD || (process.env.MEMHOUSE_PASSWORD_FILE
|
|
166
|
+
? require('fs').readFileSync(process.env.MEMHOUSE_PASSWORD_FILE, 'utf-8').replace(/\r?\n$/, '') : ''),
|
|
167
|
+
database: process.env.MEMHOUSE_DB || 'mem',
|
|
168
|
+
member: injectedMember || process.env.MEMHOUSE_MEMBER || null,
|
|
169
|
+
schema: 3,
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
let client = null;
|
|
173
|
+
function getClient() {
|
|
174
|
+
if (client) return client;
|
|
175
|
+
// Required lazily so the unit tests can load this module without node_modules.
|
|
176
|
+
const { createClient, ClickHouseLogLevel } = require('@clickhouse/client');
|
|
177
|
+
client = createClient({
|
|
178
|
+
log: { level: process.env.MEMHOUSE_DEBUG ? ClickHouseLogLevel.DEBUG : ClickHouseLogLevel.OFF },
|
|
179
|
+
url: config.url, username: config.username, password: config.password, database: config.database,
|
|
180
|
+
request_timeout: 60000,
|
|
181
|
+
// NO `final` here — see queries-v3.js. FINAL is per table, in the SQL.
|
|
182
|
+
clickhouse_settings: { output_format_json_quote_64bit_integers: 0 },
|
|
183
|
+
});
|
|
184
|
+
return client;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function execOnce(spec) {
|
|
188
|
+
if (injectedExec) return injectedExec(spec);
|
|
189
|
+
if (spec.empty) return [];
|
|
190
|
+
const rs = await getClient().query({ query: spec.sql, query_params: spec.params, format: 'JSONEachRow', clickhouse_settings: spec.settings || {} });
|
|
191
|
+
return rs.json();
|
|
192
|
+
}
|
|
193
|
+
const exec = Q.withFallback(execOnce);
|
|
194
|
+
const run = (spec) => Q.run(exec, spec);
|
|
195
|
+
|
|
196
|
+
let roomsP = null;
|
|
197
|
+
function rooms() {
|
|
198
|
+
if (!roomsP) {
|
|
199
|
+
roomsP = (async () => {
|
|
200
|
+
let m = config.member;
|
|
201
|
+
if (!m) { const r = await exec({ query: 'currentUser', sql: 'SELECT currentUser() AS u', params: {}, settings: {} }); m = r[0] && r[0].u; }
|
|
202
|
+
return Q.v3Rooms(m);
|
|
203
|
+
})().catch((e) => { roomsP = null; throw e; });
|
|
204
|
+
}
|
|
205
|
+
return roomsP;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A chat id from the URL: `<harness>:<session_id>`, tolerating the 0.18 `::<member>` suffix.
|
|
209
|
+
async function refOf(id, harness = null) {
|
|
210
|
+
const r = await rooms();
|
|
211
|
+
let s = String(id);
|
|
212
|
+
const tail = `::${r.member}`;
|
|
213
|
+
if (s.endsWith(tail)) s = s.slice(0, -tail.length);
|
|
214
|
+
return Q.parseSessionRef(s, harness);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ── the landing page: one request, eleven small reads ──────────────────────────
|
|
218
|
+
async function landingParts(opts, want) {
|
|
219
|
+
const r = await rooms();
|
|
220
|
+
const f = toFilters(opts);
|
|
221
|
+
const agg = (o) => exec(Q.aggregate(r, { tz: TZ, filters: f, ...o }));
|
|
222
|
+
const jobs = {
|
|
223
|
+
dhh: () => agg({ dims: ['day', 'harness', 'hour'], metrics: ['sessions'] }),
|
|
224
|
+
tot: () => agg({ metrics: ['sessions', 'named_sessions', 'messages', 'input_tokens', 'output_tokens', 'cache_read_tokens', 'cache_write_tokens', 'user_chars', 'assistant_chars', 'first_started_ms', 'last_ended_ms'] }),
|
|
225
|
+
harness: () => exec(Q.aggregate(r, { filters: { ...f, named: true }, dims: ['harness'], metrics: ['sessions'], order: [['sessions', 'desc']] })),
|
|
226
|
+
mode: () => agg({ dims: ['mode'], metrics: ['sessions'] }),
|
|
227
|
+
folder: () => agg({ dims: ['folder'], metrics: ['sessions'], order: [['sessions', 'desc']], limit: 21 }),
|
|
228
|
+
depth: () => exec(Q.aggregate(r, { filters: { ...f, minMessages: 1 }, dims: ['depth'], metrics: ['sessions'] })),
|
|
229
|
+
velocity: () => agg({ dims: ['end_month'], metrics: ['avg_messages', 'avg_tokens'] }),
|
|
230
|
+
models: () => agg({ source: 'usage', dims: ['model', 'price_model'], metrics: ['messages', 'input_tokens', 'output_tokens', 'cache_read_tokens', 'cache_write_tokens'] }),
|
|
231
|
+
tools: () => agg({ source: 'tools', dims: ['tool'], metrics: ['calls'] }),
|
|
232
|
+
large: () => run(Q.listSessions(r, { sort: 'messages', limit: 10, filters: { ...f, minMessages: 100 } })),
|
|
233
|
+
largeN: () => run(Q.countSessions(r, { ...f, minMessages: 100 })),
|
|
234
|
+
};
|
|
235
|
+
const names = want.filter((k) => jobs[k]);
|
|
236
|
+
const results = await Promise.all(names.map((k) => jobs[k]()));
|
|
237
|
+
return Object.fromEntries(names.map((k, i) => [k, results[i]]));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function overviewFrom(p) {
|
|
241
|
+
const tot = (p.tot && p.tot[0]) || {};
|
|
242
|
+
const byMode = {};
|
|
243
|
+
for (const m of p.mode || []) if (m.mode) byMode[m.mode] = num(m.sessions);
|
|
244
|
+
const monthMap = {};
|
|
245
|
+
for (const row of p.dhh || []) {
|
|
246
|
+
const month = String(row.day).slice(0, 7);
|
|
247
|
+
const m = monthMap[month] || (monthMap[month] = { count: 0, editors: {} });
|
|
248
|
+
m.count += num(row.sessions);
|
|
249
|
+
m.editors[row.harness] = (m.editors[row.harness] || 0) + num(row.sessions);
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
totalChats: num(tot.named_sessions),
|
|
253
|
+
editors: (p.harness || []).map((e) => ({ id: e.harness, count: num(e.sessions) })),
|
|
254
|
+
byMode,
|
|
255
|
+
byMonth: Object.keys(monthMap).sort().map((month) => ({ month, ...monthMap[month] })),
|
|
256
|
+
topProjects: (p.folder || []).filter((x) => x.folder).slice(0, 20).map((x) => ({
|
|
257
|
+
name: x.folder.split(/[/\\]/).slice(-2).join('/'), fullPath: x.folder, count: num(x.sessions),
|
|
258
|
+
})),
|
|
259
|
+
oldestChat: tot.sessions ? num(tot.first_started_ms) : null,
|
|
260
|
+
newestChat: tot.sessions ? num(tot.last_ended_ms) : null,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function dailyFrom(p) {
|
|
265
|
+
const daily = {};
|
|
266
|
+
for (const row of p.dhh || []) {
|
|
267
|
+
const d = daily[row.day] || (daily[row.day] = { total: 0, editors: {}, hours: {} });
|
|
268
|
+
const c = num(row.sessions);
|
|
269
|
+
d.total += c;
|
|
270
|
+
d.editors[row.harness] = (d.editors[row.harness] || 0) + c;
|
|
271
|
+
if (!d.hours[row.harness]) d.hours[row.harness] = new Array(24).fill(0);
|
|
272
|
+
d.hours[row.harness][num(row.hour)] += c;
|
|
273
|
+
}
|
|
274
|
+
return Object.keys(daily).sort().map((day) => ({ day, ...daily[day] }));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function statsFrom(p) {
|
|
278
|
+
const hourly = new Array(24).fill(0);
|
|
279
|
+
const weekdays = new Array(7).fill(0);
|
|
280
|
+
const monthEditors = {}; const sources = new Set();
|
|
281
|
+
for (const row of p.dhh || []) {
|
|
282
|
+
const c = num(row.sessions);
|
|
283
|
+
hourly[num(row.hour)] += c;
|
|
284
|
+
weekdays[new Date(`${row.day}T00:00:00Z`).getUTCDay()] += c;
|
|
285
|
+
const month = String(row.day).slice(0, 7);
|
|
286
|
+
(monthEditors[month] = monthEditors[month] || {})[row.harness] = (monthEditors[month][row.harness] || 0) + c;
|
|
287
|
+
sources.add(row.harness);
|
|
288
|
+
}
|
|
289
|
+
const depthBuckets = { '1': 0, '2-5': 0, '6-10': 0, '11-20': 0, '21-50': 0, '51-100': 0, '100+': 0 };
|
|
290
|
+
for (const d of p.depth || []) depthBuckets[d.depth] = num(d.sessions);
|
|
291
|
+
const days = [...new Set((p.dhh || []).map((x) => x.day))].sort();
|
|
292
|
+
const modelFreq = {};
|
|
293
|
+
for (const m of p.models || []) if (!EXCLUDED.has(m.model)) modelFreq[nmodel(m.model)] = (modelFreq[nmodel(m.model)] || 0) + num(m.messages);
|
|
294
|
+
const toolFreq = {}; let totalToolCalls = 0;
|
|
295
|
+
for (const t of p.tools || []) { toolFreq[t.tool] = num(t.calls); totalToolCalls += num(t.calls); }
|
|
296
|
+
const tot = (p.tot && p.tot[0]) || {};
|
|
297
|
+
let input = num(tot.input_tokens), output = num(tot.output_tokens), estimated = false;
|
|
298
|
+
if (!input && !output && (num(tot.user_chars) || num(tot.assistant_chars))) {
|
|
299
|
+
input = Math.round(num(tot.user_chars) / CHARS_PER_TOKEN); output = Math.round(num(tot.assistant_chars) / CHARS_PER_TOKEN); estimated = true;
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
hourly, weekdays, depthBuckets,
|
|
303
|
+
tokens: {
|
|
304
|
+
input, output, cacheRead: num(tot.cache_read_tokens), cacheWrite: num(tot.cache_write_tokens),
|
|
305
|
+
userChars: num(tot.user_chars), assistantChars: num(tot.assistant_chars),
|
|
306
|
+
sessions: num(tot.named_sessions), messages: num(tot.messages), estimated,
|
|
307
|
+
},
|
|
308
|
+
streaks: streaks(days),
|
|
309
|
+
monthlyTrend: { months: Object.keys(monthEditors).sort(), sources: [...sources], data: monthEditors },
|
|
310
|
+
velocity: (p.velocity || []).map((v) => ({ month: v.end_month, avgMsgs: Math.round(num(v.avg_messages) * 10) / 10, avgTokens: Math.round(num(v.avg_tokens)) })),
|
|
311
|
+
topModels: topN(modelFreq, 10), topTools: topN(toolFreq, 8), totalToolCalls,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function costsFrom(p) {
|
|
316
|
+
const tokenMap = {};
|
|
317
|
+
for (const m of p.models || []) {
|
|
318
|
+
const model = m.price_model;
|
|
319
|
+
if (!model || EXCLUDED.has(model)) continue;
|
|
320
|
+
const k = nmodel(model);
|
|
321
|
+
const t = tokenMap[k] || (tokenMap[k] = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
|
322
|
+
t.input += num(m.input_tokens); t.output += num(m.output_tokens); t.cacheRead += num(m.cache_read_tokens); t.cacheWrite += num(m.cache_write_tokens);
|
|
323
|
+
}
|
|
324
|
+
let totalCost = 0; const byModel = []; const unknown = [];
|
|
325
|
+
for (const [model, t] of Object.entries(tokenMap)) {
|
|
326
|
+
const cost = calculateCost(model, t.input, t.output, t.cacheRead, t.cacheWrite);
|
|
327
|
+
if (cost === null) { if (t.input || t.output || t.cacheRead || t.cacheWrite) unknown.push(model); continue; }
|
|
328
|
+
totalCost += cost;
|
|
329
|
+
byModel.push({ model, inputTokens: t.input, outputTokens: t.output, cacheRead: t.cacheRead, cacheWrite: t.cacheWrite, cost });
|
|
330
|
+
}
|
|
331
|
+
byModel.sort((a, b) => b.cost - a.cost);
|
|
332
|
+
return { totalCost, byModel, unknownModels: [...new Set(unknown)] };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function landing(opts = {}) {
|
|
336
|
+
const [p, version] = await Promise.all([
|
|
337
|
+
landingParts(opts, ['dhh', 'tot', 'harness', 'mode', 'folder', 'depth', 'velocity', 'models', 'tools', 'large', 'largeN']),
|
|
338
|
+
houseVersion(),
|
|
339
|
+
]);
|
|
340
|
+
const r = await rooms();
|
|
341
|
+
return {
|
|
342
|
+
overview: overviewFrom(p), daily: dailyFrom(p), stats: statsFrom(p), costs: costsFrom(p),
|
|
343
|
+
largeContext: { total: p.largeN, chats: p.large.rows.map((s) => chatItem(s, r.member)) },
|
|
344
|
+
version: version.token,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
async function getOverview(opts = {}) { return overviewFrom(await landingParts(opts, ['dhh', 'tot', 'harness', 'mode', 'folder'])); }
|
|
348
|
+
async function getDailyActivity(opts = {}) { return dailyFrom(await landingParts(opts, ['dhh'])); }
|
|
349
|
+
async function getDashboardStats(opts = {}) { return statsFrom(await landingParts(opts, ['dhh', 'tot', 'depth', 'velocity', 'models', 'tools'])); }
|
|
350
|
+
async function estimateCosts(opts = {}) { return costsFrom(await landingParts(opts, ['models'])); }
|
|
351
|
+
|
|
352
|
+
// ── sessions ───────────────────────────────────────────────────────────────────
|
|
353
|
+
async function listChats(opts = {}) {
|
|
354
|
+
const r = await rooms();
|
|
355
|
+
const f = toFilters(opts, { editorLike: true, folderLike: true });
|
|
356
|
+
if (opts.named !== false) f.named = true;
|
|
357
|
+
const [total, page] = await Promise.all([
|
|
358
|
+
run(Q.countSessions(r, f)),
|
|
359
|
+
run(Q.listSessions(r, { filters: f, limit: opts.limit || 200, cursor: opts.cursor || null, sort: opts.sort || 'ended', withUsage: true })),
|
|
360
|
+
]);
|
|
361
|
+
return { total, chats: page.rows.map((s) => chatItem(s, r.member)), nextCursor: page.nextCursor };
|
|
362
|
+
}
|
|
363
|
+
async function countChats(opts = {}) { return (await listChats({ ...opts, limit: 1 })).total; }
|
|
364
|
+
async function getChats(opts = {}) { return (await listChats(opts)).chats; }
|
|
365
|
+
|
|
366
|
+
function chatPayload(rs, member, idForm) {
|
|
367
|
+
const h = rs.header;
|
|
368
|
+
const names = new Map();
|
|
369
|
+
for (const c of rs.calls) names.set(c.call_id, c.tool_name);
|
|
370
|
+
for (const t of rs.turns) if (t.kind === 'tool_call' && t.call_id) names.set(t.call_id, t.tool_name);
|
|
371
|
+
const messages = rs.turns.map((t) => messageItem(t, names));
|
|
372
|
+
let toolMessages = 0, systemMessages = 0;
|
|
373
|
+
for (const m of messages) { if (m.role === 'tool') toolMessages++; else if (m.role === 'system') systemMessages++; }
|
|
374
|
+
const toolCalls = [];
|
|
375
|
+
for (const t of h.tools) for (let i = 0; i < t.calls; i++) toolCalls.push(t.name);
|
|
376
|
+
return {
|
|
377
|
+
id: idForm, user: member, source: h.harness, name: h.name, mode: h.mode, folder: h.folder,
|
|
378
|
+
createdAt: h.created_ms != null ? num(h.created_ms) : num(h.started_ms), lastUpdatedAt: num(h.ended_ms), encrypted: false,
|
|
379
|
+
messages,
|
|
380
|
+
stats: {
|
|
381
|
+
totalMessages: num(h.messages), userMessages: num(h.user_messages), assistantMessages: num(h.assistant_messages),
|
|
382
|
+
toolMessages, systemMessages, toolCalls, models: h.models || [],
|
|
383
|
+
totalUserChars: num(h.user_chars), totalAssistantChars: num(h.assistant_chars),
|
|
384
|
+
totalInputTokens: num(h.input_tokens), totalOutputTokens: num(h.output_tokens),
|
|
385
|
+
totalCacheRead: num(h.cache_read_tokens), totalCacheWrite: num(h.cache_write_tokens),
|
|
386
|
+
},
|
|
387
|
+
toolCallDetails: rs.calls.map((c) => ({ name: c.tool_name, args: safeJson(c.args) })),
|
|
388
|
+
// v3: identity, version, paging, sub-agents
|
|
389
|
+
harness: h.harness, session_id: h.session_id, project: h.project, host: h.host, version: h.rev,
|
|
390
|
+
agents: h.agents, days: h.days,
|
|
391
|
+
calls: rs.calls.map((c) => ({ ...c, ts: num(c.ts_ms), is_error: !!c.is_error })),
|
|
392
|
+
nextCursor: rs.nextCursor, prevCursor: rs.prevCursor,
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** One session: header + the TAIL page of turns (a sidebar opens at the bottom). */
|
|
397
|
+
async function getChat(id, { cursor = null, dir = 'backward', limit = 1000, agent = null } = {}) {
|
|
398
|
+
const r = await rooms();
|
|
399
|
+
const ref = await refOf(id);
|
|
400
|
+
const rs = await Q.readSession(exec, r, { ...ref, cursor, dir, limit, agent_id: agent });
|
|
401
|
+
// The id names the store the header resolved: a 0.18 link (no store) opens as the full ref.
|
|
402
|
+
return rs ? chatPayload(rs, r.member, Q.sessionRef(rs.header)) : null;
|
|
403
|
+
}
|
|
404
|
+
/** A page of turns and its calls: /api/chats/:id/turns. */
|
|
405
|
+
async function getChatTurns(id, opts = {}) {
|
|
406
|
+
const p = await getChat(id, { cursor: opts.cursor || null, dir: opts.dir === 'forward' ? 'forward' : 'backward', limit: opts.limit || 500, agent: opts.agent ?? null });
|
|
407
|
+
if (!p) return null;
|
|
408
|
+
return { id: p.id, harness: p.harness, session_id: p.session_id, version: p.version, messages: p.messages, toolCallDetails: p.toolCallDetails, calls: p.calls, nextCursor: p.nextCursor, prevCursor: p.prevCursor, window: p.window };
|
|
409
|
+
}
|
|
410
|
+
/** Every turn, forward, for the markdown export (bounded). */
|
|
411
|
+
async function getChatAll(id, maxTurns = 100000) {
|
|
412
|
+
const r = await rooms();
|
|
413
|
+
const ref = await refOf(id);
|
|
414
|
+
let cursor = null; let first = null; const turns = []; const calls = [];
|
|
415
|
+
do {
|
|
416
|
+
const rs = await Q.readSession(exec, r, { ...ref, cursor, dir: 'forward', limit: 5000 });
|
|
417
|
+
if (!rs) return null;
|
|
418
|
+
// Every later page reads the store the first one resolved, never a fresh guess.
|
|
419
|
+
if (!first) { first = rs; Object.assign(ref, { host: rs.header.host, harness_path: rs.header.harness_path, incarnation: rs.header.incarnation }); }
|
|
420
|
+
turns.push(...rs.turns); calls.push(...rs.calls);
|
|
421
|
+
cursor = rs.nextCursor;
|
|
422
|
+
} while (cursor && turns.length < maxTurns);
|
|
423
|
+
return chatPayload({ ...first, turns, calls, nextCursor: cursor, prevCursor: null }, r.member, Q.sessionRef(first.header));
|
|
424
|
+
}
|
|
425
|
+
async function getChatVersion(id) {
|
|
426
|
+
const r = await rooms();
|
|
427
|
+
const v = await run(Q.sessionVersion(r, await refOf(id)));
|
|
428
|
+
return v ? { version: v.rev, endedAt: v.ended_ms, messages: v.messages } : null;
|
|
429
|
+
}
|
|
430
|
+
async function houseVersion() {
|
|
431
|
+
const r = await rooms();
|
|
432
|
+
return run(Q.houseVersion(r));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// ── search, models ─────────────────────────────────────────────────────────────
|
|
436
|
+
async function search(opts = {}) {
|
|
437
|
+
const r = await rooms();
|
|
438
|
+
const spec = Q.search(r, {
|
|
439
|
+
q: opts.q, match: opts.match, limit: opts.limit,
|
|
440
|
+
filters: { harness: opts.harness || opts.editor || null, folder: opts.folder || null, project: opts.project || null, from: opts.dateFrom ?? null, to: opts.dateTo ?? null },
|
|
441
|
+
});
|
|
442
|
+
const rows = await exec(spec);
|
|
443
|
+
return {
|
|
444
|
+
terms: spec.terms,
|
|
445
|
+
hits: rows.map((h) => ({
|
|
446
|
+
id: Q.sessionRef(h), harness: h.harness, session_id: h.session_id, name: h.name, folder: h.folder, project: h.project, host: h.host,
|
|
447
|
+
endedAt: num(h.ended_ms), hits: num(h.hits), firstHit: num(h.first_hit_ms), lastHit: num(h.last_hit_ms),
|
|
448
|
+
message_id: h.message_id, agent_id: h.agent_id, role: h.role, snippet: h.snippet,
|
|
449
|
+
})),
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
async function models(opts = {}) {
|
|
454
|
+
const r = await rooms();
|
|
455
|
+
const rows = await exec(Q.aggregate(r, {
|
|
456
|
+
source: 'usage', filters: toFilters(opts), dims: ['model'], where: { excludeModels: true },
|
|
457
|
+
metrics: ['sessions', 'messages', 'input_tokens', 'output_tokens', 'cache_read_tokens', 'cache_write_tokens', 'reasoning_tokens', 'first_hour_ms', 'last_hour_ms'],
|
|
458
|
+
order: [['messages', 'desc']],
|
|
459
|
+
}));
|
|
460
|
+
let totalCost = 0;
|
|
461
|
+
const out = rows.map((m) => {
|
|
462
|
+
const cost = calculateCost(m.model, num(m.input_tokens), num(m.output_tokens), num(m.cache_read_tokens), num(m.cache_write_tokens));
|
|
463
|
+
if (cost !== null) totalCost += cost;
|
|
464
|
+
return {
|
|
465
|
+
model: m.model, normalized: nmodel(m.model), sessions: num(m.sessions), messages: num(m.messages),
|
|
466
|
+
inputTokens: num(m.input_tokens), outputTokens: num(m.output_tokens), cacheRead: num(m.cache_read_tokens),
|
|
467
|
+
cacheWrite: num(m.cache_write_tokens), reasoningTokens: num(m.reasoning_tokens), cost, priced: cost !== null,
|
|
468
|
+
firstSeen: num(m.first_hour_ms), lastSeen: num(m.last_hour_ms),
|
|
469
|
+
};
|
|
470
|
+
});
|
|
471
|
+
return { models: out, totalCost };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// ── projects, deep analytics, cost analytics, tool calls ───────────────────────
|
|
475
|
+
async function getProjects(opts = {}) {
|
|
476
|
+
const r = await rooms();
|
|
477
|
+
const f = toFilters({ hiddenFolders: opts.includeHidden ? [] : opts.hiddenFolders, dateFrom: opts.dateFrom, dateTo: opts.dateTo });
|
|
478
|
+
const [perSource, sums, modelRows, toolRows] = await Promise.all([
|
|
479
|
+
exec(Q.aggregate(r, { filters: f, dims: ['folder', 'harness'], metrics: ['sessions', 'first_started_ms', 'last_ended_ms'] })),
|
|
480
|
+
exec(Q.aggregate(r, { filters: f, dims: ['folder'], metrics: ['messages', 'input_tokens', 'output_tokens', 'user_chars', 'assistant_chars', 'cache_read_tokens', 'cache_write_tokens'] })),
|
|
481
|
+
exec(Q.aggregate(r, { source: 'usage', filters: f, dims: ['folder', 'model'], metrics: ['messages'], where: { excludeModels: true } })),
|
|
482
|
+
exec(Q.aggregate(r, { source: 'tools', filters: f, dims: ['folder', 'tool'], metrics: ['calls'] })),
|
|
483
|
+
]);
|
|
484
|
+
const map = {};
|
|
485
|
+
for (const x of perSource) {
|
|
486
|
+
if (!x.folder) continue;
|
|
487
|
+
const p = map[x.folder] || (map[x.folder] = { totalSessions: 0, editors: {}, firstSeen: num(x.first_started_ms), lastSeen: num(x.last_ended_ms) });
|
|
488
|
+
p.totalSessions += num(x.sessions); p.editors[x.harness] = num(x.sessions);
|
|
489
|
+
p.firstSeen = Math.min(p.firstSeen, num(x.first_started_ms)); p.lastSeen = Math.max(p.lastSeen, num(x.last_ended_ms));
|
|
490
|
+
}
|
|
491
|
+
const sumBy = Object.fromEntries(sums.map((x) => [x.folder, x]));
|
|
492
|
+
const modelsBy = {}; for (const x of modelRows) { const k = nmodel(x.model); (modelsBy[x.folder] = modelsBy[x.folder] || {})[k] = ((modelsBy[x.folder] || {})[k] || 0) + num(x.messages); }
|
|
493
|
+
const toolsBy = {}; for (const x of toolRows) (toolsBy[x.folder] = toolsBy[x.folder] || {})[x.tool] = num(x.calls);
|
|
494
|
+
const out = [];
|
|
495
|
+
for (const [folder, p] of Object.entries(map)) {
|
|
496
|
+
const s = sumBy[folder] || {};
|
|
497
|
+
let ti = num(s.input_tokens), to = num(s.output_tokens), est = false;
|
|
498
|
+
if (!ti && !to && (num(s.user_chars) || num(s.assistant_chars))) { ti = Math.round(num(s.user_chars) / CHARS_PER_TOKEN); to = Math.round(num(s.assistant_chars) / CHARS_PER_TOKEN); est = true; }
|
|
499
|
+
const tf = toolsBy[folder] || {};
|
|
500
|
+
out.push({
|
|
501
|
+
folder, name: folder.split(/[/\\]/).pop(), totalSessions: p.totalSessions, editors: p.editors, firstSeen: p.firstSeen, lastSeen: p.lastSeen,
|
|
502
|
+
totalMessages: num(s.messages), totalInputTokens: ti, totalOutputTokens: to, tokensEstimated: est,
|
|
503
|
+
totalUserChars: num(s.user_chars), totalAssistantChars: num(s.assistant_chars), totalToolCalls: Object.values(tf).reduce((a, b) => a + b, 0),
|
|
504
|
+
totalCacheRead: num(s.cache_read_tokens), totalCacheWrite: num(s.cache_write_tokens),
|
|
505
|
+
topModels: topN(modelsBy[folder] || {}, 10), topTools: topN(tf, 10),
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return out.sort((a, b) => b.totalSessions - a.totalSessions);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
async function getDeepAnalytics(opts = {}) {
|
|
512
|
+
const r = await rooms();
|
|
513
|
+
const f = { ...toFilters(opts, { editorLike: true }), latest: opts.limit || 500 };
|
|
514
|
+
const [tot, toolRows, modelRows] = await Promise.all([
|
|
515
|
+
exec(Q.aggregate(r, { filters: f, metrics: ['sessions', 'messages', 'user_chars', 'assistant_chars', 'input_tokens', 'output_tokens', 'cache_read_tokens', 'cache_write_tokens'] })),
|
|
516
|
+
exec(Q.aggregate(r, { source: 'tools', filters: f, dims: ['tool'], metrics: ['calls'] })),
|
|
517
|
+
exec(Q.aggregate(r, { source: 'usage', filters: f, dims: ['model'], metrics: ['messages'], where: { excludeModels: true } })),
|
|
518
|
+
]);
|
|
519
|
+
const t = tot[0] || {};
|
|
520
|
+
const toolFreq = {}; let totalToolCalls = 0;
|
|
521
|
+
for (const x of toolRows) { toolFreq[x.tool] = num(x.calls); totalToolCalls += num(x.calls); }
|
|
522
|
+
const modelFreq = {}; for (const x of modelRows) modelFreq[nmodel(x.model)] = (modelFreq[nmodel(x.model)] || 0) + num(x.messages);
|
|
523
|
+
let ti = num(t.input_tokens), to = num(t.output_tokens), est = false;
|
|
524
|
+
if (!ti && !to && (num(t.user_chars) || num(t.assistant_chars))) { ti = Math.round(num(t.user_chars) / CHARS_PER_TOKEN); to = Math.round(num(t.assistant_chars) / CHARS_PER_TOKEN); est = true; }
|
|
525
|
+
return {
|
|
526
|
+
analyzedChats: num(t.sessions), totalMessages: num(t.messages), totalToolCalls,
|
|
527
|
+
totalUserChars: num(t.user_chars), totalAssistantChars: num(t.assistant_chars),
|
|
528
|
+
totalInputTokens: ti, totalOutputTokens: to, tokensEstimated: est,
|
|
529
|
+
totalCacheRead: num(t.cache_read_tokens), totalCacheWrite: num(t.cache_write_tokens),
|
|
530
|
+
topTools: topN(toolFreq, 30), topModels: topN(modelFreq, 20),
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
async function getCostAnalytics(opts = {}) {
|
|
535
|
+
const r = await rooms();
|
|
536
|
+
const f = toFilters(opts, { editorLike: true });
|
|
537
|
+
const overallP = estimateCosts(opts);
|
|
538
|
+
const rows = []; let cursor = null;
|
|
539
|
+
do {
|
|
540
|
+
const page = await run(Q.listSessions(r, { filters: f, limit: 5000, cursor, withUsage: true }));
|
|
541
|
+
rows.push(...page.rows); cursor = page.nextCursor;
|
|
542
|
+
} while (cursor);
|
|
543
|
+
const overall = await overallP;
|
|
544
|
+
const editorAgg = {}, projectAgg = {}, monthCosts = {}; const sessionCosts = [];
|
|
545
|
+
for (const s of rows) {
|
|
546
|
+
const p = priceSession(s);
|
|
547
|
+
const month = new Date(num(s.ended_ms)).toISOString().slice(0, 7);
|
|
548
|
+
(monthCosts[month] = monthCosts[month] || { cost: 0, sessions: 0 }).cost += p.totalCost; monthCosts[month].sessions++;
|
|
549
|
+
if (p.totalCost <= 0) continue;
|
|
550
|
+
const e = editorAgg[s.harness] || (editorAgg[s.harness] = { cost: 0, models: new Set() });
|
|
551
|
+
e.cost += p.totalCost; for (const m of p.byModel) e.models.add(m.model);
|
|
552
|
+
if (s.folder) projectAgg[s.folder] = (projectAgg[s.folder] || 0) + p.totalCost;
|
|
553
|
+
sessionCosts.push({ id: Q.sessionRef(s), source: s.harness, name: s.name, folder: s.folder, model: p.byModel[0] ? p.byModel[0].model : null, cost: p.totalCost, messages: num(s.messages), lastUpdatedAt: num(s.ended_ms), harness: s.harness, session_id: s.session_id });
|
|
554
|
+
}
|
|
555
|
+
const byEditor = Object.entries(editorAgg).map(([editor, d]) => ({ editor, cost: d.cost, models: d.models.size })).sort((a, b) => b.cost - a.cost);
|
|
556
|
+
const byProject = Object.entries(projectAgg).map(([folder, cost]) => ({ folder, name: folder.split('/').pop(), cost })).sort((a, b) => b.cost - a.cost).slice(0, 20);
|
|
557
|
+
const monthly = Object.entries(monthCosts).sort((a, b) => a[0].localeCompare(b[0])).map(([month, d]) => ({ month, cost: Math.round(d.cost * 100) / 100, sessions: d.sessions }));
|
|
558
|
+
sessionCosts.sort((a, b) => (b.cost - a.cost) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
559
|
+
const totalSessions = sessionCosts.length;
|
|
560
|
+
const totalDays = monthly.length ? Math.max(1, Math.ceil((new Date(`${monthly[monthly.length - 1].month}-01`) - new Date(`${monthly[0].month}-01`)) / 86400000) + 30) : 1;
|
|
561
|
+
return {
|
|
562
|
+
totalCost: overall.totalCost, byModel: overall.byModel, unknownModels: overall.unknownModels,
|
|
563
|
+
byEditor, byProject, monthly, topSessions: sessionCosts.slice(0, 50),
|
|
564
|
+
summary: { totalSessions, avgPerSession: Math.round((totalSessions ? overall.totalCost / totalSessions : 0) * 100) / 100, avgPerDay: Math.round((overall.totalCost / totalDays) * 100) / 100, totalDays },
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
async function getToolCalls(toolName, opts = {}) {
|
|
569
|
+
const r = await rooms();
|
|
570
|
+
const rows = await exec(Q.tools(r, {
|
|
571
|
+
tool: toolName, limit: opts.limit || 200,
|
|
572
|
+
filters: { harnessLike: opts.editor || null, folder: opts.folder || null, hidden: opts.hiddenFolders, from: opts.dateFrom ?? null, to: opts.dateTo ?? null },
|
|
573
|
+
}));
|
|
574
|
+
return rows.map((c) => ({
|
|
575
|
+
toolName: c.tool_name, args: safeJson(c.args), source: c.harness, folder: c.folder, timestamp: num(c.ts_ms),
|
|
576
|
+
chatName: c.session_name, chatId: Q.sessionRef(c),
|
|
577
|
+
harness: c.harness, session_id: c.session_id, call_id: c.call_id, message_id: c.message_id, isError: !!c.is_error,
|
|
578
|
+
}));
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
async function coverage() { return run(Q.coverage(await rooms())); }
|
|
582
|
+
async function machines() { return exec(Q.machines(await rooms())); }
|
|
583
|
+
|
|
584
|
+
// ── SQL console ────────────────────────────────────────────────────────────────
|
|
585
|
+
async function rawQuery(sql) {
|
|
586
|
+
const rows = await exec({ query: 'raw', sql, params: {}, settings: {
|
|
587
|
+
max_result_rows: RAW_MAX_ROWS, max_result_bytes: RAW_MAX_BYTES, result_overflow_mode: 'break',
|
|
588
|
+
max_execution_time: 30, max_memory_usage: 8000000000,
|
|
589
|
+
} });
|
|
590
|
+
return { columns: rows.length ? Object.keys(rows[0]) : [], rows, count: rows.length, truncated: rows.length >= RAW_MAX_ROWS };
|
|
591
|
+
}
|
|
592
|
+
async function rawQueryUnguarded(sql) { return exec({ query: 'raw', sql, params: {}, settings: {} }); }
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* The SQL console's rooms. The typed examples are written against the 0.18 names
|
|
596
|
+
* (`{{sessions_v}}`, `{{messages}}`, `{{tool_calls}}` with `source`, `total_msgs`, `name`,
|
|
597
|
+
* `text_ngram`, `user_id`); hand them live, FINAL views of the profile tables under those names
|
|
598
|
+
* so they keep working, and the profile's own names beside them.
|
|
599
|
+
*/
|
|
600
|
+
function consoleRooms(r) {
|
|
601
|
+
return {
|
|
602
|
+
member: r.member, sessions: r.sessions, parts: r.parts, usage: r.usage, calls: r.calls,
|
|
603
|
+
sessions_v: `(SELECT *, harness AS source, messages AS total_msgs, title AS name, writer AS user_id FROM ${r.sessions} FINAL WHERE is_deleted = 0 AND removed = 0)`,
|
|
604
|
+
messages: `(SELECT *, harness AS source, search_ngram AS text_ngram, search_word AS text_word, writer AS user_id,
|
|
605
|
+
if(toString(served_model) != '', toString(served_model), toString(requested_model)) AS model FROM ${r.parts} FINAL WHERE is_deleted = 0 AND removed = 0)`,
|
|
606
|
+
tool_calls: `(SELECT *, harness AS source, writer AS user_id FROM ${r.calls} FINAL WHERE is_deleted = 0 AND removed = 0)`,
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
async function schema() {
|
|
610
|
+
const r = await rooms();
|
|
611
|
+
const cols = await exec({ query: 'schema', params: {}, settings: {},
|
|
612
|
+
sql: `SELECT table, name, type FROM system.columns WHERE database = currentDatabase() AND NOT startsWith(table, '.') ORDER BY table, position` });
|
|
613
|
+
const out = {};
|
|
614
|
+
for (const c of cols) (out[c.table] = out[c.table] || []).push({ name: c.name, type: c.type });
|
|
615
|
+
return { tables: Object.keys(out).sort(), schema: out, rooms: consoleRooms(r), schemaVersion: 3 };
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
return {
|
|
619
|
+
config, getClient, schemaVersion: 3,
|
|
620
|
+
adoptMovedHouse: async () => 'current',
|
|
621
|
+
landing, getOverview, getDailyActivity, getDashboardStats, estimateCosts,
|
|
622
|
+
listChats, countChats, getChats, getChat, getChatTurns, getChatAll, getChatVersion, houseVersion,
|
|
623
|
+
search, models, getProjects, getDeepAnalytics, getCostAnalytics, getToolCalls, coverage, machines,
|
|
624
|
+
rawQuery, rawQueryUnguarded, schema, rooms,
|
|
625
|
+
};
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
module.exports = createApi();
|
|
629
|
+
module.exports.createApi = createApi;
|
|
630
|
+
module.exports.priceSession = priceSession;
|
|
631
|
+
module.exports.toFilters = toFilters;
|
|
632
|
+
module.exports.chatItem = chatItem;
|