diff --git a/src/pi-llm-client.ts b/src/pi-llm-client.ts index e90d80a..724c405 100644 --- a/src/pi-llm-client.ts +++ b/src/pi-llm-client.ts @@ -34,6 +34,15 @@ export class PiLLMClient implements LLMClient { // Dynamically import Pi's AI module (available in the Pi runtime) const { complete } = await import("@mariozechner/pi-ai"); + // Resolve auth through Pi's model registry (handles /login, env vars, etc.) + const auth = await ctx.modelRegistry?.getApiKeyAndHeaders?.(model); + if (auth && !auth.ok) { + throw new LLMError( + `Pi LLM auth error: ${auth.error}. ` + + "Run /login in Pi to configure authentication.", + ); + } + const response = await complete( model, { @@ -50,6 +59,7 @@ export class PiLLMClient implements LLMClient { { temperature: 0.1, maxTokens: 256, + ...(auth?.ok ? { apiKey: auth.apiKey, headers: auth.headers } : {}), }, ); diff --git a/tests/pi-extension.test.ts b/tests/pi-extension.test.ts index f4ee51a..c6d096b 100644 --- a/tests/pi-extension.test.ts +++ b/tests/pi-extension.test.ts @@ -60,6 +60,7 @@ describe("pi-extension", () => { }, modelRegistry: { getApiKeyAndHeaders: vi.fn(async () => ({ + ok: true, apiKey: "test-key", headers: {}, })),