forked from somewheresystems/centience
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote-tweet.mjs
127 lines (106 loc) · 3.74 KB
/
quote-tweet.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { TwitterPostClient } from '../core/src/clients/twitter/post.js';
import { Runtime } from '../core/src/core/runtime.js';
let initializationInProgress = false;
let clientInitialized = false;
async function initializeClient(runtime) {
if (initializationInProgress) {
console.log("Initialization already in progress, waiting...");
while (initializationInProgress) {
await new Promise(resolve => setTimeout(resolve, 100));
}
return;
}
if (clientInitialized) {
return;
}
try {
initializationInProgress = true;
console.log("Starting Twitter client initialization...");
const client = new TwitterPostClient({ runtime });
await client.init();
clientInitialized = true;
return client;
} finally {
initializationInProgress = false;
}
}
async function quoteTweet(tweetId) {
console.log(`\n=== Starting Quote Tweet Process ===`);
console.log(`Target Tweet ID: ${tweetId}\n`);
const runtime = new Runtime();
await runtime.init();
const client = await initializeClient(runtime);
try {
// Verify tweet exists
console.log("Fetching target tweet...");
const tweet = await client.getTweet(tweetId);
if (!tweet) {
console.error(`❌ Tweet with ID ${tweetId} not found`);
process.exit(1);
}
console.log(`\n✓ Found tweet:`);
console.log(`From: @${tweet.username}`);
console.log(`Text: ${tweet.text}\n`);
// Create conversation context
const conversationContext = {
currentPost: `QUOTE TWEET REQUIRED:
From: @${tweet.username}
Tweet: "${tweet.text}"
Your quote must:
- Directly reference the content above
- Add valuable context or insight
- Stay focused on their exact topic
- Not introduce unrelated points`,
formattedConversation: `@${tweet.username}: ${tweet.text}`
};
console.log("Generating quote content...");
// Generate the quote content
const tweetContent = await client.generateTweetContent(
{
isFirstResponse: true,
currentPost: conversationContext.currentPost,
formattedConversation: conversationContext.formattedConversation
},
{
template: client.runtime.character.templates?.twitterMessageHandlerTemplate,
forceResponse: true
}
);
if (!tweetContent) {
console.error("❌ Failed to generate quote content");
process.exit(1);
}
console.log(`\n✓ Generated quote content:`, tweetContent);
// Send the quote tweet
console.log("\nSending quote tweet...");
const result = await client.requestQueue.add(
async () => await client.twitterClient.sendTweet(
tweetContent,
undefined,
[],
tweetId
)
);
console.log("\n✓ Quote tweet posted successfully!");
console.log("Result:", result);
return result;
} catch (error) {
console.error("\n❌ Error during quote tweet process:", error);
throw error;
}
}
// Main execution
const tweetId = process.argv[2];
if (!tweetId) {
console.error("❌ Please provide a tweet ID");
process.exit(1);
}
quoteTweet(tweetId)
.then(() => {
console.log("\n=== Quote Tweet Process Completed ===");
process.exit(0);
})
.catch(error => {
console.error("\n❌ Failed to complete quote tweet:", error);
process.exit(1);
});