Skip to content
Closed
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
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/services/grok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export class GrokService {
throw new Error(`Grok API error: ${response.status}`);
}

const data: any = await response.json();
const data = await response.json() as { choices?: Array<{ message?: { content?: string } }> };
if (!Array.isArray(data.choices)) {
throw new Error('Unexpected Grok API response: missing choices array');
}
const analysisText = data.choices[0]?.message?.content || '';

// Use the root post ID from the thread, not the mention text
Expand Down Expand Up @@ -113,9 +116,14 @@ export class GrokService {
}

const parsed = JSON.parse(jsonMatch[0]);


const validActions: AgentAction['type'][] = ['reply', 'search', 'generate', 'analyze'];
const actionType: AgentAction['type'] = validActions.includes(parsed.action)
? parsed.action
: 'analyze';

const action: AgentAction = {
type: parsed.action as any,
type: actionType,
target_post_id: mentionPostId,
content: parsed.content,
query: parsed.action === 'search' ? parsed.content : undefined,
Expand Down
17 changes: 12 additions & 5 deletions src/services/xapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ export class XAPIClient {
throw new Error('Failed to get user ID from response');
}

const mentionsResponse = await this.makeXAPIRequest(
`https://api.twitter.com/2/users/${userId}/mentions?max_results=10&expansions=author_id&tweet.fields=created_at,conversation_id,in_reply_to_user_id,referenced_tweets`,
'GET'
);
const params = new URLSearchParams({
max_results: '10',
expansions: 'author_id',
'tweet.fields': 'created_at,conversation_id,in_reply_to_user_id,referenced_tweets',
});
const mentionsUrl = `https://api.twitter.com/2/users/${userId}/mentions?${params.toString()}`;
const mentionsResponse = await this.makeXAPIRequest(mentionsUrl, 'GET');

if (!mentionsResponse || !Array.isArray(mentionsResponse.data)) {
console.warn('Invalid response from X API (mentions)');
Expand Down Expand Up @@ -77,7 +80,11 @@ export class XAPIClient {
'GET'
);

return this.parseThread(response.data || []);
if (!Array.isArray(response.data)) {
console.warn('Unexpected response shape from X API (thread): data is not an array');
return null;
}
return this.parseThread(response.data);
} catch (error) {
console.error('Error fetching thread:', error);
return null;
Expand Down