fix(coding-agent): lazy-init OpenAI client to avoid top-level throw

This commit is contained in:
Yunfei He
2026-05-24 22:33:58 +08:00
parent 253fa8bfc6
commit 9f9edcf991
2 changed files with 17 additions and 18 deletions
+11 -5
View File
@@ -23,10 +23,16 @@ export interface AgentOptions {
requestApproval: (command: string) => Promise<boolean>;
}
const client = new OpenAI({
apiKey: process.env["DEEPSEEK_API_KEY"],
baseURL: "https://api.deepseek.com",
});
let client: OpenAI | undefined;
function getClient(): OpenAI {
if (!client) {
client = new OpenAI({
apiKey: process.env["DEEPSEEK_API_KEY"],
baseURL: "https://api.deepseek.com",
});
}
return client;
}
const BASH_TOOL = {
type: "function" as const,
@@ -83,7 +89,7 @@ export async function runAgentLoop(
messages = [...messages, { role: "user", content: userMessage }];
while (true) {
const stream = await client.chat.completions.create({
const stream = await getClient().chat.completions.create({
model: "deepseek-v4-pro",
messages: [
{ role: "system", content: SYSTEM_PROMPT },