export function createChatStreamState() {
    return {
        textMessageIds: new Set(),
        reasoningMessageIds: new Set(),
        toolCallIds: new Set(),
    };
}
export function chatStreamFramesFromOutputEvent(event, state) {
    const eventId = "eventId" in event && typeof event.eventId === "string" ? event.eventId : undefined;
    const frames = [];
    const reasoningMessageId = reasoningIdFromOutputEvent(event);
    switch (event.type) {
        case "message_started":
            if (eventId) {
                frames.push({
                    type: "RUN_STARTED",
                    runId: eventId,
                    input: { text: event.text, source: event.source },
                });
            }
            break;
        case "message_finished":
            if (eventId)
                frames.push({ type: "RUN_FINISHED", runId: eventId });
            break;
        case "assistant_delta":
            if (eventId && event.text.length > 0) {
                const messageId = textMessageIdFromOutputEvent(event);
                ensureTextMessageStarted(frames, state, messageId, eventId);
                frames.push({ type: "TEXT_MESSAGE_CONTENT", messageId, runId: eventId, delta: event.text });
            }
            break;
        case "assistant_message":
            if (eventId) {
                const messageId = textMessageIdFromOutputEvent(event);
                ensureTextMessageStarted(frames, state, messageId, eventId);
                frames.push({ type: "TEXT_MESSAGE_END", messageId, runId: eventId, finalText: event.text });
            }
            break;
        case "thinking_started":
            if (reasoningMessageId)
                ensureReasoningStarted(frames, state, reasoningMessageId, eventId);
            break;
        case "thinking_delta":
            if (reasoningMessageId && event.text.length > 0) {
                ensureReasoningStarted(frames, state, reasoningMessageId, eventId);
                frames.push({ type: "REASONING_MESSAGE_CONTENT", messageId: reasoningMessageId, runId: eventId, delta: event.text });
            }
            break;
        case "thinking_finished":
            if (reasoningMessageId) {
                ensureReasoningStarted(frames, state, reasoningMessageId, eventId);
                frames.push({ type: "REASONING_MESSAGE_END", messageId: reasoningMessageId, runId: eventId, finalText: event.text });
            }
            break;
        case "tool_call":
            ensureToolCallStarted(frames, state, event.toolCallId, event.toolName, event.args, eventId);
            frames.push({ type: "TOOL_CALL_ARGS", toolCallId: event.toolCallId, args: event.args, argsComplete: event.argsComplete });
            break;
        case "tool_execution_started":
            ensureToolCallStarted(frames, state, event.toolCallId, event.toolName, event.args, eventId);
            break;
        case "tool_execution_updated":
            ensureToolCallStarted(frames, state, event.toolCallId, event.toolName, event.args, eventId);
            break;
        case "tool_execution_finished":
            ensureToolCallStarted(frames, state, event.toolCallId, event.toolName, undefined, eventId);
            frames.push({ type: "TOOL_CALL_RESULT", toolCallId: event.toolCallId, result: event.result, isError: event.isError });
            break;
        case "subagent_session":
            frames.push({
                type: "AGENT_DELEGATION",
                toolCallId: event.toolCallId,
                toolName: event.toolName,
                subagentName: event.subagentName,
                childPiboSessionId: event.childPiboSessionId,
                threadKey: event.threadKey,
            });
            break;
        case "execution_result":
            frames.push({ type: "EXECUTION_RESULT", runId: eventId, action: event.action, result: event.result });
            break;
        case "session_error":
            frames.push({ type: "RUN_ERROR", runId: eventId, message: event.error });
            break;
        default:
            frames.push({ type: "RAW_EVENT", event });
            break;
    }
    return frames;
}
function reasoningIdFromOutputEvent(event) {
    if (event.type !== "thinking_started" && event.type !== "thinking_delta" && event.type !== "thinking_finished") {
        return undefined;
    }
    if (!event.eventId)
        return undefined;
    const partIndex = typeof event.thinkingIndex === "number" ? event.thinkingIndex : event.contentIndex;
    return typeof partIndex === "number" ? `${event.eventId}:thinking:${partIndex}` : event.eventId;
}
function textMessageIdFromOutputEvent(event) {
    const partIndex = typeof event.assistantIndex === "number" ? event.assistantIndex : event.contentIndex;
    return partIndex === undefined || !event.eventId ? event.eventId ?? "" : `${event.eventId}:assistant:${partIndex}`;
}
function ensureTextMessageStarted(frames, state, messageId, runId) {
    if (state.textMessageIds.has(messageId))
        return;
    state.textMessageIds.add(messageId);
    frames.push({ type: "TEXT_MESSAGE_START", messageId, runId, role: "assistant" });
}
function ensureReasoningStarted(frames, state, messageId, runId) {
    if (state.reasoningMessageIds.has(messageId))
        return;
    state.reasoningMessageIds.add(messageId);
    frames.push({ type: "REASONING_MESSAGE_START", messageId, runId });
}
function ensureToolCallStarted(frames, state, toolCallId, toolName, args, runId) {
    if (state.toolCallIds.has(toolCallId))
        return;
    state.toolCallIds.add(toolCallId);
    frames.push({ type: "TOOL_CALL_START", toolCallId, toolName, args, runId });
}
