@axemere/gateway-openai 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/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import OpenAI from "openai";
1
+ import OpenAI, { AzureOpenAI } from "openai";
2
+ import type { AzureClientOptions } from "openai";
2
3
  import { AiGatewayConfig } from "@axemere/gateway";
3
4
  export { AiGatewayConfig };
5
+ export { AzureOpenAI };
4
6
  /**
5
7
  * Creates an OpenAI client pre-configured to route through the Axemere AI Gateway.
6
8
  *
@@ -12,3 +14,17 @@ export { AiGatewayConfig };
12
14
  * @param options - Additional OpenAI constructor options (merged after gateway defaults).
13
15
  */
14
16
  export declare function openaiClient(config?: AiGatewayConfig, options?: ConstructorParameters<typeof OpenAI>[0]): OpenAI;
17
+ /**
18
+ * Creates an AzureOpenAI client pre-configured to route through the Axemere AI Gateway.
19
+ *
20
+ * The returned client is a standard AzureOpenAI instance — use it exactly as you would
21
+ * the official openai package's AzureOpenAI. Requests are sent to the gateway's own
22
+ * URL (not a /proxy/azure_openai/ prefix — see above), which forwards them to the
23
+ * endpoint named by X-MVGC-Target-Host.
24
+ *
25
+ * @param config - AiGatewayConfig instance (reads env vars if omitted).
26
+ * @param azureTargetHost - Azure resource hostname (e.g. "myresource.openai.azure.com").
27
+ * Falls back to AXEMERE_AZURE_ENDPOINT if omitted.
28
+ * @param options - Additional AzureOpenAI constructor options (merged after gateway defaults).
29
+ */
30
+ export declare function azureOpenAIClient(config?: AiGatewayConfig, azureTargetHost?: string, options?: AzureClientOptions): AzureOpenAI;
package/dist/index.js CHANGED
@@ -1,11 +1,43 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
5
35
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.AiGatewayConfig = void 0;
36
+ exports.AzureOpenAI = exports.AiGatewayConfig = void 0;
7
37
  exports.openaiClient = openaiClient;
8
- const openai_1 = __importDefault(require("openai"));
38
+ exports.azureOpenAIClient = azureOpenAIClient;
39
+ const openai_1 = __importStar(require("openai"));
40
+ Object.defineProperty(exports, "AzureOpenAI", { enumerable: true, get: function () { return openai_1.AzureOpenAI; } });
9
41
  const gateway_1 = require("@axemere/gateway");
10
42
  Object.defineProperty(exports, "AiGatewayConfig", { enumerable: true, get: function () { return gateway_1.AiGatewayConfig; } });
11
43
  /**
@@ -26,3 +58,39 @@ function openaiClient(config, options) {
26
58
  ...options,
27
59
  });
28
60
  }
61
+ // Every Azure OpenAI customer gets a unique subdomain, so there is no single
62
+ // static host to put in the provider registry — unlike openai/anthropic/gemini,
63
+ // /proxy/azure_openai/ is NOT a supported routing prefix (the gateway returns a
64
+ // 400 directing callers here instead). The gateway needs the real hostname via
65
+ // X-MVGC-Target-Host, set below from AXEMERE_AZURE_ENDPOINT or azureTargetHost.
66
+ function extractAzureHostname(endpoint) {
67
+ if (!endpoint)
68
+ return "";
69
+ const withoutScheme = endpoint.includes("://") ? endpoint.split("://")[1] : endpoint;
70
+ return withoutScheme.split("/")[0];
71
+ }
72
+ /**
73
+ * Creates an AzureOpenAI client pre-configured to route through the Axemere AI Gateway.
74
+ *
75
+ * The returned client is a standard AzureOpenAI instance — use it exactly as you would
76
+ * the official openai package's AzureOpenAI. Requests are sent to the gateway's own
77
+ * URL (not a /proxy/azure_openai/ prefix — see above), which forwards them to the
78
+ * endpoint named by X-MVGC-Target-Host.
79
+ *
80
+ * @param config - AiGatewayConfig instance (reads env vars if omitted).
81
+ * @param azureTargetHost - Azure resource hostname (e.g. "myresource.openai.azure.com").
82
+ * Falls back to AXEMERE_AZURE_ENDPOINT if omitted.
83
+ * @param options - Additional AzureOpenAI constructor options (merged after gateway defaults).
84
+ */
85
+ function azureOpenAIClient(config, azureTargetHost, options) {
86
+ const cfg = config ?? new gateway_1.AiGatewayConfig();
87
+ const targetHost = extractAzureHostname(azureTargetHost || process.env["AXEMERE_AZURE_ENDPOINT"] || "");
88
+ const headers = (0, gateway_1.aiGatewayHeaders)(cfg, { targetHost: targetHost || "openai.azure.com" });
89
+ return new openai_1.AzureOpenAI({
90
+ endpoint: cfg.gateway_url,
91
+ apiKey: gateway_1.PLACEHOLDER_API_KEY,
92
+ apiVersion: "2024-06-01",
93
+ defaultHeaders: headers,
94
+ ...options,
95
+ });
96
+ }
@@ -1,9 +1,39 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
5
35
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const openai_1 = __importDefault(require("openai"));
36
+ const openai_1 = __importStar(require("openai"));
7
37
  const gateway_1 = require("@axemere/gateway");
