@chad.dev/dsh-adclip 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +4 -0
  3. package/README.md +28 -2
  4. package/client.js +2157 -0
  5. package/cordis.patch.yml +10 -0
  6. package/engine/requirements.txt +9 -0
  7. package/engine/src/adclip/__init__.py +0 -0
  8. package/engine/src/adclip/api.py +676 -0
  9. package/engine/src/adclip/image_params.py +64 -0
  10. package/engine/src/adclip/intents.py +190 -0
  11. package/engine/src/adclip/llm.py +142 -0
  12. package/engine/src/adclip/media/__init__.py +0 -0
  13. package/engine/src/adclip/media/asr.py +79 -0
  14. package/engine/src/adclip/media/ffmpeg.py +294 -0
  15. package/engine/src/adclip/media/stitch.py +124 -0
  16. package/engine/src/adclip/media/subtitles.py +45 -0
  17. package/engine/src/adclip/media/vision.py +105 -0
  18. package/engine/src/adclip/models.py +127 -0
  19. package/engine/src/adclip/params.py +363 -0
  20. package/engine/src/adclip/personas.py +60 -0
  21. package/engine/src/adclip/pipeline/__init__.py +0 -0
  22. package/engine/src/adclip/pipeline/analyze.py +133 -0
  23. package/engine/src/adclip/pipeline/generate.py +201 -0
  24. package/engine/src/adclip/pipeline/image_runner.py +131 -0
  25. package/engine/src/adclip/pipeline/persona.py +56 -0
  26. package/engine/src/adclip/pipeline/post.py +33 -0
  27. package/engine/src/adclip/pipeline/qa.py +41 -0
  28. package/engine/src/adclip/pipeline/replace.py +191 -0
  29. package/engine/src/adclip/pipeline/runner.py +138 -0
  30. package/engine/src/adclip/pipeline/script.py +226 -0
  31. package/engine/src/adclip/pipeline/segment.py +87 -0
  32. package/engine/src/adclip/providers/__init__.py +0 -0
  33. package/engine/src/adclip/providers/ark.py +122 -0
  34. package/engine/src/adclip/providers/base.py +30 -0
  35. package/engine/src/adclip/providers/discovery.py +56 -0
  36. package/engine/src/adclip/providers/kling.py +101 -0
  37. package/engine/src/adclip/providers/minimax.py +155 -0
  38. package/engine/src/adclip/providers/minimax_image.py +86 -0
  39. package/engine/src/adclip/settings.py +109 -0
  40. package/engine/src/adclip/store.py +378 -0
  41. package/host/dsh.js +60 -0
  42. package/host/engine.js +265 -0
  43. package/host/proxy.js +142 -0
  44. package/host/supervisor.js +251 -0
  45. package/icon.svg +6 -0
  46. package/index.js +39 -0
  47. package/locale/en.json +12 -0
  48. package/locale/zh.json +12 -0
  49. package/package.json +47 -4
