@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.
- package/README.md +5 -5
- package/dist/api/convert/anthropic/messages/to/bedrock/converse/request.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/anthropic/messages/response.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/anthropic/messages/stream.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/openai/chat/response.mjs +2 -0
- package/dist/api/convert/bedrock/converse/to/openai/chat/stream.mjs +2 -0
- package/dist/api/convert/openai/chat/to/bedrock/converse/request.mjs +2 -0
- package/dist/api/driver/bedrock/detect.mjs +2 -0
- package/dist/api/driver/bedrock/info.mjs +2 -0
- package/dist/api/driver/bedrock/model/list.mjs +2 -0
- package/dist/api/driver/bedrock/path.mjs +2 -0
- package/dist/api/driver/bedrock/request.mjs +2 -0
- package/dist/api/driver/bedrock/sign.mjs +2 -0
- package/dist/api/driver/vertex/auth.mjs +2 -0
- package/dist/api/driver/vertex/detect.mjs +2 -0
- package/dist/api/driver/vertex/error.mjs +2 -0
- package/dist/api/driver/vertex/info.mjs +2 -0
- package/dist/api/driver/vertex/path.mjs +2 -0
- package/dist/api/driver/vertex/request.mjs +2 -0
- package/dist/api/driver/vertex/settings.mjs +2 -0
- package/dist/api/driver/vertex/token/count.mjs +2 -0
- package/dist/api/request.mjs +2 -2
- package/dist/api/send.mjs +1 -1
- package/dist/api/stream.mjs +1 -1
- package/dist/lib/bedrock.mjs +2 -0
- package/dist/lib/capture.mjs +1 -1
- package/dist/lib/config.mjs +1 -1
- package/dist/lib/converse.mjs +3 -0
- package/dist/lib/document-urls.mjs +2 -0
- package/dist/lib/documents.mjs +4 -4
- package/dist/lib/eventstream.mjs +2 -0
- package/dist/lib/fetch-url.mjs +2 -0
- package/dist/lib/google-auth.mjs +2 -0
- package/dist/lib/http.mjs +1 -1
- package/dist/lib/protocol.mjs +1 -1
- package/dist/lib/route.mjs +1 -1
- package/dist/lib/settings.mjs +1 -1
- package/dist/lib/sigv4.mjs +7 -0
- package/dist/lib/vertex.mjs +2 -0
- package/package.json +1 -1
- package/types/src/api/convert/anthropic/messages/to/bedrock/converse/request.d.mts +14 -0
- package/types/src/api/convert/bedrock/converse/to/anthropic/messages/response.d.mts +10 -0
- package/types/src/api/convert/bedrock/converse/to/anthropic/messages/stream.d.mts +14 -0
- package/types/src/api/convert/bedrock/converse/to/openai/chat/response.d.mts +10 -0
- package/types/src/api/convert/bedrock/converse/to/openai/chat/stream.d.mts +13 -0
- package/types/src/api/convert/openai/chat/to/bedrock/converse/request.d.mts +11 -0
- package/types/src/api/driver/bedrock/detect.d.mts +9 -0
- package/types/src/api/driver/bedrock/info.d.mts +36 -0
- package/types/src/api/driver/bedrock/model/list.d.mts +11 -0
- package/types/src/api/driver/bedrock/path.d.mts +9 -0
- package/types/src/api/driver/bedrock/request.d.mts +9 -0
- package/types/src/api/driver/bedrock/sign.d.mts +9 -0
- package/types/src/api/driver/vertex/auth.d.mts +7 -0
- package/types/src/api/driver/vertex/detect.d.mts +8 -0
- package/types/src/api/driver/vertex/error.d.mts +15 -0
- package/types/src/api/driver/vertex/info.d.mts +16 -0
- package/types/src/api/driver/vertex/path.d.mts +7 -0
- package/types/src/api/driver/vertex/request.d.mts +6 -0
- package/types/src/api/driver/vertex/settings.d.mts +23 -0
- package/types/src/api/driver/vertex/token/count.d.mts +7 -0
- package/types/src/api/request.d.mts +1 -1
- package/types/src/lib/bedrock.d.mts +74 -0
- package/types/src/lib/config.d.mts +32 -3
- package/types/src/lib/converse.d.mts +81 -0
- package/types/src/lib/document-urls.d.mts +55 -0
- package/types/src/lib/eventstream.d.mts +34 -0
- package/types/src/lib/fetch-url.d.mts +64 -0
- package/types/src/lib/google-auth.d.mts +58 -0
- package/types/src/lib/http.d.mts +3 -2
- package/types/src/lib/protocol.d.mts +3 -0
- package/types/src/lib/settings.d.mts +12 -0
- package/types/src/lib/sigv4.d.mts +41 -0
- package/types/src/lib/vertex.d.mts +55 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Converse reply to an Anthropic message: the reply is read as a Chat Completions `chat.completion` (reasoning, tool calls with the
|
|
3
|
+
* model's ids, `stopReason` as `finish_reason`, `usage` with the cache counts in the prompt) and the Chat Completions conversion builds the message.
|
|
4
|
+
* @param {any} reply - Parsed Converse reply.
|
|
5
|
+
* @param {any} [state] - State from the request converter.
|
|
6
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
7
|
+
* @returns {any} Anthropic message.
|
|
8
|
+
* @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} When the reply has no message or ends in a failure.
|
|
9
|
+
*/
|
|
10
|
+
export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert ConverseStream events (`{ event, data }` messages, as `lib/eventstream` makes them from the binary frames, or Server-Sent Events text
|
|
3
|
+
* of the same events) to Anthropic events. The events become Chat Completions chunks (one id for the reply, tool calls streamed in pieces, the
|
|
4
|
+
* stop reason as the finish chunk, the `metadata` usage as the usage chunk) and the Chat Completions conversion writes the events. A failure
|
|
5
|
+
* message, an event that cannot be read and a stream that ends before `messageStop` each end the stream with an `error` event.
|
|
6
|
+
* @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - The events.
|
|
7
|
+
* @param {any} [state] - State from the request converter.
|
|
8
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
9
|
+
* @returns {AsyncGenerator<{ event: string, data: any }, void, undefined>} Anthropic events.
|
|
10
|
+
*/
|
|
11
|
+
export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
|
|
12
|
+
event: string;
|
|
13
|
+
data: any;
|
|
14
|
+
}, void, undefined>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Converse reply to a Chat Completions `chat.completion` (see `completion`). The model name is the one the client asked for, and the
|
|
3
|
+
* counts are kept on the state for the usage report.
|
|
4
|
+
* @param {any} reply - Parsed Converse reply.
|
|
5
|
+
* @param {any} [state] - State from the request converter.
|
|
6
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
7
|
+
* @returns {any} `chat.completion` object.
|
|
8
|
+
* @throws {import("../../../../../../../lib/errors.mjs").TranslatorError} When the reply has no message.
|
|
9
|
+
*/
|
|
10
|
+
export function response(reply: any, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): any;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert ConverseStream events (`{ event, data }` messages, as `lib/eventstream` makes them from the binary frames, or Server-Sent Events text
|
|
3
|
+
* of the same events) to Chat Completions chunks followed by `[DONE]`. Every chunk carries one completion id, tool calls stream their arguments
|
|
4
|
+
* in pieces, and the `metadata` event gives the usage chunk unless `state.includeUsage` is false. A failure message, an event that cannot be read
|
|
5
|
+
* and a stream that ends before `messageStop` each become an `{ error }` chunk followed by `[DONE]`.
|
|
6
|
+
* @param {ReadableStream|AsyncIterable<unknown>|Iterable<unknown>|string|Uint8Array} source - The events.
|
|
7
|
+
* @param {any} [state] - State from the request converter (model, `includeUsage`).
|
|
8
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
9
|
+
* @returns {AsyncGenerator<{ data: any|string }, void, undefined>} Chat Completions chunks.
|
|
10
|
+
*/
|
|
11
|
+
export function stream(source: ReadableStream | AsyncIterable<unknown> | Iterable<unknown> | string | Uint8Array, state?: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): AsyncGenerator<{
|
|
12
|
+
data: any | string;
|
|
13
|
+
}, void, undefined>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Chat Completions request to a Converse body (see `chatBody`). The upstream's `sampling` setting becomes `additionalModelRequestFields`.
|
|
3
|
+
* @param {any} body - Chat Completions request body.
|
|
4
|
+
* @param {Partial<import("../../../../../../../lib/settings.mjs").Settings>} [settings] - Translator settings.
|
|
5
|
+
* @returns {{body: any, state: any}} The Converse body and the conversion state the response converters read.
|
|
6
|
+
* @throws {TranslatorError} `invalid_request` when `messages` is missing.
|
|
7
|
+
*/
|
|
8
|
+
export function request(body: any, settings?: Partial<import("../../../../../../../lib/settings.mjs").Settings>): {
|
|
9
|
+
body: any;
|
|
10
|
+
state: any;
|
|
11
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Match by host name, no network access: `bedrock-runtime.<region>.amazonaws.com`, its FIPS and VPC endpoint forms, and the China partition.
|
|
3
|
+
* @param {any} context - `{ endpoint }`.
|
|
4
|
+
* @returns {{host: string, region: string} | null} Facts, or null when the host is not a Bedrock runtime host.
|
|
5
|
+
*/
|
|
6
|
+
export function detect({ endpoint }: any): {
|
|
7
|
+
host: string;
|
|
8
|
+
region: string;
|
|
9
|
+
} | null;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/bedrock/info.mjs
|
|
5
|
+
* @Date: 2026-11-19T09:00:00-08:00 (1795107600)
|
|
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-11-19T09:00:00-08:00 (1795107600)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Amazon Bedrock driver: static facts about the Bedrock runtime.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/bedrock
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Describe the Bedrock driver. Claude models go through InvokeModel with an Anthropic Messages body (`anthropic.messages`); every other model
|
|
21
|
+
* goes through Converse (`bedrock.converse`). Under `api: "auto"` the model id chooses (see `lib/capture`). Replies stream as
|
|
22
|
+
* `application/vnd.amazon.eventstream` frames (`frames`). The real paths come from the `path` leaf, because they carry the model id, and the
|
|
23
|
+
* `sign` leaf authenticates every call (a Bedrock API key as a bearer token, or SigV4).
|
|
24
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, frames: string, hostSuffixes: string[], paths: Record<string,string>}} Driver facts.
|
|
25
|
+
*/
|
|
26
|
+
export function info(): {
|
|
27
|
+
name: string;
|
|
28
|
+
protocol: string;
|
|
29
|
+
protocols: string[];
|
|
30
|
+
order: number;
|
|
31
|
+
auth: string;
|
|
32
|
+
strict: boolean;
|
|
33
|
+
frames: string;
|
|
34
|
+
hostSuffixes: string[];
|
|
35
|
+
paths: Record<string, string>;
|
|
36
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List the foundation models Bedrock reports for the region (`GET /foundation-models` on the control plane, signed like every call). Models that
|
|
3
|
+
* do not produce text are left out. Inference profiles (`us.anthropic.claude-...`, which many models need instead of the bare id) are not
|
|
4
|
+
* listed; name them in the upstream's `models`.
|
|
5
|
+
* @param {any} context - `{ endpoint, transport, signal, timeout }`.
|
|
6
|
+
* @returns {Promise<{id: string, owned_by: string}[] | null>} Models, or null when unreachable, not authenticated or not a model list.
|
|
7
|
+
*/
|
|
8
|
+
export function list({ endpoint, transport, signal, timeout }: any): Promise<{
|
|
9
|
+
id: string;
|
|
10
|
+
owned_by: string;
|
|
11
|
+
}[] | null>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the path of a call: the model id (percent-encoded as one segment) and the operation. InvokeModel is `/invoke`, or
|
|
3
|
+
* `/invoke-with-response-stream` for a stream; Converse is `/converse`, or `/converse-stream`.
|
|
4
|
+
* @param {"messages" | "converse"} kind - Which call: `messages` is InvokeModel with an Anthropic body.
|
|
5
|
+
* @param {any} context - `{ endpoint, model, stream }`.
|
|
6
|
+
* @returns {string} Path starting with `/model/`.
|
|
7
|
+
* @throws {TranslatorError} `unsupported_endpoint` (501) for any other call (embeddings, chat).
|
|
8
|
+
*/
|
|
9
|
+
export function path(kind: "messages" | "converse", { model, stream }: any): string;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape the body for the runtime. The model id and the choice of streaming are in the URL, so neither is in the body. An InvokeModel body
|
|
3
|
+
* also gets `anthropic_version: "bedrock-2023-05-31"` and loses the Anthropic API fields Bedrock does not have (`metadata`, `service_tier`,
|
|
4
|
+
* `container`, `mcp_servers`); a Converse body needs nothing more.
|
|
5
|
+
* @param {any} body - Anthropic Messages or Converse request body.
|
|
6
|
+
* @param {any} [context] - `{ protocol }`, the protocol the request goes over.
|
|
7
|
+
* @returns {any} Normalized body.
|
|
8
|
+
*/
|
|
9
|
+
export function request(body: any, context?: any): any;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authenticate a call: a Bedrock API key (the upstream's `apiKey` or `AWS_BEARER_TOKEN_BEDROCK`) is sent as a bearer token, and otherwise the
|
|
3
|
+
* call is signed with SigV4 from the upstream's access keys or the `AWS_*` environment variables (see `lib/bedrock`). The signature covers the
|
|
4
|
+
* exact JSON text of the body, which is what the transport sends.
|
|
5
|
+
* @param {any} context - `{ endpoint, method, url, headers, body, stream }`.
|
|
6
|
+
* @returns {Record<string, string>} The headers to send.
|
|
7
|
+
* @throws {import("../../../lib/errors.mjs").TranslatorError} `missing_credentials` or `missing_region` (500).
|
|
8
|
+
*/
|
|
9
|
+
export function sign({ endpoint, method, url, headers, body, stream }: any): Record<string, string>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add the Google access token as the bearer token and drop the headers Vertex AI does not take (`x-api-key`, `anthropic-version`; a client's
|
|
3
|
+
* `anthropic-beta` is kept). The token comes from `lib/google-auth.mjs`.
|
|
4
|
+
* @param {any} context - `{ endpoint, headers, transport, timeout }`.
|
|
5
|
+
* @returns {Promise<Record<string,string>>} Headers to send.
|
|
6
|
+
*/
|
|
7
|
+
export function auth({ endpoint, headers, transport, timeout }: any): Promise<Record<string, string>>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize Google's error envelope (`{ error: { code, message, status } }`, sometimes inside a one-element array) and Claude's own
|
|
3
|
+
* (`{ type: "error", error: { type, message } }`, which Vertex AI passes through) to the nested shape. Google's `status` (`NOT_FOUND`) becomes the code.
|
|
4
|
+
* @param {number} status - HTTP status.
|
|
5
|
+
* @param {*} body - Parsed upstream error body.
|
|
6
|
+
* @returns {{error: {message: string, type: string | undefined, param: *, code: *}}} Nested error body.
|
|
7
|
+
*/
|
|
8
|
+
export function error(status: number, body: any): {
|
|
9
|
+
error: {
|
|
10
|
+
message: string;
|
|
11
|
+
type: string | undefined;
|
|
12
|
+
param: any;
|
|
13
|
+
code: any;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describe the Vertex AI driver. The paths come from the `path` leaf, because they hold the project, the region and the model; the key (a Google
|
|
3
|
+
* access token) is added as a bearer token by the `auth` leaf. Vertex AI takes no `anthropic-version` header (the version is in the body).
|
|
4
|
+
* @returns {{name: string, protocol: string, protocols: string[], order: number, auth: string, strict: boolean, hosts: string[], hostSuffixes: string[], paths: Record<string,string>}} Driver facts.
|
|
5
|
+
*/
|
|
6
|
+
export function info(): {
|
|
7
|
+
name: string;
|
|
8
|
+
protocol: string;
|
|
9
|
+
protocols: string[];
|
|
10
|
+
order: number;
|
|
11
|
+
auth: string;
|
|
12
|
+
strict: boolean;
|
|
13
|
+
hosts: string[];
|
|
14
|
+
hostSuffixes: string[];
|
|
15
|
+
paths: Record<string, string>;
|
|
16
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the `rawPredict` path (`streamRawPredict` when `stream` is true) from the upstream's project and region and the request's model.
|
|
3
|
+
* @param {"messages" | "count"} kind - Which call.
|
|
4
|
+
* @param {any} context - `{ endpoint, model, stream }`.
|
|
5
|
+
* @returns {string} Path starting with `/v1/projects/`.
|
|
6
|
+
*/
|
|
7
|
+
export function path(kind: "messages" | "count", { endpoint, model, stream }: any): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/llm-translator
|
|
4
|
+
* @Filename: /src/api/driver/vertex/settings.mjs
|
|
5
|
+
* @Date: 2026-11-19T09:00:00-08:00 (1795107600)
|
|
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-11-19T09:00:00-08:00 (1795107600)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview Vertex AI driver: translator settings.
|
|
17
|
+
* @module @cldmv/llm-translator/driver/vertex
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Claude on Vertex AI speaks the Messages API, so the converters use the library defaults.
|
|
21
|
+
* @returns {any} Partial settings (empty).
|
|
22
|
+
*/
|
|
23
|
+
export function settings(): any;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Count tokens with `POST .../publishers/anthropic/models/count-tokens:rawPredict`, which takes the model in the body. Any failure (a missing
|
|
3
|
+
* project or credentials, a 404 from a region or project without the method, a network error) gives null, so the caller uses the estimate.
|
|
4
|
+
* @param {any} context - `{ endpoint, request, transport, signal, timeout }`.
|
|
5
|
+
* @returns {Promise<number | null>} Token count, or null when Vertex AI did not answer.
|
|
6
|
+
*/
|
|
7
|
+
export function count({ endpoint, request, transport, signal, timeout }: any): Promise<number | null>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Route a client request body (in the configured `front` protocol) to an upstream and convert it to that upstream's
|
|
3
3
|
* protocol. Does no I/O.
|
|
4
4
|
* @param {any} body - Client request body (Anthropic Messages or OpenAI Chat Completions, per `front`).
|
|
5
|
-
* @param {any} [options] - `{ front, headers, clientHeaders, filter, images, usage }`. `front` overrides the configured front for this call, `tokens: true` (used by `token.count`) never picks a native Messages route on a server whose own protocol is another, because the tokenizer reads Chat Completions messages, `wire: "responses"` says the body is the Chat Completions form of a Responses API request (so the protocol is chosen for a Responses client), `headers` are extra upstream headers, `clientHeaders` are client headers forwarded only to a native Anthropic upstream, `filter` restricts a forwarded OpenAI body to known fields (default: the `passthroughFilter` setting, off), `images: false` sends the request without images (used by the image fallback), `endpoint` sends to that member of the model's pool (used for failover; default: the first in the pool's order), `hold: true` (used by `send`) advances a round robin and takes the chosen endpoint's in-flight slot, which the caller then releases, `jsonObject: "instruction"` sends a `json_object` response format as a system instruction (used by the JSON fallback, and applied by itself once a refusal is remembered for the endpoint and model), `tools: false` sends the request without tools (used by the tool fallback), `usage: true` (used by `send`) makes a streaming OpenAI-front request always ask the upstream for its final usage chunk, `cacheSalt` (a trusted per-call vLLM `cache_salt`, which replaces the upstream's `cacheSalt` setting), `retry: true` (used by `send` for the same request sent again) keeps the `dropped` event from repeating, `log` is the logger events of this request also go to (used by the server), `budget` (from `createBudget`, one per `send` for all its attempts) limits the cost of decoding reasoning signatures.
|
|
5
|
+
* @param {any} [options] - `{ front, headers, clientHeaders, filter, images, usage, signal }`. `signal` aborts the fetching of `url` documents (the `documentUrls` setting). `front` overrides the configured front for this call, `tokens: true` (used by `token.count`) never picks a native Messages route on a server whose own protocol is another, because the tokenizer reads Chat Completions messages, and sends no credentials (a driver's `auth` leaf is skipped; the count call builds its own headers), `wire: "responses"` says the body is the Chat Completions form of a Responses API request (so the protocol is chosen for a Responses client), `headers` are extra upstream headers, `clientHeaders` are client headers forwarded only to a native Anthropic upstream, `filter` restricts a forwarded OpenAI body to known fields (default: the `passthroughFilter` setting, off), `images: false` sends the request without images (used by the image fallback), `endpoint` sends to that member of the model's pool (used for failover; default: the first in the pool's order), `hold: true` (used by `send`) advances a round robin and takes the chosen endpoint's in-flight slot, which the caller then releases, `jsonObject: "instruction"` sends a `json_object` response format as a system instruction (used by the JSON fallback, and applied by itself once a refusal is remembered for the endpoint and model), `tools: false` sends the request without tools (used by the tool fallback), `usage: true` (used by `send`) makes a streaming OpenAI-front request always ask the upstream for its final usage chunk, `cacheSalt` (a trusted per-call vLLM `cache_salt`, which replaces the upstream's `cacheSalt` setting), `retry: true` (used by `send` for the same request sent again) keeps the `dropped` event from repeating, `log` is the logger events of this request also go to (used by the server), `budget` (from `createBudget`, one per `send` for all its attempts) limits the cost of decoding reasoning signatures.
|
|
6
6
|
* @returns {Promise<{upstream: string, method: string, url: string, path: string, headers: Record<string,string>, body: any, state: any, pool: string[], more: boolean, images: {sent: boolean, retry: boolean, key: string}, tools: {sent: boolean, retry: boolean}, json: {sent: boolean, retry: boolean, key: string}}>} The upstream request and the state `response`/`stream`/`error` need. `pool` lists the names of the endpoints serving the model in the order they are tried (`upstream` is the first); `more` is true when the upstream is `api: "auto"` and has another protocol to fall back to; `images` tells `send` whether the body carries images, whether a refusal may be retried without them and the key under which a refusal is remembered; `tools` tells it whether the body carries tools and whether a tool-parser refusal may be retried without them; `json` tells it whether the body carries the schema emulation of `json_object`, whether a refusal may be retried with the instruction and the key under which a refusal is remembered.
|
|
7
7
|
*/
|
|
8
8
|
export function request(body: any, options?: any): Promise<{
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Does a model id name a Claude model, which InvokeModel serves with the Anthropic Messages body? An application inference profile is an
|
|
3
|
+
* opaque id and so counts as not Claude (Converse serves Claude too); set the upstream's `api` for it.
|
|
4
|
+
* @param {string | undefined} model - Model id, inference profile id or ARN.
|
|
5
|
+
* @returns {boolean} True for a Claude id.
|
|
6
|
+
*/
|
|
7
|
+
export function claude(model: string | undefined): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The region a runtime host name belongs to.
|
|
10
|
+
* @param {string} host - Host name.
|
|
11
|
+
* @returns {string | null} The region, or null when the host is not a Bedrock runtime host.
|
|
12
|
+
*/
|
|
13
|
+
export function regionOfHost(host: string): string | null;
|
|
14
|
+
/**
|
|
15
|
+
* The region of an upstream: its `region`, else the one in its URL's host name, else `AWS_REGION`, else `AWS_DEFAULT_REGION`.
|
|
16
|
+
* @param {{region?: string, url?: string}} endpoint - Runtime endpoint (or raw upstream config).
|
|
17
|
+
* @param {Record<string, string | undefined>} [env] - Environment (default `process.env`).
|
|
18
|
+
* @returns {string | null} The region, or null when none is set.
|
|
19
|
+
*/
|
|
20
|
+
export function regionOf(endpoint: {
|
|
21
|
+
region?: string;
|
|
22
|
+
url?: string;
|
|
23
|
+
}, env?: Record<string, string | undefined>): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* The default base URL of a region: `https://bedrock-runtime.<region>.amazonaws.com` (`.amazonaws.com.cn` in the China regions).
|
|
26
|
+
* @param {{region?: string}} raw - Raw upstream config without a `url`.
|
|
27
|
+
* @param {Record<string, string | undefined>} [env] - Environment (default `process.env`).
|
|
28
|
+
* @returns {string} Base URL.
|
|
29
|
+
* @throws {TranslatorError} `INVALID_CONFIG` when no region is configured.
|
|
30
|
+
*/
|
|
31
|
+
export function defaultUrl(raw: {
|
|
32
|
+
region?: string;
|
|
33
|
+
}, env?: Record<string, string | undefined>): string;
|
|
34
|
+
/**
|
|
35
|
+
* The URL path segment of a model: the id percent-encoded as one segment, because ids and ARNs contain `:` and `/`.
|
|
36
|
+
* @param {string} model - Model id, inference profile id or ARN.
|
|
37
|
+
* @returns {string} Path starting with `/model/`.
|
|
38
|
+
*/
|
|
39
|
+
export function modelPath(model: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* How an upstream authenticates, most specific first: the upstream's `apiKey` (a Bedrock API key, sent as a bearer token), its access keys
|
|
42
|
+
* (SigV4), then the environment's `AWS_BEARER_TOKEN_BEDROCK` and `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` / `AWS_SESSION_TOKEN`.
|
|
43
|
+
* @param {any} endpoint - Runtime endpoint (`apiKey`, `accessKeyId`, `secretAccessKey`, `sessionToken`).
|
|
44
|
+
* @param {Record<string, string | undefined>} [env] - Environment (default `process.env`).
|
|
45
|
+
* @returns {{bearer: string} | {keys: {accessKeyId: string, secretAccessKey: string, sessionToken?: string}} | null} The credential, or null when there is none.
|
|
46
|
+
*/
|
|
47
|
+
export function credential(endpoint: any, env?: Record<string, string | undefined>): {
|
|
48
|
+
bearer: string;
|
|
49
|
+
} | {
|
|
50
|
+
keys: {
|
|
51
|
+
accessKeyId: string;
|
|
52
|
+
secretAccessKey: string;
|
|
53
|
+
sessionToken?: string;
|
|
54
|
+
};
|
|
55
|
+
} | null;
|
|
56
|
+
/**
|
|
57
|
+
* The headers of an authenticated call: the given ones with `authorization` set. A Bedrock API key gives `Bearer <key>`. Otherwise the call is
|
|
58
|
+
* signed with SigV4 for the service `bedrock` in the upstream's region, which adds `x-amz-date`, `x-amz-security-token` (temporary
|
|
59
|
+
* credentials) and `authorization`; the body must be the exact text that is sent.
|
|
60
|
+
* @param {any} endpoint - Runtime endpoint.
|
|
61
|
+
* @param {{method: string, url: string, headers: Record<string, string>, body: string}} request - The call.
|
|
62
|
+
* @param {{env?: Record<string, string | undefined>, date?: Date}} [options] - `env` is the environment (default `process.env`), `date` the signing time (default now).
|
|
63
|
+
* @returns {Record<string, string>} Headers to send.
|
|
64
|
+
* @throws {TranslatorError} `missing_credentials` (500) when the upstream has no credential, `missing_region` (500) when SigV4 needs a region and there is none.
|
|
65
|
+
*/
|
|
66
|
+
export function authenticate(endpoint: any, request: {
|
|
67
|
+
method: string;
|
|
68
|
+
url: string;
|
|
69
|
+
headers: Record<string, string>;
|
|
70
|
+
body: string;
|
|
71
|
+
}, { env, date }?: {
|
|
72
|
+
env?: Record<string, string | undefined>;
|
|
73
|
+
date?: Date;
|
|
74
|
+
}): Record<string, string>;
|
|
@@ -18,9 +18,9 @@ export type UpstreamConfig = {
|
|
|
18
18
|
*/
|
|
19
19
|
name?: string;
|
|
20
20
|
/**
|
|
21
|
-
* Base URL of the upstream server (`http://host:8000`).
|
|
21
|
+
* Base URL of the upstream server (`http://host:8000`). Required, except for `backend: "bedrock"`, whose default is `https://bedrock-runtime.<region>.amazonaws.com`.
|
|
22
22
|
*/
|
|
23
|
-
url
|
|
23
|
+
url?: string;
|
|
24
24
|
/**
|
|
25
25
|
* Key sent to the upstream.
|
|
26
26
|
*/
|
|
@@ -38,7 +38,36 @@ export type UpstreamConfig = {
|
|
|
38
38
|
*/
|
|
39
39
|
deployment?: string;
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
41
|
+
* Google Vertex AI settings (see docs/drivers.md). `url` may be left out when `backend` is `"vertex"`: it defaults to the region's host.
|
|
42
|
+
*/
|
|
43
|
+
vertex?: {
|
|
44
|
+
project?: string;
|
|
45
|
+
region?: string;
|
|
46
|
+
credentials?: string | object;
|
|
47
|
+
accessToken?: string;
|
|
48
|
+
getToken?: (options: {
|
|
49
|
+
signal?: AbortSignal;
|
|
50
|
+
}) => string | Promise<string>;
|
|
51
|
+
metadataServer?: boolean | string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Amazon Bedrock: the AWS region SigV4 signs for. Defaults to the region in the URL's host name, then `AWS_REGION`, then `AWS_DEFAULT_REGION`.
|
|
55
|
+
*/
|
|
56
|
+
region?: string;
|
|
57
|
+
/**
|
|
58
|
+
* Amazon Bedrock: access key id for SigV4. Defaults to `AWS_ACCESS_KEY_ID`.
|
|
59
|
+
*/
|
|
60
|
+
accessKeyId?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Amazon Bedrock: secret access key for SigV4. Defaults to `AWS_SECRET_ACCESS_KEY`.
|
|
63
|
+
*/
|
|
64
|
+
secretAccessKey?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Amazon Bedrock: session token of temporary credentials. Defaults to `AWS_SESSION_TOKEN`.
|
|
67
|
+
*/
|
|
68
|
+
sessionToken?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Which protocol of the server to speak, `"auto"` by default. Canonical ids are `"anthropic.messages"`, `"openai.chat"`, `"openai.responses"`, `"ollama.chat"` and `"bedrock.converse"`; `"messages"`, `"anthropic"` and `"claude"` mean the first, `"chat"`, `"openai"` and `"chatgpt"` the second, `"responses"` and `"codex"` the third, `"native"` and `"ollama"` the fourth and `"converse"` the last. `"auto"` goes over the client's own protocol when the driver's server speaks it (Chat Completions otherwise) and falls back to the next one when the server answers 404, 405 or 501; an explicit value is always that protocol and never falls back. A protocol the driver does not list fails detection with `unsupported_api`.
|
|
42
71
|
*/
|
|
43
72
|
api?: string;
|
|
44
73
|
/**
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a Chat Completions request body to a Converse body (`messages`, `system`, `inferenceConfig`, `toolConfig` and
|
|
3
|
+
* `additionalModelRequestFields`; the model id and the choice of streaming belong to the URL, so neither is in the body). `max_tokens`,
|
|
4
|
+
* `temperature`, `top_p` and `stop` become `inferenceConfig`, tools become `toolConfig` (`tool_choice` `auto`, `required` and one named function
|
|
5
|
+
* map to `auto`, `any` and `tool`), images are sent as bytes, and `top_k`, the extension sampling fields and a thinking budget go in
|
|
6
|
+
* `additionalModelRequestFields`, which carries a model's own parameters. What Converse has no field for (`response_format`, `n`,
|
|
7
|
+
* `parallel_tool_calls`, `seed`, penalties, images given by URL, reasoning in history, ...) is left out and named in `dropped`.
|
|
8
|
+
* Converse needs a `toolConfig` whenever the history has tool blocks and has no "none" choice, so: a request without tools has its past tool calls
|
|
9
|
+
* and results written as text, and `tool_choice: "none"` with tools sends the `toolConfig` and adds a system instruction not to call tools.
|
|
10
|
+
* @param {any} body - Chat Completions request body.
|
|
11
|
+
* @param {{ dropped?: string[], sampling?: Record<string, any>, thinkingBudget?: number, marker?: string }} [context] - `dropped` collects the names of what was left out; `sampling` are extension fields for `additionalModelRequestFields`; `thinkingBudget` is the token budget of enabled thinking, sent as `thinking: { type: "enabled", budget_tokens }`; `marker` is the text that marks a failed tool result.
|
|
12
|
+
* @returns {any} Converse request body.
|
|
13
|
+
*/
|
|
14
|
+
export function chatBody(body: any, context?: {
|
|
15
|
+
dropped?: string[];
|
|
16
|
+
sampling?: Record<string, any>;
|
|
17
|
+
thinkingBudget?: number;
|
|
18
|
+
marker?: string;
|
|
19
|
+
}): any;
|
|
20
|
+
/**
|
|
21
|
+
* Chat Completions `finish_reason` for a Converse `stopReason`: `end_turn` and `stop_sequence` are `stop`, `max_tokens` and
|
|
22
|
+
* `model_context_window_exceeded` are `length`, `tool_use` is `tool_calls`, `content_filtered` and `guardrail_intervened` are `content_filter`,
|
|
23
|
+
* and `malformed_model_output` and `malformed_tool_use` are `error` (a failure, not an answer). A reply that called tools is `tool_calls`.
|
|
24
|
+
* @param {string | undefined} reason - Converse `stopReason`.
|
|
25
|
+
* @param {boolean} calls - Whether the reply carried tool calls.
|
|
26
|
+
* @returns {string} `finish_reason`.
|
|
27
|
+
*/
|
|
28
|
+
export function finishOf(reason: string | undefined, calls: boolean): string;
|
|
29
|
+
/**
|
|
30
|
+
* Chat Completions usage from a Converse `usage` object. Converse counts cache reads and writes apart from `inputTokens`; the prompt is all three.
|
|
31
|
+
* @param {any} usage - Converse `usage`.
|
|
32
|
+
* @returns {{prompt_tokens: number, completion_tokens: number, total_tokens: number, prompt_tokens_details?: {cached_tokens: number}} | undefined} Usage, or undefined when there are no counts.
|
|
33
|
+
*/
|
|
34
|
+
export function usageOf(usage: any): {
|
|
35
|
+
prompt_tokens: number;
|
|
36
|
+
completion_tokens: number;
|
|
37
|
+
total_tokens: number;
|
|
38
|
+
prompt_tokens_details?: {
|
|
39
|
+
cached_tokens: number;
|
|
40
|
+
};
|
|
41
|
+
} | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* The same counts as Anthropic usage, which is what the OpenAI front's usage report reads from a converted reply (Converse reports them the
|
|
44
|
+
* way Anthropic does: `inputTokens` leaves out the cache).
|
|
45
|
+
* @param {any} usage - Converse `usage`.
|
|
46
|
+
* @returns {{input_tokens: number, output_tokens: number, cache_read_input_tokens: number, cache_creation_input_tokens: number} | undefined} Anthropic usage, or undefined when there are no counts.
|
|
47
|
+
*/
|
|
48
|
+
export function folded(usage: any): {
|
|
49
|
+
input_tokens: number;
|
|
50
|
+
output_tokens: number;
|
|
51
|
+
cache_read_input_tokens: number;
|
|
52
|
+
cache_creation_input_tokens: number;
|
|
53
|
+
} | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Convert a (non-streaming) Converse reply to a Chat Completions `chat.completion`: text blocks are joined into the content, reasoning blocks go
|
|
56
|
+
* in the settings' reasoning field (redacted ones are left out), `toolUse` blocks become tool calls, `stopReason` becomes `finish_reason` and
|
|
57
|
+
* `usage` becomes `usage`.
|
|
58
|
+
* @param {any} reply - Parsed Converse reply.
|
|
59
|
+
* @param {{ reasoningField?: string, model?: string }} [options] - `reasoningField` is where the reasoning goes (default `reasoning`); `model` is the model name on the completion.
|
|
60
|
+
* @returns {any} `chat.completion` object.
|
|
61
|
+
* @throws {TranslatorError} `invalid_upstream_response` (502) when the reply has no message.
|
|
62
|
+
*/
|
|
63
|
+
export function completion(reply: any, options?: {
|
|
64
|
+
reasoningField?: string;
|
|
65
|
+
model?: string;
|
|
66
|
+
}): any;
|
|
67
|
+
/**
|
|
68
|
+
* Create the converter of a ConverseStream's events into Chat Completions chunks. One chunker is for one reply: it keeps the completion id
|
|
69
|
+
* (so every chunk carries the same one), the numbering of tool calls and which content block each belongs to.
|
|
70
|
+
* @param {{ model?: string, reasoningField?: string, includeUsage?: boolean }} [options] - `model` is the model name on the chunks; `reasoningField` is where the reasoning goes (default `reasoning`); `includeUsage` (default true) adds the final usage chunk.
|
|
71
|
+
* @returns {{ event: (name: string | undefined, data: any) => any[], finished: () => boolean, usage: () => any }} `event` gives the chunks one event produces (an Anthropic-shaped `error` message gives one `{ error }` chunk), `finished` says whether the reply ended (`messageStop`, or a failure) and `usage` is the Converse `usage` of the `metadata` event.
|
|
72
|
+
*/
|
|
73
|
+
export function createChunker(options?: {
|
|
74
|
+
model?: string;
|
|
75
|
+
reasoningField?: string;
|
|
76
|
+
includeUsage?: boolean;
|
|
77
|
+
}): {
|
|
78
|
+
event: (name: string | undefined, data: any) => any[];
|
|
79
|
+
finished: () => boolean;
|
|
80
|
+
usage: () => any;
|
|
81
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validate the `documentUrls` setting and fill in its defaults.
|
|
3
|
+
* @param {any} value - `null` or `false` (fetching is off), or `{ allow, maxBytes?, timeout?, maxRedirects?, maxDocuments?, allowPrivate? }`.
|
|
4
|
+
* @returns {{allow: string[], maxBytes: number, timeout: number, maxRedirects: number, maxDocuments: number, allowPrivate: boolean} | null} The complete setting, or null when fetching is off.
|
|
5
|
+
* @throws {TranslatorError} With code `invalid_setting` for anything else, including an empty or missing `allow`.
|
|
6
|
+
*/
|
|
7
|
+
export function normalize(value: any): {
|
|
8
|
+
allow: string[];
|
|
9
|
+
maxBytes: number;
|
|
10
|
+
timeout: number;
|
|
11
|
+
maxRedirects: number;
|
|
12
|
+
maxDocuments: number;
|
|
13
|
+
allowPrivate: boolean;
|
|
14
|
+
} | null;
|
|
15
|
+
/**
|
|
16
|
+
* A URL without its query and fragment, which can carry a signature or a token, for notes and events.
|
|
17
|
+
* @param {string} url - URL.
|
|
18
|
+
* @returns {string} Origin and path, or a placeholder for text that is not a URL.
|
|
19
|
+
*/
|
|
20
|
+
export function display(url: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Whether a request needs this step: fetching is on, and nothing overrides or switches off document conversion.
|
|
23
|
+
* @param {any} settings - Resolved settings.
|
|
24
|
+
* @returns {boolean} True when `url` documents should be fetched.
|
|
25
|
+
*/
|
|
26
|
+
export function wanted(settings: any): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Replace the `url` source of each `document` block (also inside `tool_result` content) with the fetched bytes as a `base64` source, which the
|
|
29
|
+
* ordinary document conversion then reads. A document that could not be fetched gets a `fetch_failed` source carrying a reason that is safe for the
|
|
30
|
+
* client, and the conversion turns it into a note; the request itself goes on. The same URL is fetched once per call, and no more than `maxDocuments`
|
|
31
|
+
* different URLs are fetched (the rest fail with that reason). Nothing is cached between calls. The body is not changed: a copy is returned when
|
|
32
|
+
* anything was replaced.
|
|
33
|
+
* @param {any} body - Anthropic Messages request body.
|
|
34
|
+
* @param {{options: any, report: (event: {kind: string, message: string, [detail: string]: any}) => void, signal?: AbortSignal, fetch?: Function, deps?: any}} spec - The normalized `documentUrls` setting, a function that receives one event per fetch, the caller's abort signal, and replaceable parts for tests (`fetch`, and the `deps` given to it).
|
|
35
|
+
* @returns {Promise<any>} The body.
|
|
36
|
+
*/
|
|
37
|
+
export function resolve(body: any, { options, report, signal, fetch, deps }: {
|
|
38
|
+
options: any;
|
|
39
|
+
report: (event: {
|
|
40
|
+
kind: string;
|
|
41
|
+
message: string;
|
|
42
|
+
[detail: string]: any;
|
|
43
|
+
}) => void;
|
|
44
|
+
signal?: AbortSignal;
|
|
45
|
+
fetch?: Function;
|
|
46
|
+
deps?: any;
|
|
47
|
+
}): Promise<any>;
|
|
48
|
+
/** Defaults of the fields of `documentUrls` other than `allow`. */
|
|
49
|
+
export const DEFAULTS: Readonly<{
|
|
50
|
+
maxBytes: number;
|
|
51
|
+
timeout: 30000;
|
|
52
|
+
maxRedirects: 3;
|
|
53
|
+
maxDocuments: 10;
|
|
54
|
+
allowPrivate: false;
|
|
55
|
+
}>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create an incremental frame parser. A frame may be split across chunks at any byte. The prelude CRC is checked as soon as the first twelve
|
|
3
|
+
* bytes are in, so a stream that is not an event stream fails at once, and the message CRC when the frame is complete.
|
|
4
|
+
* @returns {{ push: (chunk: Uint8Array) => {headers: Record<string, any>, payload: Buffer}[], flush: () => void }} Parser; `push` throws `invalid_stream_data` (502) for a bad length or CRC (when frames before the bad one were read, it returns them and throws on the next `push` or `flush`), `flush` throws when the stream ended inside a frame.
|
|
5
|
+
*/
|
|
6
|
+
export function createParser(): {
|
|
7
|
+
push: (chunk: Uint8Array) => {
|
|
8
|
+
headers: Record<string, any>;
|
|
9
|
+
payload: Buffer;
|
|
10
|
+
}[];
|
|
11
|
+
flush: () => void;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Parse an event stream (any `lib/source.mjs` input of bytes) into frames.
|
|
15
|
+
* @param {ReadableStream|AsyncIterable<Uint8Array>|Iterable<Uint8Array>|Uint8Array} source - Event stream bytes.
|
|
16
|
+
* @returns {AsyncGenerator<{headers: Record<string, any>, payload: Buffer}, void, undefined>} Frames.
|
|
17
|
+
*/
|
|
18
|
+
export function frames(source: ReadableStream | AsyncIterable<Uint8Array> | Iterable<Uint8Array> | Uint8Array): AsyncGenerator<{
|
|
19
|
+
headers: Record<string, any>;
|
|
20
|
+
payload: Buffer;
|
|
21
|
+
}, void, undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Convert an event stream to `{ event, data }` messages: the shape SSE parsing gives, so the stream converters read it like a Server-Sent Events stream.
|
|
24
|
+
* - `"invoke"` (`InvokeModelWithResponseStream`): each `chunk` event carries `{ "bytes": "<base64>" }`, which decodes to one Anthropic stream event; the message is that event, named by its `type`. Bedrock's `amazon-bedrock-invocationMetrics` field is removed.
|
|
25
|
+
* - `"converse"` (`ConverseStream`): each event is named by its `:event-type` header (`messageStart`, `contentBlockDelta`, ...) and its payload is the data.
|
|
26
|
+
* An `exception` or `error` frame becomes one Anthropic `error` message (see `failure`), and so does a stream that cannot be read (a bad checksum, a payload that is not JSON, a frame cut short, a failing connection), after which the messages end.
|
|
27
|
+
* @param {ReadableStream|AsyncIterable<Uint8Array>|Iterable<Uint8Array>|Uint8Array} source - Event stream bytes.
|
|
28
|
+
* @param {"invoke" | "converse"} kind - Which API the stream comes from.
|
|
29
|
+
* @returns {AsyncGenerator<{event: string, data: any}, void, undefined>} Messages.
|
|
30
|
+
*/
|
|
31
|
+
export function events(source: ReadableStream | AsyncIterable<Uint8Array> | Iterable<Uint8Array> | Uint8Array, kind: "invoke" | "converse"): AsyncGenerator<{
|
|
32
|
+
event: string;
|
|
33
|
+
data: any;
|
|
34
|
+
}, void, undefined>;
|