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,38 @@
|
||||
import { googleVertexApi } from "../api/google-vertex.lazy.ts";
|
||||
import type { ApiKeyAuth } from "../auth/types.ts";
|
||||
import { createProvider, type Provider } from "../models.ts";
|
||||
import { GOOGLE_VERTEX_MODELS } from "./google-vertex.models.ts";
|
||||
|
||||
const VERTEX_ADC_PATH = "~/.config/gcloud/application_default_credentials.json";
|
||||
|
||||
/**
|
||||
* Vertex accepts an explicit API key or Application Default Credentials
|
||||
* (`gcloud auth application-default login`). ADC additionally requires
|
||||
* project and location env vars, which the implementation reads itself.
|
||||
*/
|
||||
const vertexAuth: ApiKeyAuth = {
|
||||
name: "Google Cloud credentials",
|
||||
resolve: async ({ ctx, credential }) => {
|
||||
const key = credential?.key ?? (await ctx.env("GOOGLE_CLOUD_API_KEY"));
|
||||
if (key) return { auth: { apiKey: key }, source: credential?.key ? "stored credential" : "GOOGLE_CLOUD_API_KEY" };
|
||||
|
||||
const adcPath = await ctx.env("GOOGLE_APPLICATION_CREDENTIALS");
|
||||
const hasCredentials = await ctx.fileExists(adcPath ?? VERTEX_ADC_PATH);
|
||||
const hasProject = Boolean((await ctx.env("GOOGLE_CLOUD_PROJECT")) ?? (await ctx.env("GCLOUD_PROJECT")));
|
||||
const hasLocation = Boolean(await ctx.env("GOOGLE_CLOUD_LOCATION"));
|
||||
if (hasCredentials && hasProject && hasLocation) {
|
||||
return { auth: {}, source: "gcloud application default credentials" };
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
};
|
||||
|
||||
export function googleVertexProvider(): Provider<"google-vertex"> {
|
||||
return createProvider({
|
||||
id: "google-vertex",
|
||||
name: "Google Vertex AI",
|
||||
auth: { apiKey: vertexAuth },
|
||||
models: Object.values(GOOGLE_VERTEX_MODELS),
|
||||
api: googleVertexApi(),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user