Skip to content

Commit 04c9bac

Browse files
committedNov 6, 2024·
package up core and introduce plugins
1 parent 52467d6 commit 04c9bac

36 files changed

+1562
-314
lines changed
 

‎agent/index.ts

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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();

‎agent/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@eliza/agent",
3+
"version": "0.0.1",
4+
"main": "index.ts",
5+
"type": "module",
6+
"scripts": {
7+
"build": "tsup --format esm --dts",
8+
"start": "node --loader ts-node/esm index.ts --characters=\"../characters/blobert.character.json\""
9+
},
10+
"dependencies": {
11+
"@eliza/core": "workspace:*",
12+
"readline": "^1.3.0",
13+
"tsup": "^8.3.5"
14+
},
15+
"devDependencies": {
16+
"ts-node": "10.9.2"
17+
}
18+
}

‎agent/tsconfig.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": ["."]
7+
}

‎agent/tsup.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from "tsup";
2+
3+
export default defineConfig({
4+
entry: ["index.ts"],
5+
outDir: "dist",
6+
sourcemap: true,
7+
clean: true,
8+
});

‎core/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"name": "eliza",
2+
"name": "@eliza/core",
33
"version": "1.0.0",
44
"description": "",
55
"main": "dist/index.js",
66
"type": "module",
77
"types": "dist/index.d.ts",
88
"scripts": {
9-
"build": "tsc -p tsconfig.json",
9+
"build": "tsup --format esm --dts",
1010
"lint": "eslint . --fix",
11-
"start": "node --loader ts-node/esm src/index.ts",
11+
"start": "node --loader ts-node/esm src/index.ts --characters=\"../characters/blobert.character.json\"",
1212
"start:arok": "node --loader ts-node/esm src/index.ts --characters=\"../characters/arok.character.json\"",
1313
"start:service:ruby": "pm2 start pnpm --name=\"ruby\" --restart-delay=3000 --max-restarts=10 -- run start:ruby",
1414
"stop:service:ruby": "pm2 stop ruby",
@@ -84,8 +84,8 @@
8484
"@anthropic-ai/sdk": "^0.30.1",
8585
"@cliqz/adblocker-playwright": "1.34.0",
8686
"@coral-xyz/anchor": "^0.30.1",
87-
"@discordjs/rest": "2.4.0",
8887
"@discordjs/opus": "github:discordjs/opus",
88+
"@discordjs/rest": "2.4.0",
8989
"@discordjs/voice": "0.17.0",
9090
"@echogarden/espeak-ng-emscripten": "0.3.0",
9191
"@echogarden/kissfft-wasm": "0.2.0",
@@ -163,6 +163,7 @@
163163
"tiktoken": "1.0.17",
164164
"tinyld": "1.3.4",
165165
"together-ai": "^0.7.0",
166+
"tsup": "^8.3.5",
166167
"unique-names-generator": "4.7.1",
167168
"uuid": "11.0.2",
168169
"wav": "1.0.2",

‎core/src/actions/imageGeneration.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
State,
66
Action,
77
} from "../core/types.ts";
8-
import { prettyConsole } from "../index.ts";
8+
import { elizaLog } from "../index.ts";
99
import { generateCaption, generateImage } from "./imageGenerationUtils.ts";
1010

