|
| 1 | +import { |
| 2 | + Character, |
| 3 | + createAgentRuntime, |
| 4 | + createDirectRuntime, |
| 5 | + DirectClient, |
| 6 | + getTokenForProvider, |
| 7 | + IAgentRuntime, |
| 8 | + initializeClients, |
| 9 | + initializeDatabase, |
| 10 | + loadCharacters, |
| 11 | + parseArguments, |
| 12 | +} from "@eliza/core"; |
| 13 | +import { Arguments } from "@eliza/core/src/types"; |
| 14 | +import readline from "readline"; |
| 15 | + |
| 16 | +let argv: Arguments = parseArguments(); |
| 17 | +let basePath = "./"; |
| 18 | +// if argv.isroot is true, then set the base path to "../" |
| 19 | +if (argv.isRoot) { |
| 20 | + basePath = "../"; |
| 21 | +} |
| 22 | + |
| 23 | +// if the path doesnt start with a /, add the base path to the beginning of the path |
| 24 | +if (!argv.characters?.startsWith("/")) { |
| 25 | + argv.characters = `${basePath}${argv.characters}`; |
| 26 | +} |
| 27 | + |
| 28 | +let characters = loadCharacters(argv.characters); |
| 29 | +if (!characters || characters.length === 0) { |
| 30 | + console.error("No characters loaded. Please check the characters file."); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +characters.forEach((char, index) => { |
| 35 | + if (!char.name || !char.modelProvider) { |
| 36 | + console.error( |
| 37 | + `Character at index ${index} is missing required fields.` |
| 38 | + ); |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | +}); |
| 42 | + |
| 43 | +const directClient = new DirectClient(); |
| 44 | + |
| 45 | +const serverPort = parseInt(process.env.SERVER_PORT || "3000"); |
| 46 | +directClient.start(serverPort); |
| 47 | + |
| 48 | +async function startAgent(character: Character) { |
| 49 | + try { |
| 50 | + const token = getTokenForProvider(character.modelProvider, character); |
| 51 | + const db = initializeDatabase(); |
| 52 | + |
| 53 | + const runtime = await createAgentRuntime(character, db, token); |
| 54 | + const directRuntime = createDirectRuntime(character, db, token); |
| 55 | + |
| 56 | + const clients = await initializeClients( |
| 57 | + character, |
| 58 | + runtime as IAgentRuntime |
| 59 | + ); |
| 60 | + directClient.registerAgent(await directRuntime); |
| 61 | + |
| 62 | + return clients; |
| 63 | + } catch (error) { |
| 64 | + console.error( |
| 65 | + `Error starting agent for character ${character.name}:`, |
| 66 | + error |
| 67 | + ); |
| 68 | + throw error; // Re-throw after logging |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const startAgents = async () => { |
| 73 | + try { |
| 74 | + for (const character of characters) { |
| 75 | + await startAgent(character); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + console.error("Error starting agents:", error); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +startAgents().catch((error) => { |
| 83 | + console.error("Unhandled error in startAgents:", error); |
| 84 | + process.exit(1); // Exit the process after logging |
| 85 | +}); |
| 86 | + |
| 87 | +const rl = readline.createInterface({ |
| 88 | + input: process.stdin, |
| 89 | + output: process.stdout, |
| 90 | +}); |
| 91 | + |
| 92 | +function chat() { |
| 93 | + rl.question("You: ", async (input) => { |
| 94 | + if (input.toLowerCase() === "exit") { |
| 95 | + rl.close(); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const agentId = characters[0].name.toLowerCase(); |
| 100 | + const response = await fetch( |
| 101 | + `http://localhost:3000/${agentId}/message`, |
| 102 | + { |
| 103 | + method: "POST", |
| 104 | + headers: { |
| 105 | + "Content-Type": "application/json", |
| 106 | + }, |
| 107 | + body: JSON.stringify({ |
| 108 | + text: input, |
| 109 | + userId: "user", |
| 110 | + userName: "User", |
| 111 | + }), |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + const data = await response.json(); |
| 116 | + for (const message of data) { |
| 117 | + console.log(`${characters[0].name}: ${message.text}`); |
| 118 | + } |
| 119 | + chat(); |
| 120 | + }); |
| 121 | +} |
| 122 | + |
| 123 | +console.log("Chat started. Type 'exit' to quit."); |
| 124 | +chat(); |
0 commit comments