@effect/ai-anthropic 4.0.0-beta.7 → 4.0.0-beta.70
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/dist/AnthropicClient.d.ts +40 -66
- package/dist/AnthropicClient.d.ts.map +1 -1
- package/dist/AnthropicClient.js +12 -14
- package/dist/AnthropicClient.js.map +1 -1
- package/dist/AnthropicConfig.d.ts +46 -10
- package/dist/AnthropicConfig.d.ts.map +1 -1
- package/dist/AnthropicConfig.js +31 -7
- package/dist/AnthropicConfig.js.map +1 -1
- package/dist/AnthropicError.d.ts +128 -37
- package/dist/AnthropicError.d.ts.map +1 -1
- package/dist/AnthropicError.js +1 -1
- package/dist/AnthropicLanguageModel.d.ts +281 -66
- package/dist/AnthropicLanguageModel.d.ts.map +1 -1
- package/dist/AnthropicLanguageModel.js +50 -14
- package/dist/AnthropicLanguageModel.js.map +1 -1
- package/dist/AnthropicTelemetry.d.ts +23 -16
- package/dist/AnthropicTelemetry.d.ts.map +1 -1
- package/dist/AnthropicTelemetry.js +6 -4
- package/dist/AnthropicTelemetry.js.map +1 -1
- package/dist/AnthropicTool.d.ts +298 -138
- package/dist/AnthropicTool.d.ts.map +1 -1
- package/dist/AnthropicTool.js +141 -87
- package/dist/AnthropicTool.js.map +1 -1
- package/dist/Generated.d.ts +11661 -5260
- package/dist/Generated.d.ts.map +1 -1
- package/dist/Generated.js +2318 -915
- package/dist/Generated.js.map +1 -1
- package/dist/index.d.ts +52 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +52 -8
- package/dist/index.js.map +1 -1
- package/dist/internal/errors.js +7 -7
- package/dist/internal/errors.js.map +1 -1
- package/package.json +3 -3
- package/src/AnthropicClient.ts +41 -67
- package/src/AnthropicConfig.ts +47 -11
- package/src/AnthropicError.ts +130 -37
- package/src/AnthropicLanguageModel.ts +274 -33
- package/src/AnthropicTelemetry.ts +24 -17
- package/src/AnthropicTool.ts +293 -133
- package/src/Generated.ts +3751 -1815
- package/src/index.ts +52 -8
- package/src/internal/errors.ts +9 -7
|
@@ -1,18 +1,45 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `AnthropicLanguageModel` module provides the Anthropic implementation of
|
|
3
|
+
* Effect AI's `LanguageModel` service. It turns Effect AI prompts, tools, files,
|
|
4
|
+
* reasoning parts, and provider options into Anthropic Messages API requests,
|
|
5
|
+
* and converts Anthropic responses and streams back into Effect AI response
|
|
6
|
+
* parts with Anthropic-specific metadata.
|
|
7
|
+
*
|
|
8
|
+
* **When to use**
|
|
9
|
+
*
|
|
10
|
+
* - Create an Anthropic-backed model with {@link model}
|
|
11
|
+
* - Build or provide a `LanguageModel.LanguageModel` layer with {@link layer}
|
|
12
|
+
* or {@link make}
|
|
13
|
+
* - Supply default request options through {@link Config}
|
|
14
|
+
* - Override configuration for a scoped operation with {@link withConfigOverride}
|
|
15
|
+
* - Attach Anthropic provider options for prompt caching, document citations,
|
|
16
|
+
* reasoning signatures, MCP metadata, and server-side tools
|
|
17
|
+
*
|
|
18
|
+
* **Gotchas**
|
|
19
|
+
*
|
|
20
|
+
* - Prompt files are translated to Anthropic image or document blocks; only the
|
|
21
|
+
* supported media types can be sent to the provider.
|
|
22
|
+
* - Structured output support depends on the selected Claude model, so this
|
|
23
|
+
* module may use Anthropic's native structured output or fall back to a JSON
|
|
24
|
+
* response tool.
|
|
25
|
+
* - Some features require Anthropic beta headers, which are added
|
|
26
|
+
* automatically from the selected tools, files, and model capabilities.
|
|
27
|
+
*
|
|
28
|
+
* @since 4.0.0
|
|
3
29
|
*/
|
|
4
30
|
/** @effect-diagnostics preferSchemaOverJson:skip-file */
|
|
5
31
|
import * as Arr from "effect/Array"
|
|
32
|
+
import * as Context from "effect/Context"
|
|
6
33
|
import * as DateTime from "effect/DateTime"
|
|
7
34
|
import * as Effect from "effect/Effect"
|
|
8
35
|
import * as Encoding from "effect/Encoding"
|
|
9
36
|
import { dual } from "effect/Function"
|
|
10
37
|
import * as Layer from "effect/Layer"
|
|
38
|
+
import * as Option from "effect/Option"
|
|
11
39
|
import * as Predicate from "effect/Predicate"
|
|
12
40
|
import * as Redactable from "effect/Redactable"
|
|
13
41
|
import * as Schema from "effect/Schema"
|
|
14
42
|
import * as SchemaAST from "effect/SchemaAST"
|
|
15
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
16
43
|
import * as Stream from "effect/Stream"
|
|
17
44
|
import type { Span } from "effect/Tracer"
|
|
18
45
|
import type { Mutable, Simplify } from "effect/Types"
|
|
@@ -35,8 +62,8 @@ import * as InternalUtilities from "./internal/utilities.ts"
|
|
|
35
62
|
/**
|
|
36
63
|
* The available Anthropic Claude model identifiers.
|
|
37
64
|
*
|
|
38
|
-
* @since 1.0.0
|
|
39
65
|
* @category models
|
|
66
|
+
* @since 4.0.0
|
|
40
67
|
*/
|
|
41
68
|
export type Model = typeof Generated.Model.Type
|
|
42
69
|
|
|
@@ -47,13 +74,15 @@ export type Model = typeof Generated.Model.Type
|
|
|
47
74
|
/**
|
|
48
75
|
* Configuration options for the Anthropic language model.
|
|
49
76
|
*
|
|
77
|
+
* **Details**
|
|
78
|
+
*
|
|
50
79
|
* This service can be used to provide default configuration values or to
|
|
51
80
|
* override configuration on a per-request basis.
|
|
52
81
|
*
|
|
53
|
-
* @since 1.0.0
|
|
54
82
|
* @category configuration
|
|
83
|
+
* @since 4.0.0
|
|
55
84
|
*/
|
|
56
|
-
export class Config extends
|
|
85
|
+
export class Config extends Context.Service<
|
|
57
86
|
Config,
|
|
58
87
|
Simplify<
|
|
59
88
|
& Partial<
|
|
@@ -73,6 +102,8 @@ export class Config extends ServiceMap.Service<
|
|
|
73
102
|
/**
|
|
74
103
|
* Whether to use strict JSON schema validation for tool calls.
|
|
75
104
|
*
|
|
105
|
+
* **Details**
|
|
106
|
+
*
|
|
76
107
|
* Only applies to models that support structured outputs. Defaults to
|
|
77
108
|
* `true` when structured outputs are supported.
|
|
78
109
|
*/
|
|
@@ -86,6 +117,17 @@ export class Config extends ServiceMap.Service<
|
|
|
86
117
|
// =============================================================================
|
|
87
118
|
|
|
88
119
|
declare module "effect/unstable/ai/Prompt" {
|
|
120
|
+
/**
|
|
121
|
+
* Anthropic-specific options for system messages.
|
|
122
|
+
*
|
|
123
|
+
* **Details**
|
|
124
|
+
*
|
|
125
|
+
* These options are used when translating system messages into Anthropic
|
|
126
|
+
* request content.
|
|
127
|
+
*
|
|
128
|
+
* @category request
|
|
129
|
+
* @since 4.0.0
|
|
130
|
+
*/
|
|
89
131
|
export interface SystemMessageOptions extends ProviderOptions {
|
|
90
132
|
readonly anthropic?: {
|
|
91
133
|
/**
|
|
@@ -95,6 +137,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
95
137
|
} | null
|
|
96
138
|
}
|
|
97
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Anthropic-specific options for user messages.
|
|
142
|
+
*
|
|
143
|
+
* **Details**
|
|
144
|
+
*
|
|
145
|
+
* These options are used when translating user messages into Anthropic
|
|
146
|
+
* request content.
|
|
147
|
+
*
|
|
148
|
+
* @category request
|
|
149
|
+
* @since 4.0.0
|
|
150
|
+
*/
|
|
98
151
|
export interface UserMessageOptions extends ProviderOptions {
|
|
99
152
|
readonly anthropic?: {
|
|
100
153
|
/**
|
|
@@ -104,6 +157,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
104
157
|
} | null
|
|
105
158
|
}
|
|
106
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Anthropic-specific options for assistant messages.
|
|
162
|
+
*
|
|
163
|
+
* **Details**
|
|
164
|
+
*
|
|
165
|
+
* These options are used when replaying assistant messages in Anthropic
|
|
166
|
+
* conversation history.
|
|
167
|
+
*
|
|
168
|
+
* @category request
|
|
169
|
+
* @since 4.0.0
|
|
170
|
+
*/
|
|
107
171
|
export interface AssistantMessageOptions extends ProviderOptions {
|
|
108
172
|
readonly anthropic?: {
|
|
109
173
|
/**
|
|
@@ -113,6 +177,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
113
177
|
} | null
|
|
114
178
|
}
|
|
115
179
|
|
|
180
|
+
/**
|
|
181
|
+
* Anthropic-specific options for tool messages.
|
|
182
|
+
*
|
|
183
|
+
* **Details**
|
|
184
|
+
*
|
|
185
|
+
* These options are used when converting tool results into Anthropic user
|
|
186
|
+
* content blocks.
|
|
187
|
+
*
|
|
188
|
+
* @category request
|
|
189
|
+
* @since 4.0.0
|
|
190
|
+
*/
|
|
116
191
|
export interface ToolMessageOptions extends ProviderOptions {
|
|
117
192
|
readonly anthropic?: {
|
|
118
193
|
/**
|
|
@@ -122,6 +197,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
122
197
|
} | null
|
|
123
198
|
}
|
|
124
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Anthropic-specific options for text prompt parts.
|
|
202
|
+
*
|
|
203
|
+
* **When to use**
|
|
204
|
+
*
|
|
205
|
+
* Use these options to control how text blocks are sent to Anthropic.
|
|
206
|
+
*
|
|
207
|
+
* @category request
|
|
208
|
+
* @since 4.0.0
|
|
209
|
+
*/
|
|
125
210
|
export interface TextPartOptions extends ProviderOptions {
|
|
126
211
|
readonly anthropic?: {
|
|
127
212
|
/**
|
|
@@ -131,6 +216,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
131
216
|
} | null
|
|
132
217
|
}
|
|
133
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Anthropic-specific options for reasoning prompt parts.
|
|
221
|
+
*
|
|
222
|
+
* **Details**
|
|
223
|
+
*
|
|
224
|
+
* Preserves Claude thinking metadata when reasoning content is sent back to
|
|
225
|
+
* Anthropic in later turns.
|
|
226
|
+
*
|
|
227
|
+
* @category request
|
|
228
|
+
* @since 4.0.0
|
|
229
|
+
*/
|
|
134
230
|
export interface ReasoningPartOptions extends ProviderOptions {
|
|
135
231
|
readonly anthropic?: {
|
|
136
232
|
readonly info?: {
|
|
@@ -155,6 +251,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
155
251
|
} | null
|
|
156
252
|
}
|
|
157
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Anthropic-specific options for file prompt parts.
|
|
256
|
+
*
|
|
257
|
+
* **Details**
|
|
258
|
+
*
|
|
259
|
+
* Controls document metadata, citations, and prompt caching for files sent to
|
|
260
|
+
* Anthropic.
|
|
261
|
+
*
|
|
262
|
+
* @category request
|
|
263
|
+
* @since 4.0.0
|
|
264
|
+
*/
|
|
158
265
|
export interface FilePartOptions extends ProviderOptions {
|
|
159
266
|
readonly anthropic?: {
|
|
160
267
|
/**
|
|
@@ -174,12 +281,25 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
174
281
|
* Additional context about the document that will be forwarded to the
|
|
175
282
|
* large language model, but will not be used towards cited content.
|
|
176
283
|
*
|
|
284
|
+
* **When to use**
|
|
285
|
+
*
|
|
177
286
|
* Useful for storing additional document metadata as text or stringified JSON.
|
|
178
287
|
*/
|
|
179
288
|
readonly documentContext?: string | null
|
|
180
289
|
} | null
|
|
181
290
|
}
|
|
182
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Anthropic-specific options for tool call prompt parts.
|
|
294
|
+
*
|
|
295
|
+
* **Details**
|
|
296
|
+
*
|
|
297
|
+
* Carries Anthropic tool caller metadata, MCP metadata, and cache control for
|
|
298
|
+
* tool use blocks.
|
|
299
|
+
*
|
|
300
|
+
* @category request
|
|
301
|
+
* @since 4.0.0
|
|
302
|
+
*/
|
|
183
303
|
export interface ToolCallPartOptions extends ProviderOptions {
|
|
184
304
|
readonly anthropic?: {
|
|
185
305
|
readonly caller?: {
|
|
@@ -202,6 +322,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
202
322
|
} | null
|
|
203
323
|
}
|
|
204
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Anthropic-specific options for tool result prompt parts.
|
|
327
|
+
*
|
|
328
|
+
* **Details**
|
|
329
|
+
*
|
|
330
|
+
* Controls Anthropic prompt caching for tool result content.
|
|
331
|
+
*
|
|
332
|
+
* @category request
|
|
333
|
+
* @since 4.0.0
|
|
334
|
+
*/
|
|
205
335
|
export interface ToolResultPartOptions extends ProviderOptions {
|
|
206
336
|
readonly anthropic?: {
|
|
207
337
|
/**
|
|
@@ -211,6 +341,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
211
341
|
} | null
|
|
212
342
|
}
|
|
213
343
|
|
|
344
|
+
/**
|
|
345
|
+
* Anthropic-specific options for tool approval request prompt parts.
|
|
346
|
+
*
|
|
347
|
+
* **Details**
|
|
348
|
+
*
|
|
349
|
+
* Controls prompt caching for human approval requests in conversations.
|
|
350
|
+
*
|
|
351
|
+
* @category request
|
|
352
|
+
* @since 4.0.0
|
|
353
|
+
*/
|
|
214
354
|
export interface ToolApprovalRequestPartOptions extends ProviderOptions {
|
|
215
355
|
readonly anthropic?: {
|
|
216
356
|
/**
|
|
@@ -220,15 +360,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
220
360
|
} | null
|
|
221
361
|
}
|
|
222
362
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
363
|
+
/**
|
|
364
|
+
* Anthropic-specific options for tool approval response prompt parts.
|
|
365
|
+
*
|
|
366
|
+
* **Details**
|
|
367
|
+
*
|
|
368
|
+
* Controls prompt caching for human approval responses in conversations.
|
|
369
|
+
*
|
|
370
|
+
* @category request
|
|
371
|
+
* @since 4.0.0
|
|
372
|
+
*/
|
|
232
373
|
export interface ToolApprovalResponsePartOptions extends ProviderOptions {
|
|
233
374
|
readonly anthropic?: {
|
|
234
375
|
/**
|
|
@@ -240,6 +381,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
240
381
|
}
|
|
241
382
|
|
|
242
383
|
declare module "effect/unstable/ai/Response" {
|
|
384
|
+
/**
|
|
385
|
+
* Anthropic metadata attached when a reasoning block begins.
|
|
386
|
+
*
|
|
387
|
+
* **Details**
|
|
388
|
+
*
|
|
389
|
+
* Includes Claude thinking metadata needed to continue reasoning-aware
|
|
390
|
+
* conversations.
|
|
391
|
+
*
|
|
392
|
+
* @category response
|
|
393
|
+
* @since 4.0.0
|
|
394
|
+
*/
|
|
243
395
|
export interface ReasoningStartPartMetadata extends ProviderMetadata {
|
|
244
396
|
readonly anthropic?: {
|
|
245
397
|
readonly info?: {
|
|
@@ -260,6 +412,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
260
412
|
} | null
|
|
261
413
|
}
|
|
262
414
|
|
|
415
|
+
/**
|
|
416
|
+
* Anthropic metadata attached to streaming reasoning deltas.
|
|
417
|
+
*
|
|
418
|
+
* **Details**
|
|
419
|
+
*
|
|
420
|
+
* Includes the signature for streamed Claude thinking content when available.
|
|
421
|
+
*
|
|
422
|
+
* @category response
|
|
423
|
+
* @since 4.0.0
|
|
424
|
+
*/
|
|
263
425
|
export interface ReasoningDeltaPartMetadata extends ProviderMetadata {
|
|
264
426
|
readonly anthropic?: {
|
|
265
427
|
readonly info?: {
|
|
@@ -273,6 +435,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
273
435
|
} | null
|
|
274
436
|
}
|
|
275
437
|
|
|
438
|
+
/**
|
|
439
|
+
* Anthropic metadata attached to completed reasoning parts.
|
|
440
|
+
*
|
|
441
|
+
* **Details**
|
|
442
|
+
*
|
|
443
|
+
* Preserves Claude thinking or redacted thinking information for later turns.
|
|
444
|
+
*
|
|
445
|
+
* @category response
|
|
446
|
+
* @since 4.0.0
|
|
447
|
+
*/
|
|
276
448
|
export interface ReasoningPartMetadata extends ProviderMetadata {
|
|
277
449
|
readonly anthropic?: {
|
|
278
450
|
readonly info?: {
|
|
@@ -293,6 +465,17 @@ declare module "effect/unstable/ai/Response" {
|
|
|
293
465
|
} | null
|
|
294
466
|
}
|
|
295
467
|
|
|
468
|
+
/**
|
|
469
|
+
* Anthropic metadata attached to tool call response parts.
|
|
470
|
+
*
|
|
471
|
+
* **Details**
|
|
472
|
+
*
|
|
473
|
+
* Identifies Anthropic caller details and MCP tool metadata emitted by the
|
|
474
|
+
* provider.
|
|
475
|
+
*
|
|
476
|
+
* @category response
|
|
477
|
+
* @since 4.0.0
|
|
478
|
+
*/
|
|
296
479
|
export interface ToolCallPartMetadata extends ProviderMetadata {
|
|
297
480
|
readonly anthropic?: {
|
|
298
481
|
readonly caller?: {
|
|
@@ -311,6 +494,17 @@ declare module "effect/unstable/ai/Response" {
|
|
|
311
494
|
} | null
|
|
312
495
|
}
|
|
313
496
|
|
|
497
|
+
/**
|
|
498
|
+
* Anthropic metadata attached to tool result response parts.
|
|
499
|
+
*
|
|
500
|
+
* **Details**
|
|
501
|
+
*
|
|
502
|
+
* Identifies MCP tool metadata associated with provider-executed tool
|
|
503
|
+
* results.
|
|
504
|
+
*
|
|
505
|
+
* @category response
|
|
506
|
+
* @since 4.0.0
|
|
507
|
+
*/
|
|
314
508
|
export interface ToolResultPartMetadata extends ProviderMetadata {
|
|
315
509
|
readonly anthropic?: {
|
|
316
510
|
/**
|
|
@@ -325,6 +519,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
325
519
|
} | null
|
|
326
520
|
}
|
|
327
521
|
|
|
522
|
+
/**
|
|
523
|
+
* Anthropic metadata for document citations in model responses.
|
|
524
|
+
*
|
|
525
|
+
* **Details**
|
|
526
|
+
*
|
|
527
|
+
* Records the cited document span by character position or page number.
|
|
528
|
+
*
|
|
529
|
+
* @category response
|
|
530
|
+
* @since 4.0.0
|
|
531
|
+
*/
|
|
328
532
|
export interface DocumentSourcePartMetadata extends ProviderMetadata {
|
|
329
533
|
readonly anthropic?: {
|
|
330
534
|
readonly source: "document"
|
|
@@ -359,6 +563,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
359
563
|
} | null
|
|
360
564
|
}
|
|
361
565
|
|
|
566
|
+
/**
|
|
567
|
+
* Anthropic metadata for URL and web citations in model responses.
|
|
568
|
+
*
|
|
569
|
+
* **Details**
|
|
570
|
+
*
|
|
571
|
+
* Records cited URL text or web-search source freshness information.
|
|
572
|
+
*
|
|
573
|
+
* @category response
|
|
574
|
+
* @since 4.0.0
|
|
575
|
+
*/
|
|
362
576
|
export interface UrlSourcePartMetadata extends ProviderMetadata {
|
|
363
577
|
readonly anthropic?: {
|
|
364
578
|
readonly source: "url"
|
|
@@ -378,6 +592,17 @@ declare module "effect/unstable/ai/Response" {
|
|
|
378
592
|
} | null
|
|
379
593
|
}
|
|
380
594
|
|
|
595
|
+
/**
|
|
596
|
+
* Anthropic metadata attached to the finish part of a response.
|
|
597
|
+
*
|
|
598
|
+
* **Details**
|
|
599
|
+
*
|
|
600
|
+
* Includes container state, context management information, stop details, and
|
|
601
|
+
* token usage reported by Anthropic.
|
|
602
|
+
*
|
|
603
|
+
* @category response
|
|
604
|
+
* @since 4.0.0
|
|
605
|
+
*/
|
|
381
606
|
export interface FinishPartMetadata extends ProviderMetadata {
|
|
382
607
|
readonly anthropic?: {
|
|
383
608
|
readonly container: typeof Generated.BetaContainer.Encoded | null
|
|
@@ -387,6 +612,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
387
612
|
} | null
|
|
388
613
|
}
|
|
389
614
|
|
|
615
|
+
/**
|
|
616
|
+
* Anthropic metadata attached to error response parts.
|
|
617
|
+
*
|
|
618
|
+
* **Details**
|
|
619
|
+
*
|
|
620
|
+
* Includes the provider request identifier when Anthropic returns one.
|
|
621
|
+
*
|
|
622
|
+
* @category response
|
|
623
|
+
* @since 4.0.0
|
|
624
|
+
*/
|
|
390
625
|
export interface ErrorPartMetadata extends ProviderMetadata {
|
|
391
626
|
readonly anthropic?: {
|
|
392
627
|
requestId?: string | null
|
|
@@ -401,20 +636,20 @@ declare module "effect/unstable/ai/Response" {
|
|
|
401
636
|
/**
|
|
402
637
|
* Creates an Anthropic language model that can be used with `AiModel.provide`.
|
|
403
638
|
*
|
|
404
|
-
* @since 1.0.0
|
|
405
639
|
* @category constructors
|
|
640
|
+
* @since 4.0.0
|
|
406
641
|
*/
|
|
407
642
|
export const model = (
|
|
408
643
|
model: (string & {}) | Model,
|
|
409
644
|
config?: Omit<typeof Config.Service, "model">
|
|
410
645
|
): AiModel.Model<"anthropic", LanguageModel.LanguageModel, AnthropicClient> =>
|
|
411
|
-
AiModel.make("anthropic", layer({ model, config }))
|
|
646
|
+
AiModel.make("anthropic", model, layer({ model, config }))
|
|
412
647
|
|
|
413
648
|
/**
|
|
414
649
|
* Creates an Anthropic language model service.
|
|
415
650
|
*
|
|
416
|
-
* @since 1.0.0
|
|
417
651
|
* @category constructors
|
|
652
|
+
* @since 4.0.0
|
|
418
653
|
*/
|
|
419
654
|
export const make = Effect.fnUntraced(function*({ model, config: providerConfig }: {
|
|
420
655
|
readonly model: (string & {}) | Model
|
|
@@ -423,7 +658,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
423
658
|
const client = yield* AnthropicClient
|
|
424
659
|
|
|
425
660
|
const makeConfig: Effect.Effect<typeof Config.Service & { readonly model: string }> = Effect.gen(function*() {
|
|
426
|
-
const services = yield* Effect.
|
|
661
|
+
const services = yield* Effect.context<never>()
|
|
427
662
|
return { model, ...providerConfig, ...services.mapUnsafe.get(Config.key) }
|
|
428
663
|
})
|
|
429
664
|
|
|
@@ -469,6 +704,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
469
704
|
)
|
|
470
705
|
|
|
471
706
|
return yield* LanguageModel.make({
|
|
707
|
+
codecTransformer: toCodecAnthropic,
|
|
472
708
|
generateText: Effect.fnUntraced(function*(options) {
|
|
473
709
|
const config = yield* makeConfig
|
|
474
710
|
const toolNameMapper = new Tool.NameMapper(options.tools)
|
|
@@ -493,17 +729,14 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
493
729
|
return response
|
|
494
730
|
})
|
|
495
731
|
))
|
|
496
|
-
})
|
|
497
|
-
LanguageModel.CurrentCodecTransformer,
|
|
498
|
-
toCodecAnthropic
|
|
499
|
-
))
|
|
732
|
+
})
|
|
500
733
|
})
|
|
501
734
|
|
|
502
735
|
/**
|
|
503
736
|
* Creates a layer for the Anthropic language model.
|
|
504
737
|
*
|
|
505
|
-
* @since 1.0.0
|
|
506
738
|
* @category layers
|
|
739
|
+
* @since 4.0.0
|
|
507
740
|
*/
|
|
508
741
|
export const layer = (options: {
|
|
509
742
|
readonly model: (string & {}) | Model
|
|
@@ -514,37 +747,37 @@ export const layer = (options: {
|
|
|
514
747
|
/**
|
|
515
748
|
* Provides config overrides for Anthropic language model operations.
|
|
516
749
|
*
|
|
517
|
-
* @since 1.0.0
|
|
518
750
|
* @category configuration
|
|
751
|
+
* @since 4.0.0
|
|
519
752
|
*/
|
|
520
753
|
export const withConfigOverride: {
|
|
521
754
|
/**
|
|
522
755
|
* Provides config overrides for Anthropic language model operations.
|
|
523
756
|
*
|
|
524
|
-
* @since 1.0.0
|
|
525
757
|
* @category configuration
|
|
758
|
+
* @since 4.0.0
|
|
526
759
|
*/
|
|
527
760
|
(overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
528
761
|
/**
|
|
529
762
|
* Provides config overrides for Anthropic language model operations.
|
|
530
763
|
*
|
|
531
|
-
* @since 1.0.0
|
|
532
764
|
* @category configuration
|
|
765
|
+
* @since 4.0.0
|
|
533
766
|
*/
|
|
534
767
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>
|
|
535
768
|
} = dual<
|
|
536
769
|
/**
|
|
537
770
|
* Provides config overrides for Anthropic language model operations.
|
|
538
771
|
*
|
|
539
|
-
* @since 1.0.0
|
|
540
772
|
* @category configuration
|
|
773
|
+
* @since 4.0.0
|
|
541
774
|
*/
|
|
542
775
|
(overrides: typeof Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
|
|
543
776
|
/**
|
|
544
777
|
* Provides config overrides for Anthropic language model operations.
|
|
545
778
|
*
|
|
546
|
-
* @since 1.0.0
|
|
547
779
|
* @category configuration
|
|
780
|
+
* @since 4.0.0
|
|
548
781
|
*/
|
|
549
782
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
550
783
|
>(2, (self, overrides) =>
|
|
@@ -964,19 +1197,21 @@ const prepareMessages = Effect.fnUntraced(
|
|
|
964
1197
|
/**
|
|
965
1198
|
* Represents a user-defined tool that can be passed to the Anthropic API.
|
|
966
1199
|
*
|
|
967
|
-
* @since 1.0.0
|
|
968
1200
|
* @category tools
|
|
1201
|
+
* @since 4.0.0
|
|
969
1202
|
*/
|
|
970
1203
|
export type AnthropicUserDefinedTool = typeof Generated.BetaTool.Encoded
|
|
971
1204
|
|
|
972
1205
|
/**
|
|
973
1206
|
* Represents a provider-defined tool that can be passed to the Anthropic API.
|
|
974
1207
|
*
|
|
1208
|
+
* **Details**
|
|
1209
|
+
*
|
|
975
1210
|
* These include Anthropic's built-in tools like computer use, code execution,
|
|
976
1211
|
* web search, and text editing.
|
|
977
1212
|
*
|
|
978
|
-
* @since 1.0.0
|
|
979
1213
|
* @category tools
|
|
1214
|
+
* @since 4.0.0
|
|
980
1215
|
*/
|
|
981
1216
|
export type AnthropicProviderDefinedTool =
|
|
982
1217
|
| typeof Generated.BetaBashTool_20241022.Encoded
|
|
@@ -1033,9 +1268,9 @@ const prepareTools = Effect.fnUntraced(
|
|
|
1033
1268
|
const providerTools: Array<AnthropicProviderDefinedTool> = []
|
|
1034
1269
|
|
|
1035
1270
|
for (const tool of options.tools) {
|
|
1036
|
-
if (Tool.isUserDefined(tool)) {
|
|
1271
|
+
if (Tool.isUserDefined(tool) || Tool.isDynamic(tool)) {
|
|
1037
1272
|
const description = Tool.getDescription(tool)
|
|
1038
|
-
const input_schema = yield*
|
|
1273
|
+
const input_schema = yield* tryToolJsonSchema(tool, "prepareTools")
|
|
1039
1274
|
const toolStrict = Tool.getStrictMode(tool)
|
|
1040
1275
|
const strict = capabilities.supportsStructuredOutput
|
|
1041
1276
|
? (toolStrict ?? config.strictJsonSchema ?? true)
|
|
@@ -1248,7 +1483,7 @@ const buildHttpRequestDetails = (
|
|
|
1248
1483
|
method: request.method,
|
|
1249
1484
|
url: request.url,
|
|
1250
1485
|
urlParams: Array.from(request.urlParams),
|
|
1251
|
-
hash: request.hash,
|
|
1486
|
+
hash: Option.getOrUndefined(request.hash),
|
|
1252
1487
|
headers: Redactable.redact(request.headers) as Record<string, string>
|
|
1253
1488
|
})
|
|
1254
1489
|
|
|
@@ -2772,6 +3007,12 @@ const tryJsonSchema = <S extends Schema.Top>(schema: S, method: string) =>
|
|
|
2772
3007
|
catch: (error) => unsupportedSchemaError(error, method)
|
|
2773
3008
|
})
|
|
2774
3009
|
|
|
3010
|
+
const tryToolJsonSchema = <T extends Tool.Any | Tool.AnyDynamic>(tool: T, method: string) =>
|
|
3011
|
+
Effect.try({
|
|
3012
|
+
try: () => Tool.getJsonSchema(tool, { transformer: toCodecAnthropic }),
|
|
3013
|
+
catch: (error) => unsupportedSchemaError(error, method)
|
|
3014
|
+
})
|
|
3015
|
+
|
|
2775
3016
|
const getOutputFormat = Effect.fnUntraced(function*({ capabilities, options }: {
|
|
2776
3017
|
readonly capabilities: ModelCapabilities
|
|
2777
3018
|
readonly options: LanguageModel.ProviderOptions
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* semantic conventions, extending the base GenAI attributes with Anthropic-specific
|
|
6
6
|
* request and response metadata.
|
|
7
7
|
*
|
|
8
|
-
* @since
|
|
8
|
+
* @since 4.0.0
|
|
9
9
|
*/
|
|
10
10
|
import { dual } from "effect/Function"
|
|
11
11
|
import * as String from "effect/String"
|
|
@@ -17,10 +17,9 @@ import * as Telemetry from "effect/unstable/ai/Telemetry"
|
|
|
17
17
|
* The attributes used to describe telemetry in the context of Generative
|
|
18
18
|
* Artificial Intelligence (GenAI) Models requests and responses.
|
|
19
19
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @since 1.0.0
|
|
20
|
+
* @see https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
|
|
23
21
|
* @category models
|
|
22
|
+
* @since 4.0.0
|
|
24
23
|
*/
|
|
25
24
|
export type AnthropicTelemetryAttributes = Simplify<
|
|
26
25
|
& Telemetry.GenAITelemetryAttributes
|
|
@@ -32,8 +31,8 @@ export type AnthropicTelemetryAttributes = Simplify<
|
|
|
32
31
|
* All telemetry attributes which are part of the GenAI specification,
|
|
33
32
|
* including the Anthropic-specific attributes.
|
|
34
33
|
*
|
|
35
|
-
* @since 1.0.0
|
|
36
34
|
* @category models
|
|
35
|
+
* @since 4.0.0
|
|
37
36
|
*/
|
|
38
37
|
export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
|
|
39
38
|
|
|
@@ -41,8 +40,8 @@ export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & Respon
|
|
|
41
40
|
* Telemetry attributes which are part of the GenAI specification and are
|
|
42
41
|
* namespaced by `gen_ai.anthropic.request`.
|
|
43
42
|
*
|
|
44
|
-
* @since 1.0.0
|
|
45
43
|
* @category models
|
|
44
|
+
* @since 4.0.0
|
|
46
45
|
*/
|
|
47
46
|
export interface RequestAttributes {
|
|
48
47
|
/**
|
|
@@ -59,8 +58,8 @@ export interface RequestAttributes {
|
|
|
59
58
|
* Telemetry attributes which are part of the GenAI specification and are
|
|
60
59
|
* namespaced by `gen_ai.anthropic.response`.
|
|
61
60
|
*
|
|
62
|
-
* @since 1.0.0
|
|
63
61
|
* @category models
|
|
62
|
+
* @since 4.0.0
|
|
64
63
|
*/
|
|
65
64
|
export interface ResponseAttributes {
|
|
66
65
|
/**
|
|
@@ -78,8 +77,10 @@ export interface ResponseAttributes {
|
|
|
78
77
|
}
|
|
79
78
|
|
|
80
79
|
/**
|
|
81
|
-
*
|
|
80
|
+
* Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry attributes with optional Anthropic request and response attributes.
|
|
81
|
+
*
|
|
82
82
|
* @category models
|
|
83
|
+
* @since 4.0.0
|
|
83
84
|
*/
|
|
84
85
|
export type AnthropicTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
|
|
85
86
|
anthropic?: {
|
|
@@ -99,30 +100,36 @@ const addAnthropicResponseAttributes = Telemetry.addSpanAttributes("gen_ai.anthr
|
|
|
99
100
|
* Applies the specified Anthropic GenAI telemetry attributes to the provided
|
|
100
101
|
* `Span`.
|
|
101
102
|
*
|
|
102
|
-
* **
|
|
103
|
+
* **Gotchas**
|
|
104
|
+
*
|
|
105
|
+
* This method mutates the `Span` in place.
|
|
103
106
|
*
|
|
104
|
-
* @
|
|
105
|
-
* @
|
|
107
|
+
* @category utils
|
|
108
|
+
* @since 4.0.0
|
|
106
109
|
*/
|
|
107
110
|
export const addGenAIAnnotations: {
|
|
108
111
|
/**
|
|
109
112
|
* Applies the specified Anthropic GenAI telemetry attributes to the provided
|
|
110
113
|
* `Span`.
|
|
111
114
|
*
|
|
112
|
-
* **
|
|
115
|
+
* **Gotchas**
|
|
113
116
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
117
|
+
* This method mutates the `Span` in place.
|
|
118
|
+
*
|
|
119
|
+
* @category utils
|
|
120
|
+
* @since 4.0.0
|
|
116
121
|
*/
|
|
117
122
|
(options: AnthropicTelemetryAttributeOptions): (span: Span) => void
|
|
118
123
|
/**
|
|
119
124
|
* Applies the specified Anthropic GenAI telemetry attributes to the provided
|
|
120
125
|
* `Span`.
|
|
121
126
|
*
|
|
122
|
-
* **
|
|
127
|
+
* **Gotchas**
|
|
128
|
+
*
|
|
129
|
+
* This method mutates the `Span` in place.
|
|
123
130
|
*
|
|
124
|
-
* @
|
|
125
|
-
* @
|
|
131
|
+
* @category utils
|
|
132
|
+
* @since 4.0.0
|
|
126
133
|
*/
|
|
127
134
|
(span: Span, options: AnthropicTelemetryAttributeOptions): void
|
|
128
135
|
} = dual(2, (span: Span, options: AnthropicTelemetryAttributeOptions) => {
|