/*
 * 文件定位：Agent 运行态实现层 / 应用承托面。
 * 核心目的：提供客户端式调用 API，让应用更方便地使用 runtime。
 * 能力要求1：需要封装 invoke、stream、inspect、subscribe、control 等常见操作。
 * 能力要求2：它面向开发者体验，而不是内部模块间细节。
 * 边界：承托和治理运行态，不吞并执行引擎、模型适配器或官方模块内部实现。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
const defaultClientOperations = ["invoke", "stream", "inspect", "subscribe", "control"];
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
        events: ["runtime.client.rejected"],
    };
}
function callFailure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
    };
}
function cleanOperations(values) {
    return [...new Set(values ?? defaultClientOperations)];
}
export function createAgentRuntimeClient(request) {
    if (request.surface === undefined) {
        return failure("MISSING_RUNTIME_SURFACE", "runtime client requires an application runtime surface", "input");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "runtime client creation was rejected by governance", "governance");
    }
    const enabledOperations = cleanOperations(request.enabledOperations);
    const { runtimeId, applicationId } = request.surface;
    return {
        ok: true,
        client: {
            runtimeId,
            applicationId,
            enabledOperations,
            call(call) {
                if (!enabledOperations.includes(call.operation)) {
                    return callFailure("UNSUPPORTED_OPERATION", `operation ${call.operation} is not enabled for this runtime client`, "input");
                }
                return {
                    ok: true,
                    operation: call.operation,
                    runtimeId,
                    applicationId,
                    accepted: true,
                    dryRun: true,
                };
            },
        },
        events: ["runtime.client.ready"],
    };
}
