|
1 | | -import { mkdir, readFile, writeFile } from 'node:fs/promises' |
| 1 | +import { mkdir, readFile, stat, writeFile } from 'node:fs/promises' |
2 | 2 | import { homedir } from 'node:os' |
3 | 3 | import { dirname, join } from 'node:path' |
4 | 4 | import type { ApplicationPaths } from './paths.ts' |
@@ -219,3 +219,124 @@ export async function healInstalledConfigs(paths: ApplicationPaths): Promise<str |
219 | 219 | await writeFile(stampPath, `${VERSION}\n`) |
220 | 220 | return healed |
221 | 221 | } |
| 222 | + |
| 223 | +export interface PiResult { |
| 224 | + path: string |
| 225 | + applied: boolean |
| 226 | + manual: string | null |
| 227 | +} |
| 228 | + |
| 229 | +function piModelsPath(): string { |
| 230 | + return join(process.env.PI_CODING_AGENT_DIR ?? join(homedir(), '.pi', 'agent'), 'models.json') |
| 231 | +} |
| 232 | + |
| 233 | +const piProviderKeys = ['tokenmaxx-anthropic', 'tokenmaxx-openai'] |
| 234 | + |
| 235 | +// The anthropic ids pair with an API-key account (subscription auth is not for |
| 236 | +// third-party harnesses); gpt-5.6-sol is the one id the ChatGPT codex backend |
| 237 | +// accepts for subscription accounts. |
| 238 | +const piAnthropicModelIds = ['claude-opus-4-8', 'claude-sonnet-4-6'] |
| 239 | +const piOpenaiModelIds = ['gpt-5.6-sol'] |
| 240 | + |
| 241 | +function piProviders(paths: ApplicationPaths): Record<string, unknown> { |
| 242 | + const models = (ids: readonly string[]) => ids.map(id => ({ id, reasoning: true })) |
| 243 | + return { |
| 244 | + 'tokenmaxx-anthropic': { |
| 245 | + api: 'anthropic-messages', |
| 246 | + apiKey: dummyAuthToken, |
| 247 | + baseUrl: proxyBaseUrl(paths, 'anthropic'), |
| 248 | + models: models(piAnthropicModelIds) |
| 249 | + }, |
| 250 | + 'tokenmaxx-openai': { |
| 251 | + api: 'openai-responses', |
| 252 | + apiKey: dummyAuthToken, |
| 253 | + baseUrl: proxyBaseUrl(paths, 'openai'), |
| 254 | + models: models(piOpenaiModelIds) |
| 255 | + } |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +function parseJsonObject(raw: string): Record<string, unknown> | null { |
| 260 | + if (raw.trim().length === 0) { |
| 261 | + return {} |
| 262 | + } |
| 263 | + try { |
| 264 | + const parsed = JSON.parse(raw) |
| 265 | + return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) |
| 266 | + ? (parsed as Record<string, unknown>) |
| 267 | + : null |
| 268 | + } catch { |
| 269 | + return null |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +function ensureObject(parent: Record<string, unknown>, key: string): Record<string, unknown> { |
| 274 | + const value = parent[key] |
| 275 | + if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 276 | + parent[key] = {} |
| 277 | + } |
| 278 | + return parent[key] as Record<string, unknown> |
| 279 | +} |
| 280 | + |
| 281 | +async function writePiProviders( |
| 282 | + providers: Record<string, unknown> | null, |
| 283 | + manual: string |
| 284 | +): Promise<PiResult> { |
| 285 | + const path = piModelsPath() |
| 286 | + const raw = await readFileOrEmpty(path) |
| 287 | + const config = parseJsonObject(raw) |
| 288 | + if (config === null) { |
| 289 | + return { applied: false, manual, path } |
| 290 | + } |
| 291 | + const bucket = ensureObject(config, 'providers') |
| 292 | + for (const key of piProviderKeys) { |
| 293 | + delete bucket[key] |
| 294 | + } |
| 295 | + if (providers !== null) { |
| 296 | + Object.assign(bucket, providers) |
| 297 | + } |
| 298 | + await mkdir(dirname(path), { recursive: true }) |
| 299 | + await writeFile(path, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 }) |
| 300 | + return { applied: true, manual: null, path } |
| 301 | +} |
| 302 | + |
| 303 | +// pi re-reads models.json every time /model opens, so no restart is needed. |
| 304 | +export async function installPiConfig(paths: ApplicationPaths): Promise<PiResult> { |
| 305 | + return writePiProviders( |
| 306 | + piProviders(paths), |
| 307 | + `could not parse it as JSON — add this under providers yourself:\n${JSON.stringify(piProviders(paths), null, 2)}` |
| 308 | + ) |
| 309 | +} |
| 310 | + |
| 311 | +export async function uninstallPiConfig(): Promise<PiResult> { |
| 312 | + const raw = await readFile(piModelsPath(), 'utf8').catch(() => null) |
| 313 | + if (raw === null) { |
| 314 | + return { applied: false, manual: null, path: piModelsPath() } |
| 315 | + } |
| 316 | + return writePiProviders( |
| 317 | + null, |
| 318 | + 'could not parse it as JSON — remove the tokenmaxx-anthropic and tokenmaxx-openai providers yourself' |
| 319 | + ) |
| 320 | +} |
| 321 | + |
| 322 | +export interface PiStatus { |
| 323 | + present: boolean |
| 324 | + routed: boolean |
| 325 | +} |
| 326 | + |
| 327 | +// pi counts as present when its binary is on PATH or its agent directory |
| 328 | +// exists — someone who installed pi but never launched it has only the binary. |
| 329 | +export async function piStatus( |
| 330 | + which: (binary: string) => string | null = Bun.which |
| 331 | +): Promise<PiStatus> { |
| 332 | + const path = piModelsPath() |
| 333 | + const raw = await readFile(path, 'utf8').catch(() => null) |
| 334 | + const present = |
| 335 | + raw !== null || |
| 336 | + which('pi') !== null || |
| 337 | + (await stat(dirname(dirname(path))).then( |
| 338 | + () => true, |
| 339 | + () => false |
| 340 | + )) |
| 341 | + return { present, routed: raw?.includes('tokenmaxx-anthropic') ?? false } |
| 342 | +} |
0 commit comments