fix(agent): /agent/chat 请求体无法绑定(TYPE_CHECKING 导致 422,Critical) - #57
Merged
Conversation
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)。
🔍 OpenCode PR Review Required这是一个受保护的分支,merge 前需要进行 code review。 请运行以下命令进行 OpenCode review: 或者在 PR 页面评论 This is an automated reminder from PR Review Gate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Critical 回归修复
现象:部署 PR #56 后实测发现,所有
POST /agent/chat请求返回422 {"detail":[{"loc":["query","req"],"msg":"Field required"}]},agent 对话功能完全不可用。根因:
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 都 422。历史:此 bug 在 PR #21 (
3403bfa) 引入,一直存在至今。说明之前 agent 对话实际从未经此路由成功过(或走了其他路径)。修复:把
AgentChatRequest和Callable移出TYPE_CHECKING块作运行时导入,加# noqa抑制 ruffTC001/TC003(FastAPI 确需运行时可见)。验证:
body_params=[('req', <class 'AgentChatRequest'>)](修复前为空,被当 query)pytest tests/→ 69 passed, 2 skippedruff check全绿此 PR 阻塞 PR #56 的实测验证,合并后立即部署。