|
8 | 8 | * @module data-loader |
9 | 9 | */ |
10 | 10 |
|
11 | | -import { readFile } from 'node:fs/promises'; |
12 | | -import { homedir } from 'node:os'; |
| 11 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; |
| 12 | +import { homedir, tmpdir } from 'node:os'; |
13 | 13 | import path from 'node:path'; |
14 | 14 | import process from 'node:process'; |
15 | 15 | import { isDirectorySync } from 'path-type'; |
@@ -123,6 +123,11 @@ export type LoadedSessionMetadata = { |
123 | 123 | directory: string; |
124 | 124 | }; |
125 | 125 |
|
| 126 | +export type LoadOpenCodeMessagesOptions = { |
| 127 | + since?: string; |
| 128 | + until?: string; |
| 129 | +}; |
| 130 | + |
126 | 131 | /** |
127 | 132 | * Get OpenCode data directory |
128 | 133 | * @returns Path to OpenCode data directory, or null if not found |
@@ -187,6 +192,26 @@ function convertOpenCodeMessageToUsageEntry( |
187 | 192 | }; |
188 | 193 | } |
189 | 194 |
|
| 195 | +function filterOpenCodeMessagesByDateRange( |
| 196 | + entries: LoadedUsageEntry[], |
| 197 | + options?: LoadOpenCodeMessagesOptions, |
| 198 | +): LoadedUsageEntry[] { |
| 199 | + if (options?.since == null && options?.until == null) { |
| 200 | + return entries; |
| 201 | + } |
| 202 | + |
| 203 | + return entries.filter((entry) => { |
| 204 | + const dateStr = entry.timestamp.toISOString().substring(0, 10).replace(/-/g, ''); |
| 205 | + if (options.since != null && dateStr < options.since) { |
| 206 | + return false; |
| 207 | + } |
| 208 | + if (options.until != null && dateStr > options.until) { |
| 209 | + return false; |
| 210 | + } |
| 211 | + return true; |
| 212 | + }); |
| 213 | +} |
| 214 | + |
190 | 215 | async function loadOpenCodeSession( |
191 | 216 | filePath: string, |
192 | 217 | ): Promise<v.InferOutput<typeof openCodeSessionSchema> | null> { |
@@ -252,7 +277,9 @@ export async function loadOpenCodeSessions(): Promise<Map<string, LoadedSessionM |
252 | 277 | * Load all OpenCode messages |
253 | 278 | * @returns Array of LoadedUsageEntry for aggregation |
254 | 279 | */ |
255 | | -export async function loadOpenCodeMessages(): Promise<LoadedUsageEntry[]> { |
| 280 | +export async function loadOpenCodeMessages( |
| 281 | + options?: LoadOpenCodeMessagesOptions, |
| 282 | +): Promise<LoadedUsageEntry[]> { |
256 | 283 | const openCodePath = getOpenCodePath(); |
257 | 284 | if (openCodePath == null) { |
258 | 285 | return []; |
@@ -305,11 +332,54 @@ export async function loadOpenCodeMessages(): Promise<LoadedUsageEntry[]> { |
305 | 332 | entries.push(entry); |
306 | 333 | } |
307 | 334 |
|
308 | | - return entries; |
| 335 | + return filterOpenCodeMessagesByDateRange(entries, options); |
309 | 336 | } |
310 | 337 |
|
311 | 338 | if (import.meta.vitest != null) { |
312 | | - const { describe, it, expect } = import.meta.vitest; |
| 339 | + const { describe, it, expect, afterEach, vi } = import.meta.vitest; |
| 340 | + |
| 341 | + afterEach(() => { |
| 342 | + vi.unstubAllEnvs(); |
| 343 | + }); |
| 344 | + |
| 345 | + async function createOpenCodeMessageFixture( |
| 346 | + messages: Array<v.InferOutput<typeof openCodeMessageSchema>>, |
| 347 | + ): Promise<string> { |
| 348 | + const root = await mkdtemp(path.join(tmpdir(), 'opencode-data-')); |
| 349 | + const messagesDir = path.join(root, 'storage', 'message'); |
| 350 | + await mkdir(messagesDir, { recursive: true }); |
| 351 | + |
| 352 | + await Promise.all( |
| 353 | + messages.map(async (message) => |
| 354 | + writeFile(path.join(messagesDir, `${message.id}.json`), JSON.stringify(message)), |
| 355 | + ), |
| 356 | + ); |
| 357 | + |
| 358 | + return root; |
| 359 | + } |
| 360 | + |
| 361 | + function createTestMessage( |
| 362 | + id: string, |
| 363 | + timestamp: string, |
| 364 | + ): v.InferOutput<typeof openCodeMessageSchema> { |
| 365 | + return { |
| 366 | + id, |
| 367 | + sessionID: 'session-test' as v.InferOutput<typeof sessionIdSchema>, |
| 368 | + providerID: 'anthropic', |
| 369 | + modelID: 'claude-sonnet-4-5' as v.InferOutput<typeof modelNameSchema>, |
| 370 | + time: { |
| 371 | + created: Date.parse(timestamp), |
| 372 | + }, |
| 373 | + tokens: { |
| 374 | + input: 100, |
| 375 | + output: 50, |
| 376 | + }, |
| 377 | + }; |
| 378 | + } |
| 379 | + |
| 380 | + function getSortedIsoTimestamps(entries: LoadedUsageEntry[]): string[] { |
| 381 | + return entries.map((entry) => entry.timestamp.toISOString()).sort(); |
| 382 | + } |
313 | 383 |
|
314 | 384 | describe('data-loader', () => { |
315 | 385 | it('should convert OpenCode message to LoadedUsageEntry', () => { |
@@ -366,5 +436,68 @@ if (import.meta.vitest != null) { |
366 | 436 | expect(entry.usage.cacheCreationInputTokens).toBe(0); |
367 | 437 | expect(entry.costUSD).toBe(null); |
368 | 438 | }); |
| 439 | + |
| 440 | + it('should filter OpenCode messages by since date', async () => { |
| 441 | + const root = await createOpenCodeMessageFixture([ |
| 442 | + createTestMessage('msg_20240101', '2024-01-01T12:00:00.000Z'), |
| 443 | + createTestMessage('msg_20240102', '2024-01-02T12:00:00.000Z'), |
| 444 | + createTestMessage('msg_20240103', '2024-01-03T12:00:00.000Z'), |
| 445 | + ]); |
| 446 | + |
| 447 | + try { |
| 448 | + vi.stubEnv(OPENCODE_CONFIG_DIR_ENV, root); |
| 449 | + |
| 450 | + const entries = await loadOpenCodeMessages({ since: '20240102' }); |
| 451 | + |
| 452 | + expect(getSortedIsoTimestamps(entries)).toEqual([ |
| 453 | + '2024-01-02T12:00:00.000Z', |
| 454 | + '2024-01-03T12:00:00.000Z', |
| 455 | + ]); |
| 456 | + } finally { |
| 457 | + await rm(root, { recursive: true, force: true }); |
| 458 | + } |
| 459 | + }); |
| 460 | + |
| 461 | + it('should filter OpenCode messages by until date', async () => { |
| 462 | + const root = await createOpenCodeMessageFixture([ |
| 463 | + createTestMessage('msg_20240101', '2024-01-01T12:00:00.000Z'), |
| 464 | + createTestMessage('msg_20240102', '2024-01-02T12:00:00.000Z'), |
| 465 | + createTestMessage('msg_20240103', '2024-01-03T12:00:00.000Z'), |
| 466 | + ]); |
| 467 | + |
| 468 | + try { |
| 469 | + vi.stubEnv(OPENCODE_CONFIG_DIR_ENV, root); |
| 470 | + |
| 471 | + const entries = await loadOpenCodeMessages({ until: '20240102' }); |
| 472 | + |
| 473 | + expect(getSortedIsoTimestamps(entries)).toEqual([ |
| 474 | + '2024-01-01T12:00:00.000Z', |
| 475 | + '2024-01-02T12:00:00.000Z', |
| 476 | + ]); |
| 477 | + } finally { |
| 478 | + await rm(root, { recursive: true, force: true }); |
| 479 | + } |
| 480 | + }); |
| 481 | + |
| 482 | + it('should filter OpenCode messages by since and until dates', async () => { |
| 483 | + const root = await createOpenCodeMessageFixture([ |
| 484 | + createTestMessage('msg_20240101', '2024-01-01T12:00:00.000Z'), |
| 485 | + createTestMessage('msg_20240102', '2024-01-02T12:00:00.000Z'), |
| 486 | + createTestMessage('msg_20240103', '2024-01-03T12:00:00.000Z'), |
| 487 | + ]); |
| 488 | + |
| 489 | + try { |
| 490 | + vi.stubEnv(OPENCODE_CONFIG_DIR_ENV, root); |
| 491 | + |
| 492 | + const entries = await loadOpenCodeMessages({ |
| 493 | + since: '20240102', |
| 494 | + until: '20240102', |
| 495 | + }); |
| 496 | + |
| 497 | + expect(getSortedIsoTimestamps(entries)).toEqual(['2024-01-02T12:00:00.000Z']); |
| 498 | + } finally { |
| 499 | + await rm(root, { recursive: true, force: true }); |
| 500 | + } |
| 501 | + }); |
369 | 502 | }); |
370 | 503 | } |
0 commit comments