UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

31 lines (30 loc) 1.28 kB
/** * Fixes tool calls with error states by copying rawInput to input field. * This is required because error tool calls from the frontend use rawInput * but the AI SDK's convertToModelMessages expects input to generate arguments * for the OpenAI API. * @param messages - Array of message objects from the chat request * @returns Messages with error tool calls fixed, typed as UIMessage[] */ export const fixErrorToolCalls = (messages) => { return messages.map((msg) => { if (msg['role'] === 'assistant' && msg['parts'] && Array.isArray(msg['parts'])) { const fixedParts = msg['parts'].map((part) => { if (typeof part === 'object' && part !== null && 'type' in part && typeof part.type === 'string' && part.type.startsWith('tool-') && 'state' in part && part.state === 'output-error' && 'rawInput' in part && (!('input' in part) || part.input == null)) { return { ...part, input: part.rawInput }; } return part; }); return { ...msg, parts: fixedParts }; } return msg; }); };