@kognitivedev/backend-cloud 0.2.29

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 (43) hide show
  1. package/.turbo/turbo-build.log +2 -0
  2. package/.turbo/turbo-test.log +14 -0
  3. package/CHANGELOG.md +11 -0
  4. package/README.md +88 -0
  5. package/dist/cloud-voice-parameters.d.ts +11 -0
  6. package/dist/cloud-voice-parameters.js +219 -0
  7. package/dist/cloud-voice-prompt-service.d.ts +24 -0
  8. package/dist/cloud-voice-prompt-service.js +382 -0
  9. package/dist/cloud-voice-runtime-service.d.ts +73 -0
  10. package/dist/cloud-voice-runtime-service.js +443 -0
  11. package/dist/cloud-voice.d.ts +36 -0
  12. package/dist/cloud-voice.js +683 -0
  13. package/dist/index.d.ts +10 -0
  14. package/dist/index.js +26 -0
  15. package/dist/phone-control.d.ts +50 -0
  16. package/dist/phone-control.js +97 -0
  17. package/dist/phone-runtime/audio-playout-tracker.d.ts +51 -0
  18. package/dist/phone-runtime/audio-playout-tracker.js +93 -0
  19. package/dist/phone-runtime/openai-twilio-realtime.d.ts +95 -0
  20. package/dist/phone-runtime/openai-twilio-realtime.js +1074 -0
  21. package/dist/tools.d.ts +2 -0
  22. package/dist/tools.js +216 -0
  23. package/dist/types.d.ts +468 -0
  24. package/dist/types.js +2 -0
  25. package/dist/utils.d.ts +3 -0
  26. package/dist/utils.js +14 -0
  27. package/package.json +47 -0
  28. package/src/__tests__/audio-playout-tracker.test.ts +46 -0
  29. package/src/__tests__/cloud-voice.test.ts +1006 -0
  30. package/src/__tests__/openai-twilio-realtime.test.ts +1193 -0
  31. package/src/__tests__/phone-control.test.ts +105 -0
  32. package/src/cloud-voice-parameters.ts +236 -0
  33. package/src/cloud-voice-prompt-service.ts +493 -0
  34. package/src/cloud-voice-runtime-service.ts +465 -0
  35. package/src/cloud-voice.ts +831 -0
  36. package/src/index.ts +10 -0
  37. package/src/phone-control.ts +156 -0
  38. package/src/phone-runtime/audio-playout-tracker.ts +132 -0
  39. package/src/phone-runtime/openai-twilio-realtime.ts +1250 -0
  40. package/src/tools.ts +227 -0
  41. package/src/types.ts +529 -0
  42. package/src/utils.ts +11 -0
  43. package/tsconfig.json +13 -0
