import type { RecordingLiveSegment } from "@opengeni/contracts"; import { type Database } from "./database.js"; import { type RecordingActor } from "./recordings.js"; /** * How far a track's live sessions may run ahead of the audio its recorder * reports, when that audio is charged in credits. The audio goes from the * device straight to the provider, so the recorder's own total is the meter; * these bounds keep a recorder that doesn't report (or under-reports) from * getting more sessions, and never refuse one that reports honestly, however * it pauses or stays silent. * * Time is measured between the recorder's reports, so a pause never counts * against the audio. The recorder reports its total about every minute while * streaming, when it pauses and resumes, and before asking for another * session. Of the time up to a report of more audio, at most that audio plus * {@link LIVE_REPORT_GROWTH_SLACK_MILLISECONDS} (and at most * {@link LIVE_REPORT_GAP_MAX_MILLISECONDS}) counts as covered for the track; * the time up to a report of no more audio (a pause, or a session that never * connected) doesn't. A session the recorder never reported on before asking * for another counts all its time up to that request as covered, and as * unreported. The slack is for the track's own bound only: toward the * person's day (below), the reported audio is all that accounts for time, so * reporting a trickle of audio leaves the rest of the time counted. * * A session can stream at most {@link LIVE_SESSION_MAX_MILLISECONDS} (the * providers end it then), so time counts only within the latest session's * longest possible run: a long pause, even one the recorder never reported, * counts at most what is left of that hour, and the recorder asks for no new * session while paused. * * Time no reported audio accounts for (the rest of every gap between reports, * and an unreported session's time) is "unbacked": a provider session may * have been open, streaming audio nobody reported. It adds up per track, and * per person and workspace by the hour: the person may have at most * {@link LIVE_UNBACKED_DAILY_MAX_MILLISECONDS} of it in the last day, plus * {@link LIVE_UNBACKED_PER_CHARGED} times the audio charged in credits that * day, so new recordings don't reset the bound, and a session is granted only * if the day, with all it could run counted, stays within that. Starts are * serialized per bound (an advisory lock), so a burst of them can't all read * the same day, and so are saves on a track whose session has no * provisional time (one a previous release started); other saves and * releases never add to the day more than the provisional time they * replace, in the hour it was counted in. A session counts all it could run * up front, as the track's provisional time, which each report or new * request swaps for the time it actually left unaccounted plus what it could * still run: a session nobody reports on costs its whole hour, and a refusal (which rolls the request back) leaves it counted. A * session the provider never started gives it back. The person is the human * behind the request (a personal API key and a delegated session act as * them); the workspace's other API keys record no person, so they share one * bound ({@link liveBoundSubject}) and a new key doesn't reset it. * * A track gets no more sessions once: * - {@link LIVE_UNREPORTED_GRANTS_MAX} of its sessions went unreported; * - the person's unbacked time, with the new session's, would pass its daily * limit; * - it had {@link LIVE_GRANTS_FREE} sessions, one more for each * {@link LIVE_GRANT_EARNED_PER_MILLISECONDS} of audio it reported, and at * most {@link LIVE_GRANTS_MAX}; * - or the time its sessions covered is ahead of its reported audio by more * than {@link LIVE_BEHIND_SLACK_MILLISECONDS} plus * {@link LIVE_BEHIND_SLACK_FRACTION} of that time. */ export declare const LIVE_UNREPORTED_GRANTS_MAX = 4; export declare const LIVE_GRANTS_FREE = 20; export declare const LIVE_GRANT_EARNED_PER_MILLISECONDS: number; export declare const LIVE_GRANTS_MAX = 100; export declare const LIVE_BEHIND_SLACK_MILLISECONDS: number; export declare const LIVE_BEHIND_SLACK_FRACTION = 0.25; export declare const LIVE_REPORT_GAP_MAX_MILLISECONDS: number; export declare const LIVE_REPORT_GROWTH_SLACK_MILLISECONDS = 60000; export declare const LIVE_UNBACKED_DAILY_MAX_MILLISECONDS: number; /** * How much unbacked time each millisecond of live audio charged in credits * allows on top of {@link LIVE_UNBACKED_DAILY_MAX_MILLISECONDS}. A session * counts its hour up front and a meeting's last sessions keep what they could * still run, so a two-track meeting of m minutes leaves 2 × (60 − m) minutes * counted and charges 2 × m: at six, meetings of about nine minutes or longer * never use up the day, and free provider time stays at most the base plus * six times what was paid. */ export declare const LIVE_UNBACKED_PER_CHARGED = 6; /** The longest a provider's realtime session runs (OpenAI and Azure: an hour). */ export declare const LIVE_SESSION_MAX_MILLISECONDS: number; /** * Whose daily bound a live session counts against: the person, or, for the * workspace's API keys (`api_key:`, which record no person), all of them * together. Matches the row security of recording_live_unbacked_hours. */ export declare function liveBoundSubject(subjectId: string): string; /** Why a track gets no more live sessions, or null when it may have one. */ export declare function liveGrantRefusal(input: { /** Sessions granted for the track so far. */ grants: number; /** Sessions of the track the recorder never reported on. */ unreportedGrants: number; /** Audio the recorder reported streaming for the track. */ streamedMilliseconds: number; /** Time counted against the reported audio (see above). */ coveredMilliseconds: number; /** * The person's unbacked time in the last day once this session is granted: * the track's latest session closed, and the new one's longest possible * run counted. */ unbackedMilliseconds: number; /** The person's live audio charged in credits in the last day. */ chargedMilliseconds: number; }): "live_grant_limit" | "live_streamed_behind" | null; /** The one message for a track refused more live sessions. */ export declare const LIVE_GRANT_REFUSED_MESSAGE = "The live transcript stopped for this track. The recording continues and is transcribed when it ends."; /** * Note that a track streams to a live model, counting the session granted. * Only the person recording may, and only while the recording is being * captured. When the track's audio is charged in credits (`bounded`), a * track whose sessions run far ahead of the audio it reports gets no more * (see {@link liveGrantRefusal}). */ export declare function startRecordingLiveTrack(db: Database, input: RecordingActor & { recordingId: string; trackNumber: number; providerId: string; model: string; /** The track's audio is charged in credits, so its sessions are bounded. */ bounded?: boolean; }): Promise<{ language: string | null; }>; /** * Take back a session counted by {@link startRecordingLiveTrack} that the * provider never started, so a provider outage doesn't use up the track's * sessions, and no time is counted for it (its provisional time is given * back). */ export declare function releaseRecordingLiveGrant(db: Database, input: RecordingActor & { recordingId: string; trackNumber: number; }): Promise; /** A stretch of a track's streamed audio, in milliseconds from its start. */ export type RecordingLiveRange = { fromMilliseconds: number; toMilliseconds: number; }; export type AppendRecordingLiveSegmentsInput = RecordingActor & { recordingId: string; trackNumber: number; /** All audio streamed for this track so far; the stored total never shrinks. */ streamedMilliseconds: number; /** * How the track's live model is paid, asked under the recording lock with * the model the track streams to. * * `listPriceMicros` prices audio at the model's list price; the track * stores the list price of everything streamed, which a usage allowance * that prices unbilled use counts, less the list price of audio charged in * credits, so charged audio never counts twice. * * `charge`, when the audio is charged in credits, records the charge for * the range from the track's charged mark to its new stored total inside * this transaction (its usage receipt), and the mark moves with it: the * range is charged once however saves overlap or retry, and a failed * charge fails the save (nothing of it commits) for the client to retry. */ pricing: (live: RecordingLiveModel) => { listPriceMicros: ((milliseconds: number) => number) | null; charge: ((tx: Database, range: RecordingLiveRange) => Promise) | null; }; segments: ReadonlyArray<{ segmentNumber: number; startMilliseconds: number; endMilliseconds: number; text: string; }>; }; export type RecordingLiveModel = { providerId: string; model: string; }; export type AppendRecordingLiveSegmentsResult = { saved: number; ended: boolean; /** The model the track streams to, read under the lock; null once the preview ended. */ live: RecordingLiveModel | null; /** How far this call moved the track's stored streamed total. */ addedMilliseconds: number; /** * The range this call charged (its receipt committed with the save); its * credit debit follows the commit, and a debit that fails is applied from * the receipt later. */ charged: RecordingLiveRange | null; }; /** * Save finished live text. A segment number already saved keeps its first * copy. Once a transcript is ready the preview is over and nothing is saved. * The recording row lock orders a recording's appends, so the stored total * only grows, and `addedMilliseconds` counts and `charge` charges each * millisecond once however often or concurrently a client retries. */ export declare function appendRecordingLiveSegments(db: Database, input: AppendRecordingLiveSegmentsInput): Promise; /** Live text saved after `after`, oldest first, for anyone who can see the recording. */ export declare function listRecordingLiveSegments(db: Database, input: RecordingActor & { recordingId: string; after?: number | undefined; limit?: number; }): Promise<{ segments: RecordingLiveSegment[]; after: number; ended: boolean; }>;