supafone-labs 0.5.3 → 0.6.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/dist/cjs/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * A dependency-free TypeScript client for creating hosted Supafone agents
6
6
  * through the Supafone API, including managed phone numbers, voices, stages,
7
7
  * tools, recordings, transcripts, widgets, and Supafone Supervisor. It also
8
- * includes the Labs cloud sidecar oracle, hosted TTS/STT, live multilingual
8
+ * includes Supafone Supervisor, hosted TTS/STT, live multilingual
9
9
  * transcription, telemetry, agent builder, and objective-driven optimizer.
10
10
  *
11
11
  * Works in Node 18+ (native fetch/WebSocket) and the browser.
@@ -157,6 +157,19 @@ class SupafoneLabsError extends Error {
157
157
  }
158
158
  }
159
159
  exports.SupafoneLabsError = SupafoneLabsError;
160
+ function errorDetailMessage(detail, fallback) {
161
+ if (detail && typeof detail === "object" && !Array.isArray(detail)) {
162
+ const value = detail;
163
+ for (const key of ["message", "detail", "error", "code"]) {
164
+ const candidate = value[key];
165
+ if (typeof candidate === "string" && candidate.trim())
166
+ return candidate.trim();
167
+ }
168
+ }
169
+ if (typeof detail === "string" && detail.trim())
170
+ return detail.trim();
171
+ return fallback;
172
+ }
160
173
  const DEFAULT_BASE = "https://api.labs.supafone.ai";
161
174
  const DEFAULT_SUPAFONE_API_BASE = "https://api.supafone.ai";
162
175
  const COACH_SYSTEM = "You are the coaching core of a supervisor for a live voice agent. Read the " +
@@ -358,8 +371,8 @@ class SupafoneLabs {
358
371
  const text = await res.text();
359
372
  const parsed = text ? safeJson(text) : {};
360
373
  if (!res.ok) {
361
- const detail = parsed?.detail ?? text ?? `HTTP ${res.status}`;
362
- throw new SupafoneLabsError(`${method} ${path}: ${detail}`, res.status, parsed);
374
+ const detail = parsed?.detail ?? text;
375
+ throw new SupafoneLabsError(`${method} ${path}: ${errorDetailMessage(detail, `HTTP ${res.status}`)}`, res.status, parsed);
363
376
  }
364
377
  return parsed;
365
378
  }
@@ -381,8 +394,8 @@ class SupafoneLabs {
381
394
  const text = await res.text();
382
395
  const parsed = text ? safeJson(text) : {};
383
396
  if (!res.ok) {
384
- const detail = parsed?.detail ?? text ?? `HTTP ${res.status}`;
385
- throw new SupafoneLabsError(`${method} ${path}: ${detail}`, res.status, parsed);
397
+ const detail = parsed?.detail ?? text;
398
+ throw new SupafoneLabsError(`${method} ${path}: ${errorDetailMessage(detail, `HTTP ${res.status}`)}`, res.status, parsed);
386
399
  }
387
400
  return parsed;
388
401
  }
@@ -408,8 +421,8 @@ class SupafoneLabs {
408
421
  const text = await res.text();
409
422
  const parsed = text ? safeJson(text) : {};
410
423
  if (!res.ok) {
411
- const detail = parsed?.detail ?? text ?? `HTTP ${res.status}`;
412
- throw new SupafoneLabsError(`POST ${path}: ${detail}`, res.status, parsed);
424
+ const detail = parsed?.detail ?? text;
425
+ throw new SupafoneLabsError(`POST ${path}: ${errorDetailMessage(detail, `HTTP ${res.status}`)}`, res.status, parsed);
413
426
  }
414
427
  return parsed;
415
428
  }
