forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
256 lines (228 loc) · 8.38 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// utils.ts
import { Tweet } from "agent-twitter-client";
import { embeddingZeroVector } from "@ai16z/eliza/src/memory.ts";
import { Content, Memory, UUID } from "@ai16z/eliza/src/types.ts";
import { stringToUuid } from "@ai16z/eliza/src/uuid.ts";
import { ClientBase } from "./base.ts";
import { elizaLogger } from "@ai16z/eliza/src/logger.ts";
const MAX_TWEET_LENGTH = 280; // Updated to Twitter's current character limit
export const wait = (minTime: number = 1000, maxTime: number = 3000) => {
const waitTime =
Math.floor(Math.random() * (maxTime - minTime + 1)) + minTime;
return new Promise((resolve) => setTimeout(resolve, waitTime));
};
export const isValidTweet = (tweet: Tweet): boolean => {
// Filter out tweets with too many hashtags, @s, or $ signs, probably spam or garbage
const hashtagCount = (tweet.text?.match(/#/g) || []).length;
const atCount = (tweet.text?.match(/@/g) || []).length;
const dollarSignCount = (tweet.text?.match(/\$/g) || []).length;
const totalCount = hashtagCount + atCount + dollarSignCount;
return (
hashtagCount <= 1 &&
atCount <= 2 &&
dollarSignCount <= 1 &&
totalCount <= 3
);
};
export async function buildConversationThread(
tweet: Tweet,
client: ClientBase
): Promise<void> {
const thread: Tweet[] = [];
const visited: Set<string> = new Set();
async function processThread(currentTweet: Tweet) {
if (!currentTweet) {
elizaLogger.log("No current tweet found");
return;
}
// Check if the current tweet has already been saved
const memory = await client.runtime.messageManager.getMemoryById(
stringToUuid(currentTweet.id + "-" + client.runtime.agentId)
);
if (!memory) {
elizaLogger.log("Creating memory for tweet", currentTweet.id);
const roomId = stringToUuid(
currentTweet.conversationId + "-" + client.runtime.agentId
);
const userId =
currentTweet.userId === client.twitterUserId
? client.runtime.agentId
: stringToUuid(currentTweet.userId);
await client.runtime.ensureConnection(
userId,
roomId,
currentTweet.username,
currentTweet.name,
"twitter"
);
await client.runtime.messageManager.createMemory({
id: stringToUuid(
currentTweet.id + "-" + client.runtime.agentId
),
agentId: client.runtime.agentId,
userId: userId,
content: {
text: currentTweet.text,
source: "twitter",
url: currentTweet.permanentUrl,
inReplyTo: currentTweet.inReplyToStatusId
? stringToUuid(
currentTweet.inReplyToStatusId +
"-" +
client.runtime.agentId
)
: undefined,
},
createdAt: currentTweet.timestamp * 1000,
roomId,
embedding: embeddingZeroVector,
});
}
if (visited.has(currentTweet.id)) {
return;
}
visited.add(currentTweet.id);
thread.unshift(currentTweet);
if (currentTweet.inReplyToStatus) {
await processThread(currentTweet.inReplyToStatus);
}
}
await processThread(tweet);
}
export async function sendTweet(
client: ClientBase,
content: Content,
roomId: UUID,
twitterUsername: string,
inReplyTo: string
): Promise<Memory[]> {
const tweetChunks = splitTweetContent(content.text);
const sentTweets: Tweet[] = [];
let previousTweetId = inReplyTo;
for (const chunk of tweetChunks) {
const result = await client.requestQueue.add(
async () =>
await client.twitterClient.sendTweet(
chunk.trim(),
previousTweetId
)
);
// Parse the response
const body = await result.json();
const tweetResult = body.data.create_tweet.tweet_results.result;
const finalTweet: Tweet = {
id: tweetResult.rest_id,
text: tweetResult.legacy.full_text,
conversationId: tweetResult.legacy.conversation_id_str,
//createdAt:
timestamp: tweetResult.timestamp * 1000,
userId: tweetResult.legacy.user_id_str,
inReplyToStatusId: tweetResult.legacy.in_reply_to_status_id_str,
permanentUrl: `https://twitter.com/${twitterUsername}/status/${tweetResult.rest_id}`,
hashtags: [],
mentions: [],
photos: [],
thread: [],
urls: [],
videos: [],
};
sentTweets.push(finalTweet);
previousTweetId = finalTweet.id;
// Wait a bit between tweets to avoid rate limiting issues
await wait(1000, 2000);
}
const memories: Memory[] = sentTweets.map((tweet) => ({
id: stringToUuid(tweet.id + "-" + client.runtime.agentId),
agentId: client.runtime.agentId,
userId: client.runtime.agentId,
content: {
text: tweet.text,
source: "twitter",
url: tweet.permanentUrl,
inReplyTo: tweet.inReplyToStatusId
? stringToUuid(
tweet.inReplyToStatusId + "-" + client.runtime.agentId
)
: undefined,
},
roomId,
embedding: embeddingZeroVector,
createdAt: tweet.timestamp * 1000,
}));
return memories;
}
function splitTweetContent(content: string): string[] {
const maxLength = MAX_TWEET_LENGTH;
const paragraphs = content.split("\n\n").map((p) => p.trim());
const tweets: string[] = [];
let currentTweet = "";
for (const paragraph of paragraphs) {
if (!paragraph) continue;
if ((currentTweet + "\n\n" + paragraph).trim().length <= maxLength) {
if (currentTweet) {
currentTweet += "\n\n" + paragraph;
} else {
currentTweet = paragraph;
}
} else {
if (currentTweet) {
tweets.push(currentTweet.trim());
}
if (paragraph.length <= maxLength) {
currentTweet = paragraph;
} else {
// Split long paragraph into smaller chunks
const chunks = splitParagraph(paragraph, maxLength);
tweets.push(...chunks.slice(0, -1));
currentTweet = chunks[chunks.length - 1];
}
}
}
if (currentTweet) {
tweets.push(currentTweet.trim());
}
return tweets;
}
function splitParagraph(paragraph: string, maxLength: number): string[] {
const sentences = paragraph.match(/[^\.!\?]+[\.!\?]+|[^\.!\?]+$/g) || [paragraph];
const chunks: string[] = [];
let currentChunk = "";
for (const sentence of sentences) {
if ((currentChunk + " " + sentence).trim().length <= maxLength) {
if (currentChunk) {
currentChunk += " " + sentence;
} else {
currentChunk = sentence;
}
} else {
if (currentChunk) {
chunks.push(currentChunk.trim());
}
if (sentence.length <= maxLength) {
currentChunk = sentence;
} else {
// Split long sentence into smaller pieces
const words = sentence.split(" ");
currentChunk = "";
for (const word of words) {
if ((currentChunk + " " + word).trim().length <= maxLength) {
if (currentChunk) {
currentChunk += " " + word;
} else {
currentChunk = word;
}
} else {
if (currentChunk) {
chunks.push(currentChunk.trim());
}
currentChunk = word;
}
}
}
}
}
if (currentChunk) {
chunks.push(currentChunk.trim());
}
return chunks;
}