@cldmv/llm-translator 0.1.0 → 0.3.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.
Files changed (208) hide show
  1. package/README.md +182 -7
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +17 -0
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +2 -0
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +2 -0
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +6 -0
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +2 -0
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +2 -0
  8. package/dist/api/detect.mjs +2 -0
  9. package/dist/api/driver/anthropic/detect.mjs +2 -0
  10. package/dist/api/driver/anthropic/info.mjs +2 -0
  11. package/dist/api/driver/anthropic/model/list.mjs +2 -0
  12. package/dist/api/driver/anthropic/settings.mjs +2 -0
  13. package/dist/api/driver/anthropic/token/count.mjs +2 -0
  14. package/dist/api/driver/compatible/detect.mjs +2 -0
  15. package/dist/api/driver/compatible/info.mjs +2 -0
  16. package/dist/api/driver/compatible/model/list.mjs +2 -0
  17. package/dist/api/driver/compatible/request.mjs +2 -0
  18. package/dist/api/driver/compatible/settings.mjs +2 -0
  19. package/dist/api/driver/deepseek/detect.mjs +2 -0
  20. package/dist/api/driver/deepseek/error.mjs +2 -0
  21. package/dist/api/driver/deepseek/info.mjs +2 -0
  22. package/dist/api/driver/deepseek/request.mjs +2 -0
  23. package/dist/api/driver/deepseek/settings.mjs +2 -0
  24. package/dist/api/driver/llamacpp/chunk.mjs +2 -0
  25. package/dist/api/driver/llamacpp/detect.mjs +2 -0
  26. package/dist/api/driver/llamacpp/flush.mjs +2 -0
  27. package/dist/api/driver/llamacpp/info.mjs +2 -0
  28. package/dist/api/driver/llamacpp/request.mjs +2 -0
  29. package/dist/api/driver/llamacpp/response.mjs +2 -0
  30. package/dist/api/driver/llamacpp/settings.mjs +2 -0
  31. package/dist/api/driver/ollama/detect.mjs +2 -0
  32. package/dist/api/driver/ollama/info.mjs +2 -0
  33. package/dist/api/driver/ollama/request.mjs +2 -0
  34. package/dist/api/driver/ollama/settings.mjs +2 -0
  35. package/dist/api/driver/openai/detect.mjs +2 -0
  36. package/dist/api/driver/openai/info.mjs +2 -0
  37. package/dist/api/driver/openai/model/list.mjs +2 -0
  38. package/dist/api/driver/openai/request.mjs +2 -0
  39. package/dist/api/driver/openai/settings.mjs +2 -0
  40. package/dist/api/driver/sglang/detect.mjs +2 -0
  41. package/dist/api/driver/sglang/info.mjs +2 -0
  42. package/dist/api/driver/sglang/request.mjs +2 -0
  43. package/dist/api/driver/sglang/settings.mjs +2 -0
  44. package/dist/api/driver/vllm/detect.mjs +2 -0
  45. package/dist/api/driver/vllm/error.mjs +2 -0
  46. package/dist/api/driver/vllm/info.mjs +2 -0
  47. package/dist/api/driver/vllm/model/list.mjs +2 -0
  48. package/dist/api/driver/vllm/probe.mjs +2 -0
  49. package/dist/api/driver/vllm/request.mjs +2 -0
  50. package/dist/api/driver/vllm/settings.mjs +2 -0
  51. package/dist/api/driver/vllm/token/count.mjs +2 -0
  52. package/dist/api/endpoint/status.mjs +2 -0
  53. package/dist/api/error/build.mjs +2 -0
  54. package/dist/api/error/error.mjs +2 -0
  55. package/dist/api/model/get.mjs +2 -0
  56. package/dist/api/model/list.mjs +2 -0
  57. package/dist/api/request.mjs +3 -0
  58. package/dist/api/response.mjs +2 -0
  59. package/dist/api/route.mjs +2 -0
  60. package/dist/api/send.mjs +2 -0
  61. package/dist/api/server/plugin.mjs +2 -0
  62. package/dist/api/server/start.mjs +2 -0
  63. package/dist/api/server/stop.mjs +2 -0
  64. package/dist/api/sse/encode.mjs +4 -0
  65. package/dist/api/sse/parse.mjs +2 -0
  66. package/dist/api/sse/ping.mjs +2 -0
  67. package/dist/api/sse/write.mjs +2 -0
  68. package/dist/api/status.mjs +2 -0
  69. package/dist/api/stream.mjs +2 -0
  70. package/dist/api/token/count.mjs +2 -0
  71. package/dist/bin/llm-translator.mjs +3 -0
  72. package/dist/index.cjs +3 -3
  73. package/dist/index.mjs +1 -1
  74. package/dist/lib/arguments.mjs +2 -0
  75. package/dist/lib/backends.mjs +2 -0
  76. package/dist/lib/capture.mjs +2 -0
  77. package/dist/lib/cli.mjs +29 -0
  78. package/dist/lib/config.mjs +2 -0
  79. package/dist/lib/documents.mjs +12 -0
  80. package/dist/lib/errors.mjs +2 -0
  81. package/dist/lib/estimate.mjs +2 -0
  82. package/dist/lib/finish.mjs +2 -0
  83. package/dist/lib/http.mjs +2 -0
  84. package/dist/lib/ids.mjs +2 -0
  85. package/dist/lib/images.mjs +2 -0
  86. package/dist/lib/models.mjs +2 -0
  87. package/dist/lib/normalize.mjs +2 -0
  88. package/dist/lib/passthrough.mjs +2 -0
  89. package/dist/lib/pdf.mjs +2 -0
  90. package/dist/lib/ping.mjs +2 -0
  91. package/dist/lib/pool.mjs +2 -0
  92. package/dist/lib/route.mjs +2 -0
  93. package/dist/lib/routes.mjs +2 -0
  94. package/dist/lib/runtime.mjs +2 -0
  95. package/dist/lib/schema.mjs +2 -0
  96. package/dist/lib/settings.mjs +2 -0
  97. package/dist/lib/source.mjs +2 -0
  98. package/dist/lib/sse.mjs +10 -0
  99. package/dist/lib/thinking.mjs +3 -0
  100. package/dist/lib/tools.mjs +2 -0
  101. package/dist/lib/toolsearch.mjs +3 -0
  102. package/dist/lib/transport.mjs +2 -0
  103. package/dist/lib/upstream-error.mjs +2 -0
  104. package/dist/lib/usage.mjs +2 -0
  105. package/dist/lib/warn.mjs +2 -0
  106. package/package.json +40 -9
  107. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +19 -0
  108. package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +11 -0
  109. package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +15 -0
  110. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +16 -0
  111. package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +14 -0
  112. package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +24 -0
  113. package/types/src/api/detect.d.mts +7 -0
  114. package/types/src/api/driver/anthropic/detect.d.mts +26 -0
  115. package/types/src/api/driver/anthropic/info.d.mts +30 -0
  116. package/types/src/api/driver/anthropic/model/list.d.mts +6 -0
  117. package/types/src/api/driver/anthropic/settings.d.mts +23 -0
  118. package/types/src/api/driver/anthropic/token/count.d.mts +6 -0
  119. package/types/src/api/driver/compatible/detect.d.mts +9 -0
  120. package/types/src/api/driver/compatible/info.d.mts +12 -0
  121. package/types/src/api/driver/compatible/model/list.d.mts +6 -0
  122. package/types/src/api/driver/compatible/request.d.mts +6 -0
  123. package/types/src/api/driver/compatible/settings.d.mts +23 -0
  124. package/types/src/api/driver/deepseek/detect.d.mts +26 -0
  125. package/types/src/api/driver/deepseek/error.d.mts +14 -0
  126. package/types/src/api/driver/deepseek/info.d.mts +29 -0
  127. package/types/src/api/driver/deepseek/request.d.mts +8 -0
  128. package/types/src/api/driver/deepseek/settings.d.mts +25 -0
  129. package/types/src/api/driver/llamacpp/chunk.d.mts +7 -0
  130. package/types/src/api/driver/llamacpp/detect.d.mts +6 -0
  131. package/types/src/api/driver/llamacpp/flush.d.mts +26 -0
  132. package/types/src/api/driver/llamacpp/info.d.mts +31 -0
  133. package/types/src/api/driver/llamacpp/request.d.mts +8 -0
  134. package/types/src/api/driver/llamacpp/response.d.mts +8 -0
  135. package/types/src/api/driver/llamacpp/settings.d.mts +27 -0
  136. package/types/src/api/driver/ollama/detect.d.mts +6 -0
  137. package/types/src/api/driver/ollama/info.d.mts +30 -0
  138. package/types/src/api/driver/ollama/request.d.mts +8 -0
  139. package/types/src/api/driver/ollama/settings.d.mts +7 -0
  140. package/types/src/api/driver/openai/detect.d.mts +26 -0
  141. package/types/src/api/driver/openai/info.d.mts +29 -0
  142. package/types/src/api/driver/openai/model/list.d.mts +6 -0
  143. package/types/src/api/driver/openai/request.d.mts +6 -0
  144. package/types/src/api/driver/openai/settings.d.mts +23 -0
  145. package/types/src/api/driver/sglang/detect.d.mts +6 -0
  146. package/types/src/api/driver/sglang/info.d.mts +31 -0
  147. package/types/src/api/driver/sglang/request.d.mts +8 -0
  148. package/types/src/api/driver/sglang/settings.d.mts +24 -0
  149. package/types/src/api/driver/vllm/detect.d.mts +6 -0
  150. package/types/src/api/driver/vllm/error.d.mts +14 -0
  151. package/types/src/api/driver/vllm/info.d.mts +12 -0
  152. package/types/src/api/driver/vllm/model/list.d.mts +6 -0
  153. package/types/src/api/driver/vllm/probe.d.mts +25 -0
  154. package/types/src/api/driver/vllm/request.d.mts +8 -0
  155. package/types/src/api/driver/vllm/settings.d.mts +8 -0
  156. package/types/src/api/driver/vllm/token/count.d.mts +6 -0
  157. package/types/src/api/endpoint/status.d.mts +27 -0
  158. package/types/src/api/error/build.d.mts +12 -0
  159. package/types/src/api/error/error.d.mts +15 -0
  160. package/types/src/api/model/get.d.mts +9 -0
  161. package/types/src/api/model/list.d.mts +7 -0
  162. package/types/src/api/request.d.mts +26 -0
  163. package/types/src/api/response.d.mts +8 -0
  164. package/types/src/api/route.d.mts +18 -0
  165. package/types/src/api/send.d.mts +37 -0
  166. package/types/src/api/server/plugin.d.mts +11 -0
  167. package/types/src/api/server/start.d.mts +12 -0
  168. package/types/src/api/server/stop.d.mts +5 -0
  169. package/types/src/api/sse/encode.d.mts +12 -0
  170. package/types/src/api/sse/parse.d.mts +10 -0
  171. package/types/src/api/sse/ping.d.mts +16 -0
  172. package/types/src/api/sse/write.d.mts +12 -0
  173. package/types/src/api/status.d.mts +8 -0
  174. package/types/src/api/stream.d.mts +11 -0
  175. package/types/src/api/token/count.d.mts +10 -0
  176. package/types/src/index.d.mts +62 -15
  177. package/types/src/lib/arguments.d.mts +16 -0
  178. package/types/src/lib/backends.d.mts +21 -0
  179. package/types/src/lib/capture.d.mts +14 -0
  180. package/types/src/lib/cli.d.mts +35 -0
  181. package/types/src/lib/config.d.mts +51 -0
  182. package/types/src/lib/documents.d.mts +32 -0
  183. package/types/src/lib/errors.d.mts +144 -0
  184. package/types/src/lib/estimate.d.mts +75 -0
  185. package/types/src/lib/finish.d.mts +73 -0
  186. package/types/src/lib/http.d.mts +72 -0
  187. package/types/src/lib/ids.d.mts +46 -0
  188. package/types/src/lib/images.d.mts +39 -0
  189. package/types/src/lib/models.d.mts +25 -0
  190. package/types/src/lib/normalize.d.mts +37 -0
  191. package/types/src/lib/passthrough.d.mts +16 -0
  192. package/types/src/lib/pdf.d.mts +53 -0
  193. package/types/src/lib/ping.d.mts +10 -0
  194. package/types/src/lib/pool.d.mts +124 -0
  195. package/types/src/lib/route.d.mts +45 -0
  196. package/types/src/lib/routes.d.mts +17 -0
  197. package/types/src/lib/runtime.d.mts +10 -0
  198. package/types/src/lib/schema.d.mts +8 -0
  199. package/types/src/lib/settings.d.mts +189 -0
  200. package/types/src/lib/source.d.mts +32 -0
  201. package/types/src/lib/sse.d.mts +61 -0
  202. package/types/src/lib/thinking.d.mts +155 -0
  203. package/types/src/lib/tools.d.mts +28 -0
  204. package/types/src/lib/toolsearch.d.mts +91 -0
  205. package/types/src/lib/transport.d.mts +18 -0
  206. package/types/src/lib/upstream-error.d.mts +43 -0
  207. package/types/src/lib/usage.d.mts +91 -0
  208. package/types/src/lib/warn.d.mts +26 -0
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Parse the command line.
3
+ * @param {string[]} argv - Arguments without the node and script paths.
4
+ * @returns {any} Parsed values (see `parseArgs`).
5
+ */
6
+ export function parse(argv: string[]): any;
7
+ /**
8
+ * Merge flags, environment and the config file into `{ translator, server }`.
9
+ * @param {any} values - Parsed flags.
10
+ * @param {Record<string,string | undefined>} env - Environment.
11
+ * @param {any} file - Parsed config file ({} when none).
12
+ * @returns {{translator: any, server: any}} Translator config and server options.
13
+ */
14
+ export function merge(values: any, env: Record<string, string | undefined>, file: any): {
15
+ translator: any;
16
+ server: any;
17
+ };
18
+ /**
19
+ * Run the CLI. Resolves with the exit code once the server stopped (after a signal) or startup failed.
20
+ * @param {string[]} argv - Arguments without the node and script paths.
21
+ * @param {Record<string,string | undefined>} [env] - Environment.
22
+ * @param {typeof processIo} [io] - Output and signal bindings.
23
+ * @param {any} [lib] - Overridable collaborators `{ createTranslator, version }` (loaded lazily when omitted).
24
+ * @returns {Promise<number>} Process exit code.
25
+ */
26
+ export function main(argv: string[], env?: Record<string, string | undefined>, io?: typeof processIo, lib?: any): Promise<number>;
27
+ /**
28
+ * Default I/O bindings for `main`.
29
+ * @type {{out: (text: string) => void, err: (text: string) => void, onSignal: (handler: () => void) => void}}
30
+ */
31
+ export const processIo: {
32
+ out: (text: string) => void;
33
+ err: (text: string) => void;
34
+ onSignal: (handler: () => void) => void;
35
+ };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Normalize one upstream entry into the runtime endpoint record.
3
+ * @param {string | UpstreamConfig} entry - Upstream URL or config object.
4
+ * @param {number} index - Position in the list.
5
+ * @returns {any} Runtime endpoint.
6
+ */
7
+ export function endpoint(entry: string | UpstreamConfig, index: number): any;
8
+ /**
9
+ * Validate and normalize the translator config.
10
+ * @param {any} [config] - Raw config passed to `createTranslator`.
11
+ * @returns {any} Normalized config (a fresh any; the input is not mutated).
12
+ */
13
+ export function normalize(config?: any): any;
14
+ export type UpstreamConfig = {
15
+ /**
16
+ * Display name, defaults to `upstream-<index>`.
17
+ */
18
+ name?: string;
19
+ /**
20
+ * Base URL of the upstream server (`http://host:8000`).
21
+ */
22
+ url: string;
23
+ /**
24
+ * Key sent to the upstream.
25
+ */
26
+ apiKey?: string;
27
+ /**
28
+ * Extra headers sent to the upstream.
29
+ */
30
+ headers?: Record<string, string>;
31
+ /**
32
+ * Driver name, or `"auto"` (default) to detect.
33
+ */
34
+ backend?: string;
35
+ /**
36
+ * Models this upstream serves (routing).
37
+ */
38
+ models?: string[];
39
+ /**
40
+ * Marks the fallback upstream.
41
+ */
42
+ default?: boolean;
43
+ /**
44
+ * Settings overrides for this upstream.
45
+ */
46
+ settings?: object;
47
+ /**
48
+ * Server version (skips nothing, but feeds version-mapped driver settings).
49
+ */
50
+ version?: string;
51
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Context for converting documents.
3
+ * @typedef {object} DocumentContext
4
+ * @property {import("./settings.mjs").Settings} settings - Resolved settings.
5
+ * @property {Map<string, any>} toolInputs - `tool_use` inputs by id, to recover file paths.
6
+ * @property {string|null} toolUseId - Id of the tool call whose result contains the document, if any.
7
+ */
8
+ /**
9
+ * Convert a document block into Anthropic text/image blocks.
10
+ * @param {any} block - Anthropic `document` block.
11
+ * @param {DocumentContext} ctx - Conversion context.
12
+ * @returns {Promise<any[]>} Content blocks (`text` and `image`).
13
+ * @throws {TranslatorError} When `documents` is `"error"` or a PDF violates the size/page limits.
14
+ */
15
+ export function expand(block: any, ctx: DocumentContext): Promise<any[]>;
16
+ /**
17
+ * Context for converting documents.
18
+ */
19
+ export type DocumentContext = {
20
+ /**
21
+ * - Resolved settings.
22
+ */
23
+ settings: import("./settings.mjs").Settings;
24
+ /**
25
+ * - `tool_use` inputs by id, to recover file paths.
26
+ */
27
+ toolInputs: Map<string, any>;
28
+ /**
29
+ * - Id of the tool call whose result contains the document, if any.
30
+ */
31
+ toolUseId: string | null;
32
+ };
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Anthropic error type for an HTTP status.
3
+ * @param {number} status - HTTP status code.
4
+ * @returns {string} Anthropic error type.
5
+ */
6
+ export function statusToAnthropicType(status: number): string;
7
+ /**
8
+ * HTTP status for an Anthropic error type.
9
+ * @param {string} type - Anthropic error type.
10
+ * @returns {number} HTTP status code.
11
+ */
12
+ export function anthropicTypeToStatus(type: string): number;
13
+ /**
14
+ * OpenAI style error type for an Anthropic error type.
15
+ * @param {string} type - Anthropic error type.
16
+ * @returns {string} OpenAI error type.
17
+ */
18
+ export function anthropicToOpenAIType(type: string): string;
19
+ /**
20
+ * Anthropic error type for an OpenAI style error object (type and/or numeric code and/or HTTP status).
21
+ * @param {{ type?: string|null, code?: string|number|null }} error - OpenAI error object.
22
+ * @param {number} [status] - HTTP status of the reply, when known.
23
+ * @returns {string} Anthropic error type.
24
+ */
25
+ export function openaiToAnthropicType(error: {
26
+ type?: string | null;
27
+ code?: string | number | null;
28
+ }, status?: number): string;
29
+ /**
30
+ * Build an Anthropic error body.
31
+ * @param {string} type - Anthropic error type.
32
+ * @param {string} message - Error message.
33
+ * @returns {{ type: "error", error: { type: string, message: string } }} Anthropic error body.
34
+ */
35
+ export function anthropicError(type: string, message: string): {
36
+ type: "error";
37
+ error: {
38
+ type: string;
39
+ message: string;
40
+ };
41
+ };
42
+ /**
43
+ * Build an OpenAI error body.
44
+ * @param {string} message - Error message.
45
+ * @param {{ type?: string, param?: string|null, code?: string|number|null }} [fields] - Optional fields.
46
+ * @returns {{ error: { message: string, type: string, param: string|null, code: string|number|null } }} OpenAI error body.
47
+ */
48
+ export function openaiError(message: string, { type, param, code }?: {
49
+ type?: string;
50
+ param?: string | null;
51
+ code?: string | number | null;
52
+ }): {
53
+ error: {
54
+ message: string;
55
+ type: string;
56
+ param: string | null;
57
+ code: string | number | null;
58
+ };
59
+ };
60
+ /**
61
+ * Detect an OpenAI/vLLM "context length exceeded" message and rewrite it in Anthropic's wording
62
+ * (`prompt is too long: <n> tokens > <max> maximum`), which Claude Code recognises and answers with
63
+ * auto-compaction. `n` is the total the request asked for (prompt plus requested completion) when the message
64
+ * reports it, otherwise the prompt size, and `max + 1` when the message gives no size, so `n` always exceeds the maximum.
65
+ * @param {string} message - Upstream error message.
66
+ * @returns {string|null} Rewritten message, or null when the message is not a context overflow.
67
+ */
68
+ export function rewriteContextOverflow(message: string): string | null;
69
+ /**
70
+ * Token counts from a context-overflow message.
71
+ * @param {string} message - Upstream error message.
72
+ * @returns {{ prompt: number, max: number }|null} Counts (`prompt` is the requested total, or the prompt size, or `max + 1`), or null when not an overflow.
73
+ */
74
+ export function contextOverflow(message: string): {
75
+ prompt: number;
76
+ max: number;
77
+ } | null;
78
+ /**
79
+ *
80
+ * @Project: @cldmv/llm-translator
81
+ * @Filename: /src/lib/errors.mjs
82
+ * @Date: 2026-10-25T12:29:30-07:00 (1792956570)
83
+ * @Author: Nate Corcoran <CLDMV>
84
+ * @Email: <Shinrai@users.noreply.github.com>
85
+ * -----
86
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
87
+ * @Last modified time: 2026-10-25T13:31:30-07:00 (1792960290)
88
+ * -----
89
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
90
+ *
91
+ */
92
+ /**
93
+ * @fileoverview Error type shared by every converter, plus helpers that render an error in either wire protocol.
94
+ */
95
+ /**
96
+ * Error raised for requests or upstream replies the translator cannot (or will not) convert. Carries enough
97
+ * information to be rendered as an Anthropic or an OpenAI error body.
98
+ */
99
+ export class TranslatorError extends Error {
100
+ /**
101
+ * @param {string} message - Human readable message (sent to the client).
102
+ * @param {object} [options] - Error metadata.
103
+ * @param {string} [options.code] - Stable machine readable code (for example `"unsupported_field"`).
104
+ * @param {number} [options.status] - HTTP status to answer with.
105
+ * @param {string} [options.type] - Anthropic style error type.
106
+ * @param {string|null} [options.param] - Offending request parameter, when known.
107
+ * @param {unknown} [options.details] - Extra data for logs and tests.
108
+ */
109
+ constructor(message: string, options?: {
110
+ code?: string;
111
+ status?: number;
112
+ type?: string;
113
+ param?: string | null;
114
+ details?: unknown;
115
+ });
116
+ code: string;
117
+ status: number;
118
+ type: string;
119
+ param: string;
120
+ details: unknown;
121
+ /**
122
+ * Render as an Anthropic error body.
123
+ * @returns {{ type: "error", error: { type: string, message: string } }} Anthropic error body.
124
+ */
125
+ toAnthropic(): {
126
+ type: "error";
127
+ error: {
128
+ type: string;
129
+ message: string;
130
+ };
131
+ };
132
+ /**
133
+ * Render as an OpenAI error body.
134
+ * @returns {{ error: { message: string, type: string, param: string|null, code: string|number|null } }} OpenAI error body.
135
+ */
136
+ toOpenAI(): {
137
+ error: {
138
+ message: string;
139
+ type: string;
140
+ param: string | null;
141
+ code: string | number | null;
142
+ };
143
+ };
144
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Rough prompt size: one token per four characters of the serialized system prompt, messages and tools.
3
+ * @param {any} body - Upstream request body.
4
+ * @returns {number} Estimated tokens.
5
+ */
6
+ export function estimate(body: any): number;
7
+ /**
8
+ * Characters of generated output in one client-protocol stream event.
9
+ * @param {"anthropic" | "openai"} front - Client protocol.
10
+ * @param {any} data - Event data.
11
+ * @returns {number} Character count.
12
+ */
13
+ export function eventLength(front: "anthropic" | "openai", data: any): number;
14
+ /**
15
+ * Characters of generated output in a complete client-protocol reply body.
16
+ * @param {"anthropic" | "openai"} front - Client protocol.
17
+ * @param {any} body - Reply body.
18
+ * @returns {number} Character count.
19
+ */
20
+ export function bodyLength(front: "anthropic" | "openai", body: any): number;
21
+ /**
22
+ * Write estimated counts into a client-protocol usage object, replacing the zeros a converter wrote for usage the upstream did not carry.
23
+ * Only the sides present in `counts` (a number rather than null) are written; the other side keeps the real value the converter converted.
24
+ * On the OpenAI front `total_tokens` is then the sum of the final prompt and completion counts.
25
+ * @param {"anthropic" | "openai"} front - Client protocol.
26
+ * @param {any} usage - Client usage object (the converter's), or undefined.
27
+ * @param {{ input_tokens: number | null, output_tokens: number | null }} counts - Estimates; null for a side that was reported.
28
+ * @returns {any} A new usage object.
29
+ */
30
+ export function patch(front: "anthropic" | "openai", usage: any, counts: {
31
+ input_tokens: number | null;
32
+ output_tokens: number | null;
33
+ }): any;
34
+ /**
35
+ * Pass a converted client stream through and, when the upstream reported no usage, write estimates into its final usage: the
36
+ * `message_delta` event on the Anthropic front, the usage-only chunk on the OpenAI front. Nothing else changes. The input estimate is
37
+ * resolved when that event arrives (it may call the server's tokenizer), so a stream with real usage never pays for it.
38
+ * @param {"anthropic" | "openai"} front - Client protocol.
39
+ * @param {AsyncIterable<any>} events - Client events from the converter.
40
+ * @param {{ gaps: () => { input: boolean, output: boolean }, input: () => Promise<number>, report: (counts: { input_tokens: number | null, output_tokens: number | null }) => void }} hooks - `gaps()` says which sides the upstream did not report (valid once the converter reached its final usage), `input()` resolves the input estimate and `report(counts)` receives the estimates that were written (null for a side that was real).
41
+ * @returns {AsyncGenerator<any, void, undefined>} Equivalent events.
42
+ */
43
+ export function stream(front: "anthropic" | "openai", events: AsyncIterable<any>, hooks: {
44
+ gaps: () => {
45
+ input: boolean;
46
+ output: boolean;
47
+ };
48
+ input: () => Promise<number>;
49
+ report: (counts: {
50
+ input_tokens: number | null;
51
+ output_tokens: number | null;
52
+ }) => void;
53
+ }): AsyncGenerator<any, void, undefined>;
54
+ /**
55
+ *
56
+ * @Project: @cldmv/llm-translator
57
+ * @Filename: /src/lib/estimate.mjs
58
+ * @Date: 2026-10-25T12:29:36-07:00 (1792956576)
59
+ * @Author: Nate Corcoran <CLDMV>
60
+ * @Email: <Shinrai@users.noreply.github.com>
61
+ * -----
62
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
63
+ * @Last modified time: 2026-10-25T12:59:01-07:00 (1792958341)
64
+ * -----
65
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
66
+ *
67
+ */
68
+ /**
69
+ * @fileoverview Token estimates for upstreams that cannot count tokens or did not report usage.
70
+ * @module @cldmv/llm-translator/lib/estimate
71
+ */
72
+ /** Characters per token in every estimate. */
73
+ export const CHARS_PER_TOKEN: 4;
74
+ export function tokens(characters: number): number;
75
+ export function output(characters: number): number;
@@ -0,0 +1,73 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/lib/finish.mjs
5
+ * @Date: 2026-10-25T12:28:19-07:00 (1792956499)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-10-25T12:59:01-07:00 (1792958341)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+ /**
16
+ * @fileoverview Stop-reason and usage mapping between the two protocols.
17
+ */
18
+ /**
19
+ * Anthropic stop reason (and stop sequence) for an OpenAI choice.
20
+ * @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
21
+ * @param {{ hasToolCalls: boolean, matchedStop?: unknown, argumentsComplete?: boolean }} info - Whether tool calls were produced, vLLM's `stop_reason` (a string for a matched stop string), and whether the last tool call's arguments are complete. A `length` stop with incomplete arguments is `max_tokens`, not `tool_use`.
22
+ * @returns {{ stop_reason: string, stop_sequence: string|null }} Anthropic stop fields.
23
+ */
24
+ export function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete }: {
25
+ hasToolCalls: boolean;
26
+ matchedStop?: unknown;
27
+ argumentsComplete?: boolean;
28
+ }): {
29
+ stop_reason: string;
30
+ stop_sequence: string | null;
31
+ };
32
+ /**
33
+ * OpenAI `finish_reason` for an Anthropic stop reason.
34
+ * @param {string|null|undefined} stopReason - Anthropic `stop_reason`.
35
+ * @returns {string} OpenAI finish reason.
36
+ */
37
+ export function toOpenAIFinish(stopReason: string | null | undefined): string;
38
+ /**
39
+ * Anthropic usage from an OpenAI usage object. Cached prompt tokens (reported by vLLM only with prompt-token
40
+ * details enabled) are reported as `cache_read_input_tokens` and subtracted from `input_tokens`.
41
+ * @param {{ prompt_tokens?: number, completion_tokens?: number, prompt_tokens_details?: { cached_tokens?: number }|null }|null|undefined} usage - OpenAI usage.
42
+ * @returns {{ input_tokens: number, output_tokens: number, cache_creation_input_tokens: number, cache_read_input_tokens: number }} Anthropic usage.
43
+ */
44
+ export function toAnthropicUsage(usage: {
45
+ prompt_tokens?: number;
46
+ completion_tokens?: number;
47
+ prompt_tokens_details?: {
48
+ cached_tokens?: number;
49
+ } | null;
50
+ } | null | undefined): {
51
+ input_tokens: number;
52
+ output_tokens: number;
53
+ cache_creation_input_tokens: number;
54
+ cache_read_input_tokens: number;
55
+ };
56
+ /**
57
+ * OpenAI usage from an Anthropic usage object (cache tokens count as prompt tokens).
58
+ * @param {{ input_tokens?: number, output_tokens?: number, cache_creation_input_tokens?: number|null, cache_read_input_tokens?: number|null }|null|undefined} usage - Anthropic usage.
59
+ * @returns {{ prompt_tokens: number, completion_tokens: number, total_tokens: number, prompt_tokens_details?: { cached_tokens: number } }} OpenAI usage.
60
+ */
61
+ export function toOpenAIUsage(usage: {
62
+ input_tokens?: number;
63
+ output_tokens?: number;
64
+ cache_creation_input_tokens?: number | null;
65
+ cache_read_input_tokens?: number | null;
66
+ } | null | undefined): {
67
+ prompt_tokens: number;
68
+ completion_tokens: number;
69
+ total_tokens: number;
70
+ prompt_tokens_details?: {
71
+ cached_tokens: number;
72
+ };
73
+ };
@@ -0,0 +1,72 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/lib/http.mjs
5
+ * @Date: 2026-10-25T12:29:36-07:00 (1792956576)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-10-25T12:59:02-07:00 (1792958342)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+ /**
16
+ * @fileoverview Small HTTP helpers shared by the api leaves: URL joining, auth headers, JSON requests.
17
+ * @module @cldmv/llm-translator/lib/http
18
+ */
19
+ /**
20
+ * Join an endpoint base URL and a path.
21
+ * @param {string} base - Base URL without trailing slash.
22
+ * @param {string} path - Path starting with `/`.
23
+ * @returns {string} Absolute URL.
24
+ */
25
+ export function join(base: string, path: string): string;
26
+ /**
27
+ * Whether a listen address only accepts connections from this machine.
28
+ * @param {string} host - Host or address.
29
+ * @returns {boolean} True for `localhost`, `127.x.x.x` and `::1`.
30
+ */
31
+ export function isLoopback(host: string): boolean;
32
+ /**
33
+ * Build the request headers for an upstream call.
34
+ * @param {any} endpoint - Runtime endpoint.
35
+ * @param {string} [auth] - `"bearer"` (default) or `"x-api-key"`.
36
+ * @param {Record<string,string>} [extra] - Extra headers applied last.
37
+ * @returns {Record<string,string>} Header map.
38
+ */
39
+ export function headers(endpoint: any, auth?: string, extra?: Record<string, string>): Record<string, string>;
40
+ /**
41
+ * GET a path of the endpoint for detection. Within one detection pass (`context.probes` is a shared `Map`) each path is requested
42
+ * once and every driver that asks for it gets the same result, so a path fetched for one backend (for example `/v1/models`) is
43
+ * not fetched again for the next. Never rejects; treat the result as read-only.
44
+ * @param {any} context - `{ endpoint, transport, signal, timeout, probes? }`.
45
+ * @param {string} path - Path starting with `/`.
46
+ * @returns {Promise<{ok: boolean, status: number, body: any, error?: Error}>} Outcome (see `json`).
47
+ */
48
+ export function probe({ endpoint, transport, signal, timeout, probes }: any, path: string): Promise<{
49
+ ok: boolean;
50
+ status: number;
51
+ body: any;
52
+ error?: Error;
53
+ }>;
54
+ /**
55
+ * Convert a fetch `Headers` object (or plain object) to a lowercase plain object.
56
+ * @param {Headers | Record<string,string> | undefined} source - Header source.
57
+ * @returns {Record<string,string>} Plain header map.
58
+ */
59
+ export function plain(source: Headers | Record<string, string> | undefined): Record<string, string>;
60
+ /**
61
+ * Perform a request and parse the JSON body. Never throws: network failures and non-JSON bodies are reported in the result.
62
+ * @param {Function} transport - fetch-compatible function.
63
+ * @param {string} url - Absolute URL.
64
+ * @param {any} [options] - `{ method, headers, body, signal, timeout }`.
65
+ * @returns {Promise<{ok: boolean, status: number, body: any, error?: Error}>} Outcome.
66
+ */
67
+ export function json(transport: Function, url: string, options?: any): Promise<{
68
+ ok: boolean;
69
+ status: number;
70
+ body: any;
71
+ error?: Error;
72
+ }>;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Anthropic style message id.
3
+ * @returns {string} `msg_...` id.
4
+ */
5
+ export function messageId(): string;
6
+ /**
7
+ * Anthropic style tool-use id.
8
+ * @returns {string} `toolu_...` id.
9
+ */
10
+ export function toolUseId(): string;
11
+ /**
12
+ * OpenAI style completion id.
13
+ * @returns {string} `chatcmpl-...` id.
14
+ */
15
+ export function completionId(): string;
16
+ /**
17
+ * OpenAI style tool-call id, for upstream tool calls that carry none.
18
+ * @returns {string} `call_...` id.
19
+ */
20
+ export function callId(): string;
21
+ /**
22
+ * Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). Valid ids pass through unchanged, invalid characters
23
+ * (for example kimi's `functions.name:0`) become `_`, and a missing or empty id is replaced by a fresh one. The
24
+ * mapping is deterministic so a tool call and its later tool result agree.
25
+ * @param {unknown} id - Upstream id.
26
+ * @returns {string} Anthropic-valid id.
27
+ */
28
+ export function anthropicToolId(id: unknown): string;
29
+ /**
30
+ * Create a tool-name fitter. Names that already satisfy `pattern` and `max` pass through; others are sanitized and,
31
+ * when too long, shortened with a hash suffix. Every changed name is recorded in `map` (`fitted -> original`) so the
32
+ * original can be restored on the way back. Fitted names are kept unique.
33
+ * @param {{ max: number, map?: Record<string, string> }} options - Maximum length and the (JSON-serializable) map to record into.
34
+ * @returns {(name: string) => string} Fitter function.
35
+ */
36
+ export function createNameFitter({ max, map }: {
37
+ max: number;
38
+ map?: Record<string, string>;
39
+ }): (name: string) => string;
40
+ /**
41
+ * Original tool name for a fitted one (names that were never changed map to themselves).
42
+ * @param {Record<string, string>} map - `fitted -> original` map from the conversion state.
43
+ * @param {string} name - Name seen on the wire.
44
+ * @returns {string} Original name.
45
+ */
46
+ export function restoreName(map: Record<string, string>, name: string): string;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Does an upstream error message say the model does not accept images?
3
+ * @param {string} message - Upstream error message.
4
+ * @returns {boolean} True for a known "no image input" wording.
5
+ */
6
+ export function refused(message: string): boolean;
7
+ /**
8
+ * Does a Chat Completions body carry image parts?
9
+ * @param {any} body - Upstream request body.
10
+ * @returns {boolean} True when any message content has an `image_url` part.
11
+ */
12
+ export function carries(body: any): boolean;
13
+ /**
14
+ * Copy a Chat Completions body with every image part replaced by a short text note.
15
+ * @param {any} body - Upstream request body.
16
+ * @returns {any} New body; messages without images are shared with the input.
17
+ */
18
+ export function strip(body: any): any;
19
+ /**
20
+ *
21
+ * @Project: @cldmv/llm-translator
22
+ * @Filename: /src/lib/images.mjs
23
+ * @Date: 2026-10-26T06:00:20-07:00 (1793019620)
24
+ * @Author: Nate Corcoran <CLDMV>
25
+ * @Email: <Shinrai@users.noreply.github.com>
26
+ * -----
27
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
28
+ * @Last modified time: 2026-10-26T15:38:55-07:00 (1793054335)
29
+ * -----
30
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
31
+ *
32
+ */
33
+ /**
34
+ * @fileoverview Image handling for upstreams that turn out to be text-only: recognising the refusal, finding images in a
35
+ * Chat Completions body and replacing them with a note.
36
+ * @module @cldmv/llm-translator/lib/images
37
+ */
38
+ /** Text that replaces an image when the model does not accept images. */
39
+ export const NOTE: "[An image was omitted because this model does not accept images.]";
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The usable entries of a `/v1/models` reply.
3
+ * @param {{ok: boolean, body: any}} result - Outcome of the request.
4
+ * @returns {{id: string, created?: number, owned_by?: string, max_model_len?: number}[] | null} Models, or null when the reply is not a model list.
5
+ */
6
+ export function modelsOf(result: {
7
+ ok: boolean;
8
+ body: any;
9
+ }): {
10
+ id: string;
11
+ created?: number;
12
+ owned_by?: string;
13
+ max_model_len?: number;
14
+ }[] | null;
15
+ /**
16
+ * Fetch and normalize the models an upstream reports.
17
+ * @param {any} context - `{ endpoint, transport, signal, timeout, auth, extraHeaders }`.
18
+ * @returns {Promise<{id: string, created?: number, owned_by?: string, max_model_len?: number}[] | null>} Models, or null when the upstream did not answer with a model list.
19
+ */
20
+ export function fetchModels({ endpoint, transport, signal, timeout, auth, extraHeaders }: any): Promise<{
21
+ id: string;
22
+ created?: number;
23
+ owned_by?: string;
24
+ max_model_len?: number;
25
+ }[] | null>;
@@ -0,0 +1,37 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/lib/normalize.mjs
5
+ * @Date: 2026-10-25T12:29:36-07:00 (1792956576)
6
+ * @Author: Nate Corcoran <CLDMV>
7
+ * @Email: <Shinrai@users.noreply.github.com>
8
+ * -----
9
+ * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
10
+ * @Last modified time: 2026-10-25T12:59:02-07:00 (1792958342)
11
+ * -----
12
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
+ *
14
+ */
15
+ /**
16
+ * @fileoverview Request normalizers shared by the OpenAI-protocol drivers.
17
+ * @module @cldmv/llm-translator/lib/normalize
18
+ */
19
+ /**
20
+ * Remove tool fields that strict OpenAI-compatible servers reject when no tools are present.
21
+ * @param {any} body - Chat Completions request body (mutated copy is returned).
22
+ * @returns {any} Normalized body.
23
+ */
24
+ export function noEmptyTools(body: any): any;
25
+ /**
26
+ * Parse a server version string (`0.19.0rc1`, `0.32.0+cu126`) into numeric parts.
27
+ * @param {string | undefined} version - Version string.
28
+ * @returns {number[] | null} `[major, minor, patch]`, or null when unparseable.
29
+ */
30
+ export function semver(version: string | undefined): number[] | null;
31
+ /**
32
+ * Is `version` at least `minimum`? An unknown version counts as new (latest behavior).
33
+ * @param {string | undefined} version - Version under test.
34
+ * @param {string} minimum - Minimum version such as `"0.19.0"`.
35
+ * @returns {boolean} True when the version is unknown or >= minimum.
36
+ */
37
+ export function atLeast(version: string | undefined, minimum: string): boolean;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Keep only known Chat Completions fields and the extension fields the upstream's driver allows, so an untrusted client cannot inject server-side
3
+ * controls such as `kv_transfer_params`, `vllm_xargs`, `chat_template`, `cache_salt` or `priority`. `stream_options` is
4
+ * reduced to the usage flags. A driver allows more through `info().fields` (vLLM's list for `vllm` and `compatible`, llama.cpp's
5
+ * `repeat_penalty` and `grammar`, SGLang's `regex`, `ebnf` and `separate_reasoning`, and so on), so a field is forwarded only to a backend that
6
+ * understands it.
7
+ * @param {any} body - Client request body.
8
+ * @param {string[]} [extra] - Extra field names the upstream's driver allows.
9
+ * @returns {any} Filtered copy.
10
+ */
11
+ export function filterChatBody(body: any, extra?: string[]): any;
12
+ /**
13
+ * Documented vLLM sampling, template and reasoning extensions. The `vllm` and `compatible` drivers list them in `info().fields`; a backend
14
+ * that does not accept them (api.openai.com, DeepSeek, Anthropic, Ollama) does not, so they are stripped for it.
15
+ */
16
+ export const VLLM_FIELDS: string[];