@kuralle-syrinx/deepgram 2.1.0
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/.handoff/finalize-contract-probe.test.ts +193 -0
- package/LICENSE +22 -0
- package/package.json +28 -0
- package/src/index.ts +7 -0
- package/src/stt.test.ts +1153 -0
- package/src/stt.ts +721 -0
- package/src/tts.test.ts +359 -0
- package/src/tts.ts +359 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { WebSocketServer, type WebSocket } from "ws";
|
|
3
|
+
import {
|
|
4
|
+
PipelineBusImpl,
|
|
5
|
+
Route,
|
|
6
|
+
type ConversationMetricPacket,
|
|
7
|
+
type SttErrorPacket,
|
|
8
|
+
type SttResultPacket,
|
|
9
|
+
} from "@kuralle-syrinx/core";
|
|
10
|
+
import { DeepgramSTTPlugin } from "../src/index.js";
|
|
11
|
+
|
|
12
|
+
async function createServer(onConnection: (socket: WebSocket) => void): Promise<{
|
|
13
|
+
url: string;
|
|
14
|
+
close: () => Promise<void>;
|
|
15
|
+
}> {
|
|
16
|
+
const server = await new Promise<WebSocketServer>((resolve) => {
|
|
17
|
+
let nextServer: WebSocketServer;
|
|
18
|
+
nextServer = new WebSocketServer({ port: 0 }, () => resolve(nextServer));
|
|
19
|
+
});
|
|
20
|
+
server.on("connection", onConnection);
|
|
21
|
+
const address = server.address();
|
|
22
|
+
if (!address || typeof address === "string") throw new Error("expected tcp address");
|
|
23
|
+
return {
|
|
24
|
+
url: `ws://127.0.0.1:${address.port}/listen`,
|
|
25
|
+
close: () => new Promise((resolve) => {
|
|
26
|
+
for (const client of server.clients) client.terminate();
|
|
27
|
+
server.close(() => resolve());
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function waitUntil(predicate: () => boolean): Promise<void> {
|
|
33
|
+
for (let i = 0; i < 100; i += 1) {
|
|
34
|
+
if (predicate()) return;
|
|
35
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
36
|
+
}
|
|
37
|
+
throw new Error("timed out waiting for probe condition");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("Deepgram finalize contract probe", () => {
|
|
41
|
+
it("keeps a delayed Finalize response correlated to the requested context after currentContextId moves", async () => {
|
|
42
|
+
const controlMessages: string[] = [];
|
|
43
|
+
const server = await createServer((socket) => {
|
|
44
|
+
socket.on("message", (data, isBinary) => {
|
|
45
|
+
if (isBinary) return;
|
|
46
|
+
const msg = JSON.parse(data.toString()) as { type?: string };
|
|
47
|
+
if (msg.type) controlMessages.push(msg.type);
|
|
48
|
+
if (msg.type !== "Finalize") return;
|
|
49
|
+
setTimeout(() => {
|
|
50
|
+
socket.send(JSON.stringify({
|
|
51
|
+
is_final: true,
|
|
52
|
+
speech_final: false,
|
|
53
|
+
from_finalize: true,
|
|
54
|
+
channel: { alternatives: [{ transcript: "old turn confirmed", confidence: 0.91 }] },
|
|
55
|
+
}));
|
|
56
|
+
}, 10);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
const bus = new PipelineBusImpl();
|
|
60
|
+
const started = bus.start();
|
|
61
|
+
const plugin = new DeepgramSTTPlugin();
|
|
62
|
+
const finals: SttResultPacket[] = [];
|
|
63
|
+
const errors: SttErrorPacket[] = [];
|
|
64
|
+
const metrics: ConversationMetricPacket[] = [];
|
|
65
|
+
bus.on("stt.result", (pkt) => finals.push(pkt as SttResultPacket));
|
|
66
|
+
bus.on("stt.error", (pkt) => errors.push(pkt as SttErrorPacket));
|
|
67
|
+
bus.on("metric.conversation", (pkt) => metrics.push(pkt as ConversationMetricPacket));
|
|
68
|
+
|
|
69
|
+
await plugin.initialize(bus, {
|
|
70
|
+
api_key: "test",
|
|
71
|
+
endpoint_url: server.url,
|
|
72
|
+
sample_rate: 16000,
|
|
73
|
+
emit_eos_on_final: false,
|
|
74
|
+
finalize_on_speech_final: false,
|
|
75
|
+
provider_finalize_timeout_ms: 40,
|
|
76
|
+
finalize_timeout_fallback: false,
|
|
77
|
+
});
|
|
78
|
+
bus.push(Route.Main, {
|
|
79
|
+
kind: "stt.audio",
|
|
80
|
+
contextId: "old-vad-fragment",
|
|
81
|
+
timestampMs: Date.now(),
|
|
82
|
+
audio: new Uint8Array(640),
|
|
83
|
+
});
|
|
84
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
85
|
+
|
|
86
|
+
bus.push(Route.Main, {
|
|
87
|
+
kind: "turn.change",
|
|
88
|
+
contextId: "new-vad-fragment",
|
|
89
|
+
previousContextId: "old-vad-fragment",
|
|
90
|
+
reason: "probe_vad_fragment_split",
|
|
91
|
+
timestampMs: Date.now(),
|
|
92
|
+
});
|
|
93
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
94
|
+
|
|
95
|
+
plugin.forceFinalize("old-vad-fragment");
|
|
96
|
+
await waitUntil(() => finals.length > 0);
|
|
97
|
+
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
98
|
+
|
|
99
|
+
expect(controlMessages).toContain("Finalize");
|
|
100
|
+
expect(finals).toEqual([
|
|
101
|
+
expect.objectContaining({
|
|
102
|
+
contextId: "old-vad-fragment",
|
|
103
|
+
text: "old turn confirmed",
|
|
104
|
+
}),
|
|
105
|
+
]);
|
|
106
|
+
expect(errors).toHaveLength(0);
|
|
107
|
+
expect(metrics).toEqual(expect.arrayContaining([
|
|
108
|
+
expect.objectContaining({
|
|
109
|
+
name: "stt_provider_final_segment",
|
|
110
|
+
contextId: "old-vad-fragment",
|
|
111
|
+
value: expect.stringContaining("\"fromFinalize\":true"),
|
|
112
|
+
}),
|
|
113
|
+
]));
|
|
114
|
+
expect(metrics).not.toEqual(expect.arrayContaining([
|
|
115
|
+
expect.objectContaining({
|
|
116
|
+
name: "stt_provider_finalize_timeout",
|
|
117
|
+
contextId: "old-vad-fragment",
|
|
118
|
+
}),
|
|
119
|
+
]));
|
|
120
|
+
|
|
121
|
+
await plugin.close();
|
|
122
|
+
bus.stop();
|
|
123
|
+
await started;
|
|
124
|
+
await server.close();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("releases provider-final text without speech_final/from_finalize and does not need timeout fallback", async () => {
|
|
128
|
+
const controlMessages: string[] = [];
|
|
129
|
+
const server = await createServer((socket) => {
|
|
130
|
+
socket.on("message", (data, isBinary) => {
|
|
131
|
+
if (isBinary) {
|
|
132
|
+
socket.send(JSON.stringify({
|
|
133
|
+
is_final: true,
|
|
134
|
+
speech_final: false,
|
|
135
|
+
channel: { alternatives: [{ transcript: "just want to complain again so", confidence: 0.84 }] },
|
|
136
|
+
}));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const msg = JSON.parse(data.toString()) as { type?: string };
|
|
140
|
+
if (msg.type) controlMessages.push(msg.type);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
const bus = new PipelineBusImpl();
|
|
144
|
+
const started = bus.start();
|
|
145
|
+
const plugin = new DeepgramSTTPlugin();
|
|
146
|
+
const finals: SttResultPacket[] = [];
|
|
147
|
+
const errors: SttErrorPacket[] = [];
|
|
148
|
+
const metrics: ConversationMetricPacket[] = [];
|
|
149
|
+
bus.on("stt.result", (pkt) => finals.push(pkt as SttResultPacket));
|
|
150
|
+
bus.on("stt.error", (pkt) => errors.push(pkt as SttErrorPacket));
|
|
151
|
+
bus.on("metric.conversation", (pkt) => metrics.push(pkt as ConversationMetricPacket));
|
|
152
|
+
|
|
153
|
+
await plugin.initialize(bus, {
|
|
154
|
+
api_key: "test",
|
|
155
|
+
endpoint_url: server.url,
|
|
156
|
+
sample_rate: 16000,
|
|
157
|
+
emit_eos_on_final: false,
|
|
158
|
+
finalize_on_speech_final: false,
|
|
159
|
+
provider_finalize_timeout_ms: 20,
|
|
160
|
+
finalize_timeout_fallback: false,
|
|
161
|
+
});
|
|
162
|
+
bus.push(Route.Main, {
|
|
163
|
+
kind: "stt.audio",
|
|
164
|
+
contextId: "messy-live-turn",
|
|
165
|
+
timestampMs: Date.now(),
|
|
166
|
+
audio: new Uint8Array(640),
|
|
167
|
+
});
|
|
168
|
+
await waitUntil(() => metrics.some((m) => m.name === "stt_provider_final_segment"));
|
|
169
|
+
await waitUntil(() => finals.length > 0);
|
|
170
|
+
plugin.forceFinalize("messy-live-turn");
|
|
171
|
+
await new Promise((resolve) => setTimeout(resolve, 40));
|
|
172
|
+
|
|
173
|
+
expect(controlMessages).toContain("Finalize");
|
|
174
|
+
expect(finals).toEqual([
|
|
175
|
+
expect.objectContaining({
|
|
176
|
+
contextId: "messy-live-turn",
|
|
177
|
+
text: "just want to complain again so",
|
|
178
|
+
}),
|
|
179
|
+
]);
|
|
180
|
+
expect(errors).toHaveLength(0);
|
|
181
|
+
expect(metrics).toEqual(expect.arrayContaining([
|
|
182
|
+
expect.objectContaining({ name: "stt_provider_final_segment" }),
|
|
183
|
+
]));
|
|
184
|
+
expect(metrics).not.toEqual(expect.arrayContaining([
|
|
185
|
+
expect.objectContaining({ name: "stt_provider_finalize_timeout" }),
|
|
186
|
+
]));
|
|
187
|
+
|
|
188
|
+
await plugin.close();
|
|
189
|
+
bus.stop();
|
|
190
|
+
await started;
|
|
191
|
+
await server.close();
|
|
192
|
+
});
|
|
193
|
+
});
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kuralle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kuralle-syrinx/deepgram",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./src/index.ts",
|
|
8
|
+
"types": "./src/index.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./src/index.ts",
|
|
11
|
+
"./stt": "./src/stt.ts",
|
|
12
|
+
"./tts": "./src/tts.ts"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@kuralle-syrinx/core": "2.1.0",
|
|
16
|
+
"@kuralle-syrinx/ws": "2.1.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/ws": "^8.5.0",
|
|
20
|
+
"typescript": "^5.7.0",
|
|
21
|
+
"vitest": "^2.1.0",
|
|
22
|
+
"ws": "^8.18.0"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"test": "vitest run"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.ts
ADDED