Something went wrong. Try again.
Open source agent hosting on the AT Protocol
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889// Model access for everything languid runs headlessly (the harness, CLI checks).//// Uses pi's ModelRuntime, the same Models collection the pi CLI uses:// - every built-in provider,// - credentials from pi's auth.json (~/.pi/agent/auth.json, or// $PI_CODING_AGENT_DIR/auth.json), so `pi /login` subscription logins// (xAI, Anthropic Pro/Max, ChatGPT, Copilot, ...) work here too,// - environment API keys when nothing is stored for a provider,// - OAuth refresh under pi's file lock, so a running pi and languid never// both refresh (and invalidate) the same rotating token.
// - EXTRA_MODELS, models the bundled catalog doesn't list yet, added on top of pi's models.json.
import fs from 'node:fs'import os from 'node:os'import path from 'node:path'import { getAgentDir, ModelRuntime } from '@earendil-works/pi-coding-agent'
/** * Models served by a built-in provider that pi's bundled catalog doesn't have yet, as models.json * entries. Prices are the provider's, per million tokens; budgets are metered with them, because * Together's responses carry no cost. Drop an entry once the catalog lists the model. */export const EXTRA_MODELS: Record<string, Record<string, unknown>[]> = { together: [ { id: 'thinkingmachines/Inkling-2-Small', name: 'Inkling 2 Small', reasoning: true, input: ['text', 'image'], contextWindow: 1_048_576, maxTokens: 65_536, cost: { input: 0.3, output: 1, cacheRead: 0.03, cacheWrite: 0 }, ...selfHostedInkling(), }, ],}
/** The house Inkling 2 Small, as `provider/model`. */export const INKLING = 'together/thinkingmachines/Inkling-2-Small'
/** * Inkling 2 Small on our own GPUs (infra/modal): when LANGUID_INKLING_BASE_URL and * LANGUID_INKLING_API_KEY are set, the house Inkling keeps its provider and id but is served from * that OpenAI-compatible URL instead of Together's, and calls to it cost nobody anything (flatRate). */export function selfHostedInkling(env: NodeJS.ProcessEnv = process.env): { baseUrl: string; headers: Record<string, string>; contextWindow: number } | undefined { const baseUrl = env.LANGUID_INKLING_BASE_URL?.trim() const key = env.LANGUID_INKLING_API_KEY?.trim() if (!baseUrl || !key) return undefined // The server's --max-model-len (infra/modal/inkling.py), not the model's 1M. return { baseUrl, headers: { Authorization: `Bearer ${key}` }, contextWindow: 131_072 }}
/** Whether calls to `model` are paid for at a flat rate (our own GPUs), so are free to whoever makes them. */export function flatRate(model: { provider?: string; id?: string }, env: NodeJS.ProcessEnv = process.env): boolean { return `${model.provider}/${model.id}` === INKLING && !!selfHostedInkling(env)}
/** * pi's models.json (if there is one) with EXTRA_MODELS added, written to a private temp file; the * owner's own entries win. Undefined when the file can't be read as JSON: then pi's file is used as is. */export function mergedModelsPath(extra = EXTRA_MODELS, source = path.join(getAgentDir(), 'models.json')): string | undefined { let config: { providers?: Record<string, { models?: { id: string }[] }> } = {} try { if (fs.existsSync(source)) config = JSON.parse(fs.readFileSync(source, 'utf8')) } catch { return undefined } const providers = (config.providers ??= {}) for (const [provider, models] of Object.entries(extra)) { const entry = (providers[provider] ??= {}) const have = new Set((entry.models ??= []).map((m) => m.id)) for (const m of models) if (!have.has(m.id as string)) entry.models.push(m as { id: string }) } const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'languid-models-')) const file = path.join(dir, 'models.json') fs.writeFileSync(file, JSON.stringify(config), { mode: 0o600 }) return file}
let runtime: Promise<ModelRuntime> | undefined
export function createModels(): Promise<ModelRuntime> { runtime ??= ModelRuntime.create({ modelsPath: mergedModelsPath() }) return runtime}