xinghe-muse 0.0.0-stage → 2.2.2
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 +161 -2
- package/cordis.patch.yml +4 -0
- package/docs/DETAIL.md +159 -0
- package/icon.svg +14 -0
- package/install.ps1 +40 -0
- package/install.sh +27 -0
- package/lib/drive.mjs +94 -0
- package/lib/index.js +403 -0
- package/lib/platform.mjs +176 -0
- package/lib/run-muse.mjs +335 -0
- package/lib/setup.mjs +235 -0
- package/locale/en.json +8 -0
- package/locale/zh.json +8 -0
- package/package.json +42 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xinghe-muse —— 通过 ego lite 浏览器在 muse.ai 上出图、出视频。
|
|
3
|
+
*
|
|
4
|
+
* 作者:星禾
|
|
5
|
+
*
|
|
6
|
+
* 后台是一个真实浏览器:输入框、发送按钮、完成信号、下载按钮全都是网页元素,
|
|
7
|
+
* 直接读、直接点,图是下载下来的原始文件。所以不依赖屏幕,窗口被盖住、
|
|
8
|
+
* 最小化、别的程序抢焦点,都不影响它干活。
|
|
9
|
+
*
|
|
10
|
+
* 前提:ego lite 已安装,并在其中登录过 muse.ai。它不必先启动,窗口也不必可见。
|
|
11
|
+
*
|
|
12
|
+
* ── 写死的不变量(不可改):提示词原样递交 ──────────────────────────────
|
|
13
|
+
* 用户给的 prompt 一律原样递交 Muse:不 trim、不扩写、不改写、不加前后缀、不做任何变换。
|
|
14
|
+
* 只有真正没提供 prompt 时才回退到内置默认指令。
|
|
15
|
+
* 这条链路上 index.js / drive.mjs / run-muse.mjs 三处都必须守住,缺一处就不算写死。
|
|
16
|
+
* ──────────────────────────────────────────────────────────────────────
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { spawn } from 'node:child_process'
|
|
20
|
+
import { access, constants } from 'node:fs/promises'
|
|
21
|
+
import { dirname, join } from 'node:path'
|
|
22
|
+
import { fileURLToPath } from 'node:url'
|
|
23
|
+
|
|
24
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
defaultOutDir,
|
|
28
|
+
defaultVideoOutDir,
|
|
29
|
+
findEgoApp,
|
|
30
|
+
findEgoBrowser,
|
|
31
|
+
IS_MAC,
|
|
32
|
+
IS_WIN,
|
|
33
|
+
platformNote,
|
|
34
|
+
} from './platform.mjs'
|
|
35
|
+
|
|
36
|
+
export const name = 'xinghe-muse'
|
|
37
|
+
|
|
38
|
+
/** 只碰工具注册这一项宿主服务。 */
|
|
39
|
+
export const inject = ['tools']
|
|
40
|
+
|
|
41
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
42
|
+
const pkgRoot = join(here, '..')
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 跨平台驱动入口。
|
|
46
|
+
*
|
|
47
|
+
* 走 node 而不是 bash:Windows 上没有 bash,换成 Node 之后两边同一条路。
|
|
48
|
+
* 允许用 MUSE_DRIVER 覆盖,方便改过的副本单独指向。
|
|
49
|
+
*/
|
|
50
|
+
const NODE = process.execPath
|
|
51
|
+
const DRIVER = process.env.MUSE_DRIVER ?? join(pkgRoot, 'lib', 'drive.mjs')
|
|
52
|
+
|
|
53
|
+
const OUT_DIR = process.env.MUSE_OUT_DIR ?? defaultOutDir()
|
|
54
|
+
const OUT_DIR_VIDEO = process.env.MUSE_OUT_DIR_VIDEO ?? defaultVideoOutDir()
|
|
55
|
+
|
|
56
|
+
/** 没给指令时用的默认产品图指令。 */
|
|
57
|
+
const DEFAULT_PROMPT = [
|
|
58
|
+
'生成一张产品图:一只极简陶瓷香薰灯,哑光白釉,圆润流线造型,木质底座。',
|
|
59
|
+
'纯色浅灰背景,柔和自然光,45度俯视,居中构图,高清写实,商品摄影风格。',
|
|
60
|
+
'画面中不要文字、不要水印。',
|
|
61
|
+
].join('')
|
|
62
|
+
|
|
63
|
+
/** 出图慢但有上限,驱动那边夹在同一个区间。 */
|
|
64
|
+
const MIN_WAIT = 60
|
|
65
|
+
const MAX_WAIT = 600
|
|
66
|
+
|
|
67
|
+
/** 视频要排队 + 逐帧渲染,比图片慢一个量级,单独给区间。 */
|
|
68
|
+
const MIN_WAIT_VIDEO = 60
|
|
69
|
+
const MAX_WAIT_VIDEO = 1800
|
|
70
|
+
|
|
71
|
+
/** 没给指令时用的默认产品视频指令。 */
|
|
72
|
+
const DEFAULT_VIDEO_PROMPT = [
|
|
73
|
+
'生成一段产品视频:一只极简陶瓷香薰灯,哑光白釉,圆润流线造型,木质底座,',
|
|
74
|
+
'放在浅灰色台面上。镜头缓慢环绕推进,柔和自然光,浅景深,胶片质感,5 秒左右。',
|
|
75
|
+
'画面中不要文字、不要水印、不要旁白字幕。',
|
|
76
|
+
].join('')
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 起驱动进程,收它那一行 JSON 结果。
|
|
80
|
+
*
|
|
81
|
+
* 驱动的进度走 stderr,所以 stdout 整体解析。进程非零退出但没有可解析的行时,
|
|
82
|
+
* 照样把抓到的输出报回去——失败的运行本身就是调用方需要的信息。
|
|
83
|
+
*/
|
|
84
|
+
function drive(args, timeoutMs) {
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
const child = spawn(NODE, [DRIVER, ...args], {
|
|
87
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
88
|
+
windowsHide: true,
|
|
89
|
+
})
|
|
90
|
+
let out = ''
|
|
91
|
+
let err = ''
|
|
92
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs)
|
|
93
|
+
child.stdout.on('data', (chunk) => { out += chunk.toString('utf8') })
|
|
94
|
+
child.stderr.on('data', (chunk) => { err += chunk.toString('utf8') })
|
|
95
|
+
child.on('close', (code) => {
|
|
96
|
+
clearTimeout(timer)
|
|
97
|
+
resolve({ code: code ?? -1, stdout: out.trim(), stderr: err.trim() })
|
|
98
|
+
})
|
|
99
|
+
child.on('error', (error) => {
|
|
100
|
+
clearTimeout(timer)
|
|
101
|
+
resolve({ code: -1, stdout: '', stderr: error.message })
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 从驱动输出里挑出结果行,容忍夹杂的日志。 */
|
|
107
|
+
function parseResult(stdout) {
|
|
108
|
+
for (const raw of stdout.split('\n')) {
|
|
109
|
+
const line = raw.trim()
|
|
110
|
+
if (!line.startsWith('{')) continue
|
|
111
|
+
try {
|
|
112
|
+
const value = JSON.parse(line)
|
|
113
|
+
if (typeof value === 'object' && value !== null && 'ok' in value) return value
|
|
114
|
+
} catch {
|
|
115
|
+
// 截断或带前缀的行不是结果,继续找。
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return undefined
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function driverReady() {
|
|
122
|
+
try {
|
|
123
|
+
await access(DRIVER, constants.R_OK)
|
|
124
|
+
return true
|
|
125
|
+
} catch {
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function apply(ctx) {
|
|
131
|
+
ctx.effect(() => () => {})
|
|
132
|
+
|
|
133
|
+
ctx.tools.register(defineTool({
|
|
134
|
+
name: 'muse_web_status',
|
|
135
|
+
description:
|
|
136
|
+
'体检:报告当前这台机器能不能通过 ego lite 浏览器驱动 muse.ai——驱动脚本在不在、'
|
|
137
|
+
+ 'ego lite 装没装、ego-browser 能不能执行、生成的文件落到哪个目录。'
|
|
138
|
+
+ '出图出视频前先调它,能一眼看出环境齐不齐。',
|
|
139
|
+
parameters: {},
|
|
140
|
+
output: {
|
|
141
|
+
schema: {
|
|
142
|
+
type: 'object',
|
|
143
|
+
additionalProperties: false,
|
|
144
|
+
properties: {
|
|
145
|
+
platform: { type: 'string', required: true },
|
|
146
|
+
driver: { type: 'string', required: true },
|
|
147
|
+
egoBrowser: { type: 'string', required: true },
|
|
148
|
+
egoApp: { type: 'string', required: true },
|
|
149
|
+
outDir: { type: 'string', required: true },
|
|
150
|
+
outDirVideo: { type: 'string', required: true },
|
|
151
|
+
ready: { type: 'boolean', required: true },
|
|
152
|
+
summary: { type: 'string', required: true },
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
156
|
+
},
|
|
157
|
+
async execute() {
|
|
158
|
+
const ready = await driverReady()
|
|
159
|
+
const egoBrowser = await findEgoBrowser()
|
|
160
|
+
const egoApp = await findEgoApp()
|
|
161
|
+
|
|
162
|
+
const blocked = []
|
|
163
|
+
if (!ready) blocked.push(`驱动脚本找不到:${DRIVER}`)
|
|
164
|
+
if (!egoApp) blocked.push(`没装 ego lite(${platformNote()})`)
|
|
165
|
+
if (!egoBrowser) {
|
|
166
|
+
blocked.push('找不到 ego-browser 可执行文件;装在非默认位置时用环境变量 EGO_BROWSER_BIN 指过去')
|
|
167
|
+
}
|
|
168
|
+
if (!IS_MAC && !IS_WIN) blocked.push(platformNote())
|
|
169
|
+
|
|
170
|
+
const summary = blocked.length === 0
|
|
171
|
+
? `就绪。通过 ego lite 驱动 muse.ai,图片存到 ${OUT_DIR},`
|
|
172
|
+
+ `视频存到 ${OUT_DIR_VIDEO}。`
|
|
173
|
+
+ 'ego lite 不必先打开、窗口也不必可见,但必须已在里面登录 muse.ai。'
|
|
174
|
+
: `还不能用:${blocked.join(';')}。`
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
platform: process.platform,
|
|
178
|
+
driver: DRIVER,
|
|
179
|
+
egoBrowser: egoBrowser ?? '',
|
|
180
|
+
egoApp: egoApp ?? '',
|
|
181
|
+
outDir: OUT_DIR,
|
|
182
|
+
outDirVideo: OUT_DIR_VIDEO,
|
|
183
|
+
ready: blocked.length === 0,
|
|
184
|
+
summary,
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
}))
|
|
188
|
+
|
|
189
|
+
ctx.tools.register(defineTool({
|
|
190
|
+
name: 'muse_generate_image',
|
|
191
|
+
description:
|
|
192
|
+
'在 ego lite 浏览器里打开 muse.ai,新建一条旁聊发出出图指令,等这一轮跑完,'
|
|
193
|
+
+ '把生成的图片下载下来存进 muse 保存目录。'
|
|
194
|
+
+ '走的是网页,不点屏幕,所以不需要有窗口可见,也不会被别的程序抢焦点打断。'
|
|
195
|
+
+ '前提是 ego lite 已安装并已登录 muse.ai;不确定就先调 muse_web_status。',
|
|
196
|
+
parameters: {
|
|
197
|
+
prompt: {
|
|
198
|
+
type: 'string',
|
|
199
|
+
description:
|
|
200
|
+
'给 Muse 的指令。原文一个字不改地递交,不 trim、不扩写、不加前后缀。'
|
|
201
|
+
+ '不填才用内置的产品摄影指令。',
|
|
202
|
+
},
|
|
203
|
+
nameHint: {
|
|
204
|
+
type: 'string',
|
|
205
|
+
description: '存盘文件名的基名,不带扩展名。默认用 Muse 给这张图的标题,取不到就退回通用名。',
|
|
206
|
+
},
|
|
207
|
+
outDir: {
|
|
208
|
+
type: 'string',
|
|
209
|
+
description: `存到哪个文件夹,默认 ${OUT_DIR}。`,
|
|
210
|
+
},
|
|
211
|
+
timeoutSeconds: {
|
|
212
|
+
type: 'number',
|
|
213
|
+
description:
|
|
214
|
+
'给 Muse 多少秒。一轮通常 20-90 秒,给足余量更稳。'
|
|
215
|
+
+ `夹在 ${MIN_WAIT}-${MAX_WAIT} 之间,默认 180。`,
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
output: {
|
|
219
|
+
schema: {
|
|
220
|
+
type: 'object',
|
|
221
|
+
additionalProperties: false,
|
|
222
|
+
properties: {
|
|
223
|
+
ok: { type: 'boolean', required: true },
|
|
224
|
+
prompt: { type: 'string', required: true },
|
|
225
|
+
image: { type: 'string' },
|
|
226
|
+
bytes: { type: 'number' },
|
|
227
|
+
caption: { type: 'string' },
|
|
228
|
+
reply: { type: 'string', required: true },
|
|
229
|
+
threadUrl: { type: 'string', required: true },
|
|
230
|
+
detail: { type: 'string', required: true },
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
render: (_args, v) => [{
|
|
234
|
+
type: 'text',
|
|
235
|
+
text: v.ok
|
|
236
|
+
? `已保存 ${v.image}(${v.bytes ?? 0} 字节)。\nMuse 说:${v.reply || '(没有文字)'}\n`
|
|
237
|
+
+ `旁聊:${v.threadUrl}`
|
|
238
|
+
: `没出图:${v.detail}`,
|
|
239
|
+
}],
|
|
240
|
+
},
|
|
241
|
+
async execute(args) {
|
|
242
|
+
// 写死:给到就原样用(含首尾空白),一个字都不动;只有没给才回退默认。
|
|
243
|
+
const prompt = typeof args.prompt === 'string' ? args.prompt : DEFAULT_PROMPT
|
|
244
|
+
const outDir = args.outDir ?? OUT_DIR
|
|
245
|
+
const nameHint = args.nameHint ?? ''
|
|
246
|
+
const waitSeconds = Math.min(Math.max(args.timeoutSeconds ?? 180, MIN_WAIT), MAX_WAIT)
|
|
247
|
+
|
|
248
|
+
if (!(await driverReady())) {
|
|
249
|
+
return {
|
|
250
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
251
|
+
detail: `驱动入口找不到:${DRIVER}`,
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const egoBrowser = await findEgoBrowser()
|
|
256
|
+
if (!egoBrowser) {
|
|
257
|
+
return {
|
|
258
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
259
|
+
detail: '找不到 ego-browser 可执行文件。先装 ego lite;'
|
|
260
|
+
+ '装在非默认位置时用环境变量 EGO_BROWSER_BIN 指到它的绝对路径。',
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 驱动自身的上限是等待时间加上固定开销(开页、下载、收尾),
|
|
265
|
+
// 这里再宽一点,只有浏览器彻底卡死才会触发。
|
|
266
|
+
const result = await drive(
|
|
267
|
+
[prompt, outDir, String(waitSeconds), nameHint, 'image'],
|
|
268
|
+
(waitSeconds + 300) * 1000,
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
const parsed = parseResult(result.stdout)
|
|
272
|
+
if (parsed === undefined) {
|
|
273
|
+
return {
|
|
274
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
275
|
+
detail: '驱动没给出结果。'
|
|
276
|
+
+ `退出码=${result.code};stderr=${result.stderr.slice(-600) || '(空)'}`,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return {
|
|
281
|
+
ok: parsed.ok === true,
|
|
282
|
+
prompt,
|
|
283
|
+
...parsed.ok === true
|
|
284
|
+
? { image: parsed.media ?? parsed.image ?? '', bytes: parsed.bytes ?? 0, caption: parsed.caption ?? '' }
|
|
285
|
+
: {},
|
|
286
|
+
reply: parsed.reply ?? '',
|
|
287
|
+
threadUrl: parsed.threadUrl ?? '',
|
|
288
|
+
detail: parsed.detail || (parsed.ok === true
|
|
289
|
+
? `已保存到 ${parsed.media ?? parsed.image}。`
|
|
290
|
+
: '这一轮结束了但没有存下文件。'),
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
}))
|
|
294
|
+
|
|
295
|
+
ctx.tools.register(defineTool({
|
|
296
|
+
name: 'muse_generate_video',
|
|
297
|
+
description:
|
|
298
|
+
'在 ego lite 浏览器里打开 muse.ai,新建一条旁聊发出生成视频的指令,等这一轮跑完,'
|
|
299
|
+
+ '把生成的视频下载下来存进 muse 保存目录。'
|
|
300
|
+
+ '和出图走同一条网页路径,不录屏、不点屏幕,所以不需要窗口可见。'
|
|
301
|
+
+ '视频比图片慢得多,通常一到几分钟;不确定就先调 muse_web_status。',
|
|
302
|
+
parameters: {
|
|
303
|
+
prompt: {
|
|
304
|
+
type: 'string',
|
|
305
|
+
description:
|
|
306
|
+
'给 Muse 的视频指令。原文一个字不改地递交,不 trim、不扩写、不加前后缀。'
|
|
307
|
+
+ '不填才用内置的产品视频指令。',
|
|
308
|
+
},
|
|
309
|
+
nameHint: {
|
|
310
|
+
type: 'string',
|
|
311
|
+
description: '存盘文件名的基名,不带扩展名。默认用 Muse 给这段视频的标题,取不到就退回通用名。',
|
|
312
|
+
},
|
|
313
|
+
outDir: {
|
|
314
|
+
type: 'string',
|
|
315
|
+
description: `存到哪个文件夹,默认 ${OUT_DIR_VIDEO}。`,
|
|
316
|
+
},
|
|
317
|
+
timeoutSeconds: {
|
|
318
|
+
type: 'number',
|
|
319
|
+
description:
|
|
320
|
+
'给 Muse 多少秒。视频比图片慢,一轮通常 60-300 秒,给足余量更稳。'
|
|
321
|
+
+ `夹在 ${MIN_WAIT_VIDEO}-${MAX_WAIT_VIDEO} 之间,默认 420。`,
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
output: {
|
|
325
|
+
schema: {
|
|
326
|
+
type: 'object',
|
|
327
|
+
additionalProperties: false,
|
|
328
|
+
properties: {
|
|
329
|
+
ok: { type: 'boolean', required: true },
|
|
330
|
+
prompt: { type: 'string', required: true },
|
|
331
|
+
video: { type: 'string' },
|
|
332
|
+
bytes: { type: 'number' },
|
|
333
|
+
caption: { type: 'string' },
|
|
334
|
+
reply: { type: 'string', required: true },
|
|
335
|
+
threadUrl: { type: 'string', required: true },
|
|
336
|
+
detail: { type: 'string', required: true },
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
render: (_args, v) => [{
|
|
340
|
+
type: 'text',
|
|
341
|
+
text: v.ok
|
|
342
|
+
? `已保存 ${v.video}(${v.bytes ?? 0} 字节)。\nMuse 说:${v.reply || '(没有文字)'}\n`
|
|
343
|
+
+ `旁聊:${v.threadUrl}`
|
|
344
|
+
: `没出视频:${v.detail}`,
|
|
345
|
+
}],
|
|
346
|
+
},
|
|
347
|
+
async execute(args) {
|
|
348
|
+
// 写死:给到就原样用(含首尾空白),一个字都不动;只有没给才回退默认。
|
|
349
|
+
const prompt = typeof args.prompt === 'string' ? args.prompt : DEFAULT_VIDEO_PROMPT
|
|
350
|
+
const outDir = args.outDir ?? OUT_DIR_VIDEO
|
|
351
|
+
const nameHint = args.nameHint ?? ''
|
|
352
|
+
const waitSeconds = Math.min(
|
|
353
|
+
Math.max(args.timeoutSeconds ?? 420, MIN_WAIT_VIDEO),
|
|
354
|
+
MAX_WAIT_VIDEO,
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
if (!(await driverReady())) {
|
|
358
|
+
return {
|
|
359
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
360
|
+
detail: `驱动入口找不到:${DRIVER}`,
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const egoBrowser = await findEgoBrowser()
|
|
365
|
+
if (!egoBrowser) {
|
|
366
|
+
return {
|
|
367
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
368
|
+
detail: '找不到 ego-browser 可执行文件。先装 ego lite;'
|
|
369
|
+
+ '装在非默认位置时用环境变量 EGO_BROWSER_BIN 指到它的绝对路径。',
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// 视频的下载本身也可能慢(文件大),固定开销给宽一些。
|
|
374
|
+
const result = await drive(
|
|
375
|
+
[prompt, outDir, String(waitSeconds), nameHint, 'video'],
|
|
376
|
+
(waitSeconds + 420) * 1000,
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
const parsed = parseResult(result.stdout)
|
|
380
|
+
if (parsed === undefined) {
|
|
381
|
+
return {
|
|
382
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
383
|
+
detail: '驱动没给出结果。'
|
|
384
|
+
+ `退出码=${result.code};stderr=${result.stderr.slice(-600) || '(空)'}`,
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const media = parsed.media ?? parsed.video ?? parsed.image ?? ''
|
|
389
|
+
return {
|
|
390
|
+
ok: parsed.ok === true,
|
|
391
|
+
prompt,
|
|
392
|
+
...parsed.ok === true
|
|
393
|
+
? { video: media, bytes: parsed.bytes ?? 0, caption: parsed.caption ?? '' }
|
|
394
|
+
: {},
|
|
395
|
+
reply: parsed.reply ?? '',
|
|
396
|
+
threadUrl: parsed.threadUrl ?? '',
|
|
397
|
+
detail: parsed.detail || (parsed.ok === true
|
|
398
|
+
? `已保存到 ${media}。`
|
|
399
|
+
: '这一轮结束了但没有存下文件。'),
|
|
400
|
+
}
|
|
401
|
+
},
|
|
402
|
+
}))
|
|
403
|
+
}
|
package/lib/platform.mjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xinghe-muse — 跨平台定位 ego lite 与 ego-browser。
|
|
3
|
+
*
|
|
4
|
+
* 作者:星禾
|
|
5
|
+
*
|
|
6
|
+
* macOS 与 Windows 的安装落点不一样,升级后版本号目录也会变,所以这里不写死
|
|
7
|
+
* 任何一条绝对路径,而是按「环境变量 → 已知安装根目录 → PATH」的顺序逐个试,
|
|
8
|
+
* 拿到第一个真正能执行的文件就用它。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { access, constants, readdir, realpath } from 'node:fs/promises'
|
|
12
|
+
import { homedir, platform } from 'node:os'
|
|
13
|
+
import { delimiter, join } from 'node:path'
|
|
14
|
+
|
|
15
|
+
export const IS_WIN = platform() === 'win32'
|
|
16
|
+
export const IS_MAC = platform() === 'darwin'
|
|
17
|
+
|
|
18
|
+
/** 显式覆盖:设了就只认这个。 */
|
|
19
|
+
const ENV_BIN = process.env.EGO_BROWSER_BIN
|
|
20
|
+
/** 显式指定 ego lite 安装根目录。 */
|
|
21
|
+
const ENV_APP = process.env.EGO_LITE_APP
|
|
22
|
+
|
|
23
|
+
async function exists(target) {
|
|
24
|
+
try {
|
|
25
|
+
await access(target, constants.F_OK)
|
|
26
|
+
return true
|
|
27
|
+
} catch {
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function runs(target) {
|
|
33
|
+
try {
|
|
34
|
+
await access(target, IS_WIN ? constants.F_OK : constants.X_OK)
|
|
35
|
+
return true
|
|
36
|
+
} catch {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 平台约定的 ego lite 安装根目录候选。 */
|
|
42
|
+
function appCandidates() {
|
|
43
|
+
if (IS_MAC) {
|
|
44
|
+
return [
|
|
45
|
+
ENV_APP,
|
|
46
|
+
'/Applications/ego lite.app',
|
|
47
|
+
join(homedir(), 'Applications', 'ego lite.app'),
|
|
48
|
+
].filter(Boolean)
|
|
49
|
+
}
|
|
50
|
+
if (IS_WIN) {
|
|
51
|
+
const local = process.env.LOCALAPPDATA ?? join(homedir(), 'AppData', 'Local')
|
|
52
|
+
const prog = process.env.ProgramFiles ?? 'C:\\Program Files'
|
|
53
|
+
const prog86 = process.env['ProgramFiles(x86)'] ?? 'C:\\Program Files (x86)'
|
|
54
|
+
return [
|
|
55
|
+
ENV_APP,
|
|
56
|
+
join(local, 'Programs', 'ego lite'),
|
|
57
|
+
join(local, 'ego lite'),
|
|
58
|
+
join(prog, 'ego lite'),
|
|
59
|
+
join(prog86, 'ego lite'),
|
|
60
|
+
].filter(Boolean)
|
|
61
|
+
}
|
|
62
|
+
return []
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* macOS:ego-browser 的真实文件在 .app 里的版本目录下。
|
|
67
|
+
*
|
|
68
|
+
* ~/.local/share/ego/active_version_dir 是一个指向当前版本目录的软链,
|
|
69
|
+
* 升级后由 ego 自己改写,所以优先读它,读不到再枚举 Versions 目录。
|
|
70
|
+
*/
|
|
71
|
+
async function macHelperPaths(appRoot) {
|
|
72
|
+
const framework = join(appRoot, 'Contents', 'Frameworks', 'ego Framework.framework')
|
|
73
|
+
const out = []
|
|
74
|
+
|
|
75
|
+
const active = join(homedir(), '.local', 'share', 'ego', 'active_version_dir')
|
|
76
|
+
if (await exists(active)) {
|
|
77
|
+
try {
|
|
78
|
+
out.push(join(await realpath(active), 'Helpers', 'ego-browser'))
|
|
79
|
+
} catch {
|
|
80
|
+
// 软链指向的目标已经不在了,落到下面的枚举。
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const versions = join(framework, 'Versions')
|
|
85
|
+
try {
|
|
86
|
+
const entries = await readdir(versions, { withFileTypes: true })
|
|
87
|
+
for (const entry of entries) {
|
|
88
|
+
if (entry.isDirectory()) out.push(join(versions, entry.name, 'Helpers', 'ego-browser'))
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
// 没有 Versions 目录就用下面的兜底路径。
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
out.push(join(framework, 'Versions', 'Current', 'Helpers', 'ego-browser'))
|
|
95
|
+
return out
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Windows:ego-browser 通常与主程序同目录,偶尔在 bin 或 helpers 子目录。 */
|
|
99
|
+
function winHelperPaths(appRoot) {
|
|
100
|
+
return [
|
|
101
|
+
join(appRoot, 'ego-browser.exe'),
|
|
102
|
+
join(appRoot, 'ego-browser.cmd'),
|
|
103
|
+
join(appRoot, 'bin', 'ego-browser.exe'),
|
|
104
|
+
join(appRoot, 'bin', 'ego-browser.cmd'),
|
|
105
|
+
join(appRoot, 'helpers', 'ego-browser.exe'),
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** PATH 里逐个目录找。 */
|
|
110
|
+
function pathCandidates() {
|
|
111
|
+
const names = IS_WIN ? ['ego-browser.exe', 'ego-browser.cmd', 'ego-browser'] : ['ego-browser']
|
|
112
|
+
const dirs = (process.env.PATH ?? '').split(delimiter).filter(Boolean)
|
|
113
|
+
const out = []
|
|
114
|
+
for (const dir of dirs) {
|
|
115
|
+
for (const name of names) out.push(join(dir, name))
|
|
116
|
+
}
|
|
117
|
+
return out
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 找到可用的 ego-browser。
|
|
122
|
+
*
|
|
123
|
+
* @returns {Promise<string|null>} 绝对路径;找不到时为 null。
|
|
124
|
+
*/
|
|
125
|
+
export async function findEgoBrowser() {
|
|
126
|
+
if (ENV_BIN) return (await runs(ENV_BIN)) ? ENV_BIN : null
|
|
127
|
+
|
|
128
|
+
for (const appRoot of appCandidates()) {
|
|
129
|
+
const helpers = IS_MAC ? await macHelperPaths(appRoot) : winHelperPaths(appRoot)
|
|
130
|
+
for (const candidate of helpers) {
|
|
131
|
+
if (await runs(candidate)) return candidate
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (const candidate of pathCandidates()) {
|
|
136
|
+
if (await runs(candidate)) return candidate
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return null
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** ego lite 是否已安装(只判断安装物在不在,不判断能否运行)。 */
|
|
143
|
+
export async function findEgoApp() {
|
|
144
|
+
for (const appRoot of appCandidates()) {
|
|
145
|
+
if (await exists(appRoot)) return appRoot
|
|
146
|
+
}
|
|
147
|
+
return null
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** 给人看的一段说明,报告平台与找不到时该往哪看。 */
|
|
151
|
+
export function platformNote() {
|
|
152
|
+
if (IS_MAC) return 'macOS:ego lite 装在 /Applications/ego lite.app'
|
|
153
|
+
if (IS_WIN) return 'Windows:ego lite 装在 %LOCALAPPDATA%\\Programs\\ego lite'
|
|
154
|
+
return `当前平台 ${platform()} 不在支持范围内(仅 macOS 与 Windows)`
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 桌面目录,跨平台。 */
|
|
158
|
+
export function desktopDir() {
|
|
159
|
+
const homeDesktop = join(homedir(), 'Desktop')
|
|
160
|
+
return homeDesktop
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** muse 相关产物统一收在桌面 muse 目录下。 */
|
|
164
|
+
export function museRootDir() {
|
|
165
|
+
return join(desktopDir(), 'muse')
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 默认保存目录:图片。 */
|
|
169
|
+
export function defaultOutDir() {
|
|
170
|
+
return join(museRootDir(), 'muse保存图')
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 默认保存目录:视频。 */
|
|
174
|
+
export function defaultVideoOutDir() {
|
|
175
|
+
return join(museRootDir(), 'muse保存视频')
|
|
176
|
+
}
|