@kindgi/adapter-model-gemini 0.1.4-rc.3 → 0.1.4-rc.5
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/provider.d.ts +24 -9
- package/dist/provider.d.ts.map +1 -1
- package/dist/provider.js +51 -11
- package/dist/provider.js.map +1 -1
- package/package.json +2 -2
- package/src/provider.ts +73 -16
package/dist/provider.d.ts
CHANGED
|
@@ -18,24 +18,34 @@ export interface GeminiProviderOptions {
|
|
|
18
18
|
readonly metadata: Omit<ProviderMetadata, 'models'> & {
|
|
19
19
|
readonly models: readonly GeminiModelInfo[];
|
|
20
20
|
};
|
|
21
|
-
/**
|
|
22
|
-
|
|
21
|
+
/**
|
|
22
|
+
* The Vertex AI project and location (`global`, or a region such as
|
|
23
|
+
* `us-central1`): Gemini on Vertex AI, the route for a regulated
|
|
24
|
+
* deployment (its data residency, its IAM). Set this or `apiKey`.
|
|
25
|
+
*/
|
|
26
|
+
readonly vertex?: {
|
|
23
27
|
readonly project: string;
|
|
24
28
|
readonly location: string;
|
|
25
29
|
};
|
|
26
30
|
/**
|
|
27
|
-
* A service-account key (its JSON), resolved on every call.
|
|
28
|
-
* Google Application Default Credentials — `gcloud auth
|
|
31
|
+
* A service-account key (its JSON), resolved on every call, for Vertex.
|
|
32
|
+
* Absent: Google Application Default Credentials — `gcloud auth
|
|
29
33
|
* application-default login` on a laptop, the attached service account
|
|
30
34
|
* on Cloud Run and other Google Cloud runtimes.
|
|
31
35
|
*/
|
|
32
36
|
readonly credentials?: () => string | Promise<string>;
|
|
33
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* A Gemini Developer API key (from Google AI Studio), resolved on every
|
|
39
|
+
* call: Gemini without a Google Cloud project. Set this or `vertex`.
|
|
40
|
+
*/
|
|
41
|
+
readonly apiKey?: () => string | Promise<string>;
|
|
42
|
+
/** An injected client, used as is (tests). `vertex`, `credentials` and `apiKey` are then unused. */
|
|
34
43
|
readonly client?: GeminiClient;
|
|
35
44
|
}
|
|
36
45
|
/**
|
|
37
|
-
* A Gemini `ModelProvider
|
|
38
|
-
*
|
|
46
|
+
* A Gemini `ModelProvider`, on Vertex AI (`vertex`) or the Gemini
|
|
47
|
+
* Developer API (`apiKey`). Non-streaming, tool-use enabled: one
|
|
48
|
+
* `generateContent` call per `invoke`.
|
|
39
49
|
*/
|
|
40
50
|
export declare function createGeminiProvider(options: GeminiProviderOptions): ModelProvider;
|
|
41
51
|
/**
|
|
@@ -49,15 +59,20 @@ export declare function createGeminiProvider(options: GeminiProviderOptions): Mo
|
|
|
49
59
|
export declare function parseServiceAccountKey(key: string): Record<string, string>;
|
|
50
60
|
/**
|
|
51
61
|
* The adapter factory servers register under `GEMINI_ADAPTER_ID`. A
|
|
52
|
-
* provider registered with it carries
|
|
62
|
+
* provider registered with it carries `adapter_config.api`: `vertex` (the
|
|
63
|
+
* default) or `developer`.
|
|
53
64
|
*
|
|
65
|
+
* On Vertex AI (`api: "vertex"`, or none):
|
|
54
66
|
* - `adapter_config.project` — the Vertex AI project (required);
|
|
55
67
|
* - `metadata.region` — the Vertex location (`global`, `us-central1`,
|
|
56
68
|
* `northamerica-northeast1`, …); `'unspecified'` means `global`;
|
|
57
69
|
* - `secret_ref` (optional) — a service-account key. Without one the
|
|
58
70
|
* adapter uses Application Default Credentials.
|
|
71
|
+
*
|
|
72
|
+
* On the Gemini Developer API (`api: "developer"`):
|
|
73
|
+
* - `secret_ref` (required) — a Gemini API key from Google AI Studio.
|
|
59
74
|
*/
|
|
60
75
|
export declare const geminiAdapterFactory: AdapterFactory;
|
|
61
76
|
/** The Vertex project and location a registration names (see `geminiAdapterFactory`). */
|
|
62
|
-
export declare function vertexTarget(input: Parameters<AdapterFactory>[0]): GeminiProviderOptions['vertex']
|
|
77
|
+
export declare function vertexTarget(input: Parameters<AdapterFactory>[0]): NonNullable<GeminiProviderOptions['vertex']>;
|
|
63
78
|
//# sourceMappingURL=provider.d.ts.map
|
package/dist/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAE7B,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EACV,cAAc,EAGd,aAAa,EACb,gBAAgB,EACjB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,KAAK,eAAe,EAAoC,MAAM,WAAW,CAAC;AAGnF,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,iCAAiC,CAAC;AAEhE,wEAAwE;AACxE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE;QACf,eAAe,CAAC,MAAM,EAAE,yBAAyB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;KACtF,CAAC;CACH;AAED,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG;QACpD,QAAQ,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,CAAC;KAC7C,CAAC;IACF
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAE7B,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EACV,cAAc,EAGd,aAAa,EACb,gBAAgB,EACjB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAE,KAAK,eAAe,EAAoC,MAAM,WAAW,CAAC;AAGnF,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,iCAAiC,CAAC;AAEhE,wEAAwE;AACxE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,MAAM,EAAE;QACf,eAAe,CAAC,MAAM,EAAE,yBAAyB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;KACtF,CAAC;CACH;AAED,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG;QACpD,QAAQ,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,CAAC;KAC7C,CAAC;IACF;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1E;;;;;OAKG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,oGAAoG;IACpG,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;CAChC;AAID;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,aAAa,CAoGlF;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAiC1E;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,oBAAoB,EAAE,cAqBlC,CAAC;AAEF,yFAAyF;AACzF,wBAAgB,YAAY,CAC1B,KAAK,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,GACnC,WAAW,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC,CAa9C"}
|
package/dist/provider.js
CHANGED
|
@@ -8,12 +8,14 @@ import { fromGeminiResponse, toGeminiFunctions, toGeminiRequest } from './transl
|
|
|
8
8
|
export const GEMINI_ADAPTER_ID = '@kindgi/adapter-model-gemini';
|
|
9
9
|
const CLOUD_PLATFORM_SCOPE = 'https://www.googleapis.com/auth/cloud-platform';
|
|
10
10
|
/**
|
|
11
|
-
* A Gemini `ModelProvider
|
|
12
|
-
*
|
|
11
|
+
* A Gemini `ModelProvider`, on Vertex AI (`vertex`) or the Gemini
|
|
12
|
+
* Developer API (`apiKey`). Non-streaming, tool-use enabled: one
|
|
13
|
+
* `generateContent` call per `invoke`.
|
|
13
14
|
*/
|
|
14
15
|
export function createGeminiProvider(options) {
|
|
15
16
|
const { metadata } = options;
|
|
16
|
-
|
|
17
|
+
if (options.client === undefined)
|
|
18
|
+
checkTarget(metadata.id, options);
|
|
17
19
|
const modelsByName = new Map(metadata.models.map((m) => [m.name, m]));
|
|
18
20
|
// One client per credential: reused while the resolved key stays the
|
|
19
21
|
// same, rebuilt when it rotates. `''` stands for ADC.
|
|
@@ -23,13 +25,25 @@ export function createGeminiProvider(options) {
|
|
|
23
25
|
async function resolveClient() {
|
|
24
26
|
if (options.client !== undefined)
|
|
25
27
|
return options.client;
|
|
28
|
+
if (options.apiKey !== undefined) {
|
|
29
|
+
const key = await options.apiKey();
|
|
30
|
+
if (key.trim() === '') {
|
|
31
|
+
throw new Error(`${GEMINI_ADAPTER_ID}: provider "${metadata.id}"'s Gemini API key is empty.`);
|
|
32
|
+
}
|
|
33
|
+
if (cached?.key === key)
|
|
34
|
+
return cached.client;
|
|
35
|
+
const client = new GoogleGenAI({ apiKey: key, httpOptions: { fetch: attempts.fetch } });
|
|
36
|
+
cached = { key, client };
|
|
37
|
+
return client;
|
|
38
|
+
}
|
|
39
|
+
const vertex = options.vertex;
|
|
26
40
|
const key = options.credentials !== undefined ? await options.credentials() : '';
|
|
27
41
|
if (cached?.key === key)
|
|
28
42
|
return cached.client;
|
|
29
43
|
const client = new GoogleGenAI({
|
|
30
44
|
vertexai: true,
|
|
31
|
-
project:
|
|
32
|
-
location:
|
|
45
|
+
project: vertex.project,
|
|
46
|
+
location: vertex.location,
|
|
33
47
|
httpOptions: { fetch: attempts.fetch },
|
|
34
48
|
...(key !== '' && {
|
|
35
49
|
googleAuthOptions: {
|
|
@@ -131,19 +145,37 @@ export function parseServiceAccountKey(key) {
|
|
|
131
145
|
}
|
|
132
146
|
/**
|
|
133
147
|
* The adapter factory servers register under `GEMINI_ADAPTER_ID`. A
|
|
134
|
-
* provider registered with it carries
|
|
148
|
+
* provider registered with it carries `adapter_config.api`: `vertex` (the
|
|
149
|
+
* default) or `developer`.
|
|
135
150
|
*
|
|
151
|
+
* On Vertex AI (`api: "vertex"`, or none):
|
|
136
152
|
* - `adapter_config.project` — the Vertex AI project (required);
|
|
137
153
|
* - `metadata.region` — the Vertex location (`global`, `us-central1`,
|
|
138
154
|
* `northamerica-northeast1`, …); `'unspecified'` means `global`;
|
|
139
155
|
* - `secret_ref` (optional) — a service-account key. Without one the
|
|
140
156
|
* adapter uses Application Default Credentials.
|
|
157
|
+
*
|
|
158
|
+
* On the Gemini Developer API (`api: "developer"`):
|
|
159
|
+
* - `secret_ref` (required) — a Gemini API key from Google AI Studio.
|
|
141
160
|
*/
|
|
142
|
-
export const geminiAdapterFactory = (input) =>
|
|
143
|
-
metadata
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
161
|
+
export const geminiAdapterFactory = (input) => {
|
|
162
|
+
const metadata = input.metadata;
|
|
163
|
+
const api = input.config?.api ?? 'vertex';
|
|
164
|
+
if (api === 'developer') {
|
|
165
|
+
if (input.resolveApiKey === undefined) {
|
|
166
|
+
throw new Error(`${GEMINI_ADAPTER_ID}: provider "${input.metadata.id}" uses the Gemini Developer API (adapter_config.api "developer") and needs secret_ref: its Gemini API key.`);
|
|
167
|
+
}
|
|
168
|
+
return createGeminiProvider({ metadata, apiKey: input.resolveApiKey });
|
|
169
|
+
}
|
|
170
|
+
if (api !== 'vertex') {
|
|
171
|
+
throw new Error(`${GEMINI_ADAPTER_ID}: provider "${input.metadata.id}" adapter_config.api must be "vertex" or "developer", got ${JSON.stringify(api)}.`);
|
|
172
|
+
}
|
|
173
|
+
return createGeminiProvider({
|
|
174
|
+
metadata,
|
|
175
|
+
vertex: vertexTarget(input),
|
|
176
|
+
...(input.resolveApiKey !== undefined && { credentials: input.resolveApiKey }),
|
|
177
|
+
});
|
|
178
|
+
};
|
|
147
179
|
/** The Vertex project and location a registration names (see `geminiAdapterFactory`). */
|
|
148
180
|
export function vertexTarget(input) {
|
|
149
181
|
const project = input.config?.project;
|
|
@@ -157,6 +189,14 @@ export function vertexTarget(input) {
|
|
|
157
189
|
checkVertexTarget(input.metadata.id, target);
|
|
158
190
|
return target;
|
|
159
191
|
}
|
|
192
|
+
/** A provider names exactly one target: Vertex AI, or a Gemini API key. */
|
|
193
|
+
function checkTarget(providerId, options) {
|
|
194
|
+
if ((options.vertex === undefined) === (options.apiKey === undefined)) {
|
|
195
|
+
throw new Error(`${GEMINI_ADAPTER_ID}: provider "${providerId}" needs exactly one of vertex (a Vertex AI project) or apiKey (a Gemini Developer API key).`);
|
|
196
|
+
}
|
|
197
|
+
if (options.vertex !== undefined)
|
|
198
|
+
checkVertexTarget(providerId, options.vertex);
|
|
199
|
+
}
|
|
160
200
|
/**
|
|
161
201
|
* The SDK puts the project in the request path and builds the hostname
|
|
162
202
|
* from the location (`https://<location>-aiplatform.googleapis.com/`):
|
package/dist/provider.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAEjC,OAAO,EAIL,WAAW,GACZ,MAAM,eAAe,CAAC;AAQvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAErE,OAAO,EAAwB,cAAc,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AACnF,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAExF,oEAAoE;AACpE,MAAM,CAAC,MAAM,iBAAiB,GAAG,8BAA8B,CAAC;
|
|
1
|
+
{"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cAAc,GAAG,gDAAgD,CAAC;AACxE,2CAA2C;AAC3C,MAAM,eAAe,GAAG,wBAAwB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kindgi/adapter-model-gemini",
|
|
3
|
-
"version": "0.1.4-rc.
|
|
3
|
+
"version": "0.1.4-rc.5",
|
|
4
4
|
"description": "Gemini ModelProvider for @kindgi/capabilities, on Vertex AI. Wraps @google/genai with the framework's provider-neutral interface: message-trail ↔ generateContent translation, function calling with thought signatures, token and cost accounting including thinking tokens, and Google Application Default Credentials (or a service-account key from the secret store). Non-streaming, tool-use enabled.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"README.md"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@kindgi/capabilities": "0.1.4-rc.
|
|
31
|
+
"@kindgi/capabilities": "0.1.4-rc.5",
|
|
32
32
|
"@google/genai": "^2.24.0"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|
package/src/provider.ts
CHANGED
|
@@ -38,28 +38,38 @@ export interface GeminiProviderOptions {
|
|
|
38
38
|
readonly metadata: Omit<ProviderMetadata, 'models'> & {
|
|
39
39
|
readonly models: readonly GeminiModelInfo[];
|
|
40
40
|
};
|
|
41
|
-
/** The Vertex AI project and location (`global`, or a region such as `us-central1`). */
|
|
42
|
-
readonly vertex: { readonly project: string; readonly location: string };
|
|
43
41
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
42
|
+
* The Vertex AI project and location (`global`, or a region such as
|
|
43
|
+
* `us-central1`): Gemini on Vertex AI, the route for a regulated
|
|
44
|
+
* deployment (its data residency, its IAM). Set this or `apiKey`.
|
|
45
|
+
*/
|
|
46
|
+
readonly vertex?: { readonly project: string; readonly location: string };
|
|
47
|
+
/**
|
|
48
|
+
* A service-account key (its JSON), resolved on every call, for Vertex.
|
|
49
|
+
* Absent: Google Application Default Credentials — `gcloud auth
|
|
46
50
|
* application-default login` on a laptop, the attached service account
|
|
47
51
|
* on Cloud Run and other Google Cloud runtimes.
|
|
48
52
|
*/
|
|
49
53
|
readonly credentials?: () => string | Promise<string>;
|
|
50
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* A Gemini Developer API key (from Google AI Studio), resolved on every
|
|
56
|
+
* call: Gemini without a Google Cloud project. Set this or `vertex`.
|
|
57
|
+
*/
|
|
58
|
+
readonly apiKey?: () => string | Promise<string>;
|
|
59
|
+
/** An injected client, used as is (tests). `vertex`, `credentials` and `apiKey` are then unused. */
|
|
51
60
|
readonly client?: GeminiClient;
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
const CLOUD_PLATFORM_SCOPE = 'https://www.googleapis.com/auth/cloud-platform';
|
|
55
64
|
|
|
56
65
|
/**
|
|
57
|
-
* A Gemini `ModelProvider
|
|
58
|
-
*
|
|
66
|
+
* A Gemini `ModelProvider`, on Vertex AI (`vertex`) or the Gemini
|
|
67
|
+
* Developer API (`apiKey`). Non-streaming, tool-use enabled: one
|
|
68
|
+
* `generateContent` call per `invoke`.
|
|
59
69
|
*/
|
|
60
70
|
export function createGeminiProvider(options: GeminiProviderOptions): ModelProvider {
|
|
61
71
|
const { metadata } = options;
|
|
62
|
-
|
|
72
|
+
if (options.client === undefined) checkTarget(metadata.id, options);
|
|
63
73
|
const modelsByName = new Map<string, GeminiModelInfo>(
|
|
64
74
|
metadata.models.map((m) => [m.name, m] as const),
|
|
65
75
|
);
|
|
@@ -71,12 +81,25 @@ export function createGeminiProvider(options: GeminiProviderOptions): ModelProvi
|
|
|
71
81
|
const attempts = createAttemptCounter();
|
|
72
82
|
async function resolveClient(): Promise<GeminiClient> {
|
|
73
83
|
if (options.client !== undefined) return options.client;
|
|
84
|
+
if (options.apiKey !== undefined) {
|
|
85
|
+
const key = await options.apiKey();
|
|
86
|
+
if (key.trim() === '') {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`${GEMINI_ADAPTER_ID}: provider "${metadata.id}"'s Gemini API key is empty.`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (cached?.key === key) return cached.client;
|
|
92
|
+
const client = new GoogleGenAI({ apiKey: key, httpOptions: { fetch: attempts.fetch } });
|
|
93
|
+
cached = { key, client };
|
|
94
|
+
return client;
|
|
95
|
+
}
|
|
96
|
+
const vertex = options.vertex as NonNullable<GeminiProviderOptions['vertex']>;
|
|
74
97
|
const key = options.credentials !== undefined ? await options.credentials() : '';
|
|
75
98
|
if (cached?.key === key) return cached.client;
|
|
76
99
|
const client = new GoogleGenAI({
|
|
77
100
|
vertexai: true,
|
|
78
|
-
project:
|
|
79
|
-
location:
|
|
101
|
+
project: vertex.project,
|
|
102
|
+
location: vertex.location,
|
|
80
103
|
httpOptions: { fetch: attempts.fetch },
|
|
81
104
|
...(key !== '' && {
|
|
82
105
|
googleAuthOptions: {
|
|
@@ -191,25 +214,46 @@ export function parseServiceAccountKey(key: string): Record<string, string> {
|
|
|
191
214
|
|
|
192
215
|
/**
|
|
193
216
|
* The adapter factory servers register under `GEMINI_ADAPTER_ID`. A
|
|
194
|
-
* provider registered with it carries
|
|
217
|
+
* provider registered with it carries `adapter_config.api`: `vertex` (the
|
|
218
|
+
* default) or `developer`.
|
|
195
219
|
*
|
|
220
|
+
* On Vertex AI (`api: "vertex"`, or none):
|
|
196
221
|
* - `adapter_config.project` — the Vertex AI project (required);
|
|
197
222
|
* - `metadata.region` — the Vertex location (`global`, `us-central1`,
|
|
198
223
|
* `northamerica-northeast1`, …); `'unspecified'` means `global`;
|
|
199
224
|
* - `secret_ref` (optional) — a service-account key. Without one the
|
|
200
225
|
* adapter uses Application Default Credentials.
|
|
226
|
+
*
|
|
227
|
+
* On the Gemini Developer API (`api: "developer"`):
|
|
228
|
+
* - `secret_ref` (required) — a Gemini API key from Google AI Studio.
|
|
201
229
|
*/
|
|
202
|
-
export const geminiAdapterFactory: AdapterFactory = (input) =>
|
|
203
|
-
|
|
204
|
-
|
|
230
|
+
export const geminiAdapterFactory: AdapterFactory = (input) => {
|
|
231
|
+
const metadata = input.metadata as GeminiProviderOptions['metadata'];
|
|
232
|
+
const api = input.config?.api ?? 'vertex';
|
|
233
|
+
if (api === 'developer') {
|
|
234
|
+
if (input.resolveApiKey === undefined) {
|
|
235
|
+
throw new Error(
|
|
236
|
+
`${GEMINI_ADAPTER_ID}: provider "${input.metadata.id}" uses the Gemini Developer API (adapter_config.api "developer") and needs secret_ref: its Gemini API key.`,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
return createGeminiProvider({ metadata, apiKey: input.resolveApiKey });
|
|
240
|
+
}
|
|
241
|
+
if (api !== 'vertex') {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`${GEMINI_ADAPTER_ID}: provider "${input.metadata.id}" adapter_config.api must be "vertex" or "developer", got ${JSON.stringify(api)}.`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
return createGeminiProvider({
|
|
247
|
+
metadata,
|
|
205
248
|
vertex: vertexTarget(input),
|
|
206
249
|
...(input.resolveApiKey !== undefined && { credentials: input.resolveApiKey }),
|
|
207
250
|
});
|
|
251
|
+
};
|
|
208
252
|
|
|
209
253
|
/** The Vertex project and location a registration names (see `geminiAdapterFactory`). */
|
|
210
254
|
export function vertexTarget(
|
|
211
255
|
input: Parameters<AdapterFactory>[0],
|
|
212
|
-
): GeminiProviderOptions['vertex'] {
|
|
256
|
+
): NonNullable<GeminiProviderOptions['vertex']> {
|
|
213
257
|
const project = input.config?.project;
|
|
214
258
|
if (typeof project !== 'string' || project.length === 0) {
|
|
215
259
|
throw new Error(
|
|
@@ -224,13 +268,26 @@ export function vertexTarget(
|
|
|
224
268
|
return target;
|
|
225
269
|
}
|
|
226
270
|
|
|
271
|
+
/** A provider names exactly one target: Vertex AI, or a Gemini API key. */
|
|
272
|
+
function checkTarget(providerId: string, options: GeminiProviderOptions): void {
|
|
273
|
+
if ((options.vertex === undefined) === (options.apiKey === undefined)) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
`${GEMINI_ADAPTER_ID}: provider "${providerId}" needs exactly one of vertex (a Vertex AI project) or apiKey (a Gemini Developer API key).`,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (options.vertex !== undefined) checkVertexTarget(providerId, options.vertex);
|
|
279
|
+
}
|
|
280
|
+
|
|
227
281
|
/**
|
|
228
282
|
* The SDK puts the project in the request path and builds the hostname
|
|
229
283
|
* from the location (`https://<location>-aiplatform.googleapis.com/`):
|
|
230
284
|
* anything but a project id and one DNS label could move the request,
|
|
231
285
|
* with the server's credentials, to another host.
|
|
232
286
|
*/
|
|
233
|
-
function checkVertexTarget(
|
|
287
|
+
function checkVertexTarget(
|
|
288
|
+
providerId: string,
|
|
289
|
+
vertex: NonNullable<GeminiProviderOptions['vertex']>,
|
|
290
|
+
): void {
|
|
234
291
|
if (!VERTEX_PROJECT.test(vertex.project)) {
|
|
235
292
|
throw new Error(
|
|
236
293
|
`${GEMINI_ADAPTER_ID}: provider "${providerId}" adapter_config.project must be a Google Cloud project id or number.`,
|