import { extractAssistantText } from "./messages.js";
export function normalizeOneLine(text) {
    return text.replace(/\s+/g, " ").trim();
}
export function getToolResultFirstLine(toolResult) {
    const text = toolResult.content
        .filter((c) => c.type === "text")
        .map((c) => c.text)
        .join("\n")
        .trim();
    return normalizeOneLine(text.split("\n")[0] ?? "");
}
export function formatToolUiEventForProgress(uiEvent) {
    switch (uiEvent.type) {
        case "bash_started":
            return `bash running: ${uiEvent.command.replace(/\n/g, " ")}`;
        case "bash_execution":
            return uiEvent.exitCode !== null && uiEvent.exitCode !== 0
                ? `bash failed: $ ${uiEvent.command.replace(/\n/g, " ")} (exit ${uiEvent.exitCode})`
                : undefined;
        case "bash_blocked":
            return `bash blocked: $ ${uiEvent.command.replace(/\n/g, " ")} (${normalizeOneLine(uiEvent.reason)})`;
        case "bash_aborted":
            return `bash ${uiEvent.reason}: $ ${uiEvent.command.replace(/\n/g, " ")}`;
        case "web_search_started":
            return `web search: ${uiEvent.objective}`;
        case "web_search_finished":
            return uiEvent.status === "error" ? `web search failed: ? ${uiEvent.objective}` : undefined;
        case "web_fetch_started":
            return `web fetch: ${uiEvent.url}`;
        case "web_fetch_finished":
            return uiEvent.status === "error" ? `web fetch failed: ? ${uiEvent.url}` : undefined;
        case "write_success":
            return `write: ${uiEvent.path}`;
        case "write_blocked":
            return `tool blocked: write ${uiEvent.path} (${normalizeOneLine(uiEvent.reason)})`;
        case "edit_success":
            return `edit: ${uiEvent.path}`;
        case "edit_blocked":
            return `tool blocked: edit ${uiEvent.path} (${normalizeOneLine(uiEvent.reason)})`;
        case "view_image_success":
            return `view image: ${uiEvent.path}`;
        case "view_image_blocked":
            return `tool blocked: view image ${uiEvent.path} (${normalizeOneLine(uiEvent.reason)})`;
        default:
            return undefined;
    }
}
export function extractAssistantTextForProgress(message) {
    const text = extractAssistantText(message).trim();
    const firstLine = text.split("\n")[0];
    return firstLine ? `agent: ${firstLine}` : undefined;
}
export class AsyncUiEventQueue {
    buffered = [];
    waiting = [];
    closed = false;
    push(event) {
        if (this.closed)
            return;
        const next = this.waiting.shift();
        if (next) {
            next({ value: event, done: false });
            return;
        }
        this.buffered.push(event);
    }
    close() {
        if (this.closed)
            return;
        this.closed = true;
        for (const resolve of this.waiting.splice(0)) {
            resolve({ value: undefined, done: true });
        }
    }
    [Symbol.asyncIterator]() {
        return {
            next: () => {
                if (this.buffered.length > 0) {
                    return Promise.resolve({ value: this.buffered.shift(), done: false });
                }
                if (this.closed) {
                    return Promise.resolve({ value: undefined, done: true });
                }
                return new Promise((resolve) => this.waiting.push(resolve));
            },
        };
    }
}
//# sourceMappingURL=subagent_utils.js.map