Skip to content

Claude Code opened to any LLM — OpenAI, Gemini, DeepSeek, Ollama, and 200+ models via OpenAI-compatible API shim

TypeScript

Merge origin/main into codex/provider-profile-recommendations

Vasanthdev2004f51cd3aa159f
diff --cc README.md
index cef1c5c,74bc111..8a0690f
--- a/README.md
+++ b/README.md
@@@ -203,24 -226,21 +226,30 @@@ Notes
Use profile launchers to avoid repeated environment setup:
```bash
- # one-time profile bootstrap (best available provider)
-# one-time profile bootstrap (auto-detect ollama, otherwise openai)
++# one-time profile bootstrap (prefer viable local Ollama, otherwise OpenAI)
bun run profile:init
+# preview the best provider/model for your goal
+bun run profile:recommend -- --goal coding --benchmark
+
- # auto-apply the best available provider/model for your goal
++# auto-apply the best available local/openai provider/model for your goal
+bun run profile:auto -- --goal latency
+
+ # codex bootstrap (defaults to codexplan and ~/.codex/auth.json)
+ bun run profile:codex
+
# openai bootstrap with explicit key
bun run profile:init -- --provider openai --api-key sk-...
# ollama bootstrap with custom model
bun run profile:init -- --provider ollama --model llama3.1:8b
+# ollama bootstrap with intelligent model auto-selection
+bun run profile:init -- --provider ollama --goal coding
+
+ # codex bootstrap with a fast model alias
+ bun run profile:init -- --provider codex --model codexspark
+
# launch using persisted profile (.openclaude-profile.json)
bun run dev:profile
@@@ -231,13 -254,7 +263,15 @@@ bun run dev:opena
bun run dev:ollama
```
+`profile:recommend` ranks installed Ollama models for `latency`, `balanced`, or `coding`, and `profile:auto` can persist the recommendation directly.
+If no profile exists yet, `dev:profile` now uses the same goal-aware defaults when picking the initial model.
+
+Use `--provider ollama` when you want a local-only path. Auto mode falls back to OpenAI when no viable local chat model is installed.
+Goal-based Ollama selection only recommends among models that are already installed and reachable from Ollama.
+
- `dev:openai` and `dev:ollama` run `doctor:runtime` first and only launch the app if checks pass.
++Use `profile:codex` or `--provider codex` when you want the ChatGPT Codex backend.
++
+ `dev:openai`, `dev:ollama`, and `dev:codex` run `doctor:runtime` first and only launch the app if checks pass.
For `dev:ollama`, make sure Ollama is running locally before launch.
---
diff --cc package.json
index 6e28e36,0148fb0..60cab43
--- a/package.json
+++ b/package.json
@@@ -20,16 -21,15 +21,18 @@@
"dev:ollama": "bun run scripts/provider-launch.ts ollama",
"dev:ollama:fast": "bun run scripts/provider-launch.ts ollama --fast --bare",
"profile:init": "bun run scripts/provider-bootstrap.ts",
+ "profile:recommend": "bun run scripts/provider-recommend.ts",
+ "profile:auto": "bun run scripts/provider-recommend.ts --apply",
+ "profile:codex": "bun run profile:init -- --provider codex --model codexplan",
"profile:fast": "bun run profile:init -- --provider ollama --model llama3.2:3b",
"profile:code": "bun run profile:init -- --provider ollama --model qwen2.5-coder:7b",
"dev:fast": "bun run profile:fast && bun run dev:ollama:fast",
"dev:code": "bun run profile:code && bun run dev:profile",
"start": "node dist/cli.mjs",
+ "test:provider-recommendation": "node --test --experimental-strip-types src/utils/providerRecommendation.test.ts src/utils/providerProfile.test.ts",
"typecheck": "tsc --noEmit",
"smoke": "bun run build && node dist/cli.mjs --version",
+ "test:provider": "bun test src/services/api/*.test.ts",
"doctor:runtime": "bun run scripts/system-check.ts",
"doctor:runtime:json": "bun run scripts/system-check.ts --json",
"doctor:report": "bun run scripts/system-check.ts --out reports/doctor-runtime.json",
diff --cc scripts/provider-bootstrap.ts
index 7fc3ff5,34a407e..d54f49d
--- a/scripts/provider-bootstrap.ts
+++ b/scripts/provider-bootstrap.ts
@@@ -1,24 -1,23 +1,28 @@@
// @ts-nocheck
import { writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
+ import {
- DEFAULT_CODEX_BASE_URL,
+ resolveCodexApiCredentials,
+ } from '../src/services/api/providerConfig.js'
-
-type ProviderProfile = 'openai' | 'ollama' | 'codex'
-
-type ProfileFile = {
- profile: ProviderProfile
- env: {
- OPENAI_BASE_URL?: string
- OPENAI_MODEL?: string
- OPENAI_API_KEY?: string
- CODEX_API_KEY?: string
- }
- createdAt: string
-}
+import {
+ getGoalDefaultOpenAIModel,
+ normalizeRecommendationGoal,
+ recommendOllamaModel,
+} from '../src/utils/providerRecommendation.ts'
+import {
++ buildCodexProfileEnv,
+ buildOllamaProfileEnv,
+ buildOpenAIProfileEnv,
+ createProfileFile,
+ selectAutoProfile,
+ type ProfileFile,
+ type ProviderProfile,
+} from '../src/utils/providerProfile.ts'
+import {
+ getOllamaChatBaseUrl,
+ hasLocalOllama,
+ listOllamaModels,
+} from './provider-discovery.ts'
function parseArg(name: string): string | null {
const args = process.argv.slice(2)
@@@ -33,16 -32,27 +37,16 @@@ function parseProviderArg(): ProviderPr
return 'auto'
}
-async function hasLocalOllama(): Promise<boolean> {
- const endpoint = 'http://localhost:11434/api/tags'
- const controller = new AbortController()
- const timeout = setTimeout(() => controller.abort(), 1200)
+async function resolveOllamaModel(
+ argModel: string | null,
+ argBaseUrl: string | null,
+ goal: ReturnType<typeof normalizeRecommendationGoal>,
- ) : Promise<string | null> {
++): Promise<string | null> {
+ if (argModel) return argModel
- try {
- const response = await fetch(endpoint, {
- method: 'GET',
- signal: controller.signal,
- })
- return response.ok
- } catch {
- return false
- } finally {
- clearTimeout(timeout)
- }
-}
-
-function sanitizeApiKey(key: string | null): string | undefined {
- if (!key || key === 'SUA_CHAVE') return undefined
- return key
+ const discovered = await listOllamaModels(argBaseUrl || undefined)
+ const recommended = recommendOllamaModel(discovered, goal)
+ return recommended?.name ?? null
}
async function main(): Promise<void> {
@@@ -67,37 -68,34 +71,57 @@@
selected = provider
}
- const env: ProfileFile['env'] = {}
+ let env: ProfileFile['env']
if (selected === 'ollama') {
- env.OPENAI_BASE_URL = argBaseUrl || 'http://localhost:11434/v1'
- env.OPENAI_MODEL = argModel || process.env.OPENAI_MODEL || 'llama3.1:8b'
- const key = sanitizeApiKey(argApiKey || process.env.OPENAI_API_KEY || null)
- if (key) env.OPENAI_API_KEY = key
+ resolvedOllamaModel ??= await resolveOllamaModel(argModel, argBaseUrl, goal)
+ if (!resolvedOllamaModel) {
+ console.error('No viable Ollama chat model was discovered. Pull a chat model first or pass --model explicitly.')
+ process.exit(1)
+ }
+
+ env = buildOllamaProfileEnv(
+ resolvedOllamaModel,
+ {
+ baseUrl: argBaseUrl,
+ getOllamaChatBaseUrl,
+ },
+ )
+ } else if (selected === 'codex') {
- env.OPENAI_BASE_URL =
- argBaseUrl || process.env.OPENAI_BASE_URL || DEFAULT_CODEX_BASE_URL
- env.OPENAI_MODEL = argModel || process.env.OPENAI_MODEL || 'codexplan'
- const key = sanitizeApiKey(argApiKey || process.env.CODEX_API_KEY || null)
- if (key) {
- env.CODEX_API_KEY = key
- } else {
- const credentials = resolveCodexApiCredentials(process.env)
++ const builtEnv = buildCodexProfileEnv({
++ model: argModel,
++ baseUrl: argBaseUrl,
++ apiKey: argApiKey || process.env.CODEX_API_KEY || null,
++ processEnv: process.env,
++ })
++
++ if (!builtEnv) {
++ const credentials = resolveCodexApiCredentials(
++ argApiKey
++ ? { ...process.env, CODEX_API_KEY: argApiKey }
++ : process.env,
++ )
++ const authHint = credentials.authPath
++ ? ` or make sure ${credentials.authPath} exists`
++ : ''
+ if (!credentials.apiKey) {
- const authHint = credentials.authPath
- ? ` or make sure ${credentials.authPath} exists`
- : ''
+ console.error(`Codex profile requires CODEX_API_KEY${authHint}.`)
- process.exit(1)
++ } else {
++ console.error('Codex profile requires CHATGPT_ACCOUNT_ID or an auth.json that includes it.')
+ }
++ process.exit(1)
+ }
++
++ env = builtEnv
} else {
- env.OPENAI_BASE_URL = argBaseUrl || process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1'
- env.OPENAI_MODEL = argModel || process.env.OPENAI_MODEL || 'gpt-4o'
- const key = sanitizeApiKey(argApiKey || process.env.OPENAI_API_KEY || null)
- if (!key) {
+ const builtEnv = buildOpenAIProfileEnv({
+ goal,
- model:
- argModel ||
- process.env.OPENAI_MODEL ||
- getGoalDefaultOpenAIModel(goal),
++ model: argModel || null,
++ baseUrl: argBaseUrl || null,
+ apiKey: argApiKey || process.env.OPENAI_API_KEY || null,
- processEnv: {
- ...process.env,
- OPENAI_BASE_URL:
- argBaseUrl || process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1',
- },
++ processEnv: process.env,
+ })
+
+ if (!builtEnv) {
console.error('OpenAI profile requires a real API key. Use --api-key or set OPENAI_API_KEY.')
process.exit(1)
}
diff --cc scripts/provider-launch.ts
index d4e321c,74f2431..46a8741
--- a/scripts/provider-launch.ts
+++ b/scripts/provider-launch.ts
@@@ -2,21 -2,22 +2,24 @@@
import { spawn } from 'node:child_process'
import { existsSync, readFileSync } from 'node:fs'
import { resolve } from 'node:path'
+ import {
- DEFAULT_CODEX_BASE_URL,
+ resolveCodexApiCredentials,
+ } from '../src/services/api/providerConfig.js'
-
-type ProviderProfile = 'openai' | 'ollama' | 'codex'
-
-type ProfileFile = {
- profile: ProviderProfile
- env?: {
- OPENAI_BASE_URL?: string
- OPENAI_MODEL?: string
- OPENAI_API_KEY?: string
- CODEX_API_KEY?: string
- }
-}
+import {
+ normalizeRecommendationGoal,
+ recommendOllamaModel,
+} from '../src/utils/providerRecommendation.ts'
+import {
+ buildLaunchEnv,
+ selectAutoProfile,
+ type ProfileFile,
+ type ProviderProfile,
+} from '../src/utils/providerProfile.ts'
+import {
+ getOllamaChatBaseUrl,
+ hasLocalOllama,
+ listOllamaModels,
+} from './provider-discovery.ts'
type LaunchOptions = {
requestedProfile: ProviderProfile | 'auto' | null
@@@ -39,13 -37,7 +42,13 @@@ function parseLaunchOptions(argv: strin
continue
}
+ if (lower === '--goal') {
+ goal = normalizeRecommendationGoal(argv[i + 1] ?? null)
+ i++
+ continue
+ }
+
- if ((lower === 'auto' || lower === 'openai' || lower === 'ollama') && requestedProfile === 'auto') {
+ if ((lower === 'auto' || lower === 'openai' || lower === 'ollama' || lower === 'codex') && requestedProfile === 'auto') {
requestedProfile = lower as ProviderProfile | 'auto'
continue
}
@@@ -134,7 -171,7 +141,7 @@@ async function main(): Promise<void>
const options = parseLaunchOptions(process.argv.slice(2))
const requestedProfile = options.requestedProfile
if (!requestedProfile) {
- console.error('Usage: bun run scripts/provider-launch.ts [openai|ollama|auto] [--fast] [-- <cli args>]')
- console.error('Usage: bun run scripts/provider-launch.ts [openai|ollama|codex|auto] [--fast] [-- <cli args>]')
++ console.error('Usage: bun run scripts/provider-launch.ts [openai|ollama|codex|auto] [--fast] [--goal <latency|balanced|coding>] [-- <cli args>]')
process.exit(1)
}
@@@ -155,21 -188,7 +162,24 @@@
profile = requestedProfile
}
- if (profile === 'ollama' && persisted?.profile !== 'ollama') {
- const env = buildEnv(profile, persisted)
++ if (
++ profile === 'ollama' &&
++ (persisted?.profile !== 'ollama' || !persisted?.env?.OPENAI_MODEL)
++ ) {
+ resolvedOllamaModel ??= await resolveOllamaDefaultModel(options.goal)
+ if (!resolvedOllamaModel) {
+ console.error('No viable Ollama chat model was discovered. Pull a chat model first or save one with `bun run profile:init -- --provider ollama --model <model>`.')
+ process.exit(1)
+ }
+ }
+
+ const env = await buildLaunchEnv({
+ profile,
+ persisted,
+ goal: options.goal,
+ getOllamaChatBaseUrl,
+ resolveOllamaDefaultModel: async () => resolvedOllamaModel || 'llama3.1:8b',
+ })
if (options.fast) {
applyFastFlags(env)
}
@@@ -179,6 -198,17 +189,22 @@@
process.exit(1)
}
+ if (profile === 'codex') {
+ const credentials = resolveCodexApiCredentials(env)
+ if (!credentials.apiKey) {
+ const authHint = credentials.authPath
+ ? ` or make sure ${credentials.authPath} exists`
+ : ''
+ console.error(`CODEX_API_KEY is required for codex profile${authHint}. Run: bun run profile:init -- --provider codex --model codexplan`)
+ process.exit(1)
+ }
++
++ if (!credentials.accountId) {
++ console.error('CHATGPT_ACCOUNT_ID is required for codex profile. Set CHATGPT_ACCOUNT_ID/CODEX_ACCOUNT_ID or use an auth.json that includes it.')
++ process.exit(1)
++ }
+ }
+
printSummary(profile, env)
const doctorCode = await runCommand('bun run scripts/system-check.ts', env)
diff --cc src/utils/providerProfile.test.ts
index f549584,0000000..e005075
mode 100644,000000..100644
--- a/src/utils/providerProfile.test.ts
+++ b/src/utils/providerProfile.test.ts
@@@ -1,92 -1,0 +1,307 @@@
+import assert from 'node:assert/strict'
++import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
++import { tmpdir } from 'node:os'
++import { join } from 'node:path'
+import test from 'node:test'
+
+import {
++ buildCodexProfileEnv,
+ buildLaunchEnv,
+ buildOllamaProfileEnv,
++ buildOpenAIProfileEnv,
+ selectAutoProfile,
+ type ProfileFile,
+} from './providerProfile.ts'
+
+function profile(profile: ProfileFile['profile'], env: ProfileFile['env']): ProfileFile {
+ return {
+ profile,
+ env,
+ createdAt: '2026-04-01T00:00:00.000Z',
+ }
+}
+
++const missingCodexAuthPath = join(tmpdir(), 'openclaude-missing-codex-auth.json')
++
+test('matching persisted ollama env is reused for ollama launch', async () => {
+ const env = await buildLaunchEnv({
+ profile: 'ollama',
+ persisted: profile('ollama', {
+ OPENAI_BASE_URL: 'http://127.0.0.1:11435/v1',
+ OPENAI_MODEL: 'mistral:7b-instruct',
+ }),
+ goal: 'balanced',
+ processEnv: {},
+ getOllamaChatBaseUrl: () => 'http://localhost:11434/v1',
+ resolveOllamaDefaultModel: async () => 'llama3.1:8b',
+ })
+
+ assert.equal(env.OPENAI_BASE_URL, 'http://127.0.0.1:11435/v1')
+ assert.equal(env.OPENAI_MODEL, 'mistral:7b-instruct')
+})
+
+test('ollama launch ignores mismatched persisted openai env and shell model fallback', async () => {
+ const env = await buildLaunchEnv({
+ profile: 'ollama',
+ persisted: profile('openai', {
+ OPENAI_BASE_URL: 'https://api.openai.com/v1',
+ OPENAI_MODEL: 'gpt-4o',
+ OPENAI_API_KEY: 'sk-persisted',
+ }),
+ goal: 'coding',
+ processEnv: {
+ OPENAI_BASE_URL: 'https://api.deepseek.com/v1',
+ OPENAI_MODEL: 'gpt-4o-mini',
++ OPENAI_API_KEY: 'sk-live',
++ CODEX_API_KEY: 'codex-live',
++ CHATGPT_ACCOUNT_ID: 'acct_live',
+ },
+ getOllamaChatBaseUrl: () => 'http://localhost:11434/v1',
+ resolveOllamaDefaultModel: async () => 'qwen2.5-coder:7b',
+ })
+
+ assert.equal(env.OPENAI_BASE_URL, 'http://localhost:11434/v1')
+ assert.equal(env.OPENAI_MODEL, 'qwen2.5-coder:7b')
++ assert.equal(env.OPENAI_API_KEY, undefined)
++ assert.equal(env.CODEX_API_KEY, undefined)
++ assert.equal(env.CHATGPT_ACCOUNT_ID, undefined)
+})
+
+test('openai launch ignores mismatched persisted ollama env', async () => {
+ const env = await buildLaunchEnv({
+ profile: 'openai',
+ persisted: profile('ollama', {
+ OPENAI_BASE_URL: 'http://localhost:11434/v1',
+ OPENAI_MODEL: 'llama3.1:8b',
+ }),
+ goal: 'latency',
+ processEnv: {
+ OPENAI_API_KEY: 'sk-live',
++ CODEX_API_KEY: 'codex-live',
++ CHATGPT_ACCOUNT_ID: 'acct_live',
+ },
+ getOllamaChatBaseUrl: () => 'http://localhost:11434/v1',
+ resolveOllamaDefaultModel: async () => 'llama3.1:8b',
+ })
+
+ assert.equal(env.OPENAI_BASE_URL, 'https://api.openai.com/v1')
+ assert.equal(env.OPENAI_MODEL, 'gpt-4o-mini')
+ assert.equal(env.OPENAI_API_KEY, 'sk-live')
++ assert.equal(env.CODEX_API_KEY, undefined)
++ assert.equal(env.CHATGPT_ACCOUNT_ID, undefined)
++})
++
++test('openai launch ignores codex shell transport hints', async () => {
++ const env = await buildLaunchEnv({
++ profile: 'openai',
++ persisted: null,
++ goal: 'balanced',
++ processEnv: {
++ OPENAI_API_KEY: 'sk-live',
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexplan',
++ },
++ })
++
++ assert.equal(env.OPENAI_BASE_URL, 'https://api.openai.com/v1')
++ assert.equal(env.OPENAI_MODEL, 'gpt-4o')
++ assert.equal(env.OPENAI_API_KEY, 'sk-live')
++})
++
++test('openai launch ignores codex persisted transport hints', async () => {
++ const env = await buildLaunchEnv({
++ profile: 'openai',
++ persisted: profile('openai', {
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexplan',
++ OPENAI_API_KEY: 'sk-persisted',
++ }),
++ goal: 'balanced',
++ processEnv: {
++ OPENAI_API_KEY: 'sk-live',
++ },
++ })
++
++ assert.equal(env.OPENAI_BASE_URL, 'https://api.openai.com/v1')
++ assert.equal(env.OPENAI_MODEL, 'gpt-4o')
++ assert.equal(env.OPENAI_API_KEY, 'sk-live')
++})
++
++test('matching persisted codex env is reused for codex launch', async () => {
++ const env = await buildLaunchEnv({
++ profile: 'codex',
++ persisted: profile('codex', {
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexspark',
++ CODEX_API_KEY: 'codex-persisted',
++ CHATGPT_ACCOUNT_ID: 'acct_persisted',
++ }),
++ goal: 'balanced',
++ processEnv: {
++ CODEX_AUTH_JSON_PATH: missingCodexAuthPath,
++ },
++ })
++
++ assert.equal(env.OPENAI_BASE_URL, 'https://chatgpt.com/backend-api/codex')
++ assert.equal(env.OPENAI_MODEL, 'codexspark')
++ assert.equal(env.CODEX_API_KEY, 'codex-persisted')
++ assert.equal(env.CHATGPT_ACCOUNT_ID, 'acct_persisted')
++})
++
++test('codex launch normalizes poisoned persisted base urls', async () => {
++ const env = await buildLaunchEnv({
++ profile: 'codex',
++ persisted: profile('codex', {
++ OPENAI_BASE_URL: 'https://api.openai.com/v1',
++ OPENAI_MODEL: 'codexspark',
++ CHATGPT_ACCOUNT_ID: 'acct_persisted',
++ }),
++ goal: 'balanced',
++ processEnv: {
++ CODEX_AUTH_JSON_PATH: missingCodexAuthPath,
++ },
++ })
++
++ assert.equal(env.OPENAI_BASE_URL, 'https://chatgpt.com/backend-api/codex')
++ assert.equal(env.OPENAI_MODEL, 'codexspark')
++})
++
++test('codex launch ignores mismatched persisted openai env', async () => {
++ const env = await buildLaunchEnv({
++ profile: 'codex',
++ persisted: profile('openai', {
++ OPENAI_BASE_URL: 'https://api.openai.com/v1',
++ OPENAI_MODEL: 'gpt-4o',
++ OPENAI_API_KEY: 'sk-persisted',
++ }),
++ goal: 'balanced',
++ processEnv: {
++ OPENAI_BASE_URL: 'https://api.openai.com/v1',
++ OPENAI_MODEL: 'gpt-4o-mini',
++ OPENAI_API_KEY: 'sk-live',
++ CODEX_API_KEY: 'codex-live',
++ CHATGPT_ACCOUNT_ID: 'acct_live',
++ },
++ })
++
++ assert.equal(env.OPENAI_BASE_URL, 'https://chatgpt.com/backend-api/codex')
++ assert.equal(env.OPENAI_MODEL, 'codexplan')
++ assert.equal(env.OPENAI_API_KEY, undefined)
++ assert.equal(env.CODEX_API_KEY, 'codex-live')
++ assert.equal(env.CHATGPT_ACCOUNT_ID, 'acct_live')
++})
++
++test('codex launch ignores placeholder codex env keys', async () => {
++ const env = await buildLaunchEnv({
++ profile: 'codex',
++ persisted: profile('codex', {
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexspark',
++ CODEX_API_KEY: 'codex-persisted',
++ CHATGPT_ACCOUNT_ID: 'acct_persisted',
++ }),
++ goal: 'balanced',
++ processEnv: {
++ CODEX_API_KEY: 'SUA_CHAVE',
++ CODEX_AUTH_JSON_PATH: missingCodexAuthPath,
++ },
++ })
++
++ assert.equal(env.CODEX_API_KEY, 'codex-persisted')
++ assert.equal(env.CHATGPT_ACCOUNT_ID, 'acct_persisted')
++})
++
++test('codex launch prefers auth account id over stale persisted value', async () => {
++ const codexHome = mkdtempSync(join(tmpdir(), 'openclaude-codex-'))
++ try {
++ writeFileSync(
++ join(codexHome, 'auth.json'),
++ JSON.stringify({
++ access_token: 'codex-live',
++ account_id: 'acct_auth',
++ }),
++ 'utf8',
++ )
++
++ const env = await buildLaunchEnv({
++ profile: 'codex',
++ persisted: profile('codex', {
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexspark',
++ CHATGPT_ACCOUNT_ID: 'acct_persisted',
++ }),
++ goal: 'balanced',
++ processEnv: {
++ CODEX_HOME: codexHome,
++ },
++ })
++
++ assert.equal(env.CHATGPT_ACCOUNT_ID, 'acct_auth')
++ } finally {
++ rmSync(codexHome, { recursive: true, force: true })
++ }
+})
+
+test('ollama profiles never persist openai api keys', () => {
+ const env = buildOllamaProfileEnv('llama3.1:8b', {
+ getOllamaChatBaseUrl: () => 'http://localhost:11434/v1',
+ })
+
+ assert.deepEqual(env, {
+ OPENAI_BASE_URL: 'http://localhost:11434/v1',
+ OPENAI_MODEL: 'llama3.1:8b',
+ })
+ assert.equal('OPENAI_API_KEY' in env, false)
+})
+
++test('codex profiles accept explicit codex credentials', () => {
++ const env = buildCodexProfileEnv({
++ model: 'codexspark',
++ apiKey: 'codex-live',
++ processEnv: {
++ CHATGPT_ACCOUNT_ID: 'acct_123',
++ },
++ })
++
++ assert.deepEqual(env, {
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexspark',
++ CODEX_API_KEY: 'codex-live',
++ CHATGPT_ACCOUNT_ID: 'acct_123',
++ })
++})
++
++test('codex profiles require a chatgpt account id', () => {
++ const env = buildCodexProfileEnv({
++ model: 'codexspark',
++ apiKey: 'codex-live',
++ processEnv: {
++ CODEX_AUTH_JSON_PATH: missingCodexAuthPath,
++ },
++ })
++
++ assert.equal(env, null)
++})
++
++test('openai profiles ignore codex shell transport hints', () => {
++ const env = buildOpenAIProfileEnv({
++ goal: 'balanced',
++ apiKey: 'sk-live',
++ processEnv: {
++ OPENAI_BASE_URL: 'https://chatgpt.com/backend-api/codex',
++ OPENAI_MODEL: 'codexplan',
++ OPENAI_API_KEY: 'sk-live',
++ },
++ })
++
++ assert.deepEqual(env, {
++ OPENAI_BASE_URL: 'https://api.openai.com/v1',
++ OPENAI_MODEL: 'gpt-4o',
++ OPENAI_API_KEY: 'sk-live',
++ })
++})
++
+test('auto profile falls back to openai when no viable ollama model exists', () => {
+ assert.equal(selectAutoProfile(null), 'openai')
+ assert.equal(selectAutoProfile('qwen2.5-coder:7b'), 'ollama')
+})
diff --cc src/utils/providerProfile.ts
index 2cd7188,0000000..466c966
mode 100644,000000..100644
--- a/src/utils/providerProfile.ts
+++ b/src/utils/providerProfile.ts
@@@ -1,123 -1,0 +1,232 @@@
++import {
++ DEFAULT_CODEX_BASE_URL,
++ DEFAULT_OPENAI_BASE_URL,
++ isCodexBaseUrl,
++ resolveCodexApiCredentials,
++ resolveProviderRequest,
++} from '../services/api/providerConfig.ts'
+import {
+ getGoalDefaultOpenAIModel,
+ type RecommendationGoal,
+} from './providerRecommendation.ts'
+
- export type ProviderProfile = 'openai' | 'ollama'
++export type ProviderProfile = 'openai' | 'ollama' | 'codex'
+
+export type ProfileEnv = {
+ OPENAI_BASE_URL?: string
+ OPENAI_MODEL?: string
+ OPENAI_API_KEY?: string
++ CODEX_API_KEY?: string
++ CHATGPT_ACCOUNT_ID?: string
++ CODEX_ACCOUNT_ID?: string
+}
+
+export type ProfileFile = {
+ profile: ProviderProfile
+ env: ProfileEnv
+ createdAt: string
+}
+
+export function sanitizeApiKey(
+ key: string | null | undefined,
+): string | undefined {
+ if (!key || key === 'SUA_CHAVE') return undefined
+ return key
+}
+
+export function buildOllamaProfileEnv(
+ model: string,
+ options: {
+ baseUrl?: string | null
+ getOllamaChatBaseUrl: (baseUrl?: string) => string
+ },
+): ProfileEnv {
+ return {
+ OPENAI_BASE_URL: options.getOllamaChatBaseUrl(options.baseUrl ?? undefined),
+ OPENAI_MODEL: model,
+ }
+}
+
+export function buildOpenAIProfileEnv(options: {
+ goal: RecommendationGoal
+ model?: string | null
++ baseUrl?: string | null
+ apiKey?: string | null
+ processEnv?: NodeJS.ProcessEnv
+}): ProfileEnv | null {
+ const processEnv = options.processEnv ?? process.env
+ const key = sanitizeApiKey(options.apiKey ?? processEnv.OPENAI_API_KEY)
+ if (!key) {
+ return null
+ }
+
++ const defaultModel = getGoalDefaultOpenAIModel(options.goal)
++ const shellOpenAIRequest = resolveProviderRequest({
++ model: processEnv.OPENAI_MODEL,
++ baseUrl: processEnv.OPENAI_BASE_URL,
++ fallbackModel: defaultModel,
++ })
++ const useShellOpenAIConfig = shellOpenAIRequest.transport === 'chat_completions'
++
+ return {
- OPENAI_BASE_URL: processEnv.OPENAI_BASE_URL || 'https://api.openai.com/v1',
- OPENAI_MODEL: options.model || getGoalDefaultOpenAIModel(options.goal),
++ OPENAI_BASE_URL:
++ options.baseUrl ||
++ (useShellOpenAIConfig ? processEnv.OPENAI_BASE_URL : undefined) ||
++ DEFAULT_OPENAI_BASE_URL,
++ OPENAI_MODEL:
++ options.model ||
++ (useShellOpenAIConfig ? processEnv.OPENAI_MODEL : undefined) ||
++ defaultModel,
+ OPENAI_API_KEY: key,
+ }
+}
+
++export function buildCodexProfileEnv(options: {
++ model?: string | null
++ baseUrl?: string | null
++ apiKey?: string | null
++ processEnv?: NodeJS.ProcessEnv
++}): ProfileEnv | null {
++ const processEnv = options.processEnv ?? process.env
++ const key = sanitizeApiKey(options.apiKey ?? processEnv.CODEX_API_KEY)
++ const credentialEnv = key
++ ? ({ ...processEnv, CODEX_API_KEY: key } as NodeJS.ProcessEnv)
++ : processEnv
++ const credentials = resolveCodexApiCredentials(credentialEnv)
++ if (!credentials.apiKey || !credentials.accountId) {
++ return null
++ }
++
++ const env: ProfileEnv = {
++ OPENAI_BASE_URL: options.baseUrl || DEFAULT_CODEX_BASE_URL,
++ OPENAI_MODEL: options.model || 'codexplan',
++ }
++
++ if (key) {
++ env.CODEX_API_KEY = key
++ }
++
++ env.CHATGPT_ACCOUNT_ID = credentials.accountId
++
++ return env
++}
++
+export function createProfileFile(
+ profile: ProviderProfile,
+ env: ProfileEnv,
+): ProfileFile {
+ return {
+ profile,
+ env,
+ createdAt: new Date().toISOString(),
+ }
+}
+
+export function selectAutoProfile(
+ recommendedOllamaModel: string | null,
+): ProviderProfile {
+ return recommendedOllamaModel ? 'ollama' : 'openai'
+}
+
+export async function buildLaunchEnv(options: {
+ profile: ProviderProfile
+ persisted: ProfileFile | null
+ goal: RecommendationGoal
+ processEnv?: NodeJS.ProcessEnv
+ getOllamaChatBaseUrl?: (baseUrl?: string) => string
+ resolveOllamaDefaultModel?: (goal: RecommendationGoal) => Promise<string>
+}): Promise<NodeJS.ProcessEnv> {
+ const processEnv = options.processEnv ?? process.env
+ const persistedEnv =
+ options.persisted?.profile === options.profile
+ ? options.persisted.env ?? {}
+ : {}
+
+ const env: NodeJS.ProcessEnv = {
+ ...processEnv,
+ CLAUDE_CODE_USE_OPENAI: '1',
+ }
+
+ if (options.profile === 'ollama') {
+ const getOllamaBaseUrl =
+ options.getOllamaChatBaseUrl ?? (() => 'http://localhost:11434/v1')
+ const resolveOllamaModel =
+ options.resolveOllamaDefaultModel ?? (async () => 'llama3.1:8b')
+
+ env.OPENAI_BASE_URL = persistedEnv.OPENAI_BASE_URL || getOllamaBaseUrl()
+ env.OPENAI_MODEL =
+ persistedEnv.OPENAI_MODEL ||
+ (await resolveOllamaModel(options.goal))
+
- if (!processEnv.OPENAI_API_KEY || processEnv.OPENAI_API_KEY === 'SUA_CHAVE') {
- delete env.OPENAI_API_KEY
++ delete env.OPENAI_API_KEY
++ delete env.CODEX_API_KEY
++ delete env.CHATGPT_ACCOUNT_ID
++ delete env.CODEX_ACCOUNT_ID
++
++ return env
++ }
++
++ if (options.profile === 'codex') {
++ env.OPENAI_BASE_URL =
++ persistedEnv.OPENAI_BASE_URL && isCodexBaseUrl(persistedEnv.OPENAI_BASE_URL)
++ ? persistedEnv.OPENAI_BASE_URL
++ : DEFAULT_CODEX_BASE_URL
++ env.OPENAI_MODEL = persistedEnv.OPENAI_MODEL || 'codexplan'
++ delete env.OPENAI_API_KEY
++
++ const codexKey =
++ sanitizeApiKey(processEnv.CODEX_API_KEY) ||
++ sanitizeApiKey(persistedEnv.CODEX_API_KEY)
++ const liveCodexCredentials = resolveCodexApiCredentials(processEnv)
++ const codexAccountId =
++ processEnv.CHATGPT_ACCOUNT_ID ||
++ processEnv.CODEX_ACCOUNT_ID ||
++ liveCodexCredentials.accountId ||
++ persistedEnv.CHATGPT_ACCOUNT_ID ||
++ persistedEnv.CODEX_ACCOUNT_ID
++ if (codexKey) {
++ env.CODEX_API_KEY = codexKey
++ } else {
++ delete env.CODEX_API_KEY
+ }
+
++ if (codexAccountId) {
++ env.CHATGPT_ACCOUNT_ID = codexAccountId
++ } else {
++ delete env.CHATGPT_ACCOUNT_ID
++ }
++ delete env.CODEX_ACCOUNT_ID
++
+ return env
+ }
+
++ const defaultOpenAIModel = getGoalDefaultOpenAIModel(options.goal)
++ const shellOpenAIRequest = resolveProviderRequest({
++ model: processEnv.OPENAI_MODEL,
++ baseUrl: processEnv.OPENAI_BASE_URL,
++ fallbackModel: defaultOpenAIModel,
++ })
++ const persistedOpenAIRequest = resolveProviderRequest({
++ model: persistedEnv.OPENAI_MODEL,
++ baseUrl: persistedEnv.OPENAI_BASE_URL,
++ fallbackModel: defaultOpenAIModel,
++ })
++ const useShellOpenAIConfig = shellOpenAIRequest.transport === 'chat_completions'
++ const usePersistedOpenAIConfig =
++ (!persistedEnv.OPENAI_MODEL && !persistedEnv.OPENAI_BASE_URL) ||
++ persistedOpenAIRequest.transport === 'chat_completions'
++
+ env.OPENAI_BASE_URL =
- processEnv.OPENAI_BASE_URL ||
- persistedEnv.OPENAI_BASE_URL ||
- 'https://api.openai.com/v1'
++ (useShellOpenAIConfig ? processEnv.OPENAI_BASE_URL : undefined) ||
++ (usePersistedOpenAIConfig ? persistedEnv.OPENAI_BASE_URL : undefined) ||
++ DEFAULT_OPENAI_BASE_URL
+ env.OPENAI_MODEL =
- processEnv.OPENAI_MODEL ||
- persistedEnv.OPENAI_MODEL ||
- getGoalDefaultOpenAIModel(options.goal)
++ (useShellOpenAIConfig ? processEnv.OPENAI_MODEL : undefined) ||
++ (usePersistedOpenAIConfig ? persistedEnv.OPENAI_MODEL : undefined) ||
++ defaultOpenAIModel
+ env.OPENAI_API_KEY = processEnv.OPENAI_API_KEY || persistedEnv.OPENAI_API_KEY
++ delete env.CODEX_API_KEY
++ delete env.CHATGPT_ACCOUNT_ID
++ delete env.CODEX_ACCOUNT_ID
+ return env
+}