@cloud411716/fancy-webnovel 4.7.2 → 4.8.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 +1 -1
- package/docs/douyin-plugin.md +35 -15
- package/docs/material-library/README.md +5 -2
- package/docs/material-library/import.js +13 -3
- package/docs/material-library/prompt.md +7 -3
- package/docs/material-library/queries.surql +17 -9
- package/docs/material-library/sample.jsonl +1 -1
- package/docs/material-library/schema.surql +3 -1
- package/docs/workspace-mcp.md +8 -6
- package/lib/material-db.js +4 -4
- package/package.json +1 -1
- package/plugins/fancy-bootstrap/index.js +2 -2
- package/plugins/fancy-db/index.js +1 -1
- package/plugins/fancy-douyin/deploy.js +5 -4
- package/plugins/fancy-douyin/index.js +467 -31
- package/plugins/fancy-douyin/prompts/00-capability-prompt.md +22 -0
- package/plugins/fancy-douyin/prompts/01-analysis-prompt.md +44 -6
- package/plugins/fancy-douyin/prompts/02-video-analysis-prompt.md +95 -0
- package/plugins/fancy-douyin/scripts/crawl-video.cjs +420 -0
- package/plugins/fancy-douyin/scripts/scan-account.cjs +3 -2
- package/plugins/fancy-douyin/state.js +4 -3
- package/plugins/fancy-douyin/templates.js +114 -7
- package/plugins/fancy-material/exporter.js +3 -1
- package/plugins/fancy-material/inventory.js +3 -2
- package/plugins/fancy-material/scripts/query.js +11 -4
- package/plugins/fancy-material/templates.js +4 -3
- package/plugins/fancy-material/validate.js +11 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* fancy-douyin — crawl Douyin image-text works into the material library.
|
|
2
|
+
* fancy-douyin — crawl Douyin image-text and video works into the material library.
|
|
3
3
|
*
|
|
4
4
|
* Commands:
|
|
5
5
|
* /fancy-douyin crawl every recorded account
|
|
@@ -32,13 +32,19 @@ import {
|
|
|
32
32
|
readCrawled,
|
|
33
33
|
removeAccount,
|
|
34
34
|
} from './state.js';
|
|
35
|
-
import { analysisPrompt, helpText, loginQuestion, summaryText } from './templates.js';
|
|
35
|
+
import { analysisPrompt, capabilityPrompt, helpText, loginQuestion, summaryText, videoAnalysisPrompt, videoRelevancePrompt } from './templates.js';
|
|
36
36
|
|
|
37
37
|
export const inject = ['commands', 'userQuestions'];
|
|
38
38
|
|
|
39
39
|
const ANALYSIS_TIMEOUT_MS = 20 * 60 * 1000;
|
|
40
40
|
const ANALYSIS_IDLE_GRACE_MS = 60 * 1000;
|
|
41
|
+
const CAPABILITY_TIMEOUT_MS = 5 * 60 * 1000;
|
|
42
|
+
const CAPABILITY_IDLE_GRACE_MS = 30 * 1000;
|
|
43
|
+
const RELEVANCE_TIMEOUT_MS = 5 * 60 * 1000;
|
|
44
|
+
const RELEVANCE_IDLE_GRACE_MS = 30 * 1000;
|
|
41
45
|
const SCRIPT_TIMEOUT_MS = 4 * 60 * 1000;
|
|
46
|
+
const VIDEO_SCRIPT_TIMEOUT_MS = 10 * 60 * 1000;
|
|
47
|
+
const MAX_VIDEO_SECONDS = 60;
|
|
42
48
|
const LOGIN_RETRIES = 5;
|
|
43
49
|
const NOTE_DELAY_MIN_MS = 6000;
|
|
44
50
|
const NOTE_DELAY_MAX_MS = 12000;
|
|
@@ -132,9 +138,9 @@ async function stopChrome(root, tools, paths) {
|
|
|
132
138
|
} catch { /* best effort */ }
|
|
133
139
|
}
|
|
134
140
|
|
|
135
|
-
async function runWithLogin(ctx, invocation, session, root, toolPath, args, outDir, paths, label, nodePath, browser, tools) {
|
|
141
|
+
async function runWithLogin(ctx, invocation, session, root, toolPath, args, outDir, paths, label, nodePath, browser, tools, timeout = SCRIPT_TIMEOUT_MS) {
|
|
136
142
|
for (let attempt = 1; attempt <= LOGIN_RETRIES; attempt += 1) {
|
|
137
|
-
const { result, json } = await runTool(toolPath, args, { root, nodePath });
|
|
143
|
+
const { result, json } = await runTool(toolPath, args, { root, nodePath, timeout });
|
|
138
144
|
if (json && json.login_required) {
|
|
139
145
|
if (browser.mode === 'headless') {
|
|
140
146
|
// Headless cannot show a login UI. Close it, start headed Chrome and
|
|
@@ -247,14 +253,18 @@ export async function importOtherMaterials(root, file) {
|
|
|
247
253
|
try {
|
|
248
254
|
for (const record of records) {
|
|
249
255
|
try {
|
|
250
|
-
|
|
256
|
+
const content = {
|
|
251
257
|
source: 'other',
|
|
252
258
|
kind: record.kind,
|
|
253
259
|
name: record.name,
|
|
254
260
|
tags: record.tags,
|
|
255
261
|
genres: record.genres,
|
|
256
262
|
note: typeof record.note === 'string' ? record.note : '',
|
|
257
|
-
}
|
|
263
|
+
};
|
|
264
|
+
if (typeof record.subkind === 'string' && record.subkind.trim()) {
|
|
265
|
+
content.subkind = record.subkind.trim();
|
|
266
|
+
}
|
|
267
|
+
await db.create(new RecordId('material', record.id)).content(content);
|
|
258
268
|
inserted += 1;
|
|
259
269
|
} catch (err) {
|
|
260
270
|
if (DUPLICATE_RE.test(err && err.message ? err.message : String(err))) {
|
|
@@ -272,11 +282,11 @@ export async function importOtherMaterials(root, file) {
|
|
|
272
282
|
|
|
273
283
|
function retryPrompt(target, detail) {
|
|
274
284
|
return [
|
|
275
|
-
'
|
|
285
|
+
'上一批素材文件没有通过检查。',
|
|
276
286
|
'',
|
|
277
287
|
'目标文件:`' + target + '`',
|
|
278
288
|
'',
|
|
279
|
-
'
|
|
289
|
+
'问题:',
|
|
280
290
|
'```text',
|
|
281
291
|
String(detail || '').trim(),
|
|
282
292
|
'```',
|
|
@@ -287,18 +297,163 @@ function retryPrompt(target, detail) {
|
|
|
287
297
|
].join('\n');
|
|
288
298
|
}
|
|
289
299
|
|
|
290
|
-
|
|
291
|
-
|
|
300
|
+
export function expectedItemCount(title) {
|
|
301
|
+
const match = String(title || '').match(/(\d+)\s*\+?\s*(?:个|种|条)/);
|
|
302
|
+
if (!match) return 0;
|
|
303
|
+
const count = Number(match[1]);
|
|
304
|
+
return Number.isFinite(count) && count >= 5 ? count : 0;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const CJK_RE = /[\u3400-\u9fff\uf900-\ufaff]/;
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* 抖音素材的 kind 必须是中文作品主题,方便 LLM 直接用中文查询。
|
|
311
|
+
* 旧提示词会把 kind 写成 color_word / person_appearance,导致按中文查不到,
|
|
312
|
+
* 所以这里做一次硬校验,发现英文 kind 就带着错误重试。
|
|
313
|
+
*/
|
|
314
|
+
export function kindLanguageProblem(file) {
|
|
315
|
+
let records;
|
|
316
|
+
try {
|
|
317
|
+
records = readJsonl(file);
|
|
318
|
+
} catch {
|
|
319
|
+
return '';
|
|
320
|
+
}
|
|
321
|
+
const bad = records
|
|
322
|
+
.map((record) => (typeof record.kind === 'string' ? record.kind.trim() : ''))
|
|
323
|
+
.filter((kind) => kind && !CJK_RE.test(kind));
|
|
324
|
+
if (!bad.length) return '';
|
|
325
|
+
return '以下素材的 kind 不是中文:' + [...new Set(bad)].slice(0, 8).join('、')
|
|
326
|
+
+ '。抖音 source=other 素材的 kind 必须写中文作品主题(如 唇色、瞳色、特殊体质),'
|
|
327
|
+
+ '不要写 color_word、person_appearance 这类英文内部标识。请改写后重新写入同一个文件。';
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 清单类作品(标题里写“100个/100种”)必须按条目数量逐条提取,
|
|
332
|
+
* 并额外产出 10 条同 kind + 同 subkind 的扩写素材。
|
|
333
|
+
* 只有格式校验不够:模型可能只提炼了 20 条创作句,却完全漏掉原清单。
|
|
334
|
+
*/
|
|
335
|
+
export function coverageProblem(file, title) {
|
|
336
|
+
const expected = expectedItemCount(title);
|
|
337
|
+
if (!expected) return '';
|
|
338
|
+
const required = expected + 10;
|
|
339
|
+
let records;
|
|
340
|
+
try {
|
|
341
|
+
records = readJsonl(file);
|
|
342
|
+
} catch {
|
|
343
|
+
return '';
|
|
344
|
+
}
|
|
345
|
+
if (records.length >= required) return '';
|
|
346
|
+
return '作品标题写着 ' + expected + ' 条清单,加上同 kind + 同 subkind 的 10 条扩写素材,'
|
|
347
|
+
+ '至少应有 ' + required + ' 条,但当前只有 ' + records.length
|
|
348
|
+
+ ' 条。清单类内容必须逐条输出:name 保留原文条目,不要只提炼一部分或改写成意象句;'
|
|
349
|
+
+ '补全原始条目和 10 条扩写素材后,重新写入同一个文件。';
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function parseFirstJsonObject(text) {
|
|
353
|
+
let cleaned = String(text || '').replace(/^\uFEFF/, '').trim();
|
|
354
|
+
cleaned = cleaned
|
|
355
|
+
.replace(/^```(?:json)?\s*/i, '')
|
|
356
|
+
.replace(/\s*```$/, '')
|
|
357
|
+
.trim();
|
|
358
|
+
if (!cleaned) return null;
|
|
359
|
+
const candidates = [cleaned, cleaned.split(/\r?\n/).find((line) => line.trim()) || ''];
|
|
360
|
+
for (const candidate of candidates) {
|
|
361
|
+
try {
|
|
362
|
+
const value = JSON.parse(candidate.trim());
|
|
363
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) return value;
|
|
364
|
+
} catch { /* try next */ }
|
|
365
|
+
}
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function truthyFlag(value) {
|
|
370
|
+
return value === true || String(value).toLowerCase() === 'true';
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Parse the capability self-check file. Missing fields default to false so a
|
|
375
|
+
* malformed answer never silently enables a modality the model cannot handle.
|
|
376
|
+
*/
|
|
377
|
+
export function parseCapabilityResult(text) {
|
|
378
|
+
const data = parseFirstJsonObject(text);
|
|
379
|
+
if (!data) return null;
|
|
380
|
+
return {
|
|
381
|
+
image_supported: truthyFlag(data.image_supported),
|
|
382
|
+
video_supported: truthyFlag(data.video_supported),
|
|
383
|
+
reason: typeof data.reason === 'string' ? data.reason.trim() : '',
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Read the first-5-seconds relevance marker written by the LLM.
|
|
389
|
+
* Returns null when the file is missing/unparseable, so the caller can retry.
|
|
390
|
+
*/
|
|
391
|
+
export function readRelevanceMarker(file) {
|
|
392
|
+
let text;
|
|
393
|
+
try {
|
|
394
|
+
text = fs.readFileSync(file, 'utf8');
|
|
395
|
+
} catch {
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
const data = parseFirstJsonObject(text);
|
|
399
|
+
if (!data) return null;
|
|
400
|
+
if (truthyFlag(data.skip)) {
|
|
401
|
+
return { skip: true, reason: String(data.reason || '前 5 秒与网文创作无关').trim() };
|
|
402
|
+
}
|
|
403
|
+
if (truthyFlag(data.relevant) || truthyFlag(data.related)) {
|
|
404
|
+
return { skip: false, reason: String(data.reason || '').trim() };
|
|
405
|
+
}
|
|
406
|
+
return null;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function detectCapabilities(ctx, invocation, session, taskList, root, runDir) {
|
|
410
|
+
const file = path.join(runDir, 'capabilities.json');
|
|
411
|
+
try { fs.rmSync(file, { force: true }); } catch { /* ignore */ }
|
|
412
|
+
taskList.watchTurns(invocation);
|
|
413
|
+
let before = null;
|
|
414
|
+
for (let attempt = 1; attempt <= 2; attempt += 1) {
|
|
415
|
+
const sent = await llmFollowup(invocation, capabilityPrompt({ capabilityFile: file }), {
|
|
416
|
+
appendTempRule: false,
|
|
417
|
+
});
|
|
418
|
+
if (!sent || sent.ok !== true) {
|
|
419
|
+
return {
|
|
420
|
+
ok: false,
|
|
421
|
+
error: '能力自检提示词发送失败:' + ((sent && sent.error) || '未知错误'),
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
const waitForTurn = makeWaitForTurn(invocation, {
|
|
425
|
+
timeoutMs: CAPABILITY_TIMEOUT_MS,
|
|
426
|
+
idleGraceMs: CAPABILITY_IDLE_GRACE_MS,
|
|
427
|
+
signal: invocation.signal,
|
|
428
|
+
changed: (files, baseline) => {
|
|
429
|
+
try {
|
|
430
|
+
const text = fs.readFileSync(files[0], 'utf8');
|
|
431
|
+
return baseline === null || baseline === undefined ? text.trim().length > 0 : text !== baseline;
|
|
432
|
+
} catch {
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
});
|
|
437
|
+
const waited = await waitForTurn([file], before);
|
|
438
|
+
if (waited && waited.ok) {
|
|
439
|
+
let text = '';
|
|
440
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch { /* retry */ }
|
|
441
|
+
const parsed = parseCapabilityResult(text);
|
|
442
|
+
if (parsed) return { ok: true, ...parsed };
|
|
443
|
+
before = text || null;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
if (waited && waited.code === 'ABORTED') {
|
|
447
|
+
return { ok: false, aborted: true, error: '用户中断' };
|
|
448
|
+
}
|
|
449
|
+
try { before = fs.readFileSync(file, 'utf8'); } catch { before = null; }
|
|
450
|
+
}
|
|
451
|
+
return { ok: false, error: 'LLM 没有返回可解析的能力自检结果' };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
async function analyzeMaterials(ctx, invocation, session, {
|
|
455
|
+
root, prompt, target, title, materialTools, taskList,
|
|
292
456
|
}) {
|
|
293
|
-
const prompt = analysisPrompt({
|
|
294
|
-
accountUrl: note.account_url,
|
|
295
|
-
note,
|
|
296
|
-
screenshots,
|
|
297
|
-
targetFile: target,
|
|
298
|
-
workspace: root,
|
|
299
|
-
queryScript: materialTools.queryScript,
|
|
300
|
-
validateScript: materialTools.validateScript,
|
|
301
|
-
});
|
|
302
457
|
const waitForTurn = makeWaitForTurn(invocation, {
|
|
303
458
|
timeoutMs: ANALYSIS_TIMEOUT_MS,
|
|
304
459
|
idleGraceMs: ANALYSIS_IDLE_GRACE_MS,
|
|
@@ -360,6 +515,16 @@ async function analyzeNote(ctx, invocation, session, {
|
|
|
360
515
|
detail = checked.detail;
|
|
361
516
|
continue;
|
|
362
517
|
}
|
|
518
|
+
const kindProblem = kindLanguageProblem(target);
|
|
519
|
+
if (kindProblem) {
|
|
520
|
+
detail = kindProblem;
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
const coverage = coverageProblem(target, title);
|
|
524
|
+
if (coverage) {
|
|
525
|
+
detail = coverage;
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
363
528
|
try {
|
|
364
529
|
const result = await importOtherMaterials(root, target);
|
|
365
530
|
return { ok: true, ...result };
|
|
@@ -370,6 +535,98 @@ async function analyzeNote(ctx, invocation, session, {
|
|
|
370
535
|
return { ok: false, error: '素材没有通过校验:' + detail };
|
|
371
536
|
}
|
|
372
537
|
|
|
538
|
+
async function analyzeNote(ctx, invocation, session, {
|
|
539
|
+
root, note, screenshots, target, materialTools, paths, taskList,
|
|
540
|
+
}) {
|
|
541
|
+
const prompt = analysisPrompt({
|
|
542
|
+
accountUrl: note.account_url,
|
|
543
|
+
note,
|
|
544
|
+
screenshots,
|
|
545
|
+
targetFile: target,
|
|
546
|
+
workspace: root,
|
|
547
|
+
queryScript: materialTools.queryScript,
|
|
548
|
+
validateScript: materialTools.validateScript,
|
|
549
|
+
});
|
|
550
|
+
return analyzeMaterials(ctx, invocation, session, {
|
|
551
|
+
root,
|
|
552
|
+
prompt,
|
|
553
|
+
target,
|
|
554
|
+
title: note.title,
|
|
555
|
+
materialTools,
|
|
556
|
+
taskList,
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
async function analyzeVideoRelevance(ctx, invocation, session, {
|
|
561
|
+
root, accountUrl, video, target, taskList,
|
|
562
|
+
}) {
|
|
563
|
+
const file = path.join(video.dir, 'relevance.json');
|
|
564
|
+
try { fs.rmSync(file, { force: true }); } catch { /* ignore */ }
|
|
565
|
+
taskList.watchTurns(invocation);
|
|
566
|
+
const prompt = videoRelevancePrompt({
|
|
567
|
+
accountUrl,
|
|
568
|
+
video,
|
|
569
|
+
firstFrames: video.first5s || [],
|
|
570
|
+
targetFile: file,
|
|
571
|
+
});
|
|
572
|
+
let before = null;
|
|
573
|
+
for (let attempt = 1; attempt <= 2; attempt += 1) {
|
|
574
|
+
const sent = await llmFollowup(invocation, attempt === 1 ? prompt : prompt + '\n\n上一次没有写出可解析的 relevance.json,请重新判断并覆盖写入同一个路径。', {
|
|
575
|
+
appendTempRule: false,
|
|
576
|
+
});
|
|
577
|
+
if (!sent || sent.ok !== true) {
|
|
578
|
+
return { ok: false, error: '前 5 秒判断提示词发送失败:' + ((sent && sent.error) || '未知错误') };
|
|
579
|
+
}
|
|
580
|
+
const waitForTurn = makeWaitForTurn(invocation, {
|
|
581
|
+
timeoutMs: RELEVANCE_TIMEOUT_MS,
|
|
582
|
+
idleGraceMs: RELEVANCE_IDLE_GRACE_MS,
|
|
583
|
+
signal: invocation.signal,
|
|
584
|
+
changed: (files, baseline) => {
|
|
585
|
+
try {
|
|
586
|
+
const text = fs.readFileSync(files[0], 'utf8');
|
|
587
|
+
return baseline === null || baseline === undefined ? text.trim().length > 0 : text !== baseline;
|
|
588
|
+
} catch {
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
const waited = await waitForTurn([file], before);
|
|
594
|
+
if (waited && waited.ok) {
|
|
595
|
+
const marker = readRelevanceMarker(file);
|
|
596
|
+
if (marker) return { ok: true, ...marker };
|
|
597
|
+
try { before = fs.readFileSync(file, 'utf8'); } catch { before = null; }
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
if (waited && waited.code === 'ABORTED') {
|
|
601
|
+
return { ok: false, aborted: true, error: '用户中断' };
|
|
602
|
+
}
|
|
603
|
+
try { before = fs.readFileSync(file, 'utf8'); } catch { before = null; }
|
|
604
|
+
}
|
|
605
|
+
return { ok: false, error: '前 5 秒相关性判断没有返回可解析结果' };
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
async function analyzeVideo(ctx, invocation, session, {
|
|
609
|
+
root, accountUrl, video, target, materialTools, taskList,
|
|
610
|
+
}) {
|
|
611
|
+
const prompt = videoAnalysisPrompt({
|
|
612
|
+
accountUrl,
|
|
613
|
+
video,
|
|
614
|
+
frames: video.frames || [],
|
|
615
|
+
targetFile: target,
|
|
616
|
+
workspace: root,
|
|
617
|
+
queryScript: materialTools.queryScript,
|
|
618
|
+
validateScript: materialTools.validateScript,
|
|
619
|
+
});
|
|
620
|
+
return analyzeMaterials(ctx, invocation, session, {
|
|
621
|
+
root,
|
|
622
|
+
prompt,
|
|
623
|
+
target,
|
|
624
|
+
title: video.title,
|
|
625
|
+
materialTools,
|
|
626
|
+
taskList,
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
|
|
373
630
|
async function runCrawl(ctx, invocation, session, taskList) {
|
|
374
631
|
const root = workspaceOf(session);
|
|
375
632
|
const paths = ensureDouyinDir(root);
|
|
@@ -378,6 +635,25 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
378
635
|
return notify(session, 'error', '还没有记录抖音账号。先运行:/fancy-douyin add <主页地址>');
|
|
379
636
|
}
|
|
380
637
|
|
|
638
|
+
const runDir = path.join(paths.output, stampOfNow());
|
|
639
|
+
fs.mkdirSync(runDir, { recursive: true });
|
|
640
|
+
|
|
641
|
+
// 先做能力自检:如果模型既不能看图也不能看视频,直接退出,
|
|
642
|
+
// 不启动 Chrome,也不扫描账号。
|
|
643
|
+
taskList.advance(1);
|
|
644
|
+
taskList.progress('LLM 能力自检(图片 / 视频)');
|
|
645
|
+
const capabilities = await detectCapabilities(ctx, invocation, session, taskList, root, runDir);
|
|
646
|
+
if (!capabilities.ok) {
|
|
647
|
+
return notify(session, 'error', '❌ LLM 能力自检失败:' + capabilities.error);
|
|
648
|
+
}
|
|
649
|
+
if (!capabilities.image_supported && !capabilities.video_supported) {
|
|
650
|
+
return notify(session, 'error', [
|
|
651
|
+
'❌ 当前模型既没有图片识别能力,也没有视频识别能力,已退出。',
|
|
652
|
+
capabilities.reason ? '模型说明:' + capabilities.reason : '',
|
|
653
|
+
'请切换到支持视觉识别的模型后重试。',
|
|
654
|
+
].filter(Boolean).join('\n'));
|
|
655
|
+
}
|
|
656
|
+
|
|
381
657
|
const tools = deployDouyinTools(root);
|
|
382
658
|
if (!tools.ok) {
|
|
383
659
|
return notify(session, 'error', '❌ 部署抖音脚本失败:\n' + (tools.problems.join('\n') || '未知错误'));
|
|
@@ -394,19 +670,21 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
394
670
|
return notify(session, 'error', '❌ 启动 Chrome 失败:' + started.error);
|
|
395
671
|
}
|
|
396
672
|
const browser = { cdp: started.cdp, mode: started.mode };
|
|
397
|
-
taskList.advance(
|
|
673
|
+
taskList.advance(2);
|
|
398
674
|
taskList.watchTurns(invocation);
|
|
399
675
|
|
|
400
676
|
try {
|
|
401
|
-
const runDir = path.join(paths.output, stampOfNow());
|
|
402
|
-
fs.mkdirSync(runDir, { recursive: true });
|
|
403
677
|
const crawled = readCrawled(root);
|
|
404
678
|
const failures = [];
|
|
405
679
|
let scannedAccounts = 0;
|
|
406
680
|
let foundNotes = 0;
|
|
681
|
+
let foundVideos = 0;
|
|
407
682
|
let skippedNotes = 0;
|
|
408
|
-
let
|
|
683
|
+
let skippedNotesByCapability = 0;
|
|
684
|
+
let skippedVideosByCapability = 0;
|
|
685
|
+
let processedWorks = 0;
|
|
409
686
|
let skippedIrrelevant = 0;
|
|
687
|
+
let skippedLongVideos = 0;
|
|
410
688
|
let inserted = 0;
|
|
411
689
|
let skippedMaterials = 0;
|
|
412
690
|
let aborted = false;
|
|
@@ -434,10 +712,17 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
434
712
|
|
|
435
713
|
scannedAccounts += 1;
|
|
436
714
|
const notes = Array.isArray(scan.json.notes) ? scan.json.notes : [];
|
|
715
|
+
const videos = Array.isArray(scan.json.videos) ? scan.json.videos : [];
|
|
437
716
|
foundNotes += notes.length;
|
|
438
|
-
|
|
717
|
+
foundVideos += videos.length;
|
|
718
|
+
taskList.advance(3);
|
|
719
|
+
|
|
439
720
|
for (const card of notes) {
|
|
440
721
|
if (abortRequested(invocation)) { aborted = true; break outer; }
|
|
722
|
+
if (!capabilities.image_supported) {
|
|
723
|
+
skippedNotesByCapability += 1;
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
441
726
|
const noteId = noteIdFromUrl(card.href);
|
|
442
727
|
if (!noteId) continue;
|
|
443
728
|
if (crawled[noteId]) {
|
|
@@ -451,7 +736,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
451
736
|
'--out', noteDir, '--login-state', paths.loginState],
|
|
452
737
|
noteDir,
|
|
453
738
|
paths,
|
|
454
|
-
'
|
|
739
|
+
'采集图文作品',
|
|
455
740
|
tools.nodePath,
|
|
456
741
|
browser,
|
|
457
742
|
tools,
|
|
@@ -466,10 +751,10 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
466
751
|
failures.push({ id: noteId, url: card.href, reason: '没有截图' });
|
|
467
752
|
continue;
|
|
468
753
|
}
|
|
469
|
-
|
|
754
|
+
processedWorks += 1;
|
|
470
755
|
note.account_url = account.url;
|
|
471
756
|
const target = path.join(noteDir, 'materials.jsonl');
|
|
472
|
-
taskList.advance(
|
|
757
|
+
taskList.advance(4);
|
|
473
758
|
taskList.progress(`LLM 整理图文素材(${note.title || noteId})`);
|
|
474
759
|
const analyzed = await analyzeNote(ctx, invocation, session, {
|
|
475
760
|
root,
|
|
@@ -492,6 +777,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
492
777
|
account_url: account.url,
|
|
493
778
|
url: note.url || card.href,
|
|
494
779
|
title: note.title || card.text || '',
|
|
780
|
+
type: 'note',
|
|
495
781
|
screenshots: note.screenshots.map((shot) => shot.file),
|
|
496
782
|
dir: noteDir,
|
|
497
783
|
inserted: 0,
|
|
@@ -506,6 +792,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
506
792
|
account_url: account.url,
|
|
507
793
|
url: note.url || card.href,
|
|
508
794
|
title: note.title || card.text || '',
|
|
795
|
+
type: 'note',
|
|
509
796
|
screenshots: note.screenshots.map((shot) => shot.file),
|
|
510
797
|
dir: noteDir,
|
|
511
798
|
inserted: analyzed.inserted,
|
|
@@ -518,16 +805,158 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
518
805
|
await sleep(delay);
|
|
519
806
|
}
|
|
520
807
|
}
|
|
808
|
+
|
|
809
|
+
for (const card of videos) {
|
|
810
|
+
if (abortRequested(invocation)) { aborted = true; break outer; }
|
|
811
|
+
if (!capabilities.video_supported) {
|
|
812
|
+
skippedVideosByCapability += 1;
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
const videoId = noteIdFromUrl(card.href);
|
|
816
|
+
if (!videoId) continue;
|
|
817
|
+
if (crawled[videoId]) {
|
|
818
|
+
skippedNotes += 1;
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
821
|
+
const videoDir = path.join(accountDir, videoId);
|
|
822
|
+
const videoResult = await runWithLogin(
|
|
823
|
+
ctx, invocation, session, root, tools.scripts.video,
|
|
824
|
+
['--workspace', root, '--video', card.href, '--cdp', browser.cdp,
|
|
825
|
+
'--out', videoDir, '--login-state', paths.loginState,
|
|
826
|
+
'--max-seconds', String(MAX_VIDEO_SECONDS)],
|
|
827
|
+
videoDir,
|
|
828
|
+
paths,
|
|
829
|
+
'采集视频作品',
|
|
830
|
+
tools.nodePath,
|
|
831
|
+
browser,
|
|
832
|
+
tools,
|
|
833
|
+
VIDEO_SCRIPT_TIMEOUT_MS,
|
|
834
|
+
);
|
|
835
|
+
if (!videoResult.ok) {
|
|
836
|
+
failures.push({ id: videoId, url: card.href, reason: videoResult.error });
|
|
837
|
+
if (videoResult.cancelled) { aborted = true; break outer; }
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
const video = videoResult.json.video || {};
|
|
841
|
+
if (videoResult.json.skip) {
|
|
842
|
+
skippedLongVideos += 1;
|
|
843
|
+
markCrawled(root, videoId, {
|
|
844
|
+
account_url: account.url,
|
|
845
|
+
url: video.url || card.href,
|
|
846
|
+
title: video.title || card.text || '',
|
|
847
|
+
type: 'video',
|
|
848
|
+
duration: video.duration,
|
|
849
|
+
dir: videoDir,
|
|
850
|
+
inserted: 0,
|
|
851
|
+
skipped: 0,
|
|
852
|
+
skip: true,
|
|
853
|
+
reason: videoResult.json.reason || '视频时长超过 1 分钟',
|
|
854
|
+
});
|
|
855
|
+
crawled[videoId] = true;
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (!video || !Array.isArray(video.frames) || !video.frames.length) {
|
|
859
|
+
failures.push({ id: videoId, url: card.href, reason: '没有视频关键帧' });
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
processedWorks += 1;
|
|
863
|
+
video.account_url = account.url;
|
|
864
|
+
const target = path.join(videoDir, 'materials.jsonl');
|
|
865
|
+
taskList.advance(4);
|
|
866
|
+
taskList.progress(`LLM 判断视频前 5 秒(${video.title || videoId})`);
|
|
867
|
+
const relevance = await analyzeVideoRelevance(ctx, invocation, session, {
|
|
868
|
+
root,
|
|
869
|
+
accountUrl: account.url,
|
|
870
|
+
video,
|
|
871
|
+
target,
|
|
872
|
+
taskList,
|
|
873
|
+
});
|
|
874
|
+
if (!relevance.ok) {
|
|
875
|
+
failures.push({ id: videoId, url: card.href, reason: relevance.error });
|
|
876
|
+
if (relevance.aborted) { aborted = true; break outer; }
|
|
877
|
+
continue;
|
|
878
|
+
}
|
|
879
|
+
if (relevance.skip) {
|
|
880
|
+
skippedIrrelevant += 1;
|
|
881
|
+
markCrawled(root, videoId, {
|
|
882
|
+
account_url: account.url,
|
|
883
|
+
url: video.url || card.href,
|
|
884
|
+
title: video.title || card.text || '',
|
|
885
|
+
type: 'video',
|
|
886
|
+
duration: video.duration,
|
|
887
|
+
dir: videoDir,
|
|
888
|
+
inserted: 0,
|
|
889
|
+
skipped: 0,
|
|
890
|
+
skip: true,
|
|
891
|
+
reason: relevance.reason || '前 5 秒与网文创作无关',
|
|
892
|
+
});
|
|
893
|
+
crawled[videoId] = true;
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
taskList.progress(`LLM 整理视频素材(${video.title || videoId})`);
|
|
898
|
+
const analyzed = await analyzeVideo(ctx, invocation, session, {
|
|
899
|
+
root,
|
|
900
|
+
accountUrl: account.url,
|
|
901
|
+
video,
|
|
902
|
+
target,
|
|
903
|
+
materialTools,
|
|
904
|
+
taskList,
|
|
905
|
+
});
|
|
906
|
+
if (!analyzed.ok) {
|
|
907
|
+
failures.push({ id: videoId, url: card.href, reason: analyzed.error });
|
|
908
|
+
if (analyzed.aborted) { aborted = true; break outer; }
|
|
909
|
+
if (analyzed.fatal) break outer;
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
if (analyzed.skipped) {
|
|
913
|
+
skippedIrrelevant += 1;
|
|
914
|
+
markCrawled(root, videoId, {
|
|
915
|
+
account_url: account.url,
|
|
916
|
+
url: video.url || card.href,
|
|
917
|
+
title: video.title || card.text || '',
|
|
918
|
+
type: 'video',
|
|
919
|
+
duration: video.duration,
|
|
920
|
+
dir: videoDir,
|
|
921
|
+
inserted: 0,
|
|
922
|
+
skipped: 0,
|
|
923
|
+
skip: true,
|
|
924
|
+
reason: analyzed.reason || '与网文创作无关',
|
|
925
|
+
});
|
|
926
|
+
} else {
|
|
927
|
+
inserted += analyzed.inserted;
|
|
928
|
+
skippedMaterials += analyzed.skipped;
|
|
929
|
+
markCrawled(root, videoId, {
|
|
930
|
+
account_url: account.url,
|
|
931
|
+
url: video.url || card.href,
|
|
932
|
+
title: video.title || card.text || '',
|
|
933
|
+
type: 'video',
|
|
934
|
+
duration: video.duration,
|
|
935
|
+
dir: videoDir,
|
|
936
|
+
inserted: analyzed.inserted,
|
|
937
|
+
skipped: analyzed.skipped,
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
crawled[videoId] = true;
|
|
941
|
+
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
942
|
+
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
943
|
+
await sleep(delay);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
521
946
|
}
|
|
522
947
|
|
|
523
|
-
taskList.advance(
|
|
948
|
+
taskList.advance(5);
|
|
524
949
|
return notify(session, (aborted || failures.length) ? 'error' : 'success', summaryText({
|
|
525
950
|
accounts: accounts.length,
|
|
526
951
|
scannedAccounts,
|
|
527
952
|
foundNotes,
|
|
953
|
+
foundVideos,
|
|
954
|
+
skippedNotesByCapability,
|
|
955
|
+
skippedVideosByCapability,
|
|
528
956
|
skippedNotes,
|
|
529
|
-
processedNotes,
|
|
957
|
+
processedNotes: processedWorks,
|
|
530
958
|
skippedIrrelevant,
|
|
959
|
+
skippedLongVideos,
|
|
531
960
|
inserted,
|
|
532
961
|
skippedMaterials,
|
|
533
962
|
failures,
|
|
@@ -580,13 +1009,20 @@ function douyinTaskTitles(invocation) {
|
|
|
580
1009
|
if (command === 'add') return ['解析主页地址', '保存账号'];
|
|
581
1010
|
if (command === 'list') return ['读取账号列表'];
|
|
582
1011
|
if (command === 'remove') return ['解析目标账号', '删除账号'];
|
|
583
|
-
return [
|
|
1012
|
+
return [
|
|
1013
|
+
'检查账号与素材库',
|
|
1014
|
+
'LLM 能力自检(图片 / 视频)',
|
|
1015
|
+
'扫描账号主页',
|
|
1016
|
+
'采集图文 / 视频作品',
|
|
1017
|
+
'LLM 整理素材',
|
|
1018
|
+
'写入素材库',
|
|
1019
|
+
];
|
|
584
1020
|
}
|
|
585
1021
|
|
|
586
1022
|
export async function apply(ctx) {
|
|
587
1023
|
registerCommandWithTasks(ctx, {
|
|
588
1024
|
name: 'fancy-douyin',
|
|
589
|
-
description: '🎬
|
|
1025
|
+
description: '🎬 抖音素材采集 ( usage: /fancy-douyin [add <主页> | list | remove <序号|地址> | help] )',
|
|
590
1026
|
input: { hint: '[add <主页> | list | remove <序号|地址> | help]' },
|
|
591
1027
|
handler: async (invocation, taskList) => {
|
|
592
1028
|
const session = invocation.agent.session;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
你正在为 `/fancy-douyin` 做视觉能力自检。
|
|
2
|
+
|
|
3
|
+
请只根据你当前模型的真实能力回答,不要猜测,也不要尝试 OCR、tesseract、外部识别接口或任何文字识别替代方案。
|
|
4
|
+
|
|
5
|
+
- **图片识别**:你能用 `read` 工具打开图片文件,并真正看到画面内容吗?
|
|
6
|
+
- **视频识别**:插件会把视频按时间抽成关键帧图片;你能读取这些关键帧,并据此还原视频里发生了什么吗?
|
|
7
|
+
|
|
8
|
+
把结果写入下面的绝对路径(覆盖写):
|
|
9
|
+
|
|
10
|
+
```text
|
|
11
|
+
{{CAPABILITY_FILE}}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
严格写成一个 JSON 对象,不要 Markdown 代码围栏、解释或前后缀:
|
|
15
|
+
|
|
16
|
+
```json
|
|
17
|
+
{"image_supported": true, "video_supported": true, "reason": "当前模型可以读取图片和视频关键帧"}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
不具备的能力写 `false`。如果不确定,也写 `false`。
|
|
21
|
+
|
|
22
|
+
写完后只回复:`已写入 {{CAPABILITY_FILE}}`。
|