@cldmv/llm-translator 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -7
- package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +17 -0
- package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +2 -0
- package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +2 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +6 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +2 -0
- package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +2 -0
- package/dist/api/detect.mjs +2 -0
- package/dist/api/driver/anthropic/detect.mjs +2 -0
- package/dist/api/driver/anthropic/info.mjs +2 -0
- package/dist/api/driver/anthropic/model/list.mjs +2 -0
- package/dist/api/driver/anthropic/settings.mjs +2 -0
- package/dist/api/driver/anthropic/token/count.mjs +2 -0
- package/dist/api/driver/compatible/detect.mjs +2 -0
- package/dist/api/driver/compatible/info.mjs +2 -0
- package/dist/api/driver/compatible/model/list.mjs +2 -0
- package/dist/api/driver/compatible/request.mjs +2 -0
- package/dist/api/driver/compatible/settings.mjs +2 -0
- package/dist/api/driver/openai/detect.mjs +2 -0
- package/dist/api/driver/openai/info.mjs +2 -0
- package/dist/api/driver/openai/model/list.mjs +2 -0
- package/dist/api/driver/openai/request.mjs +2 -0
- package/dist/api/driver/openai/settings.mjs +2 -0
- package/dist/api/driver/vllm/detect.mjs +2 -0
- package/dist/api/driver/vllm/error.mjs +2 -0
- package/dist/api/driver/vllm/info.mjs +2 -0
- package/dist/api/driver/vllm/model/list.mjs +2 -0
- package/dist/api/driver/vllm/probe.mjs +2 -0
- package/dist/api/driver/vllm/request.mjs +2 -0
- package/dist/api/driver/vllm/settings.mjs +2 -0
- package/dist/api/driver/vllm/token/count.mjs +2 -0
- package/dist/api/error/build.mjs +2 -0
- package/dist/api/error/error.mjs +2 -0
- package/dist/api/model/list.mjs +2 -0
- package/dist/api/request.mjs +3 -0
- package/dist/api/response.mjs +2 -0
- package/dist/api/send.mjs +2 -0
- package/dist/api/server/plugin.mjs +2 -0
- package/dist/api/server/start.mjs +2 -0
- package/dist/api/server/stop.mjs +2 -0
- package/dist/api/sse/encode.mjs +4 -0
- package/dist/api/sse/parse.mjs +2 -0
- package/dist/api/sse/ping.mjs +2 -0
- package/dist/api/sse/write.mjs +2 -0
- package/dist/api/status.mjs +2 -0
- package/dist/api/stream.mjs +2 -0
- package/dist/api/token/count.mjs +2 -0
- package/dist/bin/llm-translator.mjs +3 -0
- package/dist/index.mjs +1 -1
- package/dist/lib/arguments.mjs +2 -0
- package/dist/lib/capture.mjs +2 -0
- package/dist/lib/cli.mjs +29 -0
- package/dist/lib/config.mjs +2 -0
- package/dist/lib/documents.mjs +12 -0
- package/dist/lib/errors.mjs +2 -0
- package/dist/lib/estimate.mjs +2 -0
- package/dist/lib/finish.mjs +2 -0
- package/dist/lib/http.mjs +2 -0
- package/dist/lib/ids.mjs +2 -0
- package/dist/lib/images.mjs +2 -0
- package/dist/lib/models.mjs +2 -0
- package/dist/lib/normalize.mjs +2 -0
- package/dist/lib/passthrough.mjs +2 -0
- package/dist/lib/pdf.mjs +2 -0
- package/dist/lib/ping.mjs +2 -0
- package/dist/lib/route.mjs +2 -0
- package/dist/lib/routes.mjs +2 -0
- package/dist/lib/runtime.mjs +2 -0
- package/dist/lib/schema.mjs +2 -0
- package/dist/lib/settings.mjs +2 -0
- package/dist/lib/source.mjs +2 -0
- package/dist/lib/sse.mjs +10 -0
- package/dist/lib/thinking.mjs +2 -0
- package/dist/lib/transport.mjs +2 -0
- package/dist/lib/upstream-error.mjs +2 -0
- package/dist/lib/warn.mjs +2 -0
- package/package.json +30 -5
- package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +28 -0
- package/types/src/api/convert/anthropic/messages/to/openai/chat/response.d.mts +24 -0
- package/types/src/api/convert/anthropic/messages/to/openai/chat/stream.d.mts +29 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +30 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/response.d.mts +28 -0
- package/types/src/api/convert/openai/chat/to/anthropic/messages/stream.d.mts +38 -0
- package/types/src/api/detect.d.mts +21 -0
- package/types/src/api/driver/anthropic/detect.d.mts +26 -0
- package/types/src/api/driver/anthropic/info.d.mts +30 -0
- package/types/src/api/driver/anthropic/model/list.d.mts +20 -0
- package/types/src/api/driver/anthropic/settings.d.mts +23 -0
- package/types/src/api/driver/anthropic/token/count.d.mts +20 -0
- package/types/src/api/driver/compatible/detect.d.mts +23 -0
- package/types/src/api/driver/compatible/info.d.mts +29 -0
- package/types/src/api/driver/compatible/model/list.d.mts +20 -0
- package/types/src/api/driver/compatible/request.d.mts +20 -0
- package/types/src/api/driver/compatible/settings.d.mts +23 -0
- package/types/src/api/driver/openai/detect.d.mts +26 -0
- package/types/src/api/driver/openai/info.d.mts +29 -0
- package/types/src/api/driver/openai/model/list.d.mts +20 -0
- package/types/src/api/driver/openai/request.d.mts +20 -0
- package/types/src/api/driver/openai/settings.d.mts +23 -0
- package/types/src/api/driver/vllm/detect.d.mts +20 -0
- package/types/src/api/driver/vllm/error.d.mts +28 -0
- package/types/src/api/driver/vllm/info.d.mts +29 -0
- package/types/src/api/driver/vllm/model/list.d.mts +20 -0
- package/types/src/api/driver/vllm/probe.d.mts +39 -0
- package/types/src/api/driver/vllm/request.d.mts +22 -0
- package/types/src/api/driver/vllm/settings.d.mts +22 -0
- package/types/src/api/driver/vllm/token/count.d.mts +20 -0
- package/types/src/api/error/build.d.mts +26 -0
- package/types/src/api/error/error.d.mts +29 -0
- package/types/src/api/model/list.d.mts +21 -0
- package/types/src/api/request.d.mts +35 -0
- package/types/src/api/response.d.mts +22 -0
- package/types/src/api/send.d.mts +32 -0
- package/types/src/api/server/plugin.d.mts +24 -0
- package/types/src/api/server/start.d.mts +26 -0
- package/types/src/api/server/stop.d.mts +19 -0
- package/types/src/api/sse/encode.d.mts +26 -0
- package/types/src/api/sse/parse.d.mts +24 -0
- package/types/src/api/sse/ping.d.mts +30 -0
- package/types/src/api/sse/write.d.mts +26 -0
- package/types/src/api/status.d.mts +22 -0
- package/types/src/api/stream.d.mts +25 -0
- package/types/src/api/token/count.d.mts +24 -0
- package/types/src/index.d.mts +61 -4
- package/types/src/lib/arguments.d.mts +30 -0
- package/types/src/lib/capture.d.mts +28 -0
- package/types/src/lib/cli.d.mts +49 -0
- package/types/src/lib/config.d.mts +65 -0
- package/types/src/lib/documents.d.mts +43 -0
- package/types/src/lib/errors.d.mts +144 -0
- package/types/src/lib/estimate.d.mts +24 -0
- package/types/src/lib/finish.d.mts +73 -0
- package/types/src/lib/http.d.mts +58 -0
- package/types/src/lib/ids.d.mts +55 -0
- package/types/src/lib/images.d.mts +39 -0
- package/types/src/lib/models.d.mts +25 -0
- package/types/src/lib/normalize.d.mts +37 -0
- package/types/src/lib/passthrough.d.mts +22 -0
- package/types/src/lib/pdf.d.mts +67 -0
- package/types/src/lib/ping.d.mts +24 -0
- package/types/src/lib/route.d.mts +38 -0
- package/types/src/lib/routes.d.mts +22 -0
- package/types/src/lib/runtime.d.mts +24 -0
- package/types/src/lib/schema.d.mts +22 -0
- package/types/src/lib/settings.d.mts +175 -0
- package/types/src/lib/source.d.mts +32 -0
- package/types/src/lib/sse.d.mts +72 -0
- package/types/src/lib/thinking.d.mts +60 -0
- package/types/src/lib/transport.d.mts +31 -0
- package/types/src/lib/upstream-error.d.mts +57 -0
- package/types/src/lib/warn.d.mts +26 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/finish.mjs
|
|
5
|
+
* @Date: 2026-10-25T12:28:19-07:00 (1792956499)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-25T12:59:01-07:00 (1792958341)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Stop-reason and usage mapping between the two protocols.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Anthropic stop reason (and stop sequence) for an OpenAI choice.
|
|
20
|
+
* @param {string|null|undefined} finishReason - OpenAI `finish_reason`.
|
|
21
|
+
* @param {{ hasToolCalls: boolean, matchedStop?: unknown, argumentsComplete?: boolean }} info - Whether tool calls were produced, vLLM's `stop_reason` (a string for a matched stop string), and whether the last tool call's arguments are complete. A `length` stop with incomplete arguments is `max_tokens`, not `tool_use`.
|
|
22
|
+
* @returns {{ stop_reason: string, stop_sequence: string|null }} Anthropic stop fields.
|
|
23
|
+
*/
|
|
24
|
+
export declare function toAnthropicStop(finishReason: string | null | undefined, { hasToolCalls, matchedStop, argumentsComplete }: {
|
|
25
|
+
hasToolCalls: boolean;
|
|
26
|
+
matchedStop?: unknown;
|
|
27
|
+
argumentsComplete?: boolean;
|
|
28
|
+
}): {
|
|
29
|
+
stop_reason: string;
|
|
30
|
+
stop_sequence: string | null;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* OpenAI `finish_reason` for an Anthropic stop reason.
|
|
34
|
+
* @param {string|null|undefined} stopReason - Anthropic `stop_reason`.
|
|
35
|
+
* @returns {string} OpenAI finish reason.
|
|
36
|
+
*/
|
|
37
|
+
export declare function toOpenAIFinish(stopReason: string | null | undefined): string;
|
|
38
|
+
/**
|
|
39
|
+
* Anthropic usage from an OpenAI usage object. Cached prompt tokens (reported by vLLM only with prompt-token
|
|
40
|
+
* details enabled) are reported as `cache_read_input_tokens` and subtracted from `input_tokens`.
|
|
41
|
+
* @param {{ prompt_tokens?: number, completion_tokens?: number, prompt_tokens_details?: { cached_tokens?: number }|null }|null|undefined} usage - OpenAI usage.
|
|
42
|
+
* @returns {{ input_tokens: number, output_tokens: number, cache_creation_input_tokens: number, cache_read_input_tokens: number }} Anthropic usage.
|
|
43
|
+
*/
|
|
44
|
+
export declare function toAnthropicUsage(usage: {
|
|
45
|
+
prompt_tokens?: number;
|
|
46
|
+
completion_tokens?: number;
|
|
47
|
+
prompt_tokens_details?: {
|
|
48
|
+
cached_tokens?: number;
|
|
49
|
+
} | null;
|
|
50
|
+
} | null | undefined): {
|
|
51
|
+
input_tokens: number;
|
|
52
|
+
output_tokens: number;
|
|
53
|
+
cache_creation_input_tokens: number;
|
|
54
|
+
cache_read_input_tokens: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* OpenAI usage from an Anthropic usage object (cache tokens count as prompt tokens).
|
|
58
|
+
* @param {{ input_tokens?: number, output_tokens?: number, cache_creation_input_tokens?: number|null, cache_read_input_tokens?: number|null }|null|undefined} usage - Anthropic usage.
|
|
59
|
+
* @returns {{ prompt_tokens: number, completion_tokens: number, total_tokens: number, prompt_tokens_details?: { cached_tokens: number } }} OpenAI usage.
|
|
60
|
+
*/
|
|
61
|
+
export declare function toOpenAIUsage(usage: {
|
|
62
|
+
input_tokens?: number;
|
|
63
|
+
output_tokens?: number;
|
|
64
|
+
cache_creation_input_tokens?: number | null;
|
|
65
|
+
cache_read_input_tokens?: number | null;
|
|
66
|
+
} | null | undefined): {
|
|
67
|
+
prompt_tokens: number;
|
|
68
|
+
completion_tokens: number;
|
|
69
|
+
total_tokens: number;
|
|
70
|
+
prompt_tokens_details?: {
|
|
71
|
+
cached_tokens: number;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/http.mjs
|
|
5
|
+
* @Date: 2026-10-25T12:29:36-07:00 (1792956576)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-25T12:59:02-07:00 (1792958342)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Small HTTP helpers shared by the api leaves: URL joining, auth headers, JSON requests.
|
|
17
|
+
* @module @cldmv/llm-translator/lib/http
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Join an endpoint base URL and a path.
|
|
21
|
+
* @param {string} base - Base URL without trailing slash.
|
|
22
|
+
* @param {string} path - Path starting with `/`.
|
|
23
|
+
* @returns {string} Absolute URL.
|
|
24
|
+
*/
|
|
25
|
+
export declare function join(base: string, path: string): string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether a listen address only accepts connections from this machine.
|
|
28
|
+
* @param {string} host - Host or address.
|
|
29
|
+
* @returns {boolean} True for `localhost`, `127.x.x.x` and `::1`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isLoopback(host: string): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Build the request headers for an upstream call.
|
|
34
|
+
* @param {any} endpoint - Runtime endpoint.
|
|
35
|
+
* @param {string} [auth] - `"bearer"` (default) or `"x-api-key"`.
|
|
36
|
+
* @param {Record<string,string>} [extra] - Extra headers applied last.
|
|
37
|
+
* @returns {Record<string,string>} Header map.
|
|
38
|
+
*/
|
|
39
|
+
export declare function headers(endpoint: any, auth?: string, extra?: Record<string, string>): Record<string, string>;
|
|
40
|
+
/**
|
|
41
|
+
* Convert a fetch `Headers` object (or plain object) to a lowercase plain object.
|
|
42
|
+
* @param {Headers | Record<string,string> | undefined} source - Header source.
|
|
43
|
+
* @returns {Record<string,string>} Plain header map.
|
|
44
|
+
*/
|
|
45
|
+
export declare function plain(source: Headers | Record<string, string> | undefined): Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Perform a request and parse the JSON body. Never throws: network failures and non-JSON bodies are reported in the result.
|
|
48
|
+
* @param {Function} transport - fetch-compatible function.
|
|
49
|
+
* @param {string} url - Absolute URL.
|
|
50
|
+
* @param {any} [options] - `{ method, headers, body, signal, timeout }`.
|
|
51
|
+
* @returns {Promise<{ok: boolean, status: number, body: any, error?: Error}>} Outcome.
|
|
52
|
+
*/
|
|
53
|
+
export declare function json(transport: Function, url: string, options?: any): Promise<{
|
|
54
|
+
ok: boolean;
|
|
55
|
+
status: number;
|
|
56
|
+
body: any;
|
|
57
|
+
error?: Error;
|
|
58
|
+
}>;
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
/**
|
|
16
|
+
* Anthropic style message id.
|
|
17
|
+
* @returns {string} `msg_...` id.
|
|
18
|
+
*/
|
|
19
|
+
export declare function messageId(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Anthropic style tool-use id.
|
|
22
|
+
* @returns {string} `toolu_...` id.
|
|
23
|
+
*/
|
|
24
|
+
export declare function toolUseId(): string;
|
|
25
|
+
/**
|
|
26
|
+
* OpenAI style completion id.
|
|
27
|
+
* @returns {string} `chatcmpl-...` id.
|
|
28
|
+
*/
|
|
29
|
+
export declare function completionId(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). Valid ids pass through unchanged, invalid characters
|
|
32
|
+
* (for example kimi's `functions.name:0`) become `_`, and a missing or empty id is replaced by a fresh one. The
|
|
33
|
+
* mapping is deterministic so a tool call and its later tool result agree.
|
|
34
|
+
* @param {unknown} id - Upstream id.
|
|
35
|
+
* @returns {string} Anthropic-valid id.
|
|
36
|
+
*/
|
|
37
|
+
export declare function anthropicToolId(id: unknown): string;
|
|
38
|
+
/**
|
|
39
|
+
* Create a tool-name fitter. Names that already satisfy `pattern` and `max` pass through; others are sanitized and,
|
|
40
|
+
* when too long, shortened with a hash suffix. Every changed name is recorded in `map` (`fitted -> original`) so the
|
|
41
|
+
* original can be restored on the way back. Fitted names are kept unique.
|
|
42
|
+
* @param {{ max: number, map?: Record<string, string> }} options - Maximum length and the (JSON-serializable) map to record into.
|
|
43
|
+
* @returns {(name: string) => string} Fitter function.
|
|
44
|
+
*/
|
|
45
|
+
export declare function createNameFitter({ max, map }: {
|
|
46
|
+
max: number;
|
|
47
|
+
map?: Record<string, string>;
|
|
48
|
+
}): (name: string) => string;
|
|
49
|
+
/**
|
|
50
|
+
* Original tool name for a fitted one (names that were never changed map to themselves).
|
|
51
|
+
* @param {Record<string, string>} map - `fitted -> original` map from the conversion state.
|
|
52
|
+
* @param {string} name - Name seen on the wire.
|
|
53
|
+
* @returns {string} Original name.
|
|
54
|
+
*/
|
|
55
|
+
export declare function restoreName(map: Record<string, string>, name: string): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/images.mjs
|
|
5
|
+
* @Date: 2026-10-26T06:00:20-07:00 (1793019620)
|
|
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-26T15:38:55-07:00 (1793054335)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Image handling for upstreams that turn out to be text-only: recognising the refusal, finding images in a
|
|
17
|
+
* Chat Completions body and replacing them with a note.
|
|
18
|
+
* @module @cldmv/llm-translator/lib/images
|
|
19
|
+
*/
|
|
20
|
+
/** 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;
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Fetch and normalize the models an upstream reports.
|
|
17
|
+
* @param {any} context - `{ endpoint, transport, signal, timeout, auth, extraHeaders }`.
|
|
18
|
+
* @returns {Promise<{id: string, created?: number, owned_by?: string, max_model_len?: number}[] | null>} Models, or null when the upstream did not answer with a model list.
|
|
19
|
+
*/
|
|
20
|
+
export declare function fetchModels({ endpoint, transport, signal, timeout, auth, extraHeaders }: any): Promise<{
|
|
21
|
+
id: string;
|
|
22
|
+
created?: number;
|
|
23
|
+
owned_by?: string;
|
|
24
|
+
max_model_len?: number;
|
|
25
|
+
}[] | null>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/normalize.mjs
|
|
5
|
+
* @Date: 2026-10-25T12:29:36-07:00 (1792956576)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-25T12:59:02-07:00 (1792958342)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Request normalizers shared by the OpenAI-protocol drivers.
|
|
17
|
+
* @module @cldmv/llm-translator/lib/normalize
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Remove tool fields that strict OpenAI-compatible servers reject when no tools are present.
|
|
21
|
+
* @param {any} body - Chat Completions request body (mutated copy is returned).
|
|
22
|
+
* @returns {any} Normalized body.
|
|
23
|
+
*/
|
|
24
|
+
export declare function noEmptyTools(body: any): any;
|
|
25
|
+
/**
|
|
26
|
+
* Parse a server version string (`0.19.0rc1`, `0.32.0+cu126`) into numeric parts.
|
|
27
|
+
* @param {string | undefined} version - Version string.
|
|
28
|
+
* @returns {number[] | null} `[major, minor, patch]`, or null when unparseable.
|
|
29
|
+
*/
|
|
30
|
+
export declare function semver(version: string | undefined): number[] | null;
|
|
31
|
+
/**
|
|
32
|
+
* Is `version` at least `minimum`? An unknown version counts as new (latest behavior).
|
|
33
|
+
* @param {string | undefined} version - Version under test.
|
|
34
|
+
* @param {string} minimum - Minimum version such as `"0.19.0"`.
|
|
35
|
+
* @returns {boolean} True when the version is unknown or >= minimum.
|
|
36
|
+
*/
|
|
37
|
+
export declare function atLeast(version: string | undefined, minimum: string): boolean;
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
|
17
|
+
* controls such as `kv_transfer_params`, `vllm_xargs`, `chat_template`, `cache_salt` or `priority`. `stream_options` is
|
|
18
|
+
* reduced to the usage flags.
|
|
19
|
+
* @param {any} body - Client request body.
|
|
20
|
+
* @returns {any} Filtered copy.
|
|
21
|
+
*/
|
|
22
|
+
export declare function filterChatBody(body: any): any;
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
/**
|
|
16
|
+
* Load the PDF libraries. Safe to call repeatedly; each call re-imports. A library that fails to load is reported
|
|
17
|
+
* in the result instead of throwing, so the translator can degrade (text only, or a note).
|
|
18
|
+
* @param {(name: string) => Promise<any>} [importer] - Module importer (overridable for tests).
|
|
19
|
+
* @returns {Promise<{ unpdf: boolean, canvas: boolean, errors: { unpdf?: string, canvas?: string } }>} Load result.
|
|
20
|
+
*/
|
|
21
|
+
export declare function load(importer?: (name: string) => Promise<any>): Promise<{
|
|
22
|
+
unpdf: boolean;
|
|
23
|
+
canvas: boolean;
|
|
24
|
+
errors: {
|
|
25
|
+
unpdf?: string;
|
|
26
|
+
canvas?: string;
|
|
27
|
+
};
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Which libraries are currently loaded.
|
|
31
|
+
* @returns {{ unpdf: boolean, canvas: boolean }} Availability.
|
|
32
|
+
*/
|
|
33
|
+
export declare function available(): {
|
|
34
|
+
unpdf: boolean;
|
|
35
|
+
canvas: boolean;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Extract the text of every page.
|
|
39
|
+
* @param {Uint8Array} bytes - PDF bytes.
|
|
40
|
+
* @param {{ maxPages: number, maxBytes: number }} limits - Limits (Anthropic: 100 pages, 32 MB).
|
|
41
|
+
* @returns {Promise<{ pageCount: number, title: string|null, pages: string[] }>} Page texts and the document title.
|
|
42
|
+
* @throws {TranslatorError} `pdf_too_large`, `pdf_too_many_pages`, `pdf_unreadable` or `pdf_unavailable`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function extract(bytes: Uint8Array, limits: {
|
|
45
|
+
maxPages: number;
|
|
46
|
+
maxBytes: number;
|
|
47
|
+
}): Promise<{
|
|
48
|
+
pageCount: number;
|
|
49
|
+
title: string | null;
|
|
50
|
+
pages: string[];
|
|
51
|
+
}>;
|
|
52
|
+
/**
|
|
53
|
+
* Render pages to PNG images.
|
|
54
|
+
* @param {Uint8Array} bytes - PDF bytes.
|
|
55
|
+
* @param {number[]} pageNumbers - 1-based page numbers.
|
|
56
|
+
* @param {{ maxPages: number, maxBytes: number, scale: number }} options - Limits and render scale.
|
|
57
|
+
* @returns {Promise<{ media_type: "image/png", data: string }[]>} Base64 PNGs in page order.
|
|
58
|
+
* @throws {TranslatorError} `pdf_unavailable` when @napi-rs/canvas is not loaded.
|
|
59
|
+
*/
|
|
60
|
+
export declare function render(bytes: Uint8Array, pageNumbers: number[], options: {
|
|
61
|
+
maxPages: number;
|
|
62
|
+
maxBytes: number;
|
|
63
|
+
scale: number;
|
|
64
|
+
}): Promise<{
|
|
65
|
+
media_type: "image/png";
|
|
66
|
+
data: string;
|
|
67
|
+
}[]>;
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
/**
|
|
16
|
+
* Wrap an async iterable so that `marker` is yielded every `interval` ms while the source produces nothing.
|
|
17
|
+
* Closing the wrapper closes the source.
|
|
18
|
+
* @template T
|
|
19
|
+
* @param {AsyncIterable<T>} source - Event source.
|
|
20
|
+
* @param {number} interval - Silence in ms before a ping; `0` (or less) disables pings.
|
|
21
|
+
* @param {*} marker - Value yielded as the ping, or a function called each time one is due that returns it.
|
|
22
|
+
* @returns {AsyncGenerator<T | *>} Source events interleaved with pings.
|
|
23
|
+
*/
|
|
24
|
+
export declare function withPings<T>(source: AsyncIterable<T>, interval: number, marker: any): AsyncGenerator<T | any>;
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
/**
|
|
20
|
+
* Does the endpoint serve the model?
|
|
21
|
+
* @param {any} endpoint - Runtime endpoint.
|
|
22
|
+
* @param {string} model - Upstream model name.
|
|
23
|
+
* @returns {boolean} True when the endpoint lists the model, or is an Anthropic upstream and the model is a `claude-*` id.
|
|
24
|
+
*/
|
|
25
|
+
export declare function serves(endpoint: any, model: string): boolean;
|
|
26
|
+
/**
|
|
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`).
|
|
29
|
+
* @param {any[]} endpoints - Runtime endpoints.
|
|
30
|
+
* @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.
|
|
33
|
+
*/
|
|
34
|
+
export declare function pick(config: any, endpoints: any[], model: string | undefined): {
|
|
35
|
+
endpoint: any;
|
|
36
|
+
model: string | undefined;
|
|
37
|
+
unserved: boolean;
|
|
38
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/routes.mjs
|
|
5
|
+
* @Date: 2026-10-25T12:56:43-07:00 (1792958203)
|
|
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
|
+
* Register the routes for one front on a Fastify instance.
|
|
17
|
+
* @param {any} fastify - Fastify instance (usually an encapsulated plugin scope).
|
|
18
|
+
* @param {any} options - `{ apiKey, bodyLimit }`.
|
|
19
|
+
* @param {any} captured - Captured translator parts `{ front, interval, filter, send, count, list, build, encode, write }`; `front` applies to this instance only.
|
|
20
|
+
* @returns {void}
|
|
21
|
+
*/
|
|
22
|
+
export declare function register(fastify: any, options: any, captured: any): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/runtime.mjs
|
|
5
|
+
* @Date: 2026-10-25T12:30:20-07:00 (1792956620)
|
|
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
|
+
* Per-instance context (`config`, `state`); valid only synchronously inside a leaf call.
|
|
17
|
+
* @type {any}
|
|
18
|
+
*/
|
|
19
|
+
export declare const context: any;
|
|
20
|
+
/**
|
|
21
|
+
* Proxy over the whole api (`self.convert.…`, `self.driver.…`); valid only synchronously inside a leaf call.
|
|
22
|
+
* @type {any}
|
|
23
|
+
*/
|
|
24
|
+
export declare const self: any;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/lib/schema.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
|
+
* Return a sanitized deep copy of a JSON Schema: drops `$schema` and numeric bounds at or beyond +-(2^53 - 1)
|
|
17
|
+
* (Claude Code emits `maximum: 9007199254740991` on integers, which strict grammar backends dislike). The input is
|
|
18
|
+
* never mutated.
|
|
19
|
+
* @param {unknown} schema - JSON Schema (or any JSON value found inside one).
|
|
20
|
+
* @returns {unknown} Sanitized copy.
|
|
21
|
+
*/
|
|
22
|
+
export declare function sanitize(schema: unknown): unknown;
|