@absolutejs/voice 0.0.22-beta.7 → 0.0.22-beta.71
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/README.md +205 -0
- package/dist/agent.d.ts +2 -0
- package/dist/angular/index.d.ts +6 -0
- package/dist/angular/index.js +833 -43
- package/dist/angular/voice-app-kit-status.service.d.ts +12 -0
- package/dist/angular/voice-ops-status.component.d.ts +15 -0
- package/dist/angular/voice-provider-capabilities.service.d.ts +12 -0
- package/dist/angular/voice-provider-status.service.d.ts +12 -0
- package/dist/angular/voice-routing-status.service.d.ts +11 -0
- package/dist/angular/voice-stream.service.d.ts +2 -0
- package/dist/angular/voice-turn-quality.service.d.ts +12 -0
- package/dist/angular/voice-workflow-status.service.d.ts +12 -0
- package/dist/appKit.d.ts +94 -0
- package/dist/assistantHealth.d.ts +81 -0
- package/dist/client/actions.d.ts +22 -0
- package/dist/client/appKitStatus.d.ts +19 -0
- package/dist/client/connection.d.ts +3 -0
- package/dist/client/htmxBootstrap.js +44 -2
- package/dist/client/index.d.ts +26 -0
- package/dist/client/index.js +1290 -2
- package/dist/client/opsStatusWidget.d.ts +40 -0
- package/dist/client/providerCapabilities.d.ts +19 -0
- package/dist/client/providerCapabilitiesWidget.d.ts +32 -0
- package/dist/client/providerSimulationControls.d.ts +33 -0
- package/dist/client/providerSimulationControlsWidget.d.ts +20 -0
- package/dist/client/providerStatus.d.ts +19 -0
- package/dist/client/providerStatusWidget.d.ts +32 -0
- package/dist/client/routingStatus.d.ts +19 -0
- package/dist/client/routingStatusWidget.d.ts +28 -0
- package/dist/client/turnQuality.d.ts +19 -0
- package/dist/client/turnQualityWidget.d.ts +32 -0
- package/dist/client/workflowStatus.d.ts +19 -0
- package/dist/diagnosticsRoutes.d.ts +44 -0
- package/dist/evalRoutes.d.ts +213 -0
- package/dist/handoff.d.ts +54 -0
- package/dist/handoffHealth.d.ts +94 -0
- package/dist/index.d.ts +44 -4
- package/dist/index.js +5677 -139
- package/dist/modelAdapters.d.ts +75 -0
- package/dist/opsConsoleRoutes.d.ts +77 -0
- package/dist/opsWebhook.d.ts +126 -0
- package/dist/outcomeContract.d.ts +112 -0
- package/dist/providerAdapters.d.ts +48 -0
- package/dist/providerCapabilities.d.ts +92 -0
- package/dist/providerHealth.d.ts +79 -0
- package/dist/qualityRoutes.d.ts +76 -0
- package/dist/queue.d.ts +52 -0
- package/dist/react/VoiceOpsStatus.d.ts +6 -0
- package/dist/react/VoiceProviderCapabilities.d.ts +6 -0
- package/dist/react/VoiceProviderSimulationControls.d.ts +5 -0
- package/dist/react/VoiceProviderStatus.d.ts +6 -0
- package/dist/react/VoiceRoutingStatus.d.ts +6 -0
- package/dist/react/VoiceTurnQuality.d.ts +6 -0
- package/dist/react/index.d.ts +13 -0
- package/dist/react/index.js +1884 -11
- package/dist/react/useVoiceAppKitStatus.d.ts +8 -0
- package/dist/react/useVoiceController.d.ts +2 -0
- package/dist/react/useVoiceProviderCapabilities.d.ts +8 -0
- package/dist/react/useVoiceProviderSimulationControls.d.ts +10 -0
- package/dist/react/useVoiceProviderStatus.d.ts +8 -0
- package/dist/react/useVoiceRoutingStatus.d.ts +8 -0
- package/dist/react/useVoiceStream.d.ts +2 -0
- package/dist/react/useVoiceTurnQuality.d.ts +8 -0
- package/dist/react/useVoiceWorkflowStatus.d.ts +8 -0
- package/dist/resilienceRoutes.d.ts +117 -0
- package/dist/sessionReplay.d.ts +175 -0
- package/dist/svelte/createVoiceAppKitStatus.d.ts +8 -0
- package/dist/svelte/createVoiceOpsStatus.d.ts +9 -0
- package/dist/svelte/createVoiceProviderCapabilities.d.ts +10 -0
- package/dist/svelte/createVoiceProviderSimulationControls.d.ts +11 -0
- package/dist/svelte/createVoiceProviderStatus.d.ts +10 -0
- package/dist/svelte/createVoiceRoutingStatus.d.ts +10 -0
- package/dist/svelte/createVoiceTurnQuality.d.ts +10 -0
- package/dist/svelte/createVoiceWorkflowStatus.d.ts +8 -0
- package/dist/svelte/index.d.ts +8 -0
- package/dist/svelte/index.js +1330 -3
- package/dist/telephonyOutcome.d.ts +178 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +1537 -7
- package/dist/testing/ioProviderSimulator.d.ts +41 -0
- package/dist/testing/providerSimulator.d.ts +44 -0
- package/dist/toolContract.d.ts +130 -0
- package/dist/toolRuntime.d.ts +50 -0
- package/dist/trace.d.ts +1 -1
- package/dist/turnQuality.d.ts +94 -0
- package/dist/types.d.ts +84 -2
- package/dist/vue/VoiceOpsStatus.d.ts +30 -0
- package/dist/vue/VoiceProviderCapabilities.d.ts +51 -0
- package/dist/vue/VoiceProviderSimulationControls.d.ts +88 -0
- package/dist/vue/VoiceProviderStatus.d.ts +51 -0
- package/dist/vue/VoiceRoutingStatus.d.ts +51 -0
- package/dist/vue/VoiceTurnQuality.d.ts +51 -0
- package/dist/vue/index.d.ts +13 -0
- package/dist/vue/index.js +1934 -25
- package/dist/vue/useVoiceAppKitStatus.d.ts +9 -0
- package/dist/vue/useVoiceProviderCapabilities.d.ts +9 -0
- package/dist/vue/useVoiceProviderSimulationControls.d.ts +24 -0
- package/dist/vue/useVoiceProviderStatus.d.ts +9 -0
- package/dist/vue/useVoiceRoutingStatus.d.ts +8 -0
- package/dist/vue/useVoiceStream.d.ts +2 -0
- package/dist/vue/useVoiceTurnQuality.d.ts +9 -0
- package/dist/vue/useVoiceWorkflowStatus.d.ts +9 -0
- package/dist/workflowContract.d.ts +91 -0
- package/package.json +1 -1
package/dist/testing/index.js
CHANGED
|
@@ -2105,6 +2105,12 @@ var serverMessageToAction = (message) => {
|
|
|
2105
2105
|
sessionId: message.sessionId,
|
|
2106
2106
|
type: "complete"
|
|
2107
2107
|
};
|
|
2108
|
+
case "call_lifecycle":
|
|
2109
|
+
return {
|
|
2110
|
+
event: message.event,
|
|
2111
|
+
sessionId: message.sessionId,
|
|
2112
|
+
type: "call_lifecycle"
|
|
2113
|
+
};
|
|
2108
2114
|
case "error":
|
|
2109
2115
|
return {
|
|
2110
2116
|
message: normalizeErrorMessage(message.message),
|
|
@@ -2148,7 +2154,7 @@ var DEFAULT_SCENARIO_QUERY_PARAM = "scenarioId";
|
|
|
2148
2154
|
var noop = () => {};
|
|
2149
2155
|
var noopUnsubscribe = () => noop;
|
|
2150
2156
|
var NOOP_CONNECTION = {
|
|
2151
|
-
|
|
2157
|
+
callControl: noop,
|
|
2152
2158
|
close: noop,
|
|
2153
2159
|
endTurn: noop,
|
|
2154
2160
|
getReadyState: () => WS_CLOSED,
|
|
@@ -2156,6 +2162,7 @@ var NOOP_CONNECTION = {
|
|
|
2156
2162
|
getSessionId: () => "",
|
|
2157
2163
|
send: noop,
|
|
2158
2164
|
sendAudio: noop,
|
|
2165
|
+
start: () => {},
|
|
2159
2166
|
subscribe: noopUnsubscribe
|
|
2160
2167
|
};
|
|
2161
2168
|
var createSessionId = () => crypto.randomUUID();
|
|
@@ -2177,6 +2184,7 @@ var isVoiceServerMessage = (value) => {
|
|
|
2177
2184
|
switch (value.type) {
|
|
2178
2185
|
case "audio":
|
|
2179
2186
|
case "assistant":
|
|
2187
|
+
case "call_lifecycle":
|
|
2180
2188
|
case "complete":
|
|
2181
2189
|
case "error":
|
|
2182
2190
|
case "final":
|
|
@@ -2317,6 +2325,12 @@ var createVoiceConnection = (path, options = {}) => {
|
|
|
2317
2325
|
const endTurn = () => {
|
|
2318
2326
|
send({ type: "end_turn" });
|
|
2319
2327
|
};
|
|
2328
|
+
const callControl = (message) => {
|
|
2329
|
+
send({
|
|
2330
|
+
...message,
|
|
2331
|
+
type: "call_control"
|
|
2332
|
+
});
|
|
2333
|
+
};
|
|
2320
2334
|
const close = () => {
|
|
2321
2335
|
clearTimers();
|
|
2322
2336
|
if (state.ws) {
|
|
@@ -2334,7 +2348,7 @@ var createVoiceConnection = (path, options = {}) => {
|
|
|
2334
2348
|
};
|
|
2335
2349
|
connect();
|
|
2336
2350
|
return {
|
|
2337
|
-
|
|
2351
|
+
callControl,
|
|
2338
2352
|
close,
|
|
2339
2353
|
endTurn,
|
|
2340
2354
|
getReadyState: () => state.ws?.readyState ?? WS_CLOSED,
|
|
@@ -2342,6 +2356,7 @@ var createVoiceConnection = (path, options = {}) => {
|
|
|
2342
2356
|
getSessionId: () => state.sessionId,
|
|
2343
2357
|
send,
|
|
2344
2358
|
sendAudio,
|
|
2359
|
+
start,
|
|
2345
2360
|
subscribe
|
|
2346
2361
|
};
|
|
2347
2362
|
};
|
|
@@ -2350,6 +2365,7 @@ var createVoiceConnection = (path, options = {}) => {
|
|
|
2350
2365
|
var createInitialState2 = () => ({
|
|
2351
2366
|
assistantAudio: [],
|
|
2352
2367
|
assistantTexts: [],
|
|
2368
|
+
call: null,
|
|
2353
2369
|
error: null,
|
|
2354
2370
|
isConnected: false,
|
|
2355
2371
|
scenarioId: null,
|
|
@@ -2393,6 +2409,20 @@ var createVoiceStreamStore = () => {
|
|
|
2393
2409
|
status: "completed"
|
|
2394
2410
|
};
|
|
2395
2411
|
break;
|
|
2412
|
+
case "call_lifecycle":
|
|
2413
|
+
state = {
|
|
2414
|
+
...state,
|
|
2415
|
+
call: {
|
|
2416
|
+
...state.call,
|
|
2417
|
+
disposition: action.event.type === "end" ? action.event.disposition : state.call?.disposition,
|
|
2418
|
+
endedAt: action.event.type === "end" ? action.event.at : state.call?.endedAt,
|
|
2419
|
+
events: [...state.call?.events ?? [], action.event],
|
|
2420
|
+
lastEventAt: action.event.at,
|
|
2421
|
+
startedAt: state.call?.startedAt ?? action.event.at
|
|
2422
|
+
},
|
|
2423
|
+
sessionId: action.sessionId
|
|
2424
|
+
};
|
|
2425
|
+
break;
|
|
2396
2426
|
case "connected":
|
|
2397
2427
|
state = {
|
|
2398
2428
|
...state,
|
|
@@ -2479,6 +2509,9 @@ var createVoiceStream = (path, options = {}) => {
|
|
|
2479
2509
|
}
|
|
2480
2510
|
});
|
|
2481
2511
|
return {
|
|
2512
|
+
callControl(message) {
|
|
2513
|
+
connection.callControl(message);
|
|
2514
|
+
},
|
|
2482
2515
|
close() {
|
|
2483
2516
|
unsubscribeConnection();
|
|
2484
2517
|
connection.close();
|
|
@@ -2522,6 +2555,9 @@ var createVoiceStream = (path, options = {}) => {
|
|
|
2522
2555
|
get assistantAudio() {
|
|
2523
2556
|
return store.getSnapshot().assistantAudio;
|
|
2524
2557
|
},
|
|
2558
|
+
get call() {
|
|
2559
|
+
return store.getSnapshot().call;
|
|
2560
|
+
},
|
|
2525
2561
|
sendAudio(audio) {
|
|
2526
2562
|
connection.sendAudio(audio);
|
|
2527
2563
|
},
|
|
@@ -2854,6 +2890,7 @@ var resolveVoiceRuntimePreset = (name = "default") => {
|
|
|
2854
2890
|
var createInitialState3 = (stream) => ({
|
|
2855
2891
|
assistantAudio: [...stream.assistantAudio],
|
|
2856
2892
|
assistantTexts: [...stream.assistantTexts],
|
|
2893
|
+
call: stream.call,
|
|
2857
2894
|
error: stream.error,
|
|
2858
2895
|
isConnected: stream.isConnected,
|
|
2859
2896
|
isRecording: false,
|
|
@@ -2883,6 +2920,7 @@ var createVoiceController = (path, options = {}) => {
|
|
|
2883
2920
|
...state,
|
|
2884
2921
|
assistantAudio: [...stream.assistantAudio],
|
|
2885
2922
|
assistantTexts: [...stream.assistantTexts],
|
|
2923
|
+
call: stream.call,
|
|
2886
2924
|
error: stream.error,
|
|
2887
2925
|
isConnected: stream.isConnected,
|
|
2888
2926
|
partial: stream.partial,
|
|
@@ -2960,6 +2998,7 @@ var createVoiceController = (path, options = {}) => {
|
|
|
2960
2998
|
bindHTMX(bindingOptions) {
|
|
2961
2999
|
return bindVoiceHTMX(stream, bindingOptions);
|
|
2962
3000
|
},
|
|
3001
|
+
callControl: (message) => stream.callControl(message),
|
|
2963
3002
|
close,
|
|
2964
3003
|
endTurn: () => stream.endTurn(),
|
|
2965
3004
|
get error() {
|
|
@@ -3012,6 +3051,9 @@ var createVoiceController = (path, options = {}) => {
|
|
|
3012
3051
|
},
|
|
3013
3052
|
get assistantAudio() {
|
|
3014
3053
|
return state.assistantAudio;
|
|
3054
|
+
},
|
|
3055
|
+
get call() {
|
|
3056
|
+
return state.call;
|
|
3015
3057
|
}
|
|
3016
3058
|
};
|
|
3017
3059
|
};
|
|
@@ -3468,6 +3510,1004 @@ var loadVoiceTestFixtures = async (fixtureDirectory) => {
|
|
|
3468
3510
|
}
|
|
3469
3511
|
return fixtures;
|
|
3470
3512
|
};
|
|
3513
|
+
// src/testing/ioProviderSimulator.ts
|
|
3514
|
+
var defaultFailureMessage = (input) => `Simulated ${input.provider} ${input.kind.toUpperCase()} ${input.operation} failure.`;
|
|
3515
|
+
var resolveRecoveryElapsedMs = (value, provider) => {
|
|
3516
|
+
if (typeof value === "number") {
|
|
3517
|
+
return value;
|
|
3518
|
+
}
|
|
3519
|
+
return value?.[provider] ?? 25;
|
|
3520
|
+
};
|
|
3521
|
+
var createHealth = (input) => ({
|
|
3522
|
+
consecutiveFailures: input.status === "healthy" ? 0 : 1,
|
|
3523
|
+
lastFailureAt: input.status === "healthy" ? undefined : input.now,
|
|
3524
|
+
provider: input.provider,
|
|
3525
|
+
status: input.status,
|
|
3526
|
+
suppressedUntil: input.suppressedUntil
|
|
3527
|
+
});
|
|
3528
|
+
var resolveFallback = async (options, provider) => {
|
|
3529
|
+
const configured = typeof options.fallback === "function" ? await options.fallback(provider) : options.fallback;
|
|
3530
|
+
return (configured ?? options.providers).find((candidate) => candidate !== provider);
|
|
3531
|
+
};
|
|
3532
|
+
var createVoiceIOProviderFailureSimulator = (options) => {
|
|
3533
|
+
if (options.providers.length === 0) {
|
|
3534
|
+
throw new Error("At least one provider is required.");
|
|
3535
|
+
}
|
|
3536
|
+
const now = options.now ?? Date.now;
|
|
3537
|
+
const operation = options.operation ?? "open";
|
|
3538
|
+
const cooldownMs = Math.max(0, options.cooldownMs ?? 30000);
|
|
3539
|
+
const emit = async (event, input) => {
|
|
3540
|
+
await options.onProviderEvent?.(event, input);
|
|
3541
|
+
};
|
|
3542
|
+
const run = async (provider, mode) => {
|
|
3543
|
+
if (!options.providers.includes(provider)) {
|
|
3544
|
+
throw new Error(`${provider} is not configured for simulation.`);
|
|
3545
|
+
}
|
|
3546
|
+
const startedAt = now();
|
|
3547
|
+
const sessionId = options.sessionId?.({ mode, now: startedAt, provider }) ?? `${options.kind}-provider-sim-${startedAt}`;
|
|
3548
|
+
if (mode === "recovery") {
|
|
3549
|
+
await emit({
|
|
3550
|
+
at: startedAt,
|
|
3551
|
+
attempt: 0,
|
|
3552
|
+
elapsedMs: resolveRecoveryElapsedMs(options.recoveryElapsedMs, provider),
|
|
3553
|
+
kind: options.kind,
|
|
3554
|
+
latencyBudgetMs: options.latencyBudgets?.[provider],
|
|
3555
|
+
operation,
|
|
3556
|
+
provider,
|
|
3557
|
+
providerHealth: createHealth({
|
|
3558
|
+
now: startedAt,
|
|
3559
|
+
provider,
|
|
3560
|
+
status: "healthy"
|
|
3561
|
+
}),
|
|
3562
|
+
selectedProvider: provider,
|
|
3563
|
+
status: "success"
|
|
3564
|
+
}, { mode, provider, sessionId });
|
|
3565
|
+
return {
|
|
3566
|
+
mode,
|
|
3567
|
+
provider,
|
|
3568
|
+
sessionId,
|
|
3569
|
+
status: "simulated"
|
|
3570
|
+
};
|
|
3571
|
+
}
|
|
3572
|
+
const fallbackProvider = await resolveFallback(options, provider);
|
|
3573
|
+
const suppressedUntil = startedAt + cooldownMs;
|
|
3574
|
+
await emit({
|
|
3575
|
+
at: startedAt,
|
|
3576
|
+
attempt: 0,
|
|
3577
|
+
elapsedMs: options.failureElapsedMs ?? 10,
|
|
3578
|
+
error: (options.failureMessage ?? defaultFailureMessage)({
|
|
3579
|
+
kind: options.kind,
|
|
3580
|
+
operation,
|
|
3581
|
+
provider
|
|
3582
|
+
}),
|
|
3583
|
+
fallbackProvider,
|
|
3584
|
+
kind: options.kind,
|
|
3585
|
+
latencyBudgetMs: options.latencyBudgets?.[provider],
|
|
3586
|
+
operation,
|
|
3587
|
+
provider,
|
|
3588
|
+
providerHealth: createHealth({
|
|
3589
|
+
now: startedAt,
|
|
3590
|
+
provider,
|
|
3591
|
+
status: "suppressed",
|
|
3592
|
+
suppressedUntil
|
|
3593
|
+
}),
|
|
3594
|
+
selectedProvider: provider,
|
|
3595
|
+
status: "error",
|
|
3596
|
+
suppressedUntil
|
|
3597
|
+
}, { mode, provider, sessionId });
|
|
3598
|
+
if (fallbackProvider) {
|
|
3599
|
+
await emit({
|
|
3600
|
+
at: startedAt + 1,
|
|
3601
|
+
attempt: 1,
|
|
3602
|
+
elapsedMs: resolveRecoveryElapsedMs(options.recoveryElapsedMs, fallbackProvider),
|
|
3603
|
+
fallbackProvider,
|
|
3604
|
+
kind: options.kind,
|
|
3605
|
+
latencyBudgetMs: options.latencyBudgets?.[fallbackProvider],
|
|
3606
|
+
operation,
|
|
3607
|
+
provider: fallbackProvider,
|
|
3608
|
+
providerHealth: createHealth({
|
|
3609
|
+
now: startedAt + 1,
|
|
3610
|
+
provider: fallbackProvider,
|
|
3611
|
+
status: "healthy"
|
|
3612
|
+
}),
|
|
3613
|
+
selectedProvider: provider,
|
|
3614
|
+
status: "fallback"
|
|
3615
|
+
}, { mode, provider, sessionId });
|
|
3616
|
+
}
|
|
3617
|
+
return {
|
|
3618
|
+
fallbackProvider,
|
|
3619
|
+
mode,
|
|
3620
|
+
provider,
|
|
3621
|
+
sessionId,
|
|
3622
|
+
status: "simulated",
|
|
3623
|
+
suppressedUntil
|
|
3624
|
+
};
|
|
3625
|
+
};
|
|
3626
|
+
return {
|
|
3627
|
+
run
|
|
3628
|
+
};
|
|
3629
|
+
};
|
|
3630
|
+
// src/modelAdapters.ts
|
|
3631
|
+
var resolveVoiceProviderRoutingPolicyPreset = (preset, options = {}) => {
|
|
3632
|
+
switch (preset) {
|
|
3633
|
+
case "balanced":
|
|
3634
|
+
return {
|
|
3635
|
+
fallbackMode: "provider-error",
|
|
3636
|
+
strategy: "balanced",
|
|
3637
|
+
weights: {
|
|
3638
|
+
cost: 1,
|
|
3639
|
+
latencyMs: 0.005,
|
|
3640
|
+
priority: 1,
|
|
3641
|
+
quality: 10,
|
|
3642
|
+
...options.weights
|
|
3643
|
+
},
|
|
3644
|
+
...options
|
|
3645
|
+
};
|
|
3646
|
+
case "cost-cap":
|
|
3647
|
+
return {
|
|
3648
|
+
fallbackMode: "provider-error",
|
|
3649
|
+
strategy: "prefer-cheapest",
|
|
3650
|
+
...options
|
|
3651
|
+
};
|
|
3652
|
+
case "cost-first":
|
|
3653
|
+
return {
|
|
3654
|
+
fallbackMode: "provider-error",
|
|
3655
|
+
strategy: "prefer-cheapest",
|
|
3656
|
+
...options
|
|
3657
|
+
};
|
|
3658
|
+
case "latency-first":
|
|
3659
|
+
return {
|
|
3660
|
+
fallbackMode: "provider-error",
|
|
3661
|
+
strategy: "prefer-fastest",
|
|
3662
|
+
...options
|
|
3663
|
+
};
|
|
3664
|
+
case "quality-first":
|
|
3665
|
+
return {
|
|
3666
|
+
fallbackMode: "provider-error",
|
|
3667
|
+
strategy: "quality-first",
|
|
3668
|
+
...options
|
|
3669
|
+
};
|
|
3670
|
+
}
|
|
3671
|
+
};
|
|
3672
|
+
var OUTPUT_SCHEMA = {
|
|
3673
|
+
additionalProperties: false,
|
|
3674
|
+
properties: {
|
|
3675
|
+
assistantText: {
|
|
3676
|
+
type: "string"
|
|
3677
|
+
},
|
|
3678
|
+
complete: {
|
|
3679
|
+
type: "boolean"
|
|
3680
|
+
},
|
|
3681
|
+
escalate: {
|
|
3682
|
+
additionalProperties: false,
|
|
3683
|
+
properties: {
|
|
3684
|
+
metadata: {
|
|
3685
|
+
additionalProperties: true,
|
|
3686
|
+
type: "object"
|
|
3687
|
+
},
|
|
3688
|
+
reason: {
|
|
3689
|
+
type: "string"
|
|
3690
|
+
}
|
|
3691
|
+
},
|
|
3692
|
+
required: ["reason"],
|
|
3693
|
+
type: "object"
|
|
3694
|
+
},
|
|
3695
|
+
noAnswer: {
|
|
3696
|
+
additionalProperties: false,
|
|
3697
|
+
properties: {
|
|
3698
|
+
metadata: {
|
|
3699
|
+
additionalProperties: true,
|
|
3700
|
+
type: "object"
|
|
3701
|
+
}
|
|
3702
|
+
},
|
|
3703
|
+
type: "object"
|
|
3704
|
+
},
|
|
3705
|
+
result: {
|
|
3706
|
+
additionalProperties: true,
|
|
3707
|
+
type: "object"
|
|
3708
|
+
},
|
|
3709
|
+
transfer: {
|
|
3710
|
+
additionalProperties: false,
|
|
3711
|
+
properties: {
|
|
3712
|
+
metadata: {
|
|
3713
|
+
additionalProperties: true,
|
|
3714
|
+
type: "object"
|
|
3715
|
+
},
|
|
3716
|
+
reason: {
|
|
3717
|
+
type: "string"
|
|
3718
|
+
},
|
|
3719
|
+
target: {
|
|
3720
|
+
type: "string"
|
|
3721
|
+
}
|
|
3722
|
+
},
|
|
3723
|
+
required: ["target"],
|
|
3724
|
+
type: "object"
|
|
3725
|
+
},
|
|
3726
|
+
voicemail: {
|
|
3727
|
+
additionalProperties: false,
|
|
3728
|
+
properties: {
|
|
3729
|
+
metadata: {
|
|
3730
|
+
additionalProperties: true,
|
|
3731
|
+
type: "object"
|
|
3732
|
+
}
|
|
3733
|
+
},
|
|
3734
|
+
type: "object"
|
|
3735
|
+
}
|
|
3736
|
+
},
|
|
3737
|
+
type: "object"
|
|
3738
|
+
};
|
|
3739
|
+
var ROUTE_RESULT_INSTRUCTION = "Return only a JSON object with assistantText, complete, transfer, escalate, voicemail, noAnswer, and result when you are not calling tools. Only set transfer, escalate, voicemail, or noAnswer when the user explicitly asks for that lifecycle outcome or a tool result says that exact outcome. Do not infer voicemail from generic words like voice, voice app, or voice integration.";
|
|
3740
|
+
var stripJSONCodeFence = (value) => {
|
|
3741
|
+
const trimmed = value.trim();
|
|
3742
|
+
const match = trimmed.match(/^```(?:json)?\s*([\s\S]*?)\s*```$/i);
|
|
3743
|
+
return match?.[1]?.trim() ?? value;
|
|
3744
|
+
};
|
|
3745
|
+
var parseJSON = (value) => {
|
|
3746
|
+
try {
|
|
3747
|
+
const parsed = JSON.parse(stripJSONCodeFence(value));
|
|
3748
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
3749
|
+
} catch {
|
|
3750
|
+
return {
|
|
3751
|
+
assistantText: value
|
|
3752
|
+
};
|
|
3753
|
+
}
|
|
3754
|
+
};
|
|
3755
|
+
var parseJSONValue = (value) => {
|
|
3756
|
+
try {
|
|
3757
|
+
return JSON.parse(value);
|
|
3758
|
+
} catch {
|
|
3759
|
+
return value;
|
|
3760
|
+
}
|
|
3761
|
+
};
|
|
3762
|
+
|
|
3763
|
+
class VoiceProviderTimeoutError extends Error {
|
|
3764
|
+
provider;
|
|
3765
|
+
timeoutMs;
|
|
3766
|
+
constructor(provider, timeoutMs) {
|
|
3767
|
+
super(`Voice provider ${provider} exceeded ${timeoutMs}ms latency budget.`);
|
|
3768
|
+
this.name = "VoiceProviderTimeoutError";
|
|
3769
|
+
this.provider = provider;
|
|
3770
|
+
this.timeoutMs = timeoutMs;
|
|
3771
|
+
}
|
|
3772
|
+
}
|
|
3773
|
+
var getMessageToolCalls = (message) => {
|
|
3774
|
+
const toolCalls = message.metadata?.toolCalls;
|
|
3775
|
+
return Array.isArray(toolCalls) ? toolCalls.filter((toolCall) => toolCall && typeof toolCall === "object" && typeof toolCall.name === "string") : [];
|
|
3776
|
+
};
|
|
3777
|
+
var createHTTPError = (provider, response) => new Error(`${provider} voice assistant model failed: HTTP ${response.status}`);
|
|
3778
|
+
var sleep = (ms) => new Promise((resolve2) => {
|
|
3779
|
+
setTimeout(resolve2, ms);
|
|
3780
|
+
});
|
|
3781
|
+
var errorMessage = (error) => error instanceof Error ? error.message : String(error);
|
|
3782
|
+
var defaultIsRateLimitError = (error) => /(\b429\b|rate limit|quota|too many requests)/i.test(errorMessage(error));
|
|
3783
|
+
var normalizeRouteOutput = (output) => {
|
|
3784
|
+
const result = {};
|
|
3785
|
+
if (typeof output.assistantText === "string") {
|
|
3786
|
+
result.assistantText = output.assistantText;
|
|
3787
|
+
}
|
|
3788
|
+
if (typeof output.complete === "boolean") {
|
|
3789
|
+
result.complete = output.complete;
|
|
3790
|
+
}
|
|
3791
|
+
if (output.result !== undefined) {
|
|
3792
|
+
result.result = output.result;
|
|
3793
|
+
}
|
|
3794
|
+
if (output.transfer && typeof output.transfer === "object") {
|
|
3795
|
+
const transfer = output.transfer;
|
|
3796
|
+
if (typeof transfer.target === "string") {
|
|
3797
|
+
result.transfer = {
|
|
3798
|
+
metadata: transfer.metadata && typeof transfer.metadata === "object" ? transfer.metadata : undefined,
|
|
3799
|
+
reason: typeof transfer.reason === "string" ? transfer.reason : undefined,
|
|
3800
|
+
target: transfer.target
|
|
3801
|
+
};
|
|
3802
|
+
}
|
|
3803
|
+
}
|
|
3804
|
+
if (output.escalate && typeof output.escalate === "object") {
|
|
3805
|
+
const escalate = output.escalate;
|
|
3806
|
+
if (typeof escalate.reason === "string") {
|
|
3807
|
+
result.escalate = {
|
|
3808
|
+
metadata: escalate.metadata && typeof escalate.metadata === "object" ? escalate.metadata : undefined,
|
|
3809
|
+
reason: escalate.reason
|
|
3810
|
+
};
|
|
3811
|
+
}
|
|
3812
|
+
}
|
|
3813
|
+
if (output.voicemail && typeof output.voicemail === "object") {
|
|
3814
|
+
const voicemail = output.voicemail;
|
|
3815
|
+
result.voicemail = {
|
|
3816
|
+
metadata: voicemail.metadata && typeof voicemail.metadata === "object" ? voicemail.metadata : undefined
|
|
3817
|
+
};
|
|
3818
|
+
}
|
|
3819
|
+
if (output.noAnswer && typeof output.noAnswer === "object") {
|
|
3820
|
+
const noAnswer = output.noAnswer;
|
|
3821
|
+
result.noAnswer = {
|
|
3822
|
+
metadata: noAnswer.metadata && typeof noAnswer.metadata === "object" ? noAnswer.metadata : undefined
|
|
3823
|
+
};
|
|
3824
|
+
}
|
|
3825
|
+
return result;
|
|
3826
|
+
};
|
|
3827
|
+
var createJSONVoiceAssistantModel = (options) => ({
|
|
3828
|
+
generate: async (input) => {
|
|
3829
|
+
const output = await options.generate(input);
|
|
3830
|
+
if ("assistantText" in output || "toolCalls" in output || "complete" in output || "transfer" in output || "escalate" in output) {
|
|
3831
|
+
return output;
|
|
3832
|
+
}
|
|
3833
|
+
return options.mapOutput?.(output) ?? normalizeRouteOutput(output);
|
|
3834
|
+
}
|
|
3835
|
+
});
|
|
3836
|
+
var createVoiceProviderRouter = (options) => {
|
|
3837
|
+
const providerIds = Object.keys(options.providers);
|
|
3838
|
+
const firstProvider = providerIds[0];
|
|
3839
|
+
const policy = typeof options.policy === "string" ? options.policy === "balanced" || options.policy === "cost-cap" || options.policy === "cost-first" || options.policy === "latency-first" || options.policy === "quality-first" ? resolveVoiceProviderRoutingPolicyPreset(options.policy) : {
|
|
3840
|
+
strategy: options.policy
|
|
3841
|
+
} : options.policy;
|
|
3842
|
+
const strategy = policy?.strategy ?? "prefer-selected";
|
|
3843
|
+
const fallbackMode = policy?.fallbackMode ?? options.fallbackMode ?? "provider-error";
|
|
3844
|
+
const healthOptions = typeof options.providerHealth === "object" ? options.providerHealth : options.providerHealth ? {} : undefined;
|
|
3845
|
+
const healthState = new Map;
|
|
3846
|
+
const now = () => healthOptions?.now?.() ?? Date.now();
|
|
3847
|
+
const failureThreshold = Math.max(1, healthOptions?.failureThreshold ?? 1);
|
|
3848
|
+
const cooldownMs = Math.max(0, healthOptions?.cooldownMs ?? 30000);
|
|
3849
|
+
const rateLimitCooldownMs = Math.max(0, healthOptions?.rateLimitCooldownMs ?? 60000);
|
|
3850
|
+
const getProviderTimeoutMs = (provider) => {
|
|
3851
|
+
const timeoutMs = options.providerProfiles?.[provider]?.timeoutMs ?? options.timeoutMs;
|
|
3852
|
+
return typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : undefined;
|
|
3853
|
+
};
|
|
3854
|
+
const getHealth = (provider) => {
|
|
3855
|
+
const existing = healthState.get(provider);
|
|
3856
|
+
if (existing) {
|
|
3857
|
+
return existing;
|
|
3858
|
+
}
|
|
3859
|
+
const next = {
|
|
3860
|
+
consecutiveFailures: 0,
|
|
3861
|
+
provider,
|
|
3862
|
+
status: "healthy"
|
|
3863
|
+
};
|
|
3864
|
+
healthState.set(provider, next);
|
|
3865
|
+
return next;
|
|
3866
|
+
};
|
|
3867
|
+
const cloneHealth = (provider) => {
|
|
3868
|
+
if (!healthOptions) {
|
|
3869
|
+
return;
|
|
3870
|
+
}
|
|
3871
|
+
return {
|
|
3872
|
+
...getHealth(provider)
|
|
3873
|
+
};
|
|
3874
|
+
};
|
|
3875
|
+
const getSuppressionRemainingMs = (provider) => {
|
|
3876
|
+
if (!healthOptions) {
|
|
3877
|
+
return;
|
|
3878
|
+
}
|
|
3879
|
+
const suppressedUntil = getHealth(provider).suppressedUntil;
|
|
3880
|
+
return typeof suppressedUntil === "number" ? Math.max(0, suppressedUntil - now()) : undefined;
|
|
3881
|
+
};
|
|
3882
|
+
const isSuppressed = (provider) => {
|
|
3883
|
+
if (!healthOptions) {
|
|
3884
|
+
return false;
|
|
3885
|
+
}
|
|
3886
|
+
const health = getHealth(provider);
|
|
3887
|
+
return typeof health.suppressedUntil === "number" && health.suppressedUntil > now();
|
|
3888
|
+
};
|
|
3889
|
+
const recordProviderSuccess = (provider) => {
|
|
3890
|
+
if (!healthOptions) {
|
|
3891
|
+
return;
|
|
3892
|
+
}
|
|
3893
|
+
const health = getHealth(provider);
|
|
3894
|
+
health.consecutiveFailures = 0;
|
|
3895
|
+
health.status = "healthy";
|
|
3896
|
+
health.suppressedUntil = undefined;
|
|
3897
|
+
return cloneHealth(provider);
|
|
3898
|
+
};
|
|
3899
|
+
const recordProviderError = (provider, isProviderError, rateLimited) => {
|
|
3900
|
+
if (!healthOptions || !isProviderError) {
|
|
3901
|
+
return cloneHealth(provider);
|
|
3902
|
+
}
|
|
3903
|
+
const currentTime = now();
|
|
3904
|
+
const health = getHealth(provider);
|
|
3905
|
+
health.consecutiveFailures += 1;
|
|
3906
|
+
health.lastFailureAt = currentTime;
|
|
3907
|
+
if (rateLimited) {
|
|
3908
|
+
health.lastRateLimitedAt = currentTime;
|
|
3909
|
+
}
|
|
3910
|
+
if (rateLimited || health.consecutiveFailures >= failureThreshold) {
|
|
3911
|
+
health.status = "suppressed";
|
|
3912
|
+
health.suppressedUntil = currentTime + (rateLimited ? rateLimitCooldownMs : cooldownMs);
|
|
3913
|
+
}
|
|
3914
|
+
return cloneHealth(provider);
|
|
3915
|
+
};
|
|
3916
|
+
const resolveAllowedProviders = async (input) => {
|
|
3917
|
+
const allowProviders = policy?.allowProviders ?? options.allowProviders;
|
|
3918
|
+
const allowed = typeof allowProviders === "function" ? await allowProviders(input) : allowProviders;
|
|
3919
|
+
return new Set(allowed ?? providerIds);
|
|
3920
|
+
};
|
|
3921
|
+
const passesBudgetFilters = (provider) => {
|
|
3922
|
+
const profile = options.providerProfiles?.[provider];
|
|
3923
|
+
if (typeof policy?.maxCost === "number" && typeof profile?.cost === "number" && profile.cost > policy.maxCost) {
|
|
3924
|
+
return false;
|
|
3925
|
+
}
|
|
3926
|
+
if (typeof policy?.maxLatencyMs === "number" && typeof profile?.latencyMs === "number" && profile.latencyMs > policy.maxLatencyMs) {
|
|
3927
|
+
return false;
|
|
3928
|
+
}
|
|
3929
|
+
if (typeof policy?.minQuality === "number" && typeof profile?.quality === "number" && profile.quality < policy.minQuality) {
|
|
3930
|
+
return false;
|
|
3931
|
+
}
|
|
3932
|
+
return true;
|
|
3933
|
+
};
|
|
3934
|
+
const getBalancedScore = (provider) => {
|
|
3935
|
+
const profile = options.providerProfiles?.[provider];
|
|
3936
|
+
if (policy?.scoreProvider) {
|
|
3937
|
+
return policy.scoreProvider(provider, profile);
|
|
3938
|
+
}
|
|
3939
|
+
const weights = policy?.weights ?? {};
|
|
3940
|
+
return (profile?.cost ?? Number.MAX_SAFE_INTEGER) * (weights.cost ?? 1) + (profile?.latencyMs ?? Number.MAX_SAFE_INTEGER) * (weights.latencyMs ?? 0.005) + (profile?.priority ?? 0) * (weights.priority ?? 1) - (profile?.quality ?? 0) * (weights.quality ?? 10);
|
|
3941
|
+
};
|
|
3942
|
+
const sortProviders = (providers) => {
|
|
3943
|
+
if (strategy !== "prefer-cheapest" && strategy !== "prefer-fastest" && strategy !== "quality-first" && strategy !== "balanced") {
|
|
3944
|
+
return providers;
|
|
3945
|
+
}
|
|
3946
|
+
return [...providers].sort((left, right) => {
|
|
3947
|
+
const leftProfile = options.providerProfiles?.[left];
|
|
3948
|
+
const rightProfile = options.providerProfiles?.[right];
|
|
3949
|
+
if (strategy === "quality-first") {
|
|
3950
|
+
return (rightProfile?.quality ?? Number.MIN_SAFE_INTEGER) - (leftProfile?.quality ?? Number.MIN_SAFE_INTEGER) || (leftProfile?.priority ?? Number.MAX_SAFE_INTEGER) - (rightProfile?.priority ?? Number.MAX_SAFE_INTEGER) || (leftProfile?.latencyMs ?? Number.MAX_SAFE_INTEGER) - (rightProfile?.latencyMs ?? Number.MAX_SAFE_INTEGER) || (leftProfile?.cost ?? Number.MAX_SAFE_INTEGER) - (rightProfile?.cost ?? Number.MAX_SAFE_INTEGER);
|
|
3951
|
+
}
|
|
3952
|
+
if (strategy === "balanced") {
|
|
3953
|
+
return getBalancedScore(left) - getBalancedScore(right);
|
|
3954
|
+
}
|
|
3955
|
+
const leftValue = strategy === "prefer-cheapest" ? leftProfile?.cost ?? Number.MAX_SAFE_INTEGER : leftProfile?.latencyMs ?? Number.MAX_SAFE_INTEGER;
|
|
3956
|
+
const rightValue = strategy === "prefer-cheapest" ? rightProfile?.cost ?? Number.MAX_SAFE_INTEGER : rightProfile?.latencyMs ?? Number.MAX_SAFE_INTEGER;
|
|
3957
|
+
return leftValue - rightValue || (leftProfile?.priority ?? Number.MAX_SAFE_INTEGER) - (rightProfile?.priority ?? Number.MAX_SAFE_INTEGER);
|
|
3958
|
+
});
|
|
3959
|
+
};
|
|
3960
|
+
const resolveOrder = async (input) => {
|
|
3961
|
+
const selectedProvider = await options.selectProvider?.(input);
|
|
3962
|
+
const allowedProviders = await resolveAllowedProviders(input);
|
|
3963
|
+
const fallbackOrder = typeof options.fallback === "function" ? await options.fallback(input) : options.fallback;
|
|
3964
|
+
const allowedRankedProviders = sortProviders([
|
|
3965
|
+
...fallbackOrder ?? providerIds
|
|
3966
|
+
]).filter((provider) => allowedProviders.has(provider));
|
|
3967
|
+
const rankedProviders = allowedRankedProviders.filter(passesBudgetFilters);
|
|
3968
|
+
const healthyRankedProviders = healthOptions ? rankedProviders.filter((provider) => !isSuppressed(provider)) : rankedProviders;
|
|
3969
|
+
const candidateRankedProviders = healthyRankedProviders.length ? healthyRankedProviders : rankedProviders;
|
|
3970
|
+
const preferred = selectedProvider && allowedProviders.has(selectedProvider) && passesBudgetFilters(selectedProvider) && (!healthOptions || !isSuppressed(selectedProvider)) ? selectedProvider : candidateRankedProviders[0] ?? firstProvider;
|
|
3971
|
+
const seen = new Set;
|
|
3972
|
+
const order = [];
|
|
3973
|
+
const candidates = strategy === "ordered" ? candidateRankedProviders : [
|
|
3974
|
+
preferred,
|
|
3975
|
+
...candidateRankedProviders,
|
|
3976
|
+
...providerIds.filter((provider) => !healthOptions || !isSuppressed(provider))
|
|
3977
|
+
];
|
|
3978
|
+
for (const provider of candidates) {
|
|
3979
|
+
if (!provider || seen.has(provider) || !allowedProviders.has(provider) || !options.providers[provider]) {
|
|
3980
|
+
continue;
|
|
3981
|
+
}
|
|
3982
|
+
seen.add(provider);
|
|
3983
|
+
order.push(provider);
|
|
3984
|
+
}
|
|
3985
|
+
return {
|
|
3986
|
+
order,
|
|
3987
|
+
selectedProvider: preferred
|
|
3988
|
+
};
|
|
3989
|
+
};
|
|
3990
|
+
const emit = async (event, input) => {
|
|
3991
|
+
await options.onProviderEvent?.(event, input);
|
|
3992
|
+
};
|
|
3993
|
+
const runProvider = async (provider, model, input) => {
|
|
3994
|
+
const timeoutMs = getProviderTimeoutMs(provider);
|
|
3995
|
+
if (!timeoutMs) {
|
|
3996
|
+
return model.generate(input);
|
|
3997
|
+
}
|
|
3998
|
+
let timeout;
|
|
3999
|
+
try {
|
|
4000
|
+
return await Promise.race([
|
|
4001
|
+
model.generate(input),
|
|
4002
|
+
new Promise((_, reject) => {
|
|
4003
|
+
timeout = setTimeout(() => reject(new VoiceProviderTimeoutError(provider, timeoutMs)), timeoutMs);
|
|
4004
|
+
})
|
|
4005
|
+
]);
|
|
4006
|
+
} finally {
|
|
4007
|
+
if (timeout) {
|
|
4008
|
+
clearTimeout(timeout);
|
|
4009
|
+
}
|
|
4010
|
+
}
|
|
4011
|
+
};
|
|
4012
|
+
return {
|
|
4013
|
+
generate: async (input) => {
|
|
4014
|
+
const { order, selectedProvider } = await resolveOrder(input);
|
|
4015
|
+
if (!selectedProvider || order.length === 0) {
|
|
4016
|
+
throw new Error("Voice provider router has no available providers.");
|
|
4017
|
+
}
|
|
4018
|
+
let lastError;
|
|
4019
|
+
for (const [index, provider] of order.entries()) {
|
|
4020
|
+
const model = options.providers[provider];
|
|
4021
|
+
if (!model) {
|
|
4022
|
+
continue;
|
|
4023
|
+
}
|
|
4024
|
+
const startedAt = Date.now();
|
|
4025
|
+
try {
|
|
4026
|
+
const output = await runProvider(provider, model, input);
|
|
4027
|
+
const providerHealth = recordProviderSuccess(provider);
|
|
4028
|
+
await emit({
|
|
4029
|
+
at: Date.now(),
|
|
4030
|
+
attempt: index + 1,
|
|
4031
|
+
elapsedMs: Date.now() - startedAt,
|
|
4032
|
+
fallbackProvider: provider === selectedProvider ? undefined : provider,
|
|
4033
|
+
latencyBudgetMs: getProviderTimeoutMs(provider),
|
|
4034
|
+
provider,
|
|
4035
|
+
providerHealth,
|
|
4036
|
+
recovered: provider !== selectedProvider,
|
|
4037
|
+
selectedProvider,
|
|
4038
|
+
status: provider === selectedProvider ? "success" : "fallback"
|
|
4039
|
+
}, input);
|
|
4040
|
+
return output;
|
|
4041
|
+
} catch (error) {
|
|
4042
|
+
lastError = error;
|
|
4043
|
+
const hasNextProvider = index < order.length - 1;
|
|
4044
|
+
const isProviderError = options.isProviderError?.(error, provider) ?? true;
|
|
4045
|
+
const timedOut = options.isTimeoutError?.(error, provider) ?? error instanceof VoiceProviderTimeoutError;
|
|
4046
|
+
const rateLimited = options.isRateLimitError?.(error, provider) ?? defaultIsRateLimitError(error);
|
|
4047
|
+
const shouldFallback = fallbackMode === "provider-error" ? isProviderError : fallbackMode === "rate-limit" ? isProviderError && rateLimited : false;
|
|
4048
|
+
const providerHealth = recordProviderError(provider, isProviderError, rateLimited);
|
|
4049
|
+
const nextProvider = hasNextProvider ? order[index + 1] : undefined;
|
|
4050
|
+
await emit({
|
|
4051
|
+
at: Date.now(),
|
|
4052
|
+
attempt: index + 1,
|
|
4053
|
+
elapsedMs: Date.now() - startedAt,
|
|
4054
|
+
error: errorMessage(error),
|
|
4055
|
+
fallbackProvider: shouldFallback ? nextProvider : undefined,
|
|
4056
|
+
latencyBudgetMs: getProviderTimeoutMs(provider),
|
|
4057
|
+
provider,
|
|
4058
|
+
providerHealth,
|
|
4059
|
+
rateLimited,
|
|
4060
|
+
selectedProvider,
|
|
4061
|
+
suppressionRemainingMs: getSuppressionRemainingMs(provider),
|
|
4062
|
+
suppressedUntil: providerHealth?.suppressedUntil,
|
|
4063
|
+
status: "error",
|
|
4064
|
+
timedOut
|
|
4065
|
+
}, input);
|
|
4066
|
+
if (!hasNextProvider || !shouldFallback) {
|
|
4067
|
+
throw error;
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
}
|
|
4071
|
+
throw lastError ?? new Error("Voice provider router did not run a provider.");
|
|
4072
|
+
}
|
|
4073
|
+
};
|
|
4074
|
+
};
|
|
4075
|
+
var messageToOpenAIInput = (message) => {
|
|
4076
|
+
if (message.role === "tool") {
|
|
4077
|
+
return [
|
|
4078
|
+
{
|
|
4079
|
+
call_id: message.toolCallId ?? message.name ?? crypto.randomUUID(),
|
|
4080
|
+
output: message.content,
|
|
4081
|
+
type: "function_call_output"
|
|
4082
|
+
}
|
|
4083
|
+
];
|
|
4084
|
+
}
|
|
4085
|
+
const toolCalls = getMessageToolCalls(message);
|
|
4086
|
+
if (message.role === "assistant" && toolCalls.length) {
|
|
4087
|
+
return toolCalls.map((toolCall) => ({
|
|
4088
|
+
arguments: JSON.stringify(toolCall.args),
|
|
4089
|
+
call_id: toolCall.id ?? crypto.randomUUID(),
|
|
4090
|
+
name: toolCall.name,
|
|
4091
|
+
type: "function_call"
|
|
4092
|
+
}));
|
|
4093
|
+
}
|
|
4094
|
+
return [
|
|
4095
|
+
{
|
|
4096
|
+
content: message.content,
|
|
4097
|
+
role: message.role === "system" ? "developer" : message.role
|
|
4098
|
+
}
|
|
4099
|
+
];
|
|
4100
|
+
};
|
|
4101
|
+
var messagesToOpenAIInput = (messages) => messages.flatMap(messageToOpenAIInput);
|
|
4102
|
+
var messageToAnthropicMessage = (message) => {
|
|
4103
|
+
if (message.role === "system") {
|
|
4104
|
+
return;
|
|
4105
|
+
}
|
|
4106
|
+
if (message.role === "tool") {
|
|
4107
|
+
if (!message.toolCallId) {
|
|
4108
|
+
return {
|
|
4109
|
+
content: `Tool result from ${message.name ?? "tool"}: ${message.content}`,
|
|
4110
|
+
role: "user"
|
|
4111
|
+
};
|
|
4112
|
+
}
|
|
4113
|
+
return {
|
|
4114
|
+
content: [
|
|
4115
|
+
{
|
|
4116
|
+
content: message.content,
|
|
4117
|
+
tool_use_id: message.toolCallId,
|
|
4118
|
+
type: "tool_result"
|
|
4119
|
+
}
|
|
4120
|
+
],
|
|
4121
|
+
role: "user"
|
|
4122
|
+
};
|
|
4123
|
+
}
|
|
4124
|
+
const toolCalls = getMessageToolCalls(message);
|
|
4125
|
+
if (message.role === "assistant" && toolCalls.length) {
|
|
4126
|
+
return {
|
|
4127
|
+
content: [
|
|
4128
|
+
...message.content ? [
|
|
4129
|
+
{
|
|
4130
|
+
text: message.content,
|
|
4131
|
+
type: "text"
|
|
4132
|
+
}
|
|
4133
|
+
] : [],
|
|
4134
|
+
...toolCalls.map((toolCall) => ({
|
|
4135
|
+
id: toolCall.id ?? crypto.randomUUID(),
|
|
4136
|
+
input: toolCall.args,
|
|
4137
|
+
name: toolCall.name,
|
|
4138
|
+
type: "tool_use"
|
|
4139
|
+
}))
|
|
4140
|
+
],
|
|
4141
|
+
role: "assistant"
|
|
4142
|
+
};
|
|
4143
|
+
}
|
|
4144
|
+
return {
|
|
4145
|
+
content: message.content,
|
|
4146
|
+
role: message.role
|
|
4147
|
+
};
|
|
4148
|
+
};
|
|
4149
|
+
var toGeminiSchema = (schema) => {
|
|
4150
|
+
const next = {};
|
|
4151
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
4152
|
+
if (key === "additionalProperties") {
|
|
4153
|
+
continue;
|
|
4154
|
+
}
|
|
4155
|
+
if (key === "type" && typeof value === "string") {
|
|
4156
|
+
next[key] = value.toUpperCase();
|
|
4157
|
+
continue;
|
|
4158
|
+
}
|
|
4159
|
+
if (Array.isArray(value)) {
|
|
4160
|
+
next[key] = value.map((item) => item && typeof item === "object" ? toGeminiSchema(item) : item);
|
|
4161
|
+
continue;
|
|
4162
|
+
}
|
|
4163
|
+
if (value && typeof value === "object") {
|
|
4164
|
+
next[key] = toGeminiSchema(value);
|
|
4165
|
+
continue;
|
|
4166
|
+
}
|
|
4167
|
+
next[key] = value;
|
|
4168
|
+
}
|
|
4169
|
+
return next;
|
|
4170
|
+
};
|
|
4171
|
+
var messageToGeminiContent = (message) => {
|
|
4172
|
+
if (message.role === "system") {
|
|
4173
|
+
return;
|
|
4174
|
+
}
|
|
4175
|
+
if (message.role === "tool") {
|
|
4176
|
+
return {
|
|
4177
|
+
parts: [
|
|
4178
|
+
{
|
|
4179
|
+
functionResponse: {
|
|
4180
|
+
id: message.toolCallId,
|
|
4181
|
+
name: message.name ?? "tool",
|
|
4182
|
+
response: {
|
|
4183
|
+
result: parseJSONValue(message.content)
|
|
4184
|
+
}
|
|
4185
|
+
}
|
|
4186
|
+
}
|
|
4187
|
+
],
|
|
4188
|
+
role: "user"
|
|
4189
|
+
};
|
|
4190
|
+
}
|
|
4191
|
+
const toolCalls = getMessageToolCalls(message);
|
|
4192
|
+
if (message.role === "assistant" && toolCalls.length) {
|
|
4193
|
+
return {
|
|
4194
|
+
parts: [
|
|
4195
|
+
...message.content ? [
|
|
4196
|
+
{
|
|
4197
|
+
text: message.content
|
|
4198
|
+
}
|
|
4199
|
+
] : [],
|
|
4200
|
+
...toolCalls.map((toolCall) => ({
|
|
4201
|
+
functionCall: {
|
|
4202
|
+
args: toolCall.args,
|
|
4203
|
+
id: toolCall.id,
|
|
4204
|
+
name: toolCall.name
|
|
4205
|
+
}
|
|
4206
|
+
}))
|
|
4207
|
+
],
|
|
4208
|
+
role: "model"
|
|
4209
|
+
};
|
|
4210
|
+
}
|
|
4211
|
+
return {
|
|
4212
|
+
parts: [
|
|
4213
|
+
{
|
|
4214
|
+
text: message.content
|
|
4215
|
+
}
|
|
4216
|
+
],
|
|
4217
|
+
role: message.role === "assistant" ? "model" : "user"
|
|
4218
|
+
};
|
|
4219
|
+
};
|
|
4220
|
+
var extractText = (response) => {
|
|
4221
|
+
if (typeof response.output_text === "string") {
|
|
4222
|
+
return response.output_text;
|
|
4223
|
+
}
|
|
4224
|
+
const output = Array.isArray(response.output) ? response.output : [];
|
|
4225
|
+
for (const item of output) {
|
|
4226
|
+
if (!item || typeof item !== "object") {
|
|
4227
|
+
continue;
|
|
4228
|
+
}
|
|
4229
|
+
const record = item;
|
|
4230
|
+
const content = Array.isArray(record.content) ? record.content : [];
|
|
4231
|
+
for (const contentItem of content) {
|
|
4232
|
+
if (!contentItem || typeof contentItem !== "object") {
|
|
4233
|
+
continue;
|
|
4234
|
+
}
|
|
4235
|
+
const contentRecord = contentItem;
|
|
4236
|
+
if (typeof contentRecord.text === "string") {
|
|
4237
|
+
return contentRecord.text;
|
|
4238
|
+
}
|
|
4239
|
+
}
|
|
4240
|
+
}
|
|
4241
|
+
return "";
|
|
4242
|
+
};
|
|
4243
|
+
var extractToolCalls = (response) => {
|
|
4244
|
+
const output = Array.isArray(response.output) ? response.output : [];
|
|
4245
|
+
const toolCalls = [];
|
|
4246
|
+
for (const item of output) {
|
|
4247
|
+
if (!item || typeof item !== "object") {
|
|
4248
|
+
continue;
|
|
4249
|
+
}
|
|
4250
|
+
const record = item;
|
|
4251
|
+
if (record.type !== "function_call" || typeof record.name !== "string") {
|
|
4252
|
+
continue;
|
|
4253
|
+
}
|
|
4254
|
+
const args = typeof record.arguments === "string" ? parseJSON(record.arguments) : {};
|
|
4255
|
+
toolCalls.push({
|
|
4256
|
+
args,
|
|
4257
|
+
id: typeof record.call_id === "string" ? record.call_id : typeof record.id === "string" ? record.id : undefined,
|
|
4258
|
+
name: record.name
|
|
4259
|
+
});
|
|
4260
|
+
}
|
|
4261
|
+
return toolCalls;
|
|
4262
|
+
};
|
|
4263
|
+
var createOpenAIVoiceAssistantModel = (options) => {
|
|
4264
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
4265
|
+
const baseUrl = options.baseUrl ?? "https://api.openai.com/v1";
|
|
4266
|
+
const model = options.model ?? "gpt-4.1-mini";
|
|
4267
|
+
return {
|
|
4268
|
+
generate: async (input) => {
|
|
4269
|
+
const response = await fetchImpl(`${baseUrl.replace(/\/$/, "")}/responses`, {
|
|
4270
|
+
body: JSON.stringify({
|
|
4271
|
+
input: messagesToOpenAIInput(input.messages),
|
|
4272
|
+
instructions: [
|
|
4273
|
+
input.system,
|
|
4274
|
+
"Return a JSON object with assistantText, complete, transfer, escalate, voicemail, noAnswer, and result when you are not calling tools."
|
|
4275
|
+
].filter(Boolean).join(`
|
|
4276
|
+
|
|
4277
|
+
`),
|
|
4278
|
+
max_output_tokens: options.maxOutputTokens,
|
|
4279
|
+
model,
|
|
4280
|
+
temperature: options.temperature,
|
|
4281
|
+
text: {
|
|
4282
|
+
format: {
|
|
4283
|
+
name: "voice_route_result",
|
|
4284
|
+
schema: OUTPUT_SCHEMA,
|
|
4285
|
+
strict: false,
|
|
4286
|
+
type: "json_schema"
|
|
4287
|
+
}
|
|
4288
|
+
},
|
|
4289
|
+
tool_choice: input.tools.length ? "auto" : "none",
|
|
4290
|
+
tools: input.tools.map((tool) => ({
|
|
4291
|
+
description: tool.description,
|
|
4292
|
+
name: tool.name,
|
|
4293
|
+
parameters: tool.parameters ?? {
|
|
4294
|
+
additionalProperties: true,
|
|
4295
|
+
type: "object"
|
|
4296
|
+
},
|
|
4297
|
+
strict: false,
|
|
4298
|
+
type: "function"
|
|
4299
|
+
}))
|
|
4300
|
+
}),
|
|
4301
|
+
headers: {
|
|
4302
|
+
authorization: `Bearer ${options.apiKey}`,
|
|
4303
|
+
"content-type": "application/json"
|
|
4304
|
+
},
|
|
4305
|
+
method: "POST"
|
|
4306
|
+
});
|
|
4307
|
+
if (!response.ok) {
|
|
4308
|
+
throw createHTTPError("OpenAI", response);
|
|
4309
|
+
}
|
|
4310
|
+
const body = await response.json();
|
|
4311
|
+
if (body.usage && typeof body.usage === "object") {
|
|
4312
|
+
await options.onUsage?.(body.usage);
|
|
4313
|
+
}
|
|
4314
|
+
const toolCalls = extractToolCalls(body);
|
|
4315
|
+
if (toolCalls.length) {
|
|
4316
|
+
return {
|
|
4317
|
+
toolCalls
|
|
4318
|
+
};
|
|
4319
|
+
}
|
|
4320
|
+
return normalizeRouteOutput(parseJSON(extractText(body)));
|
|
4321
|
+
}
|
|
4322
|
+
};
|
|
4323
|
+
};
|
|
4324
|
+
var extractAnthropicText = (response) => {
|
|
4325
|
+
const content = Array.isArray(response.content) ? response.content : [];
|
|
4326
|
+
return content.map((item) => item && typeof item === "object" && item.type === "text" && typeof item.text === "string" ? item.text : "").filter(Boolean).join(`
|
|
4327
|
+
`);
|
|
4328
|
+
};
|
|
4329
|
+
var extractAnthropicToolCalls = (response) => {
|
|
4330
|
+
const content = Array.isArray(response.content) ? response.content : [];
|
|
4331
|
+
const toolCalls = [];
|
|
4332
|
+
for (const item of content) {
|
|
4333
|
+
if (!item || typeof item !== "object") {
|
|
4334
|
+
continue;
|
|
4335
|
+
}
|
|
4336
|
+
const record = item;
|
|
4337
|
+
if (record.type !== "tool_use" || typeof record.name !== "string") {
|
|
4338
|
+
continue;
|
|
4339
|
+
}
|
|
4340
|
+
toolCalls.push({
|
|
4341
|
+
args: record.input && typeof record.input === "object" ? record.input : {},
|
|
4342
|
+
id: typeof record.id === "string" ? record.id : undefined,
|
|
4343
|
+
name: record.name
|
|
4344
|
+
});
|
|
4345
|
+
}
|
|
4346
|
+
return toolCalls;
|
|
4347
|
+
};
|
|
4348
|
+
var createAnthropicVoiceAssistantModel = (options) => {
|
|
4349
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
4350
|
+
const baseUrl = options.baseUrl ?? "https://api.anthropic.com/v1";
|
|
4351
|
+
const model = options.model ?? "claude-sonnet-4-5";
|
|
4352
|
+
return {
|
|
4353
|
+
generate: async (input) => {
|
|
4354
|
+
const response = await fetchImpl(`${baseUrl.replace(/\/$/, "")}/messages`, {
|
|
4355
|
+
body: JSON.stringify({
|
|
4356
|
+
max_tokens: options.maxOutputTokens ?? 1024,
|
|
4357
|
+
messages: input.messages.map(messageToAnthropicMessage).filter(Boolean),
|
|
4358
|
+
model,
|
|
4359
|
+
system: [input.system, ROUTE_RESULT_INSTRUCTION].filter(Boolean).join(`
|
|
4360
|
+
|
|
4361
|
+
`),
|
|
4362
|
+
temperature: options.temperature,
|
|
4363
|
+
tool_choice: input.tools.length ? { type: "auto" } : { type: "none" },
|
|
4364
|
+
tools: input.tools.map((tool) => ({
|
|
4365
|
+
description: tool.description,
|
|
4366
|
+
input_schema: tool.parameters ?? {
|
|
4367
|
+
additionalProperties: true,
|
|
4368
|
+
type: "object"
|
|
4369
|
+
},
|
|
4370
|
+
name: tool.name
|
|
4371
|
+
}))
|
|
4372
|
+
}),
|
|
4373
|
+
headers: {
|
|
4374
|
+
"anthropic-version": options.version ?? "2023-06-01",
|
|
4375
|
+
"content-type": "application/json",
|
|
4376
|
+
"x-api-key": options.apiKey
|
|
4377
|
+
},
|
|
4378
|
+
method: "POST"
|
|
4379
|
+
});
|
|
4380
|
+
if (!response.ok) {
|
|
4381
|
+
throw createHTTPError("Anthropic", response);
|
|
4382
|
+
}
|
|
4383
|
+
const body = await response.json();
|
|
4384
|
+
if (body.usage && typeof body.usage === "object") {
|
|
4385
|
+
await options.onUsage?.(body.usage);
|
|
4386
|
+
}
|
|
4387
|
+
const toolCalls = extractAnthropicToolCalls(body);
|
|
4388
|
+
if (toolCalls.length) {
|
|
4389
|
+
return {
|
|
4390
|
+
assistantText: extractAnthropicText(body) || undefined,
|
|
4391
|
+
toolCalls
|
|
4392
|
+
};
|
|
4393
|
+
}
|
|
4394
|
+
return normalizeRouteOutput(parseJSON(extractAnthropicText(body)));
|
|
4395
|
+
}
|
|
4396
|
+
};
|
|
4397
|
+
};
|
|
4398
|
+
var extractGeminiCandidateParts = (response) => {
|
|
4399
|
+
const candidates = Array.isArray(response.candidates) ? response.candidates : [];
|
|
4400
|
+
const first = candidates[0];
|
|
4401
|
+
if (!first || typeof first !== "object") {
|
|
4402
|
+
return [];
|
|
4403
|
+
}
|
|
4404
|
+
const content = first.content;
|
|
4405
|
+
if (!content || typeof content !== "object") {
|
|
4406
|
+
return [];
|
|
4407
|
+
}
|
|
4408
|
+
const parts = content.parts;
|
|
4409
|
+
return Array.isArray(parts) ? parts : [];
|
|
4410
|
+
};
|
|
4411
|
+
var extractGeminiText = (response) => extractGeminiCandidateParts(response).map((part) => part && typeof part === "object" && typeof part.text === "string" ? part.text : "").filter(Boolean).join(`
|
|
4412
|
+
`);
|
|
4413
|
+
var extractGeminiToolCalls = (response) => {
|
|
4414
|
+
const toolCalls = [];
|
|
4415
|
+
for (const part of extractGeminiCandidateParts(response)) {
|
|
4416
|
+
if (!part || typeof part !== "object") {
|
|
4417
|
+
continue;
|
|
4418
|
+
}
|
|
4419
|
+
const functionCall = part.functionCall;
|
|
4420
|
+
if (!functionCall || typeof functionCall !== "object") {
|
|
4421
|
+
continue;
|
|
4422
|
+
}
|
|
4423
|
+
const record = functionCall;
|
|
4424
|
+
if (typeof record.name !== "string") {
|
|
4425
|
+
continue;
|
|
4426
|
+
}
|
|
4427
|
+
toolCalls.push({
|
|
4428
|
+
args: record.args && typeof record.args === "object" ? record.args : {},
|
|
4429
|
+
id: typeof record.id === "string" ? record.id : undefined,
|
|
4430
|
+
name: record.name
|
|
4431
|
+
});
|
|
4432
|
+
}
|
|
4433
|
+
return toolCalls;
|
|
4434
|
+
};
|
|
4435
|
+
var createGeminiVoiceAssistantModel = (options) => {
|
|
4436
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
4437
|
+
const baseUrl = options.baseUrl ?? "https://generativelanguage.googleapis.com/v1beta";
|
|
4438
|
+
const model = options.model ?? "gemini-2.5-flash";
|
|
4439
|
+
const maxRetries = Math.max(0, options.maxRetries ?? 2);
|
|
4440
|
+
return {
|
|
4441
|
+
generate: async (input) => {
|
|
4442
|
+
const endpoint = `${baseUrl.replace(/\/$/, "")}/models/${encodeURIComponent(model)}:generateContent?key=${encodeURIComponent(options.apiKey)}`;
|
|
4443
|
+
let response;
|
|
4444
|
+
for (let attempt = 0;attempt <= maxRetries; attempt += 1) {
|
|
4445
|
+
response = await fetchImpl(endpoint, {
|
|
4446
|
+
body: JSON.stringify({
|
|
4447
|
+
contents: input.messages.map(messageToGeminiContent).filter(Boolean),
|
|
4448
|
+
generationConfig: {
|
|
4449
|
+
maxOutputTokens: options.maxOutputTokens,
|
|
4450
|
+
...input.tools.length ? {} : {
|
|
4451
|
+
responseMimeType: "application/json",
|
|
4452
|
+
responseSchema: toGeminiSchema(OUTPUT_SCHEMA)
|
|
4453
|
+
},
|
|
4454
|
+
temperature: options.temperature
|
|
4455
|
+
},
|
|
4456
|
+
systemInstruction: {
|
|
4457
|
+
parts: [
|
|
4458
|
+
{
|
|
4459
|
+
text: [input.system, ROUTE_RESULT_INSTRUCTION].filter(Boolean).join(`
|
|
4460
|
+
|
|
4461
|
+
`)
|
|
4462
|
+
}
|
|
4463
|
+
]
|
|
4464
|
+
},
|
|
4465
|
+
tools: input.tools.length ? [
|
|
4466
|
+
{
|
|
4467
|
+
functionDeclarations: input.tools.map((tool) => ({
|
|
4468
|
+
description: tool.description,
|
|
4469
|
+
name: tool.name,
|
|
4470
|
+
parameters: toGeminiSchema(tool.parameters ?? {
|
|
4471
|
+
additionalProperties: true,
|
|
4472
|
+
type: "object"
|
|
4473
|
+
})
|
|
4474
|
+
}))
|
|
4475
|
+
}
|
|
4476
|
+
] : undefined
|
|
4477
|
+
}),
|
|
4478
|
+
headers: {
|
|
4479
|
+
"content-type": "application/json"
|
|
4480
|
+
},
|
|
4481
|
+
method: "POST"
|
|
4482
|
+
});
|
|
4483
|
+
if (response.ok || response.status !== 429 && response.status < 500 || attempt === maxRetries) {
|
|
4484
|
+
break;
|
|
4485
|
+
}
|
|
4486
|
+
const retryAfter = Number(response.headers.get("retry-after"));
|
|
4487
|
+
await sleep(Number.isFinite(retryAfter) && retryAfter > 0 ? retryAfter * 1000 : 500 * 2 ** attempt);
|
|
4488
|
+
}
|
|
4489
|
+
if (!response) {
|
|
4490
|
+
throw new Error("Gemini voice assistant model failed: no response");
|
|
4491
|
+
}
|
|
4492
|
+
if (!response.ok) {
|
|
4493
|
+
throw createHTTPError("Gemini", response);
|
|
4494
|
+
}
|
|
4495
|
+
const body = await response.json();
|
|
4496
|
+
if (body.usageMetadata && typeof body.usageMetadata === "object") {
|
|
4497
|
+
await options.onUsage?.(body.usageMetadata);
|
|
4498
|
+
}
|
|
4499
|
+
const toolCalls = extractGeminiToolCalls(body);
|
|
4500
|
+
if (toolCalls.length) {
|
|
4501
|
+
return {
|
|
4502
|
+
assistantText: extractGeminiText(body) || undefined,
|
|
4503
|
+
toolCalls
|
|
4504
|
+
};
|
|
4505
|
+
}
|
|
4506
|
+
return normalizeRouteOutput(parseJSON(extractGeminiText(body)));
|
|
4507
|
+
}
|
|
4508
|
+
};
|
|
4509
|
+
};
|
|
4510
|
+
|
|
3471
4511
|
// src/store.ts
|
|
3472
4512
|
var createId = () => crypto.randomUUID();
|
|
3473
4513
|
var createVoiceSessionRecord = (id, scenarioId) => ({
|
|
@@ -3508,6 +4548,118 @@ var toVoiceSessionSummary = (session) => ({
|
|
|
3508
4548
|
turnCount: session.turns.length
|
|
3509
4549
|
});
|
|
3510
4550
|
|
|
4551
|
+
// src/testing/providerSimulator.ts
|
|
4552
|
+
var getContextQuery = (context) => context.query;
|
|
4553
|
+
var titleCaseProvider = (provider) => provider.split(/[-_\s]+/).filter(Boolean).map((part) => part[0]?.toUpperCase() + part.slice(1)).join(" ");
|
|
4554
|
+
var resolveRequestedProvider = (context, providers) => {
|
|
4555
|
+
const provider = getContextQuery(context).provider;
|
|
4556
|
+
return providers.includes(provider) ? provider : providers[0];
|
|
4557
|
+
};
|
|
4558
|
+
var createVoiceProviderFailureSimulator = (options) => {
|
|
4559
|
+
if (options.providers.length === 0) {
|
|
4560
|
+
throw new Error("At least one provider is required.");
|
|
4561
|
+
}
|
|
4562
|
+
const providerModels = Object.fromEntries(options.providers.map((provider) => [
|
|
4563
|
+
provider,
|
|
4564
|
+
{
|
|
4565
|
+
generate: async (input) => {
|
|
4566
|
+
const query = getContextQuery(input.context);
|
|
4567
|
+
if (provider === query.simulateFailureProvider) {
|
|
4568
|
+
const label = options.providerLabel?.(provider) ?? titleCaseProvider(provider);
|
|
4569
|
+
throw new Error(`${label} voice assistant model failed: HTTP 429`);
|
|
4570
|
+
}
|
|
4571
|
+
if (options.response) {
|
|
4572
|
+
return options.response({
|
|
4573
|
+
...input,
|
|
4574
|
+
mode: query.recoverProvider === provider ? "recovery" : "failure",
|
|
4575
|
+
provider
|
|
4576
|
+
});
|
|
4577
|
+
}
|
|
4578
|
+
return {
|
|
4579
|
+
assistantText: `Simulated ${provider} provider recovered.`
|
|
4580
|
+
};
|
|
4581
|
+
}
|
|
4582
|
+
}
|
|
4583
|
+
]));
|
|
4584
|
+
const router = createVoiceProviderRouter({
|
|
4585
|
+
allowProviders: async (input) => {
|
|
4586
|
+
const recoverProvider = getContextQuery(input.context).recoverProvider;
|
|
4587
|
+
if (recoverProvider) {
|
|
4588
|
+
return [recoverProvider];
|
|
4589
|
+
}
|
|
4590
|
+
if (typeof options.allowProviders === "function") {
|
|
4591
|
+
return options.allowProviders(input);
|
|
4592
|
+
}
|
|
4593
|
+
return options.allowProviders ?? options.providers;
|
|
4594
|
+
},
|
|
4595
|
+
fallback: async (input) => {
|
|
4596
|
+
const selectedProvider = resolveRequestedProvider(input.context, options.providers);
|
|
4597
|
+
if (typeof options.fallback === "function") {
|
|
4598
|
+
return options.fallback(selectedProvider, input);
|
|
4599
|
+
}
|
|
4600
|
+
return options.fallback ?? options.providers.filter((provider) => provider !== selectedProvider);
|
|
4601
|
+
},
|
|
4602
|
+
fallbackMode: "provider-error",
|
|
4603
|
+
isProviderError: options.isProviderError,
|
|
4604
|
+
isRateLimitError: options.isRateLimitError,
|
|
4605
|
+
onProviderEvent: options.onProviderEvent,
|
|
4606
|
+
policy: "prefer-selected",
|
|
4607
|
+
providerHealth: options.providerHealth ?? {
|
|
4608
|
+
cooldownMs: 30000,
|
|
4609
|
+
failureThreshold: 1,
|
|
4610
|
+
rateLimitCooldownMs: 120000
|
|
4611
|
+
},
|
|
4612
|
+
providers: providerModels,
|
|
4613
|
+
selectProvider: ({ context }) => resolveRequestedProvider(context, options.providers)
|
|
4614
|
+
});
|
|
4615
|
+
const run = async (provider, mode) => {
|
|
4616
|
+
const now = Date.now();
|
|
4617
|
+
const session = createVoiceSessionRecord(`provider-sim-${now}`, "provider-simulation");
|
|
4618
|
+
const turn = {
|
|
4619
|
+
committedAt: now,
|
|
4620
|
+
id: `provider-sim-turn-${now}`,
|
|
4621
|
+
text: mode === "failure" ? `Simulate ${provider} provider failure.` : `Simulate ${provider} provider recovery.`,
|
|
4622
|
+
transcripts: []
|
|
4623
|
+
};
|
|
4624
|
+
const context = {
|
|
4625
|
+
query: {
|
|
4626
|
+
provider,
|
|
4627
|
+
...mode === "recovery" ? { recoverProvider: provider } : {},
|
|
4628
|
+
...mode === "failure" ? { simulateFailureProvider: provider } : {}
|
|
4629
|
+
}
|
|
4630
|
+
};
|
|
4631
|
+
const result = await router.generate({
|
|
4632
|
+
agentId: "provider-simulator",
|
|
4633
|
+
context,
|
|
4634
|
+
messages: [
|
|
4635
|
+
{
|
|
4636
|
+
content: turn.text,
|
|
4637
|
+
role: "user"
|
|
4638
|
+
}
|
|
4639
|
+
],
|
|
4640
|
+
session,
|
|
4641
|
+
system: "Simulate provider routing without calling external APIs.",
|
|
4642
|
+
tools: [],
|
|
4643
|
+
turn
|
|
4644
|
+
});
|
|
4645
|
+
return {
|
|
4646
|
+
mode,
|
|
4647
|
+
provider,
|
|
4648
|
+
replayHref: options.replayHref === false ? undefined : typeof options.replayHref === "function" ? options.replayHref({
|
|
4649
|
+
provider,
|
|
4650
|
+
sessionId: session.id,
|
|
4651
|
+
turnId: turn.id
|
|
4652
|
+
}) : `${options.replayHref ?? "/api/voice-sessions"}/${encodeURIComponent(session.id)}/replay/htmx`,
|
|
4653
|
+
result,
|
|
4654
|
+
sessionId: session.id,
|
|
4655
|
+
status: "simulated",
|
|
4656
|
+
turnId: turn.id
|
|
4657
|
+
};
|
|
4658
|
+
};
|
|
4659
|
+
return {
|
|
4660
|
+
run
|
|
4661
|
+
};
|
|
4662
|
+
};
|
|
3511
4663
|
// src/memoryStore.ts
|
|
3512
4664
|
var createVoiceMemoryStore = () => {
|
|
3513
4665
|
const sessions = new Map;
|
|
@@ -3533,6 +4685,289 @@ var createVoiceMemoryStore = () => {
|
|
|
3533
4685
|
// src/session.ts
|
|
3534
4686
|
import { Buffer } from "buffer";
|
|
3535
4687
|
|
|
4688
|
+
// src/handoff.ts
|
|
4689
|
+
var toHex = (bytes) => Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
4690
|
+
var signHandoffBody = async (input) => {
|
|
4691
|
+
const encoder = new TextEncoder;
|
|
4692
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(input.secret), {
|
|
4693
|
+
hash: "SHA-256",
|
|
4694
|
+
name: "HMAC"
|
|
4695
|
+
}, false, ["sign"]);
|
|
4696
|
+
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(`${input.timestamp}.${input.body}`));
|
|
4697
|
+
return `sha256=${toHex(new Uint8Array(signature))}`;
|
|
4698
|
+
};
|
|
4699
|
+
var toErrorMessage = (error) => error instanceof Error ? error.message : String(error);
|
|
4700
|
+
var createSkippedDelivery = (adapter) => ({
|
|
4701
|
+
adapterId: adapter.id,
|
|
4702
|
+
adapterKind: adapter.kind,
|
|
4703
|
+
status: "skipped"
|
|
4704
|
+
});
|
|
4705
|
+
var aggregateHandoffStatus = (deliveries) => {
|
|
4706
|
+
const statuses = Object.values(deliveries).map((delivery) => delivery.status);
|
|
4707
|
+
if (statuses.some((status) => status === "failed")) {
|
|
4708
|
+
return "failed";
|
|
4709
|
+
}
|
|
4710
|
+
if (statuses.some((status) => status === "delivered")) {
|
|
4711
|
+
return "delivered";
|
|
4712
|
+
}
|
|
4713
|
+
return "skipped";
|
|
4714
|
+
};
|
|
4715
|
+
var createHandoffDeliveryId = (input) => [
|
|
4716
|
+
"voice-handoff",
|
|
4717
|
+
input.sessionId,
|
|
4718
|
+
input.action,
|
|
4719
|
+
Date.now(),
|
|
4720
|
+
crypto.randomUUID()
|
|
4721
|
+
].join(":");
|
|
4722
|
+
var resolveHandoffDeliveryError = (deliveries) => Object.values(deliveries).map((delivery) => delivery.error).find(Boolean);
|
|
4723
|
+
var defaultWebhookBody = (input) => ({
|
|
4724
|
+
action: input.action,
|
|
4725
|
+
metadata: input.metadata,
|
|
4726
|
+
reason: input.reason,
|
|
4727
|
+
result: input.result,
|
|
4728
|
+
session: {
|
|
4729
|
+
id: input.session.id,
|
|
4730
|
+
scenarioId: input.session.scenarioId,
|
|
4731
|
+
status: input.session.status
|
|
4732
|
+
},
|
|
4733
|
+
source: "absolutejs-voice",
|
|
4734
|
+
target: input.target
|
|
4735
|
+
});
|
|
4736
|
+
var deliverVoiceHandoff = async (input) => {
|
|
4737
|
+
if (!input.config || input.config.adapters.length === 0) {
|
|
4738
|
+
return;
|
|
4739
|
+
}
|
|
4740
|
+
const deliveries = {};
|
|
4741
|
+
for (const adapter of input.config.adapters) {
|
|
4742
|
+
if (adapter.actions && !adapter.actions.includes(input.handoff.action)) {
|
|
4743
|
+
deliveries[adapter.id] = createSkippedDelivery(adapter);
|
|
4744
|
+
continue;
|
|
4745
|
+
}
|
|
4746
|
+
try {
|
|
4747
|
+
const result = await adapter.handoff(input.handoff);
|
|
4748
|
+
deliveries[adapter.id] = {
|
|
4749
|
+
...result,
|
|
4750
|
+
adapterId: adapter.id,
|
|
4751
|
+
adapterKind: adapter.kind
|
|
4752
|
+
};
|
|
4753
|
+
} catch (error) {
|
|
4754
|
+
deliveries[adapter.id] = {
|
|
4755
|
+
adapterId: adapter.id,
|
|
4756
|
+
adapterKind: adapter.kind,
|
|
4757
|
+
error: toErrorMessage(error),
|
|
4758
|
+
status: "failed"
|
|
4759
|
+
};
|
|
4760
|
+
if (input.config.failMode === "throw") {
|
|
4761
|
+
throw error;
|
|
4762
|
+
}
|
|
4763
|
+
}
|
|
4764
|
+
}
|
|
4765
|
+
return {
|
|
4766
|
+
action: input.handoff.action,
|
|
4767
|
+
deliveries,
|
|
4768
|
+
status: aggregateHandoffStatus(deliveries)
|
|
4769
|
+
};
|
|
4770
|
+
};
|
|
4771
|
+
var createVoiceHandoffDeliveryRecord = (input) => {
|
|
4772
|
+
const now = Date.now();
|
|
4773
|
+
return {
|
|
4774
|
+
action: input.action,
|
|
4775
|
+
context: input.context,
|
|
4776
|
+
createdAt: now,
|
|
4777
|
+
deliveryAttempts: 0,
|
|
4778
|
+
deliveryStatus: "pending",
|
|
4779
|
+
id: input.id ?? createHandoffDeliveryId({
|
|
4780
|
+
action: input.action,
|
|
4781
|
+
sessionId: input.session.id
|
|
4782
|
+
}),
|
|
4783
|
+
metadata: input.metadata,
|
|
4784
|
+
reason: input.reason,
|
|
4785
|
+
result: input.result,
|
|
4786
|
+
session: input.session,
|
|
4787
|
+
sessionId: input.session.id,
|
|
4788
|
+
target: input.target,
|
|
4789
|
+
updatedAt: now
|
|
4790
|
+
};
|
|
4791
|
+
};
|
|
4792
|
+
var applyVoiceHandoffDeliveryResult = (delivery, result) => ({
|
|
4793
|
+
...delivery,
|
|
4794
|
+
deliveredAt: result.status === "delivered" || result.status === "skipped" ? Date.now() : delivery.deliveredAt,
|
|
4795
|
+
deliveries: result.deliveries,
|
|
4796
|
+
deliveryAttempts: (delivery.deliveryAttempts ?? 0) + 1,
|
|
4797
|
+
deliveryError: result.status === "failed" ? resolveHandoffDeliveryError(result.deliveries) : undefined,
|
|
4798
|
+
deliveryStatus: result.status,
|
|
4799
|
+
updatedAt: Date.now()
|
|
4800
|
+
});
|
|
4801
|
+
var deliverVoiceHandoffDelivery = async (options) => {
|
|
4802
|
+
const result = await deliverVoiceHandoff({
|
|
4803
|
+
config: {
|
|
4804
|
+
adapters: options.adapters,
|
|
4805
|
+
failMode: options.failMode
|
|
4806
|
+
},
|
|
4807
|
+
handoff: {
|
|
4808
|
+
action: options.delivery.action,
|
|
4809
|
+
api: options.api,
|
|
4810
|
+
context: options.delivery.context,
|
|
4811
|
+
metadata: options.delivery.metadata,
|
|
4812
|
+
reason: options.delivery.reason,
|
|
4813
|
+
result: options.delivery.result,
|
|
4814
|
+
session: options.delivery.session,
|
|
4815
|
+
target: options.delivery.target
|
|
4816
|
+
}
|
|
4817
|
+
});
|
|
4818
|
+
return result ? applyVoiceHandoffDeliveryResult(options.delivery, result) : {
|
|
4819
|
+
...options.delivery,
|
|
4820
|
+
deliveryAttempts: (options.delivery.deliveryAttempts ?? 0) + 1,
|
|
4821
|
+
deliveryStatus: "skipped",
|
|
4822
|
+
updatedAt: Date.now()
|
|
4823
|
+
};
|
|
4824
|
+
};
|
|
4825
|
+
var createVoiceMemoryHandoffDeliveryStore = () => {
|
|
4826
|
+
const deliveries = new Map;
|
|
4827
|
+
return {
|
|
4828
|
+
get: async (id) => deliveries.get(id),
|
|
4829
|
+
list: async () => [...deliveries.values()].sort((left, right) => left.createdAt - right.createdAt || left.id.localeCompare(right.id)),
|
|
4830
|
+
remove: async (id) => {
|
|
4831
|
+
deliveries.delete(id);
|
|
4832
|
+
},
|
|
4833
|
+
set: async (id, delivery) => {
|
|
4834
|
+
deliveries.set(id, delivery);
|
|
4835
|
+
}
|
|
4836
|
+
};
|
|
4837
|
+
};
|
|
4838
|
+
var createVoiceWebhookHandoffAdapter = (options) => ({
|
|
4839
|
+
actions: options.actions,
|
|
4840
|
+
handoff: async (input) => {
|
|
4841
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
4842
|
+
if (typeof fetchImpl !== "function") {
|
|
4843
|
+
return {
|
|
4844
|
+
deliveredTo: options.url,
|
|
4845
|
+
error: "Handoff delivery failed: fetch is not available in this runtime.",
|
|
4846
|
+
status: "failed"
|
|
4847
|
+
};
|
|
4848
|
+
}
|
|
4849
|
+
const body = JSON.stringify(await options.body?.(input) ?? defaultWebhookBody(input));
|
|
4850
|
+
const headers = {
|
|
4851
|
+
"content-type": "application/json",
|
|
4852
|
+
...options.headers
|
|
4853
|
+
};
|
|
4854
|
+
if (options.signingSecret) {
|
|
4855
|
+
const timestamp = String(Date.now());
|
|
4856
|
+
headers["x-absolutejs-timestamp"] = timestamp;
|
|
4857
|
+
headers["x-absolutejs-signature"] = await signHandoffBody({
|
|
4858
|
+
body,
|
|
4859
|
+
secret: options.signingSecret,
|
|
4860
|
+
timestamp
|
|
4861
|
+
});
|
|
4862
|
+
}
|
|
4863
|
+
const controller = options.timeoutMs && options.timeoutMs > 0 ? new AbortController : undefined;
|
|
4864
|
+
const timeout = controller && options.timeoutMs ? setTimeout(() => controller.abort(), options.timeoutMs) : undefined;
|
|
4865
|
+
try {
|
|
4866
|
+
const response = await fetchImpl(options.url, {
|
|
4867
|
+
body,
|
|
4868
|
+
headers,
|
|
4869
|
+
method: options.method ?? "POST",
|
|
4870
|
+
signal: controller?.signal
|
|
4871
|
+
});
|
|
4872
|
+
if (!response.ok) {
|
|
4873
|
+
return {
|
|
4874
|
+
deliveredTo: options.url,
|
|
4875
|
+
error: `Handoff delivery failed with response ${response.status}.`,
|
|
4876
|
+
status: "failed"
|
|
4877
|
+
};
|
|
4878
|
+
}
|
|
4879
|
+
return {
|
|
4880
|
+
deliveredAt: Date.now(),
|
|
4881
|
+
deliveredTo: options.url,
|
|
4882
|
+
status: "delivered"
|
|
4883
|
+
};
|
|
4884
|
+
} finally {
|
|
4885
|
+
if (timeout) {
|
|
4886
|
+
clearTimeout(timeout);
|
|
4887
|
+
}
|
|
4888
|
+
}
|
|
4889
|
+
},
|
|
4890
|
+
id: options.id,
|
|
4891
|
+
kind: options.kind ?? "webhook"
|
|
4892
|
+
});
|
|
4893
|
+
var escapeXml = (value) => value.replaceAll("&", "&").replaceAll('"', """).replaceAll("'", "'").replaceAll("<", "<").replaceAll(">", ">");
|
|
4894
|
+
var defaultTwilioTransferTwiML = (input) => {
|
|
4895
|
+
if (!input.target) {
|
|
4896
|
+
return "<Response><Hangup /></Response>";
|
|
4897
|
+
}
|
|
4898
|
+
return `<Response><Dial>${escapeXml(input.target)}</Dial></Response>`;
|
|
4899
|
+
};
|
|
4900
|
+
var resolveTwilioCallSid = async (resolver, input) => {
|
|
4901
|
+
if (typeof resolver === "function") {
|
|
4902
|
+
return resolver(input);
|
|
4903
|
+
}
|
|
4904
|
+
if (typeof resolver === "string" && resolver.length > 0) {
|
|
4905
|
+
return resolver;
|
|
4906
|
+
}
|
|
4907
|
+
const metadataSid = typeof input.metadata?.callSid === "string" ? input.metadata.callSid : undefined;
|
|
4908
|
+
const sessionMetadata = input.session.metadata && typeof input.session.metadata === "object" ? input.session.metadata : undefined;
|
|
4909
|
+
const sessionSid = typeof sessionMetadata?.callSid === "string" ? sessionMetadata.callSid : undefined;
|
|
4910
|
+
return metadataSid ?? sessionSid;
|
|
4911
|
+
};
|
|
4912
|
+
var createVoiceTwilioRedirectHandoffAdapter = (options) => ({
|
|
4913
|
+
actions: options.actions ?? ["transfer"],
|
|
4914
|
+
handoff: async (input) => {
|
|
4915
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
4916
|
+
const callSid = await resolveTwilioCallSid(options.callSid, input);
|
|
4917
|
+
if (!callSid) {
|
|
4918
|
+
return {
|
|
4919
|
+
error: "Twilio handoff requires a callSid.",
|
|
4920
|
+
status: "failed"
|
|
4921
|
+
};
|
|
4922
|
+
}
|
|
4923
|
+
if (typeof fetchImpl !== "function") {
|
|
4924
|
+
return {
|
|
4925
|
+
error: "Twilio handoff failed: fetch is not available in this runtime.",
|
|
4926
|
+
status: "failed"
|
|
4927
|
+
};
|
|
4928
|
+
}
|
|
4929
|
+
const url = `https://api.twilio.com/2010-04-01/Accounts/${encodeURIComponent(options.accountSid)}/Calls/${encodeURIComponent(callSid)}.json`;
|
|
4930
|
+
const body = new URLSearchParams({
|
|
4931
|
+
Twiml: await (options.buildTwiML?.(input) ?? defaultTwilioTransferTwiML(input))
|
|
4932
|
+
});
|
|
4933
|
+
const auth = btoa(`${options.accountSid}:${options.authToken}`);
|
|
4934
|
+
const controller = options.timeoutMs && options.timeoutMs > 0 ? new AbortController : undefined;
|
|
4935
|
+
const timeout = controller && options.timeoutMs ? setTimeout(() => controller.abort(), options.timeoutMs) : undefined;
|
|
4936
|
+
try {
|
|
4937
|
+
const response = await fetchImpl(url, {
|
|
4938
|
+
body,
|
|
4939
|
+
headers: {
|
|
4940
|
+
authorization: `Basic ${auth}`,
|
|
4941
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
4942
|
+
},
|
|
4943
|
+
method: "POST",
|
|
4944
|
+
signal: controller?.signal
|
|
4945
|
+
});
|
|
4946
|
+
if (!response.ok) {
|
|
4947
|
+
return {
|
|
4948
|
+
deliveredTo: url,
|
|
4949
|
+
error: `Twilio handoff failed with response ${response.status}.`,
|
|
4950
|
+
status: "failed"
|
|
4951
|
+
};
|
|
4952
|
+
}
|
|
4953
|
+
return {
|
|
4954
|
+
deliveredAt: Date.now(),
|
|
4955
|
+
deliveredTo: url,
|
|
4956
|
+
metadata: {
|
|
4957
|
+
callSid
|
|
4958
|
+
},
|
|
4959
|
+
status: "delivered"
|
|
4960
|
+
};
|
|
4961
|
+
} finally {
|
|
4962
|
+
if (timeout) {
|
|
4963
|
+
clearTimeout(timeout);
|
|
4964
|
+
}
|
|
4965
|
+
}
|
|
4966
|
+
},
|
|
4967
|
+
id: options.id ?? "twilio-redirect",
|
|
4968
|
+
kind: "twilio-redirect"
|
|
4969
|
+
});
|
|
4970
|
+
|
|
3536
4971
|
// src/logger.ts
|
|
3537
4972
|
var noop2 = () => {};
|
|
3538
4973
|
var createNoopLogger = () => ({
|
|
@@ -3725,6 +5160,7 @@ var pushCallLifecycleEvent = (session, input) => {
|
|
|
3725
5160
|
}
|
|
3726
5161
|
return lifecycle;
|
|
3727
5162
|
};
|
|
5163
|
+
var getLatestCallLifecycleEvent = (session) => session.call?.events.at(-1);
|
|
3728
5164
|
var createVoiceSession = (options) => {
|
|
3729
5165
|
const logger = resolveLogger(options.logger);
|
|
3730
5166
|
const reconnect = {
|
|
@@ -3825,6 +5261,64 @@ var createVoiceSession = (options) => {
|
|
|
3825
5261
|
});
|
|
3826
5262
|
}
|
|
3827
5263
|
};
|
|
5264
|
+
const sendCallLifecycle = async (session) => {
|
|
5265
|
+
const event = getLatestCallLifecycleEvent(session);
|
|
5266
|
+
if (!event) {
|
|
5267
|
+
return;
|
|
5268
|
+
}
|
|
5269
|
+
await send({
|
|
5270
|
+
event,
|
|
5271
|
+
sessionId: options.id,
|
|
5272
|
+
type: "call_lifecycle"
|
|
5273
|
+
});
|
|
5274
|
+
};
|
|
5275
|
+
const runHandoff = async (input) => {
|
|
5276
|
+
const queuedDelivery = options.handoff?.deliveryQueue ? createVoiceHandoffDeliveryRecord({
|
|
5277
|
+
action: input.action,
|
|
5278
|
+
context: options.context,
|
|
5279
|
+
metadata: input.metadata,
|
|
5280
|
+
reason: input.reason,
|
|
5281
|
+
result: input.result,
|
|
5282
|
+
session: input.session,
|
|
5283
|
+
target: input.target
|
|
5284
|
+
}) : undefined;
|
|
5285
|
+
if (queuedDelivery) {
|
|
5286
|
+
await options.handoff?.deliveryQueue?.set(queuedDelivery.id, queuedDelivery);
|
|
5287
|
+
}
|
|
5288
|
+
if (options.handoff?.enqueueOnly) {
|
|
5289
|
+
return;
|
|
5290
|
+
}
|
|
5291
|
+
const result = await deliverVoiceHandoff({
|
|
5292
|
+
config: options.handoff,
|
|
5293
|
+
handoff: {
|
|
5294
|
+
action: input.action,
|
|
5295
|
+
api,
|
|
5296
|
+
context: options.context,
|
|
5297
|
+
metadata: input.metadata,
|
|
5298
|
+
reason: input.reason,
|
|
5299
|
+
result: input.result,
|
|
5300
|
+
session: input.session,
|
|
5301
|
+
target: input.target
|
|
5302
|
+
}
|
|
5303
|
+
});
|
|
5304
|
+
if (!result) {
|
|
5305
|
+
return;
|
|
5306
|
+
}
|
|
5307
|
+
if (queuedDelivery) {
|
|
5308
|
+
const updatedDelivery = applyVoiceHandoffDeliveryResult(queuedDelivery, result);
|
|
5309
|
+
await options.handoff?.deliveryQueue?.set(updatedDelivery.id, updatedDelivery);
|
|
5310
|
+
}
|
|
5311
|
+
await appendTrace({
|
|
5312
|
+
metadata: input.metadata,
|
|
5313
|
+
payload: {
|
|
5314
|
+
...result,
|
|
5315
|
+
reason: input.reason,
|
|
5316
|
+
target: input.target
|
|
5317
|
+
},
|
|
5318
|
+
session: input.session,
|
|
5319
|
+
type: "call.handoff"
|
|
5320
|
+
});
|
|
5321
|
+
};
|
|
3828
5322
|
const readSession = async () => options.store.getOrCreate(options.id);
|
|
3829
5323
|
const writeSession = async (mutate) => {
|
|
3830
5324
|
const session = await options.store.getOrCreate(options.id);
|
|
@@ -4015,6 +5509,7 @@ var createVoiceSession = (options) => {
|
|
|
4015
5509
|
await appendTrace({
|
|
4016
5510
|
payload: {
|
|
4017
5511
|
disposition,
|
|
5512
|
+
metadata: input.metadata,
|
|
4018
5513
|
reason: input.reason,
|
|
4019
5514
|
target: input.target,
|
|
4020
5515
|
type: "end"
|
|
@@ -4022,6 +5517,7 @@ var createVoiceSession = (options) => {
|
|
|
4022
5517
|
session,
|
|
4023
5518
|
type: "call.lifecycle"
|
|
4024
5519
|
});
|
|
5520
|
+
await sendCallLifecycle(session);
|
|
4025
5521
|
await send({
|
|
4026
5522
|
sessionId: options.id,
|
|
4027
5523
|
type: "complete"
|
|
@@ -4101,6 +5597,15 @@ var createVoiceSession = (options) => {
|
|
|
4101
5597
|
session,
|
|
4102
5598
|
type: "call.lifecycle"
|
|
4103
5599
|
});
|
|
5600
|
+
await sendCallLifecycle(session);
|
|
5601
|
+
await runHandoff({
|
|
5602
|
+
action: "transfer",
|
|
5603
|
+
metadata: input.metadata,
|
|
5604
|
+
reason: input.reason,
|
|
5605
|
+
result: input.result,
|
|
5606
|
+
session,
|
|
5607
|
+
target: input.target
|
|
5608
|
+
});
|
|
4104
5609
|
await completeInternal(input.result, {
|
|
4105
5610
|
disposition: "transferred",
|
|
4106
5611
|
invokeOnComplete: false,
|
|
@@ -4126,6 +5631,14 @@ var createVoiceSession = (options) => {
|
|
|
4126
5631
|
session,
|
|
4127
5632
|
type: "call.lifecycle"
|
|
4128
5633
|
});
|
|
5634
|
+
await sendCallLifecycle(session);
|
|
5635
|
+
await runHandoff({
|
|
5636
|
+
action: "escalate",
|
|
5637
|
+
metadata: input.metadata,
|
|
5638
|
+
reason: input.reason,
|
|
5639
|
+
result: input.result,
|
|
5640
|
+
session
|
|
5641
|
+
});
|
|
4129
5642
|
await completeInternal(input.result, {
|
|
4130
5643
|
disposition: "escalated",
|
|
4131
5644
|
invokeOnComplete: false,
|
|
@@ -4148,6 +5661,13 @@ var createVoiceSession = (options) => {
|
|
|
4148
5661
|
session,
|
|
4149
5662
|
type: "call.lifecycle"
|
|
4150
5663
|
});
|
|
5664
|
+
await sendCallLifecycle(session);
|
|
5665
|
+
await runHandoff({
|
|
5666
|
+
action: "no-answer",
|
|
5667
|
+
metadata: input?.metadata,
|
|
5668
|
+
result: input?.result,
|
|
5669
|
+
session
|
|
5670
|
+
});
|
|
4151
5671
|
await completeInternal(input?.result, {
|
|
4152
5672
|
disposition: "no-answer",
|
|
4153
5673
|
invokeOnComplete: false,
|
|
@@ -4169,6 +5689,13 @@ var createVoiceSession = (options) => {
|
|
|
4169
5689
|
session,
|
|
4170
5690
|
type: "call.lifecycle"
|
|
4171
5691
|
});
|
|
5692
|
+
await sendCallLifecycle(session);
|
|
5693
|
+
await runHandoff({
|
|
5694
|
+
action: "voicemail",
|
|
5695
|
+
metadata: input?.metadata,
|
|
5696
|
+
result: input?.result,
|
|
5697
|
+
session
|
|
5698
|
+
});
|
|
4172
5699
|
await completeInternal(input?.result, {
|
|
4173
5700
|
disposition: "voicemail",
|
|
4174
5701
|
invokeOnComplete: false,
|
|
@@ -4955,6 +6482,7 @@ var createVoiceSession = (options) => {
|
|
|
4955
6482
|
session,
|
|
4956
6483
|
type: "call.lifecycle"
|
|
4957
6484
|
});
|
|
6485
|
+
await sendCallLifecycle(session);
|
|
4958
6486
|
}
|
|
4959
6487
|
await send({
|
|
4960
6488
|
sessionId: options.id,
|
|
@@ -5545,7 +7073,7 @@ var createVoiceCallReviewFromLiveTelephonyReport = (report, options = {}) => {
|
|
|
5545
7073
|
}
|
|
5546
7074
|
};
|
|
5547
7075
|
};
|
|
5548
|
-
var
|
|
7076
|
+
var toErrorMessage2 = (error) => {
|
|
5549
7077
|
if (typeof error === "string" && error.trim().length > 0) {
|
|
5550
7078
|
return error;
|
|
5551
7079
|
}
|
|
@@ -5632,7 +7160,7 @@ var createVoiceCallReviewRecorder = (options = {}) => {
|
|
|
5632
7160
|
};
|
|
5633
7161
|
},
|
|
5634
7162
|
recordError: (error) => {
|
|
5635
|
-
const message =
|
|
7163
|
+
const message = toErrorMessage2(error);
|
|
5636
7164
|
errors.push(message);
|
|
5637
7165
|
push("turn", "error", {
|
|
5638
7166
|
reason: message
|
|
@@ -6341,7 +7869,7 @@ var runVoiceSessionBenchmarkSeries = async (input) => {
|
|
|
6341
7869
|
import { Buffer as Buffer2 } from "buffer";
|
|
6342
7870
|
var TWILIO_MULAW_SAMPLE_RATE = 8000;
|
|
6343
7871
|
var VOICE_PCM_SAMPLE_RATE = 16000;
|
|
6344
|
-
var
|
|
7872
|
+
var escapeXml2 = (value) => value.replaceAll("&", "&").replaceAll('"', """).replaceAll("'", "'").replaceAll("<", "<").replaceAll(">", ">");
|
|
6345
7873
|
var normalizeOnTurn = (handler) => {
|
|
6346
7874
|
if (handler.length > 1) {
|
|
6347
7875
|
const directHandler = handler;
|
|
@@ -6537,8 +8065,8 @@ var createTwilioSocketAdapter = (socket, getState) => ({
|
|
|
6537
8065
|
}
|
|
6538
8066
|
});
|
|
6539
8067
|
var createTwilioVoiceResponse = (options) => {
|
|
6540
|
-
const parameters = Object.entries(options.parameters ?? {}).filter((entry) => entry[1] !== undefined).map(([name, value]) => `<Parameter name="${
|
|
6541
|
-
return `<?xml version="1.0" encoding="UTF-8"?><Response><Connect><Stream url="${
|
|
8068
|
+
const parameters = Object.entries(options.parameters ?? {}).filter((entry) => entry[1] !== undefined).map(([name, value]) => `<Parameter name="${escapeXml2(name)}" value="${escapeXml2(String(value))}" />`).join("");
|
|
8069
|
+
return `<?xml version="1.0" encoding="UTF-8"?><Response><Connect><Stream url="${escapeXml2(options.streamUrl)}"${options.track ? ` track="${escapeXml2(options.track)}"` : ""}${options.streamName ? ` name="${escapeXml2(options.streamName)}"` : ""}>${parameters}</Stream></Connect></Response>`;
|
|
6542
8070
|
};
|
|
6543
8071
|
var createTwilioMediaStreamBridge = (socket, options) => {
|
|
6544
8072
|
const runtimePreset = resolveVoiceRuntimePreset(options.preset);
|
|
@@ -7208,6 +8736,8 @@ export {
|
|
|
7208
8736
|
getDefaultVoiceDuplexBenchmarkScenarios,
|
|
7209
8737
|
getDefaultTTSBenchmarkFixtures,
|
|
7210
8738
|
evaluateSTTBenchmarkAcceptance,
|
|
8739
|
+
createVoiceProviderFailureSimulator,
|
|
8740
|
+
createVoiceIOProviderFailureSimulator,
|
|
7211
8741
|
createVoiceCallReviewRecorder,
|
|
7212
8742
|
createVoiceCallReviewFromLiveTelephonyReport,
|
|
7213
8743
|
createTelephonyVoiceTestFixtures,
|