AI Voice Agent Backend
Module 13 · ai · intermediate
normalize.js
// Turn any voice platform's tool-call payload into one item per tool call:
// { platform, tool, args, tool_call_id }
const out = [];
for (const item of $input.all()) {
const body = item.json.body ?? {};
if (body.message?.toolCallList) {
// Vapi: { message: { type: 'tool-calls', toolCallList: [{ id, function: { name, arguments } }] } }
for (const tc of body.message.toolCallList) {
const fn = tc.function ?? tc;
let args = fn.arguments ?? fn.parameters ?? {};
if (typeof args === 'string') {
try { args = JSON.parse(args); } catch (e) { args = {}; }
}
out.push({ json: { platform: 'vapi', tool: fn.name, args, tool_call_id: tc.id } });
}
} else if (body.name) {
// Retell: { name, args, call }
out.push({ json: { platform: 'retell', tool: body.name, args: body.args ?? {}, tool_call_id: null } });
} else {
// Retell "args only" mode or a plain test call: the tool name comes from ?tool=
out.push({ json: { platform: 'plain', tool: item.json.query?.tool ?? 'unknown', args: body, tool_call_id: null } });
}
}
return out;