Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: MAX_TWEET_LENGTH env implementation #912

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 200;
onur-saf marked this conversation as resolved.
Show resolved Hide resolved

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
20 changes: 12 additions & 8 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
onur-saf marked this conversation as resolved.
Show resolved Hide resolved

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();
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 @@ -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(
Expand Down