@polybiouslabs/polybious
Version:
Polybius is a next-generation intelligent agent framework built for adaptability across diverse domains. It merges contextual awareness, multi-agent collaboration, and predictive reasoning to deliver dynamic, self-optimizing performance.
247 lines (246 loc) • 11.1 kB
JavaScript
import { BaseTool } from '../base-tool';
import { logger } from '../../config/logger';
export class ContentEnhancementTool extends BaseTool {
getConfig() {
return {
name: 'content_enhancement',
description: 'Enhance and optimize content for better engagement and reach',
parameters: {
type: 'object',
properties: {
content: {
type: 'string',
description: 'Original content to enhance',
maxLength: 2000
},
platform: {
type: 'string',
description: 'Target platform for optimization',
enum: ['twitter', 'instagram', 'facebook', 'linkedin', 'tiktok'],
default: 'twitter'
},
enhancement_type: {
type: 'string',
description: 'Type of enhancement to apply',
enum: ['hashtags', 'emoji', 'formatting', 'engagement', 'all'],
default: 'all'
},
target_audience: {
type: 'string',
description: 'Target audience demographic',
enum: ['general', 'young_adults', 'professionals', 'creatives', 'tech_enthusiasts'],
default: 'general'
}
},
required: ['content']
},
handler: 'ContentEnhancementTool',
enabled: true
};
}
async execute(params) {
const { content, platform = 'twitter', enhancement_type = 'all', target_audience = 'general' } = params;
try {
logger.info('Enhancing content', { platform, enhancement_type, target_audience });
let enhancedContent = content;
const enhancements = [];
if (enhancement_type === 'all' || enhancement_type === 'hashtags') {
const result = this.addHashtags(enhancedContent, platform, target_audience);
enhancedContent = result.content;
enhancements.push(...result.added);
}
if (enhancement_type === 'all' || enhancement_type === 'emoji') {
const result = this.addEmojis(enhancedContent, platform, target_audience);
enhancedContent = result.content;
enhancements.push(...result.added);
}
if (enhancement_type === 'all' || enhancement_type === 'formatting') {
const result = this.improveFormatting(enhancedContent, platform);
enhancedContent = result.content;
enhancements.push(...result.improvements);
}
if (enhancement_type === 'all' || enhancement_type === 'engagement') {
const result = this.addEngagementElements(enhancedContent, platform);
enhancedContent = result.content;
enhancements.push(...result.elements);
}
return {
original: content,
enhanced: enhancedContent,
platform,
enhancements,
metrics: this.analyzeEnhancement(content, enhancedContent),
suggestions: this.generateSuggestions(enhancedContent, platform),
timestamp: new Date().toISOString()
};
}
catch (error) {
logger.error('Content enhancement failed', { error });
throw new Error(`Content enhancement failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
addHashtags(content, platform, audience) {
const hashtagSuggestions = {
general: ['#trending', '#viral', '#content', '#social'],
young_adults: ['#GenZ', '#millennial', '#trending', '#lifestyle'],
professionals: ['#business', '#professional', '#career', '#networking'],
creatives: ['#creative', '#art', '#design', '#inspiration'],
tech_enthusiasts: ['#tech', '#innovation', '#AI', '#digital']
};
const platformHashtags = {
twitter: ['#TwitterTips', '#SocialMedia'],
instagram: ['#InstaGood', '#PhotoOfTheDay'],
linkedin: ['#LinkedIn', '#Professional'],
facebook: ['#Facebook', '#Community'],
tiktok: ['#TikTok', '#ForYou']
};
const relevantHashtags = [
...(hashtagSuggestions[audience] || hashtagSuggestions.general),
...(platformHashtags[platform] || [])
].slice(0, 3);
const existingHashtags = content.match(/#\w+/g) || [];
const newHashtags = relevantHashtags.filter(tag => !existingHashtags.includes(tag));
return {
content: content + ' ' + newHashtags.join(' '),
added: newHashtags
};
}
addEmojis(content, platform, audience) {
const emojiMap = {
excitement: ['🚀', '✨', '🔥', '💥'],
positive: ['😊', '👍', '🌟', '💪'],
creative: ['🎨', '💡', '🌈', '✨'],
tech: ['💻', '🤖', '⚡', '🔬'],
celebration: ['🎉', '🥳', '🏆', '🎊']
};
let enhancedContent = content;
const addedEmojis = [];
// Simple keyword-based emoji addition
if (content.toLowerCase().includes('great') || content.toLowerCase().includes('amazing')) {
const emoji = emojiMap.positive[Math.floor(Math.random() * emojiMap.positive.length)];
enhancedContent = enhancedContent.replace(/(great|amazing)/i, `$1 ${emoji}`);
addedEmojis.push(emoji);
}
if (content.toLowerCase().includes('new') || content.toLowerCase().includes('launch')) {
const emoji = emojiMap.excitement[Math.floor(Math.random() * emojiMap.excitement.length)];
enhancedContent += ` ${emoji}`;
addedEmojis.push(emoji);
}
return {
content: enhancedContent,
added: addedEmojis
};
}
improveFormatting(content, platform) {
let improvedContent = content;
const improvements = [];
// Add line breaks for readability
if (content.length > 100 && !content.includes('\n')) {
improvedContent = content.replace(/\. /g, '.\n\n');
improvements.push('Added line breaks for readability');
}
// Platform-specific formatting
if (platform === 'twitter' && content.length > 200) {
// Suggest thread format
improvements.push('Consider breaking into Twitter thread');
}
if (platform === 'linkedin' && !content.includes('\n\n')) {
improvedContent = content.replace(/\. /g, '.\n\n');
improvements.push('Added paragraph breaks for LinkedIn format');
}
return {
content: improvedContent,
improvements
};
}
addEngagementElements(content, platform) {
let enhancedContent = content;
const elements = [];
const engagementPhrases = [
"What do you think?",
"Share your thoughts!",
"Let me know in the comments!",
"Tag someone who needs to see this!",
"Double tap if you agree!"
];
// Add engagement phrase based on platform
if (platform === 'instagram' && !content.includes('?')) {
enhancedContent += '\n\n' + engagementPhrases[4]; // Double tap
elements.push('Added engagement call-to-action');
}
else if (!content.includes('?')) {
enhancedContent += '\n\n' + engagementPhrases[0]; // What do you think?
elements.push('Added question to encourage engagement');
}
return {
content: enhancedContent,
elements
};
}
analyzeEnhancement(original, enhanced) {
return {
originalLength: original.length,
enhancedLength: enhanced.length,
lengthIncrease: enhanced.length - original.length,
hashtagCount: (enhanced.match(/#\w+/g) || []).length,
emojiCount: (enhanced.match(/[\u{1F600}-\u{1F64F}]|[\u{1F300}-\u{1F5FF}]|[\u{1F680}-\u{1F6FF}]|[\u{1F1E0}-\u{1F1FF}]/gu) || []).length,
engagementScore: this.calculateEngagementScore(enhanced),
readabilityScore: this.calculateReadabilityScore(enhanced)
};
}
calculateEngagementScore(content) {
let score = 0;
// Questions increase engagement
if (content.includes('?'))
score += 20;
// Calls to action
const cta = ['share', 'comment', 'like', 'tag', 'follow', 'click'];
cta.forEach(word => {
if (content.toLowerCase().includes(word))
score += 10;
});
// Emojis increase engagement
const emojiCount = (content.match(/[\u{1F600}-\u{1F64F}]|[\u{1F300}-\u{1F5FF}]|[\u{1F680}-\u{1F6FF}]|[\u{1F1E0}-\u{1F1FF}]/gu) || []).length;
score += Math.min(emojiCount * 5, 25);
// Hashtags for discoverability
const hashtagCount = (content.match(/#\w+/g) || []).length;
score += Math.min(hashtagCount * 3, 15);
return Math.min(score, 100);
}
calculateReadabilityScore(content) {
const sentences = content.split(/[.!?]+/).filter(s => s.trim().length > 0);
const words = content.split(/\s+/).filter(w => w.length > 0);
const avgWordsPerSentence = words.length / sentences.length;
// Ideal is 15-20 words per sentence
let score = 100;
if (avgWordsPerSentence > 25)
score -= 20;
if (avgWordsPerSentence < 5)
score -= 10;
// Penalty for very long content
if (content.length > 1000)
score -= 15;
return Math.max(score, 0);
}
generateSuggestions(content, platform) {
const suggestions = [];
if (content.length > 280 && platform === 'twitter') {
suggestions.push('Content exceeds Twitter character limit - consider shortening');
}
const hashtagCount = (content.match(/#\w+/g) || []).length;
if (hashtagCount === 0) {
suggestions.push('Add relevant hashtags to improve discoverability');
}
else if (hashtagCount > 5) {
suggestions.push('Consider reducing hashtags - too many can look spammy');
}
if (!content.includes('?') && !content.includes('!')) {
suggestions.push('Add questions or exclamations to increase engagement');
}
const emojiCount = (content.match(/[\u{1F600}-\u{1F64F}]|[\u{1F300}-\u{1F5FF}]|[\u{1F680}-\u{1F6FF}]|[\u{1F1E0}-\u{1F1FF}]/gu) || []).length;
if (emojiCount === 0) {
suggestions.push('Consider adding emojis to make content more engaging');
}
return suggestions;
}
}