dsh-plugin-litellm-bridge 2.0.9 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -725,6 +725,32 @@ window.__ModuleLoader__.load({
725
725
 
726
726
  const allIds = (pool.models || []).map((x) => x.id);
727
727
 
728
+ // ---------------------------------------------------------------------
729
+ // 【关键】链成员的唯一标识必须是「供应商 + 模型」,不能只用模型名。
730
+ //
731
+ // 实测模型池里有大量同名模型分布在不同供应商/账号:
732
+ // agnes-3.0-flash → agnes / agnes-backup / llm-pi-ai (3 个不同账号)
733
+ // DeepSeek-V4.1-Flash → amd / trae / workbuddy (3 个不同渠道)
734
+ // GLM-5.3-Flash → amd / trae
735
+ //
736
+ // 这些是**不同凭证、不同配额**的独立路由,恰恰是降级最该利用的
737
+ //(1 号账号限流就换 2 号账号的同一个模型)。
738
+ // 若按模型名去重,选了 agnes 的 agnes-3.0-flash 后就再也选不到
739
+ // agnes-backup 的同名模型 —— 把最核心的备份场景堵死了。
740
+ //
741
+ // 所以链里存 "供应商::模型" 复合键;写进网关配置时再拆开。
742
+ // ---------------------------------------------------------------------
743
+ const keyOf = (provider, id) => `${provider}::${id}`;
744
+ const parseKey = (k) => {
745
+ const i = String(k).indexOf('::');
746
+ return i < 0 ? { provider: '', id: String(k) } : {
747
+ provider: String(k).slice(0, i),
748
+ id: String(k).slice(i + 2),
749
+ };
750
+ };
751
+ /** 把池里的 (provider,id) 转成展示用文字 */
752
+ const labelOf = (provider, id) => (provider ? `${provider} / ${id}` : id);
753
+
728
754
  return h('div', null,
729
755
  // ---- 使用引导:第一次进来的人 3 秒看懂这页在做什么 ----
730
756
  h('div', {
@@ -796,7 +822,17 @@ window.__ModuleLoader__.load({
796
822
  const cfg = prof?.agents?.[at]
797
823
  || { label: at, primary: '', timeoutMs: 600000, fallbacks: [] };
798
824
 
799
- const chain = [cfg.primary, ...(cfg.fallbacks || [])].filter(Boolean);
825
+ // 链里存的是 "供应商::模型" 复合键;兼容旧数据(纯模型名)
826
+ const chainKeys = [cfg.primary, ...(cfg.fallbacks || [])]
827
+ .filter(Boolean)
828
+ .map((v) => {
829
+ const s = String(v);
830
+ if (s.includes('::')) return s;
831
+ // 旧数据只有模型名:在池里找它的供应商,找不到就留空供应商
832
+ const hit = (pool.models || []).find((m) => m.id === s);
833
+ return hit ? keyOf(hit.provider, hit.id) : keyOf('', s);
834
+ });
835
+ const chain = chainKeys;
800
836
  const isSub = at !== 'root';
801
837
 
802
838
  // 每个角色的说明:用「人话」讲清楚它管什么
@@ -830,47 +866,60 @@ window.__ModuleLoader__.load({
830
866
  setChain(l);
831
867
  };
832
868
  const removeAt = (i) => setChain(chain.filter((_, j) => j !== i));
833
- const addModel = (id) => {
834
- if (!id || chain.includes(id)) return;
835
- setChain([...chain, id]);
869
+ /** 添加一个 (provider, model) —— 复合键去重,同名不同账号可以共存 */
870
+ const addModel = (provider, id) => {
871
+ if (!id) return;
872
+ const k = keyOf(provider, id);
873
+ if (chain.includes(k)) return;
874
+ setChain([...chain, k]);
836
875
  };
837
876
 
838
877
  /** 一个可拖拽的链节点 */
839
- const row = (name, i) => h('div', {
840
- key: `${at}-${name}-${i}`,
841
- draggable: true,
842
- onDragStart: () => onDragStart(at, i),
843
- onDragOver: (e) => e.preventDefault(),
844
- onDrop: () => onDropChain(at, i),
845
- style: {
846
- display: 'flex', alignItems: 'center', gap: 8, padding: '6px 10px',
847
- marginBottom: 4, borderRadius: 6, cursor: 'grab',
848
- background: i === 0 ? tint(T.success, 7) : T.bgLayer2,
849
- border: `1px solid ${i === 0 ? tint(T.success, 30) : T.borderL1}`,
850
- },
851
- },
852
- h('span', { style: { opacity: 0.4, ...S.mono } }, '⠿'),
853
- h('span', {
878
+ const row = (key, i) => {
879
+ const { provider, id } = parseKey(key);
880
+ return h('div', {
881
+ key: `${at}-${key}-${i}`,
882
+ draggable: true,
883
+ onDragStart: () => onDragStart(at, i),
884
+ onDragOver: (e) => e.preventDefault(),
885
+ onDrop: () => onDropChain(at, i),
854
886
  style: {
855
- ...S.mono, fontSize: 11, width: 20, textAlign: 'center',
856
- flex: '0 0 20px', fontWeight: 600,
857
- color: i === 0 ? T.success : T.labelSecondary,
887
+ display: 'flex', alignItems: 'center', gap: 8, padding: '6px 10px',
888
+ marginBottom: 4, borderRadius: 6, cursor: 'grab',
889
+ background: i === 0 ? tint(T.success, 7) : T.bgLayer2,
890
+ border: `1px solid ${i === 0 ? tint(T.success, 30) : T.borderL1}`,
858
891
  },
859
- }, String(i + 1)),
860
- h('span', { style: { flex: '1 1 auto', ...S.mono, wordBreak: 'break-all' } }, safeText(name)),
861
- i === 0
862
- ? h('span', { style: S.badge(T.success, tint(T.success, 16)) }, '先用这个')
863
- : h('span', { style: S.badge() }, i === 1 ? '失败后换它' : `${i + 1} 顺位`),
864
- i > 0 && h('button', {
865
- style: { ...S.btnSm, height: 22, fontSize: 11 },
866
- onClick: () => moveUp(i),
867
- title: '往前挪一位(往前 = 更优先)',
868
- }, '↑'),
869
- h('button', {
870
- style: { ...S.btnSm, height: 22, fontSize: 11, color: T.error, borderColor: tint(T.error, 35) },
871
- onClick: () => removeAt(i),
872
- }, '删'),
873
- );
892
+ },
893
+ h('span', { style: { opacity: 0.4, ...S.mono } }, '⠿'),
894
+ h('span', {
895
+ style: {
896
+ ...S.mono, fontSize: 11, width: 20, textAlign: 'center',
897
+ flex: '0 0 20px', fontWeight: 600,
898
+ color: i === 0 ? T.success : T.labelSecondary,
899
+ },
900
+ }, String(i + 1)),
901
+ // 【关键】必须把供应商显式展示出来,
902
+ // 否则「agnes 的 agnes-3.0-flash」和「agnes-backup 的 agnes-3.0-flash」
903
+ // 在列表里长得一模一样,用户根本分不清哪个是哪个账号。
904
+ provider
905
+ ? h('span', { style: S.badge(T.brand, tint(T.brand, 12)), title: `账号/供应商:${provider}` },
906
+ safeText(provider))
907
+ : h('span', { style: S.badge(T.warn, tint(T.warn, 14)) }, '供应商未知'),
908
+ h('span', { style: { flex: '1 1 auto', ...S.mono, wordBreak: 'break-all' } }, safeText(id)),
909
+ i === 0
910
+ ? h('span', { style: S.badge(T.success, tint(T.success, 16)) }, '先用这个')
911
+ : h('span', { style: S.badge() }, i === 1 ? '失败后换它' : `${i + 1} 顺位`),
912
+ i > 0 && h('button', {
913
+ style: { ...S.btnSm, height: 22, fontSize: 11 },
914
+ onClick: () => moveUp(i),
915
+ title: '往前挪一位(往前 = 更优先)',
916
+ }, '↑'),
917
+ h('button', {
918
+ style: { ...S.btnSm, height: 22, fontSize: 11, color: T.error, borderColor: tint(T.error, 35) },
919
+ onClick: () => removeAt(i),
920
+ }, '删'),
921
+ );
922
+ };
874
923
 
875
924
  return h('div', { key: at, style: { marginBottom: 22 } },
876
925
  // 标题行
@@ -912,21 +961,37 @@ window.__ModuleLoader__.load({
912
961
  }, '还没有选模型。从下面的下拉框加第一个 —— 它就是这个链的首选。')
913
962
  : h('div', { style: { marginBottom: 8 } },
914
963
  h('div', { style: { ...S.label } }, '降级顺序(从上到下 = 先试这个,不行再试下一个)'),
915
- chain.map((name, i) => row(name, i)),
964
+ chain.map((key, i) => row(key, i)),
916
965
  ),
917
966
 
918
967
  // 追加模型
968
+ //
969
+ // 【去重规则】用 "供应商::模型" 复合键,**不是** 模型名。
970
+ // 这样 agnes 与 agnes-backup 的同名模型可以同时出现在链里 ——
971
+ // 它们本来就是两个账号、两份配额,互为备份正是降级的意义。
919
972
  h('div', { style: { ...S.row, marginBottom: 10 } },
920
973
  h('select', {
921
974
  style: { ...S.inputAuto, flex: '1 1 300px' },
922
975
  value: '',
923
- onChange: (e) => { addModel(e.target.value); e.target.value = ''; },
976
+ onChange: (e) => {
977
+ const raw = e.target.value;
978
+ e.target.value = '';
979
+ if (!raw) return;
980
+ const { provider, id } = parseKey(raw);
981
+ addModel(provider, id);
982
+ },
924
983
  },
925
984
  h('option', { value: '' },
926
985
  chain.length ? '+ 再加一个备选模型' : '+ 选择这个链的第一个模型'),
927
986
  byProvider.map(([prov, provRows]) => h('optgroup', { key: prov, label: prov },
928
- provRows.filter((m) => !chain.includes(m.id)).map((m) =>
929
- h('option', { key: `${prov}:${m.id}`, value: m.id }, m.id)),
987
+ provRows
988
+ .filter((m) => !chain.includes(keyOf(prov, m.id)))
989
+ .map((m) => {
990
+ // 同一供应商下有重名时加后缀,避免浏览器无法区分
991
+ const dup = provRows.filter((x) => x.id === m.id).length > 1;
992
+ return h('option', { key: keyOf(prov, m.id), value: keyOf(prov, m.id) },
993
+ dup ? `${m.id}(${prov})` : m.id);
994
+ }),
930
995
  )),
931
996
  ),
932
997
  ),
@@ -61,39 +61,66 @@ export function assembleChainConfig(state, pool = null) {
61
61
  }
62
62
 
63
63
  const warnings = [];
64
- /** 真实上游:model id → { name, provider, baseUrl, keyEnv, timeoutSec } */
64
+ /** 真实上游:复合键 → { name, providerId, baseUrl, keyEnv, timeoutSec } */
65
65
  const upstreams = new Map();
66
66
  /** 链:{ pid, at, head, chain, timeoutMs } */
67
67
  const chains = [];
68
68
 
69
+ /**
70
+ * 解析链成员标识。
71
+ *
72
+ * 【复合键】界面存的是 "供应商::模型",因为同一个模型名可能挂在多个
73
+ * 供应商下(实测 agnes-3.0-flash 同时存在于 agnes / agnes-backup / llm-pi-ai;
74
+ * DeepSeek-V4.1-Flash 同时存在于 amd / trae / workbuddy)。
75
+ * 它们是不同账号、不同配额,互为备份正是降级的核心用途,
76
+ * 所以必须区分到「供应商级」而不是「模型名级」。
77
+ *
78
+ * 【兼容旧数据】早期版本只存纯模型名,这里回退到按池里第一个匹配解析。
79
+ */
80
+ const parseMember = (member) => {
81
+ const s = String(member || '');
82
+ const i = s.indexOf('::');
83
+ if (i >= 0) {
84
+ return { provider: s.slice(0, i), model: s.slice(i + 2), key: s };
85
+ }
86
+ // 旧数据:纯模型名 → 找池里第一个归属供应商
87
+ const provider = providerOfModel.get(s) || '';
88
+ return { provider, model: s, key: provider ? `${provider}::${s}` : s };
89
+ };
90
+
69
91
  // ---- 1. 收集所有链与上游 ------------------------------------------------
70
92
  for (const pid of Object.keys(routes)) {
71
93
  const agents = routes[pid]?.agents || {};
72
94
  for (const at of Object.keys(agents)) {
73
95
  const cfg = agents[at];
74
96
  if (!cfg?.primary) continue;
75
- const chain = [cfg.primary, ...(cfg.fallbacks || [])].filter(Boolean);
76
- if (!chain.length) continue;
97
+ const members = [cfg.primary, ...(cfg.fallbacks || [])].filter(Boolean);
98
+ if (!members.length) continue;
99
+
100
+ const parsed = members.map(parseMember);
77
101
 
78
102
  chains.push({
79
103
  pid,
80
104
  at,
81
105
  head: `${at}-${pid}-primary`,
82
- chain,
106
+ // 链在配置里用**供应商限定的实名**引用,避免同名串味
107
+ chain: parsed.map((p) => p.model),
108
+ members: parsed,
83
109
  timeoutMs: cfg.timeoutMs || 600000,
84
110
  });
85
111
 
86
- for (const modelId of chain) {
87
- if (upstreams.has(modelId)) continue;
88
- const providerId = providerOfModel.get(modelId) || '';
89
- const prov = providerMap.get(providerId);
112
+ for (const member of parsed) {
113
+ const { provider: providerId, model: modelId, key } = member;
114
+ if (upstreams.has(key)) continue;
115
+
90
116
  if (!providerId) {
91
117
  warnings.push(
92
- `模型 ${modelId} 不在 DSH 模型池里(可能已被删除或改了名)。` +
118
+ `模型 ${modelId} 找不到归属供应商(可能不在 DSH 模型池里)。` +
93
119
  '请到【设置-模型】确认,或在「路由配置」里重新选择。',
94
120
  );
95
121
  continue;
96
122
  }
123
+ const prov = providerMap.get(providerId);
97
124
  if (!prov?.baseUrl) {
98
125
  warnings.push(
99
126
  `供应商 ${providerId} 缺少 API 地址(baseURL),模型 ${modelId} 无法下发。` +
@@ -101,7 +128,9 @@ export function assembleChainConfig(state, pool = null) {
101
128
  );
102
129
  continue;
103
130
  }
104
- upstreams.set(modelId, {
131
+
132
+ upstreams.set(key, {
133
+ key,
105
134
  name: modelId,
106
135
  providerId,
107
136
  prefix: protocolPrefix(prov),
@@ -117,6 +146,31 @@ export function assembleChainConfig(state, pool = null) {
117
146
  const lines = [];
118
147
  const push = (s = '') => lines.push(s);
119
148
 
149
+ /**
150
+ * 给每个上游分配一个**在 model_list 内唯一**的 model_name。
151
+ *
152
+ * 【为什么需要改】同名模型可能来自不同供应商账号:
153
+ * agnes::agnes-3.0-flash (账号 1)
154
+ * agnes-backup::agnes-3.0-flash (账号 2)
155
+ * llm-pi-ai::agnes-3.0-flash (平台通道)
156
+ * LiteLLM 的 model_list 按 model_name 索引,两个同名条目会互相覆盖,
157
+ * 导致降级时永远只能命中其中一个 —— 备份账号形同虚设。
158
+ *
159
+ * 规则:模型名在全表唯一时保持原样(可读性最好);
160
+ * 一旦重名,就追加 @供应商 后缀区分。
161
+ */
162
+ const nameCount = new Map();
163
+ for (const u of upstreams.values()) {
164
+ nameCount.set(u.name, (nameCount.get(u.name) || 0) + 1);
165
+ }
166
+ for (const u of upstreams.values()) {
167
+ u.litellmName = (nameCount.get(u.name) || 0) > 1
168
+ ? `${u.name}@${u.providerId}`
169
+ : u.name;
170
+ }
171
+ /** 复合键 → 实际写进 YAML 的 model_name */
172
+ const yamlNameOf = (key) => upstreams.get(key)?.litellmName || null;
173
+
120
174
  push('# ============================================================================');
121
175
  push('# Generated by dsh-plugin-litellm-bridge - profile fallback chains');
122
176
  push(`# Generated at: ${new Date().toISOString()}`);
@@ -125,12 +179,13 @@ export function assembleChainConfig(state, pool = null) {
125
179
  push('# This file only mirrors those upstreams so the gateway can execute the');
126
180
  push('# fallback order. The plugin itself performs no routing / retry / fallback.');
127
181
  push('# api_key entries are os.environ references - never plaintext.');
182
+ push('# Entries with the same model but different accounts get a @provider suffix.');
128
183
  push('#');
129
184
 
130
185
  push('model_list:');
131
186
  for (const u of upstreams.values()) {
132
- push(` # ${u.providerId}`);
133
- push(` - model_name: ${u.name}`);
187
+ push(` # provider: ${u.providerId}${u.litellmName !== u.name ? ` | model: ${u.name}` : ''}`);
188
+ push(` - model_name: ${u.litellmName}`);
134
189
  push(' litellm_params:');
135
190
  push(` model: ${u.prefix}/${u.name}`);
136
191
  push(` api_base: ${u.baseUrl}`);
@@ -139,9 +194,9 @@ export function assembleChainConfig(state, pool = null) {
139
194
  }
140
195
  // 链头:逻辑名 → 链首模型的真实上游
141
196
  for (const c of chains) {
142
- const first = upstreams.get(c.chain[0]);
197
+ const firstKey = c.members?.[0]?.key;
198
+ const first = firstKey ? upstreams.get(firstKey) : null;
143
199
  if (!first) continue;
144
- if (upstreams.has(c.head)) continue;
145
200
  push(` # chain head: ${c.pid} / ${c.at}`);
146
201
  push(` - model_name: ${c.head}`);
147
202
  push(' litellm_params:');
@@ -153,20 +208,34 @@ export function assembleChainConfig(state, pool = null) {
153
208
  push('');
154
209
 
155
210
  // ---- 3. 降级顺序(网关执行)--------------------------------------------
211
+ // 注意:fallbacks 里必须用**YAML 中的实名**(带 @provider 后缀的那个),
212
+ // 不能用裸模型名,否则同名上游会串。
156
213
  push('litellm_settings:');
157
214
  push(' # Fallback order is assembled by the plugin, executed by the gateway.');
158
- const multi = chains.filter((c) => c.chain.length >= 2 && upstreams.has(c.chain[0]));
215
+ const multi = chains.filter((c) => (c.members || []).length >= 2);
159
216
  push(' fallbacks:');
160
217
  if (!multi.length) {
161
218
  push(' []');
162
219
  } else {
163
- for (const c of multi) push(` - ${c.head}: [${c.chain.slice(1).map((m) => `"${m}"`).join(', ')}]`);
220
+ for (const c of multi) {
221
+ const rest = c.members.slice(1)
222
+ .map((m) => yamlNameOf(m.key))
223
+ .filter(Boolean)
224
+ .map((n) => `"${n}"`);
225
+ if (!rest.length) continue;
226
+ push(` - ${c.head}: [${rest.join(', ')}]`);
227
+ }
164
228
  }
165
229
  push(' context_window_fallbacks:');
166
230
  if (!multi.length) {
167
231
  push(' []');
168
232
  } else {
169
- for (const c of multi) push(` - ${c.head}: ["${c.chain[c.chain.length - 1]}"]`);
233
+ for (const c of multi) {
234
+ const last = c.members[c.members.length - 1];
235
+ const n = yamlNameOf(last.key);
236
+ if (!n) continue;
237
+ push(` - ${c.head}: ["${n}"]`);
238
+ }
170
239
  }
171
240
  push(' num_retries: 2');
172
241
  push(' request_timeout: 900');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-litellm-bridge",
3
- "version": "2.0.9",
3
+ "version": "2.1.0",
4
4
  "description": "LiteLLM gateway bridge for DSH: assemble root / coder / research fallback chains from the DSH model pool and hand the order to a LiteLLM gateway that executes retries and fallbacks. The plugin performs no routing itself.",
5
5
  "keywords": [
6
6
  "dsh",