xinghe-muse 0.0.0-stage → 2.3.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 +184 -2
- package/cordis.patch.yml +4 -0
- package/docs/DETAIL.md +198 -0
- package/icon.svg +14 -0
- package/install.ps1 +40 -0
- package/install.sh +27 -0
- package/lib/drive.mjs +104 -0
- package/lib/index.js +624 -0
- package/lib/platform.mjs +176 -0
- package/lib/run-muse.mjs +565 -0
- package/lib/setup.mjs +235 -0
- package/lib/stitch.swift +89 -0
- package/locale/en.json +8 -0
- package/locale/zh.json +8 -0
- package/package.json +43 -4
package/lib/index.js
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xinghe-muse —— 通过 ego lite 浏览器在 muse.ai 上出图、出视频。
|
|
3
|
+
*
|
|
4
|
+
* 作者:星禾
|
|
5
|
+
*
|
|
6
|
+
* 后台是一个真实浏览器:输入框、发送按钮、完成信号、下载按钮全都是网页元素,
|
|
7
|
+
* 直接读、直接点,图是下载下来的原始文件。所以不依赖屏幕,窗口被盖住、
|
|
8
|
+
* 最小化、别的程序抢焦点,都不影响它干活。
|
|
9
|
+
*
|
|
10
|
+
* 前提:ego lite 已安装,并在其中登录过 muse.ai。它不必先启动,窗口也不必可见。
|
|
11
|
+
*
|
|
12
|
+
* ── 参考素材(v2.3.0)──────────────────────────────────────────────────
|
|
13
|
+
* 用户给的参考视频、参考图,原样挂到 muse.ai 的输入框上,和提示词一起递交。
|
|
14
|
+
* 用法:muse_generate_video({ reference: '/abs/a.mp4', prompt: '...' }),
|
|
15
|
+
* 多个素材用换行分隔。挂载走页面里的 input[type=file],不点屏幕。
|
|
16
|
+
* ──────────────────────────────────────────────────────────────────────
|
|
17
|
+
*
|
|
18
|
+
* ── 写死的不变量(不可改):提示词原样递交 ──────────────────────────────
|
|
19
|
+
* 用户给的 prompt 一律原样递交 Muse:不 trim、不扩写、不改写、不加前后缀、不做任何变换。
|
|
20
|
+
* 只有真正没提供 prompt 时才回退到内置默认指令。
|
|
21
|
+
* 这条链路上 index.js / drive.mjs / run-muse.mjs 三处都必须守住,缺一处就不算写死。
|
|
22
|
+
* ──────────────────────────────────────────────────────────────────────
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { spawn } from 'node:child_process'
|
|
26
|
+
import { homedir } from 'node:os'
|
|
27
|
+
import { tmpdir } from 'node:os'
|
|
28
|
+
import { access, constants } from 'node:fs/promises'
|
|
29
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path'
|
|
30
|
+
import { mkdir, stat } from 'node:fs/promises'
|
|
31
|
+
import { fileURLToPath } from 'node:url'
|
|
32
|
+
|
|
33
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
34
|
+
|
|
35
|
+
import {
|
|
36
|
+
defaultOutDir,
|
|
37
|
+
defaultVideoOutDir,
|
|
38
|
+
findEgoApp,
|
|
39
|
+
findEgoBrowser,
|
|
40
|
+
IS_MAC,
|
|
41
|
+
IS_WIN,
|
|
42
|
+
platformNote,
|
|
43
|
+
} from './platform.mjs'
|
|
44
|
+
|
|
45
|
+
export const name = 'xinghe-muse'
|
|
46
|
+
|
|
47
|
+
/** 只碰工具注册这一项宿主服务。 */
|
|
48
|
+
export const inject = ['tools']
|
|
49
|
+
|
|
50
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
51
|
+
const pkgRoot = join(here, '..')
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 跨平台驱动入口。
|
|
55
|
+
*
|
|
56
|
+
* 走 node 而不是 bash:Windows 上没有 bash,换成 Node 之后两边同一条路。
|
|
57
|
+
* 允许用 MUSE_DRIVER 覆盖,方便改过的副本单独指向。
|
|
58
|
+
*/
|
|
59
|
+
const NODE = process.execPath
|
|
60
|
+
const DRIVER = process.env.MUSE_DRIVER ?? join(pkgRoot, 'lib', 'drive.mjs')
|
|
61
|
+
|
|
62
|
+
const OUT_DIR = process.env.MUSE_OUT_DIR ?? defaultOutDir()
|
|
63
|
+
const OUT_DIR_VIDEO = process.env.MUSE_OUT_DIR_VIDEO ?? defaultVideoOutDir()
|
|
64
|
+
|
|
65
|
+
/** 没给指令时用的默认产品图指令。 */
|
|
66
|
+
const DEFAULT_PROMPT = [
|
|
67
|
+
'生成一张产品图:一只极简陶瓷香薰灯,哑光白釉,圆润流线造型,木质底座。',
|
|
68
|
+
'纯色浅灰背景,柔和自然光,45度俯视,居中构图,高清写实,商品摄影风格。',
|
|
69
|
+
'画面中不要文字、不要水印。',
|
|
70
|
+
].join('')
|
|
71
|
+
|
|
72
|
+
/** 出图慢但有上限,驱动那边夹在同一个区间。 */
|
|
73
|
+
const MIN_WAIT = 60
|
|
74
|
+
const MAX_WAIT = 600
|
|
75
|
+
|
|
76
|
+
/** 视频要排队 + 逐帧渲染,比图片慢一个量级,单独给区间。 */
|
|
77
|
+
const MIN_WAIT_VIDEO = 60
|
|
78
|
+
const MAX_WAIT_VIDEO = 1800
|
|
79
|
+
|
|
80
|
+
/** 没给指令时用的默认产品视频指令。 */
|
|
81
|
+
const DEFAULT_VIDEO_PROMPT = [
|
|
82
|
+
'生成一段产品视频:一只极简陶瓷香薰灯,哑光白釉,圆润流线造型,木质底座,',
|
|
83
|
+
'放在浅灰色台面上。镜头缓慢环绕推进,柔和自然光,浅景深,胶片质感,5 秒左右。',
|
|
84
|
+
'画面中不要文字、不要水印、不要旁白字幕。',
|
|
85
|
+
].join('')
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 起驱动进程,收它那一行 JSON 结果。
|
|
89
|
+
*
|
|
90
|
+
* 驱动的进度走 stderr,所以 stdout 整体解析。进程非零退出但没有可解析的行时,
|
|
91
|
+
* 照样把抓到的输出报回去——失败的运行本身就是调用方需要的信息。
|
|
92
|
+
*/
|
|
93
|
+
function drive(args, timeoutMs) {
|
|
94
|
+
return new Promise((resolve) => {
|
|
95
|
+
const child = spawn(NODE, [DRIVER, ...args], {
|
|
96
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
97
|
+
windowsHide: true,
|
|
98
|
+
})
|
|
99
|
+
let out = ''
|
|
100
|
+
let err = ''
|
|
101
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs)
|
|
102
|
+
child.stdout.on('data', (chunk) => { out += chunk.toString('utf8') })
|
|
103
|
+
child.stderr.on('data', (chunk) => { err += chunk.toString('utf8') })
|
|
104
|
+
child.on('close', (code) => {
|
|
105
|
+
clearTimeout(timer)
|
|
106
|
+
resolve({ code: code ?? -1, stdout: out.trim(), stderr: err.trim() })
|
|
107
|
+
})
|
|
108
|
+
child.on('error', (error) => {
|
|
109
|
+
clearTimeout(timer)
|
|
110
|
+
resolve({ code: -1, stdout: '', stderr: error.message })
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 从驱动输出里挑出结果行,容忍夹杂的日志。 */
|
|
116
|
+
function parseResult(stdout) {
|
|
117
|
+
for (const raw of stdout.split('\n')) {
|
|
118
|
+
const line = raw.trim()
|
|
119
|
+
if (!line.startsWith('{')) continue
|
|
120
|
+
try {
|
|
121
|
+
const value = JSON.parse(line)
|
|
122
|
+
if (typeof value === 'object' && value !== null && 'ok' in value) return value
|
|
123
|
+
} catch {
|
|
124
|
+
// 截断或带前缀的行不是结果,继续找。
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return undefined
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 把 reference 参数拆成绝对路径数组。
|
|
132
|
+
*
|
|
133
|
+
* 一个字段里允许塞多个:换行、分号、竖线都当分隔符,路径中间的空格不拆(微信那种带空格的名字很常见)。
|
|
134
|
+
* 相对路径按当前工作目录补全,其余一个字不动。
|
|
135
|
+
*/
|
|
136
|
+
function parseRefs(value) {
|
|
137
|
+
if (typeof value !== 'string' || value.trim() === '') return []
|
|
138
|
+
return value
|
|
139
|
+
.split(/[\r\n;|]+/)
|
|
140
|
+
.map((one) => one.trim())
|
|
141
|
+
.filter((one) => one !== '')
|
|
142
|
+
.map((one) => (isAbsolute(one) ? one : resolve(one)))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function driverReady() {
|
|
146
|
+
try {
|
|
147
|
+
await access(DRIVER, constants.R_OK)
|
|
148
|
+
return true
|
|
149
|
+
} catch {
|
|
150
|
+
return false
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function apply(ctx) {
|
|
155
|
+
ctx.effect(() => () => {})
|
|
156
|
+
|
|
157
|
+
ctx.tools.register(defineTool({
|
|
158
|
+
name: 'muse_web_status',
|
|
159
|
+
description:
|
|
160
|
+
'体检:报告当前这台机器能不能通过 ego lite 浏览器驱动 muse.ai——驱动脚本在不在、'
|
|
161
|
+
+ 'ego lite 装没装、ego-browser 能不能执行、生成的文件落到哪个目录。'
|
|
162
|
+
+ '出图出视频前先调它,能一眼看出环境齐不齐。',
|
|
163
|
+
parameters: {},
|
|
164
|
+
output: {
|
|
165
|
+
schema: {
|
|
166
|
+
type: 'object',
|
|
167
|
+
additionalProperties: false,
|
|
168
|
+
properties: {
|
|
169
|
+
platform: { type: 'string', required: true },
|
|
170
|
+
driver: { type: 'string', required: true },
|
|
171
|
+
egoBrowser: { type: 'string', required: true },
|
|
172
|
+
egoApp: { type: 'string', required: true },
|
|
173
|
+
outDir: { type: 'string', required: true },
|
|
174
|
+
outDirVideo: { type: 'string', required: true },
|
|
175
|
+
ready: { type: 'boolean', required: true },
|
|
176
|
+
summary: { type: 'string', required: true },
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
render: (_args, v) => [{ type: 'text', text: v.summary }],
|
|
180
|
+
},
|
|
181
|
+
async execute() {
|
|
182
|
+
const ready = await driverReady()
|
|
183
|
+
const egoBrowser = await findEgoBrowser()
|
|
184
|
+
const egoApp = await findEgoApp()
|
|
185
|
+
|
|
186
|
+
const blocked = []
|
|
187
|
+
if (!ready) blocked.push(`驱动脚本找不到:${DRIVER}`)
|
|
188
|
+
if (!egoApp) blocked.push(`没装 ego lite(${platformNote()})`)
|
|
189
|
+
if (!egoBrowser) {
|
|
190
|
+
blocked.push('找不到 ego-browser 可执行文件;装在非默认位置时用环境变量 EGO_BROWSER_BIN 指过去')
|
|
191
|
+
}
|
|
192
|
+
if (!IS_MAC && !IS_WIN) blocked.push(platformNote())
|
|
193
|
+
|
|
194
|
+
const summary = blocked.length === 0
|
|
195
|
+
? `就绪。通过 ego lite 驱动 muse.ai,图片存到 ${OUT_DIR},`
|
|
196
|
+
+ `视频存到 ${OUT_DIR_VIDEO}。`
|
|
197
|
+
+ 'ego lite 不必先打开、窗口也不必可见,但必须已在里面登录 muse.ai。'
|
|
198
|
+
: `还不能用:${blocked.join(';')}。`
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
platform: process.platform,
|
|
202
|
+
driver: DRIVER,
|
|
203
|
+
egoBrowser: egoBrowser ?? '',
|
|
204
|
+
egoApp: egoApp ?? '',
|
|
205
|
+
outDir: OUT_DIR,
|
|
206
|
+
outDirVideo: OUT_DIR_VIDEO,
|
|
207
|
+
ready: blocked.length === 0,
|
|
208
|
+
summary,
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
}))
|
|
212
|
+
|
|
213
|
+
ctx.tools.register(defineTool({
|
|
214
|
+
name: 'muse_generate_image',
|
|
215
|
+
description:
|
|
216
|
+
'在 ego lite 浏览器里打开 muse.ai,新建一条旁聊发出出图指令,等这一轮跑完,'
|
|
217
|
+
+ '把生成的图片下载下来存进 muse 保存目录。'
|
|
218
|
+
+ '走的是网页,不点屏幕,所以不需要有窗口可见,也不会被别的程序抢焦点打断。'
|
|
219
|
+
+ '前提是 ego lite 已安装并已登录 muse.ai;不确定就先调 muse_web_status。',
|
|
220
|
+
parameters: {
|
|
221
|
+
prompt: {
|
|
222
|
+
type: 'string',
|
|
223
|
+
description:
|
|
224
|
+
'给 Muse 的指令。原文一个字不改地递交,不 trim、不扩写、不加前后缀。'
|
|
225
|
+
+ '不填才用内置的产品摄影指令。',
|
|
226
|
+
},
|
|
227
|
+
reference: {
|
|
228
|
+
type: 'string',
|
|
229
|
+
description:
|
|
230
|
+
'参考素材的绝对路径:参考视频(.mp4/.mov/.webm)或参考图(.png/.jpg/.webp)都行,'
|
|
231
|
+
+ '多个用换行分隔。给到就先把素材挂到输入框上,再递提示词;'
|
|
232
|
+
+ '要复刻某条视频的镜头、人物、场景时,把那条视频传给它。',
|
|
233
|
+
},
|
|
234
|
+
nameHint: {
|
|
235
|
+
type: 'string',
|
|
236
|
+
description: '存盘文件名的基名,不带扩展名。默认用 Muse 给这张图的标题,取不到就退回通用名。',
|
|
237
|
+
},
|
|
238
|
+
outDir: {
|
|
239
|
+
type: 'string',
|
|
240
|
+
description: `存到哪个文件夹,默认 ${OUT_DIR}。`,
|
|
241
|
+
},
|
|
242
|
+
timeoutSeconds: {
|
|
243
|
+
type: 'number',
|
|
244
|
+
description:
|
|
245
|
+
'给 Muse 多少秒。一轮通常 20-90 秒,给足余量更稳。'
|
|
246
|
+
+ `夹在 ${MIN_WAIT}-${MAX_WAIT} 之间,默认 180。`,
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
output: {
|
|
250
|
+
schema: {
|
|
251
|
+
type: 'object',
|
|
252
|
+
additionalProperties: false,
|
|
253
|
+
properties: {
|
|
254
|
+
ok: { type: 'boolean', required: true },
|
|
255
|
+
prompt: { type: 'string', required: true },
|
|
256
|
+
image: { type: 'string' },
|
|
257
|
+
bytes: { type: 'number' },
|
|
258
|
+
caption: { type: 'string' },
|
|
259
|
+
reply: { type: 'string', required: true },
|
|
260
|
+
threadUrl: { type: 'string', required: true },
|
|
261
|
+
references: { type: 'array', items: { type: 'string' } },
|
|
262
|
+
detail: { type: 'string', required: true },
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
render: (_args, v) => [{
|
|
266
|
+
type: 'text',
|
|
267
|
+
text: v.ok
|
|
268
|
+
? `已保存 ${v.image}(${v.bytes ?? 0} 字节)。\nMuse 说:${v.reply || '(没有文字)'}\n`
|
|
269
|
+
+ `旁聊:${v.threadUrl}`
|
|
270
|
+
: `没出图:${v.detail}`,
|
|
271
|
+
}],
|
|
272
|
+
},
|
|
273
|
+
async execute(args) {
|
|
274
|
+
// 写死:给到就原样用(含首尾空白),一个字都不动;只有没给才回退默认。
|
|
275
|
+
const prompt = typeof args.prompt === 'string' ? args.prompt : DEFAULT_PROMPT
|
|
276
|
+
const refs = parseRefs(args.reference)
|
|
277
|
+
const outDir = args.outDir ?? OUT_DIR
|
|
278
|
+
const nameHint = args.nameHint ?? ''
|
|
279
|
+
const waitSeconds = Math.min(Math.max(args.timeoutSeconds ?? 180, MIN_WAIT), MAX_WAIT)
|
|
280
|
+
|
|
281
|
+
if (!(await driverReady())) {
|
|
282
|
+
return {
|
|
283
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
284
|
+
detail: `驱动入口找不到:${DRIVER}`,
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const egoBrowser = await findEgoBrowser()
|
|
289
|
+
if (!egoBrowser) {
|
|
290
|
+
return {
|
|
291
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
292
|
+
detail: '找不到 ego-browser 可执行文件。先装 ego lite;'
|
|
293
|
+
+ '装在非默认位置时用环境变量 EGO_BROWSER_BIN 指到它的绝对路径。',
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// 驱动自身的上限是等待时间加上固定开销(开页、下载、收尾),
|
|
298
|
+
// 这里再宽一点,只有浏览器彻底卡死才会触发。
|
|
299
|
+
const result = await drive(
|
|
300
|
+
[prompt, outDir, String(waitSeconds), nameHint, 'image', JSON.stringify(refs)],
|
|
301
|
+
(waitSeconds + 300) * 1000,
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
const parsed = parseResult(result.stdout)
|
|
305
|
+
if (parsed === undefined) {
|
|
306
|
+
return {
|
|
307
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
308
|
+
detail: '驱动没给出结果。'
|
|
309
|
+
+ `退出码=${result.code};stderr=${result.stderr.slice(-600) || '(空)'}`,
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
ok: parsed.ok === true,
|
|
315
|
+
prompt,
|
|
316
|
+
...parsed.ok === true
|
|
317
|
+
? { image: parsed.media ?? parsed.image ?? '', bytes: parsed.bytes ?? 0, caption: parsed.caption ?? '' }
|
|
318
|
+
: {},
|
|
319
|
+
reply: parsed.reply ?? '',
|
|
320
|
+
threadUrl: parsed.threadUrl ?? '',
|
|
321
|
+
references: refs,
|
|
322
|
+
detail: parsed.detail || (parsed.ok === true
|
|
323
|
+
? `已保存到 ${parsed.media ?? parsed.image}。`
|
|
324
|
+
: '这一轮结束了但没有存下文件。'),
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
}))
|
|
328
|
+
|
|
329
|
+
ctx.tools.register(defineTool({
|
|
330
|
+
name: 'muse_generate_video',
|
|
331
|
+
description:
|
|
332
|
+
'在 ego lite 浏览器里打开 muse.ai,新建一条旁聊发出生成视频的指令,等这一轮跑完,'
|
|
333
|
+
+ '把生成的视频下载下来存进 muse 保存目录。'
|
|
334
|
+
+ '和出图走同一条网页路径,不录屏、不点屏幕,所以不需要窗口可见。'
|
|
335
|
+
+ '视频比图片慢得多,通常一到几分钟;不确定就先调 muse_web_status。',
|
|
336
|
+
parameters: {
|
|
337
|
+
prompt: {
|
|
338
|
+
type: 'string',
|
|
339
|
+
description:
|
|
340
|
+
'给 Muse 的视频指令。原文一个字不改地递交,不 trim、不扩写、不加前后缀。'
|
|
341
|
+
+ '不填才用内置的产品视频指令。',
|
|
342
|
+
},
|
|
343
|
+
reference: {
|
|
344
|
+
type: 'string',
|
|
345
|
+
description:
|
|
346
|
+
'参考素材的绝对路径:参考视频(.mp4/.mov/.webm)或参考图(.png/.jpg/.webp)都行,'
|
|
347
|
+
+ '多个用换行分隔。给到就先把素材挂到输入框上,再递提示词;'
|
|
348
|
+
+ '要复刻某条视频的镜头、人物、场景时,把那条视频传给它。',
|
|
349
|
+
},
|
|
350
|
+
nameHint: {
|
|
351
|
+
type: 'string',
|
|
352
|
+
description: '存盘文件名的基名,不带扩展名。默认用 Muse 给这段视频的标题,取不到就退回通用名。',
|
|
353
|
+
},
|
|
354
|
+
outDir: {
|
|
355
|
+
type: 'string',
|
|
356
|
+
description: `存到哪个文件夹,默认 ${OUT_DIR_VIDEO}。`,
|
|
357
|
+
},
|
|
358
|
+
timeoutSeconds: {
|
|
359
|
+
type: 'number',
|
|
360
|
+
description:
|
|
361
|
+
'给 Muse 多少秒。视频比图片慢,一轮通常 60-300 秒,给足余量更稳。'
|
|
362
|
+
+ `夹在 ${MIN_WAIT_VIDEO}-${MAX_WAIT_VIDEO} 之间,默认 420。`,
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
output: {
|
|
366
|
+
schema: {
|
|
367
|
+
type: 'object',
|
|
368
|
+
additionalProperties: false,
|
|
369
|
+
properties: {
|
|
370
|
+
ok: { type: 'boolean', required: true },
|
|
371
|
+
prompt: { type: 'string', required: true },
|
|
372
|
+
video: { type: 'string' },
|
|
373
|
+
bytes: { type: 'number' },
|
|
374
|
+
caption: { type: 'string' },
|
|
375
|
+
reply: { type: 'string', required: true },
|
|
376
|
+
threadUrl: { type: 'string', required: true },
|
|
377
|
+
references: { type: 'array', items: { type: 'string' } },
|
|
378
|
+
detail: { type: 'string', required: true },
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
render: (_args, v) => [{
|
|
382
|
+
type: 'text',
|
|
383
|
+
text: v.ok
|
|
384
|
+
? `已保存 ${v.video}(${v.bytes ?? 0} 字节)。\nMuse 说:${v.reply || '(没有文字)'}\n`
|
|
385
|
+
+ `旁聊:${v.threadUrl}`
|
|
386
|
+
: `没出视频:${v.detail}`,
|
|
387
|
+
}],
|
|
388
|
+
},
|
|
389
|
+
async execute(args) {
|
|
390
|
+
// 写死:给到就原样用(含首尾空白),一个字都不动;只有没给才回退默认。
|
|
391
|
+
const prompt = typeof args.prompt === 'string' ? args.prompt : DEFAULT_VIDEO_PROMPT
|
|
392
|
+
const refs = parseRefs(args.reference)
|
|
393
|
+
const outDir = args.outDir ?? OUT_DIR_VIDEO
|
|
394
|
+
const nameHint = args.nameHint ?? ''
|
|
395
|
+
const waitSeconds = Math.min(
|
|
396
|
+
Math.max(args.timeoutSeconds ?? 420, MIN_WAIT_VIDEO),
|
|
397
|
+
MAX_WAIT_VIDEO,
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
if (!(await driverReady())) {
|
|
401
|
+
return {
|
|
402
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
403
|
+
detail: `驱动入口找不到:${DRIVER}`,
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const egoBrowser = await findEgoBrowser()
|
|
408
|
+
if (!egoBrowser) {
|
|
409
|
+
return {
|
|
410
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
411
|
+
detail: '找不到 ego-browser 可执行文件。先装 ego lite;'
|
|
412
|
+
+ '装在非默认位置时用环境变量 EGO_BROWSER_BIN 指到它的绝对路径。',
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// 视频的下载本身也可能慢(文件大),固定开销给宽一些。
|
|
417
|
+
const result = await drive(
|
|
418
|
+
[prompt, outDir, String(waitSeconds), nameHint, 'video', JSON.stringify(refs)],
|
|
419
|
+
(waitSeconds + 420) * 1000,
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
const parsed = parseResult(result.stdout)
|
|
423
|
+
if (parsed === undefined) {
|
|
424
|
+
return {
|
|
425
|
+
ok: false, prompt, reply: '', threadUrl: '',
|
|
426
|
+
detail: '驱动没给出结果。'
|
|
427
|
+
+ `退出码=${result.code};stderr=${result.stderr.slice(-600) || '(空)'}`,
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const media = parsed.media ?? parsed.video ?? parsed.image ?? ''
|
|
432
|
+
return {
|
|
433
|
+
ok: parsed.ok === true,
|
|
434
|
+
prompt,
|
|
435
|
+
...parsed.ok === true
|
|
436
|
+
? { video: media, bytes: parsed.bytes ?? 0, caption: parsed.caption ?? '' }
|
|
437
|
+
: {},
|
|
438
|
+
reply: parsed.reply ?? '',
|
|
439
|
+
threadUrl: parsed.threadUrl ?? '',
|
|
440
|
+
references: refs,
|
|
441
|
+
detail: parsed.detail || (parsed.ok === true
|
|
442
|
+
? `已保存到 ${media}。`
|
|
443
|
+
: '这一轮结束了但没有存下文件。'),
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** 叠化拼接用的编译缓存目录:编译一次,之后直接跑。 */
|
|
447
|
+
const STITCH_CACHE = join(homedir(), '.cache', 'xinghe-muse')
|
|
448
|
+
/** ~/.cache 不可写(沙箱、只读家目录)时退到系统临时目录,别让拼接为了个缓存目录就废掉。 */
|
|
449
|
+
const STITCH_CACHE_FALLBACK = join(tmpdir(), 'xinghe-muse')
|
|
450
|
+
const STITCH_SWIFT = join(pkgRoot, 'lib', 'stitch.swift')
|
|
451
|
+
|
|
452
|
+
/** 跑一个子进程,收退出码和输出。 */
|
|
453
|
+
function run(cmd, args, env) {
|
|
454
|
+
return new Promise((done) => {
|
|
455
|
+
const child = spawn(cmd, args, {
|
|
456
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
457
|
+
windowsHide: true,
|
|
458
|
+
env: { ...process.env, ...(env ?? {}) },
|
|
459
|
+
})
|
|
460
|
+
let out = ''
|
|
461
|
+
let err = ''
|
|
462
|
+
child.stdout.on('data', (c) => { out += c.toString('utf8') })
|
|
463
|
+
child.stderr.on('data', (c) => { err += c.toString('utf8') })
|
|
464
|
+
child.on('error', (e) => done({ code: -1, out, err: err + e.message }))
|
|
465
|
+
child.on('close', (code) => done({ code: code ?? -1, out, err }))
|
|
466
|
+
})
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* 拿到可用的拼接器。
|
|
471
|
+
*
|
|
472
|
+
* macOS 走 AVFoundation(swiftc 编一次,缓存在 ~/.cache/xinghe-muse);
|
|
473
|
+
* 其它平台走 ffmpeg + ffprobe,两个都得在 PATH 上。
|
|
474
|
+
*/
|
|
475
|
+
async function stitcher() {
|
|
476
|
+
if (process.platform === 'darwin') {
|
|
477
|
+
let lastError = ''
|
|
478
|
+
for (const cache of [STITCH_CACHE, STITCH_CACHE_FALLBACK]) {
|
|
479
|
+
const bin = join(cache, 'stitch')
|
|
480
|
+
let fresh = false
|
|
481
|
+
try {
|
|
482
|
+
const [b, sw] = await Promise.all([stat(bin), stat(STITCH_SWIFT)])
|
|
483
|
+
fresh = b.mtimeMs >= sw.mtimeMs
|
|
484
|
+
} catch {
|
|
485
|
+
fresh = false
|
|
486
|
+
}
|
|
487
|
+
if (!fresh) {
|
|
488
|
+
try {
|
|
489
|
+
await mkdir(cache, { recursive: true })
|
|
490
|
+
} catch (error) {
|
|
491
|
+
lastError = `缓存目录建不了:${cache}(${error.message})`
|
|
492
|
+
continue
|
|
493
|
+
}
|
|
494
|
+
const built = await run('swiftc', ['-O', STITCH_SWIFT, '-o', bin], {
|
|
495
|
+
CLANG_MODULE_CACHE_PATH: cache,
|
|
496
|
+
SWIFT_MODULECACHE_PATH: cache,
|
|
497
|
+
TMPDIR: cache,
|
|
498
|
+
})
|
|
499
|
+
if (built.code !== 0) {
|
|
500
|
+
lastError = `swiftc 编译拼接器失败:${built.err.slice(-400)}`
|
|
501
|
+
continue
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return { kind: 'swift', bin }
|
|
505
|
+
}
|
|
506
|
+
return { error: lastError || '拼接器编译不出来,装 Xcode 命令行工具后再试。' }
|
|
507
|
+
}
|
|
508
|
+
return { kind: 'ffmpeg', bin: 'ffmpeg' }
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** 两段叠化:一次只接一刀,多段按顺序两两接下去。 */
|
|
512
|
+
async function stitchOnce(kind, bin, a, b, out, overlap) {
|
|
513
|
+
if (kind === 'swift') {
|
|
514
|
+
const r = await run(bin, [out, a, b, String(overlap)])
|
|
515
|
+
return r.code === 0 ? undefined : `拼接失败:${(r.err || r.out).slice(-400)}`
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const probe = await run('ffprobe', [
|
|
519
|
+
'-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', a,
|
|
520
|
+
])
|
|
521
|
+
const durA = Number(probe.out.trim())
|
|
522
|
+
if (!Number.isFinite(durA) || durA <= 0) {
|
|
523
|
+
return '读不到第一段的时长,ffprobe 没给出数字;装好 ffmpeg 全套再试。'
|
|
524
|
+
}
|
|
525
|
+
const offset = Math.max(durA - overlap, 0)
|
|
526
|
+
const filter = `[0:v][1:v]xfade=transition=fade:duration=${overlap}:offset=${offset}[v];`
|
|
527
|
+
+ `[0:a][1:a]acrossfade=d=${overlap}[a]`
|
|
528
|
+
const r = await run(bin, [
|
|
529
|
+
'-y', '-i', a, '-i', b,
|
|
530
|
+
'-filter_complex', filter,
|
|
531
|
+
'-map', '[v]', '-map', '[a]',
|
|
532
|
+
'-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-crf', '18',
|
|
533
|
+
'-c:a', 'aac', '-b:a', '192k',
|
|
534
|
+
out,
|
|
535
|
+
])
|
|
536
|
+
return r.code === 0 ? undefined : `ffmpeg 拼接失败:${r.err.slice(-400)}`
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
}))
|
|
540
|
+
|
|
541
|
+
ctx.tools.register(defineTool({
|
|
542
|
+
name: 'muse_stitch_videos',
|
|
543
|
+
description:
|
|
544
|
+
'把 muse 分段生成的几段视频接成一条:默认 0.35 秒叠化(视频 xfade + 音频交叉淡化),'
|
|
545
|
+
+ '接缝看不出硬切、声音不打架。Muse 单条口型上限 10 秒,长口播就是分段生成再在这里接。'
|
|
546
|
+
+ 'macOS 用 AVFoundation(swiftc 编一次),其它平台要 ffmpeg + ffprobe。',
|
|
547
|
+
parameters: {
|
|
548
|
+
parts: {
|
|
549
|
+
type: 'string',
|
|
550
|
+
description: '按顺序排列的分段视频绝对路径,多个用换行分隔,至少两段。',
|
|
551
|
+
},
|
|
552
|
+
outFile: {
|
|
553
|
+
type: 'string',
|
|
554
|
+
description: '成品输出路径,给绝对路径,扩展名 .mp4。已存在会被覆盖。',
|
|
555
|
+
},
|
|
556
|
+
overlapSeconds: {
|
|
557
|
+
type: 'number',
|
|
558
|
+
description: '叠化时长,默认 0.35 秒。接缝那头是静音时 0.3–0.4 最稳,太短能看出跳。',
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
output: {
|
|
562
|
+
schema: {
|
|
563
|
+
type: 'object',
|
|
564
|
+
additionalProperties: false,
|
|
565
|
+
properties: {
|
|
566
|
+
ok: { type: 'boolean', required: true },
|
|
567
|
+
output: { type: 'string' },
|
|
568
|
+
bytes: { type: 'number' },
|
|
569
|
+
parts: { type: 'array', items: { type: 'string' } },
|
|
570
|
+
detail: { type: 'string', required: true },
|
|
571
|
+
},
|
|
572
|
+
},
|
|
573
|
+
render: (_args, v) => [{
|
|
574
|
+
type: 'text',
|
|
575
|
+
text: v.ok
|
|
576
|
+
? `已接成 ${v.output}(${v.bytes ?? 0} 字节,${(v.parts ?? []).length} 段)`
|
|
577
|
+
: `没接成:${v.detail}`,
|
|
578
|
+
}],
|
|
579
|
+
},
|
|
580
|
+
async execute(args) {
|
|
581
|
+
const parts = parseRefs(args.parts)
|
|
582
|
+
const overlap = Math.min(Math.max(args.overlapSeconds ?? 0.35, 0.05), 1.5)
|
|
583
|
+
const outFile = args.outFile ? (isAbsolute(args.outFile) ? args.outFile : resolve(args.outFile)) : ''
|
|
584
|
+
|
|
585
|
+
if (parts.length < 2) {
|
|
586
|
+
return { ok: false, detail: '至少要两段才谈得上接。', parts }
|
|
587
|
+
}
|
|
588
|
+
if (!outFile) {
|
|
589
|
+
return { ok: false, detail: '给一个成品输出路径(绝对路径,.mp4)。', parts }
|
|
590
|
+
}
|
|
591
|
+
for (const one of parts) {
|
|
592
|
+
try {
|
|
593
|
+
await stat(one)
|
|
594
|
+
} catch {
|
|
595
|
+
return { ok: false, detail: `这段找不到:${one}`, parts }
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
const engine = await stitcher()
|
|
600
|
+
if (engine.error) return { ok: false, detail: engine.error, parts }
|
|
601
|
+
|
|
602
|
+
// 多段:每次把「已接好的」和下一段接一刀,中间文件放临时目录。
|
|
603
|
+
const work = join(tmpdir(), `xinghe-muse-stitch-${Date.now()}`)
|
|
604
|
+
await mkdir(work, { recursive: true })
|
|
605
|
+
let current = parts[0]
|
|
606
|
+
for (let i = 1; i < parts.length; i += 1) {
|
|
607
|
+
const last = i === parts.length - 1
|
|
608
|
+
const target = last ? outFile : join(work, `step-${i}.mp4`)
|
|
609
|
+
const bad = await stitchOnce(engine.kind, engine.bin, current, parts[i], target, overlap)
|
|
610
|
+
if (bad) return { ok: false, detail: bad, parts }
|
|
611
|
+
current = target
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const info = await stat(outFile).catch(() => undefined)
|
|
615
|
+
return {
|
|
616
|
+
ok: true,
|
|
617
|
+
output: outFile,
|
|
618
|
+
bytes: info?.size ?? 0,
|
|
619
|
+
parts,
|
|
620
|
+
detail: `叠化 ${overlap}s,${parts.length} 段接成一条。`,
|
|
621
|
+
}
|
|
622
|
+
},
|
|
623
|
+
}))
|
|
624
|
+
}
|