Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ai16z/eliza into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Nov 3, 2024
2 parents 47ccfc1 + 8084bc8 commit 940cfbb
Show file tree
Hide file tree
Showing 38 changed files with 762 additions and 302 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ jobs:
- name: Run Prettier
run: pnpm run prettier --check .

- name: Create test env file
run: |
echo "TEST_DATABASE_CLIENT=sqlite" > core/.env.test
echo "NODE_ENV=test" >> core/.env.test
- name: Run tests
run: cd core && pnpm test

- name: Build packages
run: pnpm run build
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{
"type": "node",
"request": "launch",
"name": "Launch via npm",
"runtimeExecutable": "npm",
"name": "Launch via pnpm",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["run", "dev"],
"skipFiles": ["<node_internals>/**"]
}
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,28 @@ downloads the model from huggingface and queries it locally
## Discord Bot

For help with setting up your Discord Bot, check out here: https://discordjs.guide/preparations/setting-up-a-bot-application.html

# Development

## Testing

To run the test suite:

```bash
pnpm test # Run tests once
pnpm test:watch # Run tests in watch mode
```

For database-specific tests:
```bash
pnpm test:sqlite # Run tests with SQLite
pnpm test:sqljs # Run tests with SQL.js
```

Tests are written using Jest and can be found in `src/**/*.test.ts` files. The test environment is configured to:
- Load environment variables from `.env.test`
- Use a 2-minute timeout for long-running tests
- Support ESM modules
- Run tests in sequence (--runInBand)

To create new tests, add a `.test.ts` file adjacent to the code you're testing.
3 changes: 2 additions & 1 deletion core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ HELIUS_API_KEY=
## Telegram
TELEGRAM_BOT_TOKEN=

