fix(integrations): address code-review findings

n8n: throw on FAILED memory events (was silently returned), poll on any
non-terminal status (PENDING or RUNNING), use top_k (not limit) for
search, coerce search/getAll responses to arrays, guard invalid-JSON
metadata, reject empty updates, mark user_id required; lint clean.

Zapier: coerce boolean fields (String(x) !== 'false') + boolean defaults
so "Wait"/"Infer = No" are honored, throw on HTTP >= 400 (was silent
empty/fake-success), throw on FAILED events, use top_k, numeric coercion
with clamping, mark user_id required.
This commit is contained in:
Himanshu-Sangshetti
2026-07-20 11:55:29 +05:30
parent 9882eea7b8
commit e66e97b725
8 changed files with 136 additions and 54 deletions
+32 -12
View File
@@ -8,27 +8,44 @@ 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' || status === 'FAILED') {
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 Error(`Timed out waiting for memory event ${eventId} to complete`);
throw new z.errors.Error(
`Timed out waiting for memory event ${eventId} to complete`,
'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';
const wait = String(bundle.inputData.waitForCompletion) !== 'false';
const body = {
messages: [{ role: bundle.inputData.role || 'user', content: bundle.inputData.content }],
infer: bundle.inputData.infer !== undefined ? bundle.inputData.infer : true,
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) {
body.metadata =
typeof bundle.inputData.metadata === 'string'
? JSON.parse(bundle.inputData.metadata)
: 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({
@@ -38,12 +55,15 @@ const perform = async (z, bundle) => {
});
const data = response.data;
const wait = bundle.inputData.waitForCompletion !== false;
// infer=true returns {event_id, status:PENDING}; optionally wait for the result.
if (wait && data.status && data.status !== 'SUCCEEDED' && data.event_id) {
// infer=true returns {event_id, status:PENDING|RUNNING}; optionally wait.
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;
};
@@ -78,14 +98,14 @@ module.exports = {
key: 'infer',
label: 'Infer',
type: 'boolean',
default: 'true',
default: true,
helpText: 'Run LLM extraction (async). Turn off to store verbatim (synchronous).',
},
{
key: 'waitForCompletion',
label: 'Wait for Completion',
type: 'boolean',
default: 'true',
default: true,
helpText: 'Poll until extraction finishes and return the resulting memories.',
},
],