@nvae/llmswitch 1.0.0 → 1.2.0
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/bridge/server.js +7 -6
- package/dist/index.js +0 -0
- package/dist/utils/fetch-models.js +26 -12
- package/package.json +1 -1
package/dist/bridge/server.js
CHANGED
|
@@ -6,6 +6,7 @@ import { anthropicToChatRequest } from "./anthropic-translate-request.js";
|
|
|
6
6
|
import { chatChunkToAnthropicEvents, chatCompletionToAnthropicMessage, createAnthropicStreamState, forceCompleteAnthropicStream, parseChatSseLine, } from "./anthropic-translate-response.js";
|
|
7
7
|
import { collectCustomToolNames, responsesToChatRequest, responsesToCompletionsRequest, } from "./translate-request.js";
|
|
8
8
|
import { chatChunkToResponsesEvents, chatCompletionToResponse, createStreamState, forceCompleteStream, parseChatSseLine as parseChatSseLineResponses, } from "./translate-response.js";
|
|
9
|
+
import { modelItemId, modelsFromPayload } from "../utils/fetch-models.js";
|
|
9
10
|
function headerValue(value) {
|
|
10
11
|
return Array.isArray(value) ? value[0] : value;
|
|
11
12
|
}
|
|
@@ -149,8 +150,7 @@ async function fetchModelsJson(upstream) {
|
|
|
149
150
|
}
|
|
150
151
|
try {
|
|
151
152
|
const json = (await response.json());
|
|
152
|
-
|
|
153
|
-
return { ok: true, status: 200, data: data };
|
|
153
|
+
return { ok: true, status: 200, data: modelsFromPayload(json) };
|
|
154
154
|
}
|
|
155
155
|
catch {
|
|
156
156
|
return { ok: false, status: 502, data: [] };
|
|
@@ -175,13 +175,14 @@ async function proxyModelsMerged(_req, res, upstreams) {
|
|
|
175
175
|
if (!result.ok)
|
|
176
176
|
continue;
|
|
177
177
|
for (const item of result.data) {
|
|
178
|
-
const id = item
|
|
179
|
-
? String(item.id)
|
|
180
|
-
: "";
|
|
178
|
+
const id = modelItemId(item);
|
|
181
179
|
if (!id || seen.has(id))
|
|
182
180
|
continue;
|
|
183
181
|
seen.add(id);
|
|
184
|
-
|
|
182
|
+
const row = item && typeof item === "object"
|
|
183
|
+
? item
|
|
184
|
+
: {};
|
|
185
|
+
merged.push(row.id ? item : { ...row, id });
|
|
185
186
|
}
|
|
186
187
|
}
|
|
187
188
|
if (!merged.length && results.every((r) => !r.ok)) {
|
package/dist/index.js
CHANGED
|
File without changes
|
|
@@ -69,7 +69,25 @@ export function buildModelsRequestHeaders(apiFormat, apiKey, customHeaders = {})
|
|
|
69
69
|
}
|
|
70
70
|
return headers;
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Extract a model identifier from a list item. Providers disagree on the field
|
|
74
|
+
* name: OpenAI uses `id`, some gateways `name`/`model`, and Codex-style
|
|
75
|
+
* catalogs (e.g. BigModel) use `slug`.
|
|
76
|
+
*/
|
|
77
|
+
export function modelItemId(item) {
|
|
78
|
+
if (typeof item === "string")
|
|
79
|
+
return item.trim();
|
|
80
|
+
if (!item || typeof item !== "object")
|
|
81
|
+
return "";
|
|
82
|
+
const row = item;
|
|
83
|
+
const id = row.id ?? row.name ?? row.model ?? row.slug;
|
|
84
|
+
return typeof id === "string" ? id.trim() : "";
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Collect the model-array buckets from a /models payload, accepting the common
|
|
88
|
+
* `data` and `models` shapes plus a bare array.
|
|
89
|
+
*/
|
|
90
|
+
export function modelsFromPayload(payload) {
|
|
73
91
|
if (!payload || typeof payload !== "object")
|
|
74
92
|
return [];
|
|
75
93
|
const root = payload;
|
|
@@ -80,18 +98,14 @@ export function parseModelIds(payload) {
|
|
|
80
98
|
buckets.push(...root.models);
|
|
81
99
|
if (Array.isArray(payload))
|
|
82
100
|
buckets.push(...payload);
|
|
101
|
+
return buckets;
|
|
102
|
+
}
|
|
103
|
+
export function parseModelIds(payload) {
|
|
83
104
|
const ids = new Set();
|
|
84
|
-
for (const item of
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
if (!item || typeof item !== "object")
|
|
90
|
-
continue;
|
|
91
|
-
const row = item;
|
|
92
|
-
const id = row.id ?? row.name ?? row.model;
|
|
93
|
-
if (typeof id === "string" && id.trim())
|
|
94
|
-
ids.add(id.trim());
|
|
105
|
+
for (const item of modelsFromPayload(payload)) {
|
|
106
|
+
const id = modelItemId(item);
|
|
107
|
+
if (id)
|
|
108
|
+
ids.add(id);
|
|
95
109
|
}
|
|
96
110
|
return Array.from(ids).sort((a, b) => a.localeCompare(b));
|
|
97
111
|
}
|