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:
@@ -27,5 +27,5 @@ module.exports = {
|
||||
},
|
||||
],
|
||||
// Shown on the connection label in the Zap editor.
|
||||
connectionLabel: 'Mem0 ({{bundle.authData.baseUrl}})',
|
||||
connectionLabel: 'Mem0',
|
||||
};
|
||||
|
||||
@@ -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.',
|
||||
},
|
||||
],
|
||||
|
||||
@@ -14,7 +14,9 @@ const includeApiKey = (request, z, bundle) => {
|
||||
return request;
|
||||
};
|
||||
|
||||
// Surface auth failures as Zapier auth errors so users are prompted to reconnect.
|
||||
// Surface HTTP failures as errors. z.request does NOT throw on non-2xx by
|
||||
// default, so without this a 4xx/5xx would flow downstream as a fake success
|
||||
// (empty search results / error body returned as a created memory).
|
||||
const handleBadResponses = (response, z, _bundle) => {
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
throw new z.errors.Error(
|
||||
@@ -23,6 +25,15 @@ const handleBadResponses = (response, z, _bundle) => {
|
||||
response.status,
|
||||
);
|
||||
}
|
||||
if (response.status >= 400) {
|
||||
const data = response.data || {};
|
||||
const detail = data.detail || data.error || data.message || response.content || 'unknown error';
|
||||
throw new z.errors.Error(
|
||||
`Mem0 API request failed (HTTP ${response.status}): ${detail}`,
|
||||
'Mem0ApiError',
|
||||
response.status,
|
||||
);
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ const perform = async (z, bundle) => {
|
||||
const response = await z.request({
|
||||
url: '/v3/memories/',
|
||||
method: 'POST',
|
||||
params: { page: 1, page_size: bundle.inputData.limit || 50 },
|
||||
params: { page: 1, page_size: Math.max(1, Math.floor(Number(bundle.inputData.limit) || 50)) },
|
||||
body,
|
||||
});
|
||||
const data = response.data;
|
||||
@@ -24,8 +24,8 @@ module.exports = {
|
||||
operation: {
|
||||
perform,
|
||||
inputFields: [
|
||||
{ key: 'user_id', label: 'User ID', type: 'string' },
|
||||
{ key: 'limit', label: 'Limit', type: 'integer', default: '50' },
|
||||
{ key: 'user_id', label: 'User ID', type: 'string', required: true },
|
||||
{ key: 'limit', label: 'Limit', type: 'integer', default: 50 },
|
||||
],
|
||||
sample: { id: '00000000-0000-0000-0000-000000000000', memory: 'User loves hiking' },
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ const perform = async (z, bundle) => {
|
||||
const body = {
|
||||
query: bundle.inputData.query,
|
||||
output_format: 'v1.1',
|
||||
limit: bundle.inputData.limit || 10,
|
||||
top_k: Math.max(1, Math.floor(Number(bundle.inputData.limit) || 10)),
|
||||
};
|
||||
if (bundle.inputData.user_id) body.filters = { user_id: bundle.inputData.user_id };
|
||||
|
||||
@@ -29,8 +29,8 @@ module.exports = {
|
||||
perform,
|
||||
inputFields: [
|
||||
{ key: 'query', label: 'Query', type: 'string', required: true },
|
||||
{ key: 'user_id', label: 'User ID', type: 'string' },
|
||||
{ key: 'limit', label: 'Limit', type: 'integer', default: '10' },
|
||||
{ key: 'user_id', label: 'User ID', type: 'string', required: true },
|
||||
{ key: 'limit', label: 'Limit', type: 'integer', default: 10 },
|
||||
],
|
||||
sample: { id: '00000000-0000-0000-0000-000000000000', memory: 'User loves hiking' },
|
||||
},
|
||||
|
||||
@@ -66,4 +66,14 @@ describe('Mem0 Zapier integration (E2E)', () => {
|
||||
});
|
||||
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.searches.search_memories.operation.perform, {
|
||||
authData,
|
||||
inputData: { query: 'anything' },
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user