MCPcopy Create free account
hub / github.com/cortexkit/magic-context / migrateOpenCodeSessionToPi

Function migrateOpenCodeSessionToPi

packages/cli/src/commands/migrate.ts:894–1028  ·  view source on GitHub ↗
(
    opts: MigrateOpenCodeSessionToPiOptions,
)

Source from the content-addressed store, hash-verified

892}
893
894export function migrateOpenCodeSessionToPi(
895 opts: MigrateOpenCodeSessionToPiOptions,
896): MigrationResult {
897 const fs = opts.fs ?? defaultFs();
898 const now = opts.now ?? new Date();
899 const opencodeDbPath = opts.opencodeDbPath ?? defaultOpenCodeDbPath();
900 const piSessionsRoot = opts.piSessionsRoot ?? defaultPiSessionsRoot();
901 const ownsDb = !opts.db;
902 const db = opts.db ?? new Database(opencodeDbPath, { readonly: true });
903
904 // Cortexkit DB: when not provided explicitly, open the canonical
905 // shared DB read-write (we'll INSERT into compartments + session_facts).
906 // Pass null to skip the cortexkit copy entirely (legacy V1 behavior).
907 let cortexkitDb: DatabaseLike | null;
908 let ownsCortexkitDb = false;
909 if (opts.cortexkitDb === null) {
910 cortexkitDb = null;
911 } else if (opts.cortexkitDb !== undefined) {
912 cortexkitDb = opts.cortexkitDb;
913 } else {
914 try {
915 cortexkitDb = new Database(defaultCortexkitDbPath());
916 ownsCortexkitDb = true;
917 } catch {
918 // If the cortexkit DB doesn't exist yet (Magic Context never
919 // loaded on this machine), skip the copy gracefully — the
920 // migration still produces a usable Pi JSONL.
921 cortexkitDb = null;
922 }
923 }
924
925 try {
926 const { session, sourceMessageCount, messages, parts } = fetchRows(
927 db,
928 opts.sessionId,
929 opts.maxMessages,
930 );
931 const model = extractModel(messages);
932 const provider = opts.provider ?? model.provider;
933 const modelId = opts.modelId ?? model.modelId;
934 const cwd = session.directory ?? session.path ?? process.cwd();
935 const outputDir = join(piSessionsRoot, projectPathToPiDirSlug(cwd));
936 const buildResult = buildPiEntries({
937 session,
938 messages,
939 parts,
940 now,
941 provider,
942 modelId,
943 });
944 // Copy magic-context durable state (compartments + facts) to the
945 // new Pi session_id when the cortexkit DB is reachable.
946 let copyResult: CopyMagicContextStateResult = {
947 compartmentsCopied: 0,
948 factsCopied: 0,
949 boundariesApproximated: 0,
950 };
951 let commitMagicContextState: (() => void) | null = null;

Callers 2

migrate.test.tsFile · 0.90
runMigrateCliFunction · 0.85

Calls 12

defaultFsFunction · 0.85
defaultPiSessionsRootFunction · 0.85
defaultCortexkitDbPathFunction · 0.85
fetchRowsFunction · 0.85
extractModelFunction · 0.85
projectPathToPiDirSlugFunction · 0.85
buildPiEntriesFunction · 0.85
copyMagicContextStateFunction · 0.85
insertCompactionMarkerFunction · 0.85
defaultOpenCodeDbPathFunction · 0.70
closeMethod · 0.45

Tested by

no test coverage detected