1d40812f2a
Each AI provider now has its own module implementing a ProviderModule interface with createGateway, getAuthHeaders, and fetchModels methods. This replaces the monolithic switch statements in ai-provider.ts and models.post.ts with a clean registry pattern.
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { createAnthropic } from '@ai-sdk/anthropic';
|
|
import { GatewayFetchError } from '~~/server/utils/ai-provider';
|
|
import { Err, Ok } from '~~/types/result';
|
|
import type { ProviderModule } from '.';
|
|
import { getModelData } from './helpers';
|
|
|
|
export default {
|
|
id: 'anthropic',
|
|
baseUrl: 'https://api.anthropic.com/v1',
|
|
modelsEndpoint: '/models',
|
|
|
|
createGateway({ apiKey, baseURL }) {
|
|
if (apiKey === undefined) {
|
|
return Err(GatewayFetchError.NoProviderApiKey);
|
|
}
|
|
|
|
return Ok({
|
|
gateway: createAnthropic({ apiKey, baseURL }),
|
|
streamTransformer: undefined,
|
|
textTransformer: undefined,
|
|
});
|
|
},
|
|
|
|
getAuthHeaders(apiKey) {
|
|
return apiKey
|
|
? { 'x-api-key': apiKey, 'anthropic-version': '2023-06-01' }
|
|
: {};
|
|
},
|
|
|
|
async fetchModels({ baseURL, apiKey, modelsDevData }) {
|
|
const res = await fetch(`${baseURL}/models`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'x-api-key': `${apiKey}`,
|
|
'anthropic-version': '2023-06-01',
|
|
},
|
|
});
|
|
const data = await res.json();
|
|
|
|
if (!res.ok) {
|
|
throw createError({ statusCode: res.status, message: JSON.stringify(data) });
|
|
}
|
|
|
|
return data.data.map((model: any) => ({
|
|
...getModelData(model.id, 'anthropic', modelsDevData),
|
|
id: model.id,
|
|
name: model.display_name ?? model.id,
|
|
}));
|
|
},
|
|
} satisfies ProviderModule;
|