feat(integrations): add Zapier app for Mem0
A Zapier Platform CLI app (TypeScript) wrapping the hosted Mem0 v3 REST API: Add / Search / Get Memories / Delete actions with custom API-key auth. - Written in TypeScript, compiles to dist/; offline unit tests (mocked z.request) plus live E2E; `zapier validate` clean - Add supports custom_instructions, custom_categories, includes, excludes; Wait-for-Completion defaults off (extraction can exceed Zapier's 30s step limit) - Middleware surfaces non-2xx as errors (no silent empty results) - Anonymous opt-out telemetry (MEM0_TELEMETRY=false) - Docs page + docs.json/llms.txt entry, ci-gate wiring, AGENTS.md rows Split out of #6057 per review (separate PRs per integration).
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import * as zapier from 'zapier-platform-core';
|
||||
import App from '../src';
|
||||
|
||||
const appTester = zapier.createAppTester(App as any);
|
||||
|
||||
const authData = {
|
||||
apiKey: process.env.MEM0_API_KEY,
|
||||
baseUrl: process.env.MEM0_BASE_URL || 'https://api.mem0.ai',
|
||||
};
|
||||
|
||||
const userId = `zapier-e2e-${Date.now()}`;
|
||||
|
||||
// Retry an async op until `done` is satisfied or attempts run out. Extraction is
|
||||
// async, so the default Add returns before the memory is searchable.
|
||||
const until = async <T>(
|
||||
fn: () => Promise<T>,
|
||||
done: (r: T) => boolean,
|
||||
{ attempts = 30, delayMs = 2000 } = {},
|
||||
): Promise<T> => {
|
||||
let last: T = undefined as unknown as T;
|
||||
for (let i = 0; i < attempts; i++) {
|
||||
last = await fn();
|
||||
if (done(last)) return last;
|
||||
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||
}
|
||||
return last;
|
||||
};
|
||||
|
||||
// The E2E suite hits the live Mem0 API, so it only runs when MEM0_API_KEY is
|
||||
// set (locally / with a secret). In CI without a key it is skipped, not failed.
|
||||
const describeE2E = authData.apiKey ? describe : describe.skip;
|
||||
|
||||
describeE2E('Mem0 Zapier integration (E2E)', () => {
|
||||
it('authentication.test succeeds', async () => {
|
||||
const res: any = await appTester((App as any).authentication.test, { authData });
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it('adds, searches, lists, and deletes a memory', async () => {
|
||||
// Add via the default path: returns immediately with an event id.
|
||||
const added: any = await appTester((App as any).creates.add_memory.operation.perform, {
|
||||
authData,
|
||||
inputData: {
|
||||
content: 'I love hiking in the Alps and my favorite food is sushi',
|
||||
user_id: userId,
|
||||
},
|
||||
});
|
||||
expect(added.event_id).toBeDefined();
|
||||
|
||||
// Extraction is async; retry search until the memory is indexed.
|
||||
// Budget generously — live extraction can occasionally exceed a minute.
|
||||
const found: any[] = await until(
|
||||
() =>
|
||||
appTester((App as any).searches.search_memories.operation.perform, {
|
||||
authData,
|
||||
inputData: { query: 'outdoor activities', user_id: userId, limit: 5 },
|
||||
}),
|
||||
(r: any[]) => Array.isArray(r) && r.length > 0,
|
||||
{ attempts: 60, delayMs: 2000 },
|
||||
);
|
||||
expect(Array.isArray(found)).toBe(true);
|
||||
expect(found.length).toBeGreaterThan(0);
|
||||
|
||||
// Get all
|
||||
const all: any[] = await appTester((App as any).searches.get_memories.operation.perform, {
|
||||
authData,
|
||||
inputData: { user_id: userId },
|
||||
});
|
||||
expect(Array.isArray(all)).toBe(true);
|
||||
expect(all.length).toBeGreaterThan(0);
|
||||
|
||||
// Cleanup: delete every memory we created
|
||||
for (const mem of all) {
|
||||
await appTester((App as any).creates.delete_memory.operation.perform, {
|
||||
authData,
|
||||
inputData: { memory_id: mem.id },
|
||||
});
|
||||
}
|
||||
|
||||
const afterDelete: any[] = await appTester(
|
||||
(App as any).searches.get_memories.operation.perform,
|
||||
{ authData, inputData: { user_id: userId } },
|
||||
);
|
||||
expect(afterDelete.length).toBe(0);
|
||||
});
|
||||
|
||||
it('surfaces API errors instead of returning an empty array (search needs a filter)', async () => {
|
||||
// filters is required by the API; omitting it must throw, not return [].
|
||||
await expect(
|
||||
appTester((App as any).searches.search_memories.operation.perform, {
|
||||
authData,
|
||||
inputData: { query: 'anything' },
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
// Offline unit tests: they mock `z.request`, so they run unconditionally in CI
|
||||
// (unlike the live E2E suite in mem0.test.ts, gated on MEM0_API_KEY). They cover
|
||||
// what `zapier validate` can't: boolean coercion, URL join, metadata, array shapes.
|
||||
|
||||
import addMemory from '../src/creates/add_memory';
|
||||
import deleteMemory from '../src/creates/delete_memory';
|
||||
import searchMemories from '../src/searches/search_memories';
|
||||
import getMemories from '../src/searches/get_memories';
|
||||
import { includeApiKey } from '../src/middleware';
|
||||
|
||||
// Minimal `z` stub: hands back queued responses and records every request.
|
||||
const makeZ = (responses: any[] = []): any => {
|
||||
const queue = [...responses];
|
||||
const requests: any[] = [];
|
||||
return {
|
||||
requests,
|
||||
request: async (opts: any) => {
|
||||
requests.push(opts);
|
||||
const next = queue.shift();
|
||||
return next !== undefined ? next : { data: {} };
|
||||
},
|
||||
errors: {
|
||||
Error: class Mem0Error extends Error {
|
||||
name: string;
|
||||
status?: number;
|
||||
constructor(message: string, name?: string, status?: number) {
|
||||
super(message);
|
||||
this.name = name || 'Error';
|
||||
this.status = status;
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
describe('add_memory (offline)', () => {
|
||||
it('coerces infer="false" to a boolean and does not poll by default', async () => {
|
||||
const z = makeZ([{ data: { event_id: 'e1', status: 'PENDING' } }]);
|
||||
const res = await addMemory.operation.perform(z, {
|
||||
inputData: { content: 'hi', user_id: 'u1', infer: 'false' },
|
||||
} as any);
|
||||
// waitForCompletion defaults off -> a single request (the add), no poll.
|
||||
expect(z.requests).toHaveLength(1);
|
||||
expect(z.requests[0].body.infer).toBe(false);
|
||||
expect((res as any).status).toBe('PENDING');
|
||||
});
|
||||
|
||||
it('polls the event only when waitForCompletion="true"', async () => {
|
||||
const z = makeZ([
|
||||
{ data: { event_id: 'e1', status: 'PENDING' } },
|
||||
{ data: { status: 'SUCCEEDED', results: [{ id: 'm1' }] } },
|
||||
]);
|
||||
const res = await addMemory.operation.perform(z, {
|
||||
inputData: { content: 'hi', user_id: 'u1', waitForCompletion: 'true' },
|
||||
} as any);
|
||||
expect(z.requests).toHaveLength(2);
|
||||
expect(z.requests[1].url).toBe('/v1/event/e1/');
|
||||
expect((res as any).status).toBe('SUCCEEDED');
|
||||
});
|
||||
|
||||
it('throws a clear error on invalid JSON metadata', async () => {
|
||||
const z = makeZ();
|
||||
await expect(
|
||||
addMemory.operation.perform(z, {
|
||||
inputData: { content: 'hi', user_id: 'u1', metadata: '{not json' },
|
||||
} as any),
|
||||
).rejects.toThrow('Metadata must be valid JSON.');
|
||||
});
|
||||
|
||||
it('forwards custom_instructions and parses custom_categories JSON', async () => {
|
||||
const z = makeZ([{ data: { event_id: 'e1', status: 'PENDING' } }]);
|
||||
await addMemory.operation.perform(z, {
|
||||
inputData: {
|
||||
content: 'hi',
|
||||
user_id: 'u1',
|
||||
custom_instructions: 'keep durable facts',
|
||||
custom_categories: '[{"work":"job related"}]',
|
||||
},
|
||||
} as any);
|
||||
expect(z.requests[0].body.custom_instructions).toBe('keep durable facts');
|
||||
expect(z.requests[0].body.custom_categories).toEqual([{ work: 'job related' }]);
|
||||
});
|
||||
|
||||
it('throws a clear error on invalid Custom Categories JSON', async () => {
|
||||
const z = makeZ();
|
||||
await expect(
|
||||
addMemory.operation.perform(z, {
|
||||
inputData: { content: 'hi', user_id: 'u1', custom_categories: '{bad' },
|
||||
} as any),
|
||||
).rejects.toThrow('Custom Categories must be valid JSON.');
|
||||
});
|
||||
|
||||
it('forwards includes and excludes when provided', async () => {
|
||||
const z = makeZ([{ data: { event_id: 'e1', status: 'PENDING' } }]);
|
||||
await addMemory.operation.perform(z, {
|
||||
inputData: { content: 'hi', user_id: 'u1', includes: 'work facts', excludes: 'small talk' },
|
||||
} as any);
|
||||
expect(z.requests[0].body.includes).toBe('work facts');
|
||||
expect(z.requests[0].body.excludes).toBe('small talk');
|
||||
});
|
||||
});
|
||||
|
||||
describe('search / get array-shape enforcement (offline)', () => {
|
||||
it('search unwraps an object {results:[...]} into an array', async () => {
|
||||
const z = makeZ([{ data: { results: [{ id: 'm1' }] } }]);
|
||||
const res = await searchMemories.operation.perform(z, {
|
||||
inputData: { query: 'x', user_id: 'u1' },
|
||||
} as any);
|
||||
expect(Array.isArray(res)).toBe(true);
|
||||
expect(res).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('get_memories returns [] when the API returns neither array nor results', async () => {
|
||||
const z = makeZ([{ data: {} }]);
|
||||
const res = await getMemories.operation.perform(z, { inputData: { user_id: 'u1' } } as any);
|
||||
expect(Array.isArray(res)).toBe(true);
|
||||
expect(res).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('get_memories forwards page and page_size as numbers', async () => {
|
||||
const z = makeZ([{ data: { results: [] } }]);
|
||||
await getMemories.operation.perform(z, {
|
||||
inputData: { user_id: 'u1', page: '2', limit: '10' },
|
||||
} as any);
|
||||
expect(z.requests[0].params).toEqual({ page: 2, page_size: 10 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete_memory (offline)', () => {
|
||||
it('encodes the memory id in the URL path', async () => {
|
||||
const z = makeZ([{ data: {} }]);
|
||||
await deleteMemory.operation.perform(z, { inputData: { memory_id: 'a/b c' } } as any);
|
||||
expect(z.requests[0].url).toBe('/v1/memories/a%2Fb%20c/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('includeApiKey middleware (offline)', () => {
|
||||
it('prepends the base URL and injects the auth header', () => {
|
||||
const req = includeApiKey({ url: '/v3/memories/' }, null as any, {
|
||||
authData: { apiKey: 'k', baseUrl: 'https://api.mem0.ai/' },
|
||||
} as any);
|
||||
expect(req.url).toBe('https://api.mem0.ai/v3/memories/');
|
||||
expect(req.headers!.Authorization).toBe('Token k');
|
||||
});
|
||||
|
||||
it('leaves absolute URLs untouched', () => {
|
||||
const req = includeApiKey({ url: 'https://other.example/x' }, null as any, {
|
||||
authData: { apiKey: 'k' },
|
||||
} as any);
|
||||
expect(req.url).toBe('https://other.example/x');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user