/** * Register the `supergrok` provider with pi. */ import type { OAuthCredentials } from "@earendil-works/pi-ai"; import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent"; import { API_BASE } from "./constants.ts"; import { login, refreshToken } from "./oauth.ts"; export type RegisterSupergrokOptions = { /** Called after a successful login so we can re-discover models with the new token. */ onLoggedIn?: (credentials: OAuthCredentials) => void | Promise; }; export function registerSupergrok( pi: ExtensionAPI, models: ProviderModelConfig[], opts: RegisterSupergrokOptions = {}, ): void { pi.registerProvider("supergrok", { name: "SuperGrok (xAI OAuth)", baseUrl: API_BASE, // Env fallback: set XAI_API_KEY to skip OAuth entirely. apiKey: "$XAI_API_KEY", api: "openai-completions", authHeader: true, models, oauth: { name: "SuperGrok (xAI OAuth)", login: async (callbacks) => { const credentials = await login(callbacks); await opts.onLoggedIn?.(credentials); return credentials; }, refreshToken, getApiKey: (cred) => cred.access, }, }); }