/*
 * 文件定位：Agent 运行态实现层 / 应用承托面。
 * 核心目的：创建应用侧 runtime session，用来隔离会话、上下文和调用状态。
 * 能力要求1：需要支持一个应用挂多个 Agent 或同一 Agent 多会话的情况。
 * 能力要求2：它不等于记忆系统，只负责 runtime 层会话边界。
 * 边界：承托和治理运行态，不吞并执行引擎、模型适配器或官方模块内部实现。
 * 对接：需要服务 applicationSurface、officialModuleSurface、governancePlane、invocationMethod 和 inspection/debug 等运行面。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
function isBlank(value) {
    return typeof value !== "string" || value.trim().length === 0;
}
function cleanList(values) {
    return [...new Set((values ?? []).map((value) => value.trim()).filter(Boolean))];
}
function failure(code, message, boundary) {
    return {
        ok: false,
        error: { code, message, boundary },
        events: ["runtime.session.rejected"],
    };
}
export function createAgentRuntimeSession(request) {
    if (isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtimeId is required before creating a runtime session", "input");
    }
    if (isBlank(request.applicationId)) {
        return failure("MISSING_APPLICATION_ID", "applicationId is required before creating a runtime session", "input");
    }
    if (isBlank(request.agentId)) {
        return failure("MISSING_AGENT_ID", "agentId is required before creating a runtime session", "input");
    }
    if (request.runtimeReady === false) {
        return failure("RUNTIME_NOT_READY", "runtime session requires a ready runtime", "runtime-state");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "runtime session request was rejected by contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "runtime session request was rejected by governance", "governance");
    }
    const runtimeId = request.runtimeId.trim();
    const applicationId = request.applicationId.trim();
    const agentId = request.agentId.trim();
    const sessionKey = request.sessionKey?.trim() || "default";
    return {
        ok: true,
        session: {
            sessionId: `${runtimeId}:${applicationId}:${agentId}:${sessionKey}`,
            runtimeId,
            applicationId,
            agentId,
            sessionKey,
            contextKeys: cleanList(request.initialContextKeys),
            callState: request.callState ?? "idle",
            isolation: "runtime-session",
            unsafeSideEffects: false,
        },
        events: ["runtime.session.created"],
    };
}
