@crewhaus/voice-runtime 0.7.0 → 0.8.1
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/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +9 -0
- package/dist/index.d.ts +33 -31
- package/dist/index.js +140 -13
- package/package.json +9 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
CrewHaus Factory
|
|
2
|
+
Copyright 2026 Max Meier
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Max Meier
|
|
5
|
+
(operating as StudioMax — https://studiomax.io) and contributors.
|
|
6
|
+
|
|
7
|
+
The CrewHaus name and logo are trademarks of Max Meier. See
|
|
8
|
+
https://github.com/crewhaus/factory/blob/main/TRADEMARK.md for the
|
|
9
|
+
use-of-marks policy.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @crewhaus/voice-runtime
|
|
2
|
+
|
|
3
|
+
Realtime voice loop — provider-agnostic RealtimeAdapter + OpenAI Realtime WebSocket adapter (Section 24 VOICE)
|
|
4
|
+
|
|
5
|
+
Part of [CrewHaus](https://github.com/crewhaus/factory). Most people install the CLI (`npm install -g crewhaus`) rather than this package.
|
|
6
|
+
|
|
7
|
+
Requires [Bun](https://bun.sh) `>=1.3.14`; plain Node is not supported.
|
|
8
|
+
|
|
9
|
+
[Source](https://github.com/crewhaus/factory/tree/main/packages/voice-runtime)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Catalog R16 `voice-runtime` — Section 24 VOICE.
|
|
3
|
-
*
|
|
4
|
-
* Provider-agnostic realtime audio adapter. The user-facing surface is
|
|
5
|
-
* a small interface — `RealtimeAdapter` — with one concrete adapter in
|
|
6
|
-
* v0 (`createOpenAIRealtimeAdapter`) and a Vapi stub that lazy-loads.
|
|
7
|
-
*
|
|
8
|
-
* Audio framing: every adapter normalises to 16-bit signed-int PCM at
|
|
9
|
-
* 24kHz mono. The OpenAI adapter speaks this format natively; the Vapi
|
|
10
|
-
* adapter (when implemented) will resample its native Opus stream
|
|
11
|
-
* through a server-side helper. Frames are passed as `Int16Array` so
|
|
12
|
-
* downstream consumers (vad-engine) don't have to re-decode bytes.
|
|
13
|
-
*
|
|
14
|
-
* Event surface:
|
|
15
|
-
* - `transcript_partial { text }` — incremental transcript chunks
|
|
16
|
-
* - `transcript_final { text }` — when the user's utterance ends
|
|
17
|
-
* - `audio_chunk { pcm }` — agent's TTS audio (pcm16 24kHz)
|
|
18
|
-
* - `tool_use { id, name, input }` — agent function calls
|
|
19
|
-
* - `interrupt { reason }` — emitted on barge-in
|
|
20
|
-
* - `disconnect { code?, reason? }` — final
|
|
21
|
-
*
|
|
22
|
-
* The OpenAI adapter is HEADLESS — no microphone, no speakers. The
|
|
23
|
-
* caller pumps PCM in via `sendAudio(pcm)`; the adapter emits audio +
|
|
24
|
-
* transcript events back. This keeps the runtime usable in three
|
|
25
|
-
* deployment modes:
|
|
26
|
-
* 1. Browser → daemon over WebRTC (browser has the mic/speaker).
|
|
27
|
-
* 2. SIP gateway → daemon (telephony provides PCM).
|
|
28
|
-
* 3. Node-only smoke (synthesize PCM in TS, pipe in, assert events).
|
|
29
|
-
*/
|
|
30
1
|
import { CrewhausError } from "@crewhaus/errors";
|
|
31
2
|
export declare class VoiceRuntimeError extends CrewhausError {
|
|
32
3
|
readonly name = "VoiceRuntimeError";
|
|
@@ -71,6 +42,22 @@ export type RealtimeEvent = {
|
|
|
71
42
|
provider: ProviderId;
|
|
72
43
|
ts: string;
|
|
73
44
|
payload: unknown;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The prompt-injection check refused the caller's input: a transcript of
|
|
48
|
+
* their speech (`itemId` is its conversation item, deleted) or text passed
|
|
49
|
+
* to `sendText` (not sent). The detector's rule ids, never the text.
|
|
50
|
+
*/
|
|
51
|
+
| {
|
|
52
|
+
kind: "input_blocked";
|
|
53
|
+
itemId?: string;
|
|
54
|
+
rules: string[];
|
|
55
|
+
}
|
|
56
|
+
/** The check kept the caller's input but flagged it. Rule ids, never the text. */
|
|
57
|
+
| {
|
|
58
|
+
kind: "input_warned";
|
|
59
|
+
itemId?: string;
|
|
60
|
+
rules: string[];
|
|
74
61
|
};
|
|
75
62
|
export type RealtimeEventHandler = (event: RealtimeEvent) => void;
|
|
76
63
|
export type RealtimeConnectOptions = {
|
|
@@ -102,13 +89,28 @@ export interface RealtimeAdapter {
|
|
|
102
89
|
* timing is too slow for a one-shot smoke). Idempotent.
|
|
103
90
|
*/
|
|
104
91
|
commitInput(): void;
|
|
105
|
-
/**
|
|
106
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Inject a text turn (no audio). Useful for tests + chat-only mixed mode.
|
|
94
|
+
* The text is classified at TrustOrigin `channel` before it is sent:
|
|
95
|
+
* malicious text is not sent (an `input_blocked` event; the call goes on),
|
|
96
|
+
* suspicious text is sent with an `input_warned` event. Throws at call time
|
|
97
|
+
* when not connected; otherwise the returned promise resolves once the
|
|
98
|
+
* text is sent or refused, in call order, and never rejects (a send that
|
|
99
|
+
* fails after the check is an `error` event).
|
|
100
|
+
*/
|
|
101
|
+
sendText(text: string): void | Promise<void>;
|
|
107
102
|
/** Cancel the in-flight response. Used by `barge-in-controller`. */
|
|
108
103
|
interrupt(reason: string): void;
|
|
109
104
|
on(handler: RealtimeEventHandler): () => void;
|
|
110
105
|
disconnect(): Promise<void>;
|
|
111
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* The model a session asks to transcribe the caller's speech
|
|
109
|
+
* (`session.audio.input.transcription.model`), so each transcript can be
|
|
110
|
+
* classified before later replies build on it. The provider bills it as
|
|
111
|
+
* transcription.
|
|
112
|
+
*/
|
|
113
|
+
export declare const DEFAULT_INPUT_TRANSCRIPTION_MODEL = "gpt-transcribe";
|
|
112
114
|
/** Test injection: a factory that returns a WebSocket-like (skips `new`). */
|
|
113
115
|
export type WebSocketFactory = (url: string, init?: {
|
|
114
116
|
headers?: Record<string, string>;
|
package/dist/index.js
CHANGED
|
@@ -17,8 +17,19 @@
|
|
|
17
17
|
* - `audio_chunk { pcm }` — agent's TTS audio (pcm16 24kHz)
|
|
18
18
|
* - `tool_use { id, name, input }` — agent function calls
|
|
19
19
|
* - `interrupt { reason }` — emitted on barge-in
|
|
20
|
+
* - `input_blocked { itemId?, rules }` — the prompt-injection check
|
|
21
|
+
* refused the caller's input (a transcript of their speech, or text
|
|
22
|
+
* passed to `sendText`); the call goes on
|
|
23
|
+
* - `input_warned { itemId?, rules }` — it kept the input, flagged
|
|
20
24
|
* - `disconnect { code?, reason? }` — final
|
|
21
25
|
*
|
|
26
|
+
* The caller's input is screened at TrustOrigin `channel`. On the OpenAI
|
|
27
|
+
* Realtime adapter the session asks the provider to transcribe the caller,
|
|
28
|
+
* and each transcript is classified: a malicious one cancels the reply in
|
|
29
|
+
* progress and deletes the caller's item, so later replies do not build on
|
|
30
|
+
* it. Text passed to `sendText` is classified before it is sent, on both
|
|
31
|
+
* adapters. The two input events carry rule ids, never the text.
|
|
32
|
+
*
|
|
22
33
|
* The OpenAI adapter is HEADLESS — no microphone, no speakers. The
|
|
23
34
|
* caller pumps PCM in via `sendAudio(pcm)`; the adapter emits audio +
|
|
24
35
|
* transcript events back. This keeps the runtime usable in three
|
|
@@ -27,6 +38,7 @@
|
|
|
27
38
|
* 2. SIP gateway → daemon (telephony provides PCM).
|
|
28
39
|
* 3. Node-only smoke (synthesize PCM in TS, pipe in, assert events).
|
|
29
40
|
*/
|
|
41
|
+
import { boundaryRuleIds, classifyBoundary, } from "@crewhaus/boundary-classifier";
|
|
30
42
|
import { CrewhausError } from "@crewhaus/errors";
|
|
31
43
|
export class VoiceRuntimeError extends CrewhausError {
|
|
32
44
|
name = "VoiceRuntimeError";
|
|
@@ -34,6 +46,52 @@ export class VoiceRuntimeError extends CrewhausError {
|
|
|
34
46
|
super("runtime", message, cause);
|
|
35
47
|
}
|
|
36
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* One adapter's input lane: each check, and what follows it (a send, a
|
|
51
|
+
* cancel), runs in the order it was queued, and a failure surfaces as an
|
|
52
|
+
* `error` event, never as a rejected promise.
|
|
53
|
+
*/
|
|
54
|
+
function createInputLane(emit) {
|
|
55
|
+
let tail = Promise.resolve();
|
|
56
|
+
return (task) => {
|
|
57
|
+
const next = tail.then(task).catch((err) => {
|
|
58
|
+
emit({ kind: "error", message: `input check: ${errorText(err)}` });
|
|
59
|
+
});
|
|
60
|
+
tail = next;
|
|
61
|
+
return next;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Classify caller input at TrustOrigin `channel`: the action and the rule ids. */
|
|
65
|
+
async function checkCallerInput(text) {
|
|
66
|
+
const boundary = await classifyBoundary(text, { origin: "channel" });
|
|
67
|
+
return { action: boundary.action, rules: boundaryRuleIds(boundary.verdict) };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* `sendText`'s body after the call-time connection check: classify, then
|
|
71
|
+
* `deliver` unless refused. A delivery that throws (the socket closed during
|
|
72
|
+
* the check) is an `error` event.
|
|
73
|
+
*/
|
|
74
|
+
function sendChecked(lane, emit, text, deliver) {
|
|
75
|
+
return lane(async () => {
|
|
76
|
+
const verdict = await checkCallerInput(text);
|
|
77
|
+
if (verdict.action === "redact") {
|
|
78
|
+
emit({ kind: "input_blocked", rules: verdict.rules });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
deliver();
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
emit({ kind: "error", message: `sendText: ${errorText(err)}` });
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (verdict.action === "warn")
|
|
89
|
+
emit({ kind: "input_warned", rules: verdict.rules });
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function errorText(err) {
|
|
93
|
+
return err instanceof Error ? err.message : String(err);
|
|
94
|
+
}
|
|
37
95
|
// ---------------------------------------------------------------------------
|
|
38
96
|
// OpenAI Realtime adapter — speaks the GA `/v1/realtime` WebSocket protocol.
|
|
39
97
|
//
|
|
@@ -54,6 +112,8 @@ export class VoiceRuntimeError extends CrewhausError {
|
|
|
54
112
|
// - server events renamed: `response.audio.delta` →
|
|
55
113
|
// `response.output_audio.delta`, `response.audio_transcript.{delta,done}`
|
|
56
114
|
// → `response.output_audio_transcript.{delta,done}`
|
|
115
|
+
// - input transcription is `audio.input.transcription` (the beta
|
|
116
|
+
// `input_audio_transcription` field is never sent)
|
|
57
117
|
//
|
|
58
118
|
// The READ path still accepts the legacy event names as aliases — they cost
|
|
59
119
|
// three case labels and keep the adapter usable against a gateway or proxy
|
|
@@ -68,6 +128,13 @@ const OPENAI_REALTIME_SAMPLE_RATE = 24_000;
|
|
|
68
128
|
*/
|
|
69
129
|
const DEFAULT_OPENAI_REALTIME_MODEL = "gpt-realtime";
|
|
70
130
|
const DEFAULT_OPENAI_REALTIME_URL = "wss://api.openai.com/v1/realtime";
|
|
131
|
+
/**
|
|
132
|
+
* The model a session asks to transcribe the caller's speech
|
|
133
|
+
* (`session.audio.input.transcription.model`), so each transcript can be
|
|
134
|
+
* classified before later replies build on it. The provider bills it as
|
|
135
|
+
* transcription.
|
|
136
|
+
*/
|
|
137
|
+
export const DEFAULT_INPUT_TRANSCRIPTION_MODEL = "gpt-transcribe";
|
|
71
138
|
export function createOpenAIRealtimeAdapter(opts = {}) {
|
|
72
139
|
let ws;
|
|
73
140
|
let connected = false;
|
|
@@ -83,11 +150,48 @@ export function createOpenAIRealtimeAdapter(opts = {}) {
|
|
|
83
150
|
}
|
|
84
151
|
}
|
|
85
152
|
}
|
|
86
|
-
function
|
|
153
|
+
function openSocket() {
|
|
87
154
|
if (ws === undefined || ws.readyState !== 1 /* OPEN */) {
|
|
88
155
|
throw new VoiceRuntimeError("adapter is not connected (or socket is not open)");
|
|
89
156
|
}
|
|
90
|
-
ws
|
|
157
|
+
return ws;
|
|
158
|
+
}
|
|
159
|
+
function send(message) {
|
|
160
|
+
openSocket().send(JSON.stringify(message));
|
|
161
|
+
}
|
|
162
|
+
const inputLane = createInputLane(emit);
|
|
163
|
+
/**
|
|
164
|
+
* A transcript of the caller's speech (`undefined` when the provider could
|
|
165
|
+
* not make one), classified at `channel`. Malicious, or unreadable: the
|
|
166
|
+
* reply in progress is cancelled and the caller's item deleted, so later
|
|
167
|
+
* replies do not build on it, and `input_blocked` is emitted; the call goes
|
|
168
|
+
* on. Suspicious: `input_warned`. The transcript is never emitted.
|
|
169
|
+
*/
|
|
170
|
+
function checkTranscript(itemId, transcript) {
|
|
171
|
+
void inputLane(async () => {
|
|
172
|
+
const verdict = transcript === undefined
|
|
173
|
+
? { action: "redact", rules: ["transcription-failed"] }
|
|
174
|
+
: await checkCallerInput(transcript);
|
|
175
|
+
const item = itemId !== undefined ? { itemId } : {};
|
|
176
|
+
if (verdict.action === "redact") {
|
|
177
|
+
// Each send tolerates a closed socket: the verdict is reported either way.
|
|
178
|
+
for (const message of [
|
|
179
|
+
{ type: "response.cancel" },
|
|
180
|
+
...(itemId !== undefined ? [{ type: "conversation.item.delete", item_id: itemId }] : []),
|
|
181
|
+
]) {
|
|
182
|
+
try {
|
|
183
|
+
send(message);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
/* socket closed: nothing left to cancel or delete */
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
emit({ kind: "input_blocked", ...item, rules: verdict.rules });
|
|
190
|
+
}
|
|
191
|
+
else if (verdict.action === "warn") {
|
|
192
|
+
emit({ kind: "input_warned", ...item, rules: verdict.rules });
|
|
193
|
+
}
|
|
194
|
+
});
|
|
91
195
|
}
|
|
92
196
|
function handleServerEvent(raw) {
|
|
93
197
|
if (typeof raw !== "object" || raw === null)
|
|
@@ -165,6 +269,16 @@ export function createOpenAIRealtimeAdapter(opts = {}) {
|
|
|
165
269
|
case "response.cancel":
|
|
166
270
|
emit({ kind: "interrupt", reason: "response cancelled" });
|
|
167
271
|
break;
|
|
272
|
+
// The caller's own speech, transcribed because the session asks for it
|
|
273
|
+
// (the transcript events above are the AGENT's). Each one is checked.
|
|
274
|
+
case "conversation.item.input_audio_transcription.completed": {
|
|
275
|
+
const transcript = ev["transcript"];
|
|
276
|
+
checkTranscript(typeof ev["item_id"] === "string" ? ev["item_id"] : undefined, typeof transcript === "string" ? transcript : undefined);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
case "conversation.item.input_audio_transcription.failed":
|
|
280
|
+
checkTranscript(typeof ev["item_id"] === "string" ? ev["item_id"] : undefined, undefined);
|
|
281
|
+
break;
|
|
168
282
|
case "error": {
|
|
169
283
|
const errObj = ev["error"];
|
|
170
284
|
emit({ kind: "error", message: errObj?.message ?? "unknown error" });
|
|
@@ -243,6 +357,8 @@ export function createOpenAIRealtimeAdapter(opts = {}) {
|
|
|
243
357
|
// because GA rejects a combined ["text","audio"] request; the audio
|
|
244
358
|
// response still streams transcript deltas, which is where this
|
|
245
359
|
// adapter's transcript_partial/transcript_final events come from.
|
|
360
|
+
// `audio.input.transcription` asks for the CALLER's transcript too, so
|
|
361
|
+
// each utterance can be classified (see checkTranscript).
|
|
246
362
|
send({
|
|
247
363
|
type: "session.update",
|
|
248
364
|
session: {
|
|
@@ -256,6 +372,7 @@ export function createOpenAIRealtimeAdapter(opts = {}) {
|
|
|
256
372
|
input: {
|
|
257
373
|
format: { type: "audio/pcm", rate: OPENAI_REALTIME_SAMPLE_RATE },
|
|
258
374
|
turn_detection: connectOpts.vad === "none" ? null : { type: "server_vad" },
|
|
375
|
+
transcription: { model: DEFAULT_INPUT_TRANSCRIPTION_MODEL },
|
|
259
376
|
},
|
|
260
377
|
output: {
|
|
261
378
|
// `rate` is REQUIRED on an audio/pcm output format — the server
|
|
@@ -294,15 +411,18 @@ export function createOpenAIRealtimeAdapter(opts = {}) {
|
|
|
294
411
|
}
|
|
295
412
|
},
|
|
296
413
|
sendText(text) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
type: "
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
414
|
+
openSocket(); // not connected: refused at call time
|
|
415
|
+
return sendChecked(inputLane, emit, text, () => {
|
|
416
|
+
send({
|
|
417
|
+
type: "conversation.item.create",
|
|
418
|
+
item: {
|
|
419
|
+
type: "message",
|
|
420
|
+
role: "user",
|
|
421
|
+
content: [{ type: "input_text", text }],
|
|
422
|
+
},
|
|
423
|
+
});
|
|
424
|
+
send({ type: "response.create" });
|
|
304
425
|
});
|
|
305
|
-
send({ type: "response.create" });
|
|
306
426
|
},
|
|
307
427
|
interrupt(reason) {
|
|
308
428
|
try {
|
|
@@ -360,12 +480,16 @@ export function createVapiRealtimeAdapter(opts = {}) {
|
|
|
360
480
|
}
|
|
361
481
|
}
|
|
362
482
|
}
|
|
363
|
-
function
|
|
483
|
+
function openSocket() {
|
|
364
484
|
if (ws === undefined || ws.readyState !== 1) {
|
|
365
485
|
throw new VoiceRuntimeError("vapi adapter is not connected (or socket is not open)");
|
|
366
486
|
}
|
|
367
|
-
ws
|
|
487
|
+
return ws;
|
|
368
488
|
}
|
|
489
|
+
function send(message) {
|
|
490
|
+
openSocket().send(JSON.stringify(message));
|
|
491
|
+
}
|
|
492
|
+
const inputLane = createInputLane(emit);
|
|
369
493
|
return {
|
|
370
494
|
providerId: "vapi",
|
|
371
495
|
sampleRate: OPENAI_REALTIME_SAMPLE_RATE,
|
|
@@ -434,7 +558,10 @@ export function createVapiRealtimeAdapter(opts = {}) {
|
|
|
434
558
|
send({ type: "input_audio_buffer.commit" });
|
|
435
559
|
},
|
|
436
560
|
sendText(text) {
|
|
437
|
-
|
|
561
|
+
openSocket(); // not connected: refused at call time
|
|
562
|
+
return sendChecked(inputLane, emit, text, () => {
|
|
563
|
+
send({ type: "response.create", response: { modalities: ["text", "audio"], input: text } });
|
|
564
|
+
});
|
|
438
565
|
},
|
|
439
566
|
interrupt(_reason) {
|
|
440
567
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crewhaus/voice-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Realtime voice loop — provider-agnostic RealtimeAdapter + OpenAI Realtime WebSocket adapter (Section 24 VOICE)",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -8,14 +8,16 @@
|
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js"
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"scripts": {
|
|
15
16
|
"test": "bun test src"
|
|
16
17
|
},
|
|
17
18
|
"dependencies": {
|
|
18
|
-
"@crewhaus/
|
|
19
|
+
"@crewhaus/boundary-classifier": "0.8.1",
|
|
20
|
+
"@crewhaus/errors": "0.8.1"
|
|
19
21
|
},
|
|
20
22
|
"license": "Apache-2.0",
|
|
21
23
|
"author": {
|
|
@@ -35,5 +37,8 @@
|
|
|
35
37
|
"publishConfig": {
|
|
36
38
|
"access": "public"
|
|
37
39
|
},
|
|
38
|
-
"files": ["dist", "README.md", "LICENSE", "NOTICE"]
|
|
40
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
41
|
+
"engines": {
|
|
42
|
+
"bun": ">=1.3.14"
|
|
43
|
+
}
|
|
39
44
|
}
|