@effect/ai-amazon-bedrock 0.16.1 → 4.1.4
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/AGENTS.md +434 -0
- package/CLAUDE.md +434 -0
- package/README.md +76 -3
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +28 -0
- package/dist/AmazonBedrockClient.d.ts +227 -0
- package/dist/AmazonBedrockClient.d.ts.map +1 -0
- package/dist/AmazonBedrockClient.js +281 -0
- package/dist/AmazonBedrockClient.js.map +1 -0
- package/dist/AmazonBedrockConfig.d.ts +76 -0
- package/dist/AmazonBedrockConfig.d.ts.map +1 -0
- package/dist/AmazonBedrockConfig.js +41 -0
- package/dist/AmazonBedrockConfig.js.map +1 -0
- package/dist/AmazonBedrockLanguageModel.d.ts +315 -0
- package/dist/AmazonBedrockLanguageModel.d.ts.map +1 -0
- package/dist/AmazonBedrockLanguageModel.js +991 -0
- package/dist/AmazonBedrockLanguageModel.js.map +1 -0
- package/dist/AmazonBedrockSchema.d.ts +918 -0
- package/dist/AmazonBedrockSchema.d.ts.map +1 -0
- package/dist/AmazonBedrockSchema.js +536 -0
- package/dist/AmazonBedrockSchema.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/{esm/index.js → index.js} +13 -12
- package/dist/index.js.map +1 -0
- package/dist/internal/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +185 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/eventStream.d.ts +2 -0
- package/dist/internal/eventStream.d.ts.map +1 -0
- package/dist/internal/eventStream.js +180 -0
- package/dist/internal/eventStream.js.map +1 -0
- package/dist/internal/sigv4.d.ts +2 -0
- package/dist/internal/sigv4.d.ts.map +1 -0
- package/dist/internal/sigv4.js +115 -0
- package/dist/internal/sigv4.js.map +1 -0
- package/dist/{dts/internal → internal}/utilities.d.ts.map +1 -1
- package/dist/internal/utilities.js +17 -0
- package/dist/internal/utilities.js.map +1 -0
- package/package.json +45 -84
- package/src/AmazonBedrockClient.ts +447 -263
- package/src/AmazonBedrockConfig.ts +55 -40
- package/src/AmazonBedrockLanguageModel.ts +950 -929
- package/src/AmazonBedrockSchema.ts +526 -970
- package/src/index.ts +15 -14
- package/src/internal/errors.ts +207 -0
- package/src/internal/eventStream.ts +208 -0
- package/src/internal/sigv4.ts +154 -0
- package/src/internal/utilities.ts +15 -17
- package/AmazonBedrockClient/package.json +0 -6
- package/AmazonBedrockConfig/package.json +0 -6
- package/AmazonBedrockLanguageModel/package.json +0 -6
- package/AmazonBedrockSchema/package.json +0 -6
- package/AmazonBedrockTool/package.json +0 -6
- package/EventStreamEncoding/package.json +0 -6
- package/dist/cjs/AmazonBedrockClient.js +0 -191
- package/dist/cjs/AmazonBedrockClient.js.map +0 -1
- package/dist/cjs/AmazonBedrockConfig.js +0 -30
- package/dist/cjs/AmazonBedrockConfig.js.map +0 -1
- package/dist/cjs/AmazonBedrockLanguageModel.js +0 -1051
- package/dist/cjs/AmazonBedrockLanguageModel.js.map +0 -1
- package/dist/cjs/AmazonBedrockSchema.js +0 -800
- package/dist/cjs/AmazonBedrockSchema.js.map +0 -1
- package/dist/cjs/AmazonBedrockTool.js +0 -53
- package/dist/cjs/AmazonBedrockTool.js.map +0 -1
- package/dist/cjs/EventStreamEncoding.js +0 -86
- package/dist/cjs/EventStreamEncoding.js.map +0 -1
- package/dist/cjs/index.js +0 -20
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/internal/utilities.js +0 -27
- package/dist/cjs/internal/utilities.js.map +0 -1
- package/dist/dts/AmazonBedrockClient.d.ts +0 -95
- package/dist/dts/AmazonBedrockClient.d.ts.map +0 -1
- package/dist/dts/AmazonBedrockConfig.d.ts +0 -47
- package/dist/dts/AmazonBedrockConfig.d.ts.map +0 -1
- package/dist/dts/AmazonBedrockLanguageModel.d.ts +0 -147
- package/dist/dts/AmazonBedrockLanguageModel.d.ts.map +0 -1
- package/dist/dts/AmazonBedrockSchema.d.ts +0 -1875
- package/dist/dts/AmazonBedrockSchema.d.ts.map +0 -1
- package/dist/dts/AmazonBedrockTool.d.ts +0 -206
- package/dist/dts/AmazonBedrockTool.d.ts.map +0 -1
- package/dist/dts/EventStreamEncoding.d.ts +0 -16
- package/dist/dts/EventStreamEncoding.d.ts.map +0 -1
- package/dist/dts/index.d.ts +0 -25
- package/dist/dts/index.d.ts.map +0 -1
- package/dist/esm/AmazonBedrockClient.js +0 -179
- package/dist/esm/AmazonBedrockClient.js.map +0 -1
- package/dist/esm/AmazonBedrockConfig.js +0 -22
- package/dist/esm/AmazonBedrockConfig.js.map +0 -1
- package/dist/esm/AmazonBedrockLanguageModel.js +0 -1040
- package/dist/esm/AmazonBedrockLanguageModel.js.map +0 -1
- package/dist/esm/AmazonBedrockSchema.js +0 -731
- package/dist/esm/AmazonBedrockSchema.js.map +0 -1
- package/dist/esm/AmazonBedrockTool.js +0 -45
- package/dist/esm/AmazonBedrockTool.js.map +0 -1
- package/dist/esm/EventStreamEncoding.js +0 -78
- package/dist/esm/EventStreamEncoding.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/internal/utilities.js +0 -19
- package/dist/esm/internal/utilities.js.map +0 -1
- package/dist/esm/package.json +0 -4
- package/index/package.json +0 -6
- package/src/AmazonBedrockTool.ts +0 -273
- package/src/EventStreamEncoding.ts +0 -118
- /package/dist/{dts/internal → internal}/utilities.d.ts +0 -0
|
@@ -1,316 +1,500 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `AmazonBedrockClient` module defines the low-level Effect service used to
|
|
3
|
+
* call the Amazon Bedrock Runtime Converse API. It wraps an `HttpClient` with
|
|
4
|
+
* AWS Signature Version 4 signing or Bedrock API key authentication, request
|
|
5
|
+
* defaults, response decoding, event stream decoding and error mapping to the
|
|
6
|
+
* unified `AiError` type.
|
|
7
|
+
*
|
|
8
|
+
* **Mental model**
|
|
9
|
+
*
|
|
10
|
+
* `HttpClient.HttpClient` provides the transport, and the `Digest` and `Mac`
|
|
11
|
+
* services of `effect/crypto` provide the SHA-256 and HMAC that SigV4 signing
|
|
12
|
+
* runs on. {@link make} turns {@link Options} into an
|
|
13
|
+
* {@link AmazonBedrockClient} service, while {@link layer} and
|
|
14
|
+
* {@link layerConfig} provide that service as a layer. The service exposes
|
|
15
|
+
* helpers for the `converse` and `converse-stream` endpoints.
|
|
16
|
+
*
|
|
17
|
+
* @stability unstable
|
|
18
|
+
* @since 4.0.0
|
|
3
19
|
*/
|
|
4
|
-
import * as AiError from "
|
|
5
|
-
import * as
|
|
6
|
-
import * as HttpBody from "@effect/platform/HttpBody"
|
|
7
|
-
import * as HttpClient from "@effect/platform/HttpClient"
|
|
8
|
-
import * as HttpClientError from "@effect/platform/HttpClientError"
|
|
9
|
-
import * as HttpClientRequest from "@effect/platform/HttpClientRequest"
|
|
10
|
-
import * as HttpClientResponse from "@effect/platform/HttpClientResponse"
|
|
11
|
-
import { AwsV4Signer } from "aws4fetch"
|
|
12
|
-
import * as Arr from "effect/Array"
|
|
20
|
+
import * as AiError from "effect/ai/AiError"
|
|
21
|
+
import * as Array from "effect/Array"
|
|
13
22
|
import * as Config from "effect/Config"
|
|
14
|
-
import type { ConfigError } from "effect/ConfigError"
|
|
15
23
|
import * as Context from "effect/Context"
|
|
24
|
+
import * as Digest from "effect/crypto/Digest"
|
|
25
|
+
import * as Mac from "effect/crypto/Mac"
|
|
16
26
|
import * as Effect from "effect/Effect"
|
|
17
27
|
import { identity } from "effect/Function"
|
|
28
|
+
import * as Headers from "effect/http/Headers"
|
|
29
|
+
import * as HttpBody from "effect/http/HttpBody"
|
|
30
|
+
import * as HttpClient from "effect/http/HttpClient"
|
|
31
|
+
import type * as HttpClientError from "effect/http/HttpClientError"
|
|
32
|
+
import * as HttpClientRequest from "effect/http/HttpClientRequest"
|
|
33
|
+
import * as HttpClientResponse from "effect/http/HttpClientResponse"
|
|
18
34
|
import * as Layer from "effect/Layer"
|
|
19
|
-
import
|
|
35
|
+
import * as Predicate from "effect/Predicate"
|
|
20
36
|
import * as Redacted from "effect/Redacted"
|
|
21
|
-
import
|
|
37
|
+
import * as Schema from "effect/Schema"
|
|
22
38
|
import * as Stream from "effect/Stream"
|
|
23
|
-
import { AmazonBedrockConfig } from "./AmazonBedrockConfig.
|
|
24
|
-
import type { ConverseRequest } from "./AmazonBedrockSchema.
|
|
25
|
-
import { ConverseResponse,
|
|
26
|
-
import * as
|
|
39
|
+
import { AmazonBedrockConfig } from "./AmazonBedrockConfig.ts"
|
|
40
|
+
import type { ConverseRequest } from "./AmazonBedrockSchema.ts"
|
|
41
|
+
import { ConverseResponse, ConverseStreamEvent, ConverseStreamException } from "./AmazonBedrockSchema.ts"
|
|
42
|
+
import * as Errors from "./internal/errors.ts"
|
|
43
|
+
import * as EventStream from "./internal/eventStream.ts"
|
|
44
|
+
import * as SigV4 from "./internal/sigv4.ts"
|
|
45
|
+
|
|
46
|
+
// =============================================================================
|
|
47
|
+
// Service Interface
|
|
48
|
+
// =============================================================================
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Represents the Amazon Bedrock client service with methods for the Converse
|
|
52
|
+
* API, including regular and streaming message creation.
|
|
53
|
+
*
|
|
54
|
+
* @stability unstable
|
|
55
|
+
* @category services
|
|
56
|
+
* @since 4.0.0
|
|
57
|
+
*/
|
|
58
|
+
export interface Service {
|
|
59
|
+
/**
|
|
60
|
+
* The underlying `HttpClient`, with the base URL and authentication
|
|
61
|
+
* applied, for Bedrock Runtime operations this service does not model.
|
|
62
|
+
* Resolving the credentials can fail with an `AiError`.
|
|
63
|
+
*/
|
|
64
|
+
readonly client: HttpClient.HttpClient.With<HttpClientError.HttpClientError | AiError.AiError>
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Calls the Converse operation and maps all errors to the unified `AiError`
|
|
68
|
+
* type. `modelId` names the model in the URL; the other fields form the
|
|
69
|
+
* request body.
|
|
70
|
+
*/
|
|
71
|
+
readonly converse: (
|
|
72
|
+
request: typeof ConverseRequest.Encoded
|
|
73
|
+
) => Effect.Effect<ConverseResponse, AiError.AiError>
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Calls the ConverseStream operation and decodes the event stream, mapping
|
|
77
|
+
* all errors to the unified `AiError` type. An exception frame in the
|
|
78
|
+
* stream, such as a `throttlingException`, fails the stream.
|
|
79
|
+
*/
|
|
80
|
+
readonly converseStream: (
|
|
81
|
+
request: typeof ConverseRequest.Encoded
|
|
82
|
+
) => Stream.Stream<ConverseStreamEvent, AiError.AiError>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// =============================================================================
|
|
86
|
+
// Service Identifier
|
|
87
|
+
// =============================================================================
|
|
27
88
|
|
|
28
89
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
90
|
+
* Service tag for the Amazon Bedrock client.
|
|
91
|
+
*
|
|
92
|
+
* @see {@link make} for constructing a client effectfully
|
|
93
|
+
* @see {@link layer} for providing a client from explicit options
|
|
94
|
+
* @see {@link layerConfig} for providing a client from `Config`
|
|
95
|
+
*
|
|
96
|
+
* @stability unstable
|
|
97
|
+
* @category services
|
|
98
|
+
* @since 4.0.0
|
|
31
99
|
*/
|
|
32
|
-
export class AmazonBedrockClient extends Context.
|
|
100
|
+
export class AmazonBedrockClient extends Context.Service<AmazonBedrockClient, Service>()(
|
|
33
101
|
"@effect/ai-amazon-bedrock/AmazonBedrockClient"
|
|
34
|
-
)
|
|
102
|
+
) {}
|
|
103
|
+
|
|
104
|
+
// =============================================================================
|
|
105
|
+
// Options
|
|
106
|
+
// =============================================================================
|
|
35
107
|
|
|
36
108
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
109
|
+
* AWS credentials, in the shape of the AWS SDK's `AwsCredentialIdentity`, so
|
|
110
|
+
* the SDK's credential providers fit without an adapter.
|
|
111
|
+
*
|
|
112
|
+
* @stability unstable
|
|
113
|
+
* @category models
|
|
114
|
+
* @since 4.0.0
|
|
39
115
|
*/
|
|
40
|
-
export interface
|
|
41
|
-
readonly
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
schema: Schema.Schema<A, I, R>
|
|
46
|
-
) => Stream.Stream<A, AiError.AiError, R>
|
|
47
|
-
|
|
48
|
-
readonly converse: (options: {
|
|
49
|
-
readonly params?: { "anthropic-beta"?: string | undefined } | undefined
|
|
50
|
-
readonly payload: typeof ConverseRequest.Encoded
|
|
51
|
-
}) => Effect.Effect<ConverseResponse, AiError.AiError>
|
|
52
|
-
|
|
53
|
-
readonly converseStream: (options: {
|
|
54
|
-
readonly params?: { "anthropic-beta"?: string | undefined } | undefined
|
|
55
|
-
readonly payload: typeof ConverseRequest.Encoded
|
|
56
|
-
}) => Stream.Stream<ConverseResponseStreamEvent, AiError.AiError>
|
|
116
|
+
export interface Credentials {
|
|
117
|
+
readonly accessKeyId: string
|
|
118
|
+
readonly secretAccessKey: string
|
|
119
|
+
/** An empty token is sent as no token. */
|
|
120
|
+
readonly sessionToken?: string | undefined
|
|
57
121
|
}
|
|
58
122
|
|
|
59
123
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
124
|
+
* Configuration for creating an Amazon Bedrock client.
|
|
125
|
+
*
|
|
126
|
+
* **Details**
|
|
127
|
+
*
|
|
128
|
+
* The client authenticates in one of three ways, in this order of precedence:
|
|
129
|
+
*
|
|
130
|
+
* - `apiKey`, a Bedrock API key, sent as `Authorization: Bearer <key>`.
|
|
131
|
+
* - `credentials`, an effect resolved before every request, signed with
|
|
132
|
+
* SigV4. This is where a credential provider such as `fromNodeProviderChain()`
|
|
133
|
+
* from `@aws-sdk/credential-providers` plugs in, wrapped in
|
|
134
|
+
* `Effect.promise`. To avoid resolving credentials on every request, build
|
|
135
|
+
* the layer inside `Layer.unwrap` and pass the effect that
|
|
136
|
+
* `Effect.cachedWithTTL` returns.
|
|
137
|
+
* - `accessKeyId` and `secretAccessKey`, with an optional `sessionToken`,
|
|
138
|
+
* signed with SigV4.
|
|
139
|
+
*
|
|
140
|
+
* Without any of them, every request fails with an `AuthenticationError` of
|
|
141
|
+
* kind `MissingKey`.
|
|
142
|
+
*
|
|
143
|
+
* @stability unstable
|
|
144
|
+
* @category options
|
|
145
|
+
* @since 4.0.0
|
|
62
146
|
*/
|
|
63
|
-
export
|
|
147
|
+
export interface Options {
|
|
148
|
+
/**
|
|
149
|
+
* The AWS region.
|
|
150
|
+
*
|
|
151
|
+
* @default "us-east-1"
|
|
152
|
+
*/
|
|
153
|
+
readonly region?: string | undefined
|
|
154
|
+
/**
|
|
155
|
+
* The base URL for the Bedrock Runtime API. Override to use a proxy or a
|
|
156
|
+
* different endpoint.
|
|
157
|
+
*
|
|
158
|
+
* @default `https://bedrock-runtime.${region}.amazonaws.com`
|
|
159
|
+
*/
|
|
64
160
|
readonly apiUrl?: string | undefined
|
|
65
|
-
readonly accessKeyId
|
|
66
|
-
readonly secretAccessKey
|
|
161
|
+
readonly accessKeyId?: string | undefined
|
|
162
|
+
readonly secretAccessKey?: Redacted.Redacted<string> | undefined
|
|
67
163
|
readonly sessionToken?: Redacted.Redacted<string> | undefined
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
164
|
+
/**
|
|
165
|
+
* A Bedrock API key, sent as `Authorization: Bearer <key>` in place of a
|
|
166
|
+
* SigV4 signature.
|
|
167
|
+
*/
|
|
168
|
+
readonly apiKey?: Redacted.Redacted<string> | undefined
|
|
169
|
+
/**
|
|
170
|
+
* Resolves the AWS credentials of each request.
|
|
171
|
+
*/
|
|
172
|
+
readonly credentials?: Effect.Effect<Credentials, AiError.AiError> | undefined
|
|
173
|
+
/**
|
|
174
|
+
* Optional transformer for the underlying HTTP client, such as middleware,
|
|
175
|
+
* logging, or custom request/response handling.
|
|
176
|
+
*/
|
|
177
|
+
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// =============================================================================
|
|
181
|
+
// Constructor
|
|
182
|
+
// =============================================================================
|
|
183
|
+
|
|
184
|
+
type RequestCredentials =
|
|
185
|
+
| { readonly _tag: "Bearer"; readonly apiKey: Redacted.Redacted<string> }
|
|
186
|
+
| { readonly _tag: "SigV4"; readonly credentials: SigV4.SigningCredentials }
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The credentials of the request in flight. The client resolves them before
|
|
190
|
+
* `HttpClient.execute` and provides them around it, so that a resolution
|
|
191
|
+
* failure is not an `HttpClientError` that a retry policy would retry, and so
|
|
192
|
+
* that every retry inside `execute` signs again with them.
|
|
193
|
+
*/
|
|
194
|
+
const CurrentRequestCredentials = Context.Reference<RequestCredentials | undefined>(
|
|
195
|
+
"@effect/ai-amazon-bedrock/AmazonBedrockClient/CurrentRequestCredentials",
|
|
196
|
+
{ defaultValue: () => undefined }
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
const appendRedactedHeaders = Array.append("x-amz-security-token")
|
|
200
|
+
|
|
201
|
+
const withRedactedHeaders = Effect.updateService(Headers.CurrentRedactedNames, appendRedactedHeaders)
|
|
202
|
+
|
|
203
|
+
const missingKey = (method: string, description: string) =>
|
|
204
|
+
Errors.make(method, new AiError.AuthenticationError({ kind: "MissingKey", description }))
|
|
205
|
+
|
|
206
|
+
const sigV4 = (accessKeyId: string, secretAccessKey: string, sessionToken: string | undefined): RequestCredentials => ({
|
|
207
|
+
_tag: "SigV4",
|
|
208
|
+
credentials: { accessKeyId, secretAccessKey, sessionToken: sessionToken === "" ? undefined : sessionToken }
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
const makeResolveCredentials = (
|
|
212
|
+
options: Options
|
|
213
|
+
): (method: string) => Effect.Effect<RequestCredentials, AiError.AiError> => {
|
|
214
|
+
if (options.apiKey !== undefined) {
|
|
215
|
+
const credentials = Effect.succeed<RequestCredentials>({ _tag: "Bearer", apiKey: options.apiKey })
|
|
216
|
+
return () => credentials
|
|
217
|
+
}
|
|
218
|
+
if (options.credentials !== undefined) {
|
|
219
|
+
const resolve = options.credentials
|
|
220
|
+
return (method) =>
|
|
221
|
+
Effect.flatMap(resolve, (credentials) =>
|
|
222
|
+
credentials.accessKeyId === "" || credentials.secretAccessKey === ""
|
|
223
|
+
? Effect.fail(
|
|
224
|
+
missingKey(method, "The credentials effect returned an empty access key ID or secret access key")
|
|
225
|
+
)
|
|
226
|
+
: Effect.succeed(sigV4(credentials.accessKeyId, credentials.secretAccessKey, credentials.sessionToken)))
|
|
227
|
+
}
|
|
228
|
+
if (options.accessKeyId !== undefined && options.secretAccessKey !== undefined) {
|
|
229
|
+
const credentials = Effect.succeed(sigV4(
|
|
230
|
+
options.accessKeyId,
|
|
231
|
+
Redacted.value(options.secretAccessKey),
|
|
232
|
+
options.sessionToken === undefined ? undefined : Redacted.value(options.sessionToken)
|
|
233
|
+
))
|
|
234
|
+
return () => credentials
|
|
235
|
+
}
|
|
236
|
+
return (method) =>
|
|
237
|
+
Effect.fail(missingKey(
|
|
238
|
+
method,
|
|
239
|
+
"AmazonBedrockClient has no credentials: set `apiKey`, `credentials`, or `accessKeyId` with `secretAccessKey`"
|
|
240
|
+
))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const authenticateRequest = (signing: {
|
|
244
|
+
readonly region: string
|
|
245
|
+
readonly digest: Digest.Digest["Service"]
|
|
246
|
+
readonly mac: Mac.Mac["Service"]
|
|
72
247
|
}) =>
|
|
73
|
-
Effect.
|
|
74
|
-
const
|
|
248
|
+
Effect.fnUntraced(function*(request: HttpClientRequest.HttpClientRequest) {
|
|
249
|
+
const credentials = yield* CurrentRequestCredentials
|
|
250
|
+
if (Predicate.isUndefined(credentials)) {
|
|
251
|
+
return yield* Effect.die(new Error("AmazonBedrockClient sent a request without resolving its credentials"))
|
|
252
|
+
}
|
|
253
|
+
switch (credentials._tag) {
|
|
254
|
+
case "Bearer": {
|
|
255
|
+
return HttpClientRequest.bearerToken(request, credentials.apiKey)
|
|
256
|
+
}
|
|
257
|
+
case "SigV4": {
|
|
258
|
+
return yield* SigV4.sign(request, { ...signing, credentials: credentials.credentials })
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
})
|
|
75
262
|
|
|
76
|
-
|
|
263
|
+
const decodeResponse = HttpClientResponse.schemaBodyJson(ConverseResponse)
|
|
264
|
+
const decodeStreamEvent = Schema.decodeUnknownEffect(ConverseStreamEvent)
|
|
265
|
+
const decodeStreamException = Schema.decodeUnknownEffect(ConverseStreamException)
|
|
77
266
|
|
|
78
|
-
|
|
267
|
+
/**
|
|
268
|
+
* Creates an Amazon Bedrock client service with the given options.
|
|
269
|
+
*
|
|
270
|
+
* **Details**
|
|
271
|
+
*
|
|
272
|
+
* The client signs with the `Digest` and `Mac` services in its context, so
|
|
273
|
+
* the caller chooses their implementation: the WebCrypto layers of
|
|
274
|
+
* `effect/crypto`, or the layers of a platform package such as
|
|
275
|
+
* `NodeCrypto.layer` from `@effect/platform-node`. They are required for
|
|
276
|
+
* every way of authenticating, although a client with an `apiKey` never
|
|
277
|
+
* calls them. A `CryptoError` from the `Mac` service is a defect.
|
|
278
|
+
*
|
|
279
|
+
* @see {@link layer} for providing the client as a `Layer` from explicit options
|
|
280
|
+
* @see {@link layerConfig} for providing the client as a `Layer` with `Config`-based settings
|
|
281
|
+
*
|
|
282
|
+
* @stability unstable
|
|
283
|
+
* @category constructors
|
|
284
|
+
* @since 4.0.0
|
|
285
|
+
*/
|
|
286
|
+
export const make = Effect.fnUntraced(
|
|
287
|
+
function*(
|
|
288
|
+
options: Options
|
|
289
|
+
): Effect.fn.Return<Service, never, HttpClient.HttpClient | Digest.Digest | Mac.Mac> {
|
|
290
|
+
const region = options.region ?? "us-east-1"
|
|
291
|
+
const apiUrl = options.apiUrl ?? `https://bedrock-runtime.${region}.amazonaws.com`
|
|
292
|
+
const baseClient = yield* HttpClient.HttpClient
|
|
293
|
+
const digest = yield* Digest.Digest
|
|
294
|
+
const mac = yield* Mac.Mac
|
|
295
|
+
const resolveCredentials = makeResolveCredentials(options)
|
|
296
|
+
|
|
297
|
+
// Resolves the credentials of one request before it executes, so that a
|
|
298
|
+
// credentials failure is not an `HttpClientError` a retry policy would
|
|
299
|
+
// retry, and provides them for `authenticateRequest` to sign each attempt.
|
|
300
|
+
const withCredentials = (method: string) => <A, E, R>(self: Effect.Effect<A, E, R>) =>
|
|
301
|
+
Effect.flatMap(
|
|
302
|
+
resolveCredentials(method),
|
|
303
|
+
(credentials) => Effect.provideService(self, CurrentRequestCredentials, credentials)
|
|
304
|
+
)
|
|
79
305
|
|
|
80
|
-
const httpClient =
|
|
306
|
+
const httpClient = baseClient.pipe(
|
|
81
307
|
HttpClient.mapRequest((request) =>
|
|
82
|
-
request.pipe(
|
|
83
|
-
HttpClientRequest.prependUrl(options.apiUrl ?? `https://bedrock-runtime.${region}.amazonaws.com`),
|
|
84
|
-
HttpClientRequest.acceptJson
|
|
85
|
-
)
|
|
308
|
+
request.pipe(HttpClientRequest.prependUrl(apiUrl), HttpClientRequest.acceptJson)
|
|
86
309
|
),
|
|
87
|
-
HttpClient.mapRequestEffect(
|
|
88
|
-
|
|
89
|
-
const signer = new AwsV4Signer({
|
|
90
|
-
service: "bedrock",
|
|
91
|
-
url: request.url,
|
|
92
|
-
method: request.method,
|
|
93
|
-
headers: Object.entries(originalHeaders),
|
|
94
|
-
body: prepareBody(request.body),
|
|
95
|
-
region,
|
|
96
|
-
accessKeyId: options.accessKeyId,
|
|
97
|
-
secretAccessKey: Redacted.value(options.secretAccessKey),
|
|
98
|
-
...(options.sessionToken ? { sessionToken: Redacted.value(options.sessionToken) } : {})
|
|
99
|
-
})
|
|
100
|
-
const { headers: signedHeaders } = yield* Effect.promise(() => signer.sign())
|
|
101
|
-
const headers = Headers.merge(originalHeaders, Headers.fromInput(signedHeaders))
|
|
102
|
-
return HttpClientRequest.setHeaders(request, headers)
|
|
103
|
-
})),
|
|
104
|
-
options.transformClient ? options.transformClient : identity
|
|
310
|
+
HttpClient.mapRequestEffect(authenticateRequest({ region, digest, mac })),
|
|
311
|
+
options.transformClient ?? identity
|
|
105
312
|
)
|
|
106
313
|
|
|
107
|
-
|
|
314
|
+
// The exposed client resolves the credentials of each request itself and
|
|
315
|
+
// redacts the session token in header tracing, as the helpers do.
|
|
316
|
+
const client = HttpClient.makeWith(
|
|
317
|
+
(requestEffect) =>
|
|
318
|
+
Effect.flatMap(
|
|
319
|
+
requestEffect,
|
|
320
|
+
(request) => httpClient.execute(request).pipe(withCredentials("execute"), withRedactedHeaders)
|
|
321
|
+
),
|
|
322
|
+
Effect.succeed as HttpClient.HttpClient.Preprocess<never, never>
|
|
323
|
+
)
|
|
108
324
|
|
|
109
|
-
const
|
|
110
|
-
transformClient: (client) =>
|
|
111
|
-
AmazonBedrockConfig.getOrUndefined.pipe(
|
|
112
|
-
Effect.map((config) => config?.transformClient ? config.transformClient(client) : client)
|
|
113
|
-
)
|
|
114
|
-
})
|
|
325
|
+
const httpClientOk = HttpClient.filterStatusOk(httpClient)
|
|
115
326
|
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}) => Effect.Effect<ConverseResponse, AiError.AiError> = Effect.fnUntraced(
|
|
120
|
-
function*(request) {
|
|
121
|
-
return yield* client.converse(request).pipe(
|
|
122
|
-
Effect.catchTags({
|
|
123
|
-
RequestError: (error) =>
|
|
124
|
-
AiError.HttpRequestError.fromRequestError({
|
|
125
|
-
module: "AmazonBedrockClient",
|
|
126
|
-
method: "converse",
|
|
127
|
-
error
|
|
128
|
-
}),
|
|
129
|
-
ResponseError: (error) =>
|
|
130
|
-
AiError.HttpResponseError.fromResponseError({
|
|
131
|
-
module: "AmazonBedrockClient",
|
|
132
|
-
method: "converse",
|
|
133
|
-
error
|
|
134
|
-
}),
|
|
135
|
-
ParseError: (error) =>
|
|
136
|
-
AiError.MalformedOutput.fromParseError({
|
|
137
|
-
module: "AmazonBedrockClient",
|
|
138
|
-
method: "converse",
|
|
139
|
-
error
|
|
140
|
-
})
|
|
141
|
-
})
|
|
142
|
-
)
|
|
143
|
-
}
|
|
327
|
+
const configClient = Effect.map(
|
|
328
|
+
AmazonBedrockConfig.getOrUndefined,
|
|
329
|
+
(config) => (config?.transformClient ?? identity)(httpClientOk)
|
|
144
330
|
)
|
|
145
331
|
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
method: "streamRequest",
|
|
159
|
-
error
|
|
160
|
-
}),
|
|
161
|
-
ResponseError: (error) =>
|
|
162
|
-
AiError.HttpResponseError.fromResponseError({
|
|
163
|
-
module: "AmazonBedrockClient",
|
|
164
|
-
method: "streamRequest",
|
|
165
|
-
error
|
|
166
|
-
}),
|
|
167
|
-
ParseError: (error) =>
|
|
168
|
-
AiError.MalformedOutput.fromParseError({
|
|
169
|
-
module: "AmazonBedrockClient",
|
|
170
|
-
method: "streamRequest",
|
|
171
|
-
error
|
|
172
|
-
})
|
|
173
|
-
})
|
|
332
|
+
const execute = (
|
|
333
|
+
operation: "converse" | "converse-stream",
|
|
334
|
+
method: string,
|
|
335
|
+
{ modelId, ...body }: typeof ConverseRequest.Encoded
|
|
336
|
+
) =>
|
|
337
|
+
configClient.pipe(
|
|
338
|
+
Effect.flatMap((client) =>
|
|
339
|
+
client.execute(HttpClientRequest.post(`/model/${encodeURIComponent(modelId)}/${operation}`, {
|
|
340
|
+
body: HttpBody.jsonUnsafe(body)
|
|
341
|
+
}))
|
|
342
|
+
),
|
|
343
|
+
withCredentials(method)
|
|
174
344
|
)
|
|
175
345
|
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
headers: Headers.fromInput({
|
|
183
|
-
"anthropic-beta": options.params?.["anthropic-beta"]
|
|
346
|
+
const converse: Service["converse"] = (request) =>
|
|
347
|
+
execute("converse", "converse", request).pipe(
|
|
348
|
+
Effect.flatMap(decodeResponse),
|
|
349
|
+
Effect.catchTags({
|
|
350
|
+
HttpClientError: (error) => Errors.mapHttpClientError(error, "converse"),
|
|
351
|
+
SchemaError: (error) => Effect.fail(Errors.mapSchemaError(error, "converse"))
|
|
184
352
|
}),
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
353
|
+
withRedactedHeaders
|
|
354
|
+
)
|
|
355
|
+
|
|
356
|
+
const decodeFrame = Effect.fnUntraced(function*(frame: EventStream.Frame) {
|
|
357
|
+
const messageType = frame.headers[":message-type"]
|
|
358
|
+
switch (messageType) {
|
|
359
|
+
case "event": {
|
|
360
|
+
const eventType = frame.headers[":event-type"]
|
|
361
|
+
if (Predicate.isUndefined(eventType)) {
|
|
362
|
+
return undefined
|
|
363
|
+
}
|
|
364
|
+
const payload = yield* EventStream.parseJsonPayload(frame)
|
|
365
|
+
return yield* decodeStreamEvent({ [eventType]: payload })
|
|
366
|
+
}
|
|
367
|
+
case "exception": {
|
|
368
|
+
const payload = yield* EventStream.parseJsonPayload(frame)
|
|
369
|
+
const exception = yield* decodeStreamException(payload)
|
|
370
|
+
return yield* Errors.mapStreamException(
|
|
371
|
+
frame.headers[":exception-type"] ?? "unknown exception",
|
|
372
|
+
exception.message,
|
|
373
|
+
"converseStream"
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
case "error": {
|
|
377
|
+
return yield* Errors.make(
|
|
378
|
+
"converseStream",
|
|
379
|
+
new AiError.InternalProviderError({
|
|
380
|
+
description: `Event stream error frame [${frame.headers[":error-code"] ?? "unknown"}]: ${
|
|
381
|
+
frame.headers[":error-message"] ?? "no error message provided"
|
|
382
|
+
}`
|
|
383
|
+
})
|
|
384
|
+
)
|
|
385
|
+
}
|
|
386
|
+
default: {
|
|
387
|
+
return undefined
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
const converseStream: Service["converseStream"] = (request) =>
|
|
393
|
+
execute("converse-stream", "converseStream", request).pipe(
|
|
394
|
+
HttpClientResponse.stream,
|
|
395
|
+
EventStream.decode,
|
|
396
|
+
Stream.mapEffect(decodeFrame),
|
|
397
|
+
Stream.filter(Predicate.isNotUndefined),
|
|
398
|
+
Stream.catchTags({
|
|
399
|
+
HttpClientError: (error) => Stream.fromEffect(Errors.mapHttpClientError(error, "converseStream")),
|
|
400
|
+
SchemaError: (error) => Stream.fail(Errors.mapSchemaError(error, "converseStream")),
|
|
401
|
+
EventStreamError: (error) => Stream.fail(Errors.mapEventStreamError(error, "converseStream"))
|
|
402
|
+
}),
|
|
403
|
+
Stream.updateService(Headers.CurrentRedactedNames, appendRedactedHeaders)
|
|
404
|
+
)
|
|
189
405
|
|
|
190
406
|
return AmazonBedrockClient.of({
|
|
191
407
|
client,
|
|
192
|
-
streamRequest,
|
|
193
408
|
converse,
|
|
194
409
|
converseStream
|
|
195
410
|
})
|
|
196
|
-
}
|
|
411
|
+
},
|
|
412
|
+
withRedactedHeaders
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
// =============================================================================
|
|
416
|
+
// Layers
|
|
417
|
+
// =============================================================================
|
|
197
418
|
|
|
198
419
|
/**
|
|
199
|
-
*
|
|
420
|
+
* Creates a layer for the Amazon Bedrock client with the given options.
|
|
421
|
+
*
|
|
422
|
+
* **Example** (Providing the transport and the crypto services)
|
|
423
|
+
*
|
|
424
|
+
* ```ts
|
|
425
|
+
* import { AmazonBedrockClient } from "@effect/ai-amazon-bedrock"
|
|
426
|
+
* import { Layer, Redacted } from "effect"
|
|
427
|
+
* import { Digest, Mac, SecureRandom } from "effect/crypto"
|
|
428
|
+
* import { FetchHttpClient } from "effect/http"
|
|
429
|
+
*
|
|
430
|
+
* const BedrockClient = AmazonBedrockClient.layer({
|
|
431
|
+
* region: "eu-west-1",
|
|
432
|
+
* accessKeyId: "AKIAIOSFODNN7EXAMPLE",
|
|
433
|
+
* secretAccessKey: Redacted.make("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
|
|
434
|
+
* }).pipe(
|
|
435
|
+
* Layer.provide([
|
|
436
|
+
* FetchHttpClient.layer,
|
|
437
|
+
* Digest.layerWebCrypto,
|
|
438
|
+
* Mac.layerWebCrypto.pipe(Layer.provide(SecureRandom.layerWebCrypto))
|
|
439
|
+
* ])
|
|
440
|
+
* )
|
|
441
|
+
* ```
|
|
442
|
+
*
|
|
443
|
+
* @see {@link make} for constructing the client service effectfully
|
|
444
|
+
* @see {@link layerConfig} for loading client settings from `Config`
|
|
445
|
+
*
|
|
446
|
+
* @stability unstable
|
|
200
447
|
* @category layers
|
|
448
|
+
* @since 4.0.0
|
|
201
449
|
*/
|
|
202
|
-
export const layer = (
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
readonly sessionToken?: Redacted.Redacted<string> | undefined
|
|
207
|
-
readonly region?: string | undefined
|
|
208
|
-
readonly transformClient?: (
|
|
209
|
-
client: HttpClient.HttpClient
|
|
210
|
-
) => HttpClient.HttpClient
|
|
211
|
-
}): Layer.Layer<AmazonBedrockClient, never, HttpClient.HttpClient> => Layer.scoped(AmazonBedrockClient, make(options))
|
|
450
|
+
export const layer = (
|
|
451
|
+
options: Options
|
|
452
|
+
): Layer.Layer<AmazonBedrockClient, never, HttpClient.HttpClient | Digest.Digest | Mac.Mac> =>
|
|
453
|
+
Layer.effect(AmazonBedrockClient, make(options))
|
|
212
454
|
|
|
213
455
|
/**
|
|
214
|
-
*
|
|
456
|
+
* Creates a layer for the Amazon Bedrock client, loading credentials and other
|
|
457
|
+
* settings via Effect's `Config` module.
|
|
458
|
+
*
|
|
459
|
+
* **Details**
|
|
460
|
+
*
|
|
461
|
+
* Each setting defaults to the environment variable that the AWS tools read:
|
|
462
|
+
* `AWS_REGION`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`,
|
|
463
|
+
* `AWS_SESSION_TOKEN` and, for a Bedrock API key, `AWS_BEARER_TOKEN_BEDROCK`.
|
|
464
|
+
* Every one of them is optional; see {@link Options} for which of them are
|
|
465
|
+
* used when several are set.
|
|
466
|
+
*
|
|
467
|
+
* @see {@link layer} for providing the client from explicit options instead of `Config`
|
|
468
|
+
* @see {@link make} for constructing the client service effectfully
|
|
469
|
+
*
|
|
470
|
+
* @stability unstable
|
|
215
471
|
* @category layers
|
|
472
|
+
* @since 4.0.0
|
|
216
473
|
*/
|
|
217
|
-
export const layerConfig = (
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
474
|
+
export const layerConfig = (options?: {
|
|
475
|
+
readonly region?: Config.Config<string | undefined> | undefined
|
|
476
|
+
readonly apiUrl?: Config.Config<string | undefined> | undefined
|
|
477
|
+
readonly accessKeyId?: Config.Config<string | undefined> | undefined
|
|
478
|
+
readonly secretAccessKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
479
|
+
readonly sessionToken?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
480
|
+
readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined
|
|
481
|
+
readonly credentials?: Effect.Effect<Credentials, AiError.AiError> | undefined
|
|
482
|
+
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
|
|
483
|
+
}): Layer.Layer<AmazonBedrockClient, Config.ConfigError, HttpClient.HttpClient | Digest.Digest | Mac.Mac> =>
|
|
484
|
+
Layer.effect(
|
|
485
|
+
AmazonBedrockClient,
|
|
486
|
+
Effect.gen(function*() {
|
|
487
|
+
return yield* make({
|
|
488
|
+
region: yield* options?.region ?? optional(Config.String("AWS_REGION")),
|
|
489
|
+
apiUrl: yield* options?.apiUrl ?? Effect.undefined,
|
|
490
|
+
accessKeyId: yield* options?.accessKeyId ?? optional(Config.String("AWS_ACCESS_KEY_ID")),
|
|
491
|
+
secretAccessKey: yield* options?.secretAccessKey ?? optional(Config.Redacted("AWS_SECRET_ACCESS_KEY")),
|
|
492
|
+
sessionToken: yield* options?.sessionToken ?? optional(Config.Redacted("AWS_SESSION_TOKEN")),
|
|
493
|
+
apiKey: yield* options?.apiKey ?? optional(Config.Redacted("AWS_BEARER_TOKEN_BEDROCK")),
|
|
494
|
+
credentials: options?.credentials,
|
|
495
|
+
transformClient: options?.transformClient
|
|
496
|
+
})
|
|
497
|
+
})
|
|
233
498
|
)
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// =============================================================================
|
|
237
|
-
// Client
|
|
238
|
-
// =============================================================================
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* @since 1.0.0
|
|
242
|
-
* @category models
|
|
243
|
-
*/
|
|
244
|
-
export interface Client {
|
|
245
|
-
readonly converse: (options: {
|
|
246
|
-
readonly params?: { "anthropic-beta"?: string | undefined }
|
|
247
|
-
readonly payload: typeof ConverseRequest.Encoded
|
|
248
|
-
}) => Effect.Effect<typeof ConverseResponse.Type, HttpClientError.HttpClientError | ParseError>
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const makeClient = (
|
|
252
|
-
httpClient: HttpClient.HttpClient,
|
|
253
|
-
options: {
|
|
254
|
-
readonly transformClient?: ((client: HttpClient.HttpClient) => Effect.Effect<HttpClient.HttpClient>) | undefined
|
|
255
|
-
}
|
|
256
|
-
): Client => {
|
|
257
|
-
const unexpectedStatus = (response: HttpClientResponse.HttpClientResponse) =>
|
|
258
|
-
Effect.flatMap(
|
|
259
|
-
Effect.orElseSucceed(response.json, () => "Unexpected status code"),
|
|
260
|
-
(description) =>
|
|
261
|
-
Effect.fail(
|
|
262
|
-
new HttpClientError.ResponseError({
|
|
263
|
-
request: response.request,
|
|
264
|
-
response,
|
|
265
|
-
reason: "StatusCode",
|
|
266
|
-
description: typeof description === "string" ? description : JSON.stringify(description)
|
|
267
|
-
})
|
|
268
|
-
)
|
|
269
|
-
)
|
|
270
|
-
const withResponse: <A, E>(
|
|
271
|
-
f: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<A, E>
|
|
272
|
-
) => (
|
|
273
|
-
request: HttpClientRequest.HttpClientRequest
|
|
274
|
-
) => Effect.Effect<any, any> = options.transformClient
|
|
275
|
-
? (f) => (request) =>
|
|
276
|
-
Effect.flatMap(
|
|
277
|
-
Effect.flatMap(options.transformClient!(httpClient), (client) => client.execute(request)),
|
|
278
|
-
f
|
|
279
|
-
)
|
|
280
|
-
: (f) => (request) => Effect.flatMap(httpClient.execute(request), f)
|
|
281
|
-
const decodeSuccess =
|
|
282
|
-
<A, I, R>(schema: Schema.Schema<A, I, R>) => (response: HttpClientResponse.HttpClientResponse) =>
|
|
283
|
-
HttpClientResponse.schemaBodyJson(schema)(response)
|
|
284
|
-
return {
|
|
285
|
-
converse: ({ params, payload: { modelId, ...payload } }) =>
|
|
286
|
-
HttpClientRequest.post(`/model/${modelId}/converse`).pipe(
|
|
287
|
-
HttpClientRequest.setHeaders({
|
|
288
|
-
"anthropic-beta": params?.["anthropic-beta"] ?? undefined
|
|
289
|
-
}),
|
|
290
|
-
HttpClientRequest.bodyUnsafeJson(payload),
|
|
291
|
-
withResponse(HttpClientResponse.matchStatus({
|
|
292
|
-
"2xx": decodeSuccess(ConverseResponse),
|
|
293
|
-
orElse: unexpectedStatus
|
|
294
|
-
}))
|
|
295
|
-
)
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
499
|
|
|
299
|
-
const
|
|
300
|
-
switch (body._tag) {
|
|
301
|
-
case "Raw":
|
|
302
|
-
case "Uint8Array": {
|
|
303
|
-
if (typeof body.body === "string") {
|
|
304
|
-
return body.body
|
|
305
|
-
}
|
|
306
|
-
if (body.body instanceof Uint8Array) {
|
|
307
|
-
return new TextDecoder().decode(body.body)
|
|
308
|
-
}
|
|
309
|
-
if (body.body instanceof ArrayBuffer) {
|
|
310
|
-
return new TextDecoder().decode(body.body)
|
|
311
|
-
}
|
|
312
|
-
return JSON.stringify(body.body)
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
throw new Error("Unsupported HttpBody: " + body._tag)
|
|
316
|
-
}
|
|
500
|
+
const optional = <A>(config: Config.Config<A>): Config.Config<A | undefined> => Config.withDefault(config, undefined)
|