-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.ts
More file actions
187 lines (144 loc) Β· 6.23 KB
/
examples.ts
File metadata and controls
187 lines (144 loc) Β· 6.23 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Example: Custom Agent Usage
*
* This example shows how to use MyXstack components programmatically
* to build custom automation workflows.
*/
import { loadConfig } from './services/config.js';
import { XAPIClient } from './services/xapi.js';
import { GrokService } from './services/grok.js';
async function example1_fetchAndAnalyzeMention() {
console.log('\n=== Example 1: Fetch and Analyze a Mention ===\n');
const config = loadConfig();
const xClient = new XAPIClient(config.xApiConfig);
const grok = new GrokService(config.xaiApiKey);
// Fetch recent mentions
const mentions = await xClient.fetchMentions(config.username);
console.log(`Found ${mentions.length} mention(s)`);
if (mentions.length > 0) {
const mention = mentions[0];
console.log(`\nMention from @${mention.post.author_username}:`);
console.log(`"${mention.post.text}"`);
// Fetch thread context
const conversationId = mention.post.conversation_id || mention.post.id;
const thread = await xClient.fetchThread(conversationId);
if (thread) {
console.log(`\nThread has ${thread.replies.length + 1} posts`);
// Analyze with Grok
const analysis = await grok.analyzeAndDecide(mention.post.text, thread, mention.post.id);
console.log(`\nGrok's Decision:`);
console.log(` Action: ${analysis.action.type}`);
console.log(` Confidence: ${(analysis.confidence * 100).toFixed(1)}%`);
console.log(` Reasoning: ${analysis.explanation}`);
if (analysis.action.content) {
console.log(` Suggested content: "${analysis.action.content}"`);
}
}
}
}
async function example2_customReply() {
console.log('\n=== Example 2: Post a Custom Reply ===\n');
const config = loadConfig();
const xClient = new XAPIClient(config.xApiConfig);
// In this example, we manually specify a tweet to reply to
const tweetId = 'sim_123456789'; // Replace with real tweet ID
const replyText = 'Thanks for reaching out! This is an automated response from MyXstack. π€';
console.log(`Posting reply to tweet ${tweetId}...`);
const success = await xClient.postReply(tweetId, replyText);
if (success) {
console.log('β
Reply posted successfully!');
} else {
console.log('β Failed to post reply');
}
}
async function example3_searchAndSummarize() {
console.log('\n=== Example 3: Search and Summarize ===\n');
const config = loadConfig();
const xClient = new XAPIClient(config.xApiConfig);
const grok = new GrokService(config.xaiApiKey);
// Search for tweets about a topic
const query = 'artificial intelligence';
console.log(`Searching for: "${query}"`);
const results = await xClient.searchTweets(query);
console.log(`Found ${results.length} results`);
if (results.length > 0) {
console.log('\nTop results:');
results.slice(0, 3).forEach((tweet, idx) => {
console.log(`${idx + 1}. @${tweet.author_username}: "${tweet.text}"`);
});
// You could then use Grok to summarize these results
console.log('\n(In a real scenario, Grok could summarize these findings)');
}
}
async function example4_monitorKeyword() {
console.log('\n=== Example 4: Monitor Keyword ===\n');
const config = loadConfig();
const xClient = new XAPIClient(config.xApiConfig);
const keyword = 'AI agent';
console.log(`Monitoring keyword: "${keyword}"`);
console.log('Checking every 10 seconds for 30 seconds...\n');
let checks = 0;
const maxChecks = 3;
const intervalId = setInterval(async () => {
checks++;
console.log(`Check ${checks}/${maxChecks}...`);
const results = await xClient.searchTweets(keyword);
console.log(` Found ${results.length} tweets mentioning "${keyword}"`);
if (checks >= maxChecks) {
clearInterval(intervalId);
console.log('\nβ
Monitoring complete!');
}
}, 10000);
}
async function example5_batchProcessMentions() {
console.log('\n=== Example 5: Batch Process Mentions ===\n');
const config = loadConfig();
const xClient = new XAPIClient(config.xApiConfig);
const grok = new GrokService(config.xaiApiKey);
// Fetch all mentions
const mentions = await xClient.fetchMentions(config.username);
console.log(`Processing ${mentions.length} mention(s) in batch...\n`);
// Process each mention
for (let i = 0; i < mentions.length; i++) {
const mention = mentions[i];
console.log(`[${i + 1}/${mentions.length}] Processing mention from @${mention.post.author_username}`);
const conversationId = mention.post.conversation_id || mention.post.id;
const thread = await xClient.fetchThread(conversationId);
if (thread) {
const analysis = await grok.analyzeAndDecide(mention.post.text, thread, mention.post.id);
console.log(` β Action: ${analysis.action.type} (${(analysis.confidence * 100).toFixed(0)}% confidence)`);
// In a real scenario, you might execute the action here
// await executeAction(analysis.action);
}
console.log('');
}
console.log('β
Batch processing complete!');
}
// Main function to run examples
async function main() {
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log('β MyXstack - Programmatic Usage Examples β');
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
try {
// Uncomment the example you want to run:
await example1_fetchAndAnalyzeMention();
// await example2_customReply();
// await example3_searchAndSummarize();
// await example4_monitorKeyword(); // This one runs for 30 seconds
// await example5_batchProcessMentions();
console.log('\n⨠Example completed successfully!\n');
} catch (error) {
console.error('β Error running example:', error);
}
}
// Run if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}
export {
example1_fetchAndAnalyzeMention,
example2_customReply,
example3_searchAndSummarize,
example4_monitorKeyword,
example5_batchProcessMentions,
};