/** * PairingService - Manages secure DM access via pairing codes. * * State contracts live in quint/PairingIdentity.qnt and quint/PairingAdmission.qnt. * This service handles the pairing workflow for messaging channels: * 1. Unknown users send a DM and receive a pairing code * 2. Bot owner approves the code via CLI or API * 3. User is added to the allowlist and can now send DMs */ import { type ApprovePairingParams, type ApprovePairingResult, type PairingAllowlistEntry, type PairingChannel, type PairingConfig, type PairingPage, type PairingPageOptions, type PairingRequest, type UpsertPairingRequestParams, type UpsertPairingRequestResult } from "../types/pairing.js"; import type { IAgentRuntime } from "../types/runtime.js"; import { Service } from "../types/service.js"; /** * PairingService handles secure DM pairing for messaging channels. * * When a user sends a DM to a bot with dmPolicy="pairing": * 1. If not in allowlist, a pairing request is created with a code * 2. The code is sent back to the user * 3. The bot owner approves the code via CLI/API * 4. The user is added to the allowlist */ export declare class PairingService extends Service { static serviceType: "pairing"; capabilityDescription: string; private pairingConfig; /** * Last-issued pairing-reply timestamps keyed by the channel/sender tuple. * Reply suppression is deliberately decoupled from request-row existence: * eviction, expiry cleanup, or manual deletion of the requests table must * not re-arm an unsolicited pairing reply for a sender who was already * answered within the TTL. */ private readonly pairingReplyClaims; /** * Serializes the read-check-create portion of request admission in this * runtime. Without this queue, concurrent first messages can all observe a * free slot and overrun `maxPendingRequests` before any create is visible. */ private pairingRequestAdmissionTail; /** Bound on retained reply claims before expired entries are swept. */ private static readonly MAX_PAIRING_REPLY_CLAIMS; constructor(runtime: IAgentRuntime, config?: PairingConfig); /** * Start the PairingService with the given runtime. */ static start(runtime: IAgentRuntime): Promise; /** * Stop the PairingService. */ stop(): Promise; /** * Generate a random pairing code. * Uses a human-friendly alphabet that excludes ambiguous characters. * * Entropy comes from the platform CSPRNG, never `Math.random()`: a * predictable code would let an attacker forecast a victim's pending * pairing code and socially engineer the owner into approving the wrong * sender. Bytes are rejection-sampled so every alphabet index is equally * likely (no modulo bias). */ private generateCode; /** * Generate a unique code that doesn't conflict with existing codes. */ private generateUniqueCode; private isExpiredAt; private requestExpiryCutoff; private cleanupExpiredRequests; private newestFirst; private pageInfo; /** * List all pending pairing requests for a channel. * Expired requests are automatically filtered out. */ listPendingRequests(channel: PairingChannel): Promise; /** Lists pending pairing requests newest first, carrying page bounds into supporting adapters. */ listPendingRequestsPage(channel: PairingChannel, options?: PairingPageOptions): Promise>; /** * Claim the one pairing reply available to a sender per request TTL. * * Returns true at most once per `requestTtlMs` window per * (channel, senderId), no matter how often the sender's request row is * deleted and recreated in between. This keeps an attacker cycling * identities on one channel from turning request churn into a sustained * stream of unsolicited pairing replies from the operator's account. */ claimPairingReply(channel: PairingChannel, senderId: string): boolean; /** * Create or update a pairing request for a sender. * If the sender already has a pending request, returns the existing code. * If too many pending requests exist, returns empty code. */ upsertRequest(params: UpsertPairingRequestParams): Promise; private upsertRequestSerial; /** * Approve a pairing code and add the sender to the allowlist. * Returns null if the code is not found or expired. */ approveCode(params: ApprovePairingParams): Promise; /** * Get the allowlist for a channel. */ getAllowlist(channel: PairingChannel): Promise; /** Get one bounded page of allowlist entries, newest first. */ getAllowlistPage(channel: PairingChannel, options?: PairingPageOptions): Promise>; /** * Check if a sender is in the allowlist. */ isAllowed(channel: PairingChannel, senderId: string): Promise; /** * Add a sender directly to the allowlist (bypass pairing). */ addToAllowlist(channel: PairingChannel, senderId: string, metadata?: Record): Promise; /** * Remove a sender from the allowlist. */ removeFromAllowlist(channel: PairingChannel, senderId: string): Promise; }