@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/voice/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EACL,wBAAwB,EACxB,iBAAiB,EACjB,IAAI,EACJ,SAAS,EACT,MAAM,EACN,MAAM,EACN,IAAI,EACJ,MAAM,EACN,QAAQ,EACR,GAAG,EACH,YAAY,EACZ,UAAU,GAKX,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AACvF,OAAO,EACL,oBAAoB,EACpB,KAAK,GAIN,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,wBAAwB,EACxB,iBAAiB,EACjB,mBAAmB,EACnB,YAAY,EACZ,gBAAgB,EAChB,kBAAkB,EAClB,yBAAyB,GAI1B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,eAAe,EACf,MAAM,EACN,oBAAoB,GAErB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,QAAQ,EACR,oBAAoB,EACpB,eAAe,EACf,QAAQ,EACR,WAAW,EACX,iBAAiB,EACjB,UAAU,GAGX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,cAAc,EAAmC,MAAM,mBAAmB,CAAC;AAC/G,OAAO,EAAE,YAAY,EAAwC,MAAM,cAAc,CAAC;AAClF,OAAO,EACL,UAAU,EACV,oBAAoB,EACpB,cAAc,EACd,eAAe,GAQhB,MAAM,eAAe,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Opening hours as an app stores them (JSON on its line or business row):
3
+ * `{ mon: [["09:00","18:00"]], …, sun: [] }` — a list of open intervals per day.
4
+ */
5
+ export type OpeningHours = Partial<Record<DayKey, [string, string][]>>;
6
+ export declare const DAY_KEYS: readonly ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
7
+ export type DayKey = (typeof DAY_KEYS)[number];
8
+ export declare function parseOpeningHours(value: unknown): OpeningHours | null;
9
+ /** "09:00" → "9am", "17:30" → "5:30pm", "12:00" → "12pm" — how people say times. */
10
+ export declare function spokenTime(hhmm: string): string;
11
+ /** "Monday to Friday 9am to 6pm, Saturday 9am to 1pm, and closed on Sunday." */
12
+ export declare function describeOpeningHours(hours: OpeningHours | null): string;
13
+ /** Whether the line is open at `at` in its own timezone. */
14
+ export declare function isOpenAt(hours: OpeningHours | null, at: Date, timeZone?: string): boolean;
15
+ /** Whether any opening hours are set. With none, the line is never treated as closed. */
16
+ export declare function hasOpeningHours(hours: OpeningHours | null): hours is OpeningHours;
17
+ /**
18
+ * When the line next opens after `at`, as a caller would be told it: "at 9am today",
19
+ * "at 9am tomorrow", "at 9am on Monday". Null when no opening hours are set. Bank holidays aren't
20
+ * known here, so a bank holiday is treated as the weekday it falls on.
21
+ */
22
+ export declare function nextOpening(hours: OpeningHours | null, at: Date, timeZone?: string): string | null;
23
+ //# sourceMappingURL=opening-hours.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opening-hours.d.ts","sourceRoot":"","sources":["../../src/voice/opening-hours.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC;AAEvE,eAAO,MAAM,QAAQ,4DAA6D,CAAC;AACnF,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC;AAY/C,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,GAAG,IAAI,CAYrE;AAED,oFAAoF;AACpF,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAK/C;AAOD,gFAAgF;AAChF,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,GAAG,MAAM,CAevE;AAED,4DAA4D;AAC5D,wBAAgB,QAAQ,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,SAAkB,GAAG,OAAO,CAgBlG;AAED,yFAAyF;AACzF,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,GAAG,KAAK,IAAI,YAAY,CAEjF;AAmBD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,QAAQ,SAAkB,GAAG,MAAM,GAAG,IAAI,CAc3G"}
@@ -0,0 +1,117 @@
1
+ export const DAY_KEYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
2
+ const DAY_NAMES = {
3
+ mon: "Monday",
4
+ tue: "Tuesday",
5
+ wed: "Wednesday",
6
+ thu: "Thursday",
7
+ fri: "Friday",
8
+ sat: "Saturday",
9
+ sun: "Sunday",
10
+ };
11
+ export function parseOpeningHours(value) {
12
+ if (!value || typeof value !== "object")
13
+ return null;
14
+ const hours = {};
15
+ for (const day of DAY_KEYS) {
16
+ const intervals = value[day];
17
+ if (!Array.isArray(intervals))
18
+ continue;
19
+ hours[day] = intervals.filter((i) => Array.isArray(i) && i.length === 2 && i.every((t) => typeof t === "string" && /^\d{2}:\d{2}$/.test(t)));
20
+ }
21
+ return hours;
22
+ }
23
+ /** "09:00" → "9am", "17:30" → "5:30pm", "12:00" → "12pm" — how people say times. */
24
+ export function spokenTime(hhmm) {
25
+ const [h, m] = hhmm.split(":").map(Number);
26
+ const suffix = h >= 12 ? "pm" : "am";
27
+ const hour12 = h % 12 === 0 ? 12 : h % 12;
28
+ return m === 0 ? `${hour12}${suffix}` : `${hour12}:${String(m).padStart(2, "0")}${suffix}`;
29
+ }
30
+ function daySentence(intervals) {
31
+ if (!intervals || intervals.length === 0)
32
+ return "closed";
33
+ return intervals.map(([open, close]) => `${spokenTime(open)} to ${spokenTime(close)}`).join(" and ");
34
+ }
35
+ /** "Monday to Friday 9am to 6pm, Saturday 9am to 1pm, and closed on Sunday." */
36
+ export function describeOpeningHours(hours) {
37
+ if (!hours)
38
+ return "Opening hours haven't been set up yet.";
39
+ const groups = [];
40
+ for (const day of DAY_KEYS) {
41
+ const text = daySentence(hours[day]);
42
+ const last = groups[groups.length - 1];
43
+ if (last && last.text === text)
44
+ last.to = day;
45
+ else
46
+ groups.push({ from: day, to: day, text });
47
+ }
48
+ const parts = groups.map((g) => {
49
+ const days = g.from === g.to ? DAY_NAMES[g.from] : `${DAY_NAMES[g.from]} to ${DAY_NAMES[g.to]}`;
50
+ return g.text === "closed" ? `closed on ${days}` : `${days} ${g.text}`;
51
+ });
52
+ const sentence = parts.length > 1 ? `${parts.slice(0, -1).join(", ")}, and ${parts[parts.length - 1]}` : parts[0];
53
+ return `${sentence.charAt(0).toUpperCase()}${sentence.slice(1)}.`;
54
+ }
55
+ /** Whether the line is open at `at` in its own timezone. */
56
+ export function isOpenAt(hours, at, timeZone = "Europe/London") {
57
+ if (!hours)
58
+ return false;
59
+ const parts = new Intl.DateTimeFormat("en-GB", {
60
+ timeZone,
61
+ weekday: "short",
62
+ hour: "2-digit",
63
+ minute: "2-digit",
64
+ hourCycle: "h23",
65
+ }).formatToParts(at);
66
+ const weekday = parts
67
+ .find((p) => p.type === "weekday")
68
+ ?.value.toLowerCase()
69
+ .slice(0, 3);
70
+ const time = `${parts.find((p) => p.type === "hour")?.value}:${parts.find((p) => p.type === "minute")?.value}`;
71
+ if (!weekday)
72
+ return false;
73
+ return (hours[weekday] ?? []).some(([open, close]) => time >= open && time < close);
74
+ }
75
+ /** Whether any opening hours are set. With none, the line is never treated as closed. */
76
+ export function hasOpeningHours(hours) {
77
+ return Boolean(hours && DAY_KEYS.some((d) => (hours[d] ?? []).length > 0));
78
+ }
79
+ /** The weekday and "HH:MM" at `at` in a timezone. */
80
+ function localDayAndTime(at, timeZone) {
81
+ const parts = new Intl.DateTimeFormat("en-GB", {
82
+ timeZone,
83
+ weekday: "short",
84
+ hour: "2-digit",
85
+ minute: "2-digit",
86
+ hourCycle: "h23",
87
+ }).formatToParts(at);
88
+ const weekday = parts
89
+ .find((p) => p.type === "weekday")
90
+ ?.value.toLowerCase()
91
+ .slice(0, 3);
92
+ const time = `${parts.find((p) => p.type === "hour")?.value}:${parts.find((p) => p.type === "minute")?.value}`;
93
+ return { day: DAY_KEYS.indexOf(weekday), time };
94
+ }
95
+ /**
96
+ * When the line next opens after `at`, as a caller would be told it: "at 9am today",
97
+ * "at 9am tomorrow", "at 9am on Monday". Null when no opening hours are set. Bank holidays aren't
98
+ * known here, so a bank holiday is treated as the weekday it falls on.
99
+ */
100
+ export function nextOpening(hours, at, timeZone = "Europe/London") {
101
+ if (!hasOpeningHours(hours))
102
+ return null;
103
+ const { day, time } = localDayAndTime(at, timeZone);
104
+ for (let ahead = 0; ahead <= 7; ahead++) {
105
+ const key = DAY_KEYS[(day + ahead) % 7];
106
+ const opens = (hours[key] ?? [])
107
+ .map(([open]) => open)
108
+ .filter((open) => ahead > 0 || open > time)
109
+ .sort()[0];
110
+ if (!opens)
111
+ continue;
112
+ const when = ahead === 0 ? "today" : ahead === 1 ? "tomorrow" : `on ${DAY_NAMES[key]}`;
113
+ return `at ${spokenTime(opens)} ${when}`;
114
+ }
115
+ return null;
116
+ }
117
+ //# sourceMappingURL=opening-hours.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opening-hours.js","sourceRoot":"","sources":["../../src/voice/opening-hours.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAU,CAAC;AAGnF,MAAM,SAAS,GAA2B;IACxC,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,SAAS;IACd,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,UAAU;IACf,GAAG,EAAE,QAAQ;IACb,GAAG,EAAE,UAAU;IACf,GAAG,EAAE,QAAQ;CACd,CAAC;AAEF,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACrD,MAAM,KAAK,GAAiB,EAAE,CAAC;IAC/B,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,MAAM,SAAS,GAAI,KAAiC,CAAC,GAAG,CAAC,CAAC;QAC1D,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;YAAE,SAAS;QACxC,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,MAAM,CAC3B,CAAC,CAAC,EAAyB,EAAE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AAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACzC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,eAAe,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IACpD,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QACxC,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,CAAE,CAAC;QACzC,MAAM,KAAK,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;aAC7B,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC;aACrB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC,IAAI,IAAI,GAAG,IAAI,CAAC;aAC1C,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACb,IAAI,CAAC,KAAK;YAAE,SAAS;QACrB,MAAM,IAAI,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;QACvF,OAAO,MAAM,UAAU,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;IAC3C,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Whether a line is open now, and where a transfer goes when it isn't.
3
+ *
4
+ * Decided here, from the opening hours, rather than by the model: whether a
5
+ * caller is put through to a person can't depend on how a reply was worded. A
6
+ * line with no opening hours set is never treated as closed.
7
+ *
8
+ * What a caller hears when a line is closed is the app's own wording, so none is
9
+ * here (PharmaLine's names NHS 111; a switchboard says when it will call back).
10
+ */
11
+ export interface LineHours {
12
+ openingHours: unknown;
13
+ timezone: string;
14
+ /** Where transfers go while closed, or null for no transfer. */
15
+ outOfHoursTransferNumber: string | null;
16
+ }
17
+ export interface LineStatus {
18
+ open: boolean;
19
+ /** "at 9am tomorrow": null while open or when no hours are set. */
20
+ opensAgain: string | null;
21
+ /** Where transfers go while closed (E.164), or null for no transfer. */
22
+ outOfHoursNumber: string | null;
23
+ }
24
+ export declare function lineStatus(line: LineHours, now?: Date): LineStatus;
25
+ /** Whether a transfer reaches a person right now: open, or closed with an out-of-hours number. */
26
+ export declare function teamReachable(status: LineStatus): boolean;
27
+ /** The number a transfer rings now, or null when the caller should hear the closed line instead. */
28
+ export declare function transferTarget(status: LineStatus, transferNumber: string | null): string | null;
29
+ //# sourceMappingURL=out-of-hours.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"out-of-hours.d.ts","sourceRoot":"","sources":["../../src/voice/out-of-hours.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AAEH,MAAM,WAAW,SAAS;IACxB,YAAY,EAAE,OAAO,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,gEAAgE;IAChE,wBAAwB,EAAE,MAAM,GAAG,IAAI,CAAC;CACzC;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,mEAAmE;IACnE,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,wEAAwE;IACxE,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CACjC;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,GAAE,IAAiB,GAAG,UAAU,CAQ9E;AAED,kGAAkG;AAClG,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAEzD;AAED,oGAAoG;AACpG,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,IAAI,CAE/F"}
@@ -0,0 +1,20 @@
1
+ import { hasOpeningHours, isOpenAt, nextOpening, parseOpeningHours } from "./opening-hours.js";
2
+ import { toE164 } from "./phone.js";
3
+ export function lineStatus(line, now = new Date()) {
4
+ const hours = parseOpeningHours(line.openingHours);
5
+ const open = !hasOpeningHours(hours) || isOpenAt(hours, now, line.timezone);
6
+ return {
7
+ open,
8
+ opensAgain: open ? null : nextOpening(hours, now, line.timezone),
9
+ outOfHoursNumber: toE164(line.outOfHoursTransferNumber),
10
+ };
11
+ }
12
+ /** Whether a transfer reaches a person right now: open, or closed with an out-of-hours number. */
13
+ export function teamReachable(status) {
14
+ return status.open || Boolean(status.outOfHoursNumber);
15
+ }
16
+ /** The number a transfer rings now, or null when the caller should hear the closed line instead. */
17
+ export function transferTarget(status, transferNumber) {
18
+ return status.open ? toE164(transferNumber) : status.outOfHoursNumber;
19
+ }
20
+ //# sourceMappingURL=out-of-hours.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"out-of-hours.js","sourceRoot":"","sources":["../../src/voice/out-of-hours.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC/F,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AA4BpC,MAAM,UAAU,UAAU,CAAC,IAAe,EAAE,MAAY,IAAI,IAAI,EAAE;IAChE,MAAM,KAAK,GAAG,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACnD,MAAM,IAAI,GAAG,CAAC,eAAe,CAAC,KAAK,CAAC,IAAI,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC5E,OAAO;QACL,IAAI;QACJ,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC;QAChE,gBAAgB,EAAE,MAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC;KACxD,CAAC;AACJ,CAAC;AAED,kGAAkG;AAClG,MAAM,UAAU,aAAa,CAAC,MAAkB;IAC9C,OAAO,MAAM,CAAC,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;AACzD,CAAC;AAED,oGAAoG;AACpG,MAAM,UAAU,cAAc,CAAC,MAAkB,EAAE,cAA6B;IAC9E,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC;AACxE,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Normalise a UK-style or international phone number to E.164 (`+441614960000`) so numbers typed
3
+ * into Settings ("0161 496 0000") match what the carrier sends. Returns null when it can't be a
4
+ * phone number.
5
+ */
6
+ export declare function toE164(value: string | null | undefined, defaultCountryCode?: string): string | null;
7
+ /**
8
+ * Whether the platform may dial this number to put a caller through to staff.
9
+ *
10
+ * Every transfer is dialled from the app's own Twilio account, and an owner sets the transfer
11
+ * and out-of-hours numbers themselves — so a number that earns money when called (premium 09,
12
+ * revenue-sharing 084/087, personal 070) or costs a lot to reach (international) would let anyone
13
+ * who can make the line transfer run up the platform's bill (security round 3, finding 7). A
14
+ * team answers on a UK landline (01, 02), a non-geographic 03, a mobile (07, but not 070
15
+ * personal numbers or 076 pagers), or freephone (0800, 0808). Takes an E.164 number.
16
+ */
17
+ export declare function isAllowedTransferNumber(e164: string | null | undefined): boolean;
18
+ /** A caller's own number as presented, or null when it is withheld or not a number. */
19
+ export declare function presentedNumber(value: string | null | undefined): string | null;
20
+ /** What an owner is told when a number is refused. */
21
+ export declare const TRANSFER_NUMBER_RULE = "Use a UK landline, mobile or freephone number (01, 02, 03, 07 or 0800/0808) \u2014 premium, personal and international numbers can't be used";
22
+ /** How a line's own number reaches the AI line (PharmaLine's `Organization.callForwarding`). */
23
+ export type CallForwardingMode = "EVERY_CALL" | "EVERY_CALL_EXCEPT_AI_LINE" | "BUSY_OR_UNANSWERED";
24
+ /**
25
+ * Whether ringing `number` reaches the AI line again rather than a person: it is the line's own
26
+ * number, and every call to that is diverted to the AI line. `dialTarget` never dials it, and an
27
+ * app's settings should warn when a transfer number is one.
28
+ */
29
+ export declare function divertsToAiLine(line: {
30
+ phone: string | null;
31
+ callForwarding: CallForwardingMode | null;
32
+ }, number: string | null): boolean;
33
+ //# sourceMappingURL=phone.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"phone.d.ts","sourceRoot":"","sources":["../../src/voice/phone.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,kBAAkB,SAAO,GAAG,MAAM,GAAG,IAAI,CAejG;AAED;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAEhF;AAQD,uFAAuF;AACvF,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAI/E;AAED,sDAAsD;AACtD,eAAO,MAAM,oBAAoB,iJAC0G,CAAC;AAE5I,gGAAgG;AAChG,MAAM,MAAM,kBAAkB,GAAG,YAAY,GAAG,2BAA2B,GAAG,oBAAoB,CAAC;AAEnG;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,cAAc,EAAE,kBAAkB,GAAG,IAAI,CAAA;CAAE,EACzE,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,OAAO,CAGT"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Normalise a UK-style or international phone number to E.164 (`+441614960000`) so numbers typed
3
+ * into Settings ("0161 496 0000") match what the carrier sends. Returns null when it can't be a
4
+ * phone number.
5
+ */
6
+ export function toE164(value, defaultCountryCode = "44") {
7
+ if (!value)
8
+ return null;
9
+ const trimmed = value.trim();
10
+ const digits = trimmed.replace(/\D/g, "");
11
+ if (!digits)
12
+ return null;
13
+ let e164;
14
+ if (trimmed.startsWith("+"))
15
+ e164 = `+${digits}`;
16
+ else if (digits.startsWith("00"))
17
+ e164 = `+${digits.slice(2)}`;
18
+ else if (digits.startsWith("0"))
19
+ e164 = `+${defaultCountryCode}${digits.slice(1)}`;
20
+ else
21
+ e164 = `+${digits}`;
22
+ // E.164 allows up to 15 digits; anything shorter than 8 isn't a dialable line.
23
+ const length = e164.length - 1;
24
+ return length >= 8 && length <= 15 ? e164 : null;
25
+ }
26
+ /**
27
+ * Whether the platform may dial this number to put a caller through to staff.
28
+ *
29
+ * Every transfer is dialled from the app's own Twilio account, and an owner sets the transfer
30
+ * and out-of-hours numbers themselves — so a number that earns money when called (premium 09,
31
+ * revenue-sharing 084/087, personal 070) or costs a lot to reach (international) would let anyone
32
+ * who can make the line transfer run up the platform's bill (security round 3, finding 7). A
33
+ * team answers on a UK landline (01, 02), a non-geographic 03, a mobile (07, but not 070
34
+ * personal numbers or 076 pagers), or freephone (0800, 0808). Takes an E.164 number.
35
+ */
36
+ export function isAllowedTransferNumber(e164) {
37
+ return Boolean(e164 && /^\+44(?:1\d{8,9}|2\d{9}|3\d{9}|7[1-57-9]\d{8}|80[08]\d{6,7})$/.test(e164));
38
+ }
39
+ /**
40
+ * Carrier stand-ins for a number that isn't presented — withheld, restricted, unavailable — which
41
+ * Twilio sends in place of a caller ID. Thousands of callers share each, so none is a caller.
42
+ */
43
+ const NOT_A_CALLER = new Set(["+266696687", "+86282452253", "+7378742833", "+2562533", "+8656696"]);
44
+ /** A caller's own number as presented, or null when it is withheld or not a number. */
45
+ export function presentedNumber(value) {
46
+ const digits = value?.replace(/[^\d+]/g, "");
47
+ if (!digits || !/^\+\d{8,15}$/.test(digits) || NOT_A_CALLER.has(digits))
48
+ return null;
49
+ return digits;
50
+ }
51
+ /** What an owner is told when a number is refused. */
52
+ export const TRANSFER_NUMBER_RULE = "Use a UK landline, mobile or freephone number (01, 02, 03, 07 or 0800/0808) — premium, personal and international numbers can't be used";
53
+ /**
54
+ * Whether ringing `number` reaches the AI line again rather than a person: it is the line's own
55
+ * number, and every call to that is diverted to the AI line. `dialTarget` never dials it, and an
56
+ * app's settings should warn when a transfer number is one.
57
+ */
58
+ export function divertsToAiLine(line, number) {
59
+ const own = toE164(line.phone);
60
+ return line.callForwarding === "EVERY_CALL" && Boolean(own) && toE164(number) === own;
61
+ }
62
+ //# sourceMappingURL=phone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"phone.js","sourceRoot":"","sources":["../../src/voice/phone.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,KAAgC,EAAE,kBAAkB,GAAG,IAAI;IAChF,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IACxB,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC1C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IAEzB,IAAI,IAAY,CAAC;IACjB,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;SAC5C,IAAI,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,IAAI,GAAG,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;SAC1D,IAAI,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,IAAI,GAAG,IAAI,kBAAkB,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;;QAC9E,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;IAEzB,+EAA+E;IAC/E,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAC/B,OAAO,MAAM,IAAI,CAAC,IAAI,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;AACnD,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CAAC,IAA+B;IACrE,OAAO,OAAO,CAAC,IAAI,IAAI,+DAA+D,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;AACrG,CAAC;AAED;;;GAGG;AACH,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,cAAc,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC;AAEpG,uFAAuF;AACvF,MAAM,UAAU,eAAe,CAAC,KAAgC;IAC9D,MAAM,MAAM,GAAG,KAAK,EAAE,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;IAC7C,IAAI,CAAC,MAAM,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAC;IACrF,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,sDAAsD;AACtD,MAAM,CAAC,MAAM,oBAAoB,GAC/B,yIAAyI,CAAC;AAK5I;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,IAAyE,EACzE,MAAqB;IAErB,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/B,OAAO,IAAI,CAAC,cAAc,KAAK,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC;AACxF,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The relay's side of an app's agent API: JSON over HTTPS with the call's
3
+ * session token as a Bearer token, and every answer in the `{ success, data }`
4
+ * shape the house apps use. The routes and their bodies are the app's own.
5
+ */
6
+ export declare class AgentApiError extends Error {
7
+ readonly status?: number | undefined;
8
+ constructor(message: string, status?: number | undefined);
9
+ }
10
+ export interface AgentFetch {
11
+ get<T>(path: string): Promise<T>;
12
+ post<T>(path: string, body: unknown): Promise<T>;
13
+ }
14
+ export interface AgentFetchOptions {
15
+ /** The app's base URL, such as `https://support.example.com`. */
16
+ baseUrl: string;
17
+ /** The call's session token. */
18
+ sessionToken: string;
19
+ /** Per-request timeout. Default 8 s: a caller is waiting. */
20
+ timeoutMs?: number;
21
+ fetch?: typeof fetch;
22
+ }
23
+ export declare function createAgentFetch(o: AgentFetchOptions): AgentFetch;
24
+ //# sourceMappingURL=agent-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-client.d.ts","sourceRoot":"","sources":["../../../src/voice/relay/agent-client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,qBAAa,aAAc,SAAQ,KAAK;IAGpC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM;gBADxB,OAAO,EAAE,MAAM,EACN,MAAM,CAAC,EAAE,MAAM,YAAA;CAK3B;AAED,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAClD;AAED,MAAM,WAAW,iBAAiB;IAChC,iEAAiE;IACjE,OAAO,EAAE,MAAM,CAAC;IAChB,gCAAgC;IAChC,YAAY,EAAE,MAAM,CAAC;IACrB,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,iBAAiB,GAAG,UAAU,CAgCjE"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The relay's side of an app's agent API: JSON over HTTPS with the call's
3
+ * session token as a Bearer token, and every answer in the `{ success, data }`
4
+ * shape the house apps use. The routes and their bodies are the app's own.
5
+ */
6
+ export class AgentApiError extends Error {
7
+ status;
8
+ constructor(message, status) {
9
+ super(message);
10
+ this.status = status;
11
+ this.name = "AgentApiError";
12
+ }
13
+ }
14
+ export function createAgentFetch(o) {
15
+ const timeoutMs = o.timeoutMs ?? 8_000;
16
+ const doFetch = o.fetch ?? fetch;
17
+ async function call(method, path, body) {
18
+ const response = await doFetch(new URL(path, o.baseUrl), {
19
+ method,
20
+ headers: {
21
+ authorization: `Bearer ${o.sessionToken}`,
22
+ ...(body === undefined ? {} : { "content-type": "application/json" }),
23
+ },
24
+ body: body === undefined ? undefined : JSON.stringify(body),
25
+ signal: AbortSignal.timeout(timeoutMs),
26
+ });
27
+ let json;
28
+ try {
29
+ json = (await response.json());
30
+ }
31
+ catch {
32
+ json = null;
33
+ }
34
+ if (!response.ok || !json || json.success === false) {
35
+ const message = typeof json?.error === "string" ? json.error : json?.error?.message;
36
+ throw new AgentApiError(message ?? `Agent API ${method} ${path} failed (${response.status})`, response.status);
37
+ }
38
+ return json.data;
39
+ }
40
+ return {
41
+ get: (path) => call("GET", path),
42
+ post: (path, body) => call("POST", path, body),
43
+ };
44
+ }
45
+ //# sourceMappingURL=agent-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-client.js","sourceRoot":"","sources":["../../../src/voice/relay/agent-client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,OAAO,aAAc,SAAQ,KAAK;IAG3B;IAFX,YACE,OAAe,EACN,MAAe;QAExB,KAAK,CAAC,OAAO,CAAC,CAAC;QAFN,WAAM,GAAN,MAAM,CAAS;QAGxB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AAiBD,MAAM,UAAU,gBAAgB,CAAC,CAAoB;IACnD,MAAM,SAAS,GAAG,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC;IACvC,MAA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IAED,OAAO;QACL,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC;QAChC,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC;KAC/C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,124 @@
1
+ import Anthropic from "@anthropic-ai/sdk";
2
+ import OpenAI from "openai";
3
+ import { type ResolvedAi } from "../../ai/config.js";
4
+ import { type AiCallResult } from "../../ai/provider.js";
5
+ /**
6
+ * The one place a voice relay calls either provider's SDK. Lifted from
7
+ * PharmaLineAI (0.27.7); its tools are now the app's, passed in.
8
+ *
9
+ * Everything above this file asks the same thing ("here is the call so far, what
10
+ * do you say and which tool do you want?") and gets back spoken text and tool
11
+ * calls. Which company answered is a deployment decision, not something the call
12
+ * session knows about. The harness in `@connextar/house/ai` answers one message
13
+ * with prose or a parsed object; a phone call is a streamed, multi-turn
14
+ * conversation with tools, which is why this is its own file.
15
+ *
16
+ * It hides three real differences, and deliberately nothing else:
17
+ *
18
+ * 1. **The history has two shapes.** Anthropic takes tool results as blocks in
19
+ * the next user message; OpenAI's Responses API takes them as their own items.
20
+ * 2. **Output budgets mean different things.** An OpenAI reasoning model spends
21
+ * `max_output_tokens` on reasoning before it says a word, hence the floor.
22
+ * 3. **OpenAI stores responses by default.** A call's words are personal data,
23
+ * so `store: false` is sent on every request.
24
+ */
25
+ /** The slice of the Messages API the dialog needs, so tests can script the model. */
26
+ export type ContentBlock = {
27
+ type: "text";
28
+ text: string;
29
+ } | {
30
+ type: "tool_use";
31
+ id: string;
32
+ name: string;
33
+ input: Record<string, unknown>;
34
+ };
35
+ export type ToolResultBlock = {
36
+ type: "tool_result";
37
+ tool_use_id: string;
38
+ content: string;
39
+ };
40
+ export interface DialogMessage {
41
+ role: "user" | "assistant";
42
+ content: string | (ContentBlock | ToolResultBlock | {
43
+ type: "text";
44
+ text: string;
45
+ })[];
46
+ }
47
+ export interface ModelTurn {
48
+ content: ContentBlock[];
49
+ /** The reply stopped at the token limit, so its words and any tool call may be incomplete. */
50
+ cutOff?: boolean;
51
+ }
52
+ /** What one model request used, as the provider counted it: the harness's usage shape. */
53
+ export type TurnUsage = AiCallResult;
54
+ /** A tool, described the way the Messages API takes it. */
55
+ export interface DialogTool {
56
+ name: string;
57
+ description: string;
58
+ input_schema: {
59
+ type: "object";
60
+ properties: Record<string, unknown>;
61
+ required?: string[];
62
+ };
63
+ }
64
+ export interface DialogModel {
65
+ respond(args: {
66
+ /** The instructions as blocks, most stable first. Each boundary is a cache boundary. */
67
+ system: string[];
68
+ messages: DialogMessage[];
69
+ signal: AbortSignal;
70
+ /** Streamed text, forwarded to Twilio as it arrives so speech starts early. */
71
+ onText: (delta: string) => void;
72
+ /** The request's token usage, reported once when it is known. */
73
+ onUsage?: (usage: TurnUsage) => void;
74
+ /** Which tool list to offer (`tools(team)`), such as whether anyone can be put through. */
75
+ team?: string;
76
+ }): Promise<ModelTurn>;
77
+ }
78
+ /** The resolved provider, key and model, how long a reply may run, and the app's tools. */
79
+ export interface DialogModelOptions extends ResolvedAi {
80
+ maxTokens?: number;
81
+ /** How hard an OpenAI reasoning model thinks before it speaks. Sent only when set. */
82
+ reasoningEffort?: OpenAI.ReasoningEffort;
83
+ /** How hard a current Claude model thinks before it speaks. Sent only when set. */
84
+ anthropicEffort?: Anthropic.OutputConfig["effort"];
85
+ /** Claude only: `disabled` turns its adaptive thinking off, for measuring; not a default. */
86
+ anthropicThinking?: "adaptive" | "disabled";
87
+ /** The tools offered for each `team` value (default `"open"`). Called once per value and kept. */
88
+ tools: (team: string) => DialogTool[];
89
+ /** The tool whose `say` argument is spoken while it streams, such as `transfer_to_team`. */
90
+ transferTool?: string;
91
+ /** Flags on the transfer tool that silence its streamed line (see `TransferLineStream`). */
92
+ transferSilentKeys?: readonly string[];
93
+ /** Names the shared prompt cache on OpenAI (`cacheKey`). */
94
+ cacheKeyPrefix?: string;
95
+ }
96
+ /** Claude's tools. The transfer streams its arguments as they are written, so its line can be spoken early. */
97
+ export declare function anthropicTools(tools: DialogTool[], transferTool?: string): Anthropic.Tool[];
98
+ /** The same tools, described the way the Responses API asks for them. */
99
+ export declare function openAiTools(tools: DialogTool[]): OpenAI.Responses.FunctionTool[];
100
+ /** The dialog policy for the configured provider, streaming text and calling the app's tools. */
101
+ export declare function createDialogModel(opts: DialogModelOptions): DialogModel;
102
+ /** Anthropic's usage block, in the relay's terms. Its `input_tokens` already excludes the cache. */
103
+ export declare function anthropicUsage(model: string, usage: Anthropic.Usage): TurnUsage;
104
+ /**
105
+ * OpenAI's usage block, in the relay's terms. OpenAI counts cache reads and
106
+ * writes inside `input_tokens`, so both are taken out and priced at their own
107
+ * rates; reasoning tokens are already inside `output_tokens`.
108
+ */
109
+ export declare function openAiUsage(model: string, usage: OpenAI.Responses.ResponseUsage): TurnUsage;
110
+ /** Names the shared prefix: the first block of instructions, the same for every tenant. */
111
+ export declare function cacheKey(system: string[], prefix?: string): string;
112
+ /**
113
+ * The conversation with a cache boundary on its last block, so the next request
114
+ * reads everything said so far from the cache. A copy: the history is left alone.
115
+ */
116
+ export declare function withConversationCached(messages: DialogMessage[]): Anthropic.MessageParam[];
117
+ /**
118
+ * The call so far, as Responses API input items. Text becomes a message, a tool
119
+ * call a `function_call` and its result a `function_call_output`, in order.
120
+ * Items carry no `id`, so they are read as fresh input, which is what lets
121
+ * `store: false` work across turns.
122
+ */
123
+ export declare function toResponsesInput(messages: DialogMessage[]): OpenAI.Responses.ResponseInputItem[];
124
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../../../src/voice/relay/model.ts"],"names":[],"mappings":"AAEA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAwC,KAAK,UAAU,EAAE,MAAM,oB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