Skip to content

Commit 4a01dc4

Browse files
committed
fixes
1 parent 9195d6a commit 4a01dc4

File tree

11 files changed

+668
-132
lines changed

11 files changed

+668
-132
lines changed

Dockerfile

+18-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,24 @@ RUN service cron start
188188
EXPOSE 3000 5173
189189

190190
# CMD that fetches and runs the entrypoint script
191-
CMD dbus-daemon --system --fork && sh -c 'echo "Fetching latest container entrypoint script..." && \
191+
CMD sh -c 'rm -f /run/dbus/pid && \
192+
# Create necessary directories for D-Bus
193+
mkdir -p /run/dbus && \
194+
chmod 755 /run/dbus && \
195+
mkdir -p /var/run/dbus && \
196+
# Generate machine ID if needed
197+
if [ ! -f /var/lib/dbus/machine-id ]; then \
198+
dbus-uuidgen > /var/lib/dbus/machine-id; \
199+
fi && \
200+
# Create the Chrome D-Bus service file if it doesn't exist
201+
mkdir -p /usr/share/dbus-1/services && \
202+
if [ ! -f /usr/share/dbus-1/services/org.chromium.SessionManager.service ]; then \
203+
echo "[D-BUS Service]\nName=org.chromium.SessionManager\nExec=/usr/bin/chromium --no-sandbox\nUser=root" > /usr/share/dbus-1/services/org.chromium.SessionManager.service; \
204+
fi && \
205+
# Start D-Bus daemon
206+
dbus-daemon --system --fork && \
207+
# Fetch and run the entrypoint script
208+
echo "Fetching latest container entrypoint script..." && \
192209
gsutil cp "gs://${AGENTS_BUCKET_NAME}/_project-files/container-entrypoint.sh" /app/container-entrypoint.sh && \
193210
chmod +x /app/container-entrypoint.sh && \
194211
/app/container-entrypoint.sh'

packages/client-farcaster/src/client.ts

