@cldmv/llm-translator 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/README.md +12 -6
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +11 -11
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +1 -1
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +1 -1
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +3 -3
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  8. package/dist/api/detect.mjs +1 -1
  9. package/dist/api/driver/anthropic/token/count.mjs +1 -1
  10. package/dist/api/driver/compatible/detect.mjs +1 -1
  11. package/dist/api/driver/compatible/info.mjs +1 -1
  12. package/dist/api/driver/deepseek/detect.mjs +2 -0
  13. package/dist/api/driver/deepseek/error.mjs +2 -0
  14. package/dist/api/driver/deepseek/info.mjs +2 -0
  15. package/dist/api/driver/deepseek/request.mjs +2 -0
  16. package/dist/api/driver/deepseek/settings.mjs +2 -0
  17. package/dist/api/driver/llamacpp/chunk.mjs +2 -0
  18. package/dist/api/driver/llamacpp/detect.mjs +2 -0
  19. package/dist/api/driver/llamacpp/flush.mjs +2 -0
  20. package/dist/api/driver/llamacpp/info.mjs +2 -0
  21. package/dist/api/driver/llamacpp/request.mjs +2 -0
  22. package/dist/api/driver/llamacpp/response.mjs +2 -0
  23. package/dist/api/driver/llamacpp/settings.mjs +2 -0
  24. package/dist/api/driver/ollama/detect.mjs +2 -0
  25. package/dist/api/driver/ollama/info.mjs +2 -0
  26. package/dist/api/driver/ollama/request.mjs +2 -0
  27. package/dist/api/driver/ollama/settings.mjs +2 -0
  28. package/dist/api/driver/sglang/detect.mjs +2 -0
  29. package/dist/api/driver/sglang/info.mjs +2 -0
  30. package/dist/api/driver/sglang/request.mjs +2 -0
  31. package/dist/api/driver/sglang/settings.mjs +2 -0
  32. package/dist/api/driver/vllm/detect.mjs +1 -1
  33. package/dist/api/driver/vllm/info.mjs +1 -1
  34. package/dist/api/endpoint/status.mjs +2 -0
  35. package/dist/api/error/error.mjs +1 -1
  36. package/dist/api/model/get.mjs +2 -0
  37. package/dist/api/request.mjs +2 -2
  38. package/dist/api/route.mjs +2 -0
  39. package/dist/api/send.mjs +1 -1
  40. package/dist/api/server/plugin.mjs +1 -1
  41. package/dist/api/server/start.mjs +1 -1
  42. package/dist/api/status.mjs +1 -1
  43. package/dist/api/stream.mjs +1 -1
  44. package/dist/index.cjs +3 -3
  45. package/dist/index.mjs +1 -1
  46. package/dist/lib/backends.mjs +2 -0
  47. package/dist/lib/capture.mjs +1 -1
  48. package/dist/lib/cli.mjs +1 -1
  49. package/dist/lib/config.mjs +1 -1
  50. package/dist/lib/estimate.mjs +1 -1
  51. package/dist/lib/http.mjs +1 -1
  52. package/dist/lib/ids.mjs +1 -1
  53. package/dist/lib/models.mjs +1 -1
  54. package/dist/lib/passthrough.mjs +1 -1
  55. package/dist/lib/pool.mjs +2 -0
  56. package/dist/lib/route.mjs +1 -1
  57. package/dist/lib/routes.mjs +1 -1
  58. package/dist/lib/settings.mjs +1 -1
  59. package/dist/lib/thinking.mjs +2 -1
  60. package/dist/lib/tools.mjs +2 -0
  61. package/dist/lib/toolsearch.mjs +3 -0
  62. package/dist/lib/transport.mjs +1 -1
  63. package/dist/lib/usage.mjs +2 -0
  64. package/package.json +17 -7
  65. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -15
  66. package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +3 -16
  67. package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +1 -15
  68. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +1 -15
  69. package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +3 -17
  70. package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +1 -15
  71. package/types/src/api/detect.d.mts +1 -15
  72. package/types/src/api/driver/anthropic/detect.d.mts +1 -1
  73. package/types/src/api/driver/anthropic/info.d.mts +1 -1
  74. package/types/src/api/driver/anthropic/model/list.d.mts +1 -15
  75. package/types/src/api/driver/anthropic/settings.d.mts +1 -1
  76. package/types/src/api/driver/anthropic/token/count.d.mts +1 -15
  77. package/types/src/api/driver/compatible/detect.d.mts +2 -16
  78. package/types/src/api/driver/compatible/info.d.mts +3 -20
  79. package/types/src/api/driver/compatible/model/list.d.mts +1 -15
  80. package/types/src/api/driver/compatible/request.d.mts +1 -15
  81. package/types/src/api/driver/compatible/settings.d.mts +1 -1
  82. package/types/src/api/driver/deepseek/detect.d.mts +26 -0
  83. package/types/src/api/driver/deepseek/error.d.mts +14 -0
  84. package/types/src/api/driver/deepseek/info.d.mts +29 -0
  85. package/types/src/api/driver/deepseek/request.d.mts +8 -0
  86. package/types/src/api/driver/deepseek/settings.d.mts +25 -0
  87. package/types/src/api/driver/llamacpp/chunk.d.mts +7 -0
  88. package/types/src/api/driver/llamacpp/detect.d.mts +6 -0
  89. package/types/src/api/driver/llamacpp/flush.d.mts +26 -0
  90. package/types/src/api/driver/llamacpp/info.d.mts +31 -0
  91. package/types/src/api/driver/llamacpp/request.d.mts +8 -0
  92. package/types/src/api/driver/llamacpp/response.d.mts +8 -0
  93. package/types/src/api/driver/llamacpp/settings.d.mts +27 -0
  94. package/types/src/api/driver/ollama/detect.d.mts +6 -0
  95. package/types/src/api/driver/ollama/info.d.mts +30 -0
  96. package/types/src/api/driver/ollama/request.d.mts +8 -0
  97. package/types/src/api/driver/ollama/settings.d.mts +7 -0
  98. package/types/src/api/driver/openai/detect.d.mts +1 -1
  99. package/types/src/api/driver/openai/info.d.mts +1 -1
  100. package/types/src/api/driver/openai/model/list.d.mts +1 -15
  101. package/types/src/api/driver/openai/request.d.mts +1 -15
  102. package/types/src/api/driver/openai/settings.d.mts +1 -1
  103. package/types/src/api/driver/sglang/detect.d.mts +6 -0
  104. package/types/src/api/driver/sglang/info.d.mts +31 -0
  105. package/types/src/api/driver/sglang/request.d.mts +8 -0
  106. package/types/src/api/driver/sglang/settings.d.mts +24 -0
  107. package/types/src/api/driver/vllm/detect.d.mts +3 -17
  108. package/types/src/api/driver/vllm/error.d.mts +1 -15
  109. package/types/src/api/driver/vllm/info.d.mts +3 -20
  110. package/types/src/api/driver/vllm/model/list.d.mts +1 -15
  111. package/types/src/api/driver/vllm/probe.d.mts +1 -15
  112. package/types/src/api/driver/vllm/request.d.mts +1 -15
  113. package/types/src/api/driver/vllm/settings.d.mts +1 -15
  114. package/types/src/api/driver/vllm/token/count.d.mts +1 -15
  115. package/types/src/api/endpoint/status.d.mts +27 -0
  116. package/types/src/api/error/build.d.mts +1 -15
  117. package/types/src/api/error/error.d.mts +1 -15
  118. package/types/src/api/model/get.d.mts +9 -0
  119. package/types/src/api/model/list.d.mts +1 -15
  120. package/types/src/api/request.d.mts +8 -17
  121. package/types/src/api/response.d.mts +1 -15
  122. package/types/src/api/route.d.mts +18 -0
  123. package/types/src/api/send.d.mts +21 -16
  124. package/types/src/api/server/plugin.d.mts +6 -19
  125. package/types/src/api/server/start.d.mts +2 -16
  126. package/types/src/api/server/stop.d.mts +1 -15
  127. package/types/src/api/sse/encode.d.mts +1 -15
  128. package/types/src/api/sse/parse.d.mts +1 -15
  129. package/types/src/api/sse/ping.d.mts +1 -15
  130. package/types/src/api/sse/write.d.mts +1 -15
  131. package/types/src/api/status.d.mts +2 -16
  132. package/types/src/api/stream.d.mts +1 -15
  133. package/types/src/api/token/count.d.mts +1 -15
  134. package/types/src/index.d.mts +24 -34
  135. package/types/src/lib/arguments.d.mts +2 -16
  136. package/types/src/lib/backends.d.mts +21 -0
  137. package/types/src/lib/capture.d.mts +2 -16
  138. package/types/src/lib/cli.d.mts +12 -26
  139. package/types/src/lib/config.d.mts +11 -25
  140. package/types/src/lib/documents.d.mts +16 -27
  141. package/types/src/lib/errors.d.mts +75 -75
  142. package/types/src/lib/estimate.d.mts +58 -7
  143. package/types/src/lib/finish.d.mts +4 -4
  144. package/types/src/lib/http.d.mts +19 -5
  145. package/types/src/lib/ids.d.mts +11 -20
  146. package/types/src/lib/images.d.mts +19 -19
  147. package/types/src/lib/models.d.mts +13 -13
  148. package/types/src/lib/normalize.d.mts +3 -3
  149. package/types/src/lib/passthrough.d.mts +11 -17
  150. package/types/src/lib/pdf.d.mts +4 -18
  151. package/types/src/lib/ping.d.mts +1 -15
  152. package/types/src/lib/pool.d.mts +124 -0
  153. package/types/src/lib/route.d.mts +31 -24
  154. package/types/src/lib/routes.d.mts +10 -15
  155. package/types/src/lib/runtime.d.mts +2 -16
  156. package/types/src/lib/schema.d.mts +1 -15
  157. package/types/src/lib/settings.d.mts +68 -54
  158. package/types/src/lib/source.d.mts +2 -2
  159. package/types/src/lib/sse.d.mts +21 -32
  160. package/types/src/lib/thinking.d.mts +117 -22
  161. package/types/src/lib/tools.d.mts +28 -0
  162. package/types/src/lib/toolsearch.d.mts +91 -0
  163. package/types/src/lib/transport.d.mts +3 -16
  164. package/types/src/lib/upstream-error.d.mts +4 -18
  165. package/types/src/lib/usage.d.mts +91 -0
  166. package/types/src/lib/warn.d.mts +1 -1
