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.
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
|
import { GatewayFetchError } from '~~/server/utils/ai-provider';
|
|
import { Err, Ok } from '~~/types/result';
|
|
import type { ProviderModule } from '.';
|
|
import { getModelData } from './helpers';
|
|
|
|
export default {
|
|
id: 'google',
|
|
baseUrl: 'https://generativelanguage.googleapis.com/v1beta',
|
|
modelsEndpoint: '/models',
|
|
|
|
createGateway({ apiKey, baseURL }) {
|
|
if (apiKey === undefined) {
|
|
return Err(GatewayFetchError.NoProviderApiKey);
|
|
}
|
|
|
|
return Ok({
|
|
gateway: createGoogleGenerativeAI({ apiKey, baseURL }),
|
|
streamTransformer: undefined,
|
|
textTransformer: undefined,
|
|
});
|
|
},
|
|
|
|
getAuthHeaders(apiKey) {
|
|
return apiKey ? { 'x-goog-api-key': apiKey } : {};
|
|
},
|
|
|
|
async fetchModels({ baseURL, apiKey, modelsDevData }) {
|
|
const res = await fetch(`${baseURL}/models`, {
|
|
method: 'GET',
|
|
headers: { 'x-goog-api-key': `${apiKey}` },
|
|
});
|
|
const data = await res.json();
|
|
|
|
if (!res.ok) {
|
|
throw createError({ statusCode: res.status, message: JSON.stringify(data) });
|
|
}
|
|
|
|
return data.models.map((model: any) => ({
|
|
...getModelData(model.name.replace('models/', ''), 'google', modelsDevData),
|
|
id: model.name.replace('models/', ''),
|
|
name: model.displayName,
|
|
}));
|
|
},
|
|
} satisfies ProviderModule;
|