import { useCallback } from "react";
export function useAgentRunResyncSocketHandler(input) {
    const { agent, actions, messagesRef, currentRunIdRef, lastSeqRef, lastResyncedMessageIdsRef, normalizeRunId, noteMissingRunId, appendToolUiEvent, createNewStreamMessage, } = input;
    return useCallback((messageType, payload) => {
        if (messageType !== "agent:run:resync") {
            return false;
        }
        const resyncRunId = normalizeRunId(payload?.runId);
        if (resyncRunId === null) {
            noteMissingRunId("agent:run:resync");
        }
        else if (currentRunIdRef.current !== null &&
            resyncRunId !== currentRunIdRef.current) {
            appendToolUiEvent("ui:stream-runid-dropped", `agent:run:resync runId=${resyncRunId} currentRunId=${currentRunIdRef.current}`);
            return true;
        }
        const resyncMessageId = payload?.messageId;
        if (!resyncMessageId) {
            return true;
        }
        const totalContent = String(payload?.totalContent ?? "");
        const reasoning = String(payload?.reasoning ?? "");
        const seq = Number(payload?.seq ?? 0);
        const rawToolCalls = Array.isArray(payload?.toolCalls)
            ? payload.toolCalls
            : [];
        const normalizedToolCalls = rawToolCalls
            .filter((toolCall) => toolCall && typeof toolCall === "object")
            .map((toolCall) => {
            const toolCallId = toolCall.toolCallId || toolCall.id || crypto.randomUUID();
            return {
                id: toolCallId,
                messageId: resyncMessageId,
                toolCallId,
                functionName: toolCall.functionName ||
                    toolCall.name ||
                    toolCall.displayName ||
                    "",
                functionArguments: toolCall.functionArguments ?? toolCall.arguments ?? "",
                functionResult: toolCall.functionResult ?? toolCall.result ?? "",
                functionResultRichContent: toolCall.functionResultRichContent ??
                    toolCall.richContent ??
                    undefined,
                functionError: toolCall.functionError ?? toolCall.error ?? "",
                isCompleted: toolCall.isCompleted === true,
                responseTimeMs: toolCall.responseTimeMs,
                createdDate: toolCall.createdDate ||
                    payload?.timestamp ||
                    new Date().toISOString(),
                requiresApproval: toolCall.requiresApproval,
                ...(toolCall.dbMessageId
                    ? { dbMessageId: toolCall.dbMessageId }
                    : {}),
            };
        });
        const existingIndex = messagesRef.current.findIndex((message) => message.id === resyncMessageId);
        if (existingIndex === -1) {
            const created = createNewStreamMessage(resyncMessageId, agent);
            const seeded = {
                ...created,
                content: totalContent,
                reasoning,
                toolCalls: normalizedToolCalls,
            };
            const updated = [...messagesRef.current, seeded];
            messagesRef.current = updated;
            actions.replaceTranscript(updated);
            return true;
        }
        else {
            const updated = messagesRef.current.map((message, index) => index === existingIndex
                ? {
                    ...message,
                    content: totalContent,
                    reasoning,
                    toolCalls: normalizedToolCalls,
                }
                : message);
            messagesRef.current = updated;
            actions.replaceTranscript(updated);
        }
        if (seq && seq > lastSeqRef.current) {
            lastSeqRef.current = seq;
        }
        actions.runFrame({
            runId: payload?.runId,
            seq,
        });
        lastResyncedMessageIdsRef.current.add(resyncMessageId.toLowerCase());
        return true;
    }, [
        actions,
        agent,
        appendToolUiEvent,
        createNewStreamMessage,
        currentRunIdRef,
        lastResyncedMessageIdsRef,
        lastSeqRef,
        messagesRef,
        normalizeRunId,
        noteMissingRunId,
    ]);
}
//# sourceMappingURL=useAgentRunResyncSocketHandler.js.map