9993c96907
Move provider auth and OAuth flows onto pi-ai Models, compose models.json and extension overlays through ModelRuntime, and retain ModelRegistry as an extension compatibility facade.
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import type { ApiKeyAuth, OAuthAuth } from "./types.ts";
|
|
|
|
/**
|
|
* Standard api-key auth: a stored credential key wins, otherwise the first
|
|
* set env var resolves. Includes a `login` that prompts for the key.
|
|
* Providers with non-standard resolution (provider env, ambient files, IAM)
|
|
* write their own `ApiKeyAuth`.
|
|
*/
|
|
export function envApiKeyAuth(name: string, envVars: readonly string[]): ApiKeyAuth {
|
|
return {
|
|
name,
|
|
login: async (interaction) => {
|
|
const key = await interaction.prompt({ type: "secret", message: `Enter ${name}` });
|
|
return { type: "api_key", key };
|
|
},
|
|
resolve: async ({ ctx, credential }) => {
|
|
if (credential?.key) return { auth: { apiKey: credential.key }, source: "stored credential" };
|
|
for (const envVar of envVars) {
|
|
const value = await ctx.env(envVar);
|
|
if (value) return { auth: { apiKey: value }, source: envVar };
|
|
}
|
|
return undefined;
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Wraps a dynamically imported `OAuthAuth` so provider definitions can
|
|
* advertise OAuth without importing the implementation. The flow loads on
|
|
* first `login`/`refresh`/`toAuth` call; callers keep Node-only flow code out
|
|
* of bundles by loading through a bundler-opaque dynamic import (variable
|
|
* specifier, see the bedrock lazy wrapper).
|
|
*/
|
|
export function lazyOAuth(input: { name: string; load: () => Promise<OAuthAuth> }): OAuthAuth {
|
|
let promise: Promise<OAuthAuth> | undefined;
|
|
const loaded = () => {
|
|
promise ??= input.load();
|
|
return promise;
|
|
};
|
|
return {
|
|
name: input.name,
|
|
login: async (interaction) => (await loaded()).login(interaction),
|
|
refresh: async (credential) => (await loaded()).refresh(credential),
|
|
toAuth: async (credential) => (await loaded()).toAuth(credential),
|
|
};
|
|
}
|