Files
mem0/integrations/zapier-mem0/creates/add_memory.js
T
Himanshu-Sangshetti cf861ebf5a fix(integrations): address review feedback on Zapier and n8n
Zapier:
- Bound the poll budget under Zapier's step timeout and default Wait for
  Completion off; timeout message notes the add likely still succeeded
- Add offline unit tests (mocked z.request) and run them in CI
- Expose a Page input on Get Memories
- Encode memory_id in the delete path
- Update E2E to test the default no-wait path (add then search-retry)

n8n:
- Clarify the Infer description (controls extraction, not waiting)
- Add an App ID field and require at least one entity id before add
- Pass itemIndex into pollEvent errors
- Unwrap event results so wait-path output matches search and get many
2026-07-20 11:55:29 +05:30

121 lines
3.9 KiB
JavaScript

'use strict';
const POLL_INTERVAL_MS = 1500;
// Bounded so the poll budget stays under Zapier's per-step execution timeout.
const MAX_POLL_ATTEMPTS = 12;
// Polls GET /v1/event/{id}/ until the async memory-addition event resolves.
const pollEvent = async (z, eventId) => {
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) {
const res = await z.request({ url: `/v1/event/${eventId}/`, method: 'GET' });
const status = res.data && res.data.status;
if (status === 'SUCCEEDED') {
return res.data;
}
if (status === 'FAILED') {
const reason = (res.data && (res.data.error || res.data.message)) || 'unknown error';
throw new z.errors.Error(`Mem0 memory event ${eventId} failed: ${reason}`, 'Mem0EventFailed', 400);
}
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
}
throw new z.errors.Error(
`Timed out waiting for memory event ${eventId}. The add was accepted and is ` +
`likely still completing on the server — a timeout here does not mean it failed.`,
'Mem0Timeout',
408,
);
};
const perform = async (z, bundle) => {
// Zapier boolean fields can arrive as the strings 'true'/'false'; coerce
// explicitly so "Infer = No" / "Wait = No" are honored.
const infer = String(bundle.inputData.infer) !== 'false';
// Waiting is opt-in (the poll path can exceed Zapier's step timeout).
const wait = String(bundle.inputData.waitForCompletion) === 'true';
const body = {
messages: [{ role: bundle.inputData.role || 'user', content: bundle.inputData.content }],
infer,
};
if (bundle.inputData.user_id) body.user_id = bundle.inputData.user_id;
if (bundle.inputData.agent_id) body.agent_id = bundle.inputData.agent_id;
if (bundle.inputData.run_id) body.run_id = bundle.inputData.run_id;
if (bundle.inputData.metadata) {
try {
body.metadata =
typeof bundle.inputData.metadata === 'string'
? JSON.parse(bundle.inputData.metadata)
: bundle.inputData.metadata;
} catch (e) {
throw new z.errors.Error('Metadata must be valid JSON.', 'InvalidInput', 400);
}
}
const response = await z.request({
url: '/v3/memories/add/',
method: 'POST',
body,
});
const data = response.data;
// Add returns {event_id, status:PENDING|RUNNING}; poll only when opted in.
if (wait && data.event_id && data.status !== 'SUCCEEDED' && data.status !== 'FAILED') {
return pollEvent(z, data.event_id);
}
if (data.status === 'FAILED') {
const reason = data.error || data.message || 'unknown error';
throw new z.errors.Error(`Mem0 memory add failed: ${reason}`, 'Mem0EventFailed', 400);
}
return data;
};
module.exports = {
key: 'add_memory',
noun: 'Memory',
display: {
label: 'Add Memory',
description: 'Extract and store memories from a message.',
},
operation: {
perform,
inputFields: [
{
key: 'content',
label: 'Content',
type: 'text',
required: true,
helpText: 'The message content to extract memories from.',
},
{
key: 'role',
label: 'Role',
choices: { user: 'User', assistant: 'Assistant', system: 'System' },
default: 'user',
},
{ key: 'user_id', label: 'User ID', type: 'string' },
{ key: 'agent_id', label: 'Agent ID', type: 'string' },
{ key: 'run_id', label: 'Run ID', type: 'string' },
{ key: 'metadata', label: 'Metadata (JSON)', type: 'string' },
{
key: 'infer',
label: 'Infer',
type: 'boolean',
default: 'true',
helpText: 'Run LLM extraction over the message. Turn off to store it verbatim.',
},
{
key: 'waitForCompletion',
label: 'Wait for Completion',
type: 'boolean',
default: 'false',
helpText:
'Poll until extraction finishes and return the resulting memories. ' +
'Leave off (default) to return immediately with an event ID — extraction can take ' +
'longer than Zapier allows this step to run, and a timeout does not mean the add failed.',
},
],
sample: { status: 'SUCCEEDED', event_id: '00000000-0000-0000-0000-000000000000', results: [] },
},
};