import { type Database } from "./database.js"; import { type DisconnectAuditActor } from "./disconnect-audit.js"; export type TeamsInstallation = { tenant_id: string; account_id: string; workspace_id: string; generation: string; active: boolean; group_revision: number; }; export type TeamsGroupAudience = Array<{ actorId: string; subjectId: string; }>; export type TeamsRoute = TeamsInstallation & { group_audience: TeamsGroupAudience | null; group_active: boolean; route_id: string; actor_id: string; subject_id: string; personal: boolean; channel_id: string | null; conversation_id: string; thread_id: string | null; service_url: string; bot_id: string; session_id: string; /** Where a 1:1 route's session lives when not in the installation's workspace: * the linked person's own Personal workspace, or (with workspace routing) a * workspace of the organization they can start work in. Null keeps the * installation's. */ session_workspace_id: string | null; /** The agent a 1:1 route's chat runs as, or null for the plain assistant. */ agent_id?: string | null; session_bound: boolean; delivered_sequence: number; /** The latest reply, request for input or failure event in the route's session. */ relevant_event_sequence: number; lease_token: string | null; }; export type TeamsInbox = { event_id: string; route_id: string; actor_id: string; subject_id: string; payload: Record; }; export type TeamsOutbox = { source_sequence: number; operation_id: string; payload: { text: string; notice?: "admission" | "audience_changed"; /** An agent reply: shown with Teams' AI label and feedback buttons. */ ai?: true; /** The turn an agent reply came from, for feedback about it. */ turnId?: string; /** * A notice about the chat's session, shown with a button that opens it. * The button's URL is built when the notice is sent. `text` keeps the * notice with its URL written out, which is what replicas from before * notice cards send during a rolling deploy. */ link?: "session"; /** The notice as shown on the card, without its URL. */ cardText?: string; }; route_id: string; }; export declare const teamsScoped: (db: Database, i: TeamsInstallation, fn: (tx: Database) => Promise) => Promise; export declare function listTeamsInstallations(db: Database, afterTenantId?: string | null): Promise; export declare function getTeamsInstallation(db: Database, tenantId: string): Promise; export declare function getWorkspaceTeamsInstallation(db: Database, workspaceId: string): Promise; /** * How long a welcomed personal chat isn't welcomed again: long enough to cover * every delivery of one install, short enough that reinstalling later welcomes * the person afresh. */ export declare const TEAMS_WELCOME_WINDOW_MS: number; export type TeamsWelcomeClaim = { scopeHash: string; claimId: string; }; /** * Claims the welcome for a personal chat, once across every API replica and * every delivery of the install. Null when the chat was claimed within * `windowMs`. Works before an organization has connected: the row holds only a * hash of the tenant and conversation, and grants nothing. */ export declare function claimTeamsWelcome(db: Database, input: { tenantId: string; conversationId: string; }, windowMs?: number): Promise; /** Gives up a claim whose welcome wasn't sent, unless a newer claim replaced it. */ export declare function releaseTeamsWelcome(db: Database, claim: TeamsWelcomeClaim): Promise; /** How long a welcome claim is kept after its window: a day, then it is removed. */ export declare const TEAMS_WELCOME_RETENTION_MS: number; /** * Removes up to `limit` claims older than the retention, so the table holds * roughly a day of installs. Returns how many it removed. */ export declare function pruneTeamsWelcomes(db: Database, limit?: number, retentionMs?: number): Promise; /** * How long an activity the bot answered itself isn't answered again: long * enough to cover every redelivery Teams makes of it. */ export declare const TEAMS_REPLY_CLAIM_WINDOW_MS: number; export type TeamsReplyClaim = { scopeHash: string; claimId: string; }; /** * Claims the reply to one Teams activity (help, link, unlink, an onboarding * reply or a notice), once across every API replica and every redelivery of * the activity. Null when it was claimed within `windowMs`. Works before an * organization has connected: the row holds only a hash of the tenant, * conversation and activity id, and grants nothing. */ export declare function claimTeamsReply(db: Database, input: { tenantId: string; conversationId: string; activityId: string; }, windowMs?: number): Promise; /** Gives up a claim whose reply Teams never received, unless a newer claim replaced it. */ export declare function releaseTeamsReply(db: Database, claim: TeamsReplyClaim): Promise; /** How long a reply claim is kept: a day, then it is removed. */ export declare const TEAMS_REPLY_CLAIM_RETENTION_MS: number; /** * Removes up to `limit` reply claims older than the retention, so the table * holds roughly a day of replies. Returns how many it removed. */ export declare function pruneTeamsReplyClaims(db: Database, limit?: number, retentionMs?: number): Promise; /** * One limit on Teams onboarding replies: at most `max` replies per `spanMs` * window for `key` (such as a budget and a person, a tenant or the whole * deployment), and none within `cooldownMs` of the last. A window starts with * its first reply. Keys are hashed before they are stored. */ export type TeamsOnboardingWindow = { key: string; spanMs: number; max: number; cooldownMs?: number; }; /** A granted reservation: when, on the database clock, it was counted. */ export type TeamsOnboardingGrant = { grantedAtMs: number; }; /** * Reserves one onboarding reply when every window allows it, across every API * replica; otherwise reserves nothing. A refusal is decided by a read alone, * so refused traffic writes nothing and waits on no lock. A grant locks the * rows in one order, then reads the database clock, so a reservation that * waited for a lock never sees a later reply as being in its future. */ export declare function reserveTeamsOnboardingReply(db: Database, windows: TeamsOnboardingWindow[]): Promise; /** * Gives back a reservation whose reply Teams clearly never received. Only * while `owner` (the person's own window) still holds it: each window is given * back only if it is the one the reservation was counted in, not a later one. */ export declare function releaseTeamsOnboardingReply(db: Database, grant: TeamsOnboardingGrant, owner: TeamsOnboardingWindow, windows: TeamsOnboardingWindow[]): Promise; /** How long an onboarding limit row is kept after its window began and its last reply. */ export declare const TEAMS_ONBOARDING_LIMIT_RETENTION_MS: number; /** * Removes up to `limit` onboarding limit rows whose window began and whose * last reply was over `retentionMs` ago. Returns how many it removed. */ export declare function pruneTeamsOnboardingLimits(db: Database, limit?: number, retentionMs?: number): Promise; export declare function installTeamsBot(db: Database, input: { accountId: string; workspaceId: string; tenantId: string; actorId: string; subjectId: string; nonce: string; /** * Self-serve setup connects a new organization or reactivates its own * disconnected binding; it never rotates an active installation, even * when two setups race. */ requireInactive?: boolean; /** * A disconnected binding of this tenant in another workspace to remove in * the same transaction, so the move either completes or changes nothing. */ replaceInactive?: { installation: TeamsInstallation; audit: DisconnectAuditActor; } | null; /** * This workspace's disconnected binding to another tenant, to remove in * the same transaction, so the workspace connects to `tenantId` instead or * changes nothing. The old tenant keeps no link, route, choice or pending * delivery here and can no longer reach the workspace. */ releaseWorkspaceInactive?: { installation: TeamsInstallation; audit: DisconnectAuditActor; } | null; }): Promise; export declare function linkTeamsActor(db: Database, i: TeamsInstallation, input: { actorId: string; subjectId: string; nonce: string; }): Promise; export declare function getTeamsActor(db: Database, i: TeamsInstallation, actorId: string): Promise; export declare function getOwnTeamsLink(db: Database, i: TeamsInstallation, subjectId: string): Promise<{ actor_id: string; } | null>; export declare function removeTeamsLink(db: Database, i: TeamsInstallation, subjectId: string): Promise; export declare function disconnectTeamsBot(db: Database, i: TeamsInstallation, options?: { /** Who disconnected the bot: audited in the same transaction when it was active. */ audit?: DisconnectAuditActor | null; }): Promise; export declare function enqueueTeamsMessage(db: Database, i: TeamsInstallation, input: { routeId: string; actorId: string; subjectId: string; personal: boolean; channelId?: string | null; groupAudience?: TeamsGroupAudience; groupRevision?: number; conversationId: string; threadId: string | null; serviceUrl: string; botId: string; eventId: string; payload: unknown; /** A 1:1 route's session in the sender's own Personal workspace. */ sessionWorkspaceId?: string | null; /** * The caller's live authority check for a 1:1 route that names another * workspace of the organization or an agent, run in the enqueue * transaction with `tx` (read everything through it). It replaces the * Personal-workspace-only rule and must also confirm the agent may run in * that workspace. A route that names an agent is refused without it. * Throws `teams_reach_changed` when the organization's Teams reach * changed while it ran; the caller may check again. */ authorizeSessionWorkspace?: (tx: Database, target: { workspaceId: string; agentId: string | null; }) => Promise; /** The agent a 1:1 route's chat runs as. */ agentId?: string | null; }): Promise<{ /** The route the message joined, which for a group chat may be a new one. */ routeId: string; /** False when the event was already queued, as on a redelivery. */ queued: boolean; /** The route's reply progress when the message was queued; see `teamsReplyPending`. */ replySequence: number; }>; export declare function claimTeamsRoute(db: Database, i: TeamsInstallation): Promise; export declare function releaseTeamsRoute(db: Database, i: TeamsInstallation, r: TeamsRoute): Promise; export declare function getTeamsPendingMessage(db: Database, i: TeamsInstallation, r: TeamsRoute): Promise; export declare function settleTeamsMessage(db: Database, i: TeamsInstallation, r: TeamsRoute, eventId: string, error: string | null, /** The session turn that answers an admitted message, when known. */ turnId?: string | null): Promise; /** * Records that a turn's reply, request for input or failure was queued for * Teams, which ends typing for the messages that turn answers. */ export declare function markTeamsTurnAnswered(db: Database, i: TeamsInstallation, r: TeamsRoute, turnId: string): Promise; /** * The routing notes (`payload.teamsRouting`) of the messages a turn answers, * oldest first: what the ingress recorded about where each one was sent. */ export declare function getTeamsTurnRouting(db: Database, i: TeamsInstallation, r: TeamsRoute, turnId: string): Promise[]>; export declare function enqueueTeamsOutput(db: Database, i: TeamsInstallation, r: TeamsRoute, sequence: number, parts: string[], notice?: { eventId: string; kind: "admission" | "audience_changed"; }, reply?: { turnId: string | null; }, card?: { link: "session"; text: string; }): Promise; /** * Whether the reply to one inbound message is still being produced: the * message waits to be admitted, or it was admitted and the turn answering it * has had no reply, request for input or failure queued yet. A message * admitted without a known turn falls back to its chat: no reply, request * or failure since it was queued (`replySequence` from * `enqueueTeamsMessage`). Both lookups are by primary key. */ export declare function teamsReplyPending(db: Database, i: TeamsInstallation, input: { eventId: string; replySequence: number; }): Promise; /** An agent reply the bot delivered in one Teams conversation, found by its Teams message id. */ export type TeamsDeliveredReply = { route_id: string; /** The installation's workspace, which holds the route. */ workspace_id: string; subject_id: string; actor_id: string; personal: boolean; session_id: string; session_workspace_id: string | null; agent_id: string | null; turn_id: string | null; }; export declare function getTeamsDeliveredReply(db: Database, i: TeamsInstallation, input: { conversationId: string; providerMessageId: string; }): Promise; export declare function claimTeamsOutput(db: Database, i: TeamsInstallation, r: TeamsRoute, noticesOnly?: boolean): Promise; export declare function settleTeamsOutput(db: Database, i: TeamsInstallation, operationId: string, result: { state: "delivered" | "unknown" | "failed" | "pending"; providerMessageId?: string; error?: string; retryMs?: number; }): Promise; export declare function renewTeamsRoute(db: Database, i: TeamsInstallation, r: TeamsRoute): Promise; export declare function teamsDeliveryStatus(db: Database, i: TeamsInstallation, subjectId: string): Promise<{ pending: number; unknown: number; failed: number; }>; export declare function refuseTeamsRouteOutputs(db: Database, i: TeamsInstallation, r: TeamsRoute, error: string): Promise; /** Membership notifications only revoke; a later verified roster creates a new route. */ export declare function invalidateTeamsGroup(db: Database, i: TeamsInstallation, conversationId: string, expected?: { routeId?: string; revision?: number; leaseToken?: string | null; }): Promise; export declare function teamsGroupRouteActive(db: Database, i: TeamsInstallation, r: TeamsRoute): Promise; /** Messages kept per channel thread or group chat, newest first. */ export declare const TEAMS_CONTEXT_MESSAGES_PER_THREAD = 100; /** How long a recorded message stays available as context. */ export declare const TEAMS_CONTEXT_RETENTION_DAYS = 7; export type TeamsContextRecord = { messageId: string; actorId: string; actorName: string | null; text: string; hasAttachments: boolean; mentioned: boolean; occurredAt: Date; }; /** * Keeps one human message from a shared Teams conversation as context for a * later mention. Idempotent per message; trims the conversation thread to its * newest `TEAMS_CONTEXT_MESSAGES_PER_THREAD` rows and drops the workspace's * rows past retention as it goes. */ export declare function recordTeamsContextMessage(db: Database, i: TeamsInstallation, input: { conversationId: string; threadId: string | null; messageId: string; actorId: string; actorName: string | null; text: string; hasAttachments: boolean; mentioned: boolean; occurredAt: string; }): Promise; /** * Recorded messages in one channel thread or group chat before a mention, * newest first: at most `limit`, excluding the mention itself and anything * recorded after it. With `rootPostsOnly`, only channel root posts (a new * channel post mentioning Opengeni sees the posts before it). */ export declare function listTeamsContextMessages(db: Database, i: TeamsInstallation, input: { conversationId: string; threadId: string | null; beforeMessageId: string; notAfter: string; limit: number; rootPostsOnly?: boolean; }): Promise; /** * Marks a recorded message as a mention Opengeni accepted, so a later mention * that continues the same chat reads back only to it. Refused mentions stay * ordinary context. */ export declare function markTeamsContextMentionAccepted(db: Database, i: TeamsInstallation, input: { conversationId: string; messageId: string; }): Promise; /** * Applies a Teams edit or deletion to a recorded message: an edit replaces its * text, and a deletion (or an edit that leaves nothing) removes it. Messages * never recorded stay unrecorded. */ export declare function reviseTeamsContextMessage(db: Database, i: TeamsInstallation, input: { conversationId: string; messageId: string; edit: { text: string; hasAttachments: boolean; } | null; }): Promise; /** * Forgets every recorded message in one conversation and moves its join * cutoff to now: when people are added to a group chat, a newcomer must not * get answers built from history Teams did not share with them. Holding the * installation row exclusively orders this against every context write (which * shares it), so a delayed delivery of an older message either lands first and * is deleted here, or sees the new cutoff and is not kept. */ export declare function clearTeamsContextConversation(db: Database, i: TeamsInstallation, conversationId: string): Promise;