前言¶
在 DeepSeek Harness(DSH)裏走 OpenAI 兼容接口、中間又掛 LiteLLM 一類反向代理時,常見需求是給上游帶會話類 HTTP 頭(例如 x-opencode-session)。實測在客戶端只加請求頭,經 LiteLLM 這一跳往往仍會上游 500;把同樣信息放進 JSON body 的 extra_headers 字段後,代理纔會轉發,上游才能 200。
DSH 自帶的 llm-pi-ai 適配器對白名單字段轉發有限,不能像隨意改 body 那樣純靠配置塞 extra_headers;社區裏「只加 HTTP 頭」的會話插件在這條鏈路上也無效。dsh-litellm-extra-headers 是維護者 chenyifawan-cell 發佈的宿主插件(分類:聯網工具),在命中的 provider 與上游 host 上,通過 patch globalThis.fetch,把 extra_headers 寫進 POST .../chat/completions 的 JSON body——這是 README 裏寫明、經 LiteLLM 中繼仍能生效的注入點。
下面介紹插件定位、配置方式與使用注意。
這是什麼¶
- 名稱:
dsh-litellm-extra-headers(npm 包名同名,當前資料版本0.1.0) - 維護者:chenyifawan-cell(GitHub:
chenyifawan-cell/dsh-litellm-extra-headers) - 許可證:MIT
- 一句話:給 DSH 用的宿主插件,對配置裏列出的 provider 路由和上游 host,在 OpenAI 兼容的 chat/completions 請求 body 裏合併
extra_headers: { "<header>": "<value>" },用於繞過 LiteLLM 等代理丟棄客戶端 HTTP 頭的問題。
社區目錄頁:https://www.skillhub.cn/plugins/chenyifawan-cell/dsh-litellm-extra-headers
源碼與說明:https://github.com/chenyifawan-cell/dsh-litellm-extra-headers
爲什麼需要 body 級 extra_headers¶
README 中已覈實的結論可以概括爲四點:
- 客戶端 HTTP 頭在 LiteLLM 一跳被丟棄;僅 header 方式上游 500,改
body.extra_headers後 200。 - DSH 的
llm-pi-ai適配器在profileOptions()裏只白名單轉發部分字段,宿主側沒有暴露能塞任意 body 字段的options.samplingParams。 - 現成的
dsh-opencode-session走的是加 HTTP 頭,且默認providers爲['opencode', 'opencode-go'],不含自定義路由鍵(如gwds、go)。 - pi-ai 的
openai-completions使用官方openaiSDK(資料記載 v6.40.0),經globalThis.fetch發請求,因此運行時替換globalThis.fetch是當前可用的宿主注入點。
因此文檔建議:不要指望再靠別處單獨加 header;需要新字段時往 body 裏合併。
核心功能¶
雙層白名單匹配¶
- 在
llm/streamwaterfall 中:僅當options.provider落在配置的providers且存在options.sessionId時,把{ value, provider, model, session }放進AsyncLocalStorage。 - 在
fetchpatch 中:僅當目標 URL 的host落在配置的hosts時修改 body。
路由鍵與上游地址都未配置時,插件保持惰性並打 warning,避免對未告知的網關偷偷加字段。
只改 chat/completions 的 JSON body¶
條件包括:method === 'POST'、content-type 含 application/json、init.body 爲字符串、路徑以 /chat/completions 結尾。解析失敗則原樣放行;若 body 裏已有 extra_headers,則合併且不覆蓋調用方已有同名鍵。
取值模式¶
通過 mode 決定寫入 extra_headers 的值來源:
| 模式 | 含義 |
|---|---|
session-id(默認) |
DSH 會話 id,跨輪次、跨重啓穩定 |
uuid |
每會話一個進程內隨機 uuid |
fixed |
配置項 value 的字面量;未提供 value 會在加載時報錯 |
默認頭字段名爲 x-opencode-session,可用 header 改成其它名稱。
實現約束(資料中的邊界)¶
- 零依賴(
node:async_hooks/node:crypto/node:fs/promises);安裝與卸載用ctx.effect(),dispose 時按引用還原globalThis.fetch。 input爲Request對象、body 無法同步讀出時不改;當前 pi-ai + openai SDK 走 URL + 字符串 body,不觸發該路徑。- 只注入
extra_headers,不做重試、不改 HTTP 頭、不改其它 body 字段。
安裝與啓用¶
包已發佈在 npm:dsh-litellm-extra-headers@0.1.0。Node 要求 >=20。
# npm(推薦)
dsh plugin --profile web add dsh-litellm-extra-headers
其它安裝方式(README 原文):
# 從本地目錄(開發時推薦,link 安裝,改動即時生效)
dsh plugin --profile web add ./dsh-litellm-extra-headers
# 從打包產物
dsh plugin --profile web add ./dsh-litellm-extra-headers-0.1.0.tgz
# 從 git 主機(本包是純 JS,無需 pnpm 放行構建腳本)
dsh plugin --profile web add github:chenyifawan-cell/dsh-litellm-extra-headers
dsh plugin add 識別到包內 dsh.bundle 後會自動把插件寫入 profile 的 bundles;包內 cordis.patch.yml 會插入對應插件行。裝完後可確認配置裏是否出現本插件:
dsh --profile web --dump-config | Select-String litellm-extra-headers
也可手工把插件行寫進 profile 的 cordis.patch.yml(指向包名或 index.js 路徑)。
典型配置¶
包內默認 providers / hosts 爲空列表(空 = 惰性,不注入)。需要把你的路由鍵和上游地址填進去才生效。示例來自 README:
- insert:
- id: litellm-extra-headers
name: dsh-litellm-extra-headers
config:
providers: [gwds, go] # 你的路由鍵
hosts: ["litellm.internal:4000"] # 只有這些上游才注入
header: x-opencode-session
mode: session-id
value: null
debug: false
debugFile: null
配置字段說明¶
| 字段 | 類型 | 默認 | 說明 |
|---|---|---|---|
providers |
string[] |
[] |
llm/stream 的 options.provider 路由鍵白名單,必須顯式列出,大小寫不敏感 |
hosts |
string[] |
[] |
允許注入的上游 host[:port];帶 scheme/path 會歸一化,必須顯式列出 |
header |
string |
x-opencode-session |
寫入 extra_headers 的鍵名 |
mode |
session-id | uuid | fixed |
session-id |
取值來源 |
value |
string |
— | mode: fixed 時的字面量 |
debug |
boolean |
false |
每次注入決策打 ctx.logger.info |
debugFile |
string |
— | 每次注入決策追加一行 JSON,便於無控制台時排錯 |
驗證與排錯思路¶
維護者在 README 中給出的驗證順序:
- 倉庫內
npm test(node --test,19 項單元測試)。 npm run test:e2e:本地 mock 上游 +dsh --profile headless對照有無插件行,斷言 body 出現extra_headers、同會話值穩定、對照組無該字段、debugFile有event: "inject"。- 在能連真實 LiteLLM 的機器上再跑一輪,用
debugFile確認注入記錄;對照curl僅 HTTP 頭 vs body 帶extra_headers的行爲差異。
日常使用時,可把 debug 或 debugFile 打開,對照 provider、model、session、host、path 與 reason 字段判斷是否命中白名單。
適用場景與注意¶
適合誰
- DSH 通過自定義 provider 路由到 LiteLLM(或同類只認
body.extra_headers的網關),上游要求會話親和或自定義頭,且無法依賴客戶端 HTTP 頭透傳的場景。 - 需要按 provider + 上游 host 精細控制「只給自己的網關加字段」的團隊。
使用前請知悉
- 插件以當前 dsh 進程權限 運行,會 patch 全局
fetch;安裝前應閱讀源碼與 MIT 許可證,確認與現有插件無衝突。 providers或hosts留空時插件 deliberately 不工作,這是設計行爲,不是裝壞。- SkillHub 等社區目錄爲獨立站點,與 DeepSeek / 幻方無官方從屬關係;DSH 生態強調「一切皆插件」,本包是其中一例聯網工具類擴展。
結尾¶
dsh-litellm-extra-headers 解決的是「LiteLLM 中繼丟 HTTP 頭、DSH 又難以從配置直寫 body」的組合問題:在明確列出的 provider 與上游 host 上,把會話等信息寫進 extra_headers,讓代理能轉發、上游能認。
- 目錄頁:https://www.skillhub.cn/plugins/chenyifawan-cell/dsh-litellm-extra-headers
- GitHub:https://github.com/chenyifawan-cell/dsh-litellm-extra-headers