@effect/ai-amazon-bedrock 0.6.1 → 0.7.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/AmazonBedrockTool/package.json +6 -0
- package/dist/cjs/AmazonBedrockClient.js +71 -145
- package/dist/cjs/AmazonBedrockClient.js.map +1 -1
- package/dist/cjs/AmazonBedrockLanguageModel.js +833 -337
- package/dist/cjs/AmazonBedrockLanguageModel.js.map +1 -1
- package/dist/cjs/AmazonBedrockSchema.js +96 -41
- package/dist/cjs/AmazonBedrockSchema.js.map +1 -1
- package/dist/cjs/AmazonBedrockTool.js +53 -0
- package/dist/cjs/AmazonBedrockTool.js.map +1 -0
- package/dist/cjs/EventStreamEncoding.js +5 -4
- package/dist/cjs/EventStreamEncoding.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/internal/utilities.js +3 -1
- package/dist/cjs/internal/utilities.js.map +1 -1
- package/dist/dts/AmazonBedrockClient.d.ts +34 -34
- package/dist/dts/AmazonBedrockClient.d.ts.map +1 -1
- package/dist/dts/AmazonBedrockLanguageModel.d.ts +72 -20
- package/dist/dts/AmazonBedrockLanguageModel.d.ts.map +1 -1
- package/dist/dts/AmazonBedrockSchema.d.ts +549 -108
- package/dist/dts/AmazonBedrockSchema.d.ts.map +1 -1
- package/dist/dts/AmazonBedrockTool.d.ts +184 -0
- package/dist/dts/AmazonBedrockTool.d.ts.map +1 -0
- package/dist/dts/EventStreamEncoding.d.ts +2 -2
- package/dist/dts/EventStreamEncoding.d.ts.map +1 -1
- package/dist/dts/index.d.ts +4 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/esm/AmazonBedrockClient.js +71 -144
- package/dist/esm/AmazonBedrockClient.js.map +1 -1
- package/dist/esm/AmazonBedrockLanguageModel.js +830 -333
- package/dist/esm/AmazonBedrockLanguageModel.js.map +1 -1
- package/dist/esm/AmazonBedrockSchema.js +95 -39
- package/dist/esm/AmazonBedrockSchema.js.map +1 -1
- package/dist/esm/AmazonBedrockTool.js +45 -0
- package/dist/esm/AmazonBedrockTool.js.map +1 -0
- package/dist/esm/EventStreamEncoding.js +5 -3
- package/dist/esm/EventStreamEncoding.js.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/utilities.js +2 -0
- package/dist/esm/internal/utilities.js.map +1 -1
- package/package.json +13 -4
- package/src/AmazonBedrockClient.ts +144 -241
- package/src/AmazonBedrockLanguageModel.ts +962 -372
- package/src/AmazonBedrockSchema.ts +136 -48
- package/src/AmazonBedrockTool.ts +251 -0
- package/src/EventStreamEncoding.ts +80 -73
- package/src/index.ts +5 -0
- package/src/internal/utilities.ts +6 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/ai-amazon-bedrock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Effect modules for working with Amazon Bedrock AI apis",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,10 +17,11 @@
|
|
|
17
17
|
"gpt-tokenizer": "^2.9.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@effect/ai": "^0.
|
|
20
|
+
"@effect/ai": "^0.27.0",
|
|
21
|
+
"@effect/ai-anthropic": "^0.17.0",
|
|
21
22
|
"@effect/experimental": "^0.54.6",
|
|
22
|
-
"@effect/platform": "^0.90.
|
|
23
|
-
"effect": "^3.17.
|
|
23
|
+
"@effect/platform": "^0.90.9",
|
|
24
|
+
"effect": "^3.17.13"
|
|
24
25
|
},
|
|
25
26
|
"publishConfig": {
|
|
26
27
|
"provenance": true
|
|
@@ -55,6 +56,11 @@
|
|
|
55
56
|
"import": "./dist/esm/AmazonBedrockSchema.js",
|
|
56
57
|
"default": "./dist/cjs/AmazonBedrockSchema.js"
|
|
57
58
|
},
|
|
59
|
+
"./AmazonBedrockTool": {
|
|
60
|
+
"types": "./dist/dts/AmazonBedrockTool.d.ts",
|
|
61
|
+
"import": "./dist/esm/AmazonBedrockTool.js",
|
|
62
|
+
"default": "./dist/cjs/AmazonBedrockTool.js"
|
|
63
|
+
},
|
|
58
64
|
"./EventStreamEncoding": {
|
|
59
65
|
"types": "./dist/dts/EventStreamEncoding.d.ts",
|
|
60
66
|
"import": "./dist/esm/EventStreamEncoding.js",
|
|
@@ -80,6 +86,9 @@
|
|
|
80
86
|
"AmazonBedrockSchema": [
|
|
81
87
|
"./dist/dts/AmazonBedrockSchema.d.ts"
|
|
82
88
|
],
|
|
89
|
+
"AmazonBedrockTool": [
|
|
90
|
+
"./dist/dts/AmazonBedrockTool.d.ts"
|
|
91
|
+
],
|
|
83
92
|
"EventStreamEncoding": [
|
|
84
93
|
"./dist/dts/EventStreamEncoding.d.ts"
|
|
85
94
|
],
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @since 1.0.0
|
|
3
3
|
*/
|
|
4
|
-
import * as
|
|
5
|
-
import * as AiResponse from "@effect/ai/AiResponse"
|
|
4
|
+
import * as AiError from "@effect/ai/AiError"
|
|
6
5
|
import * as Headers from "@effect/platform/Headers"
|
|
7
|
-
import
|
|
6
|
+
import * as HttpBody from "@effect/platform/HttpBody"
|
|
8
7
|
import * as HttpClient from "@effect/platform/HttpClient"
|
|
9
8
|
import * as HttpClientError from "@effect/platform/HttpClientError"
|
|
10
9
|
import * as HttpClientRequest from "@effect/platform/HttpClientRequest"
|
|
@@ -14,70 +13,52 @@ import * as Arr from "effect/Array"
|
|
|
14
13
|
import * as Config from "effect/Config"
|
|
15
14
|
import type { ConfigError } from "effect/ConfigError"
|
|
16
15
|
import * as Context from "effect/Context"
|
|
17
|
-
import * as Data from "effect/Data"
|
|
18
16
|
import * as Effect from "effect/Effect"
|
|
19
17
|
import { identity } from "effect/Function"
|
|
20
18
|
import * as Layer from "effect/Layer"
|
|
21
|
-
import * as Option from "effect/Option"
|
|
22
19
|
import type { ParseError } from "effect/ParseResult"
|
|
23
|
-
import * as Predicate from "effect/Predicate"
|
|
24
20
|
import * as Redacted from "effect/Redacted"
|
|
21
|
+
import type * as Schema from "effect/Schema"
|
|
25
22
|
import * as Stream from "effect/Stream"
|
|
26
|
-
import type { Mutable } from "effect/Types"
|
|
27
23
|
import { AmazonBedrockConfig } from "./AmazonBedrockConfig.js"
|
|
28
|
-
import type { ProviderMetadata } from "./AmazonBedrockLanguageModel.js"
|
|
29
24
|
import type { ConverseRequest } from "./AmazonBedrockSchema.js"
|
|
30
|
-
import { ConverseResponse,
|
|
25
|
+
import { ConverseResponse, ConverseResponseStreamEvent } from "./AmazonBedrockSchema.js"
|
|
31
26
|
import * as EventStreamEncoding from "./EventStreamEncoding.js"
|
|
32
|
-
import * as InternalUtilities from "./internal/utilities.js"
|
|
33
|
-
|
|
34
|
-
const constDisableValidation = { disableValidation: true } as const
|
|
35
27
|
|
|
36
28
|
/**
|
|
37
29
|
* @since 1.0.0
|
|
38
|
-
* @category
|
|
30
|
+
* @category Context
|
|
39
31
|
*/
|
|
40
32
|
export class AmazonBedrockClient extends Context.Tag(
|
|
41
33
|
"@effect/ai-amazon-bedrock/AmazonBedrockClient"
|
|
42
|
-
)<AmazonBedrockClient,
|
|
34
|
+
)<AmazonBedrockClient, Service>() {}
|
|
43
35
|
|
|
44
36
|
/**
|
|
45
37
|
* @since 1.0.0
|
|
38
|
+
* @category Models
|
|
46
39
|
*/
|
|
47
|
-
export
|
|
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
|
-
}
|
|
40
|
+
export interface Service {
|
|
41
|
+
readonly client: Client
|
|
63
42
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
43
|
+
readonly streamRequest: <A, I, R>(
|
|
44
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
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>
|
|
76
57
|
}
|
|
77
58
|
|
|
78
59
|
/**
|
|
79
60
|
* @since 1.0.0
|
|
80
|
-
* @category
|
|
61
|
+
* @category Constructors
|
|
81
62
|
*/
|
|
82
63
|
export const make = (options: {
|
|
83
64
|
readonly apiUrl?: string | undefined
|
|
@@ -130,154 +111,86 @@ export const make = (options: {
|
|
|
130
111
|
)
|
|
131
112
|
})
|
|
132
113
|
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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))
|
|
114
|
+
const converse: (options: {
|
|
115
|
+
readonly params?: { "anthropic-beta"?: string | undefined } | undefined
|
|
116
|
+
readonly payload: typeof ConverseRequest.Encoded
|
|
117
|
+
}) => Effect.Effect<ConverseResponse, AiError.AiError> = Effect.fnUntraced(
|
|
118
|
+
function*(request) {
|
|
119
|
+
return yield* client.converse(request).pipe(
|
|
120
|
+
Effect.catchTags({
|
|
121
|
+
RequestError: (error) =>
|
|
122
|
+
AiError.HttpRequestError.fromRequestError({
|
|
123
|
+
module: "AmazonBedrockClient",
|
|
124
|
+
method: "converse",
|
|
125
|
+
error
|
|
126
|
+
}),
|
|
127
|
+
ResponseError: (error) =>
|
|
128
|
+
AiError.HttpResponseError.fromResponseError({
|
|
129
|
+
module: "AmazonBedrockClient",
|
|
130
|
+
method: "converse",
|
|
131
|
+
error
|
|
132
|
+
}),
|
|
133
|
+
ParseError: (error) =>
|
|
134
|
+
AiError.MalformedOutput.fromParseError({
|
|
135
|
+
module: "AmazonBedrockClient",
|
|
136
|
+
method: "converse",
|
|
137
|
+
error
|
|
138
|
+
})
|
|
276
139
|
})
|
|
277
140
|
)
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
const streamRequest = <A, I, R>(
|
|
145
|
+
request: HttpClientRequest.HttpClientRequest,
|
|
146
|
+
schema: Schema.Schema<A, I, R>
|
|
147
|
+
): Stream.Stream<A, AiError.AiError, R> =>
|
|
148
|
+
httpClient.execute(request).pipe(
|
|
149
|
+
Effect.map((r) => r.stream),
|
|
150
|
+
Stream.unwrapScoped,
|
|
151
|
+
Stream.pipeThroughChannel(EventStreamEncoding.makeChannel(schema)),
|
|
152
|
+
Stream.catchTags({
|
|
153
|
+
RequestError: (error) =>
|
|
154
|
+
AiError.HttpRequestError.fromRequestError({
|
|
155
|
+
module: "AmazonBedrockClient",
|
|
156
|
+
method: "streamRequest",
|
|
157
|
+
error
|
|
158
|
+
}),
|
|
159
|
+
ResponseError: (error) =>
|
|
160
|
+
AiError.HttpResponseError.fromResponseError({
|
|
161
|
+
module: "AmazonBedrockClient",
|
|
162
|
+
method: "streamRequest",
|
|
163
|
+
error
|
|
164
|
+
}),
|
|
165
|
+
ParseError: (error) =>
|
|
166
|
+
AiError.MalformedOutput.fromParseError({
|
|
167
|
+
module: "AmazonBedrockClient",
|
|
168
|
+
method: "streamRequest",
|
|
169
|
+
error
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
const converseStream = (options: {
|
|
175
|
+
readonly params?: { "anthropic-beta"?: string | undefined } | undefined
|
|
176
|
+
readonly payload: typeof ConverseRequest.Encoded
|
|
177
|
+
}): Stream.Stream<ConverseResponseStreamEvent, AiError.AiError> => {
|
|
178
|
+
const { modelId, ...body } = options.payload
|
|
179
|
+
const request = HttpClientRequest.post(`/model/${modelId}/converse-stream`, {
|
|
180
|
+
headers: Headers.fromInput({
|
|
181
|
+
"anthropic-beta": options.params?.["anthropic-beta"]
|
|
182
|
+
}),
|
|
183
|
+
body: HttpBody.unsafeJson(body)
|
|
278
184
|
})
|
|
185
|
+
return streamRequest(request, ConverseResponseStreamEvent)
|
|
186
|
+
}
|
|
279
187
|
|
|
280
|
-
return AmazonBedrockClient.of({
|
|
188
|
+
return AmazonBedrockClient.of({
|
|
189
|
+
client,
|
|
190
|
+
streamRequest,
|
|
191
|
+
converse,
|
|
192
|
+
converseStream
|
|
193
|
+
})
|
|
281
194
|
})
|
|
282
195
|
|
|
283
196
|
/**
|
|
@@ -318,73 +231,69 @@ export const layerConfig = (
|
|
|
318
231
|
)
|
|
319
232
|
}
|
|
320
233
|
|
|
234
|
+
// =============================================================================
|
|
235
|
+
// Client
|
|
236
|
+
// =============================================================================
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @since 1.0.0
|
|
240
|
+
* @category models
|
|
241
|
+
*/
|
|
242
|
+
export interface Client {
|
|
243
|
+
readonly converse: (options: {
|
|
244
|
+
readonly params?: { "anthropic-beta"?: string | undefined }
|
|
245
|
+
readonly payload: typeof ConverseRequest.Encoded
|
|
246
|
+
}) => Effect.Effect<typeof ConverseResponse.Type, HttpClientError.HttpClientError | ParseError>
|
|
247
|
+
}
|
|
248
|
+
|
|
321
249
|
const makeClient = (
|
|
322
250
|
httpClient: HttpClient.HttpClient,
|
|
323
251
|
options: {
|
|
324
252
|
readonly transformClient?: ((client: HttpClient.HttpClient) => Effect.Effect<HttpClient.HttpClient>) | undefined
|
|
325
253
|
}
|
|
326
|
-
):
|
|
327
|
-
const unexpectedStatus = (
|
|
328
|
-
request: HttpClientRequest.HttpClientRequest,
|
|
329
|
-
response: HttpClientResponse.HttpClientResponse
|
|
330
|
-
) =>
|
|
254
|
+
): Client => {
|
|
255
|
+
const unexpectedStatus = (response: HttpClientResponse.HttpClientResponse) =>
|
|
331
256
|
Effect.flatMap(
|
|
332
|
-
Effect.orElseSucceed(response.
|
|
257
|
+
Effect.orElseSucceed(response.json, () => "Unexpected status code"),
|
|
333
258
|
(description) =>
|
|
334
259
|
Effect.fail(
|
|
335
260
|
new HttpClientError.ResponseError({
|
|
336
|
-
request,
|
|
261
|
+
request: response.request,
|
|
337
262
|
response,
|
|
338
263
|
reason: "StatusCode",
|
|
339
|
-
description
|
|
264
|
+
description: typeof description === "string" ? description : JSON.stringify(description)
|
|
340
265
|
})
|
|
341
266
|
)
|
|
342
267
|
)
|
|
343
|
-
const
|
|
344
|
-
|
|
268
|
+
const withResponse: <A, E>(
|
|
269
|
+
f: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<A, E>
|
|
270
|
+
) => (
|
|
271
|
+
request: HttpClientRequest.HttpClientRequest
|
|
272
|
+
) => Effect.Effect<any, any> = options.transformClient
|
|
273
|
+
? (f) => (request) =>
|
|
274
|
+
Effect.flatMap(
|
|
275
|
+
Effect.flatMap(options.transformClient!(httpClient), (client) => client.execute(request)),
|
|
276
|
+
f
|
|
277
|
+
)
|
|
278
|
+
: (f) => (request) => Effect.flatMap(httpClient.execute(request), f)
|
|
279
|
+
const decodeSuccess =
|
|
280
|
+
<A, I, R>(schema: Schema.Schema<A, I, R>) => (response: HttpClientResponse.HttpClientResponse) =>
|
|
281
|
+
HttpClientResponse.schemaBodyJson(schema)(response)
|
|
345
282
|
return {
|
|
346
|
-
converse: ({ modelId, ...payload }) =>
|
|
347
|
-
HttpClientRequest.
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
})
|
|
357
|
-
))
|
|
358
|
-
)
|
|
359
|
-
),
|
|
360
|
-
converseStream: ({ modelId, ...payload }) =>
|
|
361
|
-
HttpClientRequest.make("POST")(`/model/${modelId}/converse-stream`).pipe(
|
|
362
|
-
(request) => Effect.orDie(HttpClientRequest.bodyJson(request, payload)),
|
|
363
|
-
Effect.flatMap((request) =>
|
|
364
|
-
Effect.flatMap(applyClientTransform(httpClient), (httpClient) =>
|
|
365
|
-
Effect.flatMap(
|
|
366
|
-
httpClient.execute(request),
|
|
367
|
-
HttpClientResponse.filterStatusOk
|
|
368
|
-
))
|
|
369
|
-
)
|
|
283
|
+
converse: ({ params, payload: { modelId, ...payload } }) =>
|
|
284
|
+
HttpClientRequest.post(`/model/${modelId}/converse`).pipe(
|
|
285
|
+
HttpClientRequest.setHeaders({
|
|
286
|
+
"anthropic-beta": params?.["anthropic-beta"] ?? undefined
|
|
287
|
+
}),
|
|
288
|
+
HttpClientRequest.bodyUnsafeJson(payload),
|
|
289
|
+
withResponse(HttpClientResponse.matchStatus({
|
|
290
|
+
"2xx": decodeSuccess(ConverseResponse),
|
|
291
|
+
orElse: unexpectedStatus
|
|
292
|
+
}))
|
|
370
293
|
)
|
|
371
294
|
}
|
|
372
295
|
}
|
|
373
296
|
|
|
374
|
-
/**
|
|
375
|
-
* @since 1.0.0
|
|
376
|
-
* @category models
|
|
377
|
-
*/
|
|
378
|
-
export class StreamChunk extends Data.Class<{
|
|
379
|
-
readonly parts: Array<StreamChunkPart>
|
|
380
|
-
}> {}
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* @since 1.0.0
|
|
384
|
-
* @category models
|
|
385
|
-
*/
|
|
386
|
-
export type StreamChunkPart = any /* ContentPart | ToolCallPart | UsagePart */
|
|
387
|
-
|
|
388
297
|
const prepareBody = (body: HttpBody.HttpBody): string => {
|
|
389
298
|
switch (body._tag) {
|
|
390
299
|
case "Raw":
|
|
@@ -403,9 +312,3 @@ const prepareBody = (body: HttpBody.HttpBody): string => {
|
|
|
403
312
|
}
|
|
404
313
|
throw new Error("Unsupported HttpBody: " + body._tag)
|
|
405
314
|
}
|
|
406
|
-
|
|
407
|
-
type RawToolCall = {
|
|
408
|
-
readonly id: string
|
|
409
|
-
readonly name: string
|
|
410
|
-
params: string
|
|
411
|
-
}
|