@memberjunction/ai-bridge-server 6.2.0-edge.2 → 6.2.0-edge.4

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
@@ -9,7 +9,7 @@ It is the server half of the `AIBridgeEngineBase` / `AIBridgeEngine` pair, exact
9
9
  `@memberjunction/aiengine`'s `AIEngine` builds on `@memberjunction/ai-engine-base`'s `AIEngineBase`.
10
10
  The cross-platform, metadata-only base (provider/identity/channel cache, the abstract
11
11
  `BaseRealtimeBridge` driver, `TurnTakingPolicy`, media-track types) lives in
12
- [`@memberjunction/ai-bridge-base`](../BridgeBase); this package adds everything that actually *runs* a
12
+ [`@memberjunction/ai-bridge-base`](../Base); this package adds everything that actually *runs* a
13
13
  bridged session.
14
14
 
15
15
  > See the architecture plan at `/plans/realtime/realtime-bridges-architecture.md` (§1 transport seam,
@@ -34,26 +34,98 @@ npm install @memberjunction/ai-bridge-server
34
34
  ## The transport seam (the heart)
35
35
 
36
36
  The realtime session contract (`IRealtimeSession` in `@memberjunction/ai`) is already
37
- media-agnostic — `SendInput(chunk)` is what the agent hears, `OnOutput(handler)` is what it says —
38
- but it had **no client-facing pipe**. A bridge *is* that pipe. `AIBridgeEngine.wireTransportSeam`
39
- connects the two directions:
37
+ media-agnostic — `SendInput(frame)` is what the agent hears (and sees), `OnOutput(handler)` is what it says,
38
+ `OnVideoFrame(handler)` is the video it shows — but it had **no client-facing pipe**. A bridge *is* that pipe.
39
+ `AIBridgeEngine.wireTransportSeam` connects the two directions:
40
40
 
41
41
  ```typescript
42
- // Inbound: endpoint media → the agent hears it.
42
+ // Inbound: endpoint media → the agent hears (and, for a video model, sees) it.
43
43
  bridge.OnMedia((frame) => {
44
44
  const chunk = frameToArrayBuffer(frame); // unwrap BridgeMediaFrame → ArrayBuffer
45
- if (chunk) realtimeSession.SendInput(chunk);
45
+ const kind = mediaKindOf(frame.Track); // 'video' for video-in and screen-in, else 'audio'
46
+ if (chunk) realtimeSession.SendInput({ Data: chunk, Kind: kind, MimeType: frame.MimeType, TimestampMs: frame.TimestampMs });
46
47
  });
47
48
 
48
49
  // Outbound: the agent speaks → into the meeting/call.
49
50
  realtimeSession.OnOutput((chunk) => {
50
51
  bridge.SendMedia('audio-out', arrayBufferToFrame(chunk, 'audio-out'));
51
52
  });
53
+
54
+ // Outbound video: the agent's typed video frames → video-out, with their MIME type.
55
+ realtimeSession.OnVideoFrame?.((frame) => {
56
+ bridge.SendMedia('video-out', { Track: 'video-out', Bytes: frame.Data, MimeType: frame.MimeType });
57
+ });
52
58
  ```
53
59
 
54
- Audio is the first track lit up; video/screen ride the same two methods unchanged once the model
55
- emits them. The engine never constructs the realtime model — the `IRealtimeSession` is **injected**
56
- via `StartBridgeSessionParams.RealtimeSession`, so the only coupling is the seam itself.
60
+ The snippet leaves out the video gate, the notes to the model and the floor gate; see [Video](#video). The engine
61
+ never constructs the realtime model — the `IRealtimeSession` is **injected** via
62
+ `StartBridgeSessionParams.RealtimeSession`, so the only coupling is the seam itself.
63
+
64
+ ## Video
65
+
66
+ ### What the driver is told
67
+
68
+ `StartBridgeSession` passes the driver its `Configuration` plus facts read from the realtime session: its audio rates
69
+ (`InboundSampleRate`, `OutboundSampleRate`) and its video facts, `InboundVideoStreams` (how many camera or screen sources
70
+ it takes at once; `0` when it takes no video) and `InboundVideoRate` (frames per second; absent when the model declares
71
+ none). The video facts come from the session's declared capabilities (`InboundVideoStreamsOf`, `InboundVideoRateOf` in
72
+ `@memberjunction/ai`), so the rate comes from the model, not the driver.
73
+
74
+ ### Video in: the gate and the notes
75
+
76
+ - **The gate.** A `video-in` or `screen-in` frame goes only to a session whose capabilities declare an inbound video
77
+ track. Any other session would read the image as audio, so the engine drops the frame (one verbose log line per
78
+ session). No inbound frame is sent while the model session is being replaced.
79
+ - **"[You can now see: …]"** goes to the model right before the first frame of a source new to the current model
80
+ session: the first source, the next one after a switch, and a source seen again after it ended. A replacement model
81
+ session (after a drop, or the audio-only one below) starts with no sources seen, so it gets the note with its first
82
+ frame. Sources are told apart by `SourceID`; a frame without one gets no note.
83
+ - **"[You can no longer see: …]"** goes when the driver reports through `OnVideoSourceEnded` that a source whose frames
84
+ reached the current model session stopped, a switch to another source included. A source the model never saw gets no
85
+ note.
86
+ - **The label.** Both notes name the source by its `SourceLabel` ("Ada's camera"). A source without one is "a
87
+ participant's camera" or "a participant's screen", never its `SourceID`.
88
+ - The notes go through the session's optional `SendContextNote`, and a driver may hold them: Gemini Live queues a note
89
+ until the agent's reply ends but sends frames at once, so while the agent talks, a new source's first frames can reach
90
+ the model before its note.
91
+
92
+ `VideoSourceSeenNote` and `VideoSourceEndedNote` build the two notes.
93
+
94
+ ### Video out
95
+
96
+ `OnVideoFrame` → `SendMedia('video-out')`. Each typed frame (`RealtimeVideoFrame`: a fragmented MP4 piece, an encoded
97
+ chunk or an image) goes to the bridge with its bytes, its `MimeType`, so the driver can tell an avatar's MP4 from other
98
+ video, and what it says about its video (`Width`, `Height`, `KeyFrame`):
99
+
100
+ - an image: its own size when it has one, and `KeyFrame: true`;
101
+ - a chunk: its own size when it has one, and its own key-frame flag;
102
+ - an fMP4 init segment: its own size, read from the piece itself, and no `KeyFrame`;
103
+ - an fMP4 fragment with video: whether its first video frame is a key frame, and the size the stream's init segment
104
+ gives the video track. An audio-only fragment, a fragment before any init segment, or one the driver couldn't read
105
+ carries none of these.
106
+
107
+ When the session's speech is floor-gated (`FullDuplexTurnGate`, in a room with more than one agent), its video is gated
108
+ too. An fMP4 fragment counts by the duration of its audio track, which carries the agent's voice
109
+ (`FullDuplexTurnGate.OnOutputDuration`). An init segment passes and is kept for reading the fragments. Chunks and images
110
+ carry no voice and pass. A cut flushes the bridge's queued media.
111
+
112
+ The deprecated `OnVideoOutput` (untyped bytes) is still forwarded to `video-out`, without a MIME type.
113
+
114
+ ### When the avatar can't be shown
115
+
116
+ A driver that publishes the agent's avatar reports through `OnAvatarUnavailable` when it took the avatar down
117
+ (`'decoder-failed'` or `'publish-failed'`). The engine then replaces the model session once, through the host's
118
+ `StartBridgeSessionParams.RecoverRealtimeSessionWithoutAvatar`, with one that renders no avatar, seeded with the
119
+ conversation so far, so the model stops generating video nobody sees. Inbound audio is held while the replacement opens,
120
+ and the replacement must use the same audio sample rates. Nothing is replaced when the host gave no factory, or the
121
+ session is ending, recovering or already replaced; if the replacement fails, the session goes on as it was. This is
122
+ separate from `RecoverRealtimeSession`'s attempts after a dropped model session.
123
+
124
+ ### Model usage
125
+
126
+ The engine records no model usage. For an agent's bridged session, `RealtimeClientSessionService.WireBridgeRealtimeSession`
127
+ in `@memberjunction/ai-agents` records the model session's `OnUsage` on that session's own co-agent prompt run, so a
128
+ replacement session opened through it has a run of its own.
57
129
 
58
130
  ## Usage
59
131
 
@@ -106,9 +178,11 @@ Phase-2 bridge-chat channel, and `Silent` does nothing.
106
178
  ## Testing the seam without a platform
107
179
 
108
180
  `LoopbackBridge` echoes every outbound frame back inbound, so with a mock `IRealtimeSession` you can
109
- assert media flows **bridge → session** and **session → bridge** with no external service. See
110
- `src/__tests__/transport-seam.test.ts` for the round-trip, lifecycle, turn-taking, participant, and
111
- janitor tests.
181
+ assert media flows **bridge → session** and **session → bridge** with no external service. Its
182
+ `EmitVideoSourceEnded` and `EmitAvatarUnavailable` drive the ended-source note and the audio-only replacement. See
183
+ `src/__tests__/transport-seam.test.ts` for the round-trip, lifecycle, turn-taking, participant,
184
+ janitor and video-note tests, and `src/__tests__/avatar-output.test.ts` for video out, the floor gate and the
185
+ audio-only replacement.
112
186
 
113
187
  ## Composition with the realtime engine
114
188