+92-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export class FarcasterClient {
1010
cache: Map<string, any>;
1111
lastInteractionTimestamp: Date;
1212
farcasterConfig: FarcasterConfig;
13+
lastPollTime: Date;
14+
lastCleanupTime: Date;
1315

1416
constructor(opts: {
1517
runtime: IAgentRuntime;
@@ -26,6 +28,8 @@ export class FarcasterClient {
2628
this.signerUuid = opts.signerUuid;
2729
this.lastInteractionTimestamp = new Date();
2830
this.farcasterConfig = opts.farcasterConfig;
31+
this.lastPollTime = new Date();
32+
this.lastCleanupTime = new Date();
2933
}
3034

3135
async loadCastFromNeynarResponse(neynarResponse: any): Promise<Cast> {
@@ -136,14 +140,67 @@ export class FarcasterClient {
136140
return timeline;
137141
}
138142

143+
// async getMentions(request: FidRequest): Promise<Cast[]> {
144+
// const neynarMentionsResponse = await this.neynar.fetchAllNotifications({
145+
// fid: request.fid,
146+
// type: ["mentions", "replies"],
147+
// });
148+
// const mentions: Cast[] = [];
149+
150+
// neynarMentionsResponse.notifications.map((notification) => {
151+
// const cast = {
152+
// hash: notification.cast!.hash,
153+
// authorFid: notification.cast!.author.fid,
154+
// text: notification.cast!.text,
155+
// profile: {
156+
// fid: notification.cast!.author.fid,
157+
// name: notification.cast!.author.display_name || "anon",
158+
// username: notification.cast!.author.username,
159+
// },
160+
// ...(notification.cast!.parent_hash
161+
// ? {
162+
// inReplyTo: {
163+
// hash: notification.cast!.parent_hash,
164+
// fid: notification.cast!.parent_author.fid,
165+
// },
166+
// }
167+
// : {}),
168+
// embeds: notification.cast!.embeds || [],
169+
// timestamp: new Date(notification.cast!.timestamp),
170+
// };
171+
// mentions.push(cast.);
172+
// this.cache.set(`farcaster/cast/${cast.hash}`, cast);
173+
// });
174+
175+
// // console.log('Notifications Counts:', mentions?.length)
176+
177+
// // const neynarMentionMarkedAsSeen = await this.neynar.markNotificationsAsSeen({
178+
// // signerUuid: this.signerUuid
179+
// // });
180+
181+
// // console.log('Notifications Marked as Seen:', neynarMentionMarkedAsSeen);
182+
183+
// return mentions;
184+
// }
185+
139186
async getMentions(request: FidRequest): Promise<Cast[]> {
187+
// Current poll time
188+
const currentPollTime = new Date();
189+
140190
const neynarMentionsResponse = await this.neynar.fetchAllNotifications({
141191
fid: request.fid,
142192
type: ["mentions", "replies"],
143193
});
144194
const mentions: Cast[] = [];
145-
195+
146196
neynarMentionsResponse.notifications.map((notification) => {
197+
const notificationTime = new Date(notification.cast!.timestamp);
198+
199+
// Skip if the notification is older than our last poll time
200+
if (notificationTime <= this.lastPollTime) {
201+
return;
202+
}
203+
147204
const cast = {
148205
hash: notification.cast!.hash,
149206
authorFid: notification.cast!.author.fid,
@@ -162,20 +219,18 @@ export class FarcasterClient {
162219
}
163220
: {}),
164221
embeds: notification.cast!.embeds || [],
165-
timestamp: new Date(notification.cast!.timestamp),
222+
timestamp: notificationTime,
166223
};
167224
mentions.push(cast);
168225
this.cache.set(`farcaster/cast/${cast.hash}`, cast);
169226
});
170-
171-
// console.log('Notifications Counts:', mentions?.length)
172-
173-
// const neynarMentionMarkedAsSeen = await this.neynar.markNotificationsAsSeen({
174-
// signerUuid: this.signerUuid
175-
// });
176-
177-
// console.log('Notifications Marked as Seen:', neynarMentionMarkedAsSeen);
178-
227+
228+
// Update the last poll time for the next call
229+
this.lastPollTime = currentPollTime;
230+
231+
// Clear old cache entries periodically (every 30 minutes)
232+
this.cleanupOldCacheEntries();
233+
console.log('New Notification Casts:', mentions?.length);
179234
return mentions;
180235
}
181236

@@ -241,4 +296,30 @@ export class FarcasterClient {
241296
//nextPageToken: results.nextPageToken,
242297
};
243298
}
299+
300+
// Helper method to clean up old cache entries
301+
private cleanupOldCacheEntries(): void {
302+
// Run cleanup every 30 minutes
303+
const now = new Date();
304+
if (now.getTime() - this.lastCleanupTime.getTime() > 1800000) {
305+
elizaLogger.info("Cleaning up old cache entries");
306+
307+
// Get all keys that start with farcaster/cast/
308+
const castKeys = Array.from(this.cache.keys())
309+
.filter(key => key.startsWith('farcaster/cast/'));
310+
311+
// Remove entries older than 1 hour
312+
const cutoffTime = new Date();
313+
cutoffTime.setHours(cutoffTime.getHours() - 1);
314+
315+
for (const key of castKeys) {
316+
const cast = this.cache.get(key);
317+
if (cast && cast.timestamp && cast.timestamp < cutoffTime) {
318+
this.cache.delete(key);
319+
}
320+
}
321+
322+
this.lastCleanupTime = now;
323+
}
324+
}
244325
}

packages/client-farcaster/src/interactions.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
type IAgentRuntime,
1212
type IImageDescriptionService,
1313
ServiceType,
14-
getEmbeddingZeroVector
14+
getEmbeddingZeroVector,
15+
messageCompletionFooter,
16+
shouldRespondFooter
1517
} from "@elizaos/core";
1618
import type { FarcasterClient } from "./client";
1719
import { toHex } from "viem";
@@ -226,7 +228,7 @@ export class FarcasterInteractionManager {
226228
elizaLogger.error("Error accessing image description service:", error);
227229
}
228230

