@sinch/functions-runtime 0.7.2 → 0.7.3

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 CHANGED
@@ -462,6 +462,8 @@ export const voiceWebhook = onCall({
462
462
 
463
463
  `ctx.recording?.storageKey` is available in `onEnd` for the stream recording. For a plain voice
464
464
  function, `recordToFunctions()` on the builder does the same as `sinch`.
465
+ To record with your own `startRecording`, `await context.storage.recordingUrl(callId, 'mp3')` returns the
466
+ same presigned PUT URL (`recordings/<callId>.<format>`, `mp3` by default or `wav`) for `destinationUrl`.
465
467
 
466
468
  ### Talking to an LLM over Voice Relay
467
469
 
@@ -687,19 +689,19 @@ export function setup(runtime: SinchRuntime) {
687
689
 
688
690
  Every handler receives a `FunctionContext`.
689
691
 
690
- | Member | What it is |
691
- | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
692
- | `context.config` | `projectId`, `functionName`, `environment`, `variables`, and `getVariable`/`requireVariable`/`getSecret`/`requireSecret`/`hasVariable`… |
693
- | `context.env` | Environment variables, including secrets |
694
- | `context.cache` | Key-value store with TTLs: `get`, `set`, `has`, `delete`, `extend`, `keys`, `getMany`. In memory locally, shared in production |
695
- | `context.storage` | Durable files: `write`, `read`, `list`, `exists`, `delete`, `getRecordingDestination`. Local filesystem in dev, object storage in production |
696
- | `context.database` | Path to a per-function SQLite file, replicated in production. Open it with `node:sqlite` or any SQLite driver |
697
- | `context.assets(name)` | Reads a file from the function's `assets/` directory |
698
- | `context.voice` | Voice API v2 client: `call`, `callWithStream`, `bridge`, `transfer`, `patch`, `onCall`. Always present |
699
- | `context.voice.v1` | Voice API v1 client, when `VOICE_APPLICATION_KEY` and `VOICE_APPLICATION_SECRET` are set |
700
- | `context.conversation` | Conversation API client, when `CONVERSATION_APP_ID` is set |
701
- | `context.sms` | SMS API client, when `SMS_SERVICE_PLAN_ID` is set |
702
- | `context.numbers` | Numbers API client, when `ENABLE_NUMBERS_API=true` |
692
+ | Member | What it is |
693
+ | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
694
+ | `context.config` | `projectId`, `functionName`, `environment`, `variables`, and `getVariable`/`requireVariable`/`getSecret`/`requireSecret`/`hasVariable`… |
695
+ | `context.env` | Environment variables, including secrets |
696
+ | `context.cache` | Key-value store with TTLs: `get`, `set`, `has`, `delete`, `extend`, `keys`, `getMany`. In memory locally, shared in production |
697
+ | `context.storage` | Durable files: `write`, `read`, `list`, `exists`, `delete`, `recordingUrl`. Local filesystem in dev, object storage in production |
698
+ | `context.database` | Path to a per-function SQLite file, replicated in production. Open it with `node:sqlite` or any SQLite driver |
699
+ | `context.assets(name)` | Reads a file from the function's `assets/` directory |
700
+ | `context.voice` | Voice API v2 client: `call`, `callWithStream`, `bridge`, `transfer`, `patch`, `onCall`. Always present |
701
+ | `context.voice.v1` | Voice API v1 client, when `VOICE_APPLICATION_KEY` and `VOICE_APPLICATION_SECRET` are set |
702
+ | `context.conversation` | Conversation API client, when `CONVERSATION_APP_ID` is set |
703
+ | `context.sms` | SMS API client, when `SMS_SERVICE_PLAN_ID` is set |
704
+ | `context.numbers` | Numbers API client, when `ENABLE_NUMBERS_API=true` |
703
705
 
704
706
  The API clients also need `PROJECT_ID`, `PROJECT_ID_API_KEY` and `PROJECT_ID_API_SECRET`, which `sinch auth login` provides. A client whose variables are missing is `undefined` — except `context.voice`, which is always there and reports missing credentials when a request is actually sent. Full list: [SDK environment variables](https://developers.sinch.com/docs/functions/reference/sdk-env-vars).
705
707
 
@@ -22,7 +22,7 @@ import {
22
22
  require_express,
23
23
  secretsLoader,
24
24
  setupRequestHandler
25
- } from "../chunk-KGCIUPCT.js";
25
+ } from "../chunk-KCLRM3MM.js";
26
26
 
27
27
  // ../../node_modules/dotenv/package.json
28
28
  var require_package = __commonJS({
@@ -59783,6 +59783,12 @@ var MenuTemplates = {
59783
59783
 
59784
59784
  // ../runtime-shared/dist/storage/recording.js
59785
59785
  var RECORDING_RECEIVER_PATH = "/_sinch/recording/";
59786
+ function normalizeRecordingFormat(format) {
59787
+ const f = format.toLowerCase();
59788
+ if (f !== "mp3" && f !== "wav")
59789
+ throw new Error(`Unsupported recording format "${format}"`);
59790
+ return f;
59791
+ }
59786
59792
  function recordingKey(callId, format) {
59787
59793
  return `recordings/${callId.replace(/[^\w-]/g, "_")}.${format}`;
59788
59794
  }
@@ -63555,7 +63561,7 @@ async function handleVoiceWebhook(userFunction, context, event, request, logger)
63555
63561
  }
63556
63562
  async function rewriteRecordingSentinels(body, callId, storage) {
63557
63563
  const resolve = async (options) => {
63558
- if (!callId || !storage?.getRecordingDestination) {
63564
+ if (!callId || !(storage?.recordingUrl ?? storage?.getRecordingDestination)) {
63559
63565
  console.warn(`[recording] Dropping the recording command: ${callId ? "this storage cannot presign a recording destination" : "the event has no callId"}.`);
63560
63566
  return false;
63561
63567
  }
@@ -63565,7 +63571,7 @@ async function rewriteRecordingSentinels(body, callId, storage) {
63565
63571
  return false;
63566
63572
  }
63567
63573
  try {
63568
- options["destinationUrl"] = await storage.getRecordingDestination(callId, format);
63574
+ options["destinationUrl"] = await (storage.recordingUrl ? storage.recordingUrl(callId, format) : storage.getRecordingDestination(callId, format));
63569
63575
  return true;
63570
63576
  } catch (err) {
63571
63577
  console.warn("[recording] Dropping the recording command, presigning failed:", err);
@@ -65629,7 +65635,7 @@ function createOpenAiAdapter(webSocketClient) {
65629
65635
  break;
65630
65636
  }
65631
65637
  case "session.delegation.created": {
65632
- const delegationId = msg["delegation_id"];
65638
+ const delegationId = msg["delegation"]?.id ?? msg["delegation_id"];
65633
65639
  if (delegationId)
65634
65640
  session.delegationCreatedAtMs.set(delegationId, Date.now());
65635
65641
  break;
@@ -68065,6 +68071,8 @@ function realtimeAgent(options) {
68065
68071
  const callConnectedAtMs = Date.now();
68066
68072
  let lastUserTurnAt;
68067
68073
  let firstAudioAtMs;
68074
+ let earlyRunStartAt;
68075
+ let lastAudioAt;
68068
68076
  const playout = createPlayoutTracker(PLAYOUT_GAP_MS);
68069
68077
  const untimedAgentEntries = [];
68070
68078
  let bytesPerMs = 8e3 * 2 / 1e3;
@@ -68208,6 +68216,10 @@ function realtimeAgent(options) {
68208
68216
  onAudioOut(pcm) {
68209
68217
  if (firstAudioAtMs === void 0 && lastUserTurnAt !== void 0)
68210
68218
  firstAudioAtMs = Date.now();
68219
+ const frameAt = Date.now();
68220
+ if (lastUserTurnAt === void 0 && (lastAudioAt === void 0 || frameAt - lastAudioAt > PLAYOUT_GAP_MS))
68221
+ earlyRunStartAt = frameAt;
68222
+ lastAudioAt = frameAt;
68211
68223
  const nowMs = Date.now();
68212
68224
  const speech = isSpeechFrame(pcm);
68213
68225
  if (speech)
@@ -68239,13 +68251,15 @@ function realtimeAgent(options) {
68239
68251
  flushPendingStreamLog();
68240
68252
  timeUntimedAgentEntries();
68241
68253
  lastUserTurnAt = Date.now();
68242
- firstAudioAtMs = void 0;
68254
+ firstAudioAtMs = earlyRunStartAt !== void 0 && lastAudioAt !== void 0 && Date.now() - lastAudioAt <= PLAYOUT_GAP_MS ? earlyRunStartAt : void 0;
68255
+ earlyRunStartAt = void 0;
68243
68256
  entry.speechStartAt = pendingSpeechStartAt;
68244
68257
  entry.speechEndAt = pendingSpeechEndAt;
68245
68258
  pendingSpeechStartAt = void 0;
68246
68259
  pendingSpeechEndAt = void 0;
68247
68260
  speechEndFromProvider = false;
68248
68261
  } else if (turn.role === "agent" && lastUserTurnAt !== void 0) {
68262
+ earlyRunStartAt = void 0;
68249
68263
  entry.totalMs = Date.now() - lastUserTurnAt;
68250
68264
  lastUserTurnAt = void 0;
68251
68265
  entry.delegationMs = turn.delegationMs;
@@ -68263,8 +68277,10 @@ function realtimeAgent(options) {
68263
68277
  currentAgentEntry = entry;
68264
68278
  pendingStreamLog = { entry, totalMs: entry.totalMs, firstAudioAtMs };
68265
68279
  }
68266
- } else if (turn.role === "agent")
68280
+ } else if (turn.role === "agent") {
68281
+ earlyRunStartAt = void 0;
68267
68282
  untimedAgentEntries.push(entry);
68283
+ }
68268
68284
  appendTranscript(entry);
68269
68285
  },
68270
68286
  onSpeechStart() {
@@ -71634,6 +71650,10 @@ var LocalStorage = class {
71634
71650
  }
71635
71651
  return url2;
71636
71652
  }
71653
+ /** The URL `recordToFunctions()` records to; see {@link IFunctionStorage.recordingUrl}. */
71654
+ async recordingUrl(callId, format = "mp3") {
71655
+ return this.getRecordingDestination(callId, normalizeRecordingFormat(format));
71656
+ }
71637
71657
  async write(key, data) {
71638
71658
  const filePath = this.resolvePath(key);
71639
71659
  await fs3.mkdir(path4.dirname(filePath), { recursive: true });
@@ -77577,6 +77597,7 @@ export {
77577
77597
  createSimpleMenu,
77578
77598
  MenuTemplates,
77579
77599
  RECORDING_RECEIVER_PATH,
77600
+ normalizeRecordingFormat,
77580
77601
  recordingKey,
77581
77602
  UniversalConfig,
77582
77603
  createConfig,
@@ -78030,4 +78051,4 @@ serve-static/index.js:
78030
78051
  * MIT Licensed
78031
78052
  *)
78032
78053
  */
78033
- //# sourceMappingURL=chunk-KGCIUPCT.js.map
78054
+ //# sourceMappingURL=chunk-KCLRM3MM.js.map