mirror of
https://github.com/JohnRiceML/clawport-ui.git
synced 2026-08-14 00:47:50 +00:00
Move OpenAI client initialization from module top-level to a lazy singleton (lib/openai.ts) called inside route handlers. Prevents build errors when gateway env vars aren't set. Also adds openclaw.json agents.defaults.workspace as the first workspace detection source, before falling back to filesystem paths. Inspired by PR #21 (cherry-picked the good parts, skipped the fragile cli-utils rewrite and the agents-registry simplification that dropped sub-agents). Bumps to v0.8.8. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
21 lines
512 B
TypeScript
21 lines
512 B
TypeScript
import OpenAI from 'openai'
|
|
import { gatewayBaseUrl } from './env'
|
|
|
|
/**
|
|
* Lazy-initialized OpenAI client routed through the OpenClaw gateway.
|
|
*
|
|
* Call inside route handlers, not at module top level, so builds don't
|
|
* fail when environment variables aren't set.
|
|
*/
|
|
let _openai: OpenAI | null = null
|
|
|
|
export function getOpenAIClient(): OpenAI {
|
|
if (!_openai) {
|
|
_openai = new OpenAI({
|
|
baseURL: gatewayBaseUrl(),
|
|
apiKey: process.env.OPENCLAW_GATEWAY_TOKEN || '',
|
|
})
|
|
}
|
|
return _openai
|
|
}
|