Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add callback to action in farcaster client #1002

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 46 additions & 35 deletions packages/client-farcaster/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
ModelClass,
stringToUuid,
elizaLogger,
HandlerCallback,
Content,
type IAgentRuntime,
} from "@ai16z/eliza";
import type { FarcasterClient } from "./client";
Expand Down Expand Up @@ -219,56 +221,65 @@ export class FarcasterInteractionManager {
messageHandlerTemplate,
});

const response = await generateMessageResponse({
const responseContent = await generateMessageResponse({
runtime: this.runtime,
context,
modelClass: ModelClass.LARGE,
});

response.inReplyTo = memoryId;
responseContent.inReplyTo = memoryId;

if (!response.text) return;
if (!responseContent.text) return;

if (this.runtime.getSetting("FARCASTER_DRY_RUN") === "true") {
elizaLogger.info(
`Dry run: would have responded to cast ${cast.hash} with ${response.text}`
`Dry run: would have responded to cast ${cast.hash} with ${responseContent.text}`
);
return;
}

try {
elizaLogger.info(`Replying to cast ${cast.hash}.`);

const results = await sendCast({
runtime: this.runtime,
client: this.client,
signerUuid: this.signerUuid,
profile: cast.profile,
content: response,
roomId: memory.roomId,
inReplyTo: {
fid: cast.authorFid,
hash: cast.hash,
},
});
// sendCast lost response action, so we need to add it back here
results[0].memory.content.action = response.action;

const newState = await this.runtime.updateRecentMessageState(state);

for (const { memory } of results) {
await this.runtime.messageManager.createMemory(memory);
const callback: HandlerCallback = async (
content: Content,
files: any[]
) => {
try {
if (memoryId && !content.inReplyTo) {
content.inReplyTo = memoryId;
}
const results = await sendCast({
runtime: this.runtime,
client: this.client,
signerUuid: this.signerUuid,
profile: cast.profile,
content: content,
roomId: memory.roomId,
inReplyTo: {
fid: cast.authorFid,
hash: cast.hash,
},
});
// sendCast lost response action, so we need to add it back here
results[0].memory.content.action = content.action;

for (const { memory } of results) {
await this.runtime.messageManager.createMemory(memory);
}
return results.map((result) => result.memory);
} catch (error) {
console.error("Error sending response cast:", error);
return [];
}
};

await this.runtime.evaluate(memory, newState);
const responseMessages = await callback(responseContent);

await this.runtime.processActions(
memory,
results.map((result) => result.memory),
newState
);
} catch (error) {
elizaLogger.error(`Error sending response cast: ${error}`);
}
const newState = await this.runtime.updateRecentMessageState(state);

await this.runtime.processActions(
memory,
responseMessages,
newState,
callback
);
}
}
Loading