chompyy 0.5.0 → 0.6.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 +11 -0
- package/dist/agentlimits.js +77 -0
- package/dist/agentusage.js +96 -0
- package/dist/appstate.js +63 -3
- package/dist/away.js +44 -5
- package/dist/blueprint.js +3 -7
- package/dist/board.js +1 -0
- package/dist/cli.js +263 -5
- package/dist/codexquota.js +106 -0
- package/dist/codexusage.js +10 -0
- package/dist/config.js +2 -0
- package/dist/context.js +30 -0
- package/dist/costs.js +1 -0
- package/dist/demon.js +20 -0
- package/dist/label.js +33 -0
- package/dist/menu.js +17 -0
- package/dist/nightcap.js +60 -0
- package/dist/nudge.js +47 -18
- package/dist/pacing.js +16 -1
- package/dist/questions.js +71 -0
- package/dist/recap.js +74 -0
- package/dist/receipt.js +48 -0
- package/dist/recipe.js +79 -0
- package/dist/stamps.js +14 -0
- package/dist/triggers.js +5 -0
- package/dist/usage.js +21 -1
- package/dist/vendor/codeburn/antigravity.js +309 -0
- package/dist/vendor/codeburn/codex.js +429 -0
- package/dist/vendor/codeburn/copilot.js +292 -0
- package/dist/vendor/codeburn/gemini.js +195 -0
- package/dist/vendor/codeburn/grokbot.js +182 -0
- package/dist/vendor/codeburn/kimi.js +178 -0
- package/dist/vendor/codeburn/shims/chalk.js +14 -0
- package/dist/vendor/codeburn/shims/strip-ansi.js +6 -0
- package/dist/vendor/codeburn/src/act/backup.js +83 -0
- package/dist/vendor/codeburn/src/act/model-defaults.js +151 -0
- package/dist/vendor/codeburn/src/act/types.js +37 -0
- package/dist/vendor/codeburn/src/bash-utils.js +204 -0
- package/dist/vendor/codeburn/src/behavioral-weight.js +47 -0
- package/dist/vendor/codeburn/src/cache-dir.js +26 -0
- package/dist/vendor/codeburn/src/cache-refresh-lock.js +543 -0
- package/dist/vendor/codeburn/src/cache-sweep.js +67 -0
- package/dist/vendor/codeburn/src/classifier.js +209 -0
- package/dist/vendor/codeburn/src/claude-identity.js +223 -0
- package/dist/vendor/codeburn/src/codex-cache.js +377 -0
- package/dist/vendor/codeburn/src/codex-fork-replay.js +232 -0
- package/dist/vendor/codeburn/src/codex-jsonl-fields.js +99 -0
- package/dist/vendor/codeburn/src/codex-session-index.js +113 -0
- package/dist/vendor/codeburn/src/codex-throughput.js +544 -0
- package/dist/vendor/codeburn/src/compare-stats.js +405 -0
- package/dist/vendor/codeburn/src/config.js +145 -0
- package/dist/vendor/codeburn/src/content-utils.js +56 -0
- package/dist/vendor/codeburn/src/context-tree-codex.js +329 -0
- package/dist/vendor/codeburn/src/context-tree.js +664 -0
- package/dist/vendor/codeburn/src/copilot-aiu.js +16 -0
- package/dist/vendor/codeburn/src/currency.js +189 -0
- package/dist/vendor/codeburn/src/cursor-cache.js +238 -0
- package/dist/vendor/codeburn/src/cursor-import.js +402 -0
- package/dist/vendor/codeburn/src/cursor-sync.js +256 -0
- package/dist/vendor/codeburn/src/daily-cache.js +2036 -0
- package/dist/vendor/codeburn/src/data/litellm-snapshot.json +1 -0
- package/dist/vendor/codeburn/src/data/pricing-fallback.json +1 -0
- package/dist/vendor/codeburn/src/day-aggregator.js +327 -0
- package/dist/vendor/codeburn/src/editor-data-dir.js +11 -0
- package/dist/vendor/codeburn/src/fetch-utils.js +18 -0
- package/dist/vendor/codeburn/src/format.js +118 -0
- package/dist/vendor/codeburn/src/fs-utils.js +270 -0
- package/dist/vendor/codeburn/src/git-origin.js +334 -0
- package/dist/vendor/codeburn/src/granular-history.js +328 -0
- package/dist/vendor/codeburn/src/hermes-session-ledger.js +512 -0
- package/dist/vendor/codeburn/src/launcher-homes.js +142 -0
- package/dist/vendor/codeburn/src/live-sessions.js +358 -0
- package/dist/vendor/codeburn/src/math-utils.js +8 -0
- package/dist/vendor/codeburn/src/menubar-json.js +294 -0
- package/dist/vendor/codeburn/src/models.js +1879 -0
- package/dist/vendor/codeburn/src/optimize-one-hour-cache.js +92 -0
- package/dist/vendor/codeburn/src/optimize.js +4110 -0
- package/dist/vendor/codeburn/src/parse-workers.js +219 -0
- package/dist/vendor/codeburn/src/parser.js +6953 -0
- package/dist/vendor/codeburn/src/path-privacy.js +26 -0
- package/dist/vendor/codeburn/src/plans.js +135 -0
- package/dist/vendor/codeburn/src/pr-signals.js +132 -0
- package/dist/vendor/codeburn/src/project-filter-warnings.js +33 -0
- package/dist/vendor/codeburn/src/provider-issues.js +30 -0
- package/dist/vendor/codeburn/src/providers/amp.js +286 -0
- package/dist/vendor/codeburn/src/providers/antigravity.js +1731 -0
- package/dist/vendor/codeburn/src/providers/claude.js +551 -0
- package/dist/vendor/codeburn/src/providers/cline-cli.js +401 -0
- package/dist/vendor/codeburn/src/providers/cline.js +63 -0
- package/dist/vendor/codeburn/src/providers/codebuff.js +336 -0
- package/dist/vendor/codeburn/src/providers/codewhale.js +442 -0
- package/dist/vendor/codeburn/src/providers/codex.js +1720 -0
- package/dist/vendor/codeburn/src/providers/command-code.js +196 -0
- package/dist/vendor/codeburn/src/providers/copilot.js +3278 -0
- package/dist/vendor/codeburn/src/providers/crush.js +226 -0
- package/dist/vendor/codeburn/src/providers/cursor-agent.js +837 -0
- package/dist/vendor/codeburn/src/providers/cursor.js +1163 -0
- package/dist/vendor/codeburn/src/providers/devin.js +569 -0
- package/dist/vendor/codeburn/src/providers/droid.js +324 -0
- package/dist/vendor/codeburn/src/providers/dsh.js +690 -0
- package/dist/vendor/codeburn/src/providers/forge.js +245 -0
- package/dist/vendor/codeburn/src/providers/gemini.js +230 -0
- package/dist/vendor/codeburn/src/providers/goose.js +252 -0
- package/dist/vendor/codeburn/src/providers/grok.js +581 -0
- package/dist/vendor/codeburn/src/providers/grokbot.js +290 -0
- package/dist/vendor/codeburn/src/providers/hermes.js +771 -0
- package/dist/vendor/codeburn/src/providers/ibm-bob.js +53 -0
- package/dist/vendor/codeburn/src/providers/index.js +360 -0
- package/dist/vendor/codeburn/src/providers/kilo-code.js +50 -0
- package/dist/vendor/codeburn/src/providers/kimi.js +363 -0
- package/dist/vendor/codeburn/src/providers/kimicode.js +402 -0
- package/dist/vendor/codeburn/src/providers/kiro.js +1174 -0
- package/dist/vendor/codeburn/src/providers/lingtai-tui.js +372 -0
- package/dist/vendor/codeburn/src/providers/mistral-vibe-unified.js +236 -0
- package/dist/vendor/codeburn/src/providers/mistral-vibe.js +405 -0
- package/dist/vendor/codeburn/src/providers/mux.js +242 -0
- package/dist/vendor/codeburn/src/providers/open-design.js +217 -0
- package/dist/vendor/codeburn/src/providers/openclaude.js +309 -0
- package/dist/vendor/codeburn/src/providers/openclaw.js +442 -0
- package/dist/vendor/codeburn/src/providers/opencode-file-parser.js +131 -0
- package/dist/vendor/codeburn/src/providers/opencode.js +91 -0
- package/dist/vendor/codeburn/src/providers/pi.js +429 -0
- package/dist/vendor/codeburn/src/providers/quickdesk.js +491 -0
- package/dist/vendor/codeburn/src/providers/qwen.js +193 -0
- package/dist/vendor/codeburn/src/providers/session-message.js +118 -0
- package/dist/vendor/codeburn/src/providers/sqlite-session-parser.js +457 -0
- package/dist/vendor/codeburn/src/providers/types.js +5 -0
- package/dist/vendor/codeburn/src/providers/vercel-gateway.js +135 -0
- package/dist/vendor/codeburn/src/providers/vscode-cline-parser.js +212 -0
- package/dist/vendor/codeburn/src/providers/warp.js +575 -0
- package/dist/vendor/codeburn/src/providers/zcode.js +249 -0
- package/dist/vendor/codeburn/src/providers/zed.js +187 -0
- package/dist/vendor/codeburn/src/providers/zerostack.js +139 -0
- package/dist/vendor/codeburn/src/quota/antigravity.js +310 -0
- package/dist/vendor/codeburn/src/quota/claude.js +244 -0
- package/dist/vendor/codeburn/src/quota/cursor.js +301 -0
- package/dist/vendor/codeburn/src/quota/security.js +299 -0
- package/dist/vendor/codeburn/src/quota/types.js +1 -0
- package/dist/vendor/codeburn/src/scan-facts.js +99 -0
- package/dist/vendor/codeburn/src/session-cache.js +3040 -0
- package/dist/vendor/codeburn/src/session-count-label.js +14 -0
- package/dist/vendor/codeburn/src/session-output.js +139 -0
- package/dist/vendor/codeburn/src/session-population.js +30 -0
- package/dist/vendor/codeburn/src/sessions-report.js +1342 -0
- package/dist/vendor/codeburn/src/sqlite.js +528 -0
- package/dist/vendor/codeburn/src/sync/otlp.js +420 -0
- package/dist/vendor/codeburn/src/telemetry-snapshot.js +244 -0
- package/dist/vendor/codeburn/src/token-estimate.js +5 -0
- package/dist/vendor/codeburn/src/types.js +16 -0
- package/dist/vendor/codeburn/src/work-units.js +126 -0
- package/dist/vendor/codeburn/src/workflow-insights.js +228 -0
- package/dist/vendor/codeburn/src/wsl.js +198 -0
- package/dist/vendor/codeburn/src/yield.js +666 -0
- package/package.json +1 -1
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { readdir, readFile, stat } from 'fs/promises';
|
|
3
|
+
import { basename, join } from 'path';
|
|
4
|
+
import { homedir } from 'os';
|
|
5
|
+
import { createHash } from 'node:crypto';
|
|
6
|
+
import zlib from 'zlib';
|
|
7
|
+
import { readSessionFile } from '../fs-utils.js';
|
|
8
|
+
import { calculateCost, ownValue } from '../models.js';
|
|
9
|
+
import { extractBashCommands } from '../bash-utils.js';
|
|
10
|
+
import { getSqliteLoadError, isSqliteAvailable, isSqliteBusyError, openDatabase } from '../sqlite.js';
|
|
11
|
+
const toolNameMap = {
|
|
12
|
+
bash: 'Bash',
|
|
13
|
+
exec: 'Bash',
|
|
14
|
+
read: 'Read',
|
|
15
|
+
edit: 'Edit',
|
|
16
|
+
write: 'Write',
|
|
17
|
+
glob: 'Glob',
|
|
18
|
+
grep: 'Grep',
|
|
19
|
+
task: 'Agent',
|
|
20
|
+
dispatch_agent: 'Agent',
|
|
21
|
+
fetch: 'WebFetch',
|
|
22
|
+
search: 'WebSearch',
|
|
23
|
+
todo: 'TodoWrite',
|
|
24
|
+
patch: 'Patch',
|
|
25
|
+
};
|
|
26
|
+
function newState() {
|
|
27
|
+
return { sessionId: '', sessionTimestamp: '', currentModel: '', pendingUserMessage: '', calls: [], idLessCounts: new Map() };
|
|
28
|
+
}
|
|
29
|
+
function consumeEvent(state, entry, rowCreatedAtMs) {
|
|
30
|
+
if (entry.type === 'session') {
|
|
31
|
+
state.sessionId = entry.id ?? state.sessionId;
|
|
32
|
+
state.sessionTimestamp = entry.timestamp ?? '';
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (entry.type === 'model_change') {
|
|
36
|
+
state.currentModel = entry.modelId ?? state.currentModel;
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (entry.type === 'custom' && entry.customType === 'model-snapshot') {
|
|
40
|
+
state.currentModel = entry.data?.modelId ?? state.currentModel;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (entry.type !== 'message' || !entry.message)
|
|
44
|
+
return;
|
|
45
|
+
const msg = entry.message;
|
|
46
|
+
if (msg.role === 'user') {
|
|
47
|
+
if (!state.pendingUserMessage && Array.isArray(msg.content)) {
|
|
48
|
+
const textBlock = msg.content.find(c => c.type === 'text' && c.text);
|
|
49
|
+
state.pendingUserMessage = (textBlock?.text ?? '').slice(0, 500);
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (msg.role !== 'assistant')
|
|
54
|
+
return;
|
|
55
|
+
const model = msg.model ?? state.currentModel;
|
|
56
|
+
if (msg.usage) {
|
|
57
|
+
const { tools, bashCommands } = extractTools(msg.content);
|
|
58
|
+
// A SQLite row whose envelope carries no timestamp falls back to the row's
|
|
59
|
+
// created_at before the store-wide fallback in finalizeCalls.
|
|
60
|
+
let timestamp = entry.timestamp ?? state.sessionTimestamp;
|
|
61
|
+
if (!timestamp && rowCreatedAtMs !== undefined)
|
|
62
|
+
timestamp = new Date(rowCreatedAtMs).toISOString();
|
|
63
|
+
// An id-less envelope gets a payload hash plus its occurrence index within
|
|
64
|
+
// the session. Both eras preserve event order, so the same id-less event
|
|
65
|
+
// hashes and counts to the same key in either storage era — a session
|
|
66
|
+
// re-parsed from the other era still collapses to one — while repeats
|
|
67
|
+
// within a session no longer drop real calls as duplicates.
|
|
68
|
+
let dedupId = entry.id;
|
|
69
|
+
if (!dedupId) {
|
|
70
|
+
const hash = createHash('sha256').update(JSON.stringify([
|
|
71
|
+
model,
|
|
72
|
+
entry.timestamp ?? state.sessionTimestamp,
|
|
73
|
+
msg.usage.input,
|
|
74
|
+
msg.usage.output,
|
|
75
|
+
msg.usage.cacheRead,
|
|
76
|
+
msg.usage.cacheWrite,
|
|
77
|
+
msg.usage.cost?.total ?? 0,
|
|
78
|
+
tools,
|
|
79
|
+
bashCommands,
|
|
80
|
+
])).digest('hex').slice(0, 16);
|
|
81
|
+
const n = state.idLessCounts.get(hash) ?? 0;
|
|
82
|
+
state.idLessCounts.set(hash, n + 1);
|
|
83
|
+
dedupId = `h:${hash}:${n}`;
|
|
84
|
+
}
|
|
85
|
+
state.calls.push({
|
|
86
|
+
model,
|
|
87
|
+
usage: msg.usage,
|
|
88
|
+
tools,
|
|
89
|
+
bashCommands,
|
|
90
|
+
timestamp,
|
|
91
|
+
...(rowCreatedAtMs !== undefined ? { createdAtMs: rowCreatedAtMs } : {}),
|
|
92
|
+
userMessage: state.pendingUserMessage,
|
|
93
|
+
dedupId,
|
|
94
|
+
});
|
|
95
|
+
state.pendingUserMessage = '';
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function* finalizeCalls(state, defaultSessionId, fallbackTs, seenKeys) {
|
|
99
|
+
const sessionId = state.sessionId || defaultSessionId;
|
|
100
|
+
for (let i = 0; i < state.calls.length; i++) {
|
|
101
|
+
const call = state.calls[i];
|
|
102
|
+
const dedupKey = `openclaw:${sessionId}:${call.dedupId || i}`;
|
|
103
|
+
if (seenKeys.has(dedupKey))
|
|
104
|
+
continue;
|
|
105
|
+
seenKeys.add(dedupKey);
|
|
106
|
+
const u = call.usage;
|
|
107
|
+
// OpenClaw writes what the call actually cost, per message (never a
|
|
108
|
+
// running session total), in USD. Absent or 0 means "not recorded":
|
|
109
|
+
// those fall back to token pricing and must stay re-priceable.
|
|
110
|
+
const costFromProvider = u.cost?.total ?? 0;
|
|
111
|
+
const isReported = costFromProvider > 0;
|
|
112
|
+
const costUSD = isReported
|
|
113
|
+
? costFromProvider
|
|
114
|
+
: calculateCost(call.model, u.input, u.output, u.cacheWrite, u.cacheRead, 0);
|
|
115
|
+
let ts = new Date(call.timestamp);
|
|
116
|
+
// A present-but-unparseable envelope timestamp must not land the call on
|
|
117
|
+
// the store's mtime ("now" on a live gateway): retry the row's created_at
|
|
118
|
+
// before the store-wide fallback.
|
|
119
|
+
if (isNaN(ts.getTime()) || ts.getTime() < 1_000_000_000_000) {
|
|
120
|
+
ts = call.createdAtMs !== undefined ? new Date(call.createdAtMs) : fallbackTs;
|
|
121
|
+
}
|
|
122
|
+
if (isNaN(ts.getTime()) || ts.getTime() < 1_000_000_000_000)
|
|
123
|
+
ts = fallbackTs;
|
|
124
|
+
if (isNaN(ts.getTime()) || ts.getTime() < 1_000_000_000_000)
|
|
125
|
+
continue;
|
|
126
|
+
yield {
|
|
127
|
+
provider: 'openclaw',
|
|
128
|
+
model: call.model || 'openclaw-auto',
|
|
129
|
+
inputTokens: u.input,
|
|
130
|
+
outputTokens: u.output,
|
|
131
|
+
cacheCreationInputTokens: u.cacheWrite,
|
|
132
|
+
cacheReadInputTokens: u.cacheRead,
|
|
133
|
+
cachedInputTokens: u.cacheRead,
|
|
134
|
+
reasoningTokens: 0,
|
|
135
|
+
webSearchRequests: 0,
|
|
136
|
+
costUSD,
|
|
137
|
+
...(isReported ? { costFromBilling: true } : {}),
|
|
138
|
+
tools: [...new Set(call.tools)],
|
|
139
|
+
bashCommands: [...new Set(call.bashCommands)],
|
|
140
|
+
timestamp: ts.toISOString(),
|
|
141
|
+
speed: 'standard',
|
|
142
|
+
deduplicationKey: dedupKey,
|
|
143
|
+
userMessage: call.userMessage,
|
|
144
|
+
sessionId,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function getOpenClawDirs() {
|
|
149
|
+
const home = homedir();
|
|
150
|
+
return [
|
|
151
|
+
join(home, '.openclaw', 'agents'),
|
|
152
|
+
join(home, '.clawdbot', 'agents'),
|
|
153
|
+
join(home, '.moltbot', 'agents'),
|
|
154
|
+
join(home, '.moldbot', 'agents'),
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
function extractTools(content) {
|
|
158
|
+
const tools = [];
|
|
159
|
+
const bashCommands = [];
|
|
160
|
+
if (!content)
|
|
161
|
+
return { tools, bashCommands };
|
|
162
|
+
for (const block of content) {
|
|
163
|
+
if ((block.type === 'tool_use' || block.type === 'toolCall') && block.name) {
|
|
164
|
+
const mapped = ownValue(toolNameMap, block.name) ?? block.name;
|
|
165
|
+
tools.push(mapped);
|
|
166
|
+
if (mapped === 'Bash' && block.arguments && typeof block.arguments.command === 'string') {
|
|
167
|
+
bashCommands.push(...extractBashCommands(block.arguments.command));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return { tools, bashCommands };
|
|
172
|
+
}
|
|
173
|
+
function createJsonlParser(source, seenKeys) {
|
|
174
|
+
return {
|
|
175
|
+
async *parse() {
|
|
176
|
+
const raw = await readSessionFile(source.path);
|
|
177
|
+
if (raw === null)
|
|
178
|
+
return;
|
|
179
|
+
const state = newState();
|
|
180
|
+
for (const line of raw.split('\n')) {
|
|
181
|
+
if (!line.trim())
|
|
182
|
+
continue;
|
|
183
|
+
let entry;
|
|
184
|
+
try {
|
|
185
|
+
entry = JSON.parse(line);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
consumeEvent(state, entry);
|
|
191
|
+
}
|
|
192
|
+
// Fallback for a call whose own timestamp is missing/unparseable: the
|
|
193
|
+
// file's mtime keeps the call inside the session's window instead of
|
|
194
|
+
// dropping real spend (matches openclaude.ts/codewhale.ts).
|
|
195
|
+
const fileStat = await stat(source.path).catch(() => null);
|
|
196
|
+
const fileMtime = fileStat ? fileStat.mtime : new Date(NaN);
|
|
197
|
+
yield* finalizeCalls(state, basename(source.path, '.jsonl'), fileMtime, seenKeys);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/// agent-schema 23 (in flight upstream) lets a transcript row store its event
|
|
202
|
+
/// as a zstd BLOB instead of event_json TEXT.
|
|
203
|
+
// The schema CHECK bounds a compressed payload and its decoded form at 4 MiB;
|
|
204
|
+
// decode with headroom rather than an unbounded buffer.
|
|
205
|
+
const MAX_EVENT_DECODED_BYTES = 8 * 1024 * 1024;
|
|
206
|
+
const sqliteNoticeSent = new Set();
|
|
207
|
+
function sqliteNotice(message) {
|
|
208
|
+
if (sqliteNoticeSent.has(message))
|
|
209
|
+
return;
|
|
210
|
+
sqliteNoticeSent.add(message);
|
|
211
|
+
process.stderr.write(message);
|
|
212
|
+
}
|
|
213
|
+
function transcriptHasZstdColumn(db) {
|
|
214
|
+
try {
|
|
215
|
+
const columns = db.query('PRAGMA table_info(transcript_events)');
|
|
216
|
+
return columns.some(c => c.name === 'event_zstd');
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function decodeEventPayload(row, dbPath) {
|
|
223
|
+
if (typeof row.event_json === 'string')
|
|
224
|
+
return row.event_json;
|
|
225
|
+
if (row.event_zstd == null || !(row.event_zstd instanceof Uint8Array))
|
|
226
|
+
return null;
|
|
227
|
+
try {
|
|
228
|
+
return zlib.zstdDecompressSync(row.event_zstd, { maxOutputLength: MAX_EVENT_DECODED_BYTES }).toString('utf-8');
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// A torn row understates usage silently unless we say so; one notice per
|
|
232
|
+
// store keeps it visible without per-row spam.
|
|
233
|
+
sqliteNotice(`codeburn: openclaw: ${dbPath} has compressed transcript rows that fail to decode; skipping them\n`);
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/// Source paths carry the session id after the database path (forge.ts
|
|
238
|
+
/// convention), so each SQLite session parses — and dedups — on its own.
|
|
239
|
+
function splitSqliteSource(path) {
|
|
240
|
+
const idx = path.lastIndexOf(':');
|
|
241
|
+
if (idx < 0)
|
|
242
|
+
return null;
|
|
243
|
+
return { dbPath: path.slice(0, idx), sessionId: path.slice(idx + 1) };
|
|
244
|
+
}
|
|
245
|
+
// Keyset batches keep peak memory to one batch of envelopes, not the whole
|
|
246
|
+
// session, for agents whose transcript DB reaches gigabytes (#1504 class).
|
|
247
|
+
const TRANSCRIPT_BATCH_ROWS = 2000;
|
|
248
|
+
function createSqliteParser(source, seenKeys) {
|
|
249
|
+
return {
|
|
250
|
+
async *parse() {
|
|
251
|
+
if (!isSqliteAvailable()) {
|
|
252
|
+
process.stderr.write(getSqliteLoadError() + '\n');
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const split = splitSqliteSource(source.path);
|
|
256
|
+
if (!split)
|
|
257
|
+
return;
|
|
258
|
+
let db;
|
|
259
|
+
try {
|
|
260
|
+
db = openDatabase(split.dbPath);
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const state = newState();
|
|
266
|
+
try {
|
|
267
|
+
const zstdColumn = transcriptHasZstdColumn(db) ? ', event_zstd' : '';
|
|
268
|
+
let lastSeq = -1;
|
|
269
|
+
for (;;) {
|
|
270
|
+
const rows = db.query(`SELECT seq, event_json${zstdColumn}, created_at
|
|
271
|
+
FROM transcript_events
|
|
272
|
+
WHERE session_id = ? AND seq > ?
|
|
273
|
+
ORDER BY seq
|
|
274
|
+
LIMIT ?`, [split.sessionId, lastSeq, TRANSCRIPT_BATCH_ROWS]);
|
|
275
|
+
for (const row of rows) {
|
|
276
|
+
lastSeq = Number(row.seq);
|
|
277
|
+
const payload = decodeEventPayload(row, split.dbPath);
|
|
278
|
+
if (payload === null)
|
|
279
|
+
continue;
|
|
280
|
+
let entry;
|
|
281
|
+
try {
|
|
282
|
+
entry = JSON.parse(payload);
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
consumeEvent(state, entry, Number(row.created_at));
|
|
288
|
+
}
|
|
289
|
+
if (rows.length < TRANSCRIPT_BATCH_ROWS)
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
finally {
|
|
294
|
+
db.close();
|
|
295
|
+
}
|
|
296
|
+
// Same fallback policy as the JSONL side, but against the store: a call
|
|
297
|
+
// with no usable timestamp rides the database file's mtime.
|
|
298
|
+
const fileStat = await stat(split.dbPath).catch(() => null);
|
|
299
|
+
const fileMtime = fileStat ? fileStat.mtime : new Date(NaN);
|
|
300
|
+
yield* finalizeCalls(state, split.sessionId, fileMtime, seenKeys);
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
async function discoverSqliteSessions(agentsDir, agent) {
|
|
305
|
+
const dbPath = join(agentsDir, agent, 'agent', 'openclaw-agent.sqlite');
|
|
306
|
+
const dbStat = await stat(dbPath).catch(() => null);
|
|
307
|
+
if (!dbStat)
|
|
308
|
+
return { sources: [], sessionIds: new Set() };
|
|
309
|
+
if (!isSqliteAvailable()) {
|
|
310
|
+
sqliteNotice(getSqliteLoadError() + '\n');
|
|
311
|
+
return { sources: [], sessionIds: new Set() };
|
|
312
|
+
}
|
|
313
|
+
let db;
|
|
314
|
+
try {
|
|
315
|
+
db = openDatabase(dbPath);
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
// A locked store proves nothing about absence: reading it as "no sessions"
|
|
319
|
+
// would let that run look complete-but-empty while the legacy JSONL of
|
|
320
|
+
// already-migrated sessions comes back. BUSY propagates like hermes.ts.
|
|
321
|
+
if (isSqliteBusyError(err))
|
|
322
|
+
throw err;
|
|
323
|
+
return { sources: [], sessionIds: new Set() };
|
|
324
|
+
}
|
|
325
|
+
try {
|
|
326
|
+
const rows = db.query('SELECT session_id FROM transcript_events GROUP BY session_id ORDER BY session_id');
|
|
327
|
+
const sources = [];
|
|
328
|
+
const sessionIds = new Set();
|
|
329
|
+
for (const row of rows) {
|
|
330
|
+
if (typeof row.session_id !== 'string' || !row.session_id)
|
|
331
|
+
continue;
|
|
332
|
+
sessionIds.add(row.session_id);
|
|
333
|
+
sources.push({ path: `${dbPath}:${row.session_id}`, project: agent, provider: 'openclaw' });
|
|
334
|
+
}
|
|
335
|
+
return { sources, sessionIds };
|
|
336
|
+
}
|
|
337
|
+
catch (err) {
|
|
338
|
+
if (isSqliteBusyError(err))
|
|
339
|
+
throw err;
|
|
340
|
+
// Not an agent-schema store (no transcript_events) or unreadable: the
|
|
341
|
+
// JSONL discovery still covers pre-migration layouts.
|
|
342
|
+
return { sources: [], sessionIds: new Set() };
|
|
343
|
+
}
|
|
344
|
+
finally {
|
|
345
|
+
db.close();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
async function discoverInDir(agentsDir) {
|
|
349
|
+
const sources = [];
|
|
350
|
+
let agentDirs;
|
|
351
|
+
try {
|
|
352
|
+
const entries = await readdir(agentsDir, { withFileTypes: true });
|
|
353
|
+
agentDirs = entries.filter(e => e.isDirectory()).map(e => e.name);
|
|
354
|
+
}
|
|
355
|
+
catch {
|
|
356
|
+
return sources;
|
|
357
|
+
}
|
|
358
|
+
for (const agent of agentDirs) {
|
|
359
|
+
const sessionsDir = join(agentsDir, agent, 'sessions');
|
|
360
|
+
let indexData = {};
|
|
361
|
+
try {
|
|
362
|
+
const indexRaw = await readFile(join(sessionsDir, 'sessions.json'), 'utf-8');
|
|
363
|
+
indexData = JSON.parse(indexRaw);
|
|
364
|
+
}
|
|
365
|
+
catch { /* no index, fall back to directory scan */ }
|
|
366
|
+
const seenFiles = new Set();
|
|
367
|
+
const jsonlSources = [];
|
|
368
|
+
for (const entry of Object.values(indexData)) {
|
|
369
|
+
if (entry.sessionFile) {
|
|
370
|
+
seenFiles.add(entry.sessionFile);
|
|
371
|
+
jsonlSources.push({ source: { path: entry.sessionFile, project: agent, provider: 'openclaw' }, sessionId: entry.sessionId });
|
|
372
|
+
}
|
|
373
|
+
else if (entry.sessionId) {
|
|
374
|
+
const filePath = join(sessionsDir, `${entry.sessionId}.jsonl`);
|
|
375
|
+
seenFiles.add(filePath);
|
|
376
|
+
jsonlSources.push({ source: { path: filePath, project: agent, provider: 'openclaw' }, sessionId: entry.sessionId });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
try {
|
|
380
|
+
const files = await readdir(sessionsDir);
|
|
381
|
+
for (const f of files) {
|
|
382
|
+
if (!f.endsWith('.jsonl'))
|
|
383
|
+
continue;
|
|
384
|
+
const filePath = join(sessionsDir, f);
|
|
385
|
+
if (seenFiles.has(filePath))
|
|
386
|
+
continue;
|
|
387
|
+
jsonlSources.push({ source: { path: filePath, project: agent, provider: 'openclaw' }, sessionId: basename(f, '.jsonl') });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
catch { /* directory may not exist */ }
|
|
391
|
+
// Post-2026-09-01 the transcript lives in the per-agent SQLite store and
|
|
392
|
+
// the .jsonl files are archived or renamed *.jsonl.deleted.<ts> (#1259).
|
|
393
|
+
const sqlite = await discoverSqliteSessions(agentsDir, agent);
|
|
394
|
+
// Once a session was imported, the store is authoritative and a legacy
|
|
395
|
+
// file still on disk (partial migration, restored backup) must not shadow
|
|
396
|
+
// it. Keeping both live is not an alternative: the parse-time dedup seeds
|
|
397
|
+
// from the legacy file's cached turns, so the store's first parse would
|
|
398
|
+
// bake a suppressed result into its session-cache entry, and once the
|
|
399
|
+
// legacy file is archived and its cache entry evicted, the imported
|
|
400
|
+
// history would be gone from every report until the store changed.
|
|
401
|
+
for (const { source, sessionId } of jsonlSources) {
|
|
402
|
+
if (sqlite.sessionIds.has(sessionId))
|
|
403
|
+
continue;
|
|
404
|
+
sources.push(source);
|
|
405
|
+
}
|
|
406
|
+
sources.push(...sqlite.sources);
|
|
407
|
+
}
|
|
408
|
+
return sources;
|
|
409
|
+
}
|
|
410
|
+
export function createOpenClawProvider(overrideDir) {
|
|
411
|
+
return {
|
|
412
|
+
name: 'openclaw',
|
|
413
|
+
displayName: 'OpenClaw',
|
|
414
|
+
modelDisplayName(model) {
|
|
415
|
+
return model;
|
|
416
|
+
},
|
|
417
|
+
toolDisplayName(rawTool) {
|
|
418
|
+
return ownValue(toolNameMap, rawTool) ?? rawTool;
|
|
419
|
+
},
|
|
420
|
+
async probeRoots() {
|
|
421
|
+
// #899: empty agents root that exists must be distinguishable from missing install.
|
|
422
|
+
const roots = overrideDir ? [overrideDir] : getOpenClawDirs();
|
|
423
|
+
return roots.map(path => ({ path, label: 'agents' }));
|
|
424
|
+
},
|
|
425
|
+
async discoverSessions() {
|
|
426
|
+
if (overrideDir)
|
|
427
|
+
return discoverInDir(overrideDir);
|
|
428
|
+
const all = [];
|
|
429
|
+
for (const dir of getOpenClawDirs()) {
|
|
430
|
+
const sessions = await discoverInDir(dir);
|
|
431
|
+
all.push(...sessions);
|
|
432
|
+
}
|
|
433
|
+
return all;
|
|
434
|
+
},
|
|
435
|
+
createSessionParser(source, seenKeys) {
|
|
436
|
+
return source.path.endsWith('.jsonl')
|
|
437
|
+
? createJsonlParser(source, seenKeys)
|
|
438
|
+
: createSqliteParser(source, seenKeys);
|
|
439
|
+
},
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
export const openclaw = createOpenClawProvider();
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { readdir, readFile } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { buildAssistantCall, sanitize } from './session-message.js';
|
|
5
|
+
async function readJson(path) {
|
|
6
|
+
try {
|
|
7
|
+
return JSON.parse(await readFile(path, 'utf8'));
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
async function readParts(dataDir, messageId) {
|
|
14
|
+
const dir = join(dataDir, 'storage', 'part', messageId);
|
|
15
|
+
let files;
|
|
16
|
+
try {
|
|
17
|
+
files = (await readdir(dir)).sort();
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
const parts = [];
|
|
23
|
+
for (const f of files) {
|
|
24
|
+
if (!f.endsWith('.json'))
|
|
25
|
+
continue;
|
|
26
|
+
const part = await readJson(join(dir, f));
|
|
27
|
+
if (part)
|
|
28
|
+
parts.push(part);
|
|
29
|
+
}
|
|
30
|
+
return parts;
|
|
31
|
+
}
|
|
32
|
+
export async function discoverOpenCodeFileSessions(dataDir, providerName) {
|
|
33
|
+
const sessionRoot = join(dataDir, 'storage', 'session');
|
|
34
|
+
let projectDirs;
|
|
35
|
+
try {
|
|
36
|
+
projectDirs = await readdir(sessionRoot);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const sources = [];
|
|
42
|
+
for (const project of projectDirs) {
|
|
43
|
+
let files;
|
|
44
|
+
try {
|
|
45
|
+
files = await readdir(join(sessionRoot, project));
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
for (const f of files) {
|
|
51
|
+
if (!f.endsWith('.json'))
|
|
52
|
+
continue;
|
|
53
|
+
const path = join(sessionRoot, project, f);
|
|
54
|
+
const meta = await readJson(path);
|
|
55
|
+
if (!meta?.id)
|
|
56
|
+
continue;
|
|
57
|
+
sources.push({
|
|
58
|
+
path,
|
|
59
|
+
project: sanitize(meta.directory || meta.title || ''),
|
|
60
|
+
provider: providerName,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return sources;
|
|
65
|
+
}
|
|
66
|
+
export function createOpenCodeFileSessionParser(source, seenKeys, dataDir, providerName) {
|
|
67
|
+
return {
|
|
68
|
+
async *parse() {
|
|
69
|
+
const meta = await readJson(source.path);
|
|
70
|
+
if (!meta?.id)
|
|
71
|
+
return;
|
|
72
|
+
const sessionId = meta.id;
|
|
73
|
+
const messageDir = join(dataDir, 'storage', 'message', sessionId);
|
|
74
|
+
let messageFiles;
|
|
75
|
+
try {
|
|
76
|
+
messageFiles = await readdir(messageDir);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const messages = [];
|
|
82
|
+
for (const f of messageFiles) {
|
|
83
|
+
if (!f.endsWith('.json'))
|
|
84
|
+
continue;
|
|
85
|
+
const data = await readJson(join(messageDir, f));
|
|
86
|
+
if (!data)
|
|
87
|
+
continue;
|
|
88
|
+
messages.push({ id: data.id ?? f.replace(/\.json$/, ''), data });
|
|
89
|
+
}
|
|
90
|
+
messages.sort((a, b) => {
|
|
91
|
+
const byTime = (a.data.time?.created ?? 0) - (b.data.time?.created ?? 0);
|
|
92
|
+
if (byTime !== 0)
|
|
93
|
+
return byTime;
|
|
94
|
+
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
|
95
|
+
});
|
|
96
|
+
let currentUserMessage = '';
|
|
97
|
+
for (const { id, data } of messages) {
|
|
98
|
+
if (data.role === 'user') {
|
|
99
|
+
const parts = await readParts(dataDir, id);
|
|
100
|
+
const text = parts
|
|
101
|
+
.filter((p) => p.type === 'text')
|
|
102
|
+
.map((p) => p.text ?? '')
|
|
103
|
+
.filter(Boolean)
|
|
104
|
+
.join(' ');
|
|
105
|
+
if (text)
|
|
106
|
+
currentUserMessage = text;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (data.role !== 'assistant' && data.role !== 'model')
|
|
110
|
+
continue;
|
|
111
|
+
const dedupKey = `${providerName}:${sessionId}:${id}`;
|
|
112
|
+
if (seenKeys.has(dedupKey))
|
|
113
|
+
continue;
|
|
114
|
+
const parts = await readParts(dataDir, id);
|
|
115
|
+
const call = buildAssistantCall({
|
|
116
|
+
providerName,
|
|
117
|
+
dedupKey,
|
|
118
|
+
sessionId,
|
|
119
|
+
data,
|
|
120
|
+
parts,
|
|
121
|
+
timeCreatedMs: data.time?.created ?? meta.time?.created ?? 0,
|
|
122
|
+
userMessage: currentUserMessage,
|
|
123
|
+
});
|
|
124
|
+
if (!call)
|
|
125
|
+
continue;
|
|
126
|
+
seenKeys.add(dedupKey);
|
|
127
|
+
yield call;
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { homedir } from 'os';
|
|
4
|
+
import { getShortModelName, ownValue } from '../models.js';
|
|
5
|
+
import { discoverSqliteSessions, createSqliteSessionParser } from './sqlite-session-parser.js';
|
|
6
|
+
import { discoverOpenCodeFileSessions, createOpenCodeFileSessionParser } from './opencode-file-parser.js';
|
|
7
|
+
const toolNameMap = {
|
|
8
|
+
bash: 'Bash',
|
|
9
|
+
read: 'Read',
|
|
10
|
+
edit: 'Edit',
|
|
11
|
+
write: 'Write',
|
|
12
|
+
glob: 'Glob',
|
|
13
|
+
grep: 'Grep',
|
|
14
|
+
task: 'Agent',
|
|
15
|
+
fetch: 'WebFetch',
|
|
16
|
+
search: 'WebSearch',
|
|
17
|
+
todo: 'TodoWrite',
|
|
18
|
+
skill: 'Skill',
|
|
19
|
+
patch: 'Patch',
|
|
20
|
+
};
|
|
21
|
+
function getDataDir(dataDir) {
|
|
22
|
+
// Test seam: createOpenCodeProvider(tmpDir) points at a base dir that still
|
|
23
|
+
// gets the 'opencode' subdirectory appended, preserving existing fixtures
|
|
24
|
+
// (tmpDir/opencode/opencode*.db and tmpDir/opencode/storage/...).
|
|
25
|
+
if (dataDir)
|
|
26
|
+
return join(dataDir, 'opencode');
|
|
27
|
+
// Production override for OpenCode-compatible forks/renames (e.g. MiMoCode at
|
|
28
|
+
// ~/.local/share/mimocode). This is the EXACT data directory — no 'opencode'
|
|
29
|
+
// suffix — so a fork writing <dir>/<prefix>*.db or <dir>/storage/... is found
|
|
30
|
+
// instead of silently yielding zero sessions. (issue #617)
|
|
31
|
+
const override = process.env['OPENCODE_DATA_DIR'];
|
|
32
|
+
if (override)
|
|
33
|
+
return override;
|
|
34
|
+
// Default: $XDG_DATA_HOME/opencode or ~/.local/share/opencode.
|
|
35
|
+
const base = process.env['XDG_DATA_HOME'] ?? join(homedir(), '.local', 'share');
|
|
36
|
+
return join(base, 'opencode');
|
|
37
|
+
}
|
|
38
|
+
function getSqliteConfig(dataDir) {
|
|
39
|
+
return {
|
|
40
|
+
providerName: 'opencode',
|
|
41
|
+
displayName: 'OpenCode',
|
|
42
|
+
dbDir: getDataDir(dataDir),
|
|
43
|
+
// Truthy check (not `??`): an empty-string `OPENCODE_DB_PREFIX` must fall
|
|
44
|
+
// back to 'opencode'. With `??`, '' survives as the prefix and
|
|
45
|
+
// `discoverSqliteSessions` matches every '*.db' file (filename.startsWith('')
|
|
46
|
+
// is always true), sweeping unrelated DBs into discovery. Aligns with
|
|
47
|
+
// `OPENCODE_DATA_DIR`'s truthy handling above, and makes behavior identical
|
|
48
|
+
// for unset vs empty — which matches the env fingerprint, since
|
|
49
|
+
// `computeEnvFingerprint` collapses both to 'OPENCODE_DB_PREFIX='. (issue #617)
|
|
50
|
+
dbFilePrefix: process.env['OPENCODE_DB_PREFIX'] || 'opencode',
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function createOpenCodeProvider(dataDir) {
|
|
54
|
+
const sqliteConfig = getSqliteConfig(dataDir);
|
|
55
|
+
const resolvedDataDir = getDataDir(dataDir);
|
|
56
|
+
return {
|
|
57
|
+
name: 'opencode',
|
|
58
|
+
displayName: 'OpenCode',
|
|
59
|
+
modelDisplayName(model) {
|
|
60
|
+
const stripped = model.replace(/^[^/]+\//, '');
|
|
61
|
+
return getShortModelName(stripped);
|
|
62
|
+
},
|
|
63
|
+
toolDisplayName(rawTool) {
|
|
64
|
+
return ownValue(toolNameMap, rawTool) ?? rawTool;
|
|
65
|
+
},
|
|
66
|
+
// OpenCode migrated from file-based JSON (storage/session/*.json) to a
|
|
67
|
+
// SQLite DB (opencode.db). After an in-place upgrade, legacy JSON files
|
|
68
|
+
// remain on disk while all new data flows into the SQLite DB. Merge both
|
|
69
|
+
// sources so migrated installs keep reporting legacy sessions AND pick up
|
|
70
|
+
// current SQLite data. Dedup is handled per-message in createSessionParser
|
|
71
|
+
// via seenKeys (keyed by `${provider}:${sessionId}:${messageId}`).
|
|
72
|
+
// Both the legacy JSON store (storage/session/*.json) and the SQLite DB
|
|
73
|
+
// (opencode*.db) live under this one data dir, resolved via OPENCODE_DATA_DIR
|
|
74
|
+
// or XDG_DATA_HOME the same way discoverSessions does.
|
|
75
|
+
async probeRoots() {
|
|
76
|
+
return [{ path: resolvedDataDir, label: 'data' }];
|
|
77
|
+
},
|
|
78
|
+
async discoverSessions() {
|
|
79
|
+
const fileSessions = await discoverOpenCodeFileSessions(resolvedDataDir, 'opencode');
|
|
80
|
+
const sqliteSessions = await discoverSqliteSessions(sqliteConfig);
|
|
81
|
+
return [...fileSessions, ...sqliteSessions];
|
|
82
|
+
},
|
|
83
|
+
createSessionParser(source, seenKeys) {
|
|
84
|
+
if (source.path.endsWith('.json')) {
|
|
85
|
+
return createOpenCodeFileSessionParser(source, seenKeys, resolvedDataDir, 'opencode');
|
|
86
|
+
}
|
|
87
|
+
return createSqliteSessionParser(source, seenKeys, sqliteConfig);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export const opencode = createOpenCodeProvider();
|