Preserve provider recommendation workflows while integrating Codex profile support, safer launch isolation, and updated docs/scripts from upstream main.
225 lines
6.5 KiB
TypeScript
225 lines
6.5 KiB
TypeScript
// @ts-nocheck
|
|
import { spawn } from 'node:child_process'
|
|
import { existsSync, readFileSync } from 'node:fs'
|
|
import { resolve } from 'node:path'
|
|
import {
|
|
resolveCodexApiCredentials,
|
|
} from '../src/services/api/providerConfig.js'
|
|
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
|
|
passthroughArgs: string[]
|
|
fast: boolean
|
|
goal: ReturnType<typeof normalizeRecommendationGoal>
|
|
}
|
|
|
|
function parseLaunchOptions(argv: string[]): LaunchOptions {
|
|
let requestedProfile: ProviderProfile | 'auto' | null = 'auto'
|
|
const passthroughArgs: string[] = []
|
|
let fast = false
|
|
let goal = normalizeRecommendationGoal(process.env.OPENCLAUDE_PROFILE_GOAL)
|
|
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const arg = argv[i]!
|
|
const lower = arg.toLowerCase()
|
|
if (lower === '--fast') {
|
|
fast = true
|
|
continue
|
|
}
|
|
|
|
if (lower === '--goal') {
|
|
goal = normalizeRecommendationGoal(argv[i + 1] ?? null)
|
|
i++
|
|
continue
|
|
}
|
|
|
|
if ((lower === 'auto' || lower === 'openai' || lower === 'ollama' || lower === 'codex') && requestedProfile === 'auto') {
|
|
requestedProfile = lower as ProviderProfile | 'auto'
|
|
continue
|
|
}
|
|
|
|
if (arg.startsWith('--')) {
|
|
passthroughArgs.push(arg)
|
|
continue
|
|
}
|
|
|
|
if (requestedProfile === 'auto') {
|
|
requestedProfile = null
|
|
break
|
|
}
|
|
|
|
passthroughArgs.push(arg)
|
|
}
|
|
|
|
return {
|
|
requestedProfile,
|
|
passthroughArgs,
|
|
fast,
|
|
goal,
|
|
}
|
|
}
|
|
|
|
function loadPersistedProfile(): ProfileFile | null {
|
|
const path = resolve(process.cwd(), '.openclaude-profile.json')
|
|
if (!existsSync(path)) return null
|
|
try {
|
|
const parsed = JSON.parse(readFileSync(path, 'utf8')) as ProfileFile
|
|
if (parsed.profile === 'openai' || parsed.profile === 'ollama' || parsed.profile === 'codex') {
|
|
return parsed
|
|
}
|
|
return null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function resolveOllamaDefaultModel(
|
|
goal: ReturnType<typeof normalizeRecommendationGoal>,
|
|
): Promise<string | null> {
|
|
const models = await listOllamaModels()
|
|
const recommended = recommendOllamaModel(models, goal)
|
|
return recommended?.name ?? null
|
|
}
|
|
|
|
function runCommand(command: string, env: NodeJS.ProcessEnv): Promise<number> {
|
|
return new Promise(resolve => {
|
|
const child = spawn(command, {
|
|
cwd: process.cwd(),
|
|
env,
|
|
stdio: 'inherit',
|
|
shell: true,
|
|
})
|
|
|
|
child.on('close', code => resolve(code ?? 1))
|
|
child.on('error', () => resolve(1))
|
|
})
|
|
}
|
|
|
|
function applyFastFlags(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
|
env.CLAUDE_CODE_SIMPLE ??= '1'
|
|
env.CLAUDE_CODE_DISABLE_THINKING ??= '1'
|
|
env.DISABLE_INTERLEAVED_THINKING ??= '1'
|
|
env.DISABLE_AUTO_COMPACT ??= '1'
|
|
env.CLAUDE_CODE_DISABLE_AUTO_MEMORY ??= '1'
|
|
env.CLAUDE_CODE_DISABLE_BACKGROUND_TASKS ??= '1'
|
|
return env
|
|
}
|
|
|
|
function quoteArg(arg: string): string {
|
|
if (!arg.includes(' ') && !arg.includes('"')) return arg
|
|
return `"${arg.replace(/"/g, '\\"')}"`
|
|
}
|
|
|
|
function printSummary(profile: ProviderProfile, env: NodeJS.ProcessEnv): void {
|
|
const keySet = profile === 'codex'
|
|
? Boolean(resolveCodexApiCredentials(env).apiKey)
|
|
: Boolean(env.OPENAI_API_KEY)
|
|
console.log(`Launching profile: ${profile}`)
|
|
console.log(`OPENAI_BASE_URL=${env.OPENAI_BASE_URL}`)
|
|
console.log(`OPENAI_MODEL=${env.OPENAI_MODEL}`)
|
|
console.log(
|
|
`${profile === 'codex' ? 'CODEX_API_KEY_SET' : 'OPENAI_API_KEY_SET'}=${keySet}`,
|
|
)
|
|
}
|
|
|
|
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|codex|auto] [--fast] [--goal <latency|balanced|coding>] [-- <cli args>]')
|
|
process.exit(1)
|
|
}
|
|
|
|
const persisted = loadPersistedProfile()
|
|
let profile: ProviderProfile
|
|
let resolvedOllamaModel: string | null = null
|
|
|
|
if (requestedProfile === 'auto') {
|
|
if (persisted) {
|
|
profile = persisted.profile
|
|
} else if (await hasLocalOllama()) {
|
|
resolvedOllamaModel = await resolveOllamaDefaultModel(options.goal)
|
|
profile = selectAutoProfile(resolvedOllamaModel)
|
|
} else {
|
|
profile = 'openai'
|
|
}
|
|
} else {
|
|
profile = requestedProfile
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
if (profile === 'openai' && (!env.OPENAI_API_KEY || env.OPENAI_API_KEY === 'SUA_CHAVE')) {
|
|
console.error('OPENAI_API_KEY is required for openai profile and cannot be SUA_CHAVE. Run: bun run profile:init -- --provider openai --api-key <key>')
|
|
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)
|
|
if (doctorCode !== 0) {
|
|
console.error('Runtime doctor failed. Fix configuration before launching.')
|
|
process.exit(doctorCode)
|
|
}
|
|
|
|
const cliArgs = options.passthroughArgs.map(quoteArg).join(' ')
|
|
const devCommand = cliArgs ? `bun run dev -- ${cliArgs}` : 'bun run dev'
|
|
const devCode = await runCommand(devCommand, env)
|
|
process.exit(devCode)
|
|
}
|
|
|
|
await main()
|
|
|
|
export {}
|