fix(assistant): 回应 #293 CR — 匿名短路不抛错 + ZHIPU_API_KEY 显式校验#294
fix(assistant): 回应 #293 CR — 匿名短路不抛错 + ZHIPU_API_KEY 显式校验#294longsizhuo merged 1 commit intomainfrom
Conversation
Copilot 在 PR #293 指出两点,follow-up 修复: - **app/api/chat/route.ts**:原先用 `throw new Error("Anonymous request")` 触发 fallback,副作用是每个匿名请求都被 catch 打成带 stack 的 "Java Backend unavailable" warn,生产日志会刷爆。改成显式 if-else 分支 直接短路到本地推理,不抛错,只打一行 info 级日志。顺带清理了多余的 x-satoken 三元展开。 - **lib/ai/providers/intern.ts**:原先把 process.env.ZHIPU_API_KEY 直接 喂给 createOpenAICompatible,漏配时下游会 401/500,UI 上依旧表现为 "unauthorized" —— 这正是 issue #285 要根治的症状,不能再放任。加了 显式校验,缺失时抛出带 Vercel 配置指引的错误。
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR responds to prior review feedback by reducing noisy fallback logging for anonymous chat requests and failing fast with a clear error when the Zhipu API key is missing, improving operational debuggability for the assistant’s free-model path.
Changes:
- Short-circuit anonymous requests in
/api/chatso they don’t enter the backend-proxy try/catch path (avoids warn + stack spam). - Add explicit
ZHIPU_API_KEYvalidation before creating the Zhipu OpenAI-compatible client, with a Vercel configuration hint in the thrown error.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| app/api/chat/route.ts | Avoids throwing for anonymous proxy-skips; keeps backend proxy attempt only for authenticated requests and reduces log noise. |
| lib/ai/providers/intern.ts | Validates ZHIPU_API_KEY early and throws an actionable configuration error before client creation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const satoken = req.headers.get("x-satoken"); | ||
| if (!satoken) { | ||
| console.log( | ||
| "[Chat Fallback Proxy] ⏭️ Anonymous request, skip backend proxy, use local inference.", | ||
| ); | ||
| } else { |
There was a problem hiding this comment.
req.clone() is always executed, but in the anonymous (!satoken) path the proxy is skipped so proxyReq is never used. To avoid unnecessary body cloning/memory overhead on high-volume anonymous traffic, consider cloning lazily only inside the authenticated branch right before the proxy fetch (or only when you actually need to call proxyReq.text()).
背景
#293 已合入,Copilot 在 review 中留了 2 条意见,本 PR 全部回应。
改动
1.
app/api/chat/route.ts(Copilot CR #1)问题:上一版用 `throw new Error("Anonymous request, skip backend proxy.")` 触发 fallback,副作用是每个匿名请求都会被外层 catch 打成 `Java Backend unavailable or timed out, fallback to local Next.js inference. Error: ...` 带 stack 的 warn,生产日志被刷爆。
修复:改成显式 `if-else` 分支,匿名请求不进 try/catch,只打一行 info 级日志:
```
[Chat Fallback Proxy] ⏭️ Anonymous request, skip backend proxy, use local inference.
```
顺带把重复读 header 的三元展开 `...(req.headers.get("x-satoken") ? { satoken: ... } : {})` 简化为 `satoken` 缩写(都已确认非空才进此分支)。
2.
lib/ai/providers/intern.ts(Copilot CR #2)问题:上一版把 `process.env.ZHIPU_API_KEY` 直接喂给 `createOpenAICompatible`。若 Vercel 漏配该环境变量 → 下游 401/500 → UI 上依旧表现为 "unauthorized" —— 这正是 issue #285 的原症状,相当于绕一圈还是炸。
修复:在创建 client 前显式校验,缺失时抛出带 Vercel 配置指引 的错误,运维看日志一眼就知道补哪个 var:
```
Missing required environment variable ZHIPU_API_KEY. 配置位置:Vercel
Project Settings → Environment Variables。免费 key 从 https://open.bigmodel.cn/ 获取。
```
测试