56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
/**
|
|
* providers/index.js — provider registry
|
|
*
|
|
* Reads ~/.pi/agent/models.json and returns typed provider config for all
|
|
* providers we know how to probe. Non-anthropic-messages providers (e.g. zai)
|
|
* are silently skipped.
|
|
*/
|
|
|
|
import { readFileSync } from 'fs';
|
|
import { homedir } from 'os';
|
|
|
|
/**
|
|
* Classify a provider by name and config.
|
|
* @returns {'teams-direct'|'shelley-proxy'|'api-direct'|null}
|
|
*/
|
|
function classifyProvider(name, config) {
|
|
if (name === 'shelley-proxy') return 'shelley-proxy';
|
|
if (name === 'api-ateam') return 'api-direct';
|
|
if (config.api === 'anthropic-messages' && name.startsWith('team-')) return 'teams-direct';
|
|
// Gemini — classify by baseUrl or name
|
|
const baseUrl = (config.baseUrl || '').toLowerCase();
|
|
if (baseUrl.includes('generativelanguage.googleapis.com') || name.toLowerCase().includes('gemini')) {
|
|
return 'gemini-api';
|
|
}
|
|
// x.ai — classify by baseUrl or name
|
|
if (baseUrl.includes('x.ai') || name.toLowerCase().includes('xai') || name.toLowerCase().includes('grok')) {
|
|
return 'xai-api';
|
|
}
|
|
return null; // skip (zai, etc.)
|
|
}
|
|
|
|
/**
|
|
* Load and classify all providers from models.json.
|
|
* @returns {Object} map of provider name → { name, type, baseUrl, apiKey }
|
|
*/
|
|
export function getProviders() {
|
|
const modelsJson = JSON.parse(
|
|
readFileSync(`${homedir()}/.pi/agent/models.json`, 'utf-8')
|
|
);
|
|
|
|
const providers = {};
|
|
for (const [name, config] of Object.entries(modelsJson.providers)) {
|
|
const type = classifyProvider(name, config);
|
|
if (!type) continue;
|
|
providers[name] = {
|
|
name,
|
|
type,
|
|
baseUrl: config.baseUrl,
|
|
apiKey: config.apiKey || null,
|
|
};
|
|
}
|
|
return providers;
|
|
}
|
|
|
|
// Alias for backwards compatibility
|
|
export const loadProviders = getProviders;
|