/**
 * Ensure finish_reason and content invariants on response side:
 * - If tool_calls present, finish_reason defaults to 'tool_calls'
 * - If tool_calls present, message.content=null (Chat path)
 */
export class ResponseFinishInvariantsFilter {
    name = 'response_finish_invariants';
    stage = 'response_post';
    apply(input) {
        try {
            const out = JSON.parse(JSON.stringify(input || {}));
            const choices = Array.isArray(out.choices) ? out.choices : [];
            for (const ch of choices) {
                const msg = ch && ch.message ? ch.message : undefined;
                const tcs = msg && Array.isArray(msg.tool_calls) ? msg.tool_calls : [];
                if (tcs.length > 0) {
                    if (ch.finish_reason == null)
                        ch.finish_reason = 'tool_calls';
                    if (msg && typeof msg === 'object')
                        msg.content = null;
                }
            }
            out.choices = choices;
            return { ok: true, data: out };
        }
        catch {
            return { ok: true, data: input };
        }
    }
}