1111
export const imageGeneration: Action = {
@@ -27,19 +27,19 @@ export const imageGeneration: Action = {
2727
options: any,
2828
callback: HandlerCallback
2929
) => {
30-
prettyConsole.log("Composing state for message:", message);
30+
elizaLog.log("Composing state for message:", message);
3131
state = (await runtime.composeState(message)) as State;
3232
const userId = runtime.agentId;
33-
prettyConsole.log("User ID:", userId);
33+
elizaLog.log("User ID:", userId);
3434

3535
const imagePrompt = message.content.text;
36-
prettyConsole.log("Image prompt received:", imagePrompt);
36+
elizaLog.log("Image prompt received:", imagePrompt);
3737

3838
// TODO: Generate a prompt for the image
3939

4040
const res: { image: string; caption: string }[] = [];
4141

42-
prettyConsole.log("Generating image with prompt:", imagePrompt);
42+
elizaLog.log("Generating image with prompt:", imagePrompt);
4343
const images = await generateImage(
4444
{
4545
prompt: imagePrompt,
@@ -51,13 +51,13 @@ export const imageGeneration: Action = {
5151
);
5252

5353
if (images.success && images.data && images.data.length > 0) {
54-
prettyConsole.log(
54+
elizaLog.log(
5555
"Image generation successful, number of images:",
5656
images.data.length
5757
);
5858
for (let i = 0; i < images.data.length; i++) {
5959
const image = images.data[i];
60-
prettyConsole.log(`Processing image ${i + 1}:`, image);
60+
elizaLog.log(`Processing image ${i + 1}:`, image);
6161

6262
const caption = await generateCaption(
6363
{
@@ -66,7 +66,7 @@ export const imageGeneration: Action = {
6666
runtime
6767
);
6868

69-
prettyConsole.log(
69+
elizaLog.log(
7070
`Generated caption for image ${i + 1}:`,
7171
caption.title
7272
);
@@ -90,7 +90,7 @@ export const imageGeneration: Action = {
9090
);
9191
}
9292
} else {
93-
prettyConsole.error("Image generation failed or returned no data.");
93+
elizaLog.error("Image generation failed or returned no data.");
9494
}
9595
},
9696
examples: [

‎core/src/adapters/sqlite/sqlite_vec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as sqliteVec from "sqlite-vec";
22
import { Database } from "better-sqlite3";
3-
import { prettyConsole } from "../../index.ts";
3+
import { elizaLog } from "../../index.ts";
44

55
// Loads the sqlite-vec extensions into the provided SQLite database
66
export function loadVecExtensions(db: Database): void {
77
try {
88
// Load sqlite-vec extensions
99
sqliteVec.load(db);
10-
prettyConsole.log("sqlite-vec extensions loaded successfully.");
10+
elizaLog.log("sqlite-vec extensions loaded successfully.");
1111
} catch (error) {
12-
prettyConsole.error("Failed to load sqlite-vec extensions:", error);
12+
elizaLog.error("Failed to load sqlite-vec extensions:", error);
1313
throw error;
1414
}
1515
}

‎core/src/cli/colors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export class PrettyConsole {
1+
export class elizaLog {
22
closeByNewLine = true;
33
useIcons = true;
44
logsTitle = "LOGS";

‎core/src/cli/config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import yaml from "js-yaml";
33
import path from "path";
44
import { fileURLToPath } from "url";
55
import { Action } from "../core/types";
6-
import { prettyConsole } from "../index.ts";
6+
import { elizaLog } from "../index.ts";
77

88
const ROOT_DIR = path.resolve(fileURLToPath(import.meta.url), "../../../src");
99

@@ -30,13 +30,13 @@ export async function loadCustomActions(
3030

3131
for (const config of actionConfigs) {
3232
const resolvedPath = path.resolve(ROOT_DIR, config.path);
33-
prettyConsole.log(`Importing action from: ${resolvedPath}`); // Debugging log
33+
elizaLog.log(`Importing action from: ${resolvedPath}`); // Debugging log
3434

3535
try {
3636
const actionModule = await import(resolvedPath);
3737
actions.push(actionModule[config.name]);
3838
} catch (error) {
39-
prettyConsole.error(
39+
elizaLog.error(
4040
`Failed to import action from ${resolvedPath}:`,
4141
error
4242
);

‎core/src/cli/index.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AgentRuntime } from "../core/runtime.ts";
1414
import { defaultActions } from "../core/actions.ts";
1515
import { Arguments } from "../types/index.ts";
1616
import { loadActionConfigs, loadCustomActions } from "./config.ts";
17-
import { prettyConsole } from "../index.ts";
17+
import { elizaLog } from "../index.ts";
1818

1919
export async function initializeClients(
2020
character: Character,
@@ -76,7 +76,6 @@ export function loadCharacters(charactersArg: string): Character[] {
7676
return path;
7777
});
7878

79-
8079
const loadedCharacters = [];
8180

8281
if (characterPaths?.length > 0) {
@@ -210,11 +209,11 @@ export async function startTelegram(
210209
runtime: IAgentRuntime,
211210
character: Character
212211
) {
213-
prettyConsole.log("🔍 Attempting to start Telegram bot...");
212+
elizaLog.log("🔍 Attempting to start Telegram bot...");
214213
const botToken = runtime.getSetting("TELEGRAM_BOT_TOKEN");
215214

216215
if (!botToken) {
217-
prettyConsole.error(
216+
elizaLog.error(
218217
`❌ Telegram bot token is not set for character ${character.name}.`
219218
);
220219
return null;
@@ -223,12 +222,12 @@ export async function startTelegram(
223222
try {
224223
const telegramClient = new Client.TelegramClient(runtime, botToken);
225224
await telegramClient.start();
226-
prettyConsole.success(
225+
elizaLog.success(
227226
`✅ Telegram client successfully started for character ${character.name}`
228227
);
229228
return telegramClient;
230229
} catch (error) {
231-
prettyConsole.error(
230+
elizaLog.error(
232231
`❌ Error creating/starting Telegram client for ${character.name}:`,
233232
error
234233
);
@@ -237,7 +236,7 @@ export async function startTelegram(
237236
}
238237

239238
export async function startTwitter(runtime: IAgentRuntime) {
240-
prettyConsole.log("Starting Twitter clients...");
239+
elizaLog.log("Starting Twitter clients...");
241240
const twitterSearchClient = new Client.TwitterSearchClient(runtime);
242241
await wait();
243242
const twitterInteractionClient = new Client.TwitterInteractionClient(

0 commit comments

Comments
 (0)