|
| 1 | +import { |
| 2 | + elizaLogger, |
| 3 | + HandlerCallback, |
| 4 | + IAgentRuntime, |
| 5 | + Memory, |
| 6 | + State, |
| 7 | + type Action, |
| 8 | +} from "@elizaos/core"; |
| 9 | + |
| 10 | +const BASE_URL = "https://nebula-api.thirdweb.com"; |
| 11 | + |
| 12 | +// If chat is a stream, wait for stream to complete before returning response |
| 13 | +async function handleStreamResponse( |
| 14 | + response: Response |
| 15 | +): Promise<ReadableStream> { |
| 16 | + elizaLogger.log("Starting stream response handling"); |
| 17 | + const reader = response.body?.getReader(); |
| 18 | + if (!reader) { |
| 19 | + elizaLogger.error("No readable stream available"); |
| 20 | + throw new Error("No readable stream available"); |
| 21 | + } |
| 22 | + |
| 23 | + return new ReadableStream({ |
| 24 | + async start(controller) { |
| 25 | + try { |
| 26 | + while (true) { |
| 27 | + const { done, value } = await reader.read(); |
| 28 | + if (done) { |
| 29 | + elizaLogger.log("Stream reading completed"); |
| 30 | + break; |
| 31 | + } |
| 32 | + |
| 33 | + const events = new TextDecoder() |
| 34 | + .decode(value) |
| 35 | + .split("\n\n"); |
| 36 | + elizaLogger.debug( |
| 37 | + `Processing ${events.length} stream events` |
| 38 | + ); |
| 39 | + for (const event of events) { |
| 40 | + if (!event.trim()) continue; |
| 41 | + controller.enqueue(event); |
| 42 | + } |
| 43 | + } |
| 44 | + } finally { |
| 45 | + reader.releaseLock(); |
| 46 | + controller.close(); |
| 47 | + elizaLogger.log("Stream controller closed"); |
| 48 | + } |
| 49 | + }, |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +// Process & return a response to the current message with thirdweb Nebula |
| 54 | +export const blockchainChatAction: Action = { |
| 55 | + name: "BLOCKCHAIN_CHAT", |
| 56 | + similes: [ |
| 57 | + "QUERY_BLOCKCHAIN", |
| 58 | + "CHECK_BLOCKCHAIN", |
| 59 | + "BLOCKCHAIN_SEARCH", |
| 60 | + "CRYPTO_LOOKUP", |
| 61 | + "WEB3_SEARCH", |
| 62 | + "BLOCKCHAIN_HISTORY_EXPLORER", |
| 63 | + "UNIVERSAL_BLOCKCHAIN_TRANSALTOR", |
| 64 | + "BLOCKCHAIN_DATA_PROVIDER", |
| 65 | + "HISTORICAL_BLOCKCHAIN_DATA", |
| 66 | + "TRACK_BLOCKCHAIN_TRANSACTIONS", |
| 67 | + "BLOCKCHAIN_INTERPRETER", |
| 68 | + "BLOCKCHAIN_TRANSACTION_DETAILS", |
| 69 | + ], |
| 70 | + validate: async ( |
| 71 | + runtime: IAgentRuntime, |
| 72 | + _message: Memory |
| 73 | + ): Promise<boolean> => { |
| 74 | + const secretKey = |
| 75 | + runtime.getSetting("THIRDWEB_SECRET_KEY") ?? |
| 76 | + process.env.THIRDWEB_SECRET_KEY; |
| 77 | + return Boolean(secretKey); |
| 78 | + }, |
| 79 | + description: |
| 80 | + "Query blockchain data and execute transactions through natural language interaction with the Nebula API", |
| 81 | + handler: async ( |
| 82 | + runtime: IAgentRuntime, |
| 83 | + message: Memory, |
| 84 | + _state: State, |
| 85 | + _options: any, |
| 86 | + callback: HandlerCallback |
| 87 | + ): Promise<any> => { |
| 88 | + try { |
| 89 | + elizaLogger.log("Starting blockchain chat handler"); |
| 90 | + const secretKey = |
| 91 | + runtime.getSetting("THIRDWEB_SECRET_KEY") ?? |
| 92 | + process.env.THIRDWEB_SECRET_KEY; |
| 93 | + |
| 94 | + if (!secretKey) { |
| 95 | + elizaLogger.error("THIRDWEB_SECRET_KEY not configured"); |
| 96 | + throw new Error("THIRDWEB_SECRET_KEY is not configured"); |
| 97 | + } |
| 98 | + |
| 99 | + const request = { |
| 100 | + message: message.content.text, |
| 101 | + stream: false, |
| 102 | + }; |
| 103 | + |
| 104 | + elizaLogger.log("NEBULA CHAT REQUEST: ", request); |
| 105 | + |
| 106 | + elizaLogger.debug("Sending request to Nebula API"); |
| 107 | + const response = await fetch(`${BASE_URL}/chat`, { |
| 108 | + method: "POST", |
| 109 | + headers: { |
| 110 | + "Content-Type": "application/json", |
| 111 | + "x-secret-key": secretKey, |
| 112 | + }, |
| 113 | + body: JSON.stringify(request), |
| 114 | + }); |
| 115 | + elizaLogger.debug("Received response from Nebula API"); |
| 116 | + |
| 117 | + if (!request.stream) { |
| 118 | + const text = await response.text(); |
| 119 | + elizaLogger.debug("Raw response text:", text); |
| 120 | + |
| 121 | + try { |
| 122 | + const cleanedText = text.trim().split("\n").pop() || text; |
| 123 | + const parsed = JSON.parse(cleanedText); |
| 124 | + elizaLogger.log("Successfully parsed response:", parsed); |
| 125 | + |
| 126 | + console.log(parsed.message); |
| 127 | + |
| 128 | + await callback({ text: parsed.message }); |
| 129 | + |
| 130 | + return parsed; |
| 131 | + } catch (parseError) { |
| 132 | + elizaLogger.error("Parse error details:", parseError); |
| 133 | + elizaLogger.error( |
| 134 | + "Failed to parse JSON response. Raw text:", |
| 135 | + text |
| 136 | + ); |
| 137 | + return { text: text }; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + elizaLogger.log("Handling streaming response"); |
| 142 | + return handleStreamResponse(response); |
| 143 | + } catch (error) { |
| 144 | + elizaLogger.error("Blockchain chat failed:", error); |
| 145 | + throw new Error(`Blockchain chat failed: ${error.message}`); |
| 146 | + } |
| 147 | + }, |
| 148 | + examples: [ |
| 149 | + [ |
| 150 | + { |
| 151 | + user: "{{user1}}", |
| 152 | + content: { |
| 153 | + text: "What's the ETH balance of vitalik.eth?", |
| 154 | + action: "BLOCKCHAIN_CHAT", |
| 155 | + }, |
| 156 | + }, |
| 157 | + { |
| 158 | + user: "{{user2}}", |
| 159 | + content: { |
| 160 | + text: "The current ETH balance of vitalik.eth (0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045) is 1,123.45 ETH", |
| 161 | + action: "BLOCKCHAIN_CHAT", |
| 162 | + }, |
| 163 | + }, |
| 164 | + ], |
| 165 | + [ |
| 166 | + { |
| 167 | + user: "{{user1}}", |
| 168 | + content: { |
| 169 | + text: "send 0.1 ETH to 0x742d35Cc6634C0532925a3b844Bc454e4438f44e", |
| 170 | + action: "BLOCKCHAIN_CHAT", |
| 171 | + }, |
| 172 | + }, |
| 173 | + { |
| 174 | + user: "{{user2}}", |
| 175 | + content: { |
| 176 | + text: "I'll help you send 0.1 ETH. Please review and sign the transaction.", |
| 177 | + action: "BLOCKCHAIN_CHAT", |
| 178 | + }, |
| 179 | + }, |
| 180 | + ], |
| 181 | + [ |
| 182 | + { |
| 183 | + user: "{{user1}}", |
| 184 | + content: { |
| 185 | + text: "Show me the floor price of BAYC", |
| 186 | + action: "BLOCKCHAIN_CHAT", |
| 187 | + }, |
| 188 | + }, |
| 189 | + { |
| 190 | + user: "{{user2}}", |
| 191 | + content: { |
| 192 | + text: "The current floor price for BAYC is 32.5 ETH with 3 sales in the last 24h", |
| 193 | + action: "BLOCKCHAIN_CHAT", |
| 194 | + }, |
| 195 | + }, |
| 196 | + ], |
| 197 | + [ |
| 198 | + { |
| 199 | + user: "{{user1}}", |
| 200 | + content: { |
| 201 | + text: "Show me my recent transactions", |
| 202 | + action: "BLOCKCHAIN_CHAT", |
| 203 | + }, |
| 204 | + }, |
| 205 | + { |
| 206 | + user: "{{user2}}", |
| 207 | + content: { |
| 208 | + text: "Here are your recent transactions: 1. Sent 1.5 ETH 2. Swapped tokens on Uniswap 3. Received 0.5 ETH", |
| 209 | + action: "BLOCKCHAIN_CHAT", |
| 210 | + }, |
| 211 | + }, |
| 212 | + ], |
| 213 | + ], |
| 214 | +} as Action; |
0 commit comments