|
| 1 | +import { |
| 2 | + type Action, |
| 3 | + generateText, |
| 4 | + type HandlerCallback, |
| 5 | + type IAgentRuntime, |
| 6 | + type Memory, |
| 7 | + ModelClass, |
| 8 | + type State, |
| 9 | + composeContext, |
| 10 | + generateObject, |
| 11 | +} from "@elizaos/core"; |
| 12 | +import { CdpAgentkit } from "@coinbase/cdp-agentkit-core"; |
| 13 | +import { CdpToolkit, type Tool } from "@coinbase/cdp-langchain"; |
| 14 | + |
| 15 | +type GetAgentKitActionsParams = { |
| 16 | + getClient: () => Promise<CdpAgentkit>; |
| 17 | + config?: { |
| 18 | + networkId?: string; |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Get all AgentKit actions |
| 24 | + */ |
| 25 | +export async function getAgentKitActions({ |
| 26 | + getClient, |
| 27 | +}: GetAgentKitActionsParams): Promise<Action[]> { |
| 28 | + const agentkit = await getClient(); |
| 29 | + const cdpToolkit = new CdpToolkit(agentkit); |
| 30 | + const tools = cdpToolkit.getTools(); |
| 31 | + const actions = tools.map((tool: Tool) => ({ |
| 32 | + name: tool.name.toUpperCase(), |
| 33 | + description: tool.description, |
| 34 | + similes: [], |
| 35 | + validate: async () => true, |
| 36 | + handler: async ( |
| 37 | + runtime: IAgentRuntime, |
| 38 | + message: Memory, |
| 39 | + state: State | undefined, |
| 40 | + options?: Record<string, unknown>, |
| 41 | + callback?: HandlerCallback |
| 42 | + ): Promise<boolean> => { |
| 43 | + try { |
| 44 | + const client = await getClient(); |
| 45 | + let currentState = |
| 46 | + state ?? (await runtime.composeState(message)); |
| 47 | + currentState = |
| 48 | + await runtime.updateRecentMessageState(currentState); |
| 49 | + |
| 50 | + const parameterContext = composeParameterContext( |
| 51 | + tool, |
| 52 | + currentState |
| 53 | + ); |
| 54 | + const parameters = await generateParameters( |
| 55 | + runtime, |
| 56 | + parameterContext, |
| 57 | + tool |
| 58 | + ); |
| 59 | + |
| 60 | + const result = await executeToolAction( |
| 61 | + tool, |
| 62 | + parameters, |
| 63 | + client |
| 64 | + ); |
| 65 | + |
| 66 | + const responseContext = composeResponseContext( |
| 67 | + tool, |
| 68 | + result, |
| 69 | + currentState |
| 70 | + ); |
| 71 | + const response = await generateResponse( |
| 72 | + runtime, |
| 73 | + responseContext |
| 74 | + ); |
| 75 | + |
| 76 | + callback?.({ text: response, content: result }); |
| 77 | + return true; |
| 78 | + } catch (error) { |
| 79 | + const errorMessage = |
| 80 | + error instanceof Error ? error.message : String(error); |
| 81 | + callback?.({ |
| 82 | + text: `Error executing action ${tool.name}: ${errorMessage}`, |
| 83 | + content: { error: errorMessage }, |
| 84 | + }); |
| 85 | + return false; |
| 86 | + } |
| 87 | + }, |
| 88 | + examples: [], |
| 89 | + })); |
| 90 | + return actions; |
| 91 | +} |
| 92 | + |
| 93 | +async function executeToolAction( |
| 94 | + tool: Tool, |
| 95 | + parameters: any, |
| 96 | + client: CdpAgentkit |
| 97 | +): Promise<unknown> { |
| 98 | + const toolkit = new CdpToolkit(client); |
| 99 | + const tools = toolkit.getTools(); |
| 100 | + const selectedTool = tools.find((t) => t.name === tool.name); |
| 101 | + |
| 102 | + if (!selectedTool) { |
| 103 | + throw new Error(`Tool ${tool.name} not found`); |
| 104 | + } |
| 105 | + |
| 106 | + return await selectedTool.call(parameters); |
| 107 | +} |
| 108 | + |
| 109 | +function composeParameterContext(tool: any, state: State): string { |
| 110 | + const contextTemplate = `{{recentMessages}} |
| 111 | +
|
| 112 | +Given the recent messages, extract the following information for the action "${tool.name}": |
| 113 | +${tool.description} |
| 114 | +`; |
| 115 | + return composeContext({ state, template: contextTemplate }); |
| 116 | +} |
| 117 | + |
| 118 | +async function generateParameters( |
| 119 | + runtime: IAgentRuntime, |
| 120 | + context: string, |
| 121 | + tool: Tool |
| 122 | +): Promise<unknown> { |
| 123 | + const { object } = await generateObject({ |
| 124 | + runtime, |
| 125 | + context, |
| 126 | + modelClass: ModelClass.LARGE, |
| 127 | + schema: tool.schema, |
| 128 | + }); |
| 129 | + |
| 130 | + return object; |
| 131 | +} |
| 132 | + |
| 133 | +function composeResponseContext( |
| 134 | + tool: Tool, |
| 135 | + result: unknown, |
| 136 | + state: State |
| 137 | +): string { |
| 138 | + const responseTemplate = ` |
| 139 | +# Action Examples |
| 140 | +{{actionExamples}} |
| 141 | +
|
| 142 | +# Knowledge |
| 143 | +{{knowledge}} |
| 144 | +
|
| 145 | +# Task: Generate dialog and actions for the character {{agentName}}. |
| 146 | +About {{agentName}}: |
| 147 | +{{bio}} |
| 148 | +{{lore}} |
| 149 | +
|
| 150 | +{{providers}} |
| 151 | +
|
| 152 | +{{attachments}} |
| 153 | +
|
| 154 | +# Capabilities |
| 155 | +Note that {{agentName}} is capable of reading/seeing/hearing various forms of media, including images, videos, audio, plaintext and PDFs. Recent attachments have been included above under the "Attachments" section. |
| 156 | +
|
| 157 | +The action "${tool.name}" was executed successfully. |
| 158 | +Here is the result: |
| 159 | +${JSON.stringify(result)} |
| 160 | +
|
| 161 | +{{actions}} |
| 162 | +
|
| 163 | +Respond to the message knowing that the action was successful and these were the previous messages: |
| 164 | +{{recentMessages}} |
| 165 | +`; |
| 166 | + return composeContext({ state, template: responseTemplate }); |
| 167 | +} |
| 168 | + |
| 169 | +async function generateResponse( |
| 170 | + runtime: IAgentRuntime, |
| 171 | + context: string |
| 172 | +): Promise<string> { |
| 173 | + return generateText({ |
| 174 | + runtime, |
| 175 | + context, |
| 176 | + modelClass: ModelClass.LARGE, |
| 177 | + }); |
| 178 | +} |
0 commit comments