229-
// Format image descriptions the same way Twitter does
231+
// Format image descriptions
230232
const imageDescriptionsText = imageDescriptionsArray.length > 0
231233
? `\nImages in Cast:\n${imageDescriptionsArray.map((desc, i) =>
232234
`Image ${i + 1}: Title: ${desc.title}\nDescription: ${desc.description}`).join("\n\n")}`
@@ -245,7 +247,9 @@ export class FarcasterInteractionManager {
245247
state,
246248
template:
247249
this.runtime.character.templates
248-
?.farcasterShouldRespondTemplate ||
250+
?.farcasterShouldRespondTemplate &&
251+
this.runtime.character.templates
252+
?.farcasterShouldRespondTemplate + shouldRespondFooter ||
249253
this.runtime.character?.templates?.shouldRespondTemplate ||
250254
shouldRespondTemplate,
251255
});
@@ -290,17 +294,18 @@ export class FarcasterInteractionManager {
290294
state,
291295
template:
292296
this.runtime.character.templates
293-
?.farcasterMessageHandlerTemplate ??
294-
this.runtime.character?.templates?.messageHandlerTemplate ??
297+
?.farcasterMessageHandlerTemplate && this.runtime.character.templates
298+
?.farcasterMessageHandlerTemplate + messageCompletionFooter ||
299+
this.runtime.character?.templates?.messageHandlerTemplate ||
295300
messageHandlerTemplate,
296301
});
297-
302+
console.log('[Farcaster] Context:', context);
298303
const responseContent = await generateMessageResponse({
299304
runtime: this.runtime,
300305
context,
301306
modelClass: ModelClass.LARGE,
302307
});
303-
308+
console.log('[Farcaster] Response:', responseContent);
304309
responseContent.inReplyTo = memoryId;
305310

306311
if (!responseContent.text) return;

packages/client-farcaster/src/prompts.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ ${timeline.map(formatCast).join("\n")}
1919
`;
2020

2121
export const headerTemplate = `
22+
# Action Examples
23+
{{actionExamples}}
24+
(Action examples are for reference only. Do not use the information from them in your response.)
25+
2226
{{timeline}}
2327
2428
# Knowledge
@@ -50,11 +54,20 @@ export const messageHandlerTemplate =
5054
Recent interactions between {{agentName}} and other users:
5155
{{recentPostInteractions}}
5256
53-
Thread of casts You Are Replying To:
57+
Thread of casts You Are Replying To and for context:
5458
{{formattedConversation}}
5559
5660
# Task: Generate a post in the voice, style and perspective of {{agentName}} (@{{farcasterUsername}}):
57-
{{currentPost}}` +
61+
{{currentPost}}
62+
63+
You MUST include an action if the current post text includes a prompt that is similar to one of the available actions mentioned here:
64+
{{actionNames}}
65+
{{actions}}
66+
67+
Here is the current post text again. Remember to include an action if the current post text includes a prompt that asks for one of the available actions mentioned above (does not need to be exact)
68+
{{currentPost}}
69+
70+
` +
5871
messageCompletionFooter;
5972

6073
export const shouldRespondTemplate =

packages/client-telegram/src/messageManager.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
elizaLogger,
66
ServiceType,
77
composeRandomUser,
8+
messageCompletionFooter,
9+
shouldRespondFooter,
810
} from "@elizaos/core";
911
import { getEmbeddingZeroVector } from "@elizaos/core";
1012
import {
@@ -884,7 +886,9 @@ export class MessageManager {
884886
state,
885887
template:
886888
this.runtime.character.templates
887-
?.telegramShouldRespondTemplate ||
889+
?.telegramShouldRespondTemplate &&
890+
this.runtime.character.templates
891+
?.telegramShouldRespondTemplate + shouldRespondFooter ||
888892
this.runtime.character?.templates?.shouldRespondTemplate ||
889893
composeRandomUser(telegramShouldRespondTemplate, 2),
890894
});
@@ -1412,7 +1416,9 @@ export class MessageManager {
14121416
state,
14131417
template:
14141418
this.runtime.character.templates
1415-
?.telegramMessageHandlerTemplate ||
1419+
?.telegramMessageHandlerTemplate &&
1420+
this.runtime.character.templates
1421+
?.telegramMessageHandlerTemplate + messageCompletionFooter ||
14161422
this.runtime.character?.templates
14171423
?.messageHandlerTemplate ||
14181424
telegramMessageHandlerTemplate,

0 commit comments

Comments
 (0)