|
1 | 1 | import { Client, IAgentRuntime, elizaLogger } from "@elizaos/core";
|
| 2 | +import createRabbiTraderPlugin from "@elizaos/plugin-rabbi-trader"; |
2 | 3 |
|
3 | 4 | export class AutoClient {
|
4 |
| - interval: NodeJS.Timeout; |
5 |
| - runtime: IAgentRuntime; |
| 5 | + private interval: NodeJS.Timeout | null = null; |
| 6 | + private runtime: IAgentRuntime; |
| 7 | + private plugin: any; |
| 8 | + static readonly CLIENT_NAME = "auto"; |
6 | 9 |
|
7 | 10 | constructor(runtime: IAgentRuntime) {
|
| 11 | + elizaLogger.log("AutoClient constructor called"); |
8 | 12 | this.runtime = runtime;
|
| 13 | + this.initialize(); |
| 14 | + } |
| 15 | + |
| 16 | + private async initialize() { |
| 17 | + try { |
| 18 | + elizaLogger.log("AutoClient initialization started"); |
| 19 | + await this.initializePlugin(); |
| 20 | + elizaLogger.log("AutoClient initialization completed"); |
| 21 | + } catch (error) { |
| 22 | + elizaLogger.error("AutoClient initialization failed:", error); |
| 23 | + throw error; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private async initializePlugin() { |
| 28 | + try { |
| 29 | + elizaLogger.log( |
| 30 | + "Initializing Rabbi Trader plugin in AutoClient..." |
| 31 | + ); |
| 32 | + |
| 33 | + this.plugin = await createRabbiTraderPlugin( |
| 34 | + (key: string) => this.runtime.getSetting(key), |
| 35 | + this.runtime |
| 36 | + ); |
| 37 | + |
| 38 | + elizaLogger.log("Plugin created, initializing..."); |
| 39 | + await this.plugin.initialize(this.runtime); |
| 40 | + |
| 41 | + elizaLogger.log("Plugin initialized, starting..."); |
| 42 | + await this.plugin.start(); |
| 43 | + |
| 44 | + if (this.plugin.onStart) { |
| 45 | + elizaLogger.log("Triggering plugin onStart..."); |
| 46 | + await this.plugin.onStart(); |
| 47 | + } |
| 48 | + |
| 49 | + elizaLogger.log( |
| 50 | + "Rabbi Trader plugin fully initialized and started in AutoClient" |
| 51 | + ); |
| 52 | + } catch (error) { |
| 53 | + elizaLogger.error( |
| 54 | + "Failed to initialize Rabbi Trader plugin in AutoClient:", |
| 55 | + { |
| 56 | + error, |
| 57 | + phase: "initialization", |
| 58 | + pluginState: this.plugin ? "created" : "null", |
| 59 | + } |
| 60 | + ); |
| 61 | + throw error; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public async stop() { |
| 66 | + elizaLogger.log("Stopping AutoClient..."); |
| 67 | + try { |
| 68 | + if (this.interval) { |
| 69 | + clearInterval(this.interval); |
| 70 | + this.interval = null; |
| 71 | + } |
| 72 | + |
| 73 | + if (this.plugin?.cleanup) { |
| 74 | + await this.plugin.cleanup(); |
| 75 | + } |
9 | 76 |
|
10 |
| - // start a loop that runs every x seconds |
11 |
| - this.interval = setInterval( |
12 |
| - async () => { |
13 |
| - elizaLogger.log("running auto client..."); |
14 |
| - }, |
15 |
| - 60 * 60 * 1000 |
16 |
| - ); // 1 hour in milliseconds |
| 77 | + elizaLogger.log("AutoClient stopped successfully"); |
| 78 | + } catch (error) { |
| 79 | + elizaLogger.error("Error stopping AutoClient:", error); |
| 80 | + throw error; |
| 81 | + } |
17 | 82 | }
|
18 | 83 | }
|
19 | 84 |
|
20 | 85 | export const AutoClientInterface: Client = {
|
21 | 86 | start: async (runtime: IAgentRuntime) => {
|
| 87 | + elizaLogger.log("Starting AutoClient interface..."); |
22 | 88 | const client = new AutoClient(runtime);
|
23 | 89 | return client;
|
24 | 90 | },
|
25 |
| - stop: async (_runtime: IAgentRuntime) => { |
26 |
| - console.warn("Direct client does not support stopping yet"); |
| 91 | + stop: async (runtime: IAgentRuntime) => { |
| 92 | + elizaLogger.log("Stopping AutoClient interface..."); |
| 93 | + const clients = (runtime as any).clients || {}; |
| 94 | + const client = clients["auto"] as AutoClient; |
| 95 | + if (client) { |
| 96 | + await client.stop(); |
| 97 | + } |
27 | 98 | },
|
28 | 99 | };
|
29 | 100 |
|
|
0 commit comments