
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="fac69471-392b-5cbd-95c2-178d6ca436c2")}catch(e){}}();
const CLINE_USER_ATTENTION_TOOL_NAMES = new Set(["ask_followup_question", "plan_mode_respond"]);
const WINDOWS_INVALID_SESSION_ID_CHARS = /[<>:"/\\|?*]/g;
export function now() {
    return Date.now();
}
export function cloneSummary(summary) {
    return {
        ...summary,
        latestHookActivity: summary.latestHookActivity ? { ...summary.latestHookActivity } : null,
        latestTurnCheckpoint: summary.latestTurnCheckpoint ? { ...summary.latestTurnCheckpoint } : null,
        previousTurnCheckpoint: summary.previousTurnCheckpoint ? { ...summary.previousTurnCheckpoint } : null,
    };
}
export function cloneMessage(message) {
    return {
        ...message,
        images: message.images ? message.images.map((image) => ({ ...image })) : message.images,
        meta: message.meta ? { ...message.meta } : message.meta,
    };
}
export function createDefaultSummary(taskId) {
    return {
        taskId,
        state: "idle",
        mode: null,
        agentId: "cline",
        workspacePath: null,
        pid: null,
        startedAt: null,
        updatedAt: now(),
        lastOutputAt: null,
        reviewReason: null,
        exitCode: null,
        lastHookAt: null,
        latestHookActivity: null,
        warningMessage: null,
        latestTurnCheckpoint: null,
        previousTurnCheckpoint: null,
    };
}
export function updateSummary(entry, patch) {
    entry.summary = {
        ...entry.summary,
        ...patch,
        updatedAt: now(),
    };
    return cloneSummary(entry.summary);
}
export function createMessage(taskId, role, content, images) {
    return {
        id: `${taskId}-${now()}-${Math.random().toString(36).slice(2, 8)}`,
        role,
        content,
        images: images && images.length > 0 ? images.map((image) => ({ ...image })) : undefined,
        createdAt: now(),
    };
}
export function createMessageWithMeta(taskId, role, content, meta, images) {
    return {
        ...createMessage(taskId, role, content, images),
        meta,
    };
}
export function createSessionId(taskId) {
    return `${toSessionIdTaskPrefix(taskId)}-${now()}-${Math.random().toString(36).slice(2, 10)}`;
}
export function buildSessionIdPrefix(taskId) {
    return `${toSessionIdTaskPrefix(taskId)}-`;
}
function toSessionIdTaskPrefix(taskId) {
    const normalized = taskId.replace(WINDOWS_INVALID_SESSION_ID_CHARS, "_").trim();
    return normalized.length > 0 ? normalized : "session";
}
export function isClineUserAttentionTool(toolName) {
    if (!toolName) {
        return false;
    }
    return CLINE_USER_ATTENTION_TOOL_NAMES.has(toolName.trim().toLowerCase());
}
export function canReturnToRunning(reviewReason) {
    return reviewReason === "attention" || reviewReason === "hook" || reviewReason === "error";
}
export function latestAssistantMessageMatches(entry, content) {
    const latestAssistant = getLatestAssistantMessage(entry);
    if (!latestAssistant) {
        return false;
    }
    return latestAssistant.content.trim() === content.trim();
}
export function clearActiveTurnState(entry) {
    entry.activeAssistantMessageId = null;
    entry.activeReasoningMessageId = null;
    entry.toolMessageIdByToolCallId.clear();
    entry.toolInputByToolCallId.clear();
}
export function appendAssistantChunk(entry, taskId, chunk) {
    const existingMessageId = entry.activeAssistantMessageId;
    if (existingMessageId) {
        const updatedMessage = updateMessageInEntry(entry, existingMessageId, (currentMessage) => ({
            ...currentMessage,
            content: `${currentMessage.content}${chunk}`,
        }));
        if (updatedMessage) {
            return updatedMessage;
        }
    }
    return createAssistantMessage(entry, taskId, chunk);
}
export function setOrCreateAssistantMessage(entry, taskId, content) {
    if (!entry.activeAssistantMessageId) {
        return null;
    }
    const updatedMessage = updateMessageInEntry(entry, entry.activeAssistantMessageId, (currentMessage) => ({
        ...currentMessage,
        content,
    }));
    if (updatedMessage) {
        return updatedMessage;
    }
    return createAssistantMessage(entry, taskId, content);
}
export function appendReasoningChunk(entry, taskId, chunk) {
    const existingMessageId = entry.activeReasoningMessageId;
    if (existingMessageId) {
        const updatedMessage = updateMessageInEntry(entry, existingMessageId, (currentMessage) => ({
            ...currentMessage,
            content: `${currentMessage.content}${chunk}`,
        }));
        if (updatedMessage) {
            return updatedMessage;
        }
    }
    return createReasoningMessage(entry, taskId, chunk);
}
export function setOrCreateReasoningMessage(entry, taskId, content) {
    if (!entry.activeReasoningMessageId) {
        return null;
    }
    const updatedMessage = updateMessageInEntry(entry, entry.activeReasoningMessageId, (currentMessage) => ({
        ...currentMessage,
        content,
    }));
    if (updatedMessage) {
        return updatedMessage;
    }
    return createReasoningMessage(entry, taskId, content);
}
export function createAssistantMessage(entry, taskId, content) {
    const message = createMessage(taskId, "assistant", content);
    entry.messages.push(message);
    entry.activeAssistantMessageId = message.id;
    return message;
}
export function createReasoningMessage(entry, taskId, content) {
    const message = createMessageWithMeta(taskId, "reasoning", content, {
        streamType: "reasoning",
    });
    entry.messages.push(message);
    entry.activeReasoningMessageId = message.id;
    return message;
}
export function startToolCallMessage(entry, taskId, input) {
    const toolContent = buildToolCallContent({
        toolName: input.toolName,
        input: input.input,
    });
    const message = createMessageWithMeta(taskId, "tool", toolContent, {
        toolName: input.toolName,
        hookEventName: "tool_call_start",
        toolCallId: input.toolCallId,
        streamType: "tool",
    });
    entry.messages.push(message);
    if (input.toolCallId) {
        entry.toolMessageIdByToolCallId.set(input.toolCallId, message.id);
        entry.toolInputByToolCallId.set(input.toolCallId, input.input);
    }
    return message;
}
export function finishToolCallMessage(entry, taskId, input) {
    const existingMessageId = input.toolCallId
        ? entry.toolMessageIdByToolCallId.get(input.toolCallId) ?? null
        : null;
    const toolInput = input.toolCallId ? entry.toolInputByToolCallId.get(input.toolCallId) : undefined;
    const content = buildToolCallContent({
        toolName: input.toolName,
        input: toolInput,
        output: input.output,
        error: input.error,
        durationMs: input.durationMs,
    });
    if (existingMessageId) {
        const updatedMessage = updateMessageInEntry(entry, existingMessageId, (currentMessage) => ({
            ...currentMessage,
            content,
            meta: {
                ...(currentMessage.meta ?? {}),
                toolName: input.toolName,
                hookEventName: "tool_call_end",
                toolCallId: input.toolCallId,
                streamType: "tool",
            },
        }));
        if (updatedMessage) {
            if (input.toolCallId) {
                entry.toolMessageIdByToolCallId.delete(input.toolCallId);
                entry.toolInputByToolCallId.delete(input.toolCallId);
            }
            return updatedMessage;
        }
    }
    const message = createMessageWithMeta(taskId, "tool", content, {
        toolName: input.toolName,
        hookEventName: "tool_call_end",
        toolCallId: input.toolCallId,
        streamType: "tool",
    });
    if (input.toolCallId) {
        entry.toolMessageIdByToolCallId.delete(input.toolCallId);
        entry.toolInputByToolCallId.delete(input.toolCallId);
    }
    entry.messages.push(message);
    return message;
}
function stringifyPayload(payload) {
    if (payload === undefined || payload === null) {
        return "";
    }
    if (typeof payload === "string") {
        return payload;
    }
    try {
        return JSON.stringify(payload, null, 2);
    }
    catch {
        return String(payload);
    }
}
function buildToolCallContent(input) {
    const lines = [];
    lines.push(`Tool: ${input.toolName ?? "unknown"}`);
    const inputText = stringifyPayload(input.input);
    if (inputText) {
        lines.push("Input:");
        lines.push(inputText);
    }
    if (input.error) {
        lines.push("Error:");
        lines.push(input.error);
    }
    else if (input.output !== undefined) {
        const outputText = stringifyPayload(input.output);
        if (outputText) {
            lines.push("Output:");
            lines.push(outputText);
        }
    }
    if (typeof input.durationMs === "number" && Number.isFinite(input.durationMs)) {
        lines.push(`Duration: ${Math.max(0, Math.round(input.durationMs))}ms`);
    }
    return lines.join("\n");
}
function updateMessageInEntry(entry, messageId, updater) {
    const messageIndex = entry.messages.findIndex((message) => message.id === messageId);
    if (messageIndex < 0) {
        return null;
    }
    const currentMessage = entry.messages[messageIndex];
    if (!currentMessage) {
        return null;
    }
    const nextMessage = updater(currentMessage);
    entry.messages[messageIndex] = nextMessage;
    return nextMessage;
}
function getLatestAssistantMessage(entry) {
    for (let index = entry.messages.length - 1; index >= 0; index -= 1) {
        const message = entry.messages[index];
        if (message?.role === "assistant") {
            return message;
        }
    }
    return null;
}
//# sourceMappingURL=cline-session-state.js.map
//# debugId=fac69471-392b-5cbd-95c2-178d6ca436c2
