import { sanitizeText } from "../../shared/index.js";
export function isActiveBackgroundStatus(status) {
    return status === "running";
}
export function isTerminalBackgroundStatus(status) {
    return status === "completed" || status === "failed" || status === "cancelled";
}
export function formatBackgroundTaskLine(task) {
    const owner = task.target_agent ?? task.kind;
    const session = task.session_id ? ` session:${task.session_id}` : "";
    const stale = task.stale_at ? ` stale:${task.stale_reason ?? task.stale_at}` : "";
    const latest = task.last_message ? ` latest:${task.last_message.slice(0, 160)}` : "";
    return `- ${task.background_id} ${owner} ${task.status}${session}${stale}${latest}`;
}
export function formatBackgroundReminder(tasks) {
    const lines = tasks.map(formatBackgroundTaskLine);
    const failed = tasks.filter((task) => task.status === "failed");
    const action = failed.length
        ? `\n\nACTION REQUIRED: ${failed.length} background task(s) failed. Read output, then retry, cancel, or report the blocker.`
        : "";
    return `<system-reminder>
[SEED background update]
${lines.join("\n")}

Use /seed-status for the run summary or seed_background_output for full child-session output.${action}
</system-reminder>`;
}
export function formatBackgroundNotificationReminder(notifications) {
    const lines = notifications.map(formatBackgroundNotificationLine);
    const failed = notifications.filter((notification) => notification.severity === "error");
    const action = failed.length
        ? `\n\nACTION REQUIRED: ${failed.length} background task notification(s) failed. Read output, then retry, cancel, or report the blocker.`
        : "";
    return `<system-reminder>
[SEED background update]
${lines.join("\n")}

Use /seed-status for the run summary, seed_background_notifications for pending parent notices, or seed_background_output for full child-session output.${action}
</system-reminder>`;
}
export function formatFullSessionOutput(messages, options) {
    const sinceIndex = options.since_message_id
        ? messages.findIndex((message) => messageId(message) === options.since_message_id)
        : -1;
    const sliced = messages.slice(sinceIndex >= 0 ? sinceIndex + 1 : 0);
    const limit = Math.max(1, Math.min(options.message_limit ?? 80, 100));
    const selected = sliced.slice(-limit);
    const body = selected
        .map((message) => formatSessionMessage(message, options))
        .filter(Boolean)
        .join("\n\n---\n\n");
    return sanitizeText(body || "(No session messages matched the requested filters.)");
}
function formatSessionMessage(message, options) {
    const id = messageId(message) ?? "unknown-message";
    const role = message.info?.role ?? message.role ?? "unknown";
    const chunks = messageTextParts(message, options);
    if (!chunks.length)
        return "";
    return `### ${role} ${id}\n\n${chunks.join("\n\n")}`;
}
function messageId(message) {
    return message.info?.id ?? message.id;
}
function messageTextParts(message, options) {
    const chunks = [];
    for (const part of message.parts ?? []) {
        if (part.type === "text" && typeof part.text === "string") {
            chunks.push(part.text);
            continue;
        }
        if (part.type === "reasoning" && options.include_thinking === true && typeof part.text === "string") {
            chunks.push(part.text);
            continue;
        }
        if ((part.type === "tool" || part.type === "tool_result") && options.include_tool_results === true) {
            const text = partContentText(part.content);
            if (text)
                chunks.push(text);
        }
    }
    return chunks.map((chunk) => sanitizeText(chunk).slice(0, 8000)).filter(Boolean);
}
function partContentText(content) {
    if (typeof content === "string")
        return content;
    if (Array.isArray(content)) {
        return content.map(partContentText).filter(Boolean).join("\n");
    }
    if (content && typeof content === "object" && "text" in content && typeof content.text === "string") {
        return content.text;
    }
    return "";
}
function formatBackgroundNotificationLine(notification) {
    const owner = notification.target_agent ?? "background";
    const session = notification.session_id ? ` session:${notification.session_id}` : "";
    const action = notification.next_action === "review_output" ? "review output" : "retry or hand off";
    return `- ${notification.background_id} ${owner} ${notification.background_status}${session}; ${action}: ${notification.title}`;
}
//# sourceMappingURL=manager.js.map