Skip to content

Commit

Permalink
Merge branch 'ai16z:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
IkigaiLabsETH authored Nov 3, 2024
2 parents 5a32afb + 8084bc8 commit df1fd14
Show file tree
Hide file tree
Showing 32 changed files with 644 additions and 142 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
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"],
};
}
10 changes: 6 additions & 4 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"dev": "tsc && nodemon",
"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 @@ -66,7 +67,8 @@
"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 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
23 changes: 12 additions & 11 deletions core/src/adapters/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ 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(", ");
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 @@ -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
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
Loading

0 comments on commit df1fd14

Please sign in to comment.