import { prettyPrintJson } from "./prettyPrintJson.js";
function pushBlocksForMessage(msg, blocks) {
    var _a;
    for (const part of (_a = msg.content) !== null && _a !== void 0 ? _a : []) {
        if (part.type === 'output_text') {
            blocks.push({ type: 'text', data: { text: part.text } });
        }
        if (part.type === 'refusal') {
            blocks.push({ type: 'text', data: { text: part.refusal } });
        }
    }
}
function pushBlocksForReasoning(reasoning, blocks) {
    var _a, _b, _c, _d, _e;
    const content = (_d = (_b = (_a = reasoning.content) === null || _a === void 0 ? void 0 : _a.map((c) => c.text)) !== null && _b !== void 0 ? _b : (_c = reasoning.summary) === null || _c === void 0 ? void 0 : _c.map((s) => s.text)) !== null && _d !== void 0 ? _d : [];
    blocks.push(Object.assign(Object.assign({}, (Boolean(reasoning.id) && { id: reasoning.id })), { type: 'thinking', data: {
            content: content.length <= 1 ? ((_e = content[0]) !== null && _e !== void 0 ? _e : '') : content,
            status: 'thought',
            defaultExpanded: false,
        } }));
}
function pushBlocksForFunctionCall(fn, blocks) {
    blocks.push({
        type: 'tool',
        id: fn.call_id,
        data: { toolName: fn.name, status: 'loading' },
    });
}
function pushBlocksForMcpCall(mcp, blocks) {
    var _a, _b, _c, _d;
    const statusByMcpStatus = {
        completed: 'success',
        failed: 'error',
    };
    const status = (_b = statusByMcpStatus[(_a = mcp.status) !== null && _a !== void 0 ? _a : '']) !== null && _b !== void 0 ? _b : 'loading';
    let bodyText;
    if (typeof mcp.error === 'string' && mcp.error.length > 0) {
        bodyText = mcp.error;
    }
    else if (typeof mcp.output === 'string' && mcp.output.length > 0) {
        bodyText = mcp.output;
    }
    const mcpRequest = prettyPrintJson(mcp.arguments);
    const mcpResponse = prettyPrintJson(bodyText);
    blocks.push({
        type: 'tool',
        id: mcp.id,
        data: Object.assign(Object.assign(Object.assign({ toolName: (_d = (_c = mcp.name) !== null && _c !== void 0 ? _c : mcp.server_label) !== null && _d !== void 0 ? _d : 'MCP', status }, (bodyText ? { bodyContent: bodyText } : {})), (mcpRequest ? { mcpRequest } : {})), (mcpResponse ? { mcpResponse } : {})),
    });
}
function pushBlocksForItem(item, blocks) {
    if (item.type === 'message' && 'role' in item && item.role === 'assistant') {
        pushBlocksForMessage(item, blocks);
        return;
    }
    if (item.type === 'reasoning') {
        pushBlocksForReasoning(item, blocks);
        return;
    }
    if (item.type === 'function_call') {
        pushBlocksForFunctionCall(item, blocks);
        return;
    }
    if (item.type === 'mcp_call') {
        pushBlocksForMcpCall(item, blocks);
    }
}
/** OpenAI response output (flat array of message/reasoning/function_call) → TAssistantMessage content. */
export function mapOutputToContent(output) {
    const blocks = [];
    for (const item of output !== null && output !== void 0 ? output : []) {
        pushBlocksForItem(item, blocks);
    }
    if (blocks.length === 0)
        return '';
    if (blocks.length === 1 && blocks[0].type === 'text')
        return blocks[0].data.text;
    return blocks;
}
//# sourceMappingURL=mapOutputToContent.js.map
