@khanglvm/llm-router 2.0.1 → 2.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanglvm/llm-router",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "LLM Router: single gateway endpoint for multi-provider LLMs with unified OpenAI+Anthropic format and seamless fallback",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm-router",
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
"deploy:worker": "node ./src/cli-entry.js deploy",
|
|
32
32
|
"test:provider-live": "node --test --test-concurrency=1 ./test/live-provider-suite.test.js",
|
|
33
33
|
"test:provider-smoke": "npm run test:provider-live",
|
|
34
|
-
"test:amp-smoke": "node ./scripts/amp-smoke-suite.mjs"
|
|
34
|
+
"test:amp-smoke": "node ./scripts/amp-smoke-suite.mjs",
|
|
35
|
+
"prepublishOnly": "npm run test:provider-live"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
38
|
"@levu/snap": "^0.3.13"
|
|
@@ -11,6 +11,20 @@ import { FIXED_LOCAL_ROUTER_HOST, FIXED_LOCAL_ROUTER_PORT } from "./local-server
|
|
|
11
11
|
|
|
12
12
|
const SERVICE_NAME = "llm-router";
|
|
13
13
|
const LAUNCH_AGENT_ID = "dev.llm-router";
|
|
14
|
+
const STARTUP_ENV_PASSTHROUGH_KEYS = [
|
|
15
|
+
"NODE_EXTRA_CA_CERTS",
|
|
16
|
+
"SSL_CERT_FILE",
|
|
17
|
+
"SSL_CERT_DIR",
|
|
18
|
+
"HTTP_PROXY",
|
|
19
|
+
"HTTPS_PROXY",
|
|
20
|
+
"ALL_PROXY",
|
|
21
|
+
"NO_PROXY",
|
|
22
|
+
"http_proxy",
|
|
23
|
+
"https_proxy",
|
|
24
|
+
"all_proxy",
|
|
25
|
+
"no_proxy",
|
|
26
|
+
"npm_config_cafile"
|
|
27
|
+
];
|
|
14
28
|
|
|
15
29
|
function resolveDarwinDomain() {
|
|
16
30
|
const uid = process.getuid?.();
|
|
@@ -83,13 +97,48 @@ function isMissingServiceMessage(value) {
|
|
|
83
97
|
|| text.includes("unit llm-router.service could not be found");
|
|
84
98
|
}
|
|
85
99
|
|
|
100
|
+
function escapeXml(value) {
|
|
101
|
+
return String(value)
|
|
102
|
+
.replaceAll("&", "&")
|
|
103
|
+
.replaceAll("<", "<")
|
|
104
|
+
.replaceAll(">", ">")
|
|
105
|
+
.replaceAll('"', """)
|
|
106
|
+
.replaceAll("'", "'");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function buildStartupEnvironment(env = process.env) {
|
|
110
|
+
const configuredCliPath = String(env?.LLM_ROUTER_CLI_PATH || "").trim();
|
|
111
|
+
const startupEnv = {
|
|
112
|
+
LLM_ROUTER_MANAGED_BY_STARTUP: "1",
|
|
113
|
+
LLM_ROUTER_CLI_PATH: configuredCliPath || String(resolveStartupCliEntryPath({ env }) || "").trim()
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
for (const key of STARTUP_ENV_PASSTHROUGH_KEYS) {
|
|
117
|
+
const value = String(env?.[key] || "").trim();
|
|
118
|
+
if (!value) continue;
|
|
119
|
+
startupEnv[key] = value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return startupEnv;
|
|
123
|
+
}
|
|
124
|
+
|
|
86
125
|
function buildLaunchAgentPlist({ nodePath, cliPath, configPath, host, port, watchConfig, watchBinary, requireAuth }) {
|
|
87
126
|
const logDir = path.join(os.homedir(), "Library", "Logs");
|
|
88
127
|
const stdoutPath = path.join(logDir, "llm-router.out.log");
|
|
89
128
|
const stderrPath = path.join(logDir, "llm-router.err.log");
|
|
90
129
|
const args = [nodePath, cliPath, ...makeExecArgs({ configPath, host, port, watchConfig, watchBinary, requireAuth })];
|
|
130
|
+
const environment = {
|
|
131
|
+
...buildStartupEnvironment({
|
|
132
|
+
...process.env,
|
|
133
|
+
LLM_ROUTER_CLI_PATH: cliPath
|
|
134
|
+
}),
|
|
135
|
+
LLM_ROUTER_CLI_PATH: cliPath
|
|
136
|
+
};
|
|
91
137
|
|
|
92
|
-
const xmlArgs = args.map((arg) => ` <string>${arg}</string>`).join("\n");
|
|
138
|
+
const xmlArgs = args.map((arg) => ` <string>${escapeXml(arg)}</string>`).join("\n");
|
|
139
|
+
const xmlEnvironment = Object.entries(environment)
|
|
140
|
+
.map(([key, value]) => ` <key>${escapeXml(key)}</key>\n <string>${escapeXml(value)}</string>`)
|
|
141
|
+
.join("\n");
|
|
93
142
|
|
|
94
143
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
95
144
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
@@ -107,17 +156,14 @@ ${xmlArgs}
|
|
|
107
156
|
<true/>
|
|
108
157
|
<key>EnvironmentVariables</key>
|
|
109
158
|
<dict>
|
|
110
|
-
|
|
111
|
-
<string>1</string>
|
|
112
|
-
<key>LLM_ROUTER_CLI_PATH</key>
|
|
113
|
-
<string>${cliPath}</string>
|
|
159
|
+
${xmlEnvironment}
|
|
114
160
|
</dict>
|
|
115
161
|
<key>StandardOutPath</key>
|
|
116
|
-
<string>${stdoutPath}</string>
|
|
162
|
+
<string>${escapeXml(stdoutPath)}</string>
|
|
117
163
|
<key>StandardErrorPath</key>
|
|
118
|
-
<string>${stderrPath}</string>
|
|
164
|
+
<string>${escapeXml(stderrPath)}</string>
|
|
119
165
|
<key>WorkingDirectory</key>
|
|
120
|
-
<string>${process.cwd()}</string>
|
|
166
|
+
<string>${escapeXml(process.cwd())}</string>
|
|
121
167
|
</dict>
|
|
122
168
|
</plist>
|
|
123
169
|
`;
|
|
@@ -126,6 +172,17 @@ ${xmlArgs}
|
|
|
126
172
|
function buildSystemdService({ nodePath, cliPath, configPath, host, port, watchConfig, watchBinary, requireAuth }) {
|
|
127
173
|
const execArgs = makeExecArgs({ configPath, host, port, watchConfig, watchBinary, requireAuth }).map(quoteArg).join(" ");
|
|
128
174
|
const execStart = `${quoteArg(nodePath)} ${quoteArg(cliPath)} ${execArgs}`;
|
|
175
|
+
const environment = {
|
|
176
|
+
...buildStartupEnvironment({
|
|
177
|
+
...process.env,
|
|
178
|
+
LLM_ROUTER_CLI_PATH: cliPath
|
|
179
|
+
}),
|
|
180
|
+
LLM_ROUTER_CLI_PATH: cliPath,
|
|
181
|
+
NODE_ENV: "production"
|
|
182
|
+
};
|
|
183
|
+
const systemdEnvironment = Object.entries(environment)
|
|
184
|
+
.map(([key, value]) => `Environment=${key}=${value}`)
|
|
185
|
+
.join("\n");
|
|
129
186
|
|
|
130
187
|
return `[Unit]
|
|
131
188
|
Description=LLM Router local route
|
|
@@ -136,9 +193,7 @@ Type=simple
|
|
|
136
193
|
ExecStart=${execStart}
|
|
137
194
|
Restart=always
|
|
138
195
|
RestartSec=2
|
|
139
|
-
|
|
140
|
-
Environment=LLM_ROUTER_MANAGED_BY_STARTUP=1
|
|
141
|
-
Environment=LLM_ROUTER_CLI_PATH=${cliPath}
|
|
196
|
+
${systemdEnvironment}
|
|
142
197
|
WorkingDirectory=${process.cwd()}
|
|
143
198
|
|
|
144
199
|
[Install]
|
|
@@ -80,7 +80,7 @@ const READ_WEB_PAGE_FUNCTION_PARAMETERS = {
|
|
|
80
80
|
additionalProperties: true
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
-
const
|
|
83
|
+
const OPENAI_CHAT_WEB_SEARCH_TOOL = Object.freeze({
|
|
84
84
|
type: "function",
|
|
85
85
|
function: {
|
|
86
86
|
name: SEARCH_TOOL_NAME,
|
|
@@ -89,13 +89,20 @@ const OPENAI_WEB_SEARCH_TOOL = Object.freeze({
|
|
|
89
89
|
}
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
+
const OPENAI_RESPONSES_WEB_SEARCH_TOOL = Object.freeze({
|
|
93
|
+
type: "function",
|
|
94
|
+
name: SEARCH_TOOL_NAME,
|
|
95
|
+
description: "Search the web for current information, news, documentation, or real-time facts.",
|
|
96
|
+
parameters: WEB_SEARCH_FUNCTION_PARAMETERS
|
|
97
|
+
});
|
|
98
|
+
|
|
92
99
|
const CLAUDE_WEB_SEARCH_TOOL = Object.freeze({
|
|
93
100
|
name: SEARCH_TOOL_NAME,
|
|
94
101
|
description: "Search the web for current information, news, documentation, or real-time facts.",
|
|
95
102
|
input_schema: WEB_SEARCH_FUNCTION_PARAMETERS
|
|
96
103
|
});
|
|
97
104
|
|
|
98
|
-
const
|
|
105
|
+
const OPENAI_CHAT_READ_WEB_PAGE_TOOL = Object.freeze({
|
|
99
106
|
type: "function",
|
|
100
107
|
function: {
|
|
101
108
|
name: READ_WEB_PAGE_TOOL_NAME,
|
|
@@ -104,6 +111,13 @@ const OPENAI_READ_WEB_PAGE_TOOL = Object.freeze({
|
|
|
104
111
|
}
|
|
105
112
|
});
|
|
106
113
|
|
|
114
|
+
const OPENAI_RESPONSES_READ_WEB_PAGE_TOOL = Object.freeze({
|
|
115
|
+
type: "function",
|
|
116
|
+
name: READ_WEB_PAGE_TOOL_NAME,
|
|
117
|
+
description: "Fetch and extract the readable text and table content from a web page URL.",
|
|
118
|
+
parameters: READ_WEB_PAGE_FUNCTION_PARAMETERS
|
|
119
|
+
});
|
|
120
|
+
|
|
107
121
|
const CLAUDE_READ_WEB_PAGE_TOOL = Object.freeze({
|
|
108
122
|
name: READ_WEB_PAGE_TOOL_NAME,
|
|
109
123
|
description: "Fetch and extract the readable text and table content from a web page URL.",
|
|
@@ -1269,7 +1283,20 @@ export function shouldInterceptAmpWebSearch({ clientType, originalBody, runtimeC
|
|
|
1269
1283
|
return true;
|
|
1270
1284
|
}
|
|
1271
1285
|
|
|
1272
|
-
|
|
1286
|
+
function getOpenAIInterceptToolDefinitions(requestKind) {
|
|
1287
|
+
if (requestKind === "responses") {
|
|
1288
|
+
return {
|
|
1289
|
+
webSearch: OPENAI_RESPONSES_WEB_SEARCH_TOOL,
|
|
1290
|
+
readWebPage: OPENAI_RESPONSES_READ_WEB_PAGE_TOOL
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
return {
|
|
1294
|
+
webSearch: OPENAI_CHAT_WEB_SEARCH_TOOL,
|
|
1295
|
+
readWebPage: OPENAI_CHAT_READ_WEB_PAGE_TOOL
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
export function rewriteProviderBodyForAmpWebSearch(providerBody, targetFormat, requestKind = undefined) {
|
|
1273
1300
|
const tools = Array.isArray(providerBody?.tools) ? providerBody.tools : [];
|
|
1274
1301
|
if (tools.length === 0) {
|
|
1275
1302
|
return {
|
|
@@ -1301,8 +1328,9 @@ export function rewriteProviderBodyForAmpWebSearch(providerBody, targetFormat) {
|
|
|
1301
1328
|
}
|
|
1302
1329
|
|
|
1303
1330
|
if (targetFormat === FORMATS.OPENAI) {
|
|
1304
|
-
|
|
1305
|
-
if (interceptedToolNames.has(
|
|
1331
|
+
const toolDefinitions = getOpenAIInterceptToolDefinitions(requestKind);
|
|
1332
|
+
if (interceptedToolNames.has(SEARCH_TOOL_NAME)) nextTools.push(toolDefinitions.webSearch);
|
|
1333
|
+
if (interceptedToolNames.has(READ_WEB_PAGE_TOOL_NAME)) nextTools.push(toolDefinitions.readWebPage);
|
|
1306
1334
|
} else if (targetFormat === FORMATS.CLAUDE) {
|
|
1307
1335
|
if (interceptedToolNames.has(SEARCH_TOOL_NAME)) nextTools.push(CLAUDE_WEB_SEARCH_TOOL);
|
|
1308
1336
|
if (interceptedToolNames.has(READ_WEB_PAGE_TOOL_NAME)) nextTools.push(CLAUDE_READ_WEB_PAGE_TOOL);
|
|
@@ -516,7 +516,7 @@ export async function makeProviderCall({
|
|
|
516
516
|
providerBody = declaredOpenAIHostedWebSearchRewrite.providerBody;
|
|
517
517
|
}
|
|
518
518
|
if (interceptAmpWebSearch) {
|
|
519
|
-
providerBody = rewriteProviderBodyForAmpWebSearch(providerBody, targetFormat).providerBody;
|
|
519
|
+
providerBody = rewriteProviderBodyForAmpWebSearch(providerBody, targetFormat, requestKind).providerBody;
|
|
520
520
|
}
|
|
521
521
|
logToolRouting({
|
|
522
522
|
env,
|