Skip to content

fix(agent): /agent/chat 请求体无法绑定(TYPE_CHECKING 导致 422,Critical) - #57

Merged
Color2333 merged 1 commit into
mainfrom
fix/agent-chat-body-binding
Jul 18, 2026
Merged

fix(agent): /agent/chat 请求体无法绑定(TYPE_CHECKING 导致 422,Critical)#57
Color2333 merged 1 commit into
mainfrom
fix/agent-chat-body-binding

Conversation

@Color2333

Copy link
Copy Markdown
Owner

Critical 回归修复

现象:部署 PR #56 后实测发现,所有 POST /agent/chat 请求返回 422 {"detail":[{"loc":["query","req"],"msg":"Field required"}]},agent 对话功能完全不可用。

根因apps/api/routers/agent.pyAgentChatRequest 放在 if TYPE_CHECKING: 块里导入,配合 from __future__ import annotations(PEP 563)使所有注解变为字符串。FastAPI 用 get_type_hints 解析路由签名时,模块命名空间没有 AgentChatRequest,无法把 JSON body 绑定到 req 参数,错误地当成 query 参数 → 任何 POST 都 422。

历史:此 bug 在 PR #21 (3403bfa) 引入,一直存在至今。说明之前 agent 对话实际从未经此路由成功过(或走了其他路径)。

修复:把 AgentChatRequestCallable 移出 TYPE_CHECKING 块作运行时导入,加 # noqa 抑制 ruff TC001/TC003(FastAPI 确需运行时可见)。

验证

  • 本地 FastAPI 路由 introspect:body_params=[('req', <class 'AgentChatRequest'>)](修复前为空,被当 query)
  • pytest tests/ → 69 passed, 2 skipped
  • ruff check 全绿

此 PR 阻塞 PR #56 的实测验证,合并后立即部署。

Critical 回归:apps/api/routers/agent.py 把 AgentChatRequest 放在
if TYPE_CHECKING: 块里导入,配合 `from __future__ import annotations`
(PEP 563) 使所有注解变为字符串。FastAPI 用 get_type_hints 解析路由签名时
模块命名空间没有 AgentChatRequest,无法把 JSON body 绑定到 req 参数,
错误地当成 query 参数 → 任何 POST /agent/chat 都返回 422
{"detail":[{"loc":["query","req"],...}]}。

实测确认:部署后所有 /agent/chat 请求 422(包括前端),agent 对话功能
完全不可用。此 bug 在 PR #21 (3403bfa) 引入,一直存在至今。

修复:把 AgentChatRequest 和 Callable 移出 TYPE_CHECKING 块作运行时导入,
加 noqa 抑制 ruff TC001/TC003(FastAPI 确需运行时可见)。

验证:本地 FastAPI 路由 introspect 显示 body_params=[('req', AgentChatRequest)],
之前为空(当 query)。
@Color2333
Color2333 merged commit 11a2b2c into main Jul 18, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown

🔍 OpenCode PR Review Required

这是一个受保护的分支,merge 前需要进行 code review。

请运行以下命令进行 OpenCode review:

/oc review https://github.com/Color2333/PaperMind/pull/$PR_NUM

或者在 PR 页面评论 /oc 来触发 OpenCode review。


This is an automated reminder from PR Review Gate.

@Color2333
Color2333 deleted the fix/agent-chat-body-binding branch July 18, 2026 14:54
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.

1 participant