fix(kb): resolve kb_id path parameter in dashboard retrieve endpoint - #9485
fix(kb): resolve kb_id path parameter in dashboard retrieve endpoint#9485JosephTian876 wants to merge 2 commits into
Conversation
📄 Knowledge reviewDosu skipped reviewing this PR because your organization has used its |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
KnowledgeBaseService.retrieve, when bothkb_namesandkb_idare provided,kb_namessilently wins; consider either validating that they are consistent (same KB) or explicitly documenting and enforcing the precedence to avoid confusing, hard-to-debug situations. - The retrieval logic now depends on raw
dictaccess tokb_id(payload.get("kb_id")); if this pattern grows, consider introducing a small typed request object or helper to centralize thekb_id→kb_namesresolution and reduce the implicit contract on thedatashape.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `KnowledgeBaseService.retrieve`, when both `kb_names` and `kb_id` are provided, `kb_names` silently wins; consider either validating that they are consistent (same KB) or explicitly documenting and enforcing the precedence to avoid confusing, hard-to-debug situations.
- The retrieval logic now depends on raw `dict` access to `kb_id` (`payload.get("kb_id")`); if this pattern grows, consider introducing a small typed request object or helper to centralize the `kb_id` → `kb_names` resolution and reduce the implicit contract on the `data` shape.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
`POST /api/v1/knowledge-bases/{kb_id}/retrieve` identifies the knowledge
base with the `kb_id` path parameter, and the route already forwards it as
`service.retrieve({"kb_id": kb_id, **body})`. However `retrieve()` only ever
read `kb_names`, which that route never supplies, so every request from the
WebUI retrieval panel failed with "缺少参数 kb_names 或格式错误".
`kb_names` is still honoured when present, keeping the legacy
`POST /api/kb/retrieve` endpoint working unchanged.
daef07a to
555ac39
Compare
The method had no docstring, so the precedence between `kb_names` and the `kb_id` path parameter was only discoverable by reading the body. Spell out that an explicit `kb_names` wins, which is what keeps the legacy multi-base `POST /api/kb/retrieve` endpoint working.
|
Thanks for the review. Addressed the first point, and I'd like to push back on the second. > when both Fair — the precedence was only stated in the PR description, not in the code. I've documented it in 6b9cb8a rather than rejecting the combination, because the two are not interchangeable: > consider introducing a small typed request object or helper to centralize the I'd rather not, as it would work against the repo's own guidelines:
The suggestion is prefixed with "if this pattern grows" — agreed, and at that point the right fix is converting the whole service at once, not just this method. Happy to revisit if a maintainer prefers otherwise. |
The knowledge base retrieval panel in the WebUI is completely unusable: every
search fails with
缺少参数 kb_names 或格式错误.POST /api/v1/knowledge-bases/{kb_id}/retrieveidentifies the knowledge basewith the
kb_idpath parameter, and the route already forwards it:But
KnowledgeBaseService.retrieve()only ever readkb_names, which thisroute never supplies (
KnowledgeBaseRetrieveRequesthas no such field, and thedashboard client sends only
query/top_k/score_threshold). Sokb_nameswas structurally always
Noneand the request was rejected before any lookuphappened — reproducible 100% of the time, for any knowledge base and any query.
The service's
retrieve()was written for the legacyPOST /api/kb/retrieveendpoint, which does pass
kb_namesin the body. When the v1 REST route wasintroduced in #8688, the
kb_id→kb_namestranslation was never added.Modifications / 改动点
astrbot/dashboard/services/knowledge_base_service.py— whenkb_namesisabsent, resolve the
kb_idthe route already passes into thekb_namesthemanager expects:
Resolving through the name is safe because
KnowledgeBaseManager.create_kb()already enforces unique
kb_name(知识库名称 '{kb_name}' 已存在).An explicit
kb_namesstill takes precedence, so the legacyPOST /api/kb/retrieveendpoint keeps working unchanged.tests/unit/test_knowledge_base_service_contract.py— 4 regression tests:resolution from
kb_id, the route→service binding, the legacykb_namespath,and the two error cases.
Screenshots or Test Results / 运行截图或测试结果
The new tests fail on
masterand pass with the fix (same test code both runs):Surrounding suites, no new failures:
The 3 failures are the pre-existing
tests/test_dashboard.py::test_t2i_*cases;they fail identically on a clean
mastercheckout and are unrelated to this change.No API schema or OpenAPI definition changed, so no frontend client regeneration
is needed.
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Fix knowledge base retrieval to work with the v1 dashboard endpoint by resolving kb_id path parameters into kb_names and enforcing proper error handling when no knowledge base is specified.
Bug Fixes:
Tests: