@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,53 @@
1
+ /**
2
+ * Load the PDF libraries. Safe to call repeatedly; each call re-imports. A library that fails to load is reported
3
+ * in the result instead of throwing, so the translator can degrade (text only, or a note).
4
+ * @param {(name: string) => Promise<any>} [importer] - Module importer (overridable for tests).
5
+ * @returns {Promise<{ unpdf: boolean, canvas: boolean, errors: { unpdf?: string, canvas?: string } }>} Load result.
6
+ */
7
+ export function load(importer?: (name: string) => Promise<any>): Promise<{
8
+ unpdf: boolean;
9
+ canvas: boolean;
10
+ errors: {
11
+ unpdf?: string;
12
+ canvas?: string;
13
+ };
14
+ }>;
15
+ /**
16
+ * Which libraries are currently loaded.
17
+ * @returns {{ unpdf: boolean, canvas: boolean }} Availability.
18
+ */
19
+ export function available(): {
20
+ unpdf: boolean;
21
+ canvas: boolean;
22
+ };
23
+ /**
24
+ * Extract the text of every page.
25
+ * @param {Uint8Array} bytes - PDF bytes.
26
+ * @param {{ maxPages: number, maxBytes: number }} limits - Limits (Anthropic: 100 pages, 32 MB).
27
+ * @returns {Promise<{ pageCount: number, title: string|null, pages: string[] }>} Page texts and the document title.
28
+ * @throws {TranslatorError} `pdf_too_large`, `pdf_too_many_pages`, `pdf_unreadable` or `pdf_unavailable`.
29
+ */
30
+ export function extract(bytes: Uint8Array, limits: {
31
+ maxPages: number;
32
+ maxBytes: number;
33
+ }): Promise<{
34
+ pageCount: number;
35
+ title: string | null;
36
+ pages: string[];
37
+ }>;
38
+ /**
39
+ * Render pages to PNG images.
40
+ * @param {Uint8Array} bytes - PDF bytes.
41
+ * @param {number[]} pageNumbers - 1-based page numbers.
42
+ * @param {{ maxPages: number, maxBytes: number, scale: number }} options - Limits and render scale.
43
+ * @returns {Promise<{ media_type: "image/png", data: string }[]>} Base64 PNGs in page order.
44
+ * @throws {TranslatorError} `pdf_unavailable` when @napi-rs/canvas is not loaded.
45
+ */
46
+ export function render(bytes: Uint8Array, pageNumbers: number[], options: {
47
+ maxPages: number;
48
+ maxBytes: number;
49
+ scale: number;
50
+ }): Promise<{
51
+ media_type: "image/png";
52
+ data: string;
53
+ }[]>;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Wrap an async iterable so that `marker` is yielded every `interval` ms while the source produces nothing.
3
+ * Closing the wrapper closes the source.
4
+ * @template T
5
+ * @param {AsyncIterable<T>} source - Event source.
6
+ * @param {number} interval - Silence in ms before a ping; `0` (or less) disables pings.
7
+ * @param {*} marker - Value yielded as the ping, or a function called each time one is due that returns it.
8
+ * @returns {AsyncGenerator<T | *>} Source events interleaved with pings.
9
+ */
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
+ };
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Does the endpoint serve the model?
3
+ * @param {any} endpoint - Runtime endpoint.
4
+ * @param {string} model - Upstream model name.
5
+ * @returns {boolean} True when the endpoint lists the model, or is an Anthropic upstream and the model is a `claude-*` id.
6
+ */
7
+ export function serves(endpoint: any, model: string): boolean;
8
+ /**
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`).
12
+ * @param {any[]} endpoints - Runtime endpoints.
13
+ * @param {string | undefined} model - Model named by the client.
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.
19
+ */
20
+ export function pick(config: any, endpoints: any[], model: string | undefined, state: any, advance?: boolean): {
21
+ endpoint: any;
22
+ model: string | undefined;
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[];
45
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Events for a stream reply committed before the upstream answered: the upstream's stream when it accepted, else its error
3
+ * as a final error event (the HTTP status is already 200, so the error travels in-band).
4
+ * @param {Promise<any>} sending - Pending `send` result.
5
+ * @param {any} api - Captured translator parts.
6
+ * @param {string} [model] - Model the client asked for (used in the early `message_start`).
7
+ * @returns {AsyncGenerator<any>} Client events.
8
+ */
9
+ export function late(sending: Promise<any>, api: any, model?: string): AsyncGenerator<any>;
10
+ /**
11
+ * Register the routes for one front on a Fastify instance.
12
+ * @param {any} fastify - Fastify instance (usually an encapsulated plugin scope).
13
+ * @param {any} options - `{ apiKey, bodyLimit, authorize, onUsage }`.
14
+ * @param {any} captured - Captured translator parts `{ front, interval, filter, send, route, count, list, get, build, encode, write }`; `front` applies to this instance only.
15
+ * @returns {void}
16
+ */
17
+ export function register(fastify: any, options: any, captured: any): void;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Per-instance context (`config`, `state`); valid only synchronously inside a leaf call.
3
+ * @type {any}
4
+ */
5
+ export const context: any;
6
+ /**
7
+ * Proxy over the whole api (`self.convert.…`, `self.driver.…`); valid only synchronously inside a leaf call.
8
+ * @type {any}
9
+ */
10
+ export const self: any;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Return a sanitized deep copy of a JSON Schema: drops `$schema` and numeric bounds at or beyond +-(2^53 - 1)
3
+ * (Claude Code emits `maximum: 9007199254740991` on integers, which strict grammar backends dislike). The input is
4
+ * never mutated.
5
+ * @param {unknown} schema - JSON Schema (or any JSON value found inside one).
6
+ * @returns {unknown} Sanitized copy.
7
+ */
8
+ export function sanitize(schema: unknown): unknown;
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Merge settings layers (later layers win, `undefined` values are skipped, `pdf` merges key by key) and validate
3
+ * the enumerated keys. The result is a fresh object; inputs are never mutated.
4
+ * @param {...(Partial<Settings>|null|undefined)} layers - Layers in increasing precedence.
5
+ * @returns {Settings} Resolved settings.
6
+ */
7
+ export function resolve(...layers: (Partial<Settings> | null | undefined)[]): Settings;
8
+ /**
9
+ * Settings understood by the converters. Every key is optional when passed in; `resolve()` fills the rest.
10
+ * @typedef {object} Settings
11
+ * @property {"auto"|"keep"|"hoist"|"user"|"drop"} inlineSystem - How mid-conversation `role:"system"` messages are handled. `user` folds them into the adjacent user turn as `<system-reminder>` text, `keep` forwards them as system messages, `hoist` appends them to the leading system message, `drop` discards them. `auto` behaves like `user` unless a driver probed the chat template and resolved it.
12
+ * @property {boolean} stripBillingHeader - Remove the `x-anthropic-billing-header` system block.
13
+ * @property {"reasoning"|"reasoning_content"} reasoningField - Field used when SENDING reasoning to an OpenAI-style server (reading accepts both).
14
+ * @property {boolean} thinkTags - Parse a leading `<think>...</think>` in content as reasoning.
15
+ * @property {boolean} thinkOpen - With `thinkTags`: the output starts inside a thought because the chat template opens `<think>` in the generation prompt, so reasoning runs up to the first `</think>`. Without a `</think>` the content is plain text.
16
+ * @property {boolean} reasoningRoundTrip - Carry reasoning the upstream needs back across turns. OpenAI-protocol upstream: the provider's reasoning fields (`reasoning_content`, `reasoning`, `reasoning_details`) travel in the thinking block's `llmt:` signature and are restored on the next request. Anthropic-protocol upstream with an OpenAI client: thinking blocks and their signatures travel as `reasoning_details` items the client replays. Off by default; the DeepSeek driver turns it on.
17
+ * @property {"chat_template_kwargs"|"reasoning_effort"|"none"} thinking - How Anthropic `thinking` is requested from the server.
18
+ * @property {boolean} thinkingBudget - Send `thinking_token_budget` for `thinking.type = "enabled"`.
19
+ * @property {boolean} effort - Map `output_config.effort` to `reasoning_effort`.
20
+ * @property {boolean} images - The model accepts images. When false images are replaced by a text note.
21
+ * @property {boolean} imageFallback - Retry a request once without images when an OpenAI-protocol upstream answers 400 that the model does not accept images, and remember that for the endpoint and model until the process ends.
22
+ * @property {boolean} toolFallback - Retry a request once without tools when an OpenAI-protocol upstream answers 400 that tool parsing is not enabled (vLLM started without `--enable-auto-tool-choice` and `--tool-call-parser`). Off by default: the request then runs without tools, which silently changes what the model can do.
23
+ * @property {"extract"|"note"|"error"} documents - How `document` blocks are handled.
24
+ * @property {((block: object, ctx: object) => Promise<object[]>|object[])|null} documentConverter - Override for `document` conversion; returns Anthropic content blocks.
25
+ * @property {{ pages: boolean, maxPages: number, maxBytes: number, maxImages: number, scale: number }} pdf - PDF handling: render page images, Anthropic-like limits, image budget and render scale.
26
+ * @property {"pass"|"sanitize"} toolSchema - Sanitize tool JSON Schemas (drop `$schema`, remove unbounded integer limits).
27
+ * @property {"forward"|"drop"} deferredTools - Offer `defer_loading` tools as `toolSearch` says, or drop them (and the search tool).
28
+ * @property {"expand"|"inline"|"native"} toolSearch - Claude Code's tool search. `expand` offers a deferred tool once a `tool_reference` (or a `ToolSearch` result) loads it; `inline` offers every deferred tool up front and hides `ToolSearch` (it stays when `DeferredToolPlaceholder` shows Claude Code holds tools whose definitions it did not send; `expand` does the same when the request has no `ToolSearch` at all); `native` passes `defer_loading` and `tool_reference` through to a server that understands them.
29
+ * @property {boolean} toolErrors - Prefix `is_error` tool results with `[error] `.
30
+ * @property {"max_completion_tokens"|"max_tokens"} maxTokensField - Field used for the output cap.
31
+ * @property {boolean} streamUsage - Request `stream_options.include_usage` on streamed requests.
32
+ * @property {boolean} continuousUsage - Also request `stream_options.continuous_usage_stats` (vLLM extension; real OpenAI may reject it).
33
+ * @property {"continue_final_message"|"none"} prefill - Map a trailing assistant message to `continue_final_message`.
34
+ * @property {boolean} topK - Forward `top_k`.
35
+ * @property {boolean} responseFormat - Map `output_config.format` to `response_format`.
36
+ * @property {boolean} parallelToolCalls - Map `disable_parallel_tool_use` to `parallel_tool_calls: false`.
37
+ * @property {"drop"|"error"} unknownFields - Unknown request fields are dropped (and listed in `state.dropped`) or rejected.
38
+ * @property {number} pingInterval - Milliseconds of upstream silence before the SSE writer emits a `ping` (0 disables).
39
+ * @property {"buffer"|"stream"} toolStreaming - OpenAI to Anthropic streams: `buffer` holds each tool call until its arguments are complete and emits one whole `tool_use` block (text and thinking keep streaming and appear before the block); `stream` forwards argument fragments incrementally and holds text that arrives meanwhile until the tool blocks are done.
40
+ * @property {boolean} effortPassthrough - vLLM driver: send `xhigh` and `max` reasoning effort unchanged instead of clamping them to `high`.
41
+ * @property {boolean} passthroughFilter - Restrict bodies forwarded from the OpenAI front to an OpenAI-protocol upstream to standard Chat Completions fields plus documented vLLM extensions. Off in the library; the server turns it on unless set to false.
42
+ * @property {number} contextWindow - Prompt size in tokens the upstream truncates at, or 0 when unknown. Drivers whose server silently truncates (Ollama) warn once when a request is estimated to exceed it.
43
+ * @property {number} defaultMaxTokens - `max_tokens` used when an OpenAI request has none (Anthropic requires one).
44
+ */
45
+ /** @type {Readonly<Settings>} */
46
+ export const DEFAULTS: Readonly<Settings>;
47
+ /**
48
+ * Settings understood by the converters. Every key is optional when passed in; `resolve()` fills the rest.
49
+ */
50
+ export type Settings = {
51
+ /**
52
+ * - How mid-conversation `role:"system"` messages are handled. `user` folds them into the adjacent user turn as `<system-reminder>` text, `keep` forwards them as system messages, `hoist` appends them to the leading system message, `drop` discards them. `auto` behaves like `user` unless a driver probed the chat template and resolved it.
53
+ */
54
+ inlineSystem: "auto" | "keep" | "hoist" | "user" | "drop";
55
+ /**
56
+ * - Remove the `x-anthropic-billing-header` system block.
57
+ */
58
+ stripBillingHeader: boolean;
59
+ /**
60
+ * - Field used when SENDING reasoning to an OpenAI-style server (reading accepts both).
61
+ */
62
+ reasoningField: "reasoning" | "reasoning_content";
63
+ /**
64
+ * - Parse a leading `<think>...</think>` in content as reasoning.
65
+ */
66
+ thinkTags: boolean;
67
+ /**
68
+ * - With `thinkTags`: the output starts inside a thought because the chat template opens `<think>` in the generation prompt, so reasoning runs up to the first `</think>`. Without a `</think>` the content is plain text.
69
+ */
70
+ thinkOpen: boolean;
71
+ /**
72
+ * - Carry reasoning the upstream needs back across turns. OpenAI-protocol upstream: the provider's reasoning fields (`reasoning_content`, `reasoning`, `reasoning_details`) travel in the thinking block's `llmt:` signature and are restored on the next request. Anthropic-protocol upstream with an OpenAI client: thinking blocks and their signatures travel as `reasoning_details` items the client replays. Off by default; the DeepSeek driver turns it on.
73
+ */
74
+ reasoningRoundTrip: boolean;
75
+ /**
76
+ * - How Anthropic `thinking` is requested from the server.
77
+ */
78
+ thinking: "chat_template_kwargs" | "reasoning_effort" | "none";
79
+ /**
80
+ * - Send `thinking_token_budget` for `thinking.type = "enabled"`.
81
+ */
82
+ thinkingBudget: boolean;
83
+ /**
84
+ * - Map `output_config.effort` to `reasoning_effort`.
85
+ */
86
+ effort: boolean;
87
+ /**
88
+ * - The model accepts images. When false images are replaced by a text note.
89
+ */
90
+ images: boolean;
91
+ /**
92
+ * - Retry a request once without images when an OpenAI-protocol upstream answers 400 that the model does not accept images, and remember that for the endpoint and model until the process ends.
93
+ */
94
+ imageFallback: boolean;
95
+ /**
96
+ * - Retry a request once without tools when an OpenAI-protocol upstream answers 400 that tool parsing is not enabled (vLLM started without `--enable-auto-tool-choice` and `--tool-call-parser`). Off by default: the request then runs without tools, which silently changes what the model can do.
97
+ */
98
+ toolFallback: boolean;
99
+ /**
100
+ * - How `document` blocks are handled.
101
+ */
102
+ documents: "extract" | "note" | "error";
103
+ /**
104
+ * - Override for `document` conversion; returns Anthropic content blocks.
105
+ */
106
+ documentConverter: ((block: object, ctx: object) => Promise<object[]> | object[]) | null;
107
+ /**
108
+ * - PDF handling: render page images, Anthropic-like limits, image budget and render scale.
109
+ */
110
+ pdf: {
111
+ pages: boolean;
112
+ maxPages: number;
113
+ maxBytes: number;
114
+ maxImages: number;
115
+ scale: number;
116
+ };
117
+ /**
118
+ * - Sanitize tool JSON Schemas (drop `$schema`, remove unbounded integer limits).
119
+ */
120
+ toolSchema: "pass" | "sanitize";
121
+ /**
122
+ * - Offer `defer_loading` tools as `toolSearch` says, or drop them (and the search tool).
123
+ */
124
+ deferredTools: "forward" | "drop";
125
+ /**
126
+ * - Claude Code's tool search. `expand` offers a deferred tool once a `tool_reference` (or a `ToolSearch` result) loads it; `inline` offers every deferred tool up front and hides `ToolSearch` (it stays when `DeferredToolPlaceholder` shows Claude Code holds tools whose definitions it did not send; `expand` does the same when the request has no `ToolSearch` at all); `native` passes `defer_loading` and `tool_reference` through to a server that understands them.
127
+ */
128
+ toolSearch: "expand" | "inline" | "native";
129
+ /**
130
+ * - Prefix `is_error` tool results with `[error] `.
131
+ */
132
+ toolErrors: boolean;
133
+ /**
134
+ * - Field used for the output cap.
135
+ */
136
+ maxTokensField: "max_completion_tokens" | "max_tokens";
137
+ /**
138
+ * - Request `stream_options.include_usage` on streamed requests.
139
+ */
140
+ streamUsage: boolean;
141
+ /**
142
+ * - Also request `stream_options.continuous_usage_stats` (vLLM extension; real OpenAI may reject it).
143
+ */
144
+ continuousUsage: boolean;
145
+ /**
146
+ * - Map a trailing assistant message to `continue_final_message`.
147
+ */
148
+ prefill: "continue_final_message" | "none";
149
+ /**
150
+ * - Forward `top_k`.
151
+ */
152
+ topK: boolean;
153
+ /**
154
+ * - Map `output_config.format` to `response_format`.
155
+ */
156
+ responseFormat: boolean;
157
+ /**
158
+ * - Map `disable_parallel_tool_use` to `parallel_tool_calls: false`.
159
+ */
160
+ parallelToolCalls: boolean;
161
+ /**
162
+ * - Unknown request fields are dropped (and listed in `state.dropped`) or rejected.
163
+ */
164
+ unknownFields: "drop" | "error";
165
+ /**
166
+ * - Milliseconds of upstream silence before the SSE writer emits a `ping` (0 disables).
167
+ */
168
+ pingInterval: number;
169
+ /**
170
+ * - OpenAI to Anthropic streams: `buffer` holds each tool call until its arguments are complete and emits one whole `tool_use` block (text and thinking keep streaming and appear before the block); `stream` forwards argument fragments incrementally and holds text that arrives meanwhile until the tool blocks are done.
171
+ */
172
+ toolStreaming: "buffer" | "stream";
173
+ /**
174
+ * - vLLM driver: send `xhigh` and `max` reasoning effort unchanged instead of clamping them to `high`.
175
+ */
176
+ effortPassthrough: boolean;
177
+ /**
178
+ * - Restrict bodies forwarded from the OpenAI front to an OpenAI-protocol upstream to standard Chat Completions fields plus documented vLLM extensions. Off in the library; the server turns it on unless set to false.
179
+ */
180
+ passthroughFilter: boolean;
181
+ /**
182
+ * - Prompt size in tokens the upstream truncates at, or 0 when unknown. Drivers whose server silently truncates (Ollama) warn once when a request is estimated to exceed it.
183
+ */
184
+ contextWindow: number;
185
+ /**
186
+ * - `max_tokens` used when an OpenAI request has none (Anthropic requires one).
187
+ */
188
+ defaultMaxTokens: number;
189
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/lib/source.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
+ /**
16
+ * @fileoverview Normalize the many shapes a streaming body can arrive in.
17
+ */
18
+ /**
19
+ * Iterate any supported source as raw items: a `ReadableStream`, an async iterable, an iterable, or a single
20
+ * string / `Uint8Array` / object. Closing the returned iterator (early `break`, `return()`, a throw) cancels the
21
+ * underlying stream or iterator so aborts propagate upstream.
22
+ * @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array|any} source - Input source.
23
+ * @returns {AsyncGenerator<any, void, undefined>} Raw items in order.
24
+ */
25
+ export function items(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array | any): AsyncGenerator<any, void, undefined>;
26
+ /**
27
+ * Iterate a source as decoded text. Strings pass through, `Uint8Array` chunks are decoded as streaming UTF-8
28
+ * (multi-byte characters may span chunks).
29
+ * @param {ReadableStream|AsyncIterable<string|Uint8Array>|Iterable<string|Uint8Array>|string|Uint8Array} source - Input source.
30
+ * @returns {AsyncGenerator<string, void, undefined>} Text chunks.
31
+ */
32
+ export function text(source: ReadableStream | AsyncIterable<string | Uint8Array> | Iterable<string | Uint8Array> | string | Uint8Array): AsyncGenerator<string, void, undefined>;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * A parsed SSE event.
3
+ * @typedef {object} SseEvent
4
+ * @property {string} [event] - Event name.
5
+ * @property {string} data - Data lines joined with `\n`.
6
+ * @property {string} [id] - Event id (applies to the event it is sent with only).
7
+ */
8
+ /**
9
+ * Create an incremental SSE parser. Handles `\n`, `\r\n` and `\r` line endings, comments, a leading BOM, multi-line
10
+ * `data:` fields and chunk boundaries anywhere (including inside a line or a line terminator).
11
+ * @returns {{ push: (chunk: string) => SseEvent[], flush: () => SseEvent[] }} Parser.
12
+ */
13
+ export function createParser(): {
14
+ push: (chunk: string) => SseEvent[];
15
+ flush: () => SseEvent[];
16
+ };
17
+ /**
18
+ * Parse SSE text (any `lib/source.mjs` input) into events.
19
+ * @param {ReadableStream|AsyncIterable<string|Uint8Array>|Iterable<string|Uint8Array>|string|Uint8Array} source - SSE bytes or text.
20
+ * @returns {AsyncGenerator<SseEvent, void, undefined>} Parsed events.
21
+ */
22
+ export function parse(source: ReadableStream | AsyncIterable<string | Uint8Array> | Iterable<string | Uint8Array> | string | Uint8Array): AsyncGenerator<SseEvent, void, undefined>;
23
+ /**
24
+ * Encode one event as SSE text. Object `data` is JSON-encoded; string `data` is written as is (multi-line strings
25
+ * become several `data:` lines).
26
+ * @param {{ event?: string, data: unknown, id?: string }} message - Event to encode.
27
+ * @returns {string} SSE frame terminated by a blank line.
28
+ */
29
+ export function encode({ event, data, id }: {
30
+ event?: string;
31
+ data: unknown;
32
+ id?: string;
33
+ }): string;
34
+ /**
35
+ * Normalize a stream input into `{ event, data }` messages whose `data` is parsed JSON (or the literal string
36
+ * `"[DONE]"`). Accepts SSE text/bytes, already-parsed `{ event, data }` pairs, or bare payload objects (OpenAI
37
+ * chunks, Anthropic event payloads). A bare payload object that has a `type` becomes `event: <type>`.
38
+ * @param {ReadableStream|AsyncIterable<any>|Iterable<any>|string|Uint8Array} source - Stream input.
39
+ * @returns {AsyncGenerator<{ event?: string, data: any }, void, undefined>} Messages.
40
+ */
41
+ export function messages(source: ReadableStream | AsyncIterable<any> | Iterable<any> | string | Uint8Array): AsyncGenerator<{
42
+ event?: string;
43
+ data: any;
44
+ }, void, undefined>;
45
+ /**
46
+ * A parsed SSE event.
47
+ */
48
+ export type SseEvent = {
49
+ /**
50
+ * - Event name.
51
+ */
52
+ event?: string;
53
+ /**
54
+ * - Data lines joined with `\n`.
55
+ */
56
+ data: string;
57
+ /**
58
+ * - Event id (applies to the event it is sent with only).
59
+ */
60
+ id?: string;
61
+ };