@axemere/gateway-google 0.2.1 → 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 CHANGED
@@ -1,11 +1,11 @@
1
1
  # @axemere/gateway-google
2
2
 
3
- Google Generative AI drop-in wrapper for the [Axemere AI Gateway](https://axemere.ai).
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/generative-ai
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 model = ai.getGenerativeModel({ model: "gemini-1.5-flash" });
19
- const result = await model.generateContent("Explain quantum entanglement in one sentence.");
20
- console.log(result.response.text());
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 `GoogleGenerativeAI` instance — no other code changes needed.
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 { GoogleGenerativeAI } from "@google/generative-ai";
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
- * GoogleGenerativeAI subclass that automatically routes all model requests
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 GoogleGenerativeAI instance — use it exactly as you would
20
- * the official @google/generative-ai package. All requests are proxied through the gateway,
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 GoogleGenerativeAI instance with gateway routing applied to every model request.
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 model = ai.getGenerativeModel({ model: "gemini-1.5-flash" });
30
- * const result = await model.generateContent("Hello");
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): GatewayGenerativeAI;
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 generative_ai_1 = require("@google/generative-ai");
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
- * GoogleGenerativeAI subclass that automatically routes all model requests
10
- * through the Axemere AI Gateway. Gateway auth headers and base URL are injected
11
- * into every getGenerativeModel() call without requiring the caller to pass
12
- * requestOptions manually.
13
- */
14
- class GatewayGenerativeAI extends generative_ai_1.GoogleGenerativeAI {
15
- constructor(apiKey, gatewayRequestOptions) {
16
- super(apiKey);
17
- this._gatewayRequestOptions = gatewayRequestOptions;
18
- }
19
- getGenerativeModel(modelParams, requestOptions) {
20
- // Merge gateway defaults under caller-supplied options so callers can
21
- // still override baseUrl or add their own custom headers.
22
- const merged = {
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
- merged.customHeaders = gwHeaders;
36
- return super.getGenerativeModel(modelParams, merged);
37
- }
24
+ return fetch(input, init);
25
+ };
38
26
  }
39
27
  /**
40
- * Creates a GoogleGenerativeAI client pre-configured to route through the Axemere AI Gateway.
28
+ * Creates a GoogleGenAI client pre-configured to route through the Axemere AI Gateway.
41
29
  *
42
- * The returned client is a standard GoogleGenerativeAI instance — use it exactly as you would
43
- * the official @google/generative-ai package. All requests are proxied through the gateway,
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 GoogleGenerativeAI instance with gateway routing applied to every model request.
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 model = ai.getGenerativeModel({ model: "gemini-1.5-flash" });
53
- * const result = await model.generateContent("Hello");
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
- const gatewayRequestOptions = {
60
- baseUrl: cfg.gateway_url,
61
- customHeaders: new Headers(headers),
62
- };
63
- return new GatewayGenerativeAI(gateway_1.PLACEHOLDER_API_KEY, gatewayRequestOptions);
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
  }
@@ -1,54 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const generative_ai_1 = require("@google/generative-ai");
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 GoogleGenerativeAI instance", () => {
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(generative_ai_1.GoogleGenerativeAI);
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
- // Retrieve the options injected into getGenerativeModel by calling it and
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
- const model = client.getGenerativeModel({ model: "gemini-1.5-flash" });
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
- it("caller requestOptions override gateway defaults", () => {
47
- const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
48
- const client = (0, index_1.genaiClient)(cfg);
49
- const model = client.getGenerativeModel({ model: "gemini-1.5-flash" }, { baseUrl: "http://other-gateway:9090" });
50
- const opts = model
51
- ._requestOptions;
52
- expect(opts.baseUrl).toBe("http://other-gateway:9090");
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.1",
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/generative-ai": ">=0.21.0"
25
+ "@google/genai": ">=1.0.0"
25
26
  },
26
27
  "dependencies": {
27
- "@axemere/gateway": "0.2.1"
28
+ "@axemere/gateway": "workspace:*"
28
29
  },
29
30
  "devDependencies": {
30
- "@google/generative-ai": "^0.24.0",
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.