@pyai/sdk 0.9.3 → 0.12.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 (49) hide show
  1. package/CLI.schema.json +11 -0
  2. package/README.md +114 -3
  3. package/dist/cli-omni-template.d.ts +2 -2
  4. package/dist/cli-omni-template.js +2 -2
  5. package/dist/cli.js +39 -4
  6. package/dist/dialer/appServer.d.ts +21 -0
  7. package/dist/dialer/appServer.js +195 -0
  8. package/dist/dialer/dialerElement.d.ts +23 -0
  9. package/dist/dialer/dialerElement.js +116 -0
  10. package/dist/dialer/dialingQueue.d.ts +36 -0
  11. package/dist/dialer/dialingQueue.js +110 -0
  12. package/dist/dialer/index.d.ts +6 -0
  13. package/dist/dialer/index.js +6 -0
  14. package/dist/dialer/indexedDbJournal.d.ts +11 -0
  15. package/dist/dialer/indexedDbJournal.js +62 -0
  16. package/dist/dialer/managedDialer.d.ts +65 -0
  17. package/dist/dialer/managedDialer.js +219 -0
  18. package/dist/dialer/meetingRecorder.d.ts +83 -0
  19. package/dist/dialer/meetingRecorder.js +261 -0
  20. package/dist/dialer/meetingRecorderElement.d.ts +20 -0
  21. package/dist/dialer/meetingRecorderElement.js +138 -0
  22. package/dist/dialer/meetingRecordingStore.d.ts +17 -0
  23. package/dist/dialer/meetingRecordingStore.js +129 -0
  24. package/dist/dialer/serverBroker.d.ts +3 -0
  25. package/dist/dialer/serverBroker.js +23 -0
  26. package/dist/dialer/voiceSdkAudio.d.ts +33 -0
  27. package/dist/dialer/voiceSdkAudio.js +66 -0
  28. package/dist/index.d.ts +136 -6
  29. package/dist/index.js +36 -3
  30. package/package.json +24 -1
  31. package/src/cli-omni-template.ts +2 -2
  32. package/src/cli.ts +25 -4
  33. package/src/dialer/app/app.css +1 -0
  34. package/src/dialer/app/app.html +7 -0
  35. package/src/dialer/app/app.js +88 -0
  36. package/src/dialer/app/icon.svg +1 -0
  37. package/src/dialer/app/manifest.webmanifest +1 -0
  38. package/src/dialer/appServer.ts +151 -0
  39. package/src/dialer/dialerElement.ts +87 -0
  40. package/src/dialer/dialingQueue.ts +93 -0
  41. package/src/dialer/index.ts +6 -0
  42. package/src/dialer/indexedDbJournal.ts +56 -0
  43. package/src/dialer/managedDialer.ts +190 -0
  44. package/src/dialer/meetingRecorder.ts +220 -0
  45. package/src/dialer/meetingRecorderElement.ts +99 -0
  46. package/src/dialer/meetingRecordingStore.ts +99 -0
  47. package/src/dialer/serverBroker.ts +23 -0
  48. package/src/dialer/voiceSdkAudio.ts +61 -0
  49. package/src/index.ts +121 -5
