import { jsx as _jsx } from "react/jsx-runtime";
import BashProgress from '../components/bash-progress.js';
import { generateKey } from '../session/key-generator.js';
import { executeBashCommand, formatBashResultForLLM } from '../tools/execute-bash.js';
import { parseToolArguments } from '../utils/tool-args-parser.js';
import { formatValidationError } from '../utils/tool-validation.js';
/**
 * Run an `execute_bash` tool call through the streaming executor: validate the
 * args (the streaming executor bypasses the validated registry handler, so the
 * per-tool validator must run here), mount a live `BashProgress` so output shows
 * while the command runs, await completion, then clear the live component.
 *
 * Used by the unified tool-execution routine in `tool-executor` for both
 * auto-executed and user-approved bash calls, so the live bash rendering
 * behaves identically regardless of how the tool was approved. Callers own how
 * the completed state is displayed (inline vs. deferred tally).
 */
export async function runStreamingBashTool(toolCall, toolManager, setLiveComponent, keyPrefix, signal) {
    const parsedArgs = parseToolArguments(toolCall.function.arguments);
    const validator = toolManager?.getToolValidator(toolCall.function.name);
    const validation = validator
        ? await validator(parsedArgs)
        : { valid: true };
    if (!validation.valid) {
        setLiveComponent(null);
        return {
            toolCall,
            result: {
                tool_call_id: toolCall.id,
                role: 'tool',
                name: toolCall.function.name,
                content: formatValidationError(validation.error, validation.details),
            },
        };
    }
    const commandStr = parsedArgs.command;
    const { executionId, promise } = executeBashCommand(commandStr, { signal });
    setLiveComponent(_jsx(BashProgress, { executionId: executionId, command: commandStr, isLive: true }, generateKey(`${keyPrefix}-${toolCall.id}`)));
    const bashState = await promise;
    setLiveComponent(null);
    return {
        toolCall,
        result: {
            tool_call_id: toolCall.id,
            role: 'tool',
            name: toolCall.function.name,
            content: formatBashResultForLLM(bashState),
        },
        bashState,
    };
}
//# sourceMappingURL=streaming-bash-tool.js.map