|
| 1 | +import axios from "axios"; |
| 2 | +import { |
| 3 | + ActionExample, |
| 4 | + composeContext, |
| 5 | + Content, |
| 6 | + elizaLogger, |
| 7 | + generateObjectDeprecated, |
| 8 | + HandlerCallback, |
| 9 | + IAgentRuntime, |
| 10 | + Memory, |
| 11 | + ModelClass, |
| 12 | + State, |
| 13 | + type Action, |
| 14 | +} from "@elizaos/core"; |
| 15 | + |
| 16 | +export interface LaunchAgentContent extends Content { |
| 17 | + name: string; |
| 18 | + config: string; |
| 19 | +} |
| 20 | + |
| 21 | +function isLaunchAgentContent(content: any): content is LaunchAgentContent { |
| 22 | + elizaLogger.log("Content for launchAgent", content); |
| 23 | + return typeof content.name === "string" && typeof content.config === "string"; |
| 24 | +} |
| 25 | + |
| 26 | +const launchTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. |
| 27 | +
|
| 28 | +Example response: |
| 29 | +\`\`\`json |
| 30 | +{ |
| 31 | + "name": "xiaohuo", |
| 32 | +} |
| 33 | +\`\`\` |
| 34 | +
|
| 35 | +{{recentMessages}} |
| 36 | +
|
| 37 | +Given the recent messages, extract the following information about the requested agent launch: |
| 38 | +- Agent name |
| 39 | +- Character json config |
| 40 | +`; |
| 41 | + |
| 42 | +export default { |
| 43 | + name: "LAUNCH_AGENT", |
| 44 | + similes: ["CREATE_AGENT", "DEPLOY_AGENT", "DEPLOY_ELIZA", "DEPLOY_BOT"], |
| 45 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 46 | + return true; |
| 47 | + }, |
| 48 | + description: "Launch an Eliza agent", |
| 49 | + handler: async ( |
| 50 | + runtime: IAgentRuntime, |
| 51 | + message: Memory, |
| 52 | + state: State, |
| 53 | + _options: { [key: string]: unknown }, |
| 54 | + callback?: HandlerCallback |
| 55 | + ): Promise<boolean> => { |
| 56 | + elizaLogger.log("Starting LAUNCH_AGENT handler..."); |
| 57 | + // Initialize or update state |
| 58 | + if (!state) { |
| 59 | + state = (await runtime.composeState(message)) as State; |
| 60 | + } else { |
| 61 | + state = await runtime.updateRecentMessageState(state); |
| 62 | + } |
| 63 | + |
| 64 | + // Compose launch context |
| 65 | + const launchContext = composeContext({ |
| 66 | + state, |
| 67 | + template: launchTemplate, |
| 68 | + }); |
| 69 | + |
| 70 | + // Generate launch content |
| 71 | + const content = await generateObjectDeprecated({ |
| 72 | + runtime, |
| 73 | + context: launchContext, |
| 74 | + modelClass: ModelClass.LARGE, |
| 75 | + }); |
| 76 | + |
| 77 | + // Validate launch content |
| 78 | + if (!isLaunchAgentContent(content)) { |
| 79 | + elizaLogger.error("Invalid launch content", content); |
| 80 | + if (callback) { |
| 81 | + callback({ |
| 82 | + text: "Unable to process launch agent request. Invalid content provided.", |
| 83 | + content: { error: "Invalid launch agent content" }, |
| 84 | + }); |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + const autonomeJwt = runtime.getSetting("AUTONOME_JWT_TOKEN"); |
| 90 | + const autonomeRpc = runtime.getSetting("AUTONOME_RPC"); |
| 91 | + |
| 92 | + const requestBody = { |
| 93 | + name: content.name, |
| 94 | + config: content.config, |
| 95 | + creationMethod: 2, |
| 96 | + envList: {}, |
| 97 | + templateId: "Eliza", |
| 98 | + |
| 99 | + const sendPostRequest = async () => { |
| 100 | + try { |
| 101 | + const response = await axios.post(autonomeRpc, requestBody, { |
| 102 | + headers: { |
| 103 | + Authorization: `Bearer ${autonomeJwt}`, |
| 104 | + "Content-Type": "application/json", |
| 105 | + }, |
| 106 | + }); |
| 107 | + return response; |
| 108 | + } catch (error) { |
| 109 | + console.error("Error making RPC call:", error); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + try { |
| 114 | + const resp = await sendPostRequest(); |
| 115 | + if (resp && resp.data && resp.data.app && resp.data.app.id) { |
| 116 | + elizaLogger.log( |
| 117 | + "Launching successful, please find your agent on" |
| 118 | + ); |
| 119 | + elizaLogger.log( |
| 120 | + "https://dev.autonome.fun/autonome/" + |
| 121 | + resp.data.app.id + |
| 122 | + "/details" |
| 123 | + ); |
| 124 | + } |
| 125 | + if (callback) { |
| 126 | + callback({ |
| 127 | + text: `Successfully launch agent ${content.name}`, |
| 128 | + content: { |
| 129 | + success: true, |
| 130 | + appId: |
| 131 | + "https://dev.autonome.fun/autonome/" + |
| 132 | + resp.data.app.id + |
| 133 | + "/details", |
| 134 | + }, |
| 135 | + }); |
| 136 | + } |
| 137 | + return true; |
| 138 | + } catch (error) { |
| 139 | + if (callback) { |
| 140 | + elizaLogger.error("Error during launching agent"); |
| 141 | + elizaLogger.error(error); |
| 142 | + callback({ |
| 143 | + text: `Error launching agent: ${error.message}`, |
| 144 | + content: { error: error.message }, |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + }, |
| 149 | + examples: [ |
| 150 | + [ |
| 151 | + { |
| 152 | + user: "{{user1}}", |
| 153 | + content: { |
| 154 | + text: "Launch an agent, name is xiaohuo", |
| 155 | + }, |
| 156 | + }, |
| 157 | + { |
| 158 | + user: "{{user2}}", |
| 159 | + content: { |
| 160 | + text: "I'll launch the agent now...", |
| 161 | + action: "LAUNCH_AGENT", |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + user: "{{user2}}", |
| 166 | + content: { |
| 167 | + text: "Successfully launch agent, id is ba2e8369-e256-4a0d-9f90-9c64e306dc9f", |
| 168 | + }, |
| 169 | + }, |
| 170 | + ], |
| 171 | + ] as ActionExample[][], |
| 172 | +} as Action; |
0 commit comments