@@ -0,0 +1,61 @@
1
+ import type { TelephonyBrowserSession } from "../index.ts";
2
+ import type { DialerAudio } from "./managedDialer.ts";
3
+
4
+ interface VoiceCall {
5
+ on(event: string, callback: (...args: unknown[]) => void): void;
6
+ removeAllListeners(): void;
7
+ disconnect(): void;
8
+ mute(value: boolean): void;
9
+ sendDigits(digits: string): void;
10
+ status(): string;
11
+ }
12
+ interface VoiceDevice {
13
+ connect(options: { params: Record<string, string> }): Promise<VoiceCall>;
14
+ on(event: string, callback: (...args: unknown[]) => void): void;
15
+ removeAllListeners(): void;
16
+ destroy(): void;
17
+ }
18
+ /** Inject the supported Voice SDK's Device constructor. Lazy-load it only when
19
+ * calling starts; the dialer shell itself has no framework or media dependency. */
20
+ export class VoiceSdkAudio implements DialerAudio {
21
+ private device: VoiceDevice | null = null;
22
+ private call: VoiceCall | null = null;
23
+ private generation = 0;
24
+ private notify: ((state: "ringing" | "active" | "disconnected") => void) | null = null;
25
+ private readonly createDevice: (token: string) => VoiceDevice;
26
+ constructor(createDevice: (token: string) => VoiceDevice) { this.createDevice = createDevice; }
27
+ async connect(session: TelephonyBrowserSession, event: (state: "ringing" | "active" | "disconnected") => void) {
28
+ if (!session.access_token) throw new Error("No unconsumed calling capability.");
29
+ this.disconnect(); const generation = this.generation;
30
+ const device = this.createDevice(session.access_token); this.device = device; this.notify = event;
31
+ const emit = (state: "ringing" | "active" | "disconnected") => { if (generation === this.generation) this.notify?.(state); };
32
+ device.on("error", () => emit("disconnected"));
33
+ try {
34
+ const call = await device.connect({ params: { ...session.connect_params } });
35
+ if (generation !== this.generation) {
36
+ try { call.removeAllListeners(); } catch { /* Continue releasing stale audio. */ }
37
+ try { call.disconnect(); } catch { /* The owning device is already destroyed. */ }
38
+ return;
39
+ }
40
+ this.call = call;
41
+ call.on("ringing", () => emit("ringing"));
42
+ call.on("accept", () => emit("active"));
43
+ for (const name of ["disconnect", "cancel", "reject", "error"]) call.on(name, () => emit("disconnected"));
44
+ emit(call.status() === "open" ? "active" : "ringing");
45
+ } catch (error) {
46
+ if (generation === this.generation) this.disconnect();
47
+ throw error;
48
+ }
49
+ }
50
+ disconnect() {
51
+ this.generation++; this.notify = null;
52
+ const call = this.call, device = this.device; this.call = null; this.device = null;
53
+ // Local SDK failures must not prevent the controller's durable remote stop.
54
+ for (const close of [() => call?.removeAllListeners(), () => call?.disconnect(),
55
+ () => device?.removeAllListeners(), () => device?.destroy()]) {
56
+ try { close(); } catch { /* All owned resources still get a cleanup attempt. */ }
57
+ }
58
+ }
59
+ mute(value: boolean) { if (!this.call) throw new Error("No active audio."); this.call.mute(value); }
60
+ sendDigits(digits: string) { if (!this.call) throw new Error("No active audio."); this.call.sendDigits(digits); }
61
+ }
package/src/index.ts CHANGED
@@ -233,6 +233,7 @@ export type OmniStagedLanguage = "ht" | "zh-CN" | "ar-MSA" | "ar-Gulf";
233
233
 
