chompyy 0.4.7 → 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 +40 -6
- package/dist/advice.js +2 -1
- package/dist/agentlimits.js +77 -0
- package/dist/agentusage.js +96 -0
- package/dist/appstate.js +72 -5
- package/dist/away.js +44 -5
- package/dist/blueprint.js +3 -7
- package/dist/board.js +4 -0
- package/dist/cli.js +328 -15
- 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/daytime.js +68 -0
- package/dist/demon.js +20 -0
- package/dist/didwork.js +46 -0
- package/dist/label.js +33 -0
- package/dist/learning.js +35 -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/send.js +46 -20
- package/dist/stamps.js +14 -0
- package/dist/taste.js +67 -7
- package/dist/triggers.js +5 -0
- package/dist/unkeep.js +220 -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 +11 -3
|
@@ -0,0 +1,837 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { createHash } from 'crypto';
|
|
3
|
+
import { existsSync } from 'fs';
|
|
4
|
+
import { readdir, readFile, stat } from 'fs/promises';
|
|
5
|
+
import { join, basename, dirname } from 'path';
|
|
6
|
+
import { homedir } from 'os';
|
|
7
|
+
import { calculateCost, getShortModelName, ownValue } from '../models.js';
|
|
8
|
+
import { blobToText, isSqliteAvailable, isSqliteBusyError, openDatabase } from '../sqlite.js';
|
|
9
|
+
import { normalizeContentBlocks } from '../content-utils.js';
|
|
10
|
+
import { estimateTokensFromChars } from '../token-estimate.js';
|
|
11
|
+
import { getCursorDbPath } from './cursor.js';
|
|
12
|
+
const CURSOR_AGENT_COST_MODEL = 'claude-sonnet-4-5';
|
|
13
|
+
const MAX_USER_TEXT_LENGTH = 500;
|
|
14
|
+
const DIGITS_ONLY = /^\d+$/;
|
|
15
|
+
const UUID_LIKE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
|
+
const USER_MARKER = /^\s*user:\s*/i;
|
|
17
|
+
const ASSISTANT_MARKER = /^\s*A:\s*/;
|
|
18
|
+
const THINKING_MARKER = /^\s*\[Thinking\]\s*/;
|
|
19
|
+
const TOOL_CALL_MARKER = /^\s*\[Tool call\]\s*(.+?)\s*$/i;
|
|
20
|
+
const TOOL_RESULT_MARKER = /^\s*\[Tool result\]\b/i;
|
|
21
|
+
const USER_QUERY_OPEN = '<user_query>';
|
|
22
|
+
const USER_QUERY_CLOSE = '</user_query>';
|
|
23
|
+
const warnedUnrecognizedTranscripts = new Set();
|
|
24
|
+
const STORE_DB_NAME = 'store.db';
|
|
25
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
26
|
+
// Cursor stamps each prompt with the local wall-clock minute and its offset:
|
|
27
|
+
// `<timestamp>Thursday, Sep 3, 2026, 6:52 AM (UTC-7)</timestamp>`.
|
|
28
|
+
const PROMPT_TIMESTAMP = /<timestamp>[A-Za-z]+, ([A-Za-z]{3}) (\d{1,2}), (\d{4}), (\d{1,2}):(\d{2}) (AM|PM) \(UTC(?:([+-]\d{1,2})(?::(\d{2}))?)?\)<\/timestamp>/;
|
|
29
|
+
// Root conversation-state blob (protobuf): field 1 repeats the message blob
|
|
30
|
+
// ids in order, field 9 is the workspace URI, field 26 a millisecond stamp.
|
|
31
|
+
const ROOT_MESSAGE_FIELD = 1;
|
|
32
|
+
const ROOT_WORKSPACE_FIELD = 9;
|
|
33
|
+
const ROOT_TIMESTAMP_FIELD = 26;
|
|
34
|
+
const CONVERSATION_SUMMARY_QUERY = `
|
|
35
|
+
SELECT conversationId, model, title, updatedAt
|
|
36
|
+
FROM conversation_summaries
|
|
37
|
+
WHERE conversationId = ?
|
|
38
|
+
`;
|
|
39
|
+
const modelDisplayNames = {
|
|
40
|
+
'claude-4.5-opus-high-thinking': 'Opus 4.5 (Thinking)',
|
|
41
|
+
'claude-4-opus': 'Opus 4',
|
|
42
|
+
'claude-4-sonnet-thinking': 'Sonnet 4 (Thinking)',
|
|
43
|
+
'claude-4.5-sonnet-thinking': 'Sonnet 4.5 (Thinking)',
|
|
44
|
+
'claude-4.6-sonnet': 'Sonnet 4.6',
|
|
45
|
+
'composer-1': 'Composer 1',
|
|
46
|
+
'grok-code-fast-1': 'Grok Code Fast',
|
|
47
|
+
'gemini-3-pro': 'Gemini 3 Pro',
|
|
48
|
+
'gpt-5.1-codex-high': 'GPT-5.1 Codex',
|
|
49
|
+
'gpt-5': 'GPT-5',
|
|
50
|
+
'gpt-4.1': 'GPT-4.1',
|
|
51
|
+
default: 'Auto (Sonnet est.)',
|
|
52
|
+
};
|
|
53
|
+
function getCursorAgentBaseDir(baseDirOverride) {
|
|
54
|
+
if (baseDirOverride)
|
|
55
|
+
return baseDirOverride;
|
|
56
|
+
// Windows paths unverified; tracked as Open Question 3 in issue #55.
|
|
57
|
+
return join(homedir(), '.cursor');
|
|
58
|
+
}
|
|
59
|
+
function getProjectsDir(baseDir) {
|
|
60
|
+
return join(baseDir, 'projects');
|
|
61
|
+
}
|
|
62
|
+
function getChatsDir(baseDir) {
|
|
63
|
+
return join(baseDir, 'chats');
|
|
64
|
+
}
|
|
65
|
+
function getAttributionDbPath(baseDir) {
|
|
66
|
+
return join(baseDir, 'ai-tracking', 'ai-code-tracking.db');
|
|
67
|
+
}
|
|
68
|
+
function estimateTokens(charCount) {
|
|
69
|
+
if (charCount <= 0)
|
|
70
|
+
return 0;
|
|
71
|
+
return estimateTokensFromChars(charCount);
|
|
72
|
+
}
|
|
73
|
+
function parseToolName(raw) {
|
|
74
|
+
const clean = raw.trim();
|
|
75
|
+
if (clean.length === 0)
|
|
76
|
+
return 'unknown';
|
|
77
|
+
return clean.toLowerCase().replace(/\s+/g, '-');
|
|
78
|
+
}
|
|
79
|
+
function normalizeTimestamp(raw) {
|
|
80
|
+
if (raw === null || raw === undefined)
|
|
81
|
+
return null;
|
|
82
|
+
if (typeof raw === 'string') {
|
|
83
|
+
const trimmed = raw.trim();
|
|
84
|
+
if (trimmed.length === 0)
|
|
85
|
+
return null;
|
|
86
|
+
if (DIGITS_ONLY.test(trimmed)) {
|
|
87
|
+
const num = Number(trimmed);
|
|
88
|
+
if (!Number.isNaN(num)) {
|
|
89
|
+
const ms = num < 1e12 ? num * 1000 : num;
|
|
90
|
+
return new Date(ms).toISOString();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const parsed = new Date(trimmed);
|
|
94
|
+
if (!Number.isNaN(parsed.getTime()))
|
|
95
|
+
return parsed.toISOString();
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const ms = raw < 1e12 ? raw * 1000 : raw;
|
|
99
|
+
return new Date(ms).toISOString();
|
|
100
|
+
}
|
|
101
|
+
function prettifyProjectId(raw) {
|
|
102
|
+
if (!raw)
|
|
103
|
+
return raw;
|
|
104
|
+
if (DIGITS_ONLY.test(raw)) {
|
|
105
|
+
const num = Number(raw);
|
|
106
|
+
if (!Number.isNaN(num) && raw.length >= 13) {
|
|
107
|
+
const iso = new Date(num).toISOString();
|
|
108
|
+
return `cursor-agent:${iso}`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const withoutPrefix = raw.replace(/^-Users-/, '');
|
|
112
|
+
const parts = withoutPrefix.split('-').filter(Boolean);
|
|
113
|
+
if (parts.length > 0)
|
|
114
|
+
return parts[parts.length - 1];
|
|
115
|
+
return raw;
|
|
116
|
+
}
|
|
117
|
+
function resolveModel(raw) {
|
|
118
|
+
if (!raw || raw === 'default')
|
|
119
|
+
return 'cursor-agent-auto';
|
|
120
|
+
return raw;
|
|
121
|
+
}
|
|
122
|
+
function costModel(model) {
|
|
123
|
+
return model === 'cursor-agent-auto' ? CURSOR_AGENT_COST_MODEL : model;
|
|
124
|
+
}
|
|
125
|
+
function transcriptStem(transcriptPath) {
|
|
126
|
+
const name = basename(transcriptPath);
|
|
127
|
+
if (name.endsWith('.jsonl'))
|
|
128
|
+
return name.slice(0, -'.jsonl'.length);
|
|
129
|
+
if (name.endsWith('.txt'))
|
|
130
|
+
return name.slice(0, -'.txt'.length);
|
|
131
|
+
return name;
|
|
132
|
+
}
|
|
133
|
+
function toConversationId(transcriptPath) {
|
|
134
|
+
const filename = transcriptStem(transcriptPath);
|
|
135
|
+
if (filename.length === 36 && UUID_LIKE.test(filename))
|
|
136
|
+
return filename;
|
|
137
|
+
return createHash('sha1').update(transcriptPath).digest('hex').slice(0, 16);
|
|
138
|
+
}
|
|
139
|
+
async function appendTranscriptSources(scanDir, projectId, sources) {
|
|
140
|
+
const transcriptEntries = await readdir(scanDir, { withFileTypes: true });
|
|
141
|
+
for (const transcript of transcriptEntries) {
|
|
142
|
+
// Legacy format: .txt files directly in the scan dir
|
|
143
|
+
if (transcript.isFile() && transcript.name.endsWith('.txt')) {
|
|
144
|
+
sources.push({
|
|
145
|
+
path: join(scanDir, transcript.name),
|
|
146
|
+
project: projectId,
|
|
147
|
+
provider: 'cursor-agent',
|
|
148
|
+
});
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
// Composer 2 format: UUID subdirectories with .jsonl files
|
|
152
|
+
if (transcript.isDirectory() && UUID_LIKE.test(transcript.name)) {
|
|
153
|
+
const subdir = join(scanDir, transcript.name);
|
|
154
|
+
const subEntries = await readdir(subdir, { withFileTypes: true }).catch(() => []);
|
|
155
|
+
const transcriptFilesByStem = new Map();
|
|
156
|
+
for (const sub of subEntries) {
|
|
157
|
+
if (sub.isFile() && (sub.name.endsWith('.jsonl') || sub.name.endsWith('.txt'))) {
|
|
158
|
+
const stem = transcriptStem(sub.name);
|
|
159
|
+
const existing = transcriptFilesByStem.get(stem) ?? {};
|
|
160
|
+
if (sub.name.endsWith('.jsonl')) {
|
|
161
|
+
transcriptFilesByStem.set(stem, { ...existing, jsonl: sub.name });
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
transcriptFilesByStem.set(stem, { ...existing, txt: sub.name });
|
|
165
|
+
}
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
// Subagent transcripts inside a subagents/ directory
|
|
169
|
+
if (sub.isDirectory() && sub.name === 'subagents') {
|
|
170
|
+
const subagentEntries = await readdir(join(subdir, sub.name), { withFileTypes: true }).catch(() => []);
|
|
171
|
+
for (const sa of subagentEntries) {
|
|
172
|
+
if (!sa.isFile())
|
|
173
|
+
continue;
|
|
174
|
+
if (!sa.name.endsWith('.jsonl') && !sa.name.endsWith('.txt'))
|
|
175
|
+
continue;
|
|
176
|
+
sources.push({
|
|
177
|
+
path: join(subdir, sub.name, sa.name),
|
|
178
|
+
project: projectId,
|
|
179
|
+
provider: 'cursor-agent',
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const files of transcriptFilesByStem.values()) {
|
|
185
|
+
const selectedName = files.jsonl ?? files.txt;
|
|
186
|
+
if (selectedName) {
|
|
187
|
+
sources.push({
|
|
188
|
+
path: join(subdir, selectedName),
|
|
189
|
+
project: projectId,
|
|
190
|
+
provider: 'cursor-agent',
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function extractUserQuery(userBlock, maxLength = MAX_USER_TEXT_LENGTH) {
|
|
198
|
+
const chunks = [];
|
|
199
|
+
let cursor = 0;
|
|
200
|
+
while (cursor < userBlock.length) {
|
|
201
|
+
const openIndex = userBlock.indexOf(USER_QUERY_OPEN, cursor);
|
|
202
|
+
if (openIndex === -1)
|
|
203
|
+
break;
|
|
204
|
+
const start = openIndex + USER_QUERY_OPEN.length;
|
|
205
|
+
const closeIndex = userBlock.indexOf(USER_QUERY_CLOSE, start);
|
|
206
|
+
if (closeIndex === -1) {
|
|
207
|
+
chunks.push(userBlock.slice(start).trim());
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
chunks.push(userBlock.slice(start, closeIndex).trim());
|
|
211
|
+
cursor = closeIndex + USER_QUERY_CLOSE.length;
|
|
212
|
+
}
|
|
213
|
+
const combined = chunks.filter(Boolean).join(' ').replace(/\s+/g, ' ').trim();
|
|
214
|
+
return combined.slice(0, maxLength);
|
|
215
|
+
}
|
|
216
|
+
function parseJsonlTranscript(raw) {
|
|
217
|
+
const lines = raw.split(/\r?\n/).filter(l => l.trim());
|
|
218
|
+
if (lines.length === 0)
|
|
219
|
+
return { turns: [], recognized: false };
|
|
220
|
+
const turns = [];
|
|
221
|
+
let lastUserDisplay = '';
|
|
222
|
+
let lastUserFull = '';
|
|
223
|
+
let seenUser = false;
|
|
224
|
+
let userBilled = false;
|
|
225
|
+
let recognized = false;
|
|
226
|
+
let promptAt;
|
|
227
|
+
for (const line of lines) {
|
|
228
|
+
let entry;
|
|
229
|
+
try {
|
|
230
|
+
entry = JSON.parse(line);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
// A turn that errored or was aborted before any assistant message leaves
|
|
236
|
+
// only a `turn_ended` line (or a user line and nothing else); that is a
|
|
237
|
+
// Cursor transcript with nothing to bill, not an unknown format.
|
|
238
|
+
if (entry.type === 'turn_ended')
|
|
239
|
+
recognized = true;
|
|
240
|
+
if (entry.role === 'user') {
|
|
241
|
+
recognized = true;
|
|
242
|
+
const texts = normalizeContentBlocks(entry.message?.content)
|
|
243
|
+
.filter(c => c.type === 'text')
|
|
244
|
+
.map(c => c.text ?? '');
|
|
245
|
+
const combined = texts.join(' ');
|
|
246
|
+
const full = extractUserQuery(combined, Number.POSITIVE_INFINITY) || combined;
|
|
247
|
+
promptAt = parsePromptTimestamp(combined) ?? promptAt;
|
|
248
|
+
lastUserFull = full;
|
|
249
|
+
lastUserDisplay = full.slice(0, MAX_USER_TEXT_LENGTH);
|
|
250
|
+
seenUser = true;
|
|
251
|
+
userBilled = false;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (entry.role === 'assistant' && seenUser) {
|
|
255
|
+
const content = normalizeContentBlocks(entry.message?.content);
|
|
256
|
+
const bodyParts = [];
|
|
257
|
+
const tools = [];
|
|
258
|
+
for (const block of content) {
|
|
259
|
+
if (block.type === 'text' && block.text) {
|
|
260
|
+
bodyParts.push(block.text);
|
|
261
|
+
}
|
|
262
|
+
else if (block.type === 'tool_use' && block.name) {
|
|
263
|
+
tools.push(`cursor:${block.name.toLowerCase()}`);
|
|
264
|
+
if (block.input !== undefined) {
|
|
265
|
+
try {
|
|
266
|
+
bodyParts.push(JSON.stringify(block.input));
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
// Unserializable tool input contributes its name only (above).
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
turns.push({
|
|
275
|
+
userMessage: lastUserDisplay,
|
|
276
|
+
userTextFull: lastUserFull,
|
|
277
|
+
carried: userBilled,
|
|
278
|
+
assistant: {
|
|
279
|
+
body: bodyParts.join('\n').trim(),
|
|
280
|
+
reasoning: '',
|
|
281
|
+
tools,
|
|
282
|
+
},
|
|
283
|
+
...(promptAt ? { promptAt } : {}),
|
|
284
|
+
});
|
|
285
|
+
userBilled = true;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return { turns, recognized: recognized || turns.length > 0 };
|
|
289
|
+
}
|
|
290
|
+
function parseTranscript(raw) {
|
|
291
|
+
const lines = raw.split(/\r?\n/);
|
|
292
|
+
let recognized = false;
|
|
293
|
+
const pendingUsers = [];
|
|
294
|
+
let lastUserMessage = null;
|
|
295
|
+
const turns = [];
|
|
296
|
+
let active = 'none';
|
|
297
|
+
let userLines = [];
|
|
298
|
+
let assistantLines = [];
|
|
299
|
+
const flushUser = () => {
|
|
300
|
+
if (userLines.length === 0)
|
|
301
|
+
return;
|
|
302
|
+
const userQuery = extractUserQuery(userLines.join('\n'), Number.POSITIVE_INFINITY);
|
|
303
|
+
if (userQuery.length > 0)
|
|
304
|
+
pendingUsers.push(userQuery);
|
|
305
|
+
userLines = [];
|
|
306
|
+
};
|
|
307
|
+
const flushAssistant = () => {
|
|
308
|
+
if (assistantLines.length === 0)
|
|
309
|
+
return;
|
|
310
|
+
let output = '';
|
|
311
|
+
let reasoning = '';
|
|
312
|
+
const toolsByTurn = new Map();
|
|
313
|
+
for (const line of assistantLines) {
|
|
314
|
+
if (TOOL_RESULT_MARKER.test(line))
|
|
315
|
+
continue;
|
|
316
|
+
const thinkingMatch = line.match(THINKING_MARKER);
|
|
317
|
+
if (thinkingMatch) {
|
|
318
|
+
const body = line.replace(THINKING_MARKER, '').trim();
|
|
319
|
+
if (body.length > 0)
|
|
320
|
+
reasoning += `${body}\n`;
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
const toolMatch = line.match(TOOL_CALL_MARKER);
|
|
324
|
+
if (toolMatch) {
|
|
325
|
+
const parsedTool = parseToolName(toolMatch[1] ?? '');
|
|
326
|
+
const toolKey = `cursor:${parsedTool}`;
|
|
327
|
+
toolsByTurn.set(toolKey, true);
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
output += `${line}\n`;
|
|
331
|
+
}
|
|
332
|
+
const carried = pendingUsers.length === 0;
|
|
333
|
+
const userMessage = carried ? lastUserMessage : pendingUsers.shift();
|
|
334
|
+
if (userMessage !== null) {
|
|
335
|
+
lastUserMessage = userMessage;
|
|
336
|
+
const tools = Array.from(toolsByTurn.keys());
|
|
337
|
+
turns.push({
|
|
338
|
+
userMessage: userMessage.slice(0, MAX_USER_TEXT_LENGTH),
|
|
339
|
+
userTextFull: userMessage,
|
|
340
|
+
carried,
|
|
341
|
+
assistant: {
|
|
342
|
+
body: output.trim(),
|
|
343
|
+
reasoning: reasoning.trim(),
|
|
344
|
+
tools,
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
assistantLines = [];
|
|
349
|
+
};
|
|
350
|
+
for (const line of lines) {
|
|
351
|
+
if (USER_MARKER.test(line)) {
|
|
352
|
+
recognized = true;
|
|
353
|
+
if (active === 'user')
|
|
354
|
+
flushUser();
|
|
355
|
+
if (active === 'assistant')
|
|
356
|
+
flushAssistant();
|
|
357
|
+
active = 'user';
|
|
358
|
+
userLines = [line.replace(USER_MARKER, '')];
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (ASSISTANT_MARKER.test(line)) {
|
|
362
|
+
recognized = true;
|
|
363
|
+
if (active === 'user')
|
|
364
|
+
flushUser();
|
|
365
|
+
if (active === 'assistant')
|
|
366
|
+
flushAssistant();
|
|
367
|
+
active = 'assistant';
|
|
368
|
+
assistantLines = [line.replace(ASSISTANT_MARKER, '')];
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
if (active === 'user') {
|
|
372
|
+
userLines.push(line);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (active === 'assistant') {
|
|
376
|
+
assistantLines.push(line);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (active === 'user')
|
|
380
|
+
flushUser();
|
|
381
|
+
if (active === 'assistant')
|
|
382
|
+
flushAssistant();
|
|
383
|
+
return { turns, recognized };
|
|
384
|
+
}
|
|
385
|
+
function parsePromptTimestamp(text) {
|
|
386
|
+
const m = PROMPT_TIMESTAMP.exec(text);
|
|
387
|
+
if (!m)
|
|
388
|
+
return null;
|
|
389
|
+
const month = MONTHS.indexOf(m[1]);
|
|
390
|
+
if (month < 0)
|
|
391
|
+
return null;
|
|
392
|
+
const hour = (Number(m[4]) % 12) + (m[6] === 'PM' ? 12 : 0);
|
|
393
|
+
const sign = m[7]?.startsWith('-') ? -1 : 1;
|
|
394
|
+
const offsetMinutes = sign * (Math.abs(Number(m[7] ?? 0)) * 60 + Number(m[8] ?? 0));
|
|
395
|
+
const ms = Date.UTC(Number(m[3]), month, Number(m[2]), hour, Number(m[5])) - offsetMinutes * 60_000;
|
|
396
|
+
return Number.isNaN(ms) ? null : new Date(ms).toISOString();
|
|
397
|
+
}
|
|
398
|
+
function readVarint(buf, pos) {
|
|
399
|
+
let result = 0;
|
|
400
|
+
let scale = 1;
|
|
401
|
+
while (pos < buf.length) {
|
|
402
|
+
const byte = buf[pos++];
|
|
403
|
+
result += (byte & 0x7f) * scale;
|
|
404
|
+
if (byte < 0x80)
|
|
405
|
+
return [result, pos];
|
|
406
|
+
scale *= 128;
|
|
407
|
+
if (scale > 2 ** 63)
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
function readProtoFields(buf) {
|
|
413
|
+
const fields = [];
|
|
414
|
+
let pos = 0;
|
|
415
|
+
while (pos < buf.length) {
|
|
416
|
+
const key = readVarint(buf, pos);
|
|
417
|
+
if (!key)
|
|
418
|
+
return null;
|
|
419
|
+
pos = key[1];
|
|
420
|
+
const field = Math.floor(key[0] / 8);
|
|
421
|
+
const wire = key[0] % 8;
|
|
422
|
+
if (wire === 0) {
|
|
423
|
+
const v = readVarint(buf, pos);
|
|
424
|
+
if (!v)
|
|
425
|
+
return null;
|
|
426
|
+
fields.push({ field, value: v[0] });
|
|
427
|
+
pos = v[1];
|
|
428
|
+
}
|
|
429
|
+
else if (wire === 2) {
|
|
430
|
+
const len = readVarint(buf, pos);
|
|
431
|
+
if (!len || len[1] + len[0] > buf.length)
|
|
432
|
+
return null;
|
|
433
|
+
fields.push({ field, value: buf.subarray(len[1], len[1] + len[0]) });
|
|
434
|
+
pos = len[1] + len[0];
|
|
435
|
+
}
|
|
436
|
+
else if (wire === 1) {
|
|
437
|
+
pos += 8;
|
|
438
|
+
}
|
|
439
|
+
else if (wire === 5) {
|
|
440
|
+
pos += 4;
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return pos === buf.length ? fields : null;
|
|
447
|
+
}
|
|
448
|
+
function toBytes(value) {
|
|
449
|
+
if (value instanceof Uint8Array)
|
|
450
|
+
return value;
|
|
451
|
+
if (typeof value === 'string')
|
|
452
|
+
return Buffer.from(value, 'utf-8');
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
// Every conversation-state root still in the store, oldest first, merged into
|
|
456
|
+
// one message order. The latest root alone drops what Cursor summarized away
|
|
457
|
+
// on a long session; older roots still hold those messages.
|
|
458
|
+
function readStoreMessages(db) {
|
|
459
|
+
const rows = db.query('SELECT id, data FROM blobs ORDER BY rowid');
|
|
460
|
+
const jsonBlobs = new Map();
|
|
461
|
+
const others = [];
|
|
462
|
+
for (const row of rows) {
|
|
463
|
+
const data = toBytes(row.data);
|
|
464
|
+
if (!data || typeof row.id !== 'string')
|
|
465
|
+
continue;
|
|
466
|
+
if (data[0] === 0x7b)
|
|
467
|
+
jsonBlobs.set(row.id, data);
|
|
468
|
+
else
|
|
469
|
+
others.push(data);
|
|
470
|
+
}
|
|
471
|
+
const order = [];
|
|
472
|
+
const seen = new Set();
|
|
473
|
+
let workspaceUri = null;
|
|
474
|
+
for (const data of others) {
|
|
475
|
+
const fields = readProtoFields(data);
|
|
476
|
+
if (!fields || !fields.some(f => f.field === ROOT_TIMESTAMP_FIELD && typeof f.value === 'number'))
|
|
477
|
+
continue;
|
|
478
|
+
const ids = [];
|
|
479
|
+
for (const f of fields) {
|
|
480
|
+
if (f.field !== ROOT_MESSAGE_FIELD || typeof f.value === 'number' || f.value.length !== 32)
|
|
481
|
+
continue;
|
|
482
|
+
ids.push(Buffer.from(f.value).toString('hex'));
|
|
483
|
+
}
|
|
484
|
+
if (ids.length === 0 || !ids.every(id => jsonBlobs.has(id)))
|
|
485
|
+
continue;
|
|
486
|
+
for (const id of ids) {
|
|
487
|
+
if (seen.has(id))
|
|
488
|
+
continue;
|
|
489
|
+
seen.add(id);
|
|
490
|
+
order.push(id);
|
|
491
|
+
}
|
|
492
|
+
const ws = fields.find(f => f.field === ROOT_WORKSPACE_FIELD && typeof f.value !== 'number');
|
|
493
|
+
if (ws)
|
|
494
|
+
workspaceUri = blobToText(ws.value);
|
|
495
|
+
}
|
|
496
|
+
const messages = [];
|
|
497
|
+
for (const id of order) {
|
|
498
|
+
try {
|
|
499
|
+
messages.push(JSON.parse(blobToText(jsonBlobs.get(id))));
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
// A blob that is not a message contributes nothing.
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return { messages, workspaceUri };
|
|
506
|
+
}
|
|
507
|
+
function readStoreSession(dbPath, fallbackTimestamp) {
|
|
508
|
+
let db = null;
|
|
509
|
+
try {
|
|
510
|
+
db = openDatabase(dbPath);
|
|
511
|
+
const metaRow = db.query(`SELECT value FROM meta WHERE key = '0'`)[0];
|
|
512
|
+
if (!metaRow || typeof metaRow.value !== 'string' || !/^(?:[0-9a-f]{2})+$/i.test(metaRow.value))
|
|
513
|
+
return null;
|
|
514
|
+
const meta = JSON.parse(Buffer.from(metaRow.value, 'hex').toString('utf-8'));
|
|
515
|
+
const agentId = meta.agentId;
|
|
516
|
+
if (typeof agentId !== 'string' || agentId !== basename(dirname(dbPath)))
|
|
517
|
+
return null;
|
|
518
|
+
const { messages, workspaceUri } = readStoreMessages(db);
|
|
519
|
+
let timestamp = typeof meta.createdAt === 'number' ? normalizeTimestamp(meta.createdAt) ?? fallbackTimestamp : fallbackTimestamp;
|
|
520
|
+
let lastUserFull = '';
|
|
521
|
+
let userBilled = true;
|
|
522
|
+
const turns = [];
|
|
523
|
+
for (const message of messages) {
|
|
524
|
+
const blocks = (typeof message.content === 'string'
|
|
525
|
+
? [{ type: 'text', text: message.content }]
|
|
526
|
+
: Array.isArray(message.content) ? message.content : []);
|
|
527
|
+
if (message.role === 'user') {
|
|
528
|
+
const text = blocks.filter(b => b.type === 'text').map(b => b.text ?? '').join(' ');
|
|
529
|
+
const query = extractUserQuery(text, Number.POSITIVE_INFINITY);
|
|
530
|
+
// Cursor-injected context (<user_info>, conversation summaries) is
|
|
531
|
+
// not a prompt and never appears in the exported transcript.
|
|
532
|
+
if (!query)
|
|
533
|
+
continue;
|
|
534
|
+
lastUserFull = query;
|
|
535
|
+
userBilled = false;
|
|
536
|
+
timestamp = parsePromptTimestamp(text) ?? timestamp;
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
if (message.role !== 'assistant')
|
|
540
|
+
continue;
|
|
541
|
+
const bodyParts = [];
|
|
542
|
+
const reasoningParts = [];
|
|
543
|
+
const tools = [];
|
|
544
|
+
for (const block of blocks) {
|
|
545
|
+
if (block.type === 'text' && block.text) {
|
|
546
|
+
bodyParts.push(block.text);
|
|
547
|
+
}
|
|
548
|
+
else if (block.type === 'reasoning') {
|
|
549
|
+
if (block.text)
|
|
550
|
+
reasoningParts.push(block.text);
|
|
551
|
+
}
|
|
552
|
+
else if (block.type === 'tool-call' && block.toolName) {
|
|
553
|
+
tools.push(`cursor:${block.toolName.toLowerCase()}`);
|
|
554
|
+
if (block.args !== undefined) {
|
|
555
|
+
try {
|
|
556
|
+
bodyParts.push(JSON.stringify(block.args));
|
|
557
|
+
}
|
|
558
|
+
catch {
|
|
559
|
+
// Unserializable tool input contributes its name only (above).
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
const body = bodyParts.join('\n').trim();
|
|
565
|
+
const reasoning = reasoningParts.join('\n').trim();
|
|
566
|
+
// Empty placeholders (an aborted or errored step) never reach the transcript.
|
|
567
|
+
if (!body && !reasoning && tools.length === 0)
|
|
568
|
+
continue;
|
|
569
|
+
turns.push({
|
|
570
|
+
userMessage: lastUserFull.slice(0, MAX_USER_TEXT_LENGTH),
|
|
571
|
+
userTextFull: lastUserFull,
|
|
572
|
+
carried: userBilled,
|
|
573
|
+
assistant: { body, reasoning, tools },
|
|
574
|
+
timestamp,
|
|
575
|
+
});
|
|
576
|
+
userBilled = true;
|
|
577
|
+
}
|
|
578
|
+
let workspacePath = null;
|
|
579
|
+
if (workspaceUri?.startsWith('file://')) {
|
|
580
|
+
try {
|
|
581
|
+
// Not fileURLToPath: on Windows it rejects a drive-less file URI, and
|
|
582
|
+
// only the dash-joined project name is needed here.
|
|
583
|
+
workspacePath = decodeURIComponent(new URL(workspaceUri).pathname);
|
|
584
|
+
}
|
|
585
|
+
catch {
|
|
586
|
+
workspacePath = null;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return { agentId, workspacePath, turns };
|
|
590
|
+
}
|
|
591
|
+
finally {
|
|
592
|
+
db?.close();
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
function createStoreParser(source, seenKeys) {
|
|
596
|
+
return {
|
|
597
|
+
async *parse() {
|
|
598
|
+
const fallbackTimestamp = (await stat(source.path)).mtime.toISOString();
|
|
599
|
+
let session;
|
|
600
|
+
try {
|
|
601
|
+
session = readStoreSession(source.path, fallbackTimestamp);
|
|
602
|
+
}
|
|
603
|
+
catch {
|
|
604
|
+
session = null;
|
|
605
|
+
}
|
|
606
|
+
if (!session) {
|
|
607
|
+
if (!warnedUnrecognizedTranscripts.has(source.path)) {
|
|
608
|
+
warnedUnrecognizedTranscripts.add(source.path);
|
|
609
|
+
process.stderr.write(`codeburn: skipped ${basename(dirname(source.path))}/${STORE_DB_NAME}: unrecognized cursor-agent store\n`);
|
|
610
|
+
}
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
// Same derivation as the dash-joined projects/ directory name transcripts use.
|
|
614
|
+
const project = session.workspacePath
|
|
615
|
+
? prettifyProjectId(session.workspacePath.replace(/[^A-Za-z0-9]+/g, '-'))
|
|
616
|
+
: undefined;
|
|
617
|
+
for (let turnIndex = 0; turnIndex < session.turns.length; turnIndex++) {
|
|
618
|
+
const turn = session.turns[turnIndex];
|
|
619
|
+
const deduplicationKey = `cursor-agent:${session.agentId}:${turnIndex}`;
|
|
620
|
+
if (seenKeys.has(deduplicationKey))
|
|
621
|
+
continue;
|
|
622
|
+
seenKeys.add(deduplicationKey);
|
|
623
|
+
// Priced like transcripts, so a session keeps one price whichever file
|
|
624
|
+
// it is read from (Cursor writes the transcript when the session ends).
|
|
625
|
+
const model = 'cursor-agent-auto';
|
|
626
|
+
const inputTokens = turn.carried ? 0 : estimateTokens(turn.userTextFull.length);
|
|
627
|
+
const outputTokens = estimateTokens(turn.assistant.body.length);
|
|
628
|
+
const reasoningTokens = estimateTokens(turn.assistant.reasoning.length);
|
|
629
|
+
yield {
|
|
630
|
+
provider: 'cursor-agent',
|
|
631
|
+
model,
|
|
632
|
+
inputTokens,
|
|
633
|
+
outputTokens,
|
|
634
|
+
cacheCreationInputTokens: 0,
|
|
635
|
+
cacheReadInputTokens: 0,
|
|
636
|
+
cachedInputTokens: 0,
|
|
637
|
+
reasoningTokens,
|
|
638
|
+
webSearchRequests: 0,
|
|
639
|
+
costUSD: calculateCost(costModel(model), inputTokens, outputTokens + reasoningTokens, 0, 0, 0),
|
|
640
|
+
costIsEstimated: true,
|
|
641
|
+
tools: turn.assistant.tools,
|
|
642
|
+
bashCommands: [],
|
|
643
|
+
timestamp: turn.timestamp,
|
|
644
|
+
speed: 'standard',
|
|
645
|
+
deduplicationKey,
|
|
646
|
+
userMessage: turn.userMessage,
|
|
647
|
+
sessionId: session.agentId,
|
|
648
|
+
...(project ? { project } : {}),
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
// Store-only sessions: a session whose transcript was exported is read from the
|
|
655
|
+
// transcript alone, so the two can never both count.
|
|
656
|
+
async function appendStoreSources(chatsDir, transcriptIds, sources) {
|
|
657
|
+
const hashDirs = await readdir(chatsDir, { withFileTypes: true }).catch(() => []);
|
|
658
|
+
for (const hashDir of hashDirs) {
|
|
659
|
+
if (!hashDir.isDirectory())
|
|
660
|
+
continue;
|
|
661
|
+
const agentDirs = await readdir(join(chatsDir, hashDir.name), { withFileTypes: true }).catch(() => []);
|
|
662
|
+
for (const agentDir of agentDirs) {
|
|
663
|
+
if (!agentDir.isDirectory() || !UUID_LIKE.test(agentDir.name) || transcriptIds.has(agentDir.name))
|
|
664
|
+
continue;
|
|
665
|
+
const path = join(chatsDir, hashDir.name, agentDir.name, STORE_DB_NAME);
|
|
666
|
+
if (!existsSync(path))
|
|
667
|
+
continue;
|
|
668
|
+
sources.push({ path, project: 'cursor-agent', provider: 'cursor-agent' });
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
// Cursor IDE agent-mode chats also write agent transcripts, but the `cursor`
|
|
673
|
+
// provider already counts them from the IDE's own record (composerData plus
|
|
674
|
+
// per-bubble rows), which carries the model, per-turn times and Cursor's
|
|
675
|
+
// context meter. Their ids match, so those transcripts are left to it.
|
|
676
|
+
function loadIdeComposerIds(ideDbPath) {
|
|
677
|
+
const ids = new Set();
|
|
678
|
+
if (!existsSync(ideDbPath) || !isSqliteAvailable())
|
|
679
|
+
return ids;
|
|
680
|
+
let db = null;
|
|
681
|
+
try {
|
|
682
|
+
db = openDatabase(ideDbPath);
|
|
683
|
+
const rows = db.query("SELECT substr(key, length('composerData:') + 1) AS id FROM cursorDiskKV WHERE key >= 'composerData:' AND key < 'composerData;'");
|
|
684
|
+
for (const row of rows)
|
|
685
|
+
ids.add(row.id);
|
|
686
|
+
}
|
|
687
|
+
catch (err) {
|
|
688
|
+
if (isSqliteBusyError(err))
|
|
689
|
+
throw err;
|
|
690
|
+
}
|
|
691
|
+
finally {
|
|
692
|
+
db?.close();
|
|
693
|
+
}
|
|
694
|
+
return ids;
|
|
695
|
+
}
|
|
696
|
+
function sourceSessionId(source) {
|
|
697
|
+
return basename(source.path) === STORE_DB_NAME ? basename(dirname(source.path)) : toConversationId(source.path);
|
|
698
|
+
}
|
|
699
|
+
function createParser(source, seenKeys, dbPath, summariesByConversationId) {
|
|
700
|
+
return {
|
|
701
|
+
async *parse() {
|
|
702
|
+
const conversationId = toConversationId(source.path);
|
|
703
|
+
let summary = summariesByConversationId.get(conversationId);
|
|
704
|
+
let db = null;
|
|
705
|
+
try {
|
|
706
|
+
if (!summary) {
|
|
707
|
+
if (existsSync(dbPath)) {
|
|
708
|
+
try {
|
|
709
|
+
db = openDatabase(dbPath);
|
|
710
|
+
const rows = db.query(CONVERSATION_SUMMARY_QUERY, [conversationId]);
|
|
711
|
+
if (rows.length > 0) {
|
|
712
|
+
const row = rows[0];
|
|
713
|
+
summary = {
|
|
714
|
+
conversationId: row.conversationId,
|
|
715
|
+
model: row.model,
|
|
716
|
+
title: row.title,
|
|
717
|
+
updatedAt: normalizeTimestamp(row.updatedAt),
|
|
718
|
+
};
|
|
719
|
+
summariesByConversationId.set(conversationId, summary);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
catch {
|
|
723
|
+
summary = undefined;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
const transcript = await readFile(source.path, 'utf-8');
|
|
728
|
+
const isJsonl = source.path.endsWith('.jsonl');
|
|
729
|
+
const parsed = isJsonl ? parseJsonlTranscript(transcript) : parseTranscript(transcript);
|
|
730
|
+
if (!parsed.recognized) {
|
|
731
|
+
if (!warnedUnrecognizedTranscripts.has(source.path)) {
|
|
732
|
+
warnedUnrecognizedTranscripts.add(source.path);
|
|
733
|
+
process.stderr.write(`codeburn: skipped ${basename(source.path)}: unrecognized cursor-agent transcript format\n`);
|
|
734
|
+
}
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
let timestamp = summary?.updatedAt ?? null;
|
|
738
|
+
if (!timestamp) {
|
|
739
|
+
const fileStat = await stat(source.path);
|
|
740
|
+
timestamp = fileStat.mtime.toISOString();
|
|
741
|
+
}
|
|
742
|
+
const model = resolveModel(summary?.model ?? null);
|
|
743
|
+
for (let turnIndex = 0; turnIndex < parsed.turns.length; turnIndex++) {
|
|
744
|
+
const turn = parsed.turns[turnIndex];
|
|
745
|
+
const inputTokens = turn.carried ? 0 : estimateTokens(turn.userTextFull.length);
|
|
746
|
+
const outputTokens = estimateTokens(turn.assistant.body.length);
|
|
747
|
+
const reasoningTokens = estimateTokens(turn.assistant.reasoning.length);
|
|
748
|
+
const deduplicationKey = `cursor-agent:${conversationId}:${turnIndex}`;
|
|
749
|
+
if (seenKeys.has(deduplicationKey))
|
|
750
|
+
continue;
|
|
751
|
+
seenKeys.add(deduplicationKey);
|
|
752
|
+
const costUSD = calculateCost(costModel(model), inputTokens, outputTokens + reasoningTokens, 0, 0, 0);
|
|
753
|
+
yield {
|
|
754
|
+
provider: 'cursor-agent',
|
|
755
|
+
model,
|
|
756
|
+
inputTokens,
|
|
757
|
+
outputTokens,
|
|
758
|
+
cacheCreationInputTokens: 0,
|
|
759
|
+
cacheReadInputTokens: 0,
|
|
760
|
+
cachedInputTokens: 0,
|
|
761
|
+
reasoningTokens,
|
|
762
|
+
webSearchRequests: 0,
|
|
763
|
+
costUSD,
|
|
764
|
+
costIsEstimated: true,
|
|
765
|
+
tools: turn.assistant.tools,
|
|
766
|
+
bashCommands: [],
|
|
767
|
+
// Not the session's last write: that lands work Cursor already
|
|
768
|
+
// billed after a synced export's newest event, counting it twice.
|
|
769
|
+
timestamp: turn.promptAt ?? timestamp,
|
|
770
|
+
speed: 'standard',
|
|
771
|
+
deduplicationKey,
|
|
772
|
+
userMessage: turn.userMessage,
|
|
773
|
+
sessionId: conversationId,
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
finally {
|
|
778
|
+
db?.close();
|
|
779
|
+
}
|
|
780
|
+
},
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
export function createCursorAgentProvider(baseDirOverride, ideDbPathOverride) {
|
|
784
|
+
const baseDir = getCursorAgentBaseDir(baseDirOverride);
|
|
785
|
+
const projectsDir = getProjectsDir(baseDir);
|
|
786
|
+
const chatsDir = getChatsDir(baseDir);
|
|
787
|
+
const dbPath = getAttributionDbPath(baseDir);
|
|
788
|
+
const summariesByConversationId = new Map();
|
|
789
|
+
return {
|
|
790
|
+
name: 'cursor-agent',
|
|
791
|
+
displayName: 'Cursor Agent',
|
|
792
|
+
modelDisplayName(model) {
|
|
793
|
+
if (model === 'cursor-agent-auto')
|
|
794
|
+
return 'Cursor (auto)';
|
|
795
|
+
const label = ownValue(modelDisplayNames, model) ?? getShortModelName(model);
|
|
796
|
+
return `${label} (est.)`;
|
|
797
|
+
},
|
|
798
|
+
toolDisplayName(rawTool) {
|
|
799
|
+
return rawTool;
|
|
800
|
+
},
|
|
801
|
+
async probeRoots() {
|
|
802
|
+
return [
|
|
803
|
+
{ path: projectsDir, label: 'projects' },
|
|
804
|
+
{ path: chatsDir, label: 'chats' },
|
|
805
|
+
{ path: dbPath, label: 'db' },
|
|
806
|
+
];
|
|
807
|
+
},
|
|
808
|
+
async discoverSessions() {
|
|
809
|
+
const projectEntries = existsSync(projectsDir) ? await readdir(projectsDir, { withFileTypes: true }) : [];
|
|
810
|
+
const sources = [];
|
|
811
|
+
for (const entry of projectEntries) {
|
|
812
|
+
if (!entry.isDirectory())
|
|
813
|
+
continue;
|
|
814
|
+
const projectId = prettifyProjectId(entry.name);
|
|
815
|
+
const projectDir = join(projectsDir, entry.name);
|
|
816
|
+
if (entry.name === 'agent-transcripts') {
|
|
817
|
+
await appendTranscriptSources(projectDir, projectId, sources);
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
const transcriptDir = join(projectDir, 'agent-transcripts');
|
|
821
|
+
if (!existsSync(transcriptDir))
|
|
822
|
+
continue;
|
|
823
|
+
await appendTranscriptSources(transcriptDir, projectId, sources);
|
|
824
|
+
}
|
|
825
|
+
const transcriptIds = new Set(sources.map(s => toConversationId(s.path)));
|
|
826
|
+
await appendStoreSources(chatsDir, transcriptIds, sources);
|
|
827
|
+
const ideIds = loadIdeComposerIds(ideDbPathOverride ?? getCursorDbPath());
|
|
828
|
+
return ideIds.size === 0 ? sources : sources.filter(s => !ideIds.has(sourceSessionId(s)));
|
|
829
|
+
},
|
|
830
|
+
createSessionParser(source, seenKeys) {
|
|
831
|
+
if (basename(source.path) === STORE_DB_NAME)
|
|
832
|
+
return createStoreParser(source, seenKeys);
|
|
833
|
+
return createParser(source, seenKeys, dbPath, summariesByConversationId);
|
|
834
|
+
},
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
export const cursor_agent = createCursorAgentProvider();
|