3
3
// This action should be able to run on a schedule
4
4
// store tweets as memories in db, no reason really to get twitter here
5
5
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" ;
7
15
8
16
export default class Twitter {
9
17
runtime : IAgentRuntime ;
@@ -16,7 +24,6 @@ export default class Twitter {
16
24
}
17
25
18
26
async syncRawTweets ( ) : Promise < boolean > {
19
- console . log ( 'syncRawTweets' )
20
27
await this . runtime . ensureEmbeddingDimension ( )
21
28
try {
22
29
const username = this . runtime . getSetting ( "TWITTER_USERNAME" ) ;
@@ -28,15 +35,24 @@ export default class Twitter {
28
35
source : "twitter" ,
29
36
type : ChannelType . FEED
30
37
} ) ;
31
- console . log ( '' )
32
38
33
39
// 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 ) )
39
49
}
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 ) ;
40
56
41
57
// Check for character-level Twitter credentials
42
58
const twitterConfig : Partial < TwitterConfig > = {
@@ -51,19 +67,12 @@ export default class Twitter {
51
67
Object . entries ( twitterConfig ) . filter ( ( [ _ , v ] ) => v !== undefined )
52
68
) as TwitterConfig ;
53
69
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 ) ;
61
72
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 ) ;
64
74
let syncCount = 0 ;
65
-
66
- for ( const item of list ) {
75
+ for await ( const item of list ) {
67
76
if ( item ?. text && ! item ?. isRetweet ) {
68
77
const tweetId = createUniqueUuid ( this . runtime , item . id ) ;
69
78
@@ -74,7 +83,6 @@ export default class Twitter {
74
83
}
75
84
76
85
// Create memory for the tweet
77
- console . log ( 'creating tweet memory' )
78
86
await this . runtime . messageManager . createMemory ( {
79
87
id : tweetId ,
80
88
userId : this . runtime . agentId ,
@@ -105,7 +113,7 @@ export default class Twitter {
105
113
return true ;
106
114
} catch ( error ) {
107
115
console . error ( 'error syncing tweets' , error )
108
- logger . error ( "Error syncing tweets:" , error ) ;
116
+ // logger.error("Error syncing tweets:", error);
109
117
return false ;
110
118
}
111
119
}
0 commit comments