@axemere/gateway-google 0.2.0 → 1.0.2
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 +8 -6
- package/dist/index.d.ts +11 -20
- package/dist/index.js +35 -41
- package/dist/index.test.js +35 -28
- package/package.json +11 -19
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# @axemere/gateway-google
|
|
2
2
|
|
|
3
|
-
Google
|
|
3
|
+
Google GenAI drop-in wrapper for the [Axemere AI Gateway](https://axemere.ai).
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @axemere/gateway-google @google/
|
|
8
|
+
npm install @axemere/gateway-google @google/genai
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
@@ -15,12 +15,14 @@ import { genaiClient } from "@axemere/gateway-google";
|
|
|
15
15
|
import { AiGatewayConfig } from "@axemere/gateway";
|
|
16
16
|
|
|
17
17
|
const ai = genaiClient(new AiGatewayConfig());
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const response = await ai.models.generateContent({
|
|
19
|
+
model: "gemini-3.8-flash",
|
|
20
|
+
contents: "Explain quantum entanglement in one sentence.",
|
|
21
|
+
});
|
|
22
|
+
console.log(response.text);
|
|
21
23
|
```
|
|
22
24
|
|
|
23
|
-
The returned client is a standard `
|
|
25
|
+
The returned client is a standard `GoogleGenAI` instance — no other code changes needed.
|
|
24
26
|
|
|
25
27
|
## Configuration
|
|
26
28
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,33 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { ModelParams, RequestOptions } from "@google/generative-ai";
|
|
1
|
+
import { GoogleGenAI } from "@google/genai";
|
|
3
2
|
import { AiGatewayConfig } from "@axemere/gateway";
|
|
4
3
|
export { AiGatewayConfig };
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
7
|
-
* through the Axemere AI Gateway. Gateway auth headers and base URL are injected
|
|
8
|
-
* into every getGenerativeModel() call without requiring the caller to pass
|
|
9
|
-
* requestOptions manually.
|
|
10
|
-
*/
|
|
11
|
-
declare class GatewayGenerativeAI extends GoogleGenerativeAI {
|
|
12
|
-
private readonly _gatewayRequestOptions;
|
|
13
|
-
constructor(apiKey: string, gatewayRequestOptions: RequestOptions);
|
|
14
|
-
getGenerativeModel(modelParams: ModelParams, requestOptions?: RequestOptions): import("@google/generative-ai").GenerativeModel;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Creates a GoogleGenerativeAI client pre-configured to route through the Axemere AI Gateway.
|
|
5
|
+
* Creates a GoogleGenAI client pre-configured to route through the Axemere AI Gateway.
|
|
18
6
|
*
|
|
19
|
-
* The returned client is a standard
|
|
20
|
-
* the official @google/
|
|
7
|
+
* The returned client is a standard GoogleGenAI instance — use it exactly as you would
|
|
8
|
+
* the official @google/genai package. All requests are proxied through the gateway,
|
|
21
9
|
* which applies policy, budgets, and records usage.
|
|
22
10
|
*
|
|
23
11
|
* @param config - AiGatewayConfig instance (reads env vars if omitted).
|
|
24
|
-
* @returns A
|
|
12
|
+
* @returns A GoogleGenAI instance with gateway routing applied to every request.
|
|
25
13
|
*
|
|
26
14
|
* @example
|
|
27
15
|
* ```ts
|
|
28
16
|
* const ai = genaiClient(new AiGatewayConfig());
|
|
29
|
-
* const
|
|
30
|
-
*
|
|
17
|
+
* const response = await ai.models.generateContent({
|
|
18
|
+
* model: "gemini-3.8-flash",
|
|
19
|
+
* contents: "Hello",
|
|
20
|
+
* });
|
|
21
|
+
* console.log(response.text);
|
|
31
22
|
* ```
|
|
32
23
|
*/
|
|
33
|
-
export declare function genaiClient(config?: AiGatewayConfig):
|
|
24
|
+
export declare function genaiClient(config?: AiGatewayConfig): GoogleGenAI;
|
package/dist/index.js
CHANGED
|
@@ -2,63 +2,57 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AiGatewayConfig = void 0;
|
|
4
4
|
exports.genaiClient = genaiClient;
|
|
5
|
-
const
|
|
5
|
+
const genai_1 = require("@google/genai");
|
|
6
6
|
const gateway_1 = require("@axemere/gateway");
|
|
7
7
|
Object.defineProperty(exports, "AiGatewayConfig", { enumerable: true, get: function () { return gateway_1.AiGatewayConfig; } });
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
...this._gatewayRequestOptions,
|
|
24
|
-
...requestOptions,
|
|
25
|
-
};
|
|
26
|
-
// Merge customHeaders: gateway headers first, caller headers win on conflict.
|
|
27
|
-
// Note: x-goog-api-key cannot be set via customHeaders (SDK enforces this),
|
|
28
|
-
// so the placeholder key is forwarded as-is. The gateway recognises
|
|
29
|
-
// PLACEHOLDER_API_KEY ("axemere-proxy-managed") and does not treat it as BYOK.
|
|
30
|
-
const gwHeaders = new Headers(this._gatewayRequestOptions.customHeaders ?? {});
|
|
31
|
-
if (requestOptions?.customHeaders) {
|
|
32
|
-
const callerHeaders = new Headers(requestOptions.customHeaders);
|
|
33
|
-
callerHeaders.forEach((val, key) => gwHeaders.set(key, val));
|
|
8
|
+
// The underlying SDK unconditionally sets "x-goog-api-key" from the apiKey
|
|
9
|
+
// constructor arg on every request, with no public way to remove or
|
|
10
|
+
// override it via headers (unlike other provider SDKs, Google's considers
|
|
11
|
+
// it a reserved header). If the placeholder key reaches the gateway in that
|
|
12
|
+
// header, the gateway treats it as a BYOK (bring-your-own-key) declaration
|
|
13
|
+
// and forwards it to Google verbatim — which Google then rejects as an
|
|
14
|
+
// invalid key. Stripping it here, via the SDK's own httpOptions.fetch hook,
|
|
15
|
+
// is what lets the gateway fall back to injecting its own managed
|
|
16
|
+
// credential instead.
|
|
17
|
+
function stripGoogApiKeyFetch() {
|
|
18
|
+
return async (input, init) => {
|
|
19
|
+
if (init?.headers) {
|
|
20
|
+
const headers = new Headers(init.headers);
|
|
21
|
+
headers.delete("x-goog-api-key");
|
|
22
|
+
init = { ...init, headers };
|
|
34
23
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
24
|
+
return fetch(input, init);
|
|
25
|
+
};
|
|
38
26
|
}
|
|
39
27
|
/**
|
|
40
|
-
* Creates a
|
|
28
|
+
* Creates a GoogleGenAI client pre-configured to route through the Axemere AI Gateway.
|
|
41
29
|
*
|
|
42
|
-
* The returned client is a standard
|
|
43
|
-
* the official @google/
|
|
30
|
+
* The returned client is a standard GoogleGenAI instance — use it exactly as you would
|
|
31
|
+
* the official @google/genai package. All requests are proxied through the gateway,
|
|
44
32
|
* which applies policy, budgets, and records usage.
|
|
45
33
|
*
|
|
46
34
|
* @param config - AiGatewayConfig instance (reads env vars if omitted).
|
|
47
|
-
* @returns A
|
|
35
|
+
* @returns A GoogleGenAI instance with gateway routing applied to every request.
|
|
48
36
|
*
|
|
49
37
|
* @example
|
|
50
38
|
* ```ts
|
|
51
39
|
* const ai = genaiClient(new AiGatewayConfig());
|
|
52
|
-
* const
|
|
53
|
-
*
|
|
40
|
+
* const response = await ai.models.generateContent({
|
|
41
|
+
* model: "gemini-3.8-flash",
|
|
42
|
+
* contents: "Hello",
|
|
43
|
+
* });
|
|
44
|
+
* console.log(response.text);
|
|
54
45
|
* ```
|
|
55
46
|
*/
|
|
56
47
|
function genaiClient(config) {
|
|
57
48
|
const cfg = config ?? new gateway_1.AiGatewayConfig();
|
|
58
49
|
const headers = (0, gateway_1.aiGatewayHeaders)(cfg, { targetHost: "generativelanguage.googleapis.com" });
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
return new genai_1.GoogleGenAI({
|
|
51
|
+
apiKey: gateway_1.PLACEHOLDER_API_KEY,
|
|
52
|
+
httpOptions: {
|
|
53
|
+
baseUrl: cfg.gateway_url,
|
|
54
|
+
headers,
|
|
55
|
+
fetch: stripGoogApiKeyFetch(),
|
|
56
|
+
},
|
|
57
|
+
});
|
|
64
58
|
}
|
package/dist/index.test.js
CHANGED
|
@@ -1,54 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const genai_1 = require("@google/genai");
|
|
4
4
|
const gateway_1 = require("@axemere/gateway");
|
|
5
5
|
const index_1 = require("./index");
|
|
6
|
+
function httpOptionsOf(client) {
|
|
7
|
+
return client.httpOptions;
|
|
8
|
+
}
|
|
6
9
|
describe("genaiClient()", () => {
|
|
7
|
-
it("returns a
|
|
10
|
+
it("returns a GoogleGenAI instance", () => {
|
|
8
11
|
const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
|
|
9
12
|
const client = (0, index_1.genaiClient)(cfg);
|
|
10
|
-
expect(client).toBeInstanceOf(
|
|
13
|
+
expect(client).toBeInstanceOf(genai_1.GoogleGenAI);
|
|
11
14
|
});
|
|
12
15
|
it("constructs with default config when no config passed", () => {
|
|
13
16
|
delete process.env["AXEMERE_GATEWAY_URL"];
|
|
14
17
|
delete process.env["AXEMERE_GATEWAY_TOKEN"];
|
|
15
18
|
expect(() => (0, index_1.genaiClient)()).not.toThrow();
|
|
16
19
|
});
|
|
20
|
+
it("sets baseUrl to the gateway URL", () => {
|
|
21
|
+
const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
|
|
22
|
+
const client = (0, index_1.genaiClient)(cfg);
|
|
23
|
+
expect(httpOptionsOf(client).baseUrl).toBe("http://localhost:7080");
|
|
24
|
+
});
|
|
17
25
|
it("injects Authorization header from gateway_token", () => {
|
|
18
26
|
const cfg = new gateway_1.AiGatewayConfig({
|
|
19
27
|
gateway_url: "http://localhost:7080",
|
|
20
28
|
gateway_token: "tok_test",
|
|
21
29
|
});
|
|
22
30
|
const client = (0, index_1.genaiClient)(cfg);
|
|
23
|
-
|
|
24
|
-
// inspecting the GenerativeModel's private _requestOptions.
|
|
25
|
-
const model = client.getGenerativeModel({ model: "gemini-1.5-flash" });
|
|
26
|
-
const opts = model
|
|
27
|
-
._requestOptions;
|
|
28
|
-
expect(opts.customHeaders.get("Authorization")).toBe("Bearer tok_test");
|
|
29
|
-
});
|
|
30
|
-
it("sets baseUrl to the gateway URL", () => {
|
|
31
|
-
const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
|
|
32
|
-
const client = (0, index_1.genaiClient)(cfg);
|
|
33
|
-
const model = client.getGenerativeModel({ model: "gemini-1.5-flash" });
|
|
34
|
-
const opts = model
|
|
35
|
-
._requestOptions;
|
|
36
|
-
expect(opts.baseUrl).toBe("http://localhost:7080");
|
|
31
|
+
expect(httpOptionsOf(client).headers?.["Authorization"]).toBe("Bearer tok_test");
|
|
37
32
|
});
|
|
38
33
|
it("sets X-MVGC-Target-Host to generativelanguage.googleapis.com", () => {
|
|
39
34
|
const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
|
|
40
35
|
const client = (0, index_1.genaiClient)(cfg);
|
|
41
|
-
|
|
42
|
-
const opts = model
|
|
43
|
-
._requestOptions;
|
|
44
|
-
expect(opts.customHeaders.get("X-MVGC-Target-Host")).toBe("generativelanguage.googleapis.com");
|
|
36
|
+
expect(httpOptionsOf(client).headers?.["X-MVGC-Target-Host"]).toBe("generativelanguage.googleapis.com");
|
|
45
37
|
});
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
// Pins the real bug this migration fixed: the previous @google/generative-ai
|
|
39
|
+
// based implementation had no way to stop the SDK from sending the
|
|
40
|
+
// placeholder apiKey as a real "x-goog-api-key" header, which the gateway
|
|
41
|
+
// treats as a BYOK declaration and forwards to Google verbatim — Google
|
|
42
|
+
// then rejects it as an invalid key. Live-verified against a real gateway.
|
|
43
|
+
describe("x-goog-api-key stripping", () => {
|
|
44
|
+
afterEach(() => jest.restoreAllMocks());
|
|
45
|
+
it("removes x-goog-api-key before the request reaches fetch", async () => {
|
|
46
|
+
const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
|
|
47
|
+
const client = (0, index_1.genaiClient)(cfg);
|
|
48
|
+
let capturedHeaders;
|
|
49
|
+
jest.spyOn(globalThis, "fetch").mockImplementation(async (_input, init) => {
|
|
50
|
+
capturedHeaders = new Headers(init?.headers);
|
|
51
|
+
return new Response(JSON.stringify({}), { status: 200 });
|
|
52
|
+
});
|
|
53
|
+
await httpOptionsOf(client).fetch?.("http://localhost:7080/v1beta/models/x", {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { "x-goog-api-key": "axemere-proxy-managed", "content-type": "application/json" },
|
|
56
|
+
});
|
|
57
|
+
expect(capturedHeaders?.has("x-goog-api-key")).toBe(false);
|
|
58
|
+
expect(capturedHeaders?.get("content-type")).toBe("application/json");
|
|
59
|
+
});
|
|
53
60
|
});
|
|
54
61
|
});
|
package/package.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axemere/gateway-google",
|
|
3
|
-
"version": "0.2
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Axemere AI Gateway SDK — Google Generative AI wrapper",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist",
|
|
9
|
-
"README.md"
|
|
10
|
-
],
|
|
7
|
+
"files": ["dist", "README.md"],
|
|
11
8
|
"license": "MIT",
|
|
12
9
|
"engines": {
|
|
13
10
|
"node": ">=18"
|
|
@@ -20,14 +17,18 @@
|
|
|
20
17
|
"publishConfig": {
|
|
21
18
|
"access": "public"
|
|
22
19
|
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"test": "jest"
|
|
23
|
+
},
|
|
23
24
|
"peerDependencies": {
|
|
24
|
-
"@google/
|
|
25
|
+
"@google/genai": ">=1.0.0"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@axemere/gateway": "
|
|
28
|
+
"@axemere/gateway": "workspace:*"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
|
-
"@google/
|
|
31
|
+
"@google/genai": "^3.7.0",
|
|
31
32
|
"typescript": "^5.4.0",
|
|
32
33
|
"@types/node": "^20.0.0",
|
|
33
34
|
"@types/jest": "^29.0.0",
|
|
@@ -38,16 +39,7 @@
|
|
|
38
39
|
"preset": "ts-jest",
|
|
39
40
|
"testEnvironment": "node",
|
|
40
41
|
"transform": {
|
|
41
|
-
"^.+\\.tsx?$": [
|
|
42
|
-
"ts-jest",
|
|
43
|
-
{
|
|
44
|
-
"useESM": false
|
|
45
|
-
}
|
|
46
|
-
]
|
|
42
|
+
"^.+\\.tsx?$": ["ts-jest", { "useESM": false }]
|
|
47
43
|
}
|
|
48
|
-
},
|
|
49
|
-
"scripts": {
|
|
50
|
-
"build": "tsc",
|
|
51
|
-
"test": "jest"
|
|
52
44
|
}
|
|
53
|
-
}
|
|
45
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Axemere LLC
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|