|
| 1 | +import { |
| 2 | + Action, |
| 3 | + elizaLogger, |
| 4 | + IAgentRuntime, |
| 5 | + Memory, |
| 6 | + HandlerCallback, |
| 7 | + State, |
| 8 | + getEmbeddingZeroVector, |
| 9 | + Content, |
| 10 | + composeContext, |
| 11 | + generateMessageResponse, |
| 12 | + ModelClass, |
| 13 | +} from "@elizaos/core"; |
| 14 | + |
| 15 | +interface PassportScore { |
| 16 | + address: string; |
| 17 | + score: string; |
| 18 | + threshold: string; |
| 19 | + passing_score: string; |
| 20 | +} |
| 21 | + |
| 22 | +const createTokenMemory = async ( |
| 23 | + runtime: IAgentRuntime, |
| 24 | + _message: Memory, |
| 25 | + formattedOutput: string |
| 26 | +) => { |
| 27 | + const memory: Memory = { |
| 28 | + userId: _message.userId, |
| 29 | + agentId: _message.agentId, |
| 30 | + roomId: _message.roomId, |
| 31 | + content: { text: formattedOutput }, |
| 32 | + createdAt: Date.now(), |
| 33 | + embedding: getEmbeddingZeroVector(), |
| 34 | + }; |
| 35 | + await runtime.messageManager.createMemory(memory); |
| 36 | +}; |
| 37 | + |
| 38 | +export const addressTemplate = `From previous sentence extract only the Ethereum address being asked about. |
| 39 | +Respond with a JSON markdown block containing only the extracted value: |
| 40 | +
|
| 41 | +\`\`\`json |
| 42 | +{ |
| 43 | +"address": string | null |
| 44 | +} |
| 45 | +\`\`\` |
| 46 | +`; |
| 47 | + |
| 48 | +export const getPassportScoreAction: Action = { |
| 49 | + name: "GET_PASSPORT_SCORE", |
| 50 | + description: "Get score from Passport API for an address", |
| 51 | + validate: async (runtime: IAgentRuntime, _message: Memory) => { |
| 52 | + elizaLogger.log("Validating runtime for GET_PASSPORT_SCORE..."); |
| 53 | + const apiKey = runtime.getSetting("PASSPORT_API_KEY"); |
| 54 | + const scorerId = runtime.getSetting("PASSPORT_SCORER"); |
| 55 | + if (!apiKey || !scorerId) { |
| 56 | + elizaLogger.error( |
| 57 | + "Missing PASSPORT_API_KEY or PASSPORT_SCORER settings" |
| 58 | + ); |
| 59 | + return false; |
| 60 | + } |
| 61 | + return true; |
| 62 | + }, |
| 63 | + handler: async ( |
| 64 | + runtime: IAgentRuntime, |
| 65 | + _message: Memory, |
| 66 | + state: State, |
| 67 | + _options: any, |
| 68 | + callback: HandlerCallback |
| 69 | + ) => { |
| 70 | + elizaLogger.log("Starting GET_PASSPORT_SCORE handler..."); |
| 71 | + const apiKey = runtime.getSetting("PASSPORT_API_KEY"); |
| 72 | + const scorerId = runtime.getSetting("PASSPORT_SCORER"); |
| 73 | + |
| 74 | + if (!state) { |
| 75 | + state = (await runtime.composeState(_message)) as State; |
| 76 | + } else { |
| 77 | + state = await runtime.updateRecentMessageState(state); |
| 78 | + } |
| 79 | + |
| 80 | + const context = composeContext({ |
| 81 | + state, |
| 82 | + template: `${_message.content.text}\n${addressTemplate}`, |
| 83 | + }); |
| 84 | + |
| 85 | + const addressRequest = await generateMessageResponse({ |
| 86 | + runtime, |
| 87 | + context, |
| 88 | + modelClass: ModelClass.SMALL, |
| 89 | + }); |
| 90 | + |
| 91 | + const address = addressRequest.address as string; |
| 92 | + |
| 93 | + if (!address) { |
| 94 | + callback({ text: "Address is required." }, []); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + const response = await fetch( |
| 100 | + `https://api.passport.xyz/v2/stamps/${scorerId}/score/${address}`, |
| 101 | + { |
| 102 | + method: "GET", |
| 103 | + headers: { |
| 104 | + "X-API-KEY": apiKey, |
| 105 | + accept: "application/json", |
| 106 | + }, |
| 107 | + } |
| 108 | + ); |
| 109 | + |
| 110 | + if (!response.ok) { |
| 111 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 112 | + } |
| 113 | + |
| 114 | + const data: PassportScore = await response.json(); |
| 115 | + const formattedOutput = `Address: ${data.address}\nScore: ${data.score}${data.passing_score ? "\nScore is above threshold" : `\nScore is below threshold (${data.threshold})`}`; |
| 116 | + |
| 117 | + await createTokenMemory(runtime, _message, formattedOutput); |
| 118 | + |
| 119 | + callback({ text: formattedOutput }, []); |
| 120 | + } catch (error) { |
| 121 | + elizaLogger.error("Error fetching Passport score:", error); |
| 122 | + callback( |
| 123 | + { |
| 124 | + text: "Failed to fetch Passport score. Please check the logs for more details.", |
| 125 | + }, |
| 126 | + [] |
| 127 | + ); |
| 128 | + } |
| 129 | + }, |
| 130 | + examples: [], |
| 131 | + similes: [ |
| 132 | + "GET_PASSPORT_SCORE", |
| 133 | + "FETCH_PASSPORT_SCORE", |
| 134 | + "CHECK_PASSPORT_SCORE", |
| 135 | + "VIEW_PASSPORT_SCORE", |
| 136 | + ], |
| 137 | +}; |
0 commit comments