|
| 1 | +import { |
| 2 | + type WalletClient, |
| 3 | + type Plugin, |
| 4 | + getDeferredTools, |
| 5 | + addParametersToDescription, |
| 6 | + type ChainForWalletClient, |
| 7 | + type DeferredTool, |
| 8 | +} from "@goat-sdk/core"; |
| 9 | +import { |
| 10 | + type Action, |
| 11 | + generateText, |
| 12 | + type HandlerCallback, |
| 13 | + type IAgentRuntime, |
| 14 | + type Memory, |
| 15 | + ModelClass, |
| 16 | + type State, |
| 17 | + composeContext, |
| 18 | + generateObjectV2, |
| 19 | +} from "@ai16z/eliza"; |
| 20 | + |
| 21 | +type GetOnChainActionsParams<TWalletClient extends WalletClient> = { |
| 22 | + chain: ChainForWalletClient<TWalletClient>; |
| 23 | + getWalletClient: (runtime: IAgentRuntime) => Promise<TWalletClient>; |
| 24 | + plugins: Plugin<TWalletClient>[]; |
| 25 | + supportsSmartWallets?: boolean; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Get all the on chain actions for the given wallet client and plugins |
| 30 | + * |
| 31 | + * @param params |
| 32 | + * @returns |
| 33 | + */ |
| 34 | +export async function getOnChainActions<TWalletClient extends WalletClient>({ |
| 35 | + getWalletClient, |
| 36 | + plugins, |
| 37 | + chain, |
| 38 | + supportsSmartWallets, |
| 39 | +}: GetOnChainActionsParams<TWalletClient>): Promise<Action[]> { |
| 40 | + const tools = await getDeferredTools<TWalletClient>({ |
| 41 | + plugins, |
| 42 | + wordForTool: "action", |
| 43 | + chain, |
| 44 | + supportsSmartWallets, |
| 45 | + }); |
| 46 | + |
| 47 | + return tools |
| 48 | + .map((action) => ({ |
| 49 | + ...action, |
| 50 | + name: action.name.toUpperCase(), |
| 51 | + })) |
| 52 | + .map((tool) => createAction(tool, getWalletClient)); |
| 53 | +} |
| 54 | + |
| 55 | +function createAction<TWalletClient extends WalletClient>( |
| 56 | + tool: DeferredTool<TWalletClient>, |
| 57 | + getWalletClient: (runtime: IAgentRuntime) => Promise<TWalletClient> |
| 58 | +): Action { |
| 59 | + return { |
| 60 | + name: tool.name, |
| 61 | + similes: [], |
| 62 | + description: tool.description, |
| 63 | + validate: async () => true, |
| 64 | + handler: async ( |
| 65 | + runtime: IAgentRuntime, |
| 66 | + message: Memory, |
| 67 | + state: State | undefined, |
| 68 | + options?: Record<string, unknown>, |
| 69 | + callback?: HandlerCallback |
| 70 | + ): Promise<boolean> => { |
| 71 | + try { |
| 72 | + const walletClient = await getWalletClient(runtime); |
| 73 | + let currentState = |
| 74 | + state ?? (await runtime.composeState(message)); |
| 75 | + currentState = |
| 76 | + await runtime.updateRecentMessageState(currentState); |
| 77 | + |
| 78 | + const parameterContext = composeParameterContext( |
| 79 | + tool, |
| 80 | + currentState |
| 81 | + ); |
| 82 | + const parameters = await generateParameters( |
| 83 | + runtime, |
| 84 | + parameterContext, |
| 85 | + tool |
| 86 | + ); |
| 87 | + |
| 88 | + const parsedParameters = tool.parameters.safeParse(parameters); |
| 89 | + if (!parsedParameters.success) { |
| 90 | + callback?.({ |
| 91 | + text: `Invalid parameters for action ${tool.name}: ${parsedParameters.error.message}`, |
| 92 | + content: { error: parsedParameters.error.message }, |
| 93 | + }); |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + const result = await tool.method( |
| 98 | + walletClient, |
| 99 | + parsedParameters.data |
| 100 | + ); |
| 101 | + const responseContext = composeResponseContext( |
| 102 | + tool, |
| 103 | + result, |
| 104 | + currentState |
| 105 | + ); |
| 106 | + const response = await generateResponse( |
| 107 | + runtime, |
| 108 | + responseContext |
| 109 | + ); |
| 110 | + |
| 111 | + callback?.({ text: response, content: result }); |
| 112 | + return true; |
| 113 | + } catch (error) { |
| 114 | + const errorMessage = |
| 115 | + error instanceof Error ? error.message : String(error); |
| 116 | + callback?.({ |
| 117 | + text: `Error executing action ${tool.name}: ${errorMessage}`, |
| 118 | + content: { error: errorMessage }, |
| 119 | + }); |
| 120 | + return false; |
| 121 | + } |
| 122 | + }, |
| 123 | + examples: [], |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +function composeParameterContext<TWalletClient extends WalletClient>( |
| 128 | + tool: DeferredTool<TWalletClient>, |
| 129 | + state: State |
| 130 | +): string { |
| 131 | + const contextTemplate = `{{recentMessages}} |
| 132 | +
|
| 133 | +Given the recent messages, extract the following information for the action "${tool.name}": |
| 134 | +${addParametersToDescription("", tool.parameters)} |
| 135 | +`; |
| 136 | + return composeContext({ state, template: contextTemplate }); |
| 137 | +} |
| 138 | + |
| 139 | +async function generateParameters<TWalletClient extends WalletClient>( |
| 140 | + runtime: IAgentRuntime, |
| 141 | + context: string, |
| 142 | + tool: DeferredTool<TWalletClient> |
| 143 | +): Promise<unknown> { |
| 144 | + const { object } = await generateObjectV2({ |
| 145 | + runtime, |
| 146 | + context, |
| 147 | + modelClass: ModelClass.SMALL, |
| 148 | + schema: tool.parameters, |
| 149 | + }); |
| 150 | + |
| 151 | + return object; |
| 152 | +} |
| 153 | + |
| 154 | +function composeResponseContext<TWalletClient extends WalletClient>( |
| 155 | + tool: DeferredTool<TWalletClient>, |
| 156 | + result: unknown, |
| 157 | + state: State |
| 158 | +): string { |
| 159 | + const responseTemplate = ` |
| 160 | + # Action Examples |
| 161 | +{{actionExamples}} |
| 162 | +(Action examples are for reference only. Do not use the information from them in your response.) |
| 163 | +
|
| 164 | +# Knowledge |
| 165 | +{{knowledge}} |
| 166 | +
|
| 167 | +# Task: Generate dialog and actions for the character {{agentName}}. |
| 168 | +About {{agentName}}: |
| 169 | +{{bio}} |
| 170 | +{{lore}} |
| 171 | +
|
| 172 | +{{providers}} |
| 173 | +
|
| 174 | +{{attachments}} |
| 175 | +
|
| 176 | +# Capabilities |
| 177 | +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. |
| 178 | +
|
| 179 | +The action "${tool.name}" was executed successfully. |
| 180 | +Here is the result: |
| 181 | +${JSON.stringify(result)} |
| 182 | +
|
| 183 | +{{actions}} |
| 184 | +
|
| 185 | +Respond to the message knowing that the action was successful and these were the previous messages: |
| 186 | +{{recentMessages}} |
| 187 | + `; |
| 188 | + return composeContext({ state, template: responseTemplate }); |
| 189 | +} |
| 190 | + |
| 191 | +async function generateResponse( |
| 192 | + runtime: IAgentRuntime, |
| 193 | + context: string |
| 194 | +): Promise<string> { |
| 195 | + return generateText({ |
| 196 | + runtime, |
| 197 | + context, |
| 198 | + modelClass: ModelClass.SMALL, |
| 199 | + }); |
| 200 | +} |
0 commit comments