@llmgateway/models 0.0.1 → 1.0.1
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 +104 -0
- package/dist/index.d.ts +0 -9
- package/dist/index.js +0 -9
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/dist/get-cheapest-from-available-providers.d.ts +0 -30
- package/dist/get-cheapest-from-available-providers.js +0 -183
- package/dist/get-cheapest-from-available-providers.js.map +0 -1
- package/dist/get-cheapest-model-for-provider.d.ts +0 -2
- package/dist/get-cheapest-model-for-provider.js +0 -49
- package/dist/get-cheapest-model-for-provider.js.map +0 -1
- package/dist/get-provider-endpoint.d.ts +0 -3
- package/dist/get-provider-endpoint.js +0 -243
- package/dist/get-provider-endpoint.js.map +0 -1
- package/dist/get-provider-headers.d.ts +0 -5
- package/dist/get-provider-headers.js +0 -45
- package/dist/get-provider-headers.js.map +0 -1
- package/dist/models.spec.d.ts +0 -1
- package/dist/models.spec.js +0 -263
- package/dist/models.spec.js.map +0 -1
- package/dist/prepare-request-body.d.ts +0 -10
- package/dist/prepare-request-body.js +0 -1081
- package/dist/prepare-request-body.js.map +0 -1
- package/dist/prepare-request-body.spec.d.ts +0 -1
- package/dist/prepare-request-body.spec.js +0 -231
- package/dist/prepare-request-body.spec.js.map +0 -1
- package/dist/process-image-url.d.ts +0 -4
- package/dist/process-image-url.js +0 -121
- package/dist/process-image-url.js.map +0 -1
- package/dist/transform-anthropic-messages.d.ts +0 -2
- package/dist/transform-anthropic-messages.js +0 -185
- package/dist/transform-anthropic-messages.js.map +0 -1
- package/dist/transform-google-messages.d.ts +0 -25
- package/dist/transform-google-messages.js +0 -122
- package/dist/transform-google-messages.js.map +0 -1
- package/dist/validate-provider-key.d.ts +0 -4
- package/dist/validate-provider-key.js +0 -113
- package/dist/validate-provider-key.js.map +0 -1
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { logger } from "@llmgateway/logger";
|
|
2
|
-
import { processImageUrl } from "./process-image-url.js";
|
|
3
|
-
import { isImageUrlContent, isTextContent, } from "./types.js";
|
|
4
|
-
export async function transformAnthropicMessages(messages, isProd = false, provider, _model, maxImageSizeMB = 20, userPlan = null, initialCacheControlCount = 0, minCacheableChars = 1024 * 4) {
|
|
5
|
-
const results = [];
|
|
6
|
-
const shouldApplyCacheControl = provider === "anthropic";
|
|
7
|
-
let cacheControlCount = initialCacheControlCount;
|
|
8
|
-
const maxCacheControlBlocks = 4;
|
|
9
|
-
const seenToolUseIds = new Set();
|
|
10
|
-
const idMapping = new Map();
|
|
11
|
-
const toolResultCount = new Map();
|
|
12
|
-
const groupedMessages = [];
|
|
13
|
-
const toolMessageGroups = new Map();
|
|
14
|
-
for (const message of messages) {
|
|
15
|
-
const originalRole = message.role === "user" && message.tool_call_id ? "tool" : message.role;
|
|
16
|
-
if (originalRole === "tool" && message.tool_call_id) {
|
|
17
|
-
if (!toolMessageGroups.has(message.tool_call_id)) {
|
|
18
|
-
toolMessageGroups.set(message.tool_call_id, []);
|
|
19
|
-
}
|
|
20
|
-
toolMessageGroups.get(message.tool_call_id).push(message);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
for (const [_toolCallId, toolMessages] of Array.from(toolMessageGroups)) {
|
|
24
|
-
if (toolMessages.length > 0) {
|
|
25
|
-
toolMessages.forEach((toolMessage) => {
|
|
26
|
-
groupedMessages.push(toolMessage);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
toolMessageGroups.clear();
|
|
31
|
-
groupedMessages.push(message);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
for (const [_toolCallId, toolMessages] of Array.from(toolMessageGroups)) {
|
|
35
|
-
if (toolMessages.length > 0) {
|
|
36
|
-
toolMessages.forEach((toolMessage) => {
|
|
37
|
-
groupedMessages.push(toolMessage);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
for (const m of groupedMessages) {
|
|
42
|
-
let content = [];
|
|
43
|
-
if (Array.isArray(m.content)) {
|
|
44
|
-
content = await Promise.all(m.content.map(async (part) => {
|
|
45
|
-
if (isImageUrlContent(part)) {
|
|
46
|
-
try {
|
|
47
|
-
const { data, mimeType } = await processImageUrl(part.image_url.url, isProd, maxImageSizeMB, userPlan);
|
|
48
|
-
return {
|
|
49
|
-
type: "image",
|
|
50
|
-
source: {
|
|
51
|
-
type: "base64",
|
|
52
|
-
media_type: mimeType,
|
|
53
|
-
data: data,
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
logger.error(`Failed to fetch image ${part.image_url.url}`, {
|
|
59
|
-
err: error instanceof Error ? error : new Error(String(error)),
|
|
60
|
-
});
|
|
61
|
-
return {
|
|
62
|
-
type: "text",
|
|
63
|
-
text: `[Image failed to load: ${part.image_url.url}]`,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (isTextContent(part) && part.text && !part.cache_control) {
|
|
68
|
-
const shouldCache = shouldApplyCacheControl &&
|
|
69
|
-
part.text.length >= minCacheableChars &&
|
|
70
|
-
cacheControlCount < maxCacheControlBlocks;
|
|
71
|
-
if (shouldCache) {
|
|
72
|
-
cacheControlCount++;
|
|
73
|
-
return {
|
|
74
|
-
...part,
|
|
75
|
-
cache_control: { type: "ephemeral" },
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return part;
|
|
80
|
-
}));
|
|
81
|
-
}
|
|
82
|
-
else if (m.content && typeof m.content === "string") {
|
|
83
|
-
const shouldCache = shouldApplyCacheControl &&
|
|
84
|
-
m.content.length >= minCacheableChars &&
|
|
85
|
-
cacheControlCount < maxCacheControlBlocks;
|
|
86
|
-
const textContent = {
|
|
87
|
-
type: "text",
|
|
88
|
-
text: m.content,
|
|
89
|
-
...(shouldCache && { cache_control: { type: "ephemeral" } }),
|
|
90
|
-
};
|
|
91
|
-
if (shouldCache) {
|
|
92
|
-
cacheControlCount++;
|
|
93
|
-
}
|
|
94
|
-
content = [textContent];
|
|
95
|
-
}
|
|
96
|
-
if (m.tool_calls && Array.isArray(m.tool_calls)) {
|
|
97
|
-
const toolUseBlocks = m.tool_calls.map((toolCall, index) => {
|
|
98
|
-
let uniqueId = toolCall.id;
|
|
99
|
-
const duplicatesInSameMessage = m
|
|
100
|
-
.tool_calls.slice(0, index)
|
|
101
|
-
.filter((tc) => tc.id === toolCall.id);
|
|
102
|
-
if (duplicatesInSameMessage.length > 0) {
|
|
103
|
-
uniqueId = `${toolCall.id}_${duplicatesInSameMessage.length + 1}`;
|
|
104
|
-
}
|
|
105
|
-
if (seenToolUseIds.has(uniqueId)) {
|
|
106
|
-
let counter = 1;
|
|
107
|
-
let newId = `${uniqueId}_${counter}`;
|
|
108
|
-
while (seenToolUseIds.has(newId)) {
|
|
109
|
-
counter++;
|
|
110
|
-
newId = `${uniqueId}_${counter}`;
|
|
111
|
-
}
|
|
112
|
-
uniqueId = newId;
|
|
113
|
-
}
|
|
114
|
-
if (!idMapping.has(toolCall.id)) {
|
|
115
|
-
idMapping.set(toolCall.id, []);
|
|
116
|
-
}
|
|
117
|
-
idMapping.get(toolCall.id).push(uniqueId);
|
|
118
|
-
seenToolUseIds.add(uniqueId);
|
|
119
|
-
return {
|
|
120
|
-
type: "tool_use",
|
|
121
|
-
id: uniqueId,
|
|
122
|
-
name: toolCall.function.name,
|
|
123
|
-
input: JSON.parse(toolCall.function.arguments),
|
|
124
|
-
};
|
|
125
|
-
});
|
|
126
|
-
content = content.concat(toolUseBlocks);
|
|
127
|
-
}
|
|
128
|
-
const originalRole = m.role === "user" && m.tool_call_id ? "tool" : m.role;
|
|
129
|
-
if (originalRole === "tool" && m.tool_call_id && m.content !== undefined) {
|
|
130
|
-
let toolResultContent;
|
|
131
|
-
const contentStr = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
|
|
132
|
-
try {
|
|
133
|
-
const parsed = JSON.parse(contentStr);
|
|
134
|
-
if (typeof parsed === "object") {
|
|
135
|
-
toolResultContent = contentStr;
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
toolResultContent = String(parsed);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
catch {
|
|
142
|
-
toolResultContent = contentStr;
|
|
143
|
-
}
|
|
144
|
-
if (!toolResultContent || toolResultContent.trim() === "") {
|
|
145
|
-
toolResultContent = "No output";
|
|
146
|
-
}
|
|
147
|
-
const mappedToolUseIds = idMapping.get(m.tool_call_id) || [
|
|
148
|
-
m.tool_call_id,
|
|
149
|
-
];
|
|
150
|
-
const currentCount = toolResultCount.get(m.tool_call_id) || 0;
|
|
151
|
-
toolResultCount.set(m.tool_call_id, currentCount + 1);
|
|
152
|
-
if (mappedToolUseIds.length > 1 && currentCount === 0) {
|
|
153
|
-
content = mappedToolUseIds.map((mappedId) => ({
|
|
154
|
-
type: "tool_result",
|
|
155
|
-
tool_use_id: mappedId,
|
|
156
|
-
content: toolResultContent,
|
|
157
|
-
}));
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
const toolUseId = mappedToolUseIds[currentCount] || mappedToolUseIds[0];
|
|
161
|
-
content = [
|
|
162
|
-
{
|
|
163
|
-
type: "tool_result",
|
|
164
|
-
tool_use_id: toolUseId,
|
|
165
|
-
content: toolResultContent,
|
|
166
|
-
},
|
|
167
|
-
];
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
const filteredContent = content.filter((part) => !(isTextContent(part) && (!part.text || part.text.trim() === "")));
|
|
171
|
-
if (filteredContent.length === 0 &&
|
|
172
|
-
(!m.tool_calls || m.tool_calls.length === 0)) {
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
const { tool_calls: _, tool_call_id: __, ...messageWithoutToolFields } = m;
|
|
176
|
-
const anthropicRole = messageWithoutToolFields.role === "assistant" ? "assistant" : "user";
|
|
177
|
-
results.push({
|
|
178
|
-
...messageWithoutToolFields,
|
|
179
|
-
content: filteredContent,
|
|
180
|
-
role: anthropicRole,
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
return results;
|
|
184
|
-
}
|
|
185
|
-
//# sourceMappingURL=transform-anthropic-messages.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transform-anthropic-messages.js","sourceRoot":"","sources":["../src/transform-anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAGN,iBAAiB,EACjB,aAAa,GAKb,MAAM,YAAY,CAAC;AAOpB,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC/C,QAAuB,EACvB,MAAM,GAAG,KAAK,EACd,QAAiB,EACjB,MAAe,EACf,cAAc,GAAG,EAAE,EACnB,WAAkC,IAAI,EACtC,wBAAwB,GAAG,CAAC,EAC5B,iBAAiB,GAAG,IAAI,GAAG,CAAC;IAE5B,MAAM,OAAO,GAAuB,EAAE,CAAC;IAIvC,MAAM,uBAAuB,GAAG,QAAQ,KAAK,WAAW,CAAC;IAGzD,IAAI,iBAAiB,GAAG,wBAAwB,CAAC;IACjD,MAAM,qBAAqB,GAAG,CAAC,CAAC;IAGhC,MAAM,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;IAEzC,MAAM,SAAS,GAAG,IAAI,GAAG,EAAoB,CAAC;IAE9C,MAAM,eAAe,GAAG,IAAI,GAAG,EAAkB,CAAC;IAGlD,MAAM,eAAe,GAAkB,EAAE,CAAC;IAC1C,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAyB,CAAC;IAE3D,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAEhC,MAAM,YAAY,GACjB,OAAO,CAAC,IAAI,KAAK,MAAM,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC;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|
-
mime_type: string;
|
|
7
|
-
data: string;
|
|
8
|
-
};
|
|
9
|
-
functionCall?: {
|
|
10
|
-
name: string;
|
|
11
|
-
args: Record<string, unknown>;
|
|
12
|
-
};
|
|
13
|
-
functionResponse?: {
|
|
14
|
-
name: string;
|
|
15
|
-
response: {
|
|
16
|
-
result: unknown;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
interface GoogleMessageExtended {
|
|
21
|
-
role: "user" | "model";
|
|
22
|
-
parts: GooglePart[];
|
|
23
|
-
}
|
|
24
|
-
export declare function transformGoogleMessages(messages: BaseMessage[], isProd?: boolean, maxImageSizeMB?: number, userPlan?: "free" | "pro" | null, thoughtSignatureCache?: Map<string, string>): Promise<GoogleMessageExtended[]>;
|
|
25
|
-
export {};
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { processImageUrl } from "./process-image-url.js";
|
|
2
|
-
import { isImageUrlContent, isTextContent } from "./types.js";
|
|
3
|
-
export async function transformGoogleMessages(messages, isProd = false, maxImageSizeMB = 20, userPlan = null, thoughtSignatureCache) {
|
|
4
|
-
const result = [];
|
|
5
|
-
for (const m of messages) {
|
|
6
|
-
if (m.role === "tool") {
|
|
7
|
-
const lastMsg = result[result.length - 1];
|
|
8
|
-
const functionResponsePart = {
|
|
9
|
-
functionResponse: {
|
|
10
|
-
name: m.name || "unknown_function",
|
|
11
|
-
response: {
|
|
12
|
-
result: m.content,
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
if (lastMsg && lastMsg.role === "user") {
|
|
17
|
-
lastMsg.parts.push(functionResponsePart);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
result.push({
|
|
21
|
-
role: "user",
|
|
22
|
-
parts: [functionResponsePart],
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
if (m.role === "assistant" && m.tool_calls && m.tool_calls.length > 0) {
|
|
28
|
-
const parts = [];
|
|
29
|
-
if (m.content) {
|
|
30
|
-
if (Array.isArray(m.content)) {
|
|
31
|
-
for (const content of m.content) {
|
|
32
|
-
if (isTextContent(content)) {
|
|
33
|
-
const textPart = { text: content.text };
|
|
34
|
-
const extraContent = content.extra_content;
|
|
35
|
-
if (extraContent?.google?.thought_signature) {
|
|
36
|
-
textPart.thoughtSignature =
|
|
37
|
-
extraContent.google.thought_signature;
|
|
38
|
-
}
|
|
39
|
-
parts.push(textPart);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else if (typeof m.content === "string" && m.content) {
|
|
44
|
-
parts.push({ text: m.content });
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
for (const toolCall of m.tool_calls) {
|
|
48
|
-
if (toolCall.type === "function") {
|
|
49
|
-
let args = {};
|
|
50
|
-
try {
|
|
51
|
-
args = JSON.parse(toolCall.function.arguments || "{}");
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
args = {};
|
|
55
|
-
}
|
|
56
|
-
const functionCallPart = {
|
|
57
|
-
functionCall: {
|
|
58
|
-
name: toolCall.function.name,
|
|
59
|
-
args,
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
const extraContent = toolCall.extra_content;
|
|
63
|
-
if (extraContent?.google?.thought_signature) {
|
|
64
|
-
functionCallPart.thoughtSignature =
|
|
65
|
-
extraContent.google.thought_signature;
|
|
66
|
-
}
|
|
67
|
-
else if (thoughtSignatureCache && toolCall.id) {
|
|
68
|
-
const cachedSignature = thoughtSignatureCache.get(toolCall.id);
|
|
69
|
-
if (cachedSignature) {
|
|
70
|
-
functionCallPart.thoughtSignature = cachedSignature;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
parts.push(functionCallPart);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
result.push({
|
|
77
|
-
role: "model",
|
|
78
|
-
parts,
|
|
79
|
-
});
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
const role = m.role === "assistant" ? "model" : "user";
|
|
83
|
-
const parts = [];
|
|
84
|
-
if (Array.isArray(m.content)) {
|
|
85
|
-
for (const content of m.content) {
|
|
86
|
-
if (isTextContent(content)) {
|
|
87
|
-
const textPart = { text: content.text };
|
|
88
|
-
const extraContent = content.extra_content;
|
|
89
|
-
if (extraContent?.google?.thought_signature) {
|
|
90
|
-
textPart.thoughtSignature = extraContent.google.thought_signature;
|
|
91
|
-
}
|
|
92
|
-
parts.push(textPart);
|
|
93
|
-
}
|
|
94
|
-
else if (isImageUrlContent(content)) {
|
|
95
|
-
const imageUrl = content.image_url.url;
|
|
96
|
-
try {
|
|
97
|
-
const { data, mimeType } = await processImageUrl(imageUrl, isProd, maxImageSizeMB, userPlan);
|
|
98
|
-
parts.push({
|
|
99
|
-
inline_data: {
|
|
100
|
-
mime_type: mimeType,
|
|
101
|
-
data: data,
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
const errorMsg = error instanceof Error ? error.message : "Unknown error";
|
|
107
|
-
throw new Error(`Failed to process image: ${errorMsg}`);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
throw new Error(`Not supported content type yet: ${content.type}`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
parts.push({ text: m.content });
|
|
117
|
-
}
|
|
118
|
-
result.push({ role, parts });
|
|
119
|
-
}
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
//# sourceMappingURL=transform-google-messages.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transform-google-messages.js","sourceRoot":"","sources":["../src/transform-google-messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAoB,iBAAiB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC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AC;wBACJ,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,MAAM,eAAe,CAC/C,QAAQ,EACR,MAAM,EACN,cAAc,EACd,QAAQ,CACR,CAAC;wBACF,KAAK,CAAC,IAAI,CAAC;4BACV,WAAW,EAAE;gCACZ,SAAS,EAAE,QAAQ;gCACnB,IAAI,EAAE,IAAI;6BACV;yBACD,CAAC,CAAC;oBACJ,CAAC;oBAAC,OAAO,KAAK,EAAE,CAAC;wBAEhB,MAAM,QAAQ,GACb,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,CAAC;wBAC1D,MAAM,IAAI,KAAK,CAAC,4BAA4B,QAAQ,EAAE,CAAC,CAAC;oBACzD,CAAC;gBACF,CAAC;qBAAM,CAAC;oBACP,MAAM,IAAI,KAAK,CACd,mCAAoC,OAAe,CAAC,IAAI,EAAE,CAC1D,CAAC;gBACH,CAAC;YACF,CAAC;QACF,CAAC;aAAM,CAAC;YAEP,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;QACjC,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { ProviderId } from "./providers.js";
|
|
2
|
-
import type { ProviderValidationResult } from "./types.js";
|
|
3
|
-
import type { ProviderKeyOptions } from "@llmgateway/db";
|
|
4
|
-
export declare function validateProviderKey(provider: ProviderId, token: string, baseUrl?: string, skipValidation?: boolean, providerKeyOptions?: ProviderKeyOptions): Promise<ProviderValidationResult>;
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { logger } from "@llmgateway/logger";
|
|
2
|
-
import { getCheapestModelForProvider } from "./get-cheapest-model-for-provider.js";
|
|
3
|
-
import { getProviderEndpoint } from "./get-provider-endpoint.js";
|
|
4
|
-
import { getProviderHeaders } from "./get-provider-headers.js";
|
|
5
|
-
import { models } from "./models.js";
|
|
6
|
-
import { prepareRequestBody } from "./prepare-request-body.js";
|
|
7
|
-
export async function validateProviderKey(provider, token, baseUrl, skipValidation = false, providerKeyOptions) {
|
|
8
|
-
if (skipValidation) {
|
|
9
|
-
return { valid: true };
|
|
10
|
-
}
|
|
11
|
-
if (provider === "custom") {
|
|
12
|
-
return { valid: true };
|
|
13
|
-
}
|
|
14
|
-
try {
|
|
15
|
-
let validationModel;
|
|
16
|
-
if (provider === "azure" && providerKeyOptions?.azure_validation_model) {
|
|
17
|
-
validationModel = providerKeyOptions.azure_validation_model;
|
|
18
|
-
logger.debug("Using Azure validation model from options", {
|
|
19
|
-
provider,
|
|
20
|
-
validationModel,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
else if (provider === "openai") {
|
|
24
|
-
validationModel = "gpt-4o-mini";
|
|
25
|
-
logger.debug("Using fixed OpenAI validation model", {
|
|
26
|
-
provider,
|
|
27
|
-
validationModel,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
const cheapestModel = getCheapestModelForProvider(provider);
|
|
32
|
-
logger.debug("Using cheapest validation model", {
|
|
33
|
-
provider,
|
|
34
|
-
validationModel: cheapestModel || undefined,
|
|
35
|
-
});
|
|
36
|
-
if (!cheapestModel) {
|
|
37
|
-
throw new Error(`No model with pricing information found for provider ${provider}`);
|
|
38
|
-
}
|
|
39
|
-
validationModel = cheapestModel;
|
|
40
|
-
}
|
|
41
|
-
const modelDef = models.find((m) => m.providers.some((p) => p.providerId === provider && p.modelName === validationModel));
|
|
42
|
-
const modelId = modelDef?.id;
|
|
43
|
-
const effectiveModelId = provider === "azure" && providerKeyOptions?.azure_validation_model
|
|
44
|
-
? providerKeyOptions.azure_validation_model
|
|
45
|
-
: modelId;
|
|
46
|
-
logger.debug("Validation endpoint configuration", {
|
|
47
|
-
provider,
|
|
48
|
-
validationModel,
|
|
49
|
-
modelId,
|
|
50
|
-
effectiveModelId,
|
|
51
|
-
providerKeyOptions,
|
|
52
|
-
});
|
|
53
|
-
const endpoint = getProviderEndpoint(provider, baseUrl, effectiveModelId, provider === "google-ai-studio" || provider === "google-vertex"
|
|
54
|
-
? token
|
|
55
|
-
: undefined, false, false, false, providerKeyOptions);
|
|
56
|
-
const systemMessage = {
|
|
57
|
-
role: "system",
|
|
58
|
-
content: "You are a helpful assistant.",
|
|
59
|
-
};
|
|
60
|
-
const minimalMessage = {
|
|
61
|
-
role: "user",
|
|
62
|
-
content: "Hello",
|
|
63
|
-
};
|
|
64
|
-
const messages = [systemMessage, minimalMessage];
|
|
65
|
-
const providerMapping = modelDef?.providers.find((p) => p.providerId === provider && p.modelName === validationModel);
|
|
66
|
-
const supportedParameters = providerMapping?.supportedParameters;
|
|
67
|
-
const supportsMaxTokens = supportedParameters?.includes("max_tokens") &&
|
|
68
|
-
providerMapping?.providerId !== "azure";
|
|
69
|
-
const payload = await prepareRequestBody(provider, validationModel, messages, false, undefined, supportsMaxTokens ? 10 : undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, false, false, 20, null, undefined, undefined, undefined);
|
|
70
|
-
const headers = getProviderHeaders(provider, token);
|
|
71
|
-
headers["Content-Type"] = "application/json";
|
|
72
|
-
const response = await fetch(endpoint, {
|
|
73
|
-
method: "POST",
|
|
74
|
-
headers,
|
|
75
|
-
body: JSON.stringify(payload),
|
|
76
|
-
});
|
|
77
|
-
if (!response.ok) {
|
|
78
|
-
const errorText = await response.text();
|
|
79
|
-
let errorMessage = `Error from provider: ${response.status} ${response.statusText}`;
|
|
80
|
-
try {
|
|
81
|
-
const errorJson = JSON.parse(errorText);
|
|
82
|
-
if (errorJson.error?.message) {
|
|
83
|
-
errorMessage = errorJson.error.message;
|
|
84
|
-
}
|
|
85
|
-
else if (errorJson.message) {
|
|
86
|
-
errorMessage = errorJson.message;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
catch { }
|
|
90
|
-
if (response.status === 401) {
|
|
91
|
-
return {
|
|
92
|
-
valid: false,
|
|
93
|
-
statusCode: response.status,
|
|
94
|
-
model: validationModel,
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
valid: false,
|
|
99
|
-
error: errorMessage,
|
|
100
|
-
statusCode: response.status,
|
|
101
|
-
model: validationModel,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
return { valid: true, model: validationModel };
|
|
105
|
-
}
|
|
106
|
-
catch (error) {
|
|
107
|
-
return {
|
|
108
|
-
valid: false,
|
|
109
|
-
error: error instanceof Error ? error.message : "Unknown error occurred",
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
//# sourceMappingURL=validate-provider-key.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validate-provider-key.js","sourceRoot":"","sources":["../src/validate-provider-key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,2BAA2B,EAAE,MAAM,sCAAsC,CAAC;AACnF,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,MAAM,EAA6B,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAS/D,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACxC,QAAoB,EACpB,KAAa,EACb,OAAgB,EAChB,cAAc,GAAG,KAAK,EACtB,kBAAuC;IAGvC,IAAI,cAAc,EAAE,CAAC;QACpB,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;IAGD,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,EAAE,KAAK,EAAE,IAAI,EAA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