Skip to content

Commit f9733e0

Browse files
authored
Merge pull request #15 from ai16z/main
merge from main
2 parents 1989eae + 8084bc8 commit f9733e0

30 files changed

+534
-135
lines changed

.github/workflows/ci.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ jobs:
2525
- name: Run Prettier
2626
run: pnpm run prettier --check .
2727

28+
- name: Create test env file
29+
run: |
30+
echo "TEST_DATABASE_CLIENT=sqlite" > core/.env.test
31+
echo "NODE_ENV=test" >> core/.env.test
32+
33+
- name: Run tests
34+
run: cd core && pnpm test
35+
2836
- name: Build packages
2937
run: pnpm run build

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,28 @@ downloads the model from huggingface and queries it locally
143143
## Discord Bot
144144

145145
For help with setting up your Discord Bot, check out here: https://discordjs.guide/preparations/setting-up-a-bot-application.html
146+
147+
# Development
148+
149+
## Testing
150+
151+
To run the test suite:
152+
153+
```bash
154+
pnpm test # Run tests once
155+
pnpm test:watch # Run tests in watch mode
156+
```
157+
158+
For database-specific tests:
159+
```bash
160+
pnpm test:sqlite # Run tests with SQLite
161+
pnpm test:sqljs # Run tests with SQL.js
162+
```
163+
164+
Tests are written using Jest and can be found in `src/**/*.test.ts` files. The test environment is configured to:
165+
- Load environment variables from `.env.test`
166+
- Use a 2-minute timeout for long-running tests
167+
- Support ESM modules
168+
- Run tests in sequence (--runInBand)
169+
170+
To create new tests, add a `.test.ts` file adjacent to the code you're testing.

core/.env.example

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ HELIUS_API_KEY=
4242
## Telegram
4343
TELEGRAM_BOT_TOKEN=
4444

45-
TOGETHER_API_KEY=
45+
TOGETHER_API_KEY=
46+
SERVER_PORT=3000

core/.env.test

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TEST_DATABASE_CLIENT=sqlite
2+
NODE_ENV=test

