@cldmv/llm-translator 0.13.0 → 0.14.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 (73) hide show
  1. package/README.md +5 -5
  2. package/dist/api/convert/anthropic/messages/to/bedrock/converse/request.mjs +2 -0
  3. package/dist/api/convert/bedrock/converse/to/anthropic/messages/response.mjs +2 -0
  4. package/dist/api/convert/bedrock/converse/to/anthropic/messages/stream.mjs +2 -0
  5. package/dist/api/convert/bedrock/converse/to/openai/chat/response.mjs +2 -0
  6. package/dist/api/convert/bedrock/converse/to/openai/chat/stream.mjs +2 -0
  7. package/dist/api/convert/openai/chat/to/bedrock/converse/request.mjs +2 -0
  8. package/dist/api/driver/bedrock/detect.mjs +2 -0
  9. package/dist/api/driver/bedrock/info.mjs +2 -0
  10. package/dist/api/driver/bedrock/model/list.mjs +2 -0
  11. package/dist/api/driver/bedrock/path.mjs +2 -0
  12. package/dist/api/driver/bedrock/request.mjs +2 -0
  13. package/dist/api/driver/bedrock/sign.mjs +2 -0
  14. package/dist/api/driver/vertex/auth.mjs +2 -0
  15. package/dist/api/driver/vertex/detect.mjs +2 -0
  16. package/dist/api/driver/vertex/error.mjs +2 -0
  17. package/dist/api/driver/vertex/info.mjs +2 -0
  18. package/dist/api/driver/vertex/path.mjs +2 -0
  19. package/dist/api/driver/vertex/request.mjs +2 -0
  20. package/dist/api/driver/vertex/settings.mjs +2 -0
  21. package/dist/api/driver/vertex/token/count.mjs +2 -0
  22. package/dist/api/request.mjs +2 -2
  23. package/dist/api/send.mjs +1 -1
  24. package/dist/api/stream.mjs +1 -1
  25. package/dist/lib/bedrock.mjs +2 -0
  26. package/dist/lib/capture.mjs +1 -1
  27. package/dist/lib/config.mjs +1 -1
  28. package/dist/lib/converse.mjs +3 -0
  29. package/dist/lib/document-urls.mjs +2 -0
  30. package/dist/lib/documents.mjs +4 -4
  31. package/dist/lib/eventstream.mjs +2 -0
  32. package/dist/lib/fetch-url.mjs +2 -0
  33. package/dist/lib/google-auth.mjs +2 -0
  34. package/dist/lib/http.mjs +1 -1
  35. package/dist/lib/protocol.mjs +1 -1
  36. package/dist/lib/route.mjs +1 -1
  37. package/dist/lib/settings.mjs +1 -1
  38. package/dist/lib/sigv4.mjs +7 -0
  39. package/dist/lib/vertex.mjs +2 -0
  40. package/package.json +1 -1
  41. package/types/src/api/convert/anthropic/messages/to/bedrock/converse/request.d.mts +14 -0
  42. package/types/src/api/convert/bedrock/converse/to/anthropic/messages/response.d.mts +10 -0
  43. package/types/src/api/convert/bedrock/converse/to/anthropic/messages/stream.d.mts +14 -0
  44. package/types/src/api/convert/bedrock/converse/to/openai/chat/response.d.mts +10 -0
  45. package/types/src/api/convert/bedrock/converse/to/openai/chat/stream.d.mts +13 -0
  46. package/types/src/api/convert/openai/chat/to/bedrock/converse/request.d.mts +11 -0
  47. package/types/src/api/driver/bedrock/detect.d.mts +9 -0
  48. package/types/src/api/driver/bedrock/info.d.mts +36 -0
  49. package/types/src/api/driver/bedrock/model/list.d.mts +11 -0
  50. package/types/src/api/driver/bedrock/path.d.mts +9 -0
  51. package/types/src/api/driver/bedrock/request.d.mts +9 -0
  52. package/types/src/api/driver/bedrock/sign.d.mts +9 -0
  53. package/types/src/api/driver/vertex/auth.d.mts +7 -0
  54. package/types/src/api/driver/vertex/detect.d.mts +8 -0
  55. package/types/src/api/driver/vertex/error.d.mts +15 -0
  56. package/types/src/api/driver/vertex/info.d.mts +16 -0
  57. package/types/src/api/driver/vertex/path.d.mts +7 -0
  58. package/types/src/api/driver/vertex/request.d.mts +6 -0
  59. package/types/src/api/driver/vertex/settings.d.mts +23 -0
  60. package/types/src/api/driver/vertex/token/count.d.mts +7 -0
  61. package/types/src/api/request.d.mts +1 -1
  62. package/types/src/lib/bedrock.d.mts +74 -0
  63. package/types/src/lib/config.d.mts +32 -3
  64. package/types/src/lib/converse.d.mts +81 -0
  65. package/types/src/lib/document-urls.d.mts +55 -0
  66. package/types/src/lib/eventstream.d.mts +34 -0
  67. package/types/src/lib/fetch-url.d.mts +64 -0
  68. package/types/src/lib/google-auth.d.mts +58 -0
  69. package/types/src/lib/http.d.mts +3 -2
  70. package/types/src/lib/protocol.d.mts +3 -0
  71. package/types/src/lib/settings.d.mts +12 -0
  72. package/types/src/lib/sigv4.d.mts +41 -0
  73. package/types/src/lib/vertex.d.mts +55 -0
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The class of range an IP address is in.
3
+ * @param {string} address - IPv4 or IPv6 address (a zone id is ignored).
4
+ * @returns {string | null} `loopback`, `private`, `link-local`, `cgnat`, `unique-local`, `multicast`, `unspecified`, `reserved` or `mapped`; null for a public address; `invalid` when it is not an address.
5
+ */
6
+ export function classify(address: string): string | null;
7
+ /**
8
+ * Whether a host name matches an allow-list pattern: an exact host name, `*.example.com` (any subdomain, not `example.com` itself) or `*` (any host).
9
+ * @param {string} host - Lower-case host name without a trailing dot.
10
+ * @param {string[]} patterns - Allow-list.
11
+ * @returns {boolean} True when one pattern matches.
12
+ */
13
+ export function allowed(host: string, patterns: string[]): boolean;
14
+ /**
15
+ * Resolve a host name to every address it has, the way the connection will.
16
+ * @param {string} host - Host name.
17
+ * @returns {Promise<{address: string, family: number}[]>} Addresses.
18
+ */
19
+ export function lookup(host: string): Promise<{
20
+ address: string;
21
+ family: number;
22
+ }[]>;
23
+ /**
24
+ * Fetch a document.
25
+ * @param {string} input - The URL.
26
+ * @param {{allow: string[], maxBytes: number, timeout: number, maxRedirects: number, allowPrivate?: boolean, signal?: AbortSignal}} options - Limits and rules (see the `documentUrls` setting).
27
+ * @param {{lookup?: (host: string) => Promise<{address: string, family: number}[]>, modules?: {http: any, https: any}}} [deps] - Host name lookup and protocol modules (replaceable for tests).
28
+ * @returns {Promise<{bytes: Buffer, mediaType: string, url: string}>} The body, its media type (`application/pdf` or `text/plain`) and the final URL.
29
+ * @throws {FetchError} For anything that stops the fetch; never any other error.
30
+ */
31
+ export function fetchUrl(input: string, options: {
32
+ allow: string[];
33
+ maxBytes: number;
34
+ timeout: number;
35
+ maxRedirects: number;
36
+ allowPrivate?: boolean;
37
+ signal?: AbortSignal;
38
+ }, deps?: {
39
+ lookup?: (host: string) => Promise<{
40
+ address: string;
41
+ family: number;
42
+ }[]>;
43
+ modules?: {
44
+ http: any;
45
+ https: any;
46
+ };
47
+ }): Promise<{
48
+ bytes: Buffer;
49
+ mediaType: string;
50
+ url: string;
51
+ }>;
52
+ /**
53
+ * Why a fetch failed. `message` is safe to show the client; `detail` is for the server's own log and may name addresses and system errors.
54
+ */
55
+ export class FetchError extends Error {
56
+ /**
57
+ * @param {string} kind - `scheme`, `allow`, `dns`, `address`, `network`, `redirects`, `downgrade`, `status`, `size`, `type`, `encoding` or `timeout`.
58
+ * @param {string} message - What to tell the client.
59
+ * @param {string} [detail] - What to log.
60
+ */
61
+ constructor(kind: string, message: string, detail?: string);
62
+ kind: string;
63
+ detail: string;
64
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Forget every cached token and credential file (used by tests).
3
+ * @returns {void}
4
+ */
5
+ export function reset(): void;
6
+ /**
7
+ * Base64url without padding.
8
+ * @param {string | Buffer} input - Data.
9
+ * @returns {string} Encoded text.
10
+ */
11
+ export function base64url(input: string | Buffer): string;
12
+ /**
13
+ * Build the signed JWT a service account exchanges for an access token (RS256, `node:crypto`).
14
+ * @param {{client_email: string, private_key: string, private_key_id?: string, token_uri?: string}} credentials - A service-account key.
15
+ * @param {{now?: number, scope?: string}} [options] - `now` in ms (default the clock) and the OAuth scope.
16
+ * @returns {string} `header.claims.signature`.
17
+ */
18
+ export function assertion(credentials: {
19
+ client_email: string;
20
+ private_key: string;
21
+ private_key_id?: string;
22
+ token_uri?: string;
23
+ }, { now, scope }?: {
24
+ now?: number;
25
+ scope?: string;
26
+ }): string;
27
+ /**
28
+ * The path of gcloud's application default credentials file (`gcloud auth application-default login`).
29
+ * @param {Record<string, string | undefined>} env - Environment.
30
+ * @param {string} [platform] - `process.platform`.
31
+ * @returns {string} Path.
32
+ */
33
+ export function wellKnown(env: Record<string, string | undefined>, platform?: string): string;
34
+ /**
35
+ * The credentials the settings and environment name, without any network access: the `credentials` setting, then `GOOGLE_APPLICATION_CREDENTIALS`,
36
+ * then gcloud's application default credentials file when it exists.
37
+ * @param {any} settings - The upstream's `vertex` settings.
38
+ * @param {Record<string, string | undefined>} [env] - Environment.
39
+ * @returns {any | null} Credentials, or null when none are configured.
40
+ */
41
+ export function credentials(settings: any, env?: Record<string, string | undefined>): any | null;
42
+ /**
43
+ * Find an access token for Vertex AI. Sources, in order: the `getToken` callback (called on every request, it caches for itself), the `accessToken`
44
+ * setting, the upstream's `apiKey` (as a bearer token), the `credentials` setting, `GOOGLE_ACCESS_TOKEN`, `GOOGLE_APPLICATION_CREDENTIALS`, gcloud's
45
+ * application default credentials file, and last the metadata server when `metadataServer` is set. Tokens fetched from Google are cached until a
46
+ * minute before they expire, and concurrent callers share one refresh.
47
+ * @param {any} spec - `{ settings, apiKey, transport, timeout, signal, env, now }`. `settings` is the upstream's `vertex` object; `transport` is the
48
+ * `fetch`-compatible function the token requests go through.
49
+ * @returns {Promise<string>} Access token.
50
+ * @throws {TranslatorError} With code `upstream_auth` when there is no source or Google refuses.
51
+ */
52
+ export function token({ settings, apiKey, transport, timeout, signal, env, now }: any): Promise<string>;
53
+ /** The OAuth scope Vertex AI needs. */
54
+ export const SCOPE: "https://www.googleapis.com/auth/cloud-platform";
55
+ /** Where Google's token endpoint is, for credentials that do not name one. */
56
+ export const TOKEN_URI: "https://oauth2.googleapis.com/token";
57
+ /** Base URL of the GCE metadata server, used when `metadataServer` is `true`. */
58
+ export const METADATA_URL: "http://metadata.google.internal";
@@ -28,13 +28,14 @@ export function join(base: string, path: string): string;
28
28
  * and the model (Azure OpenAI puts the deployment and `api-version` there); otherwise `info().paths[kind]` is used, with the standard
29
29
  * OpenAI path for `responses`, `embeddings` and `models` when the driver does not name one.
30
30
  * @param {any} driver - Driver record (`info` and the optional `path` leaf).
31
- * @param {"chat" | "messages" | "responses" | "embeddings" | "models"} kind - Which call.
31
+ * @param {"chat" | "messages" | "responses" | "converse" | "embeddings" | "models"} kind - Which call.
32
32
  * @param {any} endpoint - Runtime endpoint.
33
33
  * @param {string} [model] - The upstream's model name for the request.
34
34
  * @param {string} [protocol] - The protocol the request goes over.
35
+ * @param {boolean} [stream] - Whether the reply is streamed (Vertex AI and Amazon Bedrock use a different path for it).
35
36
  * @returns {Promise<string>} Path starting with `/`.
36
37
  */
37
- export function pathOf(driver: any, kind: "chat" | "messages" | "responses" | "embeddings" | "models", endpoint: any, model?: string, protocol?: string): Promise<string>;
38
+ export function pathOf(driver: any, kind: "chat" | "messages" | "responses" | "converse" | "embeddings" | "models", endpoint: any, model?: string, protocol?: string, stream?: boolean): Promise<string>;
38
39
  /**
39
40
  * Whether a listen address only accepts connections from this machine.
40
41
  * @param {string} host - Host or address.
@@ -62,6 +62,7 @@ export const MESSAGES: "anthropic.messages";
62
62
  export const CHAT: "openai.chat";
63
63
  export const RESPONSES: "openai.responses";
64
64
  export const OLLAMA: "ollama.chat";
65
+ export const CONVERSE: "bedrock.converse";
65
66
  /** The canonical protocol ids, in the order the docs list them. */
66
67
  export const IDS: string[];
67
68
  /** Every accepted spelling of the `api` setting and the canonical id it stands for. */
@@ -81,4 +82,6 @@ export const ALIASES: Readonly<{
81
82
  "ollama.chat": "ollama.chat";
82
83
  native: "ollama.chat";
83
84
  ollama: "ollama.chat";
85
+ "bedrock.converse": "bedrock.converse";
86
+ converse: "bedrock.converse";
84
87
  }>;
@@ -23,6 +23,7 @@ export function resolve(...layers: (Partial<Settings> | null | undefined)[]): Se
23
23
  * @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.
24
24
  * @property {"extract"|"note"|"error"} documents - How `document` blocks are handled.
25
25
  * @property {((block: object, ctx: object) => Promise<object[]>|object[])|null} documentConverter - Override for `document` conversion; returns Anthropic content blocks.
26
+ * @property {null|{ allow: string[], maxBytes: number, timeout: number, maxRedirects: number, maxDocuments: number, allowPrivate: boolean }} documentUrls - Fetch the `url` sources of `document` blocks before conversion; `null` (the default) is off, and an object turns it on (`allow` is required).
26
27
  * @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.
27
28
  * @property {"pass"|"sanitize"} toolSchema - Sanitize tool JSON Schemas (drop `$schema`, remove unbounded integer limits).
28
29
  * @property {"forward"|"drop"} deferredTools - Offer `defer_loading` tools as `toolSearch` says, or drop them (and the search tool).
@@ -118,6 +119,17 @@ export type Settings = {
118
119
  * - Override for `document` conversion; returns Anthropic content blocks.
119
120
  */
120
121
  documentConverter: ((block: object, ctx: object) => Promise<object[]> | object[]) | null;
122
+ /**
123
+ * - Fetch the `url` sources of `document` blocks before conversion; `null` (the default) is off, and an object turns it on (`allow` is required).
124
+ */
125
+ documentUrls: null | {
126
+ allow: string[];
127
+ maxBytes: number;
128
+ timeout: number;
129
+ maxRedirects: number;
130
+ maxDocuments: number;
131
+ allowPrivate: boolean;
132
+ };
121
133
  /**
122
134
  * - PDF handling: render page images, Anthropic-like limits, image budget and render scale.
123
135
  */
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The `X-Amz-Date` form of a time: `YYYYMMDD'T'HHMMSS'Z'`.
3
+ * @param {Date} date - The time.
4
+ * @returns {string} Basic ISO 8601 UTC time.
5
+ */
6
+ export function stamp(date: Date): string;
7
+ /**
8
+ * Sign a request with Signature Version 4. Every header given is signed, together with `host`, `x-amz-date` and, when there is a session
9
+ * token, `x-amz-security-token`. The body is signed as the hash of the exact text or bytes passed in, so it must be what is sent.
10
+ * @param {object} request - The request.
11
+ * @param {string} [request.method] - HTTP method (default `GET`).
12
+ * @param {string} request.url - Absolute URL as it will be requested.
13
+ * @param {Record<string,string>} [request.headers] - Headers that will be sent (names in any case).
14
+ * @param {string | Uint8Array} [request.body] - Body that will be sent (default empty).
15
+ * @param {string} request.region - AWS region of the credential scope.
16
+ * @param {string} request.service - Service of the credential scope (`bedrock` for Amazon Bedrock).
17
+ * @param {string} request.accessKeyId - Access key id.
18
+ * @param {string} request.secretAccessKey - Secret access key.
19
+ * @param {string} [request.sessionToken] - Session token of temporary credentials.
20
+ * @param {Date} [request.date] - Signing time (default now).
21
+ * @returns {{headers: Record<string,string>, authorization: string, signature: string, signedHeaders: string, canonicalRequest: string, stringToSign: string}} The headers to send (the ones given plus `x-amz-date`, `x-amz-security-token` and `authorization`; `host` is left to the transport) and the intermediate values, which tests compare with published ones.
22
+ */
23
+ export function sign({ method, url, headers, body, region, service, accessKeyId, secretAccessKey, sessionToken, date }: {
24
+ method?: string;
25
+ url: string;
26
+ headers?: Record<string, string>;
27
+ body?: string | Uint8Array;
28
+ region: string;
29
+ service: string;
30
+ accessKeyId: string;
31
+ secretAccessKey: string;
32
+ sessionToken?: string;
33
+ date?: Date;
34
+ }): {
35
+ headers: Record<string, string>;
36
+ authorization: string;
37
+ signature: string;
38
+ signedHeaders: string;
39
+ canonicalRequest: string;
40
+ stringToSign: string;
41
+ };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * The API host of a region: `<region>-aiplatform.googleapis.com`, `aiplatform.googleapis.com` for `global`, and `aiplatform.<us|eu>.rep.googleapis.com`
3
+ * for the multi-region endpoints.
4
+ * @param {string} region - Region such as `us-east5`, `europe-west1`, `global`, `us` or `eu`.
5
+ * @returns {string} Host name.
6
+ */
7
+ export function host(region: string): string;
8
+ /**
9
+ * The region a Vertex AI host serves.
10
+ * @param {string} url - Upstream base URL.
11
+ * @returns {string | undefined} The region, `"global"` for the global host, `"us"` or `"eu"` for a multi-region host, undefined for any other host.
12
+ */
13
+ export function regionOf(url: string): string | undefined;
14
+ /**
15
+ * The region of an upstream: the `region` setting, else the region in a Vertex AI base URL, else `CLOUD_ML_REGION`.
16
+ * @param {any} endpoint - Runtime endpoint (`url`, `vertex`).
17
+ * @param {Record<string, string | undefined>} [env] - Environment.
18
+ * @returns {string | undefined} Region, or undefined when none is set.
19
+ */
20
+ export function regionFor(endpoint: any, env?: Record<string, string | undefined>): string | undefined;
21
+ /**
22
+ * The Google Cloud project and region of an upstream. The project is the `project` setting, else `ANTHROPIC_VERTEX_PROJECT_ID`,
23
+ * `GOOGLE_CLOUD_PROJECT` or `GCLOUD_PROJECT`, else the `project_id` (or `quota_project_id`) of the credentials file.
24
+ * @param {any} endpoint - Runtime endpoint.
25
+ * @param {Record<string, string | undefined>} [env] - Environment.
26
+ * @returns {{project: string, region: string}} Where requests go.
27
+ * @throws {TranslatorError} With code `INVALID_CONFIG` when either is unknown.
28
+ */
29
+ export function locate(endpoint: any, env?: Record<string, string | undefined>): {
30
+ project: string;
31
+ region: string;
32
+ };
33
+ /**
34
+ * The path of a Vertex AI call for Claude.
35
+ * @param {"messages" | "count"} kind - `"messages"` for a request (`rawPredict`, or `streamRawPredict` when streaming), `"count"` for token counting.
36
+ * @param {any} endpoint - Runtime endpoint.
37
+ * @param {string} [model] - Model for a `"messages"` call, for example `claude-sonnet-4-5@20250929`.
38
+ * @param {{stream?: boolean, env?: Record<string, string | undefined>}} [options] - Whether the call streams, and the environment.
39
+ * @returns {string} Path starting with `/v1/projects/`.
40
+ */
41
+ export function route(kind: "messages" | "count", endpoint: any, model?: string, { stream, env }?: {
42
+ stream?: boolean;
43
+ env?: Record<string, string | undefined>;
44
+ }): string;
45
+ /**
46
+ * The body Vertex AI takes: the Messages body with `anthropic_version` added and the model taken out (it is in the path). The model stays on the
47
+ * result as a property that is not enumerable, so it is not sent but the rest of the translator can still read `body.model`.
48
+ * @param {any} body - Anthropic Messages request body.
49
+ * @returns {any} Vertex AI body.
50
+ */
51
+ export function body(body: any): any;
52
+ /** The `anthropic_version` Vertex AI requires in the body. */
53
+ export const VERSION: "vertex-2023-10-16";
54
+ /** The hosts that are not of the form `<region>-aiplatform.googleapis.com`: the global one and the multi-region ones. */
55
+ export const HOSTS: string[];