@memberjunction/ai-gemini 5.40.2 → 5.41.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/dist/geminiEmbedding2.d.ts +27 -0
- package/dist/geminiEmbedding2.d.ts.map +1 -0
- package/dist/geminiEmbedding2.js +146 -0
- package/dist/geminiEmbedding2.js.map +1 -0
- package/dist/geminiRealtime.d.ts +151 -0
- package/dist/geminiRealtime.d.ts.map +1 -0
- package/dist/geminiRealtime.js +651 -0
- package/dist/geminiRealtime.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +86 -41
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/readme.md +26 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BaseEmbeddings, EmbedTextParams, EmbedTextsParams, EmbedTextResult, EmbedTextsResult, EmbedContentParams, EmbedContentResult, FileCapabilities } from '@memberjunction/ai';
|
|
2
|
+
import { GoogleGenAI } from '@google/genai';
|
|
3
|
+
export declare class GeminiEmbedding2 extends BaseEmbeddings {
|
|
4
|
+
private _gemini;
|
|
5
|
+
constructor(apiKey: string);
|
|
6
|
+
get GeminiClient(): GoogleGenAI;
|
|
7
|
+
GetFileCapabilities(): FileCapabilities | null;
|
|
8
|
+
EmbedText(params: EmbedTextParams): Promise<EmbedTextResult>;
|
|
9
|
+
EmbedTexts(params: EmbedTextsParams): Promise<EmbedTextsResult>;
|
|
10
|
+
/**
|
|
11
|
+
* Multimodal embed: text and/or interleaved media blocks (image/audio/video/document)
|
|
12
|
+
* fused into ONE vector in the same space as text embeddings — enabling cross-modal
|
|
13
|
+
* retrieval (a text query matching an image, audio, or video). Overrides the base
|
|
14
|
+
* BaseEmbeddings.EmbedContent, which is text-only. Content blocks are mapped to Gemini
|
|
15
|
+
* parts via GeminiLLM.MapMJContentToGeminiParts. Returns an empty vector on error,
|
|
16
|
+
* consistent with the text methods.
|
|
17
|
+
*/
|
|
18
|
+
EmbedContent(params: EmbedContentParams): Promise<EmbedContentResult>;
|
|
19
|
+
GetEmbeddingModels(): Promise<EmbeddingModelInfo[]>;
|
|
20
|
+
}
|
|
21
|
+
/** Describes an embedding model returned by {@link GeminiEmbedding2.GetEmbeddingModels}. */
|
|
22
|
+
export interface EmbeddingModelInfo {
|
|
23
|
+
Model: string;
|
|
24
|
+
Description: string;
|
|
25
|
+
OutputDimension: number;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=geminiEmbedding2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geminiEmbedding2.d.ts","sourceRoot":"","sources":["../src/geminiEmbedding2.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAGhB,kBAAkB,EAClB,kBAAkB,EAClB,gBAAgB,EACnB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,WAAW,EAAW,MAAM,eAAe,CAAC;AAsBrD,qBACa,gBAAiB,SAAQ,cAAc;IAChD,OAAO,CAAC,OAAO,CAAc;gBAEjB,MAAM,EAAE,MAAM;IAK1B,IAAW,YAAY,IAAI,WAAW,CAErC;IAEe,mBAAmB,IAAI,gBAAgB,GAAG,IAAI;IA4BjD,SAAS,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC;IAgB5D,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAgB5E;;;;;;;OAOG;IACmB,YAAY,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAyB9E,kBAAkB,IAAI,OAAO,CAAC,kBAAkB,EAAE,CAAC;CASnE;AAED,4FAA4F;AAC5F,MAAM,WAAW,kBAAkB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;CAC3B"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { BaseEmbeddings, ModelUsage, ErrorAnalyzer, } from '@memberjunction/ai';
|
|
11
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
12
|
+
import { GoogleGenAI } from '@google/genai';
|
|
13
|
+
import { GeminiLLM } from './index.js';
|
|
14
|
+
/**
|
|
15
|
+
* Gemini Embedding 2 provider for MemberJunction.
|
|
16
|
+
*
|
|
17
|
+
* A `BaseEmbeddings` implementation backed by Google's `gemini-embedding-2` — a natively
|
|
18
|
+
* MULTIMODAL embedding model (text, image, video, audio, and documents share one vector space).
|
|
19
|
+
* Overrides {@link BaseEmbeddings.EmbedContent} (text-only by default) to embed text and/or
|
|
20
|
+
* interleaved media (image/video/audio/document) into the SAME vector space as text —
|
|
21
|
+
* enabling cross-modal retrieval (a text query matching an image, audio, or video).
|
|
22
|
+
|
|
23
|
+
*
|
|
24
|
+
* Resolved via `ClassFactory.CreateInstance(BaseEmbeddings, "GeminiEmbedding2", apiKey)`.
|
|
25
|
+
*
|
|
26
|
+
* Note: `gemini-embedding-2` has no `task_type` parameter — the retrieval instruction (e.g.
|
|
27
|
+
* `"task: search result | query: …"` for queries vs `"title: … | text: …"` for documents) is
|
|
28
|
+
* carried IN the text by the caller.
|
|
29
|
+
*/
|
|
30
|
+
const DEFAULT_MODEL = 'gemini-embedding-2';
|
|
31
|
+
let GeminiEmbedding2 = class GeminiEmbedding2 extends BaseEmbeddings {
|
|
32
|
+
constructor(apiKey) {
|
|
33
|
+
super(apiKey);
|
|
34
|
+
this._gemini = new GoogleGenAI({ apiKey });
|
|
35
|
+
}
|
|
36
|
+
get GeminiClient() {
|
|
37
|
+
return this._gemini;
|
|
38
|
+
}
|
|
39
|
+
GetFileCapabilities() {
|
|
40
|
+
// Per Google's gemini-embedding-2 model spec (GA 2026-04-22):
|
|
41
|
+
// images: png/jpeg/webp/bmp/heic/heif/avif (max 6 per request)
|
|
42
|
+
// video: mpeg/mp4 (1 per request) audio: mp3/wav (1 per request) pdf (1 file, 6 pages)
|
|
43
|
+
// 'audio/mpeg' is included as a tolerant alias for the canonical MP3 type (doc lists 'audio/mp3').
|
|
44
|
+
// MaxFilesPerRequest reflects the image max (6); non-image modalities are capped at 1 by the API.
|
|
45
|
+
return {
|
|
46
|
+
SupportedMimeTypes: [
|
|
47
|
+
'image/png',
|
|
48
|
+
'image/jpeg',
|
|
49
|
+
'image/webp',
|
|
50
|
+
'image/bmp',
|
|
51
|
+
'image/heic',
|
|
52
|
+
'image/heif',
|
|
53
|
+
'image/avif',
|
|
54
|
+
'video/mpeg',
|
|
55
|
+
'video/mp4',
|
|
56
|
+
'audio/mp3',
|
|
57
|
+
'audio/mpeg',
|
|
58
|
+
'audio/wav',
|
|
59
|
+
'application/pdf',
|
|
60
|
+
],
|
|
61
|
+
MaxFileSize: 100 * 1024 * 1024, // Google docs state no hard per-file limit; generous cap
|
|
62
|
+
MaxFilesPerRequest: 6,
|
|
63
|
+
HasFileAPI: false,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async EmbedText(params) {
|
|
67
|
+
const model = params.model || DEFAULT_MODEL;
|
|
68
|
+
try {
|
|
69
|
+
const response = await this._gemini.models.embedContent({ model, contents: params.text });
|
|
70
|
+
return {
|
|
71
|
+
object: 'object',
|
|
72
|
+
model,
|
|
73
|
+
ModelUsage: new ModelUsage(0, 0),
|
|
74
|
+
vector: response.embeddings?.[0]?.values ?? [],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.error('Gemini embedding error:', ErrorAnalyzer.analyzeError(error, 'Gemini'));
|
|
79
|
+
return { object: 'object', model, ModelUsage: new ModelUsage(0, 0), vector: [] };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async EmbedTexts(params) {
|
|
83
|
+
const model = params.model || DEFAULT_MODEL;
|
|
84
|
+
try {
|
|
85
|
+
const response = await this._gemini.models.embedContent({ model, contents: params.texts });
|
|
86
|
+
return {
|
|
87
|
+
object: 'list',
|
|
88
|
+
model,
|
|
89
|
+
ModelUsage: new ModelUsage(0, 0),
|
|
90
|
+
vectors: (response.embeddings ?? []).map((e) => e.values ?? []),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.error('Gemini embedding error:', ErrorAnalyzer.analyzeError(error, 'Gemini'));
|
|
95
|
+
return { object: 'list', model, ModelUsage: new ModelUsage(0, 0), vectors: [] };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Multimodal embed: text and/or interleaved media blocks (image/audio/video/document)
|
|
100
|
+
* fused into ONE vector in the same space as text embeddings — enabling cross-modal
|
|
101
|
+
* retrieval (a text query matching an image, audio, or video). Overrides the base
|
|
102
|
+
* BaseEmbeddings.EmbedContent, which is text-only. Content blocks are mapped to Gemini
|
|
103
|
+
* parts via GeminiLLM.MapMJContentToGeminiParts. Returns an empty vector on error,
|
|
104
|
+
* consistent with the text methods.
|
|
105
|
+
*/
|
|
106
|
+
async EmbedContent(params) {
|
|
107
|
+
const model = params.model || DEFAULT_MODEL;
|
|
108
|
+
try {
|
|
109
|
+
this.ValidateContentSupported(params.content);
|
|
110
|
+
const parts = GeminiLLM.MapMJContentToGeminiParts(params.content);
|
|
111
|
+
const contents = [{ parts }];
|
|
112
|
+
const response = await this._gemini.models.embedContent({ model, contents });
|
|
113
|
+
return {
|
|
114
|
+
object: 'object',
|
|
115
|
+
model,
|
|
116
|
+
ModelUsage: new ModelUsage(0, 0),
|
|
117
|
+
vector: response.embeddings?.[0]?.values ?? [],
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
console.error('Gemini embedding (content) error:', ErrorAnalyzer.analyzeError(error, 'Gemini'));
|
|
122
|
+
// design considerations: propagate error in addition to returning an empty vector or throw error from the get-go
|
|
123
|
+
return {
|
|
124
|
+
object: 'object',
|
|
125
|
+
model,
|
|
126
|
+
ModelUsage: new ModelUsage(0, 0),
|
|
127
|
+
vector: [],
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async GetEmbeddingModels() {
|
|
132
|
+
return [
|
|
133
|
+
{
|
|
134
|
+
Model: DEFAULT_MODEL,
|
|
135
|
+
Description: 'Natively multimodal embedding model (text, image, video, audio, documents)',
|
|
136
|
+
OutputDimension: 3072,
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
GeminiEmbedding2 = __decorate([
|
|
142
|
+
RegisterClass(BaseEmbeddings, 'GeminiEmbedding2'),
|
|
143
|
+
__metadata("design:paramtypes", [String])
|
|
144
|
+
], GeminiEmbedding2);
|
|
145
|
+
export { GeminiEmbedding2 };
|
|
146
|
+
//# sourceMappingURL=geminiEmbedding2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geminiEmbedding2.js","sourceRoot":"","sources":["../src/geminiEmbedding2.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EACH,cAAc,EAKd,UAAU,EACV,aAAa,GAIhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,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AAlHY,gBAAgB;IAD5B,aAAa,CAAC,cAAc,EAAE,kBAAkB,CAAC;;GACrC,gBAAgB,CAkH5B"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { type AuthToken, type CreateAuthTokenParameters, type LiveServerMessage, type LiveConnectConfig, type FunctionDeclaration, type FunctionResponse, type Content, type Blob as GeminiBlob } from '@google/genai';
|
|
2
|
+
import { BaseRealtimeModel, type ClientRealtimeSessionConfig, type IRealtimeSession, type RealtimeSessionParams, type RealtimeToolDefinition } from '@memberjunction/ai';
|
|
3
|
+
/**
|
|
4
|
+
* The minimal subset of `@google/genai`'s `Session` that the realtime driver depends on. Declaring
|
|
5
|
+
* the seam as an interface (rather than the concrete SDK `Session`) lets unit tests inject a fully
|
|
6
|
+
* in-memory fake that drives the registered callbacks with Gemini-shaped messages and captures
|
|
7
|
+
* outbound calls — no websocket, no network.
|
|
8
|
+
*/
|
|
9
|
+
export interface GeminiLiveSession {
|
|
10
|
+
/**
|
|
11
|
+
* Streams realtime user input to the model — media frames (audio now) AND mid-session
|
|
12
|
+
* text. Realtime text is the Live API's "respond now" path for in-conversation messages:
|
|
13
|
+
* native-audio models treat {@link sendClientContent} as history seeding only and will
|
|
14
|
+
* NOT generate from it mid-call, while realtime text triggers immediately.
|
|
15
|
+
*/
|
|
16
|
+
sendRealtimeInput(params: {
|
|
17
|
+
audio?: GeminiBlob;
|
|
18
|
+
media?: GeminiBlob;
|
|
19
|
+
text?: string;
|
|
20
|
+
}): void;
|
|
21
|
+
/** Appends client content (used to seed initial context) to the conversation. */
|
|
22
|
+
sendClientContent(params: {
|
|
23
|
+
turns?: Content[];
|
|
24
|
+
turnComplete?: boolean;
|
|
25
|
+
}): void;
|
|
26
|
+
/** Replies to a server tool call with one or more function responses. */
|
|
27
|
+
sendToolResponse(params: {
|
|
28
|
+
functionResponses: FunctionResponse[] | FunctionResponse;
|
|
29
|
+
}): void;
|
|
30
|
+
/** Terminates the underlying connection. */
|
|
31
|
+
close(): void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Concrete arguments handed to {@link GeminiRealtime.connectLiveSession}. Bundles the resolved
|
|
35
|
+
* connect config (system instruction, tools, modalities, transcription) and the message callback so
|
|
36
|
+
* the seam owns the entire `ai.live.connect` call and tests can substitute it wholesale.
|
|
37
|
+
*/
|
|
38
|
+
export interface GeminiConnectArgs {
|
|
39
|
+
/** The Gemini Live model id to open the session against. */
|
|
40
|
+
Model: string;
|
|
41
|
+
/** The fully-built connect config (system instruction, tools, modalities, transcription, plus the open config bag). */
|
|
42
|
+
Config: LiveConnectConfig;
|
|
43
|
+
/** Invoked for every {@link LiveServerMessage} the server emits over the session. */
|
|
44
|
+
OnMessage: (message: LiveServerMessage) => void;
|
|
45
|
+
/** Invoked on a websocket-level error (fatal — the session is unusable). Optional. */
|
|
46
|
+
OnError?: (event: ErrorEvent) => void;
|
|
47
|
+
/** Invoked when the websocket closes. Optional. */
|
|
48
|
+
OnClose?: (event: CloseEvent) => void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Real-time, full-duplex driver for Google's **Gemini Live API**, implementing the Core
|
|
52
|
+
* {@link BaseRealtimeModel} primitive.
|
|
53
|
+
*
|
|
54
|
+
* The driver opens a bidirectional Gemini Live session, streams client audio in, and translates the
|
|
55
|
+
* provider's {@link LiveServerMessage} frames into the modality-agnostic Core events
|
|
56
|
+
* ({@link RealtimeTranscript}, {@link RealtimeToolCall}, {@link RealtimeUsage}, output media, and
|
|
57
|
+
* interruption). It registers via the MemberJunction class factory as `GeminiRealtime` and is
|
|
58
|
+
* resolved for `MJ: AI Models` typed `Realtime`.
|
|
59
|
+
*
|
|
60
|
+
* **Testability:** the live-session creation is isolated behind the overridable
|
|
61
|
+
* {@link connectLiveSession} seam, so unit tests inject a fake {@link GeminiLiveSession} and exercise
|
|
62
|
+
* the full message→event translation with no network.
|
|
63
|
+
*/
|
|
64
|
+
export declare class GeminiRealtime extends BaseRealtimeModel {
|
|
65
|
+
private geminiClient;
|
|
66
|
+
private geminiTokenClient;
|
|
67
|
+
constructor(apiKey: string);
|
|
68
|
+
/**
|
|
69
|
+
* Opens a Gemini Live session and returns the Core session handle that translates between the
|
|
70
|
+
* provider's frames and the MemberJunction realtime contract.
|
|
71
|
+
*/
|
|
72
|
+
StartSession(params: RealtimeSessionParams): Promise<IRealtimeSession>;
|
|
73
|
+
/**
|
|
74
|
+
* Gemini Live supports the client-direct topology: the server mints a short-lived ephemeral
|
|
75
|
+
* auth token (`v1alpha` `auth_tokens` API) that the browser uses to open its OWN Live
|
|
76
|
+
* websocket, while the server keeps prompt/tool authority by LOCKING the connect config into
|
|
77
|
+
* the token via `liveConnectConstraints`.
|
|
78
|
+
*/
|
|
79
|
+
get SupportsClientDirect(): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Mints an ephemeral, server-scoped Live credential for a **client-direct** session.
|
|
82
|
+
*
|
|
83
|
+
* The connect config is built EXACTLY as {@link StartSession} builds it (same
|
|
84
|
+
* {@link buildConnectConfig}: audio modality, input+output transcription, system instruction,
|
|
85
|
+
* mapped tools) and is **locked into the token** via `liveConnectConstraints` +
|
|
86
|
+
* `lockAdditionalFields: []` — so the API ignores any attempt by the browser to change the
|
|
87
|
+
* locked fields. The same config is ALSO carried in `SessionConfig` (as `{ model, config }`)
|
|
88
|
+
* because the SDK still expects the client to pass a model/config at `live.connect` time; the
|
|
89
|
+
* token-side lock is what makes the server's prompt and tool set authoritative.
|
|
90
|
+
*
|
|
91
|
+
* Expiry: the browser must open its session within
|
|
92
|
+
* {@link GEMINI_CLIENT_TOKEN_NEW_SESSION_WINDOW_MS}; the token (and thus the session's
|
|
93
|
+
* ability to send messages) dies at {@link GEMINI_CLIENT_TOKEN_EXPIRY_MS}.
|
|
94
|
+
*
|
|
95
|
+
* @param params Session configuration (model, system prompt, tools, config bag).
|
|
96
|
+
* @returns The minted {@link ClientRealtimeSessionConfig} the browser authenticates + applies.
|
|
97
|
+
*/
|
|
98
|
+
CreateClientSession(params: RealtimeSessionParams): Promise<ClientRealtimeSessionConfig>;
|
|
99
|
+
/**
|
|
100
|
+
* Mint seam for the ephemeral auth token. Production routes through the SDK's
|
|
101
|
+
* `authTokens.create` on a `v1alpha` client (ephemeral tokens are v1alpha-only); unit tests
|
|
102
|
+
* override this to return a fake token with no network.
|
|
103
|
+
*
|
|
104
|
+
* @param params The auth-token create parameters (expiry, uses, live-connect constraints).
|
|
105
|
+
* @returns The created {@link AuthToken} (its `name` is the credential the browser presents).
|
|
106
|
+
*/
|
|
107
|
+
protected mintAuthToken(params: CreateAuthTokenParameters): Promise<AuthToken>;
|
|
108
|
+
/**
|
|
109
|
+
* Lazily constructs the `v1alpha` `GoogleGenAI` client used ONLY for auth-token minting
|
|
110
|
+
* (the ephemeral-token API is exposed on `v1alpha`; the regular live client stays default).
|
|
111
|
+
*/
|
|
112
|
+
private ensureTokenClient;
|
|
113
|
+
/**
|
|
114
|
+
* Creation seam for the underlying Gemini Live session.
|
|
115
|
+
*
|
|
116
|
+
* Production code routes through `ai.live.connect`; unit tests override this method to inject a
|
|
117
|
+
* fake {@link GeminiLiveSession}. Kept as a thin, single-responsibility method so the network
|
|
118
|
+
* boundary is the *only* thing tests need to replace.
|
|
119
|
+
*
|
|
120
|
+
* @param args Resolved model, connect config, and the server-message callback.
|
|
121
|
+
* @returns A promise resolving to the live session handle.
|
|
122
|
+
*/
|
|
123
|
+
protected connectLiveSession(args: GeminiConnectArgs): Promise<GeminiLiveSession>;
|
|
124
|
+
/**
|
|
125
|
+
* Lazily constructs the `GoogleGenAI` client from the driver's API key.
|
|
126
|
+
*/
|
|
127
|
+
private ensureClient;
|
|
128
|
+
/**
|
|
129
|
+
* Builds the {@link LiveConnectConfig} from the Core session params: audio response modality,
|
|
130
|
+
* input/output transcription, system instruction, mapped tools, plus any provider-specific
|
|
131
|
+
* overrides from the open config bag.
|
|
132
|
+
*/
|
|
133
|
+
/**
|
|
134
|
+
* Projects the full connect config down to the fields Gemini's ephemeral-token API accepts
|
|
135
|
+
* as `liveConnectConstraints.config`. The token mint converts the provided keys into a
|
|
136
|
+
* field mask over `BidiGenerateContentSetup`, and only generation-level fields are valid
|
|
137
|
+
* there — `systemInstruction`, `tools`, and the transcription configs are NOT, and their
|
|
138
|
+
* presence 400s the entire mint. Only defined fields are copied (an absent key must stay
|
|
139
|
+
* absent so it doesn't enter the mask).
|
|
140
|
+
*/
|
|
141
|
+
static BuildConstraintConfig(config: LiveConnectConfig): LiveConnectConfig;
|
|
142
|
+
private buildConnectConfig;
|
|
143
|
+
/**
|
|
144
|
+
* Maps Core {@link RealtimeToolDefinition}s up to Gemini {@link FunctionDeclaration}s.
|
|
145
|
+
*
|
|
146
|
+
* The Core `ParametersSchema` is a JSON-schema object, so it rides in `parametersJsonSchema`
|
|
147
|
+
* (the SDK's JSON-schema slot) rather than the OpenAPI-style `parameters` slot.
|
|
148
|
+
*/
|
|
149
|
+
static MapToolsToFunctionDeclarations(tools: RealtimeToolDefinition[]): FunctionDeclaration[];
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=geminiRealtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geminiRealtime.d.ts","sourceRoot":"","sources":["../src/geminiRealtime.ts"],"names":[],"mappings":"AACA,OAAO,EAGH,KAAK,SAAS,EACd,KAAK,yBAAyB,EAC9B,KAAK,iBAAiB,EAEtB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EAExB,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACZ,KAAK,IAAI,IAAI,UAAU,EAC1B,MAAM,eAAe,CAAC;AAGvB,OAAO,EACH,iBAAiB,EACjB,KAAK,2BAA2B,EAChC,KAAK,gBAAgB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAO9B,MAAM,oBAAoB,CAAC;AAuB5B;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAC9B;;;;;OAKG;IACH,iBAAiB,CAAC,MAAM,EAAE;QA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