core/jest.config.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/** @type {import('ts-jest').JestConfigWithTsJest} */
22
export default {
33
preset: "ts-jest",
4-
testEnvironment: "jest-environment-node",
4+
testEnvironment: "node",
55
rootDir: "./src",
66
testMatch: ["**/*.test.ts"],
7+
setupFilesAfterEnv: ["<rootDir>/test_resources/testSetup.ts"],
8+
testTimeout: 120000,
79
globals: {
810
__DEV__: true,
911
__TEST__: true,
1012
__VERSION__: "0.0.1",
1113
},
12-
// collectCoverage: true,
13-
// collectCoverageFrom: ["**/*.{ts}", "!**/*.test.{ts}", "!**/node_modules/**", "!**/vendor/**"],
14-
// coverageDirectory: "../coverage",
1514
transform: {
1615
"^.+\\.tsx?$": [
1716
"ts-jest",
@@ -24,4 +23,4 @@ export default {
2423
"^(\\.{1,2}/.*)\\.js$": "$1",
2524
},
2625
extensionsToTreatAsEsm: [".ts"],
27-
};
26+
}

core/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
"dev": "tsc && nodemon",
3030
"build:docs": "cd docs && pnpm run build",
3131
"postinstall": "npx playwright install-deps && npx playwright install",
32-
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --runInBand --watch -f",
33-
"test:sqlite": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" TEST_DATABASE_CLIENT=sqlite jest --runInBand --watch -f",
34-
"test:sqljs": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" TEST_DATABASE_CLIENT=sqljs jest --runInBand --watch -f"
32+
"test": "jest --runInBand",
33+
"test:watch": "jest --runInBand --watch",
34+
"test:sqlite": "cross-env TEST_DATABASE_CLIENT=sqlite jest --runInBand --watch",
35+
"test:sqljs": "cross-env TEST_DATABASE_CLIENT=sqljs jest --runInBand --watch"
3536
},
3637
"author": "",
3738
"license": "MIT",
@@ -66,7 +67,8 @@
6667
"ts-node": "10.9.2",
6768
"tslib": "2.8.0",
6869
"typescript": "5.6.3",
69-
"wrangler": "3.84.0"
70+
"wrangler": "3.84.0",
71+
"@types/pdfjs-dist": "^2.10.378"
7072
},
7173
"pnpm": {
7274
"overrides": {

core/src/actions/imageGeneration.ts

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

1011
export const imageGeneration: Action = {
@@ -23,11 +24,16 @@ export const imageGeneration: Action = {
2324
options: any,
2425
callback: HandlerCallback
2526
) => {
27+
prettyConsole.log("Composing state for message:", message);
2628
state = (await runtime.composeState(message)) as State;
2729
const userId = runtime.agentId;
30+
prettyConsole.log("User ID:", userId);
2831

2932
const imagePrompt = message.content.text;
33+
prettyConsole.log("Image prompt received:", imagePrompt);
3034
const res: { image: string; caption: string }[] = [];
35+
36+
prettyConsole.log("Generating image with prompt:", imagePrompt);
3137
const images = await generateImage(
3238
{
3339
prompt: imagePrompt,
@@ -37,16 +43,29 @@ export const imageGeneration: Action = {
3743
},
3844
runtime
3945
);
46+
4047
if (images.success && images.data && images.data.length > 0) {
48+
prettyConsole.log(
49+
"Image generation successful, number of images:",
50+
images.data.length
51+
);
4152
for (let i = 0; i < images.data.length; i++) {
4253
const image = images.data[i];
54+
prettyConsole.log(`Processing image ${i + 1}:`, image);
55+
4356
const caption = await generateCaption(
4457
{
4558
imageUrl: image,
4659
},
4760
runtime
4861
);
62+
63+
prettyConsole.log(
64+
`Generated caption for image ${i + 1}:`,
65+
caption.title
66+
);
4967
res.push({ image: image, caption: caption.title });
68+
5069
callback(
5170
{
5271
text: caption.description,
@@ -64,6 +83,8 @@ export const imageGeneration: Action = {
6483
[]
6584
);
6685
}
86+
} else {
87+
prettyConsole.error("Image generation failed or returned no data.");
6788
}
6889
},
6990
examples: [

core/src/adapters/postgres.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
107107
}): Promise<Memory[]> {
108108
const client = await this.pool.connect();
109109
try {
110+
if (params.roomIds.length === 0) return [];
110111
const placeholders = params.roomIds
111112
.map((_, i) => `$${i + 2}`)
112113
.join(", ");
@@ -304,7 +305,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
304305
`;
305306

306307
if (params.unique) {
307-
sql += " AND unique = true";
308+
sql += ` AND "unique" = true`;
308309
}
309310

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

350351
if (params.start) {
351352
paramCount++;
352-
sql += ` AND "createdAt" >= to_timestamp($${paramCount / 1000})`;
353-
values.push(params.start);
353+
sql += ` AND "createdAt" >= to_timestamp($${paramCount})`;
354+
values.push(params.start/1000);
354355
}
355356

356357
if (params.end) {
357358
paramCount++;
358-
sql += ` AND "createdAt" <= to_timestamp($${paramCount / 1000})`;
359-
values.push(params.end);
359+
sql += ` AND "createdAt" <= to_timestamp($${paramCount})`;
360+
values.push(params.end/1000);
360361
}
361362

362363
if (params.unique) {
363-
sql += " AND unique = true";
364+
sql += ` AND "unique" = true`;
364365
}
365366

366367
if (params.agentId) {
@@ -382,9 +383,9 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
382383
return rows.map((row) => ({
383384
...row,
384385
content:
385-
typeof rows.content === "string"
386-
? JSON.parse(rows.content)
387-
: rows.content,
386+
typeof row.content === "string"
387+
? JSON.parse(row.content)
388+
: row.content,
388389
}));
389390
} finally {
390391
client.release();
@@ -759,7 +760,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
759760
try {
760761
let sql = `SELECT COUNT(*) as count FROM memories WHERE type = $1 AND "roomId" = $2`;
761762
if (unique) {
762-
sql += " AND unique = true";
763+
sql += ` AND "unique" = true`;
763764
}
764765

765766
const { rows } = await client.query(sql, [tableName, roomId]);
@@ -796,7 +797,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
796797
async getRoomsForParticipants(userIds: UUID[]): Promise<UUID[]> {
797798
const client = await this.pool.connect();
798799
try {
799-
const placeholders = userIds.map((_, i) => `${i + 1}`).join(", ");
800+
const placeholders = userIds.map((_, i) => `$${i + 1}`).join(", ");
800801
const { rows } = await client.query(
801802
`SELECT DISTINCT "roomId" FROM participants WHERE "userId" IN (${placeholders})`,
802803
userIds

core/src/adapters/sqlite/sqlite_vec.ts

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

45
// Loads the sqlite-vec extensions into the provided SQLite database
56
export function loadVecExtensions(db: Database): void {
67
try {
78
// Load sqlite-vec extensions
89
sqliteVec.load(db);
9-
console.log("sqlite-vec extensions loaded successfully.");
10+
prettyConsole.log("sqlite-vec extensions loaded successfully.");
1011
} catch (error) {
11-
console.error("Failed to load sqlite-vec extensions:", error);
12+
prettyConsole.error("Failed to load sqlite-vec extensions:", error);
1213
throw error;
1314
}
1415
}

0 commit comments

Comments
 (0)