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AAE,GAChG,OAAO,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAuBxC;YAEa,eAAe;CAgB7B","sourcesContent":["import type {\n\tFileSystem,\n\tJsonlSessionCreateOptions,\n\tJsonlSessionListOptions,\n\tJsonlSessionMetadata,\n\tJsonlSessionRepoApi,\n\tSession,\n} from \"../types.ts\";\nimport { SessionError, toError } from \"../types.ts\";\nimport { JsonlSessionStorage, loadJsonlSessionMetadata } from \"./jsonl-storage.ts\";\nimport {\n\tcreateSessionId,\n\tcreateTimestamp,\n\tgetEntriesToFork,\n\tgetFileSystemResultOrThrow,\n\ttoSession,\n} from \"./repo-utils.ts\";\n\ntype JsonlSessionRepoFileSystem = Pick<\n\tFileSystem,\n\t| \"cwd\"\n\t| \"absolutePath\"\n\t| \"joinPath\"\n\t| \"readTextFile\"\n\t| \"readTextLines\"\n\t| \"writeFile\"\n\t| \"appendFile\"\n\t| \"listDir\"\n\t| \"exists\"\n\t| \"createDir\"\n\t| \"remove\"\n>;\n\nfunction encodeCwd(cwd: string): string {\n\treturn `--${cwd.replace(/^[/\\\\]/, \"\").replace(/[/\\\\:]/g, \"-\")}--`;\n}\n\nexport class JsonlSessionRepo implements JsonlSessionRepoApi {\n\tprivate readonly fs: JsonlSessionRepoFileSystem;\n\tprivate readonly sessionsRootInput: string;\n\tprivate sessionsRoot: string | undefined;\n\n\tconstructor(options: { fs: JsonlSessionRepoFileSystem; sessionsRoot: string }) {\n\t\tthis.fs = options.fs;\n\t\tthis.sessionsRootInput = options.sessionsRoot;\n\t}\n\n\tprivate async getSessionsRoot(): Promise<string> {\n\t\tif (!this.sessionsRoot) {\n\t\t\tthis.sessionsRoot = getFileSystemResultOrThrow(\n\t\t\t\tawait this.fs.absolutePath(this.sessionsRootInput),\n\t\t\t\t`Failed to resolve sessions root ${this.sessionsRootInput}`,\n\t\t\t);\n\t\t}\n\t\treturn this.sessionsRoot;\n\t}\n\n\tprivate async getSessionDir(cwd: string): Promise<string> {\n\t\treturn getFileSystemResultOrThrow(\n\t\t\tawait this.fs.joinPath([await this.getSessionsRoot(), encodeCwd(cwd)]),\n\t\t\t`Failed to resolve session directory for ${cwd}`,\n\t\t);\n\t}\n\n\tprivate async createSessionFilePath(cwd: string, sessionId: string, timestamp: string): Promise<string> {\n\t\treturn getFileSystemResultOrThrow(\n\t\t\tawait this.fs.joinPath([\n\t\t\t\tawait this.getSessionDir(cwd),\n\t\t\t\t`${timestamp.replace(/[:.]/g, \"-\")}_${sessionId}.jsonl`,\n\t\t\t]),\n\t\t\t`Failed to resolve session file path for ${sessionId}`,\n\t\t);\n\t}\n\n\tasync create(options: JsonlSessionCreateOptions): Promise<Session<JsonlSessionMetadata>> {\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst sessionDir = await this.getSessionDir(options.cwd);\n\t\tgetFileSystemResultOrThrow(\n\t\t\tawait this.fs.createDir(sessionDir, { recursive: true }),\n\t\t\t`Failed to create session directory ${sessionDir}`,\n\t\t);\n\t\tconst filePath = await this.createSessionFilePath(options.cwd, id, createdAt);\n\t\tconst storage = await JsonlSessionStorage.create(this.fs, 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 (\n\t\t\t!getFileSystemResultOrThrow(await this.fs.exists(metadata.path), `Failed to check session ${metadata.path}`)\n\t\t) {\n\t\t\tthrow new SessionError(\"not_found\", `Session not found: ${metadata.path}`);\n\t\t}\n\t\tconst storage = await JsonlSessionStorage.open(this.fs, metadata.path);\n\t\treturn toSession(storage);\n\t}\n\n\tasync list(options: JsonlSessionListOptions = {}): Promise<JsonlSessionMetadata[]> {\n\t\tconst dirs = options.cwd ? [await this.getSessionDir(options.cwd)] : await this.listSessionDirs();\n\t\tconst sessions: JsonlSessionMetadata[] = [];\n\t\tfor (const dir of dirs) {\n\t\t\tif (!getFileSystemResultOrThrow(await this.fs.exists(dir), `Failed to check session directory ${dir}`)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst files = getFileSystemResultOrThrow(\n\t\t\t\tawait this.fs.listDir(dir),\n\t\t\t\t`Failed to list sessions in ${dir}`,\n\t\t\t).filter((file) => file.kind !== \"directory\" && file.name.endsWith(\".jsonl\"));\n\t\t\tfor (const file of files) {\n\t\t\t\ttry {\n\t\t\t\t\tsessions.push(await loadJsonlSessionMetadata(this.fs, file.path));\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst cause = toError(error);\n\t\t\t\t\tif (!(cause instanceof SessionError) || cause.code !== \"invalid_session\") throw cause;\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\tgetFileSystemResultOrThrow(\n\t\t\tawait this.fs.remove(metadata.path, { force: true }),\n\t\t\t`Failed to delete session ${metadata.path}`,\n\t\t);\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 sessionDir = await this.getSessionDir(options.cwd);\n\t\tgetFileSystemResultOrThrow(\n\t\t\tawait this.fs.createDir(sessionDir, { recursive: true }),\n\t\t\t`Failed to create session directory ${sessionDir}`,\n\t\t);\n\t\tconst storage = await JsonlSessionStorage.create(\n\t\t\tthis.fs,\n\t\t\tawait this.createSessionFilePath(options.cwd, id, createdAt),\n\t\t\t{\n\t\t\t\tcwd: options.cwd,\n\t\t\t\tsessionId: id,\n\t\t\t\tparentSessionPath: options.parentSessionPath ?? sourceMetadata.path,\n\t\t\t},\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\tconst sessionsRoot = await this.getSessionsRoot();\n\t\tif (\n\t\t\t!getFileSystemResultOrThrow(\n\t\t\t\tawait this.fs.exists(sessionsRoot),\n\t\t\t\t`Failed to check sessions root ${sessionsRoot}`,\n\t\t\t)\n\t\t) {\n\t\t\treturn [];\n\t\t}\n\t\tconst entries = getFileSystemResultOrThrow(\n\t\t\tawait this.fs.listDir(sessionsRoot),\n\t\t\t`Failed to list sessions root ${sessionsRoot}`,\n\t\t);\n\t\treturn entries.filter((entry) => entry.kind === \"directory\").map((entry) => entry.path);\n\t}\n}\n"]}