@memberjunction/ai-gemini 6.2.0-edge.3 → 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/dist/geminiAvatarVideoBitrate.d.ts +29 -0
- package/dist/geminiAvatarVideoBitrate.d.ts.map +1 -0
- package/dist/geminiAvatarVideoBitrate.js +58 -0
- package/dist/geminiAvatarVideoBitrate.js.map +1 -0
- package/dist/geminiBridgedAvatar.d.ts +119 -0
- package/dist/geminiBridgedAvatar.d.ts.map +1 -0
- package/dist/geminiBridgedAvatar.js +196 -0
- package/dist/geminiBridgedAvatar.js.map +1 -0
- package/dist/geminiLiveProfiles.d.ts +111 -4
- package/dist/geminiLiveProfiles.d.ts.map +1 -1
- package/dist/geminiLiveProfiles.js +169 -4
- package/dist/geminiLiveProfiles.js.map +1 -1
- package/dist/geminiLiveRelayPolicy.d.ts +87 -0
- package/dist/geminiLiveRelayPolicy.d.ts.map +1 -0
- package/dist/geminiLiveRelayPolicy.js +425 -0
- package/dist/geminiLiveRelayPolicy.js.map +1 -0
- package/dist/geminiLiveSetup.d.ts +79 -0
- package/dist/geminiLiveSetup.d.ts.map +1 -0
- package/dist/geminiLiveSetup.js +287 -0
- package/dist/geminiLiveSetup.js.map +1 -0
- package/dist/geminiRealtime.d.ts +152 -11
- package/dist/geminiRealtime.d.ts.map +1 -1
- package/dist/geminiRealtime.js +909 -133
- package/dist/geminiRealtime.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/readme.md +66 -1
|
@@ -23,6 +23,15 @@
|
|
|
23
23
|
* @module @memberjunction/ai-gemini
|
|
24
24
|
* @author MemberJunction.com
|
|
25
25
|
*/
|
|
26
|
+
import { ResolveMaxInboundVideoStreams } from '@memberjunction/ai';
|
|
27
|
+
/**
|
|
28
|
+
* The inbound video stream count a profile ACTUALLY allows: `0` when the model accepts no video (whatever
|
|
29
|
+
* the table says, so a stale number on a non-video row cannot enable streams), else its declared maximum
|
|
30
|
+
* or `1`.
|
|
31
|
+
*/
|
|
32
|
+
export function ResolveGeminiMaxInboundVideoStreams(profile) {
|
|
33
|
+
return ResolveMaxInboundVideoStreams(profile.SupportsInboundVideo, profile.MaxInboundVideoStreams);
|
|
34
|
+
}
|
|
26
35
|
/**
|
|
27
36
|
* The known Live models. Order is irrelevant — resolution takes the LONGEST matching prefix, so
|
|
28
37
|
* `gemini-3.8-live-extended-thinking` cannot be captured by `gemini-3.8-live`.
|
|
@@ -42,6 +51,7 @@ export const GEMINI_LIVE_MODEL_PROFILES = [
|
|
|
42
51
|
ProviderDefaultTurnCoverage: 'audioActivityAndAllVideo',
|
|
43
52
|
SupportsInboundVideo: true,
|
|
44
53
|
MaxInboundVideoRate: 1,
|
|
54
|
+
MaxInboundVideoStreams: 1,
|
|
45
55
|
},
|
|
46
56
|
{
|
|
47
57
|
MatchPrefix: 'gemini-3.8-live',
|
|
@@ -57,6 +67,7 @@ export const GEMINI_LIVE_MODEL_PROFILES = [
|
|
|
57
67
|
ProviderDefaultTurnCoverage: 'audioActivityAndAllVideo',
|
|
58
68
|
SupportsInboundVideo: true,
|
|
59
69
|
MaxInboundVideoRate: 1,
|
|
70
|
+
MaxInboundVideoStreams: 1,
|
|
60
71
|
},
|
|
61
72
|
{
|
|
62
73
|
// The legacy preview model. Retained deliberately: the capability table is what makes
|
|
@@ -91,27 +102,160 @@ export const GEMINI_LIVE_FALLBACK_PROFILE = {
|
|
|
91
102
|
ProactiveAudioAlwaysOn: false,
|
|
92
103
|
ProviderDefaultTurnCoverage: 'audioActivityOnly',
|
|
93
104
|
SupportsInboundVideo: false,
|
|
105
|
+
// The stream count the model WOULD take if it accepted video. The effective value is still 0 while
|
|
106
|
+
// SupportsInboundVideo is false (ResolveGeminiMaxInboundVideoStreams), so a newer unknown model that
|
|
107
|
+
// turns out to take video is one profile row away from working, with the count already stated.
|
|
108
|
+
MaxInboundVideoStreams: 1,
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* What each model renders on each endpoint, beyond its row's facts. A model with no overlay for an endpoint renders no
|
|
112
|
+
* avatar there. Matched by the row's exact `MatchPrefix`, so `gemini-3.8-live-extended-thinking` never inherits
|
|
113
|
+
* `gemini-3.8-live`'s avatar.
|
|
114
|
+
*/
|
|
115
|
+
export const GEMINI_LIVE_ENDPOINT_OVERLAYS = [
|
|
116
|
+
{
|
|
117
|
+
// Live avatars are Gemini Enterprise only, on `gemini-3.8-live` only. Encoding and muxing are from Google's sample
|
|
118
|
+
// stream (H.264 Constrained Baseline 3.1, 704x1280 at 24 fps, and AAC-LC in fragmented MP4); the spike against a
|
|
119
|
+
// live Enterprise session confirms them.
|
|
120
|
+
Endpoint: 'enterprise',
|
|
121
|
+
ModelPrefix: 'gemini-3.8-live',
|
|
122
|
+
SupportsAvatarOutput: true,
|
|
123
|
+
AvatarOutputEncoding: 'video/mp4; codecs="avc1.42c01f, mp4a.40.2"',
|
|
124
|
+
AvatarAudioMuxed: true,
|
|
125
|
+
},
|
|
126
|
+
];
|
|
127
|
+
/**
|
|
128
|
+
* What each endpoint accepts whatever the model.
|
|
129
|
+
*
|
|
130
|
+
* Turn coverage: the Developer API takes both of MJ's coverages; its `RealtimeInputConfig.TurnCoverage` enum documents
|
|
131
|
+
* `TURN_INCLUDES_ONLY_ACTIVITY` and `TURN_INCLUDES_AUDIO_ACTIVITY_AND_ALL_VIDEO`. Gemini Enterprise does not take the
|
|
132
|
+
* second: a live Vertex AI session (2026-10-09) closed at setup with 1007 "Invalid value at
|
|
133
|
+
* 'setup.realtime_input_config.turn_coverage'" on both `v1` and `v1beta1`, and opened with `TURN_INCLUDES_ONLY_ACTIVITY`.
|
|
134
|
+
*/
|
|
135
|
+
export const GEMINI_LIVE_ENDPOINT_PROFILES = {
|
|
136
|
+
developer: { AcceptedTurnCoverages: ['audioActivityOnly', 'audioActivityAndAllVideo'] },
|
|
137
|
+
enterprise: { AcceptedTurnCoverages: ['audioActivityOnly'] },
|
|
94
138
|
};
|
|
95
139
|
/**
|
|
96
|
-
*
|
|
140
|
+
* The environment variable that gives a model id this table doesn't know the profile of one it does:
|
|
141
|
+
* `<model id>=<known model id>`, comma-separated (`gemini-live-3.8-preview-1009=gemini-3.8-live`). For when Google names
|
|
142
|
+
* a model differently from the table: the catalog's `APIName` is still what Google is sent; only what the model accepts
|
|
143
|
+
* and renders (an avatar on Gemini Enterprise, for `gemini-3.8-live`) comes from the known model's row. An id matches
|
|
144
|
+
* whole, ignoring case and spaces, and an alias wins over the table. Read on each resolution: a restart applies a change.
|
|
145
|
+
*/
|
|
146
|
+
export const GEMINI_LIVE_MODEL_ALIASES_ENV = 'MJ_GEMINI_LIVE_MODEL_ALIASES';
|
|
147
|
+
/** The longest part of a bad alias entry quoted in the log line. */
|
|
148
|
+
const MAX_QUOTED_ALIAS_CHARS = 80;
|
|
149
|
+
/** The parsed alias setting, kept while the variable's text stays the same. */
|
|
150
|
+
let aliasCache = null;
|
|
151
|
+
/** Aliases already reported as used (`alias=row`), so each is logged once per process. */
|
|
152
|
+
const reportedAliases = new Set();
|
|
153
|
+
/** Resolved profiles by endpoint and model row, so every resolution of one model on one endpoint is the same object. */
|
|
154
|
+
const resolvedProfiles = new Map();
|
|
155
|
+
/**
|
|
156
|
+
* Resolves the profile for a model id on an endpoint: the model's row (an alias from
|
|
157
|
+
* {@link GEMINI_LIVE_MODEL_ALIASES_ENV}, else the longest prefix), plus what it renders on that endpoint
|
|
158
|
+
* ({@link GEMINI_LIVE_ENDPOINT_OVERLAYS}) and what the endpoint accepts ({@link GEMINI_LIVE_ENDPOINT_PROFILES}).
|
|
97
159
|
*
|
|
98
160
|
* Case- and whitespace-insensitive because model ids reach us from metadata that humans edit.
|
|
99
161
|
* Never throws and never returns undefined — an unknown model gets
|
|
100
162
|
* {@link GEMINI_LIVE_FALLBACK_PROFILE} so a new model release degrades to "treated conservatively"
|
|
101
163
|
* rather than "cannot connect".
|
|
164
|
+
*
|
|
165
|
+
* @param model The model id.
|
|
166
|
+
* @param endpoint The endpoint serving the session. Default: the Developer API.
|
|
102
167
|
*/
|
|
103
|
-
export function ResolveGeminiLiveProfile(model) {
|
|
104
|
-
const
|
|
168
|
+
export function ResolveGeminiLiveProfile(model, endpoint = 'developer') {
|
|
169
|
+
const row = resolveModelRow(model);
|
|
170
|
+
const key = `${endpoint}|${row.MatchPrefix}`;
|
|
171
|
+
let resolved = resolvedProfiles.get(key);
|
|
172
|
+
if (!resolved) {
|
|
173
|
+
resolved = { ...row, ...avatarFactsFor(row, endpoint), ...GEMINI_LIVE_ENDPOINT_PROFILES[endpoint], Endpoint: endpoint };
|
|
174
|
+
resolvedProfiles.set(key, resolved);
|
|
175
|
+
}
|
|
176
|
+
return resolved;
|
|
177
|
+
}
|
|
178
|
+
/** The model's row: its alias's row, else the longest matching prefix, else the fallback. */
|
|
179
|
+
function resolveModelRow(model) {
|
|
180
|
+
const id = normalizeModelId(model);
|
|
105
181
|
if (id.length === 0) {
|
|
106
182
|
return GEMINI_LIVE_FALLBACK_PROFILE;
|
|
107
183
|
}
|
|
184
|
+
const aliased = liveModelAliases().get(id);
|
|
185
|
+
if (aliased) {
|
|
186
|
+
reportAliasUsed(id, aliased);
|
|
187
|
+
return aliased;
|
|
188
|
+
}
|
|
189
|
+
return longestPrefixRow(id) ?? GEMINI_LIVE_FALLBACK_PROFILE;
|
|
190
|
+
}
|
|
191
|
+
/** A model id as the table compares it: trimmed and lower case. */
|
|
192
|
+
function normalizeModelId(model) {
|
|
193
|
+
return String(model ?? '').trim().toLowerCase();
|
|
194
|
+
}
|
|
195
|
+
/** The row whose prefix is the longest one the id starts with, if any. */
|
|
196
|
+
function longestPrefixRow(id) {
|
|
108
197
|
let best;
|
|
109
198
|
for (const p of GEMINI_LIVE_MODEL_PROFILES) {
|
|
110
199
|
if (id.startsWith(p.MatchPrefix) && (!best || p.MatchPrefix.length > best.MatchPrefix.length)) {
|
|
111
200
|
best = p;
|
|
112
201
|
}
|
|
113
202
|
}
|
|
114
|
-
return best
|
|
203
|
+
return best;
|
|
204
|
+
}
|
|
205
|
+
/** The deployment's aliases ({@link GEMINI_LIVE_MODEL_ALIASES_ENV}), parsed again only when the variable's text changes. */
|
|
206
|
+
function liveModelAliases() {
|
|
207
|
+
const raw = typeof process !== 'undefined' && process.env ? (process.env[GEMINI_LIVE_MODEL_ALIASES_ENV] ?? '') : '';
|
|
208
|
+
if (aliasCache?.Raw !== raw) {
|
|
209
|
+
aliasCache = { Raw: raw, Aliases: parseModelAliases(raw) };
|
|
210
|
+
}
|
|
211
|
+
return aliasCache.Aliases;
|
|
212
|
+
}
|
|
213
|
+
/** The aliases in a setting; entries that are not `<id>=<known id>` are left out and named in one log line. */
|
|
214
|
+
function parseModelAliases(raw) {
|
|
215
|
+
const aliases = new Map();
|
|
216
|
+
const bad = [];
|
|
217
|
+
for (const entry of raw.split(',').map((text) => text.trim()).filter((text) => text.length > 0)) {
|
|
218
|
+
const alias = readAliasEntry(entry);
|
|
219
|
+
if (alias) {
|
|
220
|
+
aliases.set(alias.Id, alias.Row);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
bad.push(entry);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (bad.length > 0) {
|
|
227
|
+
const quoted = bad.map((entry) => `"${entry.replace(/[^\x20-\x7E]/g, '?').slice(0, MAX_QUOTED_ALIAS_CHARS)}"`).join(', ');
|
|
228
|
+
const known = GEMINI_LIVE_MODEL_PROFILES.map((p) => p.MatchPrefix).join(', ');
|
|
229
|
+
console.warn(`[GeminiLiveProfiles] Ignored ${GEMINI_LIVE_MODEL_ALIASES_ENV} entries ${quoted}: each must be <model id>=<a known model id> (${known}).`);
|
|
230
|
+
}
|
|
231
|
+
return aliases;
|
|
232
|
+
}
|
|
233
|
+
/** One `<id>=<known id>` entry: the id and the known model's row, or `null` when it is not one. */
|
|
234
|
+
function readAliasEntry(entry) {
|
|
235
|
+
const separator = entry.indexOf('=');
|
|
236
|
+
if (separator <= 0) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
const id = normalizeModelId(entry.slice(0, separator));
|
|
240
|
+
const target = normalizeModelId(entry.slice(separator + 1));
|
|
241
|
+
const row = target.length > 0 ? longestPrefixRow(target) : undefined;
|
|
242
|
+
return id.length > 0 && row ? { Id: id, Row: row } : null;
|
|
243
|
+
}
|
|
244
|
+
/** Says once per process that a model id took an alias's profile, so the log shows the setting took effect. */
|
|
245
|
+
function reportAliasUsed(id, row) {
|
|
246
|
+
const key = `${id}=${row.MatchPrefix}`;
|
|
247
|
+
if (!reportedAliases.has(key)) {
|
|
248
|
+
reportedAliases.add(key);
|
|
249
|
+
console.log(`[GeminiLiveProfiles] Model ${id} uses the ${row.MatchPrefix} profile (${GEMINI_LIVE_MODEL_ALIASES_ENV}).`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** The row's overlay on the endpoint, or no avatar. */
|
|
253
|
+
function avatarFactsFor(row, endpoint) {
|
|
254
|
+
const overlay = GEMINI_LIVE_ENDPOINT_OVERLAYS.find((o) => o.Endpoint === endpoint && o.ModelPrefix === row.MatchPrefix);
|
|
255
|
+
if (!overlay) {
|
|
256
|
+
return { SupportsAvatarOutput: false };
|
|
257
|
+
}
|
|
258
|
+
return { SupportsAvatarOutput: overlay.SupportsAvatarOutput, AvatarOutputEncoding: overlay.AvatarOutputEncoding, AvatarAudioMuxed: overlay.AvatarAudioMuxed };
|
|
115
259
|
}
|
|
116
260
|
/**
|
|
117
261
|
* Picks the thinking level to send, or `undefined` for "send no thinkingConfig at all".
|
|
@@ -145,4 +289,25 @@ export function ResolveGeminiThinkingLevel(requested, profile) {
|
|
|
145
289
|
}
|
|
146
290
|
return { Level: want };
|
|
147
291
|
}
|
|
292
|
+
/** The coverage a session sends when it asks for none, or for one its endpoint doesn't accept: audio activity only. */
|
|
293
|
+
const FALLBACK_TURN_COVERAGE = 'audioActivityOnly';
|
|
294
|
+
/**
|
|
295
|
+
* Picks the turn coverage to send on the profile's endpoint.
|
|
296
|
+
*
|
|
297
|
+
* Nothing configured gives audio only: video frames are billed and use context, so the costly coverage is asked for,
|
|
298
|
+
* never inherited. A configured coverage the endpoint accepts is sent. Any other value (`audioActivityAndAllVideo` on
|
|
299
|
+
* Gemini Enterprise, or one that is not a coverage) gives audio only as well and comes back as `Refused`, so the caller
|
|
300
|
+
* can name both in its log line. Never throws: a coverage Google would refuse must not cost the user the session.
|
|
301
|
+
*
|
|
302
|
+
* @param requested The configured coverage (`ModelConfiguration.Realtime.TurnDetection.Coverage`), if any.
|
|
303
|
+
* @param profile A profile resolved for the session's endpoint; its {@link GeminiLiveEndpointProfile.AcceptedTurnCoverages} decide.
|
|
304
|
+
*/
|
|
305
|
+
export function ResolveGeminiTurnCoverage(requested, profile) {
|
|
306
|
+
const want = String(requested ?? '').trim();
|
|
307
|
+
if (want.length === 0) {
|
|
308
|
+
return { Coverage: FALLBACK_TURN_COVERAGE };
|
|
309
|
+
}
|
|
310
|
+
const accepted = profile.AcceptedTurnCoverages.find((coverage) => coverage === want);
|
|
311
|
+
return accepted ? { Coverage: accepted } : { Coverage: FALLBACK_TURN_COVERAGE, Refused: want };
|
|
312
|
+
}
|
|
148
313
|
//# sourceMappingURL=geminiLiveProfiles.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"geminiLiveProfiles.js","sourceRoot":"","sources":["../src/geminiLiveProfiles.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;
|
|
1
|
+
{"version":3,"file":"geminiLiveProfiles.js","sourceRoot":"","sources":["../src/geminiLiveProfiles.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,6BAA6B,EAAoF,MAAM,oBAAoB,CAAC;AA2IrJ;;;;GAIG;AACH,MAAM,UAAU,mCAAmC,CAAC,OAA+B;IAC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AAC;QACpB,OAAO,EAAE,QAAQ,EAAE,sBAAsB,EAAE,CAAC;IAChD,CAAC;IACD,MAAM,QAAQ,GAAG,OAAO,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC;IACrF,OAAO,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,sBAAsB,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AACnG,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Gemini Live's half of MJAPI's realtime relay: the server writes the setup, the browser streams input.
|
|
3
|
+
*
|
|
4
|
+
* A browser cannot hold a Gemini Enterprise credential, so a client-direct Enterprise session runs through MJAPI's relay
|
|
5
|
+
* (`/realtime/relay/<ticket>/…`; `IRealtimeRelayPolicy` in `@memberjunction/ai`). Enterprise also lets a client replace
|
|
6
|
+
* the system instruction and tools mid-session (`system` turns, `contextUpdate`), so checking the browser's first frame
|
|
7
|
+
* would lock nothing. This policy:
|
|
8
|
+
*
|
|
9
|
+
* - opens every upstream connection with the setup the server wrote (`BuildGeminiLiveSetup`), and reads only two things
|
|
10
|
+
* from the browser's setup: a resumption handle (the relay accepts only handles it forwarded to the session) and a
|
|
11
|
+
* request for audio only (a downgrade, never an upgrade);
|
|
12
|
+
* - forwards only `realtimeInput` (audio, video, text, activity markers, `audioStreamEnd`), `clientContent` whose turns
|
|
13
|
+
* are all `user` (an empty `turnComplete` commit included) and `toolResponse`, in camelCase or snake_case, with known
|
|
14
|
+
* keys only and media of the types Gemini Live takes; it drops everything else under a short label the relay counts;
|
|
15
|
+
* - finds resumption handles in server frames with a byte search before any parse (avatar video frames run to
|
|
16
|
+
* megabytes).
|
|
17
|
+
*
|
|
18
|
+
* Widening what passes lets a browser change the agent mid-session, so any widening is a security change.
|
|
19
|
+
*
|
|
20
|
+
* @module @memberjunction/ai-gemini
|
|
21
|
+
* @author MemberJunction.com
|
|
22
|
+
*/
|
|
23
|
+
import type { LiveClientSetup } from '@google/genai';
|
|
24
|
+
import type { IRealtimeRelayPolicy, RealtimeRelayFrameVerdict, RealtimeRelayOpenIntent } from '@memberjunction/ai';
|
|
25
|
+
/** What a {@link GeminiLiveRelayPolicy} needs for one relayed session. */
|
|
26
|
+
export interface GeminiLiveRelayPolicyOptions {
|
|
27
|
+
/** The setup the server wrote with `BuildGeminiLiveSetup`. Every connection of the session opens with it. */
|
|
28
|
+
Setup: LiveClientSetup;
|
|
29
|
+
/**
|
|
30
|
+
* Mints the upstream handshake's headers. Called on every upstream open, so a short-lived OAuth token (Gemini
|
|
31
|
+
* Enterprise) is fresh for each connection. Omitted on the Developer API, whose key travels in the upstream URL.
|
|
32
|
+
*/
|
|
33
|
+
UpstreamHeaders?: () => Promise<Record<string, string>>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The Gemini Live frame policy for one relayed session: it writes the setup, filters what the browser sends after it,
|
|
37
|
+
* and finds the resumption handles Google sends (see the module notes).
|
|
38
|
+
*
|
|
39
|
+
* Drop labels (counted by the relay, never payload): `oversize`, `invalid`, `empty`, `multi-key`, `setup`,
|
|
40
|
+
* `contextUpdate`, `unknown`, and `<type>.<reason>` for a `realtimeInput`, `clientContent` or `toolResponse` that fails
|
|
41
|
+
* its checks (`invalid`, `unknown`, `duplicate`, `media-type`; `system`, `model`, `role` and `part` for turns).
|
|
42
|
+
*/
|
|
43
|
+
export declare class GeminiLiveRelayPolicy implements IRealtimeRelayPolicy {
|
|
44
|
+
/** The minted setup as JSON: each connection parses its own copy, so nothing a connection does reaches another. */
|
|
45
|
+
private readonly setupJson;
|
|
46
|
+
private readonly upstreamHeaders?;
|
|
47
|
+
/**
|
|
48
|
+
* @param options The minted setup and, on Gemini Enterprise, the header source.
|
|
49
|
+
* @throws When the setup names no model.
|
|
50
|
+
*/
|
|
51
|
+
constructor(options: GeminiLiveRelayPolicyOptions);
|
|
52
|
+
/** The upstream handshake's headers for this connection: the header source's, or none. */
|
|
53
|
+
UpstreamHeaders(): Promise<Record<string, string>>;
|
|
54
|
+
/**
|
|
55
|
+
* Reads the browser's setup (the connection's first frame): its resumption handle and whether it asks for audio
|
|
56
|
+
* only. Nothing else in it is read. `null` when the frame is not a lone, well-formed `setup` message.
|
|
57
|
+
*
|
|
58
|
+
* @param firstClientFrame The browser's first text frame.
|
|
59
|
+
*/
|
|
60
|
+
ReadOpenIntent(firstClientFrame: string): RealtimeRelayOpenIntent | null;
|
|
61
|
+
/**
|
|
62
|
+
* The connection's one opening frame: the minted setup, with the browser's resumption handle on a resume and without
|
|
63
|
+
* video output when the browser asked for audio only.
|
|
64
|
+
*
|
|
65
|
+
* @param intent What the first frame asked for, as {@link ReadOpenIntent} read it and the relay granted it.
|
|
66
|
+
* @throws On a resume when the minted setup has resumption off (never opens a fresh session in its place).
|
|
67
|
+
*/
|
|
68
|
+
OpeningFrames(intent: RealtimeRelayOpenIntent): string[];
|
|
69
|
+
/**
|
|
70
|
+
* Forwards a `realtimeInput`, a `clientContent` of `user` turns or a `toolResponse` that passes its checks, as JSON
|
|
71
|
+
* re-serialized from what was checked (so a duplicate key or another parser difference cannot carry an unchecked
|
|
72
|
+
* value through). Drops anything else, under a label (see the class notes).
|
|
73
|
+
*
|
|
74
|
+
* @param frame A client text frame after the first.
|
|
75
|
+
*/
|
|
76
|
+
FilterClientFrame(frame: string): RealtimeRelayFrameVerdict;
|
|
77
|
+
/**
|
|
78
|
+
* The resumption handle a server frame carries, else `null`. Text and binary frames alike (Google sends its JSON in
|
|
79
|
+
* binary frames); a frame without the bytes `sessionResumptionUpdate` is never parsed. A handle counts only when
|
|
80
|
+
* Google marks it resumable, as the browser client does.
|
|
81
|
+
*
|
|
82
|
+
* @param data The frame's bytes.
|
|
83
|
+
* @param isBinary Whether it came as a binary frame (not needed: both kinds hold the same JSON).
|
|
84
|
+
*/
|
|
85
|
+
ObserveServerFrame(data: Uint8Array, isBinary: boolean): string | null;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=geminiLiveRelayPolicy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geminiLiveRelayPolicy.d.ts","sourceRoot":"","sources":["../src/geminiLiveRelayPolicy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,oBAAoB,EAAyB,yBAAyB,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAG1I,0EAA0E;AAC1E,MAAM,WAAW,4BAA4B;IACzC,6GAA6G;IAC7G,KAAK,EAAE,eAAe,CAAC;IACvB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CAC3D;AA+DD;;;;;;;GAOG;AACH,qBAAa,qBAAsB,YAAW,oBAAoB;IAC9D,mHAAmH;IACnH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAwC;IAEzE;;;OAGG;gBACS,OAAO,EAAE,4BAA4B;IASjD,0FAA0F;IAC7E,eAAe,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAI/D;;;;;OAKG;IACI,cAAc,CAAC,gBAAgB,EAAE,MAAM,GAAG,uBAAuB,GAAG,IAAI;IAS/E;;;;;;OAMG;IACI,aAAa,CAAC,MAAM,EAAE,uBAAuB,GAAG,MAAM,EAAE;IAY/D;;;;;;OAMG;IACI,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,yBAAyB;IAgBlE;;;;;;;OAOG;IACI,kBAAkB,CAAC,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI;CAYhF"}
|