|
| 1 | +import defaultCharacter from "../core/defaultCharacter.ts"; |
| 2 | +import settings from "../core/settings.ts"; |
| 3 | +import { Character, IAgentRuntime, ModelProvider } from "../core/types.ts"; |
| 4 | +import * as Action from "../actions/index.ts"; |
| 5 | +import * as Client from "../clients/index.ts"; |
| 6 | +import * as Adapter from "../adapters/index.ts"; |
| 7 | +import * as Provider from "../providers/index.ts"; |
| 8 | +import yargs from "yargs"; |
| 9 | +import { wait } from "../clients/twitter/utils.ts"; |
| 10 | + |
| 11 | +import fs from "fs"; |
| 12 | +import Database from "better-sqlite3"; |
| 13 | +import { AgentRuntime } from "../core/runtime.ts"; |
| 14 | +import { defaultActions } from "../core/actions.ts"; |
| 15 | +import { Arguments } from "../types/index.ts"; |
| 16 | + |
| 17 | +export async function initializeClients( |
| 18 | + character: Character, |
| 19 | + runtime: IAgentRuntime |
| 20 | +) { |
| 21 | + const clients = []; |
| 22 | + const clientTypes = |
| 23 | + character.clients?.map((str) => str.toLowerCase()) || []; |
| 24 | + |
| 25 | + if (clientTypes.includes("discord")) { |
| 26 | + clients.push(startDiscord(runtime)); |
| 27 | + } |
| 28 | + |
| 29 | + if (clientTypes.includes("telegram")) { |
| 30 | + const telegramClient = await startTelegram(runtime, character); |
| 31 | + if (telegramClient) clients.push(telegramClient); |
| 32 | + } |
| 33 | + |
| 34 | + if (clientTypes.includes("twitter")) { |
| 35 | + const twitterClients = await startTwitter(runtime); |
| 36 | + clients.push(...twitterClients); |
| 37 | + } |
| 38 | + |
| 39 | + return clients; |
| 40 | +} |
| 41 | + |
| 42 | +export function parseArguments(): Arguments { |
| 43 | + try { |
| 44 | + return yargs(process.argv.slice(2)) |
| 45 | + .option("character", { |
| 46 | + type: "string", |
| 47 | + description: "Path to the character JSON file", |
| 48 | + }) |
| 49 | + .option("characters", { |
| 50 | + type: "string", |
| 51 | + description: |
| 52 | + "Comma separated list of paths to character JSON files", |
| 53 | + }) |
| 54 | + .option("telegram", { |
| 55 | + type: "boolean", |
| 56 | + description: "Enable Telegram client", |
| 57 | + default: false, |
| 58 | + }) |
| 59 | + .parseSync() as Arguments; |
| 60 | + } catch (error) { |
| 61 | + console.error("Error parsing arguments:", error); |
| 62 | + return {}; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export function loadCharacters(charactersArg: string): Character[] { |
| 67 | + const characterPaths = charactersArg?.split(",").map((path) => path.trim()); |
| 68 | + const loadedCharacters = []; |
| 69 | + |
| 70 | + if (characterPaths?.length > 0) { |
| 71 | + for (const path of characterPaths) { |
| 72 | + try { |
| 73 | + const character = JSON.parse(fs.readFileSync(path, "utf8")); |
| 74 | + loadedCharacters.push(character); |
| 75 | + } catch (e) { |
| 76 | + console.error(`Error loading character from ${path}: ${e}`); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (loadedCharacters.length === 0) { |
| 82 | + console.log("No characters found, using default character"); |
| 83 | + loadedCharacters.push(defaultCharacter); |
| 84 | + } |
| 85 | + |
| 86 | + return loadedCharacters; |
| 87 | +} |
| 88 | + |
| 89 | +export function getTokenForProvider( |
| 90 | + provider: ModelProvider, |
| 91 | + character: Character |
| 92 | +) { |
| 93 | + switch (provider) { |
| 94 | + case ModelProvider.OPENAI: |
| 95 | + return ( |
| 96 | + character.settings?.secrets?.OPENAI_API_KEY || |
| 97 | + settings.OPENAI_API_KEY |
| 98 | + ); |
| 99 | + case ModelProvider.ANTHROPIC: |
| 100 | + return ( |
| 101 | + character.settings?.secrets?.CLAUDE_API_KEY || |
| 102 | + settings.CLAUDE_API_KEY |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | +export function initializeDatabase() { |
| 107 | + if (process.env.POSTGRES_URL) { |
| 108 | + return new Adapter.PostgresDatabaseAdapter({ |
| 109 | + connectionString: process.env.POSTGRES_URL, |
| 110 | + }); |
| 111 | + } else { |
| 112 | + return new Adapter.SqliteDatabaseAdapter(new Database("./db.sqlite")); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export function createAgentRuntime( |
| 117 | + character: Character, |
| 118 | + db: any, |
| 119 | + token: string |
| 120 | +) { |
| 121 | + return new AgentRuntime({ |
| 122 | + databaseAdapter: db, |
| 123 | + token, |
| 124 | + modelProvider: character.modelProvider, |
| 125 | + evaluators: [], |
| 126 | + character, |
| 127 | + providers: [Provider.timeProvider, Provider.boredomProvider], |
| 128 | + actions: [ |
| 129 | + ...defaultActions, |
| 130 | + Action.askClaude, |
| 131 | + Action.followRoom, |
| 132 | + Action.unfollowRoom, |
| 133 | + Action.unmuteRoom, |
| 134 | + Action.muteRoom, |
| 135 | + Action.imageGeneration, |
| 136 | + ], |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +export function createDirectRuntime( |
| 141 | + character: Character, |
| 142 | + db: any, |
| 143 | + token: string |
| 144 | +) { |
| 145 | + return new AgentRuntime({ |
| 146 | + databaseAdapter: db, |
| 147 | + token, |
| 148 | + modelProvider: character.modelProvider, |
| 149 | + evaluators: [], |
| 150 | + character, |
| 151 | + providers: [Provider.timeProvider, Provider.boredomProvider], |
| 152 | + actions: [...defaultActions], |
| 153 | + }); |
| 154 | +} |
| 155 | + |
| 156 | +export function startDiscord(runtime: IAgentRuntime) { |
| 157 | + return new Client.DiscordClient(runtime); |
| 158 | +} |
| 159 | + |
| 160 | +export async function startTelegram( |
| 161 | + runtime: IAgentRuntime, |
| 162 | + character: Character |
| 163 | +) { |
| 164 | + console.log("🔍 Attempting to start Telegram bot..."); |
| 165 | + const botToken = runtime.getSetting("TELEGRAM_BOT_TOKEN"); |
| 166 | + |
| 167 | + if (!botToken) { |
| 168 | + console.error( |
| 169 | + `❌ Telegram bot token is not set for character ${character.name}.` |
| 170 | + ); |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + const telegramClient = new Client.TelegramClient(runtime, botToken); |
| 176 | + await telegramClient.start(); |
| 177 | + console.log( |
| 178 | + `✅ Telegram client successfully started for character ${character.name}` |
| 179 | + ); |
| 180 | + return telegramClient; |
| 181 | + } catch (error) { |
| 182 | + console.error( |
| 183 | + `❌ Error creating/starting Telegram client for ${character.name}:`, |
| 184 | + error |
| 185 | + ); |
| 186 | + return null; |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +export async function startTwitter(runtime: IAgentRuntime) { |
| 191 | + console.log("Starting Twitter clients..."); |
| 192 | + const twitterSearchClient = new Client.TwitterSearchClient(runtime); |
| 193 | + await wait(); |
| 194 | + const twitterInteractionClient = new Client.TwitterInteractionClient( |
| 195 | + runtime |
| 196 | + ); |
| 197 | + await wait(); |
| 198 | + const twitterGenerationClient = new Client.TwitterGenerationClient(runtime); |
| 199 | + |
| 200 | + return [ |
| 201 | + twitterInteractionClient, |
| 202 | + twitterSearchClient, |
| 203 | + twitterGenerationClient, |
| 204 | + ]; |
| 205 | +} |
0 commit comments