@effect/ai-anthropic 4.0.0-beta.65 → 4.0.0-beta.67

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.
@@ -1,5 +1,31 @@
1
1
  /**
2
- * @since 1.0.0
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
+ * **Common tasks**
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"
@@ -36,8 +62,8 @@ import * as InternalUtilities from "./internal/utilities.ts"
36
62
  /**
37
63
  * The available Anthropic Claude model identifiers.
38
64
  *
39
- * @since 1.0.0
40
65
  * @category models
66
+ * @since 4.0.0
41
67
  */
42
68
  export type Model = typeof Generated.Model.Type
43
69
 
@@ -51,8 +77,8 @@ export type Model = typeof Generated.Model.Type
51
77
  * This service can be used to provide default configuration values or to
52
78
  * override configuration on a per-request basis.
53
79
  *
54
- * @since 1.0.0
55
80
  * @category configuration
81
+ * @since 4.0.0
56
82
  */
57
83
  export class Config extends Context.Service<
58
84
  Config,
@@ -87,6 +113,12 @@ export class Config extends Context.Service<
87
113
  // =============================================================================
88
114
 
89
115
  declare module "effect/unstable/ai/Prompt" {
116
+ /**
117
+ * Anthropic-specific options for system messages.
118
+ *
119
+ * These options are used when translating system messages into Anthropic
120
+ * request content.
121
+ */
90
122
  export interface SystemMessageOptions extends ProviderOptions {
91
123
  readonly anthropic?: {
92
124
  /**
@@ -96,6 +128,12 @@ declare module "effect/unstable/ai/Prompt" {
96
128
  } | null
97
129
  }
98
130
 
131
+ /**
132
+ * Anthropic-specific options for user messages.
133
+ *
134
+ * These options are used when translating user messages into Anthropic
135
+ * request content.
136
+ */
99
137
  export interface UserMessageOptions extends ProviderOptions {
100
138
  readonly anthropic?: {
101
139
  /**
@@ -105,6 +143,12 @@ declare module "effect/unstable/ai/Prompt" {
105
143
  } | null
106
144
  }
107
145
 
146
+ /**
147
+ * Anthropic-specific options for assistant messages.
148
+ *
149
+ * These options are used when replaying assistant messages in Anthropic
150
+ * conversation history.
151
+ */
108
152
  export interface AssistantMessageOptions extends ProviderOptions {
109
153
  readonly anthropic?: {
110
154
  /**
@@ -114,6 +158,12 @@ declare module "effect/unstable/ai/Prompt" {
114
158
  } | null
115
159
  }
116
160
 
161
+ /**
162
+ * Anthropic-specific options for tool messages.
163
+ *
164
+ * These options are used when converting tool results into Anthropic user
165
+ * content blocks.
166
+ */
117
167
  export interface ToolMessageOptions extends ProviderOptions {
118
168
  readonly anthropic?: {
119
169
  /**
@@ -123,6 +173,11 @@ declare module "effect/unstable/ai/Prompt" {
123
173
  } | null
124
174
  }
125
175
 
176
+ /**
177
+ * Anthropic-specific options for text prompt parts.
178
+ *
179
+ * Use these options to control how text blocks are sent to Anthropic.
180
+ */
126
181
  export interface TextPartOptions extends ProviderOptions {
127
182
  readonly anthropic?: {
128
183
  /**
@@ -132,6 +187,12 @@ declare module "effect/unstable/ai/Prompt" {
132
187
  } | null
133
188
  }
134
189
 
190
+ /**
191
+ * Anthropic-specific options for reasoning prompt parts.
192
+ *
193
+ * Preserves Claude thinking metadata when reasoning content is sent back to
194
+ * Anthropic in later turns.
195
+ */
135
196
  export interface ReasoningPartOptions extends ProviderOptions {
136
197
  readonly anthropic?: {
137
198
  readonly info?: {
@@ -156,6 +217,12 @@ declare module "effect/unstable/ai/Prompt" {
156
217
  } | null
157
218
  }
158
219
 
220
+ /**
221
+ * Anthropic-specific options for file prompt parts.
222
+ *
223
+ * Controls document metadata, citations, and prompt caching for files sent to
224
+ * Anthropic.
225
+ */
159
226
  export interface FilePartOptions extends ProviderOptions {
160
227
  readonly anthropic?: {
161
228
  /**
@@ -181,6 +248,12 @@ declare module "effect/unstable/ai/Prompt" {
181
248
  } | null
182
249
  }
183
250
 
251
+ /**
252
+ * Anthropic-specific options for tool call prompt parts.
253
+ *
254
+ * Carries Anthropic tool caller metadata, MCP metadata, and cache control for
255
+ * tool use blocks.
256
+ */
184
257
  export interface ToolCallPartOptions extends ProviderOptions {
185
258
  readonly anthropic?: {
186
259
  readonly caller?: {
@@ -203,6 +276,11 @@ declare module "effect/unstable/ai/Prompt" {
203
276
  } | null
204
277
  }
205
278
 
279
+ /**
280
+ * Anthropic-specific options for tool result prompt parts.
281
+ *
282
+ * Controls Anthropic prompt caching for tool result content.
283
+ */
206
284
  export interface ToolResultPartOptions extends ProviderOptions {
207
285
  readonly anthropic?: {
208
286
  /**
@@ -212,6 +290,11 @@ declare module "effect/unstable/ai/Prompt" {
212
290
  } | null
213
291
  }
214
292
 
293
+ /**
294
+ * Anthropic-specific options for tool approval request prompt parts.
295
+ *
296
+ * Controls prompt caching for human approval requests in conversations.
297
+ */
215
298
  export interface ToolApprovalRequestPartOptions extends ProviderOptions {
216
299
  readonly anthropic?: {
217
300
  /**
@@ -221,6 +304,11 @@ declare module "effect/unstable/ai/Prompt" {
221
304
  } | null
222
305
  }
223
306
 
307
+ /**
308
+ * Anthropic-specific options for tool approval response prompt parts.
309
+ *
310
+ * Controls prompt caching for human approval responses in conversations.
311
+ */
224
312
  export interface ToolApprovalResponsePartOptions extends ProviderOptions {
225
313
  readonly anthropic?: {
226
314
  /**
@@ -232,6 +320,12 @@ declare module "effect/unstable/ai/Prompt" {
232
320
  }
233
321
 
234
322
  declare module "effect/unstable/ai/Response" {
323
+ /**
324
+ * Anthropic metadata attached when a reasoning block begins.
325
+ *
326
+ * Includes Claude thinking metadata needed to continue reasoning-aware
327
+ * conversations.
328
+ */
235
329
  export interface ReasoningStartPartMetadata extends ProviderMetadata {
236
330
  readonly anthropic?: {
237
331
  readonly info?: {
@@ -252,6 +346,11 @@ declare module "effect/unstable/ai/Response" {
252
346
  } | null
253
347
  }
254
348
 
349
+ /**
350
+ * Anthropic metadata attached to streaming reasoning deltas.
351
+ *
352
+ * Includes the signature for streamed Claude thinking content when available.
353
+ */
255
354
  export interface ReasoningDeltaPartMetadata extends ProviderMetadata {
256
355
  readonly anthropic?: {
257
356
  readonly info?: {
@@ -265,6 +364,11 @@ declare module "effect/unstable/ai/Response" {
265
364
  } | null
266
365
  }
267
366
 
367
+ /**
368
+ * Anthropic metadata attached to completed reasoning parts.
369
+ *
370
+ * Preserves Claude thinking or redacted thinking information for later turns.
371
+ */
268
372
  export interface ReasoningPartMetadata extends ProviderMetadata {
269
373
  readonly anthropic?: {
270
374
  readonly info?: {
@@ -285,6 +389,12 @@ declare module "effect/unstable/ai/Response" {
285
389
  } | null
286
390
  }
287
391
 
392
+ /**
393
+ * Anthropic metadata attached to tool call response parts.
394
+ *
395
+ * Identifies Anthropic caller details and MCP tool metadata emitted by the
396
+ * provider.
397
+ */
288
398
  export interface ToolCallPartMetadata extends ProviderMetadata {
289
399
  readonly anthropic?: {
290
400
  readonly caller?: {
@@ -303,6 +413,12 @@ declare module "effect/unstable/ai/Response" {
303
413
  } | null
304
414
  }
305
415
 
416
+ /**
417
+ * Anthropic metadata attached to tool result response parts.
418
+ *
419
+ * Identifies MCP tool metadata associated with provider-executed tool
420
+ * results.
421
+ */
306
422
  export interface ToolResultPartMetadata extends ProviderMetadata {
307
423
  readonly anthropic?: {
308
424
  /**
@@ -317,6 +433,11 @@ declare module "effect/unstable/ai/Response" {
317
433
  } | null
318
434
  }
319
435
 
436
+ /**
437
+ * Anthropic metadata for document citations in model responses.
438
+ *
439
+ * Records the cited document span by character position or page number.
440
+ */
320
441
  export interface DocumentSourcePartMetadata extends ProviderMetadata {
321
442
  readonly anthropic?: {
322
443
  readonly source: "document"
@@ -351,6 +472,11 @@ declare module "effect/unstable/ai/Response" {
351
472
  } | null
352
473
  }
353
474
 
475
+ /**
476
+ * Anthropic metadata for URL and web citations in model responses.
477
+ *
478
+ * Records cited URL text or web-search source freshness information.
479
+ */
354
480
  export interface UrlSourcePartMetadata extends ProviderMetadata {
355
481
  readonly anthropic?: {
356
482
  readonly source: "url"
@@ -370,6 +496,12 @@ declare module "effect/unstable/ai/Response" {
370
496
  } | null
371
497
  }
372
498
 
499
+ /**
500
+ * Anthropic metadata attached to the finish part of a response.
501
+ *
502
+ * Includes container state, context management information, stop details, and
503
+ * token usage reported by Anthropic.
504
+ */
373
505
  export interface FinishPartMetadata extends ProviderMetadata {
374
506
  readonly anthropic?: {
375
507
  readonly container: typeof Generated.BetaContainer.Encoded | null
@@ -379,6 +511,11 @@ declare module "effect/unstable/ai/Response" {
379
511
  } | null
380
512
  }
381
513
 
514
+ /**
515
+ * Anthropic metadata attached to error response parts.
516
+ *
517
+ * Includes the provider request identifier when Anthropic returns one.
518
+ */
382
519
  export interface ErrorPartMetadata extends ProviderMetadata {
383
520
  readonly anthropic?: {
384
521
  requestId?: string | null
@@ -393,8 +530,8 @@ declare module "effect/unstable/ai/Response" {
393
530
  /**
394
531
  * Creates an Anthropic language model that can be used with `AiModel.provide`.
395
532
  *
396
- * @since 1.0.0
397
533
  * @category constructors
534
+ * @since 4.0.0
398
535
  */
399
536
  export const model = (
400
537
  model: (string & {}) | Model,
@@ -405,8 +542,8 @@ export const model = (
405
542
  /**
406
543
  * Creates an Anthropic language model service.
407
544
  *
408
- * @since 1.0.0
409
545
  * @category constructors
546
+ * @since 4.0.0
410
547
  */
411
548
  export const make = Effect.fnUntraced(function*({ model, config: providerConfig }: {
412
549
  readonly model: (string & {}) | Model
@@ -492,8 +629,8 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
492
629
  /**
493
630
  * Creates a layer for the Anthropic language model.
494
631
  *
495
- * @since 1.0.0
496
632
  * @category layers
633
+ * @since 4.0.0
497
634
  */
498
635
  export const layer = (options: {
499
636
  readonly model: (string & {}) | Model
@@ -504,37 +641,37 @@ export const layer = (options: {
504
641
  /**
505
642
  * Provides config overrides for Anthropic language model operations.
506
643
  *
507
- * @since 1.0.0
508
644
  * @category configuration
645
+ * @since 4.0.0
509
646
  */
510
647
  export const withConfigOverride: {
511
648
  /**
512
649
  * Provides config overrides for Anthropic language model operations.
513
650
  *
514
- * @since 1.0.0
515
651
  * @category configuration
652
+ * @since 4.0.0
516
653
  */
517
654
  (overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>
518
655
  /**
519
656
  * Provides config overrides for Anthropic language model operations.
520
657
  *
521
- * @since 1.0.0
522
658
  * @category configuration
659
+ * @since 4.0.0
523
660
  */
524
661
  <A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>
525
662
  } = dual<
526
663
  /**
527
664
  * Provides config overrides for Anthropic language model operations.
528
665
  *
529
- * @since 1.0.0
530
666
  * @category configuration
667
+ * @since 4.0.0
531
668
  */
532
669
  (overrides: typeof Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
533
670
  /**
534
671
  * Provides config overrides for Anthropic language model operations.
535
672
  *
536
- * @since 1.0.0
537
673
  * @category configuration
674
+ * @since 4.0.0
538
675
  */
539
676
  <A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>>
540
677
  >(2, (self, overrides) =>
@@ -954,8 +1091,8 @@ const prepareMessages = Effect.fnUntraced(
954
1091
  /**
955
1092
  * Represents a user-defined tool that can be passed to the Anthropic API.
956
1093
  *
957
- * @since 1.0.0
958
1094
  * @category tools
1095
+ * @since 4.0.0
959
1096
  */
960
1097
  export type AnthropicUserDefinedTool = typeof Generated.BetaTool.Encoded
961
1098
 
@@ -965,8 +1102,8 @@ export type AnthropicUserDefinedTool = typeof Generated.BetaTool.Encoded
965
1102
  * These include Anthropic's built-in tools like computer use, code execution,
966
1103
  * web search, and text editing.
967
1104
  *
968
- * @since 1.0.0
969
1105
  * @category tools
1106
+ * @since 4.0.0
970
1107
  */
971
1108
  export type AnthropicProviderDefinedTool =
972
1109
  | typeof Generated.BetaBashTool_20241022.Encoded
@@ -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 1.0.0
8
+ * @since 4.0.0
9
9
  */
10
10
  import { dual } from "effect/Function"
11
11
  import * as String from "effect/String"
@@ -19,8 +19,8 @@ import * as Telemetry from "effect/unstable/ai/Telemetry"
19
19
  *
20
20
  * {@see https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/}
21
21
  *
22
- * @since 1.0.0
23
22
  * @category models
23
+ * @since 4.0.0
24
24
  */
25
25
  export type AnthropicTelemetryAttributes = Simplify<
26
26
  & Telemetry.GenAITelemetryAttributes
@@ -32,8 +32,8 @@ export type AnthropicTelemetryAttributes = Simplify<
32
32
  * All telemetry attributes which are part of the GenAI specification,
33
33
  * including the Anthropic-specific attributes.
34
34
  *
35
- * @since 1.0.0
36
35
  * @category models
36
+ * @since 4.0.0
37
37
  */
38
38
  export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & ResponseAttributes
39
39
 
@@ -41,8 +41,8 @@ export type AllAttributes = Telemetry.AllAttributes & RequestAttributes & Respon
41
41
  * Telemetry attributes which are part of the GenAI specification and are
42
42
  * namespaced by `gen_ai.anthropic.request`.
43
43
  *
44
- * @since 1.0.0
45
44
  * @category models
45
+ * @since 4.0.0
46
46
  */
47
47
  export interface RequestAttributes {
48
48
  /**
@@ -59,8 +59,8 @@ export interface RequestAttributes {
59
59
  * Telemetry attributes which are part of the GenAI specification and are
60
60
  * namespaced by `gen_ai.anthropic.response`.
61
61
  *
62
- * @since 1.0.0
63
62
  * @category models
63
+ * @since 4.0.0
64
64
  */
65
65
  export interface ResponseAttributes {
66
66
  /**
@@ -78,8 +78,10 @@ export interface ResponseAttributes {
78
78
  }
79
79
 
80
80
  /**
81
- * @since 1.0.0
81
+ * Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry attributes with optional Anthropic request and response attributes.
82
+ *
82
83
  * @category models
84
+ * @since 4.0.0
83
85
  */
84
86
  export type AnthropicTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
85
87
  anthropic?: {
@@ -101,8 +103,8 @@ const addAnthropicResponseAttributes = Telemetry.addSpanAttributes("gen_ai.anthr
101
103
  *
102
104
  * **NOTE**: This method will mutate the `Span` **in-place**.
103
105
  *
104
- * @since 1.0.0
105
106
  * @category utilities
107
+ * @since 4.0.0
106
108
  */
107
109
  export const addGenAIAnnotations: {
108
110
  /**
@@ -111,8 +113,8 @@ export const addGenAIAnnotations: {
111
113
  *
112
114
  * **NOTE**: This method will mutate the `Span` **in-place**.
113
115
  *
114
- * @since 1.0.0
115
116
  * @category utilities
117
+ * @since 4.0.0
116
118
  */
117
119
  (options: AnthropicTelemetryAttributeOptions): (span: Span) => void
118
120
  /**
@@ -121,8 +123,8 @@ export const addGenAIAnnotations: {
121
123
  *
122
124
  * **NOTE**: This method will mutate the `Span` **in-place**.
123
125
  *
124
- * @since 1.0.0
125
126
  * @category utilities
127
+ * @since 4.0.0
126
128
  */
127
129
  (span: Span, options: AnthropicTelemetryAttributeOptions): void
128
130
  } = dual(2, (span: Span, options: AnthropicTelemetryAttributeOptions) => {