Skip to content

Commit

Permalink
feat: tweet parser without hast and rehype
Browse files Browse the repository at this point in the history
  • Loading branch information
luoling8192 committed Mar 4, 2025
1 parent 471ce13 commit 6fbd133
Show file tree
Hide file tree
Showing 12 changed files with 720 additions and 506 deletions.
98 changes: 41 additions & 57 deletions pnpm-lock.yaml

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

7 changes: 0 additions & 7 deletions services/twitter-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@
"@guiiai/logg": "^1.0.0",
"@modelcontextprotocol/sdk": "^1.6.1",
"@proj-airi/server-sdk": "^0.1.0",
"@types/hast": "^3.0.1",
"defu": "^6.1.4",
"dotenv": "^16.4.7",
"h3": "^1.11.0",
"hast-util-is-element": "^3.0.0",
"hast-util-select": "^6.0.4",
"hast-util-to-text": "^4.0.2",
"listhen": "^1.6.0",
"ofetch": "^1.3.3",
"playwright": "^1.50.1",
"rehype-parse": "^9.0.0",
"unified": "^11.0.3",
"unist-util-visit": "^5.0.0",
"zod": "^3.24.2"
},
"devDependencies": {
Expand Down
92 changes: 1 addition & 91 deletions services/twitter-services/src/adapters/airi-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,5 @@
import type { TimelineOptions, TwitterCredentials, TwitterService } from '../types/twitter'

import { Client } from '@proj-airi/server-sdk'

import { logger } from '../utils/logger'

/**
* Airi Adapter
* Adapts the Twitter service as an Airi module
*/
export class AiriAdapter {
private client: Client
private twitterService: TwitterService
private credentials: TwitterCredentials

constructor(twitterService: TwitterService, options: {
url?: string
token?: string
credentials: TwitterCredentials
}) {
this.twitterService = twitterService
this.credentials = options.credentials

this.client = new Client({
url: options.url,
name: 'twitter-module',
token: options.token,
possibleEvents: [
// Define event types this module can handle
'twitter:login',
'twitter:getTimeline',
'twitter:getTweetDetails',
'twitter:searchTweets',
'twitter:getUserProfile',
'twitter:followUser',
'twitter:likeTweet',
'twitter:retweet',
'twitter:postTweet',
],
})

this.setupEventHandlers()
}

/**
* Set up event handlers
*/
private setupEventHandlers(): void {
// Login handler
this.client.onEvent('twitter:login', async (event) => {
try {
const credentials = event.data.credentials as TwitterCredentials || this.credentials
const success = await this.twitterService.login(credentials)
this.client.send({
type: 'twitter:loginResult',
data: { success },
})
}
catch (error) {
this.client.send({
type: 'twitter:error',
data: { error: error.message, operation: 'login' },
})
}
})

// Timeline handler
this.client.onEvent('twitter:getTimeline', async (event) => {
try {
const options = event.data.options as TimelineOptions || {}
const tweets = await this.twitterService.getTimeline(options)
this.client.send({
type: 'twitter:timelineResult',
data: { tweets },
})
}
catch (error) {
this.client.send({
type: 'twitter:error',
data: { error: error.message, operation: 'getTimeline' },
})
}
})

// Other event handlers...
}

/**
* Start the adapter
*/
async start(): Promise<void> {
// Initialization logic can be added here
logger.airi.log('Airi Twitter adapter started')
}
}
export class AiriAdapter {}
4 changes: 2 additions & 2 deletions services/twitter-services/src/adapters/mcp-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TwitterService } from '../types/twitter'
import type { TwitterService } from '../core/twitter-service'

import { Buffer } from 'node:buffer'
import { createServer } from 'node:http'
Expand Down Expand Up @@ -388,7 +388,7 @@ export class MCPAdapter {
private extractUsernameFromUrl(url: string): string | undefined {
try {
const parsedUrl = new URL(url)
if (parsedUrl.hostname === 'twitter.com' || parsedUrl.hostname === 'x.com') {
if (parsedUrl.hostname === 'x.com') {
const pathParts = parsedUrl.pathname.split('/').filter(Boolean)
if (pathParts.length > 0 && !['search', 'explore', 'home', 'notifications', 'messages'].includes(pathParts[0])) {
return pathParts[0]
Expand Down
Loading

0 comments on commit 6fbd133

Please sign in to comment.