"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.messagesToRenderEvents = messagesToRenderEvents;
function messagesToRenderEvents(messages, taskId, agentName) {
    const events = [];
    for (const msg of messages) {
        if (msg.role === "assistant") {
            if (typeof msg.content === "string" && msg.content) {
                events.push({
                    type: "agentMessage",
                    taskId,
                    agentName,
                    message: msg.content,
                    role: "assistant",
                });
            }
            if (msg.tool_calls) {
                for (const tc of msg.tool_calls) {
                    events.push({
                        type: "toolCall",
                        taskId,
                        agentName,
                        toolCall: {
                            id: tc.id,
                            function: {
                                name: tc.function.name,
                                arguments: tc.function.arguments,
                            },
                        },
                    });
                }
            }
        }
        else if (msg.role === "tool") {
            const content = typeof msg.content === "string"
                ? msg.content
                : JSON.stringify(msg.content);
            events.push({
                type: "toolResult",
                taskId,
                agentName,
                toolCall: { function: { name: msg.name || "unknown", arguments: "" } },
                result: content,
            });
        }
        else if (msg.role === "user") {
            const content = typeof msg.content === "string"
                ? msg.content
                : Array.isArray(msg.content)
                    ? msg.content
                        .filter((c) => c.type === "text")
                        .map((c) => c.text)
                        .join("\n")
                    : String(msg.content ?? "");
            if (content.trim().startsWith("<Workflow>") || /<Workflow>/i.test(content)) {
                continue;
            }
            if (content) {
                events.push({
                    type: "agentMessage",
                    taskId,
                    agentName,
                    message: content,
                    role: "user",
                });
            }
        }
    }
    return events;
}
//# sourceMappingURL=messagesToRenderEvents.js.map