@sinch/functions-runtime 0.5.20 → 0.5.22
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 +26 -0
- package/dist/bin/sinch-runtime.js +1 -1
- package/dist/{chunk-4GLU7JTD.js → chunk-JP6PANB3.js} +102 -4
- package/dist/chunk-JP6PANB3.js.map +1 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +3 -1
- package/dist/voice/v2.d.ts +58 -0
- package/dist/voice/v2.js +3 -1
- package/package.json +1 -1
- package/dist/chunk-4GLU7JTD.js.map +0 -1
package/README.md
CHANGED
|
@@ -81,6 +81,32 @@ root, as above, or from `@sinch/functions-runtime/voice/v2` — both resolve the
|
|
|
81
81
|
declarations. The rest of the v2 surface (`MenuItemBuilder`, `dialPhone`, `dialSip`, etc.) is
|
|
82
82
|
subpath-only, for code that builds a client of its own.
|
|
83
83
|
|
|
84
|
+
### Connecting a call to an AI agent
|
|
85
|
+
|
|
86
|
+
`connectAgent` bridges an inbound call to a SIP-based AI voice agent — ElevenLabs or xAI Grok —
|
|
87
|
+
in one call, instead of hand-writing the answer/bridge/dial/teardown commands it takes:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { onCall, connectAgent, AgentProvider } from '@sinch/functions-runtime';
|
|
91
|
+
|
|
92
|
+
export const voiceWebhook = onCall({
|
|
93
|
+
incoming: (call) => connectAgent(AgentProvider.ElevenLabs, '+15551234567', { call }),
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The second argument is the number in the SIP URI's user part — for Grok the Direct SIP phone
|
|
98
|
+
number registered in the xAI console, for ElevenLabs the number attached to the agent's phone
|
|
99
|
+
integration. Neither provider routes by agent id, and it must be an E.164 number you configured,
|
|
100
|
+
never a value taken from a caller-controlled webhook field. Passing `call` (the incoming event)
|
|
101
|
+
defaults `from` to the caller's number and adds the `X-Sinch-Call-Id` / `X-Caller-Number` /
|
|
102
|
+
`X-Dialed-Number` SIP headers the agent sees as per-call context. Stream-based providers —
|
|
103
|
+
`AgentProvider.Sinch`, or a future OpenAI/Deepgram provider — are a different call shape and
|
|
104
|
+
throw; use `@sinch/agents` for those.
|
|
105
|
+
|
|
106
|
+
Return the result directly from `incoming`, as above — `callName` and the agent-leg teardown on
|
|
107
|
+
hangup are only honoured on a response to `call.incoming`, so forwarding just its `.commands`
|
|
108
|
+
elsewhere silently drops the teardown and leaves the SIP leg billing until `maxDuration`.
|
|
109
|
+
|
|
84
110
|
## Voice v1 (legacy)
|
|
85
111
|
|
|
86
112
|
v1 still works, and a function already written against it needs no changes beyond the client:
|
|
@@ -60133,11 +60133,17 @@ function toOrigin(origin2) {
|
|
|
60133
60133
|
}
|
|
60134
60134
|
return endpoint;
|
|
60135
60135
|
}
|
|
60136
|
+
var CONTROL_CHAR = /[\x00-\x1f\x7f]/;
|
|
60136
60137
|
function toCallHeaders(headers) {
|
|
60137
60138
|
if (!headers) {
|
|
60138
60139
|
return void 0;
|
|
60139
60140
|
}
|
|
60140
60141
|
const list = Array.isArray(headers) ? headers : Object.entries(headers).map(([key, value]) => ({ key, value }));
|
|
60142
|
+
for (const { key, value } of list) {
|
|
60143
|
+
if (CONTROL_CHAR.test(key) || value !== void 0 && CONTROL_CHAR.test(value)) {
|
|
60144
|
+
throw new Error(`Voice v2 call header '${key}' contains a control character (CR, LF, NUL, ...). These headers are assembled into a real SIP INVITE, so passing one through unchecked would let caller-controlled data inject extra header lines. Strip or reject it before calling.`);
|
|
60145
|
+
}
|
|
60146
|
+
}
|
|
60141
60147
|
return list.length > 0 ? list : void 0;
|
|
60142
60148
|
}
|
|
60143
60149
|
|
|
@@ -61212,6 +61218,90 @@ function toWebSocketBase(base) {
|
|
|
61212
61218
|
return `wss://${trimmed}`;
|
|
61213
61219
|
}
|
|
61214
61220
|
|
|
61221
|
+
// ../voice-v2-preview/dist/core/connect-agent.js
|
|
61222
|
+
var DEFAULT_MAX_DURATION = 1800;
|
|
61223
|
+
var DEFAULT_CALLER_LEG = "caller";
|
|
61224
|
+
var DEFAULT_AGENT_LEG = "agent";
|
|
61225
|
+
var PROVIDER_ENDPOINTS = {
|
|
61226
|
+
elevenlabs: { host: "sip.rtc.elevenlabs.io", port: 5060, transport: "TCP" },
|
|
61227
|
+
grok: { host: "sip.voice.x.ai", port: 5061, transport: "TLS" }
|
|
61228
|
+
};
|
|
61229
|
+
var E164 = /^\+\d{6,15}$/;
|
|
61230
|
+
var UNSAFE_HOST_CHAR = /[\x00-\x1f\x7f@;\s]/;
|
|
61231
|
+
function endpointNumber(value) {
|
|
61232
|
+
if (!value)
|
|
61233
|
+
return "";
|
|
61234
|
+
if (value.type === "PHONE")
|
|
61235
|
+
return value.phone.number;
|
|
61236
|
+
if (value.type === "SIP")
|
|
61237
|
+
return value.sip.endpoint;
|
|
61238
|
+
return "";
|
|
61239
|
+
}
|
|
61240
|
+
function asCallerId(value) {
|
|
61241
|
+
return E164.test(value) ? value : void 0;
|
|
61242
|
+
}
|
|
61243
|
+
function assertSipNumber(number) {
|
|
61244
|
+
if (E164.test(number))
|
|
61245
|
+
return;
|
|
61246
|
+
throw new Error(`connectAgent: '${number}' is not an E.164 phone number. This must be the provider's SIP number (e.g. '+15551234567') \u2014 the Direct SIP number registered with xAI, or the number attached to the ElevenLabs agent's phone integration \u2014 never an agent id, and never a value taken from a caller-controlled webhook field: it is interpolated straight into a SIP URI. (Looking for an agent id instead? That's @sinch/agents' connectAgent(provider, agentId), a different function.)`);
|
|
61247
|
+
}
|
|
61248
|
+
function assertSipHost(host) {
|
|
61249
|
+
if (!UNSAFE_HOST_CHAR.test(host))
|
|
61250
|
+
return;
|
|
61251
|
+
throw new Error(`connectAgent: options.host '${host}' contains a character a SIP host cannot ('@', ';', whitespace or a control character) \u2014 refusing rather than building a URI that means something other than what it looks like.`);
|
|
61252
|
+
}
|
|
61253
|
+
function toHeaderMap(headers) {
|
|
61254
|
+
if (!headers)
|
|
61255
|
+
return {};
|
|
61256
|
+
if (Array.isArray(headers)) {
|
|
61257
|
+
return Object.fromEntries(headers.map((h) => [h.key, h.value ?? ""]));
|
|
61258
|
+
}
|
|
61259
|
+
return { ...headers };
|
|
61260
|
+
}
|
|
61261
|
+
function connectAgent(provider, number, options = {}) {
|
|
61262
|
+
const endpoint = PROVIDER_ENDPOINTS[provider];
|
|
61263
|
+
if (!endpoint) {
|
|
61264
|
+
throw new Error(`connectAgent: unsupported provider '${provider}'. 'elevenlabs' and 'grok' are SIP-based and supported here. 'sinch' (the agents SDK's media stream), and any other stream-based provider such as OpenAI or Deepgram, need a different call shape \u2014 see @sinch/agents.`);
|
|
61265
|
+
}
|
|
61266
|
+
assertSipNumber(number);
|
|
61267
|
+
const host = options.host ?? endpoint.host;
|
|
61268
|
+
const port = options.port ?? endpoint.port;
|
|
61269
|
+
const transport = options.transport ?? endpoint.transport;
|
|
61270
|
+
const bridgeName = options.bridgeName ?? DEFAULT_BRIDGE_NAME;
|
|
61271
|
+
const callerLeg = options.callerLeg ?? DEFAULT_CALLER_LEG;
|
|
61272
|
+
const agentLeg = options.agentLeg ?? DEFAULT_AGENT_LEG;
|
|
61273
|
+
assertSipHost(host);
|
|
61274
|
+
const sipUri = `sip:${number}@${host}:${port};transport=${transport.toLowerCase()}`;
|
|
61275
|
+
const call = options.call?.call;
|
|
61276
|
+
const caller = call ? endpointNumber(call.from) : "";
|
|
61277
|
+
const dialled = call ? endpointNumber(call.to) : "";
|
|
61278
|
+
const autoHeaders = call ? {
|
|
61279
|
+
"X-Sinch-Call-Id": call.callId ?? "",
|
|
61280
|
+
"X-Caller-Number": caller,
|
|
61281
|
+
"X-Dialed-Number": dialled
|
|
61282
|
+
} : {};
|
|
61283
|
+
const headers = { ...autoHeaders, ...toHeaderMap(options.headers) };
|
|
61284
|
+
return {
|
|
61285
|
+
// Names the inbound leg, which the agent leg's endOnHangup and endOnFailure point at.
|
|
61286
|
+
// The API reads callName on a response to call.incoming and nowhere else.
|
|
61287
|
+
callName: callerLeg,
|
|
61288
|
+
commands: commands().answer().bridgeCall(bridgeName).dial(dialSip(sipUri, { transport, headers }), {
|
|
61289
|
+
callName: agentLeg,
|
|
61290
|
+
from: asCallerId(caller),
|
|
61291
|
+
maxDuration: options.maxDuration ?? DEFAULT_MAX_DURATION,
|
|
61292
|
+
onAnswer: (c) => c.bridgeCall(bridgeName),
|
|
61293
|
+
// The caller is already on the bridge, so every way the agent leg can end has to end
|
|
61294
|
+
// theirs too — otherwise they sit in silence until maxDuration.
|
|
61295
|
+
endOnHangup: callerLeg,
|
|
61296
|
+
endOnFailure: callerLeg
|
|
61297
|
+
}).build(),
|
|
61298
|
+
// The other direction. Teardown does not cascade in v2: endOnHangup above only covers the
|
|
61299
|
+
// agent side going away. When the CALLER hangs up nothing ends the SIP leg, so it stays up
|
|
61300
|
+
// against the provider until maxDuration, with the provider billing audio into an empty bridge.
|
|
61301
|
+
onHangup: commands().hangup(agentLeg).build()
|
|
61302
|
+
};
|
|
61303
|
+
}
|
|
61304
|
+
|
|
61215
61305
|
// ../runtime-shared/dist/voice/client.js
|
|
61216
61306
|
function attachVoiceClient(context, v1) {
|
|
61217
61307
|
let client;
|
|
@@ -70156,10 +70246,17 @@ var {
|
|
|
70156
70246
|
// src/tunnel/webhook-config.ts
|
|
70157
70247
|
var import_sdk_core2 = __toESM(require_dist11(), 1);
|
|
70158
70248
|
import { randomBytes } from "crypto";
|
|
70159
|
-
var SINCH_FN_URL_PATTERN = /\.fn(-\w+)?\.sinch\.com/;
|
|
70249
|
+
var SINCH_FN_URL_PATTERN = /\.fn(-\w+)?\.sinch\.(?:com|io)/;
|
|
70160
70250
|
function isOurWebhook(target) {
|
|
70161
70251
|
return !!target && SINCH_FN_URL_PATTERN.test(target);
|
|
70162
70252
|
}
|
|
70253
|
+
var FUNCTIONS_DOMAIN_DEFAULT = "fn.sinch.io";
|
|
70254
|
+
function deployedWebhookTarget(functionName) {
|
|
70255
|
+
const injected = process.env.FUNCTION_URL;
|
|
70256
|
+
if (injected) return `${injected.replace(/\/+$/, "")}/webhook/conversation`;
|
|
70257
|
+
const domain = process.env.SINCH_FUNCTIONS_DOMAIN || FUNCTIONS_DOMAIN_DEFAULT;
|
|
70258
|
+
return `https://${functionName}.${domain}/webhook/conversation`;
|
|
70259
|
+
}
|
|
70163
70260
|
function isTunnelUrl(target) {
|
|
70164
70261
|
return !!target && target.includes("tunnel.fn");
|
|
70165
70262
|
}
|
|
@@ -70227,7 +70324,7 @@ async function cleanupConversationWebhook(config) {
|
|
|
70227
70324
|
if (!restoreTarget || isTunnelUrl(restoreTarget)) {
|
|
70228
70325
|
const functionName = process.env.FUNCTION_NAME || process.env.FUNCTION_ID;
|
|
70229
70326
|
if (functionName) {
|
|
70230
|
-
restoreTarget =
|
|
70327
|
+
restoreTarget = deployedWebhookTarget(functionName);
|
|
70231
70328
|
console.log(`\u{1F527} Derived restore target from env: ${restoreTarget}`);
|
|
70232
70329
|
} else {
|
|
70233
70330
|
console.log("\u26A0\uFE0F Cannot restore webhook \u2014 no FUNCTION_NAME or FUNCTION_ID available");
|
|
@@ -70498,7 +70595,7 @@ function describe(error) {
|
|
|
70498
70595
|
}
|
|
70499
70596
|
|
|
70500
70597
|
// src/tunnel/index.ts
|
|
70501
|
-
var TUNNEL_GATEWAY_DEFAULT = "https://tunnel.fn.sinch.
|
|
70598
|
+
var TUNNEL_GATEWAY_DEFAULT = "https://tunnel.fn.sinch.io";
|
|
70502
70599
|
var TunnelClient = class {
|
|
70503
70600
|
ws = null;
|
|
70504
70601
|
tunnelUrl = null;
|
|
@@ -71416,6 +71513,7 @@ export {
|
|
|
71416
71513
|
resolveWebhookName,
|
|
71417
71514
|
Client,
|
|
71418
71515
|
createClient,
|
|
71516
|
+
connectAgent,
|
|
71419
71517
|
attachVoiceClient,
|
|
71420
71518
|
onCall,
|
|
71421
71519
|
toCamelCase,
|
|
@@ -71796,4 +71894,4 @@ serve-static/index.js:
|
|
|
71796
71894
|
* MIT Licensed
|
|
71797
71895
|
*)
|
|
71798
71896
|
*/
|
|
71799
|
-
//# sourceMappingURL=chunk-
|
|
71897
|
+
//# sourceMappingURL=chunk-JP6PANB3.js.map
|