@gravity-rail/sdk 0.53.0 → 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -37
- package/dist/{chat-interaction-CklXa8iQ.d.cts → chat-interaction-CZnqOfut.d.ts} +1212 -69
- package/dist/{chat-interaction-Dei_j8Sd.d.ts → chat-interaction-DZCWRbDi.d.cts} +1212 -69
- package/dist/{chunk-6F4YJJKQ.js → chunk-BF4MOZZO.js} +339 -7
- package/dist/chunk-BF4MOZZO.js.map +1 -0
- package/dist/{chunk-MMDSZ25F.js → chunk-HXVJF54M.js} +1 -1
- package/dist/chunk-HXVJF54M.js.map +1 -0
- package/dist/helpers.d.cts +4 -4
- package/dist/helpers.d.ts +4 -4
- package/dist/index.cjs +344 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +363 -9
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +6 -5
- package/dist/internal.d.ts +6 -5
- package/dist/internal.js +33 -5
- package/dist/internal.js.map +1 -1
- package/dist/{member-import-Do6D2g14.d.ts → member-import-D1QvZiy5.d.ts} +1 -1
- package/dist/{member-import-BtTWfQ46.d.cts → member-import-DzuvKnMC.d.cts} +1 -1
- package/dist/{path-DKPLfZj4.d.ts → path-C6Ujx-3w.d.cts} +51 -53
- package/dist/{path-D1ZCbboc.d.cts → path-YfkJBAbf.d.ts} +51 -53
- package/dist/realtime.cjs +26 -0
- package/dist/realtime.cjs.map +1 -1
- package/dist/realtime.d.cts +17 -0
- package/dist/realtime.d.ts +17 -0
- package/dist/realtime.js +26 -0
- package/dist/realtime.js.map +1 -1
- package/dist/schemas.d.cts +2 -2
- package/dist/schemas.d.ts +2 -2
- package/dist/schemas.js +1 -1
- package/dist/{tasks-JweebIce.d.cts → tasks-BdYhYUpU.d.cts} +92 -26
- package/dist/{tasks-JweebIce.d.ts → tasks-BdYhYUpU.d.ts} +92 -26
- package/package.json +1 -1
- package/dist/chunk-6F4YJJKQ.js.map +0 -1
- package/dist/chunk-MMDSZ25F.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { c0 as ChannelMessagesUnavailableReason, m_ as Resource$1, Z as ActivityEventLine, L as ActivityAggregateResponse, $ as ActivityEventsResponse, kE as PathDefinition } from './chat-interaction-CZnqOfut.js';
|
|
2
|
+
import { e_ as SortOrder } from './tasks-BdYhYUpU.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Generated by Orval, grouped by scripts/types-codegen/group_generated.ts
|
|
@@ -332,6 +332,36 @@ interface ChannelMessagesResponse {
|
|
|
332
332
|
reason?: ChannelMessagesUnavailableReason | null;
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Generated by Orval, grouped by scripts/types-codegen/group_generated.ts
|
|
337
|
+
* (group map from emit_openapi.py). Do not edit manually.
|
|
338
|
+
* Group: say_task
|
|
339
|
+
*/
|
|
340
|
+
/**
|
|
341
|
+
* A Say template for one supported locale.
|
|
342
|
+
*/
|
|
343
|
+
interface SayMessageTranslation {
|
|
344
|
+
locale: string;
|
|
345
|
+
/** @maxLength 4000 */
|
|
346
|
+
text: string;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* What the agent says, verbatim, when the conversation reaches this Task.
|
|
350
|
+
*
|
|
351
|
+
* ``message`` is a Jinja2 template rendered with the same variables as a
|
|
352
|
+
* Task prompt. Optional ``translations`` override it by exact locale or
|
|
353
|
+
* same-language match. Every script is optional: without an applicable
|
|
354
|
+
* nonblank script the Task completes silently. Every authored variant must
|
|
355
|
+
* parse (checked by the runtime's ``validate``).
|
|
356
|
+
*/
|
|
357
|
+
interface SayTaskConfiguration {
|
|
358
|
+
/** @maxLength 4000 */
|
|
359
|
+
message?: string;
|
|
360
|
+
waitForResponse?: boolean;
|
|
361
|
+
/** @maxItems 100 */
|
|
362
|
+
translations?: SayMessageTranslation[];
|
|
363
|
+
}
|
|
364
|
+
|
|
335
365
|
/**
|
|
336
366
|
* Generated by Orval, grouped by scripts/types-codegen/group_generated.ts
|
|
337
367
|
* (group map from emit_openapi.py). Do not edit manually.
|
|
@@ -1423,12 +1453,12 @@ interface HealthDetailRequest {
|
|
|
1423
1453
|
* Group: acquisition_funnel
|
|
1424
1454
|
*/
|
|
1425
1455
|
/**
|
|
1426
|
-
*
|
|
1456
|
+
* Self-serve signups in the window grouped by first-touch source.
|
|
1427
1457
|
*
|
|
1428
1458
|
* ``source`` matches ``BookOfBusinessAcquisitionDistribution.source``
|
|
1429
1459
|
* semantics (``utm_source`` or ``"direct"``) so the two rollups join.
|
|
1430
|
-
* Only
|
|
1431
|
-
*
|
|
1460
|
+
* Only self-serve signups WITH an acquisition record are grouped here.
|
|
1461
|
+
* Marked signups with no record are counted in ``unattributedSignupCount``.
|
|
1432
1462
|
*/
|
|
1433
1463
|
interface AcquisitionFunnelChannel {
|
|
1434
1464
|
source: string;
|
|
@@ -1436,24 +1466,28 @@ interface AcquisitionFunnelChannel {
|
|
|
1436
1466
|
latestSignupAt: string;
|
|
1437
1467
|
}
|
|
1438
1468
|
/**
|
|
1439
|
-
* One
|
|
1469
|
+
* One self-serve signup, newest-first — the same population as signupCount.
|
|
1440
1470
|
*
|
|
1441
|
-
*
|
|
1442
|
-
*
|
|
1443
|
-
*
|
|
1444
|
-
*
|
|
1471
|
+
* PII-free by construction: account id only (never email/name), the signup
|
|
1472
|
+
* method the path recorded, and the allowlisted, query-stripped marketing
|
|
1473
|
+
* values (``lib.services.acquisition.sanitize_acquisition``). ``source`` is
|
|
1474
|
+
* null when the signup has no acquisition record. A stored ``utm_source``
|
|
1475
|
+
* is returned as-is, including the string ``"unattributed"``.
|
|
1445
1476
|
*/
|
|
1446
1477
|
interface AcquisitionFunnelRecentSignup {
|
|
1447
1478
|
accountId: number;
|
|
1448
1479
|
createdAt: string;
|
|
1449
|
-
|
|
1480
|
+
/** Signup path that created the account: email, sms, or google. */
|
|
1481
|
+
signupMethod: string;
|
|
1482
|
+
/** First-touch utm_source, or "direct" when the record has no utm_source. Null when this signup has no acquisition record. */
|
|
1483
|
+
source?: string | null;
|
|
1450
1484
|
utmMedium?: string | null;
|
|
1451
1485
|
utmCampaign?: string | null;
|
|
1452
1486
|
referrer?: string | null;
|
|
1453
1487
|
landingPage?: string | null;
|
|
1454
1488
|
}
|
|
1455
1489
|
/**
|
|
1456
|
-
*
|
|
1490
|
+
* Self-serve signups by first-touch channel (superuser only).
|
|
1457
1491
|
*/
|
|
1458
1492
|
interface AcquisitionFunnelResponse {
|
|
1459
1493
|
generatedAt: string;
|
|
@@ -1461,11 +1495,14 @@ interface AcquisitionFunnelResponse {
|
|
|
1461
1495
|
windowDays: number;
|
|
1462
1496
|
windowStart: string;
|
|
1463
1497
|
captureLiveSince: string | null;
|
|
1498
|
+
/** Self-serve signups in the window: accounts created by email, SMS, or Google signup and marked by that path. Omits member provisioning, imports, invitations, and rows from before the marker (no backfill). */
|
|
1464
1499
|
signupCount: number;
|
|
1500
|
+
/** Self-serve signups in the window that have a first-touch acquisition record. */
|
|
1465
1501
|
attributedSignupCount: number;
|
|
1502
|
+
/** Self-serve signups in the window with no first-touch record. Kept out of the channel rows so direct stays a real measurement. */
|
|
1466
1503
|
unattributedSignupCount: number;
|
|
1467
1504
|
channels: AcquisitionFunnelChannel[];
|
|
1468
|
-
|
|
1505
|
+
recentSelfServeSignups: AcquisitionFunnelRecentSignup[];
|
|
1469
1506
|
}
|
|
1470
1507
|
|
|
1471
1508
|
/**
|
|
@@ -1611,45 +1648,6 @@ interface AppContext {
|
|
|
1611
1648
|
*/
|
|
1612
1649
|
declare function formatAppContextAsMarkdown(context: AppContext | undefined): string;
|
|
1613
1650
|
|
|
1614
|
-
/**
|
|
1615
|
-
* Generated by Orval, grouped by scripts/types-codegen/group_generated.ts
|
|
1616
|
-
* (group map from emit_openapi.py). Do not edit manually.
|
|
1617
|
-
* Group: say_task
|
|
1618
|
-
*/
|
|
1619
|
-
/**
|
|
1620
|
-
* A Say template for one supported locale.
|
|
1621
|
-
*/
|
|
1622
|
-
interface SayMessageTranslation {
|
|
1623
|
-
locale: string;
|
|
1624
|
-
/** @maxLength 4000 */
|
|
1625
|
-
text: string;
|
|
1626
|
-
}
|
|
1627
|
-
/**
|
|
1628
|
-
* What the agent says, verbatim, when the conversation reaches this Task.
|
|
1629
|
-
*
|
|
1630
|
-
* ``message`` is a Jinja2 template rendered with the same variables as a
|
|
1631
|
-
* Task prompt. Optional ``translations`` override it by exact locale or
|
|
1632
|
-
* same-language match. Every script is optional: without an applicable
|
|
1633
|
-
* nonblank script the Task completes silently. Every authored variant must
|
|
1634
|
-
* parse (checked by the runtime's ``validate``).
|
|
1635
|
-
*/
|
|
1636
|
-
interface SayTaskConfiguration {
|
|
1637
|
-
/** @maxLength 4000 */
|
|
1638
|
-
message?: string;
|
|
1639
|
-
waitForResponse?: boolean;
|
|
1640
|
-
/** @maxItems 100 */
|
|
1641
|
-
translations?: SayMessageTranslation[];
|
|
1642
|
-
}
|
|
1643
|
-
|
|
1644
|
-
/**
|
|
1645
|
-
* The Task types a Workflow can contain: `chat` (the conversational Task, also
|
|
1646
|
-
* stored as `standard`) and `say` (speaks a literal script, then follows its
|
|
1647
|
-
* `complete` outcome).
|
|
1648
|
-
*/
|
|
1649
|
-
type TaskType = 'chat' | 'say';
|
|
1650
|
-
|
|
1651
|
-
type DataRow = TaskDataRow | FormDataRow;
|
|
1652
|
-
|
|
1653
1651
|
/**
|
|
1654
1652
|
* Default AI model constants shared across the frontend.
|
|
1655
1653
|
*
|
|
@@ -2358,4 +2356,4 @@ declare function pathDraftFromStorage(path: StoredPath): PathDraft;
|
|
|
2358
2356
|
/** Adapt only the edited Path. Preserve original blank/null values and inert markers on a no-op. */
|
|
2359
2357
|
declare function pathDraftToStorage<T extends StoredPath>(draft: PathDraft, original: T): T;
|
|
2360
2358
|
|
|
2361
|
-
export { type
|
|
2359
|
+
export { type VoiceMetricsResponse$1 as $, type AIModelUsageResponse as A, type BillingStatistics as B, type CallEndSection as C, CreateMemberContactPointRequestSharing as D, type CurrentPlanInfo as E, DEFAULT_AGENT_CHAT_MODEL as F, DEFAULT_AGENT_REALTIME_MODEL as G, DEFAULT_AGENT_REALTIME_MODEL_CONFIG as H, DEFAULT_CHAT_MODEL as I, DEFAULT_CUSTOM_VOICE_MODEL as J, DEFAULT_VOICE_INFERENCE_MODEL as K, type DailyPhoneRowResponse as L, type DailySMSRowResponse as M, type DashboardMetrics as N, type DataRecordContext as O, type DataRecordImportOutcomes as P, type DeviceCacheFileEntry as Q, type DeviceCacheManifestResponse as R, type EfficiencyMetricsResponse as S, type EmailCountsResponse as T, type EmailUsageResponse as U, type EngineTimeSeries as V, type ErrorCodeBucket as W, type EventRuleMetrics as X, type ExportStatistics as Y, type FeatureSyncStatus as Z, type MetricSummary$1 as _, type AIUsageDetailsResponse as a, type TaskCountResponse as a$, type WorkspaceHealthResponse$1 as a0, type HealthCheckStatistics as a1, type HealthDetailRequest as a2, type HealthMetricsRequest as a3, type HealthMetricsResponse as a4, type HealthSummary as a5, type ImportStatistics as a6, MAX_EMAIL_SEND_RECIPIENTS as a7, MEMBER_RELATIONSHIP_KINDS as a8, type MemberContactPointResponse as a9, type RequestsSection as aA, type RequestsStatusEntry as aB, type ResourceMetadata as aC, type ResourceOverageResponse as aD, type ResourceUsageResponse as aE, type RetireMemberContactPointRequest as aF, type RevisionState as aG, type RevisionStateRevision as aH, type RevisionStateWorkflow as aI, type RoutineRunMetrics as aJ, SCOPES as aK, SCOPE_PRESETS as aL, type SMSCommunicationDetailsResponse as aM, type SMSMessageUsageResponse as aN, type SMSPhoneNumberUsageResponse as aO, type SayMessageTranslation as aP, type SayTaskConfiguration as aQ, type ScenarioDependencyAssessment as aR, ScopeCategory as aS, ScopeManager as aT, type ScopeName as aU, type SiteContext as aV, type SkippedWorkspace as aW, type StatisticsSummary as aX, type StoredPath as aY, type SystemStatistics as aZ, type TaskContext as a_, type MemberContactPointConfirmationResponse as aa, type MemberContactPointListResponse as ab, type MemberImportColumnResponse as ac, type MemberNotificationPreferences as ad, type MemberNotifyActionParams as ae, type MemberNotifyRecipientMode as af, type MemberUsageReport as ag, type MetricDataPoint as ah, type MetricSection as ai, type MetricSummary as aj, type MetricValue as ak, PIDS_DISPOSITIONS as al, PIDS_SOURCES as am, type PageContext as an, type PathDraft as ao, type PerformanceMetrics as ap, type PhoneCallPhoneNumberUsageResponse as aq, type PhoneCallUsageResponse as ar, type PhoneCommunicationDetailsResponse as as, type PhoneNumberUsage as at, type PidsDisposition as au, type PidsSource as av, type ProductOverageConfigResponse as aw, type ProvisionPlanRequest as ax, type ProvisionPlanResponse as ay, type RawDataRecordImportResponse as az, type AcquisitionFunnelChannel as b, type TimeSeriesData as b0, type TimeSeriesDataPoint as b1, type ToolParameter as b2, type ToolkitContext as b3, ToolkitState as b4, type TopOffender as b5, type TopResourceOverageResponse as b6, type TtsReconnectOutcomeEntry as b7, type TtsReconnectSection as b8, type UpdateMemberContactPointRequest as b9, UpdateMemberContactPointRequestContactType as ba, UpdateMemberContactPointRequestPurpose as bb, UpdateMemberContactPointRequestSharing as bc, type UsageProductSummaryResponse as bd, type UsageReportResponse as be, type UsageReportRequest as bf, type UsageStatistics as bg, type VoiceMetricsResponse as bh, type VoiceMetricsTimeRange as bi, WIDGET_SLUGS as bj, type WebhookMetrics as bk, type WidgetSlug as bl, type WorkflowContext as bm, type WorkspaceHealthDetail as bn, type WorkspaceHealthResponse as bo, type WorkspaceHealthRow as bp, type WorkspaceUsageReportResponse as bq, formatAppContextAsMarkdown as br, getAllResourceSlugs as bs, getPathDefinition as bt, getResourceMetadata as bu, getRevisionState as bv, pathDraftFromStorage as bw, pathDraftToStorage as bx, ScenarioDependencyAssessmentRefusalCodesItem as by, type ScopeDefinition as bz, type AcquisitionFunnelRecentSignup as c, type AcquisitionFunnelResponse as d, type ActivityDetail as e, type ActivityFeedResponse as f, type AdminDomainResponse as g, type AdminDomainSiteMapping as h, type AdminDomainWidgetMapping as i, type AdminPidsFindingResponse as j, type AnalyticsFilter as k, type AnalyticsQuery as l, type AppContext as m, type AttachedAddonInfo as n, type BundleScenarioPreflightResponse as o, type CallEndStatusEntry as p, type ChannelErrorBreakdown as q, type ChannelFailures as r, type ChannelMessageResponse as s, ChannelMessageDirection as t, ChannelMessageSource as u, type ChannelMessagesResponse as v, type CommunicationDetailsResponse as w, type CreateMemberContactPointRequest as x, CreateMemberContactPointRequestContactType as y, CreateMemberContactPointRequestPurpose as z };
|
package/dist/realtime.cjs
CHANGED
|
@@ -825,6 +825,32 @@ var OperatorSessionClient = class {
|
|
|
825
825
|
answer_token: answerToken
|
|
826
826
|
});
|
|
827
827
|
}
|
|
828
|
+
/**
|
|
829
|
+
* Join a live, escalated voice call on this chat.
|
|
830
|
+
*
|
|
831
|
+
* Unlike {@link answerCall} (a waiting caller whose ring poll redirects the
|
|
832
|
+
* PSTN leg), the caller here is already on the AI media stream with the AI
|
|
833
|
+
* paused for an escalation. The backend
|
|
834
|
+
* (`lib/operator_session_handler.py::_join_live_call`) authorizes, attaches
|
|
835
|
+
* this session as bridge side "a", and re-points the live caller leg onto
|
|
836
|
+
* the bridge over Twilio REST — replacing the AI stream with the human one.
|
|
837
|
+
* The server then sends `operator.call.started`, after which audio flows
|
|
838
|
+
* exactly like an answered call.
|
|
839
|
+
*
|
|
840
|
+
* The chat id is carried by the session URL, so no argument is needed. Must
|
|
841
|
+
* be called once the session is ready (state `connected`); starts the
|
|
842
|
+
* microphone first so operator audio flows the instant the bridge attaches.
|
|
843
|
+
*/
|
|
844
|
+
async joinCall() {
|
|
845
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
846
|
+
throw new Error("Operator session WebSocket is not connected");
|
|
847
|
+
}
|
|
848
|
+
if (this.sessionState !== "connected") {
|
|
849
|
+
throw new Error("Operator session is not ready");
|
|
850
|
+
}
|
|
851
|
+
await this.startMicrophone();
|
|
852
|
+
this.sendCustom("operator.call.join");
|
|
853
|
+
}
|
|
828
854
|
/**
|
|
829
855
|
* Mute or unmute the operator's outbound microphone. Muting gates mic frames
|
|
830
856
|
* locally (the caller stops hearing the operator) while callee audio keeps
|
package/dist/realtime.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/realtime/index.ts","../src/realtime/types.ts","../src/realtime/login-token.ts","../src/realtime/socket-client.ts","../src/realtime/workspace-client.ts","../src/realtime/account-client.ts","../src/audio/pcm.ts","../src/realtime/operator-session-client.ts"],"sourcesContent":["export {\n type ChannelEvent,\n type ChannelListener,\n type RealtimeAuthSession,\n type RealtimeSessionBase,\n type RealtimeLoginTokenProvider,\n type AccountRealtimeSession,\n type OperatorSessionState,\n type OperatorCallPhase,\n type Unsubscribe,\n isActiveCallPhase,\n ACTIVITY_CHANNEL,\n ACTIVITY_MEMBER_CHANNEL,\n ACCOUNT_ACTIVITY_CHANNEL,\n ACCOUNT_PRESENCE_CHANNEL,\n PRESENCE_CHANNEL,\n type ActivityChannelName,\n type ActivityInvalidationEvent,\n isActivityInvalidation,\n} from './types';\n\nexport {\n WorkspaceRealtimeClient,\n buildWorkspaceWsUrl,\n initWorkspaceSocket,\n subscribeWorkspaceChannel,\n onWorkspaceSocketConnection,\n isWorkspaceSocketConnected,\n} from './workspace-client';\n\nexport {\n AccountRealtimeClient,\n buildAccountWsUrl,\n workspaceChannel,\n initAccountSocket,\n subscribeAccountChannel,\n onAccountSocketConnection,\n isAccountSocketConnected,\n} from './account-client';\n\nexport { RealtimeSocketClient, type ProtocolInitMessage } from './socket-client';\n\nexport { LOGIN_TOKEN_TIMEOUT_MS } from './login-token';\n\nexport { OperatorSessionClient, type OperatorSessionHandlers } from './operator-session-client';\n","/** Event received from a subscribed workspace realtime channel. */\nexport interface ChannelEvent {\n type: string;\n [key: string]: unknown;\n}\n\nexport type ChannelListener = (event: ChannelEvent) => void;\nexport type Unsubscribe = () => void;\n\n/**\n * What every realtime connection needs: where the API is, and who is asking.\n *\n * The account socket needs nothing more — its scope comes from the server's\n * grant map, not from the URL — so this is the base rather than a widening of\n * {@link RealtimeAuthSession}. A workspace session is assignable to it, which\n * is what lets one session object drive both clients during cutover.\n */\nexport interface RealtimeSessionBase {\n apiUrl: string;\n /**\n * The credential for one `login` frame, fetched once per connect attempt —\n * reconnects included — and never reused.\n *\n * In a browser this mints a single-use `gr_wst_` token same-origin (the\n * server refuses anything else from a browser handshake), so page script\n * never holds the account session. A non-browser client (Node, a CLI) may\n * return its bearer credential. Resolve `null` when no token can be had;\n * the client treats that attempt as failed and retries on its backoff.\n *\n * The provider's identity is part of the session: replacing it with a\n * different function reconnects the socket.\n */\n getLoginToken: RealtimeLoginTokenProvider;\n}\n\n/** Mints the token for one realtime `login` frame. See {@link RealtimeSessionBase}. */\nexport type RealtimeLoginTokenProvider = () => Promise<string | null>;\n\n/** Auth context for workspace realtime WebSocket connections. */\nexport interface RealtimeAuthSession extends RealtimeSessionBase {\n workspaceUuid: string;\n}\n\n/** Auth context for the account realtime WebSocket. No workspace, by design. */\nexport type AccountRealtimeSession = RealtimeSessionBase;\n\nexport type OperatorSessionState = 'idle' | 'connecting' | 'connected' | 'error';\n\nexport type OperatorCallPhase =\n 'idle' | 'connecting' | 'ready' | 'dialing' | 'live' | 'onHold' | 'ended' | 'failed';\n\n/** Phases where Hold / Forward / Hang up apply to an active phone leg. */\nexport function isActiveCallPhase(phase: OperatorCallPhase): boolean {\n return phase === 'dialing' || phase === 'live' || phase === 'onHold';\n}\n\n// ---------------------------------------------------------------------------\n// Activity Spine (docs/projects/active/activity-spine.md)\n// ---------------------------------------------------------------------------\n\n/**\n * The two activity lanes a client can watch.\n *\n * `activity` is the whole workspace's feed; `activity:me` is the connected\n * member's own private lane and open items, kept separate so a busy workspace\n * does not wake every operator's badge on every event.\n *\n * Both are named constants rather than interpolated strings because the server\n * matches them exactly — a typo would subscribe successfully to nothing.\n */\nexport const ACTIVITY_CHANNEL = 'activity' as const;\nexport const ACTIVITY_MEMBER_CHANNEL = 'activity:me' as const;\n\n/**\n * The account's own lane, on the account socket only.\n *\n * Carries two things the workspace lanes cannot: the account's\n * `account_private` rows (manager / DM / agent-job chats, member-targeted\n * routine runs, and the concierge lane, which has no workspace at all), and a\n * change to the account's own grants.\n */\nexport const ACCOUNT_ACTIVITY_CHANNEL = 'account:activity' as const;\n\nexport type ActivityChannelName =\n typeof ACTIVITY_CHANNEL | typeof ACTIVITY_MEMBER_CHANNEL | typeof ACCOUNT_ACTIVITY_CHANNEL;\n\n/**\n * Operator presence, on the account socket only.\n *\n * The channel operator presence is **written** on. Presence state lives once,\n * at the account (`docs/projects/active/account-socket.md` § \"Presence,\n * migrated\"), and is projected into each chosen workspace's live set — so\n * going live is one command here rather than one per workspace socket.\n *\n * Unlike the activity lanes this one is bidirectional and does carry a\n * payload, because it is not an invalidation lane: `presence_confirmed`\n * reports the scope set the server actually honoured, which is the answer to\n * the command and is not re-fetchable from anywhere else.\n */\nexport const ACCOUNT_PRESENCE_CHANNEL = 'account:presence' as const;\n\n/** The workspace-scoped presence channel, on the per-workspace socket. */\nexport const PRESENCE_CHANNEL = 'presence' as const;\n\n/**\n * What an activity channel delivers — and deliberately all it delivers.\n *\n * **No event payloads travel over this socket.** The signal says *look again*;\n * the look goes through `POST /activity/query` (or `/counts` for a badge) under\n * the client's own session, where it is authorized against a freshly compiled\n * grant set and recorded as a bulk PHI access. A payload here would be an\n * authorization decision frozen at publish time by a producer that knows\n * nothing about who is listening — and a read that left no audit record.\n *\n * So: refetch on this, never render from it.\n */\nexport interface ActivityInvalidationEvent extends ChannelEvent {\n type: 'invalidate';\n data: {\n scope: 'workspace' | 'member' | 'account';\n reason: 'drain';\n };\n}\n\n/** Narrow a channel event to an activity invalidation. */\nexport function isActivityInvalidation(event: ChannelEvent): event is ActivityInvalidationEvent {\n if (event.type !== 'invalidate') return false;\n const data = (event as { data?: unknown }).data;\n if (typeof data !== 'object' || data === null) return false;\n const scope = (data as { scope?: unknown }).scope;\n return scope === 'workspace' || scope === 'member' || scope === 'account';\n}\n","import type { RealtimeLoginTokenProvider } from './types';\n\n/**\n * How long one connect attempt waits for its login token. A provider that\n * never settles (a hung mint request) must not hold the attempt open forever:\n * past this the attempt fails like any other and the caller's backoff applies.\n */\nexport const LOGIN_TOKEN_TIMEOUT_MS = 10_000;\n\n/**\n * Call `provider` once, bounded by {@link LOGIN_TOKEN_TIMEOUT_MS}.\n *\n * Resolves the token, or `null` when the provider throws, times out, or\n * returns anything but a non-empty string. Never rejects.\n */\nexport async function mintLoginToken(provider: RealtimeLoginTokenProvider): Promise<string | null> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n const token: unknown = await Promise.race([\n provider(),\n new Promise<null>((resolve) => {\n timer = setTimeout(() => resolve(null), LOGIN_TOKEN_TIMEOUT_MS);\n }),\n ]);\n return typeof token === 'string' && token.length > 0 ? token : null;\n } catch {\n return null;\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n}\n","/**\n * The realtime socket state machine, shared by both mounts.\n *\n * Two sockets speak the same protocol — `/api/v2/w/{wid}/ws/realtime` and\n * `/api/v2/ws/realtime` — and differ in exactly two places: the URL they open,\n * and the identity `protocol.init` hands back. Everything else (login,\n * heartbeat, reconnect with backoff, per-channel subscribe retry, listener\n * fan-out, deferred close) is one implementation here rather than two that\n * drift.\n *\n * That matters more than it sounds. The subscribe-retry and reconnect logic is\n * where the sharp edges live, and a second copy of it would be a second place\n * for a fix to be applied once.\n *\n * Protocol:\n * Client → Server: login, subscribe, unsubscribe, heartbeat, message\n * Server → Client: protocol.init, subscribed, subscribe_error, unsubscribed,\n * event, error\n */\n\nimport { mintLoginToken } from './login-token';\nimport type { ChannelEvent, ChannelListener, RealtimeSessionBase, Unsubscribe } from './types';\n\nconst HEARTBEAT_INTERVAL_MS = 30_000;\nconst RECONNECT_DELAY_MS = 3_000;\nconst MAX_RECONNECT_ATTEMPTS = 5;\nconst CLOSE_DELAY_MS = 1_000;\nconst MAX_SUBSCRIBE_RETRY_DELAY_MS = 30_000;\n\ninterface SocketState {\n ws: WebSocket | null;\n connected: boolean;\n connecting: boolean;\n /**\n * The last transport-or-server failure, or null once a connection succeeds.\n *\n * The state machine has always known this; it simply had no way to say so,\n * so every consumer hardcoded `error: null` and reported a healthy socket\n * through an outage. Two things set it: a server `error` frame (which is\n * how a rejected login is reported), and running out of reconnect\n * attempts — the point at which the client has genuinely given up and the\n * user needs to know, rather than each transient close on the way there.\n */\n lastError: string | null;\n listeners: Map<string, Set<ChannelListener>>;\n subscribedChannels: Set<string>;\n subscriptionRevision: number;\n subscriptionRetryChannels: Set<string>;\n subscriptionRetryAttempts: number;\n subscriptionRetryTimer: ReturnType<typeof setTimeout> | null;\n reconnectAttempts: number;\n reconnectTimer: ReturnType<typeof setTimeout> | null;\n heartbeatTimer: ReturnType<typeof setInterval> | null;\n closeTimer: ReturnType<typeof setTimeout> | null;\n intentionalClose: boolean;\n /**\n * Bumped by every connect attempt and every deliberate close. A connect\n * attempt awaits its login token before it opens a socket; if the\n * generation moved while it waited, a close or a newer attempt owns the\n * state and the stale attempt stops without touching it.\n */\n connectGeneration: number;\n}\n\nfunction createSocketState(): SocketState {\n return {\n ws: null,\n connected: false,\n connecting: false,\n lastError: null,\n listeners: new Map(),\n subscribedChannels: new Set(),\n subscriptionRevision: 0,\n subscriptionRetryChannels: new Set(),\n subscriptionRetryAttempts: 0,\n subscriptionRetryTimer: null,\n reconnectAttempts: 0,\n reconnectTimer: null,\n heartbeatTimer: null,\n closeTimer: null,\n intentionalClose: false,\n connectGeneration: 0,\n };\n}\n\n/** The `protocol.init` frame, before either subclass narrows it. */\nexport interface ProtocolInitMessage {\n memberId?: unknown;\n accountUuid?: unknown;\n workspaceUuids?: unknown;\n}\n\nexport abstract class RealtimeSocketClient<TSession extends RealtimeSessionBase> {\n private session: TSession | null = null;\n private readonly getSessionFn: (() => TSession | null) | null;\n private readonly state: SocketState = createSocketState();\n private readonly connectionListeners = new Set<(connected: boolean) => void>();\n\n constructor(config?: TSession | (() => TSession | null)) {\n if (typeof config === 'function') {\n this.getSessionFn = config;\n } else {\n this.getSessionFn = null;\n if (config) {\n this.session = config;\n }\n }\n }\n\n /** The WebSocket URL this mount connects to. */\n protected abstract buildUrl(session: TSession): string;\n\n /**\n * Record whatever identity `protocol.init` carried for this mount.\n *\n * Called before the connection is announced, so a `onConnection` listener\n * that immediately reads the identity always sees the new one.\n */\n protected abstract readProtocolInit(msg: ProtocolInitMessage): void;\n\n /** Forget the identity on a deliberate close. See {@link closeConnection}. */\n protected abstract clearIdentity(): void;\n\n /**\n * Whether replacing `previous` with `next` invalidates the open socket.\n *\n * A different login-token provider always does — it stands for a different\n * identity; the rest is mount-specific — a workspace client changing\n * workspace is a different socket, and an account client has no such axis.\n */\n protected sessionRequiresReconnect(previous: TSession | null, next: TSession): boolean {\n return previous?.getLoginToken !== next.getLoginToken || previous?.apiUrl !== next.apiUrl;\n }\n\n protected resolveSession(): TSession | null {\n return this.session ?? this.getSessionFn?.() ?? null;\n }\n\n private notifyConnectionListeners(): void {\n for (const listener of this.connectionListeners) {\n listener(this.state.connected);\n }\n }\n\n private clearCloseTimer(): void {\n if (this.state.closeTimer) {\n clearTimeout(this.state.closeTimer);\n this.state.closeTimer = null;\n }\n }\n\n private clearSubscriptionRetry(channel?: string): void {\n if (channel) {\n this.state.subscriptionRetryChannels.delete(channel);\n } else {\n this.state.subscriptionRetryChannels.clear();\n }\n\n if (this.state.subscriptionRetryChannels.size > 0) return;\n\n if (this.state.subscriptionRetryTimer) {\n clearTimeout(this.state.subscriptionRetryTimer);\n this.state.subscriptionRetryTimer = null;\n }\n this.state.subscriptionRetryAttempts = 0;\n }\n\n private scheduleSubscriptionRetry(channels: string[]): void {\n for (const channel of channels) {\n if (this.state.listeners.has(channel) && !this.state.subscribedChannels.has(channel)) {\n this.state.subscriptionRetryChannels.add(channel);\n }\n }\n\n if (this.state.subscriptionRetryChannels.size === 0 || this.state.subscriptionRetryTimer) {\n return;\n }\n\n this.state.subscriptionRetryAttempts += 1;\n const delay = Math.min(\n RECONNECT_DELAY_MS * this.state.subscriptionRetryAttempts,\n MAX_SUBSCRIBE_RETRY_DELAY_MS\n );\n\n this.state.subscriptionRetryTimer = setTimeout(() => {\n this.state.subscriptionRetryTimer = null;\n const retryChannels = Array.from(this.state.subscriptionRetryChannels).filter(\n (channel) =>\n this.state.listeners.has(channel) && !this.state.subscribedChannels.has(channel)\n );\n\n for (const channel of this.state.subscriptionRetryChannels) {\n if (!retryChannels.includes(channel)) {\n this.state.subscriptionRetryChannels.delete(channel);\n }\n }\n\n if (retryChannels.length === 0) {\n this.state.subscriptionRetryAttempts = 0;\n return;\n }\n\n if (this.state.connected && this.state.ws?.readyState === WebSocket.OPEN) {\n this.state.ws.send(JSON.stringify({ type: 'subscribe', channels: retryChannels }));\n }\n }, delay);\n }\n\n setSession(session: TSession): void {\n const changed = this.sessionRequiresReconnect(this.session, session);\n\n this.session = session;\n // `connecting` with no `ws` is an attempt still minting its token under\n // the old session; closing bumps the generation so it stops there.\n if (changed && (this.state.ws || this.state.connecting)) {\n this.closeConnection();\n }\n if (this.state.listeners.size > 0) {\n this.ensureConnection();\n }\n }\n\n subscribe(channel: string, listener: ChannelListener): Unsubscribe {\n this.clearCloseTimer();\n\n let listeners = this.state.listeners.get(channel);\n if (!listeners) {\n listeners = new Set();\n this.state.listeners.set(channel, listeners);\n }\n listeners.add(listener);\n\n this.ensureConnection();\n\n if (\n this.state.connected &&\n this.state.ws?.readyState === WebSocket.OPEN &&\n !this.state.subscribedChannels.has(channel)\n ) {\n this.state.ws.send(JSON.stringify({ type: 'subscribe', channels: [channel] }));\n }\n\n return () => {\n const channelListeners = this.state.listeners.get(channel);\n channelListeners?.delete(listener);\n if (channelListeners && channelListeners.size === 0) {\n this.state.listeners.delete(channel);\n this.state.subscribedChannels.delete(channel);\n this.clearSubscriptionRetry(channel);\n if (this.state.connected && this.state.ws?.readyState === WebSocket.OPEN) {\n this.state.ws.send(JSON.stringify({ type: 'unsubscribe', channels: [channel] }));\n }\n }\n\n if (this.state.listeners.size === 0) {\n this.state.closeTimer = setTimeout(() => {\n if (this.state.listeners.size === 0) {\n this.closeConnection();\n }\n }, CLOSE_DELAY_MS);\n }\n };\n }\n\n send(channel: string, data: unknown): void {\n if (!this.state.connected || !this.state.ws) return;\n this.state.ws.send(JSON.stringify({ type: 'message', channel, data }));\n }\n\n onConnection(listener: (connected: boolean) => void): () => void {\n this.connectionListeners.add(listener);\n listener(this.state.connected);\n return () => {\n this.connectionListeners.delete(listener);\n };\n }\n\n isConnected(): boolean {\n return this.state.connected;\n }\n\n /**\n * Whether the server has acknowledged this channel subscription.\n *\n * This is stricter than having a local listener: it becomes true only after\n * a `subscribed` frame, returns to false on disconnect or server-driven\n * unsubscribe, and stays false while automatic subscribe retry is pending.\n * Consumers that must order a command after subscription admission should\n * observe {@link getSubscriptionRevision} and re-read this method.\n */\n isSubscribed(channel: string): boolean {\n return this.state.subscribedChannels.has(channel);\n }\n\n /**\n * Monotonic local revision of acknowledged subscription state.\n *\n * It increments when `subscribed` or `unsubscribed` changes the server-\n * acknowledged set. It is not a protocol sequence number and resets with a\n * new client instance; its purpose is external-store invalidation before a\n * consumer calls {@link isSubscribed} again.\n */\n getSubscriptionRevision(): number {\n return this.state.subscriptionRevision;\n }\n\n /**\n * A socket is open or opening and has not yet said `protocol.init`.\n *\n * Real state, read off the state machine. Consumers used to report a\n * literal `false` here and render \"Available\" over a socket that had never\n * connected; a toggle that cannot distinguish \"not live\" from \"cannot tell\n * yet\" is the shape of that bug.\n */\n isConnecting(): boolean {\n return this.state.connecting;\n }\n\n /** The last failure the socket gave up on, or null. See {@link SocketState.lastError}. */\n getLastError(): string | null {\n return this.state.lastError;\n }\n\n protected ensureConnection(): void {\n const session = this.resolveSession();\n if (!session) return;\n if (typeof window === 'undefined' || typeof WebSocket === 'undefined') return;\n if (this.state.ws || this.state.connecting) return;\n\n this.clearCloseTimer();\n this.state.connecting = true;\n this.state.intentionalClose = false;\n const generation = ++this.state.connectGeneration;\n // `connecting` is part of what consumers render, so entering it is a\n // change they have to hear about. Without this notify a `useSyncExternal`\n // store reading `isConnecting()` would only ever sample it on some later\n // unrelated transition.\n this.notifyConnectionListeners();\n\n void this.openSocket(session, generation);\n }\n\n /**\n * Mint this attempt's login token, then open the socket with it.\n *\n * A fresh token per attempt, reconnects included: a browser's token is\n * single-use and expires in seconds, so a token from an earlier attempt is\n * already spent.\n */\n private async openSocket(session: TSession, generation: number): Promise<void> {\n const token = await mintLoginToken(session.getLoginToken);\n if (generation !== this.state.connectGeneration) return;\n if (!token) {\n this.releaseUnopenedAttempt();\n return;\n }\n\n let ws: WebSocket;\n try {\n ws = new WebSocket(this.buildUrl(session));\n } catch {\n // A bad URL throws synchronously; without this the attempt would stay\n // `connecting` forever and every later `ensureConnection` would no-op.\n this.releaseUnopenedAttempt();\n return;\n }\n\n ws.onopen = () => {\n ws.send(JSON.stringify({ type: 'login', token }));\n };\n\n ws.onmessage = (event: MessageEvent) => {\n // Frames from a socket this client has already replaced belong to\n // nobody: acting on them would fold a stale connection's\n // subscription state into the live one.\n if (this.state.ws !== null && this.state.ws !== ws) return;\n try {\n const msg = JSON.parse(String(event.data)) as ProtocolInitMessage & {\n type?: string;\n channels?: string[];\n channel?: string;\n event?: ChannelEvent;\n errors?: unknown[];\n };\n\n if (msg.type === 'protocol.init' && !this.state.connected) {\n this.state.connected = true;\n this.readProtocolInit(msg);\n this.state.connecting = false;\n this.state.reconnectAttempts = 0;\n this.state.lastError = null;\n\n if (this.state.heartbeatTimer) clearInterval(this.state.heartbeatTimer);\n this.state.heartbeatTimer = setInterval(() => {\n if (this.state.ws?.readyState === WebSocket.OPEN) {\n this.state.ws.send(JSON.stringify({ type: 'heartbeat' }));\n }\n }, HEARTBEAT_INTERVAL_MS);\n\n if (this.state.listeners.size > 0) {\n ws.send(\n JSON.stringify({\n type: 'subscribe',\n channels: Array.from(this.state.listeners.keys()),\n })\n );\n }\n\n this.notifyConnectionListeners();\n return;\n }\n\n if (msg.type === 'subscribed' && Array.isArray(msg.channels)) {\n for (const ch of msg.channels) {\n this.state.subscribedChannels.add(ch);\n this.clearSubscriptionRetry(ch);\n }\n this.state.subscriptionRevision += 1;\n this.notifyConnectionListeners();\n return;\n }\n\n if (msg.type === 'subscribe_error' && Array.isArray(msg.errors)) {\n const retryableChannels: string[] = [];\n for (const rawError of msg.errors) {\n if (!rawError || typeof rawError !== 'object') continue;\n const error = rawError as { channel?: unknown; retryable?: unknown };\n if (typeof error.channel !== 'string') continue;\n\n if (error.retryable === true) {\n retryableChannels.push(error.channel);\n } else {\n this.clearSubscriptionRetry(error.channel);\n }\n }\n this.scheduleSubscriptionRetry(retryableChannels);\n return;\n }\n\n if (msg.type === 'unsubscribed' && Array.isArray(msg.channels)) {\n // The server also sends this unprompted when a grant change\n // revoked a subscription (account socket). Forgetting the\n // channel is what makes the client's next `subscribe` a real\n // re-authorization rather than a no-op against a stale set.\n for (const ch of msg.channels) {\n this.state.subscribedChannels.delete(ch);\n }\n this.state.subscriptionRevision += 1;\n this.notifyConnectionListeners();\n this.scheduleSubscriptionRetry(msg.channels);\n return;\n }\n\n if (msg.type === 'error') {\n // The server's own refusal — a rejected login, an unknown\n // message shape. Recorded rather than dropped: it is the one\n // failure the transport cannot tell the consumer about, because\n // the socket is open and will close looking like any other.\n const message = (msg as { message?: unknown }).message;\n this.state.lastError =\n typeof message === 'string' && message ? message : 'Realtime error';\n this.notifyConnectionListeners();\n return;\n }\n\n if (msg.type === 'event' && msg.channel && msg.event) {\n const channelListeners = this.state.listeners.get(msg.channel);\n if (channelListeners) {\n for (const channelListener of channelListeners) {\n try {\n channelListener(msg.event);\n } catch (err) {\n console.error('Channel listener error:', err);\n }\n }\n }\n }\n } catch {\n // Ignore malformed frames.\n }\n };\n\n ws.onclose = () => {\n // A close from a socket that is no longer this client's would\n // otherwise clear the REPLACEMENT connection's state: null its `ws`,\n // wipe its subscribed set, and stop its heartbeat — leaving a live\n // socket that believes it is subscribed to nothing and a client that\n // believes it is disconnected. Reachable whenever a `setSession`\n // replaced the socket while the old one's close was still in flight.\n if (this.state.ws !== null && this.state.ws !== ws) return;\n\n this.state.connected = false;\n this.state.connecting = false;\n this.state.ws = null;\n this.state.subscribedChannels.clear();\n this.clearSubscriptionRetry();\n\n if (this.state.heartbeatTimer) {\n clearInterval(this.state.heartbeatTimer);\n this.state.heartbeatTimer = null;\n }\n\n this.afterConnectionEnded();\n };\n\n ws.onerror = () => {\n // onclose will fire after this\n };\n\n this.state.ws = ws;\n }\n\n /**\n * End an attempt that never opened a socket. No `onclose` will run for it,\n * so the retry decision is made here, on the normal backoff.\n */\n private releaseUnopenedAttempt(): void {\n this.state.connecting = false;\n this.afterConnectionEnded();\n }\n\n /** Decide whether a connection that just ended (or never opened) retries. */\n private afterConnectionEnded(): void {\n const willRetry =\n !this.state.intentionalClose &&\n this.state.listeners.size > 0 &&\n this.state.reconnectAttempts < MAX_RECONNECT_ATTEMPTS;\n\n if (!willRetry && !this.state.intentionalClose && this.state.listeners.size > 0) {\n // Out of attempts with listeners still waiting: the client has\n // genuinely given up, which is the moment worth surfacing. Each\n // transient close on the way here is not — a socket that reconnects\n // in three seconds did not fail, and reporting it would train\n // people to ignore the indicator.\n this.state.lastError = 'Realtime connection lost';\n }\n\n this.notifyConnectionListeners();\n\n if (willRetry) {\n this.state.reconnectAttempts += 1;\n this.state.reconnectTimer = setTimeout(() => {\n this.ensureConnection();\n }, RECONNECT_DELAY_MS * this.state.reconnectAttempts);\n }\n }\n\n protected closeConnection(): void {\n this.state.intentionalClose = true;\n this.state.connectGeneration += 1;\n this.clearCloseTimer();\n if (this.state.reconnectTimer) {\n clearTimeout(this.state.reconnectTimer);\n this.state.reconnectTimer = null;\n }\n if (this.state.heartbeatTimer) {\n clearInterval(this.state.heartbeatTimer);\n this.state.heartbeatTimer = null;\n }\n this.state.ws?.close();\n this.state.ws = null;\n this.state.connected = false;\n // Only a deliberate close forgets who we were; a transient `onclose`\n // leaves the identity in place because it is unchanged across a\n // reconnect, and the next `protocol.init` overwrites it anyway.\n this.clearIdentity();\n this.state.connecting = false;\n this.state.subscribedChannels.clear();\n this.clearSubscriptionRetry();\n this.notifyConnectionListeners();\n }\n}\n","/**\n * Unified workspace WebSocket client for real-time events.\n *\n * Mirrors `/api/v2/w/{wid}/ws/realtime`: log in with a token from the session's\n * `getLoginToken` provider (fresh per connect attempt),\n * subscribe to channels, receive AGUI events.\n *\n * The socket state machine — login, heartbeat, reconnect, subscribe retry —\n * lives in {@link RealtimeSocketClient}, shared with the account client. This\n * file is what is specific to the workspace mount: its URL, and the member id\n * `protocol.init` hands back.\n */\n\nimport { RealtimeSocketClient, type ProtocolInitMessage } from './socket-client';\nimport type { ChannelListener, RealtimeAuthSession, Unsubscribe } from './types';\n\n/** Build the workspace realtime WebSocket URL from session config. */\nexport function buildWorkspaceWsUrl(session: RealtimeAuthSession): string {\n const isSsl = session.apiUrl.startsWith('https');\n const host = session.apiUrl.replace(/^https?:\\/\\//, '');\n return `${isSsl ? 'wss' : 'ws'}://${host}/api/v2/w/${session.workspaceUuid}/ws/realtime`;\n}\n\nexport class WorkspaceRealtimeClient extends RealtimeSocketClient<RealtimeAuthSession> {\n private memberId: number | null = null;\n\n protected buildUrl(session: RealtimeAuthSession): string {\n return buildWorkspaceWsUrl(session);\n }\n\n protected readProtocolInit(msg: ProtocolInitMessage): void {\n this.memberId = typeof msg.memberId === 'number' ? msg.memberId : null;\n }\n\n protected clearIdentity(): void {\n this.memberId = null;\n }\n\n protected sessionRequiresReconnect(\n previous: RealtimeAuthSession | null,\n next: RealtimeAuthSession\n ): boolean {\n return (\n super.sessionRequiresReconnect(previous, next) ||\n previous?.workspaceUuid !== next.workspaceUuid\n );\n }\n\n /**\n * Member id the server authenticated this socket as, or null before\n * `protocol.init` arrives.\n *\n * Channels whose Redis key is workspace-wide can carry events addressed to\n * one member (presence acks, for example). Consumers compare the event's\n * addressee against this so a reply meant for another operator cannot be\n * applied locally.\n */\n getMemberId(): number | null {\n return this.memberId;\n }\n}\n\nconst defaultClient = new WorkspaceRealtimeClient();\n\nexport function initWorkspaceSocket(session: RealtimeAuthSession): void {\n defaultClient.setSession(session);\n}\n\nexport function subscribeWorkspaceChannel(channel: string, listener: ChannelListener): Unsubscribe {\n return defaultClient.subscribe(channel, listener);\n}\n\nexport function onWorkspaceSocketConnection(listener: (connected: boolean) => void): () => void {\n return defaultClient.onConnection(listener);\n}\n\nexport function isWorkspaceSocketConnected(): boolean {\n return defaultClient.isConnected();\n}\n","/**\n * Account WebSocket client — one socket per tab, rooted at the Account.\n *\n * Mirrors `/api/v2/ws/realtime`. No workspace in the URL and none in the\n * session: the login token is the whole identity, and what the socket may see comes\n * from the server's grant map, refreshed there.\n *\n * See `docs/projects/active/account-socket.md`. The point is not a second\n * socket — it is *one*. An operator covering four workspaces holds four\n * `WorkspaceRealtimeClient`s today, which is four connections, four\n * heartbeats, and no place to stand for \"what is happening across my estate\".\n * Workspace-scoped channels are reached here by qualifying their names, so the\n * same channel handlers serve both mounts and the per-workspace socket can be\n * deleted at cutover rather than lived alongside.\n *\n * ```ts\n * const client = new AccountRealtimeClient({ apiUrl, getLoginToken });\n * client.subscribe(workspaceChannel(wid, 'activity'), onInvalidate);\n * client.subscribe(ACCOUNT_ACTIVITY_CHANNEL, onInvalidate);\n * ```\n */\n\nimport { RealtimeSocketClient, type ProtocolInitMessage } from './socket-client';\nimport type { AccountRealtimeSession, ChannelListener, Unsubscribe } from './types';\n\n/** Build the account realtime WebSocket URL from session config. */\nexport function buildAccountWsUrl(session: AccountRealtimeSession): string {\n const isSsl = session.apiUrl.startsWith('https');\n const host = session.apiUrl.replace(/^https?:\\/\\//, '');\n return `${isSsl ? 'wss' : 'ws'}://${host}/api/v2/ws/realtime`;\n}\n\n/**\n * Qualify a workspace-scoped channel name for the account socket.\n *\n * `chat:42:monitor` in workspace `W` is `w/W/chat:42:monitor` here. Exported\n * as a function rather than left to callers' template strings for the reason\n * every channel name constant in this package exists: the server matches\n * exactly, so a typo subscribes successfully to nothing.\n */\nexport function workspaceChannel(workspaceUuid: string, channel: string): string {\n return `w/${workspaceUuid}/${channel}`;\n}\n\nexport class AccountRealtimeClient extends RealtimeSocketClient<AccountRealtimeSession> {\n private accountUuid: string | null = null;\n private workspaceUuids: readonly string[] = [];\n\n protected buildUrl(session: AccountRealtimeSession): string {\n return buildAccountWsUrl(session);\n }\n\n protected readProtocolInit(msg: ProtocolInitMessage): void {\n this.accountUuid = typeof msg.accountUuid === 'string' ? msg.accountUuid : null;\n this.workspaceUuids = Array.isArray(msg.workspaceUuids)\n ? msg.workspaceUuids.filter((value): value is string => typeof value === 'string')\n : [];\n }\n\n protected clearIdentity(): void {\n this.accountUuid = null;\n this.workspaceUuids = [];\n }\n\n /** Account uuid the server authenticated this socket as, or null. */\n getAccountUuid(): string | null {\n return this.accountUuid;\n }\n\n /**\n * Workspaces the server's grant map admitted at `protocol.init`.\n *\n * A **hint for rendering**, never an authorization decision: the server\n * re-checks its own map on every subscribe, and refreshes it on a floor, so\n * this snapshot can be stale in either direction. Use it to decide which\n * panes are worth opening, not which data a viewer may see.\n */\n getWorkspaceUuids(): readonly string[] {\n return this.workspaceUuids;\n }\n}\n\nconst defaultClient = new AccountRealtimeClient();\n\nexport function initAccountSocket(session: AccountRealtimeSession): void {\n defaultClient.setSession(session);\n}\n\nexport function subscribeAccountChannel(channel: string, listener: ChannelListener): Unsubscribe {\n return defaultClient.subscribe(channel, listener);\n}\n\nexport function onAccountSocketConnection(listener: (connected: boolean) => void): () => void {\n return defaultClient.onConnection(listener);\n}\n\nexport function isAccountSocketConnected(): boolean {\n return defaultClient.isConnected();\n}\n","/** PCM16 helpers aligned with main-app web voice (24 kHz). */\n\nexport const SERVER_SAMPLE_RATE = 24000;\nexport const TARGET_SAMPLE_RATE = 24000;\nexport const MONITOR_SAMPLE_RATE = 16000;\n\nexport function float32ArrayToBase64(float32Array: Float32Array): string {\n const int16Array = new Int16Array(float32Array.length);\n for (let i = 0; i < float32Array.length; i += 1) {\n const sample = Math.max(-1, Math.min(1, float32Array[i]));\n int16Array[i] = Math.round(sample * 32767);\n }\n const uint8Array = new Uint8Array(int16Array.buffer);\n let binaryString = '';\n for (let i = 0; i < uint8Array.length; i += 1) {\n binaryString += String.fromCharCode(uint8Array[i]);\n }\n return btoa(binaryString);\n}\n\nexport function base64ToFloat32Array(base64: string): Float32Array {\n const binaryString = atob(base64);\n const len = binaryString.length / 2;\n const float32Array = new Float32Array(len);\n for (let i = 0; i < len; i += 1) {\n const lo = binaryString.charCodeAt(i * 2);\n const hi = binaryString.charCodeAt(i * 2 + 1);\n let int16 = (hi << 8) | lo;\n if (int16 >= 0x8000) int16 -= 0x10000;\n float32Array[i] = int16 / 32768;\n }\n return float32Array;\n}\n\nexport function concatFloat32Arrays(arrays: Float32Array[]): Float32Array {\n const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);\n const result = new Float32Array(totalLength);\n let offset = 0;\n for (const arr of arrays) {\n result.set(arr, offset);\n offset += arr.length;\n }\n return result;\n}\n\nexport function downsampleAudio(\n audioData: Float32Array,\n originalSampleRate: number,\n targetSampleRate: number\n): Float32Array {\n if (originalSampleRate === targetSampleRate) return audioData;\n const ratio = originalSampleRate / targetSampleRate;\n const newLength = Math.round(audioData.length / ratio);\n const result = new Float32Array(newLength);\n for (let i = 0; i < newLength; i += 1) {\n const srcIndex = Math.floor(i * ratio);\n result[i] = audioData[srcIndex];\n }\n return result;\n}\n","/**\n * Human operator session WebSocket client.\n *\n * Connects to `/api/v2/w/{wid}/operator/chats/{chatId}/session-ws` — AGUI\n * events, bidirectional PCM, and call control. No AI inference.\n */\n\nimport {\n base64ToFloat32Array,\n float32ArrayToBase64,\n SERVER_SAMPLE_RATE,\n TARGET_SAMPLE_RATE,\n} from '../audio/pcm';\nimport { mintLoginToken } from './login-token';\nimport type {\n ChannelEvent,\n OperatorCallPhase,\n OperatorSessionState,\n RealtimeAuthSession,\n} from './types';\n\nexport interface OperatorSessionHandlers {\n onSessionStateChange?: (state: OperatorSessionState) => void;\n onCallPhaseChange?: (phase: OperatorCallPhase) => void;\n onAguiEvent?: (event: ChannelEvent) => void;\n onError?: (message: string) => void;\n /** Fired when the server closes the session (e.g. callee hung up). */\n onSessionClosed?: (reason?: string) => void;\n}\n\nexport class OperatorSessionClient {\n private ws: WebSocket | null = null;\n private audioContext: AudioContext | null = null;\n private mediaStream: MediaStream | null = null;\n private processor: ScriptProcessorNode | null = null;\n private sessionState: OperatorSessionState = 'idle';\n private callPhase: OperatorCallPhase = 'idle';\n private nextPlaybackTime = 0;\n private playbackSources: AudioBufferSourceNode[] = [];\n private micActive = false;\n private onHold = false;\n private muted = false;\n private receivedRemoteAudio = false;\n private heartbeatTimer: ReturnType<typeof setInterval> | null = null;\n private sessionCloseNotified = false;\n private intentionalDisconnect = false;\n /** Bumped by every connect and disconnect; a stale connect stops at its next await. */\n private connectGeneration = 0;\n\n constructor(private handlers: OperatorSessionHandlers = {}) {}\n\n getSessionState(): OperatorSessionState {\n return this.sessionState;\n }\n\n getCallPhase(): OperatorCallPhase {\n return this.callPhase;\n }\n\n private setSessionState(next: OperatorSessionState): void {\n this.sessionState = next;\n this.handlers.onSessionStateChange?.(next);\n }\n\n private setCallPhase(next: OperatorCallPhase): void {\n this.callPhase = next;\n this.handlers.onCallPhaseChange?.(next);\n }\n\n private notifySessionClosed(reason?: string): void {\n if (this.sessionCloseNotified) return;\n this.sessionCloseNotified = true;\n this.handlers.onSessionClosed?.(reason);\n }\n\n async connect(session: RealtimeAuthSession, chatId: number): Promise<void> {\n if (typeof window === 'undefined' || typeof WebSocket === 'undefined') {\n throw new Error('Operator session WebSocket is not available in this environment');\n }\n\n await this.disconnect();\n const generation = ++this.connectGeneration;\n this.sessionCloseNotified = false;\n this.setSessionState('connecting');\n this.setCallPhase('connecting');\n\n const isSsl = session.apiUrl.startsWith('https');\n const host = session.apiUrl.replace(/^https?:\\/\\//, '');\n const wsUrl = `${isSsl ? 'wss' : 'ws'}://${host}/api/v2/w/${session.workspaceUuid}/operator/chats/${chatId}/session-ws`;\n\n // One token per connect: a browser's is single-use and expires in seconds.\n const token = await mintLoginToken(session.getLoginToken);\n if (generation !== this.connectGeneration) {\n throw new Error('Operator session connect superseded');\n }\n if (!token) {\n this.setSessionState('error');\n this.setCallPhase('idle');\n throw new Error('Realtime login token unavailable');\n }\n\n await new Promise<void>((resolve, reject) => {\n const ws = new WebSocket(wsUrl);\n this.ws = ws;\n\n ws.onopen = () => {\n ws.send(JSON.stringify({ type: 'login', token }));\n };\n\n ws.onmessage = (event) => {\n try {\n const msg = JSON.parse(String(event.data)) as {\n type?: string;\n name?: string;\n value?: Record<string, unknown>;\n message?: string;\n code?: string;\n event?: ChannelEvent;\n };\n this.handleMessage(msg, resolve, reject);\n } catch {\n /* ignore malformed frames */\n }\n };\n\n ws.onerror = () => {\n const message = 'Operator session WebSocket error';\n const wasConnecting = this.sessionState === 'connecting';\n this.setSessionState('error');\n if (wasConnecting) {\n this.setCallPhase('idle');\n reject(new Error(message));\n } else {\n this.setCallPhase('failed');\n this.handlers.onError?.(message);\n }\n };\n\n ws.onclose = () => {\n this.clearHeartbeat();\n this.stopMicrophone();\n this.stopPlayback();\n const wasConnected = this.sessionState === 'connected';\n this.setSessionState('idle');\n this.setCallPhase('idle');\n if (wasConnected && !this.intentionalDisconnect) {\n this.notifySessionClosed('disconnect');\n }\n this.intentionalDisconnect = false;\n };\n });\n }\n\n private handleMessage(\n msg: {\n type?: string;\n name?: string;\n value?: Record<string, unknown>;\n message?: string;\n code?: string;\n event?: ChannelEvent;\n },\n resolveLogin: () => void,\n rejectLogin: (err: Error) => void\n ): void {\n if (msg.type === 'RUN_ERROR' || msg.type === 'error') {\n const message = msg.message ?? 'Operator session error';\n if (this.sessionState === 'connecting') {\n this.setSessionState('error');\n rejectLogin(new Error(message));\n } else {\n this.setCallPhase('failed');\n this.handlers.onError?.(message);\n }\n return;\n }\n\n if (msg.type === 'protocol.init' && this.sessionState === 'connecting') {\n return;\n }\n\n if (msg.type === 'CUSTOM' && msg.name === 'operator.session_ready') {\n this.setSessionState('connected');\n this.setCallPhase('ready');\n this.startHeartbeat();\n resolveLogin();\n return;\n }\n\n if (msg.type === 'event' && msg.event) {\n if (this.handleRemoteVoiceAudio(msg.event)) {\n return;\n }\n this.handlers.onAguiEvent?.(msg.event);\n return;\n }\n\n if (msg.type === 'CUSTOM' && msg.name === 'operator.call.started') {\n this.receivedRemoteAudio = false;\n this.setCallPhase('dialing');\n return;\n }\n\n if (msg.type === 'CUSTOM' && msg.name === 'operator.call.ended') {\n this.onHold = false;\n this.muted = false;\n this.receivedRemoteAudio = false;\n this.stopMicrophone();\n this.stopPlayback();\n const endSession = msg.value?.end_session === true;\n const reason = typeof msg.value?.reason === 'string' ? msg.value.reason : undefined;\n if (endSession) {\n this.setCallPhase('idle');\n this.notifySessionClosed(reason ?? 'peer_hangup');\n } else {\n this.setCallPhase('ready');\n }\n return;\n }\n }\n\n /** Play callee PCM from wrapped chat-stream events; returns true if handled. */\n private handleRemoteVoiceAudio(event: ChannelEvent): boolean {\n if (event.type !== 'CUSTOM' || event.name !== 'voice.audio') {\n return false;\n }\n if (this.onHold) return true;\n\n const value = event.value;\n if (!value || typeof value !== 'object') return true;\n\n const data = (value as Record<string, unknown>).data;\n if (typeof data !== 'string' || !data) return true;\n\n if (\n !this.receivedRemoteAudio &&\n (this.callPhase === 'dialing' || this.callPhase === 'live')\n ) {\n this.receivedRemoteAudio = true;\n this.setCallPhase('live');\n }\n void this.playPcm16(base64ToFloat32Array(data));\n return true;\n }\n\n private startHeartbeat(): void {\n this.clearHeartbeat();\n this.heartbeatTimer = setInterval(() => {\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(JSON.stringify({ type: 'heartbeat' }));\n }\n }, 30_000);\n }\n\n private clearHeartbeat(): void {\n if (this.heartbeatTimer) {\n clearInterval(this.heartbeatTimer);\n this.heartbeatTimer = null;\n }\n }\n\n private sendCustom(name: string, value: Record<string, unknown> = {}): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n this.ws.send(JSON.stringify({ type: 'CUSTOM', name, value }));\n }\n\n async placeCall(memberId: number): Promise<void> {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n if (this.sessionState !== 'connected') {\n throw new Error('Operator session is not ready');\n }\n await this.startMicrophone();\n this.sendCustom('operator.call.place', { member_id: memberId });\n }\n\n /**\n * Answer an inbound operator call the caller has already been routed to.\n *\n * Symmetric with {@link placeCall} but for the inbound direction: instead of\n * dialing an outbound leg it tells the backend to bridge the already-ringing\n * caller to this operator session. The backend (see\n * `lib/operator_session_handler.py::_answer_inbound_call`) registers the\n * audio bridge, marks the ring state `answered`, and replies with\n * `operator.call.started` — after which callee PCM arrives as `voice.audio`\n * events (played via {@link playPcm16}) and mic audio streams the other way,\n * exactly like an outbound call.\n *\n * Must be called once the session is ready (state `connected`). Starts the\n * microphone before sending so the operator's audio is flowing the instant\n * the bridge registers.\n */\n async answerCall(interactionId: string, answerToken: string): Promise<void> {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n if (this.sessionState !== 'connected') {\n throw new Error('Operator session is not ready');\n }\n await this.startMicrophone();\n this.sendCustom('operator.call.answer', {\n interaction_id: interactionId,\n answer_token: answerToken,\n });\n }\n\n /**\n * Mute or unmute the operator's outbound microphone. Muting gates mic frames\n * locally (the caller stops hearing the operator) while callee audio keeps\n * playing — distinct from {@link setHold}, which is a two-way pause the\n * backend is told about. Purely client-side; no server round-trip.\n */\n setMuted(muted: boolean): void {\n this.muted = muted;\n }\n\n isMuted(): boolean {\n return this.muted;\n }\n\n hangUp(): void {\n try {\n this.sendCustom('operator.call.hangup');\n } catch {\n /* socket already closed */\n }\n this.onHold = false;\n this.receivedRemoteAudio = false;\n }\n\n forwardCall(phoneNumber: string): void {\n this.receivedRemoteAudio = false;\n this.setCallPhase('dialing');\n this.sendCustom('operator.call.forward', { phone_number: phoneNumber });\n }\n\n setHold(onHold: boolean): void {\n this.onHold = onHold;\n this.sendCustom('operator.call.hold', { on_hold: onHold });\n if (this.callPhase === 'live' || this.callPhase === 'onHold') {\n this.setCallPhase(onHold ? 'onHold' : 'live');\n }\n if (onHold) {\n this.stopPlayback();\n }\n }\n\n async startMicrophone(): Promise<void> {\n if (typeof window === 'undefined') return;\n if (!navigator.mediaDevices?.getUserMedia) {\n throw new Error('Microphone not available in this browser');\n }\n if (this.micActive) return;\n\n this.mediaStream = await navigator.mediaDevices.getUserMedia({\n audio: {\n echoCancellation: true,\n noiseSuppression: true,\n autoGainControl: true,\n },\n });\n\n const Ctx =\n window.AudioContext ??\n (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;\n if (!Ctx) throw new Error('Web Audio not supported');\n\n this.audioContext = new Ctx();\n if (this.audioContext.state === 'suspended') {\n await this.audioContext.resume();\n }\n\n const source = this.audioContext.createMediaStreamSource(this.mediaStream);\n const processor = this.audioContext.createScriptProcessor(4096, 1, 1);\n this.processor = processor;\n\n processor.onaudioprocess = (event) => {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;\n if (this.onHold) return;\n if (this.muted) return;\n if (\n this.callPhase !== 'dialing' &&\n this.callPhase !== 'live' &&\n this.callPhase !== 'onHold'\n ) {\n return;\n }\n\n const input = event.inputBuffer.getChannelData(0);\n const copy = new Float32Array(input.length);\n copy.set(input);\n\n const sampleRate = this.audioContext?.sampleRate ?? TARGET_SAMPLE_RATE;\n let pcm = copy;\n if (sampleRate !== TARGET_SAMPLE_RATE) {\n const ratio = sampleRate / TARGET_SAMPLE_RATE;\n const newLen = Math.floor(copy.length / ratio);\n const down = new Float32Array(newLen);\n for (let i = 0; i < newLen; i += 1) {\n down[i] = copy[Math.floor(i * ratio)];\n }\n pcm = down;\n }\n\n this.ws.send(\n JSON.stringify({\n type: 'CUSTOM',\n name: 'voice.audio',\n value: {\n data: float32ArrayToBase64(pcm),\n format: 'pcm16',\n sample_rate: TARGET_SAMPLE_RATE,\n },\n })\n );\n };\n\n source.connect(processor);\n processor.connect(this.audioContext.destination);\n this.micActive = true;\n }\n\n stopMicrophone(): void {\n this.processor?.disconnect();\n this.processor = null;\n for (const track of this.mediaStream?.getTracks() ?? []) {\n track.stop();\n }\n this.mediaStream = null;\n this.micActive = false;\n }\n\n private async playPcm16(samples: Float32Array): Promise<void> {\n if (typeof window === 'undefined') return;\n\n const Ctx =\n window.AudioContext ??\n (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;\n if (!Ctx) return;\n\n if (!this.audioContext || this.audioContext.state === 'closed') {\n this.audioContext = new Ctx();\n }\n if (this.audioContext.state === 'suspended') {\n await this.audioContext.resume();\n }\n\n const buffer = this.audioContext.createBuffer(1, samples.length, SERVER_SAMPLE_RATE);\n buffer.copyToChannel(new Float32Array(samples), 0);\n\n const source = this.audioContext.createBufferSource();\n source.buffer = buffer;\n source.connect(this.audioContext.destination);\n\n const now = this.audioContext.currentTime;\n const startAt = this.nextPlaybackTime < now + 0.02 ? now + 0.02 : this.nextPlaybackTime;\n source.start(startAt);\n this.nextPlaybackTime = startAt + buffer.duration;\n this.playbackSources.push(source);\n source.onended = () => {\n const idx = this.playbackSources.indexOf(source);\n if (idx >= 0) this.playbackSources.splice(idx, 1);\n };\n }\n\n private stopPlayback(): void {\n for (const src of this.playbackSources) {\n try {\n src.stop();\n } catch {\n /* already stopped */\n }\n }\n this.playbackSources = [];\n this.nextPlaybackTime = 0;\n }\n\n async disconnect(): Promise<void> {\n this.connectGeneration += 1;\n this.intentionalDisconnect = true;\n if (\n this.callPhase === 'dialing' ||\n this.callPhase === 'live' ||\n this.callPhase === 'onHold'\n ) {\n this.hangUp();\n }\n this.clearHeartbeat();\n this.stopMicrophone();\n this.stopPlayback();\n if (this.ws) {\n this.ws.close();\n this.ws = null;\n }\n if (this.audioContext && this.audioContext.state !== 'closed') {\n await this.audioContext.close().catch(() => undefined);\n }\n this.audioContext = null;\n this.onHold = false;\n this.muted = false;\n this.receivedRemoteAudio = false;\n this.setSessionState('idle');\n this.setCallPhase('idle');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACoDO,SAAS,kBAAkB,OAAmC;AAClE,SAAO,UAAU,aAAa,UAAU,UAAU,UAAU;AAC/D;AAgBO,IAAM,mBAAmB;AACzB,IAAM,0BAA0B;AAUhC,IAAM,2BAA2B;AAkBjC,IAAM,2BAA2B;AAGjC,IAAM,mBAAmB;AAuBzB,SAAS,uBAAuB,OAAyD;AAC7F,MAAI,MAAM,SAAS,aAAc,QAAO;AACxC,QAAM,OAAQ,MAA6B;AAC3C,MAAI,OAAO,SAAS,YAAY,SAAS,KAAM,QAAO;AACtD,QAAM,QAAS,KAA6B;AAC5C,SAAO,UAAU,eAAe,UAAU,YAAY,UAAU;AACnE;;;AC5HO,IAAM,yBAAyB;AAQtC,eAAsB,eAAe,UAA8D;AAChG,MAAI;AACJ,MAAI;AACD,UAAM,QAAiB,MAAM,QAAQ,KAAK;AAAA,MACvC,SAAS;AAAA,MACT,IAAI,QAAc,CAAC,YAAY;AAC5B,gBAAQ,WAAW,MAAM,QAAQ,IAAI,GAAG,sBAAsB;AAAA,MACjE,CAAC;AAAA,IACJ,CAAC;AACD,WAAO,OAAO,UAAU,YAAY,MAAM,SAAS,IAAI,QAAQ;AAAA,EAClE,QAAQ;AACL,WAAO;AAAA,EACV,UAAE;AACC,QAAI,UAAU,OAAW,cAAa,KAAK;AAAA,EAC9C;AACH;;;ACPA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M,UAAU,KAAK,mBAAmB,MAAM,OAAO,MAAM,OAAO,KAAK;AACvE,WAAO,MAAM,OAAO;AACpB,SAAK,mBAAmB,UAAU,OAAO;AACzC,SAAK,gBAAgB,KAAK,MAAM;AAChC,WAAO,UAAU,MAAM;AACpB,YAAM,MAAM,KAAK,gBAAgB,QAAQ,MAAM;AAC/C,UAAI,OAAO,EAAG,MAAK,gBAAgB,OAAO,KAAK,CAAC;AAAA,IACnD;AAAA,EACH;AAAA,EAEQ,eAAqB;AAC1B,eAAW,OAAO,KAAK,iBAAiB;AACrC,UAAI;AACD,YAAI,KAAK;AAAA,MACZ,QAAQ;AAAA,MAER;AAAA,IACH;AACA,SAAK,kBAAkB,CAAC;AACxB,SAAK,mBAAmB;AAAA,EAC3B;AAAA,EAEA,MAAM,aAA4B;AAC/B,SAAK,qBAAqB;AAC1B,SAAK,wBAAwB;AAC7B,QACG,KAAK,cAAc,aACnB,KAAK,cAAc,UACnB,KAAK,cAAc,UACpB;AACC,WAAK,OAAO;AAAA,IACf;AACA,SAAK,eAAe;AACpB,SAAK,eAAe;AACpB,SAAK,aAAa;AAClB,QAAI,KAAK,IAAI;AACV,WAAK,GAAG,MAAM;AACd,WAAK,KAAK;AAAA,IACb;AACA,QAAI,KAAK,gBAAgB,KAAK,aAAa,UAAU,UAAU;AAC5D,YAAM,KAAK,aAAa,MAAM,EAAE,MAAM,MAAM,MAAS;AAAA,IACxD;AACA,SAAK,eAAe;AACpB,SAAK,SAAS;AACd,SAAK,QAAQ;AACb,SAAK,sBAAsB;AAC3B,SAAK,gBAAgB,MAAM;AAC3B,SAAK,aAAa,MAAM;AAAA,EAC3B;AACH;","names":["defaultClient"]}
|
|
1
|
+
{"version":3,"sources":["../src/realtime/index.ts","../src/realtime/types.ts","../src/realtime/login-token.ts","../src/realtime/socket-client.ts","../src/realtime/workspace-client.ts","../src/realtime/account-client.ts","../src/audio/pcm.ts","../src/realtime/operator-session-client.ts"],"sourcesContent":["export {\n type ChannelEvent,\n type ChannelListener,\n type RealtimeAuthSession,\n type RealtimeSessionBase,\n type RealtimeLoginTokenProvider,\n type AccountRealtimeSession,\n type OperatorSessionState,\n type OperatorCallPhase,\n type Unsubscribe,\n isActiveCallPhase,\n ACTIVITY_CHANNEL,\n ACTIVITY_MEMBER_CHANNEL,\n ACCOUNT_ACTIVITY_CHANNEL,\n ACCOUNT_PRESENCE_CHANNEL,\n PRESENCE_CHANNEL,\n type ActivityChannelName,\n type ActivityInvalidationEvent,\n isActivityInvalidation,\n} from './types';\n\nexport {\n WorkspaceRealtimeClient,\n buildWorkspaceWsUrl,\n initWorkspaceSocket,\n subscribeWorkspaceChannel,\n onWorkspaceSocketConnection,\n isWorkspaceSocketConnected,\n} from './workspace-client';\n\nexport {\n AccountRealtimeClient,\n buildAccountWsUrl,\n workspaceChannel,\n initAccountSocket,\n subscribeAccountChannel,\n onAccountSocketConnection,\n isAccountSocketConnected,\n} from './account-client';\n\nexport { RealtimeSocketClient, type ProtocolInitMessage } from './socket-client';\n\nexport { LOGIN_TOKEN_TIMEOUT_MS } from './login-token';\n\nexport { OperatorSessionClient, type OperatorSessionHandlers } from './operator-session-client';\n","/** Event received from a subscribed workspace realtime channel. */\nexport interface ChannelEvent {\n type: string;\n [key: string]: unknown;\n}\n\nexport type ChannelListener = (event: ChannelEvent) => void;\nexport type Unsubscribe = () => void;\n\n/**\n * What every realtime connection needs: where the API is, and who is asking.\n *\n * The account socket needs nothing more — its scope comes from the server's\n * grant map, not from the URL — so this is the base rather than a widening of\n * {@link RealtimeAuthSession}. A workspace session is assignable to it, which\n * is what lets one session object drive both clients during cutover.\n */\nexport interface RealtimeSessionBase {\n apiUrl: string;\n /**\n * The credential for one `login` frame, fetched once per connect attempt —\n * reconnects included — and never reused.\n *\n * In a browser this mints a single-use `gr_wst_` token same-origin (the\n * server refuses anything else from a browser handshake), so page script\n * never holds the account session. A non-browser client (Node, a CLI) may\n * return its bearer credential. Resolve `null` when no token can be had;\n * the client treats that attempt as failed and retries on its backoff.\n *\n * The provider's identity is part of the session: replacing it with a\n * different function reconnects the socket.\n */\n getLoginToken: RealtimeLoginTokenProvider;\n}\n\n/** Mints the token for one realtime `login` frame. See {@link RealtimeSessionBase}. */\nexport type RealtimeLoginTokenProvider = () => Promise<string | null>;\n\n/** Auth context for workspace realtime WebSocket connections. */\nexport interface RealtimeAuthSession extends RealtimeSessionBase {\n workspaceUuid: string;\n}\n\n/** Auth context for the account realtime WebSocket. No workspace, by design. */\nexport type AccountRealtimeSession = RealtimeSessionBase;\n\nexport type OperatorSessionState = 'idle' | 'connecting' | 'connected' | 'error';\n\nexport type OperatorCallPhase =\n 'idle' | 'connecting' | 'ready' | 'dialing' | 'live' | 'onHold' | 'ended' | 'failed';\n\n/** Phases where Hold / Forward / Hang up apply to an active phone leg. */\nexport function isActiveCallPhase(phase: OperatorCallPhase): boolean {\n return phase === 'dialing' || phase === 'live' || phase === 'onHold';\n}\n\n// ---------------------------------------------------------------------------\n// Activity Spine (docs/projects/active/activity-spine.md)\n// ---------------------------------------------------------------------------\n\n/**\n * The two activity lanes a client can watch.\n *\n * `activity` is the whole workspace's feed; `activity:me` is the connected\n * member's own private lane and open items, kept separate so a busy workspace\n * does not wake every operator's badge on every event.\n *\n * Both are named constants rather than interpolated strings because the server\n * matches them exactly — a typo would subscribe successfully to nothing.\n */\nexport const ACTIVITY_CHANNEL = 'activity' as const;\nexport const ACTIVITY_MEMBER_CHANNEL = 'activity:me' as const;\n\n/**\n * The account's own lane, on the account socket only.\n *\n * Carries two things the workspace lanes cannot: the account's\n * `account_private` rows (manager / DM / agent-job chats, member-targeted\n * routine runs, and the concierge lane, which has no workspace at all), and a\n * change to the account's own grants.\n */\nexport const ACCOUNT_ACTIVITY_CHANNEL = 'account:activity' as const;\n\nexport type ActivityChannelName =\n typeof ACTIVITY_CHANNEL | typeof ACTIVITY_MEMBER_CHANNEL | typeof ACCOUNT_ACTIVITY_CHANNEL;\n\n/**\n * Operator presence, on the account socket only.\n *\n * The channel operator presence is **written** on. Presence state lives once,\n * at the account (`docs/projects/active/account-socket.md` § \"Presence,\n * migrated\"), and is projected into each chosen workspace's live set — so\n * going live is one command here rather than one per workspace socket.\n *\n * Unlike the activity lanes this one is bidirectional and does carry a\n * payload, because it is not an invalidation lane: `presence_confirmed`\n * reports the scope set the server actually honoured, which is the answer to\n * the command and is not re-fetchable from anywhere else.\n */\nexport const ACCOUNT_PRESENCE_CHANNEL = 'account:presence' as const;\n\n/** The workspace-scoped presence channel, on the per-workspace socket. */\nexport const PRESENCE_CHANNEL = 'presence' as const;\n\n/**\n * What an activity channel delivers — and deliberately all it delivers.\n *\n * **No event payloads travel over this socket.** The signal says *look again*;\n * the look goes through `POST /activity/query` (or `/counts` for a badge) under\n * the client's own session, where it is authorized against a freshly compiled\n * grant set and recorded as a bulk PHI access. A payload here would be an\n * authorization decision frozen at publish time by a producer that knows\n * nothing about who is listening — and a read that left no audit record.\n *\n * So: refetch on this, never render from it.\n */\nexport interface ActivityInvalidationEvent extends ChannelEvent {\n type: 'invalidate';\n data: {\n scope: 'workspace' | 'member' | 'account';\n reason: 'drain';\n };\n}\n\n/** Narrow a channel event to an activity invalidation. */\nexport function isActivityInvalidation(event: ChannelEvent): event is ActivityInvalidationEvent {\n if (event.type !== 'invalidate') return false;\n const data = (event as { data?: unknown }).data;\n if (typeof data !== 'object' || data === null) return false;\n const scope = (data as { scope?: unknown }).scope;\n return scope === 'workspace' || scope === 'member' || scope === 'account';\n}\n","import type { RealtimeLoginTokenProvider } from './types';\n\n/**\n * How long one connect attempt waits for its login token. A provider that\n * never settles (a hung mint request) must not hold the attempt open forever:\n * past this the attempt fails like any other and the caller's backoff applies.\n */\nexport const LOGIN_TOKEN_TIMEOUT_MS = 10_000;\n\n/**\n * Call `provider` once, bounded by {@link LOGIN_TOKEN_TIMEOUT_MS}.\n *\n * Resolves the token, or `null` when the provider throws, times out, or\n * returns anything but a non-empty string. Never rejects.\n */\nexport async function mintLoginToken(provider: RealtimeLoginTokenProvider): Promise<string | null> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n const token: unknown = await Promise.race([\n provider(),\n new Promise<null>((resolve) => {\n timer = setTimeout(() => resolve(null), LOGIN_TOKEN_TIMEOUT_MS);\n }),\n ]);\n return typeof token === 'string' && token.length > 0 ? token : null;\n } catch {\n return null;\n } finally {\n if (timer !== undefined) clearTimeout(timer);\n }\n}\n","/**\n * The realtime socket state machine, shared by both mounts.\n *\n * Two sockets speak the same protocol — `/api/v2/w/{wid}/ws/realtime` and\n * `/api/v2/ws/realtime` — and differ in exactly two places: the URL they open,\n * and the identity `protocol.init` hands back. Everything else (login,\n * heartbeat, reconnect with backoff, per-channel subscribe retry, listener\n * fan-out, deferred close) is one implementation here rather than two that\n * drift.\n *\n * That matters more than it sounds. The subscribe-retry and reconnect logic is\n * where the sharp edges live, and a second copy of it would be a second place\n * for a fix to be applied once.\n *\n * Protocol:\n * Client → Server: login, subscribe, unsubscribe, heartbeat, message\n * Server → Client: protocol.init, subscribed, subscribe_error, unsubscribed,\n * event, error\n */\n\nimport { mintLoginToken } from './login-token';\nimport type { ChannelEvent, ChannelListener, RealtimeSessionBase, Unsubscribe } from './types';\n\nconst HEARTBEAT_INTERVAL_MS = 30_000;\nconst RECONNECT_DELAY_MS = 3_000;\nconst MAX_RECONNECT_ATTEMPTS = 5;\nconst CLOSE_DELAY_MS = 1_000;\nconst MAX_SUBSCRIBE_RETRY_DELAY_MS = 30_000;\n\ninterface SocketState {\n ws: WebSocket | null;\n connected: boolean;\n connecting: boolean;\n /**\n * The last transport-or-server failure, or null once a connection succeeds.\n *\n * The state machine has always known this; it simply had no way to say so,\n * so every consumer hardcoded `error: null` and reported a healthy socket\n * through an outage. Two things set it: a server `error` frame (which is\n * how a rejected login is reported), and running out of reconnect\n * attempts — the point at which the client has genuinely given up and the\n * user needs to know, rather than each transient close on the way there.\n */\n lastError: string | null;\n listeners: Map<string, Set<ChannelListener>>;\n subscribedChannels: Set<string>;\n subscriptionRevision: number;\n subscriptionRetryChannels: Set<string>;\n subscriptionRetryAttempts: number;\n subscriptionRetryTimer: ReturnType<typeof setTimeout> | null;\n reconnectAttempts: number;\n reconnectTimer: ReturnType<typeof setTimeout> | null;\n heartbeatTimer: ReturnType<typeof setInterval> | null;\n closeTimer: ReturnType<typeof setTimeout> | null;\n intentionalClose: boolean;\n /**\n * Bumped by every connect attempt and every deliberate close. A connect\n * attempt awaits its login token before it opens a socket; if the\n * generation moved while it waited, a close or a newer attempt owns the\n * state and the stale attempt stops without touching it.\n */\n connectGeneration: number;\n}\n\nfunction createSocketState(): SocketState {\n return {\n ws: null,\n connected: false,\n connecting: false,\n lastError: null,\n listeners: new Map(),\n subscribedChannels: new Set(),\n subscriptionRevision: 0,\n subscriptionRetryChannels: new Set(),\n subscriptionRetryAttempts: 0,\n subscriptionRetryTimer: null,\n reconnectAttempts: 0,\n reconnectTimer: null,\n heartbeatTimer: null,\n closeTimer: null,\n intentionalClose: false,\n connectGeneration: 0,\n };\n}\n\n/** The `protocol.init` frame, before either subclass narrows it. */\nexport interface ProtocolInitMessage {\n memberId?: unknown;\n accountUuid?: unknown;\n workspaceUuids?: unknown;\n}\n\nexport abstract class RealtimeSocketClient<TSession extends RealtimeSessionBase> {\n private session: TSession | null = null;\n private readonly getSessionFn: (() => TSession | null) | null;\n private readonly state: SocketState = createSocketState();\n private readonly connectionListeners = new Set<(connected: boolean) => void>();\n\n constructor(config?: TSession | (() => TSession | null)) {\n if (typeof config === 'function') {\n this.getSessionFn = config;\n } else {\n this.getSessionFn = null;\n if (config) {\n this.session = config;\n }\n }\n }\n\n /** The WebSocket URL this mount connects to. */\n protected abstract buildUrl(session: TSession): string;\n\n /**\n * Record whatever identity `protocol.init` carried for this mount.\n *\n * Called before the connection is announced, so a `onConnection` listener\n * that immediately reads the identity always sees the new one.\n */\n protected abstract readProtocolInit(msg: ProtocolInitMessage): void;\n\n /** Forget the identity on a deliberate close. See {@link closeConnection}. */\n protected abstract clearIdentity(): void;\n\n /**\n * Whether replacing `previous` with `next` invalidates the open socket.\n *\n * A different login-token provider always does — it stands for a different\n * identity; the rest is mount-specific — a workspace client changing\n * workspace is a different socket, and an account client has no such axis.\n */\n protected sessionRequiresReconnect(previous: TSession | null, next: TSession): boolean {\n return previous?.getLoginToken !== next.getLoginToken || previous?.apiUrl !== next.apiUrl;\n }\n\n protected resolveSession(): TSession | null {\n return this.session ?? this.getSessionFn?.() ?? null;\n }\n\n private notifyConnectionListeners(): void {\n for (const listener of this.connectionListeners) {\n listener(this.state.connected);\n }\n }\n\n private clearCloseTimer(): void {\n if (this.state.closeTimer) {\n clearTimeout(this.state.closeTimer);\n this.state.closeTimer = null;\n }\n }\n\n private clearSubscriptionRetry(channel?: string): void {\n if (channel) {\n this.state.subscriptionRetryChannels.delete(channel);\n } else {\n this.state.subscriptionRetryChannels.clear();\n }\n\n if (this.state.subscriptionRetryChannels.size > 0) return;\n\n if (this.state.subscriptionRetryTimer) {\n clearTimeout(this.state.subscriptionRetryTimer);\n this.state.subscriptionRetryTimer = null;\n }\n this.state.subscriptionRetryAttempts = 0;\n }\n\n private scheduleSubscriptionRetry(channels: string[]): void {\n for (const channel of channels) {\n if (this.state.listeners.has(channel) && !this.state.subscribedChannels.has(channel)) {\n this.state.subscriptionRetryChannels.add(channel);\n }\n }\n\n if (this.state.subscriptionRetryChannels.size === 0 || this.state.subscriptionRetryTimer) {\n return;\n }\n\n this.state.subscriptionRetryAttempts += 1;\n const delay = Math.min(\n RECONNECT_DELAY_MS * this.state.subscriptionRetryAttempts,\n MAX_SUBSCRIBE_RETRY_DELAY_MS\n );\n\n this.state.subscriptionRetryTimer = setTimeout(() => {\n this.state.subscriptionRetryTimer = null;\n const retryChannels = Array.from(this.state.subscriptionRetryChannels).filter(\n (channel) =>\n this.state.listeners.has(channel) && !this.state.subscribedChannels.has(channel)\n );\n\n for (const channel of this.state.subscriptionRetryChannels) {\n if (!retryChannels.includes(channel)) {\n this.state.subscriptionRetryChannels.delete(channel);\n }\n }\n\n if (retryChannels.length === 0) {\n this.state.subscriptionRetryAttempts = 0;\n return;\n }\n\n if (this.state.connected && this.state.ws?.readyState === WebSocket.OPEN) {\n this.state.ws.send(JSON.stringify({ type: 'subscribe', channels: retryChannels }));\n }\n }, delay);\n }\n\n setSession(session: TSession): void {\n const changed = this.sessionRequiresReconnect(this.session, session);\n\n this.session = session;\n // `connecting` with no `ws` is an attempt still minting its token under\n // the old session; closing bumps the generation so it stops there.\n if (changed && (this.state.ws || this.state.connecting)) {\n this.closeConnection();\n }\n if (this.state.listeners.size > 0) {\n this.ensureConnection();\n }\n }\n\n subscribe(channel: string, listener: ChannelListener): Unsubscribe {\n this.clearCloseTimer();\n\n let listeners = this.state.listeners.get(channel);\n if (!listeners) {\n listeners = new Set();\n this.state.listeners.set(channel, listeners);\n }\n listeners.add(listener);\n\n this.ensureConnection();\n\n if (\n this.state.connected &&\n this.state.ws?.readyState === WebSocket.OPEN &&\n !this.state.subscribedChannels.has(channel)\n ) {\n this.state.ws.send(JSON.stringify({ type: 'subscribe', channels: [channel] }));\n }\n\n return () => {\n const channelListeners = this.state.listeners.get(channel);\n channelListeners?.delete(listener);\n if (channelListeners && channelListeners.size === 0) {\n this.state.listeners.delete(channel);\n this.state.subscribedChannels.delete(channel);\n this.clearSubscriptionRetry(channel);\n if (this.state.connected && this.state.ws?.readyState === WebSocket.OPEN) {\n this.state.ws.send(JSON.stringify({ type: 'unsubscribe', channels: [channel] }));\n }\n }\n\n if (this.state.listeners.size === 0) {\n this.state.closeTimer = setTimeout(() => {\n if (this.state.listeners.size === 0) {\n this.closeConnection();\n }\n }, CLOSE_DELAY_MS);\n }\n };\n }\n\n send(channel: string, data: unknown): void {\n if (!this.state.connected || !this.state.ws) return;\n this.state.ws.send(JSON.stringify({ type: 'message', channel, data }));\n }\n\n onConnection(listener: (connected: boolean) => void): () => void {\n this.connectionListeners.add(listener);\n listener(this.state.connected);\n return () => {\n this.connectionListeners.delete(listener);\n };\n }\n\n isConnected(): boolean {\n return this.state.connected;\n }\n\n /**\n * Whether the server has acknowledged this channel subscription.\n *\n * This is stricter than having a local listener: it becomes true only after\n * a `subscribed` frame, returns to false on disconnect or server-driven\n * unsubscribe, and stays false while automatic subscribe retry is pending.\n * Consumers that must order a command after subscription admission should\n * observe {@link getSubscriptionRevision} and re-read this method.\n */\n isSubscribed(channel: string): boolean {\n return this.state.subscribedChannels.has(channel);\n }\n\n /**\n * Monotonic local revision of acknowledged subscription state.\n *\n * It increments when `subscribed` or `unsubscribed` changes the server-\n * acknowledged set. It is not a protocol sequence number and resets with a\n * new client instance; its purpose is external-store invalidation before a\n * consumer calls {@link isSubscribed} again.\n */\n getSubscriptionRevision(): number {\n return this.state.subscriptionRevision;\n }\n\n /**\n * A socket is open or opening and has not yet said `protocol.init`.\n *\n * Real state, read off the state machine. Consumers used to report a\n * literal `false` here and render \"Available\" over a socket that had never\n * connected; a toggle that cannot distinguish \"not live\" from \"cannot tell\n * yet\" is the shape of that bug.\n */\n isConnecting(): boolean {\n return this.state.connecting;\n }\n\n /** The last failure the socket gave up on, or null. See {@link SocketState.lastError}. */\n getLastError(): string | null {\n return this.state.lastError;\n }\n\n protected ensureConnection(): void {\n const session = this.resolveSession();\n if (!session) return;\n if (typeof window === 'undefined' || typeof WebSocket === 'undefined') return;\n if (this.state.ws || this.state.connecting) return;\n\n this.clearCloseTimer();\n this.state.connecting = true;\n this.state.intentionalClose = false;\n const generation = ++this.state.connectGeneration;\n // `connecting` is part of what consumers render, so entering it is a\n // change they have to hear about. Without this notify a `useSyncExternal`\n // store reading `isConnecting()` would only ever sample it on some later\n // unrelated transition.\n this.notifyConnectionListeners();\n\n void this.openSocket(session, generation);\n }\n\n /**\n * Mint this attempt's login token, then open the socket with it.\n *\n * A fresh token per attempt, reconnects included: a browser's token is\n * single-use and expires in seconds, so a token from an earlier attempt is\n * already spent.\n */\n private async openSocket(session: TSession, generation: number): Promise<void> {\n const token = await mintLoginToken(session.getLoginToken);\n if (generation !== this.state.connectGeneration) return;\n if (!token) {\n this.releaseUnopenedAttempt();\n return;\n }\n\n let ws: WebSocket;\n try {\n ws = new WebSocket(this.buildUrl(session));\n } catch {\n // A bad URL throws synchronously; without this the attempt would stay\n // `connecting` forever and every later `ensureConnection` would no-op.\n this.releaseUnopenedAttempt();\n return;\n }\n\n ws.onopen = () => {\n ws.send(JSON.stringify({ type: 'login', token }));\n };\n\n ws.onmessage = (event: MessageEvent) => {\n // Frames from a socket this client has already replaced belong to\n // nobody: acting on them would fold a stale connection's\n // subscription state into the live one.\n if (this.state.ws !== null && this.state.ws !== ws) return;\n try {\n const msg = JSON.parse(String(event.data)) as ProtocolInitMessage & {\n type?: string;\n channels?: string[];\n channel?: string;\n event?: ChannelEvent;\n errors?: unknown[];\n };\n\n if (msg.type === 'protocol.init' && !this.state.connected) {\n this.state.connected = true;\n this.readProtocolInit(msg);\n this.state.connecting = false;\n this.state.reconnectAttempts = 0;\n this.state.lastError = null;\n\n if (this.state.heartbeatTimer) clearInterval(this.state.heartbeatTimer);\n this.state.heartbeatTimer = setInterval(() => {\n if (this.state.ws?.readyState === WebSocket.OPEN) {\n this.state.ws.send(JSON.stringify({ type: 'heartbeat' }));\n }\n }, HEARTBEAT_INTERVAL_MS);\n\n if (this.state.listeners.size > 0) {\n ws.send(\n JSON.stringify({\n type: 'subscribe',\n channels: Array.from(this.state.listeners.keys()),\n })\n );\n }\n\n this.notifyConnectionListeners();\n return;\n }\n\n if (msg.type === 'subscribed' && Array.isArray(msg.channels)) {\n for (const ch of msg.channels) {\n this.state.subscribedChannels.add(ch);\n this.clearSubscriptionRetry(ch);\n }\n this.state.subscriptionRevision += 1;\n this.notifyConnectionListeners();\n return;\n }\n\n if (msg.type === 'subscribe_error' && Array.isArray(msg.errors)) {\n const retryableChannels: string[] = [];\n for (const rawError of msg.errors) {\n if (!rawError || typeof rawError !== 'object') continue;\n const error = rawError as { channel?: unknown; retryable?: unknown };\n if (typeof error.channel !== 'string') continue;\n\n if (error.retryable === true) {\n retryableChannels.push(error.channel);\n } else {\n this.clearSubscriptionRetry(error.channel);\n }\n }\n this.scheduleSubscriptionRetry(retryableChannels);\n return;\n }\n\n if (msg.type === 'unsubscribed' && Array.isArray(msg.channels)) {\n // The server also sends this unprompted when a grant change\n // revoked a subscription (account socket). Forgetting the\n // channel is what makes the client's next `subscribe` a real\n // re-authorization rather than a no-op against a stale set.\n for (const ch of msg.channels) {\n this.state.subscribedChannels.delete(ch);\n }\n this.state.subscriptionRevision += 1;\n this.notifyConnectionListeners();\n this.scheduleSubscriptionRetry(msg.channels);\n return;\n }\n\n if (msg.type === 'error') {\n // The server's own refusal — a rejected login, an unknown\n // message shape. Recorded rather than dropped: it is the one\n // failure the transport cannot tell the consumer about, because\n // the socket is open and will close looking like any other.\n const message = (msg as { message?: unknown }).message;\n this.state.lastError =\n typeof message === 'string' && message ? message : 'Realtime error';\n this.notifyConnectionListeners();\n return;\n }\n\n if (msg.type === 'event' && msg.channel && msg.event) {\n const channelListeners = this.state.listeners.get(msg.channel);\n if (channelListeners) {\n for (const channelListener of channelListeners) {\n try {\n channelListener(msg.event);\n } catch (err) {\n console.error('Channel listener error:', err);\n }\n }\n }\n }\n } catch {\n // Ignore malformed frames.\n }\n };\n\n ws.onclose = () => {\n // A close from a socket that is no longer this client's would\n // otherwise clear the REPLACEMENT connection's state: null its `ws`,\n // wipe its subscribed set, and stop its heartbeat — leaving a live\n // socket that believes it is subscribed to nothing and a client that\n // believes it is disconnected. Reachable whenever a `setSession`\n // replaced the socket while the old one's close was still in flight.\n if (this.state.ws !== null && this.state.ws !== ws) return;\n\n this.state.connected = false;\n this.state.connecting = false;\n this.state.ws = null;\n this.state.subscribedChannels.clear();\n this.clearSubscriptionRetry();\n\n if (this.state.heartbeatTimer) {\n clearInterval(this.state.heartbeatTimer);\n this.state.heartbeatTimer = null;\n }\n\n this.afterConnectionEnded();\n };\n\n ws.onerror = () => {\n // onclose will fire after this\n };\n\n this.state.ws = ws;\n }\n\n /**\n * End an attempt that never opened a socket. No `onclose` will run for it,\n * so the retry decision is made here, on the normal backoff.\n */\n private releaseUnopenedAttempt(): void {\n this.state.connecting = false;\n this.afterConnectionEnded();\n }\n\n /** Decide whether a connection that just ended (or never opened) retries. */\n private afterConnectionEnded(): void {\n const willRetry =\n !this.state.intentionalClose &&\n this.state.listeners.size > 0 &&\n this.state.reconnectAttempts < MAX_RECONNECT_ATTEMPTS;\n\n if (!willRetry && !this.state.intentionalClose && this.state.listeners.size > 0) {\n // Out of attempts with listeners still waiting: the client has\n // genuinely given up, which is the moment worth surfacing. Each\n // transient close on the way here is not — a socket that reconnects\n // in three seconds did not fail, and reporting it would train\n // people to ignore the indicator.\n this.state.lastError = 'Realtime connection lost';\n }\n\n this.notifyConnectionListeners();\n\n if (willRetry) {\n this.state.reconnectAttempts += 1;\n this.state.reconnectTimer = setTimeout(() => {\n this.ensureConnection();\n }, RECONNECT_DELAY_MS * this.state.reconnectAttempts);\n }\n }\n\n protected closeConnection(): void {\n this.state.intentionalClose = true;\n this.state.connectGeneration += 1;\n this.clearCloseTimer();\n if (this.state.reconnectTimer) {\n clearTimeout(this.state.reconnectTimer);\n this.state.reconnectTimer = null;\n }\n if (this.state.heartbeatTimer) {\n clearInterval(this.state.heartbeatTimer);\n this.state.heartbeatTimer = null;\n }\n this.state.ws?.close();\n this.state.ws = null;\n this.state.connected = false;\n // Only a deliberate close forgets who we were; a transient `onclose`\n // leaves the identity in place because it is unchanged across a\n // reconnect, and the next `protocol.init` overwrites it anyway.\n this.clearIdentity();\n this.state.connecting = false;\n this.state.subscribedChannels.clear();\n this.clearSubscriptionRetry();\n this.notifyConnectionListeners();\n }\n}\n","/**\n * Unified workspace WebSocket client for real-time events.\n *\n * Mirrors `/api/v2/w/{wid}/ws/realtime`: log in with a token from the session's\n * `getLoginToken` provider (fresh per connect attempt),\n * subscribe to channels, receive AGUI events.\n *\n * The socket state machine — login, heartbeat, reconnect, subscribe retry —\n * lives in {@link RealtimeSocketClient}, shared with the account client. This\n * file is what is specific to the workspace mount: its URL, and the member id\n * `protocol.init` hands back.\n */\n\nimport { RealtimeSocketClient, type ProtocolInitMessage } from './socket-client';\nimport type { ChannelListener, RealtimeAuthSession, Unsubscribe } from './types';\n\n/** Build the workspace realtime WebSocket URL from session config. */\nexport function buildWorkspaceWsUrl(session: RealtimeAuthSession): string {\n const isSsl = session.apiUrl.startsWith('https');\n const host = session.apiUrl.replace(/^https?:\\/\\//, '');\n return `${isSsl ? 'wss' : 'ws'}://${host}/api/v2/w/${session.workspaceUuid}/ws/realtime`;\n}\n\nexport class WorkspaceRealtimeClient extends RealtimeSocketClient<RealtimeAuthSession> {\n private memberId: number | null = null;\n\n protected buildUrl(session: RealtimeAuthSession): string {\n return buildWorkspaceWsUrl(session);\n }\n\n protected readProtocolInit(msg: ProtocolInitMessage): void {\n this.memberId = typeof msg.memberId === 'number' ? msg.memberId : null;\n }\n\n protected clearIdentity(): void {\n this.memberId = null;\n }\n\n protected sessionRequiresReconnect(\n previous: RealtimeAuthSession | null,\n next: RealtimeAuthSession\n ): boolean {\n return (\n super.sessionRequiresReconnect(previous, next) ||\n previous?.workspaceUuid !== next.workspaceUuid\n );\n }\n\n /**\n * Member id the server authenticated this socket as, or null before\n * `protocol.init` arrives.\n *\n * Channels whose Redis key is workspace-wide can carry events addressed to\n * one member (presence acks, for example). Consumers compare the event's\n * addressee against this so a reply meant for another operator cannot be\n * applied locally.\n */\n getMemberId(): number | null {\n return this.memberId;\n }\n}\n\nconst defaultClient = new WorkspaceRealtimeClient();\n\nexport function initWorkspaceSocket(session: RealtimeAuthSession): void {\n defaultClient.setSession(session);\n}\n\nexport function subscribeWorkspaceChannel(channel: string, listener: ChannelListener): Unsubscribe {\n return defaultClient.subscribe(channel, listener);\n}\n\nexport function onWorkspaceSocketConnection(listener: (connected: boolean) => void): () => void {\n return defaultClient.onConnection(listener);\n}\n\nexport function isWorkspaceSocketConnected(): boolean {\n return defaultClient.isConnected();\n}\n","/**\n * Account WebSocket client — one socket per tab, rooted at the Account.\n *\n * Mirrors `/api/v2/ws/realtime`. No workspace in the URL and none in the\n * session: the login token is the whole identity, and what the socket may see comes\n * from the server's grant map, refreshed there.\n *\n * See `docs/projects/active/account-socket.md`. The point is not a second\n * socket — it is *one*. An operator covering four workspaces holds four\n * `WorkspaceRealtimeClient`s today, which is four connections, four\n * heartbeats, and no place to stand for \"what is happening across my estate\".\n * Workspace-scoped channels are reached here by qualifying their names, so the\n * same channel handlers serve both mounts and the per-workspace socket can be\n * deleted at cutover rather than lived alongside.\n *\n * ```ts\n * const client = new AccountRealtimeClient({ apiUrl, getLoginToken });\n * client.subscribe(workspaceChannel(wid, 'activity'), onInvalidate);\n * client.subscribe(ACCOUNT_ACTIVITY_CHANNEL, onInvalidate);\n * ```\n */\n\nimport { RealtimeSocketClient, type ProtocolInitMessage } from './socket-client';\nimport type { AccountRealtimeSession, ChannelListener, Unsubscribe } from './types';\n\n/** Build the account realtime WebSocket URL from session config. */\nexport function buildAccountWsUrl(session: AccountRealtimeSession): string {\n const isSsl = session.apiUrl.startsWith('https');\n const host = session.apiUrl.replace(/^https?:\\/\\//, '');\n return `${isSsl ? 'wss' : 'ws'}://${host}/api/v2/ws/realtime`;\n}\n\n/**\n * Qualify a workspace-scoped channel name for the account socket.\n *\n * `chat:42:monitor` in workspace `W` is `w/W/chat:42:monitor` here. Exported\n * as a function rather than left to callers' template strings for the reason\n * every channel name constant in this package exists: the server matches\n * exactly, so a typo subscribes successfully to nothing.\n */\nexport function workspaceChannel(workspaceUuid: string, channel: string): string {\n return `w/${workspaceUuid}/${channel}`;\n}\n\nexport class AccountRealtimeClient extends RealtimeSocketClient<AccountRealtimeSession> {\n private accountUuid: string | null = null;\n private workspaceUuids: readonly string[] = [];\n\n protected buildUrl(session: AccountRealtimeSession): string {\n return buildAccountWsUrl(session);\n }\n\n protected readProtocolInit(msg: ProtocolInitMessage): void {\n this.accountUuid = typeof msg.accountUuid === 'string' ? msg.accountUuid : null;\n this.workspaceUuids = Array.isArray(msg.workspaceUuids)\n ? msg.workspaceUuids.filter((value): value is string => typeof value === 'string')\n : [];\n }\n\n protected clearIdentity(): void {\n this.accountUuid = null;\n this.workspaceUuids = [];\n }\n\n /** Account uuid the server authenticated this socket as, or null. */\n getAccountUuid(): string | null {\n return this.accountUuid;\n }\n\n /**\n * Workspaces the server's grant map admitted at `protocol.init`.\n *\n * A **hint for rendering**, never an authorization decision: the server\n * re-checks its own map on every subscribe, and refreshes it on a floor, so\n * this snapshot can be stale in either direction. Use it to decide which\n * panes are worth opening, not which data a viewer may see.\n */\n getWorkspaceUuids(): readonly string[] {\n return this.workspaceUuids;\n }\n}\n\nconst defaultClient = new AccountRealtimeClient();\n\nexport function initAccountSocket(session: AccountRealtimeSession): void {\n defaultClient.setSession(session);\n}\n\nexport function subscribeAccountChannel(channel: string, listener: ChannelListener): Unsubscribe {\n return defaultClient.subscribe(channel, listener);\n}\n\nexport function onAccountSocketConnection(listener: (connected: boolean) => void): () => void {\n return defaultClient.onConnection(listener);\n}\n\nexport function isAccountSocketConnected(): boolean {\n return defaultClient.isConnected();\n}\n","/** PCM16 helpers aligned with main-app web voice (24 kHz). */\n\nexport const SERVER_SAMPLE_RATE = 24000;\nexport const TARGET_SAMPLE_RATE = 24000;\nexport const MONITOR_SAMPLE_RATE = 16000;\n\nexport function float32ArrayToBase64(float32Array: Float32Array): string {\n const int16Array = new Int16Array(float32Array.length);\n for (let i = 0; i < float32Array.length; i += 1) {\n const sample = Math.max(-1, Math.min(1, float32Array[i]));\n int16Array[i] = Math.round(sample * 32767);\n }\n const uint8Array = new Uint8Array(int16Array.buffer);\n let binaryString = '';\n for (let i = 0; i < uint8Array.length; i += 1) {\n binaryString += String.fromCharCode(uint8Array[i]);\n }\n return btoa(binaryString);\n}\n\nexport function base64ToFloat32Array(base64: string): Float32Array {\n const binaryString = atob(base64);\n const len = binaryString.length / 2;\n const float32Array = new Float32Array(len);\n for (let i = 0; i < len; i += 1) {\n const lo = binaryString.charCodeAt(i * 2);\n const hi = binaryString.charCodeAt(i * 2 + 1);\n let int16 = (hi << 8) | lo;\n if (int16 >= 0x8000) int16 -= 0x10000;\n float32Array[i] = int16 / 32768;\n }\n return float32Array;\n}\n\nexport function concatFloat32Arrays(arrays: Float32Array[]): Float32Array {\n const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);\n const result = new Float32Array(totalLength);\n let offset = 0;\n for (const arr of arrays) {\n result.set(arr, offset);\n offset += arr.length;\n }\n return result;\n}\n\nexport function downsampleAudio(\n audioData: Float32Array,\n originalSampleRate: number,\n targetSampleRate: number\n): Float32Array {\n if (originalSampleRate === targetSampleRate) return audioData;\n const ratio = originalSampleRate / targetSampleRate;\n const newLength = Math.round(audioData.length / ratio);\n const result = new Float32Array(newLength);\n for (let i = 0; i < newLength; i += 1) {\n const srcIndex = Math.floor(i * ratio);\n result[i] = audioData[srcIndex];\n }\n return result;\n}\n","/**\n * Human operator session WebSocket client.\n *\n * Connects to `/api/v2/w/{wid}/operator/chats/{chatId}/session-ws` — AGUI\n * events, bidirectional PCM, and call control. No AI inference.\n */\n\nimport {\n base64ToFloat32Array,\n float32ArrayToBase64,\n SERVER_SAMPLE_RATE,\n TARGET_SAMPLE_RATE,\n} from '../audio/pcm';\nimport { mintLoginToken } from './login-token';\nimport type {\n ChannelEvent,\n OperatorCallPhase,\n OperatorSessionState,\n RealtimeAuthSession,\n} from './types';\n\nexport interface OperatorSessionHandlers {\n onSessionStateChange?: (state: OperatorSessionState) => void;\n onCallPhaseChange?: (phase: OperatorCallPhase) => void;\n onAguiEvent?: (event: ChannelEvent) => void;\n onError?: (message: string) => void;\n /** Fired when the server closes the session (e.g. callee hung up). */\n onSessionClosed?: (reason?: string) => void;\n}\n\nexport class OperatorSessionClient {\n private ws: WebSocket | null = null;\n private audioContext: AudioContext | null = null;\n private mediaStream: MediaStream | null = null;\n private processor: ScriptProcessorNode | null = null;\n private sessionState: OperatorSessionState = 'idle';\n private callPhase: OperatorCallPhase = 'idle';\n private nextPlaybackTime = 0;\n private playbackSources: AudioBufferSourceNode[] = [];\n private micActive = false;\n private onHold = false;\n private muted = false;\n private receivedRemoteAudio = false;\n private heartbeatTimer: ReturnType<typeof setInterval> | null = null;\n private sessionCloseNotified = false;\n private intentionalDisconnect = false;\n /** Bumped by every connect and disconnect; a stale connect stops at its next await. */\n private connectGeneration = 0;\n\n constructor(private handlers: OperatorSessionHandlers = {}) {}\n\n getSessionState(): OperatorSessionState {\n return this.sessionState;\n }\n\n getCallPhase(): OperatorCallPhase {\n return this.callPhase;\n }\n\n private setSessionState(next: OperatorSessionState): void {\n this.sessionState = next;\n this.handlers.onSessionStateChange?.(next);\n }\n\n private setCallPhase(next: OperatorCallPhase): void {\n this.callPhase = next;\n this.handlers.onCallPhaseChange?.(next);\n }\n\n private notifySessionClosed(reason?: string): void {\n if (this.sessionCloseNotified) return;\n this.sessionCloseNotified = true;\n this.handlers.onSessionClosed?.(reason);\n }\n\n async connect(session: RealtimeAuthSession, chatId: number): Promise<void> {\n if (typeof window === 'undefined' || typeof WebSocket === 'undefined') {\n throw new Error('Operator session WebSocket is not available in this environment');\n }\n\n await this.disconnect();\n const generation = ++this.connectGeneration;\n this.sessionCloseNotified = false;\n this.setSessionState('connecting');\n this.setCallPhase('connecting');\n\n const isSsl = session.apiUrl.startsWith('https');\n const host = session.apiUrl.replace(/^https?:\\/\\//, '');\n const wsUrl = `${isSsl ? 'wss' : 'ws'}://${host}/api/v2/w/${session.workspaceUuid}/operator/chats/${chatId}/session-ws`;\n\n // One token per connect: a browser's is single-use and expires in seconds.\n const token = await mintLoginToken(session.getLoginToken);\n if (generation !== this.connectGeneration) {\n throw new Error('Operator session connect superseded');\n }\n if (!token) {\n this.setSessionState('error');\n this.setCallPhase('idle');\n throw new Error('Realtime login token unavailable');\n }\n\n await new Promise<void>((resolve, reject) => {\n const ws = new WebSocket(wsUrl);\n this.ws = ws;\n\n ws.onopen = () => {\n ws.send(JSON.stringify({ type: 'login', token }));\n };\n\n ws.onmessage = (event) => {\n try {\n const msg = JSON.parse(String(event.data)) as {\n type?: string;\n name?: string;\n value?: Record<string, unknown>;\n message?: string;\n code?: string;\n event?: ChannelEvent;\n };\n this.handleMessage(msg, resolve, reject);\n } catch {\n /* ignore malformed frames */\n }\n };\n\n ws.onerror = () => {\n const message = 'Operator session WebSocket error';\n const wasConnecting = this.sessionState === 'connecting';\n this.setSessionState('error');\n if (wasConnecting) {\n this.setCallPhase('idle');\n reject(new Error(message));\n } else {\n this.setCallPhase('failed');\n this.handlers.onError?.(message);\n }\n };\n\n ws.onclose = () => {\n this.clearHeartbeat();\n this.stopMicrophone();\n this.stopPlayback();\n const wasConnected = this.sessionState === 'connected';\n this.setSessionState('idle');\n this.setCallPhase('idle');\n if (wasConnected && !this.intentionalDisconnect) {\n this.notifySessionClosed('disconnect');\n }\n this.intentionalDisconnect = false;\n };\n });\n }\n\n private handleMessage(\n msg: {\n type?: string;\n name?: string;\n value?: Record<string, unknown>;\n message?: string;\n code?: string;\n event?: ChannelEvent;\n },\n resolveLogin: () => void,\n rejectLogin: (err: Error) => void\n ): void {\n if (msg.type === 'RUN_ERROR' || msg.type === 'error') {\n const message = msg.message ?? 'Operator session error';\n if (this.sessionState === 'connecting') {\n this.setSessionState('error');\n rejectLogin(new Error(message));\n } else {\n this.setCallPhase('failed');\n this.handlers.onError?.(message);\n }\n return;\n }\n\n if (msg.type === 'protocol.init' && this.sessionState === 'connecting') {\n return;\n }\n\n if (msg.type === 'CUSTOM' && msg.name === 'operator.session_ready') {\n this.setSessionState('connected');\n this.setCallPhase('ready');\n this.startHeartbeat();\n resolveLogin();\n return;\n }\n\n if (msg.type === 'event' && msg.event) {\n if (this.handleRemoteVoiceAudio(msg.event)) {\n return;\n }\n this.handlers.onAguiEvent?.(msg.event);\n return;\n }\n\n if (msg.type === 'CUSTOM' && msg.name === 'operator.call.started') {\n this.receivedRemoteAudio = false;\n this.setCallPhase('dialing');\n return;\n }\n\n if (msg.type === 'CUSTOM' && msg.name === 'operator.call.ended') {\n this.onHold = false;\n this.muted = false;\n this.receivedRemoteAudio = false;\n this.stopMicrophone();\n this.stopPlayback();\n const endSession = msg.value?.end_session === true;\n const reason = typeof msg.value?.reason === 'string' ? msg.value.reason : undefined;\n if (endSession) {\n this.setCallPhase('idle');\n this.notifySessionClosed(reason ?? 'peer_hangup');\n } else {\n this.setCallPhase('ready');\n }\n return;\n }\n }\n\n /** Play callee PCM from wrapped chat-stream events; returns true if handled. */\n private handleRemoteVoiceAudio(event: ChannelEvent): boolean {\n if (event.type !== 'CUSTOM' || event.name !== 'voice.audio') {\n return false;\n }\n if (this.onHold) return true;\n\n const value = event.value;\n if (!value || typeof value !== 'object') return true;\n\n const data = (value as Record<string, unknown>).data;\n if (typeof data !== 'string' || !data) return true;\n\n if (\n !this.receivedRemoteAudio &&\n (this.callPhase === 'dialing' || this.callPhase === 'live')\n ) {\n this.receivedRemoteAudio = true;\n this.setCallPhase('live');\n }\n void this.playPcm16(base64ToFloat32Array(data));\n return true;\n }\n\n private startHeartbeat(): void {\n this.clearHeartbeat();\n this.heartbeatTimer = setInterval(() => {\n if (this.ws?.readyState === WebSocket.OPEN) {\n this.ws.send(JSON.stringify({ type: 'heartbeat' }));\n }\n }, 30_000);\n }\n\n private clearHeartbeat(): void {\n if (this.heartbeatTimer) {\n clearInterval(this.heartbeatTimer);\n this.heartbeatTimer = null;\n }\n }\n\n private sendCustom(name: string, value: Record<string, unknown> = {}): void {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n this.ws.send(JSON.stringify({ type: 'CUSTOM', name, value }));\n }\n\n async placeCall(memberId: number): Promise<void> {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n if (this.sessionState !== 'connected') {\n throw new Error('Operator session is not ready');\n }\n await this.startMicrophone();\n this.sendCustom('operator.call.place', { member_id: memberId });\n }\n\n /**\n * Answer an inbound operator call the caller has already been routed to.\n *\n * Symmetric with {@link placeCall} but for the inbound direction: instead of\n * dialing an outbound leg it tells the backend to bridge the already-ringing\n * caller to this operator session. The backend (see\n * `lib/operator_session_handler.py::_answer_inbound_call`) registers the\n * audio bridge, marks the ring state `answered`, and replies with\n * `operator.call.started` — after which callee PCM arrives as `voice.audio`\n * events (played via {@link playPcm16}) and mic audio streams the other way,\n * exactly like an outbound call.\n *\n * Must be called once the session is ready (state `connected`). Starts the\n * microphone before sending so the operator's audio is flowing the instant\n * the bridge registers.\n */\n async answerCall(interactionId: string, answerToken: string): Promise<void> {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n if (this.sessionState !== 'connected') {\n throw new Error('Operator session is not ready');\n }\n await this.startMicrophone();\n this.sendCustom('operator.call.answer', {\n interaction_id: interactionId,\n answer_token: answerToken,\n });\n }\n\n /**\n * Join a live, escalated voice call on this chat.\n *\n * Unlike {@link answerCall} (a waiting caller whose ring poll redirects the\n * PSTN leg), the caller here is already on the AI media stream with the AI\n * paused for an escalation. The backend\n * (`lib/operator_session_handler.py::_join_live_call`) authorizes, attaches\n * this session as bridge side \"a\", and re-points the live caller leg onto\n * the bridge over Twilio REST — replacing the AI stream with the human one.\n * The server then sends `operator.call.started`, after which audio flows\n * exactly like an answered call.\n *\n * The chat id is carried by the session URL, so no argument is needed. Must\n * be called once the session is ready (state `connected`); starts the\n * microphone first so operator audio flows the instant the bridge attaches.\n */\n async joinCall(): Promise<void> {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {\n throw new Error('Operator session WebSocket is not connected');\n }\n if (this.sessionState !== 'connected') {\n throw new Error('Operator session is not ready');\n }\n await this.startMicrophone();\n this.sendCustom('operator.call.join');\n }\n\n /**\n * Mute or unmute the operator's outbound microphone. Muting gates mic frames\n * locally (the caller stops hearing the operator) while callee audio keeps\n * playing — distinct from {@link setHold}, which is a two-way pause the\n * backend is told about. Purely client-side; no server round-trip.\n */\n setMuted(muted: boolean): void {\n this.muted = muted;\n }\n\n isMuted(): boolean {\n return this.muted;\n }\n\n hangUp(): void {\n try {\n this.sendCustom('operator.call.hangup');\n } catch {\n /* socket already closed */\n }\n this.onHold = false;\n this.receivedRemoteAudio = false;\n }\n\n forwardCall(phoneNumber: string): void {\n this.receivedRemoteAudio = false;\n this.setCallPhase('dialing');\n this.sendCustom('operator.call.forward', { phone_number: phoneNumber });\n }\n\n setHold(onHold: boolean): void {\n this.onHold = onHold;\n this.sendCustom('operator.call.hold', { on_hold: onHold });\n if (this.callPhase === 'live' || this.callPhase === 'onHold') {\n this.setCallPhase(onHold ? 'onHold' : 'live');\n }\n if (onHold) {\n this.stopPlayback();\n }\n }\n\n async startMicrophone(): Promise<void> {\n if (typeof window === 'undefined') return;\n if (!navigator.mediaDevices?.getUserMedia) {\n throw new Error('Microphone not available in this browser');\n }\n if (this.micActive) return;\n\n this.mediaStream = await navigator.mediaDevices.getUserMedia({\n audio: {\n echoCancellation: true,\n noiseSuppression: true,\n autoGainControl: true,\n },\n });\n\n const Ctx =\n window.AudioContext ??\n (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;\n if (!Ctx) throw new Error('Web Audio not supported');\n\n this.audioContext = new Ctx();\n if (this.audioContext.state === 'suspended') {\n await this.audioContext.resume();\n }\n\n const source = this.audioContext.createMediaStreamSource(this.mediaStream);\n const processor = this.audioContext.createScriptProcessor(4096, 1, 1);\n this.processor = processor;\n\n processor.onaudioprocess = (event) => {\n if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;\n if (this.onHold) return;\n if (this.muted) return;\n if (\n this.callPhase !== 'dialing' &&\n this.callPhase !== 'live' &&\n this.callPhase !== 'onHold'\n ) {\n return;\n }\n\n const input = event.inputBuffer.getChannelData(0);\n const copy = new Float32Array(input.length);\n copy.set(input);\n\n const sampleRate = this.audioContext?.sampleRate ?? TARGET_SAMPLE_RATE;\n let pcm = copy;\n if (sampleRate !== TARGET_SAMPLE_RATE) {\n const ratio = sampleRate / TARGET_SAMPLE_RATE;\n const newLen = Math.floor(copy.length / ratio);\n const down = new Float32Array(newLen);\n for (let i = 0; i < newLen; i += 1) {\n down[i] = copy[Math.floor(i * ratio)];\n }\n pcm = down;\n }\n\n this.ws.send(\n JSON.stringify({\n type: 'CUSTOM',\n name: 'voice.audio',\n value: {\n data: float32ArrayToBase64(pcm),\n format: 'pcm16',\n sample_rate: TARGET_SAMPLE_RATE,\n },\n })\n );\n };\n\n source.connect(processor);\n processor.connect(this.audioContext.destination);\n this.micActive = true;\n }\n\n stopMicrophone(): void {\n this.processor?.disconnect();\n this.processor = null;\n for (const track of this.mediaStream?.getTracks() ?? []) {\n track.stop();\n }\n this.mediaStream = null;\n this.micActive = false;\n }\n\n private async playPcm16(samples: Float32Array): Promise<void> {\n if (typeof window === 'undefined') return;\n\n const Ctx =\n window.AudioContext ??\n (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;\n if (!Ctx) return;\n\n if (!this.audioContext || this.audioContext.state === 'closed') {\n this.audioContext = new Ctx();\n }\n if (this.audioContext.state === 'suspended') {\n await this.audioContext.resume();\n }\n\n const buffer = this.audioContext.createBuffer(1, samples.length, SERVER_SAMPLE_RATE);\n buffer.copyToChannel(new Float32Array(samples), 0);\n\n const source = this.audioContext.createBufferSource();\n source.buffer = buffer;\n source.connect(this.audioContext.destination);\n\n const now = this.audioContext.currentTime;\n const startAt = this.nextPlaybackTime < now + 0.02 ? now + 0.02 : this.nextPlaybackTime;\n source.start(startAt);\n this.nextPlaybackTime = startAt + buffer.duration;\n this.playbackSources.push(source);\n source.onended = () => {\n const idx = this.playbackSources.indexOf(source);\n if (idx >= 0) this.playbackSources.splice(idx, 1);\n };\n }\n\n private stopPlayback(): void {\n for (const src of this.playbackSources) {\n try {\n src.stop();\n } catch {\n /* already stopped */\n }\n }\n this.playbackSources = [];\n this.nextPlaybackTime = 0;\n }\n\n async disconnect(): Promise<void> {\n this.connectGeneration += 1;\n this.intentionalDisconnect = true;\n if (\n this.callPhase === 'dialing' ||\n this.callPhase === 'live' ||\n this.callPhase === 'onHold'\n ) {\n this.hangUp();\n }\n this.clearHeartbeat();\n this.stopMicrophone();\n this.stopPlayback();\n if (this.ws) {\n this.ws.close();\n this.ws = null;\n }\n if (this.audioContext && this.audioContext.state !== 'closed') {\n await this.audioContext.close().catch(() => undefined);\n }\n this.audioContext = null;\n this.onHold = false;\n this.muted = false;\n this.receivedRemoteAudio = false;\n this.setSessionState('idle');\n this.setCallPhase('idle');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACoDO,SAAS,kBAAkB,OAAmC;AAClE,SAAO,UAAU,aAAa,UAAU,UAAU,UAAU;AAC/D;AAgBO,IAAM,mBAAmB;AACzB,IAAM,0BAA0B;AAUhC,IAAM,2BAA2B;AAkBjC,IAAM,2BAA2B;AAGjC,IAAM,mBAAmB;AAuBzB,SAAS,uBAAuB,OAAyD;AAC7F,MAAI,MAAM,SAAS,aAAc,QAAO;AACxC,QAAM,OAAQ,MAA6B;AAC3C,MAAI,OAAO,SAAS,YAAY,SAAS,KAAM,QAAO;AACtD,QAAM,QAAS,KAA6B;AAC5C,SAAO,UAAU,eAAe,UAAU,YAAY,UAAU;AACnE;;;AC5HO,IAAM,yBAAyB;AAQtC,eAAsB,eAAe,UAA8D;AAChG,MAAI;AACJ,MAAI;AACD,UAAM,QAAiB,MAAM,QAAQ,KAAK;AAAA,MACvC,SAAS;AAAA,MACT,IAAI,QAAc,CAAC,YAAY;AAC5B,gBAAQ,WAAW,MAAM,QAAQ,IAAI,GAAG,sBAAsB;AAAA,MACjE,CAAC;AAAA,IACJ,CAAC;AACD,WAAO,OAAO,UAAU,YAAY,MAAM,SAAS,IAAI,QAAQ;AAAA,EAClE,Q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