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.
Files changed (154) hide show
  1. package/README.md +11 -0
  2. package/dist/agentlimits.js +77 -0
  3. package/dist/agentusage.js +96 -0
  4. package/dist/appstate.js +63 -3
  5. package/dist/away.js +44 -5
  6. package/dist/blueprint.js +3 -7
  7. package/dist/board.js +1 -0
  8. package/dist/cli.js +263 -5
  9. package/dist/codexquota.js +106 -0
  10. package/dist/codexusage.js +10 -0
  11. package/dist/config.js +2 -0
  12. package/dist/context.js +30 -0
  13. package/dist/costs.js +1 -0
  14. package/dist/demon.js +20 -0
  15. package/dist/label.js +33 -0
  16. package/dist/menu.js +17 -0
  17. package/dist/nightcap.js +60 -0
  18. package/dist/nudge.js +47 -18
  19. package/dist/pacing.js +16 -1
  20. package/dist/questions.js +71 -0
  21. package/dist/recap.js +74 -0
  22. package/dist/receipt.js +48 -0
  23. package/dist/recipe.js +79 -0
  24. package/dist/stamps.js +14 -0
  25. package/dist/triggers.js +5 -0
  26. package/dist/usage.js +21 -1
  27. package/dist/vendor/codeburn/antigravity.js +309 -0
  28. package/dist/vendor/codeburn/codex.js +429 -0
  29. package/dist/vendor/codeburn/copilot.js +292 -0
  30. package/dist/vendor/codeburn/gemini.js +195 -0
  31. package/dist/vendor/codeburn/grokbot.js +182 -0
  32. package/dist/vendor/codeburn/kimi.js +178 -0
  33. package/dist/vendor/codeburn/shims/chalk.js +14 -0
  34. package/dist/vendor/codeburn/shims/strip-ansi.js +6 -0
  35. package/dist/vendor/codeburn/src/act/backup.js +83 -0
  36. package/dist/vendor/codeburn/src/act/model-defaults.js +151 -0
  37. package/dist/vendor/codeburn/src/act/types.js +37 -0
  38. package/dist/vendor/codeburn/src/bash-utils.js +204 -0
  39. package/dist/vendor/codeburn/src/behavioral-weight.js +47 -0
  40. package/dist/vendor/codeburn/src/cache-dir.js +26 -0
  41. package/dist/vendor/codeburn/src/cache-refresh-lock.js +543 -0
  42. package/dist/vendor/codeburn/src/cache-sweep.js +67 -0
  43. package/dist/vendor/codeburn/src/classifier.js +209 -0
  44. package/dist/vendor/codeburn/src/claude-identity.js +223 -0
  45. package/dist/vendor/codeburn/src/codex-cache.js +377 -0
  46. package/dist/vendor/codeburn/src/codex-fork-replay.js +232 -0
  47. package/dist/vendor/codeburn/src/codex-jsonl-fields.js +99 -0
  48. package/dist/vendor/codeburn/src/codex-session-index.js +113 -0
  49. package/dist/vendor/codeburn/src/codex-throughput.js +544 -0
  50. package/dist/vendor/codeburn/src/compare-stats.js +405 -0
  51. package/dist/vendor/codeburn/src/config.js +145 -0
  52. package/dist/vendor/codeburn/src/content-utils.js +56 -0
  53. package/dist/vendor/codeburn/src/context-tree-codex.js +329 -0
  54. package/dist/vendor/codeburn/src/context-tree.js +664 -0
  55. package/dist/vendor/codeburn/src/copilot-aiu.js +16 -0
  56. package/dist/vendor/codeburn/src/currency.js +189 -0
  57. package/dist/vendor/codeburn/src/cursor-cache.js +238 -0
  58. package/dist/vendor/codeburn/src/cursor-import.js +402 -0
  59. package/dist/vendor/codeburn/src/cursor-sync.js +256 -0
  60. package/dist/vendor/codeburn/src/daily-cache.js +2036 -0
  61. package/dist/vendor/codeburn/src/data/litellm-snapshot.json +1 -0
  62. package/dist/vendor/codeburn/src/data/pricing-fallback.json +1 -0
  63. package/dist/vendor/codeburn/src/day-aggregator.js +327 -0
  64. package/dist/vendor/codeburn/src/editor-data-dir.js +11 -0
  65. package/dist/vendor/codeburn/src/fetch-utils.js +18 -0
  66. package/dist/vendor/codeburn/src/format.js +118 -0
  67. package/dist/vendor/codeburn/src/fs-utils.js +270 -0
  68. package/dist/vendor/codeburn/src/git-origin.js +334 -0
  69. package/dist/vendor/codeburn/src/granular-history.js +328 -0
  70. package/dist/vendor/codeburn/src/hermes-session-ledger.js +512 -0
  71. package/dist/vendor/codeburn/src/launcher-homes.js +142 -0
  72. package/dist/vendor/codeburn/src/live-sessions.js +358 -0
  73. package/dist/vendor/codeburn/src/math-utils.js +8 -0
  74. package/dist/vendor/codeburn/src/menubar-json.js +294 -0
  75. package/dist/vendor/codeburn/src/models.js +1879 -0
  76. package/dist/vendor/codeburn/src/optimize-one-hour-cache.js +92 -0
  77. package/dist/vendor/codeburn/src/optimize.js +4110 -0
  78. package/dist/vendor/codeburn/src/parse-workers.js +219 -0
  79. package/dist/vendor/codeburn/src/parser.js +6953 -0
  80. package/dist/vendor/codeburn/src/path-privacy.js +26 -0
  81. package/dist/vendor/codeburn/src/plans.js +135 -0
  82. package/dist/vendor/codeburn/src/pr-signals.js +132 -0
  83. package/dist/vendor/codeburn/src/project-filter-warnings.js +33 -0
  84. package/dist/vendor/codeburn/src/provider-issues.js +30 -0
  85. package/dist/vendor/codeburn/src/providers/amp.js +286 -0
  86. package/dist/vendor/codeburn/src/providers/antigravity.js +1731 -0
  87. package/dist/vendor/codeburn/src/providers/claude.js +551 -0
  88. package/dist/vendor/codeburn/src/providers/cline-cli.js +401 -0
  89. package/dist/vendor/codeburn/src/providers/cline.js +63 -0
  90. package/dist/vendor/codeburn/src/providers/codebuff.js +336 -0
  91. package/dist/vendor/codeburn/src/providers/codewhale.js +442 -0
  92. package/dist/vendor/codeburn/src/providers/codex.js +1720 -0
  93. package/dist/vendor/codeburn/src/providers/command-code.js +196 -0
  94. package/dist/vendor/codeburn/src/providers/copilot.js +3278 -0
  95. package/dist/vendor/codeburn/src/providers/crush.js +226 -0
  96. package/dist/vendor/codeburn/src/providers/cursor-agent.js +837 -0
  97. package/dist/vendor/codeburn/src/providers/cursor.js +1163 -0
  98. package/dist/vendor/codeburn/src/providers/devin.js +569 -0
  99. package/dist/vendor/codeburn/src/providers/droid.js +324 -0
  100. package/dist/vendor/codeburn/src/providers/dsh.js +690 -0
  101. package/dist/vendor/codeburn/src/providers/forge.js +245 -0
  102. package/dist/vendor/codeburn/src/providers/gemini.js +230 -0
  103. package/dist/vendor/codeburn/src/providers/goose.js +252 -0
  104. package/dist/vendor/codeburn/src/providers/grok.js +581 -0
  105. package/dist/vendor/codeburn/src/providers/grokbot.js +290 -0
  106. package/dist/vendor/codeburn/src/providers/hermes.js +771 -0
  107. package/dist/vendor/codeburn/src/providers/ibm-bob.js +53 -0
  108. package/dist/vendor/codeburn/src/providers/index.js +360 -0
  109. package/dist/vendor/codeburn/src/providers/kilo-code.js +50 -0
  110. package/dist/vendor/codeburn/src/providers/kimi.js +363 -0
  111. package/dist/vendor/codeburn/src/providers/kimicode.js +402 -0
  112. package/dist/vendor/codeburn/src/providers/kiro.js +1174 -0
  113. package/dist/vendor/codeburn/src/providers/lingtai-tui.js +372 -0
  114. package/dist/vendor/codeburn/src/providers/mistral-vibe-unified.js +236 -0
  115. package/dist/vendor/codeburn/src/providers/mistral-vibe.js +405 -0
  116. package/dist/vendor/codeburn/src/providers/mux.js +242 -0
  117. package/dist/vendor/codeburn/src/providers/open-design.js +217 -0
  118. package/dist/vendor/codeburn/src/providers/openclaude.js +309 -0
  119. package/dist/vendor/codeburn/src/providers/openclaw.js +442 -0
  120. package/dist/vendor/codeburn/src/providers/opencode-file-parser.js +131 -0
  121. package/dist/vendor/codeburn/src/providers/opencode.js +91 -0
  122. package/dist/vendor/codeburn/src/providers/pi.js +429 -0
  123. package/dist/vendor/codeburn/src/providers/quickdesk.js +491 -0
  124. package/dist/vendor/codeburn/src/providers/qwen.js +193 -0
  125. package/dist/vendor/codeburn/src/providers/session-message.js +118 -0
  126. package/dist/vendor/codeburn/src/providers/sqlite-session-parser.js +457 -0
  127. package/dist/vendor/codeburn/src/providers/types.js +5 -0
  128. package/dist/vendor/codeburn/src/providers/vercel-gateway.js +135 -0
  129. package/dist/vendor/codeburn/src/providers/vscode-cline-parser.js +212 -0
  130. package/dist/vendor/codeburn/src/providers/warp.js +575 -0
  131. package/dist/vendor/codeburn/src/providers/zcode.js +249 -0
  132. package/dist/vendor/codeburn/src/providers/zed.js +187 -0
  133. package/dist/vendor/codeburn/src/providers/zerostack.js +139 -0
  134. package/dist/vendor/codeburn/src/quota/antigravity.js +310 -0
  135. package/dist/vendor/codeburn/src/quota/claude.js +244 -0
  136. package/dist/vendor/codeburn/src/quota/cursor.js +301 -0
  137. package/dist/vendor/codeburn/src/quota/security.js +299 -0
  138. package/dist/vendor/codeburn/src/quota/types.js +1 -0
  139. package/dist/vendor/codeburn/src/scan-facts.js +99 -0
  140. package/dist/vendor/codeburn/src/session-cache.js +3040 -0
  141. package/dist/vendor/codeburn/src/session-count-label.js +14 -0
  142. package/dist/vendor/codeburn/src/session-output.js +139 -0
  143. package/dist/vendor/codeburn/src/session-population.js +30 -0
  144. package/dist/vendor/codeburn/src/sessions-report.js +1342 -0
  145. package/dist/vendor/codeburn/src/sqlite.js +528 -0
  146. package/dist/vendor/codeburn/src/sync/otlp.js +420 -0
  147. package/dist/vendor/codeburn/src/telemetry-snapshot.js +244 -0
  148. package/dist/vendor/codeburn/src/token-estimate.js +5 -0
  149. package/dist/vendor/codeburn/src/types.js +16 -0
  150. package/dist/vendor/codeburn/src/work-units.js +126 -0
  151. package/dist/vendor/codeburn/src/workflow-insights.js +228 -0
  152. package/dist/vendor/codeburn/src/wsl.js +198 -0
  153. package/dist/vendor/codeburn/src/yield.js +666 -0
  154. package/package.json +1 -1
