51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { anthropicMessagesApi } from "../api/anthropic-messages.lazy.ts";
|
|
import { lazyOAuth } from "../auth/helpers.ts";
|
|
import { loadAnthropicOAuth } from "../auth/oauth/load.ts";
|
|
import type { ApiKeyAuth } from "../auth/types.ts";
|
|
import { ANTHROPIC_API_KEY_ENV, ANTHROPIC_AUTH_TOKEN_ENV, ANTHROPIC_OAUTH_TOKEN_ENV } from "../env-api-keys.ts";
|
|
import { createProvider, type Provider } from "../models.ts";
|
|
import { ANTHROPIC_MODELS } from "./anthropic.models.ts";
|
|
|
|
function anthropicApiKeyAuth(): ApiKeyAuth {
|
|
return {
|
|
name: "Anthropic API key",
|
|
login: async (interaction) => ({
|
|
type: "api_key",
|
|
key: await interaction.prompt({ type: "secret", message: "Enter Anthropic API key" }),
|
|
}),
|
|
resolve: async ({ ctx, credential }) => {
|
|
if (credential?.key) {
|
|
return { auth: { apiKey: credential.key }, env: credential.env, source: "stored credential" };
|
|
}
|
|
|
|
const authToken = await ctx.env(ANTHROPIC_AUTH_TOKEN_ENV);
|
|
if (authToken) {
|
|
return {
|
|
auth: { headers: { Authorization: `Bearer ${authToken}` } },
|
|
source: ANTHROPIC_AUTH_TOKEN_ENV,
|
|
};
|
|
}
|
|
|
|
for (const envVar of [ANTHROPIC_OAUTH_TOKEN_ENV, ANTHROPIC_API_KEY_ENV]) {
|
|
const apiKey = await ctx.env(envVar);
|
|
if (apiKey) return { auth: { apiKey }, source: envVar };
|
|
}
|
|
return undefined;
|
|
},
|
|
};
|
|
}
|
|
|
|
export function anthropicProvider(): Provider<"anthropic-messages"> {
|
|
return createProvider({
|
|
id: "anthropic",
|
|
name: "Anthropic",
|
|
baseUrl: "https://api.anthropic.com",
|
|
auth: {
|
|
apiKey: anthropicApiKeyAuth(),
|
|
oauth: lazyOAuth({ name: "Anthropic (Claude Pro/Max)", load: loadAnthropicOAuth }),
|
|
},
|
|
models: Object.values(ANTHROPIC_MODELS),
|
|
api: anthropicMessagesApi(),
|
|
});
|
|
}
|