refactor: fix webhook create/update serialization, add payload types, and MEMORY_CATEGORIZED event (#4429)

Co-authored-by: Saket Aryan <saketaryan2002@gmail.com>
This commit is contained in:
Kartik
2026-03-19 22:26:17 +05:30
committed by GitHub
co-authored by Saket Aryan
parent 88abb29de9
commit 6663b738d5
11 changed files with 362 additions and 128 deletions
+23 -10
View File
@@ -51,43 +51,56 @@ export async function withRetry<T>(
* Poll getAll until memories appear for a user.
* The Mem0 API processes memories asynchronously — after add()
* we need to wait for them to be available.
*
* Polls every 15 seconds with a maximum of 4 retries to avoid
* hitting rate limits. Throws if results aren't available after
* all retries.
*/
export async function waitForMemories(
client: MemoryClient,
userId: string,
minCount: number,
maxWaitMs = 60_000,
maxRetries = 4,
): Promise<Memory[]> {
const start = Date.now();
while (Date.now() - start < maxWaitMs) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
const memories = await withRetry(() => client.getAll({ user_id: userId }));
if (Array.isArray(memories) && memories.length >= minCount) {
return memories;
}
await new Promise((r) => setTimeout(r, 3_000));
if (attempt < maxRetries) {
await new Promise((r) => setTimeout(r, 15_000));
}
}
return await client.getAll({ user_id: userId });
throw new Error(
`waitForMemories: expected at least ${minCount} memories for user "${userId}" but did not get them after ${maxRetries} attempts`,
);
}
/**
* Poll search until results appear. Only used by search tests —
* other test files should NOT call this to avoid wasting API credits.
*
* Polls every 15 seconds with a maximum of 4 retries. Throws if
* no results are found after all retries.
*/
export async function waitForSearchResults(
client: MemoryClient,
query: string,
options: Record<string, any>,
maxWaitMs = 60_000,
maxRetries = 4,
): Promise<Memory[]> {
const start = Date.now();
while (Date.now() - start < maxWaitMs) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
const results = await withRetry(() => client.search(query, options));
if (Array.isArray(results) && results.length > 0) {
return results;
}
await new Promise((r) => setTimeout(r, 3_000));
if (attempt < maxRetries) {
await new Promise((r) => setTimeout(r, 15_000));
}
}
return await client.search(query, options);
throw new Error(
`waitForSearchResults: no results for query "${query}" after ${maxRetries} attempts`,
);
}
/**