@ai.ntellect/core 0.6.17 → 0.6.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +1 -2
- package/README.md +123 -178
- package/dist/graph/controller.js +29 -6
- package/dist/graph/index.js +302 -62
- package/dist/index.js +21 -6
- package/dist/interfaces/index.js +15 -0
- package/dist/modules/agenda/adapters/node-cron/index.js +29 -0
- package/dist/modules/agenda/index.js +140 -0
- package/dist/{services/embedding.js → modules/embedding/adapters/ai/index.js} +24 -7
- package/dist/modules/embedding/index.js +59 -0
- package/dist/modules/memory/adapters/in-memory/index.js +210 -0
- package/dist/{memory → modules/memory}/adapters/meilisearch/index.js +97 -2
- package/dist/{memory → modules/memory}/adapters/redis/index.js +77 -15
- package/dist/modules/memory/index.js +103 -0
- package/dist/utils/{stringifiy-zod-schema.js → generate-action-schema.js} +5 -5
- package/graph/controller.ts +37 -13
- package/graph/index.ts +348 -73
- package/index.ts +24 -6
- package/interfaces/index.ts +346 -27
- package/modules/agenda/adapters/node-cron/index.ts +25 -0
- package/modules/agenda/index.ts +159 -0
- package/modules/embedding/adapters/ai/index.ts +42 -0
- package/modules/embedding/index.ts +45 -0
- package/modules/memory/adapters/in-memory/index.ts +203 -0
- package/{memory → modules/memory}/adapters/meilisearch/index.ts +114 -12
- package/modules/memory/adapters/redis/index.ts +164 -0
- package/modules/memory/index.ts +93 -0
- package/package.json +3 -1
- package/test/graph/index.test.ts +646 -0
- package/test/modules/agenda/node-cron.test.ts +286 -0
- package/test/modules/embedding/ai.test.ts +78 -0
- package/test/modules/memory/adapters/in-memory.test.ts +153 -0
- package/test/{memory → modules/memory}/adapters/meilisearch.test.ts +79 -75
- package/test/modules/memory/adapters/redis.test.ts +169 -0
- package/test/modules/memory/base.test.ts +230 -0
- package/test/services/agenda.test.ts +279 -280
- package/types/index.ts +82 -203
- package/utils/{stringifiy-zod-schema.ts → generate-action-schema.ts} +3 -3
- package/app/README.md +0 -36
- package/app/app/favicon.ico +0 -0
- package/app/app/globals.css +0 -21
- package/app/app/gun.ts +0 -0
- package/app/app/layout.tsx +0 -18
- package/app/app/page.tsx +0 -321
- package/app/eslint.config.mjs +0 -16
- package/app/next.config.ts +0 -7
- package/app/package-lock.json +0 -5912
- package/app/package.json +0 -31
- package/app/pnpm-lock.yaml +0 -4031
- package/app/postcss.config.mjs +0 -8
- package/app/public/file.svg +0 -1
- package/app/public/globe.svg +0 -1
- package/app/public/next.svg +0 -1
- package/app/public/vercel.svg +0 -1
- package/app/public/window.svg +0 -1
- package/app/tailwind.config.ts +0 -18
- package/app/tsconfig.json +0 -27
- package/dist/memory/index.js +0 -9
- package/dist/services/agenda.js +0 -115
- package/dist/services/queue.js +0 -142
- package/dist/utils/experimental-graph-rag.js +0 -152
- package/dist/utils/generate-object.js +0 -111
- package/dist/utils/inject-actions.js +0 -16
- package/dist/utils/queue-item-transformer.js +0 -24
- package/dist/utils/sanitize-results.js +0 -60
- package/memory/adapters/redis/index.ts +0 -103
- package/memory/index.ts +0 -22
- package/services/agenda.ts +0 -118
- package/services/embedding.ts +0 -26
- package/services/queue.ts +0 -145
- package/test/memory/adapters/redis.test.ts +0 -159
- package/test/memory/base.test.ts +0 -225
- package/test/services/queue.test.ts +0 -286
- package/utils/experimental-graph-rag.ts +0 -170
- package/utils/generate-object.ts +0 -117
- package/utils/inject-actions.ts +0 -19
- package/utils/queue-item-transformer.ts +0 -38
- package/utils/sanitize-results.ts +0 -66
@@ -1,60 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.ResultSanitizer = void 0;
|
4
|
-
/**
|
5
|
-
* Utility class to sanitize JSON results for evaluation
|
6
|
-
*/
|
7
|
-
class ResultSanitizer {
|
8
|
-
/**
|
9
|
-
* Sanitizes JSON results by removing special characters and formatting
|
10
|
-
* @param results - The results to sanitize
|
11
|
-
* @returns Sanitized string
|
12
|
-
*/
|
13
|
-
static sanitize(results) {
|
14
|
-
if (!results)
|
15
|
-
return "";
|
16
|
-
try {
|
17
|
-
const jsonString = JSON.stringify(results);
|
18
|
-
return (jsonString
|
19
|
-
// Basic cleanup
|
20
|
-
.replace(/\\n/g, " ") // Remove newlines
|
21
|
-
.replace(/\s+/g, " ") // Remove extra spaces
|
22
|
-
.replace(/\\"/g, '"') // Fix escaped quotes
|
23
|
-
.replace(/\\+/g, "") // Remove extra backslashes
|
24
|
-
// Remove unnecessary quotes around objects and arrays
|
25
|
-
.replace(/"\[/g, "[") // Remove quotes around arrays start
|
26
|
-
.replace(/\]"/g, "]") // Remove quotes around arrays end
|
27
|
-
.replace(/"{/g, "{") // Remove quotes around objects start
|
28
|
-
.replace(/}"/g, "}") // Remove quotes around objects end
|
29
|
-
// Clean up numbers and values
|
30
|
-
.replace(/"(\d+\.?\d*)"/g, "$1") // Remove quotes around numbers
|
31
|
-
.replace(/:\s*"(true|false|null)"/g, ": $1") // Remove quotes around booleans and null
|
32
|
-
// Clean up URLs and content
|
33
|
-
.replace(/(?<=content":")([^"]+)(?=")/g, (match) => match.trim().replace(/\s+/g, " ") // Clean content spacing
|
34
|
-
)
|
35
|
-
.replace(/(?<=link":")([^"]+)(?=")/g, (match) => match.replace(/&/g, "&") // Fix URL encodings
|
36
|
-
)
|
37
|
-
// Final cleanup
|
38
|
-
.replace(/,\s*([}\]])/g, "$1") // Remove trailing commas
|
39
|
-
.replace(/:\s+/g, ":") // Remove spaces after colons
|
40
|
-
.replace(/,\s+/g, ",") // Remove spaces after commas
|
41
|
-
.trim()); // Remove leading/trailing whitespace
|
42
|
-
}
|
43
|
-
catch (error) {
|
44
|
-
console.error("Error sanitizing results:", error);
|
45
|
-
return String(results);
|
46
|
-
}
|
47
|
-
}
|
48
|
-
/**
|
49
|
-
* Formats numbers to a consistent format
|
50
|
-
* @param value - The number to format
|
51
|
-
* @returns Formatted number string
|
52
|
-
*/
|
53
|
-
static formatNumber(value) {
|
54
|
-
return value.toLocaleString("en-US", {
|
55
|
-
maximumFractionDigits: 2,
|
56
|
-
useGrouping: false,
|
57
|
-
});
|
58
|
-
}
|
59
|
-
}
|
60
|
-
exports.ResultSanitizer = ResultSanitizer;
|
@@ -1,103 +0,0 @@
|
|
1
|
-
import { createClient } from "redis";
|
2
|
-
import { BaseMemoryService } from "../../../interfaces";
|
3
|
-
import { BaseMemoryType } from "../../../types";
|
4
|
-
|
5
|
-
export class RedisAdapter implements BaseMemoryService {
|
6
|
-
private redis;
|
7
|
-
private readonly cachePrefix: string;
|
8
|
-
private readonly cacheTTL: number;
|
9
|
-
|
10
|
-
constructor(
|
11
|
-
private readonly redisUrl: string,
|
12
|
-
options: {
|
13
|
-
cachePrefix?: string;
|
14
|
-
cacheTTL?: number;
|
15
|
-
}
|
16
|
-
) {
|
17
|
-
this.cachePrefix = options.cachePrefix || "memory:";
|
18
|
-
this.cacheTTL = options.cacheTTL || 3600;
|
19
|
-
this.redis = createClient({
|
20
|
-
url: redisUrl,
|
21
|
-
socket: {
|
22
|
-
tls: true,
|
23
|
-
rejectUnauthorized: true,
|
24
|
-
},
|
25
|
-
});
|
26
|
-
}
|
27
|
-
|
28
|
-
async initializeConnection(): Promise<void> {
|
29
|
-
this.redis.on("error", (err) => console.error("Redis Client Error:", err));
|
30
|
-
await this.redis.connect();
|
31
|
-
}
|
32
|
-
|
33
|
-
async createMemory(memory: BaseMemoryType, ttl?: number): Promise<void> {
|
34
|
-
const key = memory.roomId
|
35
|
-
? `${this.cachePrefix}${memory.roomId}:${memory.id}`
|
36
|
-
: `${this.cachePrefix}${memory.id}`;
|
37
|
-
|
38
|
-
await this.redis.set(key, JSON.stringify(memory), {
|
39
|
-
EX: ttl || this.cacheTTL,
|
40
|
-
});
|
41
|
-
}
|
42
|
-
|
43
|
-
async getMemoryById(
|
44
|
-
id: string,
|
45
|
-
roomId?: string
|
46
|
-
): Promise<BaseMemoryType | null> {
|
47
|
-
const key = roomId
|
48
|
-
? `${this.cachePrefix}${roomId}:${id}`
|
49
|
-
: `${this.cachePrefix}${id}`;
|
50
|
-
|
51
|
-
const data = await this.redis.get(key);
|
52
|
-
return data ? JSON.parse(data) : null;
|
53
|
-
}
|
54
|
-
|
55
|
-
async getMemoryByIndex(
|
56
|
-
query: string,
|
57
|
-
options: {
|
58
|
-
roomId?: string;
|
59
|
-
limit?: number;
|
60
|
-
} = {}
|
61
|
-
): Promise<BaseMemoryType[]> {
|
62
|
-
const pattern = options.roomId
|
63
|
-
? `${this.cachePrefix}${options.roomId}:*`
|
64
|
-
: `${this.cachePrefix}*`;
|
65
|
-
|
66
|
-
const keys = await this.redis.keys(pattern);
|
67
|
-
const memories = await Promise.all(
|
68
|
-
keys.map(async (key) => {
|
69
|
-
const data = await this.redis.get(key);
|
70
|
-
return data ? JSON.parse(data) : null;
|
71
|
-
})
|
72
|
-
);
|
73
|
-
return memories.filter(Boolean).slice(0, options.limit || 10);
|
74
|
-
}
|
75
|
-
|
76
|
-
async getAllMemories(): Promise<BaseMemoryType[]> {
|
77
|
-
const keys = await this.redis.keys(`${this.cachePrefix}*`);
|
78
|
-
const memories = await Promise.all(
|
79
|
-
keys.map(async (key) => {
|
80
|
-
const data = await this.redis.get(key);
|
81
|
-
return data ? JSON.parse(data) : null;
|
82
|
-
})
|
83
|
-
);
|
84
|
-
return memories.filter(Boolean);
|
85
|
-
}
|
86
|
-
|
87
|
-
async clearMemoryById(id: string): Promise<void> {
|
88
|
-
await this.redis.del(`${this.cachePrefix}${id}`);
|
89
|
-
}
|
90
|
-
|
91
|
-
async clearAllMemories(): Promise<void> {
|
92
|
-
const keys = await this.redis.keys(`${this.cachePrefix}*`);
|
93
|
-
if (keys.length > 0) {
|
94
|
-
await this.redis.del(keys);
|
95
|
-
}
|
96
|
-
}
|
97
|
-
|
98
|
-
async quit(): Promise<void> {
|
99
|
-
if (this.redis) {
|
100
|
-
await this.redis.quit();
|
101
|
-
}
|
102
|
-
}
|
103
|
-
}
|
package/memory/index.ts
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
import { BaseMemoryService } from "../interfaces";
|
2
|
-
import { BaseMemoryType, CreateMemoryInput } from "../types";
|
3
|
-
|
4
|
-
export abstract class BaseMemory {
|
5
|
-
constructor(protected readonly cacheService: BaseMemoryService) {}
|
6
|
-
|
7
|
-
abstract init(): Promise<void>;
|
8
|
-
abstract createMemory(
|
9
|
-
input: CreateMemoryInput & { embedding?: number[] }
|
10
|
-
): Promise<BaseMemoryType | undefined>;
|
11
|
-
abstract getMemoryById(
|
12
|
-
id: string,
|
13
|
-
roomId: string
|
14
|
-
): Promise<BaseMemoryType | null>;
|
15
|
-
abstract getMemoryByIndex(
|
16
|
-
query: string,
|
17
|
-
options: { roomId: string; limit?: number }
|
18
|
-
): Promise<BaseMemoryType[]>;
|
19
|
-
abstract getAllMemories(roomId: string): Promise<BaseMemoryType[]>;
|
20
|
-
abstract clearMemoryById(id: string, roomId: string): Promise<void>;
|
21
|
-
abstract clearAllMemories(): Promise<void>;
|
22
|
-
}
|
package/services/agenda.ts
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
import { ScheduledRequest } from "../types";
|
2
|
-
import cron from "node-cron";
|
3
|
-
|
4
|
-
export class Agenda {
|
5
|
-
private scheduledRequests: Map<string, ScheduledRequest> = new Map();
|
6
|
-
private cronJobs: Map<string, cron.ScheduledTask> = new Map();
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Schedule a new request to be processed later
|
10
|
-
*/
|
11
|
-
async scheduleRequest(
|
12
|
-
request: {
|
13
|
-
originalRequest: string;
|
14
|
-
cronExpression: string;
|
15
|
-
},
|
16
|
-
callbacks?: {
|
17
|
-
onScheduled?: (id: string) => void;
|
18
|
-
onExecuted?: (id: string, originalRequest: string) => void;
|
19
|
-
}
|
20
|
-
): Promise<string> {
|
21
|
-
const id = crypto.randomUUID();
|
22
|
-
|
23
|
-
const scheduledRequest: ScheduledRequest = {
|
24
|
-
id,
|
25
|
-
originalRequest: request.originalRequest,
|
26
|
-
cronExpression: request.cronExpression,
|
27
|
-
isRecurring: false,
|
28
|
-
createdAt: new Date(),
|
29
|
-
};
|
30
|
-
|
31
|
-
// Create cron job
|
32
|
-
const cronJob = cron.schedule(request.cronExpression, () => {
|
33
|
-
console.log(`🔄 Executing scheduled request: ${id}`);
|
34
|
-
|
35
|
-
if (callbacks?.onExecuted) {
|
36
|
-
callbacks.onExecuted(id, scheduledRequest.originalRequest);
|
37
|
-
}
|
38
|
-
|
39
|
-
console.log(`✅ Scheduled request executed successfully: ${id}`);
|
40
|
-
|
41
|
-
// Auto-stop pour les tâches non récurrentes
|
42
|
-
if (!scheduledRequest.isRecurring) {
|
43
|
-
this.cancelScheduledRequest(id);
|
44
|
-
}
|
45
|
-
});
|
46
|
-
|
47
|
-
// Démarrer le job en mode non-running
|
48
|
-
cronJob.stop();
|
49
|
-
|
50
|
-
// Store request and job
|
51
|
-
this.scheduledRequests.set(id, scheduledRequest);
|
52
|
-
this.cronJobs.set(id, cronJob);
|
53
|
-
|
54
|
-
if (callbacks?.onScheduled) callbacks.onScheduled(id);
|
55
|
-
|
56
|
-
// Démarrer le job après l'avoir stocké
|
57
|
-
cronJob.start();
|
58
|
-
|
59
|
-
return id;
|
60
|
-
}
|
61
|
-
|
62
|
-
/**
|
63
|
-
* Cancel a scheduled request
|
64
|
-
*/
|
65
|
-
cancelScheduledRequest(requestId: string): boolean {
|
66
|
-
const cronJob = this.cronJobs.get(requestId);
|
67
|
-
if (cronJob) {
|
68
|
-
try {
|
69
|
-
cronJob.stop();
|
70
|
-
this.cronJobs.delete(requestId);
|
71
|
-
this.scheduledRequests.delete(requestId);
|
72
|
-
return true;
|
73
|
-
} catch (error) {
|
74
|
-
console.error(`Failed to stop cron job ${requestId}:`, error);
|
75
|
-
return false;
|
76
|
-
}
|
77
|
-
}
|
78
|
-
return false;
|
79
|
-
}
|
80
|
-
|
81
|
-
/**
|
82
|
-
* Get all scheduled requests
|
83
|
-
*/
|
84
|
-
getScheduledRequests(): ScheduledRequest[] {
|
85
|
-
return Array.from(this.scheduledRequests.values());
|
86
|
-
}
|
87
|
-
|
88
|
-
/**
|
89
|
-
* Stop all cron jobs
|
90
|
-
*/
|
91
|
-
stopAll(): void {
|
92
|
-
const ids = Array.from(this.cronJobs.keys());
|
93
|
-
|
94
|
-
// Arrêter tous les jobs de manière synchrone
|
95
|
-
for (const id of ids) {
|
96
|
-
const job = this.cronJobs.get(id);
|
97
|
-
if (job) {
|
98
|
-
job.stop();
|
99
|
-
this.cronJobs.delete(id);
|
100
|
-
this.scheduledRequests.delete(id);
|
101
|
-
}
|
102
|
-
}
|
103
|
-
|
104
|
-
// Double vérification
|
105
|
-
this.cronJobs.clear();
|
106
|
-
this.scheduledRequests.clear();
|
107
|
-
}
|
108
|
-
|
109
|
-
public async stop(): Promise<void> {
|
110
|
-
this.stopAll();
|
111
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
112
|
-
}
|
113
|
-
|
114
|
-
public async cancel(query: {}): Promise<void> {
|
115
|
-
this.stopAll();
|
116
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
117
|
-
}
|
118
|
-
}
|
package/services/embedding.ts
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
import { EmbeddingService } from "../interfaces";
|
2
|
-
import { cosineSimilarity, embed, EmbeddingModel, embedMany } from "ai";
|
3
|
-
|
4
|
-
export class AIEmbeddingService implements EmbeddingService {
|
5
|
-
constructor(private readonly model: EmbeddingModel<string>) {}
|
6
|
-
|
7
|
-
async embedText(text: string): Promise<number[]> {
|
8
|
-
const { embedding } = await embed({
|
9
|
-
model: this.model,
|
10
|
-
value: text,
|
11
|
-
});
|
12
|
-
return embedding;
|
13
|
-
}
|
14
|
-
|
15
|
-
async embedMany(texts: string[]): Promise<number[][]> {
|
16
|
-
const { embeddings } = await embedMany({
|
17
|
-
model: this.model,
|
18
|
-
values: texts,
|
19
|
-
});
|
20
|
-
return embeddings;
|
21
|
-
}
|
22
|
-
|
23
|
-
calculateSimilarity(embedding1: number[], embedding2: number[]): number {
|
24
|
-
return (cosineSimilarity(embedding1, embedding2) + 1) * 50;
|
25
|
-
}
|
26
|
-
}
|
package/services/queue.ts
DELETED
@@ -1,145 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
ActionSchema,
|
3
|
-
QueueCallbacks,
|
4
|
-
QueueItem,
|
5
|
-
QueueItemParameter,
|
6
|
-
QueueResult,
|
7
|
-
} from "../types";
|
8
|
-
|
9
|
-
export class Queue {
|
10
|
-
private queue: QueueItem[] = [];
|
11
|
-
private results: QueueResult[] = [];
|
12
|
-
private callbacks: QueueCallbacks;
|
13
|
-
private actions: ActionSchema[];
|
14
|
-
private isProcessing: boolean = false;
|
15
|
-
|
16
|
-
constructor(actions: ActionSchema[], callbacks: QueueCallbacks = {}) {
|
17
|
-
this.actions = actions;
|
18
|
-
this.callbacks = callbacks;
|
19
|
-
}
|
20
|
-
|
21
|
-
add(actions: QueueItem | QueueItem[]) {
|
22
|
-
if (Array.isArray(actions)) {
|
23
|
-
this.queue.push(...actions);
|
24
|
-
} else {
|
25
|
-
this.queue.push(actions);
|
26
|
-
}
|
27
|
-
}
|
28
|
-
|
29
|
-
async execute() {
|
30
|
-
if (this.isProcessing) {
|
31
|
-
return;
|
32
|
-
}
|
33
|
-
|
34
|
-
this.isProcessing = true;
|
35
|
-
const actionPromises: Promise<QueueResult>[] = [];
|
36
|
-
|
37
|
-
for (const action of this.queue) {
|
38
|
-
const actionConfig = this.actions.find((a) => a.name === action.name);
|
39
|
-
if (actionConfig?.confirmation?.requireConfirmation) {
|
40
|
-
const shouldProceed = await this.callbacks.onConfirmationRequired?.(
|
41
|
-
actionConfig.confirmation.message ||
|
42
|
-
`Do you want to proceed with action: ${action.name}?`
|
43
|
-
);
|
44
|
-
|
45
|
-
if (!shouldProceed) {
|
46
|
-
this.results.push({
|
47
|
-
name: action.name,
|
48
|
-
parameters: this.formatArguments(action.parameters),
|
49
|
-
result: null,
|
50
|
-
error: "Action cancelled by user",
|
51
|
-
cancelled: true,
|
52
|
-
});
|
53
|
-
continue;
|
54
|
-
}
|
55
|
-
}
|
56
|
-
const parameters = this.formatArguments(action.parameters);
|
57
|
-
|
58
|
-
actionPromises.push(
|
59
|
-
this.executeAction(action)
|
60
|
-
.then((result) => {
|
61
|
-
this.callbacks.onActionComplete?.(result);
|
62
|
-
return result;
|
63
|
-
})
|
64
|
-
.catch((error) => {
|
65
|
-
const result = {
|
66
|
-
name: action.name,
|
67
|
-
parameters,
|
68
|
-
result: null,
|
69
|
-
error: error.message || "Unknown error occurred",
|
70
|
-
};
|
71
|
-
this.callbacks.onActionComplete?.(result);
|
72
|
-
return result;
|
73
|
-
})
|
74
|
-
);
|
75
|
-
}
|
76
|
-
|
77
|
-
try {
|
78
|
-
const results = await Promise.all(actionPromises);
|
79
|
-
this.results.push(...results);
|
80
|
-
this.queue = [];
|
81
|
-
this.callbacks.onQueueComplete?.(this.results);
|
82
|
-
this.isProcessing = false;
|
83
|
-
return this.results;
|
84
|
-
} catch (error) {
|
85
|
-
this.isProcessing = false;
|
86
|
-
throw error;
|
87
|
-
}
|
88
|
-
}
|
89
|
-
|
90
|
-
private formatArguments(args: QueueItemParameter[]): Record<string, string> {
|
91
|
-
return args.reduce<Record<string, string>>((acc, arg) => {
|
92
|
-
try {
|
93
|
-
// Parse the JSON string if the value is a stringified JSON object
|
94
|
-
const parsedValue = JSON.parse(arg.value);
|
95
|
-
if (
|
96
|
-
parsedValue &&
|
97
|
-
typeof parsedValue === "object" &&
|
98
|
-
"value" in parsedValue
|
99
|
-
) {
|
100
|
-
acc[parsedValue.name] = parsedValue.value;
|
101
|
-
} else {
|
102
|
-
// Fallback to original value if not in expected format
|
103
|
-
acc[arg.name] = arg.value;
|
104
|
-
}
|
105
|
-
} catch {
|
106
|
-
// If JSON parsing fails, use the original value
|
107
|
-
acc[arg.name] = arg.value;
|
108
|
-
}
|
109
|
-
return acc;
|
110
|
-
}, {});
|
111
|
-
}
|
112
|
-
|
113
|
-
private async executeAction(action: QueueItem): Promise<QueueResult> {
|
114
|
-
this.callbacks.onActionStart?.(action);
|
115
|
-
|
116
|
-
const actionConfig = this.actions.find((a) => a.name === action.name);
|
117
|
-
if (!actionConfig) {
|
118
|
-
return {
|
119
|
-
name: action.name,
|
120
|
-
parameters: {},
|
121
|
-
result: null,
|
122
|
-
error: `Action '${action.name}' not found in actions list`,
|
123
|
-
};
|
124
|
-
}
|
125
|
-
|
126
|
-
const actionArgs = this.formatArguments(action.parameters);
|
127
|
-
|
128
|
-
try {
|
129
|
-
const result = await actionConfig.execute(actionArgs);
|
130
|
-
return {
|
131
|
-
name: action.name,
|
132
|
-
parameters: actionArgs,
|
133
|
-
result,
|
134
|
-
error: null,
|
135
|
-
};
|
136
|
-
} catch (error) {
|
137
|
-
return {
|
138
|
-
name: action.name,
|
139
|
-
parameters: actionArgs,
|
140
|
-
result: null,
|
141
|
-
error: (error as Error).message || "Unknown error occurred",
|
142
|
-
};
|
143
|
-
}
|
144
|
-
}
|
145
|
-
}
|
@@ -1,159 +0,0 @@
|
|
1
|
-
import { BaseMemoryService } from "../../../interfaces";
|
2
|
-
import { RedisAdapter } from "../../../memory/adapters/redis";
|
3
|
-
import { BaseMemoryType } from "../../../types";
|
4
|
-
import { expect } from "chai";
|
5
|
-
import dotenv from "dotenv";
|
6
|
-
import Redis from "ioredis";
|
7
|
-
|
8
|
-
// Load environment variables
|
9
|
-
dotenv.config();
|
10
|
-
|
11
|
-
describe("RedisAdapter", () => {
|
12
|
-
before(function () {
|
13
|
-
this.timeout(15000);
|
14
|
-
});
|
15
|
-
|
16
|
-
let redisAdapter: RedisAdapter;
|
17
|
-
let mockBaseMemoryService: BaseMemoryService;
|
18
|
-
let redisClient: Redis | null = null;
|
19
|
-
const fixedDate = new Date("2025-01-30T07:43:50.626Z");
|
20
|
-
|
21
|
-
const testMemory: BaseMemoryType = {
|
22
|
-
id: "test-id",
|
23
|
-
data: "test data",
|
24
|
-
embedding: [0.1, 0.2, 0.3],
|
25
|
-
roomId: "test-room",
|
26
|
-
createdAt: fixedDate,
|
27
|
-
};
|
28
|
-
|
29
|
-
beforeEach(async () => {
|
30
|
-
mockBaseMemoryService = {
|
31
|
-
initializeConnection: async () => {},
|
32
|
-
createMemory: async () => {},
|
33
|
-
getMemoryById: async () => testMemory,
|
34
|
-
getMemoryByIndex: async () => [testMemory],
|
35
|
-
getAllMemories: async () => [testMemory],
|
36
|
-
clearMemoryById: async () => {},
|
37
|
-
clearAllMemories: async () => {},
|
38
|
-
};
|
39
|
-
|
40
|
-
// Use real Redis if environment variables are set, otherwise mock
|
41
|
-
if (process.env.REDIS_URL) {
|
42
|
-
redisClient = new Redis(process.env.REDIS_URL);
|
43
|
-
redisAdapter = new RedisAdapter(process.env.REDIS_URL, {
|
44
|
-
cachePrefix: "test-prefix",
|
45
|
-
cacheTTL: 3600,
|
46
|
-
});
|
47
|
-
} else {
|
48
|
-
// Mock Redis implementation
|
49
|
-
const mockRedis = {
|
50
|
-
connect: async () => {},
|
51
|
-
disconnect: async () => {},
|
52
|
-
set: async (key: string, value: string) => "OK",
|
53
|
-
get: async (key: string) => {
|
54
|
-
if (key.includes("test-id")) {
|
55
|
-
return JSON.stringify({
|
56
|
-
...testMemory,
|
57
|
-
createdAt: fixedDate.toISOString(),
|
58
|
-
});
|
59
|
-
}
|
60
|
-
return null;
|
61
|
-
},
|
62
|
-
keys: async (pattern: string) => {
|
63
|
-
return [`${pattern}test-id`];
|
64
|
-
},
|
65
|
-
mget: async (keys: string[]) => {
|
66
|
-
return keys.map(() =>
|
67
|
-
JSON.stringify({
|
68
|
-
...testMemory,
|
69
|
-
createdAt: fixedDate.toISOString(),
|
70
|
-
})
|
71
|
-
);
|
72
|
-
},
|
73
|
-
del: async () => 1,
|
74
|
-
flushall: async () => "OK",
|
75
|
-
quit: async () => {},
|
76
|
-
};
|
77
|
-
|
78
|
-
redisAdapter = new RedisAdapter(mockRedis as any, {
|
79
|
-
cachePrefix: "test-prefix",
|
80
|
-
cacheTTL: 3600,
|
81
|
-
});
|
82
|
-
}
|
83
|
-
|
84
|
-
await redisAdapter.initializeConnection();
|
85
|
-
});
|
86
|
-
|
87
|
-
afterEach(async () => {
|
88
|
-
if (redisClient) {
|
89
|
-
await redisClient.quit();
|
90
|
-
redisClient = null;
|
91
|
-
}
|
92
|
-
// @ts-ignore pour éviter l'erreur de typage
|
93
|
-
await redisAdapter?.quit?.();
|
94
|
-
});
|
95
|
-
|
96
|
-
describe("Initialization", () => {
|
97
|
-
it("should initialize storage", async () => {
|
98
|
-
await expect(redisAdapter.initializeConnection()).to.not.throw;
|
99
|
-
});
|
100
|
-
});
|
101
|
-
|
102
|
-
describe("Memory Operations", () => {
|
103
|
-
const TEST_ROOM_ID = "test-room";
|
104
|
-
|
105
|
-
it("should create memory", async () => {
|
106
|
-
await expect(
|
107
|
-
redisAdapter.createMemory({
|
108
|
-
data: "test data",
|
109
|
-
query: "test query",
|
110
|
-
roomId: TEST_ROOM_ID,
|
111
|
-
id: "test-id",
|
112
|
-
embedding: [0.1, 0.2, 0.3],
|
113
|
-
createdAt: fixedDate,
|
114
|
-
})
|
115
|
-
).to.not.throw;
|
116
|
-
});
|
117
|
-
|
118
|
-
it("should get memory by ID", async () => {
|
119
|
-
const result = await redisAdapter.getMemoryById("test-id", TEST_ROOM_ID);
|
120
|
-
if (result) {
|
121
|
-
result.createdAt = new Date(result.createdAt);
|
122
|
-
}
|
123
|
-
expect(result).to.deep.equal({
|
124
|
-
...testMemory,
|
125
|
-
createdAt: testMemory.createdAt,
|
126
|
-
});
|
127
|
-
});
|
128
|
-
|
129
|
-
it("should get memories by index", async () => {
|
130
|
-
const results = await redisAdapter.getMemoryByIndex("test", {
|
131
|
-
roomId: TEST_ROOM_ID,
|
132
|
-
limit: 10,
|
133
|
-
});
|
134
|
-
|
135
|
-
expect(results).to.be.an("array");
|
136
|
-
if (results[0]) {
|
137
|
-
results[0].createdAt = new Date(results[0].createdAt);
|
138
|
-
}
|
139
|
-
expect(results[0]).to.deep.equal(testMemory);
|
140
|
-
});
|
141
|
-
|
142
|
-
it("should get all memories", async () => {
|
143
|
-
const results = await redisAdapter.getAllMemories();
|
144
|
-
expect(results).to.be.an("array");
|
145
|
-
if (results[0]) {
|
146
|
-
results[0].createdAt = new Date(results[0].createdAt);
|
147
|
-
}
|
148
|
-
expect(results[0]).to.deep.equal(testMemory);
|
149
|
-
});
|
150
|
-
|
151
|
-
it("should clear memory by ID", async () => {
|
152
|
-
await expect(redisAdapter.clearMemoryById("test-id")).to.not.throw;
|
153
|
-
});
|
154
|
-
|
155
|
-
it("should clear all memories", async () => {
|
156
|
-
await expect(redisAdapter.clearAllMemories()).to.not.throw;
|
157
|
-
});
|
158
|
-
});
|
159
|
-
});
|