import type { JsonlStore, RawBackend } from "./types.js"; export function createJsonlStore(backend: RawBackend, name: string): JsonlStore { const fileName = `${name}.jsonl`; return { async append(value: T): Promise { const line = JSON.stringify(value); await backend.append(fileName, line); }, async read(options?: { last?: number }): Promise { const lines = await backend.readLines(fileName, options); const results: T[] = []; for (const line of lines) { try { results.push(JSON.parse(line) as T); } catch (err) { console.error(`[storage] corrupt JSONL line in ${fileName}, skipping:`, err); } } return results; }, async trim(options: { keepLast: number }): Promise { await backend.trimLines(fileName, options.keepLast); }, }; }