@goodandready/dsh-image-gen 0.10.22 → 0.10.23
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 +7 -0
- package/README.ru.md +7 -0
- package/README.zh.md +7 -0
- package/lib/client.js +155 -189
- package/lib/index.js +37 -32
- package/lib/providers.js +96 -50
- package/lib/resolve-image.js +1 -1
- package/lib/tools/generation.js +9 -9
- package/lib/updater.js +338 -0
- package/package.json +1 -1
package/lib/updater.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { readFile } from 'node:fs/promises'
|
|
4
|
+
import { homedir } from 'node:os'
|
|
5
|
+
import { basename, dirname, isAbsolute, resolve } from 'node:path'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Host-side one-click updater for @goodandready/dsh-image-gen.
|
|
10
|
+
* Implements the unified DSH plugin updater specification.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const UPDATE_HEADER = 'x-dsh-plugin-update'
|
|
14
|
+
const UPDATE_TIMEOUT_MS = 10 * 60_000
|
|
15
|
+
const VERSION_CACHE_MS = 5 * 60_000
|
|
16
|
+
let latestCache = undefined
|
|
17
|
+
|
|
18
|
+
function header(request, name) {
|
|
19
|
+
const value = request?.headers?.[name]
|
|
20
|
+
return Array.isArray(value) ? value[0] : value
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isLoopback(value) {
|
|
24
|
+
const address = value?.toLowerCase().replace(/^\[|\]$/g, '')
|
|
25
|
+
return (
|
|
26
|
+
address === 'localhost' ||
|
|
27
|
+
address === 'localhost.' ||
|
|
28
|
+
address === '::1' ||
|
|
29
|
+
address?.startsWith('127.') === true ||
|
|
30
|
+
address?.startsWith('::ffff:127.') === true
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isPrivateLan(value) {
|
|
35
|
+
const address = value?.toLowerCase().replace(/^\[|\]$/g, '')
|
|
36
|
+
if (!address) return false
|
|
37
|
+
const ipv4 = address.startsWith('::ffff:') ? address.slice(7) : address
|
|
38
|
+
return (
|
|
39
|
+
ipv4.startsWith('192.168.') ||
|
|
40
|
+
ipv4.startsWith('10.') ||
|
|
41
|
+
/^172\.(1[6-9]|2[0-9]|3[0-1])\./.test(ipv4)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isTrustedUpdateRequest(request) {
|
|
46
|
+
if (header(request, UPDATE_HEADER) !== '1') return false
|
|
47
|
+
const remote = request.socket?.remoteAddress
|
|
48
|
+
if (!isLoopback(remote) && !isPrivateLan(remote)) return false
|
|
49
|
+
const site = header(request, 'sec-fetch-site')
|
|
50
|
+
if (site !== undefined && site !== 'same-origin') return false
|
|
51
|
+
const origin = header(request, 'origin')
|
|
52
|
+
const host = header(request, 'host')
|
|
53
|
+
if (origin === undefined || host === undefined) return false
|
|
54
|
+
try {
|
|
55
|
+
const url = new URL(origin)
|
|
56
|
+
return (
|
|
57
|
+
(url.protocol === 'http:' || url.protocol === 'https:') &&
|
|
58
|
+
(isLoopback(url.hostname) || isPrivateLan(url.hostname)) &&
|
|
59
|
+
url.host === host
|
|
60
|
+
)
|
|
61
|
+
} catch {
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function validProfileName(value) {
|
|
67
|
+
return (
|
|
68
|
+
typeof value === 'string' &&
|
|
69
|
+
value !== '' &&
|
|
70
|
+
value !== '.' &&
|
|
71
|
+
value !== '..' &&
|
|
72
|
+
!value.includes('/') &&
|
|
73
|
+
!value.includes('\\') &&
|
|
74
|
+
!/[\0-\x1f\x7f]/.test(value)
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function profileNameFromArgv(argv) {
|
|
79
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
80
|
+
const arg = argv[index]
|
|
81
|
+
if (arg === '--profile' || arg === '-p') {
|
|
82
|
+
const next = argv[index + 1]
|
|
83
|
+
if (validProfileName(next)) return next
|
|
84
|
+
}
|
|
85
|
+
if (typeof arg === 'string' && arg.startsWith('--profile=')) {
|
|
86
|
+
const value = arg.slice('--profile='.length)
|
|
87
|
+
if (validProfileName(value)) return value
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function findDshCliEntry() {
|
|
94
|
+
const entry = process.argv[1]
|
|
95
|
+
if (typeof entry !== 'string' || entry === '') return undefined
|
|
96
|
+
if (!existsSync(entry)) return undefined
|
|
97
|
+
for (let directory = dirname(entry); ; directory = dirname(directory)) {
|
|
98
|
+
const manifestPath = resolve(directory, 'package.json')
|
|
99
|
+
if (existsSync(manifestPath)) {
|
|
100
|
+
try {
|
|
101
|
+
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
|
|
102
|
+
const bin =
|
|
103
|
+
typeof manifest.bin === 'string'
|
|
104
|
+
? manifest.bin
|
|
105
|
+
: typeof manifest.bin === 'object' && manifest.bin !== null
|
|
106
|
+
? manifest.bin.dsh
|
|
107
|
+
: undefined
|
|
108
|
+
if (
|
|
109
|
+
manifest.name === '@deepseek-ai/dsh' &&
|
|
110
|
+
typeof bin === 'string' &&
|
|
111
|
+
!isAbsolute(bin) &&
|
|
112
|
+
resolve(directory, bin) === resolve(entry)
|
|
113
|
+
) {
|
|
114
|
+
return entry
|
|
115
|
+
}
|
|
116
|
+
} catch {
|
|
117
|
+
// Continue searching parent directories
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const parent = dirname(directory)
|
|
121
|
+
if (parent === directory) return undefined
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function runtime() {
|
|
126
|
+
const profileDir = resolve(
|
|
127
|
+
process.env.DSH_PROFILE_DIR ?? resolve(homedir(), '.dsh', 'profiles', 'web')
|
|
128
|
+
)
|
|
129
|
+
const selected = profileNameFromArgv(process.argv)
|
|
130
|
+
const profileName = validProfileName(selected)
|
|
131
|
+
? selected
|
|
132
|
+
: validProfileName(basename(profileDir))
|
|
133
|
+
? basename(profileDir)
|
|
134
|
+
: 'web'
|
|
135
|
+
const cliEntry = findDshCliEntry()
|
|
136
|
+
return cliEntry === undefined ? { profileName, profileDir } : { profileName, profileDir, cliEntry }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function parseSemver(value) {
|
|
140
|
+
const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(value)
|
|
141
|
+
if (match === null) return undefined
|
|
142
|
+
return {
|
|
143
|
+
core: [Number(match[1]), Number(match[2]), Number(match[3])],
|
|
144
|
+
prerelease: match[4]?.split('.') ?? [],
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function comparePrerelease(left, right) {
|
|
149
|
+
if (left.length === 0 || right.length === 0) return left.length === right.length ? 0 : left.length === 0 ? 1 : -1
|
|
150
|
+
const length = Math.max(left.length, right.length)
|
|
151
|
+
for (let index = 0; index < length; index += 1) {
|
|
152
|
+
const a = left[index]
|
|
153
|
+
const b = right[index]
|
|
154
|
+
if (a === undefined || b === undefined) return a === b ? 0 : a === undefined ? -1 : 1
|
|
155
|
+
if (a === b) continue
|
|
156
|
+
const aNumeric = /^\d+$/.test(a)
|
|
157
|
+
const bNumeric = /^\d+$/.test(b)
|
|
158
|
+
if (aNumeric && bNumeric) {
|
|
159
|
+
const aNumber = BigInt(a)
|
|
160
|
+
const bNumber = BigInt(b)
|
|
161
|
+
if (aNumber !== bNumber) return aNumber > bNumber ? 1 : -1
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
if (aNumeric !== bNumeric) return aNumeric ? -1 : 1
|
|
165
|
+
return a > b ? 1 : -1
|
|
166
|
+
}
|
|
167
|
+
return 0
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function isNewerVersion(currentValue, candidateValue) {
|
|
171
|
+
const current = parseSemver(currentValue)
|
|
172
|
+
const candidate = parseSemver(candidateValue)
|
|
173
|
+
if (current === undefined || candidate === undefined) return false
|
|
174
|
+
for (let index = 0; index < 3; index += 1) {
|
|
175
|
+
if (candidate.core[index] !== current.core[index]) return candidate.core[index] > current.core[index]
|
|
176
|
+
}
|
|
177
|
+
return comparePrerelease(candidate.prerelease, current.prerelease) > 0
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async function latestVersion(packageName, registry) {
|
|
181
|
+
if (
|
|
182
|
+
latestCache?.packageName === packageName &&
|
|
183
|
+
latestCache.registry === registry &&
|
|
184
|
+
Date.now() < latestCache.expiresAt
|
|
185
|
+
) {
|
|
186
|
+
return latestCache.version
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const response = await fetch(`${registry.replace(/\/$/, '')}/${encodeURIComponent(packageName)}/latest`, {
|
|
190
|
+
signal: typeof AbortSignal !== 'undefined' && AbortSignal.timeout ? AbortSignal.timeout(8000) : undefined,
|
|
191
|
+
})
|
|
192
|
+
if (!response.ok) return undefined
|
|
193
|
+
const value = await response.json()
|
|
194
|
+
if (typeof value?.version !== 'string' || value.version === '') return undefined
|
|
195
|
+
latestCache = { packageName, registry, version: value.version, expiresAt: Date.now() + VERSION_CACHE_MS }
|
|
196
|
+
return value.version
|
|
197
|
+
} catch {
|
|
198
|
+
return undefined
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function currentVersion(manifestUrl) {
|
|
203
|
+
const value = JSON.parse(await readFile(manifestUrl, 'utf8'))
|
|
204
|
+
if (typeof value?.version !== 'string' || value.version === '') {
|
|
205
|
+
throw new Error('Cannot read current plugin version.')
|
|
206
|
+
}
|
|
207
|
+
return value.version
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function status(options, target) {
|
|
211
|
+
const current = await currentVersion(options.manifestUrl)
|
|
212
|
+
const latest = await latestVersion(options.packageName, options.registry ?? 'https://registry.npmjs.org')
|
|
213
|
+
return {
|
|
214
|
+
packageName: options.packageName,
|
|
215
|
+
currentVersion: current,
|
|
216
|
+
...(latest === undefined ? {} : { latestVersion: latest }),
|
|
217
|
+
latestCheckFailed: latest === undefined,
|
|
218
|
+
updateAvailable: latest !== undefined && isNewerVersion(current, latest),
|
|
219
|
+
profileName: target.profileName,
|
|
220
|
+
canAutoUpdate: target.cliEntry !== undefined,
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function installExact(target, packageSpec, options) {
|
|
225
|
+
if (target.cliEntry === undefined) throw new Error('Automatic update is unavailable in this runtime.')
|
|
226
|
+
await new Promise((resolvePromise, reject) => {
|
|
227
|
+
const child = spawn(
|
|
228
|
+
process.execPath,
|
|
229
|
+
[
|
|
230
|
+
target.cliEntry,
|
|
231
|
+
'plugin',
|
|
232
|
+
'--profile',
|
|
233
|
+
target.profileName,
|
|
234
|
+
'add',
|
|
235
|
+
'--config.minimumReleaseAge=0',
|
|
236
|
+
packageSpec,
|
|
237
|
+
`--registry=${options.registry ?? 'https://registry.npmjs.org/'}`,
|
|
238
|
+
],
|
|
239
|
+
{
|
|
240
|
+
cwd: target.profileDir,
|
|
241
|
+
windowsHide: true,
|
|
242
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
243
|
+
env: { ...process.env, NO_COLOR: '1' },
|
|
244
|
+
}
|
|
245
|
+
)
|
|
246
|
+
let detail = ''
|
|
247
|
+
child.stdout?.on('data', (chunk) => {
|
|
248
|
+
detail = (detail + String(chunk)).slice(-4000)
|
|
249
|
+
})
|
|
250
|
+
child.stderr?.on('data', (chunk) => {
|
|
251
|
+
detail = (detail + String(chunk)).slice(-4000)
|
|
252
|
+
})
|
|
253
|
+
const timer = setTimeout(() => {
|
|
254
|
+
child.kill()
|
|
255
|
+
reject(new Error('Update timed out; use the normal DSH update flow.'))
|
|
256
|
+
}, UPDATE_TIMEOUT_MS)
|
|
257
|
+
child.once('error', (error) => {
|
|
258
|
+
clearTimeout(timer)
|
|
259
|
+
reject(error)
|
|
260
|
+
})
|
|
261
|
+
child.once('exit', (code) => {
|
|
262
|
+
clearTimeout(timer)
|
|
263
|
+
if (code === 0) resolvePromise()
|
|
264
|
+
else reject(new Error(detail.trim() || `Update exited with code ${String(code)}.`))
|
|
265
|
+
})
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function json(response, statusCode, value) {
|
|
270
|
+
response.writeHead(statusCode, {
|
|
271
|
+
'content-type': 'application/json; charset=utf-8',
|
|
272
|
+
'cache-control': 'no-store',
|
|
273
|
+
})
|
|
274
|
+
response.end(JSON.stringify(value))
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function registerPluginUpdater(ctx, options) {
|
|
278
|
+
let installing = false
|
|
279
|
+
if (typeof ctx?.webServer?.register !== 'function') {
|
|
280
|
+
ctx?.logger?.warn?.('[dsh-image-gen] webServer service unavailable, updater skipped')
|
|
281
|
+
return () => {}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return ctx.webServer.register({
|
|
285
|
+
kind: 'exact',
|
|
286
|
+
path: options.endpoint,
|
|
287
|
+
handler: async (request, response) => {
|
|
288
|
+
try {
|
|
289
|
+
const target = runtime()
|
|
290
|
+
if (request.method === 'GET' || request.method === 'HEAD') {
|
|
291
|
+
const payload = await status(options, target)
|
|
292
|
+
response.writeHead(200, {
|
|
293
|
+
'content-type': 'application/json; charset=utf-8',
|
|
294
|
+
'cache-control': 'no-store',
|
|
295
|
+
})
|
|
296
|
+
response.end(request.method === 'HEAD' ? undefined : JSON.stringify(payload))
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
if (request.method !== 'POST') {
|
|
300
|
+
response.writeHead(405, { allow: 'GET, HEAD, POST' })
|
|
301
|
+
response.end()
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
if (!isTrustedUpdateRequest(request)) {
|
|
305
|
+
json(response, 403, { error: 'Rejected non-local or cross-origin update request.' })
|
|
306
|
+
return
|
|
307
|
+
}
|
|
308
|
+
if (installing) {
|
|
309
|
+
json(response, 409, { error: 'This plugin is already updating.' })
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
installing = true
|
|
313
|
+
try {
|
|
314
|
+
const before = await status(options, target)
|
|
315
|
+
if (before.latestVersion === undefined) {
|
|
316
|
+
json(response, 503, { error: 'The latest version is temporarily unavailable.' })
|
|
317
|
+
return
|
|
318
|
+
}
|
|
319
|
+
if (!before.updateAvailable) {
|
|
320
|
+
json(response, 200, before)
|
|
321
|
+
return
|
|
322
|
+
}
|
|
323
|
+
await installExact(target, `${options.packageName}@${before.latestVersion}`, options)
|
|
324
|
+
json(response, 200, {
|
|
325
|
+
...before,
|
|
326
|
+
updatedVersion: before.latestVersion,
|
|
327
|
+
restartRequired: true,
|
|
328
|
+
})
|
|
329
|
+
} finally {
|
|
330
|
+
installing = false
|
|
331
|
+
}
|
|
332
|
+
} catch (error) {
|
|
333
|
+
ctx?.logger?.warn?.(`[dsh-image-gen] plugin updater failed: ${String(error)}`)
|
|
334
|
+
json(response, 503, { error: 'Plugin update failed; see server logs.' })
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
})
|
|
338
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-image-gen",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.23",
|
|
4
4
|
"description": "Image generation for DeepSeek Harness: a generate_image tool with pluggable providers — the FAL queue, any OpenAI-compatible images API, or a ChatGPT/Grok subscription with no API key at all. The picture is shown inline in the conversation; the model receives either a link (works with any chat model) or the image itself (needs dsh-vision-bridge or a vision-capable model).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"deepseek-harness",
|