import { optionalRecordField, optionalStringField, requireBooleanField, requireLiteralField, requireShellGenerationExecutorOutput, requireStringArrayField, requireStringField, shellGenerationInputEnvelope, withShellGenerationProviderFlags, } from "../_shared/hostExecutorProvider.js";
export const shellInvocationConstructionDependencyDeclarations = [
    {
        dependencyId: "runtime.generationPlane.shellInvocationEnvelope",
        kind: "runtime",
        required: true,
        description: "Runtime-visible invocation envelope created from generated command and execution guard material",
    },
    {
        dependencyId: "runtime.generationPlane.shellInvocationProvider",
        kind: "runtime",
        required: false,
        description: "Optional runtime provider exposed through BaseToolExecutorPort.shell.constructInvocation",
    },
];
export function normalizeShellInvocationEnvelope(output) {
    const flagged = withShellGenerationProviderFlags(output);
    if (!requireBooleanField(flagged, "executionBlocked") || requireBooleanField(flagged, "unsafeSideEffects")) {
        throw new Error("Shell generation executor returned unsafe invocation construction flags");
    }
    return {
        kind: requireLiteralField(flagged, "kind", ["agentCore.basicTool.shell.invocation"]),
        invocationId: requireStringField(flagged, "invocationId", { nonEmpty: true }),
        runtimeId: optionalStringField(flagged, "runtimeId"),
        sessionId: optionalStringField(flagged, "sessionId"),
        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"),
        guardVerdict: requireLiteralField(flagged, "guardVerdict", ["allowed", "requires-approval", "blocked"]),
        approvalRequired: requireBooleanField(flagged, "approvalRequired"),
        status: requireLiteralField(flagged, "status", ["planned", "pending-approval"]),
        metadata: optionalRecordField(flagged, "metadata") ?? {},
        dryRun: requireBooleanField(flagged, "dryRun"),
        providerCalled: requireBooleanField(flagged, "providerCalled"),
        executionBlocked: true,
        unsafeSideEffects: false,
    };
}
export function createHostExecutorShellInvocationConstructionProvider(executor) {
    const constructInvocation = executor?.shell?.constructInvocation;
    if (constructInvocation === undefined) {
        return undefined;
    }
    return async (request) => {
        const result = await constructInvocation(shellGenerationInputEnvelope(request));
        const output = requireShellGenerationExecutorOutput(result);
        return {
            ok: true,
            toolId: "shell.invocationConstruction",
            invocation: normalizeShellInvocationEnvelope(output),
            audit: [],
            events: ["basicTool.shell.invocationConstruction.providerCalled"],
        };
    };
}
