Skip to content

🎬 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.

feat: add engagement_insights store with sync watermarks

darkzOGxfd38c3c44d2b
diff --git a/database/db.js b/database/db.js
index d66d74c..5b9cde9 100644
--- a/database/db.js
+++ b/database/db.js
@@ -214,6 +214,23 @@ class Database {
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
)`,
+ `CREATE TABLE IF NOT EXISTS engagement_insights (
+ id TEXT PRIMARY KEY,
+ video_id TEXT NOT NULL UNIQUE,
+ production_id TEXT,
+ title TEXT,
+ comment_count INTEGER DEFAULT 0,
+ analyzed_count INTEGER DEFAULT 0,
+ sentiment TEXT NOT NULL DEFAULT '{}',
+ themes TEXT NOT NULL DEFAULT '[]',
+ attention_flags TEXT NOT NULL DEFAULT '[]',
+ analysis_method TEXT DEFAULT 'ai',
+ analyzed_at TEXT,
+ last_synced_at TEXT,
+ newest_comment_at TEXT,
+ created_at TEXT DEFAULT CURRENT_TIMESTAMP,
+ updated_at TEXT DEFAULT CURRENT_TIMESTAMP
+ )`,
// Keywords Performance
`CREATE TABLE IF NOT EXISTS keyword_performance (
@@ -1983,6 +2000,80 @@ class Database {
};
}
+ async saveEngagementInsight(insight) {
+ const existing = await this.getEngagementInsight(insight.videoId);
+ const merged = { ...(existing || {}), ...insight };
+ const id = existing?.id || this.generateId('insight');
+ await this.executeQuery(
+ `INSERT INTO engagement_insights (
+ id, video_id, production_id, title, comment_count, analyzed_count,
+ sentiment, themes, attention_flags, analysis_method, analyzed_at,
+ last_synced_at, newest_comment_at
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT(video_id) DO UPDATE SET
+ production_id = excluded.production_id,
+ title = excluded.title,
+ comment_count = excluded.comment_count,
+ analyzed_count = excluded.analyzed_count,
+ sentiment = excluded.sentiment,
+ themes = excluded.themes,
+ attention_flags = excluded.attention_flags,
+ analysis_method = excluded.analysis_method,
+ analyzed_at = excluded.analyzed_at,
+ last_synced_at = excluded.last_synced_at,
+ newest_comment_at = excluded.newest_comment_at,
+ updated_at = CURRENT_TIMESTAMP`,
+ [
+ id,
+ insight.videoId,
+ merged.productionId || null,
+ merged.title || null,
+ Number(merged.commentCount || 0),
+ Number(merged.analyzedCount || 0),
+ JSON.stringify(merged.sentiment || {}),
+ JSON.stringify(merged.themes || []),
+ JSON.stringify(merged.attentionFlags || []),
+ merged.analysisMethod || 'ai',
+ merged.analyzedAt || null,
+ merged.lastSyncedAt || null,
+ merged.newestCommentAt || null
+ ]
+ );
+ return this.getEngagementInsight(insight.videoId);
+ }
+
+ async getEngagementInsight(videoId) {
+ const row = await this.getRow('SELECT * FROM engagement_insights WHERE video_id = ?', [videoId]);
+ return this.parseEngagementInsight(row);
+ }
+
+ async listEngagementInsights(options = {}) {
+ const limit = Math.max(1, Math.min(50, Number(options.limit || 12)));
+ const rows = await this.getAllRows(
+ 'SELECT * FROM engagement_insights ORDER BY updated_at DESC LIMIT ?',
+ [limit]
+ );
+ return rows.map(row => this.parseEngagementInsight(row));
+ }
+
+ parseEngagementInsight(row) {
+ if (!row) return null;
+ return {
+ ...row,
+ videoId: row.video_id,
+ productionId: row.production_id,
+ commentCount: Number(row.comment_count || 0),
+ analyzedCount: Number(row.analyzed_count || 0),
+ sentiment: JSON.parse(row.sentiment || '{}'),
+ themes: JSON.parse(row.themes || '[]'),
+ attentionFlags: JSON.parse(row.attention_flags || '[]'),
+ analysisMethod: row.analysis_method,
+ analyzedAt: row.analyzed_at,
+ lastSyncedAt: row.last_synced_at,
+ newestCommentAt: row.newest_comment_at
+ };
+ }
+
async getPublishedContentContext(youtubeId) {
const schedule = await this.getRow(
`SELECT production_id, published_at, title, metadata
diff --git a/test.js b/test.js
index ec242d8..9dd504f 100644
--- a/test.js
+++ b/test.js
@@ -47,7 +47,8 @@ class SystemTest {
{ name: 'Directory Structure', test: () => this.testDirectories() },
{ name: 'Agent Loading', test: () => this.testAgentLoading() },
{ name: 'Configuration Files', test: () => this.testConfiguration() },
- { name: 'Audience Comment Store', test: () => this.testAudienceCommentStore() }
+ { name: 'Audience Comment Store', test: () => this.testAudienceCommentStore() },
+ { name: 'Engagement Insight Store', test: () => this.testEngagementInsightStore() }
];
let passed = 0;
@@ -2292,6 +2293,38 @@ class SystemTest {
this.logger.info('Agent loading test completed successfully');
}
+ async testEngagementInsightStore() {
+ const db = new Database();
+ await db.initialize();
+ const videoId = `vid_insight_${Date.now()}`;
+ try {
+ const synced = await db.saveEngagementInsight({
+ videoId, title: 'Test video', commentCount: 4,
+ lastSyncedAt: '2026-08-23T10:00:00.000Z',
+ newestCommentAt: '2026-08-23T09:00:00.000Z'
+ });
+ if (!synced || synced.videoId !== videoId) throw new Error('saveEngagementInsight did not store the row');
+
+ const analyzed = await db.saveEngagementInsight({
+ videoId, analyzedCount: 4,
+ sentiment: { method: 'ai', positive: 3, neutral: 1, negative: 0 },
+ themes: [{ title: 'Render cache questions', summary: 'Viewers ask how caching works', kind: 'question', count: 3, commentIds: ['a', 'b', 'c'] }],
+ attentionFlags: [{ commentId: 'x', categories: ['scam'], permalink: 'https://www.youtube.com/watch?v=1&lc=x' }],
+ analysisMethod: 'ai', analyzedAt: '2026-08-23T10:05:00.000Z'
+ });
+ if (analyzed.id !== synced.id) throw new Error('Insight upsert must reuse the video row, not duplicate');
+ if (analyzed.lastSyncedAt !== '2026-08-23T10:00:00.000Z') throw new Error('Merge lost the sync watermark');
+ if (analyzed.themes[0]?.count !== 3 || analyzed.sentiment.positive !== 3) throw new Error('JSON columns did not round-trip');
+ if (analyzed.attentionFlags.length !== 1) throw new Error('attention_flags did not round-trip');
+
+ const listed = await db.listEngagementInsights({ limit: 5 });
+ if (!listed.some(item => item.videoId === videoId)) throw new Error('listEngagementInsights missed the row');
+ } finally {
+ await db.executeQuery('DELETE FROM engagement_insights WHERE video_id = ?', [videoId]);
+ await db.close();
+ }
+ }
+
async testAudienceCommentStore() {
const db = new Database();
await db.initialize();