@opencode-ai/ai 0.0.0-bootstrap.0 → 0.0.0-next-15618
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 -1
- package/dist/cache-policy.d.ts +2 -0
- package/dist/cache-policy.js +108 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +9 -0
- package/dist/llm.d.ts +223 -0
- package/dist/llm.js +81 -0
- package/dist/protocols/anthropic-messages.d.ts +503 -0
- package/dist/protocols/anthropic-messages.js +745 -0
- package/dist/protocols/bedrock-converse.d.ts +476 -0
- package/dist/protocols/bedrock-converse.js +526 -0
- package/dist/protocols/bedrock-event-stream.d.ts +9 -0
- package/dist/protocols/bedrock-event-stream.js +64 -0
- package/dist/protocols/gemini.d.ts +220 -0
- package/dist/protocols/gemini.js +420 -0
- package/dist/protocols/google-vertex-anthropic.d.ts +494 -0
- package/dist/protocols/google-vertex-anthropic.js +33 -0
- package/dist/protocols/google-vertex-gemini.d.ts +59 -0
- package/dist/protocols/google-vertex-gemini.js +18 -0
- package/dist/protocols/index.d.ts +9 -0
- package/dist/protocols/index.js +9 -0
- package/dist/protocols/openai-chat.d.ts +391 -0
- package/dist/protocols/openai-chat.js +423 -0
- package/dist/protocols/openai-compatible-chat.d.ts +73 -0
- package/dist/protocols/openai-compatible-chat.js +20 -0
- package/dist/protocols/openai-compatible-responses.d.ts +85 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-responses.d.ts +613 -0
- package/dist/protocols/openai-responses.js +828 -0
- package/dist/protocols/shared.d.ts +192 -0
- package/dist/protocols/shared.js +236 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +18 -0
- package/dist/protocols/utils/bedrock-auth.js +41 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +15 -0
- package/dist/protocols/utils/bedrock-cache.js +29 -0
- package/dist/protocols/utils/bedrock-media.d.ts +49 -0
- package/dist/protocols/utils/bedrock-media.js +68 -0
- package/dist/protocols/utils/cache.d.ts +6 -0
- package/dist/protocols/utils/cache.js +8 -0
- package/dist/protocols/utils/gemini-tool-schema.d.ts +2 -0
- package/dist/protocols/utils/gemini-tool-schema.js +83 -0
- package/dist/protocols/utils/lifecycle.d.ts +19 -0
- package/dist/protocols/utils/lifecycle.js +65 -0
- package/dist/protocols/utils/openai-options.d.ts +22 -0
- package/dist/protocols/utils/openai-options.js +64 -0
- package/dist/protocols/utils/tool-schema.d.ts +7 -0
- package/dist/protocols/utils/tool-schema.js +79 -0
- package/dist/protocols/utils/tool-stream.d.ts +155 -0
- package/dist/protocols/utils/tool-stream.js +136 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +13 -0
- package/dist/provider-error.js +122 -0
- package/dist/provider-package.d.ts +13 -0
- package/dist/provider-package.js +1 -0
- package/dist/provider.d.ts +23 -0
- package/dist/provider.js +2 -0
- package/dist/providers/amazon-bedrock.d.ts +150 -0
- package/dist/providers/amazon-bedrock.js +41 -0
- package/dist/providers/anthropic-compatible.d.ts +183 -0
- package/dist/providers/anthropic-compatible.js +44 -0
- package/dist/providers/anthropic.d.ts +181 -0
- package/dist/providers/anthropic.js +39 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +183 -0
- package/dist/providers/azure.js +89 -0
- package/dist/providers/cloudflare.d.ts +232 -0
- package/dist/providers/cloudflare.js +87 -0
- package/dist/providers/github-copilot.d.ts +167 -0
- package/dist/providers/github-copilot.js +47 -0
- package/dist/providers/google-vertex/anthropic.d.ts +2 -0
- package/dist/providers/google-vertex/anthropic.js +1 -0
- package/dist/providers/google-vertex-anthropic.d.ts +181 -0
- package/dist/providers/google-vertex-anthropic.js +46 -0
- package/dist/providers/google-vertex-shared.d.ts +22 -0
- package/dist/providers/google-vertex-shared.js +61 -0
- package/dist/providers/google-vertex.d.ts +93 -0
- package/dist/providers/google-vertex.js +49 -0
- package/dist/providers/google.d.ts +85 -0
- package/dist/providers/google.js +33 -0
- package/dist/providers/index.d.ts +15 -0
- package/dist/providers/index.js +15 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-profile.d.ts +43 -0
- package/dist/providers/openai-compatible-profile.js +12 -0
- package/dist/providers/openai-compatible-responses.d.ts +107 -0
- package/dist/providers/openai-compatible-responses.js +33 -0
- package/dist/providers/openai-compatible.d.ts +130 -0
- package/dist/providers/openai-compatible.js +55 -0
- package/dist/providers/openai-options.d.ts +30 -0
- package/dist/providers/openai-options.js +42 -0
- package/dist/providers/openai.d.ts +264 -0
- package/dist/providers/openai.js +62 -0
- package/dist/providers/openrouter.d.ts +321 -0
- package/dist/providers/openrouter.js +65 -0
- package/dist/providers/xai.d.ts +169 -0
- package/dist/providers/xai.js +43 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/route/auth-options.d.ts +34 -0
- package/dist/route/auth-options.js +14 -0
- package/dist/route/auth.d.ts +49 -0
- package/dist/route/auth.js +89 -0
- package/dist/route/client.d.ts +330 -0
- package/dist/route/client.js +211 -0
- package/dist/route/endpoint.d.ts +28 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor.d.ts +12 -0
- package/dist/route/executor.js +229 -0
- package/dist/route/framing.d.ts +23 -0
- package/dist/route/framing.js +4 -0
- package/dist/route/index.d.ts +16 -0
- package/dist/route/index.js +9 -0
- package/dist/route/protocol.d.ts +76 -0
- package/dist/route/protocol.js +17 -0
- package/dist/route/transport/http.d.ts +33 -0
- package/dist/route/transport/http.js +98 -0
- package/dist/route/transport/index.d.ts +27 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket.d.ts +56 -0
- package/dist/route/transport/websocket.js +181 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +153 -0
- package/dist/schema/errors.js +150 -0
- package/dist/schema/events.d.ts +4292 -0
- package/dist/schema/events.js +482 -0
- package/dist/schema/ids.d.ts +30 -0
- package/dist/schema/ids.js +18 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +385 -0
- package/dist/schema/messages.js +246 -0
- package/dist/schema/options.d.ts +143 -0
- package/dist/schema/options.js +195 -0
- package/dist/tool-runtime.d.ts +15 -0
- package/dist/tool-runtime.js +35 -0
- package/dist/tool.d.ts +134 -0
- package/dist/tool.js +66 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/package.json +36 -9
- package/index.js +0 -1
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { Effect, Schema, Stream } from "effect";
|
|
3
|
+
import { Headers, HttpClientRequest } from "effect/unstable/http";
|
|
4
|
+
import { LLMError, type ContentPart, type LLMRequest, type ToolFileContent, type ToolResultPart } from "../schema";
|
|
5
|
+
import { isRecord } from "../utils/record";
|
|
6
|
+
export { isRecord };
|
|
7
|
+
export declare const Json: Schema.fromJsonString<Schema.Unknown>;
|
|
8
|
+
export declare const decodeJson: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => unknown;
|
|
9
|
+
export declare const encodeJson: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => string;
|
|
10
|
+
export declare const JsonObject: Schema.$Record<Schema.String, Schema.Unknown>;
|
|
11
|
+
export declare const optionalArray: <const S extends Schema.Top>(schema: S) => Schema.optional<Schema.$Array<S>>;
|
|
12
|
+
export declare const optionalNull: <const S extends Schema.Top>(schema: S) => Schema.optional<Schema.NullOr<S>>;
|
|
13
|
+
/**
|
|
14
|
+
* Streaming tool-call accumulator. Adapters that build a tool call across
|
|
15
|
+
* multiple `tool-input-delta` chunks store the partial JSON input string here
|
|
16
|
+
* and finalize it with `parseToolInput` once the call completes.
|
|
17
|
+
*/
|
|
18
|
+
export interface ToolAccumulator {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly input: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* `Usage.totalTokens` policy shared by every route. Honors a provider-
|
|
25
|
+
* supplied total; otherwise falls back to `inputTokens + outputTokens` only
|
|
26
|
+
* when at least one is defined. Returns `undefined` when neither input nor
|
|
27
|
+
* output is known so routes don't publish a misleading `0`.
|
|
28
|
+
*
|
|
29
|
+
* Under the additive `LLM.Usage` contract, `inputTokens` and `outputTokens`
|
|
30
|
+
* are the non-cached input and visible output only. The provider-supplied
|
|
31
|
+
* `total` is the source of truth when present; the computed fallback
|
|
32
|
+
* under-counts cache and reasoning by design and exists mainly so
|
|
33
|
+
* Anthropic-style providers (which don't surface a total) still get a
|
|
34
|
+
* sensible aggregate on the input + output axes.
|
|
35
|
+
*/
|
|
36
|
+
export declare const totalTokens: (inputTokens: number | undefined, outputTokens: number | undefined, total: number | undefined) => number | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Subtract `subtrahend` from `total`, clamping to zero if the provider
|
|
39
|
+
* reports a non-sensical breakdown (e.g. `cached_tokens > prompt_tokens`).
|
|
40
|
+
* Used by protocol mappers when deriving a non-overlapping breakdown field
|
|
41
|
+
* from a provider's inclusive total — `nonCachedInputTokens` from
|
|
42
|
+
* `inputTokens - cacheReadInputTokens - cacheWriteInputTokens`.
|
|
43
|
+
*
|
|
44
|
+
* If `total` is `undefined`, returns `undefined` (we don't fabricate
|
|
45
|
+
* counts). If `subtrahend` is `undefined`, returns `total` unchanged. The
|
|
46
|
+
* provider-native breakdown stays available on `Usage.native` for debugging.
|
|
47
|
+
*/
|
|
48
|
+
export declare const subtractTokens: (total: number | undefined, subtrahend: number | undefined) => number | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Sum a list of optional token counts, returning `undefined` only when
|
|
51
|
+
* every value is `undefined` (so we don't fabricate a `0`). Used by
|
|
52
|
+
* protocol mappers to derive the inclusive `inputTokens` total from a
|
|
53
|
+
* provider that natively reports a non-overlapping breakdown
|
|
54
|
+
* (e.g. Anthropic, whose `input_tokens` is already non-cached only).
|
|
55
|
+
*/
|
|
56
|
+
export declare const sumTokens: (...values: ReadonlyArray<number | undefined>) => number | undefined;
|
|
57
|
+
export declare const eventError: (route: string, message: string, raw?: string) => LLMError;
|
|
58
|
+
export declare const parseJson: (route: string, input: string, message: string) => Effect.Effect<unknown, LLMError, never>;
|
|
59
|
+
/**
|
|
60
|
+
* Join the `text` field of a list of parts with newlines. Used by routes
|
|
61
|
+
* that flatten system / message content arrays into a single provider string
|
|
62
|
+
* (OpenAI Chat `system` content, OpenAI Responses `system` content, Gemini
|
|
63
|
+
* `systemInstruction.parts[].text`).
|
|
64
|
+
*/
|
|
65
|
+
export declare const joinText: (parts: ReadonlyArray<{
|
|
66
|
+
readonly text: string;
|
|
67
|
+
}>) => string;
|
|
68
|
+
/**
|
|
69
|
+
* Stable fallback representation for chronological `Message.system(...)`
|
|
70
|
+
* updates on routes that do not support that privileged role natively. The
|
|
71
|
+
* wrapper remains visibly lower-authority user text, preserves the original
|
|
72
|
+
* temporal position, and XML-escapes content so it cannot close the wrapper.
|
|
73
|
+
*/
|
|
74
|
+
export declare const wrapSystemUpdate: (parts: ReadonlyArray<{
|
|
75
|
+
readonly text: string;
|
|
76
|
+
}>) => string;
|
|
77
|
+
/**
|
|
78
|
+
* Chronological system updates deliberately accept text only. Do not insert
|
|
79
|
+
* raw retrieved, tool, or web content into privileged updates: keep untrusted
|
|
80
|
+
* data in ordinary user/tool messages instead.
|
|
81
|
+
*/
|
|
82
|
+
export declare const systemUpdateText: (route: string, message: import("..").Message) => Effect.Effect<{
|
|
83
|
+
readonly type: "text";
|
|
84
|
+
readonly text: string;
|
|
85
|
+
readonly cache?: import("..").CacheHint | undefined;
|
|
86
|
+
readonly metadata?: {
|
|
87
|
+
readonly [x: string]: unknown;
|
|
88
|
+
} | undefined;
|
|
89
|
+
readonly providerMetadata?: {
|
|
90
|
+
readonly [x: string]: {
|
|
91
|
+
readonly [x: string]: unknown;
|
|
92
|
+
};
|
|
93
|
+
} | undefined;
|
|
94
|
+
}[], LLMError, never>;
|
|
95
|
+
/** Lower an unsupported privileged update into visible, in-order user text. */
|
|
96
|
+
export declare const wrappedSystemUpdate: (route: string, message: import("..").Message) => Effect.Effect<{
|
|
97
|
+
type: "text";
|
|
98
|
+
text: string;
|
|
99
|
+
cache: import("..").CacheHint | undefined;
|
|
100
|
+
}, LLMError, never>;
|
|
101
|
+
/**
|
|
102
|
+
* Parse the streamed JSON input of a tool call. Treats an empty string as
|
|
103
|
+
* `"{}"` — providers occasionally finish a tool call without ever emitting
|
|
104
|
+
* input deltas (e.g. zero-arg tools). The error message is uniform across
|
|
105
|
+
* routes: `Invalid JSON input for <route> tool call <name>`.
|
|
106
|
+
*/
|
|
107
|
+
export declare const parseToolInput: (route: string, name: string, raw: string) => Effect.Effect<unknown, LLMError, never>;
|
|
108
|
+
export declare const IMAGE_MIMES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
|
109
|
+
export declare const VIDEO_MIMES: readonly ["video/mp4", "video/webm", "video/quicktime"];
|
|
110
|
+
export declare const AUDIO_MIMES: readonly ["audio/wav", "audio/mp3", "audio/aiff", "audio/aac", "audio/ogg", "audio/flac"];
|
|
111
|
+
export declare const MEDIA_MIMES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp", "video/mp4", "video/webm", "video/quicktime", "audio/wav", "audio/mp3", "audio/aiff", "audio/aac", "audio/ogg", "audio/flac"];
|
|
112
|
+
export declare const MAX_MEDIA_ENCODED_BYTES: number;
|
|
113
|
+
export declare const MAX_MEDIA_DECODED_BYTES: number;
|
|
114
|
+
export interface ValidatedMedia {
|
|
115
|
+
readonly mime: string;
|
|
116
|
+
readonly base64: string;
|
|
117
|
+
readonly dataUrl: string;
|
|
118
|
+
readonly bytes: Uint8Array;
|
|
119
|
+
}
|
|
120
|
+
export declare const validateMedia: (route: string, part: {
|
|
121
|
+
readonly type: "media";
|
|
122
|
+
readonly mediaType: string;
|
|
123
|
+
readonly data: string | Uint8Array<ArrayBufferLike>;
|
|
124
|
+
readonly filename?: string | undefined;
|
|
125
|
+
readonly metadata?: {
|
|
126
|
+
readonly [x: string]: unknown;
|
|
127
|
+
} | undefined;
|
|
128
|
+
}, supportedMimes: ReadonlySet<string>) => Effect.Effect<{
|
|
129
|
+
mime: string;
|
|
130
|
+
base64: string;
|
|
131
|
+
dataUrl: string;
|
|
132
|
+
bytes: Buffer<ArrayBuffer>;
|
|
133
|
+
}, LLMError, never>;
|
|
134
|
+
export declare const validateToolFile: (route: string, part: ToolFileContent, supportedMimes: ReadonlySet<string>) => Effect.Effect<{
|
|
135
|
+
mime: string;
|
|
136
|
+
base64: string;
|
|
137
|
+
dataUrl: string;
|
|
138
|
+
bytes: Buffer<ArrayBuffer>;
|
|
139
|
+
}, LLMError, never>;
|
|
140
|
+
export declare const trimBaseUrl: (value: string) => string;
|
|
141
|
+
export declare const toolResultText: (part: ToolResultPart) => string;
|
|
142
|
+
export declare const errorText: (error: unknown) => string;
|
|
143
|
+
/**
|
|
144
|
+
* `framing` step for Server-Sent Events. Decodes UTF-8, runs the SSE channel
|
|
145
|
+
* decoder, and drops empty / `[DONE]` keep-alive events so the downstream
|
|
146
|
+
* `decodeChunk` sees one JSON string per element. The SSE channel emits a
|
|
147
|
+
* `Retry` control event on its error channel; we drop it here (we don't
|
|
148
|
+
* implement client-driven retries) so the public error channel stays
|
|
149
|
+
* `LLMError`.
|
|
150
|
+
*/
|
|
151
|
+
export declare const sseFraming: (bytes: Stream.Stream<Uint8Array, LLMError>) => Stream.Stream<string, LLMError>;
|
|
152
|
+
/**
|
|
153
|
+
* Canonical invalid-request constructor. Lift one-line `const invalid =
|
|
154
|
+
* (message) => invalidRequest(message)` aliases out of every
|
|
155
|
+
* route so the error constructor lives in one place. If we ever extend
|
|
156
|
+
* `InvalidRequestReason` with route context or trace metadata, the change
|
|
157
|
+
* lands here.
|
|
158
|
+
*/
|
|
159
|
+
export declare const invalidRequest: (message: string) => LLMError;
|
|
160
|
+
export declare const matchToolChoice: <Auto, None, Required, Tool>(route: string, toolChoice: NonNullable<LLMRequest["toolChoice"]>, cases: {
|
|
161
|
+
readonly auto: () => Auto;
|
|
162
|
+
readonly none: () => None;
|
|
163
|
+
readonly required: () => Required;
|
|
164
|
+
readonly tool: (name: string) => Tool;
|
|
165
|
+
}) => Effect.Effect<Auto | None | Required | Tool, LLMError, never>;
|
|
166
|
+
type ContentType = ContentPart["type"];
|
|
167
|
+
export declare const supportsContent: <const Type extends ContentType>(part: ContentPart, types: ReadonlyArray<Type>) => part is Extract<ContentPart, {
|
|
168
|
+
readonly type: Type;
|
|
169
|
+
}>;
|
|
170
|
+
export declare const unsupportedContent: (route: string, role: LLMRequest["messages"][number]["role"], types: ReadonlyArray<ContentType>) => LLMError;
|
|
171
|
+
/**
|
|
172
|
+
* Build a `validate` step from a Schema decoder. Replaces the per-route
|
|
173
|
+
* lambda body `(payload) => decode(payload).pipe(Effect.mapError((e) =>
|
|
174
|
+
* invalid(e.message)))`. Any decode error is translated into
|
|
175
|
+
* `LLMError` carrying the original parse-error message.
|
|
176
|
+
*/
|
|
177
|
+
export declare const validateWith: <A, I, E extends {
|
|
178
|
+
readonly message: string;
|
|
179
|
+
}>(decode: (input: I) => Effect.Effect<A, E>) => (payload: I) => Effect.Effect<A, LLMError, never>;
|
|
180
|
+
/**
|
|
181
|
+
* Build an HTTP POST with a JSON body. Sets `content-type: application/json`
|
|
182
|
+
* automatically after caller-supplied headers so routes cannot accidentally
|
|
183
|
+
* send JSON with a stale content type. The body is passed pre-encoded so
|
|
184
|
+
* routes can choose between
|
|
185
|
+
* `Schema.encodeSync(payload)` and `ProviderShared.encodeJson(payload)`.
|
|
186
|
+
*/
|
|
187
|
+
export declare const jsonPost: (input: {
|
|
188
|
+
readonly url: string;
|
|
189
|
+
readonly body: string;
|
|
190
|
+
readonly headers?: Headers.Input;
|
|
191
|
+
}) => HttpClientRequest.HttpClientRequest;
|
|
192
|
+
export * as ProviderShared from "./shared";
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { Effect, Schema, Stream } from "effect";
|
|
3
|
+
import * as Sse from "effect/unstable/encoding/Sse";
|
|
4
|
+
import { Headers, HttpClientRequest } from "effect/unstable/http";
|
|
5
|
+
import { InvalidProviderOutputReason, InvalidRequestReason, LLMError, } from "../schema";
|
|
6
|
+
import { isRecord } from "../utils/record";
|
|
7
|
+
export { isRecord };
|
|
8
|
+
export const Json = Schema.fromJsonString(Schema.Unknown);
|
|
9
|
+
export const decodeJson = Schema.decodeUnknownSync(Json);
|
|
10
|
+
export const encodeJson = Schema.encodeSync(Json);
|
|
11
|
+
const isJson = Schema.is(Schema.Json);
|
|
12
|
+
export const JsonObject = Schema.Record(Schema.String, Schema.Unknown);
|
|
13
|
+
export const optionalArray = (schema) => Schema.optional(Schema.Array(schema));
|
|
14
|
+
export const optionalNull = (schema) => Schema.optional(Schema.NullOr(schema));
|
|
15
|
+
/**
|
|
16
|
+
* `Usage.totalTokens` policy shared by every route. Honors a provider-
|
|
17
|
+
* supplied total; otherwise falls back to `inputTokens + outputTokens` only
|
|
18
|
+
* when at least one is defined. Returns `undefined` when neither input nor
|
|
19
|
+
* output is known so routes don't publish a misleading `0`.
|
|
20
|
+
*
|
|
21
|
+
* Under the additive `LLM.Usage` contract, `inputTokens` and `outputTokens`
|
|
22
|
+
* are the non-cached input and visible output only. The provider-supplied
|
|
23
|
+
* `total` is the source of truth when present; the computed fallback
|
|
24
|
+
* under-counts cache and reasoning by design and exists mainly so
|
|
25
|
+
* Anthropic-style providers (which don't surface a total) still get a
|
|
26
|
+
* sensible aggregate on the input + output axes.
|
|
27
|
+
*/
|
|
28
|
+
export const totalTokens = (inputTokens, outputTokens, total) => {
|
|
29
|
+
if (total !== undefined)
|
|
30
|
+
return total;
|
|
31
|
+
if (inputTokens === undefined && outputTokens === undefined)
|
|
32
|
+
return undefined;
|
|
33
|
+
return (inputTokens ?? 0) + (outputTokens ?? 0);
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Subtract `subtrahend` from `total`, clamping to zero if the provider
|
|
37
|
+
* reports a non-sensical breakdown (e.g. `cached_tokens > prompt_tokens`).
|
|
38
|
+
* Used by protocol mappers when deriving a non-overlapping breakdown field
|
|
39
|
+
* from a provider's inclusive total — `nonCachedInputTokens` from
|
|
40
|
+
* `inputTokens - cacheReadInputTokens - cacheWriteInputTokens`.
|
|
41
|
+
*
|
|
42
|
+
* If `total` is `undefined`, returns `undefined` (we don't fabricate
|
|
43
|
+
* counts). If `subtrahend` is `undefined`, returns `total` unchanged. The
|
|
44
|
+
* provider-native breakdown stays available on `Usage.native` for debugging.
|
|
45
|
+
*/
|
|
46
|
+
export const subtractTokens = (total, subtrahend) => {
|
|
47
|
+
if (total === undefined)
|
|
48
|
+
return undefined;
|
|
49
|
+
if (subtrahend === undefined)
|
|
50
|
+
return total;
|
|
51
|
+
return Math.max(0, total - subtrahend);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Sum a list of optional token counts, returning `undefined` only when
|
|
55
|
+
* every value is `undefined` (so we don't fabricate a `0`). Used by
|
|
56
|
+
* protocol mappers to derive the inclusive `inputTokens` total from a
|
|
57
|
+
* provider that natively reports a non-overlapping breakdown
|
|
58
|
+
* (e.g. Anthropic, whose `input_tokens` is already non-cached only).
|
|
59
|
+
*/
|
|
60
|
+
export const sumTokens = (...values) => {
|
|
61
|
+
if (values.every((value) => value === undefined))
|
|
62
|
+
return undefined;
|
|
63
|
+
return values.reduce((acc, value) => acc + (value ?? 0), 0);
|
|
64
|
+
};
|
|
65
|
+
export const eventError = (route, message, raw) => new LLMError({
|
|
66
|
+
module: "ProviderShared",
|
|
67
|
+
method: "stream",
|
|
68
|
+
reason: new InvalidProviderOutputReason({ route, message, raw }),
|
|
69
|
+
});
|
|
70
|
+
export const parseJson = (route, input, message) => Effect.try({
|
|
71
|
+
try: () => decodeJson(input),
|
|
72
|
+
catch: () => eventError(route, message, input),
|
|
73
|
+
});
|
|
74
|
+
/**
|
|
75
|
+
* Join the `text` field of a list of parts with newlines. Used by routes
|
|
76
|
+
* that flatten system / message content arrays into a single provider string
|
|
77
|
+
* (OpenAI Chat `system` content, OpenAI Responses `system` content, Gemini
|
|
78
|
+
* `systemInstruction.parts[].text`).
|
|
79
|
+
*/
|
|
80
|
+
export const joinText = (parts) => parts.map((part) => part.text).join("\n");
|
|
81
|
+
const escapeSystemUpdateText = (text) => text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
82
|
+
/**
|
|
83
|
+
* Stable fallback representation for chronological `Message.system(...)`
|
|
84
|
+
* updates on routes that do not support that privileged role natively. The
|
|
85
|
+
* wrapper remains visibly lower-authority user text, preserves the original
|
|
86
|
+
* temporal position, and XML-escapes content so it cannot close the wrapper.
|
|
87
|
+
*/
|
|
88
|
+
export const wrapSystemUpdate = (parts) => `<system-update>\n${escapeSystemUpdateText(joinText(parts))}\n</system-update>`;
|
|
89
|
+
/**
|
|
90
|
+
* Chronological system updates deliberately accept text only. Do not insert
|
|
91
|
+
* raw retrieved, tool, or web content into privileged updates: keep untrusted
|
|
92
|
+
* data in ordinary user/tool messages instead.
|
|
93
|
+
*/
|
|
94
|
+
export const systemUpdateText = Effect.fn("ProviderShared.systemUpdateText")(function* (route, message) {
|
|
95
|
+
const content = [];
|
|
96
|
+
for (const part of message.content) {
|
|
97
|
+
if (!supportsContent(part, ["text"]))
|
|
98
|
+
return yield* unsupportedContent(route, "system", ["text"]);
|
|
99
|
+
content.push(part);
|
|
100
|
+
}
|
|
101
|
+
return content;
|
|
102
|
+
});
|
|
103
|
+
/** Lower an unsupported privileged update into visible, in-order user text. */
|
|
104
|
+
export const wrappedSystemUpdate = Effect.fn("ProviderShared.wrappedSystemUpdate")(function* (route, message) {
|
|
105
|
+
const content = yield* systemUpdateText(route, message);
|
|
106
|
+
return { type: "text", text: wrapSystemUpdate(content), cache: content.at(-1)?.cache };
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* Parse the streamed JSON input of a tool call. Treats an empty string as
|
|
110
|
+
* `"{}"` — providers occasionally finish a tool call without ever emitting
|
|
111
|
+
* input deltas (e.g. zero-arg tools). The error message is uniform across
|
|
112
|
+
* routes: `Invalid JSON input for <route> tool call <name>`.
|
|
113
|
+
*/
|
|
114
|
+
export const parseToolInput = (route, name, raw) => parseJson(route, raw || "{}", `Invalid JSON input for ${route} tool call ${name}`);
|
|
115
|
+
export const IMAGE_MIMES = ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
|
116
|
+
export const VIDEO_MIMES = ["video/mp4", "video/webm", "video/quicktime"];
|
|
117
|
+
export const AUDIO_MIMES = ["audio/wav", "audio/mp3", "audio/aiff", "audio/aac", "audio/ogg", "audio/flac"];
|
|
118
|
+
export const MEDIA_MIMES = [...IMAGE_MIMES, ...VIDEO_MIMES, ...AUDIO_MIMES];
|
|
119
|
+
export const MAX_MEDIA_ENCODED_BYTES = 28 * 1024 * 1024;
|
|
120
|
+
export const MAX_MEDIA_DECODED_BYTES = 20 * 1024 * 1024;
|
|
121
|
+
const base64Pattern = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
122
|
+
export const validateMedia = Effect.fn("ProviderShared.validateMedia")(function* (route, part, supportedMimes) {
|
|
123
|
+
const mime = part.mediaType.toLowerCase();
|
|
124
|
+
if (!supportedMimes.has(mime))
|
|
125
|
+
return yield* invalidRequest(`${route} does not support media type ${part.mediaType}`);
|
|
126
|
+
let base64;
|
|
127
|
+
if (typeof part.data !== "string") {
|
|
128
|
+
if (part.data.byteLength > MAX_MEDIA_DECODED_BYTES)
|
|
129
|
+
return yield* invalidRequest(`${route} media exceeds the ${MAX_MEDIA_DECODED_BYTES} byte decoded limit`);
|
|
130
|
+
base64 = Buffer.from(part.data).toString("base64");
|
|
131
|
+
}
|
|
132
|
+
else if (part.data.startsWith("data:")) {
|
|
133
|
+
const match = /^data:([^;,]+);base64,([A-Za-z0-9+/]*={0,2})$/s.exec(part.data);
|
|
134
|
+
if (!match)
|
|
135
|
+
return yield* invalidRequest(`${route} media data URL must contain valid base64`);
|
|
136
|
+
if (match[1].toLowerCase() !== mime)
|
|
137
|
+
return yield* invalidRequest(`${route} media type ${part.mediaType} does not match data URL type ${match[1]}`);
|
|
138
|
+
base64 = match[2];
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
base64 = part.data;
|
|
142
|
+
}
|
|
143
|
+
if (Buffer.byteLength(base64, "utf8") > MAX_MEDIA_ENCODED_BYTES)
|
|
144
|
+
return yield* invalidRequest(`${route} media exceeds the ${MAX_MEDIA_ENCODED_BYTES} byte encoded limit`);
|
|
145
|
+
if (!base64 || base64.length % 4 !== 0 || !base64Pattern.test(base64))
|
|
146
|
+
return yield* invalidRequest(`${route} media must contain valid base64`);
|
|
147
|
+
const bytes = Buffer.from(base64, "base64");
|
|
148
|
+
if (bytes.byteLength > MAX_MEDIA_DECODED_BYTES)
|
|
149
|
+
return yield* invalidRequest(`${route} media exceeds the ${MAX_MEDIA_DECODED_BYTES} byte decoded limit`);
|
|
150
|
+
if (bytes.toString("base64") !== base64)
|
|
151
|
+
return yield* invalidRequest(`${route} media must contain canonical base64`);
|
|
152
|
+
return { mime, base64, dataUrl: `data:${mime};base64,${base64}`, bytes };
|
|
153
|
+
});
|
|
154
|
+
export const validateToolFile = (route, part, supportedMimes) => validateMedia(route, { type: "media", mediaType: part.mime, data: part.uri, filename: part.name }, supportedMimes);
|
|
155
|
+
export const trimBaseUrl = (value) => value.replace(/\/+$/, "");
|
|
156
|
+
export const toolResultText = (part) => {
|
|
157
|
+
if (part.result.type === "text")
|
|
158
|
+
return String(part.result.value);
|
|
159
|
+
if (part.result.type === "error") {
|
|
160
|
+
const value = part.result.value;
|
|
161
|
+
const prototype = typeof value === "object" && value !== null && !Array.isArray(value) && Object.getPrototypeOf(value);
|
|
162
|
+
const structured = Array.isArray(value) || prototype === Object.prototype || prototype === null;
|
|
163
|
+
return structured && isJson(value) ? encodeJson(value) : String(value);
|
|
164
|
+
}
|
|
165
|
+
return encodeJson(part.result.value);
|
|
166
|
+
};
|
|
167
|
+
export const errorText = (error) => {
|
|
168
|
+
if (error instanceof Error)
|
|
169
|
+
return error.message;
|
|
170
|
+
if (typeof error === "string")
|
|
171
|
+
return error;
|
|
172
|
+
if (typeof error === "number" || typeof error === "boolean" || typeof error === "bigint")
|
|
173
|
+
return String(error);
|
|
174
|
+
if (error === null)
|
|
175
|
+
return "null";
|
|
176
|
+
if (error === undefined)
|
|
177
|
+
return "undefined";
|
|
178
|
+
return "Unknown stream error";
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* `framing` step for Server-Sent Events. Decodes UTF-8, runs the SSE channel
|
|
182
|
+
* decoder, and drops empty / `[DONE]` keep-alive events so the downstream
|
|
183
|
+
* `decodeChunk` sees one JSON string per element. The SSE channel emits a
|
|
184
|
+
* `Retry` control event on its error channel; we drop it here (we don't
|
|
185
|
+
* implement client-driven retries) so the public error channel stays
|
|
186
|
+
* `LLMError`.
|
|
187
|
+
*/
|
|
188
|
+
export const sseFraming = (bytes) => bytes.pipe(Stream.decodeText(), Stream.pipeThroughChannel(Sse.decode()), Stream.catchTag("Retry", () => Stream.empty), Stream.filter((event) => event.data.length > 0 && event.data !== "[DONE]"), Stream.map((event) => event.data));
|
|
189
|
+
/**
|
|
190
|
+
* Canonical invalid-request constructor. Lift one-line `const invalid =
|
|
191
|
+
* (message) => invalidRequest(message)` aliases out of every
|
|
192
|
+
* route so the error constructor lives in one place. If we ever extend
|
|
193
|
+
* `InvalidRequestReason` with route context or trace metadata, the change
|
|
194
|
+
* lands here.
|
|
195
|
+
*/
|
|
196
|
+
export const invalidRequest = (message) => new LLMError({
|
|
197
|
+
module: "ProviderShared",
|
|
198
|
+
method: "request",
|
|
199
|
+
reason: new InvalidRequestReason({ message }),
|
|
200
|
+
});
|
|
201
|
+
export const matchToolChoice = (route, toolChoice, cases) => Effect.gen(function* () {
|
|
202
|
+
if (toolChoice.type === "auto")
|
|
203
|
+
return cases.auto();
|
|
204
|
+
if (toolChoice.type === "none")
|
|
205
|
+
return cases.none();
|
|
206
|
+
if (toolChoice.type === "required")
|
|
207
|
+
return cases.required();
|
|
208
|
+
if (!toolChoice.name)
|
|
209
|
+
return yield* invalidRequest(`${route} tool choice requires a tool name`);
|
|
210
|
+
return cases.tool(toolChoice.name);
|
|
211
|
+
});
|
|
212
|
+
const formatContentTypes = (types) => {
|
|
213
|
+
if (types.length <= 1)
|
|
214
|
+
return types[0] ?? "";
|
|
215
|
+
if (types.length === 2)
|
|
216
|
+
return `${types[0]} and ${types[1]}`;
|
|
217
|
+
return `${types.slice(0, -1).join(", ")}, and ${types.at(-1)}`;
|
|
218
|
+
};
|
|
219
|
+
export const supportsContent = (part, types) => types.includes(part.type);
|
|
220
|
+
export const unsupportedContent = (route, role, types) => invalidRequest(`${route} ${role} messages only support ${formatContentTypes(types)} content for now`);
|
|
221
|
+
/**
|
|
222
|
+
* Build a `validate` step from a Schema decoder. Replaces the per-route
|
|
223
|
+
* lambda body `(payload) => decode(payload).pipe(Effect.mapError((e) =>
|
|
224
|
+
* invalid(e.message)))`. Any decode error is translated into
|
|
225
|
+
* `LLMError` carrying the original parse-error message.
|
|
226
|
+
*/
|
|
227
|
+
export const validateWith = (decode) => (payload) => decode(payload).pipe(Effect.mapError((error) => invalidRequest(error.message)));
|
|
228
|
+
/**
|
|
229
|
+
* Build an HTTP POST with a JSON body. Sets `content-type: application/json`
|
|
230
|
+
* automatically after caller-supplied headers so routes cannot accidentally
|
|
231
|
+
* send JSON with a stale content type. The body is passed pre-encoded so
|
|
232
|
+
* routes can choose between
|
|
233
|
+
* `Schema.encodeSync(payload)` and `ProviderShared.encodeJson(payload)`.
|
|
234
|
+
*/
|
|
235
|
+
export const jsonPost = (input) => HttpClientRequest.post(input.url).pipe(HttpClientRequest.setHeaders(Headers.set(Headers.fromInput(input.headers), "content-type", "application/json")), HttpClientRequest.bodyText(input.body, "application/json"));
|
|
236
|
+
export * as ProviderShared from "./shared";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Auth } from "../../route/auth";
|
|
2
|
+
/**
|
|
3
|
+
* AWS credentials for SigV4 signing. Bedrock also supports Bearer API key auth,
|
|
4
|
+
* which provider facades configure as route auth instead of SigV4. STS-vended
|
|
5
|
+
* credentials should be refreshed by the consumer (rebuild the model) before
|
|
6
|
+
* they expire; the route does not refresh.
|
|
7
|
+
*/
|
|
8
|
+
export interface Credentials {
|
|
9
|
+
readonly region: string;
|
|
10
|
+
readonly accessKeyId: string;
|
|
11
|
+
readonly secretAccessKey: string;
|
|
12
|
+
readonly sessionToken?: string;
|
|
13
|
+
}
|
|
14
|
+
/** Sign the exact JSON bytes with SigV4 using credentials configured on the route. */
|
|
15
|
+
export declare const sigV4: (credentials: Credentials | undefined) => Auth.Definition;
|
|
16
|
+
/** Bedrock route auth defaults to SigV4 and expects credentials from route configuration. */
|
|
17
|
+
export declare const auth: Auth.Definition;
|
|
18
|
+
export * as BedrockAuth from "./bedrock-auth";
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { AwsV4Signer } from "aws4fetch";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Headers } from "effect/unstable/http";
|
|
4
|
+
import { Auth } from "../../route/auth";
|
|
5
|
+
import { ProviderShared } from "../shared";
|
|
6
|
+
const signRequest = (input) => Effect.tryPromise({
|
|
7
|
+
try: async () => {
|
|
8
|
+
const signed = await new AwsV4Signer({
|
|
9
|
+
url: input.url,
|
|
10
|
+
method: "POST",
|
|
11
|
+
headers: Object.entries(input.headers),
|
|
12
|
+
body: input.body,
|
|
13
|
+
region: input.credentials.region,
|
|
14
|
+
accessKeyId: input.credentials.accessKeyId,
|
|
15
|
+
secretAccessKey: input.credentials.secretAccessKey,
|
|
16
|
+
sessionToken: input.credentials.sessionToken,
|
|
17
|
+
service: "bedrock",
|
|
18
|
+
}).sign();
|
|
19
|
+
return Object.fromEntries(signed.headers.entries());
|
|
20
|
+
},
|
|
21
|
+
catch: (error) => ProviderShared.invalidRequest(`Bedrock Converse SigV4 signing failed: ${error instanceof Error ? error.message : String(error)}`),
|
|
22
|
+
});
|
|
23
|
+
/** Sign the exact JSON bytes with SigV4 using credentials configured on the route. */
|
|
24
|
+
export const sigV4 = (credentials) => Auth.custom((input) => {
|
|
25
|
+
return Effect.gen(function* () {
|
|
26
|
+
if (!credentials) {
|
|
27
|
+
return yield* ProviderShared.invalidRequest("Bedrock Converse requires either route bearer auth or AWS credentials configured on the route");
|
|
28
|
+
}
|
|
29
|
+
const headersForSigning = Headers.set(input.headers, "content-type", "application/json");
|
|
30
|
+
const signed = yield* signRequest({
|
|
31
|
+
url: input.url,
|
|
32
|
+
body: input.body,
|
|
33
|
+
headers: headersForSigning,
|
|
34
|
+
credentials,
|
|
35
|
+
});
|
|
36
|
+
return Headers.setAll(headersForSigning, signed);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
/** Bedrock route auth defaults to SigV4 and expects credentials from route configuration. */
|
|
40
|
+
export const auth = sigV4(undefined);
|
|
41
|
+
export * as BedrockAuth from "./bedrock-auth";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { CacheHint } from "../../schema";
|
|
3
|
+
import { type Breakpoints } from "./cache";
|
|
4
|
+
export declare const CachePointBlock: Schema.Struct<{
|
|
5
|
+
readonly cachePoint: Schema.Struct<{
|
|
6
|
+
readonly type: Schema.tag<"default">;
|
|
7
|
+
readonly ttl: Schema.optional<Schema.Literals<readonly ["5m", "1h"]>>;
|
|
8
|
+
}>;
|
|
9
|
+
}>;
|
|
10
|
+
export type CachePointBlock = Schema.Schema.Type<typeof CachePointBlock>;
|
|
11
|
+
export declare const BEDROCK_BREAKPOINT_CAP = 4;
|
|
12
|
+
export type { Breakpoints } from "./cache";
|
|
13
|
+
export declare const breakpoints: () => Breakpoints;
|
|
14
|
+
export declare const block: (breakpoints: Breakpoints, cache: CacheHint | undefined) => CachePointBlock | undefined;
|
|
15
|
+
export * as BedrockCache from "./bedrock-cache";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { newBreakpoints, ttlBucket } from "./cache";
|
|
3
|
+
// Bedrock cache markers are positional: emit a `cachePoint` block immediately
|
|
4
|
+
// after the content the caller wants treated as a cacheable prefix. Bedrock
|
|
5
|
+
// accepts optional `ttl: "5m" | "1h"` on cachePoint, mirroring Anthropic.
|
|
6
|
+
export const CachePointBlock = Schema.Struct({
|
|
7
|
+
cachePoint: Schema.Struct({
|
|
8
|
+
type: Schema.tag("default"),
|
|
9
|
+
ttl: Schema.optional(Schema.Literals(["5m", "1h"])),
|
|
10
|
+
}),
|
|
11
|
+
});
|
|
12
|
+
// Bedrock-Claude enforces the same 4-breakpoint cap as the Anthropic Messages
|
|
13
|
+
// API. Callers pass a shared counter through every `block()` call site so the
|
|
14
|
+
// budget is respected across `system`, `messages`, and `tools`.
|
|
15
|
+
export const BEDROCK_BREAKPOINT_CAP = 4;
|
|
16
|
+
export const breakpoints = () => newBreakpoints(BEDROCK_BREAKPOINT_CAP);
|
|
17
|
+
const DEFAULT_5M = { cachePoint: { type: "default" } };
|
|
18
|
+
const DEFAULT_1H = { cachePoint: { type: "default", ttl: "1h" } };
|
|
19
|
+
export const block = (breakpoints, cache) => {
|
|
20
|
+
if (cache?.type !== "ephemeral" && cache?.type !== "persistent")
|
|
21
|
+
return undefined;
|
|
22
|
+
if (breakpoints.remaining <= 0) {
|
|
23
|
+
breakpoints.dropped += 1;
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
breakpoints.remaining -= 1;
|
|
27
|
+
return ttlBucket(cache.ttlSeconds) === "1h" ? DEFAULT_1H : DEFAULT_5M;
|
|
28
|
+
};
|
|
29
|
+
export * as BedrockCache from "./bedrock-cache";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
export declare const ImageFormat: Schema.Literals<readonly ["png", "jpeg", "gif", "webp"]>;
|
|
3
|
+
export type ImageFormat = Schema.Schema.Type<typeof ImageFormat>;
|
|
4
|
+
export declare const ImageBlock: Schema.Struct<{
|
|
5
|
+
readonly image: Schema.Struct<{
|
|
6
|
+
readonly format: Schema.Literals<readonly ["png", "jpeg", "gif", "webp"]>;
|
|
7
|
+
readonly source: Schema.Struct<{
|
|
8
|
+
readonly bytes: Schema.String;
|
|
9
|
+
}>;
|
|
10
|
+
}>;
|
|
11
|
+
}>;
|
|
12
|
+
export type ImageBlock = Schema.Schema.Type<typeof ImageBlock>;
|
|
13
|
+
export declare const DocumentFormat: Schema.Literals<readonly ["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"]>;
|
|
14
|
+
export type DocumentFormat = Schema.Schema.Type<typeof DocumentFormat>;
|
|
15
|
+
export declare const DocumentBlock: Schema.Struct<{
|
|
16
|
+
readonly document: Schema.Struct<{
|
|
17
|
+
readonly format: Schema.Literals<readonly ["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"]>;
|
|
18
|
+
readonly name: Schema.String;
|
|
19
|
+
readonly source: Schema.Struct<{
|
|
20
|
+
readonly bytes: Schema.String;
|
|
21
|
+
}>;
|
|
22
|
+
}>;
|
|
23
|
+
}>;
|
|
24
|
+
export type DocumentBlock = Schema.Schema.Type<typeof DocumentBlock>;
|
|
25
|
+
export declare const lower: (part: {
|
|
26
|
+
readonly type: "media";
|
|
27
|
+
readonly mediaType: string;
|
|
28
|
+
readonly data: string | Uint8Array<ArrayBufferLike>;
|
|
29
|
+
readonly filename?: string | undefined;
|
|
30
|
+
readonly metadata?: {
|
|
31
|
+
readonly [x: string]: unknown;
|
|
32
|
+
} | undefined;
|
|
33
|
+
}) => Effect.Effect<{
|
|
34
|
+
readonly document: {
|
|
35
|
+
readonly format: "pdf" | "csv" | "doc" | "docx" | "xls" | "xlsx" | "html" | "txt" | "md";
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly source: {
|
|
38
|
+
readonly bytes: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
} | {
|
|
42
|
+
image: {
|
|
43
|
+
format: "png" | "jpeg" | "gif" | "webp";
|
|
44
|
+
source: {
|
|
45
|
+
bytes: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}, import("../..").LLMError, never>;
|
|
49
|
+
export * as BedrockMedia from "./bedrock-media";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { ProviderShared } from "../shared";
|
|
3
|
+
// Bedrock Converse accepts image `format` as the file extension and
|
|
4
|
+
// `source.bytes` as base64 in the JSON wire format.
|
|
5
|
+
export const ImageFormat = Schema.Literals(["png", "jpeg", "gif", "webp"]);
|
|
6
|
+
export const ImageBlock = Schema.Struct({
|
|
7
|
+
image: Schema.Struct({
|
|
8
|
+
format: ImageFormat,
|
|
9
|
+
source: Schema.Struct({ bytes: Schema.String }),
|
|
10
|
+
}),
|
|
11
|
+
});
|
|
12
|
+
// Bedrock document blocks require a user-facing name so the model can refer to
|
|
13
|
+
// the uploaded document.
|
|
14
|
+
export const DocumentFormat = Schema.Literals(["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"]);
|
|
15
|
+
export const DocumentBlock = Schema.Struct({
|
|
16
|
+
document: Schema.Struct({
|
|
17
|
+
format: DocumentFormat,
|
|
18
|
+
name: Schema.String,
|
|
19
|
+
source: Schema.Struct({ bytes: Schema.String }),
|
|
20
|
+
}),
|
|
21
|
+
});
|
|
22
|
+
const IMAGE_FORMATS = {
|
|
23
|
+
"image/png": "png",
|
|
24
|
+
"image/jpeg": "jpeg",
|
|
25
|
+
"image/jpg": "jpeg",
|
|
26
|
+
"image/gif": "gif",
|
|
27
|
+
"image/webp": "webp",
|
|
28
|
+
};
|
|
29
|
+
const DOCUMENT_FORMATS = {
|
|
30
|
+
"application/pdf": "pdf",
|
|
31
|
+
"text/csv": "csv",
|
|
32
|
+
"application/msword": "doc",
|
|
33
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
34
|
+
"application/vnd.ms-excel": "xls",
|
|
35
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
36
|
+
"text/html": "html",
|
|
37
|
+
"text/plain": "txt",
|
|
38
|
+
"text/markdown": "md",
|
|
39
|
+
};
|
|
40
|
+
const documentBlock = (part, format, bytes) => ({
|
|
41
|
+
document: {
|
|
42
|
+
format,
|
|
43
|
+
name: part.filename ?? `document.${format}`,
|
|
44
|
+
source: { bytes },
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
// Route by MIME. Known image/document formats lower into a typed block; anything
|
|
48
|
+
// else fails with a clear error instead of silently degrading to a malformed
|
|
49
|
+
// document block. Image MIME types not in `IMAGE_FORMATS` (e.g. `image/svg+xml`)
|
|
50
|
+
// get an image-specific error so the caller knows it's a format-support issue,
|
|
51
|
+
// not a kind-detection issue.
|
|
52
|
+
export const lower = Effect.fn("BedrockMedia.lower")(function* (part) {
|
|
53
|
+
const mime = part.mediaType.toLowerCase();
|
|
54
|
+
const imageFormat = IMAGE_FORMATS[mime];
|
|
55
|
+
if (imageFormat) {
|
|
56
|
+
const media = yield* ProviderShared.validateMedia("Bedrock Converse", part, new Set(Object.keys(IMAGE_FORMATS)));
|
|
57
|
+
return { image: { format: imageFormat, source: { bytes: media.base64 } } };
|
|
58
|
+
}
|
|
59
|
+
if (mime.startsWith("image/"))
|
|
60
|
+
return yield* ProviderShared.invalidRequest(`Bedrock Converse does not support image media type ${part.mediaType}`);
|
|
61
|
+
const documentFormat = DOCUMENT_FORMATS[mime];
|
|
62
|
+
if (documentFormat) {
|
|
63
|
+
const media = yield* ProviderShared.validateMedia("Bedrock Converse", part, new Set(Object.keys(DOCUMENT_FORMATS)));
|
|
64
|
+
return documentBlock(part, documentFormat, media.base64);
|
|
65
|
+
}
|
|
66
|
+
return yield* ProviderShared.invalidRequest(`Bedrock Converse does not support media type ${part.mediaType}`);
|
|
67
|
+
});
|
|
68
|
+
export * as BedrockMedia from "./bedrock-media";
|