import React from 'react'; import { Box, Container, Typography, TextField, Paper, Button } from '@mui/material'; import { CopilotSidebar } from '@copilotkit/react-ui'; import { useCopilotReadable, useCopilotAction } from '@copilotkit/react-core'; import '@copilotkit/react-ui/styles.css'; import RegisterFacebookActions from './RegisterFacebookActions'; import RegisterFacebookEditActions from './RegisterFacebookEditActions'; import RegisterFacebookActionsEnhanced from './RegisterFacebookActionsEnhanced'; import { PlatformPersonaProvider, usePlatformPersonaContext } from '../shared/PersonaContext/PlatformPersonaProvider'; const useCopilotActionTyped = useCopilotAction as any; // --- Simple localStorage-backed chat memory --- const HISTORY_KEY = 'fbwriter:chatHistory'; const PREFS_KEY = 'fbwriter:preferences'; type ChatMsg = { role: 'user' | 'assistant'; content: string; ts: number }; function loadHistory(): ChatMsg[] { try { const raw = localStorage.getItem(HISTORY_KEY); if (!raw) return []; const arr = JSON.parse(raw); if (!Array.isArray(arr)) return []; return arr.filter((m: any) => m && typeof m.content === 'string' && (m.role === 'user' || m.role === 'assistant')); } catch { return []; } } function saveHistory(msgs: ChatMsg[]) { try { localStorage.setItem(HISTORY_KEY, JSON.stringify(msgs.slice(-50))); } catch {} } function pushHistory(role: 'user' | 'assistant', content: string) { const msgs = loadHistory(); msgs.push({ role, content: String(content || '').slice(0, 4000), ts: Date.now() }); saveHistory(msgs); } function clearHistory() { try { localStorage.removeItem(HISTORY_KEY); } catch {} } function getPreferences(): Record { try { return JSON.parse(localStorage.getItem(PREFS_KEY) || '{}') || {}; } catch { return {}; } } function summarizeHistory(maxChars = 1000): string { const msgs = loadHistory(); if (!msgs.length) return ''; const recent = msgs.slice(-10).map(m => `${m.role === 'user' ? 'User' : 'Assistant'}: ${m.content}`); const joined = recent.join('\n'); return joined.length > maxChars ? `${joined.slice(0, maxChars)}…` : joined; } function computeEditedText(op: string, src: string): string { const opL = (op || '').toLowerCase(); if (opL === 'shorten') return src.length > 240 ? src.slice(0, 220) + '…' : src; if (opL === 'lengthen') return src + '\n\nLearn more at our page!'; if (opL === 'tightenhook') { const lines = src.split('\n'); if (lines.length) lines[0] = 'šŸ”„ ' + lines[0].replace(/^\W+/, ''); return lines.join('\n'); } if (opL === 'addcta') return src + '\n\nšŸ‘‰ Tell us your thoughts in the comments!'; if (opL === 'casual') return src.replace(/\b(you will|you should)\b/gi, "you'll").replace(/\bdo not\b/gi, "don't"); if (opL === 'professional') return src.replace(/\bcan't\b/gi, 'cannot').replace(/\bwon't\b/gi, 'will not'); if (opL === 'upbeat') return src + ' šŸŽ‰'; return src; } function diffMarkup(oldText: string, newText: string): string { const MAX = 4000; const a = (oldText || '').slice(0, MAX); const b = (newText || '').slice(0, MAX); const n = a.length, m = b.length; const dp: number[][] = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0)); for (let i = n - 1; i >= 0; i--) { for (let j = m - 1; j >= 0; j--) { if (a[i] === b[j]) dp[i][j] = dp[i + 1][j + 1] + 1; else dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]); } } let i = 0, j = 0; let out = ''; while (i < n && j < m) { if (a[i] === b[j]) { out += a[i]; i++; j++; } else if (dp[i + 1][j] >= dp[i][j + 1]) { out += `${escapeHtml(a[i])}`; i++; } else { out += `${escapeHtml(b[j])}`; j++; } } while (i < n) { out += `${escapeHtml(a[i++])}`; } while (j < m) { out += `${escapeHtml(b[j++])}`; } if (oldText.length > MAX || newText.length > MAX) out += ' …'; return out; } function escapeHtml(s: string): string { return s.replace(/&/g, '&').replace(//g, '>'); } function simpleMarkdownToHtml(markdown: string): string { // Very small, safe-ish markdown renderer for bold, italics, lists, headings, paragraphs // 1) Escape HTML first let html = escapeHtml(markdown || ''); // 2) Headings (##, # at line start) html = html.replace(/^###\s+(.*)$/gm, '

$1

'); html = html.replace(/^##\s+(.*)$/gm, '

$1

'); html = html.replace(/^#\s+(.*)$/gm, '

$1

'); // 3) Bold and italics html = html.replace(/\*\*(.*?)\*\*/g, '$1'); html = html.replace(/\*(.*?)\*/g, '$1'); // 4) Lists: lines starting with * or - html = html.replace(/^(?:\*|-)\s+(.+)$/gm, '
  • $1
  • '); // Wrap consecutive
  • into