export const AGENT_HISTORY_LIMIT = 1000;
export const MACHINE_CAPACITY_REASON = "machineCapacity";
export const MACHINE_CAPACITY_DETAIL = "waitingForCapacity";
export function mergeAgentOperationHistory(existing, incoming, limit = AGENT_HISTORY_LIMIT) {
    const merged = new Map(existing.map((operation) => [operation.id, operation]));
    for (const operation of incoming) {
        merged.set(operation.id, operation);
    }
    return Array.from(merged.values())
        .sort((left, right) => new Date(right.date).getTime() - new Date(left.date).getTime())
        .slice(0, limit);
}
export function buildPlaceholderAgentDetails(agentStub) {
    const now = new Date().toISOString();
    const updated = agentStub.updatedDate || now;
    // AgentDetails has required fields, but some workspaces only pass an Agent stub initially.
    // This placeholder keeps streaming/tool-call UI working until `getAgent()` returns full details.
    return {
        ...agentStub,
        name: agentStub.name || "Agent",
        userId: agentStub.userId || "",
        updatedDate: updated,
        profileName: agentStub.profileName || "",
        model: agentStub.model || "",
        createdDate: agentStub.createdDate || updated,
        totalTokensUsed: 0,
        totalInputTokens: 0,
        totalOutputTokens: 0,
        totalCachedInputTokens: 0,
        totalInputTokenCost: 0,
        totalOutputTokenCost: 0,
        totalCachedInputTokenCost: 0,
        totalImageCost: 0,
        totalCost: 0,
        currency: agentStub.currency || "USD",
        messageCount: agentStub.messageCount || 0,
    };
}
export function normalizeDialogAgentId(value) {
    return value?.trim().toLowerCase() || "";
}
export function normalizeServerExecutionStatus(value) {
    return (value || "").replace(/\s+/g, "").toLowerCase();
}
export function isFinishedServerExecutionStatus(value) {
    const normalized = normalizeServerExecutionStatus(value);
    return normalized === "completed" || normalized === "cancelled";
}
export function isInactiveServerExecutionStatus(value) {
    const normalized = normalizeServerExecutionStatus(value);
    return (normalized === "completed" ||
        normalized === "cancelled" ||
        normalized === "idle");
}
export function formatAllowanceSource(source) {
    const normalized = source?.trim();
    if (!normalized)
        return null;
    if (normalized === "user")
        return "User granted";
    if (normalized.startsWith("preconfigured:profile:"))
        return "Profile";
    if (normalized.startsWith("preconfigured:skill:"))
        return "Skill";
    if (normalized.startsWith("system:")) {
        return normalized.slice("system:".length).replace(/[-_]+/g, " ").trim();
    }
    return normalized;
}
export function formatAllowanceLabel(allowance) {
    return `${allowance.operationType || "*"}${"itemPath" in allowance
        ? ` ${allowance.itemPath}`
        : ` ${allowance.normalizedPath}`}`;
}
export function normalizeProfileAllowanceOperations(operationTypes) {
    const normalized = (operationTypes || [])
        .map((operationType) => String(operationType || "").trim())
        .filter((operationType) => operationType.length > 0);
    return normalized.length > 0 ? normalized : ["*"];
}
export function getAgentRunMessageAgentId(payload) {
    const agentId = payload?.agentId;
    return typeof agentId === "string" ? normalizeDialogAgentId(agentId) : null;
}
export function getAgentRunMessageSeq(payload) {
    const seq = payload?.seq;
    return typeof seq === "number" ? seq : null;
}
export function getAgentRunMessageDetail(type, payload) {
    if (type === "agent:run:delta") {
        return payload?.type || null;
    }
    if (type === "agent:run:status") {
        if (payload?.data?.reason === MACHINE_CAPACITY_REASON) {
            return MACHINE_CAPACITY_DETAIL;
        }
        return payload?.data?.state || payload?.data?.status || null;
    }
    if (type === "agent:run:error") {
        return payload?.error || null;
    }
    if (type === "agent:run:complete") {
        return payload?.finalStatus || null;
    }
    if (type === "agent:run:start") {
        return payload?.agentName || null;
    }
    return null;
}
export function isHeartbeatRunEventMessage(message) {
    if (!message)
        return false;
    if (message.type === "agent:run:delta") {
        const deltaType = message.payload?.type;
        return String(deltaType || "").toLowerCase() === "heartbeat";
    }
    if (message.type === "agent:run:status") {
        const state = message.payload?.data?.state || message.payload?.data?.status;
        return String(state || "").toLowerCase() === "heartbeat";
    }
    return false;
}
export function getVisibleDialogRegistry() {
    const registry = globalThis.__agentDialogVisibleCallbacks;
    return registry && typeof registry === "object" ? registry : {};
}
export function isAgentErrorStatusValue(status) {
    return status === "error";
}
//# sourceMappingURL=agentMessageHelpers.js.map