refactor: extract provider logic into modular registry
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.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { createMistral } from '@ai-sdk/mistral';
|
||||
import { GatewayFetchError } from '~~/server/utils/ai-provider';
|
||||
import { Err, Ok } from '~~/types/result';
|
||||
import type { ProviderModule } from '.';
|
||||
import { getModelData, mergeSets } from './helpers';
|
||||
|
||||
export default {
|
||||
id: 'mistral',
|
||||
baseUrl: 'https://api.mistral.ai/v1',
|
||||
modelsEndpoint: '/models',
|
||||
|
||||
createGateway({ apiKey, baseURL }) {
|
||||
if (apiKey === undefined) {
|
||||
return Err(GatewayFetchError.NoProviderApiKey);
|
||||
}
|
||||
|
||||
return Ok({
|
||||
gateway: createMistral({ apiKey, baseURL }),
|
||||
streamTransformer: undefined,
|
||||
textTransformer: undefined,
|
||||
});
|
||||
},
|
||||
|
||||
getAuthHeaders(apiKey) {
|
||||
return apiKey ? { Authorization: `Bearer ${apiKey}` } : {};
|
||||
},
|
||||
|
||||
async fetchModels({ baseURL, apiKey, modelsDevData }) {
|
||||
const res = await fetch(`${baseURL}/models`, {
|
||||
method: 'GET',
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (!res.ok) {
|
||||
throw createError({ statusCode: res.status, message: JSON.stringify(data) });
|
||||
}
|
||||
|
||||
const models = [];
|
||||
for (const model of data.data) {
|
||||
const inputModalities = new Set<string>();
|
||||
const capabilities = new Set<string>();
|
||||
|
||||
for (const capability of Object.keys(model.capabilities)) {
|
||||
if (model.capabilities[capability] === false) continue;
|
||||
|
||||
switch (capability) {
|
||||
case 'function_calling':
|
||||
capabilities.add('tools');
|
||||
break;
|
||||
case 'completion_chat':
|
||||
inputModalities.add('text');
|
||||
capabilities.add('completion');
|
||||
break;
|
||||
case 'vision':
|
||||
inputModalities.add('image');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const modelData = getModelData(model.id, 'mistral', modelsDevData);
|
||||
|
||||
models.push({
|
||||
...modelData,
|
||||
id: model.id,
|
||||
name: modelData.name || model.name || model.id,
|
||||
attributes: {
|
||||
...modelData.attributes,
|
||||
inputModalities: mergeSets(
|
||||
inputModalities,
|
||||
new Set(modelData.attributes?.inputModalities || [])
|
||||
),
|
||||
capabilities: mergeSets(
|
||||
capabilities,
|
||||
new Set(modelData.attributes?.capabilities || [])
|
||||
),
|
||||
contextWindow: model.max_context_length || modelData.attributes?.contextWindow,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return models;
|
||||
},
|
||||
} satisfies ProviderModule;
|
||||
Reference in New Issue
Block a user