@axemere/gateway-langchain 0.2.1 → 1.0.3
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 +15 -2
- package/dist/index.js +45 -7
- package/dist/tool-calling.test.d.ts +1 -0
- package/dist/tool-calling.test.js +109 -0
- package/package.json +11 -19
- package/LICENSE +0 -21
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { BaseChatModel, BaseChatModelParams } from "@langchain/core/language_models/chat_models";
|
|
2
2
|
import type { BaseMessage } from "@langchain/core/messages";
|
|
3
|
+
import { AIMessageChunk } from "@langchain/core/messages";
|
|
3
4
|
import type { ChatResult } from "@langchain/core/outputs";
|
|
4
5
|
import { ChatGenerationChunk } from "@langchain/core/outputs";
|
|
5
6
|
import type { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
7
|
+
import type { BindToolsInput } from "@langchain/core/language_models/chat_models";
|
|
8
|
+
import type { BaseLanguageModelInput } from "@langchain/core/language_models/base";
|
|
9
|
+
import type { Runnable } from "@langchain/core/runnables";
|
|
6
10
|
import { AiGatewayConfig, GatewayError, PolicyDeniedError } from "@axemere/gateway";
|
|
7
11
|
import { openaiClient } from "@axemere/gateway-openai";
|
|
8
12
|
import { anthropicClient } from "@axemere/gateway-anthropic";
|
|
@@ -47,10 +51,19 @@ export declare class ChatAiGateway extends BaseChatModel {
|
|
|
47
51
|
private readonly _config;
|
|
48
52
|
constructor(fields: ChatAiGatewayFields);
|
|
49
53
|
_llmType(): string;
|
|
54
|
+
/**
|
|
55
|
+
* Return a copy of this model with the given tools bound.
|
|
56
|
+
*
|
|
57
|
+
* Always produces OpenAI-shaped tool dicts — _buildActionRequest
|
|
58
|
+
* translates into whatever shape this.provider actually needs
|
|
59
|
+
* (Anthropic, Gemini, or passthrough for OpenAI-compatible providers),
|
|
60
|
+
* the same single translation path AiGatewayClient uses.
|
|
61
|
+
*/
|
|
62
|
+
bindTools(tools: BindToolsInput[], kwargs?: Record<string, unknown>): Runnable<BaseLanguageModelInput, AIMessageChunk, this["ParsedCallOptions"]>;
|
|
50
63
|
private _getConfig;
|
|
51
64
|
private _effectiveLabels;
|
|
52
65
|
private _buildActionRequest;
|
|
53
66
|
private _requestHeaders;
|
|
54
|
-
_generate(messages: BaseMessage[],
|
|
55
|
-
_stream(messages: BaseMessage[],
|
|
67
|
+
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
|
|
68
|
+
_stream(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
56
69
|
}
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const chat_models_1 = require("@langchain/core/language_models/chat_models");
|
|
|
5
5
|
const messages_1 = require("@langchain/core/messages");
|
|
6
6
|
const messages_2 = require("@langchain/core/messages");
|
|
7
7
|
const outputs_1 = require("@langchain/core/outputs");
|
|
8
|
+
const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
8
9
|
const gateway_1 = require("@axemere/gateway");
|
|
9
10
|
Object.defineProperty(exports, "AiGatewayConfig", { enumerable: true, get: function () { return gateway_1.AiGatewayConfig; } });
|
|
10
11
|
Object.defineProperty(exports, "GatewayError", { enumerable: true, get: function () { return gateway_1.GatewayError; } });
|
|
@@ -181,7 +182,17 @@ function parseGeminiBody(body) {
|
|
|
181
182
|
return new messages_1.AIMessage({ content: "" });
|
|
182
183
|
const parts = candidates[0].content?.parts ?? [];
|
|
183
184
|
const text = parts.map((p) => p.text ?? "").join("");
|
|
184
|
-
|
|
185
|
+
const toolCalls = parts
|
|
186
|
+
.filter((p) => p.functionCall)
|
|
187
|
+
.map((p) => ({
|
|
188
|
+
id: p.functionCall?.id ?? `call_${crypto.randomUUID()}`,
|
|
189
|
+
name: p.functionCall?.name ?? "",
|
|
190
|
+
args: p.functionCall?.args ?? {},
|
|
191
|
+
type: "tool_call",
|
|
192
|
+
}));
|
|
193
|
+
return toolCalls.length > 0
|
|
194
|
+
? new messages_1.AIMessage({ content: text, tool_calls: toolCalls })
|
|
195
|
+
: new messages_1.AIMessage({ content: text });
|
|
185
196
|
}
|
|
186
197
|
// ---------------------------------------------------------------------------
|
|
187
198
|
// SSE stream text extraction
|
|
@@ -234,6 +245,18 @@ class ChatAiGateway extends chat_models_1.BaseChatModel {
|
|
|
234
245
|
_llmType() {
|
|
235
246
|
return "axemere-gateway";
|
|
236
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* Return a copy of this model with the given tools bound.
|
|
250
|
+
*
|
|
251
|
+
* Always produces OpenAI-shaped tool dicts — _buildActionRequest
|
|
252
|
+
* translates into whatever shape this.provider actually needs
|
|
253
|
+
* (Anthropic, Gemini, or passthrough for OpenAI-compatible providers),
|
|
254
|
+
* the same single translation path AiGatewayClient uses.
|
|
255
|
+
*/
|
|
256
|
+
bindTools(tools, kwargs) {
|
|
257
|
+
const formattedTools = tools.map((t) => (0, function_calling_1.convertToOpenAITool)(t));
|
|
258
|
+
return this.bind({ tools: formattedTools, ...kwargs });
|
|
259
|
+
}
|
|
237
260
|
_getConfig() {
|
|
238
261
|
return this._config ?? new gateway_1.AiGatewayConfig();
|
|
239
262
|
}
|
|
@@ -271,8 +294,14 @@ class ChatAiGateway extends chat_models_1.BaseChatModel {
|
|
|
271
294
|
params["system"] = system;
|
|
272
295
|
if (this.temperature !== undefined)
|
|
273
296
|
params["temperature"] = this.temperature;
|
|
274
|
-
if (tools?.length)
|
|
275
|
-
|
|
297
|
+
if (tools?.length) {
|
|
298
|
+
// bindTools() always produces OpenAI-shaped dicts; translate
|
|
299
|
+
// into Anthropic's native custom-tool shape the same way
|
|
300
|
+
// AiGatewayClient does, so this wrapper and the raw client
|
|
301
|
+
// behave identically. Idempotent — already-native tool
|
|
302
|
+
// blocks pass through unchanged.
|
|
303
|
+
params["tools"] = (0, gateway_1.translateAnthropicTools)(tools);
|
|
304
|
+
}
|
|
276
305
|
}
|
|
277
306
|
else {
|
|
278
307
|
// gemini
|
|
@@ -280,6 +309,13 @@ class ChatAiGateway extends chat_models_1.BaseChatModel {
|
|
|
280
309
|
params = { contents, generationConfig };
|
|
281
310
|
if (systemInstruction)
|
|
282
311
|
params["systemInstruction"] = systemInstruction;
|
|
312
|
+
if (tools?.length) {
|
|
313
|
+
// Previously missing entirely: tools was never added to
|
|
314
|
+
// params for Gemini, so a tool bound via bindTools() never
|
|
315
|
+
// reached the model. See the anthropic branch above for why
|
|
316
|
+
// translation happens here rather than in bindTools() itself.
|
|
317
|
+
params["tools"] = (0, gateway_1.translateGeminiTools)(tools);
|
|
318
|
+
}
|
|
283
319
|
}
|
|
284
320
|
if (stream)
|
|
285
321
|
params["stream"] = true;
|
|
@@ -312,9 +348,10 @@ class ChatAiGateway extends chat_models_1.BaseChatModel {
|
|
|
312
348
|
headers["Authorization"] = `Bearer ${cfg.gateway_token}`;
|
|
313
349
|
return headers;
|
|
314
350
|
}
|
|
315
|
-
async _generate(messages,
|
|
351
|
+
async _generate(messages, options, _runManager) {
|
|
316
352
|
const cfg = this._getConfig();
|
|
317
|
-
const
|
|
353
|
+
const boundTools = options["tools"];
|
|
354
|
+
const actionRequest = this._buildActionRequest(messages, cfg, false, boundTools);
|
|
318
355
|
let response;
|
|
319
356
|
try {
|
|
320
357
|
response = await fetch(`${cfg.gateway_url}/v1/actions:execute`, {
|
|
@@ -384,10 +421,11 @@ class ChatAiGateway extends chat_models_1.BaseChatModel {
|
|
|
384
421
|
],
|
|
385
422
|
};
|
|
386
423
|
}
|
|
387
|
-
async *_stream(messages,
|
|
424
|
+
async *_stream(messages, options, runManager) {
|
|
388
425
|
const cfg = this._getConfig();
|
|
389
426
|
const route = gateway_1.PROVIDER_ROUTES[this.provider];
|
|
390
|
-
const
|
|
427
|
+
const boundTools = options["tools"];
|
|
428
|
+
const actionRequest = this._buildActionRequest(messages, cfg, true, boundTools);
|
|
391
429
|
let response;
|
|
392
430
|
try {
|
|
393
431
|
response = await fetch(`${cfg.gateway_url}/v1/actions:execute`, {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const index_1 = require("./index");
|
|
4
|
+
/**
|
|
5
|
+
* Pins a real bug: ChatAiGateway had no working tool-calling at all.
|
|
6
|
+
* bindTools() didn't exist, _buildActionRequest's `tools` parameter was
|
|
7
|
+
* never populated at any real call site, and the Gemini response parser
|
|
8
|
+
* never looked for functionCall parts — so even with tools somehow
|
|
9
|
+
* forwarded, a real Gemini tool call would still come back with an empty
|
|
10
|
+
* AIMessage.tool_calls list.
|
|
11
|
+
*/
|
|
12
|
+
const weatherTool = {
|
|
13
|
+
name: "get_weather",
|
|
14
|
+
description: "Get the current weather for a location",
|
|
15
|
+
schema: {
|
|
16
|
+
type: "object",
|
|
17
|
+
properties: { location: { type: "string" } },
|
|
18
|
+
required: ["location"],
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
function mockFetch(bodyFactory) {
|
|
22
|
+
let capturedParams = {};
|
|
23
|
+
jest.spyOn(globalThis, "fetch").mockImplementation((_, init) => {
|
|
24
|
+
const body = JSON.parse(init?.body);
|
|
25
|
+
capturedParams = body["action"]["params"];
|
|
26
|
+
return Promise.resolve(new Response(JSON.stringify(bodyFactory(capturedParams)), { status: 200 }));
|
|
27
|
+
});
|
|
28
|
+
return () => capturedParams;
|
|
29
|
+
}
|
|
30
|
+
afterEach(() => jest.restoreAllMocks());
|
|
31
|
+
describe("ChatAiGateway.bindTools — request shape per provider", () => {
|
|
32
|
+
it("forwards OpenAI-shaped tools untouched for openai", async () => {
|
|
33
|
+
const getParams = mockFetch(() => ({
|
|
34
|
+
decision: "allow",
|
|
35
|
+
record_id: "rec_1",
|
|
36
|
+
result: { status_code: 200, body: { choices: [{ message: { content: "" } }] } },
|
|
37
|
+
}));
|
|
38
|
+
const llm = new index_1.ChatAiGateway({ provider: "openai", model: "gpt-4o-mini" });
|
|
39
|
+
await llm.bindTools([weatherTool]).invoke("weather?");
|
|
40
|
+
const tools = getParams()["tools"];
|
|
41
|
+
expect(tools[0]["type"]).toBe("function");
|
|
42
|
+
expect(tools[0]["function"]["name"]).toBe("get_weather");
|
|
43
|
+
});
|
|
44
|
+
it("translates tools into Anthropic's native custom-tool shape", async () => {
|
|
45
|
+
const getParams = mockFetch(() => ({
|
|
46
|
+
decision: "allow",
|
|
47
|
+
record_id: "rec_2",
|
|
48
|
+
result: { status_code: 200, body: { content: [] } },
|
|
49
|
+
}));
|
|
50
|
+
const llm = new index_1.ChatAiGateway({ provider: "anthropic", model: "claude-haiku-4-5-20251001" });
|
|
51
|
+
await llm.bindTools([weatherTool]).invoke("weather?");
|
|
52
|
+
const tools = getParams()["tools"];
|
|
53
|
+
expect(tools).toEqual([{
|
|
54
|
+
name: "get_weather",
|
|
55
|
+
description: "Get the current weather for a location",
|
|
56
|
+
input_schema: weatherTool.schema,
|
|
57
|
+
}]);
|
|
58
|
+
});
|
|
59
|
+
it("translates tools into Gemini's functionDeclarations shape (previously dropped entirely)", async () => {
|
|
60
|
+
const getParams = mockFetch(() => ({
|
|
61
|
+
decision: "allow",
|
|
62
|
+
record_id: "rec_3",
|
|
63
|
+
result: { status_code: 200, body: { candidates: [{ content: { parts: [] } }] } },
|
|
64
|
+
}));
|
|
65
|
+
const llm = new index_1.ChatAiGateway({ provider: "gemini", model: "gemini-3.8-flash" });
|
|
66
|
+
await llm.bindTools([weatherTool]).invoke("weather?");
|
|
67
|
+
const tools = getParams()["tools"];
|
|
68
|
+
expect(tools).toHaveLength(1);
|
|
69
|
+
const declarations = tools[0]["functionDeclarations"];
|
|
70
|
+
expect(declarations[0]["name"]).toBe("get_weather");
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe("ChatAiGateway — Gemini response tool_calls parsing", () => {
|
|
74
|
+
it("extracts a functionCall part into AIMessage.tool_calls", async () => {
|
|
75
|
+
mockFetch(() => ({
|
|
76
|
+
decision: "allow",
|
|
77
|
+
record_id: "rec_4",
|
|
78
|
+
result: {
|
|
79
|
+
status_code: 200,
|
|
80
|
+
body: {
|
|
81
|
+
candidates: [{
|
|
82
|
+
content: {
|
|
83
|
+
parts: [{ functionCall: { name: "get_weather", args: { location: "Boston" } } }],
|
|
84
|
+
},
|
|
85
|
+
}],
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}));
|
|
89
|
+
const llm = new index_1.ChatAiGateway({ provider: "gemini", model: "gemini-3.8-flash" });
|
|
90
|
+
const res = await llm.bindTools([weatherTool]).invoke("weather?");
|
|
91
|
+
expect(res.tool_calls).toHaveLength(1);
|
|
92
|
+
expect(res.tool_calls?.[0].name).toBe("get_weather");
|
|
93
|
+
expect(res.tool_calls?.[0].args).toEqual({ location: "Boston" });
|
|
94
|
+
});
|
|
95
|
+
it("returns no tool_calls for a text-only response", async () => {
|
|
96
|
+
mockFetch(() => ({
|
|
97
|
+
decision: "allow",
|
|
98
|
+
record_id: "rec_5",
|
|
99
|
+
result: {
|
|
100
|
+
status_code: 200,
|
|
101
|
+
body: { candidates: [{ content: { parts: [{ text: "hello" }] } }] },
|
|
102
|
+
},
|
|
103
|
+
}));
|
|
104
|
+
const llm = new index_1.ChatAiGateway({ provider: "gemini", model: "gemini-3.8-flash" });
|
|
105
|
+
const res = await llm.invoke("hi");
|
|
106
|
+
expect(res.content).toBe("hello");
|
|
107
|
+
expect(res.tool_calls ?? []).toHaveLength(0);
|
|
108
|
+
});
|
|
109
|
+
});
|
package/package.json
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axemere/gateway-langchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Axemere AI Gateway SDK — LangChain BaseChatModel integration",
|
|
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,13 +17,17 @@
|
|
|
20
17
|
"publishConfig": {
|
|
21
18
|
"access": "public"
|
|
22
19
|
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"test": "jest"
|
|
23
|
+
},
|
|
23
24
|
"peerDependencies": {
|
|
24
25
|
"@langchain/core": ">=0.3.0"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@axemere/gateway": "
|
|
28
|
-
"@axemere/gateway-openai": "
|
|
29
|
-
"@axemere/gateway-anthropic": "
|
|
28
|
+
"@axemere/gateway": "workspace:*",
|
|
29
|
+
"@axemere/gateway-openai": "workspace:*",
|
|
30
|
+
"@axemere/gateway-anthropic": "workspace:*"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@langchain/core": "^0.3.0",
|
|
@@ -40,16 +41,7 @@
|
|
|
40
41
|
"preset": "ts-jest",
|
|
41
42
|
"testEnvironment": "node",
|
|
42
43
|
"transform": {
|
|
43
|
-
"^.+\\.tsx?$": [
|
|
44
|
-
"ts-jest",
|
|
45
|
-
{
|
|
46
|
-
"useESM": false
|
|
47
|
-
}
|
|
48
|
-
]
|
|
44
|
+
"^.+\\.tsx?$": ["ts-jest", { "useESM": false }]
|
|
49
45
|
}
|
|
50
|
-
},
|
|
51
|
-
"scripts": {
|
|
52
|
-
"build": "tsc",
|
|
53
|
-
"test": "jest"
|
|
54
46
|
}
|
|
55
|
-
}
|
|
47
|
+
}
|
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.
|