Files
openclaw/src/commands/migrate.ts
Kevin Lin a1ac559ed7 feat(codex): enable native plugin app support (#78733)
* feat(codex): add native plugin config schema

* feat(codex): add native plugin inventory activation

* feat(codex): configure native plugin apps for threads

* feat(codex): enforce plugin elicitation policy

* feat(codex): migrate native plugins

* docs(codex): document native plugin support

* fix(codex): harden plugin migration refresh

* fix(codex): satisfy plugin activation lint

* fix: stabilize codex plugin app config

* fix: address codex plugin review feedback

* fix: key codex plugin app cache by websocket credentials

* fix: keep codex plugin app fingerprints stable

* fix: refresh codex plugin cache test fixtures

* fix: refresh plugin app readiness after activation

* fix: support remote codex plugin activation

* fix: recover plugin app bindings after cache refresh

* fix: force codex app refresh after plugin activation

* fix: recover partial codex plugin app bindings

* fix: sync codex plugin selection config

* fix: keep codex plugin activation fail closed

* fix: align codex plugin protocol types with main

* fix: refresh partial codex plugin app bindings

* fix: key codex app cache by env api key

* fix: skip failed codex plugin migration config

* test: update codex prompt snapshots

* fix: fail closed on missing codex app inventory entries

* fix(codex): enforce native plugin policy gates

* fix(codex): normalize native plugin policy types

* fix(codex): fail closed on plugin refresh errors

* fix(codex): use native plugin destructive policy

* fix(codex): key plugin cache by api-key profiles

* fix(codex): drop unshipped plugin fingerprint compat

* fix(codex): let native app policy gate plugin tools

* fix(codex): allow open-world plugin app tools

* fix(codex): revalidate native plugin app bindings

* fix(codex): preserve plugin binding on recheck failure

* docs(codex): clarify plugin harness scope

* fix(codex): return activation report state exhaustively

* test(codex): refresh prompt snapshots after rebase

* fix(codex): match namespaced plugin ids
2026-05-07 17:20:28 -07:00

276 lines
8.4 KiB
TypeScript

import { cancel, isCancel } from "@clack/prompts";
import { promptYesNo } from "../cli/prompt.js";
import { getRuntimeConfig } from "../config/config.js";
import { redactMigrationPlan } from "../plugin-sdk/migration.js";
import {
ensureStandaloneMigrationProviderRegistryLoaded,
resolvePluginMigrationProviders,
} from "../plugins/migration-provider-runtime.js";
import type { MigrationApplyResult, MigrationPlan } from "../plugins/types.js";
import type { RuntimeEnv } from "../runtime.js";
import { writeRuntimeJson } from "../runtime.js";
import { stylePromptHint, stylePromptMessage, stylePromptTitle } from "../terminal/prompt-style.js";
import { runMigrationApply } from "./migrate/apply.js";
import { formatMigrationPlan } from "./migrate/output.js";
import { createMigrationPlan, resolveMigrationProvider } from "./migrate/providers.js";
import {
applyMigrationPluginSelection,
applyMigrationSelectedSkillItemIds,
applyMigrationSkillSelection,
formatMigrationSkillSelectionHint,
formatMigrationSkillSelectionLabel,
getDefaultMigrationSkillSelectionValues,
getMigrationSkillSelectionValue,
getSelectableMigrationSkillItems,
MIGRATION_SKILL_SELECTION_SKIP,
MIGRATION_SKILL_SELECTION_TOGGLE_ALL_OFF,
MIGRATION_SKILL_SELECTION_TOGGLE_ALL_ON,
resolveInteractiveMigrationSkillSelection,
} from "./migrate/selection.js";
import { promptMigrationSkillSelectionValues } from "./migrate/skill-selection-prompt.js";
import type {
MigrateApplyOptions,
MigrateCommonOptions,
MigrateDefaultOptions,
} from "./migrate/types.js";
export type { MigrateApplyOptions, MigrateCommonOptions, MigrateDefaultOptions };
function selectMigrationItems(plan: MigrationPlan, opts: MigrateCommonOptions): MigrationPlan {
return applyMigrationPluginSelection(
applyMigrationSkillSelection(plan, opts.skills),
opts.plugins,
);
}
async function promptCodexMigrationSkillSelection(
runtime: RuntimeEnv,
plan: MigrationPlan,
opts: MigrateCommonOptions & { yes?: boolean },
): Promise<MigrationPlan | null> {
if (
plan.providerId !== "codex" ||
opts.yes ||
opts.json ||
opts.skills !== undefined ||
!process.stdin.isTTY
) {
return plan;
}
const skillItems = getSelectableMigrationSkillItems(plan);
if (skillItems.length === 0) {
return plan;
}
const selected = await promptMigrationSkillSelectionValues({
message: stylePromptMessage("Select Codex skills to migrate into this agent"),
options: [
{
value: MIGRATION_SKILL_SELECTION_SKIP,
label: "Skip for now",
},
{
value: MIGRATION_SKILL_SELECTION_TOGGLE_ALL_ON,
label: "Toggle all on",
},
{
value: MIGRATION_SKILL_SELECTION_TOGGLE_ALL_OFF,
label: "Toggle all off",
},
...skillItems.map((item) => {
const hint = formatMigrationSkillSelectionHint(item);
return {
value: getMigrationSkillSelectionValue(item),
label: formatMigrationSkillSelectionLabel(item),
hint: hint === undefined ? undefined : stylePromptHint(hint),
};
}),
],
initialValues: getDefaultMigrationSkillSelectionValues(skillItems),
required: false,
selectableValues: skillItems.map(getMigrationSkillSelectionValue),
});
if (isCancel(selected)) {
cancel(stylePromptTitle("Migration cancelled.") ?? "Migration cancelled.");
runtime.log("Migration cancelled.");
return null;
}
const selection = resolveInteractiveMigrationSkillSelection(skillItems, selected ?? []);
if (selection.action === "skip") {
runtime.log("Codex skill migration skipped for now.");
return null;
}
const selectedPlan = applyMigrationSelectedSkillItemIds(plan, selection.selectedItemIds);
runtime.log(
`Selected ${selection.selectedItemIds.size} of ${skillItems.length} Codex skills for migration.`,
);
return selectedPlan;
}
export async function migrateListCommand(runtime: RuntimeEnv, opts: { json?: boolean } = {}) {
const cfg = getRuntimeConfig();
ensureStandaloneMigrationProviderRegistryLoaded({ cfg });
const providers = resolvePluginMigrationProviders({ cfg }).map((provider) => ({
id: provider.id,
label: provider.label,
description: provider.description,
}));
if (opts.json) {
writeRuntimeJson(runtime, { providers });
return;
}
if (providers.length === 0) {
runtime.log("No migration providers found.");
return;
}
runtime.log(
providers
.map((provider) =>
provider.description
? `${provider.id}\t${provider.label} - ${provider.description}`
: `${provider.id}\t${provider.label}`,
)
.join("\n"),
);
}
export async function migratePlanCommand(
runtime: RuntimeEnv,
opts: MigrateCommonOptions,
): Promise<MigrationPlan> {
const providerId = opts.provider?.trim();
if (!providerId) {
throw new Error("Migration provider is required.");
}
const plan = selectMigrationItems(
await createMigrationPlan(runtime, { ...opts, provider: providerId }),
opts,
);
if (opts.json) {
writeRuntimeJson(runtime, redactMigrationPlan(plan));
} else {
runtime.log(formatMigrationPlan(plan).join("\n"));
}
return plan;
}
export async function migrateApplyCommand(
runtime: RuntimeEnv,
opts: MigrateApplyOptions & { yes: true },
): Promise<MigrationApplyResult>;
export async function migrateApplyCommand(
runtime: RuntimeEnv,
opts: MigrateApplyOptions,
): Promise<MigrationApplyResult | MigrationPlan>;
export async function migrateApplyCommand(
runtime: RuntimeEnv,
opts: MigrateApplyOptions,
): Promise<MigrationApplyResult | MigrationPlan> {
const providerId = opts.provider?.trim();
if (!providerId) {
throw new Error("Migration provider is required.");
}
if (opts.noBackup && !opts.force) {
throw new Error("--no-backup requires --force.");
}
if (!opts.yes && !process.stdin.isTTY) {
throw new Error("openclaw migrate apply requires --yes in non-interactive mode.");
}
const provider = resolveMigrationProvider(providerId);
if (!opts.yes) {
const plan = await migratePlanCommand(runtime, {
...opts,
provider: providerId,
json: opts.json,
});
if (opts.json) {
return plan;
}
const selectedPlan = await promptCodexMigrationSkillSelection(runtime, plan, opts);
if (!selectedPlan) {
return plan;
}
const ok = await promptYesNo("Apply this migration now?", false);
if (!ok) {
runtime.log("Migration cancelled.");
return selectedPlan;
}
return await runMigrationApply({
runtime,
opts: { ...opts, provider: providerId, yes: true, preflightPlan: selectedPlan },
providerId,
provider,
});
}
return await runMigrationApply({ runtime, opts, providerId, provider });
}
export async function migrateDefaultCommand(
runtime: RuntimeEnv,
opts: MigrateDefaultOptions,
): Promise<MigrationPlan | MigrationApplyResult> {
const providerId = opts.provider?.trim();
if (!providerId) {
await migrateListCommand(runtime, { json: opts.json });
return {
providerId: "list",
source: "",
summary: {
total: 0,
planned: 0,
migrated: 0,
skipped: 0,
conflicts: 0,
errors: 0,
sensitive: 0,
},
items: [],
};
}
const plan =
opts.json && opts.yes && !opts.dryRun
? selectMigrationItems(
await createMigrationPlan(runtime, { ...opts, provider: providerId }),
opts,
)
: await migratePlanCommand(runtime, {
...opts,
provider: providerId,
json: opts.json && (opts.dryRun || !opts.yes),
});
if (opts.dryRun) {
return plan;
}
if (opts.json && !opts.yes) {
return plan;
}
if (!opts.yes) {
if (!process.stdin.isTTY) {
runtime.log("Re-run with --yes to apply this migration non-interactively.");
return plan;
}
const selectedPlan = await promptCodexMigrationSkillSelection(runtime, plan, opts);
if (!selectedPlan) {
return plan;
}
const ok = await promptYesNo("Apply this migration now?", false);
if (!ok) {
runtime.log("Migration cancelled.");
return selectedPlan;
}
return await migrateApplyCommand(runtime, {
...opts,
provider: providerId,
yes: true,
json: opts.json,
preflightPlan: selectedPlan,
});
}
return await migrateApplyCommand(runtime, {
...opts,
provider: providerId,
yes: true,
json: opts.json,
preflightPlan: plan,
});
}