new flow for Pi provider/model settings

This commit is contained in:
2026-02-22 20:19:11 +00:00
parent 1372f53782
commit f1e3ce4b76
15 changed files with 249 additions and 460 deletions
@@ -2,6 +2,7 @@ import { join } from 'node:path';
import { createRouter } from '../../create-router';
import { DATA_PATH } from '../../data-path';
import { syncLocalProvidersToPiConfig } from './sync-pi-config';
import { invalidateModelCache } from '../pi/list-models';
export const piMonoRouter = createRouter();
@@ -214,6 +215,7 @@ piMonoRouter.put('/api-keys', async (ctx) => {
delete keys[key];
}
await writeApiKeys(keys);
invalidateModelCache();
return ctx.json({ ok: true });
});
@@ -1,194 +0,0 @@
import { logger } from '../pi/logger';
export type ApiProvider = {
id: string;
name: string;
apiType: 'openai-completions' | 'anthropic-messages' | 'google-generative-ai';
baseUrl: string;
envKeys: string[];
getApiKey: (keys: Record<string, string>) => string | null;
hardcodedModels?: boolean;
};
/**
* Registry of API-based providers that Officer supports
* These will be synced to Pi's config and their models fetched
*/
export const API_PROVIDERS: ApiProvider[] = [
{
id: 'openai',
name: 'OpenAI',
apiType: 'openai-completions',
baseUrl: 'https://api.openai.com/v1',
envKeys: ['OPENAI_API_KEY'],
getApiKey: (keys) => keys.OPENAI_API_KEY || null,
},
{
id: 'anthropic',
name: 'Anthropic',
apiType: 'anthropic-messages',
baseUrl: 'https://api.anthropic.com/v1',
envKeys: ['ANTHROPIC_API_KEY'],
getApiKey: (keys) => keys.ANTHROPIC_API_KEY || null,
},
{
id: 'opencode',
name: 'OpenCode Zen',
apiType: 'openai-completions',
baseUrl: 'https://api.opencodezen.com/v1',
envKeys: ['OPENCODE_API_KEY'],
getApiKey: (keys) => keys.OPENCODE_API_KEY || null,
hardcodedModels: true, // No public /models endpoint
},
{
id: 'groq',
name: 'Groq',
apiType: 'openai-completions',
baseUrl: 'https://api.groq.com/openai/v1',
envKeys: ['GROQ_API_KEY'],
getApiKey: (keys) => keys.GROQ_API_KEY || null,
},
{
id: 'mistral',
name: 'Mistral',
apiType: 'openai-completions',
baseUrl: 'https://api.mistral.ai/v1',
envKeys: ['MISTRAL_API_KEY'],
getApiKey: (keys) => keys.MISTRAL_API_KEY || null,
},
{
id: 'xai',
name: 'xAI',
apiType: 'openai-completions',
baseUrl: 'https://api.x.ai/v1',
envKeys: ['XAI_API_KEY'],
getApiKey: (keys) => keys.XAI_API_KEY || null,
},
{
id: 'openrouter',
name: 'OpenRouter',
apiType: 'openai-completions',
baseUrl: 'https://openrouter.ai/api/v1',
envKeys: ['OPENROUTER_API_KEY'],
getApiKey: (keys) => keys.OPENROUTER_API_KEY || null,
},
{
id: 'google',
name: 'Google',
apiType: 'google-generative-ai',
baseUrl: 'https://generativelanguage.googleapis.com/v1beta',
envKeys: ['GOOGLE_API_KEY', 'GEMINI_API_KEY'],
getApiKey: (keys) => keys.GOOGLE_API_KEY || keys.GEMINI_API_KEY || null,
},
];
/**
* Fetch models from an OpenAI-compatible provider
*/
async function fetchOpenAIModels(baseUrl: string, apiKey: string): Promise<{ id: string; name: string }[]> {
try {
const res = await fetch(`${baseUrl}/models`, {
headers: { Authorization: `Bearer ${apiKey}` },
signal: AbortSignal.timeout(5000),
});
if (!res.ok) {
logger.error('Failed to fetch models from OpenAI-compatible API', { baseUrl, status: res.status });
return [];
}
const data = await res.json();
if (!data.data || !Array.isArray(data.data)) return [];
return data.data.map((m: any) => ({
id: m.id,
name: m.id,
}));
} catch (err) {
logger.error('Failed to fetch OpenAI models', { baseUrl, error: String(err) });
return [];
}
}
/**
* Fetch models from Anthropic API
*/
async function fetchAnthropicModels(apiKey: string): Promise<{ id: string; name: string }[]> {
// Anthropic doesn't have a /models endpoint, so we return known models
// These are documented at https://docs.anthropic.com/en/docs/about-claude/models
return [
{ id: 'claude-3-5-sonnet-20241022', name: 'Claude 3.5 Sonnet' },
{ id: 'claude-3-5-haiku-20241022', name: 'Claude 3.5 Haiku' },
{ id: 'claude-3-opus-20240229', name: 'Claude 3 Opus' },
{ id: 'claude-3-sonnet-20240229', name: 'Claude 3 Sonnet' },
{ id: 'claude-3-haiku-20240307', name: 'Claude 3 Haiku' },
];
}
/**
* Fetch models from Google Generative AI
*/
async function fetchGoogleModels(apiKey: string): Promise<{ id: string; name: string }[]> {
// Google also doesn't expose a simple models endpoint, return known models
return [
{ id: 'gemini-2.0-flash-exp', name: 'Gemini 2.0 Flash' },
{ id: 'gemini-1.5-pro', name: 'Gemini 1.5 Pro' },
{ id: 'gemini-1.5-flash', name: 'Gemini 1.5 Flash' },
{ id: 'gemini-1.5-flash-8b', name: 'Gemini 1.5 Flash 8B' },
];
}
/**
* Fetch models from OpenCode Zen
*/
async function fetchOpenCodeModels(apiKey: string): Promise<{ id: string; name: string }[]> {
// OpenCode Zen doesn't expose a public models endpoint, return known models
return [
{ id: 'big-pickle', name: 'Big Pickle' },
{ id: 'claude-haiku-4-5', name: 'Claude Haiku 4.5' },
{ id: 'claude-sonnet-4-5', name: 'Claude Sonnet 4.5' },
{ id: 'claude-opus-4-5', name: 'Claude Opus 4.5' },
{ id: 'gemini-3-flash', name: 'Gemini 3 Flash' },
];
}
/**
* Fetch models from a configured API provider
*/
export async function fetchProviderModels(
provider: ApiProvider,
apiKey: string,
): Promise<{ id: string; name: string }[]> {
// Handle providers with hardcoded models first
if (provider.id === 'opencode') {
return fetchOpenCodeModels(apiKey);
}
switch (provider.apiType) {
case 'openai-completions':
return fetchOpenAIModels(provider.baseUrl, apiKey);
case 'anthropic-messages':
return fetchAnthropicModels(apiKey);
case 'google-generative-ai':
return fetchGoogleModels(apiKey);
default:
logger.warn('Unknown API type for provider', { provider: provider.id, apiType: provider.apiType });
return [];
}
}
/**
* Get all configured API providers with their API keys
*/
export function getConfiguredProviders(storedKeys: Record<string, string>): Array<{ provider: ApiProvider; apiKey: string }> {
const configured: Array<{ provider: ApiProvider; apiKey: string }> = [];
for (const provider of API_PROVIDERS) {
const apiKey = provider.getApiKey(storedKeys);
if (apiKey) {
configured.push({ provider, apiKey });
}
}
return configured;
}
+22 -110
View File
@@ -1,11 +1,10 @@
import { join } from 'node:path';
import { homedir } from 'node:os';
import type { LocalProvider } from './pi-mono';
import { readLocalProviders, readApiKeys } from './pi-mono';
import { getConfiguredProviders, fetchProviderModels } from './provider-registry';
import { readLocalProviders } from './pi-mono';
import { PI_CONFIG_DIR } from '../../data-path';
import { invalidateModelCache } from '../pi/list-models';
import { logger } from '../pi/logger';
const PI_CONFIG_DIR = join(homedir(), '.pi', 'agent');
const PI_MODELS_FILE = join(PI_CONFIG_DIR, 'models.json');
type PiModelConfig = {
@@ -31,107 +30,48 @@ type PiModelConfig = {
};
/**
* Sync Officer's providers (both API-based and local) to Pi's models.json config
* This makes Pi aware of all providers configured in Officer
* Sync Officer's local providers to Pi's models.json config.
* Only local providers are synced — API providers are handled by Pi via env vars.
*/
export async function syncLocalProvidersToPiConfig(): Promise<void> {
try {
// Read existing Pi config
const piModelsFile = Bun.file(PI_MODELS_FILE);
let piConfig: PiModelConfig;
if (await piModelsFile.exists()) {
piConfig = await piModelsFile.json();
} else {
piConfig = { providers: {} };
}
// Remove ALL Officer-managed provider entries (cleanup)
// Remove all officer-local-* entries (will be re-added below)
for (const providerId of Object.keys(piConfig.providers)) {
if (providerId.startsWith('officer-local-') ||
['openai', 'anthropic', 'opencode', 'groq', 'mistral', 'xai', 'openrouter', 'google'].includes(providerId)) {
if (providerId.startsWith('officer-local-')) {
delete piConfig.providers[providerId];
}
}
// 1. Sync API-based providers
const storedKeys = await readApiKeys();
const configuredProviders = getConfiguredProviders(storedKeys);
logger.info('Syncing API-based providers to Pi config', { count: configuredProviders.length });
for (const { provider, apiKey } of configuredProviders) {
try {
const providerModels = await fetchProviderModels(provider, apiKey);
if (providerModels.length === 0) {
logger.warn('No models found for API provider', { provider: provider.name });
continue;
}
piConfig.providers[provider.id] = {
baseUrl: provider.baseUrl,
apiKey,
api: provider.apiType,
models: providerModels.map(m => ({
id: m.id,
name: m.name,
reasoning: false,
input: ['text'],
contextWindow: 128000,
maxTokens: 4096,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
},
})),
};
logger.info('Added API provider to Pi config', {
providerId: provider.id,
providerName: provider.name,
modelCount: providerModels.length
});
} catch (err) {
logger.error('Failed to sync API provider', {
provider: provider.name,
error: String(err)
});
}
}
// 2. Sync local providers
// Add local providers
const localProviders = await readLocalProviders();
logger.info('Syncing local providers to Pi config', { count: localProviders.length });
// Add local providers to Pi config
for (const lp of localProviders) {
const providerId = `officer-local-${lp.id}`;
// Fetch models from the local provider
const models = await fetchModelsFromLocalProvider(lp);
if (models.length === 0) {
logger.warn('No models found for local provider', { provider: lp.name });
continue;
}
// All local providers use OpenAI-compatible API
// (llama.cpp, Ollama with /v1 endpoint, LM Studio, etc.)
const apiType = 'openai-completions';
// llama.cpp and OpenAI-compatible servers expect /v1 prefix
// Ollama also supports OpenAI-compatible at /v1
const baseUrl = lp.apiType === 'ollama'
const baseUrl = lp.apiType === 'ollama'
? `${lp.url}/v1`
: lp.url.endsWith('/v1') ? lp.url : `${lp.url}/v1`;
piConfig.providers[providerId] = {
baseUrl,
apiKey: lp.auth?.type === 'api-key' ? lp.auth.apiKey : 'none',
api: apiType,
api: 'openai-completions',
models: models.map(m => ({
id: m.id,
name: m.name || m.id,
@@ -139,24 +79,15 @@ export async function syncLocalProvidersToPiConfig(): Promise<void> {
input: ['text'],
contextWindow: m.contextWindow || 128000,
maxTokens: m.maxTokens || 4096,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
},
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
})),
};
logger.info('Added local provider to Pi config', {
providerId,
providerName: lp.name,
modelCount: models.length
});
logger.info('Added local provider to Pi config', { providerId, providerName: lp.name, modelCount: models.length });
}
// Write updated config back to Pi
await Bun.write(PI_MODELS_FILE, JSON.stringify(piConfig, null, 2));
invalidateModelCache();
logger.info('Successfully synced local providers to Pi config');
} catch (err) {
logger.error('Failed to sync local providers to Pi config', { error: String(err) });
@@ -164,12 +95,12 @@ export async function syncLocalProvidersToPiConfig(): Promise<void> {
}
async function fetchModelsFromLocalProvider(
lp: LocalProvider
lp: LocalProvider,
): Promise<{ id: string; name?: string; contextWindow?: number; maxTokens?: number }[]> {
try {
const base = lp.url.replace(/\/+$/, '');
const path = lp.apiType === 'ollama' ? '/api/tags' : '/v1/models';
const headers: Record<string, string> = {};
if (lp.auth?.type === 'api-key') {
headers['Authorization'] = `Bearer ${lp.auth.apiKey}`;
@@ -184,40 +115,21 @@ async function fetchModelsFromLocalProvider(
clearTimeout(timer);
if (!res.ok) {
logger.error('Failed to fetch models from local provider', {
provider: lp.name,
status: res.status
});
logger.error('Failed to fetch models from local provider', { provider: lp.name, status: res.status });
return [];
}
const data = await res.json();
// Parse Ollama response
if (lp.apiType === 'ollama' && data.models) {
return data.models.map((m: any) => ({
id: m.name,
name: m.name,
contextWindow: 128000,
maxTokens: 4096,
}));
}
// Parse OpenAI-compatible response (including LM Studio)
else if (data.data) {
return data.data.map((m: any) => ({
id: m.id,
name: m.id,
contextWindow: 128000,
maxTokens: 4096,
}));
return data.models.map((m: any) => ({ id: m.name, name: m.name, contextWindow: 128000, maxTokens: 4096 }));
} else if (data.data) {
return data.data.map((m: any) => ({ id: m.id, name: m.id, contextWindow: 128000, maxTokens: 4096 }));
}
return [];
} catch (err) {
logger.error('Failed to fetch models from local provider', {
provider: lp.name,
error: String(err)
});
logger.error('Failed to fetch models from local provider', { provider: lp.name, error: String(err) });
return [];
}
}