Skip to content

Commit e0c72a4

Browse files
authoredDec 21, 2024
Merge pull request #1279 from ai16z/feat/redis_adapter
feat: Redis Cache Implementation
2 parents 227fcdf + 4a1fd97 commit e0c72a4

File tree

7 files changed

+17562
-166
lines changed

7 files changed

+17562
-166
lines changed
 

‎packages/adapter-redis/.npmignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*
2+
3+
!dist/**
4+
!package.json
5+
!readme.md
6+
!tsup.config.ts
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import eslintGlobalConfig from "../../eslint.config.mjs";
2+
3+
export default [...eslintGlobalConfig];

‎packages/adapter-redis/package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "@ai16z/adapter-redis",
3+
"version": "0.1.6-alpha.4",
4+
"main": "dist/index.js",
5+
"type": "module",
6+
"types": "dist/index.d.ts",
7+
"dependencies": {
8+
"@ai16z/eliza": "workspace:*",
9+
"ioredis": "5.4.2"
10+
},
11+
"devDependencies": {
12+
"@types/ioredis": "^5.0.0",
13+
"tsup": "8.3.5"
14+
},
15+
"scripts": {
16+
"build": "tsup --format esm --dts",
17+
"dev": "tsup --format esm --dts --watch",
18+
"lint": "eslint --fix --cache ."
19+
},
20+
"peerDependencies": {
21+
"whatwg-url": "7.1.0"
22+
}
23+
}

‎packages/adapter-redis/src/index.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import Redis from "ioredis";
2+
import { IDatabaseCacheAdapter, UUID, elizaLogger } from "@ai16z/eliza";
3+
4+
export class RedisClient implements IDatabaseCacheAdapter {
5+
private client: Redis;
6+
7+
constructor(redisUrl: string) {
8+
this.client = new Redis(redisUrl);
9+
10+
this.client.on("connect", () => {
11+
elizaLogger.success("Connected to Redis");
12+
});
13+
14+
this.client.on("error", (err) => {
15+
elizaLogger.error("Redis error:", err);
16+
});
17+
}
18+
19+
async getCache(params: {
20+
agentId: UUID;
21+
key: string;
22+
}): Promise<string | undefined> {
23+
try {
24+
const redisKey = this.buildKey(params.agentId, params.key);
25+
const value = await this.client.get(redisKey);
26+
return value || undefined;
27+
} catch (err) {
28+
elizaLogger.error("Error getting cache:", err);
29+
return undefined;
30+
}
31+
}
32+
33+
async setCache(params: {
34+
agentId: UUID;
35+
key: string;
36+
value: string;
37+
}): Promise<boolean> {
38+
try {
39+
const redisKey = this.buildKey(params.agentId, params.key);
40+
await this.client.set(redisKey, params.value);
41+
return true;
42+
} catch (err) {
43+
elizaLogger.error("Error setting cache:", err);
44+
return false;
45+
}
46+
}
47+
48+
async deleteCache(params: {
49+
agentId: UUID;
50+
key: string;
51+
}): Promise<boolean> {
52+
try {
53+
const redisKey = this.buildKey(params.agentId, params.key);
54+
const result = await this.client.del(redisKey);
55+
return result > 0;
56+
} catch (err) {
57+
elizaLogger.error("Error deleting cache:", err);
58+
return false;
59+
}
60+
}
61+
62+
async disconnect(): Promise<void> {
63+
try {
64+
await this.client.quit();
65+
elizaLogger.success("Disconnected from Redis");
66+
} catch (err) {
67+
elizaLogger.error("Error disconnecting from Redis:", err);
68+
}
69+
}
70+
71+
private buildKey(agentId: UUID, key: string): string {
72+
return `${agentId}:${key}`; // Constructs a unique key based on agentId and key
73+
}
74+
}
75+
76+
export default RedisClient;

‎packages/adapter-redis/tsconfig.json

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

‎packages/adapter-redis/tsup.config.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig } from "tsup";
2+
3+
export default defineConfig({
4+
entry: ["src/index.ts"],
5+
outDir: "dist",
6+
sourcemap: true,
7+
clean: true,
8+
format: ["esm"], // Ensure you're targeting CommonJS
9+
external: [
10+
"dotenv", // Externalize dotenv to prevent bundling
11+
"fs", // Externalize fs to use Node.js built-in module
12+
"path", // Externalize other built-ins if necessary
13+
"@reflink/reflink",
14+
"@node-llama-cpp",
15+
"https",
16+
"http",
17+
"agentkeepalive",
18+
"uuid",
19+
// Add other modules you want to externalize
20+
],
21+
});

0 commit comments

Comments
 (0)