import type { EnglishTutorSettings } from "./settings.ts"; import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; import type { Model } from "@earendil-works/pi-ai"; export interface ModelAuth { apiKey: string; headers?: Record; env?: Record; } export type ResolveModelResult = { ok: true; model: Model; auth: ModelAuth } | { ok: false; error: string }; /** Resolves the configured correction model and its credentials, or a single human-readable failure reason. */ export async function resolveCorrectionModel( ctx: ExtensionContext, settings: EnglishTutorSettings, ): Promise { const model = ctx.modelRegistry.find(settings.correctionProvider, settings.correctionModel); if (!model) { return { ok: false, error: `Correction model not found: ${settings.correctionProvider}/${settings.correctionModel}` }; } const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model); if (!auth.ok) return { ok: false, error: auth.error }; if (!auth.apiKey) return { ok: false, error: `No API key configured for ${settings.correctionProvider}` }; return { ok: true, model, auth: { apiKey: auth.apiKey, headers: auth.headers, env: auth.env } }; }