@@ -0,0 +1,336 @@
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 } from 'path';
4
+ import { homedir } from 'os';
5
+ import { calculateCost, ownValue } from '../models.js';
6
+ import { extractBashCommands } from '../bash-utils.js';
7
+ // Codebuff (formerly Manicode) uses a credit-based billing system. The local
8
+ // chat-messages.json doesn't record per-call token counts the way Claude Code
9
+ // or Codex do -- only `credits` on completed assistant messages. We convert
10
+ // credits to USD using Codebuff's retail pay-as-you-go rate so the cost shows
11
+ // up in the dashboard even when tokens are absent. The rate intentionally
12
+ // rounds up to the public PAYG tier ($0.01 / credit) so we never understate
13
+ // spend; users on a subscription plan get a conservative upper bound.
14
+ const USD_PER_CREDIT = 0.01;
15
+ // Codebuff's chat history lives under `~/.config/manicode/` (the legacy
16
+ // product name is still on disk). Development and staging channels use
17
+ // `manicode-dev` and `manicode-staging` -- we walk all three when present.
18
+ const CHANNELS = ['manicode', 'manicode-dev', 'manicode-staging'];
19
+ const modelDisplayNames = {
20
+ codebuff: 'Codebuff',
21
+ 'codebuff-base': 'Codebuff Base',
22
+ 'codebuff-base2': 'Codebuff Base 2',
23
+ 'codebuff-lite': 'Codebuff Lite',
24
+ 'codebuff-max': 'Codebuff Max',
25
+ };
26
+ // Codebuff's native tool names map to codeburn's canonical tool set so
27
+ // classifier heuristics (edit/read/bash/etc.) behave consistently with the
28
+ // other providers.
29
+ const toolNameMap = {
30
+ read_files: 'Read',
31
+ read_file: 'Read',
32
+ code_search: 'Grep',
33
+ glob: 'Glob',
34
+ find_files: 'Glob',
35
+ str_replace: 'Edit',
36
+ edit_file: 'Edit',
37
+ write_file: 'Write',
38
+ run_terminal_command: 'Bash',
39
+ terminal: 'Bash',
40
+ spawn_agents: 'Agent',
41
+ spawn_agent: 'Agent',
42
+ write_todos: 'TodoWrite',
43
+ create_plan: 'TodoWrite',
44
+ browser_logs: 'WebFetch',
45
+ web_search: 'WebSearch',
46
+ fetch_url: 'WebFetch',
47
+ };
48
+ // Tool names we ignore for classification -- they're not useful signals for
49
+ // distinguishing "coding" vs "exploration" vs "planning" work.
50
+ const IGNORED_TOOLS = new Set(['suggest_followups', 'end_turn']);
51
+ // Shared by discoverSessions and probeRoots. Factory / CODEBUFF_DATA_DIR win
52
+ // as a single root; otherwise discovery walks every CHANNEL before existence
53
+ // filtering. Empty / blank factory is unset (same as #938 truthiness).
54
+ export function getCodebuffRootSet(override) {
55
+ if (override && override.trim())
56
+ return [override];
57
+ const envPath = process.env['CODEBUFF_DATA_DIR'];
58
+ if (envPath && envPath.trim())
59
+ return [envPath];
60
+ const configDir = join(homedir(), '.config');
61
+ return CHANNELS.map(channel => join(configDir, channel));
62
+ }
63
+ function pickNumber(...vals) {
64
+ for (const v of vals) {
65
+ if (typeof v === 'number' && Number.isFinite(v))
66
+ return v;
67
+ }
68
+ return undefined;
69
+ }
70
+ function normalizeUsage(u) {
71
+ if (!u)
72
+ return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
73
+ return {
74
+ input: pickNumber(u.inputTokens, u.input_tokens, u.promptTokens, u.prompt_tokens) ?? 0,
75
+ output: pickNumber(u.outputTokens, u.output_tokens, u.completionTokens, u.completion_tokens) ?? 0,
76
+ cacheRead: pickNumber(u.cacheReadInputTokens, u.cache_read_input_tokens, u.promptTokensDetails?.cachedTokens, u.prompt_tokens_details?.cached_tokens) ?? 0,
77
+ cacheWrite: pickNumber(u.cacheCreationInputTokens, u.cache_creation_input_tokens) ?? 0,
78
+ };
79
+ }
80
+ function coerceTimestamp(value) {
81
+ if (value == null)
82
+ return '';
83
+ if (typeof value === 'number') {
84
+ return Number.isFinite(value) ? new Date(value).toISOString() : '';
85
+ }
86
+ const parsed = Date.parse(value);
87
+ return Number.isFinite(parsed) ? new Date(parsed).toISOString() : value;
88
+ }
89
+ function parseChatIdToIso(chatId) {
90
+ const iso = chatId.replace(/(\d{4}-\d{2}-\d{2}T\d{2})-(\d{2})-(\d{2})/, '$1:$2:$3');
91
+ const parsed = Date.parse(iso);
92
+ return Number.isFinite(parsed) ? new Date(parsed).toISOString() : '';
93
+ }
94
+ function extractCwd(meta) {
95
+ const rs = meta?.runState;
96
+ if (!rs)
97
+ return null;
98
+ return (rs.sessionState?.projectContext?.cwd ??
99
+ rs.sessionState?.fileContext?.cwd ??
100
+ rs.sessionState?.cwd ??
101
+ rs.cwd ??
102
+ null);
103
+ }
104
+ function extractAgentType(meta) {
105
+ return meta?.runState?.sessionState?.mainAgentState?.agentType ?? null;
106
+ }
107
+ function collectBlockTools(blocks, acc) {
108
+ if (!Array.isArray(blocks))
109
+ return;
110
+ for (const block of blocks) {
111
+ if (!block || typeof block !== 'object')
112
+ continue;
113
+ if (block.type === 'tool' && typeof block.toolName === 'string') {
114
+ const raw = block.toolName;
115
+ if (!IGNORED_TOOLS.has(raw)) {
116
+ acc.tools.push(ownValue(toolNameMap, raw) ?? raw);
117
+ }
118
+ if ((raw === 'run_terminal_command' || raw === 'terminal') && block.input) {
119
+ const cmd = block.input['command'];
120
+ if (typeof cmd === 'string') {
121
+ acc.bash.push(...extractBashCommands(cmd));
122
+ }
123
+ }
124
+ }
125
+ if (block.type === 'agent' && Array.isArray(block.blocks)) {
126
+ collectBlockTools(block.blocks, acc);
127
+ }
128
+ }
129
+ }
130
+ function resolveModel(meta, stashedModel) {
131
+ const direct = meta?.model ?? meta?.modelId ?? meta?.codebuff?.model;
132
+ if (direct)
133
+ return direct;
134
+ if (stashedModel)
135
+ return stashedModel;
136
+ const agentType = extractAgentType(meta);
137
+ if (agentType)
138
+ return `codebuff-${agentType}`;
139
+ return 'codebuff';
140
+ }
141
+ function usageFromHistory(meta) {
142
+ const hist = meta?.runState?.sessionState?.mainAgentState?.messageHistory;
143
+ if (!Array.isArray(hist))
144
+ return { model: null, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
145
+ for (let i = hist.length - 1; i >= 0; i--) {
146
+ const entry = hist[i];
147
+ if (!entry || entry.role !== 'assistant' || !entry.providerOptions)
148
+ continue;
149
+ const u = normalizeUsage(entry.providerOptions.usage ?? entry.providerOptions.codebuff?.usage);
150
+ if (u.input > 0 || u.output > 0 || u.cacheRead > 0 || u.cacheWrite > 0) {
151
+ return { model: entry.providerOptions.codebuff?.model ?? null, ...u };
152
+ }
153
+ }
154
+ return { model: null, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
155
+ }
156
+ async function readJson(filePath) {
157
+ try {
158
+ const raw = await readFile(filePath, 'utf-8');
159
+ return JSON.parse(raw);
160
+ }
161
+ catch {
162
+ return null;
163
+ }
164
+ }
165
+ async function discoverChannel(root) {
166
+ const sources = [];
167
+ const projectsDir = join(root, 'projects');
168
+ let projectNames;
169
+ try {
170
+ projectNames = await readdir(projectsDir);
171
+ }
172
+ catch {
173
+ return sources;
174
+ }
175
+ for (const projectName of projectNames) {
176
+ const chatsDir = join(projectsDir, projectName, 'chats');
177
+ let chatIds;
178
+ try {
179
+ chatIds = await readdir(chatsDir);
180
+ }
181
+ catch {
182
+ continue;
183
+ }
184
+ for (const chatId of chatIds) {
185
+ const chatDir = join(chatsDir, chatId);
186
+ const dirStat = await stat(chatDir).catch(() => null);
187
+ if (!dirStat?.isDirectory())
188
+ continue;
189
+ const messagesPath = join(chatDir, 'chat-messages.json');
190
+ const messagesStat = await stat(messagesPath).catch(() => null);
191
+ if (!messagesStat?.isFile())
192
+ continue;
193
+ // Resolve the real cwd from run-state.json so sessions group by the
194
+ // originating project directory instead of the sanitized chat folder
195
+ // name (which is often the same for many users).
196
+ const runState = await readJson(join(chatDir, 'run-state.json'));
197
+ const cwd = extractCwd({ runState: runState ?? undefined });
198
+ const project = cwd ? basename(cwd) : projectName;
199
+ sources.push({ path: chatDir, project, provider: 'codebuff' });
200
+ }
201
+ }
202
+ return sources;
203
+ }
204
+ async function discoverSessionsInRoots(roots) {
205
+ const results = [];
206
+ for (const root of roots) {
207
+ results.push(...await discoverChannel(root));
208
+ }
209
+ return results;
210
+ }
211
+ // Downstream aggregation groups sessions by `(provider, sessionId, project)`
212
+ // (see src/parser.ts). Codebuff chat folders are ISO timestamps, which means
213
+ // the same `chatId` can legitimately appear under each channel root
214
+ // (`manicode`, `manicode-dev`, `manicode-staging`) and even resolve to the
215
+ // same project cwd. To keep those sessions distinct we include the channel
216
+ // identity in the sessionId. The channel is derived from the fixed path
217
+ // structure Codebuff writes on disk: `<channelRoot>/projects/<project>/chats/<chatId>`.
218
+ // Returns null when the path doesn't match that shape so the caller can fall
219
+ // back to a plain chatId.
220
+ //
221
+ // We use '/' as the channel/chatId separator rather than ':' because
222
+ // src/parser.ts builds its session key as `${provider}:${sessionId}:${project}`
223
+ // and reconstructs the sessionId with `key.split(':')[1]` -- any colon inside
224
+ // sessionId would get truncated to just the channel name downstream.
225
+ function extractChannelFromChatDir(chatDir) {
226
+ const chatsDir = dirname(chatDir);
227
+ if (basename(chatsDir) !== 'chats')
228
+ return null;
229
+ const projectDir = dirname(chatsDir);
230
+ const projectsDir = dirname(projectDir);
231
+ if (basename(projectsDir) !== 'projects')
232
+ return null;
233
+ const channel = basename(dirname(projectsDir));
234
+ return channel ? channel : null;
235
+ }
236
+ function createParser(source, seenKeys) {
237
+ return {
238
+ async *parse() {
239
+ const chatDir = source.path;
240
+ const chatId = basename(chatDir);
241
+ const channel = extractChannelFromChatDir(chatDir);
242
+ const sessionId = channel ? `${channel}/${chatId}` : chatId;
243
+ const fallbackTs = parseChatIdToIso(chatId);
244
+ const messages = await readJson(join(chatDir, 'chat-messages.json'));
245
+ if (!Array.isArray(messages))
246
+ return;
247
+ let pendingUserMessage = '';
248
+ for (const [idx, msg] of messages.entries()) {
249
+ if (!msg || typeof msg !== 'object')
250
+ continue;
251
+ const variant = msg.variant ?? msg.role;
252
+ if (variant === 'user') {
253
+ if (typeof msg.content === 'string' && msg.content.length > 0) {
254
+ pendingUserMessage = msg.content;
255
+ }
256
+ continue;
257
+ }
258
+ if (variant !== 'ai' && variant !== 'agent' && variant !== 'assistant')
259
+ continue;
260
+ const credits = typeof msg.credits === 'number' && Number.isFinite(msg.credits) ? msg.credits : 0;
261
+ const directUsage = normalizeUsage(msg.metadata?.usage ?? msg.metadata?.codebuff?.usage);
262
+ const stashedUsage = usageFromHistory(msg.metadata);
263
+ const hasDirect = directUsage.input > 0 ||
264
+ directUsage.output > 0 ||
265
+ directUsage.cacheRead > 0 ||
266
+ directUsage.cacheWrite > 0;
267
+ const usage = hasDirect ? directUsage : stashedUsage;
268
+ const stashedModel = stashedUsage.model;
269
+ // Skip messages with neither credits nor tokens -- they're typically
270
+ // in-progress mode dividers or empty framing blocks.
271
+ if (credits === 0 && usage.input === 0 && usage.output === 0 && usage.cacheRead === 0 && usage.cacheWrite === 0) {
272
+ continue;
273
+ }
274
+ const model = resolveModel(msg.metadata, stashedModel);
275
+ const timestamp = coerceTimestamp(msg.timestamp ?? msg.metadata?.timestamp) || fallbackTs;
276
+ const dedupId = msg.id ?? String(idx);
277
+ const dedupKey = `codebuff:${chatDir}:${dedupId}`;
278
+ if (seenKeys.has(dedupKey))
279
+ continue;
280
+ seenKeys.add(dedupKey);
281
+ const acc = { tools: [], bash: [] };
282
+ collectBlockTools(msg.blocks, acc);
283
+ // Prefer calculated cost from tokens when available (multi-provider
284
+ // models routed through Codebuff still show up in LiteLLM); otherwise
285
+ // fall back to the credit-based approximation.
286
+ let costUSD = calculateCost(model, usage.input, usage.output, usage.cacheWrite, usage.cacheRead, 0);
287
+ if (costUSD === 0 && credits > 0) {
288
+ costUSD = credits * USD_PER_CREDIT;
289
+ }
290
+ yield {
291
+ provider: 'codebuff',
292
+ model,
293
+ inputTokens: usage.input,
294
+ outputTokens: usage.output,
295
+ cacheCreationInputTokens: usage.cacheWrite,
296
+ cacheReadInputTokens: usage.cacheRead,
297
+ cachedInputTokens: usage.cacheRead,
298
+ reasoningTokens: 0,
299
+ webSearchRequests: 0,
300
+ costUSD,
301
+ tools: acc.tools,
302
+ bashCommands: acc.bash,
303
+ timestamp,
304
+ speed: 'standard',
305
+ deduplicationKey: dedupKey,
306
+ userMessage: pendingUserMessage,
307
+ sessionId,
308
+ };
309
+ pendingUserMessage = '';
310
+ }
311
+ },
312
+ };
313
+ }
314
+ export function createCodebuffProvider(baseDir) {
315
+ const roots = getCodebuffRootSet(baseDir);
316
+ return {
317
+ name: 'codebuff',
318
+ displayName: 'Codebuff',
319
+ modelDisplayName(model) {
320
+ return ownValue(modelDisplayNames, model) ?? model;
321
+ },
322
+ toolDisplayName(rawTool) {
323
+ return ownValue(toolNameMap, rawTool) ?? rawTool;
324
+ },
325
+ async probeRoots() {
326
+ return roots.map(path => ({ path, label: 'chats' }));
327
+ },
328
+ async discoverSessions() {
329
+ return discoverSessionsInRoots(roots);
330
+ },
331
+ createSessionParser(source, seenKeys) {
332
+ return createParser(source, seenKeys);
333
+ },
334
+ };
335
+ }
336
+ export const codebuff = createCodebuffProvider();