8
38
  const index_1 = require("./index");
9
39
  describe("openaiClient()", () => {
@@ -48,3 +78,51 @@ describe("openaiClient()", () => {
48
78
  expect(client.maxRetries).toBe(0);
49
79
  });
50
80
  });
81
+ describe("azureOpenAIClient()", () => {
82
+ // Pins the fix for a real bug: Azure OpenAI has no single static host (every
83
+ // customer gets a unique subdomain), so /proxy/azure_openai/ is NOT a
84
+ // supported routing prefix — the gateway needs the real hostname via
85
+ // X-MVGC-Target-Host instead. Before this factory existed, the only Node
86
+ // example touching Azure built its own URL with cfg.proxyUrl("azure_openai"),
87
+ // which always 404'd.
88
+ it("returns an AzureOpenAI instance", () => {
89
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
90
+ const client = (0, index_1.azureOpenAIClient)(cfg, "myresource.openai.azure.com");
91
+ expect(client).toBeInstanceOf(openai_1.AzureOpenAI);
92
+ });
93
+ it("sets baseURL to the bare gateway URL, not a /proxy/azure_openai/ prefix", () => {
94
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
95
+ const client = (0, index_1.azureOpenAIClient)(cfg, "myresource.openai.azure.com");
96
+ expect(client.baseURL).toBe("http://localhost:7080/openai");
97
+ });
98
+ it("sets X-MVGC-Target-Host from the azureTargetHost argument", () => {
99
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
100
+ const client = (0, index_1.azureOpenAIClient)(cfg, "myresource.openai.azure.com");
101
+ const opts = client._options;
102
+ expect(opts.defaultHeaders["X-MVGC-Target-Host"]).toBe("myresource.openai.azure.com");
103
+ });
104
+ it("falls back to AXEMERE_AZURE_ENDPOINT when azureTargetHost is omitted", () => {
105
+ process.env["AXEMERE_AZURE_ENDPOINT"] = "env-resource.openai.azure.com";
106
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
107
+ const client = (0, index_1.azureOpenAIClient)(cfg);
108
+ const opts = client._options;
109
+ expect(opts.defaultHeaders["X-MVGC-Target-Host"]).toBe("env-resource.openai.azure.com");
110
+ delete process.env["AXEMERE_AZURE_ENDPOINT"];
111
+ });
112
+ it("strips scheme and path when given a full URL instead of a bare hostname", () => {
113
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
114
+ const client = (0, index_1.azureOpenAIClient)(cfg, "https://myresource.openai.azure.com/some/path");
115
+ const opts = client._options;
116
+ expect(opts.defaultHeaders["X-MVGC-Target-Host"]).toBe("myresource.openai.azure.com");
117
+ });
118
+ it("uses the placeholder apiKey — the real credential is held by the gateway", () => {
119
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
120
+ const client = (0, index_1.azureOpenAIClient)(cfg, "myresource.openai.azure.com");
121
+ expect(client.apiKey).toBe("axemere-proxy-managed");
122
+ });
123
+ it("merges extra options after gateway defaults", () => {
124
+ const cfg = new gateway_1.AiGatewayConfig({ gateway_url: "http://localhost:7080" });
125
+ const client = (0, index_1.azureOpenAIClient)(cfg, "myresource.openai.azure.com", { apiVersion: "2024-10-01" });
126
+ expect(client.apiVersion).toBe("2024-10-01");
127
+ });
128
+ });
package/package.json CHANGED
@@ -1,13 +1,10 @@
1
1
  {
2
2
  "name": "@axemere/gateway-openai",
3
- "version": "0.2.0",
3
+ "version": "1.0.2",
4
4
  "description": "Axemere AI Gateway SDK — OpenAI drop-in 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,11 +17,18 @@
20
17
  "publishConfig": {
21
18
  "access": "public"
22
19
  },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "test": "jest"
23
+ },
24
+ "peerDependencies": {
25
+ "openai": ">=4.0.0"
26
+ },
23
27
  "dependencies": {
24
- "openai": "^4.0.0",
25
- "@axemere/gateway": "0.2.0"
28
+ "@axemere/gateway": "workspace:*"
26
29
  },
27
30
  "devDependencies": {
31
+ "openai": "^4.104.0",
28
32
  "typescript": "^5.4.0",
29
33
  "@types/node": "^20.0.0",
30
34
  "@types/jest": "^29.0.0",
@@ -35,16 +39,7 @@
35
39
  "preset": "ts-jest",
36
40
  "testEnvironment": "node",
37
41
  "transform": {
38
- "^.+\\.tsx?$": [
39
- "ts-jest",
40
- {
41
- "useESM": false
42
- }
43
- ]
42
+ "^.+\\.tsx?$": ["ts-jest", { "useESM": false }]
44
43
  }
45
- },
46
- "scripts": {
47
- "build": "tsc",
48
- "test": "jest"
49
44
  }
50
- }
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.