Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function example1_fetchAndAnalyzeMention() {
console.log(`\nThread has ${thread.replies.length + 1} posts`);

// Analyze with Grok
const analysis = await grok.analyzeAndDecide(mention.post.text, thread);
const analysis = await grok.analyzeAndDecide(mention.post.text, thread, mention.post.id);

console.log(`\nGrok's Decision:`);
console.log(` Action: ${analysis.action.type}`);
Expand Down Expand Up @@ -139,7 +139,7 @@ async function example5_batchProcessMentions() {
const thread = await xClient.fetchThread(conversationId);

if (thread) {
const analysis = await grok.analyzeAndDecide(mention.post.text, 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
Expand Down
3 changes: 2 additions & 1 deletion src/services/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ export class AutonomousAgent {
console.log('\n🤖 Analyzing with Grok AI...');
const analysis = await this.grokService.analyzeAndDecide(
mention.post.text,
thread
thread,
mention.post.id
);

console.log(` Action: ${analysis.action.type.toUpperCase()}`);
Expand Down
17 changes: 9 additions & 8 deletions src/services/grok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export class GrokService {
* Analyze a mention and thread context to determine appropriate action
* @param mention - The text content of the mention to analyze
* @param thread - The thread context including root post and replies
* @param mentionPostId - The ID of the post where the agent was mentioned
* @returns Analysis with recommended action
*/
async analyzeAndDecide(mention: string, thread: XThread): Promise<GrokAnalysis> {
async analyzeAndDecide(mention: string, thread: XThread, mentionPostId: string): Promise<GrokAnalysis> {
if (this.simulationMode) {
return this.simulateAnalysis(mention, thread);
return this.simulateAnalysis(mention, thread, mentionPostId);
}

try {
Expand Down Expand Up @@ -61,12 +62,12 @@ export class GrokService {
const data: any = await response.json();
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected any. Specify a different type.

Copilot uses AI. Check for mistakes.
const analysisText = data.choices[0]?.message?.content || '';

// Use the root post ID from the thread, not the mention text
return this.parseGrokResponse(analysisText, thread.root_post.id);
// Use the mention post ID to reply to the specific post that mentioned the agent
return this.parseGrokResponse(analysisText, mentionPostId);
} catch (error) {
console.error('Error calling Grok API:', error);
// Fallback to simulation
return this.simulateAnalysis(mention, thread);
return this.simulateAnalysis(mention, thread, mentionPostId);
}
}

Expand Down Expand Up @@ -145,7 +146,7 @@ export class GrokService {
/**
* Simulate Grok analysis for testing
*/
private simulateAnalysis(mention: string, thread: XThread): GrokAnalysis {
private simulateAnalysis(mention: string, thread: XThread, mentionPostId: string): GrokAnalysis {
console.log('🤖 Simulated Grok Analysis:');
console.log(` Analyzing: "${mention}"`);

Expand All @@ -159,7 +160,7 @@ export class GrokService {
const analysis: GrokAnalysis = {
action: {
type: 'reply',
target_post_id: thread.root_post.id,
target_post_id: mentionPostId,
content: 'Thanks for reaching out! I\'ve analyzed your question and here\'s my insight: Based on the context, I\'d recommend exploring this topic further. Let me know if you need more specific information!',
reasoning: 'Detected a question, providing helpful response',
},
Expand All @@ -174,7 +175,7 @@ export class GrokService {
const analysis: GrokAnalysis = {
action: {
type: 'analyze',
target_post_id: thread.root_post.id,
target_post_id: mentionPostId,
reasoning: 'No clear action needed, just acknowledgment',
},
confidence: 0.7,
Expand Down
Loading