Skip to content

Commit

Permalink
Merge pull request #912 from onur-saf/main
Browse files Browse the repository at this point in the history
MAX_TWEET_LENGTH env implementation
  • Loading branch information
jkbrooks authored Dec 8, 2024
2 parents 24ff695 + 28090ed commit d492308
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
10 changes: 10 additions & 0 deletions packages/client-twitter/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { IAgentRuntime } from "@ai16z/eliza";
import { z } from "zod";

const DEFAULT_MAX_TWEET_LENGTH = 280;

export const twitterEnvSchema = z.object({
TWITTER_DRY_RUN: z
.string()
Expand All @@ -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<typeof twitterEnvSchema>;
Expand All @@ -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);
Expand Down
23 changes: 14 additions & 9 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ 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.`;

const MAX_TWEET_LENGTH = 280;
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.
*/
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();
Expand All @@ -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 {
Expand Down Expand Up @@ -147,6 +148,7 @@ export class TwitterPostClient {
},
{
twitterUserName: this.client.profile.username,
maxTweetLength: this.runtime.getSetting("MAX_TWEET_LENGTH"),
}
);

Expand All @@ -171,7 +173,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(
Expand Down

0 comments on commit d492308

Please sign in to comment.