// Migrate Claude provider module implements model/runtime integration. import fs from "node:fs/promises"; import path from "node:path"; import type { MigrationProviderContext } from "openclaw/plugin-sdk/plugin-entry"; import type { OpenClawConfig } from "openclaw/plugin-sdk/provider-auth"; import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path"; const tempRoots = new Set(); const logger = { info() {}, warn() {}, error() {}, debug() {}, }; export async function makeTempRoot() { const root = await fs.mkdtemp( path.join(resolvePreferredOpenClawTmpDir(), "openclaw-migrate-claude-"), ); tempRoots.add(root); return root; } export async function cleanupTempRoots() { for (const root of tempRoots) { await fs.rm(root, { force: true, recursive: true }); } tempRoots.clear(); } export async function writeFile(filePath: string, content: string) { await fs.mkdir(path.dirname(filePath), { recursive: true }); await fs.writeFile(filePath, content, "utf8"); } export function makeConfigRuntime( config: OpenClawConfig, onWrite?: (next: OpenClawConfig) => void, ): NonNullable { const commitConfig = (next: OpenClawConfig) => { for (const key of Object.keys(config) as Array) { delete config[key]; } Object.assign(config, next); onWrite?.(next); }; return { config: { current: () => config, mutateConfigFile: async ({ afterWrite, mutate, }: { afterWrite?: unknown; mutate: (draft: OpenClawConfig, context: unknown) => Promise | void; }) => { const next = structuredClone(config); const result = await mutate(next, { snapshot: { path: "/tmp/openclaw.json", exists: true, raw: "{}", parsed: {}, valid: true, issues: [], warnings: [], legacyIssues: [], config: next, resolved: next, runtimeConfig: next, sourceConfig: next, }, previousHash: "test", }); commitConfig(next); return { nextConfig: next, afterWrite, followUp: { mode: "auto", requiresRestart: false }, result, }; }, replaceConfigFile: async ({ afterWrite, nextConfig, }: { afterWrite?: unknown; nextConfig: OpenClawConfig; }) => { commitConfig(nextConfig); return { nextConfig, afterWrite, followUp: { mode: "auto", requiresRestart: false }, }; }, }, } as NonNullable; } export function makeContext(params: { source: string; stateDir: string; workspaceDir: string; config?: OpenClawConfig; includeSecrets?: boolean; overwrite?: boolean; reportDir?: string; runtime?: MigrationProviderContext["runtime"]; }): MigrationProviderContext { const config = params.config ?? ({ agents: { defaults: { workspace: params.workspaceDir, }, }, } as OpenClawConfig); return { config, stateDir: params.stateDir, source: params.source, includeSecrets: params.includeSecrets, overwrite: params.overwrite, reportDir: params.reportDir, runtime: params.runtime, logger, }; }