From b92dfc378ce4c0b8c3c56c092695ab807c27dd71 Mon Sep 17 00:00:00 2001 From: onur-saf <48022642+onur-saf@users.noreply.github.com> Date: Sun, 8 Dec 2024 07:33:10 +0300 Subject: [PATCH 1/3] MAX_TWEET_LENGTH env implementation --- packages/client-twitter/src/environment.ts | 10 ++++++++++ packages/client-twitter/src/post.ts | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/client-twitter/src/environment.ts b/packages/client-twitter/src/environment.ts index 52af051fe8..88197ad20d 100644 --- a/packages/client-twitter/src/environment.ts +++ b/packages/client-twitter/src/environment.ts @@ -1,6 +1,8 @@ import { IAgentRuntime } from "@ai16z/eliza"; import { z } from "zod"; +const DEFAULT_MAX_TWEET_LENGTH = 200; + export const twitterEnvSchema = z.object({ TWITTER_DRY_RUN: z .string() @@ -9,6 +11,10 @@ export const twitterEnvSchema = z.object({ TWITTER_PASSWORD: z.string().min(1, "Twitter password is required"), TWITTER_EMAIL: z.string().email("Valid Twitter email is required"), TWITTER_COOKIES: z.string().optional(), + MAX_TWEET_LENGTH: z + .string() + .pipe(z.coerce.number().min(0).int()) + .default(DEFAULT_MAX_TWEET_LENGTH.toString()), }); export type TwitterConfig = z.infer; @@ -34,6 +40,10 @@ export async function validateTwitterConfig( TWITTER_COOKIES: runtime.getSetting("TWITTER_COOKIES") || process.env.TWITTER_COOKIES, + MAX_TWEET_LENGTH: + runtime.getSetting("MAX_TWEET_LENGTH") || + process.env.MAX_TWEET_LENGTH || + DEFAULT_MAX_TWEET_LENGTH.toString(), }; return twitterEnvSchema.parse(config); diff --git a/packages/client-twitter/src/post.ts b/packages/client-twitter/src/post.ts index c25d74bd33..24c05dd24f 100644 --- a/packages/client-twitter/src/post.ts +++ b/packages/client-twitter/src/post.ts @@ -30,20 +30,21 @@ const twitterPostTemplate = ` Write a 1-3 sentence post that is {{adjective}} about {{topic}} (without mentioning {{topic}} directly), from the perspective of {{agentName}}. Do not add commentary or acknowledge this request, just write the post. Your response should not contain any questions. Brief, concise statements only. The total character count MUST be less than 280. No emojis. Use \\n\\n (double spaces) between statements.`; -const MAX_TWEET_LENGTH = 280; - /** * Truncate text to fit within the Twitter character limit, ensuring it ends at a complete sentence. */ -function truncateToCompleteSentence(text: string): string { - if (text.length <= MAX_TWEET_LENGTH) { +function truncateToCompleteSentence( + text: string, + maxTweetLength: number +): string { + if (text.length <= maxTweetLength) { return text; } // Attempt to truncate at the last period within the limit const truncatedAtPeriod = text.slice( 0, - text.lastIndexOf(".", MAX_TWEET_LENGTH) + 1 + text.lastIndexOf(".", maxTweetLength) + 1 ); if (truncatedAtPeriod.trim().length > 0) { return truncatedAtPeriod.trim(); @@ -52,14 +53,14 @@ function truncateToCompleteSentence(text: string): string { // If no period is found, truncate to the nearest whitespace const truncatedAtSpace = text.slice( 0, - text.lastIndexOf(" ", MAX_TWEET_LENGTH) + text.lastIndexOf(" ", maxTweetLength) ); if (truncatedAtSpace.trim().length > 0) { return truncatedAtSpace.trim() + "..."; } // Fallback: Hard truncate and add ellipsis - return text.slice(0, MAX_TWEET_LENGTH - 3).trim() + "..."; + return text.slice(0, maxTweetLength - 3).trim() + "..."; } export class TwitterPostClient { @@ -171,7 +172,10 @@ export class TwitterPostClient { .trim(); // Use the helper function to truncate to complete sentence - const content = truncateToCompleteSentence(formattedTweet); + const content = truncateToCompleteSentence( + formattedTweet, + Number(this.runtime.getSetting("MAX_TWEET_LENGTH")) + ); if (this.runtime.getSetting("TWITTER_DRY_RUN") === "true") { elizaLogger.info( From ab320b00414dfdf7d8e7e5fe9c8b98247ffc34d0 Mon Sep 17 00:00:00 2001 From: onur-saf <48022642+onur-saf@users.noreply.github.com> Date: Sun, 8 Dec 2024 17:49:34 +0300 Subject: [PATCH 2/3] update DEFAULT_MAX_TWEET_LENGTH to 280 --- packages/client-twitter/src/environment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client-twitter/src/environment.ts b/packages/client-twitter/src/environment.ts index 88197ad20d..2221e87ec7 100644 --- a/packages/client-twitter/src/environment.ts +++ b/packages/client-twitter/src/environment.ts @@ -1,7 +1,7 @@ import { IAgentRuntime } from "@ai16z/eliza"; import { z } from "zod"; -const DEFAULT_MAX_TWEET_LENGTH = 200; +const DEFAULT_MAX_TWEET_LENGTH = 280; export const twitterEnvSchema = z.object({ TWITTER_DRY_RUN: z From 28090ed5c23e597358d139e7b00165a39d4321e7 Mon Sep 17 00:00:00 2001 From: onur-saf <48022642+onur-saf@users.noreply.github.com> Date: Sun, 8 Dec 2024 18:16:46 +0300 Subject: [PATCH 3/3] update default twitterPostTemplate string --- packages/client-twitter/src/post.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/client-twitter/src/post.ts b/packages/client-twitter/src/post.ts index 24c05dd24f..3c7ff1d08e 100644 --- a/packages/client-twitter/src/post.ts +++ b/packages/client-twitter/src/post.ts @@ -28,7 +28,7 @@ const twitterPostTemplate = ` # Task: Generate a post in the voice and style and perspective of {{agentName}} @{{twitterUserName}}. Write a 1-3 sentence post that is {{adjective}} about {{topic}} (without mentioning {{topic}} directly), from the perspective of {{agentName}}. Do not add commentary or acknowledge this request, just write the post. -Your response should not contain any questions. Brief, concise statements only. The total character count MUST be less than 280. No emojis. Use \\n\\n (double spaces) between statements.`; +Your response should not contain any questions. Brief, concise statements only. The total character count MUST be less than {{maxTweetLength}}. No emojis. Use \\n\\n (double spaces) between statements.`; /** * Truncate text to fit within the Twitter character limit, ensuring it ends at a complete sentence. @@ -148,6 +148,7 @@ export class TwitterPostClient { }, { twitterUserName: this.client.profile.username, + maxTweetLength: this.runtime.getSetting("MAX_TWEET_LENGTH"), } );