Files
JohnRiceMLandClaude Opus 4.6 a51ac3cbe0 fix: lazy OpenAI client + canonical workspace detection from openclaw.json
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>
2026-03-23 10:36:17 -05:00

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
}