File size: 2,697 Bytes
0ae3f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
 * Abstract backend interface and factory.
 */

import type { Mem0Config } from "../config.js";
import { PlatformBackend } from "./platform.js";

export interface AddOptions {
	userId?: string;
	agentId?: string;
	appId?: string;
	runId?: string;
	metadata?: Record<string, unknown>;
	immutable?: boolean;
	infer?: boolean;
	expires?: string;
	categories?: string[];
	enableGraph?: boolean;
}

export interface SearchOptions {
	userId?: string;
	agentId?: string;
	appId?: string;
	runId?: string;
	topK?: number;
	threshold?: number;
	rerank?: boolean;
	keyword?: boolean;
	filters?: Record<string, unknown>;
	fields?: string[];
	enableGraph?: boolean;
}

export interface ListOptions {
	userId?: string;
	agentId?: string;
	appId?: string;
	runId?: string;
	page?: number;
	pageSize?: number;
	category?: string;
	after?: string;
	before?: string;
	enableGraph?: boolean;
}

export interface DeleteOptions {
	all?: boolean;
	userId?: string;
	agentId?: string;
	appId?: string;
	runId?: string;
}

export interface EntityIds {
	userId?: string;
	agentId?: string;
	appId?: string;
	runId?: string;
}

export interface Backend {
	add(
		content?: string,
		messages?: Record<string, unknown>[],
		opts?: AddOptions,
	): Promise<Record<string, unknown>>;

	search(
		query: string,
		opts?: SearchOptions,
	): Promise<Record<string, unknown>[]>;

	get(memoryId: string): Promise<Record<string, unknown>>;

	listMemories(opts?: ListOptions): Promise<Record<string, unknown>[]>;

	update(
		memoryId: string,
		content?: string,
		metadata?: Record<string, unknown>,
	): Promise<Record<string, unknown>>;

	delete(
		memoryId?: string,
		opts?: DeleteOptions,
	): Promise<Record<string, unknown>>;

	deleteEntities(opts: EntityIds): Promise<Record<string, unknown>>;

	ping(): Promise<Record<string, unknown>>;

	status(opts?: { userId?: string; agentId?: string }): Promise<
		Record<string, unknown>
	>;

	entities(entityType: string): Promise<Record<string, unknown>[]>;

	listEvents(): Promise<Record<string, unknown>[]>;

	getEvent(eventId: string): Promise<Record<string, unknown>>;
}

export class AuthError extends Error {
	constructor(
		message = "Authentication failed. Your API key may be invalid or expired.",
	) {
		super(message);
		this.name = "AuthError";
	}
}

export class NotFoundError extends Error {
	constructor(path: string) {
		super(`Resource not found: ${path}`);
		this.name = "NotFoundError";
	}
}

export class APIError extends Error {
	constructor(path: string, detail: string) {
		super(`Bad request to ${path}: ${detail}`);
		this.name = "APIError";
	}
}

export function getBackend(config: Mem0Config): Backend {
	return new PlatformBackend(config.platform);
}