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,135 @@
|
|
|
1
|
+
// @ts-nocheck: codeburn type-checks this under its own tsconfig; Chompyy adds noUncheckedIndexedAccess (see UPSTREAM.md)
|
|
2
|
+
import { getShortModelName } from '../models.js';
|
|
3
|
+
import { RetryableSourceError } from './types.js';
|
|
4
|
+
import { fetchWithTimeout } from '../fetch-utils.js';
|
|
5
|
+
// The report is a DAILY AGGREGATE per model: no request ids, no timestamps, no
|
|
6
|
+
// attribution. Nothing in a row can be matched against the local tools that
|
|
7
|
+
// were pointed at the gateway (Claude Code via ANTHROPIC_BASE_URL, Codex,
|
|
8
|
+
// OpenCode, Cline/Kilo, Cursor), which record the same requests
|
|
9
|
+
// themselves. Gateway spend is therefore shown as its own row and kept out of
|
|
10
|
+
// headline totals unless `includeGatewayInTotals` is set — see
|
|
11
|
+
// `excludeProviderFromDay` in src/usage-aggregator.ts.
|
|
12
|
+
const REPORT_URL = 'https://ai-gateway.vercel.sh/v1/report';
|
|
13
|
+
const RETRYABLE_STATUSES = new Set([408, 409, 425, 429]);
|
|
14
|
+
export function getVercelGatewayApiKey() {
|
|
15
|
+
const key = process.env['AI_GATEWAY_API_KEY'] ?? process.env['VERCEL_OIDC_TOKEN'];
|
|
16
|
+
return key?.trim() ? key.trim() : null;
|
|
17
|
+
}
|
|
18
|
+
function formatUtcDate(d) {
|
|
19
|
+
const y = d.getUTCFullYear();
|
|
20
|
+
const m = String(d.getUTCMonth() + 1).padStart(2, '0');
|
|
21
|
+
const day = String(d.getUTCDate()).padStart(2, '0');
|
|
22
|
+
return `${y}-${m}-${day}`;
|
|
23
|
+
}
|
|
24
|
+
export async function fetchVercelGatewayReport(dateRange) {
|
|
25
|
+
const key = getVercelGatewayApiKey();
|
|
26
|
+
if (!key)
|
|
27
|
+
return [];
|
|
28
|
+
const params = new URLSearchParams({
|
|
29
|
+
start_date: formatUtcDate(dateRange.start),
|
|
30
|
+
end_date: formatUtcDate(dateRange.end),
|
|
31
|
+
date_part: 'day',
|
|
32
|
+
group_by: 'model',
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
const res = await fetchWithTimeout(`${REPORT_URL}?${params}`, {
|
|
36
|
+
method: 'GET',
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${key}`,
|
|
39
|
+
Accept: 'application/json',
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
if (!res.ok) {
|
|
43
|
+
const detail = await res.text().catch(() => '');
|
|
44
|
+
process.stderr.write(`codeburn: Vercel AI Gateway report failed (HTTP ${res.status}). ` +
|
|
45
|
+
'Requires AI_GATEWAY_API_KEY or VERCEL_OIDC_TOKEN (Pro/Enterprise for /v1/report). ' +
|
|
46
|
+
`${detail.slice(0, 200)}\n`);
|
|
47
|
+
// A timeout, conflict, rate limit or server error says nothing about the
|
|
48
|
+
// spend: keep the cached report and retry. 408/409/429/5xx are the
|
|
49
|
+
// statuses Vercel's own AI SDK retries; 425 asks for a retry by
|
|
50
|
+
// definition. Other statuses (no access) mean no report.
|
|
51
|
+
if (RETRYABLE_STATUSES.has(res.status) || res.status >= 500)
|
|
52
|
+
throw new RetryableSourceError(`Vercel AI Gateway report HTTP ${res.status}`);
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const body = (await res.json());
|
|
56
|
+
return body.results ?? [];
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
if (err instanceof RetryableSourceError)
|
|
60
|
+
throw err;
|
|
61
|
+
process.stderr.write(`codeburn: Vercel AI Gateway report unreachable (${err instanceof Error ? err.message : String(err)}).\n`);
|
|
62
|
+
throw new RetryableSourceError('Vercel AI Gateway report unreachable', { cause: err });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function createParser(source, seenKeys, dateRange) {
|
|
66
|
+
return {
|
|
67
|
+
async *parse() {
|
|
68
|
+
if (!dateRange)
|
|
69
|
+
return;
|
|
70
|
+
const rows = await fetchVercelGatewayReport(dateRange);
|
|
71
|
+
for (const row of rows) {
|
|
72
|
+
const day = row.day ?? '';
|
|
73
|
+
const model = row.model ?? 'unknown';
|
|
74
|
+
const costUSD = row.total_cost ?? 0;
|
|
75
|
+
const inputTokens = row.input_tokens ?? 0;
|
|
76
|
+
const outputTokens = row.output_tokens ?? 0;
|
|
77
|
+
if (costUSD === 0 && inputTokens === 0 && outputTokens === 0)
|
|
78
|
+
continue;
|
|
79
|
+
const deduplicationKey = `vercel-gateway:${day}:${model}`;
|
|
80
|
+
if (seenKeys.has(deduplicationKey))
|
|
81
|
+
continue;
|
|
82
|
+
seenKeys.add(deduplicationKey);
|
|
83
|
+
// One row is a whole day of requests for one model, so it stands for
|
|
84
|
+
// `request_count` of them. Cost and tokens stay whole on this single
|
|
85
|
+
// call; only request counters read the count (behavioralCallWeight).
|
|
86
|
+
const requestCount = row.request_count;
|
|
87
|
+
yield {
|
|
88
|
+
provider: 'vercel-gateway',
|
|
89
|
+
model,
|
|
90
|
+
...(typeof requestCount === 'number' && requestCount > 1 ? { requestCount } : {}),
|
|
91
|
+
inputTokens,
|
|
92
|
+
outputTokens,
|
|
93
|
+
cacheCreationInputTokens: row.cache_creation_input_tokens ?? 0,
|
|
94
|
+
cacheReadInputTokens: row.cached_input_tokens ?? 0,
|
|
95
|
+
cachedInputTokens: 0,
|
|
96
|
+
reasoningTokens: row.reasoning_tokens ?? 0,
|
|
97
|
+
webSearchRequests: 0,
|
|
98
|
+
costUSD,
|
|
99
|
+
tools: [],
|
|
100
|
+
bashCommands: [],
|
|
101
|
+
timestamp: day ? `${day}T12:00:00.000Z` : '',
|
|
102
|
+
speed: 'standard',
|
|
103
|
+
deduplicationKey,
|
|
104
|
+
userMessage: '',
|
|
105
|
+
sessionId: `${day}:${model}`,
|
|
106
|
+
project: source.project,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export const vercelGateway = {
|
|
113
|
+
name: 'vercel-gateway',
|
|
114
|
+
displayName: 'Vercel AI Gateway',
|
|
115
|
+
network: true,
|
|
116
|
+
modelDisplayName(model) {
|
|
117
|
+
const slash = model.indexOf('/');
|
|
118
|
+
return getShortModelName(slash >= 0 ? model.slice(slash + 1) : model);
|
|
119
|
+
},
|
|
120
|
+
toolDisplayName(rawTool) {
|
|
121
|
+
return rawTool;
|
|
122
|
+
},
|
|
123
|
+
async discoverSessions() {
|
|
124
|
+
if (!getVercelGatewayApiKey())
|
|
125
|
+
return [];
|
|
126
|
+
return [{
|
|
127
|
+
path: 'vercel-ai-gateway:report',
|
|
128
|
+
project: 'Vercel AI Gateway',
|
|
129
|
+
provider: 'vercel-gateway',
|
|
130
|
+
}];
|
|
131
|
+
},
|
|
132
|
+
createSessionParser(source, seenKeys, dateRange) {
|
|
133
|
+
return createParser(source, seenKeys, dateRange);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
@@ -0,0 +1,212 @@
|
|
|
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, dirname, join, posix, win32 } from 'path';
|
|
4
|
+
import { homedir } from 'os';
|
|
5
|
+
import { calculateCost } from '../models.js';
|
|
6
|
+
export function getVSCodeGlobalStoragePaths(extensionId, homeDir = homedir(), platform = process.platform) {
|
|
7
|
+
const pathJoin = platform === 'win32' ? win32.join : posix.join;
|
|
8
|
+
if (platform === 'darwin') {
|
|
9
|
+
return [
|
|
10
|
+
pathJoin(homeDir, 'Library', 'Application Support', 'Code', 'User', 'globalStorage', extensionId),
|
|
11
|
+
pathJoin(homeDir, 'Library', 'Application Support', 'Code - Insiders', 'User', 'globalStorage', extensionId),
|
|
12
|
+
pathJoin(homeDir, 'Library', 'Application Support', 'VSCodium', 'User', 'globalStorage', extensionId),
|
|
13
|
+
];
|
|
14
|
+
}
|
|
15
|
+
if (platform === 'win32') {
|
|
16
|
+
return [
|
|
17
|
+
pathJoin(homeDir, 'AppData', 'Roaming', 'Code', 'User', 'globalStorage', extensionId),
|
|
18
|
+
pathJoin(homeDir, 'AppData', 'Roaming', 'Code - Insiders', 'User', 'globalStorage', extensionId),
|
|
19
|
+
pathJoin(homeDir, 'AppData', 'Roaming', 'VSCodium', 'User', 'globalStorage', extensionId),
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
return [
|
|
23
|
+
pathJoin(homeDir, '.config', 'Code', 'User', 'globalStorage', extensionId),
|
|
24
|
+
pathJoin(homeDir, '.config', 'Code - Insiders', 'User', 'globalStorage', extensionId),
|
|
25
|
+
pathJoin(homeDir, '.config', 'VSCodium', 'User', 'globalStorage', extensionId),
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
export function getVSCodeGlobalStoragePath(extensionId) {
|
|
29
|
+
return getVSCodeGlobalStoragePaths(extensionId)[0];
|
|
30
|
+
}
|
|
31
|
+
// The roots discoverClineTasks scans: an explicit override wins, otherwise
|
|
32
|
+
// every VS Code variant's globalStorage. Exported so a provider's probeRoots()
|
|
33
|
+
// can report exactly what discovery reads by calling the same function, rather
|
|
34
|
+
// than mirroring this logic and drifting from it.
|
|
35
|
+
export function clineTaskRoots(extensionId, overrideDir) {
|
|
36
|
+
return overrideDir
|
|
37
|
+
? (Array.isArray(overrideDir) ? overrideDir : [overrideDir])
|
|
38
|
+
: getVSCodeGlobalStoragePaths(extensionId);
|
|
39
|
+
}
|
|
40
|
+
export async function discoverClineTasks(extensionId, providerName, displayName, overrideDir) {
|
|
41
|
+
return discoverClineTasksInBaseDirs(clineTaskRoots(extensionId, overrideDir), providerName, displayName);
|
|
42
|
+
}
|
|
43
|
+
export async function discoverClineTasksInBaseDirs(baseDirs, providerName, displayName) {
|
|
44
|
+
const sources = [];
|
|
45
|
+
const seen = new Set();
|
|
46
|
+
for (const baseDir of baseDirs) {
|
|
47
|
+
for (const source of await discoverClineTasksInBaseDir(baseDir, providerName, displayName)) {
|
|
48
|
+
if (seen.has(source.path))
|
|
49
|
+
continue;
|
|
50
|
+
seen.add(source.path);
|
|
51
|
+
sources.push(source);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return sources;
|
|
55
|
+
}
|
|
56
|
+
async function discoverClineTasksInBaseDir(baseDir, providerName, displayName) {
|
|
57
|
+
const tasksDir = join(baseDir, 'tasks');
|
|
58
|
+
const sources = [];
|
|
59
|
+
let taskDirs;
|
|
60
|
+
try {
|
|
61
|
+
taskDirs = await readdir(tasksDir);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return sources;
|
|
65
|
+
}
|
|
66
|
+
for (const taskId of taskDirs) {
|
|
67
|
+
const taskDir = join(tasksDir, taskId);
|
|
68
|
+
const dirStat = await stat(taskDir).catch(() => null);
|
|
69
|
+
if (!dirStat?.isDirectory())
|
|
70
|
+
continue;
|
|
71
|
+
const uiPath = join(taskDir, 'ui_messages.json');
|
|
72
|
+
const uiStat = await stat(uiPath).catch(() => null);
|
|
73
|
+
if (!uiStat?.isFile())
|
|
74
|
+
continue;
|
|
75
|
+
// The file, not the task directory: Cline rewrites ui_messages.json in
|
|
76
|
+
// place, which never moves the directory's own mtime or size.
|
|
77
|
+
sources.push({ path: uiPath, project: displayName, provider: providerName });
|
|
78
|
+
}
|
|
79
|
+
return sources;
|
|
80
|
+
}
|
|
81
|
+
const MODEL_TAG_RE = /<model>([^<]+)<\/model>/;
|
|
82
|
+
const WORKSPACE_DIR_RE = /Current Workspace Directory \(([^)]+)\)/;
|
|
83
|
+
function extractHistoryMeta(taskDir, fallbackModel) {
|
|
84
|
+
return readFile(join(taskDir, 'api_conversation_history.json'), 'utf-8')
|
|
85
|
+
.then(raw => {
|
|
86
|
+
const msgs = JSON.parse(raw);
|
|
87
|
+
if (!Array.isArray(msgs))
|
|
88
|
+
return { model: fallbackModel, workspace: null };
|
|
89
|
+
let model = null;
|
|
90
|
+
let workspace = null;
|
|
91
|
+
for (const msg of msgs) {
|
|
92
|
+
if (msg.role !== 'user' || !Array.isArray(msg.content))
|
|
93
|
+
continue;
|
|
94
|
+
for (const block of msg.content) {
|
|
95
|
+
if (typeof block.text !== 'string')
|
|
96
|
+
continue;
|
|
97
|
+
if (!model) {
|
|
98
|
+
const mm = MODEL_TAG_RE.exec(block.text);
|
|
99
|
+
if (mm)
|
|
100
|
+
model = mm[1].includes('/') ? mm[1].split('/').pop() : mm[1];
|
|
101
|
+
}
|
|
102
|
+
if (!workspace) {
|
|
103
|
+
const wm = WORKSPACE_DIR_RE.exec(block.text);
|
|
104
|
+
if (wm)
|
|
105
|
+
workspace = wm[1];
|
|
106
|
+
}
|
|
107
|
+
if (model && workspace)
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
if (model && workspace)
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
return { model: model ?? fallbackModel, workspace };
|
|
114
|
+
})
|
|
115
|
+
.catch(() => ({ model: fallbackModel, workspace: null }));
|
|
116
|
+
}
|
|
117
|
+
function workspaceToProject(workspace) {
|
|
118
|
+
return basename(workspace) || workspace;
|
|
119
|
+
}
|
|
120
|
+
export function createClineParser(source, seenKeys, providerName, fallbackModel = 'cline-auto') {
|
|
121
|
+
return {
|
|
122
|
+
async *parse() {
|
|
123
|
+
const taskDir = dirname(source.path);
|
|
124
|
+
const taskId = basename(taskDir);
|
|
125
|
+
let uiRaw;
|
|
126
|
+
try {
|
|
127
|
+
uiRaw = await readFile(source.path, 'utf-8');
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let uiMessages;
|
|
133
|
+
try {
|
|
134
|
+
uiMessages = JSON.parse(uiRaw);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (!Array.isArray(uiMessages))
|
|
140
|
+
return;
|
|
141
|
+
const meta = await extractHistoryMeta(taskDir, fallbackModel);
|
|
142
|
+
const model = meta.model;
|
|
143
|
+
const project = meta.workspace ? workspaceToProject(meta.workspace) : undefined;
|
|
144
|
+
const projectPath = meta.workspace ?? undefined;
|
|
145
|
+
let userMessage = '';
|
|
146
|
+
for (const msg of uiMessages) {
|
|
147
|
+
if (msg.type === 'say' && (msg.say === 'user_feedback' || msg.say === 'text')) {
|
|
148
|
+
userMessage = (msg.text ?? '').slice(0, 500);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const apiReqEntries = uiMessages.filter(m => m.type === 'say' && m.say === 'api_req_started');
|
|
153
|
+
for (const [index, entry] of apiReqEntries.entries()) {
|
|
154
|
+
const dedupKey = `${providerName}:${taskId}:${index}`;
|
|
155
|
+
if (seenKeys.has(dedupKey))
|
|
156
|
+
continue;
|
|
157
|
+
seenKeys.add(dedupKey);
|
|
158
|
+
let tokensIn = 0;
|
|
159
|
+
let tokensOut = 0;
|
|
160
|
+
let cacheReads = 0;
|
|
161
|
+
let cacheWrites = 0;
|
|
162
|
+
let cost;
|
|
163
|
+
if (entry.text) {
|
|
164
|
+
try {
|
|
165
|
+
const parsed = JSON.parse(entry.text);
|
|
166
|
+
tokensIn = parsed.tokensIn ?? 0;
|
|
167
|
+
tokensOut = parsed.tokensOut ?? 0;
|
|
168
|
+
cacheReads = parsed.cacheReads ?? 0;
|
|
169
|
+
cacheWrites = parsed.cacheWrites ?? 0;
|
|
170
|
+
cost = parsed.cost;
|
|
171
|
+
}
|
|
172
|
+
catch { }
|
|
173
|
+
}
|
|
174
|
+
if (tokensIn === 0 && tokensOut === 0)
|
|
175
|
+
continue;
|
|
176
|
+
// entry.ts is truthy-checked but not validity-checked: a malformed
|
|
177
|
+
// ts (garbage string, out-of-range number) makes new Date().toISOString()
|
|
178
|
+
// throw RangeError, which would abort the whole session's parse. Guard it.
|
|
179
|
+
const tsDate = entry.ts ? new Date(entry.ts) : null;
|
|
180
|
+
const timestamp = tsDate && !Number.isNaN(tsDate.getTime()) ? tsDate.toISOString() : '';
|
|
181
|
+
// Cline's `cost` is what the request was billed on its real route
|
|
182
|
+
// (cache writes and gateway prices included, 0 for a free model); a
|
|
183
|
+
// token re-price would replace it with a list price for a model the
|
|
184
|
+
// task may not even name. Only a missing cost is priced from tokens.
|
|
185
|
+
const isReported = typeof cost === 'number' && Number.isFinite(cost) && cost >= 0;
|
|
186
|
+
const costUSD = isReported ? cost : calculateCost(model, tokensIn, tokensOut, cacheWrites, cacheReads, 0);
|
|
187
|
+
yield {
|
|
188
|
+
provider: providerName,
|
|
189
|
+
model,
|
|
190
|
+
inputTokens: tokensIn,
|
|
191
|
+
outputTokens: tokensOut,
|
|
192
|
+
cacheCreationInputTokens: cacheWrites,
|
|
193
|
+
cacheReadInputTokens: cacheReads,
|
|
194
|
+
cachedInputTokens: cacheReads,
|
|
195
|
+
reasoningTokens: 0,
|
|
196
|
+
webSearchRequests: 0,
|
|
197
|
+
costUSD,
|
|
198
|
+
...(isReported ? { costFromBilling: true } : {}),
|
|
199
|
+
tools: [],
|
|
200
|
+
bashCommands: [],
|
|
201
|
+
timestamp,
|
|
202
|
+
speed: 'standard',
|
|
203
|
+
deduplicationKey: dedupKey,
|
|
204
|
+
userMessage: index === 0 ? userMessage : '',
|
|
205
|
+
sessionId: taskId,
|
|
206
|
+
project,
|
|
207
|
+
projectPath,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|