/**
 * Standard MCP response formatters.
 *
 * Uses compact JSON (no indentation) to minimize token count for LLM consumers.
 * Errors keep minimal formatting for readability in tool-call error surfaces.
 */
import { svgToPngBase64 } from "./svg/render-png.js";
/**
 * Coerce registry list payloads into the `{ items, total?, page? }` shape expected by
 * `harness_list`'s output schema and MCP `structuredContent` (objects only — raw arrays
 * cannot be sent as structured content).
 *
 * Harness Code endpoints such as `pr_activity` return a top-level JSON array; without
 * this step, clients that require structured output fail with output validation errors.
 */
export function normalizeHarnessListPayload(result, options) {
    const page = options?.page;
    if (Array.isArray(result)) {
        return {
            items: result,
            total: result.length,
            ...(page !== undefined ? { page } : {}),
        };
    }
    if (result !== null && typeof result === "object" && !Array.isArray(result)) {
        const r = result;
        if (!Array.isArray(r.items)) {
            for (const key of ["body", "content", "data", "objects", "features"]) {
                const arr = r[key];
                if (Array.isArray(arr)) {
                    const total = typeof r.total === "number" ? r.total : arr.length;
                    return {
                        ...r,
                        items: arr,
                        total,
                        ...(page !== undefined && r.page === undefined ? { page } : {}),
                    };
                }
            }
        }
        else if (Array.isArray(r.items) && r.total === undefined) {
            return { ...r, total: r.items.length };
        }
    }
    return result;
}
export function jsonResult(data) {
    return {
        content: [{ type: "text", text: JSON.stringify(data) }],
        // MCP structuredContent must be an object; arrays and primitives are intentionally excluded.
        ...(data !== null && typeof data === "object" && !Array.isArray(data)
            ? { structuredContent: data }
            : {}),
    };
}
export function errorResult(message) {
    return {
        content: [{ type: "text", text: JSON.stringify({ error: message }) }],
        isError: true,
    };
}
export async function imageResult(svgString) {
    const data = await svgToPngBase64(svgString);
    return {
        content: [{ type: "image", data, mimeType: "image/png" }],
    };
}
export async function mixedResult(data, svgString, options) {
    const scale = options?.scale ?? 3;
    const imageData = await svgToPngBase64(svgString, { scale });
    return {
        content: [
            { type: "text", text: JSON.stringify(data) },
            { type: "image", data: imageData, mimeType: "image/png" },
        ],
        // MCP structuredContent must be an object; arrays and primitives are intentionally excluded.
        ...(data !== null && typeof data === "object" && !Array.isArray(data)
            ? { structuredContent: data }
            : {}),
    };
}
//# sourceMappingURL=response-formatter.js.map