import { optionalStringField, requireBooleanField, requireLiteralField, requireShellGenerationExecutorOutput, requireStringArrayField, requireStringField, shellGenerationInputEnvelope, withShellGenerationProviderFlags, } from "../_shared/hostExecutorProvider.js";
export const shellCommandGenerationDependencyDeclarations = [
    {
        dependencyId: "runtime.generationPlane.shellCommandPolicy",
        kind: "permission",
        required: true,
        description: "Runtime permission hint allowing shell command-line generation without executing a process",
    },
    {
        dependencyId: "runtime.generationPlane.shellCommandProvider",
        kind: "runtime",
        required: false,
        description: "Optional runtime provider exposed through BaseToolExecutorPort.shell.generateCommand",
    },
];
export function normalizeShellCommandGenerationOutput(output) {
    const flagged = withShellGenerationProviderFlags(output);
    if (!requireBooleanField(flagged, "executionBlocked") || requireBooleanField(flagged, "unsafeSideEffects")) {
        throw new Error("Shell generation executor returned unsafe command generation flags");
    }
    return {
        kind: requireLiteralField(flagged, "kind", ["agentCore.basicTool.shell.commandGeneration"]),
        shell: requireLiteralField(flagged, "shell", ["sh", "bash", "zsh"]),
        commandLine: requireStringField(flagged, "commandLine", { nonEmpty: true }),
        argv: requireStringArrayField(flagged, "argv"),
        executable: requireStringField(flagged, "executable", { nonEmpty: true }),
        workingDirectory: optionalStringField(flagged, "workingDirectory"),
        environmentKeys: requireStringArrayField(flagged, "environmentKeys"),
        requiredPermission: requireLiteralField(flagged, "requiredPermission", ["shell:generate"]),
        dryRun: requireBooleanField(flagged, "dryRun"),
        providerCalled: requireBooleanField(flagged, "providerCalled"),
        executionBlocked: true,
        unsafeSideEffects: false,
    };
}
export function createHostExecutorShellCommandGenerationProvider(executor) {
    const generateCommand = executor?.shell?.generateCommand;
    if (generateCommand === undefined) {
        return undefined;
    }
    return async (request) => {
        const result = await generateCommand(shellGenerationInputEnvelope(request));
        const output = requireShellGenerationExecutorOutput(result);
        return {
            ok: true,
            toolId: "shell.commandGeneration",
            output: normalizeShellCommandGenerationOutput(output),
            audit: [],
            events: ["basicTool.shell.commandGeneration.providerCalled"],
        };
    };
}
