@cloud411716/fancy-webnovel 4.8.2 → 4.10.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 +32 -6
- package/docs/workspace-mcp.md +7 -5
- package/package.json +1 -1
- package/plugins/fancy-bootstrap/index.js +1 -1
- package/plugins/fancy-douyin/deploy.js +5 -4
- package/plugins/fancy-douyin/index.js +409 -129
- package/plugins/fancy-douyin/prompts/03-article-prompt.md +94 -0
- package/plugins/fancy-douyin/scripts/crawl-article.cjs +146 -0
- package/plugins/fancy-douyin/scripts/scan-account.cjs +32 -8
- package/plugins/fancy-douyin/state.js +45 -9
- package/plugins/fancy-douyin/subagents.js +70 -0
- package/plugins/fancy-douyin/templates.js +40 -4
|
@@ -31,8 +31,10 @@ import {
|
|
|
31
31
|
readAccounts,
|
|
32
32
|
readCrawled,
|
|
33
33
|
removeAccount,
|
|
34
|
+
updateAccountProfile,
|
|
34
35
|
} from './state.js';
|
|
35
|
-
import { analysisPrompt, capabilityPrompt, helpText, loginQuestion, summaryText, videoAnalysisPrompt, videoRelevancePrompt } from './templates.js';
|
|
36
|
+
import { analysisPrompt, articlePrompt, capabilityPrompt, helpText, loginQuestion, summaryText, videoAnalysisPrompt, videoRelevancePrompt } from './templates.js';
|
|
37
|
+
import { createSerialLock, getSubagentRuntime, runJobPool, subagentWorkerCount } from './subagents.js';
|
|
36
38
|
|
|
37
39
|
export const inject = ['commands', 'userQuestions'];
|
|
38
40
|
|
|
@@ -455,6 +457,36 @@ async function detectCapabilities(ctx, invocation, session, taskList, root, runD
|
|
|
455
457
|
return { ok: false, error: 'LLM 没有返回可解析的能力自检结果' };
|
|
456
458
|
}
|
|
457
459
|
|
|
460
|
+
async function finalizeMaterialFile({ root, target, title, materialTools }) {
|
|
461
|
+
const skip = readSkipMarker(target);
|
|
462
|
+
if (skip) {
|
|
463
|
+
return {
|
|
464
|
+
ok: true,
|
|
465
|
+
skipped: true,
|
|
466
|
+
reason: skip.reason,
|
|
467
|
+
inserted: 0,
|
|
468
|
+
skipped: 0,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
try {
|
|
472
|
+
normalizeOtherFile(target);
|
|
473
|
+
} catch (err) {
|
|
474
|
+
return { ok: false, detail: err && err.message ? err.message : String(err) };
|
|
475
|
+
}
|
|
476
|
+
const checked = await validateOtherFile(target, materialTools.validateScript);
|
|
477
|
+
if (!checked.ok) return { ok: false, detail: checked.detail };
|
|
478
|
+
const kindProblem = kindLanguageProblem(target);
|
|
479
|
+
if (kindProblem) return { ok: false, detail: kindProblem };
|
|
480
|
+
const coverage = coverageProblem(target, title);
|
|
481
|
+
if (coverage) return { ok: false, detail: coverage };
|
|
482
|
+
try {
|
|
483
|
+
const result = await importOtherMaterials(root, target);
|
|
484
|
+
return { ok: true, ...result };
|
|
485
|
+
} catch (err) {
|
|
486
|
+
return { ok: false, error: '导入数据库失败:' + (err && err.message ? err.message : String(err)) };
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
458
490
|
async function analyzeMaterials(ctx, invocation, session, {
|
|
459
491
|
root, prompt, target, title, materialTools, taskList,
|
|
460
492
|
}) {
|
|
@@ -497,44 +529,10 @@ async function analyzeMaterials(ctx, invocation, session, {
|
|
|
497
529
|
continue;
|
|
498
530
|
}
|
|
499
531
|
|
|
500
|
-
const
|
|
501
|
-
if (
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
skipped: true,
|
|
505
|
-
reason: skip.reason,
|
|
506
|
-
inserted: 0,
|
|
507
|
-
skipped: 0,
|
|
508
|
-
};
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
try {
|
|
512
|
-
normalizeOtherFile(target);
|
|
513
|
-
} catch (err) {
|
|
514
|
-
detail = err && err.message ? err.message : String(err);
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
const checked = await validateOtherFile(target, materialTools.validateScript);
|
|
518
|
-
if (!checked.ok) {
|
|
519
|
-
detail = checked.detail;
|
|
520
|
-
continue;
|
|
521
|
-
}
|
|
522
|
-
const kindProblem = kindLanguageProblem(target);
|
|
523
|
-
if (kindProblem) {
|
|
524
|
-
detail = kindProblem;
|
|
525
|
-
continue;
|
|
526
|
-
}
|
|
527
|
-
const coverage = coverageProblem(target, title);
|
|
528
|
-
if (coverage) {
|
|
529
|
-
detail = coverage;
|
|
530
|
-
continue;
|
|
531
|
-
}
|
|
532
|
-
try {
|
|
533
|
-
const result = await importOtherMaterials(root, target);
|
|
534
|
-
return { ok: true, ...result };
|
|
535
|
-
} catch (err) {
|
|
536
|
-
return { ok: false, error: '导入数据库失败:' + (err && err.message ? err.message : String(err)) };
|
|
537
|
-
}
|
|
532
|
+
const finalized = await finalizeMaterialFile({ root, target, title, materialTools });
|
|
533
|
+
if (finalized.ok) return finalized;
|
|
534
|
+
if (finalized.error) return finalized;
|
|
535
|
+
detail = finalized.detail;
|
|
538
536
|
}
|
|
539
537
|
return { ok: false, error: '素材没有通过校验:' + detail };
|
|
540
538
|
}
|
|
@@ -631,6 +629,146 @@ async function analyzeVideo(ctx, invocation, session, {
|
|
|
631
629
|
});
|
|
632
630
|
}
|
|
633
631
|
|
|
632
|
+
async function analyzeArticle(ctx, invocation, session, {
|
|
633
|
+
root, accountUrl, article, target, materialTools, taskList,
|
|
634
|
+
}) {
|
|
635
|
+
const prompt = articlePrompt({
|
|
636
|
+
accountUrl,
|
|
637
|
+
article,
|
|
638
|
+
targetFile: target,
|
|
639
|
+
workspace: root,
|
|
640
|
+
queryScript: materialTools.queryScript,
|
|
641
|
+
validateScript: materialTools.validateScript,
|
|
642
|
+
});
|
|
643
|
+
return analyzeMaterials(ctx, invocation, session, {
|
|
644
|
+
root,
|
|
645
|
+
prompt,
|
|
646
|
+
target: target,
|
|
647
|
+
title: article.title,
|
|
648
|
+
materialTools,
|
|
649
|
+
taskList,
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function analysisPromptForJob(job, root, materialTools) {
|
|
654
|
+
if (job.type === 'note') {
|
|
655
|
+
return analysisPrompt({
|
|
656
|
+
accountUrl: job.accountUrl,
|
|
657
|
+
note: job.note,
|
|
658
|
+
screenshots: job.screenshots,
|
|
659
|
+
targetFile: job.target,
|
|
660
|
+
workspace: root,
|
|
661
|
+
queryScript: materialTools.queryScript,
|
|
662
|
+
validateScript: materialTools.validateScript,
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
if (job.type === 'video') {
|
|
666
|
+
return videoAnalysisPrompt({
|
|
667
|
+
accountUrl: job.accountUrl,
|
|
668
|
+
video: job.video,
|
|
669
|
+
frames: job.video.frames || [],
|
|
670
|
+
targetFile: job.target,
|
|
671
|
+
workspace: root,
|
|
672
|
+
queryScript: materialTools.queryScript,
|
|
673
|
+
validateScript: materialTools.validateScript,
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
return articlePrompt({
|
|
677
|
+
accountUrl: job.accountUrl,
|
|
678
|
+
article: job.article,
|
|
679
|
+
targetFile: job.target,
|
|
680
|
+
workspace: root,
|
|
681
|
+
queryScript: materialTools.queryScript,
|
|
682
|
+
validateScript: materialTools.validateScript,
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function subagentTaskPrompt(job, root, materialTools) {
|
|
687
|
+
return [
|
|
688
|
+
'你是 fancy-douyin 的分析子 Agent,只处理下面这一篇作品。',
|
|
689
|
+
'不要扫描账号,不要启动或操作 Chrome,不要修改 crawled.json,不要运行 import.js 或直接写素材数据库。',
|
|
690
|
+
'只读取给定文件,按提示词提取素材,写入目标 JSONL,运行 validate.js 后结束。',
|
|
691
|
+
'',
|
|
692
|
+
analysisPromptForJob(job, root, materialTools),
|
|
693
|
+
].join('\n');
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
async function runSubagentAnalysisJob({
|
|
697
|
+
subagents, provider, invocation, job, root, materialTools, signal,
|
|
698
|
+
}) {
|
|
699
|
+
const prompt = subagentTaskPrompt(job, root, materialTools);
|
|
700
|
+
let activation;
|
|
701
|
+
try {
|
|
702
|
+
activation = await subagents.startActivation({
|
|
703
|
+
provider,
|
|
704
|
+
label: 'fancy-douyin ' + job.type + ' ' + job.id,
|
|
705
|
+
parent: invocation.agent,
|
|
706
|
+
prompt: [{ type: 'text', text: prompt }],
|
|
707
|
+
signal,
|
|
708
|
+
delivery: 'caller',
|
|
709
|
+
});
|
|
710
|
+
} catch (err) {
|
|
711
|
+
return { ok: false, error: '子 Agent 启动失败:' + (err && err.message ? err.message : String(err)) };
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
let result;
|
|
715
|
+
let timer = null;
|
|
716
|
+
try {
|
|
717
|
+
result = await Promise.race([
|
|
718
|
+
activation.result,
|
|
719
|
+
new Promise((_, reject) => {
|
|
720
|
+
timer = setTimeout(() => reject(new Error('子 Agent 分析超时')), ANALYSIS_TIMEOUT_MS);
|
|
721
|
+
if (timer && typeof timer.unref === 'function') timer.unref();
|
|
722
|
+
}),
|
|
723
|
+
]);
|
|
724
|
+
} catch (err) {
|
|
725
|
+
return { ok: false, error: '子 Agent 执行失败:' + (err && err.message ? err.message : String(err)) };
|
|
726
|
+
} finally {
|
|
727
|
+
if (timer) clearTimeout(timer);
|
|
728
|
+
try { await activation.dispose(); } catch { /* best effort */ }
|
|
729
|
+
}
|
|
730
|
+
if (!fs.existsSync(job.target)) {
|
|
731
|
+
const stop = result && result.stopReason ? '(' + result.stopReason + ')' : '';
|
|
732
|
+
return { ok: false, error: '子 Agent 没有写入目标文件' + stop };
|
|
733
|
+
}
|
|
734
|
+
// 文件已经写出,由父进程在串行锁内统一校验、导入和写 crawled.json。
|
|
735
|
+
return { ok: true, needsFinalize: true };
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
async function runLocalAnalysisJob(ctx, invocation, session, job, {
|
|
739
|
+
root, materialTools, paths, taskList,
|
|
740
|
+
}) {
|
|
741
|
+
if (job.type === 'note') {
|
|
742
|
+
return analyzeNote(ctx, invocation, session, {
|
|
743
|
+
root,
|
|
744
|
+
note: job.note,
|
|
745
|
+
screenshots: job.screenshots,
|
|
746
|
+
target: job.target,
|
|
747
|
+
materialTools,
|
|
748
|
+
paths,
|
|
749
|
+
taskList,
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
if (job.type === 'video') {
|
|
753
|
+
return analyzeVideo(ctx, invocation, session, {
|
|
754
|
+
root,
|
|
755
|
+
accountUrl: job.accountUrl,
|
|
756
|
+
video: job.video,
|
|
757
|
+
target: job.target,
|
|
758
|
+
materialTools,
|
|
759
|
+
taskList,
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
return analyzeArticle(ctx, invocation, session, {
|
|
763
|
+
root,
|
|
764
|
+
accountUrl: job.accountUrl,
|
|
765
|
+
article: job.article,
|
|
766
|
+
target: job.target,
|
|
767
|
+
materialTools,
|
|
768
|
+
taskList,
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
|
|
634
772
|
async function runCrawl(ctx, invocation, session, taskList) {
|
|
635
773
|
const root = workspaceOf(session);
|
|
636
774
|
const paths = ensureDouyinDir(root);
|
|
@@ -651,11 +789,8 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
651
789
|
return notify(session, 'error', '❌ LLM 能力自检失败:' + capabilities.error);
|
|
652
790
|
}
|
|
653
791
|
if (!capabilities.image_supported && !capabilities.video_supported) {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
capabilities.reason ? '模型说明:' + capabilities.reason : '',
|
|
657
|
-
'请切换到支持视觉识别的模型后重试。',
|
|
658
|
-
].filter(Boolean).join('\n'));
|
|
792
|
+
// 文章是纯文本,不依赖视觉能力;无视觉时只跳过图文和视频。
|
|
793
|
+
taskList.progress('无视觉能力,将跳过图文/视频,只处理文章');
|
|
659
794
|
}
|
|
660
795
|
|
|
661
796
|
const tools = deployDouyinTools(root);
|
|
@@ -680,9 +815,46 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
680
815
|
try {
|
|
681
816
|
const crawled = readCrawled(root);
|
|
682
817
|
const failures = [];
|
|
818
|
+
const jobs = [];
|
|
819
|
+
const queued = new Set();
|
|
820
|
+
const subagentRuntime = getSubagentRuntime(ctx);
|
|
821
|
+
const subagentWorkers = subagentRuntime ? subagentWorkerCount() : 0;
|
|
822
|
+
const useSubagents = Boolean(subagentRuntime && subagentWorkers > 0);
|
|
823
|
+
const withStateLock = createSerialLock();
|
|
824
|
+
const abortController = new AbortController();
|
|
825
|
+
if (invocation.signal) {
|
|
826
|
+
if (invocation.signal.aborted) abortController.abort();
|
|
827
|
+
else invocation.signal.addEventListener('abort', () => abortController.abort(), { once: true });
|
|
828
|
+
}
|
|
829
|
+
const jobSignal = abortController.signal;
|
|
830
|
+
const markJobFailed = (job, reason) => {
|
|
831
|
+
if (!job || !job.id) return;
|
|
832
|
+
markCrawled(root, job.id, {
|
|
833
|
+
account_url: job.accountUrl,
|
|
834
|
+
url: job.url || (job.card && job.card.href) || '',
|
|
835
|
+
title: job.title || (job.card && job.card.text) || '',
|
|
836
|
+
type: job.type,
|
|
837
|
+
duration: job.duration,
|
|
838
|
+
screenshots: job.type === 'note' && Array.isArray(job.screenshots)
|
|
839
|
+
? job.screenshots.map((shot) => shot.file)
|
|
840
|
+
: undefined,
|
|
841
|
+
body_file: job.article && job.article.body_file,
|
|
842
|
+
reading_time: job.article && job.article.reading_time,
|
|
843
|
+
body_length: job.article && job.article.body_length,
|
|
844
|
+
dir: job.dir,
|
|
845
|
+
inserted: 0,
|
|
846
|
+
skipped: 0,
|
|
847
|
+
skip: true,
|
|
848
|
+
failed: true,
|
|
849
|
+
reason,
|
|
850
|
+
});
|
|
851
|
+
crawled[job.id] = true;
|
|
852
|
+
};
|
|
683
853
|
let scannedAccounts = 0;
|
|
684
854
|
let foundNotes = 0;
|
|
685
855
|
let foundVideos = 0;
|
|
856
|
+
let foundArticles = 0;
|
|
857
|
+
let skippedArticles = 0;
|
|
686
858
|
let skippedNotes = 0;
|
|
687
859
|
let skippedNotesByCapability = 0;
|
|
688
860
|
let skippedVideosByCapability = 0;
|
|
@@ -732,10 +904,15 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
732
904
|
}
|
|
733
905
|
|
|
734
906
|
scannedAccounts += 1;
|
|
907
|
+
if (scan.json.profile) {
|
|
908
|
+
updateAccountProfile(root, account.url, scan.json.profile);
|
|
909
|
+
}
|
|
735
910
|
const notes = Array.isArray(scan.json.notes) ? scan.json.notes : [];
|
|
736
911
|
const videos = Array.isArray(scan.json.videos) ? scan.json.videos : [];
|
|
912
|
+
const articles = Array.isArray(scan.json.articles) ? scan.json.articles : [];
|
|
737
913
|
foundNotes += notes.length;
|
|
738
914
|
foundVideos += videos.length;
|
|
915
|
+
foundArticles += articles.length;
|
|
739
916
|
taskList.advance(3);
|
|
740
917
|
|
|
741
918
|
for (const card of notes) {
|
|
@@ -746,7 +923,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
746
923
|
}
|
|
747
924
|
const noteId = noteIdFromUrl(card.href);
|
|
748
925
|
if (!noteId) continue;
|
|
749
|
-
if (crawled[noteId]) {
|
|
926
|
+
if (crawled[noteId] || queued.has(noteId)) {
|
|
750
927
|
skippedNotes += 1;
|
|
751
928
|
continue;
|
|
752
929
|
}
|
|
@@ -777,53 +954,19 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
777
954
|
processedWorks += 1;
|
|
778
955
|
note.account_url = account.url;
|
|
779
956
|
const target = path.join(noteDir, 'materials.jsonl');
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
957
|
+
jobs.push({
|
|
958
|
+
type: 'note',
|
|
959
|
+
id: noteId,
|
|
960
|
+
card,
|
|
961
|
+
accountUrl: account.url,
|
|
962
|
+
url: note.url || card.href,
|
|
963
|
+
dir: noteDir,
|
|
784
964
|
note,
|
|
785
965
|
screenshots: note.screenshots,
|
|
786
966
|
target,
|
|
787
|
-
|
|
788
|
-
paths,
|
|
789
|
-
taskList,
|
|
967
|
+
title: note.title,
|
|
790
968
|
});
|
|
791
|
-
|
|
792
|
-
failures.push({ id: noteId, url: card.href, reason: analyzed.error });
|
|
793
|
-
if (analyzed.aborted) { aborted = true; break outer; }
|
|
794
|
-
if (analyzed.fatal) break outer;
|
|
795
|
-
markFailed(noteId, card, { type: 'note', dir: noteDir, title: note.title }, analyzed.error);
|
|
796
|
-
continue;
|
|
797
|
-
}
|
|
798
|
-
if (analyzed.skipped) {
|
|
799
|
-
skippedIrrelevant += 1;
|
|
800
|
-
markCrawled(root, noteId, {
|
|
801
|
-
account_url: account.url,
|
|
802
|
-
url: note.url || card.href,
|
|
803
|
-
title: note.title || card.text || '',
|
|
804
|
-
type: 'note',
|
|
805
|
-
screenshots: note.screenshots.map((shot) => shot.file),
|
|
806
|
-
dir: noteDir,
|
|
807
|
-
inserted: 0,
|
|
808
|
-
skipped: 0,
|
|
809
|
-
skip: true,
|
|
810
|
-
reason: analyzed.reason || '与网文创作无关',
|
|
811
|
-
});
|
|
812
|
-
} else {
|
|
813
|
-
inserted += analyzed.inserted;
|
|
814
|
-
skippedMaterials += analyzed.skipped;
|
|
815
|
-
markCrawled(root, noteId, {
|
|
816
|
-
account_url: account.url,
|
|
817
|
-
url: note.url || card.href,
|
|
818
|
-
title: note.title || card.text || '',
|
|
819
|
-
type: 'note',
|
|
820
|
-
screenshots: note.screenshots.map((shot) => shot.file),
|
|
821
|
-
dir: noteDir,
|
|
822
|
-
inserted: analyzed.inserted,
|
|
823
|
-
skipped: analyzed.skipped,
|
|
824
|
-
});
|
|
825
|
-
}
|
|
826
|
-
crawled[noteId] = true;
|
|
969
|
+
queued.add(noteId);
|
|
827
970
|
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
828
971
|
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
829
972
|
await sleep(delay);
|
|
@@ -838,7 +981,7 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
838
981
|
}
|
|
839
982
|
const videoId = noteIdFromUrl(card.href);
|
|
840
983
|
if (!videoId) continue;
|
|
841
|
-
if (crawled[videoId]) {
|
|
984
|
+
if (crawled[videoId] || queued.has(videoId)) {
|
|
842
985
|
skippedNotes += 1;
|
|
843
986
|
continue;
|
|
844
987
|
}
|
|
@@ -921,51 +1064,72 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
921
1064
|
continue;
|
|
922
1065
|
}
|
|
923
1066
|
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
1067
|
+
jobs.push({
|
|
1068
|
+
type: 'video',
|
|
1069
|
+
id: videoId,
|
|
1070
|
+
card,
|
|
927
1071
|
accountUrl: account.url,
|
|
1072
|
+
url: video.url || card.href,
|
|
1073
|
+
dir: videoDir,
|
|
928
1074
|
video,
|
|
929
1075
|
target,
|
|
930
|
-
|
|
931
|
-
|
|
1076
|
+
title: video.title,
|
|
1077
|
+
duration: video.duration,
|
|
932
1078
|
});
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
1079
|
+
queued.add(videoId);
|
|
1080
|
+
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
1081
|
+
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
1082
|
+
await sleep(delay);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
for (const card of articles) {
|
|
1087
|
+
if (abortRequested(invocation)) { aborted = true; break outer; }
|
|
1088
|
+
const articleId = noteIdFromUrl(card.href);
|
|
1089
|
+
if (!articleId) continue;
|
|
1090
|
+
if (crawled[articleId] || queued.has(articleId)) {
|
|
1091
|
+
skippedArticles += 1;
|
|
938
1092
|
continue;
|
|
939
1093
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
});
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
dir: videoDir,
|
|
964
|
-
inserted: analyzed.inserted,
|
|
965
|
-
skipped: analyzed.skipped,
|
|
966
|
-
});
|
|
1094
|
+
const articleDir = path.join(accountDir, articleId);
|
|
1095
|
+
const articleResult = await runWithLogin(
|
|
1096
|
+
ctx, invocation, session, root, tools.scripts.article,
|
|
1097
|
+
['--workspace', root, '--article', card.href, '--cdp', browser.cdp,
|
|
1098
|
+
'--out', articleDir, '--login-state', paths.loginState],
|
|
1099
|
+
articleDir,
|
|
1100
|
+
paths,
|
|
1101
|
+
'采集文章',
|
|
1102
|
+
tools.nodePath,
|
|
1103
|
+
browser,
|
|
1104
|
+
tools,
|
|
1105
|
+
);
|
|
1106
|
+
if (!articleResult.ok) {
|
|
1107
|
+
failures.push({ id: articleId, url: card.href, reason: articleResult.error });
|
|
1108
|
+
if (articleResult.cancelled || authFailureText(articleResult.error)) { aborted = true; break outer; }
|
|
1109
|
+
markFailed(articleId, card, { type: 'article', dir: articleDir }, articleResult.error);
|
|
1110
|
+
continue;
|
|
1111
|
+
}
|
|
1112
|
+
const article = articleResult.json.article || {};
|
|
1113
|
+
if (!article || !article.body || String(article.body).trim().length < 50) {
|
|
1114
|
+
failures.push({ id: articleId, url: card.href, reason: '文章正文为空' });
|
|
1115
|
+
markFailed(articleId, card, { type: 'article', dir: articleDir, title: article.title }, '文章正文为空');
|
|
1116
|
+
continue;
|
|
967
1117
|
}
|
|
968
|
-
|
|
1118
|
+
processedWorks += 1;
|
|
1119
|
+
article.account_url = account.url;
|
|
1120
|
+
const target = path.join(articleDir, 'materials.jsonl');
|
|
1121
|
+
jobs.push({
|
|
1122
|
+
type: 'article',
|
|
1123
|
+
id: articleId,
|
|
1124
|
+
card,
|
|
1125
|
+
accountUrl: account.url,
|
|
1126
|
+
url: article.url || card.href,
|
|
1127
|
+
dir: articleDir,
|
|
1128
|
+
article,
|
|
1129
|
+
target,
|
|
1130
|
+
title: article.title,
|
|
1131
|
+
});
|
|
1132
|
+
queued.add(articleId);
|
|
969
1133
|
if (NOTE_DELAY_MIN_MS < NOTE_DELAY_MAX_MS) {
|
|
970
1134
|
const delay = NOTE_DELAY_MIN_MS + Math.floor(Math.random() * (NOTE_DELAY_MAX_MS - NOTE_DELAY_MIN_MS));
|
|
971
1135
|
await sleep(delay);
|
|
@@ -973,15 +1137,129 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
973
1137
|
}
|
|
974
1138
|
}
|
|
975
1139
|
|
|
1140
|
+
if (jobs.length) {
|
|
1141
|
+
taskList.advance(4);
|
|
1142
|
+
let finishedJobs = 0;
|
|
1143
|
+
const progressText = () => useSubagents
|
|
1144
|
+
? `子 Agent 并行分析(${finishedJobs}/${jobs.length})`
|
|
1145
|
+
: `LLM 整理素材(${finishedJobs}/${jobs.length})`;
|
|
1146
|
+
taskList.progress(progressText());
|
|
1147
|
+
|
|
1148
|
+
const finalizeJob = async (job, analyzed) => {
|
|
1149
|
+
await withStateLock(async () => {
|
|
1150
|
+
if (analyzed && analyzed.needsFinalize) {
|
|
1151
|
+
analyzed = await finalizeMaterialFile({
|
|
1152
|
+
root,
|
|
1153
|
+
target: job.target,
|
|
1154
|
+
title: job.title,
|
|
1155
|
+
materialTools,
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
if (analyzed && analyzed.aborted) {
|
|
1159
|
+
aborted = true;
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1162
|
+
if (!analyzed || !analyzed.ok) {
|
|
1163
|
+
const reason = (analyzed && (analyzed.error || analyzed.detail)) || '素材没有通过校验';
|
|
1164
|
+
failures.push({ id: job.id, url: job.url, reason });
|
|
1165
|
+
markJobFailed(job, reason);
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
if (analyzed.skipped) {
|
|
1169
|
+
skippedIrrelevant += 1;
|
|
1170
|
+
markCrawled(root, job.id, {
|
|
1171
|
+
account_url: job.accountUrl,
|
|
1172
|
+
url: job.url,
|
|
1173
|
+
title: job.title || (job.card && job.card.text) || '',
|
|
1174
|
+
type: job.type,
|
|
1175
|
+
duration: job.duration,
|
|
1176
|
+
screenshots: job.type === 'note' && Array.isArray(job.screenshots)
|
|
1177
|
+
? job.screenshots.map((shot) => shot.file)
|
|
1178
|
+
: undefined,
|
|
1179
|
+
body_file: job.article && job.article.body_file,
|
|
1180
|
+
reading_time: job.article && job.article.reading_time,
|
|
1181
|
+
body_length: job.article && job.article.body_length,
|
|
1182
|
+
dir: job.dir,
|
|
1183
|
+
inserted: 0,
|
|
1184
|
+
skipped: 0,
|
|
1185
|
+
skip: true,
|
|
1186
|
+
reason: analyzed.reason || '与网文创作无关',
|
|
1187
|
+
});
|
|
1188
|
+
} else {
|
|
1189
|
+
inserted += analyzed.inserted || 0;
|
|
1190
|
+
skippedMaterials += analyzed.skipped || 0;
|
|
1191
|
+
markCrawled(root, job.id, {
|
|
1192
|
+
account_url: job.accountUrl,
|
|
1193
|
+
url: job.url,
|
|
1194
|
+
title: job.title || (job.card && job.card.text) || '',
|
|
1195
|
+
type: job.type,
|
|
1196
|
+
duration: job.duration,
|
|
1197
|
+
screenshots: job.type === 'note' && Array.isArray(job.screenshots)
|
|
1198
|
+
? job.screenshots.map((shot) => shot.file)
|
|
1199
|
+
: undefined,
|
|
1200
|
+
body_file: job.article && job.article.body_file,
|
|
1201
|
+
reading_time: job.article && job.article.reading_time,
|
|
1202
|
+
body_length: job.article && job.article.body_length,
|
|
1203
|
+
dir: job.dir,
|
|
1204
|
+
inserted: analyzed.inserted || 0,
|
|
1205
|
+
skipped: analyzed.skipped || 0,
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
crawled[job.id] = true;
|
|
1209
|
+
finishedJobs += 1;
|
|
1210
|
+
taskList.progress(progressText());
|
|
1211
|
+
});
|
|
1212
|
+
};
|
|
1213
|
+
|
|
1214
|
+
if (useSubagents) {
|
|
1215
|
+
taskList.progress(`子 Agent 并行分析(0/${jobs.length},并发 ${subagentWorkers})`);
|
|
1216
|
+
await runJobPool(jobs, subagentWorkers, async (job) => {
|
|
1217
|
+
if (abortRequested(invocation)) { aborted = true; return; }
|
|
1218
|
+
let analyzed;
|
|
1219
|
+
try {
|
|
1220
|
+
analyzed = await runSubagentAnalysisJob({
|
|
1221
|
+
subagents: subagentRuntime,
|
|
1222
|
+
provider: 'spawn',
|
|
1223
|
+
invocation,
|
|
1224
|
+
job,
|
|
1225
|
+
root,
|
|
1226
|
+
materialTools,
|
|
1227
|
+
signal: jobSignal,
|
|
1228
|
+
});
|
|
1229
|
+
} catch (err) {
|
|
1230
|
+
analyzed = {
|
|
1231
|
+
ok: false,
|
|
1232
|
+
error: '子 Agent 分析失败:' + (err && err.message ? err.message : String(err)),
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
await finalizeJob(job, analyzed);
|
|
1236
|
+
});
|
|
1237
|
+
} else {
|
|
1238
|
+
for (const job of jobs) {
|
|
1239
|
+
if (abortRequested(invocation)) { aborted = true; break; }
|
|
1240
|
+
const analyzed = await runLocalAnalysisJob(ctx, invocation, session, job, {
|
|
1241
|
+
root,
|
|
1242
|
+
materialTools,
|
|
1243
|
+
paths,
|
|
1244
|
+
taskList,
|
|
1245
|
+
});
|
|
1246
|
+
await finalizeJob(job, analyzed);
|
|
1247
|
+
if (analyzed && analyzed.fatal) { aborted = true; break; }
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
|
|
976
1252
|
taskList.advance(5);
|
|
977
1253
|
return notify(session, (aborted || failures.length) ? 'error' : 'success', summaryText({
|
|
978
1254
|
accounts: accounts.length,
|
|
979
1255
|
scannedAccounts,
|
|
980
1256
|
foundNotes,
|
|
981
1257
|
foundVideos,
|
|
1258
|
+
foundArticles,
|
|
982
1259
|
skippedNotesByCapability,
|
|
983
1260
|
skippedVideosByCapability,
|
|
984
1261
|
skippedNotes,
|
|
1262
|
+
skippedArticles,
|
|
985
1263
|
processedNotes: processedWorks,
|
|
986
1264
|
skippedIrrelevant,
|
|
987
1265
|
skippedLongVideos,
|
|
@@ -992,6 +1270,8 @@ async function runCrawl(ctx, invocation, session, taskList) {
|
|
|
992
1270
|
stateFile: paths.crawled,
|
|
993
1271
|
aborted,
|
|
994
1272
|
mode: browser.mode,
|
|
1273
|
+
analysisMode: useSubagents ? 'subagent' : 'single',
|
|
1274
|
+
subagentWorkers,
|
|
995
1275
|
}));
|
|
996
1276
|
} finally {
|
|
997
1277
|
await stopChrome(root, tools, paths);
|
|
@@ -1041,8 +1321,8 @@ function douyinTaskTitles(invocation) {
|
|
|
1041
1321
|
'检查账号与素材库',
|
|
1042
1322
|
'LLM 能力自检(图片 / 视频)',
|
|
1043
1323
|
'扫描账号主页',
|
|
1044
|
-
'采集图文 /
|
|
1045
|
-
'
|
|
1324
|
+
'采集图文 / 视频 / 文章作品',
|
|
1325
|
+
'分析作品素材',
|
|
1046
1326
|
'写入素材库',
|
|
1047
1327
|
];
|
|
1048
1328
|
}
|