llm-proxy 1.0.10 → 1.0.12
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.
|
@@ -4,6 +4,9 @@ exports.OutputFormatAdapter = void 0;
|
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
class OutputFormatAdapter {
|
|
6
6
|
static adaptResponse(response, provider) {
|
|
7
|
+
if (!response) {
|
|
8
|
+
throw new Error("Response object is undefined");
|
|
9
|
+
}
|
|
7
10
|
switch (provider) {
|
|
8
11
|
case types_1.Providers.OPENAI:
|
|
9
12
|
return response;
|
|
@@ -14,6 +17,23 @@ class OutputFormatAdapter {
|
|
|
14
17
|
}
|
|
15
18
|
}
|
|
16
19
|
static adaptAnthropicBedrockResponse(response) {
|
|
20
|
+
// Validate required fields
|
|
21
|
+
if (!(response === null || response === void 0 ? void 0 : response.id)) {
|
|
22
|
+
throw new Error("Missing required field: id");
|
|
23
|
+
}
|
|
24
|
+
if (!(response === null || response === void 0 ? void 0 : response.model)) {
|
|
25
|
+
throw new Error("Missing required field: model");
|
|
26
|
+
}
|
|
27
|
+
if (!(response === null || response === void 0 ? void 0 : response.usage)) {
|
|
28
|
+
throw new Error("Missing required field: usage");
|
|
29
|
+
}
|
|
30
|
+
// Validate content array
|
|
31
|
+
if (!response.content) {
|
|
32
|
+
throw new Error("Missing required field: content");
|
|
33
|
+
}
|
|
34
|
+
if (!Array.isArray(response.content)) {
|
|
35
|
+
throw new Error("Content must be an array");
|
|
36
|
+
}
|
|
17
37
|
const openAIResponse = {
|
|
18
38
|
id: response.id,
|
|
19
39
|
object: "text_completion",
|
|
@@ -26,12 +46,13 @@ class OutputFormatAdapter {
|
|
|
26
46
|
content: this.extractContent(contentBlock),
|
|
27
47
|
},
|
|
28
48
|
logprobs: null,
|
|
29
|
-
finish_reason: response.stop_reason,
|
|
49
|
+
finish_reason: response.stop_reason || "stop",
|
|
30
50
|
})),
|
|
31
51
|
usage: {
|
|
32
|
-
prompt_tokens: response.usage.input_tokens,
|
|
33
|
-
completion_tokens: response.usage.output_tokens,
|
|
34
|
-
total_tokens: response.usage.input_tokens +
|
|
52
|
+
prompt_tokens: response.usage.input_tokens || 0,
|
|
53
|
+
completion_tokens: response.usage.output_tokens || 0,
|
|
54
|
+
total_tokens: (response.usage.input_tokens || 0) +
|
|
55
|
+
(response.usage.output_tokens || 0),
|
|
35
56
|
prompt_tokens_details: { cached_tokens: 0 },
|
|
36
57
|
completion_tokens_details: { reasoning_tokens: 0 },
|
|
37
58
|
},
|
|
@@ -40,6 +61,9 @@ class OutputFormatAdapter {
|
|
|
40
61
|
return openAIResponse;
|
|
41
62
|
}
|
|
42
63
|
static mapRole(content) {
|
|
64
|
+
if (!(content === null || content === void 0 ? void 0 : content.type)) {
|
|
65
|
+
throw new Error("Content block missing required field: type");
|
|
66
|
+
}
|
|
43
67
|
switch (content.type) {
|
|
44
68
|
case types_1.BedrockAnthropicContentType.TOOL_USE:
|
|
45
69
|
case types_1.BedrockAnthropicContentType.TOOL_RESULT:
|
|
@@ -47,19 +71,34 @@ class OutputFormatAdapter {
|
|
|
47
71
|
case types_1.BedrockAnthropicContentType.TEXT:
|
|
48
72
|
return "assistant";
|
|
49
73
|
default:
|
|
50
|
-
|
|
74
|
+
throw new Error(`Unsupported content type: ${content.type}`);
|
|
51
75
|
}
|
|
52
76
|
}
|
|
53
77
|
static extractContent(content) {
|
|
78
|
+
if (!(content === null || content === void 0 ? void 0 : content.type)) {
|
|
79
|
+
throw new Error("Content block missing required field: type");
|
|
80
|
+
}
|
|
54
81
|
switch (content.type) {
|
|
55
82
|
case types_1.BedrockAnthropicContentType.TEXT:
|
|
56
|
-
|
|
83
|
+
const textContent = content;
|
|
84
|
+
if (!textContent.text) {
|
|
85
|
+
throw new Error("Text content missing required field: text");
|
|
86
|
+
}
|
|
87
|
+
return textContent.text;
|
|
57
88
|
case types_1.BedrockAnthropicContentType.TOOL_RESULT:
|
|
58
|
-
|
|
89
|
+
const toolResultContent = content;
|
|
90
|
+
if (!toolResultContent.content) {
|
|
91
|
+
throw new Error("Tool result content missing required field: content");
|
|
92
|
+
}
|
|
93
|
+
return toolResultContent.content;
|
|
59
94
|
case types_1.BedrockAnthropicContentType.TOOL_USE:
|
|
60
|
-
|
|
95
|
+
const toolUseContent = content;
|
|
96
|
+
if (!toolUseContent.id) {
|
|
97
|
+
throw new Error("Tool use content missing required field: id");
|
|
98
|
+
}
|
|
99
|
+
return toolUseContent.id;
|
|
61
100
|
default:
|
|
62
|
-
|
|
101
|
+
throw new Error(`Unsupported content type: ${content.type}`);
|
|
63
102
|
}
|
|
64
103
|
}
|
|
65
104
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OutputFormatAdapter.js","sourceRoot":"","sources":["../../src/middleware/OutputFormatAdapter.ts"],"names":[],"mappings":";;;AAAA,oCAUkB;AAElB,MAAa,mBAAmB;IAC9B,MAAM,CAAC,aAAa,CAAC,QAAa,EAAE,QAAmB;QACrD,QAAQ,QAAQ,EAAE,CAAC;YACjB,KAAK,iBAAS,CAAC,MAAM;gBACnB,OAAO,QAA0B,CAAC;YAEpC,KAAK,iBAAS,CAAC,iBAAiB;gBAC9B,OAAO,IAAI,CAAC,6BAA6B,CACvC,QAAoC,CACrC,CAAC;YAEJ;gBACE,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAC;QACzD,CAAC;IACH,CAAC;IAEO,MAAM,CAAC,6BAA6B,CAC1C,QAAkC;QAElC,MAAM,cAAc,GAAmB;YACrC,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,MAAM,EAAE,iBAAiB;YACzB,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE;YACnB,KAAK,EAAE,QAAQ,CAAC,KAAK;YACrB,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,YAAY,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;gBACtD,KAAK;gBACL,OAAO,EAAE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CAAC,QAAQ;gBACvC,MAAM,cAAc,GAAG,OAAyC,CAAC;gBACjE,IAAI,CAAC,cAAc,CAAC,EAAE,EAAE,CAAC;oBACvB,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;gBACjE,CAAC;gBACD,OAAO,cAAc,CAAC,EAAE,CAAC;YAE3B;gBACE,MAAM,IAAI,KAAK,CAAC,6BAA6B,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;CACF;AAxHD,kDAwHC"}
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llm-proxy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
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",
|