Skip to content

Commit 4963dea

Browse files
committed
fix race for manager, fix loop that getTweets returns
1 parent ce1f9cb commit 4963dea

File tree

1 file changed

+29
-21
lines changed
  • packages/agent/src/swarm/investmentManager/plugins/degen-intel/providers

1 file changed

+29
-21
lines changed

packages/agent/src/swarm/investmentManager/plugins/degen-intel/providers/twitter.ts

+29-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
// This action should be able to run on a schedule
44
// store tweets as memories in db, no reason really to get twitter here
55

6-
import { type IAgentRuntime, ServiceTypes, logger, createUniqueUuid, type UUID, ChannelType } from "@elizaos/core";
6+
import {
7+
type IAgentRuntime,
8+
type UUID,
9+
ServiceTypes,
10+
logger,
11+
createUniqueUuid,
12+
ChannelType,
13+
stringToUuid
14+
} from "@elizaos/core";
715

816
export default class Twitter {
917
runtime: IAgentRuntime;
@@ -16,7 +24,6 @@ export default class Twitter {
1624
}
1725

1826
async syncRawTweets(): Promise<boolean> {
19-
console.log('syncRawTweets')
2027
await this.runtime.ensureEmbeddingDimension()
2128
try {
2229
const username = this.runtime.getSetting("TWITTER_USERNAME");
@@ -28,15 +35,24 @@ export default class Twitter {
2835
source: "twitter",
2936
type: ChannelType.FEED
3037
});
31-
console.log('')
3238

3339
// get the twitterClient from runtime
34-
const twitterService = this.runtime.getService(ServiceTypes.TWITTER);
35-
if (!twitterService) {
36-
// can have a race condition here...
37-
logger.error("Twitter service/client not found");
38-
return false;
40+
let manager
41+
//console.log('trying to acquire manager')
42+
while(!manager) {
43+
//console.log('do we have manager?')
44+
manager = this.runtime.getService(ServiceTypes.TWITTER)
45+
if (!manager) {
46+
//console.log('Not yet, retrying in 1s')
47+
}
48+
await new Promise((resolve) => setTimeout(resolve, 1000))
3949
}
50+
console.log('degen-intel: Twitter manager acquired, starting sync')
51+
52+
const clientId = stringToUuid("default");
53+
const clientKey = manager.getClientKey(clientId, this.runtime.agentId);
54+
console.log('clientKey', clientKey)
55+
const client = manager.clients.get(clientKey);
4056

4157
// Check for character-level Twitter credentials
4258
const twitterConfig: Partial<TwitterConfig> = {
@@ -51,19 +67,12 @@ export default class Twitter {
5167
Object.entries(twitterConfig).filter(([_, v]) => v !== undefined)
5268
) as TwitterConfig;
5369

54-
// so twitterClient is a _TwitterService
55-
// with .runtime .config .clients
56-
//console.log('runtime', this.runtime)
57-
//console.log('about to create client', this.runtime.agentId)
58-
const twitterClient = await twitterService.createClient(this.runtime, this.runtime.agentId, config)
59-
//console.log('twitterClient', twitterClient)
60-
// is it running?
70+
// get existing one instead of making a new one
71+
let twitterClient = manager.getService(this.runtime.agentId, this.runtime.agentId);
6172

62-
const list = twitterClient.twitterClient.getTweets(username as string, 200);
63-
console.log('list', list.length)
73+
const list = await twitterClient.client.twitterClient.getTweets(username as string, 200);
6474
let syncCount = 0;
65-
66-
for (const item of list) {
75+
for await (const item of list) {
6776
if (item?.text && !item?.isRetweet) {
6877
const tweetId = createUniqueUuid(this.runtime, item.id);
6978

@@ -74,7 +83,6 @@ export default class Twitter {
7483
}
7584

7685
// Create memory for the tweet
77-
console.log('creating tweet memory')
7886
await this.runtime.messageManager.createMemory({
7987
id: tweetId,
8088
userId: this.runtime.agentId,
@@ -105,7 +113,7 @@ export default class Twitter {
105113
return true;
106114
} catch (error) {
107115
console.error('error syncing tweets', error)
108-
logger.error("Error syncing tweets:", error);
116+
//logger.error("Error syncing tweets:", error);
109117
return false;
110118
}
111119
}

0 commit comments

Comments
 (0)