import { createBashToolDefinition, createEditToolDefinition, createFindToolDefinition, createGrepToolDefinition, createLsToolDefinition, createReadToolDefinition, createWriteToolDefinition, } from "@sheason/pi-coding-agent";
import { HOST_PEER_ID } from "./host-peer.js";
import { createPeerToolSchema, preparePeerToolArguments, splitPeerToolArguments } from "./peer-tool-schema.js";
const PEER_TOOL_TIMEOUT_GRACE_MS = 5_000;
function resolveHostExecutorPolicy(policy) {
    return typeof policy === "function" ? policy() : policy;
}
function getPeerToolTimeoutMs(toolName, args) {
    if (toolName !== "bash") {
        return undefined;
    }
    const timeout = args.timeout;
    if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) {
        return undefined;
    }
    return Math.ceil(timeout * 1000) + PEER_TOOL_TIMEOUT_GRACE_MS;
}
function createPeerToolDefinition(baseTool, bridge, allowHostExecutor) {
    const initialAllowHostExecutor = resolveHostExecutorPolicy(allowHostExecutor);
    const parameters = createPeerToolSchema(baseTool.parameters);
    return {
        name: baseTool.name,
        label: baseTool.label,
        description: initialAllowHostExecutor
            ? `${baseTool.description} This tool is executed on the pi-hub host or a connected peer and requires an explicit peer-id.`
            : `${baseTool.description} This tool is executed on a connected peer and requires an explicit peer-id.`,
        promptSnippet: baseTool.promptSnippet,
        promptGuidelines: [
            ...(baseTool.promptGuidelines ?? []),
            initialAllowHostExecutor
                ? 'Provide peer-id explicitly for this tool call. Use "host" to execute on the pi-hub host workspace.'
                : "Provide peer-id explicitly for this tool call. The pi-hub host executor is disabled for this agent.",
            "Use group first when you do not know which peer-id is available.",
        ],
        parameters,
        renderShell: baseTool.renderShell,
        executionMode: baseTool.executionMode,
        prepareArguments: (args) => preparePeerToolArguments(args, baseTool.prepareArguments),
        async execute(toolCallId, params, signal, onUpdate, ctx) {
            const { peerId, toolArgs } = splitPeerToolArguments(params);
            if (peerId === HOST_PEER_ID) {
                if (!resolveHostExecutorPolicy(allowHostExecutor)) {
                    throw new Error("Hub Executor is disabled for this agent.");
                }
                return baseTool.execute(toolCallId, toolArgs, signal, onUpdate, ctx);
            }
            return bridge.executeTool({
                toolCallId,
                toolName: baseTool.name,
                peerId,
                args: toolArgs,
                signal,
                onUpdate,
                timeoutMs: getPeerToolTimeoutMs(baseTool.name, toolArgs),
            });
        },
        renderCall: baseTool.renderCall,
        renderResult: baseTool.renderResult,
    };
}
export { HOST_PEER_ID } from "./host-peer.js";
export function createPeerToolDefinitions(cwd, bridge, options = {}) {
    const allowHostExecutor = options.allowHostExecutor ?? true;
    return [
        createPeerToolDefinition(createReadToolDefinition(cwd), bridge, allowHostExecutor),
        createPeerToolDefinition(createWriteToolDefinition(cwd), bridge, allowHostExecutor),
        createPeerToolDefinition(createEditToolDefinition(cwd), bridge, allowHostExecutor),
        createPeerToolDefinition(createBashToolDefinition(cwd), bridge, allowHostExecutor),
        createPeerToolDefinition(createGrepToolDefinition(cwd), bridge, allowHostExecutor),
        createPeerToolDefinition(createFindToolDefinition(cwd), bridge, allowHostExecutor),
        createPeerToolDefinition(createLsToolDefinition(cwd), bridge, allowHostExecutor),
    ];
}
//# sourceMappingURL=peer-tools.js.map