import { requireBooleanField, requireLiteralField, requireRecordArrayField, requireShellGenerationExecutorOutput, requireStringArrayField, requireStringField, shellGenerationInputEnvelope, withShellGenerationProviderFlags, } from "../_shared/hostExecutorProvider.js";
export const shellArgumentAssemblyDependencyDeclarations = [
    {
        dependencyId: "runtime.generationPlane.shellArgumentPolicy",
        kind: "permission",
        required: true,
        description: "Runtime permission hint allowing shell argv generation without executing a process",
    },
    {
        dependencyId: "runtime.generationPlane.shellArgumentProvider",
        kind: "runtime",
        required: false,
        description: "Optional runtime provider exposed through BaseToolExecutorPort.shell.assembleArguments",
    },
];
export function normalizeShellArgumentAssemblyOutput(output) {
    const flagged = withShellGenerationProviderFlags(output);
    if (!requireBooleanField(flagged, "executionBlocked") || requireBooleanField(flagged, "unsafeSideEffects")) {
        throw new Error("Shell generation executor returned unsafe argument assembly flags");
    }
    return {
        kind: requireLiteralField(flagged, "kind", ["agentCore.basicTool.shell.argumentAssembly"]),
        executable: requireStringField(flagged, "executable", { nonEmpty: true }),
        argv: requireStringArrayField(flagged, "argv"),
        renderedTokens: requireRecordArrayField(flagged, "renderedTokens").map((token) => ({
            raw: requireStringField(token, "raw"),
            rendered: requireStringField(token, "rendered"),
            sensitive: requireBooleanField(token, "sensitive"),
        })),
        redactedPreview: requireStringArrayField(flagged, "redactedPreview"),
        requiredPermission: requireLiteralField(flagged, "requiredPermission", ["shell:generate"]),
        dryRun: requireBooleanField(flagged, "dryRun"),
        providerCalled: requireBooleanField(flagged, "providerCalled"),
        executionBlocked: true,
        unsafeSideEffects: false,
    };
}
export function createHostExecutorShellArgumentAssemblyProvider(executor) {
    const assembleArguments = executor?.shell?.assembleArguments;
    if (assembleArguments === undefined) {
        return undefined;
    }
    return async (request) => {
        const result = await assembleArguments(shellGenerationInputEnvelope(request));
        const output = requireShellGenerationExecutorOutput(result);
        return {
            ok: true,
            toolId: "shell.argumentAssembly",
            output: normalizeShellArgumentAssemblyOutput(output),
            audit: [],
            events: ["basicTool.shell.argumentAssembly.providerCalled"],
        };
    };
}
