-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kaito
committed
Jan 5, 2025
1 parent
154a85a
commit b89d46a
Showing
3 changed files
with
44 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,52 @@ | ||
import { Plugin, IAgentRuntime, elizaLogger } from "@elizaos/core"; | ||
import { Plugin, IAgentRuntime, elizaLogger, Service, ServiceType } from "@elizaos/core"; | ||
import { TwitterApiClient } from "./client"; | ||
import { validateTwitterApiConfig } from "./environment"; | ||
|
||
export * from "./types"; | ||
export { TwitterApiClient } from "./client"; | ||
export { validateTwitterApiConfig } from "./environment"; | ||
|
||
export const TwitterApiPlugin: Plugin = { | ||
class TwitterApiService extends Service { | ||
static serviceType = "TWITTER_SEARCH_API" as ServiceType; | ||
public client!: TwitterApiClient; | ||
|
||
async initialize(runtime: IAgentRuntime) { | ||
const config = await validateTwitterApiConfig(runtime); | ||
this.client = new TwitterApiClient(config); | ||
elizaLogger.info("Twitter API Service: Initialized successfully"); | ||
} | ||
|
||
async cleanup() { | ||
// 清理资源的逻辑 | ||
elizaLogger.info("Twitter API Service: Cleaning up..."); | ||
} | ||
|
||
getClient() { | ||
return this.client; | ||
} | ||
} | ||
|
||
export const TwitterApiPlugin = { | ||
name: "twitter-api", | ||
|
||
description: "Twitter API integration using twitterapi.io service", | ||
|
||
async init(runtime: IAgentRuntime) { | ||
try { | ||
const config = await validateTwitterApiConfig(runtime); | ||
const apiClient = new TwitterApiClient(config); | ||
|
||
// Register the client to be available for other components | ||
runtime.registerService("twitterApi", apiClient); | ||
|
||
elizaLogger.info("Twitter API Plugin: Initialized successfully"); | ||
|
||
// 使用标准的 Service 类注册方式 | ||
const twitterService = new TwitterApiService(); | ||
await twitterService.initialize(runtime); | ||
runtime.registerService(twitterService); | ||
|
||
return { | ||
async cleanup() { | ||
elizaLogger.info("Twitter API Plugin: Cleaning up..."); | ||
await twitterService.cleanup(); | ||
} | ||
}; | ||
} catch (error) { | ||
elizaLogger.error("Twitter API Plugin: Initialization failed", error); | ||
throw error; | ||
} | ||
} | ||
} | ||
} as Plugin; | ||
|
||
export default TwitterApiPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { defineConfig } from 'tsup'; | ||
|
||
export default defineConfig({ | ||
entry: ['src/index.ts'], | ||
format: ['cjs', 'esm'], | ||
dts: true, | ||
clean: true, | ||
sourcemap: true | ||
}); |