@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,155 @@
1
+ /**
2
+ * Create the signature attached to a thinking block produced from upstream reasoning. It is clearly marked as not coming from Anthropic.
3
+ *
4
+ * Format: `llmt:` followed by the base64url of a JSON payload `{"v":1}`, or `{"v":1,"p":"<producer>","d":{...}}` when provider reasoning data has to
5
+ * come back with the block. `p` names the driver and protocol that produced the data (so it is restored only for the same producer) and `d` holds
6
+ * one text field (`reasoning_content` or `reasoning`) and/or `reasoning_details`. A payload longer than 512 bytes is deflated and written
7
+ * as `llmt:~` plus base64url, unless that would not shrink it or would exceed the 30:1 inflation ratio {@link unsign} accepts. Data that
8
+ * {@link unsign} would reject (over the size limits, unknown shape) or a result over 256 KiB leaves the plain marker.
9
+ * @param {{ reasoning_content?: string, reasoning?: string, reasoning_details?: object[] }} [data] - Provider reasoning data to carry.
10
+ * @param {string} [producer] - Producer binding, `driver:protocol:host`.
11
+ * @returns {string} Signature string.
12
+ */
13
+ export function sign(data?: {
14
+ reasoning_content?: string;
15
+ reasoning?: string;
16
+ reasoning_details?: object[];
17
+ }, producer?: string): string;
18
+ /**
19
+ * Whether a signature was produced by {@link sign}.
20
+ * @param {unknown} signature - Signature value.
21
+ * @returns {signature is string} True for translator-made signatures.
22
+ */
23
+ export function isOurs(signature: unknown): signature is string;
24
+ /**
25
+ * The limits on decoding signatures within one request, shared by every conversion of it (retries and failover included): how many bytes may be
26
+ * inflated or decoded in total, how many marked signatures may be decoded, and how many decodes may fail before decoding stops for the rest of the
27
+ * request. Bytes are charged before a signature is inflated (the most it could produce) and refunded down to what it did produce, so a signature that
28
+ * fails still costs. Results are cached by producer and signature, so converting the same request again costs nothing. Once the budget is spent the
29
+ * caller falls back to the thinking text.
30
+ * Decodes on one budget run one at a time (each waits for the one before it), so concurrent callers cannot get past the checks together.
31
+ * @returns {{ bytes: number, signatures: number, failures: number, cache: Map<string, any>, queue: Promise<void> }} A fresh budget for one request.
32
+ */
33
+ export function createBudget(): {
34
+ bytes: number;
35
+ signatures: number;
36
+ failures: number;
37
+ cache: Map<string, any>;
38
+ queue: Promise<void>;
39
+ };
40
+ /**
41
+ * Read the provider reasoning data out of a signature made by {@link sign}. Strict: the signature must carry the mark, be within the size
42
+ * limits and decode to `{"v":1,"p":...,"d":{...}}` with only the known fields, each well formed, and its producer must equal `producer`
43
+ * (a payload with no producer is returned only when none is given). Anything else (a real Anthropic signature, a plain marker, a forged, garbled or
44
+ * foreign one) gives null and the caller falls back to the thinking text. Inflation is bounded while it runs: by 1 MiB per signature, by the rest of
45
+ * the per-request budget, and by a 30:1 ratio of output to compressed input, so a small signature cannot expand into a large allocation; after a few
46
+ * failed decodes the rest of the request is not decoded at all. Calls that share a budget are queued, so they are safe to make concurrently.
47
+ * @param {unknown} signature - Signature value from a thinking block.
48
+ * @param {{ producer?: string, budget?: ReturnType<typeof createBudget> }} [options] - `producer` is the current `driver:protocol:host`; `budget` is shared by all signatures of one request (see {@link createBudget}).
49
+ * @returns {Promise<{ reasoning_content?: string, reasoning?: string, reasoning_details?: object[] } | null>} The data, or null.
50
+ */
51
+ export function unsign(signature: unknown, { producer, budget }?: {
52
+ producer?: string;
53
+ budget?: ReturnType<typeof createBudget>;
54
+ }): Promise<{
55
+ reasoning_content?: string;
56
+ reasoning?: string;
57
+ reasoning_details?: object[];
58
+ } | null>;
59
+ /**
60
+ * The `reasoning_details` item for an Anthropic thinking block or redacted thinking block, in the form an OpenAI client replays.
61
+ * @param {any} block - Anthropic `thinking` or `redacted_thinking` content block (with its signature or data).
62
+ * @param {number} index - Position among what the OpenAI message shows: 0 before the text (or before the first tool call when there is no text), 1 after the text and before the first tool call, then one more for each tool call. Empty text does not count as text.
63
+ * @returns {object | null} The item, or null for a block that cannot be replayed (no signature or data).
64
+ */
65
+ export function detailOf(block: any, index: number): object | null;
66
+ /**
67
+ * Anthropic thinking and redacted thinking blocks from the `reasoning_details` an OpenAI client replayed. Strict: only items in this
68
+ * translator's Anthropic format with the expected string fields become blocks, only their known fields are copied, and at most 64 items
69
+ * totalling 1 MiB of text are taken; anything else is ignored.
70
+ * @param {unknown} details - `reasoning_details` of an assistant message.
71
+ * @returns {{ at: number, block: any }[]} Anthropic content blocks with the position (`index`) they had in the original message, which the caller uses to keep thinking in place between the text and the tool calls (possibly empty).
72
+ */
73
+ export function blocksOf(details: unknown): {
74
+ at: number;
75
+ block: any;
76
+ }[];
77
+ /**
78
+ * Which field of an OpenAI message or delta carries its reasoning text (`reasoning` or the older `reasoning_content`), if any.
79
+ * @param {{ reasoning?: unknown, reasoning_content?: unknown }|undefined} part - Message or delta.
80
+ * @returns {"reasoning"|"reasoning_content"|null} Field name.
81
+ */
82
+ export function reasoningKey(part: {
83
+ reasoning?: unknown;
84
+ reasoning_content?: unknown;
85
+ } | undefined): "reasoning" | "reasoning_content" | null;
86
+ /**
87
+ * Add a streamed `reasoning_details` fragment to the list being built, so a stream yields the same items as a JSON reply. Fragments with the
88
+ * same `index` and `type` are one item: `text` and `summary` are concatenated and `signature`, `data`, `format` and `id` are taken from the
89
+ * fragment that carries them. A fragment without an `index`, or a new one, is appended; anything that is not an object is skipped.
90
+ * @param {any[]} list - Items so far (mutated).
91
+ * @param {any} fragment - One element of a streamed `reasoning_details` array.
92
+ * @returns {any[]} The same list.
93
+ */
94
+ export function mergeDetail(list: any[], fragment: any): any[];
95
+ /**
96
+ * Readable text of a `reasoning_details` list (`reasoning.text` and `reasoning.summary` items), for a thinking block whose reasoning
97
+ * arrived only as structured details.
98
+ * @param {unknown} details - `reasoning_details` value.
99
+ * @returns {string} Concatenated text ("" for encrypted-only details).
100
+ */
101
+ export function detailsText(details: unknown): string;
102
+ /**
103
+ * Reasoning text carried by an OpenAI message or delta (`reasoning` or the older `reasoning_content`).
104
+ * @param {{ reasoning?: unknown, reasoning_content?: unknown }|undefined} part - Message or delta.
105
+ * @returns {string} Reasoning text ("" when absent).
106
+ */
107
+ export function reasoningOf(part: {
108
+ reasoning?: unknown;
109
+ reasoning_content?: unknown;
110
+ } | undefined): string;
111
+ /**
112
+ * Split a complete content string into reasoning and answer. Only a LEADING `<think>...</think>` is recognised
113
+ * (whitespace before it and after it is ignored); an unterminated `<think>` treats everything as reasoning.
114
+ *
115
+ * With `open` the output is taken to start inside a thought (the chat template already wrote `<think>` into the
116
+ * generation prompt): everything before the first `</think>` is reasoning. When no `</think>` appears the whole
117
+ * content is answer text. A literal leading `<think>` is still honoured.
118
+ * @param {string} content - Full message content.
119
+ * @param {boolean} [open] - The output starts inside a thought.
120
+ * @returns {{ reasoning: string, text: string }} Split result.
121
+ */
122
+ export function splitThink(content: string, open?: boolean): {
123
+ reasoning: string;
124
+ text: string;
125
+ };
126
+ /**
127
+ * Incremental version of {@link splitThink} for streamed content. Feed content deltas to `push()` and call `end()`
128
+ * once; each call returns `{ reasoning, text }` pieces that are safe to emit now (partial tags are held back).
129
+ *
130
+ * With `open` (see {@link splitThink}) nothing is emitted until the first `</think>` arrives, because only then is it known
131
+ * that the content so far was reasoning; if the stream ends first, everything held is released as answer text.
132
+ * @param {boolean} [open] - The output starts inside a thought.
133
+ *
134
+ * `release()` stops splitting: anything held is returned as text and all later content passes through as text (used once the server
135
+ * sends reasoning in its own field, when content is never split). `pending()` is true while, with `open`, it is not yet known whether
136
+ * the content so far is reasoning.
137
+ * @returns {{ push: (delta: string) => { reasoning: string, text: string }, end: () => { reasoning: string, text: string }, release: () => { reasoning: string, text: string }, pending: () => boolean }} Splitter.
138
+ */
139
+ export function createThinkSplitter(open?: boolean): {
140
+ push: (delta: string) => {
141
+ reasoning: string;
142
+ text: string;
143
+ };
144
+ end: () => {
145
+ reasoning: string;
146
+ text: string;
147
+ };
148
+ release: () => {
149
+ reasoning: string;
150
+ text: string;
151
+ };
152
+ pending: () => boolean;
153
+ };
154
+ /** `format` of the `reasoning_details` items that carry an Anthropic thinking block (text with its signature) or redacted thinking (data). */
155
+ export const ANTHROPIC_FORMAT: "anthropic-claude-v1";
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The hint for a refusal. When `toolFallback` is already on, suggesting it would be wrong (the retry has been spent), so only the restart advice remains.
3
+ * A refusal that names `--jinja` is llama.cpp's and gets its own advice.
4
+ * @param {boolean} toolFallback - Whether the `toolFallback` setting is on for the endpoint.
5
+ * @param {string} [message] - The upstream error message being hinted.
6
+ * @returns {string} Hint text.
7
+ */
8
+ export function hint(toolFallback: boolean, message?: string): string;
9
+ /**
10
+ * Does an upstream error message say the server has no tool parser configured?
11
+ * @param {string} message - Upstream error message.
12
+ * @returns {boolean} True for a known "tool parsing not enabled" wording.
13
+ */
14
+ export function refused(message: string): boolean;
15
+ /**
16
+ * Does a Chat Completions body carry tools?
17
+ * @param {any} body - Upstream request body.
18
+ * @returns {boolean} True when `tools` is a non-empty array.
19
+ */
20
+ export function carries(body: any): boolean;
21
+ /**
22
+ * Copy a Chat Completions body without `tools`, `tool_choice` and `parallel_tool_calls`.
23
+ * @param {any} body - Upstream request body.
24
+ * @returns {any} New body.
25
+ */
26
+ export function strip(body: any): any;
27
+ /** Added to the error message the client receives when the upstream refuses tools for want of a tool parser. */
28
+ export const HINT: "The vLLM server was started without tool-call parsing. Restart it with --enable-auto-tool-choice and --tool-call-parser <parser> (the parser depends on the model family), or set the translator setting toolFallback to retry without tools.";
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Find what the conversation has already loaded: tools named by `tool_reference` blocks, tools the assistant has called, tools a `ToolSearch`
3
+ * call selected by name (`select:A,B`, so a result that was cleared from a trimmed history does not unload them), and tools defined in
4
+ * `<function>` lines of `ToolSearch` results. Only `ToolSearch` results are read, and none for a `tool_use` id that appears on more than one
5
+ * call, so no other tool's output can add a tool. A definition that exists only in a result that history trimming removed is gone with it:
6
+ * the name stays in `loaded`, and `plan` copes with the missing definition.
7
+ * @param {any[]} messages - Anthropic messages.
8
+ * @param {Set<string>} [known] - Names of the tools the request lists; their definitions are not kept (the listed one wins) and do not count against the cap.
9
+ * @returns {{loaded: string[], searched: string[], called: string[], failed: string[], defined: Map<string, any>, capped: boolean}} Loaded names in order of first appearance, the names tool search itself loaded (selected, referenced or defined by a result; calling a tool is not evidence), names the assistant called, those of them whose every call came back as an error, the definitions found in results and whether `LIMITS.definitions` turned one away.
10
+ */
11
+ export function scan(messages: any[], known?: Set<string>): {
12
+ loaded: string[];
13
+ searched: string[];
14
+ called: string[];
15
+ failed: string[];
16
+ defined: Map<string, any>;
17
+ capped: boolean;
18
+ };
19
+ /**
20
+ * Decide which tools a request offers.
21
+ *
22
+ * - `expand`: tools without `defer_loading` in request order, then the deferred tools the conversation has loaded (or `tool_choice` names), in
23
+ * the order they were first loaded, so the list only ever grows at its end. `ToolSearch` is offered while there is something left to load.
24
+ * - `inline`: every tool except the placeholder, in request order, then the tools a `ToolSearch` result defined in text; `ToolSearch` is offered only when the placeholder shows that Claude Code holds
25
+ * tools whose definitions it did not send. `expand` behaves like `inline` when the request has neither a `ToolSearch` nor the placeholder.
26
+ * - `native`: every tool as sent; the server is expected to understand `defer_loading` and `tool_reference`.
27
+ *
28
+ * A tool a `ToolSearch` result defined in text and that the request does not list is offered after the others (`expand` only).
29
+ * `deferredTools: "drop"` leaves the deferred tools to the caller to drop and hides `ToolSearch`, which would have nothing to load.
30
+ * @param {any[]} tools - Anthropic tools of the request.
31
+ * @param {{loaded: string[], searched: string[], called: string[], failed: string[], defined: Map<string, any>}} found - Result of `scan`.
32
+ * @param {{toolSearch: "expand" | "inline" | "native", deferredTools: "forward" | "drop"}} s - Settings.
33
+ * @param {any} [choice] - The request's `tool_choice`.
34
+ * A tool tool search loaded (selected, referenced or defined by a result) whose definition is no longer anywhere (its `ToolSearch` result was trimmed) keeps `ToolSearch` offered so the
35
+ * model can search again, and one the assistant already called is offered with an empty object schema (`TRIMMED`) so its calls in the history
36
+ * still map to a tool.
37
+ * @returns {{offer: any[], loaded: string[], hidden: string[], stubbed: string[]}} Tools to convert, names of the deferred tools that are loaded, the names left out on purpose and the names of tools offered with a placeholder schema.
38
+ */
39
+ export function plan(tools: any[], found: {
40
+ loaded: string[];
41
+ searched: string[];
42
+ called: string[];
43
+ failed: string[];
44
+ defined: Map<string, any>;
45
+ }, s: {
46
+ toolSearch: "expand" | "inline" | "native";
47
+ deferredTools: "forward" | "drop";
48
+ }, choice?: any): {
49
+ offer: any[];
50
+ loaded: string[];
51
+ hidden: string[];
52
+ stubbed: string[];
53
+ };
54
+ /**
55
+ *
56
+ * @Project: @cldmv/llm-translator
57
+ * @Filename: /src/lib/toolsearch.mjs
58
+ * @Date: 2026-10-26T06:00:20-07:00 (1793019620)
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-26T15:38:55-07:00 (1793054335)
64
+ * -----
65
+ * @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
66
+ *
67
+ */
68
+ /**
69
+ * @fileoverview Claude Code's tool search: which tools a request offers when some are deferred.
70
+ *
71
+ * Claude Code marks tools it does not want to send up front with `defer_loading: true`, lists their names in a system reminder, and
72
+ * gives the model a client tool, `ToolSearch`, whose result names the tools to load: as `tool_reference` blocks, and as `<function>`
73
+ * JSON lines in the result text. A reserved `DeferredToolPlaceholder` tool (also deferred, "never call this tool") keeps the feature
74
+ * active. Anthropic's API expands the references into real tool definitions; an OpenAI-style server cannot, so the translator does it
75
+ * by changing the tools list of the next request.
76
+ * @module @cldmv/llm-translator/lib/toolsearch
77
+ */
78
+ /** The client tool the model calls to load deferred tools. */
79
+ export const SEARCH_TOOL: "ToolSearch";
80
+ /** The reserved deferred tool that is never offered. */
81
+ export const PLACEHOLDER_TOOL: "DeferredToolPlaceholder";
82
+ /** Limits on definitions read from `ToolSearch` result text, which the model's tools list is built from: count in a conversation (only definitions of tools the request does not list count), characters per definition, name, description and serialized schema. */
83
+ export const LIMITS: Readonly<{
84
+ definitions: 512;
85
+ line: 131072;
86
+ name: 256;
87
+ description: 16384;
88
+ schema: 65536;
89
+ }>;
90
+ /** Description of a tool whose definition was trimmed from the conversation. */
91
+ export const TRIMMED: "The definition of this tool was trimmed from the conversation; use ToolSearch to see its parameters.";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * `fetch`-style request.
3
+ * @param {string|URL} url - Absolute http(s) URL.
4
+ * @param {{ method?: string, headers?: Record<string, string>, body?: string|Uint8Array, signal?: AbortSignal, redirect?: string, connectTimeout?: number }} [init] - Request options.
5
+ * @param {{ http: any, https: any }} [modules] - Protocol modules (overridable for tests).
6
+ * @returns {Promise<Response>} Response whose `body` is a web `ReadableStream`.
7
+ */
8
+ export function transport(url: string | URL, init?: {
9
+ method?: string;
10
+ headers?: Record<string, string>;
11
+ body?: string | Uint8Array;
12
+ signal?: AbortSignal;
13
+ redirect?: string;
14
+ connectTimeout?: number;
15
+ }, modules?: {
16
+ http: any;
17
+ https: any;
18
+ }): Promise<Response>;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Anthropic error type for an HTTP status.
3
+ * @param {number} status - HTTP status.
4
+ * @returns {string} Anthropic `error.type`.
5
+ */
6
+ export function anthropicType(status: number): string;
7
+ /**
8
+ * OpenAI error type for an HTTP status.
9
+ * @param {number} status - HTTP status.
10
+ * @returns {string} OpenAI `error.type`.
11
+ */
12
+ export function openaiType(status: number): string;
13
+ /**
14
+ * Pull `{ message, type, code, param }` out of any known upstream error body shape: nested OpenAI/vLLM
15
+ * (`{error:{message,type,param,code}}`), flat old vLLM (`{message,type,param,code}`), Anthropic
16
+ * (`{type:"error",error:{type,message}}`), `{error:"text"}`, a bare string, or nothing.
17
+ * @param {*} body - Parsed upstream body.
18
+ * @param {number} status - HTTP status, used for the fallback message.
19
+ * @returns {{message: string, type?: string, code?: *, param?: *}} Normalized error.
20
+ */
21
+ export function parse(body: any, status: number): {
22
+ message: string;
23
+ type?: string;
24
+ code?: any;
25
+ param?: any;
26
+ };
27
+ /**
28
+ * Render an error body in the client protocol.
29
+ * @param {"anthropic" | "openai"} front - Client protocol.
30
+ * @param {number} status - HTTP status.
31
+ * @param {{message: string, type?: string, code?: *, param?: *}} info - Normalized error.
32
+ * @param {any} [options] - `{ native: boolean }`: keep the upstream `type` when it is already valid for this protocol.
33
+ * @returns {{status: number, body: any}} Status and body in the client shape.
34
+ */
35
+ export function render(front: "anthropic" | "openai", status: number, info: {
36
+ message: string;
37
+ type?: string;
38
+ code?: any;
39
+ param?: any;
40
+ }, options?: any): {
41
+ status: number;
42
+ body: any;
43
+ };
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Normalize a usage object. Fields the source did not carry stay null: unknown is never reported as 0.
3
+ * @param {"anthropic" | "openai" | "folded"} protocol - Shape of `usage`: Anthropic (`input_tokens`, ...), OpenAI (`prompt_tokens`, ...) or `folded` (Anthropic, reported the way the OpenAI front does).
4
+ * @param {any} usage - A `usage` object.
5
+ * @returns {Record<string, number | null> | null} Normalized counts, or null when there is no usage (an absent object, or all zeros, which a
6
+ * converter writes when the upstream reported nothing and no real reply has).
7
+ */
8
+ export function normalize(protocol: "anthropic" | "openai" | "folded", usage: any): Record<string, number | null> | null;
9
+ /**
10
+ * Create the meter for one request.
11
+ * @param {{front: "anthropic" | "openai", requested: string | null, model: string | null, upstream: string | null, stream: boolean}} meta - Request facts; `model` and `upstream` may be filled in later.
12
+ * @param {((report: UsageReport) => any) | undefined} onUsage - Caller's callback.
13
+ * @param {(message: string) => void} warn - Receives a message when `onUsage` throws or rejects.
14
+ * @returns {{meta: any, usage: Promise<UsageReport>, json: (body: any) => void, event: (data: any) => void, finish: (reason: UsageReport["reason"]) => void, estimate: (counts: {input_tokens: number | null, output_tokens: number | null}) => void, source: (read: (() => any) | null, protocol?: "openai" | "folded") => void, track: (source: AsyncIterable<any>, signal?: AbortSignal, hide?: boolean) => any}} Meter.
15
+ */
16
+ export function meter(meta: {
17
+ front: "anthropic" | "openai";
18
+ requested: string | null;
19
+ model: string | null;
20
+ upstream: string | null;
21
+ stream: boolean;
22
+ }, onUsage: ((report: UsageReport) => any) | undefined, warn: (message: string) => void): {
23
+ meta: any;
24
+ usage: Promise<UsageReport>;
25
+ json: (body: any) => void;
26
+ event: (data: any) => void;
27
+ finish: (reason: UsageReport["reason"]) => void;
28
+ estimate: (counts: {
29
+ input_tokens: number | null;
30
+ output_tokens: number | null;
31
+ }) => void;
32
+ source: (read: (() => any) | null, protocol?: "openai" | "folded") => void;
33
+ track: (source: AsyncIterable<any>, signal?: AbortSignal, hide?: boolean) => any;
34
+ };
35
+ export type UsageReport = {
36
+ /**
37
+ * - Prompt tokens that were not served from cache, or null when unknown.
38
+ */
39
+ input_tokens: number | null;
40
+ /**
41
+ * - Completion tokens, or null when unknown.
42
+ */
43
+ output_tokens: number | null;
44
+ /**
45
+ * - Prompt tokens served from cache, or null when unknown.
46
+ */
47
+ cache_read_input_tokens: number | null;
48
+ /**
49
+ * - Prompt tokens written to cache, or null when unknown.
50
+ */
51
+ cache_creation_input_tokens: number | null;
52
+ /**
53
+ * - True when the reply carried usage; false means every token field is null.
54
+ */
55
+ reported: boolean;
56
+ /**
57
+ * - The estimates written into the client reply for the sides the upstream did not report (a side the upstream did report is null here); null when nothing was estimated (the upstream reported both sides, a pass-through reply, a reply that failed first). Never mixed into the token fields.
58
+ */
59
+ estimated: {
60
+ input_tokens: number | null;
61
+ output_tokens: number | null;
62
+ } | null;
63
+ /**
64
+ * - True only when the reply finished normally.
65
+ */
66
+ complete: boolean;
67
+ /**
68
+ * - Why the report was produced.
69
+ */
70
+ reason: "done" | "aborted" | "error" | "upstream_error";
71
+ /**
72
+ * - Model name sent upstream.
73
+ */
74
+ model: string | null;
75
+ /**
76
+ * - Model the client asked for.
77
+ */
78
+ requested: string | null;
79
+ /**
80
+ * - Upstream endpoint name.
81
+ */
82
+ upstream: string | null;
83
+ /**
84
+ * - Client protocol.
85
+ */
86
+ front: "anthropic" | "openai";
87
+ /**
88
+ * - Whether the client asked for a stream.
89
+ */
90
+ stream: boolean;
91
+ };
@@ -0,0 +1,26 @@
1
+ /**
2
+ *
3
+ * @Project: @cldmv/llm-translator
4
+ * @Filename: /src/lib/warn.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 One-time warnings.
17
+ * @module @cldmv/llm-translator/lib/warn
18
+ */
19
+ /**
20
+ * Print a warning once per translator unless `silent` is set.
21
+ * @param {any} config - Normalized config (`silent`).
22
+ * @param {any} state - Translator state (`warned` set).
23
+ * @param {string} message - Warning text.
24
+ * @returns {boolean} True when the warning was printed.
25
+ */
26
+ export function warn(config: any, state: any, message: string): boolean;