dsh-workrally 0.2.5 → 0.3.1
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 +6 -3
- package/lib/account.js +18 -9
- package/lib/client.js +4063 -408
- package/lib/easy-writing-skills/book-creation.md +90 -0
- package/lib/easy-writing-skills/novel-writing.md +8 -0
- package/lib/easy-writing-skills/story-development.md +41 -0
- package/lib/easy-writing-skills/title-and-intro.md +67 -0
- package/lib/easyWriting.js +72 -0
- package/lib/gallery.js +110 -15
- package/lib/index.js +188 -20
- package/lib/manju-skills/chongsheng-manju/SKILL.md +311 -0
- package/lib/manju-skills/chuanyue-manju/SKILL.md +309 -0
- package/lib/manju-skills/dushi-manju/SKILL.md +311 -0
- package/lib/manju-skills/gongdou-manju/SKILL.md +312 -0
- package/lib/manju-skills/jiating-manju/SKILL.md +311 -0
- package/lib/manju-skills/minsu-manju/SKILL.md +313 -0
- package/lib/manju-skills/moshi-manju/SKILL.md +313 -0
- package/lib/manju-skills/niandai-manju/SKILL.md +311 -0
- package/lib/manju-skills/quanmou-manju/SKILL.md +311 -0
- package/lib/manju-skills/shangzhan-manju/SKILL.md +311 -0
- package/lib/manju-skills/tianchong-manju/SKILL.md +310 -0
- package/lib/manju-skills/wuxia-manju/SKILL.md +308 -0
- package/lib/manju-skills/xiaoyuan-manju/SKILL.md +312 -0
- package/lib/manju-skills/xihuan-manju/SKILL.md +312 -0
- package/lib/manju-skills/xingji-manju/SKILL.md +312 -0
- package/lib/manju-skills/xitong-manju/SKILL.md +310 -0
- package/lib/manju-skills/xuanhuan-manju/SKILL.md +306 -0
- package/lib/manju-skills/xuanyi-manju/SKILL.md +315 -0
- package/lib/manju-skills/yule-manju/SKILL.md +310 -0
- package/lib/manju-skills/zhaidou-manju/SKILL.md +311 -0
- package/lib/manju-skills/zhongtian-manju/SKILL.md +310 -0
- package/lib/manjuSkills.js +69 -0
- package/lib/mcp.js +402 -0
- package/lib/nvzhuang/anime.json +1 -0
- package/lib/nvzhuang/portrait.json +1 -0
- package/lib/nvzhuang/real.json +1 -0
- package/lib/nvzhuang.js +184 -0
- package/lib/openmotion-skills/openmotion/SKILL.md +33 -0
- package/lib/openmotion-skills/openmotion-director/SKILL.md +67 -0
- package/lib/openmotion-skills/openmotion-media/SKILL.md +42 -0
- package/lib/openmotion-skills/openmotion-production/SKILL.md +48 -0
- package/lib/openmotion-skills/openmotion-recreate/SKILL.md +91 -0
- package/lib/openmotion-skills/openmotion-template-to-video/SKILL.md +72 -0
- package/lib/openmotion-skills/openmotion-video-to-template/SKILL.md +44 -0
- package/lib/openmotion.js +128 -0
- package/lib/plaza.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
const HERE = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const DEFAULT_DIR = existsSync(join(HERE, 'manju-skills'))
|
|
7
|
+
? join(HERE, 'manju-skills')
|
|
8
|
+
: join(HERE, '..', 'assets', 'manju-skills');
|
|
9
|
+
|
|
10
|
+
export const MANJU_REPO = 'https://github.com/zhanghao123vip/ai-manju-skills';
|
|
11
|
+
|
|
12
|
+
export const MANJU_CATEGORIES = [
|
|
13
|
+
{ id: 'all', label: '全部', labelEn: 'All' },
|
|
14
|
+
{ id: 'fantasy', label: '幻想', labelEn: 'Fantasy' },
|
|
15
|
+
{ id: 'ancient', label: '古代', labelEn: 'Ancient' },
|
|
16
|
+
{ id: 'modern', label: '现代', labelEn: 'Modern' },
|
|
17
|
+
{ id: 'genre', label: '类型片', labelEn: 'Genre' },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export const MANJU_SKILLS = [
|
|
21
|
+
{ id: 'xuanhuan-manju', title: '玄幻修仙', category: 'fantasy', tag: '幻想', summary: '东方暗黑玄幻国漫、废柴逆袭、宗门试炼与大招节奏' },
|
|
22
|
+
{ id: 'xihuan-manju', title: '西幻魔法', category: 'fantasy', tag: '幻想', summary: '剑与魔法、学院试炼、王国纷争与史诗奇幻视觉' },
|
|
23
|
+
{ id: 'xingji-manju', title: '星际机甲', category: 'fantasy', tag: '幻想', summary: '太空歌剧、机甲激战、星舰舰队与硬核科幻分镜' },
|
|
24
|
+
{ id: 'xitong-manju', title: '系统无限流', category: 'fantasy', tag: '幻想', summary: '副本通关、面板数值、规则博弈与高能反转' },
|
|
25
|
+
{ id: 'moshi-manju', title: '末日废土', category: 'fantasy', tag: '幻想', summary: '丧尸灾变、庇护所求生、资源争夺与冷峻末世质感' },
|
|
26
|
+
{ id: 'chuanyue-manju', title: '穿越重生', category: 'ancient', tag: '古代', summary: '现代思维降维打击、逆天改命与古今反差爽感' },
|
|
27
|
+
{ id: 'gongdou-manju', title: '宫斗', category: 'ancient', tag: '古代', summary: '深宫步步惊心、微表情暗战、华服礼制与权柄博弈' },
|
|
28
|
+
{ id: 'zhaidou-manju', title: '宅斗', category: 'ancient', tag: '古代', summary: '高门大院、嫡庶纷争、掌家算计与情绪张力' },
|
|
29
|
+
{ id: 'quanmou-manju', title: '权谋朝堂', category: 'ancient', tag: '古代', summary: '庙堂风云、棋局博弈、连环反杀与沉稳电影镜头' },
|
|
30
|
+
{ id: 'wuxia-manju', title: '武侠江湖', category: 'ancient', tag: '古代', summary: '快意恩仇、门派纷争、刀光剑影与写意动作分镜' },
|
|
31
|
+
{ id: 'zhongtian-manju', title: '种田经商', category: 'ancient', tag: '古代', summary: '发家致富、市井烟火、基建经营与治愈成长线' },
|
|
32
|
+
{ id: 'dushi-manju', title: '都市逆袭', category: 'modern', tag: '现代', summary: '底层翻盘、打脸逆袭、快节奏情绪钩子与爽点拉满' },
|
|
33
|
+
{ id: 'tianchong-manju', title: '总裁甜宠', category: 'modern', tag: '现代', summary: '豪门甜虐、暧昧拉扯、高甜名场面与精致光影' },
|
|
34
|
+
{ id: 'jiating-manju', title: '家庭伦理', category: 'modern', tag: '现代', summary: '代际冲突、婚姻危机、现实共鸣与强情绪对白' },
|
|
35
|
+
{ id: 'chongsheng-manju', title: '重生复仇', category: 'modern', tag: '现代', summary: '前世惨死今生归来、精准设局、手撕仇人爽剧节奏' },
|
|
36
|
+
{ id: 'shangzhan-manju', title: '职场商战', category: 'modern', tag: '现代', summary: '资本博弈、职场晋升、谈判交锋与都市精英氛围' },
|
|
37
|
+
{ id: 'niandai-manju', title: '年代文', category: 'modern', tag: '现代', summary: '七八九十年代怀旧质感、时代浪潮与小人物奋斗' },
|
|
38
|
+
{ id: 'yule-manju', title: '娱乐圈', category: 'modern', tag: '现代', summary: '选秀出道、塌房反转、名利场暗战与高光舞台' },
|
|
39
|
+
{ id: 'xiaoyuan-manju', title: '大学校园青春', category: 'modern', tag: '现代', summary: '青涩暗恋、社团群像、成长遗憾与清透日系/国漫光影' },
|
|
40
|
+
{ id: 'xuanyi-manju', title: '悬疑刑侦', category: 'genre', tag: '类型片', summary: '连环迷案、线索推理、冷峻审讯与悬疑惊悚运镜' },
|
|
41
|
+
{ id: 'minsu-manju', title: '民俗怪谈', category: 'genre', tag: '类型片', summary: '中式民俗诡事、禁忌规则、志怪氛围与暗色调视听' },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
const BY_ID = new Map(MANJU_SKILLS.map(item => [item.id, item]));
|
|
45
|
+
const cache = new Map();
|
|
46
|
+
|
|
47
|
+
const stripFrontmatter = (markdown) => String(markdown || '').replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n*/, '').trim();
|
|
48
|
+
|
|
49
|
+
export const listManjuSkills = () => MANJU_SKILLS;
|
|
50
|
+
|
|
51
|
+
export const readManjuSkill = (id, baseDir = DEFAULT_DIR) => {
|
|
52
|
+
const meta = BY_ID.get(String(id || '').trim());
|
|
53
|
+
if (!meta) return null;
|
|
54
|
+
const key = `${baseDir}::${meta.id}`;
|
|
55
|
+
if (cache.has(key)) return cache.get(key);
|
|
56
|
+
const file = join(baseDir, meta.id, 'SKILL.md');
|
|
57
|
+
if (!existsSync(file)) return null;
|
|
58
|
+
const raw = readFileSync(file, 'utf8');
|
|
59
|
+
const content = stripFrontmatter(raw);
|
|
60
|
+
const result = {
|
|
61
|
+
...meta,
|
|
62
|
+
raw,
|
|
63
|
+
content,
|
|
64
|
+
chars: content.length,
|
|
65
|
+
repoUrl: `${MANJU_REPO}/blob/main/skills/${meta.id}/SKILL.md`,
|
|
66
|
+
};
|
|
67
|
+
cache.set(key, result);
|
|
68
|
+
return result;
|
|
69
|
+
};
|
package/lib/mcp.js
CHANGED
|
@@ -106,6 +106,81 @@ export const getConnection = async (ctx) => {
|
|
|
106
106
|
}
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
+
const CANVAS_PAGE = /https:\/\/workrally\.qq\.com\/toolbox\/canvas\/([\w-]{1,80})/;
|
|
110
|
+
|
|
111
|
+
/** 从新建画布的工具回执里取出编号和前端链接。链接必须落在 WorkRally 画布页。 */
|
|
112
|
+
export const parseCreatedCanvas = (result, fallbackTitle = '') => {
|
|
113
|
+
const normalized = normalizeToolResult(result);
|
|
114
|
+
if (normalized.error) throw new Error(normalized.error);
|
|
115
|
+
const value = result?.value ?? result;
|
|
116
|
+
const texts = [...textParts(result), ...textParts(value)];
|
|
117
|
+
let id = '';
|
|
118
|
+
let url = '';
|
|
119
|
+
let title = String(fallbackTitle || '').trim().slice(0, 80);
|
|
120
|
+
const takeUrl = (raw) => {
|
|
121
|
+
const match = String(raw).match(CANVAS_PAGE);
|
|
122
|
+
if (!match) return;
|
|
123
|
+
url = match[0];
|
|
124
|
+
if (!id) id = match[1];
|
|
125
|
+
};
|
|
126
|
+
const visit = (node, depth) => {
|
|
127
|
+
if (!node || typeof node !== 'object' || depth > 8) return;
|
|
128
|
+
if (Array.isArray(node)) {
|
|
129
|
+
node.forEach(entry => visit(entry, depth + 1));
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
for (const [key, val] of Object.entries(node)) {
|
|
133
|
+
if (typeof val === 'string') {
|
|
134
|
+
takeUrl(val);
|
|
135
|
+
if (!id && /^(?:canvas_?)?id$/i.test(key) && /^[\w-]{4,80}$/.test(val)) id = val;
|
|
136
|
+
if (!title && key === 'title' && val.trim()) title = val.trim().slice(0, 80);
|
|
137
|
+
} else if (val && typeof val === 'object') visit(val, depth + 1);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
normalized.payloads.forEach(payload => visit(payload, 0));
|
|
141
|
+
texts.forEach(takeUrl);
|
|
142
|
+
if (!id) throw new Error('新建画布没有返回编号');
|
|
143
|
+
if (!url) url = `https://workrally.qq.com/toolbox/canvas/${encodeURIComponent(id)}`;
|
|
144
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
145
|
+
return {
|
|
146
|
+
id,
|
|
147
|
+
title: title || '未命名画布',
|
|
148
|
+
url,
|
|
149
|
+
updated: today,
|
|
150
|
+
updatedAt: today,
|
|
151
|
+
images: 0,
|
|
152
|
+
videos: 0,
|
|
153
|
+
texts: 0,
|
|
154
|
+
thumb: '',
|
|
155
|
+
mine: true,
|
|
156
|
+
groupId: '',
|
|
157
|
+
groupName: '',
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** 在当前登录账号下新建一块无限画布,并返回可在面板内打开的链接。 */
|
|
162
|
+
export const createNativeCanvas = async (ctx, title, signal) => {
|
|
163
|
+
const definitions = await ctx?.tools?.definitions?.();
|
|
164
|
+
const defs = Array.isArray(definitions) ? definitions : [];
|
|
165
|
+
const manageDef = defs.map(tool => workrallyTool(tool?.name)).find(item => item?.action === 'canvas_manage');
|
|
166
|
+
if (!manageDef) throw new Error('尚未连接 WorkRally 账号,请先点击登录绑定你的账号');
|
|
167
|
+
if (typeof ctx?.tools?.execute !== 'function') throw new Error('DSH 原生工具执行服务不可用');
|
|
168
|
+
const name = String(title || '').trim().slice(0, 80) || `未命名画布_${new Date().toISOString().slice(0, 10).replace(/-/g, '')}`;
|
|
169
|
+
const id = randomUUID();
|
|
170
|
+
const response = await ctx.tools.execute({
|
|
171
|
+
calls: [{
|
|
172
|
+
type: 'tool-call',
|
|
173
|
+
id,
|
|
174
|
+
name: `mcp__${manageDef.serverName}__canvas_manage`,
|
|
175
|
+
arguments: JSON.stringify({ action: 'create', title: name }),
|
|
176
|
+
}],
|
|
177
|
+
signal: signal || AbortSignal.timeout(20_000),
|
|
178
|
+
});
|
|
179
|
+
const result = response?.results?.find(entry => entry.callId === id)?.result;
|
|
180
|
+
if (!result) throw new Error('新建画布没有返回结果');
|
|
181
|
+
return parseCreatedCanvas(result, name);
|
|
182
|
+
};
|
|
183
|
+
|
|
109
184
|
/** 通过已挂载的 WorkRally MCP 实时读取当前登录账号身份、项目分组与全量无限画布列表。 */
|
|
110
185
|
export const readNativeCanvases = async (ctx, signal) => {
|
|
111
186
|
const definitions = await ctx?.tools?.definitions?.();
|
|
@@ -275,3 +350,330 @@ export const readNativeCanvases = async (ctx, signal) => {
|
|
|
275
350
|
projects,
|
|
276
351
|
};
|
|
277
352
|
};
|
|
353
|
+
|
|
354
|
+
const ASSET_TYPES = new Set(['image', 'video', 'audio', '3d']);
|
|
355
|
+
|
|
356
|
+
const clipPrompt = (value) => (typeof value === 'string' ? value.trim().slice(0, 4000) : '');
|
|
357
|
+
|
|
358
|
+
const promptText = (node) => {
|
|
359
|
+
if (!node || typeof node !== 'object') return '';
|
|
360
|
+
const params = node.data?.params && typeof node.data.params === 'object'
|
|
361
|
+
? node.data.params
|
|
362
|
+
: (node.params && typeof node.params === 'object' ? node.params : {});
|
|
363
|
+
return clipPrompt(params.prompt)
|
|
364
|
+
|| clipPrompt(params.positive_prompt)
|
|
365
|
+
|| clipPrompt(params.positivePrompt)
|
|
366
|
+
|| clipPrompt(params.user_prompt)
|
|
367
|
+
|| clipPrompt(params.text)
|
|
368
|
+
|| clipPrompt(node.data?.prompt)
|
|
369
|
+
|| clipPrompt(node.prompt);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const taskIdOf = (node) => String(node?.data?.task?.taskId || node?.data?.task?.task_id || node?.task?.taskId || node?.taskId || '');
|
|
373
|
+
|
|
374
|
+
/** 在画布草稿里按素材编号取出对应节点的提示词。节点本身没有时,再用同一个任务编号上的提示词。 */
|
|
375
|
+
export const promptForAsset = (root, assetId) => {
|
|
376
|
+
const want = String(assetId || '');
|
|
377
|
+
if (!want || !root || typeof root !== 'object') return '';
|
|
378
|
+
let found = '';
|
|
379
|
+
let taskId = '';
|
|
380
|
+
const byTask = new Map();
|
|
381
|
+
const visit = (node, depth) => {
|
|
382
|
+
if (!node || typeof node !== 'object' || depth > 12) return;
|
|
383
|
+
if (Array.isArray(node)) {
|
|
384
|
+
node.forEach(entry => visit(entry, depth + 1));
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const prompt = promptText(node);
|
|
388
|
+
const tid = taskIdOf(node);
|
|
389
|
+
if (prompt && tid && !byTask.has(tid)) byTask.set(tid, prompt);
|
|
390
|
+
const asset = node.data?.asset && typeof node.data.asset === 'object' ? node.data.asset : node.asset;
|
|
391
|
+
const id = String(asset?.id || asset?.asset_id || node.asset_id || '');
|
|
392
|
+
if (id === want) {
|
|
393
|
+
if (!found) found = prompt;
|
|
394
|
+
if (!taskId) taskId = tid;
|
|
395
|
+
}
|
|
396
|
+
Object.values(node).forEach(value => { if (value && typeof value === 'object') visit(value, depth + 1); });
|
|
397
|
+
};
|
|
398
|
+
visit(root, 0);
|
|
399
|
+
return found || (taskId ? byTask.get(taskId) || '' : '');
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const parseDraftTree = (value) => {
|
|
403
|
+
if (!value || typeof value !== 'object') return null;
|
|
404
|
+
const raw = typeof value.draft === 'string' ? value.draft : '';
|
|
405
|
+
if (!raw || raw.length > 2_000_000) return null;
|
|
406
|
+
try {
|
|
407
|
+
const outer = JSON.parse(raw);
|
|
408
|
+
const content = typeof outer?.content === 'string' ? JSON.parse(outer.content) : outer?.content;
|
|
409
|
+
return content && typeof content === 'object' ? content : (outer && typeof outer === 'object' ? outer : null);
|
|
410
|
+
} catch {
|
|
411
|
+
return null;
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
const CANVAS_ASSET_ID = /^[\w-]{1,80}$/;
|
|
415
|
+
const ASSET_FILE_HOSTS = new Set(['workrally.qq.com', 'cdn.workrally.qq.com', 'zenvideo.gtimg.com', 'zenvideo-pro.gtimg.com']);
|
|
416
|
+
const ASSET_FILE_LIMIT = 200 * 1024 * 1024;
|
|
417
|
+
let assetFetcher = globalThis.fetch;
|
|
418
|
+
|
|
419
|
+
export const configureAssetFetcher = (fetchImpl) => { assetFetcher = fetchImpl || globalThis.fetch; };
|
|
420
|
+
|
|
421
|
+
export const isAssetFileUrl = (value) => {
|
|
422
|
+
try {
|
|
423
|
+
const url = new URL(value);
|
|
424
|
+
return url.protocol === 'https:' && ASSET_FILE_HOSTS.has(url.hostname) && !url.username && !url.password
|
|
425
|
+
&& (!url.port || url.port === '443');
|
|
426
|
+
} catch {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
/** 下载地址优先。短链可以做第一跳,最终文件必须落在 WorkRally 或腾讯视频 CDN。 */
|
|
432
|
+
export const pickAssetFileUrl = (payloads, assetId) => {
|
|
433
|
+
const want = String(assetId || '');
|
|
434
|
+
const ranked = [];
|
|
435
|
+
const visit = (node, matched, depth) => {
|
|
436
|
+
if (!node || typeof node !== 'object' || depth > 12) return;
|
|
437
|
+
if (Array.isArray(node)) {
|
|
438
|
+
node.forEach(entry => visit(entry, matched, depth + 1));
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const id = String(node.asset_id || node.assetId || node.id || '');
|
|
442
|
+
const now = matched || Boolean(want && id === want);
|
|
443
|
+
const details = node.asset_details && typeof node.asset_details === 'object' ? node.asset_details : null;
|
|
444
|
+
if (now || !want) {
|
|
445
|
+
const bag = details || node;
|
|
446
|
+
if (typeof bag.download_url === 'string') ranked.push(bag.download_url);
|
|
447
|
+
if (typeof bag.downloadUrl === 'string') ranked.push(bag.downloadUrl);
|
|
448
|
+
if (typeof bag.url === 'string') ranked.push(bag.url);
|
|
449
|
+
}
|
|
450
|
+
for (const [key, value] of Object.entries(node)) {
|
|
451
|
+
if (key === 'asset_details') continue;
|
|
452
|
+
if (value && typeof value === 'object') visit(value, now, depth + 1);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
(Array.isArray(payloads) ? payloads : []).forEach(payload => visit(payload, false, 0));
|
|
456
|
+
return ranked.find(isAssetFileUrl) || '';
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/** 把 asset_search 回执收成画布媒资卡片。封面优先用海报,图片才回退到会跳转的短链。 */
|
|
460
|
+
export const normalizeCanvasAssets = (payloads) => {
|
|
461
|
+
const payload = (Array.isArray(payloads) ? payloads : []).find(item => Array.isArray(item?.assets)) || {};
|
|
462
|
+
const assets = [];
|
|
463
|
+
for (const asset of payload.assets) {
|
|
464
|
+
if (!asset || typeof asset.id !== 'string' || !asset.id) continue;
|
|
465
|
+
const details = asset.asset_details && typeof asset.asset_details === 'object' ? asset.asset_details : {};
|
|
466
|
+
const type = ASSET_TYPES.has(asset.type) ? asset.type : 'image';
|
|
467
|
+
const poster = typeof details.poster === 'string' && details.poster.startsWith('https://') ? details.poster : '';
|
|
468
|
+
const pageUrl = typeof details.url === 'string' && details.url.startsWith('https://') ? details.url : '';
|
|
469
|
+
const count = (value) => {
|
|
470
|
+
const n = Number(value);
|
|
471
|
+
return Number.isFinite(n) && n > 0 ? Math.round(n) : 0;
|
|
472
|
+
};
|
|
473
|
+
assets.push({
|
|
474
|
+
id: asset.id,
|
|
475
|
+
title: typeof asset.title === 'string' ? asset.title : '',
|
|
476
|
+
type,
|
|
477
|
+
canvasId: typeof details.material_canvas_project === 'string' ? details.material_canvas_project : '',
|
|
478
|
+
poster,
|
|
479
|
+
src: poster || (type === 'image' ? pageUrl : ''),
|
|
480
|
+
width: count(details.width),
|
|
481
|
+
height: count(details.height),
|
|
482
|
+
bytes: count(details.size),
|
|
483
|
+
prompt: clipPrompt(asset.prompt) || clipPrompt(details.prompt) || clipPrompt(details.positive_prompt),
|
|
484
|
+
created: typeof asset.created_at === 'string' ? asset.created_at.slice(0, 10) : '',
|
|
485
|
+
createdAt: typeof asset.created_at === 'string' ? asset.created_at : '',
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
return {
|
|
489
|
+
assets,
|
|
490
|
+
total: Number(payload.page_info?.total) || assets.length,
|
|
491
|
+
page: Number(payload.page_info?.page) || 1,
|
|
492
|
+
};
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
/** 只读当前账号在无限画布里生成并入库的媒资,可按画布收窄。 */
|
|
496
|
+
export const readNativeAssets = async (ctx, { page = 1, materialType = '', canvasId = '', keyword = '' } = {}, signal) => {
|
|
497
|
+
const definitions = await ctx?.tools?.definitions?.();
|
|
498
|
+
const defs = Array.isArray(definitions) ? definitions : [];
|
|
499
|
+
const searchDef = defs.map(tool => workrallyTool(tool?.name)).find(item => item?.action === 'asset_search');
|
|
500
|
+
if (!searchDef) throw new Error('尚未连接 WorkRally 账号,请先点击登录绑定你的账号');
|
|
501
|
+
if (typeof ctx?.tools?.execute !== 'function') throw new Error('DSH 原生工具执行服务不可用');
|
|
502
|
+
const args = {
|
|
503
|
+
channel: ['toolbox_canvas'],
|
|
504
|
+
artifact_type: [0],
|
|
505
|
+
sort_field: 1,
|
|
506
|
+
sort_order: 1,
|
|
507
|
+
page: Math.min(500, Math.max(1, Number(page) || 1)),
|
|
508
|
+
page_size: 24,
|
|
509
|
+
};
|
|
510
|
+
if (ASSET_TYPES.has(materialType)) args.material_type = [materialType];
|
|
511
|
+
if (CANVAS_ASSET_ID.test(canvasId)) args.canvas_project_id = [canvasId];
|
|
512
|
+
const query = String(keyword || '').trim().slice(0, 80);
|
|
513
|
+
if (query) args.keyword = query;
|
|
514
|
+
const id = randomUUID();
|
|
515
|
+
const response = await ctx.tools.execute({
|
|
516
|
+
calls: [{
|
|
517
|
+
type: 'tool-call',
|
|
518
|
+
id,
|
|
519
|
+
name: `mcp__${searchDef.serverName}__asset_search`,
|
|
520
|
+
arguments: JSON.stringify(args),
|
|
521
|
+
}],
|
|
522
|
+
signal: signal || AbortSignal.timeout(20_000),
|
|
523
|
+
});
|
|
524
|
+
const result = response?.results?.find(entry => entry.callId === id)?.result;
|
|
525
|
+
if (!result) throw new Error('媒资读取失败');
|
|
526
|
+
const normalized = normalizeToolResult(result);
|
|
527
|
+
if (normalized.error) throw new Error(normalized.error);
|
|
528
|
+
return normalizeCanvasAssets(normalized.payloads);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
/** 打开详情时补提示词:先看素材本身,再按画布草稿里同一素材编号查找。 */
|
|
532
|
+
export const readAssetPrompt = async (ctx, assetId, canvasId, signal) => {
|
|
533
|
+
if (!CANVAS_ASSET_ID.test(assetId)) return '';
|
|
534
|
+
const definitions = await ctx?.tools?.definitions?.();
|
|
535
|
+
const defs = Array.isArray(definitions) ? definitions : [];
|
|
536
|
+
const detailDef = defs.map(tool => workrallyTool(tool?.name)).find(item => item?.action === 'asset_detail');
|
|
537
|
+
const manageDef = defs.map(tool => workrallyTool(tool?.name)).find(item => item?.action === 'canvas_manage');
|
|
538
|
+
if (typeof ctx?.tools?.execute !== 'function') return '';
|
|
539
|
+
const detailId = randomUUID();
|
|
540
|
+
const canvasCallId = randomUUID();
|
|
541
|
+
const calls = [];
|
|
542
|
+
if (detailDef) {
|
|
543
|
+
calls.push({
|
|
544
|
+
type: 'tool-call',
|
|
545
|
+
id: detailId,
|
|
546
|
+
name: `mcp__${detailDef.serverName}__asset_detail`,
|
|
547
|
+
arguments: JSON.stringify({ asset_ids: [assetId] }),
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
if (manageDef && CANVAS_ASSET_ID.test(canvasId)) {
|
|
551
|
+
calls.push({
|
|
552
|
+
type: 'tool-call',
|
|
553
|
+
id: canvasCallId,
|
|
554
|
+
name: `mcp__${manageDef.serverName}__canvas_manage`,
|
|
555
|
+
arguments: JSON.stringify({ action: 'get', canvas_id: canvasId }),
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
if (!calls.length) return '';
|
|
559
|
+
const response = await ctx.tools.execute({ calls, signal: signal || AbortSignal.timeout(20_000) });
|
|
560
|
+
const payloadsOf = (callId) => {
|
|
561
|
+
const result = response?.results?.find(entry => entry.callId === callId)?.result;
|
|
562
|
+
return result ? normalizeToolResult(result).payloads : [];
|
|
563
|
+
};
|
|
564
|
+
const fromAsset = payloadsOf(detailId).flatMap((payload) => {
|
|
565
|
+
const assets = Array.isArray(payload?.assets) ? payload.assets : [payload];
|
|
566
|
+
return assets
|
|
567
|
+
.filter(asset => String(asset?.id || asset?.asset_id || '') === assetId)
|
|
568
|
+
.map(asset => clipPrompt(asset?.prompt) || clipPrompt(asset?.asset_details?.prompt) || clipPrompt(asset?.asset_details?.positive_prompt));
|
|
569
|
+
}).find(Boolean);
|
|
570
|
+
if (fromAsset) return fromAsset;
|
|
571
|
+
const drafts = [];
|
|
572
|
+
const collect = (node, depth) => {
|
|
573
|
+
if (!node || typeof node !== 'object' || depth > 6) return;
|
|
574
|
+
if (typeof node.draft === 'string') drafts.push(node);
|
|
575
|
+
Object.values(node).forEach(value => { if (value && typeof value === 'object') collect(value, depth + 1); });
|
|
576
|
+
};
|
|
577
|
+
payloadsOf(canvasCallId).forEach(payload => collect(payload, 0));
|
|
578
|
+
for (const draft of drafts) {
|
|
579
|
+
const prompt = promptForAsset(parseDraftTree(draft), assetId);
|
|
580
|
+
if (prompt) return prompt;
|
|
581
|
+
}
|
|
582
|
+
return '';
|
|
583
|
+
};
|
|
584
|
+
|
|
585
|
+
const assetTitle = (payloads, assetId) => {
|
|
586
|
+
const found = [];
|
|
587
|
+
const visit = (node, depth) => {
|
|
588
|
+
if (!node || typeof node !== 'object' || depth > 12) return;
|
|
589
|
+
if (Array.isArray(node)) { node.forEach(entry => visit(entry, depth + 1)); return; }
|
|
590
|
+
if (String(node.id || node.asset_id || '') === assetId && typeof node.title === 'string') found.push(node.title);
|
|
591
|
+
Object.values(node).forEach(value => { if (value && typeof value === 'object') visit(value, depth + 1); });
|
|
592
|
+
};
|
|
593
|
+
payloads.forEach(payload => visit(payload, 0));
|
|
594
|
+
return found.find(title => title.trim()) || '';
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
/** 按素材 ID 向当前账号取下载地址和文件名,不接收调用方传入的任意网址。 */
|
|
598
|
+
export const readAssetDownloadTarget = async (ctx, assetId, signal) => {
|
|
599
|
+
if (!CANVAS_ASSET_ID.test(assetId)) throw Object.assign(new Error('素材编号无效'), { status: 400 });
|
|
600
|
+
const definitions = await ctx?.tools?.definitions?.();
|
|
601
|
+
const defs = Array.isArray(definitions) ? definitions : [];
|
|
602
|
+
const detailDef = defs.map(tool => workrallyTool(tool?.name)).find(item => item?.action === 'asset_detail');
|
|
603
|
+
if (!detailDef) throw new Error('尚未连接 WorkRally 账号,请先点击登录绑定你的账号');
|
|
604
|
+
if (typeof ctx?.tools?.execute !== 'function') throw new Error('DSH 原生工具执行服务不可用');
|
|
605
|
+
const id = randomUUID();
|
|
606
|
+
const response = await ctx.tools.execute({
|
|
607
|
+
calls: [{
|
|
608
|
+
type: 'tool-call',
|
|
609
|
+
id,
|
|
610
|
+
name: `mcp__${detailDef.serverName}__asset_detail`,
|
|
611
|
+
arguments: JSON.stringify({ asset_ids: [assetId] }),
|
|
612
|
+
}],
|
|
613
|
+
signal: signal || AbortSignal.timeout(20_000),
|
|
614
|
+
});
|
|
615
|
+
const result = response?.results?.find(entry => entry.callId === id)?.result;
|
|
616
|
+
if (!result) throw new Error('媒资读取失败');
|
|
617
|
+
const normalized = normalizeToolResult(result);
|
|
618
|
+
if (normalized.error) throw new Error(normalized.error);
|
|
619
|
+
const url = pickAssetFileUrl(normalized.payloads, assetId);
|
|
620
|
+
if (!url) throw Object.assign(new Error('没有可下载的文件'), { status: 404 });
|
|
621
|
+
return { url, title: assetTitle(normalized.payloads, assetId) };
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
/** 跟随短链,每跳都必须落在允许的域名上。最终响应留给调用方读取或取消。 */
|
|
625
|
+
const followAssetUrl = async (startUrl, signal) => {
|
|
626
|
+
if (!isAssetFileUrl(startUrl)) throw Object.assign(new Error('下载地址不在允许范围内'), { status: 400 });
|
|
627
|
+
if (typeof assetFetcher !== 'function') throw new Error('下载不可用');
|
|
628
|
+
let url = startUrl;
|
|
629
|
+
let response;
|
|
630
|
+
for (let hop = 0; hop <= 4; hop += 1) {
|
|
631
|
+
if (!isAssetFileUrl(url)) throw Object.assign(new Error('下载地址不在允许范围内'), { status: 400 });
|
|
632
|
+
response = await assetFetcher(url, { redirect: 'manual', signal });
|
|
633
|
+
if (![301, 302, 303, 307, 308].includes(response.status)) break;
|
|
634
|
+
const location = response.headers.get('location');
|
|
635
|
+
await response.body?.cancel();
|
|
636
|
+
if (!location || hop === 4) throw new Error('下载重定向次数过多');
|
|
637
|
+
url = new URL(location, url).href;
|
|
638
|
+
}
|
|
639
|
+
return { url, response };
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
/** 解析出可直接播放的音视频地址,不把文件读进内存。 */
|
|
643
|
+
export const resolveAssetPlaybackUrl = async (startUrl, signal) => {
|
|
644
|
+
const { url, response } = await followAssetUrl(startUrl, signal);
|
|
645
|
+
const mime = String(response?.headers?.get('content-type') || '').split(';')[0].trim().toLowerCase();
|
|
646
|
+
await response?.body?.cancel?.();
|
|
647
|
+
if (!response?.ok || !/^(video|audio)\//.test(mime)) throw new Error('响应不是可播放的音视频');
|
|
648
|
+
return { url, mime };
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
/** 跟随短链跳转,只接受图片、视频、音频,并限制 200 MB。 */
|
|
652
|
+
export const downloadAssetFile = async (startUrl, signal) => {
|
|
653
|
+
const { response } = await followAssetUrl(startUrl, signal);
|
|
654
|
+
const mime = String(response?.headers?.get('content-type') || '').split(';')[0].trim().toLowerCase();
|
|
655
|
+
if (!response?.ok || !/^(image|video|audio)\//.test(mime) || !response.body?.getReader) {
|
|
656
|
+
await response?.body?.cancel?.();
|
|
657
|
+
throw new Error('响应不是可下载的媒体文件');
|
|
658
|
+
}
|
|
659
|
+
const declared = Number(response.headers.get('content-length'));
|
|
660
|
+
if (declared > ASSET_FILE_LIMIT) {
|
|
661
|
+
await response.body.cancel();
|
|
662
|
+
throw Object.assign(new Error('文件超过 200 MB'), { status: 413 });
|
|
663
|
+
}
|
|
664
|
+
const reader = response.body.getReader();
|
|
665
|
+
const chunks = [];
|
|
666
|
+
let size = 0;
|
|
667
|
+
try {
|
|
668
|
+
while (true) {
|
|
669
|
+
const { done, value } = await reader.read();
|
|
670
|
+
if (done) break;
|
|
671
|
+
size += value.byteLength;
|
|
672
|
+
if (size > ASSET_FILE_LIMIT) throw Object.assign(new Error('文件超过 200 MB'), { status: 413 });
|
|
673
|
+
chunks.push(Buffer.from(value));
|
|
674
|
+
}
|
|
675
|
+
} finally {
|
|
676
|
+
await reader.cancel().catch(() => {});
|
|
677
|
+
}
|
|
678
|
+
return { bytes: Buffer.concat(chunks, size), mime };
|
|
679
|
+
};
|