@memberjunction/ai-bridge-server 6.2.0-edge.2 → 6.2.0-edge.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/ai-bridge-engine.d.ts +273 -3
- package/dist/ai-bridge-engine.d.ts.map +1 -1
- package/dist/ai-bridge-engine.js +560 -39
- package/dist/ai-bridge-engine.js.map +1 -1
- package/dist/bridge-prior-transcript.d.ts +39 -0
- package/dist/bridge-prior-transcript.d.ts.map +1 -0
- package/dist/bridge-prior-transcript.js +56 -0
- package/dist/bridge-prior-transcript.js.map +1 -0
- package/dist/calendar-watcher.d.ts.map +1 -1
- package/dist/calendar-watcher.js +1 -0
- package/dist/calendar-watcher.js.map +1 -1
- package/dist/dtmf-coalescer.d.ts +32 -0
- package/dist/dtmf-coalescer.d.ts.map +1 -0
- package/dist/dtmf-coalescer.js +60 -0
- package/dist/dtmf-coalescer.js.map +1 -0
- package/dist/full-duplex-turn-gate.d.ts +137 -0
- package/dist/full-duplex-turn-gate.d.ts.map +1 -0
- package/dist/full-duplex-turn-gate.js +207 -0
- package/dist/full-duplex-turn-gate.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/multi-agent-room-coordinator.d.ts +226 -24
- package/dist/multi-agent-room-coordinator.d.ts.map +1 -1
- package/dist/multi-agent-room-coordinator.js +376 -39
- package/dist/multi-agent-room-coordinator.js.map +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The full-duplex turn gate — the **safety net** behind a full-duplex model's own turn-taking judgement.
|
|
3
|
+
*
|
|
4
|
+
* A turn-based model waits to be triggered, so the engine decides WHEN it speaks. A full-duplex model
|
|
5
|
+
* (GPT-Live, the Gemini 3.8 Live models) listens while it speaks and decides for itself, so the first the
|
|
6
|
+
* engine hears of a turn is the model's own outbound audio. This gate sits on that outbound path and asks the
|
|
7
|
+
* room's {@link MultiAgentRoomCoordinator} whether the speech may proceed:
|
|
8
|
+
*
|
|
9
|
+
* - floor granted → the audio flows and the agent holds the floor until its turn ends;
|
|
10
|
+
* - another agent holds the floor (or one is reserved by a hand-off) → the audio is let through ONLY while
|
|
11
|
+
* it is still short enough to be a backchannel ({@link BACKCHANNEL_MAX_DURATION_MS}); the moment it runs
|
|
12
|
+
* longer it is a turn that is not allowed to start, and it is cut;
|
|
13
|
+
* - a human is speaking or the agent-to-agent loop cap is reached → cut at once.
|
|
14
|
+
*
|
|
15
|
+
* It is pure: no media, no entities, no timers — every dependency (coordinator, clock) is injected, so the
|
|
16
|
+
* whole policy is unit-testable. The engine owns the side effects (flushing queued audio, arming the floor
|
|
17
|
+
* safety timer) and acts on the returned {@link OutputGateResult}.
|
|
18
|
+
*
|
|
19
|
+
* Audio is assumed to be PCM16 mono — what the full-duplex models emit and what multi-agent rooms carry.
|
|
20
|
+
*/
|
|
21
|
+
import { type UtteranceShape } from '@memberjunction/ai-bridge-base';
|
|
22
|
+
import type { MultiAgentRoomCoordinator } from './multi-agent-room-coordinator.js';
|
|
23
|
+
/** A pause in outbound audio longer than this ends a burst for a session that is not holding the floor (ms). */
|
|
24
|
+
export declare const OUTPUT_BURST_GAP_MS = 1200;
|
|
25
|
+
/** Where an agent's current speech burst stands relative to the floor. */
|
|
26
|
+
export type OutputGatePhase =
|
|
27
|
+
/** Not speaking. The next outbound audio starts a new burst. */
|
|
28
|
+
'Idle'
|
|
29
|
+
/** Holding the floor — a real turn. */
|
|
30
|
+
| 'Floor'
|
|
31
|
+
/** Speaking over someone else's floor, still short enough to be a backchannel. */
|
|
32
|
+
| 'Overlay'
|
|
33
|
+
/** Refused: the rest of this burst is dropped. */
|
|
34
|
+
| 'Muted';
|
|
35
|
+
/** What the engine should do with one outbound audio chunk. */
|
|
36
|
+
export type OutputVerdict = 'Forward' | 'Drop' | 'Cut';
|
|
37
|
+
/** The gate's answer for one outbound chunk. */
|
|
38
|
+
export interface OutputGateResult {
|
|
39
|
+
/** `Forward`: send it. `Drop`: discard it. `Cut`: discard it AND flush audio already queued (the burst was just refused). */
|
|
40
|
+
Verdict: OutputVerdict;
|
|
41
|
+
/** `true` when THIS chunk took the floor — the engine arms its floor safety timer. */
|
|
42
|
+
TookFloor: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** Dependencies of a {@link FullDuplexTurnGate}. */
|
|
45
|
+
export interface FullDuplexTurnGateDeps {
|
|
46
|
+
/** The room's coordinator. */
|
|
47
|
+
Coordinator: MultiAgentRoomCoordinator;
|
|
48
|
+
/** The room id the coordinator keys on. */
|
|
49
|
+
RoomId: string;
|
|
50
|
+
/** This agent's `MJ: AI Agent Sessions` id. */
|
|
51
|
+
AgentSessionId: string;
|
|
52
|
+
/** The model's output sample rate (Hz) — converts bytes to duration. Defaults to 24000. */
|
|
53
|
+
SampleRateHz?: number;
|
|
54
|
+
/** Injected clock (epoch ms). Defaults to `Date.now`. */
|
|
55
|
+
Now?: () => number;
|
|
56
|
+
}
|
|
57
|
+
/** Per-agent gate over a full-duplex model's outbound audio. One instance per bridged session. */
|
|
58
|
+
export declare class FullDuplexTurnGate {
|
|
59
|
+
private readonly coordinator;
|
|
60
|
+
private readonly roomId;
|
|
61
|
+
private readonly agentSessionId;
|
|
62
|
+
private readonly sampleRateHz;
|
|
63
|
+
private readonly now;
|
|
64
|
+
private phase;
|
|
65
|
+
private burstAudioMs;
|
|
66
|
+
private lastChunkAtMs;
|
|
67
|
+
constructor(deps: FullDuplexTurnGateDeps);
|
|
68
|
+
/** The current phase (observability + tests). */
|
|
69
|
+
get Phase(): OutputGatePhase;
|
|
70
|
+
/**
|
|
71
|
+
* Decides what to do with one chunk of the model's outbound audio.
|
|
72
|
+
*
|
|
73
|
+
* @param byteLength The chunk's size in bytes (PCM16 mono).
|
|
74
|
+
* @returns Whether to forward, drop, or cut — and whether this chunk took the floor.
|
|
75
|
+
*/
|
|
76
|
+
OnOutputAudio(byteLength: number): OutputGateResult;
|
|
77
|
+
/**
|
|
78
|
+
* The agent's turn ended (its final assistant transcript). Releases the floor — reclassifying a short
|
|
79
|
+
* utterance as a backchannel so it is not billed against the loop cap — or records an overlay as the
|
|
80
|
+
* backchannel it was. Always returns the gate to `Idle`.
|
|
81
|
+
*
|
|
82
|
+
* @param utterance What was said, when known (the measured burst duration fills in `DurationMs`).
|
|
83
|
+
*/
|
|
84
|
+
EndTurn(utterance?: UtteranceShape): void;
|
|
85
|
+
/** A human preempted this agent: mute the rest of the current burst. The floor itself is the coordinator's to release. */
|
|
86
|
+
Cut(): void;
|
|
87
|
+
/** Returns to `Idle` with no coordinator side effects (teardown, barge-in). */
|
|
88
|
+
Reset(): void;
|
|
89
|
+
/** A pause between chunks ends a burst for a session that is not holding the floor (it holds it until `EndTurn`). */
|
|
90
|
+
private endStaleBurst;
|
|
91
|
+
/** First chunk of a new burst: take the floor, overlay as a possible backchannel, or refuse. */
|
|
92
|
+
private startBurst;
|
|
93
|
+
/** A turn in progress: keep forwarding while the agent still holds the floor, cut it the moment it does not. */
|
|
94
|
+
private continueFloor;
|
|
95
|
+
/** An overlay: still a backchannel while short; once it runs long it is a turn that must now win the floor or be cut. */
|
|
96
|
+
private continueOverlay;
|
|
97
|
+
/** Forwards an overlay while its accumulated duration is still a backchannel; past the bound it must win the floor or be cut. */
|
|
98
|
+
private admitOverlay;
|
|
99
|
+
/** Mutes the rest of the burst. The caller has already recorded the denial through the coordinator. */
|
|
100
|
+
private refuse;
|
|
101
|
+
private reset;
|
|
102
|
+
}
|
|
103
|
+
/** RMS level (of 32768 full scale) above which inbound audio is treated as speech rather than room noise. */
|
|
104
|
+
export declare const HUMAN_SPEECH_RMS_THRESHOLD = 700;
|
|
105
|
+
/** Minimum spacing (ms) between speech reports to the coordinator — frames arrive every ~20 ms. */
|
|
106
|
+
export declare const HUMAN_SPEECH_REPORT_INTERVAL_MS = 100;
|
|
107
|
+
/**
|
|
108
|
+
* Root-mean-square level of a PCM16 little-endian buffer.
|
|
109
|
+
*
|
|
110
|
+
* @param pcm16 Raw PCM16 mono samples.
|
|
111
|
+
* @returns The RMS in sample units (0 for an empty buffer).
|
|
112
|
+
*/
|
|
113
|
+
export declare function ComputePcm16Rms(pcm16: ArrayBuffer): number;
|
|
114
|
+
/**
|
|
115
|
+
* Energy-based detector of human speech in inbound room audio. Deliberately simple: it only has to be
|
|
116
|
+
* right enough to tell the coordinator "a person is talking" so agents yield; the room's own transcripts
|
|
117
|
+
* remain the record of what was said. It throttles its reports so the coordinator is not called per frame.
|
|
118
|
+
*/
|
|
119
|
+
export declare class HumanSpeechDetector {
|
|
120
|
+
private readonly now;
|
|
121
|
+
private readonly threshold;
|
|
122
|
+
private lastReportAtMs;
|
|
123
|
+
/**
|
|
124
|
+
* @param now Injected clock (epoch ms). Defaults to `Date.now`.
|
|
125
|
+
* @param threshold RMS threshold. Defaults to {@link HUMAN_SPEECH_RMS_THRESHOLD}.
|
|
126
|
+
*/
|
|
127
|
+
constructor(now?: () => number, threshold?: number);
|
|
128
|
+
/**
|
|
129
|
+
* Inspects one inbound PCM16 frame from a HUMAN participant.
|
|
130
|
+
*
|
|
131
|
+
* @param pcm16 The frame's samples.
|
|
132
|
+
* @returns `true` when the caller should report human speech activity to the coordinator now (the frame
|
|
133
|
+
* is speech-like AND the report interval has elapsed); `false` otherwise.
|
|
134
|
+
*/
|
|
135
|
+
ShouldReportSpeech(pcm16: ArrayBuffer): boolean;
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=full-duplex-turn-gate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"full-duplex-turn-gate.d.ts","sourceRoot":"","sources":["../src/full-duplex-turn-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAA+B,KAAK,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAClG,OAAO,KAAK,EAAuB,yBAAyB,EAAE,MAAM,gCAAgC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;;;OAMG;IACI,OAAO,CAAC,SAAS,GAAE,cAAmB,GAAG,IAAI;IASpD,0HAA0H;IACnH,GAAG,IAAI,IAAI;IAIlB,+EAA+E;IACxE,KAAK,IAAI,IAAI;IAMpB,qHAAqH;IACrH,OAAO,CAAC,aAAa;IAWrB,gGAAgG;IAChG,OAAO,CAAC,UAAU;IAgBlB,gHAAgH;IAChH,OAAO,CAAC,aAAa;IASrB,yHAAyH;IACzH,OAAO,CAAC,eAAe;IAKvB,iIAAiI;IACjI,OAAO,CAAC,YAAY;IAYpB,uGAAuG;IACvG,OAAO,CAAC,MAAM;IAKd,OAAO,CAAC,KAAK;CAIhB;AAED,6GAA6G;AAC7G,eAAO,MAAM,0BAA0B,MAAM,CAAC;AAE9C,mGAAmG;AACnG,eAAO,MAAM,+BAA+B,MAAM,CAAC;AAEnD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAU1D;AAED;;;;GAIG;AACH,qBAAa,mBAAmB;IAC5B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,cAAc,CAAK;IAE3B;;;OAGG;gBACS,GAAG,GAAE,MAAM,MAAiB,EAAE,SAAS,GAAE,MAAmC;IAKxF;;;;;;OAMG;IACI,kBAAkB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;CAWzD"}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The full-duplex turn gate — the **safety net** behind a full-duplex model's own turn-taking judgement.
|
|
3
|
+
*
|
|
4
|
+
* A turn-based model waits to be triggered, so the engine decides WHEN it speaks. A full-duplex model
|
|
5
|
+
* (GPT-Live, the Gemini 3.8 Live models) listens while it speaks and decides for itself, so the first the
|
|
6
|
+
* engine hears of a turn is the model's own outbound audio. This gate sits on that outbound path and asks the
|
|
7
|
+
* room's {@link MultiAgentRoomCoordinator} whether the speech may proceed:
|
|
8
|
+
*
|
|
9
|
+
* - floor granted → the audio flows and the agent holds the floor until its turn ends;
|
|
10
|
+
* - another agent holds the floor (or one is reserved by a hand-off) → the audio is let through ONLY while
|
|
11
|
+
* it is still short enough to be a backchannel ({@link BACKCHANNEL_MAX_DURATION_MS}); the moment it runs
|
|
12
|
+
* longer it is a turn that is not allowed to start, and it is cut;
|
|
13
|
+
* - a human is speaking or the agent-to-agent loop cap is reached → cut at once.
|
|
14
|
+
*
|
|
15
|
+
* It is pure: no media, no entities, no timers — every dependency (coordinator, clock) is injected, so the
|
|
16
|
+
* whole policy is unit-testable. The engine owns the side effects (flushing queued audio, arming the floor
|
|
17
|
+
* safety timer) and acts on the returned {@link OutputGateResult}.
|
|
18
|
+
*
|
|
19
|
+
* Audio is assumed to be PCM16 mono — what the full-duplex models emit and what multi-agent rooms carry.
|
|
20
|
+
*/
|
|
21
|
+
import { BACKCHANNEL_MAX_DURATION_MS } from '@memberjunction/ai-bridge-base';
|
|
22
|
+
/** A pause in outbound audio longer than this ends a burst for a session that is not holding the floor (ms). */
|
|
23
|
+
export const OUTPUT_BURST_GAP_MS = 1200;
|
|
24
|
+
/** Denials that mean "someone else has the floor right now" — a short utterance may still overlay as a backchannel. */
|
|
25
|
+
const OVERLAYABLE_DENIALS = new Set(['HeldByOtherAgent', 'ReservedForHandoff']);
|
|
26
|
+
/** Per-agent gate over a full-duplex model's outbound audio. One instance per bridged session. */
|
|
27
|
+
export class FullDuplexTurnGate {
|
|
28
|
+
constructor(deps) {
|
|
29
|
+
this.phase = 'Idle';
|
|
30
|
+
this.burstAudioMs = 0;
|
|
31
|
+
this.lastChunkAtMs = 0;
|
|
32
|
+
this.coordinator = deps.Coordinator;
|
|
33
|
+
this.roomId = deps.RoomId;
|
|
34
|
+
this.agentSessionId = deps.AgentSessionId;
|
|
35
|
+
this.sampleRateHz = deps.SampleRateHz && deps.SampleRateHz > 0 ? deps.SampleRateHz : 24000;
|
|
36
|
+
this.now = deps.Now ?? Date.now;
|
|
37
|
+
}
|
|
38
|
+
/** The current phase (observability + tests). */
|
|
39
|
+
get Phase() {
|
|
40
|
+
return this.phase;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Decides what to do with one chunk of the model's outbound audio.
|
|
44
|
+
*
|
|
45
|
+
* @param byteLength The chunk's size in bytes (PCM16 mono).
|
|
46
|
+
* @returns Whether to forward, drop, or cut — and whether this chunk took the floor.
|
|
47
|
+
*/
|
|
48
|
+
OnOutputAudio(byteLength) {
|
|
49
|
+
const now = this.now();
|
|
50
|
+
this.endStaleBurst(now);
|
|
51
|
+
this.lastChunkAtMs = now;
|
|
52
|
+
const chunkMs = (byteLength / 2 / this.sampleRateHz) * 1000;
|
|
53
|
+
switch (this.phase) {
|
|
54
|
+
case 'Idle':
|
|
55
|
+
return this.startBurst(chunkMs);
|
|
56
|
+
case 'Floor':
|
|
57
|
+
return this.continueFloor(chunkMs);
|
|
58
|
+
case 'Overlay':
|
|
59
|
+
return this.continueOverlay(chunkMs);
|
|
60
|
+
default:
|
|
61
|
+
return { Verdict: 'Drop', TookFloor: false };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The agent's turn ended (its final assistant transcript). Releases the floor — reclassifying a short
|
|
66
|
+
* utterance as a backchannel so it is not billed against the loop cap — or records an overlay as the
|
|
67
|
+
* backchannel it was. Always returns the gate to `Idle`.
|
|
68
|
+
*
|
|
69
|
+
* @param utterance What was said, when known (the measured burst duration fills in `DurationMs`).
|
|
70
|
+
*/
|
|
71
|
+
EndTurn(utterance = {}) {
|
|
72
|
+
if (this.phase === 'Floor') {
|
|
73
|
+
this.coordinator.CompleteTurn(this.roomId, this.agentSessionId, { Text: utterance.Text, DurationMs: utterance.DurationMs ?? this.burstAudioMs });
|
|
74
|
+
}
|
|
75
|
+
else if (this.phase === 'Overlay') {
|
|
76
|
+
this.coordinator.RecordBackchannel(this.roomId, this.agentSessionId);
|
|
77
|
+
}
|
|
78
|
+
this.reset();
|
|
79
|
+
}
|
|
80
|
+
/** A human preempted this agent: mute the rest of the current burst. The floor itself is the coordinator's to release. */
|
|
81
|
+
Cut() {
|
|
82
|
+
this.phase = 'Muted';
|
|
83
|
+
}
|
|
84
|
+
/** Returns to `Idle` with no coordinator side effects (teardown, barge-in). */
|
|
85
|
+
Reset() {
|
|
86
|
+
this.reset();
|
|
87
|
+
}
|
|
88
|
+
// ── internals ────────────────────────────────────────────────────────────────
|
|
89
|
+
/** A pause between chunks ends a burst for a session that is not holding the floor (it holds it until `EndTurn`). */
|
|
90
|
+
endStaleBurst(now) {
|
|
91
|
+
const stale = this.lastChunkAtMs > 0 && now - this.lastChunkAtMs > OUTPUT_BURST_GAP_MS;
|
|
92
|
+
if (!stale || this.phase === 'Floor') {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (this.phase === 'Overlay') {
|
|
96
|
+
this.coordinator.RecordBackchannel(this.roomId, this.agentSessionId);
|
|
97
|
+
}
|
|
98
|
+
this.reset();
|
|
99
|
+
}
|
|
100
|
+
/** First chunk of a new burst: take the floor, overlay as a possible backchannel, or refuse. */
|
|
101
|
+
startBurst(chunkMs) {
|
|
102
|
+
this.burstAudioMs = chunkMs;
|
|
103
|
+
const decision = this.coordinator.CanTakeFloor(this.roomId, this.agentSessionId, 'Turn');
|
|
104
|
+
if (decision.Granted) {
|
|
105
|
+
this.coordinator.TakeFloor(this.roomId, this.agentSessionId, 'Turn');
|
|
106
|
+
this.phase = 'Floor';
|
|
107
|
+
return { Verdict: 'Forward', TookFloor: true };
|
|
108
|
+
}
|
|
109
|
+
if (OVERLAYABLE_DENIALS.has(decision.Reason)) {
|
|
110
|
+
this.phase = 'Overlay';
|
|
111
|
+
return this.admitOverlay();
|
|
112
|
+
}
|
|
113
|
+
this.coordinator.TakeFloor(this.roomId, this.agentSessionId, 'Turn'); // records the denial in the room's events
|
|
114
|
+
return this.refuse();
|
|
115
|
+
}
|
|
116
|
+
/** A turn in progress: keep forwarding while the agent still holds the floor, cut it the moment it does not. */
|
|
117
|
+
continueFloor(chunkMs) {
|
|
118
|
+
if (!this.coordinator.IsFloorHolder(this.roomId, this.agentSessionId)) {
|
|
119
|
+
this.phase = 'Muted'; // preempted (a human started talking, or the facilitator took the floor)
|
|
120
|
+
return { Verdict: 'Cut', TookFloor: false };
|
|
121
|
+
}
|
|
122
|
+
this.burstAudioMs += chunkMs;
|
|
123
|
+
return { Verdict: 'Forward', TookFloor: false };
|
|
124
|
+
}
|
|
125
|
+
/** An overlay: still a backchannel while short; once it runs long it is a turn that must now win the floor or be cut. */
|
|
126
|
+
continueOverlay(chunkMs) {
|
|
127
|
+
this.burstAudioMs += chunkMs;
|
|
128
|
+
return this.admitOverlay();
|
|
129
|
+
}
|
|
130
|
+
/** Forwards an overlay while its accumulated duration is still a backchannel; past the bound it must win the floor or be cut. */
|
|
131
|
+
admitOverlay() {
|
|
132
|
+
if (this.burstAudioMs <= BACKCHANNEL_MAX_DURATION_MS) {
|
|
133
|
+
return { Verdict: 'Forward', TookFloor: false };
|
|
134
|
+
}
|
|
135
|
+
const decision = this.coordinator.TakeFloor(this.roomId, this.agentSessionId, 'Turn');
|
|
136
|
+
if (decision.Granted) {
|
|
137
|
+
this.phase = 'Floor';
|
|
138
|
+
return { Verdict: 'Forward', TookFloor: true };
|
|
139
|
+
}
|
|
140
|
+
return this.refuse();
|
|
141
|
+
}
|
|
142
|
+
/** Mutes the rest of the burst. The caller has already recorded the denial through the coordinator. */
|
|
143
|
+
refuse() {
|
|
144
|
+
this.phase = 'Muted';
|
|
145
|
+
return { Verdict: 'Cut', TookFloor: false };
|
|
146
|
+
}
|
|
147
|
+
reset() {
|
|
148
|
+
this.phase = 'Idle';
|
|
149
|
+
this.burstAudioMs = 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** RMS level (of 32768 full scale) above which inbound audio is treated as speech rather than room noise. */
|
|
153
|
+
export const HUMAN_SPEECH_RMS_THRESHOLD = 700;
|
|
154
|
+
/** Minimum spacing (ms) between speech reports to the coordinator — frames arrive every ~20 ms. */
|
|
155
|
+
export const HUMAN_SPEECH_REPORT_INTERVAL_MS = 100;
|
|
156
|
+
/**
|
|
157
|
+
* Root-mean-square level of a PCM16 little-endian buffer.
|
|
158
|
+
*
|
|
159
|
+
* @param pcm16 Raw PCM16 mono samples.
|
|
160
|
+
* @returns The RMS in sample units (0 for an empty buffer).
|
|
161
|
+
*/
|
|
162
|
+
export function ComputePcm16Rms(pcm16) {
|
|
163
|
+
const samples = new Int16Array(pcm16, 0, Math.floor(pcm16.byteLength / 2));
|
|
164
|
+
if (samples.length === 0) {
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
let sumSquares = 0;
|
|
168
|
+
for (let i = 0; i < samples.length; i++) {
|
|
169
|
+
sumSquares += samples[i] * samples[i];
|
|
170
|
+
}
|
|
171
|
+
return Math.sqrt(sumSquares / samples.length);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Energy-based detector of human speech in inbound room audio. Deliberately simple: it only has to be
|
|
175
|
+
* right enough to tell the coordinator "a person is talking" so agents yield; the room's own transcripts
|
|
176
|
+
* remain the record of what was said. It throttles its reports so the coordinator is not called per frame.
|
|
177
|
+
*/
|
|
178
|
+
export class HumanSpeechDetector {
|
|
179
|
+
/**
|
|
180
|
+
* @param now Injected clock (epoch ms). Defaults to `Date.now`.
|
|
181
|
+
* @param threshold RMS threshold. Defaults to {@link HUMAN_SPEECH_RMS_THRESHOLD}.
|
|
182
|
+
*/
|
|
183
|
+
constructor(now = Date.now, threshold = HUMAN_SPEECH_RMS_THRESHOLD) {
|
|
184
|
+
this.lastReportAtMs = 0;
|
|
185
|
+
this.now = now;
|
|
186
|
+
this.threshold = threshold;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Inspects one inbound PCM16 frame from a HUMAN participant.
|
|
190
|
+
*
|
|
191
|
+
* @param pcm16 The frame's samples.
|
|
192
|
+
* @returns `true` when the caller should report human speech activity to the coordinator now (the frame
|
|
193
|
+
* is speech-like AND the report interval has elapsed); `false` otherwise.
|
|
194
|
+
*/
|
|
195
|
+
ShouldReportSpeech(pcm16) {
|
|
196
|
+
if (ComputePcm16Rms(pcm16) < this.threshold) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
const now = this.now();
|
|
200
|
+
if (now - this.lastReportAtMs < HUMAN_SPEECH_REPORT_INTERVAL_MS) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
this.lastReportAtMs = now;
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
//# sourceMappingURL=full-duplex-turn-gate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"full-duplex-turn-gate.js","sourceRoot":"","sources":["../src/full-duplex-turn-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,2BAA2B,EAAuB,MAAM,gCAAgC,CAAC;AAGlG,gHAAgH;AAChH,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC;AAyBxC,uHAAuH;AACvH,MAAM,mBAAmB,GAAqC,IAAI,GAAG,CAAsB,CAAC,kBAAkB,EAAE,oBAAoB,CAAC,CAAC,CAAC;AAoBvI,kGAAkG;AAClG,MAAM,OAAO,kBAAkB;IAW3B,YAAY,IAA4B;QAJhC,UAAK,GAAoB,MAAM,CAAC;QAChC,iBAAY,GAAG,CAAC,CAAC;QACjB,kBAAa,GAAG,CAAC,CAAC;QAGtB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,cAAc,CAAC;QAC1C,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC,CA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C;AAED;;;;GAIG;AACH,MAAM,OAAO,mBAAmB;IAK5B;;;OAGG;IACH,YAAY,MAAoB,IAAI,CAAC,GAAG,EAAE,YAAoB,0BAA0B;QANhF,mBAAc,GAAG,CAAC,CAAC;QAOvB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,CAAC;IAED;;;;;;OAMG;IACI,kBAAkB,CAAC,KAAkB;QACxC,IAAI,eAAe,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;YAC1C,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,GAAG,GAAG,IAAI,CAAC,cAAc,GAAG,+BAA+B,EAAE,CAAC;YAC9D,OAAO,KAAK,CAAC;QACjB,CAAC;QACD,IAAI,CAAC,cAAc,GAAG,GAAG,CAAC;QAC1B,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './ai-bridge-engine.js';
|
|
2
2
|
export * from './multi-agent-room-coordinator.js';
|
|
3
|
+
export * from './full-duplex-turn-gate.js';
|
|
3
4
|
export * from './loopback-bridge.js';
|
|
4
5
|
export * from './openai-sip-bridge.js';
|
|
5
6
|
export * from './join-url-resolver.js';
|
|
@@ -8,4 +9,6 @@ export * from './calendar-clients.js';
|
|
|
8
9
|
export * from './calendar-watcher.js';
|
|
9
10
|
export * from './scheduled-bridge-runner.js';
|
|
10
11
|
export * from './identity-provisioner.js';
|
|
12
|
+
export * from './dtmf-coalescer.js';
|
|
13
|
+
export * from './bridge-prior-transcript.js';
|
|
11
14
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './ai-bridge-engine.js';
|
|
2
2
|
export * from './multi-agent-room-coordinator.js';
|
|
3
|
+
export * from './full-duplex-turn-gate.js';
|
|
3
4
|
export * from './loopback-bridge.js';
|
|
4
5
|
export * from './openai-sip-bridge.js';
|
|
5
6
|
export * from './join-url-resolver.js';
|
|
@@ -8,4 +9,6 @@ export * from './calendar-clients.js';
|
|
|
8
9
|
export * from './calendar-watcher.js';
|
|
9
10
|
export * from './scheduled-bridge-runner.js';
|
|
10
11
|
export * from './identity-provisioner.js';
|
|
12
|
+
export * from './dtmf-coalescer.js';
|
|
13
|
+
export * from './bridge-prior-transcript.js';
|
|
11
14
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,kBAAkB,CAAC;AACjC,cAAc,2BAA2B,CAAC"}
|