f11641d5a8
- Implemented session stores and metadata converters to import Claude and Codex sessions into OMP. - Added `--from-claude` and `--from-codex` CLI flags and `/resume` command arguments for foreign session resolution. - Updated session selector components and controllers to support listing and picking external agent sessions. - Added comprehensive unit tests and documentation covering foreign session import functionality.
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import { isRecord, readLines } from "@oh-my-pi/pi-utils";
|
|
|
|
/** One readable object record from a foreign JSONL transcript. */
|
|
export interface ForeignJsonRecord {
|
|
readonly value: Record<string, unknown>;
|
|
readonly line: number;
|
|
}
|
|
|
|
/** Stream valid object records while tolerating malformed or truncated lines. */
|
|
export async function* readForeignJsonRecords(filePath: string): AsyncGenerator<ForeignJsonRecord> {
|
|
const decoder = new TextDecoder();
|
|
let line = 0;
|
|
for await (const bytes of readLines(Bun.file(filePath).stream())) {
|
|
line += 1;
|
|
try {
|
|
const value: unknown = JSON.parse(decoder.decode(bytes));
|
|
if (isRecord(value)) yield { value, line };
|
|
} catch {
|
|
// A partially-written line must not hide the readable transcript prefix.
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Read every valid object record from a foreign JSONL transcript. */
|
|
export async function collectForeignJsonRecords(filePath: string): Promise<ForeignJsonRecord[]> {
|
|
const records: ForeignJsonRecord[] = [];
|
|
for await (const record of readForeignJsonRecords(filePath)) records.push(record);
|
|
return records;
|
|
}
|