前言¶
做 DSH 智能體時,常見需求是糾正某些工具調用:例如不要直接用 pip install,優先用 gh CLI,或讀 PDF 時改用 markitdown 技能。若把這類規則始終放進上下文,會增加不必要的上下文佔用。dsh-stream-rules 的做法是在工具調用匹配規則後注入一條轉向提示,讓智能體從同一點重試。
這是什麼¶
@jiesou/dsh-stream-rules 是 jiesou 維護的 DSH 插件,MIT 許可,包版本 0.1.7。它把 jiesou/opencode-stream-rules 的思路移植到 DSH,用於按需注入規則,而不是把規則常駐到上下文。
工作方式¶
插件在工具調用觸發時判斷規則。每個工具調用的工具名與參數會被序列化爲字符串,規則的 match 返回 true 時生效。
- 默認:通過
agent.inject()注入一條SYSTEM NOTICE轉向消息,智能體從同一點重試。 reject: true:拒絕第一次工具調用,後續嘗試允許。- 每個規則在每會話(per agent)最多觸發一次。
實現上使用 DSH 的 tools/pre-execute waterfall(deny)和 agent.inject(),沒有核心改動,沒有 monkey-patching。
安裝¶
從 npm 添加:
dsh plugin --profile <name> add @jiesou/dsh-stream-rules
從 GitHub 添加;GitHub 安裝會運行 prepare 構建:
dsh plugin --profile <name> add github:jiesou/dsh-stream-rules
也可以在 profile 的 cordis.patch.yml 中添加:
- id: stream-rules
name: '@jiesou/dsh-stream-rules'
啓用規則¶
安裝後不會立即生效,需要自己寫規則。
1、定位插件路徑:
$DSH_HOME/profiles/<name>/node_modules/@jiesou/dsh-stream-rules
2、複製示例規則文件:
mv rules/rules.js.example rules/rules.local.js
規則文件導出數組。每條規則必須有 match 和 prompt,可選 reject。以 _ 開頭的文件會被跳過。
規則示例¶
以下示例來自插件說明:
// rules/rules.local.js
export default [
{
match: (v) =>
v.includes('pip') &&
v.includes('install') &&
!v.includes('uv pip') &&
!v.includes('uvx'),
reject: true,
prompt: 'Use `uvx` or `uv venv` + `uv pip` instead of `pip install` directly',
},
{
match: (v) => v.includes('curl') && v.includes('api.github.com'),
prompt: 'Prefer using `gh` cli over `curl https://api.github.com/...`. gh offers more requests limits.',
},
{
match: (v) => v.includes('pdf'),
prompt: 'Use the `markitdown` skill to read PDF files.',
},
// add your rules here
]
字段說明:
match: (v: string) => boolean,用於判斷工具調用是否匹配
prompt: 用於轉向的提示內容
reject: 如果爲 true,先阻止該工具調用,而不是隻注入提示
如需指向不同規則目錄,可使用 config.rules:
- id: stream-rules
name: '@jiesou/dsh-stream-rules'
config:
rules: /path/to/your/rules
適用場景與注意¶
適合希望在 DSH 智能體里加入輕量行爲邊界的場景:限制某些工具調用、糾正命令選擇、優先使用特定技能。
使用前注意:
- 插件以當前 dsh 進程權限運行,安裝前應檢查源碼與許可證。
- 包聲明的 peerDependencies 爲:
@deepseek-ai/cordis >=4.0.1 <5
@deepseek-ai/dsh-llm >=0.1.0-rc.6 <0.2.0
@deepseek-ai/schemastery ^3.18.1
- 規則默認不生效,需要先編寫或複製規則文件。
reject: true只拒絕第一次工具調用,後續嘗試允許。- 每個規則每會話最多觸發一次。
鏈接¶
GitHub: https://github.com/jiesou/dsh-stream-rules