{"version":3,"file":"jsonl.d.ts","sourceRoot":"","sources":["../../../../src/harness/session/repo/jsonl.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,yBAAyB,EACzB,uBAAuB,EACvB,oBAAoB,EACpB,mBAAmB,EACnB,OAAO,EACP,MAAM,gBAAgB,CAAC;AAiBxB,qBAAa,gBAAiB,YAAW,mBAAmB;IAC3D,OAAO,CAAC,YAAY,CAAS;IAE7B,YAAY,OAAO,EAAE;QAAE,YAAY,EAAE,MAAM,CAAA;KAAE,EAE5C;IAED,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,qBAAqB;IAIvB,MAAM,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAWvF;IAEK,IAAI,CAAC,QAAQ,EAAE,oBAAoB,GAAG,OAAO,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAMjF;IAEK,IAAI,CAAC,OAAO,GAAE,uBAA4B,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC,CAgBjF;IAEK,MAAM,CAAC,QAAQ,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC,CAE1D;IAEK,IAAI,CACT,cAAc,EAAE,oBAAoB,EACpC,OAAO,EAAE,yBAAyB,GAAG;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;QAAC,EAAE,CAAC,EAAE,MAAM,CAAA;KAAE,GAChG,OAAO,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAcxC;YAEa,eAAe;CAK7B","sourcesContent":["import { constants } from \"node:fs\";\nimport { access, mkdir, readdir, rm } from \"node:fs/promises\";\nimport { join, resolve } from \"node:path\";\nimport type {\n\tJsonlSessionCreateOptions,\n\tJsonlSessionListOptions,\n\tJsonlSessionMetadata,\n\tJsonlSessionRepoApi,\n\tSession,\n} from \"../../types.js\";\nimport { JsonlSessionStorage, loadJsonlSessionMetadata } from \"../storage/jsonl.js\";\nimport { createSessionId, createTimestamp, getEntriesToFork, toSession } from \"./shared.js\";\n\nasync function exists(path: string): Promise<boolean> {\n\ttry {\n\t\tawait access(path, constants.F_OK);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nfunction encodeCwd(cwd: string): string {\n\treturn `--${cwd.replace(/^[/\\\\]/, \"\").replace(/[/\\\\:]/g, \"-\")}--`;\n}\n\nexport class JsonlSessionRepo implements JsonlSessionRepoApi {\n\tprivate sessionsRoot: string;\n\n\tconstructor(options: { sessionsRoot: string }) {\n\t\tthis.sessionsRoot = resolve(options.sessionsRoot);\n\t}\n\n\tprivate getSessionDir(cwd: string): string {\n\t\treturn join(this.sessionsRoot, encodeCwd(cwd));\n\t}\n\n\tprivate createSessionFilePath(cwd: string, sessionId: string, timestamp: string): string {\n\t\treturn join(this.getSessionDir(cwd), `${timestamp.replace(/[:.]/g, \"-\")}_${sessionId}.jsonl`);\n\t}\n\n\tasync create(options: JsonlSessionCreateOptions): Promise<Session<JsonlSessionMetadata>> {\n\t\tawait mkdir(this.sessionsRoot, { recursive: true });\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst filePath = this.createSessionFilePath(options.cwd, id, createdAt);\n\t\tconst storage = await JsonlSessionStorage.create(filePath, {\n\t\t\tcwd: options.cwd,\n\t\t\tsessionId: id,\n\t\t\tparentSessionPath: options.parentSessionPath,\n\t\t});\n\t\treturn toSession(storage);\n\t}\n\n\tasync open(metadata: JsonlSessionMetadata): Promise<Session<JsonlSessionMetadata>> {\n\t\tif (!(await exists(metadata.path))) {\n\t\t\tthrow new Error(`Session not found: ${metadata.path}`);\n\t\t}\n\t\tconst storage = await JsonlSessionStorage.open(metadata.path);\n\t\treturn toSession(storage);\n\t}\n\n\tasync list(options: JsonlSessionListOptions = {}): Promise<JsonlSessionMetadata[]> {\n\t\tconst dirs = options.cwd ? [this.getSessionDir(options.cwd)] : await this.listSessionDirs();\n\t\tconst sessions: JsonlSessionMetadata[] = [];\n\t\tfor (const dir of dirs) {\n\t\t\tif (!(await exists(dir))) continue;\n\t\t\tconst files = (await readdir(dir)).filter((file) => file.endsWith(\".jsonl\")).map((file) => join(dir, file));\n\t\t\tfor (const filePath of files) {\n\t\t\t\ttry {\n\t\t\t\t\tsessions.push(await loadJsonlSessionMetadata(filePath));\n\t\t\t\t} catch {\n\t\t\t\t\t// Ignore invalid session files when listing a directory.\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsessions.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());\n\t\treturn sessions;\n\t}\n\n\tasync delete(metadata: JsonlSessionMetadata): Promise<void> {\n\t\tawait rm(metadata.path, { force: true });\n\t}\n\n\tasync fork(\n\t\tsourceMetadata: JsonlSessionMetadata,\n\t\toptions: JsonlSessionCreateOptions & { entryId?: string; position?: \"before\" | \"at\"; id?: string },\n\t): Promise<Session<JsonlSessionMetadata>> {\n\t\tconst source = await this.open(sourceMetadata);\n\t\tconst forkedEntries = await getEntriesToFork(source.getStorage(), options);\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst storage = await JsonlSessionStorage.create(this.createSessionFilePath(options.cwd, id, createdAt), {\n\t\t\tcwd: options.cwd,\n\t\t\tsessionId: id,\n\t\t\tparentSessionPath: options.parentSessionPath ?? sourceMetadata.path,\n\t\t});\n\t\tfor (const entry of forkedEntries) {\n\t\t\tawait storage.appendEntry(entry);\n\t\t}\n\t\treturn toSession(storage);\n\t}\n\n\tprivate async listSessionDirs(): Promise<string[]> {\n\t\tif (!(await exists(this.sessionsRoot))) return [];\n\t\tconst entries = await readdir(this.sessionsRoot, { withFileTypes: true });\n\t\treturn entries.filter((entry) => entry.isDirectory()).map((entry) => join(this.sessionsRoot, entry.name));\n\t}\n}\n"]}