/*
 * 文件定位：Agent 执行引擎 / 输入输出收发层 / 输入接收面。
 * 核心目的：接收图像输入，并把图片、截图或视觉材料纳入一次 Agent 输入。
 * 能力要求1：需要区分原始图像、图像引用、视觉区域和上下文图片，不把它们直接压成文本。
 * 能力要求2：需要为后续视觉模型、工具调用或 PromptPack 上下文提供稳定输入结构。
 * 边界：只服务 agentCore 内核，不写上层产品逻辑。
 * 对接：需要被 runtime.execEngine 拉起，并和 mainLoop、stateEngine、事件暴露、工具调用策略接通。
 * 实现提示：先补稳定类型契约、最小可测行为和清晰错误边界，再接入真实执行逻辑。
 */
export const imageInputReceiverDescriptor = {
    modality: "image",
    route: "agent_executionEngine.IOTransceiver.inputReceiver",
    purpose: "preserve image material as visual input instead of flattening it into text",
    providerPayloadCreated: false,
    textFallbackCreated: false,
    unsafeSideEffects: false,
};
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, safeForRuntimeInspection: true },
        events: ["input.image.rejected"],
    };
}
function guardScopes(requestedScopes, allowedScopes) {
    const requested = cleanList(requestedScopes);
    const allowed = cleanList(allowedScopes);
    if (requested.length === 0 || allowed.length === 0) {
        return undefined;
    }
    const denied = requested.find((scope) => !allowed.includes(scope));
    if (denied) {
        return failure("SCOPE_DENIED", `image input scope ${denied} is outside runtime governance`, "scope");
    }
    return undefined;
}
function validDimensions(dimensions) {
    return (dimensions === undefined ||
        ((dimensions.width === undefined || (Number.isFinite(dimensions.width) && dimensions.width > 0)) &&
            (dimensions.height === undefined || (Number.isFinite(dimensions.height) && dimensions.height > 0))));
}
function validRegion(region) {
    return (Number.isFinite(region.x) &&
        Number.isFinite(region.y) &&
        Number.isFinite(region.width) &&
        Number.isFinite(region.height) &&
        region.width > 0 &&
        region.height > 0);
}
function normalizeImagePayload(payload) {
    if (payload.kind === "raw-image") {
        if (payload.bytes.length === 0 || isBlank(payload.format) || !validDimensions(payload.dimensions)) {
            return failure("INVALID_IMAGE_PAYLOAD", "raw image requires non-empty bytes, format, and valid dimensions", "input");
        }
        return {
            format: payload.format.trim(),
            byteLength: payload.bytes.length,
            dimensions: payload.dimensions,
        };
    }
    if (payload.kind === "image-reference") {
        if (isBlank(payload.uri) || !validDimensions(payload.dimensions)) {
            return failure("INVALID_IMAGE_PAYLOAD", "image reference requires a uri and valid dimensions", "input");
        }
        return {
            uri: payload.uri.trim(),
            format: payload.format?.trim() || undefined,
            dimensions: payload.dimensions,
        };
    }
    if (payload.kind === "visual-region") {
        if (isBlank(payload.imageId) || !validRegion(payload.region)) {
            return failure("INVALID_VISUAL_REGION", "visual region requires imageId and a positive rectangular region", "input");
        }
        return {
            imageId: payload.imageId.trim(),
            region: payload.region,
            label: payload.label?.trim() || undefined,
        };
    }
    if (isBlank(payload.contextId)) {
        return failure("INVALID_IMAGE_PAYLOAD", "context image requires contextId", "input");
    }
    return {
        contextId: payload.contextId.trim(),
        label: payload.label?.trim() || undefined,
        format: payload.format?.trim() || undefined,
    };
}
export function receiveImageInput(request) {
    if (request === undefined || isBlank(request.runtimeId)) {
        return failure("MISSING_RUNTIME_ID", "runtimeId is required before receiving image input", "input");
    }
    if (isBlank(request.sessionId)) {
        return failure("MISSING_SESSION_ID", "sessionId is required before receiving image input", "input");
    }
    if (request.runtimeReady === false) {
        return failure("RUNTIME_NOT_READY", "image input can only be accepted by a ready runtime", "runtime-state");
    }
    if (request.contract?.accepted === false) {
        return failure("CONTRACT_REJECTED", request.contract.reason ?? "image input was rejected by runtime contract surface", "contract");
    }
    if (request.governance?.accepted === false) {
        return failure("GOVERNANCE_REJECTED", request.governance.reason ?? "image input was rejected by runtime governance", "governance");
    }
    const scopeFailure = guardScopes(request.requestedScopes, request.allowedScopes);
    if (scopeFailure) {
        return scopeFailure;
    }
    if (request.payload === undefined) {
        return failure("MISSING_IMAGE_PAYLOAD", "image input requires raw, referenced, region, or context material", "input");
    }
    const visualMaterial = normalizeImagePayload(request.payload);
    if ("ok" in visualMaterial) {
        return visualMaterial;
    }
    const runtimeId = request.runtimeId?.trim() ?? "";
    const sessionId = request.sessionId?.trim() ?? "";
    return {
        ok: true,
        input: {
            kind: "image",
            runtimeId,
            sessionId,
            source: request.source ?? "user",
            payloadKind: request.payload.kind,
            visualMaterial,
            promptPackHandoff: "pending",
            textFallbackCreated: false,
            providerPayloadCreated: false,
            unsafeSideEffects: false,
        },
        events: ["input.image.received"],
    };
}