package/host/engine.js ADDED
@@ -0,0 +1,265 @@
1
+ /** Locate the Python engine, prepare its interpreter, and pick a loopback port.
2
+ *
3
+ * The plugin must work on machines that have neither this checkout nor its
4
+ * virtualenv: the engine ships inside the package, and the first launch builds
5
+ * a private venv beside the user's data. Every absolute path below is derived
6
+ * from the installed package or the data directory, never from the developer's
7
+ * working copy.
8
+ */
9
+ import { spawn } from 'node:child_process'
10
+ import { createHash } from 'node:crypto'
11
+ import crypto from 'node:crypto'
12
+ import fs from 'node:fs'
13
+ import net from 'node:net'
14
+ import os from 'node:os'
15
+ import path from 'node:path'
16
+ import process from 'node:process'
17
+
18
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
19
+ const MIN_PYTHON = [3, 10]
20
+
21
+ export function defaultDataDir() {
22
+ if (process.env.ADSTUDIO_DATA_DIR) return process.env.ADSTUDIO_DATA_DIR
23
+ if (process.platform === 'darwin') return path.join(os.homedir(), 'Library', 'Application Support', 'AdPlusStudio', 'data')
24
+ if (process.platform === 'win32') return path.join(process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'), 'AdPlusStudio', 'data')
25
+ return path.join(os.homedir(), '.local', 'share', 'adplus-studio', 'data')
26
+ }
27
+
28
+ /** Interpreter path inside a venv root. */
29
+ export function venvPython(venvDir) {
30
+ return process.platform === 'win32'
31
+ ? path.join(venvDir, 'Scripts', 'python.exe')
32
+ : path.join(venvDir, 'bin', 'python')
33
+ }
34
+
35
+ function isFile(target) {
36
+ try { return fs.statSync(target).isFile() } catch { return false }
37
+ }
38
+
39
+ function isDir(target) {
40
+ try { return fs.statSync(target).isDirectory() } catch { return false }
41
+ }
42
+
43
+ function requirementsDigest(file) {
44
+ try { return createHash('sha256').update(fs.readFileSync(file)).digest('hex').slice(0, 16) } catch { return '' }
45
+ }
46
+
47
+ function readStamp(file) {
48
+ try { return JSON.parse(fs.readFileSync(file, 'utf8')) } catch { return null }
49
+ }
50
+
51
+ /** Directories that may hold the engine sources, most specific first. */
52
+ export function engineCandidates({ config = {}, pluginRoot }) {
53
+ const list = []
54
+ if (config.engineRoot) list.push(config.engineRoot)
55
+ if (pluginRoot) {
56
+ list.push(path.join(pluginRoot, 'engine'))
57
+ list.push(path.resolve(pluginRoot, '..'))
58
+ }
59
+ return list
60
+ }
61
+
62
+ /** Import root of an engine directory: `src/` in a src-layout tree, else the root itself. */
63
+ export function engineModuleRoot(root) {
64
+ if (isFile(path.join(root, 'src', 'adclip', 'api.py'))) return path.join(root, 'src')
65
+ if (isFile(path.join(root, 'adclip', 'api.py'))) return root
66
+ return null
67
+ }
68
+
69
+ /** Resolve the engine sources and the interpreter to run them with.
70
+ *
71
+ * Order: an explicit `config.python`, a virtualenv beside the engine sources
72
+ * (a source checkout), the venv this plugin built under the data directory,
73
+ * then a bootstrap from the system `python3`.
74
+ */
75
+ export function resolveEngine({ config = {}, dataDir = defaultDataDir(), pluginRoot }) {
76
+ const candidates = engineCandidates({ config, pluginRoot })
77
+ const engineRoot = candidates.find((root) => engineModuleRoot(root) !== null) || null
78
+ const moduleRoot = engineRoot ? engineModuleRoot(engineRoot) : null
79
+ const relativeVenv = engineRoot ? path.join(engineRoot, '.venv') : null
80
+ const controlDir = path.join(dataDir, '.engine')
81
+ return {
82
+ engineRoot,
83
+ moduleRoot,
84
+ candidates,
85
+ requirements: engineRoot ? path.join(engineRoot, 'requirements.txt') : null,
86
+ controlDir,
87
+ venvDir: path.join(controlDir, 'venv'),
88
+ stampPath: path.join(controlDir, 'venv.json'),
89
+ logPath: path.join(controlDir, 'bootstrap.log'),
90
+ sourceCheckoutPython: relativeVenv ? venvPython(relativeVenv) : null,
91
+ configuredPython: config.python || null,
92
+ }
93
+ }
94
+
95
+ /** Append one bootstrap line to the log file and the in-memory tail. */
96
+ function makeLog(logPath) {
97
+ fs.mkdirSync(path.dirname(logPath), { recursive: true })
98
+ const lines = []
99
+ const write = (line) => {
100
+ const text = `[${new Date().toISOString()}] ${line}`
101
+ lines.push(text)
102
+ if (lines.length > 400) lines.splice(0, lines.length - 400)
103
+ try { fs.appendFileSync(logPath, text + '\n') } catch { /* logging never blocks startup */ }
104
+ }
105
+ return { write, tail: () => lines.slice(-60) }
106
+ }
107
+
108
+ function run(file, args, { cwd, timeoutMs = 180000 } = {}) {
109
+ return new Promise((resolve) => {
110
+ let child
111
+ try {
112
+ child = spawn(file, args, {
113
+ cwd,
114
+ stdio: ['ignore', 'pipe', 'pipe'],
115
+ env: { ...process.env, PIP_DISABLE_PIP_VERSION_CHECK: '1', PYTHONUNBUFFERED: '1' },
116
+ })
117
+ } catch (error) {
118
+ resolve({ code: -1, output: String(error?.message || error) })
119
+ return
120
+ }
121
+ let output = ''
122
+ const collect = (chunk) => {
123
+ output += String(chunk)
124
+ if (output.length > 20000) output = output.slice(-20000)
125
+ }
126
+ child.stdout?.setEncoding('utf8')
127
+ child.stderr?.setEncoding('utf8')
128
+ child.stdout?.on('data', collect)
129
+ child.stderr?.on('data', collect)
130
+ const timer = setTimeout(() => { try { child.kill('SIGKILL') } catch { /* gone */ } }, timeoutMs)
131
+ child.on('error', (error) => {
132
+ clearTimeout(timer)
133
+ resolve({ code: -1, output: output + String(error?.message || error) })
134
+ })
135
+ child.on('exit', (code, signal) => {
136
+ clearTimeout(timer)
137
+ resolve({ code: code ?? -1, signal, output })
138
+ })
139
+ })
140
+ }
141
+
142
+ function systemPythons() {
143
+ return process.platform === 'win32' ? ['python', 'py'] : ['python3', 'python']
144
+ }
145
+
146
+ /** Prompt for a loopback port, or reuse the configured one. */
147
+ export function pickPort(fixed) {
148
+ const wanted = Number(fixed)
149
+ if (Number.isInteger(wanted) && wanted > 0) return Promise.resolve(wanted)
150
+ return new Promise((resolve, reject) => {
151
+ const server = net.createServer()
152
+ server.once('error', reject)
153
+ server.listen(0, '127.0.0.1', () => {
154
+ const address = server.address()
155
+ const port = typeof address === 'object' && address ? address.port : 0
156
+ server.close(() => (port ? resolve(port) : reject(new Error('could not reserve a port'))))
157
+ })
158
+ })
159
+ }
160
+
161
+ /** A free TCP port bound to loopback only. */
162
+ export async function freePort() {
163
+ return pickPort(0)
164
+ }
165
+
166
+ /** Version of an interpreter, or null when it cannot be queried. */
167
+ async function pythonVersion(python) {
168
+ const result = await run(python, ['-c', 'import sys;print("%d.%d" % sys.version_info[:2])'], { timeoutMs: 15000 })
169
+ if (result.code !== 0) return null
170
+ const match = /^(\d+)\.(\d+)/.exec(result.output.trim())
171
+ return match ? [Number(match[1]), Number(match[2])] : null
172
+ }
173
+
174
+ function versionOk(version) {
175
+ if (!version) return false
176
+ return version[0] > MIN_PYTHON[0] || (version[0] === MIN_PYTHON[0] && version[1] >= MIN_PYTHON[1])
177
+ }
178
+
179
+ /** Smallest install that can run the engine, by platform. */
180
+ function pythonHint() {
181
+ if (process.platform === 'darwin') {
182
+ return '未找到 Python 3.10+。安装 Xcode 命令行工具(xcode-select --install)或 Homebrew:brew install python@3.12'
183
+ }
184
+ if (process.platform === 'win32') {
185
+ return '未找到 Python 3.10+。请从 https://www.python.org/downloads/ 安装(勾选 Add python.exe to PATH),或 winget install Python.Python.3.12'
186
+ }
187
+ return '未找到 Python 3.10+。请安装发行版提供的 python3-venv 与 python3-pip,例如 apt install python3 python3-venv python3-pip'
188
+ }
189
+
190
+ /** Ensure a python that can import the engine's dependencies exists.
191
+ *
192
+ * Returns `{ ok, python, source, detail, logs }`; never throws. `source` is
193
+ * one of `config`, `source-checkout`, `venv`, `system`.
194
+ */
195
+ export async function ensureRuntime({ config = {}, dataDir = defaultDataDir(), pluginRoot } = {}) {
196
+ const resolved = resolveEngine({ config, dataDir, pluginRoot })
197
+ const logger = makeLog(resolved.logPath)
198
+ const fail = (detail) => {
199
+ logger.write(`failed: ${detail}`)
200
+ return { ok: false, detail, logs: logger.tail(), ...resolved }
201
+ }
202
+ if (!resolved.engineRoot) {
203
+ return fail(`找不到引擎源码(src/adclip/api.py 或 adclip/api.py)。已尝试:${engineCandidates({ config, pluginRoot }).join('、')}`)
204
+ }
205
+ if (resolved.configuredPython) {
206
+ if (isFile(resolved.configuredPython)) {
207
+ logger.write(`using configured python ${resolved.configuredPython}`)
208
+ return { ok: true, python: resolved.configuredPython, source: 'config', engineRoot: resolved.engineRoot, moduleRoot: resolved.moduleRoot, logs: logger.tail() }
209
+ }
210
+ logger.write(`configured python missing, ignoring: ${resolved.configuredPython}`)
211
+ }
212
+ if (resolved.sourceCheckoutPython && isFile(resolved.sourceCheckoutPython)) {
213
+ logger.write(`using source-checkout python ${resolved.sourceCheckoutPython}`)
214
+ return { ok: true, python: resolved.sourceCheckoutPython, source: 'source-checkout', engineRoot: resolved.engineRoot, moduleRoot: resolved.moduleRoot, logs: logger.tail() }
215
+ }
216
+ const digest = requirementsDigest(resolved.requirements)
217
+ const venvPythonPath = venvPython(resolved.venvDir)
218
+ if (isFile(venvPythonPath)) {
219
+ const stamp = readStamp(resolved.stampPath)
220
+ if (!digest || stamp?.requirements === digest) {
221
+ logger.write(`using prepared venv ${resolved.venvDir}`)
222
+ return { ok: true, python: venvPythonPath, source: 'venv', engineRoot: resolved.engineRoot, moduleRoot: resolved.moduleRoot, logs: logger.tail() }
223
+ }
224
+ logger.write('requirements changed, rebuilding the venv')
225
+ }
226
+ if (!resolved.requirements || !isFile(resolved.requirements)) {
227
+ return fail(`引擎依赖清单缺失:${resolved.requirements}`)
228
+ }
229
+ let python = null
230
+ for (const candidate of systemPythons()) {
231
+ const version = await pythonVersion(candidate)
232
+ if (versionOk(version)) { python = candidate; break }
233
+ if (version) logger.write(`${candidate} is ${version.join('.')}, need ${MIN_PYTHON.join('.')}+`)
234
+ }
235
+ if (!python) return fail(pythonHint())
236
+ logger.write(`bootstrapping venv with ${python} (${resolved.venvDir})`)
237
+ const created = await run(python, ['-m', 'venv', resolved.venvDir], { timeoutMs: 120000 })
238
+ if (created.code !== 0) {
239
+ logger.write(created.output)
240
+ return fail('创建 Python 虚拟环境失败,请确认已安装 python3-venv(macOS 上通常随 Xcode 命令行工具提供)')
241
+ }
242
+ const installed = await run(venvPythonPath, ['-m', 'pip', 'install', '--no-input', '-r', resolved.requirements], {
243
+ cwd: resolved.engineRoot,
244
+ timeoutMs: 600000,
245
+ })
246
+ if (installed.code !== 0) {
247
+ logger.write(installed.output)
248
+ return fail('安装引擎依赖失败。请检查网络或代理后重试;也可以手动把依赖装进该虚拟环境。')
249
+ }
250
+ fs.mkdirSync(path.dirname(resolved.stampPath), { recursive: true })
251
+ fs.writeFileSync(resolved.stampPath, JSON.stringify({
252
+ requirements: digest,
253
+ python: (await pythonVersion(venvPythonPath))?.join('.') || '',
254
+ at: new Date().toISOString(),
255
+ }, null, 2))
256
+ logger.write('venv ready')
257
+ return { ok: true, python: venvPythonPath, source: 'venv', engineRoot: resolved.engineRoot, moduleRoot: resolved.moduleRoot, logs: logger.tail() }
258
+ }
259
+
260
+ /** Stable id for one engine launch, so the page can spot a replacement. */
261
+ export function launchId() {
262
+ return crypto.randomBytes(6).toString('hex')
263
+ }
264
+
265
+ export { sleep }
package/host/proxy.js ADDED
@@ -0,0 +1,142 @@
1
+ import http from 'node:http'
2
+ import { execFile } from 'node:child_process'
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+ import process from 'node:process'
6
+ import { URL } from 'node:url'
7
+
8
+ const ALLOWED_METHODS = new Set(['GET', 'POST', 'PATCH', 'DELETE', 'PUT', 'OPTIONS'])
9
+
10
+ function writeJson(res, status, body) {
11
+ const text = JSON.stringify(body)
12
+ res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
13
+ res.end(text)
14
+ }
15
+
16
+ function tokenFrom(req, url) {
17
+ const header = req.headers['x-adplus-token']
18
+ if (typeof header === 'string' && header) return header
19
+ return url.searchParams.get('token') || ''
20
+ }
21
+
22
+ function readBody(req, limit = 1_000_000) {
23
+ return new Promise((resolve, reject) => {
24
+ let size = 0
25
+ const chunks = []
26
+ req.on('data', (chunk) => {
27
+ size += chunk.length
28
+ if (size > limit) { reject(new Error('request too large')); return }
29
+ chunks.push(chunk)
30
+ })
31
+ req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
32
+ req.on('error', reject)
33
+ })
34
+ }
35
+
36
+ export function registerProxy(ctx, supervisor, dsh) {
37
+ const handler = async (req, res) => {
38
+ if (!ALLOWED_METHODS.has(req.method || 'GET')) {
39
+ writeJson(res, 405, { error: { code: 'method_not_allowed' } })
40
+ return
41
+ }
42
+ const url = new URL(req.url || '/', supervisor.url || 'http://127.0.0.1')
43
+ if (url.pathname === '/adplus/health') {
44
+ writeJson(res, 200, { ok: true, engine: supervisor.snapshot ? supervisor.snapshot() : supervisor.status })
45
+ return
46
+ }
47
+ if (!url.pathname.startsWith('/adplus/api') && !url.pathname.startsWith('/adplus/media')) {
48
+ writeJson(res, 404, { error: { code: 'not_found' } })
49
+ return
50
+ }
51
+ const token = tokenFrom(req, url)
52
+ const remote = req.socket?.remoteAddress || ''
53
+ const loopback = remote === '127.0.0.1' || remote === '::1' || remote === '::ffff:127.0.0.1'
54
+ // The page normally carries the injected token. Allow a loopback request
55
+ // without one so a freshly installed bundle works before the page refresh;
56
+ // a wrong token is always rejected.
57
+ if (token && supervisor.token && token !== supervisor.token) {
58
+ writeJson(res, 401, { error: { code: 'unauthorized', message: 'refresh the page to refresh the Ad+ token' } })
59
+ return
60
+ }
61
+ if (!token && !loopback) {
62
+ writeJson(res, 401, { error: { code: 'unauthorized', message: 'loopback or token required' } })
63
+ return
64
+ }
65
+ if (url.pathname === '/adplus/api/engine/restart' && req.method === 'POST') {
66
+ // Answers before the restart finishes: preparing a missing runtime can
67
+ // take minutes, and the page already polls /adplus/health for progress.
68
+ void supervisor.restart()
69
+ writeJson(res, 202, { ok: true, restarting: true, engine: supervisor.status })
70
+ return
71
+ }
72
+ if (!supervisor.status?.ok) {
73
+ writeJson(res, 503, { error: { code: 'engine_unavailable', message: supervisor.status?.detail || 'engine unavailable', logs: supervisor.status?.logs || [] } })
74
+ return
75
+ }
76
+ if (url.pathname === '/adplus/api/reveal' && req.method === 'POST') {
77
+ try {
78
+ const body = JSON.parse(await readBody(req) || '{}')
79
+ const dataDir = path.resolve(supervisor.dataDir || '')
80
+ const rel = String(body.path || '').replace(/^\/+/, '')
81
+ const target = path.resolve(dataDir, rel)
82
+ if (!dataDir || (target !== dataDir && !target.startsWith(dataDir + path.sep))) {
83
+ writeJson(res, 400, { error: { code: 'bad_path', message: 'path outside data directory' } })
84
+ return
85
+ }
86
+ if (!fs.existsSync(target)) {
87
+ writeJson(res, 404, { error: { code: 'not_found', message: 'path not found' } })
88
+ return
89
+ }
90
+ const args = process.platform === 'darwin' ? (fs.statSync(target).isDirectory() ? [target] : ['-R', target]) : []
91
+ if (process.platform === 'darwin') execFile('open', args, () => {})
92
+ else if (process.platform === 'win32') execFile('explorer.exe', ['/select,', target], () => {})
93
+ else execFile('xdg-open', [fs.statSync(target).isDirectory() ? target : path.dirname(target)], () => {})
94
+ writeJson(res, 200, { ok: true })
95
+ } catch (error) {
96
+ writeJson(res, 500, { error: { code: 'reveal_failed', message: String(error?.message || error) } })
97
+ }
98
+ return
99
+ }
100
+ if (url.pathname === '/adplus/api/dsh/models') {
101
+ try {
102
+ writeJson(res, 200, { dsh: await dsh.list() })
103
+ } catch (error) {
104
+ writeJson(res, 500, { error: { code: 'dsh_models_failed', message: String(error?.message || error) } })
105
+ }
106
+ return
107
+ }
108
+ if (url.pathname === '/adplus/api/dsh/chat' && req.method === 'POST') {
109
+ try {
110
+ const body = JSON.parse(await readBody(req) || '{}')
111
+ const result = await dsh.chat(body)
112
+ writeJson(res, 200, { dsh: result })
113
+ } catch (error) {
114
+ writeJson(res, 500, { error: { code: 'dsh_chat_failed', message: String(error?.message || error) } })
115
+ }
116
+ return
117
+ }
118
+ let targetPath = url.pathname
119
+ if (targetPath.startsWith('/adplus/media')) {
120
+ targetPath = '/media'
121
+ } else {
122
+ targetPath = targetPath.slice('/adplus/api'.length) || '/'
123
+ }
124
+ const targetUrl = new URL(supervisor.url)
125
+ const options = {
126
+ hostname: targetUrl.hostname,
127
+ port: targetUrl.port,
128
+ path: targetPath + (url.search ? url.search : ''),
129
+ method: req.method,
130
+ headers: { ...req.headers, host: `${targetUrl.hostname}:${targetUrl.port}` },
131
+ }
132
+ const upstream = http.request(options, (up) => {
133
+ res.writeHead(up.statusCode || 502, up.headers)
134
+ up.pipe(res)
135
+ })
136
+ upstream.on('error', (error) => {
137
+ writeJson(res, 502, { error: { code: 'engine_proxy_failed', message: String(error?.message || error) } })
138
+ })
139
+ req.pipe(upstream)
140
+ }
141
+ return ctx.webServer.register({ kind: 'prefix', path: '/adplus', handler })
142
+ }
@@ -0,0 +1,251 @@
1
+ /** Own the Python sidecar: prepare its interpreter, launch it, watch it, stop it.
2
+ *
3
+ * The plugin runs on machines that never saw this checkout, so nothing here
4
+ * may assume a developer path. `host/engine.js` resolves the bundled sources
5
+ * and builds a private venv under the data directory on first launch; this
6
+ * class only spends that answer.
7
+ */
8
+ import { spawn } from 'node:child_process'
9
+ import http from 'node:http'
10
+ import crypto from 'node:crypto'
11
+ import process from 'node:process'
12
+ import { defaultDataDir, ensureRuntime, freePort, sleep } from './engine.js'
13
+
14
+ export { defaultDataDir }
15
+
16
+ function getJson(url, timeoutMs = 1500) {
17
+ if (!url) return Promise.resolve({ ok: false, status: 0 })
18
+ return new Promise((resolve) => {
19
+ const req = http.get(url, { timeout: timeoutMs }, (res) => {
20
+ let body = ''
21
+ res.setEncoding('utf8')
22
+ res.on('data', (chunk) => { body += chunk })
23
+ res.on('end', () => {
24
+ try { resolve({ ok: res.statusCode >= 200 && res.statusCode < 300, data: JSON.parse(body), status: res.statusCode }) }
25
+ catch { resolve({ ok: false, status: res.statusCode }) }
26
+ })
27
+ })
28
+ req.on('timeout', () => { req.destroy(); resolve({ ok: false, status: 0 }) })
29
+ req.on('error', () => resolve({ ok: false, status: 0 }))
30
+ })
31
+ }
32
+
33
+ function killTree(child, signal) {
34
+ if (!child || child.exitCode !== null || child.signalCode !== null) return
35
+ if (process.platform === 'win32') {
36
+ try { spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' }) } catch { /* already gone */ }
37
+ return
38
+ }
39
+ try { process.kill(-child.pid, signal) } catch {
40
+ try { child.kill(signal) } catch { /* already gone */ }
41
+ }
42
+ }
43
+
44
+ export class EngineSupervisor {
45
+ constructor(config = {}, options = {}) {
46
+ this.config = config || {}
47
+ this.pluginRoot = options.pluginRoot || null
48
+ this.dataDir = this.config.dataDir || defaultDataDir()
49
+ const fixed = Number(this.config.port)
50
+ this.fixedPort = Number.isInteger(fixed) && fixed > 0 && fixed < 65536 ? fixed : 0
51
+ this.port = this.fixedPort
52
+ this.autoStart = this.config.autoStart !== false
53
+ this.token = crypto.randomBytes(24).toString('hex')
54
+ this.child = null
55
+ this.python = null
56
+ this.engineRoot = null
57
+ this.moduleRoot = null
58
+ this.stopping = false
59
+ this.restarts = 0
60
+ this.failures = 0
61
+ this.logs = []
62
+ this.watchTimer = null
63
+ this.running = null
64
+ this.boot = { stage: 'idle', detail: '', logs: [], source: '' }
65
+ this.status = { ok: false, detail: 'not started', url: this.url, boot: this.boot }
66
+ }
67
+
68
+ get url() { return this.port ? `http://127.0.0.1:${this.port}` : '' }
69
+
70
+ log(line) {
71
+ this.logs.push(`[${new Date().toISOString()}] ${line}`)
72
+ if (this.logs.length > 200) this.logs.splice(0, this.logs.length - 200)
73
+ }
74
+
75
+ /** One object the page can render without guessing. */
76
+ snapshot() {
77
+ return {
78
+ ...this.status,
79
+ url: this.url,
80
+ port: this.port,
81
+ python: this.python,
82
+ engineRoot: this.engineRoot,
83
+ moduleRoot: this.moduleRoot,
84
+ boot: this.boot,
85
+ logs: (this.status.logs || this.logs).slice(-40),
86
+ }
87
+ }
88
+
89
+ async health() {
90
+ return getJson(this.url ? `${this.url}/health` : '', 1500)
91
+ }
92
+
93
+ setBoot(stage, detail, extra = {}) {
94
+ this.boot = { ...this.boot, stage, detail, ...extra }
95
+ this.status = { ...this.status, boot: this.boot }
96
+ }
97
+
98
+ /** Prepare, launch and confirm the engine. Safe to call repeatedly. */
99
+ async start() {
100
+ if (this.running) return this.running
101
+ this.running = this.#start().finally(() => { this.running = null })
102
+ return this.running
103
+ }
104
+
105
+ async #start() {
106
+ if (this.fixedPort) {
107
+ const existing = await this.health()
108
+ if (existing.ok && existing.data?.service === 'adclip') {
109
+ this.status = { ok: true, detail: 'reused', url: this.url, data: existing.data, boot: this.boot }
110
+ this.setBoot('ready', '已复用正在运行的引擎')
111
+ this.startWatch()
112
+ return
113
+ }
114
+ }
115
+ if (!this.autoStart) {
116
+ this.status = { ...this.status, ok: false, detail: 'autoStart disabled' }
117
+ this.setBoot('disabled', '配置里关闭了 autoStart')
118
+ return
119
+ }
120
+ this.parentStartedAt = Date.now()
121
+ this.setBoot('preparing', '准备 Python 运行时…')
122
+ const runtime = await ensureRuntime({ config: this.config, dataDir: this.dataDir, pluginRoot: this.pluginRoot })
123
+ if (!runtime.ok) {
124
+ this.status = { ok: false, detail: runtime.detail, url: this.url, logs: runtime.logs, boot: this.boot }
125
+ this.setBoot('failed', runtime.detail, { logs: runtime.logs })
126
+ this.log(`runtime unavailable: ${runtime.detail}`)
127
+ return
128
+ }
129
+ this.python = runtime.python
130
+ this.engineRoot = runtime.engineRoot
131
+ this.moduleRoot = runtime.moduleRoot || runtime.engineRoot
132
+ this.setBoot('preparing', `引擎运行时就绪(${runtime.source}),启动服务…`, { source: runtime.source })
133
+ this.port = this.fixedPort || await freePort()
134
+ this.status = { ok: false, detail: 'starting', url: this.url, boot: this.boot }
135
+ this.spawnEngine()
136
+ const deadline = Date.now() + 30000
137
+ while (Date.now() < deadline) {
138
+ if (this.stopping) return
139
+ const check = await this.health()
140
+ if (check.ok && check.data?.service === 'adclip') {
141
+ this.status = { ok: true, detail: 'started', url: this.url, data: check.data, boot: this.boot }
142
+ this.setBoot('ready', '引擎已就绪')
143
+ this.log('engine healthy')
144
+ this.startWatch()
145
+ return
146
+ }
147
+ await sleep(400)
148
+ }
149
+ this.status = { ok: false, detail: '引擎启动超时', url: this.url, logs: this.logs.slice(-40), boot: this.boot }
150
+ this.setBoot('failed', '引擎启动超时', { logs: this.logs.slice(-40) })
151
+ this.log('engine startup timeout')
152
+ }
153
+
154
+ spawnEngine() {
155
+ if (this.child) return
156
+ const args = ['-m', 'adclip.api', '--host', '127.0.0.1', '--port', String(this.port), '--graceful-timeout', '5']
157
+ this.log(`spawn ${this.python} ${args.join(' ')} (cwd ${this.moduleRoot})`)
158
+ const child = spawn(this.python, args, {
159
+ cwd: this.moduleRoot,
160
+ env: {
161
+ ...process.env,
162
+ PYTHONUNBUFFERED: '1',
163
+ ADSTUDIO_DATA_DIR: this.dataDir,
164
+ },
165
+ stdio: ['ignore', 'pipe', 'pipe'],
166
+ detached: process.platform !== 'win32',
167
+ })
168
+ this.child = child
169
+ child.stdout?.setEncoding('utf8')
170
+ child.stderr?.setEncoding('utf8')
171
+ child.stdout?.on('data', (chunk) => this.log(String(chunk).trim()))
172
+ child.stderr?.on('data', (chunk) => this.log(String(chunk).trim()))
173
+ child.on('error', (error) => this.log(`engine spawn failed: ${error?.message || error}`))
174
+ child.on('exit', (code, signal) => {
175
+ this.log(`engine exited code=${code} signal=${signal}`)
176
+ if (this.child === child) this.child = null
177
+ if (this.stopping) return
178
+ this.stopWatch()
179
+ this.status = { ok: false, detail: `engine exited (${code ?? signal})`, url: this.url, logs: this.logs.slice(-40), boot: this.boot }
180
+ this.setBoot('stopped', `引擎退出(${code ?? signal})`, { logs: this.logs.slice(-40) })
181
+ if (this.restarts < 2 && this.autoStart) {
182
+ this.restarts += 1
183
+ setTimeout(() => { void this.start() }, 1000)
184
+ }
185
+ })
186
+ }
187
+
188
+ /** Restart after a wedged child or a failed bootstrap. */
189
+ async restart() {
190
+ this.log('restart requested')
191
+ this.restarts = 0
192
+ const wasStopping = this.stopping
193
+ this.stopping = true
194
+ await this.killChild()
195
+ this.stopping = wasStopping
196
+ this.failures = 0
197
+ await this.start()
198
+ return this.snapshot()
199
+ }
200
+
201
+ async killChild() {
202
+ const child = this.child
203
+ this.child = null
204
+ if (!child) return
205
+ this.log('stopping engine')
206
+ killTree(child, 'SIGTERM')
207
+ const deadline = Date.now() + 5000
208
+ while (Date.now() < deadline && child.exitCode === null && child.signalCode === null) await sleep(100)
209
+ if (child.exitCode === null && child.signalCode === null) {
210
+ this.log('engine ignored SIGTERM, killing it')
211
+ killTree(child, 'SIGKILL')
212
+ }
213
+ }
214
+
215
+ startWatch() {
216
+ this.stopWatch()
217
+ this.failures = 0
218
+ this.watchTimer = setInterval(() => { void this.watchOnce() }, 15000)
219
+ this.watchTimer.unref?.()
220
+ }
221
+
222
+ async watchOnce() {
223
+ if (this.stopping || this.running) return
224
+ const check = await this.health()
225
+ if (check.ok) { this.failures = 0; return }
226
+ this.failures += 1
227
+ this.log(`health check failed (${this.failures}/3)`)
228
+ if (this.failures >= 3) {
229
+ this.failures = 0
230
+ if (this.restarts >= 4) {
231
+ this.setBoot('failed', '引擎反复无响应,已停止自动重启', { logs: this.logs.slice(-40) })
232
+ return
233
+ }
234
+ this.restarts += 1
235
+ this.log('watchdog restarting the engine')
236
+ await this.restart()
237
+ }
238
+ }
239
+
240
+ stopWatch() {
241
+ if (this.watchTimer) clearInterval(this.watchTimer)
242
+ this.watchTimer = null
243
+ }
244
+
245
+ async stop() {
246
+ this.stopping = true
247
+ this.stopWatch()
248
+ await this.killChild()
249
+ this.setBoot('stopped', '已停止')
250
+ }
251
+ }
package/icon.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <rect x="4" y="8" width="56" height="48" rx="10" fill="#111827"/>
3
+ <rect x="8" y="12" width="48" height="40" rx="7" fill="#1F2937" stroke="#60A5FA" stroke-width="2"/>
4
+ <path d="M26 23.5V44.5L44 34L26 23.5Z" fill="#60A5FA"/>
5
+ <path d="M12 47L22 39L29 44L36 38.5L52 51" stroke="#FBBF24" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/>
6
+ </svg>