From 9f9edcf991f94840919109e4326ae46f0ebab3fd Mon Sep 17 00:00:00 2001 From: Yunfei He Date: Sun, 24 May 2026 22:33:58 +0800 Subject: [PATCH] fix(coding-agent): lazy-init OpenAI client to avoid top-level throw --- README.md | 19 ++++++------------- examples/coding-agent/src/agent.ts | 16 +++++++++++----- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f527907..6442f64 100644 --- a/README.md +++ b/README.md @@ -26,22 +26,15 @@ createApp(App).mount(); ```vue - diff --git a/examples/coding-agent/src/agent.ts b/examples/coding-agent/src/agent.ts index 8583214..9355ff5 100644 --- a/examples/coding-agent/src/agent.ts +++ b/examples/coding-agent/src/agent.ts @@ -23,10 +23,16 @@ export interface AgentOptions { requestApproval: (command: string) => Promise; } -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 },