← back to Norma

agents/facebook-agent/skills/report.js

98 lines

/**
 * Facebook Report Skill
 *
 * Generates a summary report of Facebook Page activity.
 * Aggregates insights, recent posts, and engagement metrics.
 * Runs in simulation mode when FB_PAGE_ACCESS_TOKEN is not set.
 */

const AGENT = 'facebook-agent';
const PLATFORM = 'facebook';

function hasCredentials() {
  return !!(process.env.FB_PAGE_ID && process.env.FB_PAGE_ACCESS_TOKEN);
}

/**
 * @param {Object} params - Request body
 * @param {number} [params.hours_back=24] - How many hours back to report
 * @returns {Promise<Object>}
 */
module.exports = async function report(params) {
  const hoursBack = params.hours_back || 24;
  const simulated = !hasCredentials();

  console.log(`[${AGENT}] Report skill invoked — simulation=${simulated}, hours_back=${hoursBack}`);

  if (simulated) {
    // --- Simulation mode: return realistic-looking report ---
    const reportData = {
      agent: AGENT,
      platform: PLATFORM,
      credentials_configured: false,
      period_hours: hoursBack,
      summary: {
        total_posts: Math.floor(Math.random() * 10) + 1,
        total_reach: Math.floor(Math.random() * 15000) + 2000,
        total_impressions: Math.floor(Math.random() * 30000) + 5000,
        total_engagements: Math.floor(Math.random() * 1500) + 200,
        new_page_likes: Math.floor(Math.random() * 30) + 5,
        top_post: {
          id: `fb_top_${Date.now()}`,
          message: 'Our latest petition has reached 10,000 signatures! Thank you for your support.',
          likes: Math.floor(Math.random() * 150) + 50,
          shares: Math.floor(Math.random() * 40) + 10,
          comments: Math.floor(Math.random() * 25) + 5,
        },
      },
      uptime: process.uptime(),
      simulated: true,
      reported_at: new Date().toISOString(),
    };

    console.log(`[${AGENT}] SIMULATED report: posts=${reportData.summary.total_posts}, reach=${reportData.summary.total_reach}`);

    return reportData;
  }

  // --- Real mode: aggregate data from other skills / API ---
  //
  // In real mode, this would:
  // 1. Call the monitor skill to get current insights
  // 2. Call the discover skill to get recent posts
  // 3. Query sdcc_agent_actions for recent actions by this agent
  // 4. Push aggregated report to Pulse via shared/pulse-reporter
  //
  // const monitor = require('./monitor');
  // const discover = require('./discover');
  // const { reportAgentStatus } = require('../../shared/pulse-reporter');
  // const { getActionSummary } = require('../../shared/audit-logger');
  //
  // const insights = await monitor({ period: 'day' });
  // const recentPosts = await discover({ limit: 10 });
  // const actionSummary = await getActionSummary(AGENT, hoursBack);
  //
  // const reportData = {
  //   agent: AGENT,
  //   platform: PLATFORM,
  //   credentials_configured: true,
  //   period_hours: hoursBack,
  //   insights: insights.insights,
  //   recent_posts: recentPosts.posts,
  //   actions: actionSummary,
  //   uptime: process.uptime(),
  //   simulated: false,
  //   reported_at: new Date().toISOString(),
  // };
  //
  // await reportAgentStatus(AGENT, {
  //   state: 'running',
  //   uptime: process.uptime(),
  //   stats: reportData.insights,
  // });
  //
  // return reportData;

  return { error: 'Real API call not yet enabled. Uncomment the code above.' };
};