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:
Zoe
2026-04-27 12:04:49 -05:00
parent 6ca4de4cab
commit 1d40812f2a
21 changed files with 1221 additions and 798 deletions
+63
View File
@@ -0,0 +1,63 @@
import { createOpenAI } from '@ai-sdk/openai';
import { GatewayFetchError } from '~~/server/utils/ai-provider';
import { Err, Ok } from '~~/types/result';
import type { ProviderModule } from '.';
export default {
id: 'closedrouter',
baseUrl: 'https://router.queef.in/v1',
modelsEndpoint: '/models',
createGateway({ apiKey, baseURL }) {
if (apiKey === undefined) {
return Err(GatewayFetchError.NoProviderApiKey);
}
return Ok({
gateway: createOpenAI({ name: 'ClosedRouter', apiKey, baseURL }),
streamTransformer: undefined,
textTransformer: undefined,
});
},
getAuthHeaders(apiKey) {
return apiKey ? { Authorization: `Bearer ${apiKey}` } : {};
},
async fetchModels({ baseURL, apiKey }) {
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) });
}
return data.data.map((model: any) => {
const capabilities = new Array<string>();
if (model.capabilities) {
if (model.capabilities.reasoning) {
capabilities.push('reasoning');
}
if (model.capabilities.tool_call) {
capabilities.push('tools');
}
}
return {
id: model.id,
name: model.name,
attributes: {
inputModalities: model.modalities?.input || ['text'],
outputModalities: model.modalities?.output || ['text'],
capabilities: capabilities,
contextWindow: model.context_window,
supported_parameters: [],
},
};
});
},
} satisfies ProviderModule;