File size: 6,664 Bytes
d522ffa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import type {
MigrationPlan,
MigrationReport,
MigrationScope,
SessionsSummary,
} from './types.js';
import { join } from 'node:path';
import { migrateConfigStep } from './steps/config.js';
import { migrateMcpStep } from './steps/mcp.js';
import { migrateUserHistoryStep } from './steps/user-history.js';
import { migrateSkillsStep } from './steps/skills.js';
import { migratePlansStep } from './steps/plans.js';
import { migrateSessionsStep } from './sessions/index.js';
import { writeReport } from './report.js';
import { writeMigrationErrorsLog } from './migration-errors-log.js';
import { appendMarkerRun, readMarker, writeMarker } from './marker.js';
const DEFAULT_MIGRATOR_VERSION = '0.1.1';
const CONFIG_CONFLICT_NOTICE =
'Your existing config.toml could not be parsed; migrated copy saved to ~/.kimi-code/config.migrated-from-kimi-cli.toml — please review and merge manually.';
const TUI_CONFLICT_NOTICE =
'Your existing tui.toml had user modifications; migrated copy saved to ~/.kimi-code/tui.migrated-from-kimi-cli.toml — please review and merge manually.';
export interface RunMigrationInput {
readonly plan: MigrationPlan;
readonly scope: MigrationScope;
readonly source: string;
readonly target: string;
/** Legacy plans dir; defaults to `~/.kimi/plans` per kimi-cli's hardcode. */
readonly plansSourceDir?: string;
readonly migratorVersion?: string;
readonly onProgress?: (msg: string) => void;
readonly onSessionProgress?: (done: number, total: number) => void;
}
export async function runMigration(input: RunMigrationInput): Promise<MigrationReport> {
const startedAt = new Date().toISOString();
const version = input.migratorVersion ?? DEFAULT_MIGRATOR_VERSION;
const log = (m: string): void => {
input.onProgress?.(m);
};
const config = input.scope.config
? await migrateConfigStep({ sourceHome: input.source, targetHome: input.target })
: {
migrated: false,
tuiExtracted: false,
droppedProviders: [],
droppedModels: [],
droppedKeys: [],
configConflicts: [],
wroteSiblingDueToConflict: false,
wroteTuiSibling: false,
migratedHooks: 0,
droppedHooks: 0,
sourceUnreadable: false,
deviceIdCopied: false,
siblingContents: { providers: [], models: [], hooks: 0 },
};
log('config done');
const mcp = input.scope.mcp
? await migrateMcpStep({ sourceHome: input.source, targetHome: input.target })
: { mergedServers: [], keptNewForConflicts: [], droppedServers: [], wroteSiblingDueToConflict: false, sourceUnreadable: false };
log('mcp done');
const userHistory = input.scope.userHistory
? await migrateUserHistoryStep({ sourceHome: input.source, targetHome: input.target })
: { copied: 0, skippedExisting: 0 };
log('user-history done');
const skills = input.scope.skills
? await migrateSkillsStep({
sourceHome: input.plan.skillsSourceHome ?? input.source,
targetHome: input.target,
})
: { copied: 0, skippedExisting: 0 };
log('skills done');
const sessions: SessionsSummary = input.scope.sessions
? await migrateSessionsStep({
sourceHome: input.source,
targetHome: input.target,
onSessionProgress: input.onSessionProgress,
})
: emptyConfigOnlySessions();
log('sessions done');
const plans = input.scope.sessions
? await migratePlansStep({ targetHome: input.target, plansSourceDir: input.plansSourceDir })
: { copied: 0, skippedExisting: 0 };
log('plans done');
const completedAt = new Date().toISOString();
const plansCopiedNotice =
plans.copied > 0
? `${plans.copied} plan file(s) copied to ${join(input.target, 'plans')} — plain copies, not wired into plan mode; reuse or delete them as you see fit.`
: null;
const report: MigrationReport = {
startedAt,
completedAt,
migratorVersion: version,
source: input.source,
target: input.target,
summary: {
config,
mcp,
userHistory,
skills,
plans,
sessions,
},
notices: {
mcpOauthServersRequiringReauth: input.plan.detectedMcpOauthServers,
oauthLoginsRequiringRelogin: input.plan.oauthCredentials,
detectedPlugins: input.plan.detectedPlugins,
configConflictNotice: config.wroteSiblingDueToConflict ? CONFIG_CONFLICT_NOTICE : null,
tuiConflictNotice: config.wroteTuiSibling ? TUI_CONFLICT_NOTICE : null,
plansCopiedNotice,
},
};
await writeReport(input.target, report);
// Append this run's outcome (per-session diagnostics on failure, a marker
// on success) to `migration-errors.log` — append-only cross-run record so a
// user can share one log covering every retry attempt.
await writeMigrationErrorsLog(input.target, {
startedAt,
failures: sessions.sessionsFailed,
});
const markerSummary: Record<string, unknown> = {
config,
mcp,
userHistory,
skills,
sessions,
};
// Do not suppress a later retry while anything in the chosen scope remains
// unresolved: unmigrated/uninspectable sessions, target conflicts, or a
// legacy config/MCP file that exists but could not be parsed. Successful
// marker persistence remains best-effort: the data and report are already
// complete, so a marker write failure must not reject.
const scopeComplete =
sessions.sessionsFailed.length === 0 &&
sessions.sessionsConflicts.length === 0 &&
!(input.scope.config && config.sourceUnreadable) &&
!(input.scope.mcp && mcp.sourceUnreadable);
if (scopeComplete) {
try {
const existingMarker = await readMarker(input.source);
if (existingMarker === undefined) {
await writeMarker(input.source, {
startedAt,
completedAt,
migratorVersion: version,
summary: markerSummary,
targetPath: input.target,
});
} else {
await appendMarkerRun(input.source, {
startedAt,
completedAt,
migratorVersion: version,
summary: markerSummary,
targetPath: input.target,
});
}
} catch {
// best-effort — see comment above
}
}
return report;
}
function emptyConfigOnlySessions(): SessionsSummary {
return {
scope: 'config-only',
bucketsScanned: 0,
bucketsSkippedNonlocalKaos: 0,
bucketsSkippedNoWorkdirFound: 0,
sessionsAttempted: 0,
sessionsMigrated: 0,
sessionsAlreadyMigrated: 0,
sessionsSkippedPlaceholder: 0,
sessionsSkippedEmpty: 0,
sessionsSkippedMalformed: 0,
sessionsFailed: [],
sessionsConflicts: [],
};
}
|