🎬 Open-source, self-hosted AI agent that runs a YouTube channel end to end: researches topics, writes scripts, generates narration, visuals and video, optimizes SEO, and publishes after your approval, then learns from analytics. Works with Gemini, OpenAI, OpenRouter, DeepSeek and more.
JavaScript ai-agentsautomationcontent-automationcontent-creationcontent-strategygoogle-geminijavascriptnodejsopenaiself-hostedseo-optimizationsocial-media-automationthumbnail-generatorvideo-generationyoutube-apiyoutube-automationyoutube-botyoutube-channelyoutube-shortsyoutube-uploader
feat: mine repeated audience requests into approval-gated recommendations
darkzOGx771463fc438b
diff --git a/test.js b/test.jsindex 0c62265..64b5b25 100644--- a/test.js+++ b/test.js@@ -53,7 +53,8 @@ class SystemTest {{ name: 'Reply Draft Lifecycle Store', test: () => this.testReplyDraftStore() },{ name: 'YouTube Scope Detection', test: () => this.testYouTubeScopeDetection() },{ name: 'Audience Comment Sync', test: () => this.testAudienceCommentSync() },- { name: 'Audience Comment Analysis', test: () => this.testAudienceCommentAnalysis() }+ { name: 'Audience Comment Analysis', test: () => this.testAudienceCommentAnalysis() },+ { name: 'Audience Idea Mining', test: () => this.testAudienceIdeaMining() }];let passed = 0;@@ -2606,6 +2607,50 @@ class SystemTest {await db.close();}}++ async testAudienceIdeaMining() {+ const db = new Database();+ await db.initialize();+ const videoId = `vid_mining_${Date.now()}`;+ try {+ for (const suffix of ['m1', 'm2', 'm3']) {+ await db.upsertAudienceComment({+ commentId: `${videoId}_${suffix}`, videoId,+ text: `Please cover local caching next (${suffix})`, publishedAt: new Date().toISOString()+ });+ }+ const service = new AudienceEngagementService(db, null, null, {});+ const insight = {+ videoId, title: 'Mining test', analysisMethod: 'ai',+ themes: [+ { title: 'Cover local caching', summary: 'Repeated requests for a caching deep-dive', kind: 'request',+ count: 3, commentIds: [`${videoId}_m1`, `${videoId}_m2`, `${videoId}_m3`] },+ { title: 'Too few asks', summary: 'Only two', kind: 'request', count: 2, commentIds: [`${videoId}_m1`, `${videoId}_m2`] },+ { title: 'Praise cluster', summary: 'Nice video', kind: 'praise', count: 5, commentIds: [`${videoId}_m1`, `${videoId}_m2`, `${videoId}_m3`] }+ ]+ };+ const saved = await service.refreshAudienceRecommendations(videoId, insight);+ if (saved.length !== 1) throw new Error(`Only the >=3 request/question theme may mine an idea; got ${saved.length}`);+ const recommendation = saved[0];+ if (recommendation.category !== 'audience_demand') throw new Error('Category must be audience_demand');+ if (recommendation.status !== 'pending') throw new Error('Mined ideas must be pending until reviewed');+ if (recommendation.confidence !== 'low') throw new Error('Ask-count 3 maps to low confidence');+ const evidence = recommendation.evidence; // parseLearningRecommendation returns it already parsed+ if (evidence.askCount !== 3 || evidence.sampleComments.length !== 3) throw new Error('Evidence is incomplete');+ if (!evidence.sampleComments[0].permalink.includes('&lc=')) throw new Error('Evidence must carry comment permalinks');+ if (recommendation.proposedChange.autoEditPublishedContent !== false) throw new Error('autoEditPublishedContent must be false');++ const again = await service.refreshAudienceRecommendations(videoId, insight);+ if (again[0].id !== recommendation.id) throw new Error('Re-analysis must dedupe by fingerprint, not duplicate');++ const nonAI = await service.refreshAudienceRecommendations(videoId, { ...insight, analysisMethod: 'fallback' });+ if (nonAI.length !== 0) throw new Error('Fallback analysis must never mine ideas');+ } finally {+ await db.executeQuery("DELETE FROM learning_recommendations WHERE category = 'audience_demand' AND evidence LIKE ?", [`%${videoId}%`]);+ await db.executeQuery('DELETE FROM audience_comments WHERE video_id = ?', [videoId]);+ await db.close();+ }+ }}// Run tests if called directlydiff --git a/utils/audience-engagement-service.js b/utils/audience-engagement-service.jsindex b8ac9fd..b1ba0fd 100644--- a/utils/audience-engagement-service.js+++ b/utils/audience-engagement-service.js@@ -280,9 +280,56 @@ Comments: ${JSON.stringify(payload)}`;return insight;}- // Fully replaced in the idea-mining task; must exist so analyzeVideo can call it.- async refreshAudienceRecommendations(_videoId, _insight) {- return [];+ normalizedTopic(title) {+ return String(title || '').toLowerCase().replace(/[^a-z0-9 ]/g, ' ').replace(/\s+/g, ' ').trim();+ }++ audienceFingerprint(videoId, topic) {+ return crypto.createHash('sha256')+ .update(JSON.stringify({ category: 'audience_demand', videoId, topic }))+ .digest('hex');+ }++ confidenceForAskCount(count) {+ if (count >= 10) return 'high';+ if (count >= 5) return 'medium';+ return 'low';+ }++ async refreshAudienceRecommendations(videoId, insight) {+ if (insight?.analysisMethod !== 'ai') return [];+ const saved = [];+ for (const theme of insight.themes || []) {+ if (!['request', 'question'].includes(theme.kind)) continue;+ if ((theme.count || 0) < 3) continue;+ const topic = this.normalizedTopic(theme.title);+ if (!topic) continue;+ const sampleComments = [];+ for (const commentId of (theme.commentIds || []).slice(0, 5)) {+ const comment = await this.db.getAudienceComment(commentId);+ if (!comment) continue;+ sampleComments.push({+ commentId,+ permalink: this.permalink(videoId, commentId),+ excerpt: String(comment.text || '').slice(0, 140)+ });+ }+ saved.push(await this.db.saveLearningRecommendation({+ fingerprint: this.audienceFingerprint(videoId, topic),+ category: 'audience_demand',+ title: `Audience request: ${theme.title}`,+ rationale: `${theme.count} commenters on "${insight.title || videoId}" raised this: ${theme.summary}`,+ evidence: { videoId, themeTitle: theme.title, askCount: theme.count, sampleComments },+ proposedChange: {+ target: 'future_topics',+ topic: theme.title,+ angle: theme.summary,+ autoEditPublishedContent: false+ },+ confidence: this.confidenceForAskCount(theme.count)+ }));+ }+ return saved;}async syncDueVideos(videos = []) {