/** * Normalize QVAC completion stream events for cap-chunk (keep under NMH ~1 MB). */ const MAX_DELTA = 64 * 1024; function clip(s) { const t = s == null ? '' : String(s); if (t.length <= MAX_DELTA) return t; return t.slice(0, MAX_DELTA); } function normalizeCompletionEvent(ev) { if (!ev || !ev.type) return null; if (ev.type === 'contentDelta') { return { type: 'contentDelta', delta: clip(ev.delta || ev.text || ev.content || '') }; } if (ev.type === 'thinkingDelta') { return { type: 'thinkingDelta', delta: clip(ev.delta || ev.text || ev.content || '') }; } if (ev.type === 'toolCall') { const call = ev.call || ev.toolCall || ev; return { type: 'toolCall', call: { id: call.id, name: call.name, arguments: call.arguments != null ? call.arguments : call.args, }, }; } if (ev.type === 'rawDelta') { return { type: 'rawDelta', delta: clip(ev.delta) }; } return { type: ev.type, delta: ev.delta != null ? clip(ev.delta) : undefined, call: ev.call }; } module.exports = { normalizeCompletionEvent, MAX_DELTA };