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: Optimize Agent Action Processing by Prioritizing Timelines and Limiting Actions Per Cycle #1824

Merged
merged 10 commits into from
Jan 6, 2025
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ POST_IMMEDIATELY=
# Twitter action processing configuration
ACTION_INTERVAL= # Interval in minutes between action processing runs (default: 5 minutes)
ENABLE_ACTION_PROCESSING=false # Set to true to enable the action processing loop
MAX_ACTIONS_PROCESSING=1 # Maximum number of actions (e.g., retweets, likes) to process in a single cycle. Helps prevent excessive or uncontrolled actions.
ACTION_TIMELINE_TYPE=foryou # Type of timeline to interact with. Options: "foryou" or "following". Default: "foryou"

# Feature Flags
IMAGE_GEN= # Set to TRUE to enable image generation
Expand Down
13 changes: 8 additions & 5 deletions packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getEmbeddingZeroVector,
elizaLogger,
stringToUuid,
ActionTimelineType,
} from "@elizaos/core";
import {
QueryTweetsResponse,
Expand Down Expand Up @@ -317,14 +318,16 @@ export class ClientBase extends EventEmitter {
return processedTimeline;
}

async fetchTimelineForActions(count: number): Promise<Tweet[]> {
async fetchTimelineForActions(): Promise<Tweet[]> {
elizaLogger.debug("fetching timeline for actions");

const agentUsername = this.twitterConfig.TWITTER_USERNAME;
const homeTimeline = await this.twitterClient.fetchHomeTimeline(
count,
[]
);

const homeTimeline =
this.twitterConfig.ACTION_TIMELINE_TYPE ===
ActionTimelineType.Following
? await this.twitterClient.fetchFollowingTimeline(20, [])
: await this.twitterClient.fetchHomeTimeline(20, []);

return homeTimeline
.map((tweet) => ({
Expand Down
20 changes: 19 additions & 1 deletion packages/client-twitter/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { parseBooleanFromText, IAgentRuntime } from "@elizaos/core";
import {
parseBooleanFromText,
IAgentRuntime,
ActionTimelineType,
} from "@elizaos/core";
import { z, ZodError } from "zod";

export const DEFAULT_MAX_TWEET_LENGTH = 280;
Expand Down Expand Up @@ -67,6 +71,10 @@ export const twitterEnvSchema = z.object({
ACTION_INTERVAL: z.number().int(),
POST_IMMEDIATELY: z.boolean(),
TWITTER_SPACES_ENABLE: z.boolean().default(false),
MAX_ACTIONS_PROCESSING: z.number().int(),
ACTION_TIMELINE_TYPE: z
.nativeEnum(ActionTimelineType)
.default(ActionTimelineType.ForYou),
});

export type TwitterConfig = z.infer<typeof twitterEnvSchema>;
Expand Down Expand Up @@ -205,6 +213,16 @@ export async function validateTwitterConfig(
runtime.getSetting("TWITTER_SPACES_ENABLE") ||
process.env.TWITTER_SPACES_ENABLE
) ?? false,

MAX_ACTIONS_PROCESSING: safeParseInt(
runtime.getSetting("MAX_ACTIONS_PROCESSING") ||
process.env.MAX_ACTIONS_PROCESSING,
1
),

ACTION_TIMELINE_TYPE:
runtime.getSetting("ACTION_TIMELINE_TYPE") ||
process.env.ACTION_TIMELINE_TYPE,
};

return twitterEnvSchema.parse(twitterConfig);
Expand Down
Loading
Loading