@cldmv/llm-translator 0.1.0 → 0.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/README.md +176 -7
- package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +17 -0
- package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +2 -0
- package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +2 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +6 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +2 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +2 -0
- package/dist/api/detect.mjs +2 -0
- package/dist/api/driver/anthropic/detect.mjs +2 -0
- package/dist/api/driver/anthropic/info.mjs +2 -0
- package/dist/api/driver/anthropic/model/list.mjs +2 -0
- package/dist/api/driver/anthropic/settings.mjs +2 -0
- package/dist/api/driver/anthropic/token/count.mjs +2 -0
- package/dist/api/driver/compatible/detect.mjs +2 -0
- package/dist/api/driver/compatible/info.mjs +2 -0
- package/dist/api/driver/compatible/model/list.mjs +2 -0
- package/dist/api/driver/compatible/request.mjs +2 -0
- package/dist/api/driver/compatible/settings.mjs +2 -0
- package/dist/api/driver/openai/detect.mjs +2 -0
- package/dist/api/driver/openai/info.mjs +2 -0
- package/dist/api/driver/openai/model/list.mjs +2 -0
- package/dist/api/driver/openai/request.mjs +2 -0
- package/dist/api/driver/openai/settings.mjs +2 -0
- package/dist/api/driver/vllm/detect.mjs +2 -0
- package/dist/api/driver/vllm/error.mjs +2 -0
- package/dist/api/driver/vllm/info.mjs +2 -0
- package/dist/api/driver/vllm/model/list.mjs +2 -0
- package/dist/api/driver/vllm/probe.mjs +2 -0
- package/dist/api/driver/vllm/request.mjs +2 -0
- package/dist/api/driver/vllm/settings.mjs +2 -0
- package/dist/api/driver/vllm/token/count.mjs +2 -0
- package/dist/api/error/build.mjs +2 -0
- package/dist/api/error/error.mjs +2 -0
- package/dist/api/model/list.mjs +2 -0
- package/dist/api/request.mjs +3 -0
- package/dist/api/response.mjs +2 -0
- package/dist/api/send.mjs +2 -0
- package/dist/api/server/plugin.mjs +2 -0
- package/dist/api/server/start.mjs +2 -0
- package/dist/api/server/stop.mjs +2 -0
- package/dist/api/sse/encode.mjs +4 -0
- package/dist/api/sse/parse.mjs +2 -0
- package/dist/api/sse/ping.mjs +2 -0
- package/dist/api/sse/write.mjs +2 -0
- package/dist/api/status.mjs +2 -0
- package/dist/api/stream.mjs +2 -0
- package/dist/api/token/count.mjs +2 -0
- package/dist/bin/llm-translator.mjs +3 -0
- package/dist/index.mjs +1 -1
- package/dist/lib/arguments.mjs +2 -0
- package/dist/lib/capture.mjs +2 -0
- package/dist/lib/cli.mjs +29 -0
- package/dist/lib/config.mjs +2 -0
- package/dist/lib/documents.mjs +12 -0
- package/dist/lib/errors.mjs +2 -0
- package/dist/lib/estimate.mjs +2 -0
- package/dist/lib/finish.mjs +2 -0
- package/dist/lib/http.mjs +2 -0
- package/dist/lib/ids.mjs +2 -0
- package/dist/lib/images.mjs +2 -0
- package/dist/lib/models.mjs +2 -0
- package/dist/lib/normalize.mjs +2 -0
- package/dist/lib/passthrough.mjs +2 -0
- package/dist/lib/pdf.mjs +2 -0
- package/dist/lib/ping.mjs +2 -0
- package/dist/lib/route.mjs +2 -0
- package/dist/lib/routes.mjs +2 -0
- package/dist/lib/runtime.mjs +2 -0
- package/dist/lib/schema.mjs +2 -0
- package/dist/lib/settings.mjs +2 -0
- package/dist/lib/source.mjs +2 -0
- package/dist/lib/sse.mjs +10 -0
- package/dist/lib/thinking.mjs +2 -0
- package/dist/lib/transport.mjs +2 -0
- package/dist/lib/upstream-error.mjs +2 -0
- package/dist/lib/warn.mjs +2 -0
- package/package.json +30 -5
- package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +28 -0
- package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +24 -0
- package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +29 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +30 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +28 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +38 -0
- package/types/src/api/detect.d.mts +21 -0
- package/types/src/api/driver/anthropic/detect.d.mts +26 -0
- package/types/src/api/driver/anthropic/info.d.mts +30 -0
- package/types/src/api/driver/anthropic/model/list.d.mts +20 -0
- package/types/src/api/driver/anthropic/settings.d.mts +23 -0
- package/types/src/api/driver/anthropic/token/count.d.mts +20 -0
- package/types/src/api/driver/compatible/detect.d.mts +23 -0
- package/types/src/api/driver/compatible/info.d.mts +29 -0
- package/types/src/api/driver/compatible/model/list.d.mts +20 -0
- package/types/src/api/driver/compatible/request.d.mts +20 -0
- package/types/src/api/driver/compatible/settings.d.mts +23 -0
- package/types/src/api/driver/openai/detect.d.mts +26 -0
- package/types/src/api/driver/openai/info.d.mts +29 -0
- package/types/src/api/driver/openai/model/list.d.mts +20 -0
- package/types/src/api/driver/openai/request.d.mts +20 -0
- package/types/src/api/driver/openai/settings.d.mts +23 -0
- package/types/src/api/driver/vllm/detect.d.mts +20 -0
- package/types/src/api/driver/vllm/error.d.mts +28 -0
- package/types/src/api/driver/vllm/info.d.mts +29 -0
- package/types/src/api/driver/vllm/model/list.d.mts +20 -0
- package/types/src/api/driver/vllm/probe.d.mts +39 -0
- package/types/src/api/driver/vllm/request.d.mts +22 -0
- package/types/src/api/driver/vllm/settings.d.mts +22 -0
- package/types/src/api/driver/vllm/token/count.d.mts +20 -0
- package/types/src/api/error/build.d.mts +26 -0
- package/types/src/api/error/error.d.mts +29 -0
- package/types/src/api/model/list.d.mts +21 -0
- package/types/src/api/request.d.mts +35 -0
- package/types/src/api/response.d.mts +22 -0
- package/types/src/api/send.d.mts +32 -0
- package/types/src/api/server/plugin.d.mts +24 -0
- package/types/src/api/server/start.d.mts +26 -0
- package/types/src/api/server/stop.d.mts +19 -0
- package/types/src/api/sse/encode.d.mts +26 -0
- package/types/src/api/sse/parse.d.mts +24 -0
- package/types/src/api/sse/ping.d.mts +30 -0
- package/types/src/api/sse/write.d.mts +26 -0
- package/types/src/api/status.d.mts +22 -0
- package/types/src/api/stream.d.mts +25 -0
- package/types/src/api/token/count.d.mts +24 -0
- package/types/src/index.d.mts +61 -4
- package/types/src/lib/arguments.d.mts +30 -0
- package/types/src/lib/capture.d.mts +28 -0
- package/types/src/lib/cli.d.mts +49 -0
- package/types/src/lib/config.d.mts +65 -0
- package/types/src/lib/documents.d.mts +43 -0
- package/types/src/lib/errors.d.mts +144 -0
- package/types/src/lib/estimate.d.mts +24 -0
- package/types/src/lib/finish.d.mts +73 -0
- package/types/src/lib/http.d.mts +58 -0
- package/types/src/lib/ids.d.mts +55 -0
- package/types/src/lib/images.d.mts +39 -0
- package/types/src/lib/models.d.mts +25 -0
- package/types/src/lib/normalize.d.mts +37 -0
- package/types/src/lib/passthrough.d.mts +22 -0
- package/types/src/lib/pdf.d.mts +67 -0
- package/types/src/lib/ping.d.mts +24 -0
- package/types/src/lib/route.d.mts +38 -0
- package/types/src/lib/routes.d.mts +22 -0
- package/types/src/lib/runtime.d.mts +24 -0
- package/types/src/lib/schema.d.mts +22 -0
- package/types/src/lib/settings.d.mts +175 -0
- package/types/src/lib/source.d.mts +32 -0
- package/types/src/lib/sse.d.mts +72 -0
- package/types/src/lib/thinking.d.mts +60 -0
- package/types/src/lib/transport.d.mts +31 -0
- package/types/src/lib/upstream-error.d.mts +57 -0
- package/types/src/lib/warn.d.mts +26 -0
package/README.md
CHANGED
|
@@ -1,17 +1,186 @@
|
|
|
1
1
|
# @cldmv/llm-translator
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bidirectional **Anthropic Messages ⇄ OpenAI Chat Completions** translator built on [slothlet](https://github.com/CLDMV/slothlet). It detects the backend behind an upstream (vLLM first, then any OpenAI-compatible server), converts requests, responses and streams in both directions, and ships an optional standalone server and CLI so Claude Code can run against local models.
|
|
4
|
+
|
|
5
|
+
Use it as a library when you want the conversion without any I/O (`request`, `response` and `stream` take plain objects), as a full round trip (`send`), or as a server that speaks the Anthropic or the OpenAI protocol to your clients.
|
|
6
|
+
|
|
7
|
+
> _Point Claude Code at a model running on your own GPU._
|
|
8
|
+
|
|
9
|
+
[![npm version]][npm_version_url] [![npm downloads]][npm_downloads_url] [![GitHub downloads]][github_downloads_url] [![Last commit]][last_commit_url] [![npm last update]][npm_last_update_url] [![coverage]][coverage_url]
|
|
4
10
|
|
|
5
11
|
> [!NOTE]
|
|
6
|
-
>
|
|
12
|
+
> **Status: 0.x.** Validated end to end for Claude Code (print mode) through the translator to vLLM. Other backends, model families and clients are covered by unit tests only and are tracked in the [Real-backend validation epic](https://github.com/CLDMV/llm-translator/issues/62). The API may change between minor versions.
|
|
13
|
+
|
|
14
|
+
[![Contributors]][contributors_url] [![Sponsor shinrai]][sponsor_url]
|
|
15
|
+
|
|
16
|
+
---
|
|
7
17
|
|
|
8
18
|
## ✨ What's New
|
|
9
19
|
|
|
10
|
-
### Latest: v0.
|
|
20
|
+
### Latest: v0.2.0 (October 2026)
|
|
21
|
+
|
|
22
|
+
- **The translator** — six converters between the Anthropic Messages API and OpenAI Chat Completions (requests, responses and streams, both directions), backend detection with `vllm`, `openai`, `anthropic` and generic `compatible` drivers, model aliases and routing, and `createTranslator` with `request`, `response`, `stream`, `send` and `detect`. Text, images, PDFs, tools and reasoning are mapped; whatever has no counterpart is dropped, rejected or replaced by a note, never guessed.
|
|
23
|
+
- **Server and CLI for Claude Code** — an Anthropic or OpenAI front over Fastify (optional peer dependency) and the `llm-translator` command. Slow upstreams are supported: no transport timeouts, and early `message_start` plus keep-alive deltas satisfy Claude Code's stream watchdog during long prefills. Models that refuse images are detected at startup or on the first refusal and retried once without them.
|
|
24
|
+
- [View full v0.2.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.2.0.md)
|
|
25
|
+
|
|
26
|
+
### Recent Releases
|
|
27
|
+
|
|
28
|
+
- **v0.1.0** (October 2026) — package scaffold: ESM and CommonJS entry points, tsup build, the `llm-translator-dev` condition with devcheck, tests on `@cldmv/vitest-runner` and the CLDMV v4 CI/release pipeline; the only export was a placeholder ([Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.1.0.md))
|
|
29
|
+
|
|
30
|
+
📚 **For complete version history and detailed release notes, see the [docs/changelog/](https://github.com/CLDMV/llm-translator/tree/master/docs/changelog/) folder.**
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 🚀 Key Features
|
|
35
|
+
|
|
36
|
+
### 🔁 **Both Directions**
|
|
37
|
+
|
|
38
|
+
Anthropic clients (Claude Code, the Anthropic SDKs) talk to OpenAI-compatible servers, and OpenAI clients talk to Anthropic upstreams. Requests, responses, streams, tool calls, usage, stop reasons and errors are converted; same-protocol traffic is forwarded without conversion.
|
|
39
|
+
|
|
40
|
+
### 🔎 **Backend Detection**
|
|
41
|
+
|
|
42
|
+
Each upstream is probed once and bound to a driver. The vLLM driver maps the server version to the right settings, checks whether the chat template accepts mid-conversation system messages and whether the model accepts images. See [docs/drivers.md](https://github.com/CLDMV/llm-translator/blob/master/docs/drivers.md).
|
|
43
|
+
|
|
44
|
+
### 🧠 **Reasoning and Tools**
|
|
45
|
+
|
|
46
|
+
Anthropic `thinking` maps to the server's reasoning request and reasoning comes back as thinking blocks. Tool calls are buffered into whole `tool_use` blocks by default, with an incremental mode available.
|
|
47
|
+
|
|
48
|
+
### 📄 **Images and PDFs**
|
|
49
|
+
|
|
50
|
+
Image blocks are converted, PDFs become per-page text plus page images when the model accepts them, and a text-only model is handled by a startup probe and a one-time retry without images.
|
|
51
|
+
|
|
52
|
+
### 🖥 **Server and CLI**
|
|
53
|
+
|
|
54
|
+
`llm-translator --upstream http://gpu-box:8000` starts a server with API-key protection for non-loopback binds, a client-header allow-list, and in-band error delivery for streams that are already committed. See [docs/server.md](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md).
|
|
55
|
+
|
|
56
|
+
### 🛡 **100% Test Coverage**
|
|
57
|
+
|
|
58
|
+
Lines, statements, functions and branches are all at 100% and enforced on every CI run, with no coverage-ignore pragmas. A harness in `tools/e2e/` drives a real vLLM and a real Claude Code; see [docs/testing.md](https://github.com/CLDMV/llm-translator/blob/master/docs/testing.md).
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## 📦 Installation
|
|
63
|
+
|
|
64
|
+
### Requirements
|
|
65
|
+
|
|
66
|
+
- Node.js 22.15 or newer
|
|
67
|
+
- Fastify 5, only for the server and the Fastify plugin (an optional peer dependency)
|
|
68
|
+
|
|
69
|
+
### Install
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npm install @cldmv/llm-translator
|
|
73
|
+
npm install fastify # only for the server and the Fastify plugin
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## 🚀 Quick Start
|
|
79
|
+
|
|
80
|
+
### Library
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { createTranslator } from "@cldmv/llm-translator";
|
|
84
|
+
|
|
85
|
+
const api = await createTranslator({
|
|
86
|
+
front: "anthropic", // the protocol your client speaks
|
|
87
|
+
upstreams: ["http://gpu-box:8000"], // vLLM is detected automatically
|
|
88
|
+
aliases: { "claude-opus-5-5": "qwen3-coder" }
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Pure conversion, no I/O: build the upstream call yourself.
|
|
92
|
+
const call = await api.request({ model: "claude-opus-5-5", max_tokens: 256, messages: [{ role: "user", content: "hi" }] });
|
|
93
|
+
// call.url, call.headers, call.body ...
|
|
94
|
+
// api.response(upstreamJson, call.state) converts a reply back,
|
|
95
|
+
// api.stream(upstreamSseBody, call.state) turns a stream into Anthropic events.
|
|
96
|
+
|
|
97
|
+
// Or let the translator do the full round trip.
|
|
98
|
+
const reply = await api.send({ model: "qwen3-coder", max_tokens: 256, stream: true, messages: [{ role: "user", content: "hi" }] });
|
|
99
|
+
if (reply.stream) for await (const { event, data } of reply.stream) console.log(event, data);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Server and Claude Code
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
npx llm-translator --upstream http://gpu-box:8000 --port 8787 --model claude-opus-5-5=qwen3-coder
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const server = await api.server.start({ port: 8787 });
|
|
110
|
+
console.log(server.url);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Then point Claude Code at it with `ANTHROPIC_BASE_URL=http://127.0.0.1:8787`. The full walkthrough, including the vLLM flags tool calling needs, is in [docs/claude-code.md](https://github.com/CLDMV/llm-translator/blob/master/docs/claude-code.md).
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## 📚 Configuration
|
|
118
|
+
|
|
119
|
+
`createTranslator(config)` takes `front`, `upstreams` (URLs or objects with `url`, `apiKey`, `backend`, `models`, `default` and `settings`), `aliases`, global `settings`, `detect` and a custom `transport`. The CLI reads the same keys from flags, `LLM_TRANSLATOR_*` environment variables and a JSON file, in that order of precedence.
|
|
120
|
+
|
|
121
|
+
Settings tune how the converters map one protocol onto the other (`inlineSystem`, `images`, `imageFallback`, `toolStreaming`, `pingInterval` and more); they are resolved per upstream from defaults, probed values, driver defaults and your overrides. The full table is in [docs/settings.md](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md).
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## 📚 Documentation
|
|
126
|
+
|
|
127
|
+
### Reference
|
|
128
|
+
|
|
129
|
+
- [Server and CLI](https://github.com/CLDMV/llm-translator/blob/master/docs/server.md)
|
|
130
|
+
- [Settings](https://github.com/CLDMV/llm-translator/blob/master/docs/settings.md)
|
|
131
|
+
- [Protocol support](https://github.com/CLDMV/llm-translator/blob/master/docs/protocol-support.md)
|
|
132
|
+
- [Drivers, detection and routing](https://github.com/CLDMV/llm-translator/blob/master/docs/drivers.md)
|
|
133
|
+
|
|
134
|
+
### Guides
|
|
135
|
+
|
|
136
|
+
- [Claude Code against vLLM](https://github.com/CLDMV/llm-translator/blob/master/docs/claude-code.md)
|
|
137
|
+
- [Testing and the real-backend harness](https://github.com/CLDMV/llm-translator/blob/master/docs/testing.md)
|
|
138
|
+
- [Known limitations](https://github.com/CLDMV/llm-translator/blob/master/docs/known-limitations.md)
|
|
139
|
+
- [Feature inventory](https://github.com/CLDMV/llm-translator/blob/master/docs/feature-inventory.md)
|
|
140
|
+
- [Design decisions](https://github.com/CLDMV/llm-translator/blob/master/docs/decisions.md)
|
|
141
|
+
|
|
142
|
+
### Repo
|
|
143
|
+
|
|
144
|
+
[![npm unpacked size]][npm_size_url] [![Repo size]][repo_size_url]
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## 🔗 Links
|
|
149
|
+
|
|
150
|
+
- **npm**: [@cldmv/llm-translator](https://www.npmjs.com/package/@cldmv/llm-translator)
|
|
151
|
+
- **GitHub**: [CLDMV/llm-translator](https://github.com/CLDMV/llm-translator)
|
|
152
|
+
- **Issues**: [GitHub Issues](https://github.com/CLDMV/llm-translator/issues)
|
|
153
|
+
- **Releases**: [GitHub Releases](https://github.com/CLDMV/llm-translator/releases)
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## 📄 License
|
|
11
158
|
|
|
12
|
-
|
|
13
|
-
- [View full v0.1.0 Changelog](https://github.com/CLDMV/llm-translator/blob/master/docs/changelog/v0/v0.1.0.md)
|
|
159
|
+
[![GitHub license]][github_license_url] [![npm license]][npm_license_url]
|
|
14
160
|
|
|
15
|
-
|
|
161
|
+
Apache-2.0 © Shinrai / CLDMV
|
|
16
162
|
|
|
17
|
-
[
|
|
163
|
+
[npm version]: https://img.shields.io/npm/v/%40cldmv%2Fllm-translator.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
164
|
+
[npm_version_url]: https://www.npmjs.com/package/@cldmv/llm-translator
|
|
165
|
+
[npm downloads]: https://img.shields.io/npm/dm/%40cldmv%2Fllm-translator.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
166
|
+
[npm_downloads_url]: https://www.npmjs.com/package/@cldmv/llm-translator
|
|
167
|
+
[github downloads]: https://img.shields.io/github/downloads/CLDMV/llm-translator/total?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
168
|
+
[github_downloads_url]: https://github.com/CLDMV/llm-translator/releases
|
|
169
|
+
[last commit]: https://img.shields.io/github/last-commit/CLDMV/llm-translator?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
170
|
+
[last_commit_url]: https://github.com/CLDMV/llm-translator/commits
|
|
171
|
+
[npm last update]: https://img.shields.io/npm/last-update/%40cldmv%2Fllm-translator?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
172
|
+
[npm_last_update_url]: https://www.npmjs.com/package/@cldmv/llm-translator
|
|
173
|
+
[npm unpacked size]: https://img.shields.io/npm/unpacked-size/%40cldmv%2Fllm-translator.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
174
|
+
[npm_size_url]: https://www.npmjs.com/package/@cldmv/llm-translator
|
|
175
|
+
[repo size]: https://img.shields.io/github/repo-size/CLDMV/llm-translator?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
176
|
+
[repo_size_url]: https://github.com/CLDMV/llm-translator
|
|
177
|
+
[github license]: https://img.shields.io/github/license/CLDMV/llm-translator.svg?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
178
|
+
[github_license_url]: https://github.com/CLDMV/llm-translator/blob/HEAD/LICENSE
|
|
179
|
+
[npm license]: https://img.shields.io/npm/l/%40cldmv%2Fllm-translator.svg?style=for-the-badge&logo=npm&logoColor=white&labelColor=CB3837
|
|
180
|
+
[npm_license_url]: https://www.npmjs.com/package/@cldmv/llm-translator
|
|
181
|
+
[coverage]: https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2FCLDMV%2Fllm-translator%2Fbadges%2Fcoverage.json&style=for-the-badge&logo=vitest&logoColor=white
|
|
182
|
+
[coverage_url]: https://github.com/CLDMV/llm-translator/blob/badges/coverage.json
|
|
183
|
+
[contributors]: https://img.shields.io/github/contributors/CLDMV/llm-translator.svg?style=for-the-badge&logo=github&logoColor=white&labelColor=181717
|
|
184
|
+
[contributors_url]: https://github.com/CLDMV/llm-translator/graphs/contributors
|
|
185
|
+
[sponsor shinrai]: https://img.shields.io/github/sponsors/shinrai?style=for-the-badge&logo=githubsponsors&logoColor=white&labelColor=EA4AAA&label=Sponsor
|
|
186
|
+
[sponsor_url]: https://github.com/sponsors/shinrai
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
var q=Object.defineProperty;var p=(e,s)=>q(e,"name",{value:s,configurable:!0});import{resolve as v}from"../../../../../../../lib/settings.mjs";import{TranslatorError as w}from"../../../../../../../lib/errors.mjs";import{createNameFitter as E}from"../../../../../../../lib/ids.mjs";import{sanitize as O}from"../../../../../../../lib/schema.mjs";import{NOTE as I}from"../../../../../../../lib/images.mjs";import{expand as A}from"../../../../../../../lib/documents.mjs";const B=new Set(["model","messages","system","max_tokens","stream","temperature","top_p","top_k","stop_sequences","tools","tool_choice","thinking","output_config","output_format","context_management"]),L=new Set(["metadata","cache_control","service_tier"]),M=new Set(["mcp_servers","container","inference_geo"]),z="x-anthropic-billing-header:";async function b(e,s){const t=v(s);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new w("messages: Field required",{code:"invalid_request",param:"messages"});const o={front:"anthropic",model:e.model,stream:e.stream===!0,thinking:e.thinking?.type??null,reasoningField:t.reasoningField,toolNames:{},dropped:[]},f=E({max:64,map:o.toolNames});for(const c of Object.keys(e))if(!(B.has(c)||L.has(c)))if(M.has(c))g(o,c);else{if(t.unknownFields==="error")throw new w(`${c}: Extra inputs are not permitted`,{code:"unsupported_field",param:c});g(o,c)}const n={model:e.model},u=F(e.system,t),_=await C(e.messages,t,o,f,u,D(e.context_management,o));n.messages=_.messages,_.prefill&&(n.continue_final_message=!0,n.add_generation_prompt=!1),e.max_tokens!==void 0&&(n[t.maxTokensField]=e.max_tokens),e.temperature!==void 0&&(n.temperature=e.temperature),e.top_p!==void 0&&(n.top_p=e.top_p),e.top_k!==void 0&&(t.topK?n.top_k=e.top_k:g(o,"top_k")),e.stop_sequences?.length>0&&(n.stop=e.stop_sequences),o.stream&&(n.stream=!0,t.streamUsage&&(n.stream_options={include_usage:!0},t.continuousUsage&&(n.stream_options.continuous_usage_stats=!0)));const d=H(e.tools??[],t,o,f);if(d.length>0){n.tools=d;const c=K(e.tool_choice,t,o,f);c!==void 0&&(n.tool_choice=c),e.tool_choice?.disable_parallel_tool_use===!0&&t.parallelToolCalls&&(n.parallel_tool_calls=!1)}return U(e,t,n),G(e,t,o,n),{body:n,state:o}}p(b,"request");function g(e,s){e.dropped.includes(s)||e.dropped.push(s)}p(g,"drop");function j(e,s,t){if(s.unknownFields==="error")throw new w(`${t}: is not supported by this translator`,{code:"unsupported_content",param:t});g(e,t)}p(j,"unsupported");function F(e,s){return e==null?"":(typeof e=="string"?[{type:"text",text:e}]:e).filter(o=>o.type==="text"&&!(s.stripBillingHeader&&o.text.trimStart().startsWith(z))).map(o=>o.text).join(`
|
|
2
|
+
|
|
3
|
+
`)}p(F,"textOf");function T(e){return typeof e=="string"?[{type:"text",text:e}]:e??[]}p(T,"asBlocks");async function C(e,s,t,o,f,n){const u=[],_=[],d=new Map,c=s.inlineSystem==="auto"?"user":s.inlineSystem,a=p(i=>{const r=u.at(-1);r?.role==="user"?r.parts.push(...i):u.push({role:"user",parts:i})},"pushUser"),h=p(async(i,r)=>{switch(i.type){case"text":return i.text===""?[]:[{type:"text",text:i.text}];case"image":return W(i,s,t);case"document":{const m=[];for(const l of await A(i,{settings:s,toolInputs:d,toolUseId:r}))m.push(...await h(l,r));return m}case"search_result":{const m=(i.content??[]).map(l=>l.text).join(`
|
|
4
|
+
`);return[{type:"text",text:`[Search result: ${i.title??""} (${i.source??""})]
|
|
5
|
+
${m}`}]}case"tool_reference":return[{type:"text",text:`[Tool reference: ${i.tool_name}]`}];default:return j(t,s,`block:${i.type}`),[]}},"toParts");for(const i of e)if(i.role==="system"){const r=F(i.content,s);if(r===""||c==="drop")continue;if(c==="keep")u.push({role:"system",content:r});else if(c==="hoist")_.push(r);else{const m=r.trimStart().startsWith("<system-reminder>")?r:`<system-reminder>
|
|
6
|
+
${r}
|
|
7
|
+
</system-reminder>`;a([{type:"text",text:m}])}}else if(i.role==="user"){const r=[],m=[];for(const x of T(i.content)){if(x.type!=="tool_result"){r.push(...await h(x,null));continue}const $=[];for(const k of T(x.content))$.push(...await h(k,x.tool_use_id));const N=$.filter(k=>k.type==="text").map(k=>k.text),S=$.filter(k=>k.type==="image_url");let y=N.join(`
|
|
8
|
+
`);S.length>0&&(m.push({type:"text",text:`Image output of tool call ${x.tool_use_id}:`},...S),y===""&&(y="(image output attached in the next message)")),x.is_error===!0&&s.toolErrors&&(y=`[error] ${y}`),u.push({role:"tool",tool_call_id:x.tool_use_id,content:y})}const l=[...m,...r];l.length>0&&a(l)}else if(i.role==="assistant"){const r={role:"assistant",texts:[],reasoning:[],tool_calls:[]};for(const l of T(i.content))l.type==="text"?l.text!==""&&r.texts.push(l.text):l.type==="thinking"?l.thinking&&r.reasoning.push(l.thinking):l.type==="redacted_thinking"?g(t,"redacted_thinking"):l.type==="tool_use"?(d.set(l.id,l.input),r.tool_calls.push({id:l.id,type:"function",function:{name:o(l.name),arguments:JSON.stringify(l.input??{})}})):j(t,s,`block:${l.type}`);const m=u.at(-1);m?.role==="assistant"?(m.texts.push(...r.texts),m.reasoning.push(...r.reasoning),m.tool_calls.push(...r.tool_calls)):u.push(r)}else throw new w(`messages: unsupported role "${i.role}"`,{code:"invalid_request",param:"messages"});return P(u,s,f,_,n)}p(C,"convertMessages");function D(e,s){let t=1/0;for(const o of e?.edits??[])typeof o.type=="string"&&o.type.startsWith("clear_thinking")?o.keep==="all"?t=1/0:t=o.keep?.value??1:g(s,`context_management.${o.type}`);return t}p(D,"thinkingTurnsToKeep");function P(e,s,t,o,f){let n=f;for(let a=e.length-1;a>=0;a--)e[a].role!=="assistant"||e[a].reasoning.length===0||(n>0?n--:e[a].reasoning=[]);const u=[],_=[t,...o].filter(a=>a!=="").join(`
|
|
9
|
+
|
|
10
|
+
`);_!==""&&u.push({role:"system",content:_});for(const a of e)if(a.role==="user"){const h=a.parts.every(i=>i.type==="text");u.push({role:"user",content:h?a.parts.map(i=>i.text).join(`
|
|
11
|
+
|
|
12
|
+
`):a.parts})}else if(a.role==="assistant"){const h={role:"assistant",content:a.texts.join(`
|
|
13
|
+
|
|
14
|
+
`)};a.reasoning.length>0&&(h[s.reasoningField]=a.reasoning.join(`
|
|
15
|
+
|
|
16
|
+
`)),a.tool_calls.length>0&&(h.tool_calls=a.tool_calls),u.push(h)}else u.push(a);const d=u.at(-1),c=s.prefill==="continue_final_message"&&d?.role==="assistant"&&!d.tool_calls&&d.content!=="";return{messages:u,prefill:c}}p(P,"finalize");function W(e,s,t){if(!s.images)return g(t,"image"),[{type:"text",text:I}];const o=e.source;return o.type==="base64"?[{type:"image_url",image_url:{url:`data:${o.media_type};base64,${o.data}`}}]:o.type==="url"?[{type:"image_url",image_url:{url:o.url}}]:(g(t,"image.file"),[{type:"text",text:"[An image supplied as an uploaded file could not be forwarded.]"}])}p(W,"imageParts");function H(e,s,t,o){const f=[];for(const n of e){if(n.type!==void 0&&n.type!=="custom"){g(t,`tool:${n.name}`);continue}if(n.defer_loading===!0&&s.deferredTools==="drop"){g(t,"deferred_tools");continue}const u=n.input_schema??{type:"object",properties:{}},_={name:o(n.name),parameters:s.toolSchema==="sanitize"?O(u):u};n.description!==void 0&&(_.description=n.description),n.strict!==void 0&&(_.strict=n.strict),f.push({type:"function",function:_})}return f}p(H,"convertTools");function K(e,s,t,o){switch(e?.type){case void 0:return;case"auto":return"auto";case"any":return"required";case"none":return"none";case"tool":return{type:"function",function:{name:o(e.name)}};default:j(t,s,`tool_choice:${e.type}`);return}}p(K,"convertToolChoice");function U(e,s,t){const o=e.thinking,f=o?.type;let n;f==="disabled"?(s.thinking==="reasoning_effort"&&(n="none"),s.thinking==="chat_template_kwargs"&&(t.chat_template_kwargs={enable_thinking:!1})):(s.effort&&e.output_config?.effort!==void 0&&(n=e.output_config.effort),f==="enabled"&&(s.thinking==="chat_template_kwargs"&&(t.chat_template_kwargs={enable_thinking:!0},s.thinkingBudget&&o.budget_tokens!==void 0&&e.max_tokens!==void 0&&(t.thinking_token_budget=Math.max(0,Math.min(o.budget_tokens,e.max_tokens-1)))),s.thinking==="reasoning_effort"&&n===void 0&&(n="medium"))),n!==void 0&&(t.reasoning_effort=n)}p(U,"applyThinking");function G(e,s,t,o){const f=e.output_config?.format??e.output_format;f!==void 0&&(s.responseFormat&&f.type==="json_schema"?o.response_format={type:"json_schema",json_schema:{name:"output",schema:f.schema}}:g(t,"output_config.format"));for(const n of Object.keys(e.output_config??{}))n!=="effort"&&n!=="format"&&g(t,`output_config.${n}`)}p(G,"applyOutputConfig");export{b as request};
|
|
17
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var h=Object.defineProperty;var r=(o,n)=>h(o,"name",{value:n,configurable:!0});import{resolve as u}from"../../../../../../../lib/settings.mjs";import{restoreName as m}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as g,toOpenAIUsage as d}from"../../../../../../../lib/finish.mjs";function j(o,n,p){const a=u(p),f=n?.toolNames??{},i=[],s=[],e=[];for(const t of o.content??[])t.type==="text"?i.push(t.text):t.type==="thinking"?s.push(t.thinking):t.type==="tool_use"&&e.push({id:t.id,type:"function",function:{name:m(f,t.name),arguments:JSON.stringify(t.input??{})}});const c={role:"assistant",content:i.length===0&&e.length>0?null:i.join("")};s.length>0&&(c[a.reasoningField]=s.join("")),e.length>0&&(c.tool_calls=e);const l={index:0,message:c,finish_reason:g(o.stop_reason)};return typeof o.stop_sequence=="string"&&(l.stop_reason=o.stop_sequence),{id:`chatcmpl-${String(o.id).replace(/^msg_/,"")}`,object:"chat.completion",created:Math.floor(Date.now()/1e3),model:n?.model??o.model,choices:[l],usage:d(o.usage)}}r(j,"response");export{j as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var k=Object.defineProperty;var i=(l,o)=>k(l,"name",{value:o,configurable:!0});import{resolve as x}from"../../../../../../../lib/settings.mjs";import{anthropicToOpenAIType as b,anthropicTypeToStatus as O}from"../../../../../../../lib/errors.mjs";import{completionId as N,restoreName as j}from"../../../../../../../lib/ids.mjs";import{toOpenAIFinish as D,toOpenAIUsage as E}from"../../../../../../../lib/finish.mjs";import{messages as I}from"../../../../../../../lib/sse.mjs";async function*F(l,o,u){const g=x(u),_=o?.toolNames??{},h=o?.includeUsage??!0,f=Math.floor(Date.now()/1e3),s=new Map;let r=N(),c=o?.model,d={},y=!1;const n=i((e,t=null,a={})=>({data:{id:r,object:"chat.completion.chunk",created:f,model:c,choices:[{index:0,delta:e,finish_reason:t,...a}]}}),"chunk"),p=i((e,t)=>({data:{error:{message:t,type:b(e),param:null,code:O(e)}}}),"failure");try{for await(const{data:e}of I(l))if(e.type==="message_start")r=`chatcmpl-${String(e.message.id).replace(/^msg_/,"")}`,c=o?.model??e.message.model,d={...e.message.usage},yield n({role:"assistant",content:""});else if(e.type==="content_block_start"){const t=e.content_block;if(t.type==="tool_use"){s.set(e.index,s.size);const a=j(_,t.name);yield n({tool_calls:[{index:s.get(e.index),id:t.id,type:"function",function:{name:a,arguments:""}}]})}}else if(e.type==="content_block_delta"){const t=e.delta;t.type==="text_delta"?yield n({content:t.text}):t.type==="thinking_delta"?yield n({[g.reasoningField]:t.thinking}):t.type==="input_json_delta"&&s.has(e.index)&&(yield n({tool_calls:[{index:s.get(e.index),function:{arguments:t.partial_json}}]}))}else if(e.type==="message_delta"){for(const[a,m]of Object.entries(e.usage??{}))m!=null&&(d[a]=m);const t=typeof e.delta?.stop_sequence=="string"?{stop_reason:e.delta.stop_sequence}:{};yield n({},D(e.delta?.stop_reason),t)}else if(e.type==="message_stop"){y=!0;break}else if(e.type==="error"){yield p(e.error.type,e.error.message),yield{data:"[DONE]"};return}}catch(e){yield p(e.type??"api_error",e.message),yield{data:"[DONE]"};return}if(!y){yield p("api_error","Upstream stream ended before completion"),yield{data:"[DONE]"};return}h&&(yield{data:{id:r,object:"chat.completion.chunk",created:f,model:c,choices:[],usage:E(d)}}),yield{data:"[DONE]"}}i(F,"stream");export{F as stream};
|
|
2
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
var j=Object.defineProperty;var u=(e,r)=>j(e,"name",{value:r,configurable:!0});import{resolve as q}from"../../../../../../../lib/settings.mjs";import{TranslatorError as _}from"../../../../../../../lib/errors.mjs";import{anthropicToolId as w,createNameFitter as v}from"../../../../../../../lib/ids.mjs";import{parseArguments as A}from"../../../../../../../lib/arguments.mjs";import{sanitize as T}from"../../../../../../../lib/schema.mjs";const N=new Set(["model","messages","max_tokens","max_completion_tokens","temperature","top_p","top_k","stop","stream","stream_options","tools","tool_choice","parallel_tool_calls","reasoning_effort","response_format","user","n"]),$=new Set(["metadata","store","service_tier"]),x=/^data:([^;,]+);base64,(.*)$/s;async function O(e,r){const a=q(r);if(e===null||typeof e!="object"||!Array.isArray(e.messages))throw new _("messages is a required property",{code:"invalid_request",param:"messages"});if(e.n!==void 0&&e.n!==1)throw new _("n: only a single choice (n = 1) is supported",{code:"unsupported_field",param:"n"});const s={front:"openai",model:e.model,stream:e.stream===!0,includeUsage:e.stream_options?.include_usage===!0,reasoningField:a.reasoningField,toolNames:{},dropped:[]},l=v({max:128,map:s.toolNames});for(const t of Object.keys(e))if(!(N.has(t)||$.has(t))){if(a.unknownFields==="error")throw new _(`${t}: Unrecognized request argument`,{code:"unsupported_field",param:t});c(s,t)}const d=[],f=[],y=u((t,o)=>{const i=f.at(-1);i?.role===t?i.blocks.push(...o):f.push({role:t,blocks:[...o]})},"push");for(const t of e.messages)switch(t.role){case"system":case"developer":{const o=b(t.content);o!==""&&d.push(o);break}case"user":y("user",S(t.content,s));break;case"assistant":{const o=[],i=b(t.content);i!==""?o.push({type:"text",text:i}):typeof t.refusal=="string"&&t.refusal!==""&&o.push({type:"text",text:t.refusal}),F(t)&&c(s,"reasoning");for(const p of t.tool_calls??[])o.push({type:"tool_use",id:w(p.id),name:l(p.function.name),input:A(p.function.arguments)});o.length>0&&y("assistant",o);break}case"tool":{const o={type:"tool_result",tool_use_id:w(t.tool_call_id),content:b(t.content)},i=f.at(-1);if(i?.role==="user"){const p=i.blocks.findIndex(h=>h.type!=="tool_result");i.blocks.splice(p===-1?i.blocks.length:p,0,o)}else f.push({role:"user",blocks:[o]});break}default:throw new _(`messages: unsupported role "${t.role}"`,{code:"invalid_request",param:"messages"})}const n={model:e.model,max_tokens:e.max_completion_tokens??e.max_tokens??a.defaultMaxTokens};d.length>0&&(n.system=d.join(`
|
|
2
|
+
|
|
3
|
+
`)),n.messages=f.map(t=>({role:t.role,content:t.blocks})),e.temperature!==void 0&&(n.temperature=Math.min(1,e.temperature)),e.top_p!==void 0&&(n.top_p=e.top_p),e.top_k!==void 0&&(n.top_k=e.top_k),e.stop!==void 0&&e.stop!==null&&(n.stop_sequences=[].concat(e.stop)),s.stream&&(n.stream=!0),typeof e.user=="string"&&(n.metadata={user_id:e.user});const g=[];for(const t of e.tools??[]){if(t.type!=="function"){c(s,`tool:${t.type}`);continue}const o=t.function,i=o.parameters??{type:"object",properties:{}},p={name:l(o.name),input_schema:a.toolSchema==="sanitize"?T(i):i};o.description!==void 0&&(p.description=o.description),g.push(p)}if(g.length>0){n.tools=g;let t=z(e.tool_choice,l);e.parallel_tool_calls===!1&&t.type!=="none"&&(t={...t,disable_parallel_tool_use:!0}),(e.tool_choice!==void 0||e.parallel_tool_calls===!1)&&(n.tool_choice=t)}const m=e.reasoning_effort;m==="none"?n.thinking={type:"disabled"}:m!=null&&(n.thinking={type:"adaptive"},n.output_config={effort:m==="minimal"?"low":m});const k=e.response_format;return k?.type==="json_schema"?n.output_config={...n.output_config,format:{type:"json_schema",schema:k.json_schema.schema}}:k?.type==="json_object"&&c(s,"response_format"),{body:n,state:s}}u(O,"request");function c(e,r){e.dropped.includes(r)||e.dropped.push(r)}u(c,"drop");function F(e){return!!(e.reasoning||e.reasoning_content)}u(F,"reasoningPresent");function b(e){return typeof e=="string"?e:(e??[]).filter(r=>r.type==="text").map(r=>r.text).join(`
|
|
4
|
+
|
|
5
|
+
`)}u(b,"plainText");function S(e,r){if(typeof e=="string")return e===""?[]:[{type:"text",text:e}];const a=[];for(const s of e??[])if(s.type==="text")a.push({type:"text",text:s.text});else if(s.type==="image_url"){const l=x.exec(s.image_url.url);a.push({type:"image",source:l?{type:"base64",media_type:l[1],data:l[2]}:{type:"url",url:s.image_url.url}})}else if(s.type==="file"&&typeof s.file?.file_data=="string"&&x.test(s.file.file_data)){const l=x.exec(s.file.file_data);a.push({type:"document",source:{type:"base64",media_type:l[1],data:l[2]}})}else c(r,`content:${s.type}`);return a}u(S,"userBlocks");function z(e,r){return e==="none"?{type:"none"}:e==="required"?{type:"any"}:typeof e=="object"&&e!==null?{type:"tool",name:r(e.function.name)}:{type:"auto"}}u(z,"toolChoice");export{O as request};
|
|
6
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var u=Object.defineProperty;var m=(s,n)=>u(s,"name",{value:n,configurable:!0});import{resolve as h}from"../../../../../../../lib/settings.mjs";import{TranslatorError as d}from"../../../../../../../lib/errors.mjs";import{messageId as y,anthropicToolId as _,restoreName as x}from"../../../../../../../lib/ids.mjs";import{sign as T,reasoningOf as k,splitThink as A}from"../../../../../../../lib/thinking.mjs";import{isComplete as C,parseArguments as N}from"../../../../../../../lib/arguments.mjs";import{toAnthropicStop as S,toAnthropicUsage as v}from"../../../../../../../lib/finish.mjs";function q(s,n,l){const c=h(l),r=s?.choices?.[0];if(!r)throw new d("Upstream response contained no choices",{code:"invalid_upstream_response",status:502});const e=r.message??{},o=[];let i=k(e),t=typeof e.content=="string"?e.content:"";i===""&&c.thinkTags&&({reasoning:i,text:t}=A(t)),i!==""&&(o.push({type:"thinking",thinking:i,signature:T()}),t=t.trimStart()),t===""&&typeof e.refusal=="string"&&(t=e.refusal),t!==""&&o.push({type:"text",text:t});const f=n?.toolNames??{},a=e.tool_calls??[];for(const p of a)o.push({type:"tool_use",id:_(p.id),name:x(f,p.function.name),input:N(p.function.arguments)});o.length===0&&o.push({type:"text",text:""});const g=S(r.finish_reason,{hasToolCalls:a.length>0,matchedStop:r.stop_reason,argumentsComplete:a.length===0||C(a.at(-1).function.arguments)});return{id:y(),type:"message",role:"assistant",model:n?.model??s.model,content:o,...g,usage:v(s.usage)}}m(q,"response");export{q as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var D=Object.defineProperty;var l=(g,f)=>D(g,"name",{value:f,configurable:!0});import{resolve as J}from"../../../../../../../lib/settings.mjs";import{TranslatorError as M,openaiToAnthropicType as F,rewriteContextOverflow as G}from"../../../../../../../lib/errors.mjs";import{isComplete as H}from"../../../../../../../lib/arguments.mjs";import{messageId as K,anthropicToolId as L,restoreName as P}from"../../../../../../../lib/ids.mjs";import{sign as Q,reasoningOf as R,createThinkSplitter as V}from"../../../../../../../lib/thinking.mjs";import{toAnthropicStop as W,toAnthropicUsage as U}from"../../../../../../../lib/finish.mjs";import{messages as X}from"../../../../../../../lib/sse.mjs";async function*se(g,f,A){const _=J(A),I=f?.toolNames??{},u=_.thinkTags?V():null,r=new Map;let p=!1,x=0,o=null,k=null,h=null,b=null,j=!1,T=0,S=!1;const N=[],s=l((e,t)=>({event:e,data:{type:e,...t}}),"event"),y=l((e,t)=>s("error",{error:{type:e,message:t}}),"fail");function*d(){if(o!==null){o.kind==="thinking"?yield s("content_block_delta",{index:o.index,delta:{type:"signature_delta",signature:Q()}}):o.kind==="tool"&&(o.entry.state="closed",o.entry.sent||(yield s("content_block_delta",{index:o.index,delta:{type:"input_json_delta",partial_json:"{}"}}))),yield s("content_block_stop",{index:o.index}),o=null;for(const e of r.values()){if(e.state!=="pending")continue;e.state="closed";const t=x++,n=e.args.join("");yield s("content_block_start",{index:t,content_block:e.block}),yield s("content_block_delta",{index:t,delta:{type:"input_json_delta",partial_json:n===""?"{}":n}}),yield s("content_block_stop",{index:t})}}}l(d,"close");function*z(){for(const e of v()){if(e.state!=="buffered")continue;yield*m("tool",e.block,e);const t=e.args.join("");t!==""&&(e.sent=!0,yield s("content_block_delta",{index:o.index,delta:{type:"input_json_delta",partial_json:t}})),yield*d()}}l(z,"flushBuffered");function v(){return[...r.entries()].sort((e,t)=>e[0]-t[0]).map(e=>e[1])}l(v,"ordered");function*m(e,t,n){yield*d(),o={kind:e,index:x++,entry:n},yield s("content_block_start",{index:o.index,content_block:t})}l(m,"begin");function*c(e,t){if(e==="text"&&S&&(t=t.trimStart()),t==="")return;if(S=e==="thinking",o?.kind==="tool"){N.push({kind:e,text:t});return}o?.kind!==e&&(yield*m(e,e==="text"?{type:"text",text:""}:{type:"thinking",thinking:"",signature:""}));const n=e==="text"?{type:"text_delta",text:t}:{type:"thinking_delta",thinking:t};yield s("content_block_delta",{index:o.index,delta:n})}l(c,"piece");function*w(e){yield*c("thinking",e.reasoning),yield*c("text",e.text)}l(w,"pieces");function*E(e){let t=e.index;t===void 0&&(t=e.id===void 0?T:r.size),T=t;let n=r.get(t);if(n===void 0){const a=e.function?.name??"",O={type:"tool_use",id:L(e.id),name:P(I,a),input:{}};n={state:_.toolStreaming==="buffer"?"buffered":"pending",sent:!1,args:[],block:O},r.set(t,n),_.toolStreaming!=="buffer"&&o?.kind!=="tool"&&(n.state="open",yield*m("tool",O,n))}let i=e.function?.arguments;i!==null&&typeof i=="object"&&(i=JSON.stringify(i)),!(i==null||i==="")&&(n.args.push(i),n.state==="open"&&(n.sent=!0,yield s("content_block_delta",{index:o.index,delta:{type:"input_json_delta",partial_json:i}})))}l(E,"toolCall");try{for await(const e of X(g)){if(e.data==="[DONE]"){j=!0;break}const t=e.data;if(t.error){const a=t.error.message??"Upstream error";yield y(F(t.error),G(a)??a);return}p||(p=!0,yield s("message_start",{message:{id:K(),type:"message",role:"assistant",model:f?.model??t.model,content:[],stop_reason:null,stop_sequence:null,usage:U(t.usage)}})),t.usage&&(k=t.usage);const n=t.choices?.[0];if(!n)continue;const i=n.delta??{};yield*c("thinking",R(i)),typeof i.content=="string"&&i.content!==""&&(u?yield*w(u.push(i.content)):yield*c("text",i.content));for(const a of i.tool_calls??[])yield*E(a);n.finish_reason&&(h=n.finish_reason),typeof n.stop_reason=="string"&&(b=n.stop_reason)}}catch(e){yield e instanceof M?y(e.type,e.message):y("api_error",e.message);return}if(!p||h===null&&!j){yield y("api_error",p?"Upstream stream ended before completion":"Upstream stream ended without data");return}u&&(yield*w(u.end())),yield*z();const C=v().at(-1),q=C===void 0||H(C.args.join(""));yield*d();for(const e of N)yield*c(e.kind,e.text);yield*d(),x===0&&(yield*m("text",{type:"text",text:""}),yield*d());const B=W(h,{hasToolCalls:r.size>0,matchedStop:b,argumentsComplete:q});yield s("message_delta",{delta:B,usage:U(k)}),yield s("message_stop",{})}l(se,"stream");export{se as stream};
|
|
2
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var i=(t,e)=>m(t,"name",{value:e,configurable:!0});import{context as u,self as l}from"../lib/runtime.mjs";import{capture as d}from"../lib/capture.mjs";import{TranslatorError as b}from"../lib/errors.mjs";import{warn as g}from"../lib/warn.mjs";async function p(t={}){const e=d(l,u),{status:n}=l,o=t.network!==!1,a={transport:e.state.transport,signal:t.signal,timeout:e.config.detectTimeout};return await Promise.all(e.state.endpoints.map(r=>w(e,r,o,a))),n()}i(p,"detect");async function w(t,e,n,o){let a,r=null;if(e.backend!=="auto"){if(a=t.drivers.find(s=>s.name===e.backend),!a)throw new b(`Unknown backend "${e.backend}" for upstream ${e.name}`,{code:"unknown_backend",status:500});r={version:e.version}}else if(n){for(const s of t.drivers)if(r=await v(s,{endpoint:e,...o}),r){a=s;break}}if(a||(a=t.drivers.find(s=>s.name==="compatible"),r={reachable:null,models:[]}),e.driver=a.name,e.protocol=a.info.protocol,e.facts=r,e.driverSettings=await a.settings?.(r)??{},e.probed={},n&&e.backend==="auto"&&a.probe){const s=i(f=>e.settings[f]!==void 0||t.config.settings[f]!==void 0,"given"),c={inlineSystem:t.settings(e).inlineSystem==="auto",images:!s("images")};(c.inlineSystem||c.images)&&(e.probed=await a.probe({endpoint:e,facts:r,want:c,...o}))}r.reachable===!1&&g(t.config,t.state,`Upstream ${e.name} (${e.url}) did not answer detection; using the generic OpenAI-compatible driver`)}i(w,"bind");async function v(t,e){try{return await t.detect?.(e)??null}catch{return null}}i(v,"detectWith");export{p as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var e=(s,o)=>a(s,"name",{value:o,configurable:!0});function t(){return{name:"anthropic",protocol:"anthropic.messages",order:20,auth:"x-api-key",headers:{"anthropic-version":"2023-06-01"},paths:{messages:"/v1/messages",models:"/v1/models",count:"/v1/messages/count_tokens"}}}e(t,"info");export{t as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var o=Object.defineProperty;var r=(e,t)=>o(e,"name",{value:t,configurable:!0});import{fetchModels as i}from"../../../../lib/models.mjs";function p(e){return i({...e,auth:"x-api-key",extraHeaders:{"anthropic-version":"2023-06-01"}})}r(p,"list");export{p as list};
|
|
2
|
+
//# sourceMappingURL=list.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var u=Object.defineProperty;var a=(t,e)=>u(t,"name",{value:e,configurable:!0});import*as s from"../../../../lib/http.mjs";async function d({endpoint:t,request:e,transport:r,signal:i,timeout:p}){const o={...e};delete o.stream,delete o.max_tokens;const n=await s.json(r,s.join(t.url,"/v1/messages/count_tokens"),{method:"POST",headers:{"content-type":"application/json","anthropic-version":"2023-06-01",...s.headers(t,"x-api-key")},body:o,signal:i,timeout:p});return n.ok&&typeof n.body?.input_tokens=="number"?n.body.input_tokens:null}a(d,"count");export{d as count};
|
|
2
|
+
//# sourceMappingURL=count.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var a=(t,e)=>r(t,"name",{value:e,configurable:!0});import{fetchModels as c}from"../../../lib/models.mjs";async function d(t){const e=await c({...t,auth:"bearer"});return{reachable:e!==null,models:(e??[]).map(o=>o.id)}}a(d,"detect");export{d as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});function c(){return{name:"compatible",protocol:"openai.chat",order:100,auth:"bearer",paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(c,"info");export{c as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var e=(n,t)=>s(n,"name",{value:t,configurable:!0});function i(){return{thinking:"none",thinkingBudget:!1,effort:!1,topK:!1,prefill:"none",streamUsage:!0,continuousUsage:!1}}e(i,"settings");export{i as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});function r(){return{name:"openai",protocol:"openai.chat",order:30,auth:"bearer",paths:{chat:"/v1/chat/completions",models:"/v1/models"}}}o(r,"info");export{r as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var o=Object.defineProperty;var e=(n,t)=>o(n,"name",{value:t,configurable:!0});function s(){return{maxTokensField:"max_completion_tokens",thinking:"reasoning_effort",thinkingBudget:!1,effort:!0,topK:!1,prefill:"none",deferredTools:"drop",streamUsage:!0,continuousUsage:!1}}e(s,"settings");export{s as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var y=Object.defineProperty;var a=(e,n)=>y(e,"name",{value:n,configurable:!0});import*as s from"../../../lib/http.mjs";async function f({endpoint:e,transport:n,signal:i,timeout:u}){const d={method:"GET",headers:s.headers(e),signal:i,timeout:u},[l,t]=await Promise.all([s.json(n,s.join(e.url,"/version"),d),s.json(n,s.join(e.url,"/v1/models"),d)]),m=typeof l.body?.version=="string"?l.body.version:void 0,r=Array.isArray(t.body?.data)?t.body.data:[];return!m&&!r.some(o=>o?.owned_by==="vllm")?null:{version:m,models:r.map(o=>o.id),maxModelLen:r.find(o=>typeof o.max_model_len=="number")?.max_model_len}}a(f,"detect");export{f as detect};
|
|
2
|
+
//# sourceMappingURL=detect.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var p=Object.defineProperty;var a=(r,o)=>p(r,"name",{value:o,configurable:!0});import*as m from"../../../lib/upstream-error.mjs";function s(r,o){const e=m.parse(o,r);return{error:{message:e.message,type:e.type,param:e.param??null,code:e.code??r}}}a(s,"error");export{s as error};
|
|
2
|
+
//# sourceMappingURL=error.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var n=Object.defineProperty;var e=(o,t)=>n(o,"name",{value:t,configurable:!0});function a(){return{name:"vllm",protocol:"openai.chat",order:10,auth:"bearer",paths:{chat:"/v1/chat/completions",models:"/v1/models",tokenize:"/tokenize",version:"/version"}}}e(a,"info");export{a as info};
|
|
2
|
+
//# sourceMappingURL=info.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var u=Object.defineProperty;var l=(e,o)=>u(e,"name",{value:o,configurable:!0});import*as s from"../../../lib/http.mjs";import{refused as g}from"../../../lib/images.mjs";const f="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";async function p({endpoint:e,transport:o,signal:r,timeout:A,facts:c,want:a={}}){const n={},i={"content-type":"application/json",...s.headers(e)},m=c?.models?.[0];if(a.inlineSystem!==!1){const t=await s.json(o,s.join(e.url,"/tokenize"),{method:"POST",headers:i,body:{model:m,messages:[{role:"system",content:"a"},{role:"user",content:"b"},{role:"system",content:"c"},{role:"user",content:"d"}]},signal:r,timeout:A});n.inlineSystem=t.ok?"keep":"user"}if(a.images===!0){const t=await s.json(o,s.join(e.url,"/v1/chat/completions"),{method:"POST",headers:i,body:{model:m,max_tokens:1,messages:[{role:"user",content:[{type:"text",text:"."},{type:"image_url",image_url:{url:`data:image/png;base64,${f}`}}]}]},signal:r,timeout:A});t.ok?n.images=!0:t.status===400&&g(JSON.stringify(t.body))&&(n.images=!1)}return n}l(p,"probe");export{p as probe};
|
|
2
|
+
//# sourceMappingURL=probe.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var n=Object.defineProperty;var e=(t,o)=>n(t,"name",{value:o,configurable:!0});import{noEmptyTools as s}from"../../../lib/normalize.mjs";const f=new Set(["xhigh","max"]);function u(t,o){const r=s(t);return f.has(r.reasoning_effort)&&o?.settings?.effortPassthrough!==!0?{...r,reasoning_effort:"high"}:r}e(u,"request");export{u as request};
|
|
2
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var t=Object.defineProperty;var o=(e,r)=>t(e,"name",{value:r,configurable:!0});import{atLeast as n,semver as i}from"../../../lib/normalize.mjs";function a(e={}){return{reasoningField:n(e.version,"0.16.0")?"reasoning":"reasoning_content",thinkingBudget:i(e.version)!==null&&n(e.version,"0.19.0"),effort:n(e.version,"0.22.0"),maxTokensField:"max_completion_tokens",streamUsage:!0,continuousUsage:!0,prefill:"continue_final_message",topK:!0,responseFormat:!0}}o(a,"settings");export{a as settings};
|
|
2
|
+
//# sourceMappingURL=settings.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l=Object.defineProperty;var s=(t,o)=>l(t,"name",{value:o,configurable:!0});import*as n from"../../../../lib/http.mjs";async function c({endpoint:t,request:o,transport:a,signal:r,timeout:d}){const e=await n.json(a,n.join(t.url,"/tokenize"),{method:"POST",headers:{"content-type":"application/json",...n.headers(t)},body:{model:o.model,messages:o.messages,tools:o.tools,add_generation_prompt:!0},signal:r,timeout:d});return e.ok&&typeof e.body?.count=="number"?e.body.count:null}s(c,"count");export{c as count};
|
|
2
|
+
//# sourceMappingURL=count.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var o=(r,t)=>a(r,"name",{value:t,configurable:!0});import{context as c}from"../../lib/runtime.mjs";import{anthropicToOpenAIType as d}from"../../lib/errors.mjs";import*as m from"../../lib/upstream-error.mjs";function u(r,t,e={}){const n=e.front??c.config.front,p=m.render(n,r,{message:t,type:n==="openai"&&e.type?d(e.type):e.type,code:e.code,param:e.param},{native:!0});return{status:r,headers:{"content-type":"application/json"},body:p.body}}o(u,"build");export{u as build};
|
|
2
|
+
//# sourceMappingURL=build.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l=Object.defineProperty;var p=(r,o)=>l(r,"name",{value:o,configurable:!0});import{context as y,self as v}from"../../lib/runtime.mjs";import{capture as b}from"../../lib/capture.mjs";import*as u from"../../lib/upstream-error.mjs";const h=["retry-after","x-should-retry"];async function x(r,o,f={},e){const n=b(v,y,e?.front),t=e?n.endpoint(e.upstream):void 0,s=t?n.driver(t):void 0;let d=o;s?.error&&(d=await s.error(r,d,{endpoint:t,state:e}));const c=u.parse(d,r);e?.unserved===!0&&r===404&&(c.message+=` The model "${e.model}" is not served by any configured upstream; map it to a served model with "aliases".`);const a=u.render(n.front,r,c,{native:e?.native===!0}),m={"content-type":"application/json"};for(const i of h)f[i]!==void 0&&(m[i]=f[i]);return{status:a.status,headers:m,body:a.body}}p(x,"error");export{x as default};
|
|
2
|
+
//# sourceMappingURL=error.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var d=(a,t)=>c(a,"name",{value:t,configurable:!0});import{context as r,self as f}from"../../lib/runtime.mjs";import{capture as _}from"../../lib/capture.mjs";async function w(a={}){const t=_(f,r,a.front),s=new Map;await Promise.all(t.state.endpoints.map(async e=>{const o=t.driver(e),m=(o.list?await o.list({endpoint:e,transport:t.state.transport,signal:a.signal,timeout:t.config.detectTimeout}):null)??(e.models??e.facts?.models??[]).map(l=>({id:l}));for(const l of m)s.set(l.id,{...l,upstream:e.name})}));for(const e of Object.keys(t.config.aliases)){const o=s.get(t.config.aliases[e]);s.set(e,{...o,id:e})}const n=[...s.values()];if(t.front==="openai")return{object:"list",data:n.map(e=>({id:e.id,object:"model",created:e.created??0,owned_by:e.owned_by??e.upstream??"llm-translator",...e.max_model_len===void 0?{}:{max_model_len:e.max_model_len}}))};const i=n.map(e=>({type:"model",id:e.id,display_name:e.id,created_at:new Date((e.created??0)*1e3).toISOString(),...e.max_model_len===void 0?{}:{max_input_tokens:e.max_model_len}}));return{data:i,has_more:!1,first_id:i[0]?.id??null,last_id:i.at(-1)?.id??null}}d(w,"list");export{w as list};
|
|
2
|
+
//# sourceMappingURL=list.mjs.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
var j=Object.defineProperty;var h=(t,s)=>j(t,"name",{value:s,configurable:!0});import{context as q,self as x}from"../lib/runtime.mjs";import{capture as y}from"../lib/capture.mjs";import{TranslatorError as k}from"../lib/errors.mjs";import{pick as w}from"../lib/route.mjs";import*as d from"../lib/http.mjs";import{filterChatBody as F}from"../lib/passthrough.mjs";import*as g from"../lib/images.mjs";async function B(t,s={}){const r=y(x,q,s.front);if(!t||typeof t!="object"||Array.isArray(t))throw new k("The request body must be a JSON object",{code:"invalid_body",status:400});const{endpoint:e,model:m,unserved:v}=w(r.config,r.state.endpoints,t.model),o=r.driver(e),l=`${e.name}
|
|
2
|
+
${m}`,p=s.images===!1||r.state.textOnly.has(l)&&r.settings(e).imageFallback,i=p?{...r.settings(e),images:!1}:r.settings(e),n=r.converters(e),f=t.stream===!0;let a,c;n?{body:a,state:c}=await n.request(t,i):a=r.front==="openai"&&(s.filter??i.passthroughFilter===!0)?F(t):{...t},p&&!n&&r.front==="openai"&&(a=g.strip(a)),a.model=m,o.request&&(a=await o.request(a,{endpoint:e,settings:i,state:c}));const u=e.protocol==="openai.chat"?o.info.paths.chat:o.info.paths.messages;return{upstream:e.name,method:"POST",url:d.join(e.url,u),path:u,headers:{"content-type":"application/json",accept:f?"text/event-stream":"application/json",...o.info.headers,...n===null&&e.protocol==="anthropic.messages"?s.clientHeaders:void 0,...d.headers(e,o.info.auth,s.headers)},body:a,state:{v:1,upstream:e.name,front:r.front,native:n===null,model:t.model,stream:f,unserved:v,convert:c},images:{sent:g.carries(a),retry:i.imageFallback&&e.protocol==="openai.chat",key:l}}}h(B,"request");export{B as request};
|
|
3
|
+
//# sourceMappingURL=request.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var d=Object.defineProperty;var i=(t,e)=>d(t,"name",{value:e,configurable:!0});import{context as p,self as c}from"../lib/runtime.mjs";import{capture as m}from"../lib/capture.mjs";async function v(t,e){const o=m(c,p,e.front),r=o.endpoint(e.upstream),s=o.driver(r);let n=t;return s.response&&(n=await s.response(n,{endpoint:r,settings:o.settings(r),state:e})),e.native?n.model===void 0?n:{...n,model:e.model??n.model}:o.converters(r).response(n,e.convert,o.settings(r))}i(v,"response");export{v as response};
|
|
2
|
+
//# sourceMappingURL=response.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var b=Object.defineProperty;var i=(r,e)=>b(r,"name",{value:e,configurable:!0});import{context as x,self as S}from"../lib/runtime.mjs";import{TranslatorError as p}from"../lib/errors.mjs";import*as w from"../lib/http.mjs";import*as E from"../lib/images.mjs";import{parse as k}from"../lib/upstream-error.mjs";const O={"content-type":"text/event-stream; charset=utf-8","cache-control":"no-cache","x-accel-buffering":"no"};async function _(r,e={}){const{transport:n,textOnly:o}=x.state,{request:f,response:c,stream:u,error:s}=S,d=s.build;try{return await H({transport:n,textOnly:o,request:f,response:c,stream:u,error:s},r,e)}catch(t){if(!(t instanceof p))throw t;return d(t.status,t.message,{type:t.type,code:t.code,param:t.param,front:e.front})}}i(_,"send");async function H({transport:r,textOnly:e,request:n,response:o,stream:f,error:c},u,s){const d={front:s.front,headers:s.headers,clientHeaders:s.clientHeaders,filter:s.filter};let t=await n(u,d),a=await g(r,t,s.signal);if(!a.ok&&a.status===400&&t.images.sent&&t.images.retry){const y=await m(a);if(!E.refused(k(y,400).message))return c(400,y,w.plain(a.headers),t.state);e.add(t.images.key),t=await n(u,{...d,images:!1}),a=await g(r,t,s.signal)}const h=w.plain(a.headers);if(!a.ok)return c(a.status,await m(a),h,t.state);if(t.state.stream)return{status:200,headers:O,stream:f(a.body,t.state)};const l=await m(a);return l===null||typeof l!="object"?c(502,"The upstream returned a body that is not JSON",h,t.state):{status:200,headers:{"content-type":"application/json"},body:await o(l,t.state)}}i(H,"round");async function g(r,e,n){try{return await r(e.url,{method:e.method,headers:e.headers,body:JSON.stringify(e.body),signal:n,redirect:"error"})}catch(o){throw n?.aborted?o:new p(`Could not reach the upstream (${e.upstream}): ${o.message}`,{code:"upstream_unreachable",status:502,details:{cause:o}})}}i(g,"call");async function m(r){const e=await r.text();try{return JSON.parse(e)}catch{return e===""?null:e}}i(m,"parse");export{_ as send};
|
|
2
|
+
//# sourceMappingURL=send.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var o=(t,r)=>c(t,"name",{value:r,configurable:!0});import{context as a,self as d}from"../../lib/runtime.mjs";import{TranslatorError as g}from"../../lib/errors.mjs";import{register as m}from"../../lib/routes.mjs";import{resolve as u}from"../../lib/settings.mjs";async function x(t,r={}){const{config:n}=a;if(r.front!==void 0&&r.front!=="anthropic"&&r.front!=="openai")throw new g(`front must be "anthropic" or "openai" (got ${JSON.stringify(r.front)})`,{code:"invalid_config",status:500});const{send:i,token:f,model:s,error:l,sse:e}=d;m(t,r,{front:r.front??n.front,filter:n.settings.passthroughFilter!==!1,interval:u(n.settings).pingInterval,send:i,count:f.count,list:s.list,build:l.build,encode:e.encode,write:e.write})}o(x,"plugin");export{x as plugin};
|
|
2
|
+
//# sourceMappingURL=plugin.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var m=Object.defineProperty;var s=(t,r)=>m(t,"name",{value:r,configurable:!0});import{context as g,self as d}from"../../lib/runtime.mjs";import{TranslatorError as c}from"../../lib/errors.mjs";import{isLoopback as b}from"../../lib/http.mjs";async function C(t={}){const{state:r}=g,{plugin:u}=d.server,{build:f}=d.error;let i;try{({default:i}=await import("fastify"))}catch(o){throw new c('server.start needs the optional peer dependency "fastify" (npm install fastify)',{code:"fastify_missing",status:500,details:{cause:o}})}const e=t.host??"127.0.0.1";if(!b(e)&&[t.apiKey??[]].flat().filter(Boolean).length===0&&t.allowUnauthenticated!==!0)throw new c(`Refusing to listen on ${e} without an apiKey: anyone who can reach the port could use the upstream. Set \`apiKey\` (CLI: --api-key), bind to 127.0.0.1, or opt in with \`allowUnauthenticated: true\` (CLI: --allow-unauthenticated).`,{code:"unauthenticated_bind",status:500});const a=i({logger:t.logger??!1,forceCloseConnections:!0});a.setNotFoundHandler((o,h)=>{const{status:p,headers:y,body:w}=f(404,`Not found: ${o.method} ${o.url}`);return h.code(p).headers(y).send(w)}),await a.register(u,{front:t.front,apiKey:t.apiKey,bodyLimit:t.bodyLimit}),await a.listen({host:e,port:t.port??8787});const l=a.server.address().port,n={url:`http://${e.includes(":")?`[${e}]`:e}:${l}`,host:e,port:l,app:a,close:s(async()=>{r.servers.delete(n),await a.close()},"close")};return r.servers.add(n),n}s(C,"start");export{C as start};
|
|
2
|
+
//# sourceMappingURL=start.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var s=Object.defineProperty;var i=(n,t)=>s(n,"name",{value:t,configurable:!0});import{context as r}from"../../lib/runtime.mjs";import{withPings as l}from"../../lib/ping.mjs";import{resolve as d}from"../../lib/settings.mjs";function m(n,t={}){const o=t.front??r.config.front,c=t.interval??d(r.config.settings).pingInterval;if(o!=="anthropic")return l(n,c,{comment:"ping"});const a=new Map;async function*p(){for await(const e of n)e.event==="content_block_start"?a.set(e.data.index,e.data.content_block?.type):e.event==="content_block_stop"&&a.delete(e.data.index),yield e}return i(p,"watched"),l(p(),c,()=>f(a))}i(m,"ping");function f(n){const t=[...n.entries()].at(-1),o=t&&_[t[1]];return o?{event:"content_block_delta",data:{type:"content_block_delta",index:t[0],delta:o}}:{event:"ping",data:{type:"ping"}}}i(f,"keepAlive");const _={text:{type:"text_delta",text:""},thinking:{type:"thinking_delta",thinking:""},tool_use:{type:"input_json_delta",partial_json:""}};export{m as ping};
|
|
2
|
+
//# sourceMappingURL=ping.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var r=Object.defineProperty;var i=(n,o)=>r(n,"name",{value:o,configurable:!0});import{self as c}from"../../lib/runtime.mjs";function w(n){return new Promise(o=>{const e=i(()=>{n.off("drain",e),n.off("close",e),o()},"done");n.once("drain",e),n.once("close",e)})}i(w,"drained");async function a(n,o,e={}){const{encode:s,ping:d}=c.sse;for await(const f of d(o,e)){if(n.destroyed||n.writableEnded)return!1;n.write(s(f))||await w(n)}return!(n.destroyed||n.writableEnded)}i(a,"write");export{a as write};
|
|
2
|
+
//# sourceMappingURL=write.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var c=Object.defineProperty;var o=(s,a)=>c(s,"name",{value:a,configurable:!0});import{context as t}from"../lib/runtime.mjs";function l(){const{config:s,state:a}=t;return{front:s.front,upstreams:a.endpoints.map(r=>({name:r.name,url:r.url,backend:r.backend,driver:r.driver,protocol:r.protocol,version:r.facts?.version??r.version,models:r.models??r.facts?.models??[],default:r.default,facts:r.facts,probed:r.probed}))}}o(l,"status");export{l as status};
|
|
2
|
+
//# sourceMappingURL=status.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var p=Object.defineProperty;var f=(e,n)=>p(e,"name",{value:n,configurable:!0});import{context as d,self as y}from"../lib/runtime.mjs";import{capture as l}from"../lib/capture.mjs";import{messages as u}from"../lib/sse.mjs";function v(e,n){return!n||!e||typeof e!="object"?e:e.message&&typeof e.message=="object"&&e.message.model!==void 0?{...e,message:{...e.message,model:n}}:e.model===void 0?e:{...e,model:n}}f(v,"restore");function D(e,n){const o=l(y,d,n.front),r=o.endpoint(n.upstream),a=o.driver(r),c=o.settings(r),s=o.converters(r),i=a.chunk,m={endpoint:r,settings:c,state:n};return s&&!i?s.stream(e,n.convert,c):s?s.stream((async function*(){for await(const t of u(e))t.data!=="[DONE]"&&(yield await i(t.data,m))})(),n.convert,c):(async function*(){for await(const t of u(e)){if(t.data==="[DONE]"){yield t;continue}const g=i?await i(t.data,m):t.data;yield{...t,data:v(g,n.model)}}})()}f(D,"stream");export{D as stream};
|
|
2
|
+
//# sourceMappingURL=stream.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var u=Object.defineProperty;var a=(o,t)=>u(o,"name",{value:t,configurable:!0});import{context as p,self as s}from"../../lib/runtime.mjs";import{capture as m}from"../../lib/capture.mjs";import{estimate as f}from"../../lib/estimate.mjs";async function b(o,t={}){const n=m(s,p,t.front),{request:i}=s,e=await i(o,{front:t.front}),r=n.endpoint(e.upstream),c=n.driver(r);return{input_tokens:(c.count?await c.count({endpoint:r,request:e.body,transport:n.state.transport,signal:t.signal,timeout:n.config.detectTimeout}):null)??f(e.body)}}a(b,"count");export{b as count};
|
|
2
|
+
//# sourceMappingURL=count.mjs.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(async()=>{try{await import("../devcheck.mjs")}catch{}})();
|
|
1
|
+
var i=Object.defineProperty;var n=(r,t)=>i(r,"name",{value:t,configurable:!0});import{fileURLToPath as d}from"node:url";import p from"@cldmv/slothlet";import{normalize as l}from"./lib/config.mjs";import{TranslatorError as m}from"./lib/errors.mjs";import{load as c}from"./lib/pdf.mjs";import{transport as f}from"./lib/transport.mjs";import{warn as s}from"./lib/warn.mjs";(async()=>{try{await import("../devcheck.mjs")}catch{}})();async function u(r){const t=l(r),o={endpoints:t.endpoints,transport:t.transport??f,servers:new Set,warned:new Set,textOnly:new Set,front:void 0},e=await c();e.errors.canvas&&s(t,o,`@napi-rs/canvas could not be loaded (${e.errors.canvas}); PDFs will be read as text only, without page images`),e.errors.unpdf&&s(t,o,`unpdf could not be loaded (${e.errors.unpdf}); PDF documents cannot be read and fall back to a note`);const a=await p({...r.slothlet,base:d(new URL("./api",import.meta.url)),mode:"eager",context:{config:t,state:o},silent:!0});return await a.detect({network:t.detect}),a}n(u,"createTranslator");var S=u;export{m as TranslatorError,u as createTranslator,S as default};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var t=(e,n)=>i(e,"name",{value:n,configurable:!0});function u(e){let n=e;if(typeof e=="string")try{n=JSON.parse(e)}catch{return}return n!==null&&typeof n=="object"&&!Array.isArray(n)?n:void 0}t(u,"decode");function r(e){return e==null||e===""||u(e)!==void 0}t(r,"isComplete");function d(e){return e==null||e===""?{}:u(e)??{_raw_arguments:typeof e=="string"?e:JSON.stringify(e)}}t(d,"parseArguments");export{r as isComplete,d as parseArguments};
|
|
2
|
+
//# sourceMappingURL=arguments.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var h=Object.defineProperty;var n=(o,r)=>h(o,"name",{value:r,configurable:!0});import{resolve as v}from"./settings.mjs";function g(o){const r=[];for(const s of Object.keys(o??{})){const e=o[s];typeof e?.info=="function"&&r.push({name:s,info:e.info(),detect:e.detect,settings:e.settings,probe:e.probe,request:e.request,response:e.response,chunk:e.chunk,error:e.error,count:e.token?.count,list:e.model?.list})}return r.sort((s,e)=>s.info.order-e.info.order)}n(g,"collect");function b(o,r,s){const{config:e,state:u}=r,c=o.convert.anthropic.messages.to.openai.chat,i=o.convert.openai.chat.to.anthropic.messages,a=s??e.front,f=g(o.driver),d=a==="anthropic"?"anthropic.messages":"openai.chat",m=a==="anthropic"?{request:c.request,response:i.response,stream:i.stream}:{request:i.request,response:c.response,stream:c.stream};return{config:e,state:u,front:a,drivers:f,endpoint:n(t=>u.endpoints.find(p=>p.name===t),"endpoint"),driver:n(t=>f.find(p=>p.name===t.driver),"driver"),settings:n(t=>v(t.driverSettings,t.probed,e.settings,t.settings),"settings"),converters:n(t=>t.protocol===d?null:m,"converters")}}n(b,"capture");export{b as capture,g as collect};
|
|
2
|
+
//# sourceMappingURL=capture.mjs.map
|
package/dist/lib/cli.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var f=Object.defineProperty;var l=(t,e)=>f(t,"name",{value:e,configurable:!0});import{readFile as d}from"node:fs/promises";import{parseArgs as S}from"node:util";const u=`llm-translator - run Claude Code (or any Anthropic/OpenAI client) against local LLM servers
|
|
2
|
+
|
|
3
|
+
Usage: llm-translator --upstream <url> [options]
|
|
4
|
+
|
|
5
|
+
Options:
|
|
6
|
+
--front <anthropic|openai> Client protocol to serve (default anthropic)
|
|
7
|
+
--upstream <url> Upstream server base URL (repeatable)
|
|
8
|
+
--upstream-key <key> API key sent to the upstream
|
|
9
|
+
--backend <name> Driver for the upstream (default auto-detect: vllm, compatible, openai, anthropic)
|
|
10
|
+
--model <alias=target> Model alias (repeatable)
|
|
11
|
+
--host <host> Listen address (default 127.0.0.1)
|
|
12
|
+
--port <port> Listen port (default 8787)
|
|
13
|
+
--api-key <key> Key clients must send (x-api-key or Authorization: Bearer)
|
|
14
|
+
--allow-unauthenticated Allow a non-loopback --host without --api-key
|
|
15
|
+
--config <file> JSON config file (translator config plus a "server" object)
|
|
16
|
+
--silent Suppress warnings
|
|
17
|
+
--no-detect Skip upstream detection
|
|
18
|
+
--version Print the version
|
|
19
|
+
--help Print this help
|
|
20
|
+
|
|
21
|
+
Environment: LLM_TRANSLATOR_FRONT, LLM_TRANSLATOR_UPSTREAM, LLM_TRANSLATOR_UPSTREAM_KEY, LLM_TRANSLATOR_BACKEND,
|
|
22
|
+
LLM_TRANSLATOR_HOST, LLM_TRANSLATOR_PORT, LLM_TRANSLATOR_API_KEY, LLM_TRANSLATOR_ALLOW_UNAUTHENTICATED (1), LLM_TRANSLATOR_CONFIG. Flags win over the
|
|
23
|
+
environment, which wins over the config file.
|
|
24
|
+
`;function O(t){return S({args:t,allowPositionals:!1,options:{front:{type:"string"},upstream:{type:"string",multiple:!0},"upstream-key":{type:"string"},backend:{type:"string"},model:{type:"string",multiple:!0},host:{type:"string"},port:{type:"string"},"api-key":{type:"string"},"allow-unauthenticated":{type:"boolean"},config:{type:"string"},silent:{type:"boolean"},"no-detect":{type:"boolean"},version:{type:"boolean"},help:{type:"boolean"}}}).values}l(O,"parse");function y(t,e,n){const{server:L,...p}=n,s=t.upstream??(e.LLM_TRANSLATOR_UPSTREAM?e.LLM_TRANSLATOR_UPSTREAM.split(","):void 0),r={...p};if(s){const i=t["upstream-key"]??e.LLM_TRANSLATOR_UPSTREAM_KEY,a=t.backend??e.LLM_TRANSLATOR_BACKEND;r.upstreams=s.map(o=>({url:o,apiKey:i,...a?{backend:a}:{}}))}const T=t.front??e.LLM_TRANSLATOR_FRONT;if(T&&(r.front=T),t.model){r.aliases={...r.aliases};for(const i of t.model){const a=i.indexOf("=");if(a<1)throw new Error(`--model expects alias=target (got "${i}")`);r.aliases[i.slice(0,a)]=i.slice(a+1)}}t.silent&&(r.silent=!0),t["no-detect"]&&(r.detect=!1);const A=t.port??e.LLM_TRANSLATOR_PORT,c={...L,...t.host??e.LLM_TRANSLATOR_HOST?{host:t.host??e.LLM_TRANSLATOR_HOST}:{},...A?{port:Number(A)}:{},...t["api-key"]??e.LLM_TRANSLATOR_API_KEY?{apiKey:t["api-key"]??e.LLM_TRANSLATOR_API_KEY}:{},...t["allow-unauthenticated"]||e.LLM_TRANSLATOR_ALLOW_UNAUTHENTICATED==="1"?{allowUnauthenticated:!0}:{}};return{translator:r,server:c}}l(y,"merge");const N={out:l(t=>process.stdout.write(`${t}
|
|
25
|
+
`),"out"),err:l(t=>process.stderr.write(`${t}
|
|
26
|
+
`),"err"),onSignal:l(t=>{process.once("SIGINT",t),process.once("SIGTERM",t)},"onSignal")};async function w(t,e=process.env,n=N,L={}){let p;try{p=O(t)}catch(s){return n.err(`${s.message}
|
|
27
|
+
|
|
28
|
+
${u}`),2}if(p.help)return n.out(u),0;if(p.version)return n.out(L.version??(await import("../../package.json",{with:{type:"json"}})).default.version),0;try{const s=p.config??e.LLM_TRANSLATOR_CONFIG,{translator:r,server:T}=y(p,e,s?JSON.parse(await d(s,"utf8")):{}),c=await(L.createTranslator??(await import("../index.mjs")).createTranslator)(r),i=await c.server.start(T),a=c.status();n.out(`llm-translator (${a.front} front) listening on ${i.url}`);for(const o of a.upstreams??[]){const R=o.version??"unknown",m=o.probed?.inlineSystem??"auto",_=o.probed?.images??"default";n.out(` upstream ${o.name} ${o.url}: driver=${o.driver} version=${R} inlineSystem=${m} images=${_}`)}return await new Promise(o=>n.onSignal(()=>o())),await c.server.stop(),await c.slothlet.shutdown(),0}catch(s){return n.err(`llm-translator: ${s.message}`),1}}l(w,"main");export{w as main,y as merge,O as parse,N as processIo};
|
|
29
|
+
//# sourceMappingURL=cli.mjs.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var a=Object.defineProperty;var s=(t,r)=>a(t,"name",{value:r,configurable:!0});import{TranslatorError as n}from"./errors.mjs";function i(t){let r=t.length;for(;t[r-1]==="/";)r--;const e=t.slice(0,r);return e.endsWith("/v1")?e.slice(0,-3):e}s(i,"baseUrl");function o(t,r){const e=typeof t=="string"?{url:t}:t;if(!e||typeof e!="object"||typeof e.url!="string"||e.url==="")throw new n(`upstreams[${r}] needs a "url"`,{code:"INVALID_CONFIG",status:500});return{name:e.name??`upstream-${r}`,url:i(e.url),apiKey:e.apiKey,headers:{...e.headers},backend:e.backend??"auto",models:Array.isArray(e.models)?[...e.models]:void 0,default:e.default===!0,settings:{...e.settings},version:e.version,driver:null,facts:null,driverSettings:{},probed:{}}}s(o,"endpoint");function d(t={}){const r=t.front??"anthropic";if(r!=="anthropic"&&r!=="openai")throw new n(`front must be "anthropic" or "openai" (got ${JSON.stringify(r)})`,{code:"INVALID_CONFIG",status:500});let e=t.upstreams??t.upstream;if(e!==void 0&&!Array.isArray(e)&&(e=[e]),!e||e.length===0)throw new n("at least one upstream is required (config.upstreams)",{code:"INVALID_CONFIG",status:500});return{front:r,endpoints:e.map(o),aliases:{...t.aliases},settings:{...t.settings},detect:t.detect!==!1,detectTimeout:t.detectTimeout??5e3,transport:t.transport,silent:t.silent===!0}}s(d,"normalize");export{o as endpoint,d as normalize};
|
|
2
|
+
//# sourceMappingURL=config.mjs.map
|