Skip to content

fix(assistant): 回应 #293 CR — 匿名短路不抛错 + ZHIPU_API_KEY 显式校验#294

Merged
longsizhuo merged 1 commit intomainfrom
fix/chatbot-cr-follow-up-285
Apr 16, 2026
Merged

fix(assistant): 回应 #293 CR — 匿名短路不抛错 + ZHIPU_API_KEY 显式校验#294
longsizhuo merged 1 commit intomainfrom
fix/chatbot-cr-follow-up-285

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

背景

#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/ 获取。
```

测试

  • `pnpm typecheck` 通过
  • 本地验证匿名请求日志:只有一行 `⏭️ Anonymous request`,没有 warn 噪音
  • 故意清空 ZHIPU_API_KEY 后验证:错误消息包含 Vercel 指引

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 配置指引的错误。
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
involutionhell-github-io Ready Ready Preview, Comment Apr 16, 2026 9:00pm
website-preview Ready Ready Preview, Comment Apr 16, 2026 9:00pm

Copilot AI review requested due to automatic review settings April 16, 2026 19:41
@longsizhuo longsizhuo merged commit caf0d63 into main Apr 16, 2026
7 of 9 checks passed
@longsizhuo longsizhuo deleted the fix/chatbot-cr-follow-up-285 branch April 16, 2026 19:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/chat so they don’t enter the backend-proxy try/catch path (avoids warn + stack spam).
  • Add explicit ZHIPU_API_KEY validation 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.

Comment thread app/api/chat/route.ts
Comment on lines +41 to +46
const satoken = req.headers.get("x-satoken");
if (!satoken) {
console.log(
"[Chat Fallback Proxy] ⏭️ Anonymous request, skip backend proxy, use local inference.",
);
} else {
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants