@connextar/house 0.25.4 → 0.27.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.
Files changed (102) hide show
  1. package/README.md +112 -0
  2. package/dist/inbox/inbox.d.ts +104 -0
  3. package/dist/inbox/inbox.d.ts.map +1 -0
  4. package/dist/inbox/inbox.js +178 -0
  5. package/dist/inbox/inbox.js.map +1 -0
  6. package/dist/inbox/index.d.ts +11 -0
  7. package/dist/inbox/index.d.ts.map +1 -0
  8. package/dist/inbox/index.js +11 -0
  9. package/dist/inbox/index.js.map +1 -0
  10. package/dist/inbox/use-selection.d.ts +23 -0
  11. package/dist/inbox/use-selection.d.ts.map +1 -0
  12. package/dist/inbox/use-selection.js +51 -0
  13. package/dist/inbox/use-selection.js.map +1 -0
  14. package/dist/voice/handoff.d.ts +20 -0
  15. package/dist/voice/handoff.d.ts.map +1 -0
  16. package/dist/voice/handoff.js +18 -0
  17. package/dist/voice/handoff.js.map +1 -0
  18. package/dist/voice/index.d.ts +16 -0
  19. package/dist/voice/index.d.ts.map +1 -0
  20. package/dist/voice/index.js +16 -0
  21. package/dist/voice/index.js.map +1 -0
  22. package/dist/voice/opening-hours.d.ts +23 -0
  23. package/dist/voice/opening-hours.d.ts.map +1 -0
  24. package/dist/voice/opening-hours.js +117 -0
  25. package/dist/voice/opening-hours.js.map +1 -0
  26. package/dist/voice/out-of-hours.d.ts +29 -0
  27. package/dist/voice/out-of-hours.d.ts.map +1 -0
  28. package/dist/voice/out-of-hours.js +20 -0
  29. package/dist/voice/out-of-hours.js.map +1 -0
  30. package/dist/voice/phone.d.ts +33 -0
  31. package/dist/voice/phone.d.ts.map +1 -0
  32. package/dist/voice/phone.js +62 -0
  33. package/dist/voice/phone.js.map +1 -0
  34. package/dist/voice/relay/agent-client.d.ts +24 -0
  35. package/dist/voice/relay/agent-client.d.ts.map +1 -0
  36. package/dist/voice/relay/agent-client.js +45 -0
  37. package/dist/voice/relay/agent-client.js.map +1 -0
  38. package/dist/voice/relay/model.d.ts +124 -0
  39. package/dist/voice/relay/model.d.ts.map +1 -0
  40. package/dist/voice/relay/model.js +304 -0
  41. package/dist/voice/relay/model.js.map +1 -0
  42. package/dist/voice/relay/protocol.d.ts +44 -0
  43. package/dist/voice/relay/protocol.d.ts.map +1 -0
  44. package/dist/voice/relay/protocol.js +16 -0
  45. package/dist/voice/relay/protocol.js.map +1 -0
  46. package/dist/voice/relay/sentence-gate.d.ts +24 -0
  47. package/dist/voice/relay/sentence-gate.d.ts.map +1 -0
  48. package/dist/voice/relay/sentence-gate.js +65 -0
  49. package/dist/voice/relay/sentence-gate.js.map +1 -0
  50. package/dist/voice/relay/server.d.ts +71 -0
  51. package/dist/voice/relay/server.d.ts.map +1 -0
  52. package/dist/voice/relay/server.js +192 -0
  53. package/dist/voice/relay/server.js.map +1 -0
  54. package/dist/voice/relay/transfer-line.d.ts +33 -0
  55. package/dist/voice/relay/transfer-line.d.ts.map +1 -0
  56. package/dist/voice/relay/transfer-line.js +116 -0
  57. package/dist/voice/relay/transfer-line.js.map +1 -0
  58. package/dist/voice/relay-index.d.ts +13 -0
  59. package/dist/voice/relay-index.d.ts.map +1 -0
  60. package/dist/voice/relay-index.js +13 -0
  61. package/dist/voice/relay-index.js.map +1 -0
  62. package/dist/voice/session-token.d.ts +58 -0
  63. package/dist/voice/session-token.d.ts.map +1 -0
  64. package/dist/voice/session-token.js +152 -0
  65. package/dist/voice/session-token.js.map +1 -0
  66. package/dist/voice/transfer.d.ts +123 -0
  67. package/dist/voice/transfer.d.ts.map +1 -0
  68. package/dist/voice/transfer.js +95 -0
  69. package/dist/voice/transfer.js.map +1 -0
  70. package/dist/voice/twilio-request.d.ts +30 -0
  71. package/dist/voice/twilio-request.d.ts.map +1 -0
  72. package/dist/voice/twilio-request.js +46 -0
  73. package/dist/voice/twilio-request.js.map +1 -0
  74. package/dist/voice/twilio-signature.d.ts +8 -0
  75. package/dist/voice/twilio-signature.d.ts.map +1 -0
  76. package/dist/voice/twilio-signature.js +22 -0
  77. package/dist/voice/twilio-signature.js.map +1 -0
  78. package/dist/voice/twiml.d.ts +93 -0
  79. package/dist/voice/twiml.d.ts.map +1 -0
  80. package/dist/voice/twiml.js +107 -0
  81. package/dist/voice/twiml.js.map +1 -0
  82. package/package.json +19 -1
  83. package/src/inbox/inbox.ts +200 -0
  84. package/src/inbox/index.ts +33 -0
  85. package/src/inbox/use-selection.ts +60 -0
  86. package/src/voice/handoff.ts +34 -0
  87. package/src/voice/index.ts +78 -0
  88. package/src/voice/opening-hours.ts +125 -0
  89. package/src/voice/out-of-hours.ts +48 -0
  90. package/src/voice/phone.ts +68 -0
  91. package/src/voice/relay/agent-client.ts +64 -0
  92. package/src/voice/relay/model.ts +412 -0
  93. package/src/voice/relay/protocol.ts +52 -0
  94. package/src/voice/relay/sentence-gate.ts +64 -0
  95. package/src/voice/relay/server.ts +242 -0
  96. package/src/voice/relay/transfer-line.ts +114 -0
  97. package/src/voice/relay-index.ts +46 -0
  98. package/src/voice/session-token.ts +205 -0
  99. package/src/voice/transfer.ts +171 -0
  100. package/src/voice/twilio-request.ts +60 -0
  101. package/src/voice/twilio-signature.ts +27 -0
  102. package/src/voice/twiml.ts +184 -0
package/README.md CHANGED
@@ -1681,6 +1681,80 @@ export async function GET() {
1681
1681
  }
1682
1682
  ```
1683
1683
 
1684
+ ### `@connextar/house/voice`, `/voice/relay`
1685
+
1686
+ A Twilio voice line with an AI on it, in two halves: the app's webhooks (`/voice`)
1687
+ and the long-running relay that holds each call's conversation (`/voice/relay`).
1688
+ From PharmaLineAI's voice layer (0.27.7), extracted as the second app, the CX
1689
+ support switchboard (`cx-support`), was about to write it again. PharmaLine
1690
+ re-exports it from its old paths, and its voice tests pass unchanged.
1691
+
1692
+ **`/voice`**: what a Next route handler needs. Imports only `node:crypto`.
1693
+
1694
+ - **TwiML**: `say(text, voice?)`, `play(url)`, `sayAndHangup`, `thenHangup`,
1695
+ `hangup`, `reject("busy" | "rejected")`, `gather({ actionUrl, input, numDigits,
1696
+ hints, prompt, … })`, `connectConversationRelay({ … preamble? })` and
1697
+ `dial({ number, callerId, actionUrl, timeout = 25, screenUrl?, statusCallback? })`.
1698
+ Every value is escaped. `screenUrl` is call screening: Twilio plays that TwiML
1699
+ to whoever answers, and an empty `<Response/>` bridges the call, which is how a
1700
+ mobile's voicemail is told apart from a person. `reject` as the first verb
1701
+ leaves the call unanswered and unbilled.
1702
+ - **Webhooks**: `createTwilioWebhooks(() => ({ authToken, webhookBaseUrl }))` →
1703
+ `{ readTwilioRequest, webhookUrl }`. The reader checks `X-Twilio-Signature`
1704
+ against the request URL and the public one (behind a proxy they differ), and
1705
+ answers 503 with no token, 403 on a bad signature. `twiml(xml)` is the response.
1706
+ - **Session tokens**: `signVoiceSession(signer, { tenantId, callSid, callerNumber })`
1707
+ and `verifyVoiceSession(verifier, token)`. Ed25519 (`vs2.`): the app holds the
1708
+ private key and mints, the relay holds the public key and only checks; the old
1709
+ HMAC `vst.` format still verifies where its secret is kept. Two hours, one socket
1710
+ per token. `generateVoiceSessionKeys()` prints a fresh pair. The wire format is
1711
+ PharmaLine's, byte for byte.
1712
+ - **Phone rules**: `toE164`, `presentedNumber` (withheld stand-ins are null),
1713
+ `isAllowedTransferNumber` (UK landline, 03, mobile and freephone only; never
1714
+ premium, personal or international), `divertsToAiLine`.
1715
+ - **Hours**: `parseOpeningHours`, `isOpenAt`, `nextOpening` ("at 9am tomorrow"),
1716
+ `describeOpeningHours`, and `lineStatus` / `teamReachable` / `transferTarget`.
1717
+ What a closed line _says_ is the app's: PharmaLine names NHS 111, a switchboard
1718
+ says when it will call back.
1719
+ - **Handoff**: `parseHandoff(HandoffData)`. Anything unexpected, including a failed
1720
+ session, is `unknown`, which the app treats as "put them through".
1721
+ - **Transfers**: `plannedTransfer(line, status)` (pure: where a transfer would go,
1722
+ or `none` / `refused` / `loops`), `dialTarget(line, status, how, { store, rules })`
1723
+ and `isTransferComingBack(line, from, store)`. House holds no database, so the
1724
+ store is the app's: `consume` (a rate-limit bucket), `refused` (its audit log, in
1725
+ its words), `remember` (the number just rung), and `isOwnNumber` /
1726
+ `takeRecentDial` for the loop check. The keys are PharmaLine's:
1727
+ `transfers:<id>:from:<caller>`, `transfers-direct:<id>`, `transfers:<id>`.
1728
+
1729
+ **`/voice/relay`**: Node only. Needs `ws` (an optional peer) and the configured
1730
+ provider's SDK.
1731
+
1732
+ - `startRelay({ port, verifier, createSession, twilio, limits, build })`: `/healthz`
1733
+ (via `/health`) and WebSockets on `/relay`. It checks Twilio's upgrade signature,
1734
+ the session token in `setup` (same call, once per token), and room: `limits.
1735
+ callsPerTenant` (default 20) and `pendingSockets` (50). A refused socket is a
1736
+ failed session to Twilio, so the app's handoff puts the caller through. The
1737
+ session is the app's: `createSession({ session, token, setup, parameters, send,
1738
+ log })` returns `{ onPrompt, onInterrupt, onDtmf?, close }`.
1739
+ - `createDialogModel({ provider, apiKey, model, tools: (team) => DialogTool[],
1740
+ transferTool, transferSilentKeys, cacheKeyPrefix, … })`: streams text from
1741
+ Claude or OpenAI with prompt caching on each block of instructions and the
1742
+ conversation, `store: false` on OpenAI, usage in the `/ai` shape, and the
1743
+ transfer tool's `say` spoken while its arguments stream (`TransferLineStream`).
1744
+ `team` picks which tool list a turn gets, so a closed line can offer a different
1745
+ transfer tool.
1746
+ - `SentenceGate` / `keepSentences`: hold streamed words back a sentence at a time
1747
+ and drop the ones that fail a rule, such as a promise to put someone through
1748
+ when nobody can be.
1749
+ - `createAgentFetch({ baseUrl, sessionToken })`: `get` / `post` to the app's
1750
+ agent API with the token as Bearer, an 8 s timeout and `{ success, data }`.
1751
+
1752
+ The call session itself (the prompt, the tools, the fail-safes) stays in each
1753
+ app: PharmaLine's checks identity and prescriptions, the switchboard's takes a
1754
+ name and a query. Both run the same ceilings (40 caller turns, 120 model
1755
+ requests, 60,000 characters), copied rather than shared until a third session
1756
+ needs them.
1757
+
1684
1758
  ### `@connextar/house/maintenance`, `/maintenance/react`
1685
1759
 
1686
1760
  Planned maintenance on the database server an app depends on, shown to people
@@ -2424,6 +2498,44 @@ filter is dropped rather than throwing when the menu entry is picked. Where view
2424
2498
  are stored — a table keyed by account, or the browser — is the app's decision,
2425
2499
  because only the app knows whether a view should follow someone to their phone.
2426
2500
 
2501
+ ### `@connextar/house/inbox`
2502
+
2503
+ The rules of a notifications inbox — which list is showing, which page of it,
2504
+ which rows are ticked and what can be done to them — with no markup and no
2505
+ database in it. AltEd's learners were sent to the home page by a notification
2506
+ because there was nowhere else to go; the inbox it built for them, and for every
2507
+ other account, is the same thing any app with a bell needs, and the same small
2508
+ things go wrong in each: a page left empty after a delete, a tick still counted
2509
+ on a row that has turned the page, "Mark read" offered for rows already read.
2510
+
2511
+ - **Filters.** `INBOX_FILTERS` is `inbox` (everything not archived), `unread`
2512
+ and `archived`. `parseInboxQuery` reads `?filter=` and `?page=` from a
2513
+ request or a page's `searchParams`, and reads anything it doesn't recognise as
2514
+ the inbox's first page. `inboxHref` writes them back, leaving the defaults out.
2515
+ - **Pages.** Numbered, not "load more": `pageCount`, `clampPage` (the page before,
2516
+ once a delete empties the last), `pageRange` for "21–40 of 57", and
2517
+ `pageWindow`, which gives the numbers a pager shows — first, last, the current
2518
+ page and its neighbours, and a `"gap"` that never hides just one page.
2519
+ - **Ticks.** `useSelection(visibleKeys)` keeps only the ticked keys still on
2520
+ show, derived on every render, so turning a page or a row leaving after an
2521
+ action can't leave something ticked the reader can't see.
2522
+ - **Actions.** `INBOX_ACTIONS` is read, unread, archive, unarchive and delete.
2523
+ `actionsFor(rows)` offers only what would change at least one ticked row;
2524
+ `actionChanges` is the field each one sets, and `readsUnread` says when the
2525
+ unread ones are read as well (archiving: the archive never holds the unread
2526
+ count, and a row read earlier keeps its time); `afterAction` and `stillListed`
2527
+ are the same rules for an optimistic update; `actionLabel` and `actionDone`
2528
+ are the words. `inboxActionSchema` is a bulk request's body — at most
2529
+ `INBOX_MAX_BULK` ids, checked for shape. Ownership is the app's query: scope
2530
+ every update to the reader, and a stranger's id changes nothing.
2531
+
2532
+ ```ts
2533
+ const { filter, page } = parseInboxQuery(new URL(request.url).searchParams);
2534
+ const parsed = inboxActionSchema.safeParse(await request.json());
2535
+ // …then, in the app's own storage:
2536
+ await db.notification.updateMany({ where: { id: { in: parsed.data.ids }, userId }, data: actionChanges(action) });
2537
+ ```
2538
+
2427
2539
  ### `@connextar/house/web-search`
2428
2540
 
2429
2541
  Google's organic results for a query, from whichever search API a deployment
@@ -0,0 +1,104 @@
1
+ import * as z from "zod";
2
+ /**
3
+ * The rules of a notifications inbox, with no markup and no database in them.
4
+ *
5
+ * Which list is showing (`filter`), which page of it (`page`), what can be done
6
+ * to the rows that are ticked (`actionsFor`), what a bulk request may carry
7
+ * (`inboxActionSchema`) and which page numbers a pager shows (`pageWindow`).
8
+ * The app owns the rows, the storage and the look; this owns the arithmetic
9
+ * that every inbox gets slightly wrong on its own.
10
+ */
11
+ /** The lists an inbox shows: everything not archived, only the unread ones, or the archive. */
12
+ export declare const INBOX_FILTERS: readonly ["inbox", "unread", "archived"];
13
+ export type InboxFilter = (typeof INBOX_FILTERS)[number];
14
+ /** What can be done to ticked rows. `delete` is permanent; `archive` is the way to put something away. */
15
+ export declare const INBOX_ACTIONS: readonly ["read", "unread", "archive", "unarchive", "delete"];
16
+ export type InboxAction = (typeof INBOX_ACTIONS)[number];
17
+ /** Rows a page, unless the app says otherwise. */
18
+ export declare const INBOX_PAGE_SIZE = 20;
19
+ /** The most rows one bulk request may name — a page and then some, never "everything" by accident. */
20
+ export declare const INBOX_MAX_BULK = 100;
21
+ /**
22
+ * The body of a bulk request: which rows, and what to do to them. The ids are the reader's own only after the
23
+ * app's query scopes them to the reader — this checks shape, never ownership.
24
+ */
25
+ export declare const inboxActionSchema: z.ZodObject<{
26
+ ids: z.ZodArray<z.ZodString>;
27
+ action: z.ZodEnum<{
28
+ delete: "delete";
29
+ read: "read";
30
+ archive: "archive";
31
+ unread: "unread";
32
+ unarchive: "unarchive";
33
+ }>;
34
+ }, z.core.$strip>;
35
+ export type InboxActionRequest = z.infer<typeof inboxActionSchema>;
36
+ type QueryInput = URLSearchParams | Record<string, string | string[] | undefined>;
37
+ /**
38
+ * `?filter=` and `?page=` from a request or a page's `searchParams`. Anything unrecognised reads as the inbox's
39
+ * first page rather than an error: an address someone edited, or one from before a filter was renamed, still opens.
40
+ */
41
+ export declare function parseInboxQuery(input: QueryInput): {
42
+ filter: InboxFilter;
43
+ page: number;
44
+ };
45
+ /** The address of one page of one list, keeping the defaults out of it so the plain inbox is the plain path. */
46
+ export declare function inboxHref(path: string, filter: InboxFilter, page: number): string;
47
+ /** How many pages `total` rows fill; an empty list is one empty page, so "page 1 of 1" is always true. */
48
+ export declare function pageCount(total: number, pageSize?: number): number;
49
+ /** A requested page brought within the list — after a delete empties the last page, the one before it. */
50
+ export declare function clampPage(page: number, pages: number): number;
51
+ /** The first and last row numbers on a page, 1-based, for "21–40 of 57"; both 0 for an empty list. */
52
+ export declare function pageRange(page: number, total: number, pageSize?: number): {
53
+ from: number;
54
+ to: number;
55
+ };
56
+ /**
57
+ * The page numbers a pager shows: always the first and last, the current one with `around` either side, and a
58
+ * `"gap"` where numbers are left out. A gap never stands for a single page — that page is shown instead, since a
59
+ * "…" hiding one number is longer than the number.
60
+ */
61
+ export declare function pageWindow(page: number, pages: number, around?: number): Array<number | "gap">;
62
+ /** What an inbox needs to know about a row to offer actions on it. */
63
+ export interface InboxRowState {
64
+ read: boolean;
65
+ archived: boolean;
66
+ }
67
+ /**
68
+ * The actions worth offering for the ticked rows, in the order a toolbar shows them. Only what would change at
69
+ * least one of them: "Mark read" is offered while any is unread, "Unarchive" only in the archive. Delete is always
70
+ * offered for a selection, and never for an empty one.
71
+ */
72
+ export declare function actionsFor(rows: readonly InboxRowState[]): InboxAction[];
73
+ /**
74
+ * The field an action sets on every named row. `delete` changes no field: the row goes.
75
+ *
76
+ * Archiving reads a row too — put away is dealt with, so the archive never holds the unread count — but only a row
77
+ * that is still unread: overwriting an earlier `readAt` would restart whatever the app keeps read rows for. That
78
+ * second, narrower update is `readsUnread`.
79
+ */
80
+ export declare function actionChanges(action: Exclude<InboxAction, "delete">, now?: Date): {
81
+ readonly readAt: Date;
82
+ readonly archivedAt?: undefined;
83
+ } | {
84
+ readonly readAt: null;
85
+ readonly archivedAt?: undefined;
86
+ } | {
87
+ readonly archivedAt: Date;
88
+ readonly readAt?: undefined;
89
+ } | {
90
+ readonly archivedAt: null;
91
+ readonly readAt?: undefined;
92
+ };
93
+ /** Whether an action also sets `readAt` on the named rows that are unread (archiving does). */
94
+ export declare function readsUnread(action: InboxAction): boolean;
95
+ /** Whether a row still belongs in a list after an action — so a toolbar knows to take it out of view. */
96
+ export declare function stillListed(filter: InboxFilter, row: InboxRowState): boolean;
97
+ /** A row's state after an action, without asking the server — for an optimistic update. */
98
+ export declare function afterAction(action: Exclude<InboxAction, "delete">, row: InboxRowState): InboxRowState;
99
+ /** The button's words for an action. */
100
+ export declare function actionLabel(action: InboxAction): string;
101
+ /** What to say once an action has been applied to `n` rows. */
102
+ export declare function actionDone(action: InboxAction, n: number): string;
103
+ export {};
104
+ //# sourceMappingURL=inbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inbox.d.ts","sourceRoot":"","sources":["../../src/inbox/inbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;;GAQG;AAEH,+FAA+F;AAC/F,eAAO,MAAM,aAAa,0CAA2C,CAAC;AACtE,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,0GAA0G;AAC1G,eAAO,MAAM,aAAa,+DAAgE,CAAC;AAC3F,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,kDAAkD;AAClD,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC,sGAAsG;AACtG,eAAO,MAAM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AAE,SAAS,aAAa,EAAE,GAAG,WAAW,EAAE,CASxE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,GAAG,GAAE,IAAiB;;;;;;;;;;;;EAW3F;AAED,+FAA+F;AAC/F,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAExD;AAED,yGAAyG;AACzG,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,aAAa,GAAG,OAAO,CAI5E;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,GAAG,EAAE,aAAa,GAAG,aAAa,CAWrG;AAcD,wCAAwC;AACxC,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAEvD;AAED,+DAA+D;AAC/D,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEjE"}
@@ -0,0 +1,178 @@
1
+ import * as z from "zod";
2
+ /**
3
+ * The rules of a notifications inbox, with no markup and no database in them.
4
+ *
5
+ * Which list is showing (`filter`), which page of it (`page`), what can be done
6
+ * to the rows that are ticked (`actionsFor`), what a bulk request may carry
7
+ * (`inboxActionSchema`) and which page numbers a pager shows (`pageWindow`).
8
+ * The app owns the rows, the storage and the look; this owns the arithmetic
9
+ * that every inbox gets slightly wrong on its own.
10
+ */
11
+ /** The lists an inbox shows: everything not archived, only the unread ones, or the archive. */
12
+ export const INBOX_FILTERS = ["inbox", "unread", "archived"];
13
+ /** What can be done to ticked rows. `delete` is permanent; `archive` is the way to put something away. */
14
+ export const INBOX_ACTIONS = ["read", "unread", "archive", "unarchive", "delete"];
15
+ /** Rows a page, unless the app says otherwise. */
16
+ export const INBOX_PAGE_SIZE = 20;
17
+ /** The most rows one bulk request may name — a page and then some, never "everything" by accident. */
18
+ export const INBOX_MAX_BULK = 100;
19
+ /** The longest id accepted. A cuid is 25 characters, a UUID 36. */
20
+ const MAX_ID_LENGTH = 64;
21
+ /**
22
+ * The body of a bulk request: which rows, and what to do to them. The ids are the reader's own only after the
23
+ * app's query scopes them to the reader — this checks shape, never ownership.
24
+ */
25
+ export const inboxActionSchema = z.object({
26
+ ids: z.array(z.string().min(1).max(MAX_ID_LENGTH)).min(1).max(INBOX_MAX_BULK),
27
+ action: z.enum(INBOX_ACTIONS),
28
+ });
29
+ function first(input, key) {
30
+ if (input instanceof URLSearchParams)
31
+ return input.get(key) ?? undefined;
32
+ const value = input[key];
33
+ return Array.isArray(value) ? value[0] : value;
34
+ }
35
+ /**
36
+ * `?filter=` and `?page=` from a request or a page's `searchParams`. Anything unrecognised reads as the inbox's
37
+ * first page rather than an error: an address someone edited, or one from before a filter was renamed, still opens.
38
+ */
39
+ export function parseInboxQuery(input) {
40
+ const rawFilter = first(input, "filter");
41
+ const filter = INBOX_FILTERS.includes(rawFilter) ? rawFilter : "inbox";
42
+ const rawPage = first(input, "page");
43
+ const parsed = rawPage && /^\d{1,6}$/.test(rawPage) ? Number(rawPage) : 1;
44
+ return { filter, page: Math.max(1, parsed) };
45
+ }
46
+ /** The address of one page of one list, keeping the defaults out of it so the plain inbox is the plain path. */
47
+ export function inboxHref(path, filter, page) {
48
+ const params = new URLSearchParams();
49
+ if (filter !== "inbox")
50
+ params.set("filter", filter);
51
+ if (page > 1)
52
+ params.set("page", String(page));
53
+ const query = params.toString();
54
+ return query ? `${path}?${query}` : path;
55
+ }
56
+ /** How many pages `total` rows fill; an empty list is one empty page, so "page 1 of 1" is always true. */
57
+ export function pageCount(total, pageSize = INBOX_PAGE_SIZE) {
58
+ return Math.max(1, Math.ceil(Math.max(0, total) / Math.max(1, pageSize)));
59
+ }
60
+ /** A requested page brought within the list — after a delete empties the last page, the one before it. */
61
+ export function clampPage(page, pages) {
62
+ return Math.min(Math.max(1, Math.floor(page) || 1), Math.max(1, pages));
63
+ }
64
+ /** The first and last row numbers on a page, 1-based, for "21–40 of 57"; both 0 for an empty list. */
65
+ export function pageRange(page, total, pageSize = INBOX_PAGE_SIZE) {
66
+ if (total <= 0)
67
+ return { from: 0, to: 0 };
68
+ const from = (page - 1) * pageSize + 1;
69
+ return { from, to: Math.min(page * pageSize, total) };
70
+ }
71
+ /**
72
+ * The page numbers a pager shows: always the first and last, the current one with `around` either side, and a
73
+ * `"gap"` where numbers are left out. A gap never stands for a single page — that page is shown instead, since a
74
+ * "…" hiding one number is longer than the number.
75
+ */
76
+ export function pageWindow(page, pages, around = 1) {
77
+ const last = Math.max(1, pages);
78
+ const current = clampPage(page, last);
79
+ const wanted = new Set([1, last]);
80
+ for (let p = current - around; p <= current + around; p++)
81
+ if (p >= 1 && p <= last)
82
+ wanted.add(p);
83
+ const sorted = [...wanted].sort((a, b) => a - b);
84
+ const out = [];
85
+ let previous = 0;
86
+ for (const p of sorted) {
87
+ if (p - previous === 2)
88
+ out.push(previous + 1);
89
+ else if (p - previous > 2)
90
+ out.push("gap");
91
+ out.push(p);
92
+ previous = p;
93
+ }
94
+ return out;
95
+ }
96
+ /**
97
+ * The actions worth offering for the ticked rows, in the order a toolbar shows them. Only what would change at
98
+ * least one of them: "Mark read" is offered while any is unread, "Unarchive" only in the archive. Delete is always
99
+ * offered for a selection, and never for an empty one.
100
+ */
101
+ export function actionsFor(rows) {
102
+ if (rows.length === 0)
103
+ return [];
104
+ const actions = [];
105
+ if (rows.some((row) => !row.read))
106
+ actions.push("read");
107
+ if (rows.some((row) => row.read))
108
+ actions.push("unread");
109
+ if (rows.some((row) => !row.archived))
110
+ actions.push("archive");
111
+ if (rows.some((row) => row.archived))
112
+ actions.push("unarchive");
113
+ actions.push("delete");
114
+ return actions;
115
+ }
116
+ /**
117
+ * The field an action sets on every named row. `delete` changes no field: the row goes.
118
+ *
119
+ * Archiving reads a row too — put away is dealt with, so the archive never holds the unread count — but only a row
120
+ * that is still unread: overwriting an earlier `readAt` would restart whatever the app keeps read rows for. That
121
+ * second, narrower update is `readsUnread`.
122
+ */
123
+ export function actionChanges(action, now = new Date()) {
124
+ switch (action) {
125
+ case "read":
126
+ return { readAt: now };
127
+ case "unread":
128
+ return { readAt: null };
129
+ case "archive":
130
+ return { archivedAt: now };
131
+ case "unarchive":
132
+ return { archivedAt: null };
133
+ }
134
+ }
135
+ /** Whether an action also sets `readAt` on the named rows that are unread (archiving does). */
136
+ export function readsUnread(action) {
137
+ return action === "archive";
138
+ }
139
+ /** Whether a row still belongs in a list after an action — so a toolbar knows to take it out of view. */
140
+ export function stillListed(filter, row) {
141
+ if (filter === "archived")
142
+ return row.archived;
143
+ if (filter === "unread")
144
+ return !row.archived && !row.read;
145
+ return !row.archived;
146
+ }
147
+ /** A row's state after an action, without asking the server — for an optimistic update. */
148
+ export function afterAction(action, row) {
149
+ switch (action) {
150
+ case "read":
151
+ return { ...row, read: true };
152
+ case "unread":
153
+ return { ...row, read: false };
154
+ case "archive":
155
+ return { read: true, archived: true };
156
+ case "unarchive":
157
+ return { ...row, archived: false };
158
+ }
159
+ }
160
+ const VERBS = {
161
+ read: { label: "Mark read", done: (n) => `Marked ${count(n)} read.` },
162
+ unread: { label: "Mark unread", done: (n) => `Marked ${count(n)} unread.` },
163
+ archive: { label: "Archive", done: (n) => `Archived ${count(n)}.` },
164
+ unarchive: { label: "Move to inbox", done: (n) => `Moved ${count(n)} to the inbox.` },
165
+ delete: { label: "Delete", done: (n) => `Deleted ${count(n)}.` },
166
+ };
167
+ function count(n) {
168
+ return n === 1 ? "1 notification" : `${n} notifications`;
169
+ }
170
+ /** The button's words for an action. */
171
+ export function actionLabel(action) {
172
+ return VERBS[action].label;
173
+ }
174
+ /** What to say once an action has been applied to `n` rows. */
175
+ export function actionDone(action, n) {
176
+ return VERBS[action].done(n);
177
+ }
178
+ //# sourceMappingURL=inbox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inbox.js","sourceRoot":"","sources":["../../src/inbox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C,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `@connextar/house/inbox` — the rules of a notifications inbox.
3
+ *
4
+ * Filters (inbox, unread, archived), numbered pages, ticked rows and the bulk
5
+ * actions on them — read, unread, archive, move back, delete — with no markup
6
+ * and no database in it: React and Zod, and nothing else. The app owns its rows,
7
+ * its storage and its look, the same bargain `grid` offers.
8
+ */
9
+ export { INBOX_ACTIONS, INBOX_FILTERS, INBOX_MAX_BULK, INBOX_PAGE_SIZE, actionChanges, actionDone, actionLabel, actionsFor, afterAction, clampPage, inboxActionSchema, inboxHref, pageCount, pageRange, pageWindow, parseInboxQuery, readsUnread, stillListed, type InboxAction, type InboxActionRequest, type InboxFilter, type InboxRowState, } from "./inbox.js";
10
+ export { useSelection, type Selection } from "./use-selection.js";
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/inbox/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,aAAa,EACb,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,SAAS,EACT,SAAS,EACT,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,WAAW,EACX,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAChB,KAAK,aAAa,GACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * `@connextar/house/inbox` — the rules of a notifications inbox.
3
+ *
4
+ * Filters (inbox, unread, archived), numbered pages, ticked rows and the bulk
5
+ * actions on them — read, unread, archive, move back, delete — with no markup
6
+ * and no database in it: React and Zod, and nothing else. The app owns its rows,
7
+ * its storage and its look, the same bargain `grid` offers.
8
+ */
9
+ export { INBOX_ACTIONS, INBOX_FILTERS, INBOX_MAX_BULK, INBOX_PAGE_SIZE, actionChanges, actionDone, actionLabel, actionsFor, afterAction, clampPage, inboxActionSchema, inboxHref, pageCount, pageRange, pageWindow, parseInboxQuery, readsUnread, stillListed, } from "./inbox.js";
10
+ export { useSelection } from "./use-selection.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/inbox/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,aAAa,EACb,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,SAAS,EACT,SAAS,EACT,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,WAAW,GAKZ,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAkB,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Ticked rows on a list that changes under them.
3
+ *
4
+ * Only the rows on show can be ticked: what is selected is always the ticked keys that are still in `visible`, so
5
+ * turning a page, changing the filter or a row leaving after an action can never leave something ticked that the
6
+ * reader can't see — and then act on it. Derived on every render rather than pruned by an effect, so there is no
7
+ * render in which a hidden row is still counted.
8
+ */
9
+ export declare function useSelection(visible: readonly string[]): {
10
+ /** The ticked keys on show, in list order. */
11
+ keys: string[];
12
+ count: number;
13
+ /** Every row on show is ticked (and there is at least one). */
14
+ all: boolean;
15
+ /** Some, but not all, are ticked — a header checkbox's "indeterminate". */
16
+ some: boolean;
17
+ isSelected: (key: string) => boolean;
18
+ toggle: (key: string, on?: boolean) => void;
19
+ toggleAll: (on?: boolean) => void;
20
+ clear: () => void;
21
+ };
22
+ export type Selection = ReturnType<typeof useSelection>;
23
+ //# sourceMappingURL=use-selection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-selection.d.ts","sourceRoot":"","sources":["../../src/inbox/use-selection.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE;IAiCnD,8CAA8C;;;IAG9C,+DAA+D;;IAE/D,2EAA2E;;sBARlC,MAAM;kBAtBV,MAAM,OAAO,OAAO;qBAWnD,OAAO;;EA0BhB;AAED,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC"}
@@ -0,0 +1,51 @@
1
+ "use client";
2
+ import * as React from "react";
3
+ /**
4
+ * Ticked rows on a list that changes under them.
5
+ *
6
+ * Only the rows on show can be ticked: what is selected is always the ticked keys that are still in `visible`, so
7
+ * turning a page, changing the filter or a row leaving after an action can never leave something ticked that the
8
+ * reader can't see — and then act on it. Derived on every render rather than pruned by an effect, so there is no
9
+ * render in which a hidden row is still counted.
10
+ */
11
+ export function useSelection(visible) {
12
+ const [ticked, setTicked] = React.useState(() => new Set());
13
+ const visibleSet = React.useMemo(() => new Set(visible), [visible]);
14
+ const keys = React.useMemo(() => visible.filter((key) => ticked.has(key)), [visible, ticked]);
15
+ const count = keys.length;
16
+ const all = visible.length > 0 && count === visible.length;
17
+ const toggle = React.useCallback((key, on) => {
18
+ setTicked((previous) => {
19
+ const next = new Set(previous);
20
+ const tick = on ?? !next.has(key);
21
+ if (tick)
22
+ next.add(key);
23
+ else
24
+ next.delete(key);
25
+ return next;
26
+ });
27
+ }, []);
28
+ const toggleAll = React.useCallback((on) => {
29
+ setTicked((previous) => {
30
+ const tickedVisible = [...visibleSet].filter((key) => previous.has(key)).length;
31
+ const tick = on ?? !(visibleSet.size > 0 && tickedVisible === visibleSet.size);
32
+ return tick ? new Set(visibleSet) : new Set();
33
+ });
34
+ }, [visibleSet]);
35
+ const clear = React.useCallback(() => setTicked(new Set()), []);
36
+ const isSelected = React.useCallback((key) => visibleSet.has(key) && ticked.has(key), [visibleSet, ticked]);
37
+ return {
38
+ /** The ticked keys on show, in list order. */
39
+ keys,
40
+ count,
41
+ /** Every row on show is ticked (and there is at least one). */
42
+ all,
43
+ /** Some, but not all, are ticked — a header checkbox's "indeterminate". */
44
+ some: count > 0 && !all,
45
+ isSelected,
46
+ toggle,
47
+ toggleAll,
48
+ clear,
49
+ };
50
+ }
51
+ //# sourceMappingURL=use-selection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-selection.js","sourceRoot":"","sources":["../../src/inbox/use-selection.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,OAA0B;IACrD,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAsB,GAAG,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;IAEjF,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IACpE,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9F,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;IAC1B,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC;IAE3D,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC,GAAW,EAAE,EAAY,EAAE,EAAE;QAC7D,SAAS,CAAC,CAAC,QAAQ,EAAE,EAAE;YACrB,M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C,CAAC,CAAC;IAEpH,OAAO;QACL,8CAA8C;QAC9C,IAAI;QACJ,KAAK;QACL,+DAA+D;QAC/D,GAAG;QACH,2EAA2E;QAC3E,IAAI,EAAE,KAAK,GAAG,CAAC,IAAI,CAAC,GAAG;QACvB,UAAU;QACV,MAAM;QACN,SAAS;QACT,KAAK;KACN,CAAC;AACJ,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The relay ends an AI session with `{ "type": "end", "handoffData": "<json>" }`;
3
+ * Twilio passes that string to the `<Connect action>` webhook as `HandoffData`.
4
+ * Never put personal data in it: the webhook looks anything else up by CallSid.
5
+ */
6
+ export interface HandoffData {
7
+ reasonCode: "transfer" | "completed";
8
+ reason?: string;
9
+ /** The caller described an emergency and was told to call 999: say it again if nobody answers. */
10
+ emergency?: boolean;
11
+ }
12
+ export interface ParsedHandoff {
13
+ /** `unknown` for anything unexpected, including a session that failed: treat it as a transfer. */
14
+ reasonCode: HandoffData["reasonCode"] | "unknown";
15
+ reason?: string;
16
+ emergency: boolean;
17
+ }
18
+ /** Read `HandoffData` defensively: anything unexpected is "unknown", which transfers to a person. */
19
+ export declare function parseHandoff(raw: string | undefined): ParsedHandoff;
20
+ //# sourceMappingURL=handoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handoff.d.ts","sourceRoot":"","sources":["../../src/voice/handoff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,UAAU,GAAG,WAAW,CAAC;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kGAAkG;IAClG,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,aAAa;IAC5B,kGAAkG;IAClG,UAAU,EAAE,WAAW,CAAC,YAAY,CAAC,GAAG,SAAS,CAAC;IAClD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,qGAAqG;AACrG,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,aAAa,CAanE"}
@@ -0,0 +1,18 @@
1
+ /** Read `HandoffData` defensively: anything unexpected is "unknown", which transfers to a person. */
2
+ export function parseHandoff(raw) {
3
+ if (!raw)
4
+ return { reasonCode: "unknown", emergency: false };
5
+ try {
6
+ const data = JSON.parse(raw);
7
+ const reasonCode = data.reasonCode === "transfer" || data.reasonCode === "completed" ? data.reasonCode : "unknown";
8
+ return {
9
+ reasonCode,
10
+ reason: typeof data.reason === "string" ? data.reason.slice(0, 200) : undefined,
11
+ emergency: data.emergency === true,
12
+ };
13
+ }
14
+ catch {
15
+ return { reasonCode: "unknown", emergency: false };
16
+ }
17
+ }
18
+ //# sourceMappingURL=handoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handoff.js","sourceRoot":"","sources":["../../src/voice/handoff.ts"],"names":[],"mappings":"AAmBA,qGAAqG;AACrG,MAAM,UAAU,YAAY,CAAC,GAAuB;IAClD,IAAI,CAAC,GAAG;QAAE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAC7D,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAoE,CAAC;QAChG,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,KAAK,UAAU,IAAI,IAAI,CAAC,UAAU,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC;QACnH,OAAO;YACL,UAAU;YACV,MAAM,EAAE,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS;YAC/E,SAAS,EAAE,IAAI,CAAC,SAAS,KAAK,IAAI;SACnC,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IACrD,CAAC;AACH,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@connextar/house/voice`: the app side of a Twilio voice line. TwiML builders,
3
+ * webhook signatures, per-call session tokens, UK phone rules, opening hours and
4
+ * the transfer policy. Imports only `node:crypto`, so a Next route handler takes
5
+ * it without the relay's WebSocket server or any model SDK.
6
+ */
7
+ export { connectConversationRelay, DEFAULT_SAY_VOICE, dial, escapeXml, gather, hangup, play, reject, response, say, sayAndHangup, thenHangup, type DialOptions, type GatherOptions, type RelayTwimlOptions, type SayVoice, } from "./twiml.js";
8
+ export { computeTwilioSignature, isValidTwilioSignature } from "./twilio-signature.js";
9
+ export { createTwilioWebhooks, twiml, type TwilioParams, type TwilioReadResult, type TwilioWebhookConfig, } from "./twilio-request.js";
10
+ export { generateVoiceSessionKeys, isUsablePublicKey, isVoiceSessionToken, publicKeyFor, signVoiceSession, verifyVoiceSession, VOICE_SESSION_TTL_SECONDS, type VoiceSession, type VoiceSigner, type VoiceVerifier, } from "./session-token.js";
11
+ export { divertsToAiLine, isAllowedTransferNumber, presentedNumber, toE164, TRANSFER_NUMBER_RULE, type CallForwardingMode, } from "./phone.js";
12
+ export { DAY_KEYS, describeOpeningHours, hasOpeningHours, isOpenAt, nextOpening, parseOpeningHours, spokenTime, type DayKey, type OpeningHours, } from "./opening-hours.js";
13
+ export { lineStatus, teamReachable, transferTarget, type LineHours, type LineStatus } from "./out-of-hours.js";
14
+ export { parseHandoff, type HandoffData, type ParsedHandoff } from "./handoff.js";
15
+ export { dialTarget, isTransferComingBack, LOOP_WINDOW_MS, plannedTransfer, type ComingBackStore, type DialRefusal, type TransferLine, type TransferRefusal, type TransferRule, type TransferRules, type TransferStore, } from "./transfer.js";
16
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/voice/index.ts"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B,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,aAAa,GACnB,MAAM,eAAe,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@connextar/house/voice`: the app side of a Twilio voice line. TwiML builders,
3
+ * webhook signatures, per-call session tokens, UK phone rules, opening hours and
4
+ * the transfer policy. Imports only `node:crypto`, so a Next route handler takes
5
+ * it without the relay's WebSocket server or any model SDK.
6
+ */
7
+ export { connectConversationRelay, DEFAULT_SAY_VOICE, dial, escapeXml, gather, hangup, play, reject, response, say, sayAndHangup, thenHangup, } from "./twiml.js";
8
+ export { computeTwilioSignature, isValidTwilioSignature } from "./twilio-signature.js";
9
+ export { createTwilioWebhooks, twiml, } from "./twilio-request.js";
10
+ export { generateVoiceSessionKeys, isUsablePublicKey, isVoiceSessionToken, publicKeyFor, signVoiceSession, verifyVoiceSession, VOICE_SESSION_TTL_SECONDS, } from "./session-token.js";
11
+ export { divertsToAiLine, isAllowedTransferNumber, presentedNumber, toE164, TRANSFER_NUMBER_RULE, } from "./phone.js";
12
+ export { DAY_KEYS, describeOpeningHours, hasOpeningHours, isOpenAt, nextOpening, parseOpeningHours, spokenTime, } from "./opening-hours.js";
13
+ export { lineStatus, teamReachable, transferTarget } from "./out-of-hours.js";
14
+ export { parseHandoff } from "./handoff.js";
15
+ export { dialTarget, isTransferComingBack, LOOP_WINDOW_MS, plannedTransfer, } from "./transfer.js";
16
+ //# sourceMappingURL=index.js.map