dsh-image-lite 0.1.0 → 0.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/README.md +4 -3
- package/client.js +155 -37
- package/index.js +33 -67
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -100,7 +100,7 @@ DSH 生图插件支持**双模式生图**:
|
|
|
100
100
|
dsh plugin --profile desktop add dsh-image-lite
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
版本、文件清单与发布记录见 <https://www.npmjs.com/package/dsh-image-lite>。由仓库内 `.github/workflows/release.yml`(GitHub OIDC)发布的版本会额外附带 `provenance` 溯源证明;`0.1.0` 是本地 `npm publish` 发布的,暂无该证明。
|
|
104
104
|
|
|
105
105
|
### 方式二:从本地源码安装(开发 / 改代码时)
|
|
106
106
|
|
|
@@ -250,8 +250,9 @@ Requires DSH `>= 0.2.1-rc.2` (Node.js `^22.19.0 || >= 24.0.0`, supplied by the h
|
|
|
250
250
|
dsh plugin --profile desktop add dsh-image-lite
|
|
251
251
|
```
|
|
252
252
|
|
|
253
|
-
|
|
254
|
-
|
|
253
|
+
Versions, file list and publish records are visible at <https://www.npmjs.com/package/dsh-image-lite>.
|
|
254
|
+
Releases produced by `.github/workflows/release.yml` (GitHub OIDC) additionally carry
|
|
255
|
+
`provenance` attestations; `0.1.0` was published locally with `npm publish` and has none.
|
|
255
256
|
|
|
256
257
|
**Option 2 — from a local checkout (for development):**
|
|
257
258
|
|
package/client.js
CHANGED
|
@@ -83,6 +83,8 @@ window.__ModuleLoader__.load({
|
|
|
83
83
|
refs: 'Reference images',
|
|
84
84
|
refsEmpty: 'No references yet. Upload, paste, or click "keep editing" on a result below.',
|
|
85
85
|
refsFull: 'Up to 5 reference images.',
|
|
86
|
+
refsPartial: 'Only part of the selection was added — up to 5 references fit.',
|
|
87
|
+
cancelled: 'Generation cancelled.',
|
|
86
88
|
upload: 'Upload',
|
|
87
89
|
clear: 'Clear',
|
|
88
90
|
clearHint: 'Clear references, prompt and results to start a new picture.',
|
|
@@ -128,6 +130,7 @@ window.__ModuleLoader__.load({
|
|
|
128
130
|
keyPlaceholder: 'sk-… paste the API key',
|
|
129
131
|
keyHint: 'Stored in the DSH credentials service — the config keeps only a masked preview.',
|
|
130
132
|
keyStoredHint: 'Already stored: {hint}. Leave empty to keep it.',
|
|
133
|
+
keySaved: 'Saved: {hint}',
|
|
131
134
|
keyEnvHint: 'Read from the environment (or an existing credential) under this name.',
|
|
132
135
|
keyReplaceHint: 'The old value here is not a credential name — paste the API key to replace it.',
|
|
133
136
|
keyWriteFailed: 'Could not store the API key: {reason}',
|
|
@@ -172,6 +175,8 @@ window.__ModuleLoader__.load({
|
|
|
172
175
|
refs: '参考图',
|
|
173
176
|
refsEmpty: '暂无参考图。可上传、粘贴,或在下方结果里点「用这张继续」。',
|
|
174
177
|
refsFull: '最多 5 张参考图。',
|
|
178
|
+
refsPartial: '只加入了其中一部分:参考图最多 5 张。',
|
|
179
|
+
cancelled: '已取消生成。',
|
|
175
180
|
upload: '上传',
|
|
176
181
|
clear: '清空',
|
|
177
182
|
clearHint: '清空参考图、描述和结果,开始新的生成。',
|
|
@@ -216,6 +221,7 @@ window.__ModuleLoader__.load({
|
|
|
216
221
|
keyPlaceholder: 'sk-… 粘贴 API 密钥',
|
|
217
222
|
keyHint: '保存到 DSH 凭据服务,配置里只留加密预览。',
|
|
218
223
|
keyStoredHint: '已保存:{hint}。留空则保持不变。',
|
|
224
|
+
keySaved: '已保存:{hint}',
|
|
219
225
|
keyEnvHint: '从这个名字的环境变量(或已有凭据)读取。',
|
|
220
226
|
keyReplaceHint: '原来那串不是凭据名,粘贴 API 密钥即可替换。',
|
|
221
227
|
keyWriteFailed: '密钥保存失败:{reason}',
|
|
@@ -498,8 +504,11 @@ window.__ModuleLoader__.load({
|
|
|
498
504
|
|
|
499
505
|
// ---------------------------------------------------------------- RPC
|
|
500
506
|
|
|
501
|
-
async function rpcCall(rpc, endpoint, payload) {
|
|
502
|
-
|
|
507
|
+
async function rpcCall(rpc, endpoint, payload, signal) {
|
|
508
|
+
// `signal` rides along to Connection's `rpc.call(channel, endpoint,
|
|
509
|
+
// payload, signal)`, which aborts the request and, through the host
|
|
510
|
+
// bridge, cancels the generation instead of letting it burn tokens.
|
|
511
|
+
const result = await rpc.call(RPC_CHANNEL, endpoint, payload, signal)
|
|
503
512
|
if (!result.ok) throw new Error(result.error?.message ?? String(result.error))
|
|
504
513
|
return result.value
|
|
505
514
|
}
|
|
@@ -648,7 +657,10 @@ window.__ModuleLoader__.load({
|
|
|
648
657
|
.then((url) => { if (!cancelled) setSrc(url) })
|
|
649
658
|
.catch(() => { if (!cancelled) setFailed(true) })
|
|
650
659
|
return () => { cancelled = true }
|
|
651
|
-
|
|
660
|
+
// `load` is rebuilt whenever cwd/sessionId arrive, and the `file`
|
|
661
|
+
// endpoint resolves relative paths against that cwd: without it here a
|
|
662
|
+
// tile that loaded before cwd was known never retries.
|
|
663
|
+
}, [key, tick, load])
|
|
652
664
|
|
|
653
665
|
if (failed) {
|
|
654
666
|
return React.createElement('button', {
|
|
@@ -903,11 +915,20 @@ window.__ModuleLoader__.load({
|
|
|
903
915
|
...buildActiveFromOption({ ...option, baseUrl: provider?.baseUrl, apiKeyEnv: provider?.apiKeyEnv }),
|
|
904
916
|
}
|
|
905
917
|
: emptyActiveValue()
|
|
906
|
-
|
|
918
|
+
// `mutate` resolves false when the form rejected the write (stale
|
|
919
|
+
// revision, read-only); swallowing that leaves the picker bouncing back
|
|
920
|
+
// with no explanation.
|
|
921
|
+
setProviderError('')
|
|
922
|
+
void liteForm.mutate([{ op: 'set', path: ['active'], value: nextActive }])
|
|
923
|
+
.then((result) => { if (result === false) setProviderError(t('saveFailed')) })
|
|
924
|
+
.catch((error) => setProviderError(`${t('saveFailed')} ${error?.message ?? String(error)}`))
|
|
907
925
|
}
|
|
908
926
|
|
|
909
927
|
function pickAspect(value) {
|
|
910
|
-
|
|
928
|
+
setProviderError('')
|
|
929
|
+
void liteForm.mutate([{ op: 'set', path: ['activeAspect'], value }])
|
|
930
|
+
.then((result) => { if (result === false) setProviderError(t('saveFailed')) })
|
|
931
|
+
.catch((error) => setProviderError(`${t('saveFailed')} ${error?.message ?? String(error)}`))
|
|
911
932
|
}
|
|
912
933
|
|
|
913
934
|
function saveSidebarEnabled(next) {
|
|
@@ -1072,7 +1093,12 @@ window.__ModuleLoader__.load({
|
|
|
1072
1093
|
async function pickFolderPath(ctx) {
|
|
1073
1094
|
const bridge = globalThis.__DSH_DIRECTORY_PICKER__
|
|
1074
1095
|
if (bridge !== undefined && typeof bridge.pick === 'function') return await bridge.pick()
|
|
1075
|
-
|
|
1096
|
+
// Read the service through ctx.get, never as `ctx.uiWorkspace`: on cordis
|
|
1097
|
+
// 4.x a service that is not in `inject` throws "cannot get property ...
|
|
1098
|
+
// without inject" on access, which would skip this fallback entirely
|
|
1099
|
+
// instead of reaching it (same trap as the host-side webServer mount).
|
|
1100
|
+
let ui
|
|
1101
|
+
try { ui = typeof ctx?.get === 'function' ? ctx.get('uiWorkspace') : undefined } catch { ui = undefined }
|
|
1076
1102
|
if (ui !== undefined && typeof ui.pickDirectory === 'function') return await ui.pickDirectory()
|
|
1077
1103
|
throw new Error('image_generate_lite: this runtime exposes no directory picker')
|
|
1078
1104
|
}
|
|
@@ -1343,6 +1369,12 @@ window.__ModuleLoader__.load({
|
|
|
1343
1369
|
|
|
1344
1370
|
// ---------------------------------------------------------------- studio panel
|
|
1345
1371
|
|
|
1372
|
+
/** True when a drag carries real files (not just text or a link). */
|
|
1373
|
+
function carriesFiles(event) {
|
|
1374
|
+
const types = event?.dataTransfer?.types
|
|
1375
|
+
return types !== undefined && types !== null && Array.from(types).includes('Files')
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1346
1378
|
function StudioBody(props) {
|
|
1347
1379
|
const t = props.t ?? translateFactory(en)
|
|
1348
1380
|
const { ctx, rpc, sessionId, liteForm, useSessions } = props
|
|
@@ -1351,6 +1383,17 @@ window.__ModuleLoader__.load({
|
|
|
1351
1383
|
const [error, setError] = React.useState('')
|
|
1352
1384
|
const [prompt, setPrompt] = React.useState(store.prompt)
|
|
1353
1385
|
const [refsFocus, setRefsFocus] = React.useState(false)
|
|
1386
|
+
const [dragging, setDragging] = React.useState(false)
|
|
1387
|
+
// The refs the uploader reads at *execution* time (a render-time count
|
|
1388
|
+
// goes stale between two quick pastes), the queue that keeps uploads
|
|
1389
|
+
// serialized, and the controller of the run in flight for cancellation.
|
|
1390
|
+
const refsRef = React.useRef(store.refs)
|
|
1391
|
+
const uploadChain = React.useRef(Promise.resolve())
|
|
1392
|
+
const abortRef = React.useRef(null)
|
|
1393
|
+
React.useEffect(() => { refsRef.current = store.refs }, [store.refs])
|
|
1394
|
+
// Leaving the panel aborts the run: it used to keep generating (and
|
|
1395
|
+
// billing) after the tab was closed.
|
|
1396
|
+
React.useEffect(() => () => { abortRef.current?.abort() }, [])
|
|
1354
1397
|
|
|
1355
1398
|
const liteSnap = useSnapshot(liteForm)
|
|
1356
1399
|
|
|
@@ -1379,42 +1422,66 @@ window.__ModuleLoader__.load({
|
|
|
1379
1422
|
if (!option) return
|
|
1380
1423
|
const provider = providers.find((p) => p.id === option.providerId)
|
|
1381
1424
|
try {
|
|
1382
|
-
await liteForm.mutate([{
|
|
1425
|
+
const applied = await liteForm.mutate([{
|
|
1383
1426
|
op: 'set',
|
|
1384
1427
|
path: ['active'],
|
|
1385
1428
|
value: {
|
|
1386
1429
|
...buildActiveFromOption({ ...option, baseUrl: provider?.baseUrl, apiKeyEnv: provider?.apiKeyEnv }),
|
|
1387
1430
|
},
|
|
1388
1431
|
}])
|
|
1389
|
-
|
|
1432
|
+
if (applied === false) setError(t('saveFailed'))
|
|
1433
|
+
} catch (error) { setError(`${t('saveFailed')} ${error?.message ?? String(error)}`) }
|
|
1390
1434
|
}
|
|
1391
1435
|
|
|
1392
1436
|
async function pickAspect(value) {
|
|
1393
1437
|
try {
|
|
1394
|
-
await liteForm.mutate([{ op: 'set', path: ['activeAspect'], value }])
|
|
1395
|
-
|
|
1438
|
+
const applied = await liteForm.mutate([{ op: 'set', path: ['activeAspect'], value }])
|
|
1439
|
+
if (applied === false) setError(t('saveFailed'))
|
|
1440
|
+
} catch (error) { setError(`${t('saveFailed')} ${error?.message ?? String(error)}`) }
|
|
1396
1441
|
}
|
|
1397
1442
|
|
|
1398
|
-
|
|
1443
|
+
function addFiles(fileList) {
|
|
1399
1444
|
const files = Array.from(fileList ?? []).filter((f) => /^image\/(png|jpe?g|webp|gif)$/i.test(f.type))
|
|
1400
|
-
if (files.length === 0) return
|
|
1401
|
-
|
|
1445
|
+
if (files.length === 0) return Promise.resolve()
|
|
1446
|
+
// Serialize every upload. Two quick pastes used to read the same
|
|
1447
|
+
// render-time ref count, both pass the room check and upload twice —
|
|
1448
|
+
// the second batch then losing to a silent `.slice(0, MAX_REFS)`.
|
|
1449
|
+
const next = uploadChain.current.then(() => uploadFiles(files), () => uploadFiles(files))
|
|
1450
|
+
uploadChain.current = next.catch(() => {})
|
|
1451
|
+
return next
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
async function uploadFiles(files) {
|
|
1455
|
+
const base = refsRef.current
|
|
1456
|
+
const room = MAX_REFS - base.length
|
|
1402
1457
|
if (room <= 0) { setError(t('refsFull')); return }
|
|
1403
|
-
const
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
reader.onerror = () => rejectData(reader.error)
|
|
1409
|
-
reader.readAsDataURL(file)
|
|
1410
|
-
})
|
|
1411
|
-
items.push({ data, mediaType: file.type === 'image/jpg' ? 'image/jpeg' : file.type, name: file.name })
|
|
1412
|
-
}
|
|
1458
|
+
const accepted = files.slice(0, room)
|
|
1459
|
+
const skipped = files.length - accepted.length
|
|
1460
|
+
// Everything that can fail sits inside one try: the FileReader awaits
|
|
1461
|
+
// used to sit outside it, so an unreadable file rejected the whole call
|
|
1462
|
+
// and every caller (paste, drop, file input) dropped it unhandled.
|
|
1413
1463
|
try {
|
|
1464
|
+
const items = []
|
|
1465
|
+
for (const file of accepted) {
|
|
1466
|
+
const data = await new Promise((resolveData, rejectData) => {
|
|
1467
|
+
const reader = new FileReader()
|
|
1468
|
+
reader.onload = () => resolveData(String(reader.result).split(',', 2)[1] ?? '')
|
|
1469
|
+
reader.onerror = () => rejectData(new Error(reader.error?.message ?? `cannot read ${file.name}`))
|
|
1470
|
+
reader.readAsDataURL(file)
|
|
1471
|
+
})
|
|
1472
|
+
items.push({ data, mediaType: file.type === 'image/jpg' ? 'image/jpeg' : file.type, name: file.name })
|
|
1473
|
+
}
|
|
1414
1474
|
const value = await rpcCall(rpc, 'admitImages', { items })
|
|
1415
|
-
const fresh = value.refs.filter((ref) => !
|
|
1416
|
-
|
|
1417
|
-
|
|
1475
|
+
const fresh = value.refs.filter((ref) => !base.some((r) => r.attachmentId === ref.attachmentId))
|
|
1476
|
+
const merged = [...base, ...fresh].slice(0, MAX_REFS)
|
|
1477
|
+
refsRef.current = merged
|
|
1478
|
+
if (fresh.length > 0) updateStore((prev) => ({
|
|
1479
|
+
...prev,
|
|
1480
|
+
refs: [...prev.refs, ...fresh.filter((ref) => !prev.refs.some((r) => r.attachmentId === ref.attachmentId))].slice(0, MAX_REFS),
|
|
1481
|
+
}))
|
|
1482
|
+
// Never drop the overflow on the floor: say what actually landed.
|
|
1483
|
+
if (skipped > 0 || base.length + fresh.length > MAX_REFS) setError(t('refsPartial'))
|
|
1484
|
+
else setError('')
|
|
1418
1485
|
} catch (err) {
|
|
1419
1486
|
setError(`${t('uploadFailed')} ${err?.message ?? ''}`)
|
|
1420
1487
|
}
|
|
@@ -1435,6 +1502,8 @@ window.__ModuleLoader__.load({
|
|
|
1435
1502
|
async function generate() {
|
|
1436
1503
|
const text = prompt.trim()
|
|
1437
1504
|
if (!text || busy) return
|
|
1505
|
+
const controller = new AbortController()
|
|
1506
|
+
abortRef.current = controller
|
|
1438
1507
|
setBusy(true)
|
|
1439
1508
|
setError('')
|
|
1440
1509
|
try {
|
|
@@ -1443,7 +1512,8 @@ window.__ModuleLoader__.load({
|
|
|
1443
1512
|
aspect: activeAspect,
|
|
1444
1513
|
prompt: text,
|
|
1445
1514
|
...store.refs.length > 0 ? { refs: store.refs } : {},
|
|
1446
|
-
})
|
|
1515
|
+
}, controller.signal)
|
|
1516
|
+
if (controller.signal.aborted) return
|
|
1447
1517
|
updateStore((prev) => ({
|
|
1448
1518
|
...prev,
|
|
1449
1519
|
prompt: text,
|
|
@@ -1457,12 +1527,18 @@ window.__ModuleLoader__.load({
|
|
|
1457
1527
|
}, ...prev.recent].slice(0, 12),
|
|
1458
1528
|
}))
|
|
1459
1529
|
} catch (err) {
|
|
1460
|
-
setError(
|
|
1530
|
+
if (controller.signal.aborted || err?.name === 'AbortError') setError(t('cancelled'))
|
|
1531
|
+
else setError(`${t('errorPrefix')}: ${err?.message ?? err}`)
|
|
1461
1532
|
} finally {
|
|
1533
|
+
if (abortRef.current === controller) abortRef.current = null
|
|
1462
1534
|
setBusy(false)
|
|
1463
1535
|
}
|
|
1464
1536
|
}
|
|
1465
1537
|
|
|
1538
|
+
function cancelGenerate() {
|
|
1539
|
+
abortRef.current?.abort()
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1466
1542
|
const canGenerate = hasActive && !!cwd && prompt.trim().length > 0 && !busy
|
|
1467
1543
|
const nothingToClear = store.refs.length === 0 && prompt.trim().length === 0 && store.recent.length === 0
|
|
1468
1544
|
// Start over: references, prompt, results — the model and aspect stay.
|
|
@@ -1472,11 +1548,34 @@ window.__ModuleLoader__.load({
|
|
|
1472
1548
|
setError('')
|
|
1473
1549
|
}
|
|
1474
1550
|
|
|
1475
|
-
|
|
1551
|
+
// A dropped file must never reach the browser's default handler: that
|
|
1552
|
+
// navigates the whole GUI to file:// and takes the live session with it.
|
|
1553
|
+
// Root-level, so every child (refs box, prompt, results) is covered.
|
|
1554
|
+
const dragHandlers = {
|
|
1555
|
+
onDragOver: (event) => {
|
|
1556
|
+
if (!carriesFiles(event)) return
|
|
1557
|
+
event.preventDefault()
|
|
1558
|
+
try { event.dataTransfer.dropEffect = 'copy' } catch {}
|
|
1559
|
+
if (!dragging) setDragging(true)
|
|
1560
|
+
},
|
|
1561
|
+
onDragLeave: (event) => {
|
|
1562
|
+
const next = event.relatedTarget
|
|
1563
|
+
if (typeof next === 'object' && next !== null && event.currentTarget.contains(next)) return
|
|
1564
|
+
setDragging(false)
|
|
1565
|
+
},
|
|
1566
|
+
onDrop: (event) => {
|
|
1567
|
+
setDragging(false)
|
|
1568
|
+
if (!carriesFiles(event)) return
|
|
1569
|
+
event.preventDefault()
|
|
1570
|
+
void addFiles(event.dataTransfer.files)
|
|
1571
|
+
},
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
return React.createElement('div', { style: css.panel, ...dragHandlers, onPaste: (event) => {
|
|
1476
1575
|
const files = Array.from(event.clipboardData?.items ?? [])
|
|
1477
1576
|
.map((item) => item.getAsFile())
|
|
1478
1577
|
.filter((f) => f !== null)
|
|
1479
|
-
if (files.length > 0) { event.preventDefault(); addFiles(files) }
|
|
1578
|
+
if (files.length > 0) { event.preventDefault(); void addFiles(files) }
|
|
1480
1579
|
} },
|
|
1481
1580
|
// Clear — top-right, red, disabled while a generation runs or when idle.
|
|
1482
1581
|
React.createElement('div', { style: css.topbar },
|
|
@@ -1528,7 +1627,7 @@ window.__ModuleLoader__.load({
|
|
|
1528
1627
|
role: 'group',
|
|
1529
1628
|
'aria-label': t('refs'),
|
|
1530
1629
|
'aria-describedby': 'dsh-igl-refs-hint',
|
|
1531
|
-
style: { ...css.drop, ...(refsFocus ? css.dropActive : null) },
|
|
1630
|
+
style: { ...css.drop, ...((refsFocus || dragging) ? css.dropActive : null) },
|
|
1532
1631
|
onFocus: () => setRefsFocus(true),
|
|
1533
1632
|
onBlur: () => setRefsFocus(false),
|
|
1534
1633
|
onClick: () => setRefsFocus(true),
|
|
@@ -1536,7 +1635,9 @@ window.__ModuleLoader__.load({
|
|
|
1536
1635
|
const files = Array.from(event.clipboardData?.items ?? [])
|
|
1537
1636
|
.map((item) => item.getAsFile())
|
|
1538
1637
|
.filter((f) => f !== null)
|
|
1539
|
-
|
|
1638
|
+
// The panel root listens for the same paste; without this the box
|
|
1639
|
+
// uploads every image twice (two attachments, two ref slots).
|
|
1640
|
+
if (files.length > 0) { event.preventDefault(); event.stopPropagation(); void addFiles(files) }
|
|
1540
1641
|
},
|
|
1541
1642
|
},
|
|
1542
1643
|
store.refs.length === 0
|
|
@@ -1577,9 +1678,14 @@ window.__ModuleLoader__.load({
|
|
|
1577
1678
|
}),
|
|
1578
1679
|
error ? React.createElement('p', { style: css.error, role: 'alert' }, error) : null,
|
|
1579
1680
|
!cwd ? React.createElement('p', { style: css.hint }, t('noCwd')) : null,
|
|
1580
|
-
React.createElement('
|
|
1581
|
-
|
|
1582
|
-
|
|
1681
|
+
React.createElement('div', { style: { ...css.row, gap: 8 } },
|
|
1682
|
+
React.createElement('button', {
|
|
1683
|
+
type: 'button', className: 'dsh-igl-primary', disabled: !canGenerate, onClick: generate,
|
|
1684
|
+
}, busy ? t('generating') : t('generate')),
|
|
1685
|
+
busy ? React.createElement('button', {
|
|
1686
|
+
type: 'button', className: 'dsh-igl-btn', onClick: cancelGenerate,
|
|
1687
|
+
}, t('cancel')) : null,
|
|
1688
|
+
),
|
|
1583
1689
|
),
|
|
1584
1690
|
// Results
|
|
1585
1691
|
React.createElement('div', { style: css.section },
|
|
@@ -1746,8 +1852,20 @@ window.__ModuleLoader__.load({
|
|
|
1746
1852
|
}),
|
|
1747
1853
|
}, CornerButton)))
|
|
1748
1854
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
1855
|
+
// Re-mount only when the gate actually flips. `lite.subscribe` fires on
|
|
1856
|
+
// every config write — including the panel's own model and aspect
|
|
1857
|
+
// pickers (1382/1394) — and releasing the pane registration on those
|
|
1858
|
+
// unmounts StudioBody, throwing away a half-typed prompt and resetting
|
|
1859
|
+
// `busy`, which lets a second generate fire while the first still runs.
|
|
1860
|
+
let mountedEnabled = sidebarStudioEnabled(lite)
|
|
1861
|
+
const sync = () => {
|
|
1862
|
+
const enabled = sidebarStudioEnabled(lite)
|
|
1863
|
+
if (enabled === mountedEnabled) return
|
|
1864
|
+
mountedEnabled = enabled
|
|
1865
|
+
release()
|
|
1866
|
+
mount()
|
|
1867
|
+
}
|
|
1868
|
+
if (mountedEnabled) mount()
|
|
1751
1869
|
const stop = typeof lite.subscribe === 'function' ? lite.subscribe(sync) : undefined
|
|
1752
1870
|
return () => { try { stop?.() } catch {} release() }
|
|
1753
1871
|
}, 'dsh-image-lite: sidebar studio'))
|
package/index.js
CHANGED
|
@@ -13,10 +13,12 @@
|
|
|
13
13
|
* write takes effect on the next tool call without a restart.
|
|
14
14
|
*
|
|
15
15
|
* P1 scope: the `openai-images` API format only (gpt-image-1 and
|
|
16
|
-
* compatible gateways). The 出图 fetch layer, the
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* compatible gateways). The 出图 fetch layer, the tool, and the
|
|
17
|
+
* system-prompt section are written against the DSH services (attachments /
|
|
18
|
+
* credentials / tools / system-prompt); the panel's RPC channel rides on
|
|
19
|
+
* Connection's generic channel registry rather than a private route, so the
|
|
20
|
+
* transport stays owned by the platform. This plugin is self-contained — no
|
|
21
|
+
* other image plugin is required.
|
|
20
22
|
*/
|
|
21
23
|
import z from '@deepseek-ai/schemastery'
|
|
22
24
|
import { credentialRef } from '@deepseek-ai/dsh-credentials'
|
|
@@ -59,7 +61,7 @@ export const ENTRY_NS = 'image-gen-lite'
|
|
|
59
61
|
/** Loopback RPC channel for the client half. */
|
|
60
62
|
export const RPC_CHANNEL = '/image-gen-lite'
|
|
61
63
|
|
|
62
|
-
const USER_AGENT = 'dsh-image-lite/0.
|
|
64
|
+
const USER_AGENT = 'dsh-image-lite/0.2.0'
|
|
63
65
|
const SUPPORTED_FORMAT = 'openai-images'
|
|
64
66
|
|
|
65
67
|
/**
|
|
@@ -640,72 +642,35 @@ function failure(error) {
|
|
|
640
642
|
}
|
|
641
643
|
|
|
642
644
|
/**
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
645
|
+
* Mount the panel's RPC channel on Connection's generic channel registry.
|
|
646
|
+
*
|
|
647
|
+
* The registry owns the transport end to end: envelope framing, the
|
|
648
|
+
* content-type check, the Host/Origin fence, 401/403 admission, request
|
|
649
|
+
* cancellation and the buffered-body cap. Its handler receives the bridge's
|
|
650
|
+
* abort signal as the third argument, so a client disconnect now actually
|
|
651
|
+
* cancels in-flight work instead of being dropped on the floor.
|
|
652
|
+
*
|
|
653
|
+
* Enter through `connection.register(owner, …)`, never its `rpc.handle` sugar:
|
|
654
|
+
* that getter takes the owner from Connection's own context, where
|
|
655
|
+
* `owner.webServer` throws "cannot get property webServer without inject" and
|
|
656
|
+
* the route silently never mounts (reproduced against cordis 4.1.0 — plain
|
|
657
|
+
* `rpc.handle`, `ctx.connection.rpc.handle` and destructured `rpc.handle` all
|
|
658
|
+
* fail the same way). Passing this inject scope explicitly is what makes
|
|
659
|
+
* `owner.webServer` resolvable, so `webServer` also has to stay in the inject
|
|
660
|
+
* list.
|
|
661
|
+
*
|
|
662
|
+
* The previous implementation mounted a prefix route of its own and
|
|
663
|
+
* re-implemented the envelope; that copy drifted from the platform (no
|
|
664
|
+
* cancellation, no body cap) and has been removed.
|
|
647
665
|
*/
|
|
648
|
-
async function serveRpc(req, res, admit, handle) {
|
|
649
|
-
let rejection
|
|
650
|
-
try {
|
|
651
|
-
rejection = admit(req)
|
|
652
|
-
} catch {
|
|
653
|
-
rejection = 500
|
|
654
|
-
}
|
|
655
|
-
if (rejection !== undefined) {
|
|
656
|
-
res.writeHead(rejection)
|
|
657
|
-
res.end(rejection === 401 ? 'unauthorized' : 'forbidden')
|
|
658
|
-
return
|
|
659
|
-
}
|
|
660
|
-
const url = new URL(req.url ?? '/', 'http://x')
|
|
661
|
-
const endpoint = url.pathname.startsWith(`${RPC_CHANNEL}/`) ? url.pathname.slice(RPC_CHANNEL.length + 1) : ''
|
|
662
|
-
if (req.method !== 'POST' || endpoint === '') {
|
|
663
|
-
res.writeHead(req.method === 'POST' ? 404 : 405)
|
|
664
|
-
res.end()
|
|
665
|
-
return
|
|
666
|
-
}
|
|
667
|
-
if ((req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase() !== 'application/json') {
|
|
668
|
-
res.writeHead(415)
|
|
669
|
-
res.end('content type must be application/json')
|
|
670
|
-
return
|
|
671
|
-
}
|
|
672
|
-
let raw = ''
|
|
673
|
-
for await (const chunk of req) raw += chunk
|
|
674
|
-
let message
|
|
675
|
-
try {
|
|
676
|
-
message = JSON.parse(raw)
|
|
677
|
-
} catch {
|
|
678
|
-
res.writeHead(400)
|
|
679
|
-
res.end('body is not JSON')
|
|
680
|
-
return
|
|
681
|
-
}
|
|
682
|
-
const rpcId = typeof message?.rpcId === 'string' ? message.rpcId : 'invalid-request'
|
|
683
|
-
const reply = (result) => {
|
|
684
|
-
res.writeHead(200, { 'content-type': 'application/json' })
|
|
685
|
-
res.end(JSON.stringify({ type: 'server-response', rpcId, result }))
|
|
686
|
-
}
|
|
687
|
-
if (message?.type !== 'client-request' || message.method !== endpoint) {
|
|
688
|
-
reply({ ok: false, error: { code: 'gateway/bad-request', message: 'method does not match endpoint', details: {} } })
|
|
689
|
-
return
|
|
690
|
-
}
|
|
691
|
-
try {
|
|
692
|
-
reply(await handle(endpoint, message.payload))
|
|
693
|
-
} catch (error) {
|
|
694
|
-
reply({ ok: false, error: { code: 'gateway/internal', message: error instanceof Error ? error.message : String(error), details: {} } })
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
|
|
698
666
|
function registerRpc(ctx, readActive, readAspect, readOutputDir) {
|
|
699
667
|
ctx.inject(['connection', 'webServer'], (scoped) => {
|
|
700
668
|
const connection = scoped.get('connection')
|
|
701
|
-
|
|
702
|
-
if (connection
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
path: RPC_CHANNEL,
|
|
707
|
-
handler: (req, res) => serveRpc(req, res, admit, handleRpc),
|
|
708
|
-
}), 'dsh-image-lite: rpc route')
|
|
669
|
+
if (connection === undefined) return
|
|
670
|
+
if (typeof connection.register !== 'function') {
|
|
671
|
+
scoped.logger?.warn?.('dsh-image-lite: this host has no connection.register; the 生图工作台 panel has no RPC channel')
|
|
672
|
+
return
|
|
673
|
+
}
|
|
709
674
|
async function handleRpc(endpoint, payload, signal) {
|
|
710
675
|
try {
|
|
711
676
|
if (typeof payload !== 'object' || payload === null) throw new BadRequest('payload must be an object')
|
|
@@ -804,6 +769,7 @@ function registerRpc(ctx, readActive, readAspect, readOutputDir) {
|
|
|
804
769
|
return failure(error)
|
|
805
770
|
}
|
|
806
771
|
}
|
|
772
|
+
connection.register(scoped, RPC_CHANNEL, handleRpc)
|
|
807
773
|
})
|
|
808
774
|
}
|
|
809
775
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-image-lite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "DSH 生图插件支持双模式生图:直接对话大模型,通过 image_generate_lite 工具生图;或在侧边栏「生图工作台」处理生图任务。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"deepseek",
|
|
@@ -65,11 +65,11 @@
|
|
|
65
65
|
"license": "MIT",
|
|
66
66
|
"peerDependencies": {
|
|
67
67
|
"@deepseek-ai/cordis": "^4.0.0",
|
|
68
|
-
"@deepseek-ai/dsh-attachment": ">=0.
|
|
69
|
-
"@deepseek-ai/dsh-credentials": ">=0.1
|
|
70
|
-
"@deepseek-ai/dsh-settings": ">=0.1
|
|
71
|
-
"@deepseek-ai/dsh-system-prompt": ">=0.1
|
|
72
|
-
"@deepseek-ai/dsh-tools": ">=0.1
|
|
68
|
+
"@deepseek-ai/dsh-attachment": ">=0.2.1-rc.1",
|
|
69
|
+
"@deepseek-ai/dsh-credentials": ">=0.2.1-rc.1",
|
|
70
|
+
"@deepseek-ai/dsh-settings": ">=0.2.1-rc.1",
|
|
71
|
+
"@deepseek-ai/dsh-system-prompt": ">=0.2.1-rc.1",
|
|
72
|
+
"@deepseek-ai/dsh-tools": ">=0.2.1-rc.1",
|
|
73
73
|
"@deepseek-ai/schemastery": "^3.18.1"
|
|
74
74
|
}
|
|
75
75
|
}
|