@@ -430,8 +443,8 @@ class SupafoneLabs {
430
443
  if (!res.ok) {
431
444
  const text = await res.text();
432
445
  const parsed = text ? safeJson(text) : {};
433
- const detail = parsed?.detail ?? text ?? `HTTP ${res.status}`;
434
- throw new SupafoneLabsError(`GET ${path}: ${detail}`, res.status, parsed);
446
+ const detail = parsed?.detail ?? text;
447
+ throw new SupafoneLabsError(`GET ${path}: ${errorDetailMessage(detail, `HTTP ${res.status}`)}`, res.status, parsed);
435
448
  }
436
449
  return {
437
450
  content: await res.arrayBuffer(),
@@ -494,8 +507,8 @@ class SupafoneLabs {
494
507
  const text = await res.text();
495
508
  const parsed = text ? safeJson(text) : {};
496
509
  if (!res.ok) {
497
- const detail = parsed?.detail ?? text ?? `HTTP ${res.status}`;
498
- throw new SupafoneLabsError(`POST ${path}: ${detail}`, res.status, parsed);
510
+ const detail = parsed?.detail ?? text;
511
+ throw new SupafoneLabsError(`POST ${path}: ${errorDetailMessage(detail, `HTTP ${res.status}`)}`, res.status, parsed);
499
512
  }
500
513
  return parsed;
501
514
  };
@@ -528,8 +541,8 @@ class SupafoneLabs {
528
541
  const text = await res.text();
529
542
  const parsed = text ? safeJson(text) : {};
530
543
  if (!res.ok) {
531
- const detail = parsed?.detail ?? text ?? `HTTP ${res.status}`;
532
- throw new SupafoneLabsError(`${method} ${path}: ${detail}`, res.status, parsed);
544
+ const detail = parsed?.detail ?? text;
545
+ throw new SupafoneLabsError(`${method} ${path}: ${errorDetailMessage(detail, `HTTP ${res.status}`)}`, res.status, parsed);
533
546
  }
534
547
  return parsed;
535
548
  }
@@ -604,22 +617,26 @@ class SupafoneLabs {
604
617
  }
605
618
  return this.requestAccountApi("POST", "/api/v1/agents/generate-intake", payload);
606
619
  }
607
- /** Raw oracle completion — full control over messages and model. */
608
- async oracle(req) {
609
- return this.request("POST", "/v1/oracle/complete", {
620
+ /** Raw Supervisor completion with either Supafone-managed or BYOK inference. */
621
+ async completeWithSupervisor(req) {
622
+ return this.request("POST", "/v1/supervisor/complete", {
610
623
  messages: req.messages,
611
- model: req.model ?? "supafone-labs-oracle",
624
+ model: req.model ?? "supafone-supervisor",
612
625
  max_tokens: req.maxTokens ?? 256,
613
626
  ...(req.temperature !== undefined ? { temperature: req.temperature } : {}),
614
627
  });
615
628
  }
629
+ /** @deprecated Use completeWithSupervisor(). */
630
+ async oracle(req) {
631
+ return this.completeWithSupervisor(req);
632
+ }
616
633
  /**
617
634
  * The one-liner: hand it the running transcript, get back a silent directive
618
635
  * (empty string when the agent is doing fine).
619
636
  */
620
637
  async whisper(transcript, opts = {}) {
621
638
  const system = opts.guardrails ? `${COACH_SYSTEM}\n\nOperator rules:\n${opts.guardrails}` : COACH_SYSTEM;
622
- const out = await this.oracle({
639
+ const out = await this.completeWithSupervisor({
623
640
  model: opts.model,
624
641
  maxTokens: opts.maxTokens ?? 120,
625
642
  ...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}),
@@ -645,7 +662,7 @@ class SupafoneLabs {
645
662
  directiveContractPrompt(contract),
646
663
  operatorRules.length ? `Operator rules:\n${operatorRules.join("\n")}` : "",
647
664
  ].filter(Boolean).join("\n\n");
648
- const out = await this.oracle({
665
+ const out = await this.completeWithSupervisor({
649
666
  model: opts.model,
650
667
  maxTokens: opts.maxTokens ?? 320,
651
668
  ...(opts.temperature !== undefined ? { temperature: opts.temperature } : {}),
@@ -728,7 +745,7 @@ class SupafoneLabs {
728
745
  balance() {
729
746
  return this.request("GET", "/v1/billing/balance");
730
747
  }
731
- /** Today's usage against your plan caps (oracle/tts/stt/…). */
748
+ /** Today's usage across Supervisor, TTS, STT, and managed runtime meters. */
732
749
  usage() {
733
750
  return this.request("GET", "/v1/usage");
734
751
  }
@@ -795,7 +812,7 @@ class SupafoneLabs {
795
812
  *
796
813
  * With `postCallAnalysis: true` on the client and a transcript (or
797
814
  * messages) present, the call is automatically classified first: the
798
- * oracle labels it against the agent's objective (achieved/missed,
815
+ * Supervisor labels it against the agent's objective (achieved/missed,
799
816
  * per-criterion verdicts, failure reasons) and files the enriched report
800
817
  * server-side. The generated labels come back on `analysis`. Analysis is
801
818
  * best-effort — on any failure the plain zero-billed report still lands.
@@ -828,7 +845,7 @@ class SupafoneLabs {
828
845
  * objective and get labels back — achieved/missed, per-criterion verdicts,
829
846
  * failure reasons, and the blended objective value. Files an enriched call
830
847
  * report server-side (feeding optimizer.improve() and objective stats).
831
- * Billed one oracle call.
848
+ * Billed one Supervisor inference.
832
849
  */
833
850
  classifyCall(input) {
834
851
  return this.request("POST", "/v1/calls/classify", compact({
@@ -840,7 +857,7 @@ class SupafoneLabs {
840
857
  nudges: input.nudges,
841
858
  }));
842
859
  }
843
- /** Available oracle model ids (live vendor catalog). */
860
+ /** Available managed and BYOK Supervisor model ids. */
844
861
  async models() {
845
862
  const d = await this.request("GET", "/v1/models");
846
863
  return d.models.map((m) => (typeof m === "string" ? m : m.id));
@@ -1145,6 +1162,10 @@ class LabsBillingNamespace {
1145
1162
  }
1146
1163
  /** Start hosted Stripe Checkout. MCP callers should render checkout_url as a link. */
1147
1164
  checkout(input = {}) {
1165
+ const sku = input.sku ?? input.packageSku ?? input.package_sku;
1166
+ if (sku) {
1167
+ return this.sm.request("POST", "/v1/billing/checkout", { sku });
1168
+ }
1148
1169
  return this.sm.request("POST", "/v1/billing/checkout", compact({
1149
1170
  kind: input.kind ?? "plan",
1150
1171
  plan_key: input.plan_key ?? input.planKey,
@@ -1155,6 +1176,12 @@ class LabsBillingNamespace {
1155
1176
  cancel_url: input.cancel_url ?? input.cancelUrl,
1156
1177
  }));
1157
1178
  }
1179
+ /** Start Stripe Checkout for one prepaid managed-minute package. */
1180
+ topUp(sku = "sf_voice_minutes_400_v1") {
1181
+ if (!sku.trim())
1182
+ return Promise.reject(new SupafoneLabsError("sku is required"));
1183
+ return this.checkout({ sku });
1184
+ }
1158
1185
  status(checkoutSessionId) {
1159
1186
  if (!checkoutSessionId?.trim())
1160
1187
  throw new SupafoneLabsError("checkoutSessionId is required");
@@ -1644,6 +1671,37 @@ class LabsPhoneNumbersNamespace {
1644
1671
  const suffix = q.toString() ? `?${q}` : "";
1645
1672
  return this.sm.requestSupafoneApi("GET", `/api/v1/labs/phone-numbers${suffix}`);
1646
1673
  }
1674
+ /**
1675
+ * Read the explicitly enrolled shared developer-number pool. This inventory
1676
+ * never infers customer or merely-unassigned numbers into the response.
1677
+ */
1678
+ pool() {
1679
+ return this.sm.requestSupafoneApi("GET", "/api/v1/labs/phone-numbers/pool");
1680
+ }
1681
+ /**
1682
+ * Connect to realtime pool state using a short-lived pool-only token. The
1683
+ * Supafone API key is never placed in the WebSocket URL.
1684
+ */
1685
+ async connectPool(opts = {}) {
1686
+ const snapshot = await this.pool();
1687
+ const stream = snapshot.stream;
1688
+ if (!stream?.url || !stream.token) {
1689
+ throw new SupafoneLabsError("Phone-pool response did not include stream metadata");
1690
+ }
1691
+ const WS = opts.WebSocketImpl ?? globalThis.WebSocket;
1692
+ if (!WS) {
1693
+ throw new SupafoneLabsError("No WebSocket available - pass opts.WebSocketImpl (for example, the ws package)");
1694
+ }
1695
+ const url = new URL(stream.url, `${this.sm.supafoneApiBaseUrl}/`);
1696
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
1697
+ url.searchParams.set("token", stream.token);
1698
+ const socket = new WS(url.toString(), stream.protocol || "developer_phone_pool_v1");
1699
+ await new Promise((resolve, reject) => {
1700
+ socket.addEventListener("open", () => resolve(), { once: true });
1701
+ socket.addEventListener("error", () => reject(new SupafoneLabsError("Could not connect to the phone-pool stream")), { once: true });
1702
+ });
1703
+ return socket;
1704
+ }
1647
1705
  /** Search Supafone-managed inventory. This uses Supafone's master telephony account. */
1648
1706
  search(opts = {}) {
1649
1707
  return this.sm.requestSupafoneApi("POST", "/api/v1/labs/phone-numbers/search", phoneNumberSearchPayload(opts));
@@ -1702,21 +1760,31 @@ class LabsPhoneNumbersNamespace {
1702
1760
  * it to the supplied agent. This is the zero-Twilio-account happy path.
1703
1761
  */
1704
1762
  async buyAndAssign(input) {
1763
+ const strategy = input.number_strategy ?? input.numberStrategy ?? (input.premium ? "premium" : "default_pool");
1705
1764
  let phoneNumber = input.phoneNumber ?? input.phone_number ?? "";
1706
1765
  if (!phoneNumber) {
1707
- const found = await this.search({
1708
- ...(input.search ?? {}),
1709
- agencyId: input.agencyId ?? input.agency_id ?? input.search?.agencyId,
1710
- limit: input.search?.limit ?? 1,
1711
- });
1712
- phoneNumber = found.numbers[0]?.phone_number ?? "";
1766
+ if (strategy === "default_pool") {
1767
+ const found = await this.pool();
1768
+ const areaCode = String(input.search?.areaCode ?? input.search?.area_code ?? "");
1769
+ phoneNumber = found.numbers.find((number) => number.available && (!areaCode || number.phone_number.startsWith(`+1${areaCode}`)))?.phone_number ?? "";
1770
+ }
1771
+ else {
1772
+ const found = await this.search({
1773
+ ...(input.search ?? {}),
1774
+ agencyId: input.agencyId ?? input.agency_id ?? input.search?.agencyId,
1775
+ limit: input.search?.limit ?? 1,
1776
+ });
1777
+ phoneNumber = found.numbers[0]?.phone_number ?? "";
1778
+ }
1713
1779
  if (!phoneNumber) {
1714
- throw new SupafoneLabsError("No Supafone-managed phone numbers matched the search");
1780
+ const source = strategy === "default_pool" ? "shared developer" : "Supafone-managed";
1781
+ throw new SupafoneLabsError(`No ${source} phone numbers matched the request`);
1715
1782
  }
1716
1783
  }
1717
1784
  return this.buy({
1718
1785
  ...input,
1719
1786
  phoneNumber,
1787
+ numberStrategy: strategy,
1720
1788
  telephony: input.telephony ?? { mode: "supafone_managed", provider: "supafone" },
1721
1789
  });
1722
1790
  }
@@ -1974,6 +2042,7 @@ function campaignSettingsPayload(settings, mode) {
1974
2042
  }
1975
2043
  function labsAgentPayload(input) {
1976
2044
  const fixedLanguage = input.preferred_language ?? input.preferredLanguage ?? input.language;
2045
+ const supervisor = input.supervisor;
1977
2046
  return compact({
1978
2047
  agency_id: input.agency_id ?? input.agencyId,
1979
2048
  agent_key: input.agent_key ?? input.agentKey,
@@ -2014,9 +2083,15 @@ function labsAgentPayload(input) {
2014
2083
  artifacts: input.artifacts ? artifactsPayload(input.artifacts) : undefined,
2015
2084
  compliance: input.compliance,
2016
2085
  tools: input.tools ? toolsPayload(input.tools) : undefined,
2086
+ email: input.email ? emailPayload(input.email) : undefined,
2017
2087
  labs: input.labs ? labsPayload(input.labs) : undefined,
2018
2088
  ultravox: input.ultravox ? ultravoxPayload(input.ultravox) : undefined,
2019
- voice_watcher: input.supervisor ?? input.voice_watcher ?? input.voiceWatcher,
2089
+ supervisor: supervisor && typeof supervisor === "object" ? supervisorPayload(supervisor) : undefined,
2090
+ voice_watcher: typeof supervisor === "boolean"
2091
+ ? supervisor
2092
+ : supervisor && typeof supervisor === "object"
2093
+ ? supervisor.enabled ?? true
2094
+ : input.voice_watcher ?? input.voiceWatcher,
2020
2095
  voice_watcher_model: input.voice_watcher_model ?? input.voiceWatcherModel,
2021
2096
  metadata: labsAgentMetadataPayload(input),
2022
2097
  });
@@ -2143,8 +2218,10 @@ function callStagesPayload(input) {
2143
2218
  return explicit.map(callStagePayload);
2144
2219
  if (explicit === false || auto === false)
2145
2220
  return false;
2146
- if (explicit === "oracle" || explicit === "template" || explicit === "off")
2221
+ if (explicit === "managed" || explicit === "template" || explicit === "off")
2147
2222
  return explicit;
2223
+ if (explicit === "oracle")
2224
+ return "managed";
2148
2225
  // Omitted means the private Supafone API generates and compiles the plan.
2149
2226
  return undefined;
2150
2227
  }
@@ -2375,9 +2452,20 @@ function toolsPayload(input) {
2375
2452
  existing_client_lookup: input.existing_client_lookup ?? input.existingClientLookup,
2376
2453
  voicemail: input.voicemail,
2377
2454
  emergency_escalation: input.emergency_escalation ?? input.emergencyEscalation,
2455
+ ivr_navigation: input.ivr_navigation ?? input.ivrNavigation,
2378
2456
  custom_tools: input.custom_tools ?? input.customTools,
2379
2457
  });
2380
2458
  }
2459
+ function emailPayload(input) {
2460
+ return compact({
2461
+ enabled: input.enabled,
2462
+ from_email: input.from_email ?? input.fromEmail,
2463
+ smtp_host: input.smtp_host ?? input.smtpHost,
2464
+ smtp_port: input.smtp_port ?? input.smtpPort,
2465
+ smtp_user: input.smtp_user ?? input.smtpUser,
2466
+ smtp_pass: input.smtp_pass ?? input.smtp_password ?? input.smtpPassword,
2467
+ });
2468
+ }
2381
2469
  function recordingPayload(input) {
2382
2470
  return compact({
2383
2471
  enabled: input.enabled,
@@ -2415,9 +2503,19 @@ function artifactsPayload(input) {
2415
2503
  });
2416
2504
  }
2417
2505
  function labsPayload(input) {
2506
+ const nestedSupervisor = input.supervisor && typeof input.supervisor === "object"
2507
+ ? input.supervisor
2508
+ : input.provider
2509
+ ? input
2510
+ : undefined;
2418
2511
  return compact({
2419
2512
  enabled: input.enabled,
2420
- voice_watcher: input.supervisor ?? input.voice_watcher ?? input.voiceWatcher,
2513
+ supervisor: nestedSupervisor ? supervisorPayload(nestedSupervisor) : undefined,
2514
+ voice_watcher: typeof input.supervisor === "boolean"
2515
+ ? input.supervisor
2516
+ : nestedSupervisor
2517
+ ? nestedSupervisor.enabled ?? true
2518
+ : input.voice_watcher ?? input.voiceWatcher,
2421
2519
  api_key: input.api_key ?? input.apiKey,
2422
2520
  model: input.model,
2423
2521
  mode: input.mode,
@@ -2429,6 +2527,16 @@ function labsPayload(input) {
2429
2527
  label: input.label,
2430
2528
  });
2431
2529
  }
2530
+ function supervisorPayload(input) {
2531
+ return compact({
2532
+ enabled: input.enabled,
2533
+ mode: input.mode,
2534
+ provider: input.provider,
2535
+ model: input.model,
2536
+ api_key: input.api_key ?? input.apiKey,
2537
+ api_key_configured: input.api_key_configured ?? input.apiKeyConfigured,
2538
+ });
2539
+ }
2432
2540
  function ultravoxPayload(input) {
2433
2541
  return compact({
2434
2542
  model: input.model,