UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

34 lines (33 loc) • 1.88 kB
import { fileHistoryScopeOf } from "../../engine/session/repo-utils.js"; import { forkHasOwnTree, sessionRootOf } from "../session-checkout.js"; import { entryInOwnLog } from "./file-history-restore.js"; import { resolveFileHistoryCoordinates } from "./prepare-file-history.js"; export async function openSessionFiles(input) { const { session, sessionId, target, history } = input; if (input.tree !== undefined && input.tree.hands === undefined) return { kind: "no_hands" }; const tree = input.tree !== undefined && input.tree.hands !== undefined ? { ...input.tree.hands, refusedAs: input.tree.refusedAs, sessions: input.tree.sessions } : undefined; if (target !== undefined && !(await entryInOwnLog(session, target))) return { kind: "target_outside_log" }; let read; if (history !== undefined) { let row; try { row = await session.getMetadata(); } catch { throw history.unaddressed(); } read = { row, ownScope: fileHistoryScopeOf(sessionId, row.incarnation) }; } const sessionRoot = tree !== undefined ? await sessionRootOf({ env: tree.env, session, sessionId, baseRoot: tree.baseRoot, sessions: tree.sessions }, tree.refusedAs) : undefined; if (read === undefined) return { kind: "opened", sessionRoot, history: undefined }; const { row, ownScope } = read; if (await forkHasOwnTree(session, sessionId, row.forkedFrom)) return { kind: "fork_with_own_tree" }; if (tree === undefined) return { kind: "opened", sessionRoot, history: { row, ownScope, historyScope: ownScope, historyRoot: "" } }; const { historyRoot, historyScope } = await resolveFileHistoryCoordinates(tree.env, sessionRoot, undefined, ownScope); return { kind: "opened", sessionRoot, history: { row, ownScope, historyScope, historyRoot } }; }