llm-proxy 1.0.21 → 1.0.23
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.
|
@@ -1,8 +1,22 @@
|
|
|
1
|
+
export declare class AdapterError extends Error {
|
|
2
|
+
details?: any;
|
|
3
|
+
constructor(message: string, details?: any);
|
|
4
|
+
}
|
|
5
|
+
export declare class UnsupportedProviderError extends AdapterError {
|
|
6
|
+
constructor(provider: string);
|
|
7
|
+
}
|
|
8
|
+
export declare class ContentExtractionError extends AdapterError {
|
|
9
|
+
constructor(contentType: string, details?: any);
|
|
10
|
+
}
|
|
11
|
+
export declare class ResponseValidationError extends AdapterError {
|
|
12
|
+
response?: any;
|
|
13
|
+
constructor(message: string, response?: any);
|
|
14
|
+
}
|
|
1
15
|
import { LLMResponse, Providers } from "../types";
|
|
2
16
|
export declare class OutputFormatAdapter {
|
|
3
17
|
static adaptResponse(response: any, provider: Providers): LLMResponse;
|
|
18
|
+
private static validateOpenAIResponse;
|
|
4
19
|
private static adaptAnthropicBedrockResponse;
|
|
5
|
-
private static adaptAnthropicBedrockStreamResponse;
|
|
6
20
|
private static mapRole;
|
|
7
21
|
private static extractContent;
|
|
8
22
|
}
|
|
@@ -1,126 +1,109 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OutputFormatAdapter = void 0;
|
|
3
|
+
exports.OutputFormatAdapter = exports.ResponseValidationError = exports.ContentExtractionError = exports.UnsupportedProviderError = exports.AdapterError = void 0;
|
|
4
|
+
// Custom error classes
|
|
5
|
+
class AdapterError extends Error {
|
|
6
|
+
constructor(message, details) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.details = details;
|
|
9
|
+
this.name = "AdapterError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.AdapterError = AdapterError;
|
|
13
|
+
class UnsupportedProviderError extends AdapterError {
|
|
14
|
+
constructor(provider) {
|
|
15
|
+
super(`Unsupported provider: ${provider}`);
|
|
16
|
+
this.name = "UnsupportedProviderError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.UnsupportedProviderError = UnsupportedProviderError;
|
|
20
|
+
class ContentExtractionError extends AdapterError {
|
|
21
|
+
constructor(contentType, details) {
|
|
22
|
+
super(`Failed to extract content for type: ${contentType}`, details);
|
|
23
|
+
this.name = "ContentExtractionError";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.ContentExtractionError = ContentExtractionError;
|
|
27
|
+
class ResponseValidationError extends AdapterError {
|
|
28
|
+
constructor(message, response) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.response = response;
|
|
31
|
+
this.name = "ResponseValidationError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.ResponseValidationError = ResponseValidationError;
|
|
4
35
|
const types_1 = require("../types");
|
|
5
36
|
class OutputFormatAdapter {
|
|
6
37
|
static adaptResponse(response, provider) {
|
|
38
|
+
if (!response) {
|
|
39
|
+
throw new ResponseValidationError("Response object is null or undefined");
|
|
40
|
+
}
|
|
7
41
|
try {
|
|
8
42
|
switch (provider) {
|
|
9
43
|
case types_1.Providers.OPENAI:
|
|
44
|
+
this.validateOpenAIResponse(response);
|
|
10
45
|
return response;
|
|
11
46
|
case types_1.Providers.ANTHROPIC_BEDROCK:
|
|
12
|
-
if (response.type === "message_start") {
|
|
13
|
-
return this.adaptAnthropicBedrockStreamResponse(response);
|
|
14
|
-
}
|
|
15
47
|
return this.adaptAnthropicBedrockResponse(response);
|
|
16
48
|
default:
|
|
17
|
-
throw new
|
|
49
|
+
throw new UnsupportedProviderError(provider);
|
|
18
50
|
}
|
|
19
51
|
}
|
|
20
52
|
catch (error) {
|
|
21
|
-
|
|
22
|
-
|
|
53
|
+
if (error instanceof AdapterError) {
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
throw new AdapterError("Failed to adapt response", {
|
|
57
|
+
provider,
|
|
58
|
+
originalError: error,
|
|
59
|
+
});
|
|
23
60
|
}
|
|
24
61
|
}
|
|
25
|
-
static
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (!response || !response.id || !response.model || !response.content) {
|
|
29
|
-
throw new Error("Invalid Anthropic Bedrock response structure");
|
|
62
|
+
static validateOpenAIResponse(response) {
|
|
63
|
+
if (!response.id || !response.choices) {
|
|
64
|
+
throw new ResponseValidationError("Invalid OpenAI response structure", response);
|
|
30
65
|
}
|
|
31
|
-
return {
|
|
32
|
-
id: response.id,
|
|
33
|
-
object: "chat.completion",
|
|
34
|
-
created: Date.now(),
|
|
35
|
-
model: response.model,
|
|
36
|
-
choices: response.content.map((contentBlock, index) => ({
|
|
37
|
-
index,
|
|
38
|
-
message: {
|
|
39
|
-
role: this.mapRole(contentBlock),
|
|
40
|
-
content: this.extractContent(contentBlock),
|
|
41
|
-
},
|
|
42
|
-
logprobs: null,
|
|
43
|
-
finish_reason: response.stop_reason || null,
|
|
44
|
-
})),
|
|
45
|
-
usage: {
|
|
46
|
-
prompt_tokens: ((_a = response.usage) === null || _a === void 0 ? void 0 : _a.input_tokens) || 0,
|
|
47
|
-
completion_tokens: ((_b = response.usage) === null || _b === void 0 ? void 0 : _b.output_tokens) || 0,
|
|
48
|
-
total_tokens: (((_c = response.usage) === null || _c === void 0 ? void 0 : _c.input_tokens) || 0) +
|
|
49
|
-
(((_d = response.usage) === null || _d === void 0 ? void 0 : _d.output_tokens) || 0),
|
|
50
|
-
prompt_tokens_details: { cached_tokens: 0 },
|
|
51
|
-
completion_tokens_details: { reasoning_tokens: 0 },
|
|
52
|
-
},
|
|
53
|
-
system_fingerprint: "anthropic_translation",
|
|
54
|
-
};
|
|
55
66
|
}
|
|
56
|
-
static
|
|
57
|
-
|
|
58
|
-
|
|
67
|
+
static adaptAnthropicBedrockResponse(response) {
|
|
68
|
+
if (!response.id || !response.content || !response.usage) {
|
|
69
|
+
throw new ResponseValidationError("Invalid Anthropic Bedrock response structure", response);
|
|
70
|
+
}
|
|
59
71
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
id: chunk.message.id || "stream_chunk",
|
|
64
|
-
object: "chat.completion.chunk",
|
|
65
|
-
created: Date.now(),
|
|
66
|
-
model: chunk.message.model || "anthropic_stream",
|
|
67
|
-
choices: [
|
|
68
|
-
{
|
|
69
|
-
index: 0,
|
|
70
|
-
delta: {
|
|
71
|
-
content: ((_b = (_a = chunk.message.content) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.type) === "text"
|
|
72
|
-
? chunk.message.content[0]
|
|
73
|
-
.text
|
|
74
|
-
: "",
|
|
75
|
-
},
|
|
76
|
-
logprobs: null,
|
|
77
|
-
finish_reason: chunk.message.stop_reason || null,
|
|
78
|
-
},
|
|
79
|
-
],
|
|
80
|
-
usage: {
|
|
81
|
-
prompt_tokens: ((_c = chunk.message.usage) === null || _c === void 0 ? void 0 : _c.input_tokens) || 0,
|
|
82
|
-
completion_tokens: ((_d = chunk.message.usage) === null || _d === void 0 ? void 0 : _d.output_tokens) || 0,
|
|
83
|
-
total_tokens: (((_e = chunk.message.usage) === null || _e === void 0 ? void 0 : _e.input_tokens) || 0) +
|
|
84
|
-
(((_f = chunk.message.usage) === null || _f === void 0 ? void 0 : _f.output_tokens) || 0),
|
|
85
|
-
prompt_tokens_details: { cached_tokens: 0 },
|
|
86
|
-
completion_tokens_details: { reasoning_tokens: 0 },
|
|
87
|
-
},
|
|
88
|
-
system_fingerprint: "anthropic_translation",
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
// For content chunks
|
|
92
|
-
return {
|
|
93
|
-
id: "stream_chunk",
|
|
94
|
-
object: "chat.completion.chunk",
|
|
72
|
+
const openAIResponse = {
|
|
73
|
+
id: response.id,
|
|
74
|
+
object: "text_completion",
|
|
95
75
|
created: Date.now(),
|
|
96
|
-
model:
|
|
97
|
-
choices:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
},
|
|
103
|
-
logprobs: null,
|
|
104
|
-
finish_reason: null,
|
|
76
|
+
model: response.model,
|
|
77
|
+
choices: response.content.map((contentBlock, index) => ({
|
|
78
|
+
index,
|
|
79
|
+
message: {
|
|
80
|
+
role: this.mapRole(contentBlock),
|
|
81
|
+
content: this.extractContent(contentBlock),
|
|
105
82
|
},
|
|
106
|
-
|
|
83
|
+
logprobs: null,
|
|
84
|
+
finish_reason: response.stop_reason,
|
|
85
|
+
})),
|
|
107
86
|
usage: {
|
|
108
|
-
prompt_tokens:
|
|
109
|
-
completion_tokens:
|
|
110
|
-
total_tokens:
|
|
87
|
+
prompt_tokens: response.usage.input_tokens,
|
|
88
|
+
completion_tokens: response.usage.output_tokens,
|
|
89
|
+
total_tokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
111
90
|
prompt_tokens_details: { cached_tokens: 0 },
|
|
112
91
|
completion_tokens_details: { reasoning_tokens: 0 },
|
|
113
92
|
},
|
|
114
93
|
system_fingerprint: "anthropic_translation",
|
|
115
94
|
};
|
|
95
|
+
return openAIResponse;
|
|
116
96
|
}
|
|
117
97
|
catch (error) {
|
|
118
|
-
throw new
|
|
98
|
+
throw new AdapterError("Failed to adapt Anthropic response", {
|
|
99
|
+
responseId: response.id,
|
|
100
|
+
originalError: error,
|
|
101
|
+
});
|
|
119
102
|
}
|
|
120
103
|
}
|
|
121
104
|
static mapRole(content) {
|
|
122
105
|
if (!content || !content.type) {
|
|
123
|
-
throw new
|
|
106
|
+
throw new ContentExtractionError("Invalid content block structure");
|
|
124
107
|
}
|
|
125
108
|
switch (content.type) {
|
|
126
109
|
case types_1.BedrockAnthropicContentType.TOOL_USE:
|
|
@@ -129,22 +112,34 @@ class OutputFormatAdapter {
|
|
|
129
112
|
case types_1.BedrockAnthropicContentType.TEXT:
|
|
130
113
|
return "assistant";
|
|
131
114
|
default:
|
|
132
|
-
|
|
115
|
+
throw new ContentExtractionError(`Unknown content type: ${content.type}`);
|
|
133
116
|
}
|
|
134
117
|
}
|
|
135
118
|
static extractContent(content) {
|
|
136
119
|
if (!content || !content.type) {
|
|
137
|
-
throw new
|
|
120
|
+
throw new ContentExtractionError("Invalid content block structure");
|
|
138
121
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
122
|
+
try {
|
|
123
|
+
switch (content.type) {
|
|
124
|
+
case types_1.BedrockAnthropicContentType.TEXT:
|
|
125
|
+
const textContent = content;
|
|
126
|
+
if (!textContent.text) {
|
|
127
|
+
throw new ContentExtractionError("Missing text in TEXT content");
|
|
128
|
+
}
|
|
129
|
+
return textContent.text;
|
|
130
|
+
case types_1.BedrockAnthropicContentType.TOOL_RESULT:
|
|
131
|
+
return content.content || "";
|
|
132
|
+
case types_1.BedrockAnthropicContentType.TOOL_USE:
|
|
133
|
+
return content.id || "";
|
|
134
|
+
default:
|
|
135
|
+
throw new ContentExtractionError(`Unsupported content type: ${content.type}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
throw new ContentExtractionError(`Failed to extract content`, {
|
|
140
|
+
contentType: content.type,
|
|
141
|
+
originalError: error,
|
|
142
|
+
});
|
|
148
143
|
}
|
|
149
144
|
}
|
|
150
145
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OutputFormatAdapter.js","sourceRoot":"","sources":["../../src/middleware/OutputFormatAdapter.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"OutputFormatAdapter.js","sourceRoot":"","sources":["../../src/middleware/OutputFormatAdapter.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;AACvB,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe,EAAS,OAAa;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAC;QADmB,YAAO,GAAP,OAAO,CAAM;QAE/C,IAAI,CAAC,I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AAG,OAAsC,CAAC;oBAC3D,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;wBACtB,MAAM,IAAI,sBAAsB,CAAC,8BAA8B,CAAC,CAAC;oBACnE,CAAC;oBACD,OAAO,WAAW,CAAC,IAAI,CAAC;gBAC1B,KAAK,mCAA2B,CAAC,WAAW;oBAC1C,OAAQ,OAA6C,CAAC,OAAO,IAAI,EAAE,CAAC;gBACtE,KAAK,mCAA2B,CAAC,QAAQ;oBACvC,OAAQ,OAA0C,CAAC,EAAE,IAAI,EAAE,CAAC;gBAC9D;oBACE,MAAM,IAAI,sBAAsB,CAC9B,6BAA6B,OAAO,CAAC,IAAI,EAAE,CAC5C,CAAC;YACN,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,sBAAsB,CAAC,2BAA2B,EAAE;gBAC5D,WAAW,EAAE,OAAO,CAAC,IAAI;gBACzB,aAAa,EAAE,KAAK;aACrB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF;AAnID,kDAmIC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llm-proxy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"description": "An LLM Proxy that allows the user to interact with different language models from different providers using unified request and response formats.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|