voice-router-dev 0.2.1 → 0.2.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/dist/index.d.mts +14 -14
- package/dist/index.d.ts +14 -14
- package/dist/index.js +96 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +96 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +47 -45
package/dist/index.mjs
CHANGED
|
@@ -2831,6 +2831,15 @@ var AssemblyAIAdapter = class extends BaseAdapter {
|
|
|
2831
2831
|
});
|
|
2832
2832
|
let sessionStatus = "connecting";
|
|
2833
2833
|
const sessionId = `assemblyai-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
2834
|
+
let audioBuffer = Buffer.alloc(0);
|
|
2835
|
+
const MIN_CHUNK_SIZE = 1600;
|
|
2836
|
+
const MAX_CHUNK_SIZE = 32e3;
|
|
2837
|
+
const flushAudioBuffer = () => {
|
|
2838
|
+
if (audioBuffer.length > 0 && ws.readyState === WebSocket2.OPEN) {
|
|
2839
|
+
ws.send(audioBuffer);
|
|
2840
|
+
audioBuffer = Buffer.alloc(0);
|
|
2841
|
+
}
|
|
2842
|
+
};
|
|
2834
2843
|
ws.on("open", () => {
|
|
2835
2844
|
sessionStatus = "open";
|
|
2836
2845
|
callbacks?.onOpen?.();
|
|
@@ -2919,8 +2928,13 @@ var AssemblyAIAdapter = class extends BaseAdapter {
|
|
|
2919
2928
|
if (ws.readyState !== WebSocket2.OPEN) {
|
|
2920
2929
|
throw new Error("WebSocket is not open");
|
|
2921
2930
|
}
|
|
2922
|
-
|
|
2931
|
+
audioBuffer = Buffer.concat([audioBuffer, chunk.data]);
|
|
2932
|
+
if (audioBuffer.length >= MIN_CHUNK_SIZE || audioBuffer.length >= MAX_CHUNK_SIZE) {
|
|
2933
|
+
ws.send(audioBuffer);
|
|
2934
|
+
audioBuffer = Buffer.alloc(0);
|
|
2935
|
+
}
|
|
2923
2936
|
if (chunk.isLast) {
|
|
2937
|
+
flushAudioBuffer();
|
|
2924
2938
|
ws.send(
|
|
2925
2939
|
JSON.stringify({
|
|
2926
2940
|
terminate_session: true
|
|
@@ -2933,6 +2947,7 @@ var AssemblyAIAdapter = class extends BaseAdapter {
|
|
|
2933
2947
|
return;
|
|
2934
2948
|
}
|
|
2935
2949
|
sessionStatus = "closing";
|
|
2950
|
+
flushAudioBuffer();
|
|
2936
2951
|
if (ws.readyState === WebSocket2.OPEN) {
|
|
2937
2952
|
ws.send(
|
|
2938
2953
|
JSON.stringify({
|
|
@@ -4273,15 +4288,46 @@ var GladiaWebhookHandler = class extends BaseWebhookHandler {
|
|
|
4273
4288
|
super(...arguments);
|
|
4274
4289
|
this.provider = "gladia";
|
|
4275
4290
|
}
|
|
4291
|
+
/**
|
|
4292
|
+
* Convert Gladia WordDTO to unified Word type
|
|
4293
|
+
*/
|
|
4294
|
+
mapWord(word) {
|
|
4295
|
+
return {
|
|
4296
|
+
text: word.word,
|
|
4297
|
+
start: word.start,
|
|
4298
|
+
end: word.end,
|
|
4299
|
+
confidence: word.confidence
|
|
4300
|
+
};
|
|
4301
|
+
}
|
|
4302
|
+
/**
|
|
4303
|
+
* Convert Gladia UtteranceDTO to unified Utterance type
|
|
4304
|
+
*/
|
|
4305
|
+
mapUtterance(utterance) {
|
|
4306
|
+
return {
|
|
4307
|
+
text: utterance.text,
|
|
4308
|
+
start: utterance.start,
|
|
4309
|
+
end: utterance.end,
|
|
4310
|
+
confidence: utterance.confidence,
|
|
4311
|
+
speaker: utterance.speaker !== void 0 ? String(utterance.speaker) : void 0,
|
|
4312
|
+
words: utterance.words?.map((w) => this.mapWord(w))
|
|
4313
|
+
};
|
|
4314
|
+
}
|
|
4276
4315
|
/**
|
|
4277
4316
|
* Check if payload matches Gladia webhook format
|
|
4317
|
+
*
|
|
4318
|
+
* Gladia callbacks have the structure:
|
|
4319
|
+
* - { id, event: "transcription.success", payload: TranscriptionResultDTO, custom_metadata? }
|
|
4320
|
+
* - { id, event: "transcription.error", error: ErrorDTO, custom_metadata? }
|
|
4278
4321
|
*/
|
|
4279
4322
|
matches(payload, _options) {
|
|
4280
4323
|
if (!payload || typeof payload !== "object") {
|
|
4281
4324
|
return false;
|
|
4282
4325
|
}
|
|
4283
4326
|
const obj = payload;
|
|
4284
|
-
if (!("
|
|
4327
|
+
if (!("id" in obj) || !("event" in obj)) {
|
|
4328
|
+
return false;
|
|
4329
|
+
}
|
|
4330
|
+
if (typeof obj.id !== "string") {
|
|
4285
4331
|
return false;
|
|
4286
4332
|
}
|
|
4287
4333
|
if (typeof obj.event !== "string") {
|
|
@@ -4290,11 +4336,13 @@ var GladiaWebhookHandler = class extends BaseWebhookHandler {
|
|
|
4290
4336
|
if (!obj.event.startsWith("transcription.")) {
|
|
4291
4337
|
return false;
|
|
4292
4338
|
}
|
|
4293
|
-
if (
|
|
4339
|
+
if (obj.event === "transcription.success" && !("payload" in obj)) {
|
|
4340
|
+
return false;
|
|
4341
|
+
}
|
|
4342
|
+
if (obj.event === "transcription.error" && !("error" in obj)) {
|
|
4294
4343
|
return false;
|
|
4295
4344
|
}
|
|
4296
|
-
|
|
4297
|
-
return typeof payloadObj.id === "string";
|
|
4345
|
+
return true;
|
|
4298
4346
|
}
|
|
4299
4347
|
/**
|
|
4300
4348
|
* Parse Gladia webhook payload to unified format
|
|
@@ -4303,38 +4351,57 @@ var GladiaWebhookHandler = class extends BaseWebhookHandler {
|
|
|
4303
4351
|
if (!this.matches(payload)) {
|
|
4304
4352
|
return this.createErrorEvent(payload, "Invalid Gladia webhook payload");
|
|
4305
4353
|
}
|
|
4306
|
-
const
|
|
4307
|
-
const jobId =
|
|
4308
|
-
const event =
|
|
4309
|
-
if (event === "transcription.created") {
|
|
4310
|
-
return {
|
|
4311
|
-
success: true,
|
|
4312
|
-
provider: this.provider,
|
|
4313
|
-
eventType: "transcription.created",
|
|
4314
|
-
data: {
|
|
4315
|
-
id: jobId,
|
|
4316
|
-
status: "queued"
|
|
4317
|
-
},
|
|
4318
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4319
|
-
raw: payload
|
|
4320
|
-
};
|
|
4321
|
-
}
|
|
4354
|
+
const obj = payload;
|
|
4355
|
+
const jobId = obj.id;
|
|
4356
|
+
const event = obj.event;
|
|
4322
4357
|
if (event === "transcription.success") {
|
|
4358
|
+
const successPayload = payload;
|
|
4359
|
+
const result = successPayload.payload;
|
|
4360
|
+
const transcription = result.transcription;
|
|
4361
|
+
const metadata = result.metadata;
|
|
4362
|
+
const utterances = transcription?.utterances?.map(
|
|
4363
|
+
(u) => this.mapUtterance(u)
|
|
4364
|
+
);
|
|
4365
|
+
const words = transcription?.utterances?.flatMap(
|
|
4366
|
+
(u) => u.words?.map((w) => this.mapWord(w)) ?? []
|
|
4367
|
+
);
|
|
4368
|
+
const speakerIds = /* @__PURE__ */ new Set();
|
|
4369
|
+
transcription?.utterances?.forEach((u) => {
|
|
4370
|
+
if (u.speaker !== void 0) {
|
|
4371
|
+
speakerIds.add(u.speaker);
|
|
4372
|
+
}
|
|
4373
|
+
});
|
|
4374
|
+
const speakers = speakerIds.size > 0 ? Array.from(speakerIds).map((id) => ({ id: String(id) })) : void 0;
|
|
4375
|
+
const summary = result.summarization?.success && result.summarization.results ? result.summarization.results : void 0;
|
|
4323
4376
|
return {
|
|
4324
4377
|
success: true,
|
|
4325
4378
|
provider: this.provider,
|
|
4326
4379
|
eventType: "transcription.completed",
|
|
4327
4380
|
data: {
|
|
4328
4381
|
id: jobId,
|
|
4329
|
-
status: "completed"
|
|
4330
|
-
|
|
4331
|
-
|
|
4382
|
+
status: "completed",
|
|
4383
|
+
text: transcription?.full_transcript,
|
|
4384
|
+
duration: metadata?.audio_duration,
|
|
4385
|
+
language: transcription?.languages?.[0],
|
|
4386
|
+
speakers,
|
|
4387
|
+
words,
|
|
4388
|
+
utterances,
|
|
4389
|
+
summary,
|
|
4390
|
+
metadata: {
|
|
4391
|
+
transcription_time: metadata?.transcription_time,
|
|
4392
|
+
billing_time: metadata?.billing_time,
|
|
4393
|
+
number_of_distinct_channels: metadata?.number_of_distinct_channels,
|
|
4394
|
+
custom_metadata: successPayload.custom_metadata
|
|
4395
|
+
},
|
|
4396
|
+
completedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
4332
4397
|
},
|
|
4333
4398
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4334
4399
|
raw: payload
|
|
4335
4400
|
};
|
|
4336
4401
|
}
|
|
4337
4402
|
if (event === "transcription.error") {
|
|
4403
|
+
const errorPayload = payload;
|
|
4404
|
+
const error = errorPayload.error;
|
|
4338
4405
|
return {
|
|
4339
4406
|
success: false,
|
|
4340
4407
|
provider: this.provider,
|
|
@@ -4342,7 +4409,11 @@ var GladiaWebhookHandler = class extends BaseWebhookHandler {
|
|
|
4342
4409
|
data: {
|
|
4343
4410
|
id: jobId,
|
|
4344
4411
|
status: "error",
|
|
4345
|
-
error: "Transcription failed"
|
|
4412
|
+
error: error?.message || "Transcription failed",
|
|
4413
|
+
metadata: {
|
|
4414
|
+
error_code: error?.code,
|
|
4415
|
+
custom_metadata: errorPayload.custom_metadata
|
|
4416
|
+
}
|
|
4346
4417
|
},
|
|
4347
4418
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4348
4419
|
raw: payload
|