Skip to content

Commit 5fd9b40

Browse files
committed
feat: improve Twitter client with action processing + fix post interval, remove double debug logs, revert truncateToCompleteSentence function
1 parent b50a5bf commit 5fd9b40

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

packages/client-twitter/src/post.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,18 @@ const MAX_TWEET_LENGTH = 240;
6262
/**
6363
* Truncate text to fit within the Twitter character limit, ensuring it ends at a complete sentence.
6464
*/
65-
function truncateToCompleteSentence(text: string): string {
66-
if (text.length <= MAX_TWEET_LENGTH) {
65+
function truncateToCompleteSentence(
66+
text: string,
67+
maxTweetLength: number
68+
): string {
69+
if (text.length <= maxTweetLength) {
6770
return text;
6871
}
6972

7073
// Attempt to truncate at the last period within the limit
7174
const truncatedAtPeriod = text.slice(
7275
0,
73-
text.lastIndexOf(".", MAX_TWEET_LENGTH) + 1
76+
text.lastIndexOf(".", maxTweetLength) + 1
7477
);
7578
if (truncatedAtPeriod.trim().length > 0) {
7679
return truncatedAtPeriod.trim();
@@ -79,16 +82,17 @@ function truncateToCompleteSentence(text: string): string {
7982
// If no period is found, truncate to the nearest whitespace
8083
const truncatedAtSpace = text.slice(
8184
0,
82-
text.lastIndexOf(" ", MAX_TWEET_LENGTH)
85+
text.lastIndexOf(" ", maxTweetLength)
8386
);
8487
if (truncatedAtSpace.trim().length > 0) {
8588
return truncatedAtSpace.trim() + "...";
8689
}
8790

8891
// Fallback: Hard truncate and add ellipsis
89-
return text.slice(0, MAX_TWEET_LENGTH - 3).trim() + "...";
92+
return text.slice(0, maxTweetLength - 3).trim() + "...";
9093
}
9194

95+
9296
export class TwitterPostClient {
9397
client: ClientBase;
9498
runtime: IAgentRuntime;
@@ -113,9 +117,9 @@ export class TwitterPostClient {
113117

114118
const lastPostTimestamp = lastPost?.timestamp ?? 0;
115119
const minMinutes =
116-
parseInt(this.runtime.getSetting("POST_INTERVAL_MIN")) || 1;
120+
parseInt(this.runtime.getSetting("POST_INTERVAL_MIN")) || 90;
117121
const maxMinutes =
118-
parseInt(this.runtime.getSetting("POST_INTERVAL_MAX")) || 2;
122+
parseInt(this.runtime.getSetting("POST_INTERVAL_MAX")) || 180;
119123
const randomMinutes =
120124
Math.floor(Math.random() * (maxMinutes - minMinutes + 1)) +
121125
minMinutes;
@@ -227,7 +231,6 @@ export class TwitterPostClient {
227231
});
228232

229233
elizaLogger.debug("generate post prompt:\n" + context);
230-
console.log("generate post prompt:\n" + context);
231234

232235
const newTweetContent = await generateText({
233236
runtime: this.runtime,
@@ -264,7 +267,7 @@ export class TwitterPostClient {
264267
}
265268

266269
// Use the helper function to truncate to complete sentence
267-
const content = truncateToCompleteSentence(cleanedContent);
270+
const content = truncateToCompleteSentence(cleanedContent, MAX_TWEET_LENGTH);
268271

269272
const removeQuotes = (str: string) =>
270273
str.replace(/^['"](.*)['"]$/, "$1");

0 commit comments

Comments
 (0)