dsh-model-routing 0.0.0-stage → 1.2.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.
- package/LICENSE +21 -0
- package/README.md +94 -2
- package/client.js +564 -0
- package/cordis.patch.yml +4 -0
- package/docs/DETAIL.md +60 -0
- package/docs/PUBLISH.md +75 -0
- package/icon.svg +6 -0
- package/index.js +566 -0
- package/locale/en.json +48 -0
- package/locale/zh.json +48 -0
- package/package.json +50 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sunner-ds
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 模型分工 / Model routing
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
给五类工作各指定一个模型,并看用量和余额:规划与文案、写代码、出图、看图审错、决策与执行。模型列表不是写死的——它读你在 DSH 里已经配好的提供方。
|
|
4
|
+
|
|
5
|
+
Assign a model to each of five kinds of work, and watch usage and balance. The model list is not hard-coded: it is read from the providers you already configured in DSH.
|
|
6
|
+
|
|
7
|
+
## 安装 / Install
|
|
8
|
+
|
|
9
|
+
`
|
|
10
|
+
dsh plugin add dsh-model-routing
|
|
11
|
+
`
|
|
12
|
+
|
|
13
|
+
装不上的话,用仓库地址或压缩包:
|
|
14
|
+
|
|
15
|
+
`
|
|
16
|
+
dsh plugin add https://github.com/downer-dr/dsh-model-routing.git
|
|
17
|
+
|
|
18
|
+
# 或者(不依赖 git)
|
|
19
|
+
https://codeload.github.com/downer-dr/dsh-model-routing/tar.gz/refs/heads/master
|
|
20
|
+
`
|
|
21
|
+
|
|
22
|
+
本地开发直接指向目录:DSH → 插件 → 添加插件。
|
|
23
|
+
|
|
24
|
+
**装完重启会话(或刷新页面),侧栏就有「模型分工」。**
|
|
25
|
+
|
|
26
|
+
Install with dsh plugin add dsh-model-routing, or use the git URL / tarball above.
|
|
27
|
+
Restart the session (or refresh) afterwards; **Model routing** then appears in the sidebar.
|
|
28
|
+
|
|
29
|
+
## 它解决什么 / What it solves
|
|
30
|
+
|
|
31
|
+
一个会话里的事不是一类:写方案要长上下文、出图要能画、审图要能看图、改文件必须谨慎。这些活的最佳模型不是同一个。
|
|
32
|
+
插件把分工变成一份可以随时改的配置,并把用量集中到一页上。
|
|
33
|
+
|
|
34
|
+
Work in one session is not one kind: planning needs a long context, image work needs a drawing model, review needs vision, and edits must be careful. The best model differs per job. This plugin turns that division into one editable config page, with usage in the same place.
|
|
35
|
+
|
|
36
|
+
## 它不只是记着 / It acts, not just records
|
|
37
|
+
|
|
38
|
+
配好之后,这份分工会被真正用起来,有两条路:
|
|
39
|
+
|
|
40
|
+
1. **系统提示**:每次装配提示时实时读当前配置,把「哪类任务用哪个模型」写成一小段提示注入进去
|
|
41
|
+
(30 行以内)。改配置立即生效,不用重启;不想注入就设 `injectPrompt: false`。
|
|
42
|
+
2. **工具 routing_for**:agent 遇到要分出去做的活(写代码、出图、审图)时先查一下
|
|
43
|
+
`routing_for {task: "code"}`,拿到 provider/model 再带去 `subagent`,而不是凭猜。
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
routing_for {task: "list"} → 五类各是什么,一行 JSON
|
|
47
|
+
routing_for {task: "code"} → {"ok":1,"task":"code","provider":"...","model":"..."}
|
|
48
|
+
routing_for {task: "nope"} → {"ok":0,"error":"unknown_task"}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
它仍然**不会**替 DSH 内核切换当前会话的模型——那需要改内核行为,插件做不到。
|
|
52
|
+
能做到的是:让 agent 知道该派谁,并且有一条可查、可验证的路径。
|
|
53
|
+
|
|
54
|
+
## 用法 / Usage
|
|
55
|
+
|
|
56
|
+
1. 安装本插件并在插件管理里启用;
|
|
57
|
+
2. 打开左侧栏的「模型分工」(设置里也有同一页);
|
|
58
|
+
3. 每一行选一个模型,点「保存」。
|
|
59
|
+
|
|
60
|
+
1. Install and enable the plugin.
|
|
61
|
+
2. Open **Model routing** in the sidebar (the same page is under Settings).
|
|
62
|
+
3. Pick a model on each row and press **Save**.
|
|
63
|
+
|
|
64
|
+
## 模型从哪来 / Where the list comes from
|
|
65
|
+
|
|
66
|
+
下拉里的选项来自你的 profile 配置(`~/.dsh/profiles/<name>/cordis.patch.yml` 里 `llm-pi-ai` 的 `providers`):提供方 id + 模型 id。
|
|
67
|
+
所以**先在 DSH 设置里把提供方配好**,这里才有东西可选;没配就用手动添加。
|
|
68
|
+
|
|
69
|
+
The dropdown is built from your profile config (`llm-pi-ai` → `providers` in `~/.dsh/profiles/<name>/cordis.patch.yml`): provider id plus model ids.
|
|
70
|
+
So configure providers in DSH Settings first; otherwise use the manual input.
|
|
71
|
+
|
|
72
|
+
## 存在哪 / Where it is stored
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
~/.dsh/model-routing.json 分工配置(routing + balances)
|
|
76
|
+
~/.dsh/model-routing-usage.json 调用日志(可选,用于中转用量表)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
本机(deepseek)那块的用量读 `~/.dsh/storages/cost-meter/ledger.json`,装了 dsh-cost-meter 才有数。
|
|
80
|
+
|
|
81
|
+
## 局限 / Limitations
|
|
82
|
+
|
|
83
|
+
- **它不调用模型**。这份配置是一份"该用谁"的约定,真正调用由 agent 或你自己的脚本完成。
|
|
84
|
+
- 余额只能手填:中转站一般没有公开的余额接口。
|
|
85
|
+
- 用量表里的"中转"一列来自 `model-routing-usage.json`,需要外部脚本往里记;不记就是空的。
|
|
86
|
+
- 模型列表读取依赖 profile 配置文件的结构,DSH 大版本改动时可能失效。
|
|
87
|
+
|
|
88
|
+
- **It does not call models.** The config is an agreement about who should do what; the actual calls come from the agent or your own scripts.
|
|
89
|
+
- Balance is manual: relays rarely expose a public balance API.
|
|
90
|
+
- The relay usage column reads `model-routing-usage.json`, which needs an external script to record calls.
|
|
91
|
+
- Catalog parsing depends on the profile config layout and may need updating when DSH changes it.
|
|
92
|
+
|
|
93
|
+
## 许可 / License
|
|
94
|
+
|
|
95
|
+
MIT.
|
package/client.js
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
// 模型分工:浏览器侧。在主侧栏和设置页提供入口。
|
|
2
|
+
window.__ModuleLoader__.load({
|
|
3
|
+
id: 'dsh-model-routing',
|
|
4
|
+
factory(require) {
|
|
5
|
+
const React = require('react')
|
|
6
|
+
const h = React.createElement
|
|
7
|
+
const { useState, useEffect } = React
|
|
8
|
+
|
|
9
|
+
const BASE = '/dsh-model-routing'
|
|
10
|
+
const PANEL = 'model-routing'
|
|
11
|
+
const LABEL = '模型分工'
|
|
12
|
+
|
|
13
|
+
function ModelRouting() {
|
|
14
|
+
const STRINGS = {
|
|
15
|
+
zh: {
|
|
16
|
+
title: '模型分工',
|
|
17
|
+
lead: '为五类任务分别指定模型,并查看各家用量与余额。',
|
|
18
|
+
tasks: '任务分工',
|
|
19
|
+
save: '保存',
|
|
20
|
+
saving: '保存中…',
|
|
21
|
+
saved: '已保存',
|
|
22
|
+
restoreDefaults: '恢复默认',
|
|
23
|
+
loading: '加载中…',
|
|
24
|
+
ready: '就绪',
|
|
25
|
+
notLoaded: '未加载',
|
|
26
|
+
unsavedChanges: '有未保存修改',
|
|
27
|
+
emptyCatalog: '还没在 DSH 里配置模型提供方。请在设置 → 模型里添加,或手动填写。',
|
|
28
|
+
addManual: '手动添加',
|
|
29
|
+
manualPlaceholder: 'provider/model',
|
|
30
|
+
noLongerConfigured: '已不存在',
|
|
31
|
+
missingModelSuffix: (label) => `(${label})`,
|
|
32
|
+
usageAndBalance: '用量与余额',
|
|
33
|
+
routedUsage: '中转用量',
|
|
34
|
+
provider: '提供方',
|
|
35
|
+
calls: '调用次数',
|
|
36
|
+
input: '输入',
|
|
37
|
+
output: '输出',
|
|
38
|
+
balance: '手记余额',
|
|
39
|
+
date: '日期',
|
|
40
|
+
noRoutedUsage: '尚未使用分身模型',
|
|
41
|
+
localTitle: '本机(deepseek)最近 7 天',
|
|
42
|
+
balanceNote: '余额中转站不提供接口,由你手填;用量来自本机记录的调用日志。',
|
|
43
|
+
readConfigError: '读不到配置:',
|
|
44
|
+
saveFailed: '保存失败:',
|
|
45
|
+
invalidResponse: '响应无效',
|
|
46
|
+
errorSeparator: ';',
|
|
47
|
+
planningLabel: '规划与文案',
|
|
48
|
+
planningHint: '长文论证、写说明稿、审改文字。',
|
|
49
|
+
codeLabel: '写代码',
|
|
50
|
+
codeHint: '写脚本、改配置、生成图表的代码。',
|
|
51
|
+
imageLabel: '出图',
|
|
52
|
+
imageHint: '生成或改写图片、封面、插图;不写代码。',
|
|
53
|
+
reviewLabel: '看图审错',
|
|
54
|
+
reviewHint: '检查图纸、界面和版面,指出问题。',
|
|
55
|
+
executeLabel: '决策与执行',
|
|
56
|
+
executeHint: '唯一能改文件、运行脚本并做最终验证。',
|
|
57
|
+
},
|
|
58
|
+
en: {
|
|
59
|
+
title: 'Model routing',
|
|
60
|
+
lead: 'Pick a model for each of the five kinds of work, and check usage and balance.',
|
|
61
|
+
tasks: 'Model per task',
|
|
62
|
+
save: 'Save',
|
|
63
|
+
saving: 'Saving…',
|
|
64
|
+
saved: 'Saved',
|
|
65
|
+
restoreDefaults: 'Restore defaults',
|
|
66
|
+
loading: 'Loading…',
|
|
67
|
+
ready: 'Ready',
|
|
68
|
+
notLoaded: 'Not loaded',
|
|
69
|
+
unsavedChanges: 'Unsaved changes',
|
|
70
|
+
emptyCatalog: 'No model provider is configured in DSH yet. Add one in Settings → Models, or fill it in by hand.',
|
|
71
|
+
addManual: 'Add by hand',
|
|
72
|
+
manualPlaceholder: 'provider/model',
|
|
73
|
+
noLongerConfigured: 'no longer configured',
|
|
74
|
+
missingModelSuffix: (label) => ` (${label})`,
|
|
75
|
+
usageAndBalance: 'Usage and balance',
|
|
76
|
+
routedUsage: 'Routed usage',
|
|
77
|
+
provider: 'Provider',
|
|
78
|
+
calls: 'Calls',
|
|
79
|
+
input: 'Input',
|
|
80
|
+
output: 'Output',
|
|
81
|
+
balance: 'Balance',
|
|
82
|
+
date: 'Date',
|
|
83
|
+
noRoutedUsage: 'No routed model used yet',
|
|
84
|
+
localTitle: 'This machine (deepseek), last 7 days',
|
|
85
|
+
balanceNote: 'Relays expose no balance API, so balances are typed in by hand; usage comes from the local call log.',
|
|
86
|
+
readConfigError: 'Could not read config: ',
|
|
87
|
+
saveFailed: 'Save failed: ',
|
|
88
|
+
invalidResponse: 'Invalid response',
|
|
89
|
+
errorSeparator: '; ',
|
|
90
|
+
planningLabel: 'Planning and writing',
|
|
91
|
+
planningHint: 'Long arguments, documentation, editing prose.',
|
|
92
|
+
codeLabel: 'Code',
|
|
93
|
+
codeHint: 'Scripts, configuration changes, chart-generating code.',
|
|
94
|
+
imageLabel: 'Images',
|
|
95
|
+
imageHint: 'Generate or edit images, covers and illustrations; no code.',
|
|
96
|
+
reviewLabel: 'Review',
|
|
97
|
+
reviewHint: 'Inspect drawings, interfaces and layouts, and name the problems.',
|
|
98
|
+
executeLabel: 'Execution',
|
|
99
|
+
executeHint: 'The only role that edits files, runs scripts and verifies the result.',
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const lang = (() => {
|
|
104
|
+
try {
|
|
105
|
+
const htmlLang = document.documentElement.getAttribute('lang') || ''
|
|
106
|
+
if (htmlLang) return htmlLang.toLowerCase().startsWith('zh') ? 'zh' : 'en'
|
|
107
|
+
} catch {}
|
|
108
|
+
try {
|
|
109
|
+
const nav = navigator.language || ''
|
|
110
|
+
return nav.toLowerCase().startsWith('zh') ? 'zh' : 'en'
|
|
111
|
+
} catch {}
|
|
112
|
+
return 'en'
|
|
113
|
+
})()
|
|
114
|
+
const t = STRINGS[lang] || STRINGS.en
|
|
115
|
+
|
|
116
|
+
const React = require('react')
|
|
117
|
+
const h = React.createElement
|
|
118
|
+
const { useEffect, useRef, useState } = React
|
|
119
|
+
const base = '/dsh-model-routing'
|
|
120
|
+
const routes = [
|
|
121
|
+
{ key: 'planning', label: t.planningLabel, hint: t.planningHint },
|
|
122
|
+
{ key: 'code', label: t.codeLabel, hint: t.codeHint },
|
|
123
|
+
{ key: 'image', label: t.imageLabel, hint: t.imageHint },
|
|
124
|
+
{ key: 'review', label: t.reviewLabel, hint: t.reviewHint },
|
|
125
|
+
{ key: 'execute', label: t.executeLabel, hint: t.executeHint },
|
|
126
|
+
]
|
|
127
|
+
const styles = {
|
|
128
|
+
wrap: {
|
|
129
|
+
boxSizing: 'border-box',
|
|
130
|
+
width: '100%',
|
|
131
|
+
maxWidth: 760,
|
|
132
|
+
padding: '18px 22px',
|
|
133
|
+
fontSize: 13,
|
|
134
|
+
lineHeight: 1.7,
|
|
135
|
+
},
|
|
136
|
+
title: { fontSize: 17, fontWeight: 600, margin: '0 0 6px' },
|
|
137
|
+
badge: { fontSize: 11, opacity: 0.6, marginBottom: 10 },
|
|
138
|
+
section: { marginTop: 18 },
|
|
139
|
+
sectionTitle: { fontSize: 13, fontWeight: 600, margin: '0 0 8px' },
|
|
140
|
+
row: {
|
|
141
|
+
display: 'grid',
|
|
142
|
+
gridTemplateColumns: '110px minmax(0, 1fr)',
|
|
143
|
+
alignItems: 'center',
|
|
144
|
+
gap: 12,
|
|
145
|
+
padding: '8px 0',
|
|
146
|
+
borderBottom: '1px solid rgba(127,127,127,0.18)',
|
|
147
|
+
},
|
|
148
|
+
label: { fontWeight: 500 },
|
|
149
|
+
hint: { opacity: 0.62, fontSize: 12, marginTop: 2 },
|
|
150
|
+
field: {
|
|
151
|
+
boxSizing: 'border-box',
|
|
152
|
+
width: '100%',
|
|
153
|
+
maxWidth: 320,
|
|
154
|
+
padding: '5px 8px',
|
|
155
|
+
borderRadius: 6,
|
|
156
|
+
border: '1px solid rgba(127,127,127,0.35)',
|
|
157
|
+
background: 'transparent',
|
|
158
|
+
color: 'inherit',
|
|
159
|
+
fontSize: 12.5,
|
|
160
|
+
},
|
|
161
|
+
actions: { marginTop: 12, display: 'flex', flexWrap: 'wrap', gap: 10, alignItems: 'center' },
|
|
162
|
+
button: {
|
|
163
|
+
padding: '6px 14px',
|
|
164
|
+
borderRadius: 6,
|
|
165
|
+
border: '1px solid rgba(127,127,127,0.4)',
|
|
166
|
+
background: 'rgba(127,127,127,0.10)',
|
|
167
|
+
color: 'inherit',
|
|
168
|
+
fontSize: 12.5,
|
|
169
|
+
},
|
|
170
|
+
muted: { opacity: 0.65, fontSize: 12 },
|
|
171
|
+
error: { color: '#df8b7c', fontSize: 12 },
|
|
172
|
+
table: { width: '100%', borderCollapse: 'collapse', fontSize: 12 },
|
|
173
|
+
th: { textAlign: 'left', fontWeight: 500, opacity: 0.65, padding: '4px 6px' },
|
|
174
|
+
td: { padding: '4px 6px', borderTop: '1px solid rgba(127,127,127,0.15)' },
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const [catalog, setCatalog] = useState(null)
|
|
178
|
+
const [server, setServer] = useState(null)
|
|
179
|
+
const [draft, setDraft] = useState(null)
|
|
180
|
+
const [loading, setLoading] = useState(true)
|
|
181
|
+
const [saving, setSaving] = useState(false)
|
|
182
|
+
const [loadError, setLoadError] = useState('')
|
|
183
|
+
const [saveError, setSaveError] = useState('')
|
|
184
|
+
const [saved, setSaved] = useState(null)
|
|
185
|
+
const [manualText, setManualText] = useState('')
|
|
186
|
+
const [manualModels, setManualModels] = useState([])
|
|
187
|
+
const saveRequest = useRef(null)
|
|
188
|
+
|
|
189
|
+
async function request(path, options) {
|
|
190
|
+
const response = await fetch(base + path, {
|
|
191
|
+
credentials: 'same-origin',
|
|
192
|
+
...options,
|
|
193
|
+
})
|
|
194
|
+
if (!response.ok) throw new Error('HTTP ' + response.status)
|
|
195
|
+
return response.json()
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
const controller = new AbortController()
|
|
200
|
+
const options = { cache: 'no-store', signal: controller.signal }
|
|
201
|
+
|
|
202
|
+
async function load() {
|
|
203
|
+
const [catalogResult, stateResult] = await Promise.allSettled([
|
|
204
|
+
request('/catalog', options),
|
|
205
|
+
request('/state', options),
|
|
206
|
+
])
|
|
207
|
+
if (controller.signal.aborted) return
|
|
208
|
+
|
|
209
|
+
if (catalogResult.status === 'fulfilled') {
|
|
210
|
+
setCatalog(catalogResult.value.providers)
|
|
211
|
+
}
|
|
212
|
+
if (stateResult.status === 'fulfilled') {
|
|
213
|
+
const data = stateResult.value
|
|
214
|
+
setServer(data)
|
|
215
|
+
setDraft({ routing: data.routing, balances: data.balances })
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const failures = [
|
|
219
|
+
['/catalog', catalogResult],
|
|
220
|
+
['/state', stateResult],
|
|
221
|
+
].filter(([, result]) => result.status === 'rejected')
|
|
222
|
+
if (failures.length) {
|
|
223
|
+
setLoadError(t.readConfigError + failures.map(([path, result]) =>
|
|
224
|
+
path + ' ' + (result.reason?.message || String(result.reason)),
|
|
225
|
+
).join(t.errorSeparator))
|
|
226
|
+
}
|
|
227
|
+
setLoading(false)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
load()
|
|
231
|
+
return () => {
|
|
232
|
+
controller.abort()
|
|
233
|
+
saveRequest.current?.abort()
|
|
234
|
+
saveRequest.current = null
|
|
235
|
+
}
|
|
236
|
+
}, [])
|
|
237
|
+
|
|
238
|
+
useEffect(() => {
|
|
239
|
+
if (!saved) return
|
|
240
|
+
const timer = setTimeout(() => setSaved(null), 2000)
|
|
241
|
+
return () => clearTimeout(timer)
|
|
242
|
+
}, [saved])
|
|
243
|
+
|
|
244
|
+
const busy = loading || saving
|
|
245
|
+
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key)
|
|
246
|
+
const serverBalances = server?.balances ?? {}
|
|
247
|
+
const draftBalances = draft?.balances ?? {}
|
|
248
|
+
const balanceNames = Object.keys(draftBalances)
|
|
249
|
+
const dirty = Boolean(draft && server && (
|
|
250
|
+
routes.some(({ key }) =>
|
|
251
|
+
draft.routing[key]?.provider !== server.routing[key]?.provider ||
|
|
252
|
+
draft.routing[key]?.model !== server.routing[key]?.model,
|
|
253
|
+
) ||
|
|
254
|
+
balanceNames.length !== Object.keys(serverBalances).length ||
|
|
255
|
+
balanceNames.some((name) =>
|
|
256
|
+
!hasOwn(serverBalances, name) || draftBalances[name] !== serverBalances[name],
|
|
257
|
+
)
|
|
258
|
+
))
|
|
259
|
+
|
|
260
|
+
function updateDraft(update) {
|
|
261
|
+
if (busy || saveRequest.current || !draft) return
|
|
262
|
+
setDraft(update)
|
|
263
|
+
setSaved(null)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function save() {
|
|
267
|
+
if (busy || saveRequest.current || !draft) return
|
|
268
|
+
const controller = new AbortController()
|
|
269
|
+
saveRequest.current = controller
|
|
270
|
+
setSaving(true)
|
|
271
|
+
setSaved(null)
|
|
272
|
+
setSaveError('')
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
const data = await request('/routing', {
|
|
276
|
+
method: 'POST',
|
|
277
|
+
headers: { 'Content-Type': 'application/json' },
|
|
278
|
+
body: JSON.stringify({ routing: draft.routing, balances: draft.balances }),
|
|
279
|
+
signal: controller.signal,
|
|
280
|
+
})
|
|
281
|
+
if (controller.signal.aborted) return
|
|
282
|
+
if (data.ok !== true || !data.routing || !data.balances) {
|
|
283
|
+
throw new Error(data.error || t.invalidResponse)
|
|
284
|
+
}
|
|
285
|
+
const readback = { routing: data.routing, balances: data.balances }
|
|
286
|
+
setServer((previous) => ({ ...previous, ...readback }))
|
|
287
|
+
setDraft(readback)
|
|
288
|
+
setSaved(readback)
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (!controller.signal.aborted) {
|
|
291
|
+
setSaveError(t.saveFailed + (error?.message || String(error)))
|
|
292
|
+
}
|
|
293
|
+
} finally {
|
|
294
|
+
if (saveRequest.current === controller) {
|
|
295
|
+
saveRequest.current = null
|
|
296
|
+
setSaving(false)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const groups = new Map()
|
|
302
|
+
for (const provider of catalog ?? []) {
|
|
303
|
+
groups.set(provider.id, {
|
|
304
|
+
label: provider.displayName || provider.id,
|
|
305
|
+
models: new Set(provider.models),
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const catalogEmpty = catalog !== null &&
|
|
310
|
+
!Array.from(groups.values()).some(({ models }) => models.size > 0)
|
|
311
|
+
|
|
312
|
+
for (const { provider, model } of manualModels) {
|
|
313
|
+
if (!groups.has(provider)) {
|
|
314
|
+
groups.set(provider, { label: provider, models: new Set() })
|
|
315
|
+
}
|
|
316
|
+
groups.get(provider).models.add(model)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const modelValue = ({ provider, model }) => provider + '|' + model
|
|
320
|
+
const hasModel = ({ provider, model }) => groups.get(provider)?.models.has(model)
|
|
321
|
+
const modelsByValue = new Map()
|
|
322
|
+
const groupOptions = []
|
|
323
|
+
for (const [provider, { label, models }] of groups) {
|
|
324
|
+
if (!models.size) continue
|
|
325
|
+
const options = Array.from(models, (model) => {
|
|
326
|
+
const selection = { provider, model }
|
|
327
|
+
const value = modelValue(selection)
|
|
328
|
+
modelsByValue.set(value, selection)
|
|
329
|
+
return h('option', { key: model, value }, model)
|
|
330
|
+
})
|
|
331
|
+
groupOptions.push(h('optgroup', { key: provider, label }, ...options))
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const separator = manualText.indexOf('/')
|
|
335
|
+
const manualProvider = separator < 0 ? '' : manualText.slice(0, separator).trim()
|
|
336
|
+
const manualModel = separator < 0 ? '' : manualText.slice(separator + 1).trim()
|
|
337
|
+
const canAddManual = Boolean(manualProvider && manualModel)
|
|
338
|
+
|
|
339
|
+
function addManual() {
|
|
340
|
+
if (busy || saveRequest.current || !canAddManual) return
|
|
341
|
+
setManualModels((previous) => {
|
|
342
|
+
if (previous.some(({ provider, model }) =>
|
|
343
|
+
provider === manualProvider && model === manualModel,
|
|
344
|
+
)) return previous
|
|
345
|
+
return [...previous, { provider: manualProvider, model: manualModel }]
|
|
346
|
+
})
|
|
347
|
+
setManualText('')
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function renderRoute({ key, label, hint }) {
|
|
351
|
+
const emptySelection = { provider: '', model: '' }
|
|
352
|
+
const current = draft?.routing?.[key] ?? emptySelection
|
|
353
|
+
const configured = server?.routing?.[key] ?? emptySelection
|
|
354
|
+
const options = new Map(modelsByValue)
|
|
355
|
+
options.set('', emptySelection)
|
|
356
|
+
|
|
357
|
+
const missingOptions = []
|
|
358
|
+
for (const selection of [current, configured]) {
|
|
359
|
+
if ((!selection.provider && !selection.model) || hasModel(selection)) continue
|
|
360
|
+
const value = modelValue(selection)
|
|
361
|
+
if (options.has(value)) continue
|
|
362
|
+
options.set(value, selection)
|
|
363
|
+
missingOptions.push(h('option', { key: value, value },
|
|
364
|
+
selection.provider + ' / ' + selection.model +
|
|
365
|
+
(catalog === null ? '' : t.missingModelSuffix(t.noLongerConfigured)),
|
|
366
|
+
))
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return h('label', { key, style: styles.row },
|
|
370
|
+
h('span', null,
|
|
371
|
+
h('span', { style: styles.label }, label),
|
|
372
|
+
h('span', { style: { ...styles.hint, display: 'block' } }, hint),
|
|
373
|
+
),
|
|
374
|
+
h('select', {
|
|
375
|
+
style: styles.field,
|
|
376
|
+
'aria-label': label,
|
|
377
|
+
value: current.provider || current.model ? modelValue(current) : '',
|
|
378
|
+
disabled: busy || !draft || groupOptions.length === 0,
|
|
379
|
+
onChange: (event) => {
|
|
380
|
+
const selection = options.get(event.target.value)
|
|
381
|
+
if (!selection) return
|
|
382
|
+
updateDraft((previous) => ({
|
|
383
|
+
...previous,
|
|
384
|
+
routing: { ...previous.routing, [key]: selection },
|
|
385
|
+
}))
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
h('option', { value: '' }, '—'),
|
|
389
|
+
...missingOptions,
|
|
390
|
+
...groupOptions),
|
|
391
|
+
)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function fmt(number) {
|
|
395
|
+
if (!number) return '0'
|
|
396
|
+
if (number >= 1e6) return (number / 1e6).toFixed(2) + 'M'
|
|
397
|
+
if (number >= 1e3) return (number / 1e3).toFixed(1) + 'k'
|
|
398
|
+
return String(number)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function table(label, columns, rows) {
|
|
402
|
+
return h('table', { style: styles.table, 'aria-label': label },
|
|
403
|
+
h('thead', null, h('tr', null,
|
|
404
|
+
...columns.map((column) =>
|
|
405
|
+
h('th', { key: column, scope: 'col', style: styles.th }, column),
|
|
406
|
+
),
|
|
407
|
+
)),
|
|
408
|
+
h('tbody', null, ...rows),
|
|
409
|
+
)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const relay = server?.relay?.byProvider ?? {}
|
|
413
|
+
const relayUsageNames = Object.keys(relay)
|
|
414
|
+
const relayNames = Array.from(new Set([...relayUsageNames, ...Object.keys(serverBalances)]))
|
|
415
|
+
const relayRows = relayNames.map((name) => {
|
|
416
|
+
const row = hasOwn(relay, name) ? relay[name] : undefined
|
|
417
|
+
return h('tr', { key: name },
|
|
418
|
+
h('td', { style: styles.td }, name),
|
|
419
|
+
h('td', { style: styles.td }, String(row?.calls ?? 0)),
|
|
420
|
+
h('td', { style: styles.td }, fmt(row?.input)),
|
|
421
|
+
h('td', { style: styles.td }, fmt(row?.output)),
|
|
422
|
+
h('td', { style: styles.td },
|
|
423
|
+
h('input', {
|
|
424
|
+
type: 'number',
|
|
425
|
+
step: 'any',
|
|
426
|
+
'aria-label': name + ' ' + t.balance,
|
|
427
|
+
style: { ...styles.field, maxWidth: 110, padding: '2px 6px' },
|
|
428
|
+
value: hasOwn(draftBalances, name) ? draftBalances[name] : '',
|
|
429
|
+
disabled: busy || !draft,
|
|
430
|
+
onChange: (event) => {
|
|
431
|
+
const { value, valueAsNumber, validity } = event.target
|
|
432
|
+
if (validity.badInput || (value !== '' && !Number.isFinite(valueAsNumber))) return
|
|
433
|
+
updateDraft((previous) => {
|
|
434
|
+
const balances = { ...previous.balances, [name]: valueAsNumber }
|
|
435
|
+
if (value === '') delete balances[name]
|
|
436
|
+
return { ...previous, balances }
|
|
437
|
+
})
|
|
438
|
+
},
|
|
439
|
+
}),
|
|
440
|
+
),
|
|
441
|
+
)
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
const localTitle = t.localTitle
|
|
445
|
+
const localRows = (server?.local?.days ?? []).slice()
|
|
446
|
+
.sort((left, right) => right.date.localeCompare(left.date))
|
|
447
|
+
.slice(0, 7)
|
|
448
|
+
.map((row) => h('tr', { key: row.date },
|
|
449
|
+
h('td', { style: styles.td }, row.date),
|
|
450
|
+
h('td', { style: styles.td }, fmt(row.input)),
|
|
451
|
+
h('td', { style: styles.td }, fmt(row.output)),
|
|
452
|
+
))
|
|
453
|
+
|
|
454
|
+
return h('div', { style: styles.wrap },
|
|
455
|
+
h('h2', { style: styles.title }, t.title),
|
|
456
|
+
h('div', { style: styles.muted }, t.lead),
|
|
457
|
+
h('div', { style: styles.badge, role: 'status' },
|
|
458
|
+
'v1.0.0 · ' + (loading ? t.loading : catalog !== null && draft ? t.ready : t.notLoaded),
|
|
459
|
+
),
|
|
460
|
+
|
|
461
|
+
h('div', { style: styles.section },
|
|
462
|
+
h('div', { style: styles.sectionTitle }, t.tasks),
|
|
463
|
+
...routes.map(renderRoute),
|
|
464
|
+
catalogEmpty ? h('div', { style: { marginTop: 10 } },
|
|
465
|
+
h('div', { style: styles.muted }, t.emptyCatalog),
|
|
466
|
+
h('div', { style: styles.actions },
|
|
467
|
+
h('input', {
|
|
468
|
+
type: 'text',
|
|
469
|
+
placeholder: t.manualPlaceholder,
|
|
470
|
+
'aria-label': t.manualPlaceholder,
|
|
471
|
+
style: styles.field,
|
|
472
|
+
value: manualText,
|
|
473
|
+
disabled: busy,
|
|
474
|
+
onChange: (event) => setManualText(event.target.value),
|
|
475
|
+
}),
|
|
476
|
+
h('button', {
|
|
477
|
+
type: 'button',
|
|
478
|
+
style: styles.button,
|
|
479
|
+
disabled: busy || !canAddManual,
|
|
480
|
+
onClick: addManual,
|
|
481
|
+
}, t.addManual),
|
|
482
|
+
),
|
|
483
|
+
) : null,
|
|
484
|
+
),
|
|
485
|
+
|
|
486
|
+
h('div', { style: styles.actions },
|
|
487
|
+
h('button', {
|
|
488
|
+
type: 'button',
|
|
489
|
+
style: styles.button,
|
|
490
|
+
onClick: save,
|
|
491
|
+
disabled: busy || !draft,
|
|
492
|
+
}, saving ? t.saving : t.save),
|
|
493
|
+
dirty ? h('span', { style: { fontSize: 12, opacity: 0.7 } }, t.unsavedChanges) : null,
|
|
494
|
+
h('button', {
|
|
495
|
+
type: 'button',
|
|
496
|
+
style: styles.button,
|
|
497
|
+
disabled: busy || !draft || !server?.defaults,
|
|
498
|
+
onClick: () => {
|
|
499
|
+
if (server?.defaults) {
|
|
500
|
+
updateDraft((previous) => ({ ...previous, routing: server.defaults }))
|
|
501
|
+
}
|
|
502
|
+
},
|
|
503
|
+
}, t.restoreDefaults),
|
|
504
|
+
saved ? h('span', { style: styles.muted, role: 'status' }, t.saved) : null,
|
|
505
|
+
),
|
|
506
|
+
loadError ? h('div', { style: styles.error, role: 'alert' }, loadError) : null,
|
|
507
|
+
saveError ? h('div', { style: styles.error, role: 'alert' }, saveError) : null,
|
|
508
|
+
|
|
509
|
+
h('div', { style: styles.section },
|
|
510
|
+
h('div', { style: styles.sectionTitle }, t.usageAndBalance),
|
|
511
|
+
table(t.routedUsage, [t.provider, t.calls, t.input, t.output, t.balance], relayRows),
|
|
512
|
+
server && relayUsageNames.length === 0
|
|
513
|
+
? h('div', { style: styles.muted }, t.noRoutedUsage)
|
|
514
|
+
: null,
|
|
515
|
+
h('div', { style: { marginTop: 12 } },
|
|
516
|
+
h('div', { style: { ...styles.muted, marginBottom: 4 } }, localTitle),
|
|
517
|
+
table(localTitle, [t.date, t.input, t.output], localRows),
|
|
518
|
+
),
|
|
519
|
+
h('div', { style: { ...styles.muted, marginTop: 8 } }, t.balanceNote),
|
|
520
|
+
),
|
|
521
|
+
)
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function SectionIcon() {
|
|
525
|
+
return h('svg', {
|
|
526
|
+
viewBox: '0 0 24 24', width: 20, height: 20, 'aria-hidden': true,
|
|
527
|
+
style: { display: 'block' },
|
|
528
|
+
},
|
|
529
|
+
h('path', {
|
|
530
|
+
d: 'M4 6h6v5H4zM14 6h6v5h-6zM4 13h6v5H4zM14 13h6v5h-6z',
|
|
531
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.5, strokeLinejoin: 'round',
|
|
532
|
+
}),
|
|
533
|
+
)
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return {
|
|
537
|
+
inject: ['slots', 'uiWorkspace'],
|
|
538
|
+
apply(ctx) {
|
|
539
|
+
ctx.slots.inject('settings.section.icon', () => ctx.slots.register({
|
|
540
|
+
name: 'settings.section.icon',
|
|
541
|
+
id: PANEL,
|
|
542
|
+
order: 45,
|
|
543
|
+
}, SectionIcon))
|
|
544
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
545
|
+
name: 'settings.section',
|
|
546
|
+
id: PANEL,
|
|
547
|
+
order: 45,
|
|
548
|
+
label: LABEL,
|
|
549
|
+
}, ModelRouting))
|
|
550
|
+
|
|
551
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
552
|
+
name: 'sidebar.panellist',
|
|
553
|
+
id: PANEL,
|
|
554
|
+
order: 35,
|
|
555
|
+
label: () => LABEL,
|
|
556
|
+
}, SectionIcon))
|
|
557
|
+
ctx.slots.inject('main', () => ctx.slots.register({
|
|
558
|
+
name: 'main',
|
|
559
|
+
key: PANEL,
|
|
560
|
+
}, ModelRouting))
|
|
561
|
+
},
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
})
|
package/cordis.patch.yml
ADDED