Skip to content

feat: adapt dify new retrieval_model schema#1745

Merged
zerob13 merged 1 commit into
devfrom
fix/dify-retrieval-model
Jun 8, 2026
Merged

feat: adapt dify new retrieval_model schema#1745
zerob13 merged 1 commit into
devfrom
fix/dify-retrieval-model

Conversation

@zhangmo8
Copy link
Copy Markdown
Collaborator

@zhangmo8 zhangmo8 commented Jun 8, 2026

背景 / Background

Closes #1744

Dify 新版检索 API 把检索配置统一收进了 retrieval_model 对象,并对其字段做了校验收紧:

  • search_method 变为必填(缺失会导致参数校验失败);
  • reranking_enable / score_threshold_enabled 必须是布尔值,旧版传 null 已不再被接受。

dify_knowledge_search 工具当前发出的请求体仍缺少 search_method、且这两个开关传的是 null,因此在新版 Dify 上检索直接报错。

改动 / Changes

仅修改 difyKnowledgeServer.tsperformDifyKnowledgeSearch 构造请求体的部分(私有方法,无外部调用方):

  • 新增 search_method: 'semantic_search' —— 语义检索不依赖数据集是否配置 Rerank 模型,对任意数据集都可用;
  • reranking_enable / score_threshold_enabled 改为布尔值,避免 null 被新版接口拒绝。

请求体由:

{
  "query": "...",
  "retrieval_model": {
    "top_k": 5,
    "score_threshold": 0.2,
    "reranking_enable": null,
    "score_threshold_enabled": null
  }
}

改为:

{
  "query": "...",
  "retrieval_model": {
    "search_method": "semantic_search",
    "reranking_enable": false,
    "top_k": 5,
    "score_threshold_enabled": false,
    "score_threshold": null
  }
}

兼容性 / Compatibility

  • 工具入参(query / topK / scoreThreshold)、schema、工具注册逻辑均未改动,对调用方零侵入。
  • 阈值过滤保持原有「不过滤」行为(改动前 score_threshold_enablednull,Dify 本就未启用阈值),因此检索结果集与改动前一致,唯一区别是新版 Dify 上由 400 报错变为正常返回。
  • 未选用截图建议的 hybrid_search + reranking_enable: true,因其要求数据集已配置 Rerank 模型,否则反而会让部分用户报错。如需混合检索,可作为后续「每知识库可配置检索方式」的增强项。

测试 / Test

  • pnpm run formatpnpm run lint:通过(0 warning / 0 error)
  • pnpm run typecheck:node / typecheck:web:通过(pre-commit 钩子自动执行)

Summary by CodeRabbit

  • Bug Fixes
    • Updated Dify knowledge search integration to use the latest API retrieval model format with semantic search, ensuring continued compatibility and optimal search performance.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 8, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates the Dify API request payload in performDifyKnowledgeSearch to conform to the newer retrieval-model format. The function now constructs a standardized retrievalModel object with fixed boolean settings (reranking_enable: false, score_threshold_enabled: false) and score_threshold: null, replacing the prior structure that sent dynamic thresholds and null placeholders.

Changes

Dify Retrieval Model Adaptation

Layer / File(s) Summary
Dify retrieval model payload update
src/main/presenter/mcpPresenter/inMemoryServers/difyKnowledgeServer.ts
The retrievalModel object sent to Dify's knowledge retrieval endpoint is restructured to use fixed boolean settings and disabled score threshold filtering, conforming to the newer API format.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A Dify dance, old steps to new,
Where thresholds rest and booleans stay true,
No more guessing, the settings align—
Semantic search shines in structured design! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #1744: adds search_method field, converts reranking_enable and score_threshold_enabled to boolean false values, and updates score_threshold handling.
Out of Scope Changes check ✅ Passed All changes are within scope of issue #1744. The PR modifies only the Dify API request payload in performDifyKnowledgeSearch with no unrelated alterations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title directly and clearly refers to the main change: adapting the Dify knowledge server to use the new retrieval_model schema. It is concise and specific.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dify-retrieval-model

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@zhangmo8 zhangmo8 changed the title fix(mcp): adapt dify new retrieval_model schema feat: adapt dify new retrieval_model schema Jun 8, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/presenter/mcpPresenter/inMemoryServers/difyKnowledgeServer.ts`:
- Around line 225-236: The code in performDifyKnowledgeSearch currently
hard-disables thresholding (retrieval_model.score_threshold_enabled=false and
score_threshold=null) while DifyKnowledgeSearchArgsSchema and the function
destructuring still accept scoreThreshold, causing a no-op public input and
misleading logs; either (A) wire the parsed scoreThreshold into retrievalModel
by setting retrieval_model.score_threshold_enabled = scoreThreshold != null and
retrieval_model.score_threshold = scoreThreshold (and update the console.log to
reflect the actual retrieval_model), or (B) if thresholding is intentionally
removed, delete scoreThreshold from DifyKnowledgeSearchArgsSchema and from the
destructuring in performDifyKnowledgeSearch and remove it from the console.log
so callers are not exposed to a dead parameter.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dd28b5cd-0fb7-4ba8-a634-6f6387c07056

📥 Commits

Reviewing files that changed from the base of the PR and between fb7f841 and fcbf3e6.

📒 Files selected for processing (1)
  • src/main/presenter/mcpPresenter/inMemoryServers/difyKnowledgeServer.ts

@zerob13 zerob13 merged commit c963568 into dev Jun 8, 2026
3 checks passed
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.

[Feature] 适配 Dify 新的 retrieval_model 参数

2 participants