This code I added in packages/client-twitter/src/post.ts
in the generateNewTweet() function.
let homeTimeline = [];
let ownPosts = [];
if (!fs.existsSync("tweetcache")) fs.mkdirSync("tweetcache");
// read the file if it exist
homeTimeline = await this.client.fetchHomeTimeline(10);
fs.writeFileSync(
"tweetcache/home_timeline.json",
JSON.stringify(homeTimeline, null, 2)
);
ownPosts = await this.client.fetchOwnPosts(
5
);
fs.writeFileSync(
"tweetcache/own_posts.json",
JSON.stringify(ownPosts, null, 2)
);
let formattedHomeTimeline =
`# ${this.runtime.character.name}'s Home Timeline (Only use them as a reference.)\n<timeline>\n` +
homeTimeline
.map((tweet) => {
return `From: (@${tweet.username})${tweet.inReplyToStatusId}\nText: ${tweet.text}\n---`;
})
.join("\n");
formattedHomeTimeline += "\n</timeline>";
let formattedOwnPosts =
`Past posts by ${this.runtime.character.name}:\n<ownPosts>\n` +
ownPosts
.filter((tweet) => !tweet.isQuoted && !tweet.isRetweet)
.map((tweet) => {
return `${tweet.text}`;
})
.join("\n");
formattedOwnPosts += "\n</ownPosts>";
const topics = this.runtime.character.topics.join(", ");
const state = await this.runtime.composeState(
{
userId: this.runtime.agentId,
roomId: roomId,
agentId: this.runtime.agentId,
content: {
text: topics || '',
action: "TWEET",
},
},
{
twitterUserName: this.client.profile.username,
timeline: formattedHomeTimeline,
ownPosts: formattedOwnPosts,
}
);
When you change the state by calling composeState
function, you can define a key name of any value, this allows for you to then simply add {{keyName}}
to the template prompts to be injected into your prompt.
Add {{timeline}}
or {{ownPosts}}
to insert into your prompt to bring more context into your prompts.
const twitterPostTemplate = `{{timeline}}
{{providers}}
# About {{agentName}} (@{{twitterUserName}}):
{{bio}}
{{lore}}
{{postDirections}}
{{characterPostExamples}}
# Task: Generate a post in the voice and style based on the post directions of {{agentName}}, aka @{{twitterUserName}}. Use the example posts as a reference for the style and tone of the post, but do not copy the exact text.
Write a post that is {{adjective}} about {{topic}} (without mentioning {{topic}} directly), from the perspective of {{agentName}}.
Do not start the post with "Humans" or "Memory" or anything like that. Make sure each post is unique.
Do not add commentary or ackwowledge this request, just write the post.
Your response should be constrained to 1 sentence and not contain any questions. No emojis. Avoid using complex words that are not commonly used.
`