/** * Setup Wizard Extension (/setup) - Refonte UX du wizard de configuration. * * Per Q8 strategy B: nouvelle commande /setup qui remplace /phi-init. * Depuis 0.97.0 c'est LE seul wizard : /phi-init (init.ts) importe * runSetupWizard d'ici au lieu d'entretenir sa propre copie, et le catalogue * de providers vit dans providers/catalog.ts (partagé avec /benchmark). UX: * - Panneau de statut persistant via setWidget (providers + assignments) * - Fuzzy search via ctx.ui.select (matching builtin Phi/upstream) * - Ping API immédiat à la saisie de clé (validation 401/200) * - Choix endpoint Alibaba (OpenAI-compat vs Anthropic-compat per Q2C) * - OpenCode Go avec auto-fetch runtime des modèles (per Q4A) * - Sélection séparée chat normal vs orchestration (5 phases) * - Persistance atomique via ApiKeyStore (chmod 0600 Unix) * - Hot-reload propagation: setKey émet "key_changed" event * * Note: ctx.ui.input ne supporte pas le masking actuellement (limitation TUI). * Pour compenser: * - Warning explicite avant la saisie ("la clé apparaitra à l'écran") * - Toutes les notifications post-saisie utilisent ApiKeyStore.maskKey() * - Storage chmod 0600 garantit la sécurité au repos */ import { existsSync, readFileSync } from "node:fs"; import { mkdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { ApiKeyStore, type ExtensionAPI, type ExtensionUIContext, getAgentDir, getApiKeyStore, getConfigWatcher, } from "phi-code"; import { ALIBABA_ENV_VAR, ALIBABA_PROVIDERS, ALIBABA_RATE_LIMITS, buildAlibabaProviderConfig, pingAlibaba, validateAlibabaApiKey, } from "./providers/alibaba.ts"; import { catalogEnvKey, getProviderCatalog, type ProviderCatalogEntry } from "./providers/catalog.ts"; import { fetchLiveModels, pingProvider, toPersistedModel } from "./providers/live-models.ts"; import { buildOpenCodeGoAnthropicProviderConfig, buildOpenCodeGoProviderConfig, getOpenCodeGoModels, OPENCODE_GO_AUTH_URL, pingOpenCodeGo, validateOpenCodeGoApiKey, } from "./providers/opencode-go.ts"; import { offlineMode, upstreamKnownIds } from "./providers/upstream-catalog.ts"; // ─── Types ─────────────────────────────────────────────────────────────── interface RouteAssignment { preferred: string; fallback: string; } interface RoutingConfigOut { routes: Record< string, { description: string; keywords: string[]; preferredModel: string; fallback: string; agent: string; } >; default: { model: string; agent: string | null }; } export const ORCHESTRATION_ROLES = [ { key: "explore", label: "Explore", desc: "Read-only analysis", agent: "explore", keywords: ["read", "analyze", "explain", "understand", "find", "search", "look", "show", "what", "how"], }, { key: "plan", label: "Plan", desc: "Architecture and design", agent: "plan", keywords: ["plan", "design", "architect", "spec", "structure", "organize", "strategy", "approach"], }, { key: "code", label: "Code", desc: "Implementation", agent: "code", keywords: ["implement", "create", "build", "refactor", "write", "add", "modify", "update", "generate"], }, { key: "test", label: "Test", desc: "Validation and tests", agent: "test", keywords: ["test", "verify", "validate", "check", "assert", "coverage"], }, { key: "review", label: "Review", desc: "Quality and security review", agent: "review", keywords: ["review", "audit", "quality", "security", "improve", "optimize"], }, ] as const; const DEBUG_KEYWORDS = ["fix", "bug", "error", "debug", "crash", "broken", "failing", "issue", "troubleshoot"]; // ─── Helpers ───────────────────────────────────────────────────────────── // The provider catalog itself lives in providers/catalog.ts (shared with // /phi-init and /benchmark). async function probeLocalProvider(provider: ProviderCatalogEntry): Promise { if (!provider.local) return []; const result = await fetchLiveModels(provider.id, { forceRefresh: true, timeoutMs: 2_500 }); return result.source === "live" ? result.models.map((m) => m.id) : []; } function buildStatusWidget( store: ApiKeyStore, available: Map, assignments: { default?: string; orchestration: Record }, ): string[] { const lines: string[] = []; lines.push("─── Phi Code Setup ───"); lines.push(""); lines.push("Providers:"); const catalog = getProviderCatalog(); for (const p of catalog) { const state = available.get(p.id); const icon = state ? "[ok]" : "[--]"; const note = state ? ` (${state.modelCount} models, ${state.source})` : ""; lines.push(` ${icon} ${p.displayName}${note}`); } lines.push(""); lines.push("Orchestration roles (used by /plan — NOT chat):"); for (const role of ORCHESTRATION_ROLES) { const a = assignments.orchestration[role.key]; const preferred = a?.preferred ?? "(not set)"; const fallback = a?.fallback ?? "(none)"; lines.push(` ${role.label.padEnd(8)} : ${preferred} / ${fallback}`); } lines.push(""); lines.push("Chat default model : controlled via `/model` (this wizard never overrides it)"); lines.push(`Keys file : ${store.configPath} (chmod 0600 on Unix)`); return lines; } function maskKeyForDisplay(key: string | undefined): string { return ApiKeyStore.maskKey(key); } export function buildRoutingConfig( defaultModel: string, orchestration: Record, ): RoutingConfigOut { const routes: RoutingConfigOut["routes"] = {}; for (const role of ORCHESTRATION_ROLES) { const a = orchestration[role.key] ?? { preferred: defaultModel, fallback: defaultModel }; routes[role.key] = { description: role.desc, keywords: [...role.keywords], preferredModel: a.preferred, fallback: a.fallback, agent: role.agent, }; } routes.debug = { description: "Debugging, fixing, error resolution", keywords: [...DEBUG_KEYWORDS], preferredModel: orchestration.code?.preferred ?? defaultModel, fallback: orchestration.code?.fallback ?? defaultModel, agent: "code", }; return { routes, default: { model: defaultModel, agent: null }, }; } export async function writeRoutingConfig(routing: RoutingConfigOut): Promise { const dir = getAgentDir(); await mkdir(dir, { recursive: true }); const path = join(dir, "routing.json"); await writeFile( path, `${JSON.stringify({ ...routing, $schema: "./routing.schema.json", version: 1 }, null, 2)}\n`, "utf-8", ); return path; } // ─── Provider configuration sub-flow ───────────────────────────────────── async function configureAlibaba( ui: ExtensionUIContext, store: ApiKeyStore, ): Promise<{ providerId: string; modelCount: number } | undefined> { ui.notify( `**Alibaba Coding Plan** (${ALIBABA_RATE_LIMITS.subscriptionType})\n` + `Rate limits: ${ALIBABA_RATE_LIMITS.per5Hours}/5h | ${ALIBABA_RATE_LIMITS.perWeek}/week | ${ALIBABA_RATE_LIMITS.perMonth}/month\n` + `Both variants share the SAME API key (env var ${ALIBABA_ENV_VAR}, format sk-sp-xxxxx).`, "info", ); const variantChoice = await ui.select("Which Alibaba endpoint to configure?", [ "OpenAI-compat (/v1) - legacy, broad compat", "Anthropic-compat (/apps/anthropic) - ~99.7% prompt cache hit (recommended)", "Both endpoints (configure once, register both)", "Cancel", ]); if (!variantChoice || variantChoice === "Cancel") return undefined; ui.notify( "WARNING: the key you type will be visible on screen during input. " + "It will be stored chmod 0600 in ~/.phi/agent/models.json.", "warning", ); const apiKey = await ui.input("Paste your Alibaba Coding Plan API key", "sk-sp-..."); if (!apiKey || apiKey.trim().length === 0) { ui.notify("No key provided. Skipped.", "warning"); return undefined; } const trimmed = apiKey.trim(); const validationError = validateAlibabaApiKey(trimmed); if (validationError) { const proceed = await ui.confirm("Invalid key format", `${validationError}. Save anyway?`); if (!proceed) return undefined; } ui.setStatus("setup-ping", "Pinging Alibaba endpoint..."); const pingResult = await pingAlibaba(trimmed); ui.setStatus("setup-ping", undefined); if (!pingResult.ok) { const proceed = await ui.confirm( "Ping failed", `Alibaba ping failed: ${pingResult.error ?? "unknown"}. Save key anyway?`, ); if (!proceed) return undefined; } else { ui.notify("Alibaba ping OK (200).", "info"); } let modelCount = 0; let lastProviderId = "alibaba-codingplan"; const installVariant = (variant: "openai" | "anthropic"): void => { const provider = ALIBABA_PROVIDERS[variant]; const config = buildAlibabaProviderConfig(variant, trimmed); store.setKey(provider.id, trimmed, { baseUrl: config.baseUrl, api: config.api, models: config.models, }); modelCount += config.models.length; lastProviderId = provider.id; }; if (variantChoice.startsWith("OpenAI-compat")) installVariant("openai"); else if (variantChoice.startsWith("Anthropic-compat")) installVariant("anthropic"); else { installVariant("openai"); installVariant("anthropic"); } ui.notify( `Alibaba configured: \`${maskKeyForDisplay(trimmed)}\` -> ${variantChoice.split(" -")[0]}\n` + `${modelCount} model entries saved to ~/.phi/agent/models.json.`, "info", ); return { providerId: lastProviderId, modelCount }; } async function configureOpenCodeGo( ui: ExtensionUIContext, store: ApiKeyStore, ): Promise<{ providerId: string; modelCount: number } | undefined> { ui.notify( `**Opencode GO (zen)** - subscribe at ${OPENCODE_GO_AUTH_URL} ($5 first month, $10/month).\n` + `After subscribing, paste your API key below.`, "info", ); ui.notify("WARNING: the key you type will be visible on screen during input.", "warning"); const apiKey = await ui.input("Paste your Opencode GO API key", "..."); if (!apiKey || apiKey.trim().length === 0) { ui.notify("No key provided. Skipped.", "warning"); return undefined; } const trimmed = apiKey.trim(); const validationError = validateOpenCodeGoApiKey(trimmed); if (validationError) { const proceed = await ui.confirm("Invalid key format", `${validationError}. Save anyway?`); if (!proceed) return undefined; } ui.setStatus("setup-ping", "Pinging Opencode GO..."); const pingResult = await pingOpenCodeGo(trimmed); ui.setStatus("setup-ping", undefined); if (!pingResult.ok) { const proceed = await ui.confirm( "Ping failed", `Opencode GO ping failed: ${pingResult.error ?? "unknown"}. Save key anyway?`, ); if (!proceed) return undefined; } else { ui.notify("Opencode GO ping OK (200).", "info"); } ui.setStatus("setup-fetch", "Fetching live Opencode GO model list..."); const { models, source } = await getOpenCodeGoModels({ apiKey: trimmed, forceRefresh: true }); ui.setStatus("setup-fetch", undefined); ui.notify(`Fetched ${models.length} Opencode GO models (source: ${source}).`, "info"); // Only models no upstream catalog describes are persisted: a models.json entry // replaces the upstream definition (context window, costs, compat) for that id. const known = await upstreamKnownIds("opencode-go", { modelsJsonPath: store.configPath, offline: offlineMode(), }); const config = buildOpenCodeGoProviderConfig(trimmed, models); const openAiCompat = config.models.filter((model) => !known.has(model.id)); store.setKey("opencode-go", trimmed, { baseUrl: config.baseUrl, api: config.api, models: openAiCompat, }); // Qwen / MiniMax are only served via the Anthropic-compatible endpoint on // OpenCode Go, so they go to a separate provider (the OpenAI shim 401s them). const anthConfig = buildOpenCodeGoAnthropicProviderConfig(trimmed, models); const anthropicCompat = anthConfig.models.filter((model) => !known.has(model.id)); if (anthropicCompat.length > 0) { store.setKey("opencode-go-anthropic", trimmed, { baseUrl: anthConfig.baseUrl, api: anthConfig.api, models: anthropicCompat, }); } ui.notify( `Opencode GO configured: \`${maskKeyForDisplay(trimmed)}\` ` + `(${openAiCompat.length} OpenAI-compat + ${anthropicCompat.length} Anthropic-compat models persisted).\n` + `The other models keep the context windows the built-in and pi.dev catalogs publish.`, "info", ); return { providerId: "opencode-go", modelCount: openAiCompat.length + anthropicCompat.length }; } async function configureGenericCloud( ui: ExtensionUIContext, store: ApiKeyStore, provider: ProviderCatalogEntry, ): Promise<{ providerId: string; modelCount: number } | undefined> { if (provider.supportsOAuth) { const authChoice = await ui.select(`Auth method for ${provider.displayName}`, [ "API Key (paste your key)", "OAuth (run /login after setup)", "Cancel", ]); if (!authChoice || authChoice === "Cancel") return undefined; if (authChoice.startsWith("OAuth")) { ui.notify(`Run \`/login ${provider.id}\` after setup to authenticate via OAuth.`, "info"); store.setKey(provider.id, "$OAUTH", { baseUrl: provider.baseUrl, api: provider.api, }); return { providerId: provider.id, modelCount: provider.staticModels.length }; } } ui.notify("WARNING: the key you type will be visible on screen during input.", "warning"); const apiKey = await ui.input(`Paste your ${provider.displayName} API key`, "..."); if (!apiKey || apiKey.trim().length === 0) { ui.notify("No key provided. Skipped.", "warning"); return undefined; } const trimmed = apiKey.trim(); if (trimmed.length < 8) { const proceed = await ui.confirm("Suspiciously short key", "Save anyway?"); if (!proceed) return undefined; } // Mute the config watcher so these programmatic writes are not echoed back as // models_json_changed events. Mute per-write because the ping/fetch below can // exceed the ignore window between the two setKey calls (cf. models.ts). const watcher = getConfigWatcher(); // Persist the key immediately so a downstream fetch failure cannot lose user input. watcher.muteForWrite("models_json_changed"); store.setKey(provider.id, trimmed, { baseUrl: provider.baseUrl, api: provider.api, }); // Optional ping for early auth diagnostics. ui.setStatus("setup-ping", `Pinging ${provider.displayName}...`); const ping = await pingProvider(provider.id, trimmed, 5_000).catch((err) => ({ ok: false, error: err instanceof Error ? err.message : String(err), })); ui.setStatus("setup-ping", undefined); if (ping.ok) { ui.notify(`${provider.displayName} ping OK (200).`, "info"); } else { ui.notify( `${provider.displayName} ping failed: ${ping.error ?? "unknown"}. Key saved; you can retry with \`/keys test ${provider.id}\`.`, "warning", ); } // Live-fetch the model catalog (falls back to the static list when offline). ui.setStatus("setup-fetch", `Fetching ${provider.displayName} model list...`); const live = await fetchLiveModels(provider.id, { apiKey: trimmed, forceRefresh: true, timeoutMs: 6_000, }); ui.setStatus("setup-fetch", undefined); const known = await upstreamKnownIds(provider.id, { modelsJsonPath: store.configPath, offline: offlineMode(), }); const models = ( live.models.length > 0 ? live.models : provider.staticModels.map((id) => ({ id, name: id, reasoning: true })) ) .map(toPersistedModel) // Only models no upstream catalog describes are persisted: a models.json // entry replaces the upstream definition (context window, costs, compat) // for that id, so persisting a known model would put a guessed window on // screen and compact the conversation far too early. .filter((model) => !known.has(model.id)); watcher.muteForWrite("models_json_changed"); store.setKey(provider.id, trimmed, { baseUrl: provider.baseUrl, api: provider.api, models, }); ui.notify( `${provider.displayName} configured: \`${maskKeyForDisplay(trimmed)}\` (${models.length} models persisted, source: ${live.source}${live.error ? `, ${live.error}` : ""}).\n` + `${known.size} further model(s) come from the built-in and pi.dev catalogs with their real context windows.`, "info", ); return { providerId: provider.id, modelCount: models.length }; } async function configureLocal( ui: ExtensionUIContext, store: ApiKeyStore, provider: ProviderCatalogEntry, ): Promise<{ providerId: string; modelCount: number } | undefined> { ui.setStatus("setup-probe", `Probing ${provider.displayName}...`); const models = await probeLocalProvider(provider); ui.setStatus("setup-probe", undefined); if (models.length === 0) { ui.notify( `${provider.displayName} not reachable at ${provider.baseUrl}. ` + `Start the server and re-run /setup, or skip.`, "warning", ); return undefined; } store.setKey(provider.id, "local", { baseUrl: provider.baseUrl, api: provider.api, models: models.map((id) => ({ id, name: id, reasoning: false, input: ["text"] as const, })), }); ui.notify(`${provider.displayName} configured: ${models.length} models discovered.`, "info"); return { providerId: provider.id, modelCount: models.length }; } // ─── Assignment sub-flow ───────────────────────────────────────────────── async function pickModelFromCatalog( ui: ExtensionUIContext, prompt: string, models: Array<{ ref: string; display: string }>, ): Promise { if (models.length === 0) { ui.notify("No models available. Configure a provider first.", "warning"); return undefined; } const KEEP = "(keep default)"; const options = [KEEP, ...models.map((m) => m.display)]; const choice = await ui.select(prompt, options); if (!choice || choice === KEEP) return undefined; // Map the displayed "id [provider]" back to the canonical "provider/id" ref. return models.find((m) => m.display === choice)?.ref; } export async function configureAssignments( ui: ExtensionUIContext, models: Array<{ ref: string; display: string }>, ): Promise<{ defaultModel: string; orchestration: Record }> { if (models.length === 0) { ui.notify("No models available for assignment. Configure a provider first.", "warning"); return { defaultModel: "default", orchestration: {} }; } ui.notify( "Assigning orchestration role models. The chat default is controlled via `/model` — " + "this wizard does NOT change it.", "info", ); // Sentinel: the orchestrator falls back to the current chat model when a // route doesn't pin a specific one. We never ask the user for a "default" // chat model here — `/model` owns that. const defaultModel = "default"; const orchestration: Record = {}; for (const role of ORCHESTRATION_ROLES) { const preferred = (await pickModelFromCatalog(ui, `${role.label} - preferred model (${role.desc})`, models)) ?? models[0].ref; const fallbackModels = models.filter((m) => m.ref !== preferred); const fallback = fallbackModels.length > 0 ? ((await pickModelFromCatalog(ui, `${role.label} - fallback model`, fallbackModels)) ?? preferred) : preferred; orchestration[role.key] = { preferred, fallback }; ui.notify(` ${role.label}: ${preferred} / ${fallback}`, "info"); } return { defaultModel, orchestration }; } // ─── Extension ─────────────────────────────────────────────────────────── /** * Settings keys phi writes by itself before the user chose anything: * scripts/postinstall.cjs writes `quietStartup` on every fresh install and the * interactive mode records `lastChangelogVersion` on its first start. */ const AUTOMATIC_SETTINGS_KEYS = new Set(["quietStartup", "lastChangelogVersion"]); /** * Whether settings.json holds anything the user chose. The mere existence of the * file (the old test) meant the first-run wizard was never offered after * `npm install -g`, since the postinstall creates it. */ export function hasUserSettings(settingsPath: string): boolean { if (!existsSync(settingsPath)) return false; try { const parsed: unknown = JSON.parse(readFileSync(settingsPath, "utf-8")); if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return true; return Object.keys(parsed).some((key) => !AUTOMATIC_SETTINGS_KEYS.has(key)); } catch { // Unreadable or hand-edited: treat as configured, never nag. return true; } } // One-time global guard so a stray async rejection inside the wizard never kills the TUI. let setupUnhandledGuard = false; function installSetupUnhandledRejectionGuard(): void { if (setupUnhandledGuard) return; setupUnhandledGuard = true; process.on("unhandledRejection", (reason) => { const message = reason instanceof Error ? reason.message : String(reason); try { process.stderr.write(`[phi-setup] swallowed unhandledRejection: ${message}\n`); } catch { // no-op } }); } /** * The full setup wizard flow. Exported so /phi-init (init.ts) delegates to the * exact same wizard instead of maintaining a drifting second copy. */ export async function runSetupWizard(ui: ExtensionUIContext): Promise { const store = getApiKeyStore(); try { store.load(); } catch { // empty file or missing, fine } try { ui.notify( "**φ Phi Code Setup Wizard**\n\n" + "This wizard configures providers and assigns models to **orchestration roles** " + "(used by `/plan`).\n" + "The **chat default model is controlled via `/model`** and stays sticky across " + "prompts — this wizard will never change it.\n\n" + "Keys are stored in `~/.phi/agent/models.json` (chmod 0600 on Unix). " + "Edit that file directly later to hot-reload (no restart needed).", "info", ); const available = new Map(); const assignments: { default?: string; orchestration: Record } = { orchestration: {}, }; const refreshAvailable = (): void => { const catalog = getProviderCatalog(); available.clear(); for (const p of catalog) { const stored = store.getProvider(p.id); if (stored?.apiKey) { const source = stored.apiKey === "$OAUTH" ? "key" : stored.apiKey === "local" ? "local" : "key"; const modelCount = Array.isArray(stored.models) ? stored.models.length : p.staticModels.length; available.set(p.id, { source, modelCount }); continue; } if (!p.local && catalogEnvKey(p)) { available.set(p.id, { source: "env", modelCount: p.staticModels.length }); } } ui.setWidget("setup-status", buildStatusWidget(store, available, assignments)); }; refreshAvailable(); let done = false; while (!done) { const catalog = getProviderCatalog(); const choices: string[] = []; for (const p of catalog) { const state = available.get(p.id); const tag = state ? "[ok]" : "[--]"; const modelTag = state ? ` (${state.modelCount} models)` : ""; choices.push(`${tag} ${p.displayName}${modelTag}`); } choices.push("---"); choices.push("Assign models to agent roles"); choices.push("Finish and save"); choices.push("Quit without saving"); const action = await ui.select("Phi Code Setup - choose an action", choices); if (!action) { done = true; break; } if (action === "Quit without saving") { const confirm = await ui.confirm( "Quit", "Discard all in-memory assignments? (provider keys already saved are kept)", ); if (confirm) { ui.setWidget("setup-status", undefined); ui.notify("Setup cancelled (saved provider keys are unchanged).", "warning"); return; } continue; } if (action === "Finish and save") { if (!assignments.default || Object.keys(assignments.orchestration).length === 0) { const proceed = await ui.confirm( "Assignments incomplete", "You have not assigned all roles. Save current state anyway?", ); if (!proceed) continue; } done = true; break; } if (action === "Assign models to agent roles") { const allModels: Array<{ ref: string; display: string }> = []; const seenRefs = new Set(); for (const p of catalog) { const stored = store.getProvider(p.id); if (!stored) continue; const models = Array.isArray(stored.models) ? stored.models : []; for (const m of models) { const id = (m as { id?: string }).id; if (typeof id !== "string") continue; // Provider-qualified ref so the same id from different providers stays distinct. const ref = `${p.id}/${id}`; if (seenRefs.has(ref)) continue; seenRefs.add(ref); allModels.push({ ref, display: `${id} [${p.id}]` }); } } const { defaultModel, orchestration } = await configureAssignments(ui, allModels); assignments.default = defaultModel; assignments.orchestration = orchestration; refreshAvailable(); continue; } if (action === "---") continue; const providerIndex = choices.indexOf(action); if (providerIndex < 0 || providerIndex >= catalog.length) continue; const provider = catalog[providerIndex]; let result: { providerId: string; modelCount: number } | undefined; try { if (provider.id === "alibaba-codingplan") { result = await configureAlibaba(ui, store); } else if (provider.id === "opencode-go") { result = await configureOpenCodeGo(ui, store); } else if (provider.local) { result = await configureLocal(ui, store, provider); } else { result = await configureGenericCloud(ui, store, provider); } } catch (err) { ui.notify(`Provider configuration failed: ${err instanceof Error ? err.message : String(err)}`, "error"); } if (result) refreshAvailable(); } // Save routing if assignments were made if (assignments.default) { try { const routing = buildRoutingConfig(assignments.default, assignments.orchestration); const routingPath = await writeRoutingConfig(routing); ui.notify(`Routing config written to \`${routingPath}\`.`, "info"); } catch (err) { ui.notify(`Failed to write routing.json: ${err}`, "error"); } } ui.setWidget("setup-status", undefined); ui.notify( "**Setup complete.**\n\n" + "Next steps:\n" + " - `/keys` to list/manage saved keys\n" + " - `/models refresh` to re-fetch the catalog from each provider's API\n" + " - `/routing` to inspect routing\n" + " - `/agents` to list sub-agents\n" + " - `/skills` to list skills\n" + " - Edit `~/.phi/agent/models.json` or `routing.json` directly: hot-reload kicks in", "info", ); } catch (err) { ui.setWidget("setup-status", undefined); ui.notify(`Setup wizard error: ${err instanceof Error ? err.message : String(err)}`, "error"); } } export default function setupExtension(pi: ExtensionAPI) { installSetupUnhandledRejectionGuard(); pi.registerCommand("setup", { description: "Phi Code setup wizard (refonte UX, replaces /phi-init)", handler: async (_args, ctx) => { await runSetupWizard(ctx.ui); }, }); // First-run detection: no user settings and no stored API keys means this // is a fresh install. Offer the wizard once, only in interactive TUI mode. pi.on("session_start", async (_event, ctx) => { try { // hasUI is also true in RPC mode, where session_start runs before the // stdin reader is attached: a confirm() there can never be answered and // the RPC session hangs forever. if (!ctx.hasUI || ctx.mode !== "tui") return; if (hasUserSettings(join(getAgentDir(), "settings.json"))) return; // A provider already usable (env key, auth.json, models.json) is a configuration. if (ctx.modelRegistry.getAvailable().length > 0) return; const store = getApiKeyStore(); if (store.listProviders().length > 0) return; const answer = await ctx.ui.confirm( "Bienvenue dans Phi Code", "Aucune configuration détectée. Lancer l'assistant de configuration maintenant ? (sinon: /setup plus tard)", ); if (answer) { await runSetupWizard(ctx.ui); } } catch { // First-run prompt must never break session start. } }); }