234
234
  export interface AgentConfig {
235
235
  name?: string;
236
+ /** Saved instructions: 8,000 Unicode characters by default; 32,000 on commitment plans. */
236
237
  persona_system_prompt?: string | null;
237
238
  /** False restricts retrieval to explicit agent bindings, including none. Default true. */
238
239
  inherit_org_knowledgebases?: boolean;
@@ -250,6 +251,7 @@ export interface AgentConfig {
250
251
  export interface Agent {
251
252
  object: "agent";
252
253
  agent_id: string;
254
+ prompt_limit_characters?: 8000 | 32000;
253
255
  name: string;
254
256
  vocabulary: string[];
255
257
  inherit_org_knowledgebases?: boolean;
@@ -1476,6 +1478,70 @@ export interface TelephonyCapabilities {
1476
1478
  sms?: boolean;
1477
1479
  }
1478
1480
 
1481
+ export type TelephonyCallStatus = "queued" | "dispatching" | "ringing" | "in_progress" | "completed" | "failed" | "canceled" | "dispatch_unknown";
1482
+ export interface TelephonyCall {
1483
+ object: "telephony.call";
1484
+ id: string;
1485
+ project_id: string;
1486
+ direction: "inbound" | "outbound";
1487
+ status: TelephonyCallStatus;
1488
+ version: number;
1489
+ from: string;
1490
+ to: string;
1491
+ dialing_mode?: "parallel";
1492
+ destinations?: string[];
1493
+ connected_to?: string | null;
1494
+ lines?: Array<{ id: string; to: string; status: "pending" | "queued" | "ringing" | "answered" | "completed" | "failed" | "canceled"; duration_seconds: number | null }>;
1495
+ from_number_id: string | null;
1496
+ agent_id: string | null;
1497
+ call_type?: "human";
1498
+ user_id?: string;
1499
+ client_reference: string | null;
1500
+ stop_requested: boolean;
1501
+ created_at: number;
1502
+ updated_at: number;
1503
+ answered_at: number | null;
1504
+ ended_at: number | null;
1505
+ [key: string]: unknown;
1506
+ }
1507
+ export type TelephonyUseCase = "transactional" | "support" | "sales" | "collections" | "survey";
1508
+ export interface TelephonyCallCreate {
1509
+ from_number_id: string;
1510
+ to: string;
1511
+ agent_id: string;
1512
+ use_case: TelephonyUseCase;
1513
+ routing_profile?: "quality_first" | "balanced" | "cost_optimized";
1514
+ client_reference?: string;
1515
+ agent_release_id?: string;
1516
+ max_duration_seconds?: number;
1517
+ stop_requested?: boolean;
1518
+ }
1519
+ export type TelephonyBrowserSessionCreate = {
1520
+ from_number_id: string;
1521
+ /** Resolve from your authenticated server session, never from untrusted form data. */
1522
+ user_id: string;
1523
+ client_reference: string;
1524
+ recording: boolean;
1525
+ use_case?: TelephonyUseCase;
1526
+ max_duration_seconds?: number;
1527
+ } & ({ to: string; destinations?: never } | { to?: never; destinations: string[] });
1528
+ export interface TelephonyBrowserSession {
1529
+ object: "telephony.browser_session";
1530
+ id: string;
1531
+ call_id: string;
1532
+ transport: "voice-sdk";
1533
+ /** Single-call capability. Keep in memory; never log or persist this token. */
1534
+ access_token: string | null;
1535
+ expires_at: number;
1536
+ connect_params: { session_id: string };
1537
+ call: TelephonyCall;
1538
+ }
1539
+
1540
+ function telephonyIdempotencyHeader(key: string): Record<string, string> {
1541
+ if (!key || key.length > 255 || /[\r\n]/.test(key)) throw new Error("A valid Idempotency-Key is required; persist it before creating a call.");
1542
+ return { "Idempotency-Key": key };
1543
+ }
1544
+
1479
1545
  export interface TelephonyAvailableNumber {
1480
1546
  object?: "telephony.available_number";
1481
1547
  /** E.164. */
@@ -1505,6 +1571,27 @@ export interface TelephonyNumber {
1505
1571
  released_at?: number | null;
1506
1572
  }
1507
1573
 
1574
+ export interface TelephonyNumberOrder {
1575
+ object: "telephony.number_order";
1576
+ id: string;
1577
+ status: "pending" | "unknown" | "completed" | "failed" | "released";
1578
+ phone_number: string;
1579
+ country: "AU";
1580
+ project_id: string;
1581
+ retry_after_seconds: number;
1582
+ message: string;
1583
+ failure_code?: string;
1584
+ number?: TelephonyNumber;
1585
+ }
1586
+ export interface TelephonyNumberPurchase {
1587
+ phone_number: string;
1588
+ agent_id?: string | null;
1589
+ provisioning_mode?: "automated" | "adopt_preowned";
1590
+ project_id?: string;
1591
+ compliance_case_id?: string;
1592
+ max_monthly_cost_cents?: number;
1593
+ }
1594
+
1508
1595
  /* ------------------------------------------------------------------------- *
1509
1596
  * Trace (compliance)
1510
1597
  * ------------------------------------------------------------------------- */
@@ -2277,12 +2364,36 @@ export class PyAI {
2277
2364
  // --- telephony (managed numbers) ---------------------------------------
2278
2365
 
2279
2366
  telephony = {
2367
+ /** Server-side methods. Keep the PyAI key in your backend. */
2368
+ calls: {
2369
+ create: (input: TelephonyCallCreate, options: { idempotencyKey: string }): Promise<TelephonyCall> =>
2370
+ this.postJson("/v1/telephony/calls", input, telephonyIdempotencyHeader(options.idempotencyKey)),
2371
+ get: (id: string): Promise<TelephonyCall> => this.getJson(`/v1/telephony/calls/${encodeURIComponent(id)}`),
2372
+ list: (input: { limit?: number; cursor?: string; direction?: "inbound" | "outbound"; status?: TelephonyCallStatus } = {}): Promise<ListResponse<TelephonyCall> & { next_cursor: string | null }> => {
2373
+ const query = new URLSearchParams();
2374
+ for (const [key, value] of Object.entries(input)) if (value !== undefined) query.set(key, String(value));
2375
+ return this.getJson(`/v1/telephony/calls${query.size ? `?${query}` : ""}`);
2376
+ },
2377
+ /** Durable stop intent; use get() until a terminal status is confirmed. */
2378
+ hangup: (id: string): Promise<TelephonyCall> => this.postJson(`/v1/telephony/calls/${encodeURIComponent(id)}/hangup`, {}),
2379
+ decideAdmission: (id: string, input: { decision: "accept" | "reject"; client_reference: string; max_duration_seconds: number }): Promise<TelephonyCall> =>
2380
+ this.postJson(`/v1/telephony/calls/${encodeURIComponent(id)}/admission`, input),
2381
+ recording: (id: string, options: { range?: string } = {}): Promise<Response> =>
2382
+ this.request(`/v1/telephony/calls/${encodeURIComponent(id)}/recording`, { headers: this.authHeaders(options.range ? { Range: options.range } : {}) }),
2383
+ deleteRecording: async (id: string): Promise<void> => { await this.deleteReq(`/v1/telephony/calls/${encodeURIComponent(id)}/recording`); },
2384
+ },
2385
+ browserSessions: {
2386
+ /** Persist input and key on your server; retries recover the original session. */
2387
+ create: (input: TelephonyBrowserSessionCreate, options: { idempotencyKey: string }): Promise<TelephonyBrowserSession> =>
2388
+ this.postJson("/v1/telephony/browser-sessions", input, telephonyIdempotencyHeader(options.idempotencyKey)),
2389
+ },
2280
2390
  numbers: {
2281
- /** Search the carrier's available US local numbers. */
2391
+ /** Search managed inventory; country availability and ordering may be gated. */
2282
2392
  available: (
2283
- params: { areaCode?: string; contains?: string; limit?: number } = {},
2393
+ params: { country?: "US" | "CA" | "IN" | "AU"; areaCode?: string; contains?: string; limit?: number } = {},
2284
2394
  ): Promise<ListResponse<TelephonyAvailableNumber>> => {
2285
2395
  const q = new URLSearchParams();
2396
+ if (params.country) q.set("country", params.country);
2286
2397
  if (params.areaCode) q.set("area_code", params.areaCode);
2287
2398
  if (params.contains) q.set("contains", params.contains);
2288
2399
  if (params.limit !== undefined) q.set("limit", String(params.limit));
@@ -2294,9 +2405,9 @@ export class PyAI {
2294
2405
  const qs = params.includeReleased ? "?include_released=true" : "";
2295
2406
  return this.getJson(`/v1/telephony/numbers${qs}`);
2296
2407
  },
2297
- /** Provision (buy) a specific available number, optionally bound to an agent. */
2298
- buy: (params: { phone_number: string; agent_id?: string | null }): Promise<TelephonyNumber> =>
2299
- this.postJson("/v1/telephony/numbers", params),
2408
+ /** Persist selection and key first. Pending/unknown orders must be recovered, never repurchased. */
2409
+ buy: (params: TelephonyNumberPurchase, options: { idempotencyKey: string }): Promise<TelephonyNumber | TelephonyNumberOrder> =>
2410
+ this.postJson("/v1/telephony/numbers", params, telephonyIdempotencyHeader(options.idempotencyKey)),
2300
2411
  /** Route a number to an agent (`agentId: null` to unassign). */
2301
2412
  assign: (id: string, agentId: string | null): Promise<TelephonyNumber> =>
2302
2413
  this.postJson(`/v1/telephony/numbers/${encodeURIComponent(id)}/assign`, { agent_id: agentId }),
@@ -2306,6 +2417,11 @@ export class PyAI {
2306
2417
  return (await res.json()) as TelephonyNumber;
2307
2418
  },
2308
2419
  },
2420
+ numberOrders: {
2421
+ get: (id: string): Promise<TelephonyNumberOrder> => this.getJson(`/v1/telephony/number-orders/${encodeURIComponent(id)}`),
2422
+ /** Read/reconcile the original order; this never purchases another number. */
2423
+ reconcile: (id: string): Promise<TelephonyNumberOrder> => this.postJson(`/v1/telephony/number-orders/${encodeURIComponent(id)}/reconcile`, {}),
2424
+ },
2309
2425
  };
2310
2426
 
2311
2427
  /** Customer-owned two-human calls. Server keys only; history is project scoped. */