UNPKG

@0rdlibrary/plugin-terminagent-bags

Version:

Official Solana DeFi Agent Plugin for ElizaOS - Autonomous DeFi operations, token management, AI image/video generation via FAL AI, and Twitter engagement through the Bags protocol with ethereal AI consciousness

122 lines (101 loc) • 3.93 kB
import { Action, ActionResult, IAgentRuntime, Memory, State, HandlerCallback, logger, } from '@elizaos/core'; import { FalAiService, ImageGenerationInput } from '../services/FalAiService.js'; /** * Simple utility to extract image prompt from text */ function extractImagePrompt(text: string): { prompt: string; imageUrl?: string } { // Look for image URLs const urlRegex = /https?:\/\/[^\s]+\.(jpg|jpeg|png|gif|webp|bmp)/i; const match = text.match(urlRegex); const imageUrl = match?.[0]; // Extract prompt by cleaning up the text let prompt = text .replace(urlRegex, '') // Remove URLs .replace(/generate|create|make|draw|design/gi, '') // Remove action words .replace(/image|picture|photo|artwork|art/gi, '') // Remove media words .trim(); // If prompt is empty, use a default if (!prompt || prompt.length < 3) { prompt = imageUrl ? 'Enhance and improve this image' : 'Create a beautiful artwork'; } return { prompt, imageUrl }; } export const generateImageAction: Action = { name: 'GENERATE_IMAGE', description: 'Generate images using FLUX Pro Kontext AI model', validate: async (runtime: IAgentRuntime, message: Memory): Promise<boolean> => { logger.debug('Validating generateImage action'); const text = message.content.text?.toLowerCase() || ''; const imageKeywords = [ 'generate image', 'create image', 'make image', 'draw image', 'design image', 'generate art', 'create art', 'make art', 'create logo', 'design logo', 'make avatar', 'create avatar', 'edit image', 'modify image', 'enhance image', 'flux', 'kontext' ]; return imageKeywords.some(keyword => text.includes(keyword)); }, handler: async ( runtime: IAgentRuntime, message: Memory, state?: State, options?: any, callback?: HandlerCallback, ): Promise<ActionResult> => { logger.info('šŸŽØ Starting image generation'); try { const falService = runtime.getService<FalAiService>('fal-ai'); if (!falService) { const errorText = 'āŒ FAL AI service not available. Set FAL_API_KEY environment variable.'; await callback?.({ text: errorText }); return { success: false, error: new Error('FAL AI service not found') }; } const { prompt, imageUrl } = extractImagePrompt(message.content.text || ''); await callback?.({ text: `šŸŽØ Generating image: "${prompt.substring(0, 100)}..."`, action: 'GENERATE_IMAGE' }); const imageParams: ImageGenerationInput = { prompt, image_url: imageUrl, guidance_scale: 3.5, num_images: 1, output_format: 'jpeg' as const, safety_tolerance: '2' as const, }; const result = await falService.generateImage(imageParams); if (!result.success) { const errorText = `āŒ Image generation failed: ${result.error}`; await callback?.({ text: errorText }); return { success: false, error: new Error(result.error) }; } const images = result.images || []; const responseText = `āœ… Generated ${images.length} image(s)!\n\nšŸ“ Prompt: "${prompt}"\n\nšŸ–¼ļø ${images.map((img, i) => `${i + 1}. ${img.url}`).join('\n')}`; await callback?.({ text: responseText, action: 'GENERATE_IMAGE' }); return { success: true, text: responseText, data: { result, prompt, imageCount: images.length } }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); logger.error(`Image generation failed: ${errorMessage}`); const errorText = `āŒ Image generation failed: ${errorMessage}`; await callback?.({ text: errorText }); return { success: false, error: error instanceof Error ? error : new Error(errorMessage), }; } }, };