@@ -1,3 +1,56 @@
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>;
1
54
  /**
2
55
  *
3
56
  * @Project: @cldmv/llm-translator
@@ -13,12 +66,10 @@
13
66
  *
14
67
  */
15
68
  /**
16
- * @fileoverview Prompt size estimate for upstreams that cannot count tokens.
69
+ * @fileoverview Token estimates for upstreams that cannot count tokens or did not report usage.
17
70
  * @module @cldmv/llm-translator/lib/estimate
18
71
  */
19
- /**
20
- * Rough prompt size: one token per four characters of the serialized system prompt, messages and tools.
21
- * @param {any} body - Upstream request body.
22
- * @returns {number} Estimated tokens.
23
- */
24
- export declare function estimate(body: any): number;
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;
@@ -21,7 +21,7 @@
21
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
22
  * @returns {{ stop_reason: string, stop_sequence: string|null }} Anthropic stop fields.
23
23
  */
24
- export declare function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete }: {
24
+ export function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete }: {
25
25
  hasToolCalls: boolean;
26
26
  matchedStop?: unknown;
27
27
  argumentsComplete?: boolean;
@@ -34,14 +34,14 @@ export declare function toAnthropicStop(finishReason: string | null | undefined,
34
34
  * @param {string|null|undefined} stopReason - Anthropic `stop_reason`.
35
35
  * @returns {string} OpenAI finish reason.
36
36
  */
37
- export declare function toOpenAIFinish(stopReason: string | null | undefined): string;
37
+ export function toOpenAIFinish(stopReason: string | null | undefined): string;
38
38
  /**
39
39
  * Anthropic usage from an OpenAI usage object. Cached prompt tokens (reported by vLLM only with prompt-token
40
40
  * details enabled) are reported as `cache_read_input_tokens` and subtracted from `input_tokens`.
41
41
  * @param {{ prompt_tokens?: number, completion_tokens?: number, prompt_tokens_details?: { cached_tokens?: number }|null }|null|undefined} usage - OpenAI usage.
42
42
  * @returns {{ input_tokens: number, output_tokens: number, cache_creation_input_tokens: number, cache_read_input_tokens: number }} Anthropic usage.
43
43
  */
44
- export declare function toAnthropicUsage(usage: {
44
+ export function toAnthropicUsage(usage: {
45
45
  prompt_tokens?: number;
46
46
  completion_tokens?: number;
47
47
  prompt_tokens_details?: {
@@ -58,7 +58,7 @@ export declare function toAnthropicUsage(usage: {
58
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
59
  * @returns {{ prompt_tokens: number, completion_tokens: number, total_tokens: number, prompt_tokens_details?: { cached_tokens: number } }} OpenAI usage.
60
60
  */
61
- export declare function toOpenAIUsage(usage: {
61
+ export function toOpenAIUsage(usage: {
62
62
  input_tokens?: number;
63
63
  output_tokens?: number;
64
64
  cache_creation_input_tokens?: number | null;
@@ -22,13 +22,13 @@
22
22
  * @param {string} path - Path starting with `/`.
23
23
  * @returns {string} Absolute URL.
24
24
  */
25
- export declare function join(base: string, path: string): string;
25
+ export function join(base: string, path: string): string;
26
26
  /**
27
27
  * Whether a listen address only accepts connections from this machine.
28
28
  * @param {string} host - Host or address.
29
29
  * @returns {boolean} True for `localhost`, `127.x.x.x` and `::1`.
30
30
  */
31
- export declare function isLoopback(host: string): boolean;
31
+ export function isLoopback(host: string): boolean;
32
32
  /**
33
33
  * Build the request headers for an upstream call.
34
34
  * @param {any} endpoint - Runtime endpoint.
@@ -36,13 +36,27 @@ export declare function isLoopback(host: string): boolean;
36
36
  * @param {Record<string,string>} [extra] - Extra headers applied last.
37
37
  * @returns {Record<string,string>} Header map.
38
38
  */
39
- export declare function headers(endpoint: any, auth?: string, extra?: Record<string, string>): Record<string, string>;
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
+ }>;
40
54
  /**
41
55
  * Convert a fetch `Headers` object (or plain object) to a lowercase plain object.
42
56
  * @param {Headers | Record<string,string> | undefined} source - Header source.
43
57
  * @returns {Record<string,string>} Plain header map.
44
58
  */
45
- export declare function plain(source: Headers | Record<string, string> | undefined): Record<string, string>;
59
+ export function plain(source: Headers | Record<string, string> | undefined): Record<string, string>;
46
60
  /**
47
61
  * Perform a request and parse the JSON body. Never throws: network failures and non-JSON bodies are reported in the result.
48
62
  * @param {Function} transport - fetch-compatible function.
@@ -50,7 +64,7 @@ export declare function plain(source: Headers | Record<string, string> | undefin
50
64
  * @param {any} [options] - `{ method, headers, body, signal, timeout }`.
51
65
  * @returns {Promise<{ok: boolean, status: number, body: any, error?: Error}>} Outcome.
52
66
  */
53
- export declare function json(transport: Function, url: string, options?: any): Promise<{
67
+ export function json(transport: Function, url: string, options?: any): Promise<{
54
68
  ok: boolean;
55
69
  status: number;
56
70
  body: any;
@@ -1,32 +1,23 @@
1
- /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/ids.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:02-07:00 (1792958342)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
1
  /**
16
2
  * Anthropic style message id.
17
3
  * @returns {string} `msg_...` id.
18
4
  */
19
- export declare function messageId(): string;
5
+ export function messageId(): string;
20
6
  /**
21
7
  * Anthropic style tool-use id.
22
8
  * @returns {string} `toolu_...` id.
23
9
  */
24
- export declare function toolUseId(): string;
10
+ export function toolUseId(): string;
25
11
  /**
26
12
  * OpenAI style completion id.
27
13
  * @returns {string} `chatcmpl-...` id.
28
14
  */
29
- export declare function completionId(): string;
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;
30
21
  /**
31
22
  * Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). Valid ids pass through unchanged, invalid characters
32
23
  * (for example kimi's `functions.name:0`) become `_`, and a missing or empty id is replaced by a fresh one. The
@@ -34,7 +25,7 @@ export declare function completionId(): string;
34
25
  * @param {unknown} id - Upstream id.
35
26
  * @returns {string} Anthropic-valid id.
36
27
  */
37
- export declare function anthropicToolId(id: unknown): string;
28
+ export function anthropicToolId(id: unknown): string;
38
29
  /**
39
30
  * Create a tool-name fitter. Names that already satisfy `pattern` and `max` pass through; others are sanitized and,
40
31
  * when too long, shortened with a hash suffix. Every changed name is recorded in `map` (`fitted -> original`) so the
@@ -42,7 +33,7 @@ export declare function anthropicToolId(id: unknown): string;
42
33
  * @param {{ max: number, map?: Record<string, string> }} options - Maximum length and the (JSON-serializable) map to record into.
43
34
  * @returns {(name: string) => string} Fitter function.
44
35
  */
45
- export declare function createNameFitter({ max, map }: {
36
+ export function createNameFitter({ max, map }: {
46
37
  max: number;
47
38
  map?: Record<string, string>;
48
39
  }): (name: string) => string;
@@ -52,4 +43,4 @@ export declare function createNameFitter({ max, map }: {
52
43
  * @param {string} name - Name seen on the wire.
53
44
  * @returns {string} Original name.
54
45
  */
55
- export declare function restoreName(map: Record<string, string>, name: string): string;
46
+ export function restoreName(map: Record<string, string>, name: string): string;
@@ -1,3 +1,21 @@
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;
1
19
  /**
2
20
  *
3
21
  * @Project: @cldmv/llm-translator
@@ -18,22 +36,4 @@
18
36
  * @module @cldmv/llm-translator/lib/images
19
37
  */
20
38
  /** Text that replaces an image when the model does not accept images. */
21
- export declare const NOTE = "[An image was omitted because this model does not accept images.]";
22
- /**
23
- * Does an upstream error message say the model does not accept images?
24
- * @param {string} message - Upstream error message.
25
- * @returns {boolean} True for a known "no image input" wording.
26
- */
27
- export declare function refused(message: string): boolean;
28
- /**
29
- * Does a Chat Completions body carry image parts?
30
- * @param {any} body - Upstream request body.
31
- * @returns {boolean} True when any message content has an `image_url` part.
32
- */
33
- export declare function carries(body: any): boolean;
34
- /**
35
- * Copy a Chat Completions body with every image part replaced by a short text note.
36
- * @param {any} body - Upstream request body.
37
- * @returns {any} New body; messages without images are shared with the input.
38
- */
39
- export declare function strip(body: any): any;
39
+ export const NOTE: "[An image was omitted because this model does not accept images.]";
@@ -1,23 +1,23 @@
1
1
  /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/models.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
- *
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.
14
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
15
  /**
16
16
  * Fetch and normalize the models an upstream reports.
17
17
  * @param {any} context - `{ endpoint, transport, signal, timeout, auth, extraHeaders }`.
18
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
19
  */
20
- export declare function fetchModels({ endpoint, transport, signal, timeout, auth, extraHeaders }: any): Promise<{
20
+ export function fetchModels({ endpoint, transport, signal, timeout, auth, extraHeaders }: any): Promise<{
21
21
  id: string;
22
22
  created?: number;
23
23
  owned_by?: string;
@@ -21,17 +21,17 @@
21
21
  * @param {any} body - Chat Completions request body (mutated copy is returned).
22
22
  * @returns {any} Normalized body.
23
23
  */
24
- export declare function noEmptyTools(body: any): any;
24
+ export function noEmptyTools(body: any): any;
25
25
  /**
26
26
  * Parse a server version string (`0.19.0rc1`, `0.32.0+cu126`) into numeric parts.
27
27
  * @param {string | undefined} version - Version string.
28
28
  * @returns {number[] | null} `[major, minor, patch]`, or null when unparseable.
29
29
  */
30
- export declare function semver(version: string | undefined): number[] | null;
30
+ export function semver(version: string | undefined): number[] | null;
31
31
  /**
32
32
  * Is `version` at least `minimum`? An unknown version counts as new (latest behavior).
33
33
  * @param {string | undefined} version - Version under test.
34
34
  * @param {string} minimum - Minimum version such as `"0.19.0"`.
35
35
  * @returns {boolean} True when the version is unknown or >= minimum.
36
36
  */
37
- export declare function atLeast(version: string | undefined, minimum: string): boolean;
37
+ export function atLeast(version: string | undefined, minimum: string): boolean;
@@ -1,22 +1,16 @@
1
1
  /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/passthrough.mjs
5
- * @Date: 2026-10-25T12:49:46-07:00 (1792957786)
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
- * Keep only known Chat Completions and documented vLLM extension fields, so an untrusted client cannot inject server-side
2
+ * Keep only known Chat Completions fields and the extension fields the upstream's driver allows, so an untrusted client cannot inject server-side
17
3
  * controls such as `kv_transfer_params`, `vllm_xargs`, `chat_template`, `cache_salt` or `priority`. `stream_options` is
18
- * reduced to the usage flags.
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.
19
7
  * @param {any} body - Client request body.
8
+ * @param {string[]} [extra] - Extra field names the upstream's driver allows.
20
9
  * @returns {any} Filtered copy.
21
10
  */
22
- export declare function filterChatBody(body: any): any;
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[];
@@ -1,24 +1,10 @@
1
- /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/pdf.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:02-07:00 (1792958342)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
1
  /**
16
2
  * Load the PDF libraries. Safe to call repeatedly; each call re-imports. A library that fails to load is reported
17
3
  * in the result instead of throwing, so the translator can degrade (text only, or a note).
18
4
  * @param {(name: string) => Promise<any>} [importer] - Module importer (overridable for tests).
19
5
  * @returns {Promise<{ unpdf: boolean, canvas: boolean, errors: { unpdf?: string, canvas?: string } }>} Load result.
20
6
  */
21
- export declare function load(importer?: (name: string) => Promise<any>): Promise<{
7
+ export function load(importer?: (name: string) => Promise<any>): Promise<{
22
8
  unpdf: boolean;
23
9
  canvas: boolean;
24
10
  errors: {
@@ -30,7 +16,7 @@ export declare function load(importer?: (name: string) => Promise<any>): Promise
30
16
  * Which libraries are currently loaded.
31
17
  * @returns {{ unpdf: boolean, canvas: boolean }} Availability.
32
18
  */
33
- export declare function available(): {
19
+ export function available(): {
34
20
  unpdf: boolean;
35
21
  canvas: boolean;
36
22
  };
@@ -41,7 +27,7 @@ export declare function available(): {
41
27
  * @returns {Promise<{ pageCount: number, title: string|null, pages: string[] }>} Page texts and the document title.
42
28
  * @throws {TranslatorError} `pdf_too_large`, `pdf_too_many_pages`, `pdf_unreadable` or `pdf_unavailable`.
43
29
  */
44
- export declare function extract(bytes: Uint8Array, limits: {
30
+ export function extract(bytes: Uint8Array, limits: {
45
31
  maxPages: number;
46
32
  maxBytes: number;
47
33
  }): Promise<{
@@ -57,7 +43,7 @@ export declare function extract(bytes: Uint8Array, limits: {
57
43
  * @returns {Promise<{ media_type: "image/png", data: string }[]>} Base64 PNGs in page order.
58
44
  * @throws {TranslatorError} `pdf_unavailable` when @napi-rs/canvas is not loaded.
59
45
  */
60
- export declare function render(bytes: Uint8Array, pageNumbers: number[], options: {
46
+ export function render(bytes: Uint8Array, pageNumbers: number[], options: {
61
47
  maxPages: number;
62
48
  maxBytes: number;
63
49
  scale: number;
@@ -1,17 +1,3 @@
1
- /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/ping.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-25T14:05:39-07:00 (1792962339)
11
- * -----
12
- * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
13
- *
14
- */
15
1
  /**
16
2
  * Wrap an async iterable so that `marker` is yielded every `interval` ms while the source produces nothing.
17
3
  * Closing the wrapper closes the source.
@@ -21,4 +7,4 @@
21
7
  * @param {*} marker - Value yielded as the ping, or a function called each time one is due that returns it.
22
8
  * @returns {AsyncGenerator<T | *>} Source events interleaved with pings.
23
9
  */
24
- export declare function withPings<T>(source: AsyncIterable<T>, interval: number, marker: any): AsyncGenerator<T | any>;
10
+ export function withPings<T>(source: AsyncIterable<T>, interval: number, marker: any): AsyncGenerator<T | any>;
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Pool config as the translator accepts it.
3
+ * @typedef {object} PoolConfig
4
+ * @property {"failover" | "round_robin" | "least_busy"} [strategy] Default strategy (default `"failover"`).
5
+ * @property {{base?: number, max?: number}} [backoff] Backoff in ms for a failing endpoint (defaults `1000` and `60000`).
6
+ * @property {number} [connectTimeout] Milliseconds to wait for the TCP connection of a request to an endpoint with pool mates (default `10000`, `0` disables). Needs the default transport.
7
+ * @property {number} [headersTimeout] Milliseconds to wait for the response headers of such a request (default `0`, off). Once the headers have arrived nothing caps the body.
8
+ * @property {Record<string, "failover" | "round_robin" | "least_busy">} [models] Strategy per model, keyed by the model name after aliases.
9
+ */
10
+ /**
11
+ * Fresh health record for an endpoint.
12
+ * @returns {{failures: number, until: number, inflight: number, requests: number, errors: number, lastError: string | null, lastFailure: number | null, lastSuccess: number | null}} Record.
13
+ */
14
+ export function health(): {
15
+ failures: number;
16
+ until: number;
17
+ inflight: number;
18
+ requests: number;
19
+ errors: number;
20
+ lastError: string | null;
21
+ lastFailure: number | null;
22
+ lastSuccess: number | null;
23
+ };
24
+ /**
25
+ * Validate and normalize the `pool` config.
26
+ * @param {PoolConfig | undefined} raw - Raw config.
27
+ * @returns {{strategy: string, backoff: {base: number, max: number}, connectTimeout: number, headersTimeout: number, models: Record<string, string>}} Normalized config.
28
+ * @throws {TranslatorError} `INVALID_CONFIG` for an unknown strategy, a bad backoff or a bad timeout.
29
+ */
30
+ export function normalize(raw?: PoolConfig | undefined): {
31
+ strategy: string;
32
+ backoff: {
33
+ base: number;
34
+ max: number;
35
+ };
36
+ connectTimeout: number;
37
+ headersTimeout: number;
38
+ models: Record<string, string>;
39
+ };
40
+ /**
41
+ * Order the endpoints that serve a model into the sequence in which they are tried: healthy endpoints first, arranged by the model's
42
+ * strategy, then the unhealthy ones, soonest to recover first. Every endpoint is included, so an all-unhealthy pool is still tried.
43
+ * @param {any} config - Normalized config (`pool`).
44
+ * @param {any} state - Translator state (`cursors`).
45
+ * @param {any[]} list - Endpoints serving the model, in configuration order.
46
+ * @param {string} model - Model name after aliases.
47
+ * @param {boolean} advance - Move the round robin cursor (false to only look).
48
+ * @param {number} [now] - Current time in ms.
49
+ * @returns {any[]} Endpoints in try order.
50
+ */
51
+ export function order(config: any, state: any, list: any[], model: string, advance: boolean, now?: number): any[];
52
+ /**
53
+ * Record a connection error or 5xx from an endpoint: it is skipped for an exponentially growing backoff.
54
+ * @param {any} config - Normalized config (`pool.backoff`).
55
+ * @param {any} endpoint - Runtime endpoint.
56
+ * @param {string} reason - Short description kept for status output.
57
+ * @param {number} [now] - Current time in ms.
58
+ * @returns {void}
59
+ */
60
+ export function failed(config: any, endpoint: any, reason: string, now?: number): void;
61
+ /**
62
+ * Record that an endpoint answered (any status below 500): it is healthy again.
63
+ * @param {any} endpoint - Runtime endpoint.
64
+ * @param {number} [now] - Current time in ms.
65
+ * @returns {void}
66
+ */
67
+ export function succeeded(endpoint: any, now?: number): void;
68
+ /**
69
+ * Health of an endpoint as status output shows it.
70
+ * @param {any} endpoint - Runtime endpoint.
71
+ * @param {number} [now] - Current time in ms.
72
+ * @returns {{healthy: boolean, failures: number, retryAt: string | null, inflight: number, requests: number, errors: number, lastError: string | null, lastFailure: string | null, lastSuccess: string | null}} Health report (times are ISO strings).
73
+ */
74
+ export function describe(endpoint: any, now?: number): {
75
+ healthy: boolean;
76
+ failures: number;
77
+ retryAt: string | null;
78
+ inflight: number;
79
+ requests: number;
80
+ errors: number;
81
+ lastError: string | null;
82
+ lastFailure: string | null;
83
+ lastSuccess: string | null;
84
+ };
85
+ /** Strategies a pool of endpoints serving the same model can use. */
86
+ export const STRATEGIES: readonly string[];
87
+ /** Default backoff in ms: the first failure skips the endpoint for `base`, each further failure doubles it up to `max`. */
88
+ export const BACKOFF: Readonly<{
89
+ base: 1000;
90
+ max: 60000;
91
+ }>;
92
+ /** Default timeouts in ms for a request to an endpoint that has pool mates (0 disables). Connect is short because a dead host is the common failure; headers is off because a server may legitimately take minutes to prefill a long prompt before it sends any header. */
93
+ export const TIMEOUTS: Readonly<{
94
+ connect: 10000;
95
+ headers: 0;
96
+ }>;
97
+ /**
98
+ * Pool config as the translator accepts it.
99
+ */
100
+ export type PoolConfig = {
101
+ /**
102
+ * Default strategy (default `"failover"`).
103
+ */
104
+ strategy?: "failover" | "round_robin" | "least_busy";
105
+ /**
106
+ * Backoff in ms for a failing endpoint (defaults `1000` and `60000`).
107
+ */
108
+ backoff?: {
109
+ base?: number;
110
+ max?: number;
111
+ };
112
+ /**
113
+ * Milliseconds to wait for the TCP connection of a request to an endpoint with pool mates (default `10000`, `0` disables). Needs the default transport.
114
+ */
115
+ connectTimeout?: number;
116
+ /**
117
+ * Milliseconds to wait for the response headers of such a request (default `0`, off). Once the headers have arrived nothing caps the body.
118
+ */
119
+ headersTimeout?: number;
120
+ /**
121
+ * Strategy per model, keyed by the model name after aliases.
122
+ */
123
+ models?: Record<string, "failover" | "round_robin" | "least_busy">;
124
+ };
@@ -1,38 +1,45 @@
1
- /**
2
- *
3
- * @Project: @cldmv/llm-translator
4
- * @Filename: /src/lib/route.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 Chooses the upstream endpoint for a requested model.
17
- * @module @cldmv/llm-translator/lib/route
18
- */
19
1
  /**
20
2
  * Does the endpoint serve the model?
21
3
  * @param {any} endpoint - Runtime endpoint.
22
4
  * @param {string} model - Upstream model name.
23
5
  * @returns {boolean} True when the endpoint lists the model, or is an Anthropic upstream and the model is a `claude-*` id.
24
6
  */
25
- export declare function serves(endpoint: any, model: string): boolean;
7
+ export function serves(endpoint: any, model: string): boolean;
26
8
  /**
27
- * Pick the endpoint for a model: alias map, then an endpoint serving the model, then the default endpoint, then the first.
28
- * @param {any} config - Normalized config (`aliases`).
9
+ * Pick the endpoint for a model: alias map, then the endpoints serving the model (a pool, ordered by the model's strategy and health;
10
+ * the first is chosen), then the default endpoint, then the first.
11
+ * @param {any} config - Normalized config (`aliases`, `pool`).
29
12
  * @param {any[]} endpoints - Runtime endpoints.
30
13
  * @param {string | undefined} model - Model named by the client.
31
- * @returns {{endpoint: any, model: string | undefined, unserved: boolean}} Chosen endpoint, the model name to send upstream and whether
32
- * no endpoint serves the model. When nothing serves it and the fallback endpoint knows exactly one model, that model is sent instead.
14
+ * @param {any} state - Translator state (`cursors`).
15
+ * @param {boolean} [advance] - Move the round robin cursor (false to only look).
16
+ * @returns {{endpoint: any, model: string | undefined, unserved: boolean, pool: any[]}} Chosen endpoint, the model name to send upstream, whether
17
+ * no endpoint serves the model and the endpoints to try in order (the chosen one first). When nothing serves it and the fallback endpoint knows
18
+ * exactly one model, that model is sent instead.
33
19
  */
34
- export declare function pick(config: any, endpoints: any[], model: string | undefined): {
20
+ export function pick(config: any, endpoints: any[], model: string | undefined, state: any, advance?: boolean): {
35
21
  endpoint: any;
36
22
  model: string | undefined;
37
23
  unserved: boolean;
24
+ pool: any[];
25
+ };
26
+ /**
27
+ * Validate a client request body and resolve where it goes. Shared by `request` and `route` so both always agree.
28
+ * @param {any} c - Captured translator parts (`capture`).
29
+ * @param {any} body - Client request body.
30
+ * @param {{endpoint?: string, advance?: boolean}} [options] - `endpoint` forces one pool member by name (used for failover; it must still serve the model, else `endpoint_not_in_pool`); `advance` moves the round robin cursor (`request` does, `route` does not).
31
+ * @returns {{endpoint: any, model: string, requested: string | undefined, unserved: boolean, convert: any, pool: any[]}} The chosen endpoint, the model name sent
32
+ * upstream, the model the client asked for, whether no endpoint serves it, the converters (null for a native pass-through) and the pool in try order.
33
+ * @throws {TranslatorError} `invalid_body` when the body is not a JSON object, `endpoint_not_in_pool` when a forced endpoint no longer serves the model.
34
+ */
35
+ export function resolve(c: any, body: any, options?: {
36
+ endpoint?: string;
37
+ advance?: boolean;
38
+ }): {
39
+ endpoint: any;
40
+ model: string;
41
+ requested: string | undefined;
42
+ unserved: boolean;
43
+ convert: any;
44
+ pool: any[];
38
45
  };