|
6 | 6 | ModelClass,
|
7 | 7 | stringToUuid,
|
8 | 8 | elizaLogger,
|
| 9 | + HandlerCallback, |
| 10 | + Content, |
9 | 11 | type IAgentRuntime,
|
10 | 12 | } from "@ai16z/eliza";
|
11 | 13 | import type { FarcasterClient } from "./client";
|
@@ -219,56 +221,65 @@ export class FarcasterInteractionManager {
|
219 | 221 | messageHandlerTemplate,
|
220 | 222 | });
|
221 | 223 |
|
222 |
| - const response = await generateMessageResponse({ |
| 224 | + const responseContent = await generateMessageResponse({ |
223 | 225 | runtime: this.runtime,
|
224 | 226 | context,
|
225 | 227 | modelClass: ModelClass.LARGE,
|
226 | 228 | });
|
227 | 229 |
|
228 |
| - response.inReplyTo = memoryId; |
| 230 | + responseContent.inReplyTo = memoryId; |
229 | 231 |
|
230 |
| - if (!response.text) return; |
| 232 | + if (!responseContent.text) return; |
231 | 233 |
|
232 | 234 | if (this.runtime.getSetting("FARCASTER_DRY_RUN") === "true") {
|
233 | 235 | elizaLogger.info(
|
234 |
| - `Dry run: would have responded to cast ${cast.hash} with ${response.text}` |
| 236 | + `Dry run: would have responded to cast ${cast.hash} with ${responseContent.text}` |
235 | 237 | );
|
236 | 238 | return;
|
237 | 239 | }
|
238 | 240 |
|
239 |
| - try { |
240 |
| - elizaLogger.info(`Replying to cast ${cast.hash}.`); |
241 |
| - |
242 |
| - const results = await sendCast({ |
243 |
| - runtime: this.runtime, |
244 |
| - client: this.client, |
245 |
| - signerUuid: this.signerUuid, |
246 |
| - profile: cast.profile, |
247 |
| - content: response, |
248 |
| - roomId: memory.roomId, |
249 |
| - inReplyTo: { |
250 |
| - fid: cast.authorFid, |
251 |
| - hash: cast.hash, |
252 |
| - }, |
253 |
| - }); |
254 |
| - // sendCast lost response action, so we need to add it back here |
255 |
| - results[0].memory.content.action = response.action; |
256 |
| - |
257 |
| - const newState = await this.runtime.updateRecentMessageState(state); |
258 |
| - |
259 |
| - for (const { memory } of results) { |
260 |
| - await this.runtime.messageManager.createMemory(memory); |
| 241 | + const callback: HandlerCallback = async ( |
| 242 | + content: Content, |
| 243 | + files: any[] |
| 244 | + ) => { |
| 245 | + try { |
| 246 | + if (memoryId && !content.inReplyTo) { |
| 247 | + content.inReplyTo = memoryId; |
| 248 | + } |
| 249 | + const results = await sendCast({ |
| 250 | + runtime: this.runtime, |
| 251 | + client: this.client, |
| 252 | + signerUuid: this.signerUuid, |
| 253 | + profile: cast.profile, |
| 254 | + content: content, |
| 255 | + roomId: memory.roomId, |
| 256 | + inReplyTo: { |
| 257 | + fid: cast.authorFid, |
| 258 | + hash: cast.hash, |
| 259 | + }, |
| 260 | + }); |
| 261 | + // sendCast lost response action, so we need to add it back here |
| 262 | + results[0].memory.content.action = content.action; |
| 263 | + |
| 264 | + for (const { memory } of results) { |
| 265 | + await this.runtime.messageManager.createMemory(memory); |
| 266 | + } |
| 267 | + return results.map((result) => result.memory); |
| 268 | + } catch (error) { |
| 269 | + console.error("Error sending response cast:", error); |
| 270 | + return []; |
261 | 271 | }
|
| 272 | + }; |
262 | 273 |
|
263 |
| - await this.runtime.evaluate(memory, newState); |
| 274 | + const responseMessages = await callback(responseContent); |
264 | 275 |
|
265 |
| - await this.runtime.processActions( |
266 |
| - memory, |
267 |
| - results.map((result) => result.memory), |
268 |
| - newState |
269 |
| - ); |
270 |
| - } catch (error) { |
271 |
| - elizaLogger.error(`Error sending response cast: ${error}`); |
272 |
| - } |
| 276 | + const newState = await this.runtime.updateRecentMessageState(state); |
| 277 | + |
| 278 | + await this.runtime.processActions( |
| 279 | + memory, |
| 280 | + responseMessages, |
| 281 | + newState, |
| 282 | + callback |
| 283 | + ); |
273 | 284 | }
|
274 | 285 | }
|
0 commit comments