Skip to content

Commit d492308

Browse files
authored
Merge pull request #912 from onur-saf/main
MAX_TWEET_LENGTH env implementation
2 parents 24ff695 + 28090ed commit d492308

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

packages/client-twitter/src/environment.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { IAgentRuntime } from "@ai16z/eliza";
22
import { z } from "zod";
33

4+
const DEFAULT_MAX_TWEET_LENGTH = 280;
5+
46
export const twitterEnvSchema = z.object({
57
TWITTER_DRY_RUN: z
68
.string()
@@ -9,6 +11,10 @@ export const twitterEnvSchema = z.object({
911
TWITTER_PASSWORD: z.string().min(1, "Twitter password is required"),
1012
TWITTER_EMAIL: z.string().email("Valid Twitter email is required"),
1113
TWITTER_COOKIES: z.string().optional(),
14+
MAX_TWEET_LENGTH: z
15+
.string()
16+
.pipe(z.coerce.number().min(0).int())
17+
.default(DEFAULT_MAX_TWEET_LENGTH.toString()),
1218
});
1319

1420
export type TwitterConfig = z.infer<typeof twitterEnvSchema>;
@@ -34,6 +40,10 @@ export async function validateTwitterConfig(
3440
TWITTER_COOKIES:
3541
runtime.getSetting("TWITTER_COOKIES") ||
3642
process.env.TWITTER_COOKIES,
43+
MAX_TWEET_LENGTH:
44+
runtime.getSetting("MAX_TWEET_LENGTH") ||
45+
process.env.MAX_TWEET_LENGTH ||
46+
DEFAULT_MAX_TWEET_LENGTH.toString(),
3747
};
3848

3949
return twitterEnvSchema.parse(config);

packages/client-twitter/src/post.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,23 @@ const twitterPostTemplate = `
2828
2929
# Task: Generate a post in the voice and style and perspective of {{agentName}} @{{twitterUserName}}.
3030
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.
31-
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.`;
32-
33-
const MAX_TWEET_LENGTH = 280;
31+
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.`;
3432

3533
/**
3634
* Truncate text to fit within the Twitter character limit, ensuring it ends at a complete sentence.
3735
*/
38-
function truncateToCompleteSentence(text: string): string {
39-
if (text.length <= MAX_TWEET_LENGTH) {
36+
function truncateToCompleteSentence(
37+
text: string,
38+
maxTweetLength: number
39+
): string {
40+
if (text.length <= maxTweetLength) {
4041
return text;
4142
}
4243

4344
// Attempt to truncate at the last period within the limit
4445
const truncatedAtPeriod = text.slice(
4546
0,
46-
text.lastIndexOf(".", MAX_TWEET_LENGTH) + 1
47+
text.lastIndexOf(".", maxTweetLength) + 1
4748
);
4849
if (truncatedAtPeriod.trim().length > 0) {
4950
return truncatedAtPeriod.trim();
@@ -52,14 +53,14 @@ function truncateToCompleteSentence(text: string): string {
5253
// If no period is found, truncate to the nearest whitespace
5354
const truncatedAtSpace = text.slice(
5455
0,
55-
text.lastIndexOf(" ", MAX_TWEET_LENGTH)
56+
text.lastIndexOf(" ", maxTweetLength)
5657
);
5758
if (truncatedAtSpace.trim().length > 0) {
5859
return truncatedAtSpace.trim() + "...";
5960
}
6061

6162
// Fallback: Hard truncate and add ellipsis
62-
return text.slice(0, MAX_TWEET_LENGTH - 3).trim() + "...";
63+
return text.slice(0, maxTweetLength - 3).trim() + "...";
6364
}
6465

6566
export class TwitterPostClient {
@@ -147,6 +148,7 @@ export class TwitterPostClient {
147148
},
148149
{
149150
twitterUserName: this.client.profile.username,
151+
maxTweetLength: this.runtime.getSetting("MAX_TWEET_LENGTH"),
150152
}
151153
);
152154

@@ -171,7 +173,10 @@ export class TwitterPostClient {
171173
.trim();
172174

173175
// Use the helper function to truncate to complete sentence
174-
const content = truncateToCompleteSentence(formattedTweet);
176+
const content = truncateToCompleteSentence(
177+
formattedTweet,
178+
Number(this.runtime.getSetting("MAX_TWEET_LENGTH"))
179+
);
175180

176181
if (this.runtime.getSetting("TWITTER_DRY_RUN") === "true") {
177182
elizaLogger.info(

0 commit comments

Comments
 (0)