TOGETHER_API_KEY=
TOGETHER_API_KEY=
SERVER_PORT=3000
2 changes: 2 additions & 0 deletions core/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST_DATABASE_CLIENT=sqlite
NODE_ENV=test
9 changes: 4 additions & 5 deletions core/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: "ts-jest",
testEnvironment: "jest-environment-node",
testEnvironment: "node",
rootDir: "./src",
testMatch: ["**/*.test.ts"],
setupFilesAfterEnv: ["<rootDir>/test_resources/testSetup.ts"],
testTimeout: 120000,
globals: {
__DEV__: true,
__TEST__: true,
__VERSION__: "0.0.1",
},
// collectCoverage: true,
// collectCoverageFrom: ["**/*.{ts}", "!**/*.test.{ts}", "!**/node_modules/**", "!**/vendor/**"],
// coverageDirectory: "../coverage",
transform: {
"^.+\\.tsx?$": [
"ts-jest",
Expand All @@ -24,4 +23,4 @@ export default {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
extensionsToTreatAsEsm: [".ts"],
};
}
26 changes: 13 additions & 13 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@
"lint": "eslint . --fix",
"start": "node --loader ts-node/esm src/index.ts",
"start:arok": "node --loader ts-node/esm src/index.ts --characters=\"../characters/arok.character.json\"",
"start:service:ruby": "pm2 start npm --name=\"ruby\" --restart-delay=3000 --max-restarts=10 -- run start:ruby",
"start:service:ruby": "pm2 start pnpm --name=\"ruby\" --restart-delay=3000 --max-restarts=10 -- run start:ruby",
"stop:service:ruby": "pm2 stop ruby",
"start:ruby": "node --loader ts-node/esm src/index.ts --characters=\"../characters/ruby.character.json\"",
"start:service:trump": "pm2 start npm --name=\"trump\" --restart-delay=3000 --max-restarts=10 -- run start:trump",
"start:service:trump": "pm2 start pnpm --name=\"trump\" --restart-delay=3000 --max-restarts=10 -- run start:trump",
"stop:service:trump": "pm2 stop trump",
"start:service:degen": "pm2 start npm --name=\"degen\" --restart-delay=3000 --max-restarts=10 -- run start:degen",
"start:service:degen": "pm2 start pnpm --name=\"degen\" --restart-delay=3000 --max-restarts=10 -- run start:degen",
"stop:service:degen": "pm2 stop degen",
"start:degen": "node --loader ts-node/esm src/index.ts --characters=\"../characters/degenspartan.json\"",
"start:service:all": "pm2 start npm --name=\"all\" --restart-delay=3000 --max-restarts=10 -- run start:all",
"start:service:all": "pm2 start pnpm --name=\"all\" --restart-delay=3000 --max-restarts=10 -- run start:all",
"stop:service:all": "pm2 stop all",
"start:all": "node --loader ts-node/esm src/index.ts --characters=\"../characters/degenspartan.json\",\"../characters/ruby.character.json\"",
"start:all": "node --loader ts-node/esm src/index.ts --characters=\"../characters/degenspartan.json\",\"../characters/ruby.character.json\",\"../characters/pmairca.character.json\"",
"start:trump": "node --loader ts-node/esm src/index.ts --characters=\"../characters/trump.character.json\"",
"start:service:tate": "pm2 start npm --name=\"tate\" --restart-delay=3000 --max-restarts=10 -- run start:tate",
"start:service:tate": "pm2 start pnpm --name=\"tate\" --restart-delay=3000 --max-restarts=10 -- run start:tate",
"stop:service:tate": "pm2 stop tate",
"start:tate": "node --loader ts-node/esm src/index.ts --characters=\"../characters/tate.character.json\"",
"watch": "tsc --watch",
"dev": "tsc && nodemon",
"build:docs": "cd docs && npm run build",
"build:docs": "cd docs && pnpm run build",
"postinstall": "npx playwright install-deps && npx playwright install",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --runInBand --watch -f",
"test:sqlite": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" TEST_DATABASE_CLIENT=sqlite jest --runInBand --watch -f",
"test:sqljs": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" TEST_DATABASE_CLIENT=sqljs jest --runInBand --watch -f"
"test": "jest --runInBand",
"test:watch": "jest --runInBand --watch",
"test:sqlite": "cross-env TEST_DATABASE_CLIENT=sqlite jest --runInBand --watch",
"test:sqljs": "cross-env TEST_DATABASE_CLIENT=sqljs jest --runInBand --watch"
},
"author": "",
"license": "MIT",
Expand Down Expand Up @@ -59,15 +60,15 @@
"itty-router": "5.0.18",
"jest": "29.7.0",
"lint-staged": "15.2.10",
"npm-run-all2": "7.0.1",
"prettier": "3.3.3",
"rimraf": "6.0.1",
"rollup": "2.79.2",
"ts-jest": "29.2.5",
"ts-node": "10.9.2",
"tslib": "2.8.0",
"typescript": "5.6.3",
"wrangler": "3.84.0"
"wrangler": "3.84.0",
"@types/pdfjs-dist": "^2.10.378"
},
"pnpm": {
"overrides": {
Expand All @@ -83,7 +84,6 @@
"@anthropic-ai/sdk": "^0.30.1",
"@cliqz/adblocker-playwright": "1.34.0",
"@coral-xyz/anchor": "^0.30.1",
"@discordjs/opus": "github:discordjs/opus",
"@discordjs/rest": "2.4.0",
"@discordjs/voice": "0.17.0",
"@echogarden/espeak-ng-emscripten": "0.3.0",
Expand Down
21 changes: 21 additions & 0 deletions core/src/actions/imageGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
State,
Action,
} from "../core/types.ts";
import { prettyConsole } from "../index.ts";
import { generateCaption, generateImage } from "./imageGenerationUtils.ts";

export const imageGeneration: Action = {
Expand All @@ -23,11 +24,16 @@ export const imageGeneration: Action = {
options: any,
callback: HandlerCallback
) => {
prettyConsole.log("Composing state for message:", message);
state = (await runtime.composeState(message)) as State;
const userId = runtime.agentId;
prettyConsole.log("User ID:", userId);

const imagePrompt = message.content.text;
prettyConsole.log("Image prompt received:", imagePrompt);
const res: { image: string; caption: string }[] = [];

prettyConsole.log("Generating image with prompt:", imagePrompt);
const images = await generateImage(
{
prompt: imagePrompt,
Expand All @@ -37,16 +43,29 @@ export const imageGeneration: Action = {
},
runtime
);

if (images.success && images.data && images.data.length > 0) {
prettyConsole.log(
"Image generation successful, number of images:",
images.data.length
);
for (let i = 0; i < images.data.length; i++) {
const image = images.data[i];
prettyConsole.log(`Processing image ${i + 1}:`, image);

const caption = await generateCaption(
{
imageUrl: image,
},
runtime
);

prettyConsole.log(
`Generated caption for image ${i + 1}:`,
caption.title
);
res.push({ image: image, caption: caption.title });

callback(
{
text: caption.description,
Expand All @@ -64,6 +83,8 @@ export const imageGeneration: Action = {
[]
);
}
} else {
prettyConsole.error("Image generation failed or returned no data.");
}
},
examples: [
Expand Down
27 changes: 14 additions & 13 deletions core/src/adapters/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
}): Promise<Memory[]> {
const client = await this.pool.connect();
try {
if (params.roomIds.length === 0) return [];
const placeholders = params.roomIds
.map((_, i) => `$${i + 2}`)
.join(", ");

let query = `SELECT * FROM memories WHERE type = $1 AND "roomId" IN (${placeholders})`;
let queryParams = [params.tableName, ...params.roomIds];

Expand Down Expand Up @@ -304,7 +305,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
`;

if (params.unique) {
sql += " AND unique = true";
sql += ` AND "unique" = true`;
}

sql += ` AND 1 - (embedding <-> $3) >= $4
Expand Down Expand Up @@ -349,18 +350,18 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {

if (params.start) {
paramCount++;
sql += ` AND "createdAt" >= to_timestamp($${paramCount / 1000})`;
values.push(params.start);
sql += ` AND "createdAt" >= to_timestamp($${paramCount})`;
values.push(params.start/1000);
}

if (params.end) {
paramCount++;
sql += ` AND "createdAt" <= to_timestamp($${paramCount / 1000})`;
values.push(params.end);
sql += ` AND "createdAt" <= to_timestamp($${paramCount})`;
values.push(params.end/1000);
}

if (params.unique) {
sql += " AND unique = true";
sql += ` AND "unique" = true`;
}

if (params.agentId) {
Expand All @@ -382,9 +383,9 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
return rows.map((row) => ({
...row,
content:
typeof rows.content === "string"
? JSON.parse(rows.content)
: rows.content,
typeof row.content === "string"
? JSON.parse(row.content)
: row.content,
}));
} finally {
client.release();
Expand Down Expand Up @@ -635,7 +636,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {

if (params.unique) {
sql += ` AND "unique" = true`;
}
}

// TODO: Test this
if (params.agentId) {
Expand Down Expand Up @@ -759,7 +760,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
try {
let sql = `SELECT COUNT(*) as count FROM memories WHERE type = $1 AND "roomId" = $2`;
if (unique) {
sql += " AND unique = true";
sql += ` AND "unique" = true`;
}

const { rows } = await client.query(sql, [tableName, roomId]);
Expand Down Expand Up @@ -796,7 +797,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
async getRoomsForParticipants(userIds: UUID[]): Promise<UUID[]> {
const client = await this.pool.connect();
try {
const placeholders = userIds.map((_, i) => `${i + 1}`).join(", ");
const placeholders = userIds.map((_, i) => `$${i + 1}`).join(", ");
const { rows } = await client.query(
`SELECT DISTINCT "roomId" FROM participants WHERE "userId" IN (${placeholders})`,
userIds
Expand Down
6 changes: 3 additions & 3 deletions core/src/adapters/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
const placeholders = params.roomIds.map(() => "?").join(", ");
let sql = `SELECT * FROM memories WHERE type = ? AND roomId IN (${placeholders})`;
let queryParams = [params.tableName, ...params.roomIds];

if (params.agentId) {
sql += ` AND userId = ?`;
queryParams.push(params.agentId);
Expand All @@ -171,9 +171,9 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
content: string;
})[];

return rows.map(row => ({
return rows.map((row) => ({
...row,
content: JSON.parse(row.content)
content: JSON.parse(row.content),
}));
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/adapters/sqlite/sqlite_vec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as sqliteVec from "sqlite-vec";
import { Database } from "better-sqlite3";
import { prettyConsole } from "../../index.ts";

// Loads the sqlite-vec extensions into the provided SQLite database
export function loadVecExtensions(db: Database): void {
try {
// Load sqlite-vec extensions
sqliteVec.load(db);
console.log("sqlite-vec extensions loaded successfully.");
prettyConsole.log("sqlite-vec extensions loaded successfully.");
} catch (error) {
console.error("Failed to load sqlite-vec extensions:", error);
prettyConsole.error("Failed to load sqlite-vec extensions:", error);
throw error;
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/adapters/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,14 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {
tableName: string;
}
): Promise<Memory[]> {

const queryParams = {
query_table_name: params.tableName,
query_roomId: params.roomId,
query_embedding: embedding,
query_match_threshold: params.match_threshold,
query_match_count: params.count,
query_unique: !!params.unique,
}
};
if (params.agentId) {
(queryParams as any).query_agentId = params.agentId;
}
Expand Down
Loading

0 comments on commit 940cfbb

Please sign in to comment.