refactor(index): use migration module from shared

Removes inline migration logic from index.ts and imports from shared/migration module.
This completes the refactoring to extract testable migration logic into a dedicated module.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2025-12-31 12:41:22 +09:00
parent ffeb92eb13
commit 2f1ede072f

View File

@@ -61,103 +61,11 @@ import { builtinTools, createCallOmoAgent, createBackgroundTools, createLookAt,
import { BackgroundManager } from "./features/background-agent"; import { BackgroundManager } from "./features/background-agent";
import { createBuiltinMcps } from "./mcp"; import { createBuiltinMcps } from "./mcp";
import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig, type HookName } from "./config"; import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig, type HookName } from "./config";
import { log, deepMerge, getUserConfigDir, addConfigLoadError, parseJsonc, detectConfigFile } from "./shared"; import { log, deepMerge, getUserConfigDir, addConfigLoadError, parseJsonc, detectConfigFile, migrateConfigFile } from "./shared";
import { PLAN_SYSTEM_PROMPT, PLAN_PERMISSION } from "./agents/plan-prompt"; import { PLAN_SYSTEM_PROMPT, PLAN_PERMISSION } from "./agents/plan-prompt";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
// Migration map: old keys → new keys (for backward compatibility)
const AGENT_NAME_MAP: Record<string, string> = {
// Legacy names (backward compatibility)
omo: "Sisyphus",
"OmO": "Sisyphus",
"OmO-Plan": "Planner-Sisyphus",
"omo-plan": "Planner-Sisyphus",
// Current names
sisyphus: "Sisyphus",
"planner-sisyphus": "Planner-Sisyphus",
build: "build",
oracle: "oracle",
librarian: "librarian",
explore: "explore",
"frontend-ui-ux-engineer": "frontend-ui-ux-engineer",
"document-writer": "document-writer",
"multimodal-looker": "multimodal-looker",
};
// Migration map: old hook names → new hook names (for backward compatibility)
const HOOK_NAME_MAP: Record<string, string> = {
// Legacy names (backward compatibility)
"anthropic-auto-compact": "anthropic-context-window-limit-recovery",
};
function migrateAgentNames(agents: Record<string, unknown>): { migrated: Record<string, unknown>; changed: boolean } {
const migrated: Record<string, unknown> = {};
let changed = false;
for (const [key, value] of Object.entries(agents)) {
const newKey = AGENT_NAME_MAP[key.toLowerCase()] ?? AGENT_NAME_MAP[key] ?? key;
if (newKey !== key) {
changed = true;
}
migrated[newKey] = value;
}
return { migrated, changed };
}
function migrateHookNames(hooks: string[]): { migrated: string[]; changed: boolean } {
const migrated: string[] = [];
let changed = false;
for (const hook of hooks) {
const newHook = HOOK_NAME_MAP[hook] ?? hook;
if (newHook !== hook) {
changed = true;
}
migrated.push(newHook);
}
return { migrated, changed };
}
function migrateConfigFile(configPath: string, rawConfig: Record<string, unknown>): boolean {
let needsWrite = false;
if (rawConfig.agents && typeof rawConfig.agents === "object") {
const { migrated, changed } = migrateAgentNames(rawConfig.agents as Record<string, unknown>);
if (changed) {
rawConfig.agents = migrated;
needsWrite = true;
}
}
if (rawConfig.omo_agent) {
rawConfig.sisyphus_agent = rawConfig.omo_agent;
delete rawConfig.omo_agent;
needsWrite = true;
}
if (rawConfig.disabled_hooks && Array.isArray(rawConfig.disabled_hooks)) {
const { migrated, changed } = migrateHookNames(rawConfig.disabled_hooks as string[]);
if (changed) {
rawConfig.disabled_hooks = migrated;
needsWrite = true;
}
}
if (needsWrite) {
try {
fs.writeFileSync(configPath, JSON.stringify(rawConfig, null, 2) + "\n", "utf-8");
log(`Migrated config file: ${configPath}`);
} catch (err) {
log(`Failed to write migrated config to ${configPath}:`, err);
}
}
return needsWrite;
}
function loadConfigFromPath(configPath: string, ctx: any): OhMyOpenCodeConfig | null { function loadConfigFromPath(configPath: string, ctx: any): OhMyOpenCodeConfig | null {
try { try {
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {