@effect/ai-amazon-bedrock 0.1.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/AmazonBedrockClient/package.json +6 -0
- package/AmazonBedrockConfig/package.json +6 -0
- package/AmazonBedrockLanguageModel/package.json +6 -0
- package/AmazonBedrockSchema/package.json +6 -0
- package/EventStreamEncoding/package.json +6 -0
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cjs/AmazonBedrockClient.js +255 -0
- package/dist/cjs/AmazonBedrockClient.js.map +1 -0
- package/dist/cjs/AmazonBedrockConfig.js +30 -0
- package/dist/cjs/AmazonBedrockConfig.js.map +1 -0
- package/dist/cjs/AmazonBedrockLanguageModel.js +507 -0
- package/dist/cjs/AmazonBedrockLanguageModel.js.map +1 -0
- package/dist/cjs/AmazonBedrockSchema.js +745 -0
- package/dist/cjs/AmazonBedrockSchema.js.map +1 -0
- package/dist/cjs/EventStreamEncoding.js +85 -0
- package/dist/cjs/EventStreamEncoding.js.map +1 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/internal/utilities.js +25 -0
- package/dist/cjs/internal/utilities.js.map +1 -0
- package/dist/dts/AmazonBedrockClient.d.ts +95 -0
- package/dist/dts/AmazonBedrockClient.d.ts.map +1 -0
- package/dist/dts/AmazonBedrockConfig.d.ts +47 -0
- package/dist/dts/AmazonBedrockConfig.d.ts.map +1 -0
- package/dist/dts/AmazonBedrockLanguageModel.d.ts +95 -0
- package/dist/dts/AmazonBedrockLanguageModel.d.ts.map +1 -0
- package/dist/dts/AmazonBedrockSchema.d.ts +1434 -0
- package/dist/dts/AmazonBedrockSchema.d.ts.map +1 -0
- package/dist/dts/EventStreamEncoding.d.ts +16 -0
- package/dist/dts/EventStreamEncoding.d.ts.map +1 -0
- package/dist/dts/index.d.ts +21 -0
- package/dist/dts/index.d.ts.map +1 -0
- package/dist/dts/internal/utilities.d.ts +2 -0
- package/dist/dts/internal/utilities.d.ts.map +1 -0
- package/dist/esm/AmazonBedrockClient.js +242 -0
- package/dist/esm/AmazonBedrockClient.js.map +1 -0
- package/dist/esm/AmazonBedrockConfig.js +22 -0
- package/dist/esm/AmazonBedrockConfig.js.map +1 -0
- package/dist/esm/AmazonBedrockLanguageModel.js +495 -0
- package/dist/esm/AmazonBedrockLanguageModel.js.map +1 -0
- package/dist/esm/AmazonBedrockSchema.js +675 -0
- package/dist/esm/AmazonBedrockSchema.js.map +1 -0
- package/dist/esm/EventStreamEncoding.js +76 -0
- package/dist/esm/EventStreamEncoding.js.map +1 -0
- package/dist/esm/index.js +21 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/utilities.js +17 -0
- package/dist/esm/internal/utilities.js.map +1 -0
- package/dist/esm/package.json +4 -0
- package/index/package.json +6 -0
- package/package.json +91 -0
- package/src/AmazonBedrockClient.ts +409 -0
- package/src/AmazonBedrockConfig.ts +78 -0
- package/src/AmazonBedrockLanguageModel.ts +623 -0
- package/src/AmazonBedrockSchema.ts +1038 -0
- package/src/EventStreamEncoding.ts +111 -0
- package/src/index.ts +24 -0
- package/src/internal/utilities.ts +21 -0
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as AiInput from "@effect/ai/AiInput"
|
|
5
|
+
import * as AiResponse from "@effect/ai/AiResponse"
|
|
6
|
+
import * as Headers from "@effect/platform/Headers"
|
|
7
|
+
import type * as HttpBody from "@effect/platform/HttpBody"
|
|
8
|
+
import * as HttpClient from "@effect/platform/HttpClient"
|
|
9
|
+
import * as HttpClientError from "@effect/platform/HttpClientError"
|
|
10
|
+
import * as HttpClientRequest from "@effect/platform/HttpClientRequest"
|
|
11
|
+
import * as HttpClientResponse from "@effect/platform/HttpClientResponse"
|
|
12
|
+
import { AwsV4Signer } from "aws4fetch"
|
|
13
|
+
import * as Arr from "effect/Array"
|
|
14
|
+
import * as Config from "effect/Config"
|
|
15
|
+
import type { ConfigError } from "effect/ConfigError"
|
|
16
|
+
import * as Context from "effect/Context"
|
|
17
|
+
import * as Data from "effect/Data"
|
|
18
|
+
import * as Effect from "effect/Effect"
|
|
19
|
+
import { identity } from "effect/Function"
|
|
20
|
+
import * as Layer from "effect/Layer"
|
|
21
|
+
import * as Option from "effect/Option"
|
|
22
|
+
import type { ParseError } from "effect/ParseResult"
|
|
23
|
+
import * as Predicate from "effect/Predicate"
|
|
24
|
+
import * as Redacted from "effect/Redacted"
|
|
25
|
+
import * as Stream from "effect/Stream"
|
|
26
|
+
import type { Mutable } from "effect/Types"
|
|
27
|
+
import { AmazonBedrockConfig } from "./AmazonBedrockConfig.js"
|
|
28
|
+
import type { ProviderMetadata } from "./AmazonBedrockLanguageModel.js"
|
|
29
|
+
import type { ConverseRequest } from "./AmazonBedrockSchema.js"
|
|
30
|
+
import { ConverseResponse, ConverseStreamResponse } from "./AmazonBedrockSchema.js"
|
|
31
|
+
import * as EventStreamEncoding from "./EventStreamEncoding.js"
|
|
32
|
+
import * as InternalUtilities from "./internal/utilities.js"
|
|
33
|
+
|
|
34
|
+
const constDisableValidation = { disableValidation: true } as const
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
* @category tags
|
|
39
|
+
*/
|
|
40
|
+
export class AmazonBedrockClient extends Context.Tag(
|
|
41
|
+
"@effect/ai-amazon-bedrock/AmazonBedrockClient"
|
|
42
|
+
)<AmazonBedrockClient, AmazonBedrockClient.Service>() {}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
*/
|
|
47
|
+
export declare namespace AmazonBedrockClient {
|
|
48
|
+
/**
|
|
49
|
+
* @since 1.0.0
|
|
50
|
+
* @category models
|
|
51
|
+
*/
|
|
52
|
+
export interface Service {
|
|
53
|
+
readonly client: Client
|
|
54
|
+
readonly streamRequest: (request: typeof ConverseRequest.Encoded) => Stream.Stream<
|
|
55
|
+
ConverseStreamResponse,
|
|
56
|
+
HttpClientError.HttpClientError | ParseError
|
|
57
|
+
>
|
|
58
|
+
readonly stream: (request: typeof ConverseRequest.Encoded) => Stream.Stream<
|
|
59
|
+
AiResponse.AiResponse,
|
|
60
|
+
HttpClientError.HttpClientError | ParseError
|
|
61
|
+
>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @since 1.0.0
|
|
66
|
+
* @category models
|
|
67
|
+
*/
|
|
68
|
+
export interface Client {
|
|
69
|
+
readonly converse: (
|
|
70
|
+
options: typeof ConverseRequest.Encoded
|
|
71
|
+
) => Effect.Effect<typeof ConverseResponse.Type, HttpClientError.HttpClientError | ParseError>
|
|
72
|
+
readonly converseStream: (
|
|
73
|
+
options: typeof ConverseRequest.Encoded
|
|
74
|
+
) => Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError | ParseError>
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @since 1.0.0
|
|
80
|
+
* @category constructors
|
|
81
|
+
*/
|
|
82
|
+
export const make = (options: {
|
|
83
|
+
readonly apiUrl?: string | undefined
|
|
84
|
+
readonly accessKeyId: string
|
|
85
|
+
readonly secretAccessKey: Redacted.Redacted<string>
|
|
86
|
+
readonly sessionToken?: Redacted.Redacted<string> | undefined
|
|
87
|
+
readonly region?: string | undefined
|
|
88
|
+
readonly transformClient?: (
|
|
89
|
+
client: HttpClient.HttpClient
|
|
90
|
+
) => HttpClient.HttpClient
|
|
91
|
+
}) =>
|
|
92
|
+
Effect.gen(function*() {
|
|
93
|
+
const region = options.region ?? "us-east-1"
|
|
94
|
+
|
|
95
|
+
const redactedHeaders = ["X-Amz-Security-Token"]
|
|
96
|
+
|
|
97
|
+
yield* Effect.locallyScopedWith(Headers.currentRedactedNames, Arr.appendAll(redactedHeaders))
|
|
98
|
+
|
|
99
|
+
const httpClient = (yield* HttpClient.HttpClient).pipe(
|
|
100
|
+
HttpClient.mapRequest((request) =>
|
|
101
|
+
request.pipe(
|
|
102
|
+
HttpClientRequest.prependUrl(options.apiUrl ?? `https://bedrock-runtime.${region}.amazonaws.com`),
|
|
103
|
+
HttpClientRequest.acceptJson
|
|
104
|
+
)
|
|
105
|
+
),
|
|
106
|
+
HttpClient.mapRequestEffect(Effect.fnUntraced(function*(request) {
|
|
107
|
+
const originalHeaders = request.headers
|
|
108
|
+
const signer = new AwsV4Signer({
|
|
109
|
+
service: "bedrock",
|
|
110
|
+
url: request.url,
|
|
111
|
+
method: request.method,
|
|
112
|
+
headers: Object.entries(originalHeaders),
|
|
113
|
+
body: prepareBody(request.body),
|
|
114
|
+
region,
|
|
115
|
+
accessKeyId: options.accessKeyId,
|
|
116
|
+
secretAccessKey: Redacted.value(options.secretAccessKey),
|
|
117
|
+
...(options.sessionToken ? { sessionToken: Redacted.value(options.sessionToken) } : {})
|
|
118
|
+
})
|
|
119
|
+
const { headers: signedHeaders } = yield* Effect.promise(() => signer.sign())
|
|
120
|
+
const headers = Headers.merge(originalHeaders, Headers.fromInput(signedHeaders))
|
|
121
|
+
return HttpClientRequest.setHeaders(request, headers)
|
|
122
|
+
})),
|
|
123
|
+
options.transformClient ? options.transformClient : identity
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
const client = makeClient(httpClient, {
|
|
127
|
+
transformClient: (client) =>
|
|
128
|
+
AmazonBedrockConfig.getOrUndefined.pipe(
|
|
129
|
+
Effect.map((config) => config?.transformClient ? config.transformClient(client) : client)
|
|
130
|
+
)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const streamRequest = (request: typeof ConverseRequest.Encoded) =>
|
|
134
|
+
client.converseStream(request).pipe(
|
|
135
|
+
Effect.map((response) => response.stream),
|
|
136
|
+
Stream.unwrap,
|
|
137
|
+
Stream.pipeThroughChannel(EventStreamEncoding.makeChannel(ConverseStreamResponse))
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
const stream = (request: typeof ConverseRequest.Encoded) =>
|
|
141
|
+
Stream.suspend(() => {
|
|
142
|
+
const toolCalls = {} as Record<number, RawToolCall>
|
|
143
|
+
let reasoningText: string | undefined = undefined
|
|
144
|
+
let usage: AiResponse.Usage = {
|
|
145
|
+
inputTokens: 0,
|
|
146
|
+
outputTokens: 0,
|
|
147
|
+
totalTokens: 0,
|
|
148
|
+
reasoningTokens: 0,
|
|
149
|
+
cacheReadInputTokens: 0,
|
|
150
|
+
cacheWriteInputTokens: 0
|
|
151
|
+
}
|
|
152
|
+
const metadata: Mutable<ProviderMetadata.Service> = {} as any
|
|
153
|
+
return streamRequest(request).pipe(
|
|
154
|
+
Stream.filterMap((response) => {
|
|
155
|
+
const parts: Array<AiResponse.Part> = []
|
|
156
|
+
|
|
157
|
+
if (Predicate.isNotUndefined(response.metadata)) {
|
|
158
|
+
usage = {
|
|
159
|
+
inputTokens: response.metadata.usage.inputTokens,
|
|
160
|
+
outputTokens: response.metadata.usage.outputTokens,
|
|
161
|
+
totalTokens: response.metadata.usage.totalTokens,
|
|
162
|
+
reasoningTokens: 0,
|
|
163
|
+
cacheReadInputTokens: response.metadata.usage.cacheReadInputTokens ?? 0,
|
|
164
|
+
cacheWriteInputTokens: response.metadata.usage.cacheWriteInputTokens ?? 0
|
|
165
|
+
}
|
|
166
|
+
metadata.metrics = response.metadata.metrics
|
|
167
|
+
if (Predicate.isNotUndefined(response.metadata.trace)) {
|
|
168
|
+
metadata.trace = response.metadata.trace
|
|
169
|
+
}
|
|
170
|
+
if (Predicate.isNotUndefined(response.metadata.performanceConfig)) {
|
|
171
|
+
metadata.performanceConfig = response.metadata.performanceConfig
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (Predicate.isNotUndefined(response.messageStart)) {
|
|
176
|
+
parts.push(
|
|
177
|
+
new AiResponse.MetadataPart({
|
|
178
|
+
model: request.modelId
|
|
179
|
+
}, constDisableValidation)
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (Predicate.isNotUndefined(response.contentBlockStart)) {
|
|
184
|
+
const index = response.contentBlockStart.contentBlockIndex
|
|
185
|
+
const toolUse = response.contentBlockStart.start.toolUse
|
|
186
|
+
toolCalls[index] = {
|
|
187
|
+
id: toolUse.toolUseId,
|
|
188
|
+
name: toolUse.name,
|
|
189
|
+
params: ""
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (Predicate.isNotUndefined(response.contentBlockDelta)) {
|
|
194
|
+
const delta = response.contentBlockDelta.delta
|
|
195
|
+
|
|
196
|
+
if ("text" in delta) {
|
|
197
|
+
parts.push(
|
|
198
|
+
new AiResponse.TextPart({
|
|
199
|
+
text: delta.text
|
|
200
|
+
}, constDisableValidation)
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if ("reasoningContent" in delta) {
|
|
205
|
+
if ("text" in delta.reasoningContent) {
|
|
206
|
+
reasoningText = delta.reasoningContent.text
|
|
207
|
+
}
|
|
208
|
+
if ("signature" in delta.reasoningContent) {
|
|
209
|
+
if (Predicate.isNotUndefined(reasoningText)) {
|
|
210
|
+
parts.push(
|
|
211
|
+
new AiResponse.ReasoningPart({
|
|
212
|
+
reasoningText,
|
|
213
|
+
signature: delta.reasoningContent.signature
|
|
214
|
+
}, constDisableValidation)
|
|
215
|
+
)
|
|
216
|
+
} else {
|
|
217
|
+
throw new Error(
|
|
218
|
+
"[BUG]: AmazonBedrockClient.stream - no reasoning text " +
|
|
219
|
+
`found for signature: ${delta.reasoningContent.signature} ` +
|
|
220
|
+
"- please report an issue at https://github.com/Effect-TS/effect/issues"
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if ("redactedContent" in delta.reasoningContent) {
|
|
225
|
+
const decoder = new TextDecoder()
|
|
226
|
+
parts.push(
|
|
227
|
+
new AiResponse.RedactedReasoningPart({
|
|
228
|
+
redactedText: decoder.decode(delta.reasoningContent.redactedContent)
|
|
229
|
+
}, constDisableValidation)
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if ("toolUse" in delta) {
|
|
235
|
+
const index = response.contentBlockDelta.contentBlockIndex
|
|
236
|
+
const tool = toolCalls[index]
|
|
237
|
+
const toolDelta = delta.toolUse.input
|
|
238
|
+
if (Predicate.isNotUndefined(tool)) {
|
|
239
|
+
tool.params += toolDelta
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (Predicate.isNotUndefined(response.contentBlockStop)) {
|
|
245
|
+
const index = response.contentBlockStop.contentBlockIndex
|
|
246
|
+
const tool = toolCalls[index]
|
|
247
|
+
if (Predicate.isNotUndefined(tool)) {
|
|
248
|
+
try {
|
|
249
|
+
const params = JSON.parse(tool.params)
|
|
250
|
+
parts.push(
|
|
251
|
+
new AiResponse.ToolCallPart({
|
|
252
|
+
id: AiInput.ToolCallId.make(tool.id, constDisableValidation),
|
|
253
|
+
name: tool.name,
|
|
254
|
+
params
|
|
255
|
+
}, constDisableValidation)
|
|
256
|
+
)
|
|
257
|
+
delete toolCalls[index]
|
|
258
|
+
// eslint-disable-next-line no-empty
|
|
259
|
+
} catch {}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (Predicate.isNotUndefined(response.messageStop)) {
|
|
264
|
+
parts.push(
|
|
265
|
+
new AiResponse.FinishPart({
|
|
266
|
+
usage,
|
|
267
|
+
reason: InternalUtilities.resolveFinishReason(response.messageStop.stopReason),
|
|
268
|
+
providerMetadata: { [InternalUtilities.ProviderMetadataKey]: metadata }
|
|
269
|
+
}, constDisableValidation)
|
|
270
|
+
)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return parts.length === 0
|
|
274
|
+
? Option.none()
|
|
275
|
+
: Option.some(AiResponse.AiResponse.make({ parts }, constDisableValidation))
|
|
276
|
+
})
|
|
277
|
+
)
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
return AmazonBedrockClient.of({ client, stream, streamRequest })
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* @since 1.0.0
|
|
285
|
+
* @category layers
|
|
286
|
+
*/
|
|
287
|
+
export const layer = (options: {
|
|
288
|
+
readonly apiUrl?: string | undefined
|
|
289
|
+
readonly accessKeyId: string
|
|
290
|
+
readonly secretAccessKey: Redacted.Redacted<string>
|
|
291
|
+
readonly sessionToken?: Redacted.Redacted<string> | undefined
|
|
292
|
+
readonly region?: string | undefined
|
|
293
|
+
readonly transformClient?: (
|
|
294
|
+
client: HttpClient.HttpClient
|
|
295
|
+
) => HttpClient.HttpClient
|
|
296
|
+
}): Layer.Layer<AmazonBedrockClient, never, HttpClient.HttpClient> => Layer.scoped(AmazonBedrockClient, make(options))
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* @since 1.0.0
|
|
300
|
+
* @category layers
|
|
301
|
+
*/
|
|
302
|
+
export const layerConfig = (
|
|
303
|
+
options: Config.Config.Wrap<{
|
|
304
|
+
readonly apiUrl?: string | undefined
|
|
305
|
+
readonly accessKeyId: string
|
|
306
|
+
readonly secretAccessKey: Redacted.Redacted<string>
|
|
307
|
+
readonly sessionToken?: Redacted.Redacted<string> | undefined
|
|
308
|
+
readonly region?: string | undefined
|
|
309
|
+
readonly transformClient?: (
|
|
310
|
+
client: HttpClient.HttpClient
|
|
311
|
+
) => HttpClient.HttpClient
|
|
312
|
+
}>
|
|
313
|
+
): Layer.Layer<AmazonBedrockClient, ConfigError, HttpClient.HttpClient> =>
|
|
314
|
+
Config.unwrap(options).pipe(
|
|
315
|
+
Effect.flatMap(make),
|
|
316
|
+
Layer.scoped(AmazonBedrockClient)
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
const makeClient = (
|
|
320
|
+
httpClient: HttpClient.HttpClient,
|
|
321
|
+
options: {
|
|
322
|
+
readonly transformClient?: ((client: HttpClient.HttpClient) => Effect.Effect<HttpClient.HttpClient>) | undefined
|
|
323
|
+
}
|
|
324
|
+
): AmazonBedrockClient.Client => {
|
|
325
|
+
const unexpectedStatus = (
|
|
326
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
327
|
+
response: HttpClientResponse.HttpClientResponse
|
|
328
|
+
) =>
|
|
329
|
+
Effect.flatMap(
|
|
330
|
+
Effect.orElseSucceed(response.text, () => "Unexpected status code"),
|
|
331
|
+
(description) =>
|
|
332
|
+
Effect.fail(
|
|
333
|
+
new HttpClientError.ResponseError({
|
|
334
|
+
request,
|
|
335
|
+
response,
|
|
336
|
+
reason: "StatusCode",
|
|
337
|
+
description
|
|
338
|
+
})
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
const applyClientTransform = (client: HttpClient.HttpClient): Effect.Effect<HttpClient.HttpClient> =>
|
|
342
|
+
options.transformClient ? options.transformClient(client) : Effect.succeed(client)
|
|
343
|
+
return {
|
|
344
|
+
converse: ({ modelId, ...payload }) =>
|
|
345
|
+
HttpClientRequest.make("POST")(`/model/${modelId}/converse`).pipe(
|
|
346
|
+
(request) => Effect.orDie(HttpClientRequest.bodyJson(request, payload)),
|
|
347
|
+
Effect.flatMap((request) =>
|
|
348
|
+
Effect.flatMap(applyClientTransform(httpClient), (httpClient) =>
|
|
349
|
+
Effect.flatMap(
|
|
350
|
+
httpClient.execute(request),
|
|
351
|
+
HttpClientResponse.matchStatus({
|
|
352
|
+
"200": (response) => HttpClientResponse.schemaBodyJson(ConverseResponse)(response),
|
|
353
|
+
orElse: (response) => unexpectedStatus(request, response)
|
|
354
|
+
})
|
|
355
|
+
))
|
|
356
|
+
)
|
|
357
|
+
),
|
|
358
|
+
converseStream: ({ modelId, ...payload }) =>
|
|
359
|
+
HttpClientRequest.make("POST")(`/model/${modelId}/converse-stream`).pipe(
|
|
360
|
+
(request) => Effect.orDie(HttpClientRequest.bodyJson(request, payload)),
|
|
361
|
+
Effect.flatMap((request) =>
|
|
362
|
+
Effect.flatMap(applyClientTransform(httpClient), (httpClient) =>
|
|
363
|
+
Effect.flatMap(
|
|
364
|
+
httpClient.execute(request),
|
|
365
|
+
HttpClientResponse.filterStatusOk
|
|
366
|
+
))
|
|
367
|
+
)
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* @since 1.0.0
|
|
374
|
+
* @category models
|
|
375
|
+
*/
|
|
376
|
+
export class StreamChunk extends Data.Class<{
|
|
377
|
+
readonly parts: Array<StreamChunkPart>
|
|
378
|
+
}> {}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* @since 1.0.0
|
|
382
|
+
* @category models
|
|
383
|
+
*/
|
|
384
|
+
export type StreamChunkPart = any /* ContentPart | ToolCallPart | UsagePart */
|
|
385
|
+
|
|
386
|
+
const prepareBody = (body: HttpBody.HttpBody): string => {
|
|
387
|
+
switch (body._tag) {
|
|
388
|
+
case "Raw":
|
|
389
|
+
case "Uint8Array": {
|
|
390
|
+
if (typeof body.body === "string") {
|
|
391
|
+
return body.body
|
|
392
|
+
}
|
|
393
|
+
if (body.body instanceof Uint8Array) {
|
|
394
|
+
return new TextDecoder().decode(body.body)
|
|
395
|
+
}
|
|
396
|
+
if (body.body instanceof ArrayBuffer) {
|
|
397
|
+
return new TextDecoder().decode(body.body)
|
|
398
|
+
}
|
|
399
|
+
return JSON.stringify(body.body)
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
throw new Error("Unsupported HttpBody: " + body._tag)
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
type RawToolCall = {
|
|
406
|
+
readonly id: string
|
|
407
|
+
readonly name: string
|
|
408
|
+
params: string
|
|
409
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type { HttpClient } from "@effect/platform/HttpClient"
|
|
5
|
+
import * as Context from "effect/Context"
|
|
6
|
+
import * as Effect from "effect/Effect"
|
|
7
|
+
import { dual } from "effect/Function"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @since 1.0.0
|
|
11
|
+
* @category Context
|
|
12
|
+
*/
|
|
13
|
+
export class AmazonBedrockConfig extends Context.Tag("@effect/ai-google/AmazonBedrockConfig")<
|
|
14
|
+
AmazonBedrockConfig,
|
|
15
|
+
AmazonBedrockConfig.Service
|
|
16
|
+
>() {
|
|
17
|
+
/**
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
static readonly getOrUndefined: Effect.Effect<typeof AmazonBedrockConfig.Service | undefined> = Effect.map(
|
|
21
|
+
Effect.context<never>(),
|
|
22
|
+
(context) => context.unsafeMap.get(AmazonBedrockConfig.key)
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @since 1.0.0
|
|
28
|
+
*/
|
|
29
|
+
export declare namespace AmazonBedrockConfig {
|
|
30
|
+
/**
|
|
31
|
+
* @since 1.0.
|
|
32
|
+
* @category Models
|
|
33
|
+
*/
|
|
34
|
+
export interface Service {
|
|
35
|
+
readonly transformClient?: (client: HttpClient) => HttpClient
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
* @category Configuration
|
|
42
|
+
*/
|
|
43
|
+
export const withClientTransform: {
|
|
44
|
+
/**
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
* @category Configuration
|
|
47
|
+
*/
|
|
48
|
+
(transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
|
|
49
|
+
/**
|
|
50
|
+
* @since 1.0.0
|
|
51
|
+
* @category Configuration
|
|
52
|
+
*/
|
|
53
|
+
<A, E, R>(
|
|
54
|
+
self: Effect.Effect<A, E, R>,
|
|
55
|
+
transform: (client: HttpClient) => HttpClient
|
|
56
|
+
): Effect.Effect<A, E, R>
|
|
57
|
+
} = dual<
|
|
58
|
+
/**
|
|
59
|
+
* @since 1.0.0
|
|
60
|
+
* @category Configuration
|
|
61
|
+
*/
|
|
62
|
+
(transform: (client: HttpClient) => HttpClient) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
|
|
63
|
+
/**
|
|
64
|
+
* @since 1.0.0
|
|
65
|
+
* @category Configuration
|
|
66
|
+
*/
|
|
67
|
+
<A, E, R>(
|
|
68
|
+
self: Effect.Effect<A, E, R>,
|
|
69
|
+
transform: (client: HttpClient) => HttpClient
|
|
70
|
+
) => Effect.Effect<A, E, R>
|
|
71
|
+
>(
|
|
72
|
+
2,
|
|
73
|
+
(self, transformClient) =>
|
|
74
|
+
Effect.flatMap(
|
|
75
|
+
AmazonBedrockConfig.getOrUndefined,
|
|
76
|
+
(config) => Effect.provideService(self, AmazonBedrockConfig, { ...config, transformClient })
|
|
77
|
+
)
|
|
78
|
+
)
|