|
| 1 | +import { |
| 2 | + ActionExample, |
| 3 | + composeContext, |
| 4 | + elizaLogger, |
| 5 | + generateObjectDeprecated, |
| 6 | + HandlerCallback, |
| 7 | + IAgentRuntime, |
| 8 | + Memory, |
| 9 | + ModelClass, |
| 10 | + State, |
| 11 | + type Action, |
| 12 | +} from "@elizaos/core"; |
| 13 | +import { BinanceService } from "../services"; |
| 14 | + |
| 15 | +const priceCheckTemplate = `Look at ONLY your LAST RESPONSE message in this conversation, where you just said which cryptocurrency price you would check. |
| 16 | +Based on ONLY that last message, provide the trading symbol. |
| 17 | +
|
| 18 | +For example: |
| 19 | +- If your last message was "I'll check the current Ethereum price..." -> return "ETH" |
| 20 | +- If your last message was "I'll check the current Solana price..." -> return "SOL" |
| 21 | +- If your last message was "I'll check the current Bitcoin price..." -> return "BTC" |
| 22 | +
|
| 23 | +\`\`\`json |
| 24 | +{ |
| 25 | + "symbol": "<symbol from your LAST response only>", |
| 26 | + "quoteCurrency": "<quote currency from your LAST response, or USDT if none mentioned>" |
| 27 | +} |
| 28 | +\`\`\` |
| 29 | +
|
| 30 | +Last part of conversation: |
| 31 | +{{recentMessages}}`; |
| 32 | + |
| 33 | +export const priceCheck: Action = { |
| 34 | + name: "GET_PRICE", |
| 35 | + similes: [ |
| 36 | + "CHECK_PRICE", |
| 37 | + "PRICE_CHECK", |
| 38 | + "GET_CRYPTO_PRICE", |
| 39 | + "CRYPTO_PRICE", |
| 40 | + "CHECK_CRYPTO_PRICE", |
| 41 | + "PRICE_LOOKUP", |
| 42 | + "CURRENT_PRICE", |
| 43 | + ], |
| 44 | + description: "Get current price information for a cryptocurrency pair", |
| 45 | + validate: async () => true, // Public endpoint |
| 46 | + handler: async ( |
| 47 | + runtime: IAgentRuntime, |
| 48 | + message: Memory, |
| 49 | + state: State, |
| 50 | + _options: Record<string, unknown>, |
| 51 | + callback?: HandlerCallback |
| 52 | + ): Promise<boolean> => { |
| 53 | + try { |
| 54 | + // Initialize or update state |
| 55 | + state = !state |
| 56 | + ? await runtime.composeState(message) |
| 57 | + : await runtime.updateRecentMessageState(state); |
| 58 | + |
| 59 | + const context = composeContext({ |
| 60 | + state, |
| 61 | + template: priceCheckTemplate, |
| 62 | + }); |
| 63 | + |
| 64 | + const rawContent = await generateObjectDeprecated({ |
| 65 | + runtime, |
| 66 | + context, |
| 67 | + modelClass: ModelClass.SMALL, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!rawContent?.symbol) { |
| 71 | + throw new Error( |
| 72 | + "Could not determine which cryptocurrency to check" |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // Ensure the content has the required shape |
| 77 | + const content = { |
| 78 | + symbol: rawContent.symbol.toString().toUpperCase().trim(), |
| 79 | + quoteCurrency: (rawContent.quoteCurrency || "USDT") |
| 80 | + .toString() |
| 81 | + .toUpperCase() |
| 82 | + .trim(), |
| 83 | + }; |
| 84 | + |
| 85 | + if (content.symbol.length < 2 || content.symbol.length > 10) { |
| 86 | + throw new Error("Invalid cryptocurrency symbol"); |
| 87 | + } |
| 88 | + |
| 89 | + const binanceService = new BinanceService(); |
| 90 | + const priceData = await binanceService.getPrice(content); |
| 91 | + |
| 92 | + if (callback) { |
| 93 | + callback({ |
| 94 | + text: `The current ${content.symbol} price is ${BinanceService.formatPrice(priceData.price)} ${content.quoteCurrency}`, |
| 95 | + content: priceData, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + return true; |
| 100 | + } catch (error) { |
| 101 | + elizaLogger.error("Error in price check:", error); |
| 102 | + if (callback) { |
| 103 | + const errorMessage = error.message.includes("Invalid API key") |
| 104 | + ? "Unable to connect to Binance API" |
| 105 | + : error.message.includes("Invalid symbol") |
| 106 | + ? `Sorry, could not find price for the cryptocurrency symbol you provided` |
| 107 | + : `Sorry, I encountered an error: ${error.message}`; |
| 108 | + |
| 109 | + callback({ |
| 110 | + text: errorMessage, |
| 111 | + content: { error: error.message }, |
| 112 | + }); |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + }, |
| 117 | + examples: [ |
| 118 | + [ |
| 119 | + { |
| 120 | + user: "{{user1}}", |
| 121 | + content: { |
| 122 | + text: "What's the current price of Bitcoin?", |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + user: "{{agent}}", |
| 127 | + content: { |
| 128 | + text: "I'll check the current Bitcoin price for you right away.", |
| 129 | + action: "GET_PRICE", |
| 130 | + }, |
| 131 | + }, |
| 132 | + { |
| 133 | + user: "{{agent}}", |
| 134 | + content: { |
| 135 | + text: "The current BTC price is 42,150.25 USDT", |
| 136 | + }, |
| 137 | + }, |
| 138 | + ], |
| 139 | + [ |
| 140 | + { |
| 141 | + user: "{{user1}}", |
| 142 | + content: { |
| 143 | + text: "Can you check ETH price in EUR?", |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + user: "{{agent}}", |
| 148 | + content: { |
| 149 | + text: "I'll fetch the current Ethereum price in euros for you.", |
| 150 | + action: "GET_PRICE", |
| 151 | + }, |
| 152 | + }, |
| 153 | + { |
| 154 | + user: "{{agent}}", |
| 155 | + content: { |
| 156 | + text: "The current ETH price is 2,245.80 EUR", |
| 157 | + }, |
| 158 | + }, |
| 159 | + ], |
| 160 | + ] as ActionExample[][], |
| 161 | +} as Action; |
0 commit comments