|
| 1 | +import { |
| 2 | + Action, |
| 3 | + IAgentRuntime, |
| 4 | + Memory, |
| 5 | + State, |
| 6 | + HandlerCallback, |
| 7 | + elizaLogger, |
| 8 | + generateText, |
| 9 | + ModelClass, |
| 10 | +} from "@elizaos/core"; |
| 11 | +import { generateSQL, executeQuery } from "../libs/chainbase"; |
| 12 | +import { responsePrompt } from "../templates"; |
| 13 | + |
| 14 | +const QUERY_PREFIX = "query onchain data:"; |
| 15 | + |
| 16 | +export const queryBlockChainData: Action = { |
| 17 | + name: "QUERY_BLOCKCHAIN_DATA", |
| 18 | + similes: ["ANALYZE_BLOCKCHAIN", "GET_CHAIN_DATA", "QUERY_ONCHAIN_DATA"], |
| 19 | + description: |
| 20 | + "Query blockchain data using natural language starting with 'query onchain data:'", |
| 21 | + |
| 22 | + validate: async (runtime: IAgentRuntime, message: Memory) => { |
| 23 | + elizaLogger.log("Validating runtime for QUERY_BLOCKCHAIN_DATA..."); |
| 24 | + return !!( |
| 25 | + runtime.character.settings.secrets?.CHAINBASE_API_KEY || |
| 26 | + process.env.CHAINBASE_API_KEY |
| 27 | + ); |
| 28 | + }, |
| 29 | + |
| 30 | + handler: async ( |
| 31 | + runtime: IAgentRuntime, |
| 32 | + message: Memory, |
| 33 | + state?: State, |
| 34 | + options?: { [key: string]: unknown }, |
| 35 | + callback?: HandlerCallback |
| 36 | + ) => { |
| 37 | + try { |
| 38 | + const messageText = message.content.text.toLowerCase(); |
| 39 | + |
| 40 | + if (!messageText.includes(QUERY_PREFIX)) { |
| 41 | + callback({ |
| 42 | + text: `Please use the format: ${QUERY_PREFIX} <your natural language query>`, |
| 43 | + }); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const queryText = message.content.text |
| 48 | + .slice( |
| 49 | + message.content.text.toLowerCase().indexOf(QUERY_PREFIX) + |
| 50 | + QUERY_PREFIX.length |
| 51 | + ) |
| 52 | + .trim(); |
| 53 | + |
| 54 | + if (!queryText) { |
| 55 | + callback({ |
| 56 | + text: `Please provide a specific query after '${QUERY_PREFIX}'`, |
| 57 | + }); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Generate SQL from natural language |
| 62 | + const sql = await generateSQL(queryText); |
| 63 | + |
| 64 | + // Execute query on Chainbase |
| 65 | + const result = await executeQuery(sql); |
| 66 | + |
| 67 | + // Use generateText to format the response |
| 68 | + const formattedResponse = await generateText({ |
| 69 | + runtime, |
| 70 | + context: responsePrompt( |
| 71 | + { |
| 72 | + sql, |
| 73 | + columns: result.columns, |
| 74 | + data: result.data, |
| 75 | + totalRows: result.totalRows, |
| 76 | + }, |
| 77 | + queryText |
| 78 | + ), |
| 79 | + modelClass: ModelClass.SMALL, |
| 80 | + }); |
| 81 | + |
| 82 | + callback({ |
| 83 | + text: formattedResponse, |
| 84 | + }); |
| 85 | + } catch (error) { |
| 86 | + elizaLogger.error("Error in queryChainbase action:", error); |
| 87 | + return "error"; |
| 88 | + } |
| 89 | + }, |
| 90 | + |
| 91 | + examples: [ |
| 92 | + [ |
| 93 | + { |
| 94 | + user: "user", |
| 95 | + content: { |
| 96 | + text: "query onchain data: Calculate the average gas used per block on Ethereum in the last 100 blocks", |
| 97 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + user: "assistant", |
| 102 | + content: { |
| 103 | + text: "📊 Query Results...", |
| 104 | + }, |
| 105 | + }, |
| 106 | + ], |
| 107 | + [ |
| 108 | + { |
| 109 | + user: "user", |
| 110 | + content: { |
| 111 | + text: "query onchain data: Show me the top 10 active Ethereum addresses by transaction count in the last 1000 blocks", |
| 112 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + user: "assistant", |
| 117 | + content: { |
| 118 | + text: "📊 Query Results...", |
| 119 | + }, |
| 120 | + }, |
| 121 | + ], |
| 122 | + [ |
| 123 | + { |
| 124 | + user: "user", |
| 125 | + content: { |
| 126 | + text: "query onchain data: List Ethereum transactions with value greater than 1 ETH in the last 1000 blocks", |
| 127 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + user: "assistant", |
| 132 | + content: { |
| 133 | + text: "📊 Query Results...", |
| 134 | + }, |
| 135 | + }, |
| 136 | + ], |
| 137 | + [ |
| 138 | + { |
| 139 | + user: "user", |
| 140 | + content: { |
| 141 | + text: "query onchain data: Calculate the total ETH transaction fees collected in the last 100 Ethereum blocks", |
| 142 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 143 | + }, |
| 144 | + }, |
| 145 | + { |
| 146 | + user: "assistant", |
| 147 | + content: { |
| 148 | + text: "📊 Query Results...", |
| 149 | + }, |
| 150 | + }, |
| 151 | + ], |
| 152 | + [ |
| 153 | + { |
| 154 | + user: "user", |
| 155 | + content: { |
| 156 | + text: "query onchain data: Show me the distribution of ETH transaction values in the last 1000 Ethereum transactions", |
| 157 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 158 | + }, |
| 159 | + }, |
| 160 | + { |
| 161 | + user: "assistant", |
| 162 | + content: { |
| 163 | + text: "📊 Query Results...", |
| 164 | + }, |
| 165 | + }, |
| 166 | + ], |
| 167 | + [ |
| 168 | + { |
| 169 | + user: "user", |
| 170 | + content: { |
| 171 | + text: "query onchain data: Find Ethereum blocks that have more than 200 transactions in the last 24 hours", |
| 172 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + user: "assistant", |
| 177 | + content: { |
| 178 | + text: "📊 Query Results...", |
| 179 | + }, |
| 180 | + }, |
| 181 | + ], |
| 182 | + [ |
| 183 | + { |
| 184 | + user: "user", |
| 185 | + content: { |
| 186 | + text: "query onchain data: What's the average gas price trend on Ethereum mainnet in the last 1000 blocks", |
| 187 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 188 | + }, |
| 189 | + }, |
| 190 | + { |
| 191 | + user: "assistant", |
| 192 | + content: { |
| 193 | + text: "📊 Query Results...", |
| 194 | + }, |
| 195 | + }, |
| 196 | + ], |
| 197 | + [ |
| 198 | + { |
| 199 | + user: "user", |
| 200 | + content: { |
| 201 | + text: "query onchain data: Show me Ethereum addresses that have both sent and received ETH in the last 100 blocks", |
| 202 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 203 | + }, |
| 204 | + }, |
| 205 | + { |
| 206 | + user: "assistant", |
| 207 | + content: { |
| 208 | + text: "📊 Query Results...", |
| 209 | + }, |
| 210 | + }, |
| 211 | + ], |
| 212 | + [ |
| 213 | + { |
| 214 | + user: "user", |
| 215 | + content: { |
| 216 | + text: "query onchain data: List the top 10 Ethereum blocks by total gas used in the last 24 hours", |
| 217 | + action: "QUERY_BLOCKCHAIN_DATA", |
| 218 | + }, |
| 219 | + }, |
| 220 | + { |
| 221 | + user: "assistant", |
| 222 | + content: { |
| 223 | + text: "📊 Query Results...", |
| 224 | + }, |
| 225 | + }, |
| 226 | + ], |
| 227 | + ], |
| 228 | +}; |
0 commit comments