@qzsy/dsh-image-generation 0.0.0-stage → 0.1.3
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/NOTICE +6 -0
- package/README.md +128 -2
- package/cordis.patch.yml +8 -0
- package/dist/client.js +1090 -0
- package/dist/index.js +785 -0
- package/package.json +68 -4
package/dist/client.js
ADDED
|
@@ -0,0 +1,1090 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-image-generation — Client half.
|
|
3
|
+
*
|
|
4
|
+
* - Settings → 生图配置: add/edit image providers and their models
|
|
5
|
+
* - Settings → 插件 → 插件配置 → 图形生成: pick the single model the tool uses
|
|
6
|
+
* - tool.call.toolview for image_generate: inline gallery
|
|
7
|
+
*/
|
|
8
|
+
window.__ModuleLoader__.load({
|
|
9
|
+
id: '@qzsy/dsh-image-generation',
|
|
10
|
+
factory: (require) => {
|
|
11
|
+
var module = { exports: {} }
|
|
12
|
+
var exports = module.exports
|
|
13
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
|
|
14
|
+
|
|
15
|
+
const React = require('react')
|
|
16
|
+
const primitives = require('@deepseek-ai/dsh-client-ui-primitives')
|
|
17
|
+
// Icon exports were renamed in DSH 0.1.7 (size suffix dropped in favor of
|
|
18
|
+
// Regular/Medium weights); resolve whichever name this runtime exports.
|
|
19
|
+
const pickIcon = (...names) => names.map((n) => primitives[n]).find((v) => typeof v === 'function')
|
|
20
|
+
const IconTrash = pickIcon('IconTrashOutline16', 'IconTrashOutlineRegular', 'IconTrashOutlineMedium')
|
|
21
|
+
const IconClose = pickIcon('IconCloseOutline16', 'IconCloseOutlineRegular', 'IconCloseOutlineMedium')
|
|
22
|
+
const IconPlus = pickIcon('IconPlusOutline16', 'IconPlusOutlineRegular', 'IconPlusOutlineMedium')
|
|
23
|
+
const IconChevronDown = pickIcon('IconChevronDownOutline14', 'IconChevronDownOutlineRegular', 'IconChevronDownOutlineMedium')
|
|
24
|
+
const IconChevronUp = pickIcon('IconChevronUpOutline14', 'IconChevronUpOutlineRegular', 'IconChevronUpOutlineMedium')
|
|
25
|
+
const IconSparkle = pickIcon('IconSparkle16', 'IconSparkleRegular', 'IconSparkleMedium')
|
|
26
|
+
const Menu = primitives.Menu
|
|
27
|
+
|
|
28
|
+
const SELECT_CSS = '.dsh-ig-select{box-sizing:border-box;background:var(--dsw-alias-bg-module-platform);height:36px;font:inherit;color:var(--dsw-alias-label-primary);cursor:pointer;border:none;border-radius:18px;align-items:center;gap:12px;padding:0 14px;font-size:14px;line-height:22px;display:inline-flex;max-width:min(100%,280px)}.dsh-ig-select:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover)}.dsh-ig-select:disabled{opacity:.5;cursor:default}.dsh-ig-select-label{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}'
|
|
29
|
+
|
|
30
|
+
function ensureSelectCss() {
|
|
31
|
+
if (typeof document === 'undefined') return
|
|
32
|
+
if (document.querySelector('style[data-plugin-css="dsh-image-generation-select"]')) return
|
|
33
|
+
const tag = document.createElement('style')
|
|
34
|
+
tag.dataset.plugin = 'dsh-image-generation'
|
|
35
|
+
tag.dataset.pluginCss = 'dsh-image-generation-select'
|
|
36
|
+
tag.textContent = SELECT_CSS
|
|
37
|
+
document.head.appendChild(tag)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const CATALOG_NS = 'image-gen'
|
|
41
|
+
const RUNTIME_NS = 'image-gen-runtime'
|
|
42
|
+
const LOCALE_NS = 'settings.image-gen'
|
|
43
|
+
const RPC_CHANNEL = '/image-gen'
|
|
44
|
+
|
|
45
|
+
const API_FORMATS = [
|
|
46
|
+
{ id: 'openai-images', label: 'OpenAI Images (/v1/images/generations)', hint: 'gpt-image-2、DALL·E、以及兼容 OpenAI Images 的网关' },
|
|
47
|
+
{ id: 'xai-images', label: 'xAI Images (/v1/images/generations)', hint: 'grok-imagine-image-2.0,使用 aspect_ratio' },
|
|
48
|
+
{ id: 'gemini-image', label: 'Gemini / Imagen (generateContent / predict)', hint: 'gemini-*-image、imagen-*;官方需 generativelanguage.googleapis.com' },
|
|
49
|
+
{ id: 'openai-chat-image', label: 'OpenAI Chat Completions (/v1/chat/completions)', hint: '在对话响应中返回图片的模型(部分聚合网关)' },
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
const en = {
|
|
53
|
+
nav: 'Image generation',
|
|
54
|
+
title: 'Image generation',
|
|
55
|
+
intro: 'Add image providers and models here. Then pick one model under Settings → Plugins → Plugin configuration → Image generation. Conversations call image_generate with that model.',
|
|
56
|
+
addProvider: 'Add provider',
|
|
57
|
+
providerName: 'Name',
|
|
58
|
+
baseUrl: 'Base URL',
|
|
59
|
+
apiFormat: 'API format',
|
|
60
|
+
apiKey: 'API Key',
|
|
61
|
+
apiKeyPlaceholder: 'Enter an API key, or leave blank to keep the stored one',
|
|
62
|
+
apiKeySet: 'Configured',
|
|
63
|
+
apiKeyUnset: 'Not configured',
|
|
64
|
+
models: 'Models',
|
|
65
|
+
addModel: 'Add model',
|
|
66
|
+
modelId: 'Model ID',
|
|
67
|
+
modelName: 'Display name',
|
|
68
|
+
save: 'Save',
|
|
69
|
+
saving: 'Saving…',
|
|
70
|
+
saved: 'Saved.',
|
|
71
|
+
cancel: 'Cancel',
|
|
72
|
+
delete: 'Delete',
|
|
73
|
+
edit: 'Edit',
|
|
74
|
+
empty: 'No image providers yet. Add one to get started.',
|
|
75
|
+
loadFailed: 'Could not load image-generation settings.',
|
|
76
|
+
retry: 'Retry',
|
|
77
|
+
readOnly: 'Settings are read-only in this deployment.',
|
|
78
|
+
nameRequired: 'Provider name is required.',
|
|
79
|
+
baseUrlRequired: 'Base URL is required.',
|
|
80
|
+
modelRequired: 'Add at least one model, or delete unused rows.',
|
|
81
|
+
modelIdRequired: 'Model ID is required.',
|
|
82
|
+
modelIdDuplicate: 'Model IDs must be unique.',
|
|
83
|
+
conflict: 'Settings changed elsewhere. Reopen this card and try again.',
|
|
84
|
+
saveFailed: 'The deployment did not accept these values.',
|
|
85
|
+
cardTitle: 'Image generation',
|
|
86
|
+
cardDescription: 'Choose the single model conversations use for image_generate.',
|
|
87
|
+
enabled: 'Enable image_generate',
|
|
88
|
+
enabledHint: 'When off, the tool refuses until you turn it back on.',
|
|
89
|
+
pickModel: 'Active model',
|
|
90
|
+
pickHint: 'Only one model can be active. Configure providers under Image generation in the sidebar.',
|
|
91
|
+
noModels: 'No models yet. Add providers under Settings → Image generation first.',
|
|
92
|
+
expand: 'Expand',
|
|
93
|
+
collapse: 'Collapse',
|
|
94
|
+
discard: 'Discard',
|
|
95
|
+
unsaved: 'Unsaved',
|
|
96
|
+
generating: 'Generating…',
|
|
97
|
+
image: 'Image',
|
|
98
|
+
viewImage: 'View image',
|
|
99
|
+
viewImageNamed: 'View {name}',
|
|
100
|
+
imageLoading: 'Loading image…',
|
|
101
|
+
imageLoadFailed: 'Could not load image. Click to retry.',
|
|
102
|
+
imagePreview: 'Image preview',
|
|
103
|
+
imageClose: 'Close',
|
|
104
|
+
defaultSize: 'Default size',
|
|
105
|
+
defaultQuality: 'Default quality',
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const zh = {
|
|
109
|
+
nav: '生图配置',
|
|
110
|
+
title: '生图配置',
|
|
111
|
+
intro: '在这里添加生图供应商和模型。然后到 设置 → 插件 → 插件配置 → 图形生成 中选择一个模型。对话需要生图时,会调用 image_generate 使用该模型。',
|
|
112
|
+
addProvider: '添加供应商',
|
|
113
|
+
providerName: '名称',
|
|
114
|
+
baseUrl: 'Base URL',
|
|
115
|
+
apiFormat: 'API 格式',
|
|
116
|
+
apiKey: 'API Key',
|
|
117
|
+
apiKeyPlaceholder: '输入 API Key,留空则保留已保存的密钥',
|
|
118
|
+
apiKeySet: '已配置',
|
|
119
|
+
apiKeyUnset: '未配置',
|
|
120
|
+
models: '模型列表',
|
|
121
|
+
addModel: '添加模型',
|
|
122
|
+
modelId: '模型 ID',
|
|
123
|
+
modelName: '显示名称',
|
|
124
|
+
save: '保存',
|
|
125
|
+
saving: '保存中…',
|
|
126
|
+
saved: '已保存。',
|
|
127
|
+
cancel: '取消',
|
|
128
|
+
delete: '删除',
|
|
129
|
+
edit: '编辑',
|
|
130
|
+
empty: '还没有生图供应商,点击下方按钮添加。',
|
|
131
|
+
loadFailed: '无法加载生图配置。',
|
|
132
|
+
retry: '重试',
|
|
133
|
+
readOnly: '当前部署的设置为只读。',
|
|
134
|
+
nameRequired: '请填写供应商名称。',
|
|
135
|
+
baseUrlRequired: '请填写 Base URL。',
|
|
136
|
+
modelRequired: '请至少添加一个模型,或删掉空行。',
|
|
137
|
+
modelIdRequired: '模型 ID 不能为空。',
|
|
138
|
+
modelIdDuplicate: '模型 ID 不能重复。',
|
|
139
|
+
conflict: '设置已在其他位置更新,请关闭后重试。',
|
|
140
|
+
saveFailed: '保存失败,已保留你的修改。',
|
|
141
|
+
cardTitle: '图形生成',
|
|
142
|
+
cardDescription: '选择对话里 image_generate 实际使用的那一个模型。',
|
|
143
|
+
enabled: '启用 image_generate',
|
|
144
|
+
enabledHint: '关闭后工具会拒绝生图,直到重新打开。',
|
|
145
|
+
pickModel: '当前模型',
|
|
146
|
+
pickHint: '只能选择一个模型。供应商和模型在左侧「生图配置」中维护。',
|
|
147
|
+
noModels: '还没有可用模型。请先到 设置 → 生图配置 添加供应商。',
|
|
148
|
+
expand: '展开设置',
|
|
149
|
+
collapse: '收起设置',
|
|
150
|
+
discard: '放弃修改',
|
|
151
|
+
unsaved: '未保存',
|
|
152
|
+
generating: '正在生成…',
|
|
153
|
+
image: '图片',
|
|
154
|
+
viewImage: '查看图片',
|
|
155
|
+
viewImageNamed: '查看 {name}',
|
|
156
|
+
imageLoading: '正在加载图片…',
|
|
157
|
+
imageLoadFailed: '图片加载失败,点击重试。',
|
|
158
|
+
imagePreview: '图片预览',
|
|
159
|
+
imageClose: '关闭',
|
|
160
|
+
defaultSize: '默认尺寸',
|
|
161
|
+
defaultQuality: '默认质量',
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function translateFactory(dict) {
|
|
165
|
+
return (key, params) => {
|
|
166
|
+
let text = dict[key] ?? en[key] ?? key
|
|
167
|
+
for (const [name, value] of Object.entries(params ?? {})) {
|
|
168
|
+
text = String(text).replaceAll(`{${name}}`, String(value))
|
|
169
|
+
}
|
|
170
|
+
return text
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function slugify(name) {
|
|
175
|
+
const slug = String(name ?? '').trim().toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')
|
|
176
|
+
return slug || 'provider'
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function apiKeyEnvFor(id) {
|
|
180
|
+
return `IMAGE_GEN_${String(id).toUpperCase().replace(/[^A-Z0-9]+/g, '_')}_API_KEY`
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function uniqueId(name, taken) {
|
|
184
|
+
const base = slugify(name)
|
|
185
|
+
if (!taken.has(base)) return base
|
|
186
|
+
let n = 2
|
|
187
|
+
while (taken.has(`${base}-${n}`)) n += 1
|
|
188
|
+
return `${base}-${n}`
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function newModel() {
|
|
192
|
+
return { id: '', name: '' }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function cloneProviders(value) {
|
|
196
|
+
const list = Array.isArray(value?.providers) ? value.providers : []
|
|
197
|
+
return list.map((row) => ({
|
|
198
|
+
id: String(row.id ?? ''),
|
|
199
|
+
name: String(row.name ?? ''),
|
|
200
|
+
baseUrl: String(row.baseUrl ?? ''),
|
|
201
|
+
apiFormat: String(row.apiFormat ?? 'openai-images'),
|
|
202
|
+
apiKeyEnv: String(row.apiKeyEnv ?? ''),
|
|
203
|
+
models: Array.isArray(row.models)
|
|
204
|
+
? row.models.map((model) => ({ id: String(model.id ?? ''), name: String(model.name ?? '') }))
|
|
205
|
+
: [],
|
|
206
|
+
}))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const css = {
|
|
210
|
+
section: { display: 'flex', flexDirection: 'column', gap: 12, maxWidth: 640, color: 'var(--dsw-alias-label-primary)' },
|
|
211
|
+
title: { margin: 0, fontSize: 18, fontWeight: 600, lineHeight: '26px' },
|
|
212
|
+
intro: { margin: 0, color: 'var(--dsw-alias-label-tertiary)', fontSize: 14, lineHeight: '22px' },
|
|
213
|
+
card: {
|
|
214
|
+
border: '1px solid var(--dsw-alias-border-l2)', borderRadius: 12,
|
|
215
|
+
padding: '12px 14px', display: 'flex', flexDirection: 'column', gap: 10,
|
|
216
|
+
background: 'var(--dsw-alias-bg-layer-3)',
|
|
217
|
+
},
|
|
218
|
+
row: { display: 'flex', alignItems: 'center', gap: 8 },
|
|
219
|
+
name: { fontWeight: 500, fontSize: 14, lineHeight: '22px', flex: 1, minWidth: 0 },
|
|
220
|
+
meta: { margin: 0, fontSize: 12, lineHeight: '18px', color: 'var(--dsw-alias-label-tertiary)' },
|
|
221
|
+
field: { display: 'flex', flexDirection: 'column', gap: 4 },
|
|
222
|
+
label: { fontSize: 12, lineHeight: '18px', color: 'var(--dsw-alias-label-secondary)' },
|
|
223
|
+
input: {
|
|
224
|
+
height: 32, width: '100%', boxSizing: 'border-box',
|
|
225
|
+
border: '1px solid var(--dsw-alias-border-l2)', borderRadius: 8,
|
|
226
|
+
padding: '0 10px', font: 'inherit', fontSize: 14, lineHeight: '22px',
|
|
227
|
+
background: 'var(--dsw-alias-bg-layer-1)', color: 'var(--dsw-alias-label-primary)',
|
|
228
|
+
},
|
|
229
|
+
modelRow: {
|
|
230
|
+
display: 'flex', alignItems: 'center', gap: 8,
|
|
231
|
+
border: '1px solid var(--dsw-alias-border-l2)', borderRadius: 10,
|
|
232
|
+
padding: '8px 10px',
|
|
233
|
+
},
|
|
234
|
+
button: {
|
|
235
|
+
boxSizing: 'border-box', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
|
|
236
|
+
height: 28, padding: '0 10px', borderRadius: 14,
|
|
237
|
+
border: '1px solid var(--dsw-alias-border-l2)', background: 'transparent',
|
|
238
|
+
color: 'var(--dsw-alias-label-primary)', font: 'inherit', fontSize: 12, lineHeight: '18px',
|
|
239
|
+
cursor: 'pointer',
|
|
240
|
+
},
|
|
241
|
+
primary: {
|
|
242
|
+
boxSizing: 'border-box', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
|
|
243
|
+
height: 32, padding: '0 14px', borderRadius: 16, border: 'none',
|
|
244
|
+
background: 'var(--dsw-alias-button-primary-fill, #3b82f6)',
|
|
245
|
+
color: 'var(--dsw-alias-label-primary-foreground, #fff)',
|
|
246
|
+
font: 'inherit', fontSize: 13, cursor: 'pointer',
|
|
247
|
+
},
|
|
248
|
+
iconBtn: {
|
|
249
|
+
boxSizing: 'border-box', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
|
|
250
|
+
width: 28, height: 28, border: 'none', background: 'transparent', cursor: 'pointer',
|
|
251
|
+
color: 'var(--dsw-alias-label-tertiary)', borderRadius: 8, padding: 0, flex: 'none',
|
|
252
|
+
},
|
|
253
|
+
error: { margin: 0, fontSize: 12, lineHeight: '18px', color: 'var(--dsw-alias-state-error-primary)' },
|
|
254
|
+
ok: { margin: 0, fontSize: 12, lineHeight: '18px', color: 'var(--dsw-alias-state-success-primary)' },
|
|
255
|
+
actions: { display: 'flex', gap: 8, justifyContent: 'flex-end', alignItems: 'center', flexWrap: 'wrap' },
|
|
256
|
+
pluginCard: {
|
|
257
|
+
border: '0.5px solid var(--dsw-alias-border-l4)', background: 'var(--dsw-alias-bg-layer-3)',
|
|
258
|
+
borderRadius: 16, listStyle: 'none',
|
|
259
|
+
},
|
|
260
|
+
pluginHeader: {
|
|
261
|
+
appearance: 'none', width: '100%', font: 'inherit', color: 'inherit', textAlign: 'left',
|
|
262
|
+
cursor: 'pointer', background: 'transparent', border: 0, borderRadius: 12,
|
|
263
|
+
display: 'flex', alignItems: 'center', gap: 12, padding: '14px 16px',
|
|
264
|
+
},
|
|
265
|
+
pluginName: { color: 'var(--dsw-alias-label-primary)', fontSize: 15, fontWeight: 600, lineHeight: 1.4 },
|
|
266
|
+
pluginDesc: { color: 'var(--dsw-alias-label-tertiary)', fontSize: 13, lineHeight: 1.5 },
|
|
267
|
+
pluginBody: { borderTop: '0.5px solid var(--dsw-alias-border-l2)', margin: '0 16px', paddingBottom: 8 },
|
|
268
|
+
switch: {
|
|
269
|
+
boxSizing: 'border-box', background: 'var(--dsw-alias-border-l3)', cursor: 'pointer',
|
|
270
|
+
border: 0, borderRadius: 10, flex: 'none', width: 36, height: 20, padding: 2, position: 'relative',
|
|
271
|
+
},
|
|
272
|
+
prefRow: { display: 'flex', alignItems: 'center', gap: 8, padding: '8px 0' },
|
|
273
|
+
prefLabel: { minWidth: 0, flex: 1, color: 'var(--dsw-alias-label-primary)', fontSize: 14, lineHeight: '22px' },
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function SelectMenu(props) {
|
|
277
|
+
const [open, setOpen] = React.useState(false)
|
|
278
|
+
const active = props.options.find((row) => row.id === props.value)?.label ?? props.placeholder ?? props.value ?? ''
|
|
279
|
+
if (typeof Menu !== 'function') return null
|
|
280
|
+
return React.createElement(Menu, {
|
|
281
|
+
open,
|
|
282
|
+
onClose: () => setOpen(false),
|
|
283
|
+
items: props.options,
|
|
284
|
+
selectedId: props.value,
|
|
285
|
+
onSelect: (id) => {
|
|
286
|
+
setOpen(false)
|
|
287
|
+
props.onPick(id)
|
|
288
|
+
},
|
|
289
|
+
align: 'end',
|
|
290
|
+
portal: true,
|
|
291
|
+
dense: true,
|
|
292
|
+
anchor: React.createElement('button', {
|
|
293
|
+
type: 'button',
|
|
294
|
+
className: 'dsh-ig-select',
|
|
295
|
+
disabled: props.disabled || props.options.length === 0,
|
|
296
|
+
'aria-haspopup': 'menu',
|
|
297
|
+
'aria-expanded': open,
|
|
298
|
+
'aria-label': props.label,
|
|
299
|
+
onClick: () => setOpen((value) => !value),
|
|
300
|
+
},
|
|
301
|
+
React.createElement('span', { className: 'dsh-ig-select-label' }, active),
|
|
302
|
+
IconChevronDown ? React.createElement(IconChevronDown, { size: 14 }) : null,
|
|
303
|
+
),
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function PrefRow(props) {
|
|
308
|
+
return React.createElement('div', { style: css.prefRow },
|
|
309
|
+
React.createElement('span', { style: css.prefLabel }, props.label),
|
|
310
|
+
React.createElement(SelectMenu, {
|
|
311
|
+
label: props.label,
|
|
312
|
+
value: props.value,
|
|
313
|
+
options: props.options,
|
|
314
|
+
disabled: props.disabled,
|
|
315
|
+
placeholder: props.placeholder,
|
|
316
|
+
onPick: props.onPick,
|
|
317
|
+
}),
|
|
318
|
+
)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function IconButton(props) {
|
|
322
|
+
return React.createElement('button', {
|
|
323
|
+
type: 'button',
|
|
324
|
+
style: { ...css.iconBtn, ...props.style },
|
|
325
|
+
disabled: props.disabled,
|
|
326
|
+
'aria-label': props.label,
|
|
327
|
+
onClick: props.onClick,
|
|
328
|
+
}, props.children)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function formatLabel(id) {
|
|
332
|
+
return API_FORMATS.find((row) => row.id === id)?.label ?? id
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function formatHint(id) {
|
|
336
|
+
return API_FORMATS.find((row) => row.id === id)?.hint ?? ''
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function useSnapshot(scope) {
|
|
340
|
+
const [snap, setSnap] = React.useState(() => scope.getSnapshot())
|
|
341
|
+
React.useEffect(() => {
|
|
342
|
+
setSnap(scope.getSnapshot())
|
|
343
|
+
return scope.subscribe(() => setSnap(scope.getSnapshot()))
|
|
344
|
+
}, [scope])
|
|
345
|
+
return snap
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function ProviderEditor(props) {
|
|
349
|
+
const { t, draft, setDraft, keyDraft, setKeyDraft, keyState, busy, error, saved, writable, onSave, onCancel, onDelete } = props
|
|
350
|
+
return React.createElement('div', { style: { display: 'flex', flexDirection: 'column', gap: 10 } },
|
|
351
|
+
React.createElement('div', { style: css.row },
|
|
352
|
+
React.createElement('input', {
|
|
353
|
+
style: { ...css.input, fontWeight: 600 },
|
|
354
|
+
value: draft.name,
|
|
355
|
+
placeholder: t('providerName'),
|
|
356
|
+
'aria-label': t('providerName'),
|
|
357
|
+
disabled: !writable || busy,
|
|
358
|
+
onChange: (event) => setDraft({ ...draft, name: event.target.value }),
|
|
359
|
+
}),
|
|
360
|
+
React.createElement(IconButton, {
|
|
361
|
+
label: t('delete'), disabled: !writable || busy, onClick: onDelete,
|
|
362
|
+
}, IconTrash ? React.createElement(IconTrash, { size: 16 }) : t('delete')),
|
|
363
|
+
),
|
|
364
|
+
React.createElement('div', { style: css.field },
|
|
365
|
+
React.createElement('span', { style: css.label }, t('baseUrl')),
|
|
366
|
+
React.createElement('input', {
|
|
367
|
+
style: css.input, value: draft.baseUrl, placeholder: 'https://api.example.com/v1',
|
|
368
|
+
'aria-label': t('baseUrl'), disabled: !writable || busy,
|
|
369
|
+
onChange: (event) => setDraft({ ...draft, baseUrl: event.target.value }),
|
|
370
|
+
}),
|
|
371
|
+
),
|
|
372
|
+
React.createElement('div', { style: css.field },
|
|
373
|
+
React.createElement(PrefRow, {
|
|
374
|
+
label: t('apiFormat'),
|
|
375
|
+
value: draft.apiFormat,
|
|
376
|
+
disabled: !writable || busy,
|
|
377
|
+
options: API_FORMATS.map((row) => ({ id: row.id, label: row.label })),
|
|
378
|
+
onPick: (id) => setDraft({ ...draft, apiFormat: id }),
|
|
379
|
+
}),
|
|
380
|
+
React.createElement('p', { style: css.meta }, formatHint(draft.apiFormat)),
|
|
381
|
+
),
|
|
382
|
+
React.createElement('div', { style: css.field },
|
|
383
|
+
React.createElement('span', { style: css.label }, `${t('apiKey')} · ${keyState ? t('apiKeySet') : t('apiKeyUnset')}`),
|
|
384
|
+
React.createElement('input', {
|
|
385
|
+
style: css.input, type: 'password', autoComplete: 'off', value: keyDraft,
|
|
386
|
+
placeholder: t('apiKeyPlaceholder'), 'aria-label': t('apiKey'),
|
|
387
|
+
disabled: !writable || busy,
|
|
388
|
+
onChange: (event) => setKeyDraft(event.target.value),
|
|
389
|
+
}),
|
|
390
|
+
),
|
|
391
|
+
React.createElement('div', { style: css.field },
|
|
392
|
+
React.createElement('span', { style: css.label }, t('models')),
|
|
393
|
+
(draft.models ?? []).map((model, index) => React.createElement('div', { key: index, style: css.modelRow },
|
|
394
|
+
React.createElement('input', {
|
|
395
|
+
style: { ...css.input, flex: 1 },
|
|
396
|
+
value: model.id,
|
|
397
|
+
placeholder: t('modelId'),
|
|
398
|
+
'aria-label': t('modelId'),
|
|
399
|
+
disabled: !writable || busy,
|
|
400
|
+
onChange: (event) => {
|
|
401
|
+
const models = draft.models.slice()
|
|
402
|
+
models[index] = { ...models[index], id: event.target.value }
|
|
403
|
+
setDraft({ ...draft, models })
|
|
404
|
+
},
|
|
405
|
+
}),
|
|
406
|
+
React.createElement('input', {
|
|
407
|
+
style: { ...css.input, flex: 1 },
|
|
408
|
+
value: model.name,
|
|
409
|
+
placeholder: t('modelName'),
|
|
410
|
+
'aria-label': t('modelName'),
|
|
411
|
+
disabled: !writable || busy,
|
|
412
|
+
onChange: (event) => {
|
|
413
|
+
const models = draft.models.slice()
|
|
414
|
+
models[index] = { ...models[index], name: event.target.value }
|
|
415
|
+
setDraft({ ...draft, models })
|
|
416
|
+
},
|
|
417
|
+
}),
|
|
418
|
+
React.createElement(IconButton, {
|
|
419
|
+
label: t('delete'), disabled: !writable || busy,
|
|
420
|
+
onClick: () => setDraft({ ...draft, models: draft.models.filter((_, i) => i !== index) }),
|
|
421
|
+
}, IconTrash ? React.createElement(IconTrash, { size: 16 }) : t('delete')),
|
|
422
|
+
)),
|
|
423
|
+
React.createElement('button', {
|
|
424
|
+
type: 'button', style: { ...css.button, alignSelf: 'flex-start', marginTop: 4, gap: 4 },
|
|
425
|
+
disabled: !writable || busy,
|
|
426
|
+
onClick: () => setDraft({ ...draft, models: [...draft.models, newModel()] }),
|
|
427
|
+
},
|
|
428
|
+
IconPlus ? React.createElement(IconPlus, { size: 14 }) : null,
|
|
429
|
+
t('addModel'),
|
|
430
|
+
),
|
|
431
|
+
),
|
|
432
|
+
error ? React.createElement('p', { style: css.error, role: 'alert' }, error) : null,
|
|
433
|
+
saved ? React.createElement('p', { style: css.ok, role: 'status' }, t('saved')) : null,
|
|
434
|
+
React.createElement('div', { style: css.actions },
|
|
435
|
+
React.createElement('button', { type: 'button', style: css.button, disabled: busy, onClick: onCancel }, t('cancel')),
|
|
436
|
+
React.createElement('button', {
|
|
437
|
+
type: 'button', style: css.primary, disabled: !writable || busy, onClick: onSave,
|
|
438
|
+
}, busy ? t('saving') : t('save')),
|
|
439
|
+
),
|
|
440
|
+
)
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function CatalogSection(props) {
|
|
444
|
+
const t = props.t ?? translateFactory(en)
|
|
445
|
+
const scope = props.catalog
|
|
446
|
+
const credentials = props.credentials
|
|
447
|
+
const snap = useSnapshot(scope)
|
|
448
|
+
const [openId, setOpenId] = React.useState(null)
|
|
449
|
+
const [draft, setDraft] = React.useState(null)
|
|
450
|
+
const [keyDraft, setKeyDraft] = React.useState('')
|
|
451
|
+
const [keyMap, setKeyMap] = React.useState({})
|
|
452
|
+
const [busy, setBusy] = React.useState(false)
|
|
453
|
+
const [error, setError] = React.useState('')
|
|
454
|
+
const [saved, setSaved] = React.useState(false)
|
|
455
|
+
const [creating, setCreating] = React.useState(false)
|
|
456
|
+
|
|
457
|
+
const providers = cloneProviders(snap.value)
|
|
458
|
+
const writable = snap.writable !== false && snap.status === 'ready'
|
|
459
|
+
|
|
460
|
+
const refreshKeys = React.useCallback(async (list) => {
|
|
461
|
+
const refs = list.map((row) => row.apiKeyEnv).filter(Boolean)
|
|
462
|
+
if (refs.length === 0 || !credentials) return
|
|
463
|
+
const response = await credentials.describe(refs)
|
|
464
|
+
if (!response?.ok) return
|
|
465
|
+
const next = {}
|
|
466
|
+
for (const ref of refs) next[ref] = response.value?.[ref]?.configured === true
|
|
467
|
+
setKeyMap(next)
|
|
468
|
+
}, [credentials])
|
|
469
|
+
|
|
470
|
+
React.useEffect(() => {
|
|
471
|
+
if (snap.status === 'ready') void refreshKeys(providers)
|
|
472
|
+
}, [snap.status, snap.revision])
|
|
473
|
+
|
|
474
|
+
function startEdit(row) {
|
|
475
|
+
setCreating(false)
|
|
476
|
+
setOpenId(row.id)
|
|
477
|
+
setDraft({ ...row, models: row.models.length ? row.models.map((m) => ({ ...m })) : [newModel()] })
|
|
478
|
+
setKeyDraft('')
|
|
479
|
+
setError('')
|
|
480
|
+
setSaved(false)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function startCreate() {
|
|
484
|
+
setCreating(true)
|
|
485
|
+
setOpenId('__new__')
|
|
486
|
+
setDraft({
|
|
487
|
+
id: '',
|
|
488
|
+
name: '',
|
|
489
|
+
baseUrl: '',
|
|
490
|
+
apiFormat: 'openai-images',
|
|
491
|
+
apiKeyEnv: '',
|
|
492
|
+
models: [newModel()],
|
|
493
|
+
})
|
|
494
|
+
setKeyDraft('')
|
|
495
|
+
setError('')
|
|
496
|
+
setSaved(false)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function validate(next) {
|
|
500
|
+
if (!String(next.name ?? '').trim()) return t('nameRequired')
|
|
501
|
+
if (!String(next.baseUrl ?? '').trim()) return t('baseUrlRequired')
|
|
502
|
+
const models = (next.models ?? []).map((m) => ({ id: String(m.id).trim(), name: String(m.name ?? '').trim() }))
|
|
503
|
+
.filter((m) => m.id || m.name)
|
|
504
|
+
if (models.length === 0) return t('modelRequired')
|
|
505
|
+
const ids = new Set()
|
|
506
|
+
for (const model of models) {
|
|
507
|
+
if (!model.id) return t('modelIdRequired')
|
|
508
|
+
if (ids.has(model.id)) return t('modelIdDuplicate')
|
|
509
|
+
ids.add(model.id)
|
|
510
|
+
}
|
|
511
|
+
return ''
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
async function save() {
|
|
515
|
+
const next = draft
|
|
516
|
+
const message = validate(next)
|
|
517
|
+
if (message) {
|
|
518
|
+
setError(message)
|
|
519
|
+
return
|
|
520
|
+
}
|
|
521
|
+
const models = next.models
|
|
522
|
+
.map((m) => ({ id: String(m.id).trim(), name: String(m.name ?? '').trim() || String(m.id).trim() }))
|
|
523
|
+
.filter((m) => m.id)
|
|
524
|
+
const taken = new Set(providers.map((row) => row.id))
|
|
525
|
+
const id = creating ? uniqueId(next.name, taken) : next.id
|
|
526
|
+
const apiKeyEnv = next.apiKeyEnv || apiKeyEnvFor(id)
|
|
527
|
+
const record = {
|
|
528
|
+
id,
|
|
529
|
+
name: String(next.name).trim(),
|
|
530
|
+
baseUrl: String(next.baseUrl).trim(),
|
|
531
|
+
apiFormat: next.apiFormat || 'openai-images',
|
|
532
|
+
apiKeyEnv,
|
|
533
|
+
models,
|
|
534
|
+
}
|
|
535
|
+
const list = creating
|
|
536
|
+
? [...providers, record]
|
|
537
|
+
: providers.map((row) => row.id === id ? record : row)
|
|
538
|
+
setBusy(true)
|
|
539
|
+
setError('')
|
|
540
|
+
setSaved(false)
|
|
541
|
+
try {
|
|
542
|
+
await scope.set('providers', list)
|
|
543
|
+
const landed = cloneProviders(scope.getSnapshot().value)
|
|
544
|
+
if (!landed.some((row) => row.id === id && row.baseUrl === record.baseUrl)) {
|
|
545
|
+
throw new Error(t('saveFailed'))
|
|
546
|
+
}
|
|
547
|
+
const key = keyDraft.trim()
|
|
548
|
+
if (key && credentials) {
|
|
549
|
+
const result = await credentials.set(apiKeyEnv, key)
|
|
550
|
+
if (result && result.ok === false) throw new Error(result.error?.message || t('saveFailed'))
|
|
551
|
+
setKeyDraft('')
|
|
552
|
+
}
|
|
553
|
+
await refreshKeys(list)
|
|
554
|
+
setCreating(false)
|
|
555
|
+
setOpenId(null)
|
|
556
|
+
setDraft(null)
|
|
557
|
+
setKeyDraft('')
|
|
558
|
+
setSaved(false)
|
|
559
|
+
} catch (err) {
|
|
560
|
+
setError(String(err?.message || t('saveFailed')))
|
|
561
|
+
} finally {
|
|
562
|
+
setBusy(false)
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
async function remove(id) {
|
|
567
|
+
setBusy(true)
|
|
568
|
+
setError('')
|
|
569
|
+
try {
|
|
570
|
+
await scope.set('providers', providers.filter((row) => row.id !== id))
|
|
571
|
+
setOpenId(null)
|
|
572
|
+
setDraft(null)
|
|
573
|
+
setCreating(false)
|
|
574
|
+
} catch (err) {
|
|
575
|
+
setError(String(err?.message || t('saveFailed')))
|
|
576
|
+
} finally {
|
|
577
|
+
setBusy(false)
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (snap.status === 'loading' || snap.status === 'idle') {
|
|
582
|
+
return React.createElement('div', { style: css.section },
|
|
583
|
+
React.createElement('h2', { style: css.title }, t('title')),
|
|
584
|
+
React.createElement('p', { style: css.intro }, t('intro')),
|
|
585
|
+
)
|
|
586
|
+
}
|
|
587
|
+
if (snap.status === 'unavailable') {
|
|
588
|
+
return React.createElement('div', { style: css.section },
|
|
589
|
+
React.createElement('h2', { style: css.title }, t('title')),
|
|
590
|
+
React.createElement('p', { style: css.error }, t('loadFailed')),
|
|
591
|
+
)
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return React.createElement('div', { style: css.section },
|
|
595
|
+
React.createElement('h2', { style: css.title }, t('title')),
|
|
596
|
+
React.createElement('p', { style: css.intro }, t('intro')),
|
|
597
|
+
!writable ? React.createElement('p', { style: css.meta }, t('readOnly')) : null,
|
|
598
|
+
providers.length === 0 && !creating ? React.createElement('p', { style: css.meta }, t('empty')) : null,
|
|
599
|
+
providers.map((row) => React.createElement('div', { key: row.id, style: css.card },
|
|
600
|
+
openId === row.id && !creating && draft
|
|
601
|
+
? React.createElement(ProviderEditor, {
|
|
602
|
+
t, draft, setDraft, keyDraft, setKeyDraft,
|
|
603
|
+
keyState: keyMap[draft.apiKeyEnv] === true,
|
|
604
|
+
busy, error, saved, writable, onSave: save,
|
|
605
|
+
onCancel: () => { setOpenId(null); setDraft(null); setError(''); setSaved(false) },
|
|
606
|
+
onDelete: () => remove(row.id),
|
|
607
|
+
})
|
|
608
|
+
: React.createElement(React.Fragment, null,
|
|
609
|
+
React.createElement('div', { style: css.row },
|
|
610
|
+
React.createElement('div', { style: css.name }, row.name || row.id),
|
|
611
|
+
React.createElement('button', {
|
|
612
|
+
type: 'button', style: css.button, disabled: busy, onClick: () => startEdit(row),
|
|
613
|
+
}, t('edit')),
|
|
614
|
+
React.createElement(IconButton, {
|
|
615
|
+
label: t('delete'), disabled: !writable || busy, onClick: () => remove(row.id),
|
|
616
|
+
}, IconTrash ? React.createElement(IconTrash, { size: 16 }) : t('delete')),
|
|
617
|
+
),
|
|
618
|
+
React.createElement('p', { style: css.meta },
|
|
619
|
+
`${row.baseUrl || '—'} · ${formatLabel(row.apiFormat)} · ${row.models.length} models`
|
|
620
|
+
+ (keyMap[row.apiKeyEnv] ? ` · ${t('apiKeySet')}` : ` · ${t('apiKeyUnset')}`),
|
|
621
|
+
),
|
|
622
|
+
),
|
|
623
|
+
)),
|
|
624
|
+
creating && draft ? React.createElement('div', { style: css.card },
|
|
625
|
+
React.createElement(ProviderEditor, {
|
|
626
|
+
t, draft, setDraft, keyDraft, setKeyDraft,
|
|
627
|
+
keyState: false, busy, error, saved, writable, onSave: save,
|
|
628
|
+
onCancel: () => { setCreating(false); setOpenId(null); setDraft(null); setError('') },
|
|
629
|
+
onDelete: () => { setCreating(false); setOpenId(null); setDraft(null) },
|
|
630
|
+
}),
|
|
631
|
+
) : null,
|
|
632
|
+
React.createElement('button', {
|
|
633
|
+
type: 'button', style: { ...css.button, alignSelf: 'flex-start', gap: 4 },
|
|
634
|
+
disabled: !writable || creating, onClick: startCreate,
|
|
635
|
+
},
|
|
636
|
+
IconPlus ? React.createElement(IconPlus, { size: 14 }) : null,
|
|
637
|
+
t('addProvider'),
|
|
638
|
+
),
|
|
639
|
+
)
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function RuntimeCard(props) {
|
|
643
|
+
const t = props.t ?? translateFactory(en)
|
|
644
|
+
const runtime = props.runtime
|
|
645
|
+
const catalog = props.catalog
|
|
646
|
+
const runtimeSnap = useSnapshot(runtime)
|
|
647
|
+
const catalogSnap = useSnapshot(catalog)
|
|
648
|
+
const [open, setOpen] = React.useState(false)
|
|
649
|
+
const [enabled, setEnabled] = React.useState(true)
|
|
650
|
+
const [choice, setChoice] = React.useState('')
|
|
651
|
+
const [size, setSize] = React.useState('1024x1024')
|
|
652
|
+
const [quality, setQuality] = React.useState('auto')
|
|
653
|
+
const [busy, setBusy] = React.useState(false)
|
|
654
|
+
const [failed, setFailed] = React.useState(false)
|
|
655
|
+
const [dirty, setDirty] = React.useState(false)
|
|
656
|
+
|
|
657
|
+
const providers = cloneProviders(catalogSnap.value)
|
|
658
|
+
const options = []
|
|
659
|
+
for (const provider of providers) {
|
|
660
|
+
for (const model of provider.models) {
|
|
661
|
+
options.push({
|
|
662
|
+
value: `${provider.id}::${model.id}`,
|
|
663
|
+
label: `${provider.name} / ${model.name || model.id}`,
|
|
664
|
+
})
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
React.useEffect(() => {
|
|
669
|
+
if (dirty) return
|
|
670
|
+
const value = runtimeSnap.value ?? {}
|
|
671
|
+
setEnabled(value.enabled !== false)
|
|
672
|
+
setChoice(value.providerId && value.modelId ? `${value.providerId}::${value.modelId}` : '')
|
|
673
|
+
setSize(value.defaultSize || '1024x1024')
|
|
674
|
+
setQuality(value.defaultQuality || 'auto')
|
|
675
|
+
}, [runtimeSnap.revision, dirty])
|
|
676
|
+
|
|
677
|
+
if (runtimeSnap.status !== 'ready') return null
|
|
678
|
+
const writable = runtimeSnap.writable !== false
|
|
679
|
+
|
|
680
|
+
async function save() {
|
|
681
|
+
const [providerId, modelId] = choice.includes('::') ? choice.split('::') : ['', '']
|
|
682
|
+
setBusy(true)
|
|
683
|
+
setFailed(false)
|
|
684
|
+
try {
|
|
685
|
+
await runtime.mutate([
|
|
686
|
+
{ op: 'set', path: ['enabled'], value: enabled },
|
|
687
|
+
{ op: 'set', path: ['providerId'], value: providerId || '' },
|
|
688
|
+
{ op: 'set', path: ['modelId'], value: modelId || '' },
|
|
689
|
+
{ op: 'set', path: ['defaultSize'], value: size },
|
|
690
|
+
{ op: 'set', path: ['defaultQuality'], value: quality },
|
|
691
|
+
])
|
|
692
|
+
const landed = runtime.getSnapshot().value
|
|
693
|
+
if (landed?.enabled !== enabled || (landed?.providerId || '') !== (providerId || '') || (landed?.modelId || '') !== (modelId || '')) {
|
|
694
|
+
setFailed(true)
|
|
695
|
+
return
|
|
696
|
+
}
|
|
697
|
+
setDirty(false)
|
|
698
|
+
setOpen(false)
|
|
699
|
+
} catch {
|
|
700
|
+
setFailed(true)
|
|
701
|
+
} finally {
|
|
702
|
+
setBusy(false)
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function discard() {
|
|
707
|
+
setDirty(false)
|
|
708
|
+
setFailed(false)
|
|
709
|
+
const value = runtimeSnap.value ?? {}
|
|
710
|
+
setEnabled(value.enabled !== false)
|
|
711
|
+
setChoice(value.providerId && value.modelId ? `${value.providerId}::${value.modelId}` : '')
|
|
712
|
+
setSize(value.defaultSize || '1024x1024')
|
|
713
|
+
setQuality(value.defaultQuality || 'auto')
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
return React.createElement('li', { style: css.pluginCard },
|
|
717
|
+
React.createElement('button', {
|
|
718
|
+
type: 'button', style: css.pluginHeader, 'aria-expanded': open,
|
|
719
|
+
'aria-label': `${t(open ? 'collapse' : 'expand')}: ${t('cardTitle')}`,
|
|
720
|
+
onClick: () => setOpen(!open),
|
|
721
|
+
},
|
|
722
|
+
React.createElement('span', { style: { display: 'flex', flexDirection: 'column', gap: 4, flex: 1, minWidth: 0 } },
|
|
723
|
+
React.createElement('span', { style: css.pluginName }, t('cardTitle')),
|
|
724
|
+
React.createElement('span', { style: css.pluginDesc }, t('cardDescription')),
|
|
725
|
+
),
|
|
726
|
+
dirty ? React.createElement('span', { style: { ...css.meta, padding: '1px 8px', borderRadius: 999, background: 'var(--dsw-alias-bg-module-platform)' } }, t('unsaved')) : null,
|
|
727
|
+
React.createElement('span', { style: { color: 'var(--dsw-alias-label-tertiary)', display: 'inline-flex' } },
|
|
728
|
+
open
|
|
729
|
+
? (IconChevronUp ? React.createElement(IconChevronUp, { size: 14 }) : t('collapse'))
|
|
730
|
+
: (IconChevronDown ? React.createElement(IconChevronDown, { size: 14 }) : t('expand')),
|
|
731
|
+
),
|
|
732
|
+
),
|
|
733
|
+
open ? React.createElement('div', { style: css.pluginBody },
|
|
734
|
+
React.createElement('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '12px 0', gap: 16 } },
|
|
735
|
+
React.createElement('span', { style: { fontSize: 13 } }, t('enabled')),
|
|
736
|
+
React.createElement('button', {
|
|
737
|
+
type: 'button', role: 'switch', 'aria-checked': enabled,
|
|
738
|
+
style: { ...css.switch, background: enabled ? 'var(--dsw-alias-brand-primary)' : 'var(--dsw-alias-border-l3)' },
|
|
739
|
+
disabled: !writable || busy,
|
|
740
|
+
onClick: () => { setEnabled(!enabled); setDirty(true) },
|
|
741
|
+
}, React.createElement('span', {
|
|
742
|
+
style: {
|
|
743
|
+
display: 'block', width: 16, height: 16, borderRadius: '50%',
|
|
744
|
+
background: 'var(--dsw-alias-label-primary-foreground, #fff)',
|
|
745
|
+
transform: enabled ? 'translateX(16px)' : 'none',
|
|
746
|
+
},
|
|
747
|
+
})),
|
|
748
|
+
),
|
|
749
|
+
React.createElement('p', { style: css.meta }, t('enabledHint')),
|
|
750
|
+
options.length === 0
|
|
751
|
+
? React.createElement('p', { style: { ...css.meta, padding: '8px 0' } }, t('noModels'))
|
|
752
|
+
: React.createElement(PrefRow, {
|
|
753
|
+
label: t('pickModel'),
|
|
754
|
+
value: choice,
|
|
755
|
+
disabled: !writable || busy,
|
|
756
|
+
placeholder: '—',
|
|
757
|
+
options: [{ id: '', label: '—' }, ...options.map((row) => ({ id: row.value, label: row.label }))],
|
|
758
|
+
onPick: (id) => { setChoice(id); setDirty(true) },
|
|
759
|
+
}),
|
|
760
|
+
options.length === 0 ? null : React.createElement('p', { style: css.meta }, t('pickHint')),
|
|
761
|
+
React.createElement(PrefRow, {
|
|
762
|
+
label: t('defaultSize'),
|
|
763
|
+
value: size,
|
|
764
|
+
disabled: !writable || busy,
|
|
765
|
+
options: ['1024x1024', '1024x1536', '1536x1024', 'auto'].map((value) => ({ id: value, label: value })),
|
|
766
|
+
onPick: (id) => { setSize(id); setDirty(true) },
|
|
767
|
+
}),
|
|
768
|
+
React.createElement(PrefRow, {
|
|
769
|
+
label: t('defaultQuality'),
|
|
770
|
+
value: quality,
|
|
771
|
+
disabled: !writable || busy,
|
|
772
|
+
options: ['low', 'medium', 'high', 'auto'].map((value) => ({ id: value, label: value })),
|
|
773
|
+
onPick: (id) => { setQuality(id); setDirty(true) },
|
|
774
|
+
}),
|
|
775
|
+
failed ? React.createElement('p', { style: css.error }, t('saveFailed')) : null,
|
|
776
|
+
React.createElement('div', { style: { ...css.actions, padding: '12px 0 4px' } },
|
|
777
|
+
React.createElement('button', { type: 'button', style: css.button, disabled: !dirty || busy, onClick: discard }, t('discard')),
|
|
778
|
+
React.createElement('button', {
|
|
779
|
+
type: 'button', style: css.primary, disabled: !dirty || busy, onClick: save,
|
|
780
|
+
}, busy ? t('saving') : t('save')),
|
|
781
|
+
),
|
|
782
|
+
) : null,
|
|
783
|
+
)
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function ImageLightbox({ src, alt, labels, onClose }) {
|
|
787
|
+
React.useEffect(() => {
|
|
788
|
+
const onKey = (event) => { if (event.key === 'Escape') onClose() }
|
|
789
|
+
window.addEventListener('keydown', onKey)
|
|
790
|
+
return () => window.removeEventListener('keydown', onKey)
|
|
791
|
+
}, [onClose])
|
|
792
|
+
return React.createElement('div', {
|
|
793
|
+
role: 'dialog', 'aria-label': labels.dialog,
|
|
794
|
+
style: {
|
|
795
|
+
position: 'fixed', inset: 0, zIndex: 10000, display: 'grid', placeItems: 'center',
|
|
796
|
+
background: 'rgba(0,0,0,.72)', padding: 24,
|
|
797
|
+
},
|
|
798
|
+
onClick: onClose,
|
|
799
|
+
},
|
|
800
|
+
React.createElement('img', {
|
|
801
|
+
src, alt, style: { maxWidth: '92vw', maxHeight: '92vh', objectFit: 'contain', borderRadius: 4 },
|
|
802
|
+
onClick: (event) => event.stopPropagation(),
|
|
803
|
+
}),
|
|
804
|
+
React.createElement('button', {
|
|
805
|
+
type: 'button', 'aria-label': labels.close,
|
|
806
|
+
style: {
|
|
807
|
+
position: 'absolute', top: 12, right: 12, width: 32, height: 32, border: 'none',
|
|
808
|
+
borderRadius: '50%', cursor: 'pointer', background: 'rgba(255,255,255,.16)', color: '#fff',
|
|
809
|
+
display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
|
|
810
|
+
},
|
|
811
|
+
onClick: onClose,
|
|
812
|
+
}, IconClose ? React.createElement(IconClose, { size: 16 }) : labels.close),
|
|
813
|
+
)
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
function MessageImage({ image, load, cwd, labels }) {
|
|
817
|
+
const [src, setSrc] = React.useState(null)
|
|
818
|
+
const [error, setError] = React.useState(false)
|
|
819
|
+
const [open, setOpen] = React.useState(false)
|
|
820
|
+
const [tick, setTick] = React.useState(0)
|
|
821
|
+
const key = image?.attachment?.attachmentId ?? image?.path ?? ''
|
|
822
|
+
React.useEffect(() => {
|
|
823
|
+
let cancelled = false
|
|
824
|
+
setError(false)
|
|
825
|
+
setSrc(null)
|
|
826
|
+
Promise.resolve(load(image, cwd)).then((url) => {
|
|
827
|
+
if (!cancelled) setSrc(url)
|
|
828
|
+
}, () => {
|
|
829
|
+
if (!cancelled) setError(true)
|
|
830
|
+
})
|
|
831
|
+
return () => { cancelled = true }
|
|
832
|
+
}, [key, load, cwd, tick])
|
|
833
|
+
if (error) {
|
|
834
|
+
return React.createElement('button', {
|
|
835
|
+
type: 'button', style: { fontSize: 12, color: 'var(--dsw-alias-state-error-primary)' },
|
|
836
|
+
onClick: () => { setError(false); setTick((n) => n + 1) },
|
|
837
|
+
}, labels.loadFailed)
|
|
838
|
+
}
|
|
839
|
+
if (!src) return React.createElement('span', { style: { fontSize: 12, color: 'var(--dsw-alias-label-tertiary)' } }, labels.loading)
|
|
840
|
+
return React.createElement(React.Fragment, null,
|
|
841
|
+
React.createElement('button', {
|
|
842
|
+
type: 'button',
|
|
843
|
+
style: {
|
|
844
|
+
display: 'grid', placeItems: 'center', overflow: 'hidden', padding: 0, borderRadius: 8,
|
|
845
|
+
border: '1px solid var(--dsw-alias-border-l2)', cursor: 'zoom-in', width: 240, maxWidth: '100%',
|
|
846
|
+
},
|
|
847
|
+
onClick: () => setOpen(true), 'aria-label': labels.open,
|
|
848
|
+
}, React.createElement('img', { src, alt: labels.image, style: { width: '100%', display: 'block' } })),
|
|
849
|
+
open ? React.createElement(ImageLightbox, { src, alt: labels.image, labels, onClose: () => setOpen(false) }) : null,
|
|
850
|
+
)
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
function derivePrompt(argsRaw) {
|
|
854
|
+
let parsed
|
|
855
|
+
try { parsed = JSON.parse(argsRaw) } catch { parsed = undefined }
|
|
856
|
+
let prompt
|
|
857
|
+
if (typeof parsed === 'object' && parsed !== null && typeof parsed.prompt === 'string') prompt = parsed.prompt
|
|
858
|
+
const line = String(prompt ?? argsRaw).split('\n', 1)[0] ?? ''
|
|
859
|
+
return line.length > 60 ? `${line.slice(0, 59)}…` : line
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function resultImages(block) {
|
|
863
|
+
if (!block || !('kind' in block)) return []
|
|
864
|
+
const images = []
|
|
865
|
+
for (const part of block.content ?? []) {
|
|
866
|
+
if (part.type === 'image' && part.attachment) images.push({ attachment: part.attachment })
|
|
867
|
+
}
|
|
868
|
+
if (images.length > 0) return images
|
|
869
|
+
const text = resultText(block)
|
|
870
|
+
for (const line of text.split('\n')) {
|
|
871
|
+
const match = line.match(/^\s*-\s+(\S+\.(?:png|jpe?g|webp|gif))\s*$/i)
|
|
872
|
+
if (match) images.push({ path: match[1] })
|
|
873
|
+
}
|
|
874
|
+
return images
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function resultText(block) {
|
|
878
|
+
if (!block || !('kind' in block)) return ''
|
|
879
|
+
const parts = []
|
|
880
|
+
for (const part of block.content ?? []) if (part.type === 'text') parts.push(part.text)
|
|
881
|
+
if (parts.length === 0 && block.error) parts.push(`${block.error.name}: ${block.error.code}`)
|
|
882
|
+
return parts.join('\n')
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
function ImageGenerateToolview(props) {
|
|
886
|
+
const { block } = props
|
|
887
|
+
const t = props.t ?? translateFactory(en)
|
|
888
|
+
if (block === undefined) return null
|
|
889
|
+
// DSH >= 0.1.7 hands explicit phases (preparing/start/result) and an
|
|
890
|
+
// owner-provided session-authorized image loader; older runtimes
|
|
891
|
+
// discriminate the node by its `kind` field and use the injected loader.
|
|
892
|
+
const phase = typeof props.phase === 'string' ? props.phase : ('kind' in block ? 'result' : 'start')
|
|
893
|
+
const settled = phase === 'result'
|
|
894
|
+
const argsRaw = (settled ? block.call?.argsRaw : block.argsRaw) ?? ''
|
|
895
|
+
const title = `image_generate: ${derivePrompt(argsRaw)}`
|
|
896
|
+
const images = resultImages(block)
|
|
897
|
+
const text = settled ? resultText(block) : ''
|
|
898
|
+
const load = (image, cwd) => {
|
|
899
|
+
if (image?.attachment && typeof props.loadImage === 'function') {
|
|
900
|
+
return Promise.resolve(props.loadImage(image.attachment))
|
|
901
|
+
}
|
|
902
|
+
if (typeof props.load === 'function') return props.load(image, cwd)
|
|
903
|
+
return Promise.reject(new Error('no image loader'))
|
|
904
|
+
}
|
|
905
|
+
const Sparkle = IconSparkle
|
|
906
|
+
const labels = {
|
|
907
|
+
image: t('image'),
|
|
908
|
+
open: t('viewImage'),
|
|
909
|
+
loading: t('imageLoading'),
|
|
910
|
+
loadFailed: t('imageLoadFailed'),
|
|
911
|
+
dialog: t('imagePreview'),
|
|
912
|
+
close: t('imageClose'),
|
|
913
|
+
}
|
|
914
|
+
return React.createElement('div', { style: { display: 'flex', flexDirection: 'column', gap: 6, padding: '4px 0' } },
|
|
915
|
+
React.createElement('div', { style: { display: 'flex', alignItems: 'center', gap: 6 } },
|
|
916
|
+
Sparkle ? React.createElement(Sparkle, { size: 14 }) : null,
|
|
917
|
+
React.createElement('span', {
|
|
918
|
+
style: { fontSize: 13, lineHeight: '20px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
919
|
+
}, title),
|
|
920
|
+
),
|
|
921
|
+
!settled ? React.createElement('p', { style: css.meta }, t('generating')) : null,
|
|
922
|
+
settled && block.isError && text ? React.createElement('p', { style: css.error }, text.split('\n', 1)[0]) : null,
|
|
923
|
+
settled && !block.isError && images.length > 0 && load
|
|
924
|
+
? React.createElement('div', { style: { display: 'flex', flexWrap: 'wrap', gap: 8 } },
|
|
925
|
+
images.map((image, index) => React.createElement(MessageImage, {
|
|
926
|
+
key: image.attachment?.attachmentId ?? image.path ?? index,
|
|
927
|
+
image,
|
|
928
|
+
load,
|
|
929
|
+
cwd: props.cwd,
|
|
930
|
+
labels,
|
|
931
|
+
})))
|
|
932
|
+
: null,
|
|
933
|
+
settled && !block.isError && images.length === 0 && text
|
|
934
|
+
? React.createElement('p', { style: { ...css.meta, whiteSpace: 'pre-wrap' } }, text)
|
|
935
|
+
: null,
|
|
936
|
+
)
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
async function callImageGen(rpc, endpoint, payload) {
|
|
940
|
+
const result = await rpc.call(RPC_CHANNEL, endpoint, payload)
|
|
941
|
+
if (!result.ok) throw new Error(result.error.message)
|
|
942
|
+
return result.value
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
function createImageLoader(rpc) {
|
|
946
|
+
return (image, cwd) => {
|
|
947
|
+
const request = image?.attachment
|
|
948
|
+
? callImageGen(rpc, 'image', { ...image.attachment })
|
|
949
|
+
: callImageGen(rpc, 'file', { path: image?.path, cwd: cwd || '' })
|
|
950
|
+
return request.then((result) => `data:${result.mediaType};base64,${result.dataBase64}`)
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* Adapt a DSH >= 0.1.6 `configForms` entry form to the settingsScope shape
|
|
956
|
+
* the cards were written against (set(key, value) + mutate(ops)).
|
|
957
|
+
* `pick` projects the merged entry config into one card's section view.
|
|
958
|
+
*/
|
|
959
|
+
function adaptConfigForm(form, pick) {
|
|
960
|
+
return {
|
|
961
|
+
getSnapshot() {
|
|
962
|
+
const snap = form.getSnapshot()
|
|
963
|
+
return {
|
|
964
|
+
status: snap.status,
|
|
965
|
+
value: snap.value === undefined ? undefined : pick(snap.value),
|
|
966
|
+
writable: snap.writable,
|
|
967
|
+
revision: snap.revision,
|
|
968
|
+
}
|
|
969
|
+
},
|
|
970
|
+
subscribe: (callback) => form.subscribe(callback),
|
|
971
|
+
set(key, value) {
|
|
972
|
+
return form.mutate([{ op: 'set', path: [key], value }])
|
|
973
|
+
},
|
|
974
|
+
mutate(ops, expectedRevision) {
|
|
975
|
+
return form.mutate(ops, expectedRevision)
|
|
976
|
+
},
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/** Inert scope for runtimes with no settings surface at all. */
|
|
981
|
+
function unavailableScope() {
|
|
982
|
+
return {
|
|
983
|
+
getSnapshot: () => ({ status: 'unavailable', value: undefined, writable: false, revision: undefined }),
|
|
984
|
+
subscribe: () => () => {},
|
|
985
|
+
set: () => Promise.resolve(false),
|
|
986
|
+
mutate: () => Promise.resolve(false),
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Bind the catalog/runtime scopes on whichever settings surface this
|
|
992
|
+
* runtime has: DSH <= 0.1.5 serves two `settingsScope` namespaces;
|
|
993
|
+
* DSH >= 0.1.6 serves one merged `configForms` entry (`image-gen`) whose
|
|
994
|
+
* volatile fields carry both.
|
|
995
|
+
*/
|
|
996
|
+
function bindScopes(ctx) {
|
|
997
|
+
const settingsScope = ctx.get('settingsScope')
|
|
998
|
+
if (settingsScope && typeof settingsScope.bind === 'function') {
|
|
999
|
+
return {
|
|
1000
|
+
legacy: true,
|
|
1001
|
+
catalog: settingsScope.bind({ namespace: CATALOG_NS }),
|
|
1002
|
+
runtime: settingsScope.bind({ namespace: RUNTIME_NS }),
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
const configForms = ctx.get('configForms')
|
|
1006
|
+
if (configForms && typeof configForms.get === 'function') {
|
|
1007
|
+
const form = configForms.get(CATALOG_NS)
|
|
1008
|
+
return {
|
|
1009
|
+
legacy: false,
|
|
1010
|
+
configForms,
|
|
1011
|
+
catalog: adaptConfigForm(form, (value) => ({
|
|
1012
|
+
providers: Array.isArray(value?.providers) ? value.providers : [],
|
|
1013
|
+
})),
|
|
1014
|
+
runtime: adaptConfigForm(form, (value) => ({
|
|
1015
|
+
enabled: value?.enabled !== false,
|
|
1016
|
+
providerId: value?.providerId ?? '',
|
|
1017
|
+
modelId: value?.modelId ?? '',
|
|
1018
|
+
defaultSize: value?.defaultSize ?? '1024x1024',
|
|
1019
|
+
defaultQuality: value?.defaultQuality ?? 'auto',
|
|
1020
|
+
})),
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
return { legacy: true, catalog: unavailableScope(), runtime: unavailableScope() }
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/** plugins.bundle.config page (DSH >= 0.1.6): the runtime picker card. */
|
|
1027
|
+
function RuntimeBundlePage(props) {
|
|
1028
|
+
if (props.view === 'summary') return null
|
|
1029
|
+
return React.createElement('ul', { style: { listStyle: 'none', margin: 0, padding: 0 } },
|
|
1030
|
+
React.createElement(RuntimeCard, props))
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// 'settingsScope' was removed in DSH 0.1.7 (replaced by 'configForms') and
|
|
1034
|
+
// must not be a hard inject, or this client half never activates there.
|
|
1035
|
+
const inject = ['slots', 'locale', 'remote', 'remote.credentials', 'connection']
|
|
1036
|
+
|
|
1037
|
+
function apply(ctx) {
|
|
1038
|
+
ensureSelectCss()
|
|
1039
|
+
ctx.effect(() => ctx.locale.register(LOCALE_NS, { zh, en }), 'dsh-image-generation: copy dictionaries')
|
|
1040
|
+
const t = ctx.locale.bind(LOCALE_NS)
|
|
1041
|
+
const { legacy, configForms, catalog, runtime } = bindScopes(ctx)
|
|
1042
|
+
const connection = ctx.get('connection')
|
|
1043
|
+
|
|
1044
|
+
ctx.slots.inject('settings.section', () => ctx.slots.register({
|
|
1045
|
+
name: 'settings.section',
|
|
1046
|
+
id: 'image-gen',
|
|
1047
|
+
order: 85,
|
|
1048
|
+
label: () => t('nav'),
|
|
1049
|
+
locale: LOCALE_NS,
|
|
1050
|
+
inject: () => ({
|
|
1051
|
+
catalog,
|
|
1052
|
+
credentials: ctx.remote.credentials,
|
|
1053
|
+
}),
|
|
1054
|
+
}, CatalogSection))
|
|
1055
|
+
|
|
1056
|
+
if (legacy) {
|
|
1057
|
+
// DSH <= 0.1.5: the model picker is a card on the Settings plugins page.
|
|
1058
|
+
ctx.slots.inject('settings.plugin.item', () => ctx.slots.register({
|
|
1059
|
+
name: 'settings.plugin.item',
|
|
1060
|
+
key: RUNTIME_NS,
|
|
1061
|
+
priority: 1000,
|
|
1062
|
+
locale: LOCALE_NS,
|
|
1063
|
+
inject: () => ({ catalog, runtime }),
|
|
1064
|
+
}, RuntimeCard))
|
|
1065
|
+
} else if (configForms) {
|
|
1066
|
+
// DSH >= 0.1.6: the picker lives on this bundle's Plugins page instead.
|
|
1067
|
+
ctx.effect(() => configForms.whileServed([CATALOG_NS], () =>
|
|
1068
|
+
ctx.slots.inject('plugins.bundle.config', () => ctx.slots.register({
|
|
1069
|
+
name: 'plugins.bundle.config',
|
|
1070
|
+
key: 'dsh-image-generation',
|
|
1071
|
+
locale: LOCALE_NS,
|
|
1072
|
+
inject: () => ({ catalog, runtime }),
|
|
1073
|
+
}, RuntimeBundlePage))), 'dsh-image-generation: plugins page')
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
if (connection?.rpc) {
|
|
1077
|
+
ctx.slots.inject('tool.call.toolview', () => ctx.slots.register({
|
|
1078
|
+
name: 'tool.call.toolview',
|
|
1079
|
+
key: 'image_generate',
|
|
1080
|
+
locale: LOCALE_NS,
|
|
1081
|
+
inject: () => ({ load: createImageLoader(connection.rpc) }),
|
|
1082
|
+
}, ImageGenerateToolview))
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
exports.apply = apply
|
|
1087
|
+
exports.inject = inject
|
|
1088
|
+
return module.exports
|
|
1089
|
+
},
|
|
1090
|
+
})
|