feat(ai): provider factories, per-provider catalogs, createProvider (phase 3)
Auth helpers in src/auth/helpers.ts: envApiKeyAuth() (stored key wins, then env vars in order, with secret-prompt login) and lazyOAuth() (flow loads on first use through bundler-opaque dynamic imports in utils/oauth/load.ts; the OAuthAuth flow exports land in phase 4). There is no OAuth factory toggle: providers that support OAuth always attach it, advertising costs nothing until login/refresh runs. createProvider() in models.ts builds providers from parts: single API implementation or a map dispatched on model.api (mixed-API providers like opencode and github-copilot); unknown api yields a stream error. generate-models.ts now emits one providers/<id>.models.ts catalog per provider (35 files, biome-excluded like models.generated.ts) and models.generated.ts becomes a generated aggregator, so importing one provider factory pulls one catalog. Typed getModel globals unchanged. One factory per built-in provider under src/providers/: envApiKeyAuth for standard providers, OAuth for anthropic/openai-codex/github-copilot, ambient ApiKeyAuth for amazon-bedrock (AWS env/profile/IAM) and google-vertex (explicit key or ADC+project+location). providers/all.ts: builtinProviders(), builtinModels(), getBuiltin* re-exports. fauxProvider() factory returns a real Provider for tests; legacy registerFauxProvider() unchanged.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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 (metadata, ambient files, IAM)
|
||||
* write their own `ApiKeyAuth`.
|
||||
*/
|
||||
export function envApiKeyAuth(name: string, envVars: readonly string[]): ApiKeyAuth {
|
||||
return {
|
||||
name,
|
||||
login: async (callbacks) => {
|
||||
const key = await callbacks.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 (callbacks) => (await loaded()).login(callbacks),
|
||||
refresh: async (credential) => (await loaded()).refresh(credential),
|
||||
toAuth: async (credential) => (await loaded()).toAuth(credential),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user