import { AvatarDebugProbeKind, } from './generated/runtime/v1/agent_service.js';
const AVATAR_DEBUG_READ_SCOPE = 'runtime.agent.avatar_debug.read';
const AVATAR_DEBUG_WRITE_SCOPE = 'runtime.agent.avatar_debug.write';
function optionalString(value) {
    return typeof value === 'string' && value.trim() ? value.trim() : undefined;
}
function toScopedBindingAttachment(input, defaults) {
    const bindingId = optionalString(input?.bindingId);
    if (!bindingId) {
        return undefined;
    }
    return {
        bindingId,
        bindingHandle: optionalString(input?.bindingHandle) || '',
        runtimeAppId: optionalString(input?.runtimeAppId) || defaults.runtimeAppId,
        appInstanceId: optionalString(input?.appInstanceId) || '',
        windowId: optionalString(input?.windowId) || '',
        avatarInstanceId: optionalString(input?.avatarInstanceId) || '',
        agentId: optionalString(input?.localAgentRef) || optionalString(defaults.localAgentRef) || '',
        conversationAnchorId: optionalString(input?.conversationAnchorId) || optionalString(defaults.conversationAnchorId) || '',
        worldId: optionalString(input?.worldId) || '',
    };
}
function requireLocalAgentIdentity(request) {
    if (optionalString(request.agentId)) {
        throw new Error('runtime avatar debug request must use localAgentRef, not agentId');
    }
    const ownerUserId = optionalString(request.ownerUserId);
    const realmAgentId = optionalString(request.realmAgentId);
    const localAgentRef = optionalString(request.localAgentRef);
    if (!ownerUserId || !realmAgentId || !localAgentRef) {
        throw new Error('runtime avatar debug request requires ownerUserId, realmAgentId, and localAgentRef');
    }
    if (!localAgentRef.startsWith('local-agent:')) {
        throw new Error('runtime avatar debug request localAgentRef is malformed');
    }
    if (localAgentRef !== `local-agent:${ownerUserId}:${realmAgentId}`) {
        throw new Error('runtime avatar debug request localAgentRef must match ownerUserId and realmAgentId');
    }
    return { ownerUserId, realmAgentId, localAgentRef };
}
async function contextForAvatarDebug(input) {
    const identity = requireLocalAgentIdentity(input.request);
    const scopedBinding = toScopedBindingAttachment(input.request.scopedBinding, {
        runtimeAppId: input.appId,
        localAgentRef: identity.localAgentRef,
        conversationAnchorId: input.request.conversationAnchorId,
    });
    if (scopedBinding) {
        return {
            appId: input.appId,
            subjectUserId: '',
            ownerUserId: identity.ownerUserId,
            realmAgentId: identity.realmAgentId,
            localAgentRef: identity.localAgentRef,
            scopedBinding,
        };
    }
    return {
        appId: input.appId,
        subjectUserId: await input.resolveSubjectUserId(input.request.subjectUserId || identity.ownerUserId),
        ownerUserId: identity.ownerUserId,
        realmAgentId: identity.realmAgentId,
        localAgentRef: identity.localAgentRef,
    };
}
export function createRuntimeAvatarDebugModule(input) {
    return {
        async snapshot(request, options) {
            const context = await contextForAvatarDebug({
                appId: input.appId,
                request,
                resolveSubjectUserId: input.resolveSubjectUserId,
            });
            const callOptions = await input.protectedAccess.getCallOptions([AVATAR_DEBUG_READ_SCOPE], options);
            return input.agent.getAvatarDebugSnapshot({
                context,
                agentId: context.localAgentRef,
                conversationAnchorId: request.conversationAnchorId,
            }, callOptions);
        },
        async requestProbe(request, options) {
            const context = await contextForAvatarDebug({
                appId: input.appId,
                request,
                resolveSubjectUserId: input.resolveSubjectUserId,
            });
            const callOptions = await input.protectedAccess.getCallOptions([AVATAR_DEBUG_WRITE_SCOPE], options);
            return input.agent.requestAvatarDebugProbe({
                context,
                agentId: context.localAgentRef,
                conversationAnchorId: request.conversationAnchorId,
                probeKind: request.probeKind,
                requestedBy: request.requestedBy,
                probeId: optionalString(request.probeId) || '',
                turnId: optionalString(request.turnId) || '',
                streamId: optionalString(request.streamId) || '',
                avatarInstanceId: optionalString(request.avatarInstanceId) || '',
                replayRequested: Boolean(request.replayRequested),
            }, callOptions);
        },
        async listProbeResults(request, options) {
            const context = await contextForAvatarDebug({
                appId: input.appId,
                request,
                resolveSubjectUserId: input.resolveSubjectUserId,
            });
            const callOptions = await input.protectedAccess.getCallOptions([AVATAR_DEBUG_READ_SCOPE], options);
            return input.agent.listAvatarDebugProbeResults({
                context,
                agentId: context.localAgentRef,
                conversationAnchorId: request.conversationAnchorId,
                probeKind: request.probeKind ?? AvatarDebugProbeKind.UNSPECIFIED,
            }, callOptions);
        },
        async getReplay(request, options) {
            const context = await contextForAvatarDebug({
                appId: input.appId,
                request,
                resolveSubjectUserId: input.resolveSubjectUserId,
            });
            const callOptions = await input.protectedAccess.getCallOptions([AVATAR_DEBUG_READ_SCOPE], options);
            return input.agent.getAvatarDebugReplay({
                context,
                agentId: context.localAgentRef,
                conversationAnchorId: request.conversationAnchorId,
                probeId: request.probeId,
            }, callOptions);
        },
    };
}
//# sourceMappingURL=runtime-avatar-debug.js.map