import { createBashToolDefinition, createEditToolDefinition, createFindToolDefinition, createGrepToolDefinition, createLsToolDefinition, createReadToolDefinition, createWriteToolDefinition, } from "@sheason/pi-coding-agent";
import { getErrorMessage } from "../../hub/utils.js";
function isPeerToolName(toolName) {
    return ["read", "write", "edit", "bash", "grep", "find", "ls"].includes(toolName);
}
export async function executePeerToolRequest(cwd, payload, socket) {
    if (!isPeerToolName(payload.toolName)) {
        socket.emit("tool:call_error", {
            toolCallId: payload.toolCallId,
            message: `Unsupported peer tool: ${payload.toolName}`,
        });
        return;
    }
    const definitions = {
        read: createReadToolDefinition(cwd),
        write: createWriteToolDefinition(cwd),
        edit: createEditToolDefinition(cwd),
        bash: createBashToolDefinition(cwd),
        grep: createGrepToolDefinition(cwd),
        find: createFindToolDefinition(cwd),
        ls: createLsToolDefinition(cwd),
    };
    const definition = definitions[payload.toolName];
    const executionContext = {};
    const abortController = new AbortController();
    const timeoutId = setTimeout(() => {
        abortController.abort();
    }, payload.timeoutMs);
    try {
        socket.emit("tool:call_ack", {
            toolCallId: payload.toolCallId,
        });
        const result = await definition.execute(payload.toolCallId, payload.args, abortController.signal, (partialResult) => {
            socket.emit("tool:call_update", {
                toolCallId: payload.toolCallId,
                partialResult,
            });
        }, executionContext);
        socket.emit("tool:call_result", {
            toolCallId: payload.toolCallId,
            result,
        });
    }
    catch (error) {
        socket.emit("tool:call_error", {
            toolCallId: payload.toolCallId,
            message: getErrorMessage(error),
        });
    }
    finally {
        clearTimeout(timeoutId);
    }
}
//# sourceMappingURL=execute-peer-tool.js.map