refactor(agent): isolate node filesystem session dependencies

This commit is contained in:
Mario Zechner
2026-05-15 00:53:33 +02:00
parent 0b54c87e24
commit 80c918c247
25 changed files with 340 additions and 257 deletions
@@ -0,0 +1,36 @@
import type { SessionMetadata, SessionStorage, SessionTreeEntry } from "../types.js";
import { Session } from "./session.js";
import { uuidv7 } from "./uuid.js";
export function createSessionId(): string {
return uuidv7();
}
export function createTimestamp(): string {
return new Date().toISOString();
}
export function toSession<TMetadata extends SessionMetadata>(storage: SessionStorage<TMetadata>): Session<TMetadata> {
return new Session(storage);
}
export async function getEntriesToFork(
storage: SessionStorage,
options: { entryId?: string; position?: "before" | "at" },
): Promise<SessionTreeEntry[]> {
if (!options.entryId) return storage.getEntries();
const target = await storage.getEntry(options.entryId);
if (!target) {
throw new Error(`Entry ${options.entryId} not found`);
}
let effectiveLeafId: string | null;
if ((options.position ?? "before") === "at") {
effectiveLeafId = target.id;
} else {
if (target.type !== "message" || target.message.role !== "user") {
throw new Error(`Entry ${options.entryId} is not a user message`);
}
effectiveLeafId = target.parentId;
}
return storage.getPathToRoot(effectiveLeafId);
}