@getzep/zep-cloud 2.5.0 → 2.6.0
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/api/resources/document/client/Client.js +13 -13
- package/api/resources/graph/client/Client.js +3 -3
- package/api/resources/graph/resources/edge/client/Client.js +4 -4
- package/api/resources/graph/resources/episode/client/Client.js +4 -4
- package/api/resources/graph/resources/node/client/Client.js +3 -3
- package/api/resources/group/client/Client.js +6 -6
- package/api/resources/memory/client/Client.js +22 -22
- package/api/resources/memory/client/requests/AddMemoryRequest.d.ts +6 -0
- package/api/resources/user/client/Client.js +8 -8
- package/dist/api/resources/document/client/Client.js +13 -13
- package/dist/api/resources/graph/client/Client.js +3 -3
- package/dist/api/resources/graph/resources/edge/client/Client.js +4 -4
- package/dist/api/resources/graph/resources/episode/client/Client.js +4 -4
- package/dist/api/resources/graph/resources/node/client/Client.js +3 -3
- package/dist/api/resources/group/client/Client.js +6 -6
- package/dist/api/resources/memory/client/Client.js +22 -22
- package/dist/api/resources/memory/client/requests/AddMemoryRequest.d.ts +6 -0
- package/dist/api/resources/user/client/Client.js +8 -8
- package/dist/serialization/resources/memory/client/requests/AddMemoryRequest.d.ts +2 -0
- package/dist/serialization/resources/memory/client/requests/AddMemoryRequest.js +2 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/examples/langgraph/.env.example +7 -0
- package/examples/langgraph/README.md +165 -0
- package/examples/langgraph/agent.ts +261 -0
- package/examples/langgraph/package-lock.json +1697 -0
- package/examples/langgraph/package.json +20 -0
- package/examples/langgraph/zep-memory.ts +271 -0
- package/package.json +1 -1
- package/serialization/resources/memory/client/requests/AddMemoryRequest.d.ts +2 -0
- package/serialization/resources/memory/client/requests/AddMemoryRequest.js +2 -0
- package/version.d.ts +1 -1
- package/version.js +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "agent",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple agent built with LangGraph.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "tsx agent.ts"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@getzep/zep-cloud": "^2.5.0",
|
|
11
|
+
"@langchain/core": "^0.3.40",
|
|
12
|
+
"@langchain/langgraph": "^0.2.51",
|
|
13
|
+
"@langchain/openai": "^0.4.4",
|
|
14
|
+
"commander": "^12.0.0",
|
|
15
|
+
"dotenv": "^16.4.1",
|
|
16
|
+
"tsx": "^4.7.0",
|
|
17
|
+
"typescript": "^5.3.3",
|
|
18
|
+
"uuid": "^9.0.1"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// zep-memory.ts
|
|
2
|
+
import { ZepClient } from "@getzep/zep-cloud";
|
|
3
|
+
import { RoleType } from "@getzep/zep-cloud/dist/api";
|
|
4
|
+
import { BaseMessage, AIMessage, HumanMessage, SystemMessage } from "@langchain/core/messages";
|
|
5
|
+
import { v4 as uuidv4 } from "uuid";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* ZepMemory adapter for LangGraph
|
|
9
|
+
* This class provides memory persistence for LangGraph using Zep
|
|
10
|
+
*/
|
|
11
|
+
export class ZepMemory {
|
|
12
|
+
private client: ZepClient;
|
|
13
|
+
private sessionId: string;
|
|
14
|
+
private initialized: boolean = false;
|
|
15
|
+
private userId?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create a new ZepMemory instance
|
|
19
|
+
* @param apiKey - Zep API key
|
|
20
|
+
* @param sessionId - Optional session ID, will generate a new one if not provided
|
|
21
|
+
* @param userId - Optional user ID to associate with the session
|
|
22
|
+
*/
|
|
23
|
+
constructor(apiKey: string, sessionId?: string, userId?: string) {
|
|
24
|
+
this.client = new ZepClient({
|
|
25
|
+
apiKey,
|
|
26
|
+
});
|
|
27
|
+
this.sessionId = sessionId || uuidv4();
|
|
28
|
+
this.userId = userId;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Initialize the memory session
|
|
33
|
+
* @param userId - Optional user ID to associate with the session
|
|
34
|
+
*/
|
|
35
|
+
async initialize(userId?: string): Promise<void> {
|
|
36
|
+
if (this.initialized) return;
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
// Use provided userId or the one from constructor or generate a new one
|
|
40
|
+
const userIdToUse = userId || this.userId || `user-${uuidv4()}`;
|
|
41
|
+
this.userId = userIdToUse;
|
|
42
|
+
|
|
43
|
+
// Check if user exists, create if not
|
|
44
|
+
let userExists = false;
|
|
45
|
+
try {
|
|
46
|
+
console.log("userIdToUse", userIdToUse);
|
|
47
|
+
await this.client.user.get(userIdToUse);
|
|
48
|
+
userExists = true;
|
|
49
|
+
console.log(`Using existing user: ${userIdToUse}`);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.log(error.constructor.name);
|
|
52
|
+
if (error.constructor.name === "NotFoundError") {
|
|
53
|
+
// User doesn't exist, we'll create it
|
|
54
|
+
console.log(`User ${userIdToUse} not found, will create`);
|
|
55
|
+
} else {
|
|
56
|
+
// For other errors, log and rethrow
|
|
57
|
+
console.error(`Error checking if user exists: ${userIdToUse}:`, error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Create user if it doesn't exist
|
|
63
|
+
if (!userExists) {
|
|
64
|
+
try {
|
|
65
|
+
await this.client.user.add({
|
|
66
|
+
userId: userIdToUse,
|
|
67
|
+
firstName: 'Sarah',
|
|
68
|
+
lastName: 'Smith',
|
|
69
|
+
email: `${userIdToUse}@example.com`, // Placeholder email
|
|
70
|
+
});
|
|
71
|
+
console.log(`Created new user: ${userIdToUse}`);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.error(`Failed to create user ${userIdToUse}:`, error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Check if session exists
|
|
79
|
+
let sessionExists = false;
|
|
80
|
+
try {
|
|
81
|
+
await this.client.memory.getSession(this.sessionId);
|
|
82
|
+
sessionExists = true;
|
|
83
|
+
console.log(`Using existing session: ${this.sessionId}`);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error.constructor.name === "NotFoundError") {
|
|
86
|
+
// Session doesn't exist, we'll create it
|
|
87
|
+
console.log(`Session ${this.sessionId} not found, will create`);
|
|
88
|
+
} else {
|
|
89
|
+
// For other errors, log and rethrow
|
|
90
|
+
console.error(`Error checking if session exists ${this.sessionId}:`, error);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Create session if it doesn't exist
|
|
96
|
+
if (!sessionExists) {
|
|
97
|
+
try {
|
|
98
|
+
await this.client.memory.addSession({
|
|
99
|
+
sessionId: this.sessionId,
|
|
100
|
+
userId: userIdToUse,
|
|
101
|
+
});
|
|
102
|
+
console.log(`Created new session: ${this.sessionId}`);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.error(`Failed to create session ${this.sessionId}:`, error);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
this.initialized = true;
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error("Failed to initialize Zep memory:", error);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Add a message to memory
|
|
118
|
+
* @param message - LangChain message to add
|
|
119
|
+
* @param withContext - Whether to return the Zep context string from memory
|
|
120
|
+
*/
|
|
121
|
+
async addMessage(message: BaseMessage, withContext: boolean = false): Promise<string | undefined> {
|
|
122
|
+
if (!this.initialized) {
|
|
123
|
+
throw new Error("Memory not initialized");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
// Convert LangChain message to Zep message format
|
|
128
|
+
const zepMessage = this.convertToZepMessage(message);
|
|
129
|
+
|
|
130
|
+
// Add message to Zep memory
|
|
131
|
+
const response = await this.client.memory.add(this.sessionId, {
|
|
132
|
+
messages: [zepMessage],
|
|
133
|
+
returnContext: withContext
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return response.context;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error("Failed to add message to Zep memory:", error);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Add multiple messages to memory
|
|
145
|
+
* @param messages - Array of LangChain messages to add
|
|
146
|
+
*/
|
|
147
|
+
async addMessages(messages: BaseMessage[]): Promise<void> {
|
|
148
|
+
if (!this.initialized) {
|
|
149
|
+
throw new Error("Memory not initialized");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
// Convert LangChain messages to Zep message format
|
|
154
|
+
const zepMessages = messages.map(msg => this.convertToZepMessage(msg));
|
|
155
|
+
|
|
156
|
+
// Add messages to Zep memory
|
|
157
|
+
await this.client.memory.add(this.sessionId, {
|
|
158
|
+
messages: zepMessages,
|
|
159
|
+
});
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.error("Failed to add messages to Zep memory:", error);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get messages from memory
|
|
168
|
+
* @param limit - Maximum number of messages to retrieve
|
|
169
|
+
*/
|
|
170
|
+
async getMessages(limit: number = 10): Promise<BaseMessage[]> {
|
|
171
|
+
if (!this.initialized) {
|
|
172
|
+
throw new Error("Memory not initialized");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const response = await this.client.memory.getSessionMessages(this.sessionId, {
|
|
177
|
+
limit,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Convert Zep messages to LangChain messages
|
|
181
|
+
return (response.messages || []).map(msg => this.convertToLangChainMessage(msg));
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error("Failed to get messages from Zep memory:", error);
|
|
184
|
+
throw error;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get memory with context for the current session
|
|
190
|
+
* This retrieves messages along with any context
|
|
191
|
+
*/
|
|
192
|
+
async getMemoryWithContext(): Promise<{ messages: BaseMessage[], context?: string }> {
|
|
193
|
+
if (!this.initialized) {
|
|
194
|
+
throw new Error("Memory not initialized");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
const memory = await this.client.memory.get(this.sessionId);
|
|
199
|
+
|
|
200
|
+
// Convert messages to LangChain format
|
|
201
|
+
const messages = (memory.messages || []).map(msg => this.convertToLangChainMessage(msg));
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
messages,
|
|
205
|
+
context: memory.context,
|
|
206
|
+
};
|
|
207
|
+
} catch (error) {
|
|
208
|
+
console.error("Failed to get memory with context:", error);
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Get the session ID
|
|
215
|
+
*/
|
|
216
|
+
getSessionId(): string {
|
|
217
|
+
return this.sessionId;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Get the user ID
|
|
222
|
+
*/
|
|
223
|
+
getUserId(): string | undefined {
|
|
224
|
+
return this.userId;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Convert a LangChain message to a Zep message
|
|
229
|
+
* @param message - LangChain message to convert
|
|
230
|
+
*/
|
|
231
|
+
private convertToZepMessage(message: BaseMessage) {
|
|
232
|
+
let roleType: RoleType;
|
|
233
|
+
let role = "";
|
|
234
|
+
|
|
235
|
+
if (message instanceof AIMessage) {
|
|
236
|
+
roleType = "assistant" as RoleType;
|
|
237
|
+
} else if (message instanceof HumanMessage) {
|
|
238
|
+
roleType = "user" as RoleType;
|
|
239
|
+
} else if (message instanceof SystemMessage) {
|
|
240
|
+
roleType = "system" as RoleType;
|
|
241
|
+
} else {
|
|
242
|
+
// Handle other message types (FunctionMessage, ToolMessage, etc.)
|
|
243
|
+
roleType = "function" as RoleType;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
content: message.content as string,
|
|
248
|
+
roleType,
|
|
249
|
+
role,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Convert a Zep message to a LangChain message
|
|
255
|
+
* @param message - Zep message to convert
|
|
256
|
+
*/
|
|
257
|
+
private convertToLangChainMessage(message: any): BaseMessage {
|
|
258
|
+
const { content, roleType } = message;
|
|
259
|
+
|
|
260
|
+
if (roleType === "assistant") {
|
|
261
|
+
return new AIMessage(content);
|
|
262
|
+
} else if (roleType === "user") {
|
|
263
|
+
return new HumanMessage(content);
|
|
264
|
+
} else if (roleType === "system") {
|
|
265
|
+
return new SystemMessage(content);
|
|
266
|
+
} else {
|
|
267
|
+
// Default to HumanMessage for other types
|
|
268
|
+
return new HumanMessage(content);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
package/package.json
CHANGED
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
import * as serializers from "../../../../index";
|
|
5
5
|
import * as Zep from "../../../../../api/index";
|
|
6
6
|
import * as core from "../../../../../core";
|
|
7
|
+
import { RoleType } from "../../../../types/RoleType";
|
|
7
8
|
import { Message } from "../../../../types/Message";
|
|
8
9
|
export declare const AddMemoryRequest: core.serialization.Schema<serializers.AddMemoryRequest.Raw, Zep.AddMemoryRequest>;
|
|
9
10
|
export declare namespace AddMemoryRequest {
|
|
10
11
|
interface Raw {
|
|
11
12
|
fact_instruction?: string | null;
|
|
13
|
+
ignore_roles?: RoleType.Raw[] | null;
|
|
12
14
|
messages: Message.Raw[];
|
|
13
15
|
return_context?: boolean | null;
|
|
14
16
|
summary_instruction?: string | null;
|
|
@@ -38,9 +38,11 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.AddMemoryRequest = void 0;
|
|
40
40
|
const core = __importStar(require("../../../../../core"));
|
|
41
|
+
const RoleType_1 = require("../../../../types/RoleType");
|
|
41
42
|
const Message_1 = require("../../../../types/Message");
|
|
42
43
|
exports.AddMemoryRequest = core.serialization.object({
|
|
43
44
|
factInstruction: core.serialization.property("fact_instruction", core.serialization.string().optional()),
|
|
45
|
+
ignoreRoles: core.serialization.property("ignore_roles", core.serialization.list(RoleType_1.RoleType).optional()),
|
|
44
46
|
messages: core.serialization.list(Message_1.Message),
|
|
45
47
|
returnContext: core.serialization.property("return_context", core.serialization.boolean().optional()),
|
|
46
48
|
summaryInstruction: core.serialization.property("summary_instruction", core.serialization.string().optional()),
|
package/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.
|
|
1
|
+
export declare const SDK_VERSION = "2.6.0";
|
package/version.js
CHANGED