kimi-proxy 0.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/LICENSE +24 -0
- package/README.md +177 -0
- package/dist/adapters/anthropicAdapter.d.ts +138 -0
- package/dist/adapters/anthropicAdapter.js +184 -0
- package/dist/adapters/anthropicAdapter.js.map +1 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.js +228 -0
- package/dist/config.js.map +1 -0
- package/dist/core/converters/anthropicToOpenAI.d.ts +11 -0
- package/dist/core/converters/anthropicToOpenAI.js +18 -0
- package/dist/core/converters/anthropicToOpenAI.js.map +1 -0
- package/dist/core/converters/openaiPassthrough.d.ts +18 -0
- package/dist/core/converters/openaiPassthrough.js +794 -0
- package/dist/core/converters/openaiPassthrough.js.map +1 -0
- package/dist/core/converters/types.d.ts +14 -0
- package/dist/core/converters/types.js +16 -0
- package/dist/core/converters/types.js.map +1 -0
- package/dist/core/ensureToolCall.d.ts +24 -0
- package/dist/core/ensureToolCall.js +93 -0
- package/dist/core/ensureToolCall.js.map +1 -0
- package/dist/core/modelRegistry.d.ts +46 -0
- package/dist/core/modelRegistry.js +92 -0
- package/dist/core/modelRegistry.js.map +1 -0
- package/dist/core/pipeline.d.ts +17 -0
- package/dist/core/pipeline.js +109 -0
- package/dist/core/pipeline.js.map +1 -0
- package/dist/core/pipelineControl.d.ts +12 -0
- package/dist/core/pipelineControl.js +51 -0
- package/dist/core/pipelineControl.js.map +1 -0
- package/dist/core/providers/openRouterProvider.d.ts +52 -0
- package/dist/core/providers/openRouterProvider.js +158 -0
- package/dist/core/providers/openRouterProvider.js.map +1 -0
- package/dist/core/providers/openaiProvider.d.ts +22 -0
- package/dist/core/providers/openaiProvider.js +79 -0
- package/dist/core/providers/openaiProvider.js.map +1 -0
- package/dist/core/providers/types.d.ts +24 -0
- package/dist/core/providers/types.js +14 -0
- package/dist/core/providers/types.js.map +1 -0
- package/dist/core/providers/vertexProvider.d.ts +37 -0
- package/dist/core/providers/vertexProvider.js +167 -0
- package/dist/core/providers/vertexProvider.js.map +1 -0
- package/dist/core/syntheticResponse.d.ts +6 -0
- package/dist/core/syntheticResponse.js +36 -0
- package/dist/core/syntheticResponse.js.map +1 -0
- package/dist/core/transforms/request/ClampMaxTokensTransform.d.ts +7 -0
- package/dist/core/transforms/request/ClampMaxTokensTransform.js +29 -0
- package/dist/core/transforms/request/ClampMaxTokensTransform.js.map +1 -0
- package/dist/core/transforms/request/DisableStreamingTransform.d.ts +7 -0
- package/dist/core/transforms/request/DisableStreamingTransform.js +15 -0
- package/dist/core/transforms/request/DisableStreamingTransform.js.map +1 -0
- package/dist/core/transforms/request/EnsureToolCallRequestTransform.d.ts +12 -0
- package/dist/core/transforms/request/EnsureToolCallRequestTransform.js +120 -0
- package/dist/core/transforms/request/EnsureToolCallRequestTransform.js.map +1 -0
- package/dist/core/transforms/response/EnsureToolCallResponseTransform.d.ts +9 -0
- package/dist/core/transforms/response/EnsureToolCallResponseTransform.js +223 -0
- package/dist/core/transforms/response/EnsureToolCallResponseTransform.js.map +1 -0
- package/dist/core/transforms/response/KimiResponseTransform.d.ts +7 -0
- package/dist/core/transforms/response/KimiResponseTransform.js +32 -0
- package/dist/core/transforms/response/KimiResponseTransform.js.map +1 -0
- package/dist/core/types.d.ts +80 -0
- package/dist/core/types.js +18 -0
- package/dist/core/types.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/persistence/logStore.d.ts +43 -0
- package/dist/persistence/logStore.js +115 -0
- package/dist/persistence/logStore.js.map +1 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +385 -0
- package/dist/server.js.map +1 -0
- package/dist/services/kimiFixer.d.ts +13 -0
- package/dist/services/kimiFixer.js +160 -0
- package/dist/services/kimiFixer.js.map +1 -0
- package/dist/services/streaming.d.ts +9 -0
- package/dist/services/streaming.js +513 -0
- package/dist/services/streaming.js.map +1 -0
- package/dist/utils/envResolver.d.ts +26 -0
- package/dist/utils/envResolver.js +64 -0
- package/dist/utils/envResolver.js.map +1 -0
- package/dist/utils/ids.d.ts +1 -0
- package/dist/utils/ids.js +7 -0
- package/dist/utils/ids.js.map +1 -0
- package/dist/utils/logger.d.ts +3 -0
- package/dist/utils/logger.js +12 -0
- package/dist/utils/logger.js.map +1 -0
- package/frontend/dist/assets/JSONViewer-97138fd7.js +3 -0
- package/frontend/dist/assets/index-0e7c091b.css +1 -0
- package/frontend/dist/assets/index-4b354ce2.js +40 -0
- package/frontend/dist/index.html +22 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Solomon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
For more information, see https://opensource.org/licenses/MIT
|
|
24
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Kimi Proxy
|
|
2
|
+
|
|
3
|
+
> [!WARNING] ⚠️
|
|
4
|
+
> **Experimental**: This project is still in development. Use with caution in production.
|
|
5
|
+
|
|
6
|
+
Makes `kimi-k2-thinking` usable across multiple LLM providers by normalizing API formats, fixing tool call and thinking format issues, and optionally ensuring the model always uses a tool call for agentic workflows.
|
|
7
|
+
|
|
8
|
+
The proxy and transformation pipelines are built generically and can be easily extended to support any model and any provider.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
<details>
|
|
13
|
+
<summary><strong>Multi-provider proxy</strong> for <code>kimi-k2-thinking</code> and other models</summary>
|
|
14
|
+
|
|
15
|
+
Seamlessly route requests to OpenAI-compatible APIs, OpenRouter, or Vertex AI using a unified client model name.
|
|
16
|
+
|
|
17
|
+
</details>
|
|
18
|
+
|
|
19
|
+
<details>
|
|
20
|
+
<summary><strong>Format fixes</strong> for tool calls and thinking blocks</summary>
|
|
21
|
+
|
|
22
|
+
For some providers, kimi-k2-thinking returns tool calls and thinking content in non-standard formats. The proxy normalizes these to the standard Anthropic format that clients expect.
|
|
23
|
+
|
|
24
|
+
**Example: Tool call normalization from content**
|
|
25
|
+
|
|
26
|
+
What the kimi-k2 provider returns (tool calls embedded in content with `<|tool_call_begin|>` markers):
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"content": "Let me search for that. <|tool_call_begin|> functions.lookup:42 <|tool_call_argument_begin|> {\"term\":\"express\"} <|tool_call_begin|> "
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
What clients receive (normalized):
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"content": "Let me search for that.",
|
|
39
|
+
"tool_calls": [
|
|
40
|
+
{
|
|
41
|
+
"id": "42",
|
|
42
|
+
"type": "function",
|
|
43
|
+
"function": {
|
|
44
|
+
"name": "lookup",
|
|
45
|
+
"arguments": "{\"term\":\"express\"}"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"finish_reason": "tool_calls"
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**Example: Thinking tags extraction and cleanup**
|
|
54
|
+
|
|
55
|
+
What kimi-k2 returns:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
(no content)(no content) Let me break down... </think> The answer is 42.
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
What clients receive:
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"content": "The answer is 42.",
|
|
66
|
+
"thinking": "Let me break down..."
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
</details>
|
|
71
|
+
|
|
72
|
+
<details>
|
|
73
|
+
<summary><strong>Tool call enforcement (optional)</strong> for reliable agentic workflows</summary>
|
|
74
|
+
|
|
75
|
+
Enable with `ensure_tool_call: true` in model config. The proxy detects missing tool calls and re-prompts the model with a reminder.
|
|
76
|
+
|
|
77
|
+
**Example enforcement flow:**
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
System: You are a helpful assistant with access to tools.
|
|
81
|
+
Always reply with at least one tool call so the client can continue.
|
|
82
|
+
|
|
83
|
+
User: What's the weather in SF?
|
|
84
|
+
|
|
85
|
+
Assistant: Let me check that for you.
|
|
86
|
+
|
|
87
|
+
System: Reminder: The client will not continue unless you reply with a tool call.
|
|
88
|
+
|
|
89
|
+
Assistant: {
|
|
90
|
+
"tool_calls": [{
|
|
91
|
+
"id": "get_weather:0",
|
|
92
|
+
"type": "function",
|
|
93
|
+
"function": {
|
|
94
|
+
"name": "get_weather",
|
|
95
|
+
"arguments": "{\"location\": \"SF\"}"
|
|
96
|
+
}
|
|
97
|
+
}]
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
</details>
|
|
102
|
+
|
|
103
|
+
<details>
|
|
104
|
+
<summary><strong>Request/response logging</strong> with web dashboard</summary>
|
|
105
|
+
|
|
106
|
+
All requests and responses are logged to SQLite and viewable through a built-in web dashboard at the root path.
|
|
107
|
+
|
|
108
|
+
</details>
|
|
109
|
+
|
|
110
|
+
<details>
|
|
111
|
+
<summary><strong>Load balancing</strong> with multiple strategies</summary>
|
|
112
|
+
|
|
113
|
+
Distribute traffic across providers using round-robin, weighted random, random, or first strategies.
|
|
114
|
+
|
|
115
|
+
</details>
|
|
116
|
+
|
|
117
|
+
- **Extensible architecture** for adding new models and providers
|
|
118
|
+
- **Provider support**: OpenAI-compatible APIs, OpenRouter, Vertex AI
|
|
119
|
+
|
|
120
|
+
## Quick Start
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pnpm install
|
|
124
|
+
cp .env.example .env
|
|
125
|
+
cp model-config.example.yaml model-config.yaml
|
|
126
|
+
# Edit .env and model-config.yaml with your provider keys and models
|
|
127
|
+
pnpm run dev
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The API runs on `http://127.0.0.1:8000` and serves the dashboard at `/`.
|
|
131
|
+
|
|
132
|
+
## Configuration
|
|
133
|
+
|
|
134
|
+
### Providers
|
|
135
|
+
|
|
136
|
+
Set environment variables in `.env`:
|
|
137
|
+
|
|
138
|
+
- **Generic OpenAI**: `OPENAI_BASE_URL`, `OPENAI_API_KEY`
|
|
139
|
+
- **OpenRouter**: `OPENROUTER_API_KEY`, `OPENROUTER_PROVIDERS` (optional), `OPENROUTER_ORDER` (optional)
|
|
140
|
+
- **Vertex AI**: `VERTEX_PROJECT_ID`, `VERTEX_LOCATION`, `GOOGLE_APPLICATION_CREDENTIALS`
|
|
141
|
+
|
|
142
|
+
### Models
|
|
143
|
+
|
|
144
|
+
Edit `model-config.yaml` to map client model names to upstream providers:
|
|
145
|
+
|
|
146
|
+
```yaml
|
|
147
|
+
default_strategy: round_robin
|
|
148
|
+
models:
|
|
149
|
+
- name: kimi-k2-thinking
|
|
150
|
+
provider: vertex
|
|
151
|
+
model: moonshotai/kimi-k2-thinking-maas
|
|
152
|
+
# Optional: enforce tool call consistency for reliable agentic workflows
|
|
153
|
+
ensure_tool_call: true
|
|
154
|
+
- name: kimi-k2-thinking
|
|
155
|
+
provider: openrouter
|
|
156
|
+
model: moonshot-ai/kimi-k2-thinking
|
|
157
|
+
weight: 2
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Dashboard
|
|
161
|
+
|
|
162
|
+
The web dashboard shows request/response logs and metrics. Access it at the root path when running the proxy.
|
|
163
|
+
|
|
164
|
+
## Development
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
pnpm run dev # Run with hot reload
|
|
168
|
+
pnpm run test # Run unit tests
|
|
169
|
+
pnpm run build # TypeScript build
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Docker
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
docker compose up --build -d # Production stack with web dashboard
|
|
176
|
+
docker compose -f docker-compose.dev.yml watch # Development with hot reload
|
|
177
|
+
```
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { JsonObject, JsonValue } from "../core/types.js";
|
|
2
|
+
export interface AnthropicContentBlock extends JsonObject {
|
|
3
|
+
type: "thinking" | "text" | "tool_use" | "tool_result";
|
|
4
|
+
thinking?: string;
|
|
5
|
+
text?: string;
|
|
6
|
+
signature?: string;
|
|
7
|
+
id?: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
input?: JsonObject;
|
|
10
|
+
tool_use_id?: string;
|
|
11
|
+
content?: string | Array<JsonObject>;
|
|
12
|
+
is_error?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface AnthropicToolDefinition extends JsonObject {
|
|
15
|
+
name: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
input_schema: JsonObject;
|
|
18
|
+
}
|
|
19
|
+
export interface AnthropicCustomToolChoice extends JsonObject {
|
|
20
|
+
type: string;
|
|
21
|
+
name?: string;
|
|
22
|
+
}
|
|
23
|
+
export type AnthropicToolChoice = {
|
|
24
|
+
type: "auto";
|
|
25
|
+
} | {
|
|
26
|
+
type: "tool";
|
|
27
|
+
name: string;
|
|
28
|
+
} | AnthropicCustomToolChoice;
|
|
29
|
+
export interface AnthropicMessageRequest extends JsonObject {
|
|
30
|
+
model: string;
|
|
31
|
+
messages: Array<{
|
|
32
|
+
role: string;
|
|
33
|
+
content: string | AnthropicContentBlock[];
|
|
34
|
+
}>;
|
|
35
|
+
system?: string | AnthropicContentBlock[];
|
|
36
|
+
tools?: AnthropicToolDefinition[];
|
|
37
|
+
tool_choice?: AnthropicToolChoice;
|
|
38
|
+
stream?: boolean;
|
|
39
|
+
temperature?: number;
|
|
40
|
+
max_tokens?: number;
|
|
41
|
+
}
|
|
42
|
+
export interface AnthropicMessageResponse extends JsonObject {
|
|
43
|
+
id: string;
|
|
44
|
+
type: "message";
|
|
45
|
+
role: "assistant";
|
|
46
|
+
content: AnthropicContentBlock[];
|
|
47
|
+
model: string;
|
|
48
|
+
stop_reason: string | null;
|
|
49
|
+
stop_sequence: string | null;
|
|
50
|
+
usage: {
|
|
51
|
+
input_tokens: number;
|
|
52
|
+
output_tokens: number;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export interface OpenAIChatMessagePart extends JsonObject {
|
|
56
|
+
type: string;
|
|
57
|
+
text?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface OpenAIChatRequest extends JsonObject {
|
|
60
|
+
model: string;
|
|
61
|
+
messages: Array<{
|
|
62
|
+
role: string;
|
|
63
|
+
content: string | OpenAIChatMessagePart[];
|
|
64
|
+
tool_calls?: OpenAIToolCall[];
|
|
65
|
+
tool_call_id?: string;
|
|
66
|
+
}>;
|
|
67
|
+
stream?: boolean;
|
|
68
|
+
temperature?: number;
|
|
69
|
+
max_tokens?: number;
|
|
70
|
+
tools?: OpenAIToolDefinition[];
|
|
71
|
+
tool_choice?: OpenAIToolChoice;
|
|
72
|
+
}
|
|
73
|
+
export interface OpenAIUsage extends JsonObject {
|
|
74
|
+
prompt_tokens?: number;
|
|
75
|
+
completion_tokens?: number;
|
|
76
|
+
total_tokens?: number;
|
|
77
|
+
}
|
|
78
|
+
export interface OpenAIReasoningBlock extends JsonObject {
|
|
79
|
+
text?: string;
|
|
80
|
+
thinking?: string;
|
|
81
|
+
signature?: string;
|
|
82
|
+
}
|
|
83
|
+
export interface OpenAIFunctionCall extends JsonObject {
|
|
84
|
+
name: string;
|
|
85
|
+
arguments: string;
|
|
86
|
+
}
|
|
87
|
+
export interface OpenAIToolCall extends JsonObject {
|
|
88
|
+
id?: string;
|
|
89
|
+
type: "function";
|
|
90
|
+
function?: OpenAIFunctionCall;
|
|
91
|
+
}
|
|
92
|
+
export interface OpenAIChatMessage extends JsonObject {
|
|
93
|
+
role?: string;
|
|
94
|
+
content?: string | OpenAIChatMessagePart[];
|
|
95
|
+
reasoning_content?: string | OpenAIReasoningBlock[];
|
|
96
|
+
tool_calls?: OpenAIToolCall[];
|
|
97
|
+
tool_call_id?: string;
|
|
98
|
+
reasoning_summary?: Array<{
|
|
99
|
+
type: string;
|
|
100
|
+
text: string;
|
|
101
|
+
}> | string;
|
|
102
|
+
metadata?: JsonObject;
|
|
103
|
+
refusal?: {
|
|
104
|
+
text?: string;
|
|
105
|
+
reason?: string;
|
|
106
|
+
content?: JsonValue;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export interface OpenAIToolDefinition extends JsonObject {
|
|
110
|
+
type: "function";
|
|
111
|
+
function: {
|
|
112
|
+
name: string;
|
|
113
|
+
description?: string;
|
|
114
|
+
parameters?: JsonObject;
|
|
115
|
+
};
|
|
116
|
+
strict?: boolean;
|
|
117
|
+
format?: string;
|
|
118
|
+
}
|
|
119
|
+
export type OpenAIToolChoice = "auto" | {
|
|
120
|
+
type: "function";
|
|
121
|
+
function: {
|
|
122
|
+
name: string;
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
export interface OpenAIChatResponse extends JsonObject {
|
|
126
|
+
id: string;
|
|
127
|
+
object: string;
|
|
128
|
+
created: number;
|
|
129
|
+
model: string;
|
|
130
|
+
choices: Array<{
|
|
131
|
+
message: OpenAIChatMessage;
|
|
132
|
+
finish_reason: string;
|
|
133
|
+
index: number;
|
|
134
|
+
}>;
|
|
135
|
+
usage?: OpenAIUsage;
|
|
136
|
+
}
|
|
137
|
+
export declare function anthropicToOpenAIRequest(request: AnthropicMessageRequest): OpenAIChatRequest;
|
|
138
|
+
export declare function openaiToAnthropicResponse(openaiResp: OpenAIChatResponse): AnthropicMessageResponse;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
export function anthropicToOpenAIRequest(request) {
|
|
3
|
+
const openaiMessages = [];
|
|
4
|
+
if (request.system) {
|
|
5
|
+
if (typeof request.system === "string") {
|
|
6
|
+
openaiMessages.push({ role: "system", content: request.system });
|
|
7
|
+
}
|
|
8
|
+
else if (Array.isArray(request.system)) {
|
|
9
|
+
const content = request.system
|
|
10
|
+
.filter((block) => block.type === "text")
|
|
11
|
+
.map((block) => block.text ?? "")
|
|
12
|
+
.join("\n");
|
|
13
|
+
openaiMessages.push({ role: "system", content });
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
for (const msg of request.messages ?? []) {
|
|
17
|
+
const { role, content } = msg;
|
|
18
|
+
if (Array.isArray(content)) {
|
|
19
|
+
const textBlocks = content.filter((b) => b.type === "text");
|
|
20
|
+
const textContent = textBlocks.map((b) => b.text ?? "").join("");
|
|
21
|
+
if (role === "assistant") {
|
|
22
|
+
const toolUses = content.filter((b) => b.type === "tool_use");
|
|
23
|
+
const toolCalls = toolUses.map((u) => ({
|
|
24
|
+
id: u.id,
|
|
25
|
+
type: "function",
|
|
26
|
+
function: {
|
|
27
|
+
name: u.name ?? "",
|
|
28
|
+
arguments: JSON.stringify(u.input ?? {}),
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
const message = {
|
|
32
|
+
role,
|
|
33
|
+
content: textContent,
|
|
34
|
+
};
|
|
35
|
+
if (toolCalls.length > 0) {
|
|
36
|
+
message.tool_calls = toolCalls;
|
|
37
|
+
}
|
|
38
|
+
openaiMessages.push(message);
|
|
39
|
+
}
|
|
40
|
+
else if (role === "user") {
|
|
41
|
+
const toolResults = content.filter((b) => b.type === "tool_result");
|
|
42
|
+
// 1. Emit tool results as separate 'tool' messages
|
|
43
|
+
for (const res of toolResults) {
|
|
44
|
+
let resultContent = "";
|
|
45
|
+
if (typeof res.content === "string") {
|
|
46
|
+
resultContent = res.content;
|
|
47
|
+
}
|
|
48
|
+
else if (Array.isArray(res.content)) {
|
|
49
|
+
resultContent = res.content
|
|
50
|
+
.map((b) => typeof b.text === "string" ? b.text : JSON.stringify(b))
|
|
51
|
+
.join("");
|
|
52
|
+
}
|
|
53
|
+
openaiMessages.push({
|
|
54
|
+
role: "tool", // OpenAI uses 'tool' role for function results
|
|
55
|
+
content: resultContent,
|
|
56
|
+
tool_call_id: res.tool_use_id,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
// 2. Emit the user text as a 'user' message
|
|
60
|
+
if (textContent || toolResults.length === 0) {
|
|
61
|
+
if (textContent) {
|
|
62
|
+
openaiMessages.push({ role, content: textContent });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
openaiMessages.push({ role, content: textContent });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
openaiMessages.push({ role, content });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const tools = (request.tools ?? []).map((tool) => ({
|
|
75
|
+
type: "function",
|
|
76
|
+
function: {
|
|
77
|
+
name: tool.name,
|
|
78
|
+
...(tool.description ? { description: tool.description } : {}),
|
|
79
|
+
parameters: tool.input_schema,
|
|
80
|
+
},
|
|
81
|
+
}));
|
|
82
|
+
const openaiReq = {
|
|
83
|
+
model: request.model,
|
|
84
|
+
messages: openaiMessages,
|
|
85
|
+
stream: request.stream ?? false,
|
|
86
|
+
temperature: request.temperature ?? 1,
|
|
87
|
+
max_tokens: request.max_tokens ?? 4096,
|
|
88
|
+
};
|
|
89
|
+
if (tools.length) {
|
|
90
|
+
openaiReq.tools = tools;
|
|
91
|
+
if (request.tool_choice) {
|
|
92
|
+
if (request.tool_choice.type === "auto") {
|
|
93
|
+
openaiReq.tool_choice = "auto";
|
|
94
|
+
}
|
|
95
|
+
else if (request.tool_choice.type === "tool" &&
|
|
96
|
+
typeof request.tool_choice.name === "string") {
|
|
97
|
+
openaiReq.tool_choice = {
|
|
98
|
+
type: "function",
|
|
99
|
+
function: { name: request.tool_choice.name },
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return openaiReq;
|
|
105
|
+
}
|
|
106
|
+
export function openaiToAnthropicResponse(openaiResp) {
|
|
107
|
+
const [choice] = openaiResp.choices;
|
|
108
|
+
const message = choice.message ?? {};
|
|
109
|
+
const contentBlocks = [];
|
|
110
|
+
const reasoning = message.reasoning_content;
|
|
111
|
+
const reasoningSignature = Buffer.from("signature_placeholder").toString("base64");
|
|
112
|
+
if (typeof reasoning === "string" && reasoning.trim()) {
|
|
113
|
+
contentBlocks.push({
|
|
114
|
+
type: "thinking",
|
|
115
|
+
thinking: reasoning,
|
|
116
|
+
signature: reasoningSignature,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
else if (Array.isArray(reasoning)) {
|
|
120
|
+
for (const block of reasoning) {
|
|
121
|
+
const thought = typeof block.thinking === "string"
|
|
122
|
+
? block.thinking
|
|
123
|
+
: typeof block.text === "string"
|
|
124
|
+
? block.text
|
|
125
|
+
: undefined;
|
|
126
|
+
if (thought) {
|
|
127
|
+
contentBlocks.push({
|
|
128
|
+
type: "thinking",
|
|
129
|
+
thinking: thought,
|
|
130
|
+
signature: block.signature ?? reasoningSignature,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const textContent = message.content;
|
|
136
|
+
if (typeof textContent === "string" && textContent.trim()) {
|
|
137
|
+
contentBlocks.push({ type: "text", text: textContent.trim() });
|
|
138
|
+
}
|
|
139
|
+
else if (Array.isArray(textContent)) {
|
|
140
|
+
for (const part of textContent) {
|
|
141
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
142
|
+
contentBlocks.push({ type: "text", text: part.text });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const toolCalls = message.tool_calls;
|
|
147
|
+
let stopReason = "end_turn";
|
|
148
|
+
if (Array.isArray(toolCalls) && toolCalls.length) {
|
|
149
|
+
stopReason = "tool_use";
|
|
150
|
+
for (const toolCall of toolCalls) {
|
|
151
|
+
let parsedArgs;
|
|
152
|
+
try {
|
|
153
|
+
parsedArgs = JSON.parse(toolCall.function?.arguments ?? "{}");
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
parsedArgs = { raw_arguments: toolCall.function?.arguments };
|
|
157
|
+
}
|
|
158
|
+
contentBlocks.push({
|
|
159
|
+
type: "tool_use",
|
|
160
|
+
id: toolCall.id,
|
|
161
|
+
name: toolCall.function?.name,
|
|
162
|
+
input: parsedArgs,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let responseId = openaiResp.id ?? "";
|
|
167
|
+
if (!responseId.startsWith("msg_")) {
|
|
168
|
+
responseId = `msg_${responseId}`;
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
id: responseId,
|
|
172
|
+
type: "message",
|
|
173
|
+
role: "assistant",
|
|
174
|
+
content: contentBlocks,
|
|
175
|
+
model: openaiResp.model,
|
|
176
|
+
stop_reason: stopReason,
|
|
177
|
+
stop_sequence: null,
|
|
178
|
+
usage: {
|
|
179
|
+
input_tokens: openaiResp.usage?.prompt_tokens ?? 0,
|
|
180
|
+
output_tokens: openaiResp.usage?.completion_tokens ?? 0,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=anthropicAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropicAdapter.js","sourceRoot":"","sources":["../../src/adapters/anthropicAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AA8IrC,MAAM,UAAU,wBAAwB,CACtC,OAAgC;IAEhC,MAAM,cAAc,GAAkC,EAAE,CAAC;IAEzD,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;QACnB,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YACvC,cAAc,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACnE,CAAC;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YACzC,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM;iBAC3B,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC;iBACxC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;iBAChC,IAAI,CAAC,IAAI,CAAC,CAAC;YACd,cAAc,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC;QACzC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC;QAE9B,IAAI,KA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rC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACnC,UAAU,GAAG,OAAO,UAAU,EAAE,CAAC;IACnC,CAAC;IAED,OAAO;QACL,EAAE,EAAE,UAAU;QACd,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,aAAa;QACtB,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,WAAW,EAAE,UAAU;QACvB,aAAa,EAAE,IAAI;QACnB,KAAK,EAAE;YACL,YAAY,EAAE,UAAU,CAAC,KAAK,EAAE,aAAa,IAAI,CAAC;YAClD,aAAa,EAAE,UAAU,CAAC,KAAK,EAAE,iBAAiB,IAAI,CAAC;SACxD;KACF,CAAC;AACJ,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { OpenAIConfig } from "./core/providers/openaiProvider.js";
|
|
2
|
+
import { OpenRouterConfig } from "./core/providers/openRouterProvider.js";
|
|
3
|
+
import { VertexConfig } from "./core/providers/vertexProvider.js";
|
|
4
|
+
import { LoadBalancingStrategy, ModelDefinition } from "./core/modelRegistry.js";
|
|
5
|
+
export interface AppConfig {
|
|
6
|
+
server: {
|
|
7
|
+
host: string;
|
|
8
|
+
port: number;
|
|
9
|
+
};
|
|
10
|
+
logging: {
|
|
11
|
+
dbPath: string;
|
|
12
|
+
};
|
|
13
|
+
streaming: {
|
|
14
|
+
delay: number;
|
|
15
|
+
chunkSize: number;
|
|
16
|
+
};
|
|
17
|
+
providers: {
|
|
18
|
+
openai?: OpenAIConfig;
|
|
19
|
+
openrouter?: OpenRouterConfig;
|
|
20
|
+
vertex?: VertexConfig;
|
|
21
|
+
};
|
|
22
|
+
models: {
|
|
23
|
+
definitions: ModelDefinition[];
|
|
24
|
+
defaultStrategy: LoadBalancingStrategy;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export declare function loadConfig(): AppConfig;
|