import { optionalRecordField, optionalStringArrayField, optionalStringField, requireBooleanField, requireLiteralField, requireNumberField, requireRecordField, requireShellGenerationExecutorOutput, requireStringArrayField, requireStringField, shellGenerationInputEnvelope, withShellGenerationProviderFlags, } from "../_shared/hostExecutorProvider.js";
export const shellScriptGenerationDependencyDeclarations = [
    {
        dependencyId: "runtime.generationPlane.shellScriptPolicy",
        kind: "permission",
        required: true,
        description: "Runtime permission hint allowing guarded shell script generation without executing a process",
    },
    {
        dependencyId: "runtime.generationPlane.shellScriptProvider",
        kind: "runtime",
        required: false,
        description: "Optional runtime provider exposed through BaseToolExecutorPort.shell.generateScript",
    },
];
function normalizeShellScriptTarget(output) {
    const environment = optionalRecordField(output, "environment");
    const normalizedEnvironment = {};
    if (environment !== undefined) {
        for (const [key, value] of Object.entries(environment)) {
            if (typeof value !== "string") {
                throw new Error("Shell generation executor returned invalid script environment");
            }
            normalizedEnvironment[key] = value;
        }
    }
    return {
        scriptName: optionalStringField(output, "scriptName"),
        shell: requireLiteralField(output, "shell", ["sh", "bash", "zsh"]),
        workingDirectory: optionalStringField(output, "workingDirectory"),
        commands: requireStringArrayField(output, "commands"),
        environment: environment === undefined ? undefined : normalizedEnvironment,
        notes: optionalStringArrayField(output, "notes"),
    };
}
export function normalizeShellScriptGenerationOutput(output) {
    const flagged = withShellGenerationProviderFlags(output);
    if (!requireBooleanField(flagged, "executionBlocked") || requireBooleanField(flagged, "unsafeSideEffects")) {
        throw new Error("Shell generation executor returned unsafe script generation flags");
    }
    return {
        kind: requireLiteralField(flagged, "kind", ["agentCore.basicTool.shell.scriptGeneration"]),
        target: normalizeShellScriptTarget(requireRecordField(flagged, "target")),
        script: requireStringField(flagged, "script", { nonEmpty: true }),
        commandPreview: requireStringArrayField(flagged, "commandPreview"),
        lineCount: requireNumberField(flagged, "lineCount"),
        requiredPermission: requireLiteralField(flagged, "requiredPermission", ["shell:script:generate"]),
        requiresTapApproval: requireBooleanField(flagged, "requiresTapApproval"),
        dryRun: requireBooleanField(flagged, "dryRun"),
        providerCalled: requireBooleanField(flagged, "providerCalled"),
        executionBlocked: true,
        unsafeSideEffects: false,
    };
}
export function createHostExecutorShellScriptGenerationProvider(executor) {
    const generateScript = executor?.shell?.generateScript;
    if (generateScript === undefined) {
        return undefined;
    }
    return async (request) => {
        const result = await generateScript(shellGenerationInputEnvelope(request));
        const output = requireShellGenerationExecutorOutput(result);
        return {
            ok: true,
            toolId: "shell.scriptGeneration",
            output: normalizeShellScriptGenerationOutput(output),
            audit: [],
            events: ["basicTool.shell.scriptGeneration.providerCalled"],
        };
    };
}