package/src/types.ts ADDED
@@ -0,0 +1,529 @@
1
+ import type {
2
+ TelephonyDestination,
3
+ TelephonyProvider,
4
+ TelephonyTransferCallResult,
5
+ TelephonyTransferMode,
6
+ TelephonyTransferPolicy,
7
+ } from "@kognitivedev/telephony";
8
+
9
+ export type CloudVoiceChannel = "web" | "iframe" | "script" | "phone" | "sip" | "outbound";
10
+
11
+ export type CloudVoiceProvider = "openai-realtime" | "gemini-live" | "kognitive-voice" | "xai-realtime";
12
+
13
+ export type CloudVoiceTransport = "webrtc" | "websocket";
14
+ export type CloudVoicePipelineTransportProvider = "kognitive-websocket" | "daily";
15
+
16
+ export type CloudVoiceToolType =
17
+ | "cloud_flow"
18
+ | "web_search"
19
+ | "knowledge_base"
20
+ | "platform_action"
21
+ | "calendar_action"
22
+ | "appointment_action"
23
+ | "cloud_voice_agent"
24
+ | "external_webhook"
25
+ | "sip_transfer"
26
+ | "code_defined_tool";
27
+
28
+ export interface CloudVoiceToolBinding {
29
+ id: string;
30
+ type: CloudVoiceToolType;
31
+ name: string;
32
+ description?: string;
33
+ inputSchema?: Record<string, unknown>;
34
+ outputSchema?: Record<string, unknown>;
35
+ parameterBindings?: Record<string, { parameter: string }>;
36
+ ui?: {
37
+ type?: "card" | "status" | "table" | "timeline" | "custom";
38
+ title?: string;
39
+ description?: string;
40
+ renderer?: string;
41
+ config?: Record<string, unknown>;
42
+ };
43
+ config: Record<string, unknown>;
44
+ }
45
+
46
+ export type CloudVoiceTelephonyProvider = Exclude<TelephonyProvider, "twilio"> | string;
47
+
48
+ export type CloudVoiceTelephonyDestination = TelephonyDestination;
49
+
50
+ export type CloudVoiceTransferMode = TelephonyTransferMode;
51
+
52
+ export type CloudVoiceTransferPolicy = TelephonyTransferPolicy & {
53
+ fallbackDestinationId?: string;
54
+ };
55
+
56
+ export interface CloudVoiceTransferDestination {
57
+ id: string;
58
+ name: string;
59
+ provider?: CloudVoiceTelephonyProvider;
60
+ destination: CloudVoiceTelephonyDestination;
61
+ policy?: CloudVoiceTransferPolicy;
62
+ metadata?: Record<string, unknown>;
63
+ }
64
+
65
+ export interface CloudVoiceSipConfig {
66
+ enabled?: boolean;
67
+ defaultConnectionId?: string;
68
+ defaultProvider?: CloudVoiceTelephonyProvider;
69
+ inboundRouting?: {
70
+ mode: "agent" | "flow" | "queue";
71
+ agentId?: string;
72
+ flowId?: string;
73
+ queueId?: string;
74
+ };
75
+ transferPolicy?: CloudVoiceTransferPolicy;
76
+ }
77
+
78
+ export type CloudVoiceFlowNodeType = "initial" | "case" | "tool" | "transfer" | "end" | string;
79
+
80
+ export interface CloudVoiceFlowNodeOutput {
81
+ id: string;
82
+ label: string;
83
+ description?: string;
84
+ desc?: string;
85
+ condition?: string;
86
+ icon?: string;
87
+ metadata?: Record<string, unknown>;
88
+ }
89
+
90
+ export interface CloudVoiceSpeechConfig extends Record<string, unknown> {
91
+ language?: string;
92
+ accent?: string;
93
+ style?: string;
94
+ pace?: string;
95
+ emotion?: string;
96
+ }
97
+
98
+ export interface CloudVoiceNodeVoiceSettings extends Record<string, unknown> {
99
+ speed?: number;
100
+ pitch?: number;
101
+ backgroundNoise?: boolean;
102
+ fillerWords?: boolean;
103
+ }
104
+
105
+ export interface CloudVoiceNodeModelSettings extends Record<string, unknown> {
106
+ temperature?: number;
107
+ maxOutputTokens?: number;
108
+ sentimentGuardrails?: boolean;
109
+ recordTranscript?: boolean;
110
+ }
111
+
112
+ export interface CloudVoiceFlowNode {
113
+ id: string;
114
+ type: CloudVoiceFlowNodeType;
115
+ entryMode?: "incoming" | "outgoing" | "both" | "default" | string;
116
+ title?: string;
117
+ prompt?: string;
118
+ firstMessage?: string;
119
+ language?: string;
120
+ maxDuration?: string | number;
121
+ provider?: CloudVoiceProvider;
122
+ model?: string;
123
+ voice?: string;
124
+ transport?: CloudVoiceTransport;
125
+ providerOptions?: Record<string, unknown>;
126
+ transcription?: Record<string, unknown> | null;
127
+ turnDetection?: Record<string, unknown> | null;
128
+ inputNoiseReduction?: Record<string, unknown> | null;
129
+ humanization?: CloudVoiceHumanizationConfig;
130
+ speech?: CloudVoiceSpeechConfig;
131
+ phoneNumberIds?: string[];
132
+ voiceSettings?: CloudVoiceNodeVoiceSettings;
133
+ modelSettings?: CloudVoiceNodeModelSettings;
134
+ outputs?: CloudVoiceFlowNodeOutput[];
135
+ toolId?: string;
136
+ toolIds?: string[];
137
+ transferTarget?: string;
138
+ transferDestinationId?: string;
139
+ transferMode?: CloudVoiceTransferMode;
140
+ metadata?: Record<string, unknown>;
141
+ }
142
+
143
+ export interface CloudVoiceFlowEdge {
144
+ id?: string;
145
+ from: string;
146
+ fromPort?: string | null;
147
+ to: string;
148
+ }
149
+
150
+ export interface CloudVoiceFlowGraph {
151
+ version?: 1;
152
+ startNodeId?: string;
153
+ nodes: CloudVoiceFlowNode[];
154
+ edges: CloudVoiceFlowEdge[];
155
+ metadata?: Record<string, unknown>;
156
+ }
157
+
158
+ export interface CloudVoiceAgentMetadata extends Record<string, unknown> {
159
+ flowGraph?: CloudVoiceFlowGraph;
160
+ speech?: CloudVoiceSpeechConfig;
161
+ sip?: CloudVoiceSipConfig;
162
+ transferDestinations?: CloudVoiceTransferDestination[];
163
+ }
164
+
165
+ export interface CloudVoiceClientToolManifest {
166
+ id: string;
167
+ name?: string;
168
+ description?: string;
169
+ inputSchema?: Record<string, unknown>;
170
+ }
171
+
172
+ export interface CloudVoiceHumanizationConfig {
173
+ enabled?: boolean;
174
+ openingMode?: "auto" | "wait";
175
+ openingStyle?: "brief" | "warm" | "professional";
176
+ fillerStyle?: "off" | "light" | "natural";
177
+ backchannelFrequency?: "off" | "low" | "medium";
178
+ disfluency?: "off" | "rare";
179
+ toolLatencyFillerMs?: number;
180
+ conversationProfile?: CloudVoiceConversationProfile;
181
+ }
182
+
183
+ export interface CloudVoiceConversationProfile {
184
+ personality: "neutral" | "warm" | "expert" | "concierge";
185
+ tone: "casual" | "professional" | "empathetic" | "polished";
186
+ pacing: "concise" | "measured" | "deliberate" | "energetic";
187
+ unclearAudio: "ask_repeat" | "confirm_best_guess";
188
+ confirmation: "critical_fields" | "all_actions" | "minimal";
189
+ escalation: "when_blocked" | "on_request" | "never";
190
+ numberReadback: boolean;
191
+ }
192
+
193
+ export interface CloudVoiceRuntimeCapabilities {
194
+ canUpdateInstructionsLive: boolean;
195
+ supportsSessionResume: boolean;
196
+ supportsToolCalling: boolean;
197
+ supportsOutputAudioTranscripts: boolean;
198
+ supportsServerVadConfig: boolean;
199
+ supportsSemanticVad?: boolean;
200
+ supportsNativeAudioOptions?: boolean;
201
+ supportsAffectiveDialog?: boolean;
202
+ supportsProactiveAudio?: boolean;
203
+ supportsPipelineEotConfig?: boolean;
204
+ supportsCartesiaTtsControls?: boolean;
205
+ }
206
+
207
+ export interface CloudVoiceCartesiaTtsOptions extends Record<string, unknown> {
208
+ modelId?: string;
209
+ voice?: string;
210
+ speed?: number | "slow" | "normal" | "fast";
211
+ emotion?: string[];
212
+ pronunciationDictId?: string;
213
+ contextMode?: "continue" | "reset";
214
+ }
215
+
216
+ export interface CloudVoicePipelineTransportConfig {
217
+ type: CloudVoiceTransport;
218
+ provider?: CloudVoicePipelineTransportProvider;
219
+ }
220
+
221
+ export interface CloudVoicePipelineSTTConfig {
222
+ provider: string;
223
+ model: string;
224
+ language?: string;
225
+ }
226
+
227
+ export interface CloudVoicePipelineLLMConfig {
228
+ provider: string;
229
+ model: string;
230
+ }
231
+
232
+ export interface CloudVoicePipelineTTSConfig {
233
+ provider: string;
234
+ model: string;
235
+ voice?: string;
236
+ options?: CloudVoiceCartesiaTtsOptions;
237
+ }
238
+
239
+ export interface CloudVoicePipelineTurnConfig extends Record<string, unknown> {
240
+ interruptResponse?: boolean;
241
+ createResponse?: boolean;
242
+ prefixPaddingMs?: number;
243
+ silenceDurationMs?: number;
244
+ threshold?: number;
245
+ }
246
+
247
+ export interface CloudVoicePipelineConfig {
248
+ transport: CloudVoicePipelineTransportConfig;
249
+ stt: CloudVoicePipelineSTTConfig;
250
+ llm: CloudVoicePipelineLLMConfig;
251
+ tts: CloudVoicePipelineTTSConfig;
252
+ turn?: CloudVoicePipelineTurnConfig;
253
+ backgroundAudio?: { preset?: string };
254
+ }
255
+
256
+ export type CloudVoiceParameterType = "string" | "number" | "boolean" | "json" | "phone" | "email";
257
+ export type CloudVoiceParameterSource = "system" | "session" | "call" | "custom";
258
+ export type CloudVoiceParameterPreset =
259
+ | "user_id"
260
+ | "session_id"
261
+ | "agent_slug"
262
+ | "channel"
263
+ | "phone"
264
+ | "caller_phone"
265
+ | "from_phone"
266
+ | "to_phone"
267
+ | "provider"
268
+ | "provider_call_id"
269
+ | "call_direction"
270
+ | "phone_number_id"
271
+ | "embed_origin";
272
+ export type CloudVoiceParameterResolutionMode = "strict" | "permissive";
273
+ export type CloudVoiceParameterValueMap = Record<string, unknown>;
274
+
275
+ export interface CloudVoiceParameterDefinition {
276
+ key: string;
277
+ label: string;
278
+ type: CloudVoiceParameterType;
279
+ required?: boolean;
280
+ description?: string;
281
+ defaultValue?: unknown;
282
+ enum?: unknown[];
283
+ source?: CloudVoiceParameterSource;
284
+ preset?: CloudVoiceParameterPreset;
285
+ sensitive?: boolean;
286
+ jsonSchema?: Record<string, unknown>;
287
+ }
288
+
289
+ export interface CloudVoiceParameterResolutionResult {
290
+ definitions: CloudVoiceParameterDefinition[];
291
+ values: CloudVoiceParameterValueMap;
292
+ missingRequired: string[];
293
+ errors: string[];
294
+ sensitiveKeys: string[];
295
+ }
296
+
297
+ export interface CloudVoiceAgentConfig {
298
+ instructions: string;
299
+ provider: CloudVoiceProvider;
300
+ model: string;
301
+ voice: string;
302
+ transport: CloudVoiceTransport;
303
+ pipeline?: CloudVoicePipelineConfig;
304
+ memoryEnabled?: boolean;
305
+ providerOptions?: Record<string, unknown>;
306
+ transcription?: Record<string, unknown> | null;
307
+ turnDetection?: Record<string, unknown> | null;
308
+ inputNoiseReduction?: Record<string, unknown> | null;
309
+ tools: CloudVoiceToolBinding[];
310
+ knowledgeBases: Array<{ pipelineId: string; name?: string; topK?: number }>;
311
+ webSearch?: { enabled?: boolean; maxResults?: number; maxPages?: number };
312
+ widget: {
313
+ title: string;
314
+ subtitle?: string;
315
+ layout?: "expanded" | "compact";
316
+ accentColor?: string;
317
+ backgroundColor?: string;
318
+ surfaceColor?: string;
319
+ textColor?: string;
320
+ mutedColor?: string;
321
+ ctaLabel?: string;
322
+ launcher?: "button" | "inline";
323
+ position?: "bottom-right" | "bottom-left";
324
+ };
325
+ channels: {
326
+ web: boolean;
327
+ iframe: boolean;
328
+ script: boolean;
329
+ phone: boolean;
330
+ sip: boolean;
331
+ outbound: boolean;
332
+ };
333
+ humanization?: CloudVoiceHumanizationConfig;
334
+ cloudVoiceConfigVersion?: 2;
335
+ parameters?: CloudVoiceParameterDefinition[];
336
+ metadata?: CloudVoiceAgentMetadata;
337
+ }
338
+
339
+ export interface CloudVoiceFunctionToolManifest {
340
+ type: "function";
341
+ name: string;
342
+ description: string;
343
+ parameters: Record<string, unknown>;
344
+ }
345
+
346
+ export interface CompiledCloudVoiceFlowInstructions {
347
+ instructions: string;
348
+ compilerVersion: string;
349
+ graphSignature: string;
350
+ startNodeId: string | null;
351
+ entryMode?: "incoming" | "outgoing" | "both";
352
+ nodeCount: number;
353
+ edgeCount: number;
354
+ referencedToolIds: string[];
355
+ missingToolIds: string[];
356
+ toolManifest: CloudVoiceFunctionToolManifest[];
357
+ warnings: string[];
358
+ }
359
+
360
+ export interface PreparedCloudVoiceConfig {
361
+ name: string;
362
+ system: string;
363
+ runtime: {
364
+ provider: CloudVoiceProvider;
365
+ mode: "pipeline" | "realtime";
366
+ transport: CloudVoiceTransport;
367
+ model: string;
368
+ voice: string;
369
+ providerOptions?: Record<string, unknown>;
370
+ pipeline?: CloudVoicePipelineConfig;
371
+ capabilities: CloudVoiceRuntimeCapabilities;
372
+ };
373
+ voiceConfig: {
374
+ system: string;
375
+ model: string;
376
+ voice: string;
377
+ speech?: CloudVoiceSpeechConfig;
378
+ languageCode?: string;
379
+ turnDetection?: Record<string, unknown> | null;
380
+ transcription?: Record<string, unknown> | null;
381
+ inputNoiseReduction?: Record<string, unknown> | null;
382
+ maxOutputTokens: number | "inf";
383
+ temperature?: number;
384
+ tts?: {
385
+ provider: "cartesia";
386
+ options: CloudVoiceCartesiaTtsOptions;
387
+ };
388
+ };
389
+ tools: [];
390
+ toolManifest: CloudVoiceFunctionToolManifest[];
391
+ resourceId: Record<string, unknown>;
392
+ parameters: CloudVoiceParameterValueMap;
393
+ metadata: {
394
+ cloudVoice: true;
395
+ sessionId: string;
396
+ cloudVoiceConfigVersion: 2;
397
+ promptCompilerVersion: string;
398
+ clientTools: string[];
399
+ transferDestinations: string[];
400
+ parameterKeys: string[];
401
+ missingRequiredParameters: string[];
402
+ sensitiveParameterKeys: string[];
403
+ flowGraph?: {
404
+ startNodeId: string | null;
405
+ entryMode?: "incoming" | "outgoing" | "both";
406
+ nodeCount: number;
407
+ edgeCount: number;
408
+ referencedToolIds: string[];
409
+ missingToolIds: string[];
410
+ };
411
+ };
412
+ }
413
+
414
+ export interface CloudVoiceAgentSnapshot {
415
+ id: string;
416
+ slug: string;
417
+ name: string;
418
+ draftVersion: number;
419
+ publishedVersion: number | null;
420
+ }
421
+
422
+ export interface CloudVoicePhonePrepareSnapshot {
423
+ schemaVersion: 1;
424
+ createdAt: string;
425
+ channel: CloudVoiceChannel;
426
+ agent: {
427
+ id: string;
428
+ slug: string;
429
+ name: string;
430
+ version: number;
431
+ };
432
+ runtime: PreparedCloudVoiceConfig["runtime"];
433
+ voiceConfig: PreparedCloudVoiceConfig["voiceConfig"];
434
+ toolManifest: CloudVoiceFunctionToolManifest[];
435
+ config: CloudVoiceAgentConfig;
436
+ parameters: CloudVoiceParameterValueMap;
437
+ }
438
+
439
+ export interface ExecuteCloudVoiceToolBindingInput {
440
+ projectId: string;
441
+ sessionId: string;
442
+ tool: CloudVoiceToolBinding;
443
+ args: unknown;
444
+ resourceId?: Record<string, unknown>;
445
+ parameters?: CloudVoiceParameterValueMap;
446
+ executeWebSearch?: (input: {
447
+ query: string;
448
+ parameters: Record<string, unknown>;
449
+ }) => Promise<unknown>;
450
+ executeCloudFlow?: (input: {
451
+ flowSlug: string;
452
+ args: unknown;
453
+ resourceId?: Record<string, unknown>;
454
+ }) => Promise<unknown>;
455
+ executeKnowledgeBase?: (input: {
456
+ pipelineId: string;
457
+ query: string;
458
+ topK: number;
459
+ }) => Promise<unknown>;
460
+ executePlatformAction?: (input: {
461
+ actionSlug: string;
462
+ connectionId: string;
463
+ args: Record<string, unknown>;
464
+ }) => Promise<unknown>;
465
+ executeCalendarAction?: (input: {
466
+ action: string;
467
+ args: Record<string, unknown>;
468
+ config?: Record<string, unknown>;
469
+ }) => Promise<unknown>;
470
+ executeAppointmentAction?: (input: {
471
+ action: string;
472
+ args: Record<string, unknown>;
473
+ config?: Record<string, unknown>;
474
+ }) => Promise<unknown>;
475
+ executeCloudVoiceAgent?: (input: {
476
+ agentSlug: string;
477
+ fromNumberId: string;
478
+ to: string;
479
+ parameters: Record<string, unknown>;
480
+ timeoutMs: number;
481
+ intervalMs: number;
482
+ metadata: Record<string, unknown>;
483
+ tool: CloudVoiceToolBinding;
484
+ args: Record<string, unknown>;
485
+ }) => Promise<unknown>;
486
+ executeExternalWebhook?: (input: {
487
+ tool: CloudVoiceToolBinding;
488
+ args: unknown;
489
+ }) => Promise<unknown>;
490
+ executeSipTransfer?: (input: {
491
+ providerCallId: string;
492
+ destination: CloudVoiceTelephonyDestination;
493
+ mode?: CloudVoiceTransferMode;
494
+ reason?: string;
495
+ policy?: CloudVoiceTransferPolicy;
496
+ metadata?: Record<string, unknown>;
497
+ }) => Promise<TelephonyTransferCallResult>;
498
+ }
499
+
500
+ export interface ExecuteCloudVoiceToolBindingResult {
501
+ result: unknown;
502
+ metadata: Record<string, unknown>;
503
+ }
504
+
505
+ export interface CloudVoicePhoneControlAdapter {
506
+ provider: string;
507
+ hangUpCall(input: {
508
+ providerCallId: string;
509
+ reason?: string;
510
+ metadata?: Record<string, unknown>;
511
+ }): Promise<{
512
+ provider: string;
513
+ providerCallId: string;
514
+ status: string;
515
+ raw?: unknown;
516
+ }>;
517
+ }
518
+
519
+ export interface CloudVoiceSipControlAdapter {
520
+ provider: string;
521
+ transferCall(input: {
522
+ providerCallId: string;
523
+ destination: CloudVoiceTelephonyDestination;
524
+ mode?: CloudVoiceTransferMode;
525
+ reason?: string;
526
+ policy?: CloudVoiceTransferPolicy;
527
+ metadata?: Record<string, unknown>;
528
+ }): Promise<TelephonyTransferCallResult>;
529
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,11 @@
1
+ export function getString(value: unknown, fallback = "") {
2
+ return typeof value === "string" && value.trim() ? value.trim() : fallback;
3
+ }
4
+
5
+ export function getNumber(value: unknown, fallback: number) {
6
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
7
+ }
8
+
9
+ export function getRecord(value: unknown): Record<string, unknown> {
10
+ return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "rootDir": "src",
6
+ "outDir": "dist",
7
+ "declaration": true,
8
+ "noEmit": false,
9
+ "incremental": false
10
+ },
11
+ "include": ["src"],
12
+ "exclude": ["src/__tests__"]
13
+ }