|
| 1 | +import { elizaLogger } from "@elizaos/core"; |
| 2 | +import { |
| 3 | + Action, |
| 4 | + HandlerCallback, |
| 5 | + IAgentRuntime, |
| 6 | + Memory, |
| 7 | + Plugin, |
| 8 | + State, |
| 9 | +} from "@elizaos/core"; |
| 10 | +import { fal } from "@fal-ai/client"; |
| 11 | +import { FAL_CONSTANTS } from "./constants"; |
| 12 | + |
| 13 | +import * as fs from 'fs'; |
| 14 | +import { Buffer } from 'buffer'; |
| 15 | +import * as path from 'path'; |
| 16 | +import * as process from 'process'; |
| 17 | + |
| 18 | +const generate3D = async (prompt: string, runtime: IAgentRuntime) => { |
| 19 | + process.env['FAL_KEY'] = FAL_CONSTANTS.API_KEY_SETTING || runtime.getSetting("FAL_API_KEY"); |
| 20 | + |
| 21 | + try { |
| 22 | + elizaLogger.log("Starting 3D generation with prompt:", prompt); |
| 23 | + |
| 24 | + const response = await fal.subscribe(FAL_CONSTANTS.API_3D_ENDPOINT, { |
| 25 | + input: { |
| 26 | + prompt: prompt, |
| 27 | + input_image_urls: [], |
| 28 | + condition_mode: "concat", // fuse concat |
| 29 | + geometry_file_format: "glb", // glb usdz fbx obj stl |
| 30 | + material: "PBR", // PBR Shaded |
| 31 | + quality: "medium", // extra-low, low, medium, high |
| 32 | + tier: "Regular" // Regular, Sketch |
| 33 | + }, |
| 34 | + logs: true, |
| 35 | + onQueueUpdate: (update) => { |
| 36 | + if (update.status === "IN_PROGRESS") { |
| 37 | + update.logs.map((log) => log.message).forEach(elizaLogger.log); |
| 38 | + } |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + |
| 43 | + elizaLogger.log( |
| 44 | + "Generation request successful, received response:", |
| 45 | + response |
| 46 | + ); |
| 47 | + |
| 48 | + |
| 49 | + return {success: true, |
| 50 | + url: response.data.model_mesh.url, |
| 51 | + file_name: response.data.model_mesh.file_name}; |
| 52 | + |
| 53 | + } catch (error) { |
| 54 | + elizaLogger.error("3D generation error:", error); |
| 55 | + return { |
| 56 | + success: false, |
| 57 | + error: error.message || "Unknown error occurred", |
| 58 | + }; |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +const ThreeDGeneration: Action = { |
| 63 | + name: "GENERATE_3D", |
| 64 | + similes: [ |
| 65 | + "3D_GENERATION", |
| 66 | + "3D_GEN", |
| 67 | + "CREATE_3D", |
| 68 | + "MAKE_3D", |
| 69 | + "TEXT23D", |
| 70 | + "TEXT_TO_3D", |
| 71 | + "3D_CREATE", |
| 72 | + "3D_MAKE", |
| 73 | + ], |
| 74 | + description: "Generate a 3D object based on a text prompt", |
| 75 | + validate: async (runtime: IAgentRuntime, _message: Memory) => { |
| 76 | + elizaLogger.log("Validating 3D generation action"); |
| 77 | + const FalApiKey = runtime.getSetting("FAL_API_KEY"); |
| 78 | + elizaLogger.log("FAL_API_KEY present:", !!FalApiKey); |
| 79 | + return !!FalApiKey; |
| 80 | + }, |
| 81 | + handler: async ( |
| 82 | + runtime: IAgentRuntime, |
| 83 | + message: Memory, |
| 84 | + _state: State, |
| 85 | + _options: any, |
| 86 | + callback: HandlerCallback |
| 87 | + ) => { |
| 88 | + elizaLogger.log("3D generation request:", message); |
| 89 | + |
| 90 | + // Clean up the prompt by removing mentions and commands |
| 91 | + const ThreeDPrompt = message.content.text |
| 92 | + .replace(/<@\d+>/g, "") // Remove mentions |
| 93 | + .replace( |
| 94 | + /generate 3D|create 3D|make 3D|render 3D/gi, |
| 95 | + "" |
| 96 | + ) // Remove commands |
| 97 | + .trim(); |
| 98 | + |
| 99 | + if (!ThreeDPrompt || ThreeDPrompt.length < 3) { |
| 100 | + callback({ |
| 101 | + text: "Could you please provide more details about what kind of 3D object you'd like me to generate? For example: 'Generate a lovely cat'", |
| 102 | + }); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + elizaLogger.log("3D prompt:", ThreeDPrompt); |
| 107 | + |
| 108 | + callback({ |
| 109 | + text: `I'll generate a 3D object based on your prompt: "${ThreeDPrompt}". This might take a few minutes...`, |
| 110 | + }); |
| 111 | + |
| 112 | + try { |
| 113 | + const result = await generate3D(ThreeDPrompt, runtime); |
| 114 | + |
| 115 | + if (result.success && result.url && result.file_name) { |
| 116 | + // Download the 3D file |
| 117 | + const response = await fetch(result.url); |
| 118 | + const arrayBuffer = await response.arrayBuffer(); |
| 119 | + const ThreeDFileName = `content_cache/generated_3d_${result.file_name}`; |
| 120 | + |
| 121 | + // ensure the directory is existed |
| 122 | + const directoryPath = path.dirname(ThreeDFileName); |
| 123 | + if (!fs.existsSync(directoryPath)) { |
| 124 | + fs.mkdirSync(directoryPath, { recursive: true }); |
| 125 | + } |
| 126 | + |
| 127 | + // Save 3D file |
| 128 | + fs.writeFileSync(ThreeDFileName, Buffer.from(arrayBuffer)); |
| 129 | + |
| 130 | + callback( |
| 131 | + { |
| 132 | + text: "Here's your generated 3D object!", |
| 133 | + attachments: [ |
| 134 | + { |
| 135 | + id: crypto.randomUUID(), |
| 136 | + url: result.url, |
| 137 | + title: "Generated 3D", |
| 138 | + source: "ThreeDGeneration", |
| 139 | + description: ThreeDPrompt, |
| 140 | + text: ThreeDPrompt, |
| 141 | + }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + [ThreeDFileName] |
| 145 | + ); // Add the 3D file to the attachments |
| 146 | + } else { |
| 147 | + callback({ |
| 148 | + text: `3D generation failed: ${result.error}`, |
| 149 | + error: true, |
| 150 | + }); |
| 151 | + } |
| 152 | + } catch (error) { |
| 153 | + elizaLogger.error(`Failed to generate 3D. Error: ${error}`); |
| 154 | + callback({ |
| 155 | + text: `3D generation failed: ${error.message}`, |
| 156 | + error: true, |
| 157 | + }); |
| 158 | + } |
| 159 | + }, |
| 160 | + examples: [ |
| 161 | + [ |
| 162 | + { |
| 163 | + user: "{{user1}}", |
| 164 | + content: { text: "Generate a 3D object of a cat playing piano" }, |
| 165 | + }, |
| 166 | + { |
| 167 | + user: "{{agentName}}", |
| 168 | + content: { |
| 169 | + text: "I'll create a 3D object of a cat playing piano for you", |
| 170 | + action: "GENERATE_3D", |
| 171 | + }, |
| 172 | + }, |
| 173 | + ], |
| 174 | + [ |
| 175 | + { |
| 176 | + user: "{{user1}}", |
| 177 | + content: { |
| 178 | + text: "Can you make a 3D object of a anime character Goku?", |
| 179 | + }, |
| 180 | + }, |
| 181 | + { |
| 182 | + user: "{{agentName}}", |
| 183 | + content: { |
| 184 | + text: "I'll generate a 3D object of a anime character Goku for you", |
| 185 | + action: "GENERATE_3D", |
| 186 | + }, |
| 187 | + }, |
| 188 | + ], |
| 189 | + ], |
| 190 | +} as Action; |
| 191 | + |
| 192 | +export const ThreeDGenerationPlugin: Plugin = { |
| 193 | + name: "3DGeneration", |
| 194 | + description: "Generate 3D using Hyper 3D", |
| 195 | + actions: [ThreeDGeneration], |
| 196 | + evaluators: [], |
| 197 | + providers: [], |
| 198 | +}; |
0 commit comments