@effect/ai-anthropic 4.0.0-beta.8 → 4.0.0-beta.81
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 +93 -74
- package/dist/AnthropicClient.d.ts.map +1 -1
- package/dist/AnthropicClient.js +51 -19
- package/dist/AnthropicClient.js.map +1 -1
- package/dist/AnthropicConfig.d.ts +55 -10
- package/dist/AnthropicConfig.d.ts.map +1 -1
- package/dist/AnthropicConfig.js +30 -7
- package/dist/AnthropicConfig.js.map +1 -1
- package/dist/AnthropicError.d.ts +136 -37
- package/dist/AnthropicError.d.ts.map +1 -1
- package/dist/AnthropicError.js +1 -1
- package/dist/AnthropicLanguageModel.d.ts +362 -52
- package/dist/AnthropicLanguageModel.d.ts.map +1 -1
- package/dist/AnthropicLanguageModel.js +88 -21
- package/dist/AnthropicLanguageModel.js.map +1 -1
- package/dist/AnthropicTelemetry.d.ts +43 -16
- package/dist/AnthropicTelemetry.d.ts.map +1 -1
- package/dist/AnthropicTelemetry.js +15 -9
- package/dist/AnthropicTelemetry.js.map +1 -1
- package/dist/AnthropicTool.d.ts +1223 -289
- package/dist/AnthropicTool.d.ts.map +1 -1
- package/dist/AnthropicTool.js +859 -201
- package/dist/AnthropicTool.js.map +1 -1
- package/dist/Generated.d.ts +1 -1
- package/dist/Generated.js +1 -1
- package/dist/index.d.ts +8 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -29
- 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 +114 -104
- package/src/AnthropicConfig.ts +56 -11
- package/src/AnthropicError.ts +138 -37
- package/src/AnthropicLanguageModel.ts +395 -43
- package/src/AnthropicTelemetry.ts +48 -22
- package/src/AnthropicTool.ts +1216 -282
- package/src/Generated.ts +1 -1
- package/src/index.ts +8 -29
- package/src/internal/errors.ts +9 -7
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The `AnthropicLanguageModel` module provides the Anthropic implementation of
|
|
3
|
+
* Effect AI's `LanguageModel` service. It translates Effect AI prompts, tools,
|
|
4
|
+
* files, reasoning content, and Anthropic-specific options into Messages API
|
|
5
|
+
* requests, then converts normal and streaming Anthropic responses back into
|
|
6
|
+
* Effect AI response content with provider metadata.
|
|
7
|
+
*
|
|
8
|
+
* @since 4.0.0
|
|
3
9
|
*/
|
|
4
10
|
/** @effect-diagnostics preferSchemaOverJson:skip-file */
|
|
5
11
|
import * as Arr from "effect/Array"
|
|
12
|
+
import * as Context from "effect/Context"
|
|
6
13
|
import * as DateTime from "effect/DateTime"
|
|
7
14
|
import * as Effect from "effect/Effect"
|
|
8
15
|
import * as Encoding from "effect/Encoding"
|
|
9
16
|
import { dual } from "effect/Function"
|
|
10
17
|
import * as Layer from "effect/Layer"
|
|
18
|
+
import * as Option from "effect/Option"
|
|
11
19
|
import * as Predicate from "effect/Predicate"
|
|
12
20
|
import * as Redactable from "effect/Redactable"
|
|
13
21
|
import * as Schema from "effect/Schema"
|
|
14
22
|
import * as SchemaAST from "effect/SchemaAST"
|
|
15
|
-
import * as ServiceMap from "effect/ServiceMap"
|
|
16
23
|
import * as Stream from "effect/Stream"
|
|
17
24
|
import type { Span } from "effect/Tracer"
|
|
18
25
|
import type { Mutable, Simplify } from "effect/Types"
|
|
@@ -33,10 +40,16 @@ import type * as Generated from "./Generated.ts"
|
|
|
33
40
|
import * as InternalUtilities from "./internal/utilities.ts"
|
|
34
41
|
|
|
35
42
|
/**
|
|
36
|
-
*
|
|
43
|
+
* Known Anthropic Claude model identifiers exposed by the generated Anthropic schema.
|
|
44
|
+
*
|
|
45
|
+
* **Details**
|
|
46
|
+
*
|
|
47
|
+
* The Anthropic language model constructors accept `Model` values and custom
|
|
48
|
+
* string model ids, so this type is best used for autocomplete and type checking
|
|
49
|
+
* of known Claude ids.
|
|
37
50
|
*
|
|
38
|
-
* @since 1.0.0
|
|
39
51
|
* @category models
|
|
52
|
+
* @since 4.0.0
|
|
40
53
|
*/
|
|
41
54
|
export type Model = typeof Generated.Model.Type
|
|
42
55
|
|
|
@@ -45,15 +58,23 @@ export type Model = typeof Generated.Model.Type
|
|
|
45
58
|
// =============================================================================
|
|
46
59
|
|
|
47
60
|
/**
|
|
48
|
-
*
|
|
61
|
+
* Context service for Anthropic language model configuration.
|
|
62
|
+
*
|
|
63
|
+
* **When to use**
|
|
64
|
+
*
|
|
65
|
+
* Use when you need scoped Anthropic model request defaults or per-operation
|
|
66
|
+
* overrides from Effect context.
|
|
67
|
+
*
|
|
68
|
+
* **Details**
|
|
49
69
|
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
70
|
+
* The service stores request fields that are merged into Anthropic Messages API
|
|
71
|
+
* requests. Scoped configuration overrides defaults supplied to `model`,
|
|
72
|
+
* `make`, or `layer`.
|
|
52
73
|
*
|
|
53
|
-
* @since 1.0.0
|
|
54
74
|
* @category configuration
|
|
75
|
+
* @since 4.0.0
|
|
55
76
|
*/
|
|
56
|
-
export class Config extends
|
|
77
|
+
export class Config extends Context.Service<
|
|
57
78
|
Config,
|
|
58
79
|
Simplify<
|
|
59
80
|
& Partial<
|
|
@@ -73,6 +94,8 @@ export class Config extends ServiceMap.Service<
|
|
|
73
94
|
/**
|
|
74
95
|
* Whether to use strict JSON schema validation for tool calls.
|
|
75
96
|
*
|
|
97
|
+
* **Details**
|
|
98
|
+
*
|
|
76
99
|
* Only applies to models that support structured outputs. Defaults to
|
|
77
100
|
* `true` when structured outputs are supported.
|
|
78
101
|
*/
|
|
@@ -86,6 +109,17 @@ export class Config extends ServiceMap.Service<
|
|
|
86
109
|
// =============================================================================
|
|
87
110
|
|
|
88
111
|
declare module "effect/unstable/ai/Prompt" {
|
|
112
|
+
/**
|
|
113
|
+
* Anthropic-specific options for system messages.
|
|
114
|
+
*
|
|
115
|
+
* **Details**
|
|
116
|
+
*
|
|
117
|
+
* These options are used when translating system messages into Anthropic
|
|
118
|
+
* request content.
|
|
119
|
+
*
|
|
120
|
+
* @category request
|
|
121
|
+
* @since 4.0.0
|
|
122
|
+
*/
|
|
89
123
|
export interface SystemMessageOptions extends ProviderOptions {
|
|
90
124
|
readonly anthropic?: {
|
|
91
125
|
/**
|
|
@@ -95,6 +129,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
95
129
|
} | null
|
|
96
130
|
}
|
|
97
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Anthropic-specific options for user messages.
|
|
134
|
+
*
|
|
135
|
+
* **Details**
|
|
136
|
+
*
|
|
137
|
+
* These options are used when translating user messages into Anthropic
|
|
138
|
+
* request content.
|
|
139
|
+
*
|
|
140
|
+
* @category request
|
|
141
|
+
* @since 4.0.0
|
|
142
|
+
*/
|
|
98
143
|
export interface UserMessageOptions extends ProviderOptions {
|
|
99
144
|
readonly anthropic?: {
|
|
100
145
|
/**
|
|
@@ -104,6 +149,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
104
149
|
} | null
|
|
105
150
|
}
|
|
106
151
|
|
|
152
|
+
/**
|
|
153
|
+
* Anthropic-specific options for assistant messages.
|
|
154
|
+
*
|
|
155
|
+
* **Details**
|
|
156
|
+
*
|
|
157
|
+
* These options are used when replaying assistant messages in Anthropic
|
|
158
|
+
* conversation history.
|
|
159
|
+
*
|
|
160
|
+
* @category request
|
|
161
|
+
* @since 4.0.0
|
|
162
|
+
*/
|
|
107
163
|
export interface AssistantMessageOptions extends ProviderOptions {
|
|
108
164
|
readonly anthropic?: {
|
|
109
165
|
/**
|
|
@@ -113,6 +169,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
113
169
|
} | null
|
|
114
170
|
}
|
|
115
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Anthropic-specific options for tool messages.
|
|
174
|
+
*
|
|
175
|
+
* **Details**
|
|
176
|
+
*
|
|
177
|
+
* These options are used when converting tool results into Anthropic user
|
|
178
|
+
* content blocks.
|
|
179
|
+
*
|
|
180
|
+
* @category request
|
|
181
|
+
* @since 4.0.0
|
|
182
|
+
*/
|
|
116
183
|
export interface ToolMessageOptions extends ProviderOptions {
|
|
117
184
|
readonly anthropic?: {
|
|
118
185
|
/**
|
|
@@ -122,6 +189,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
122
189
|
} | null
|
|
123
190
|
}
|
|
124
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Anthropic-specific options for text prompt parts.
|
|
194
|
+
*
|
|
195
|
+
* **When to use**
|
|
196
|
+
*
|
|
197
|
+
* Use when you use these options to control how text blocks are sent to Anthropic.
|
|
198
|
+
*
|
|
199
|
+
* @category request
|
|
200
|
+
* @since 4.0.0
|
|
201
|
+
*/
|
|
125
202
|
export interface TextPartOptions extends ProviderOptions {
|
|
126
203
|
readonly anthropic?: {
|
|
127
204
|
/**
|
|
@@ -131,6 +208,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
131
208
|
} | null
|
|
132
209
|
}
|
|
133
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Anthropic-specific options for reasoning prompt parts.
|
|
213
|
+
*
|
|
214
|
+
* **Details**
|
|
215
|
+
*
|
|
216
|
+
* Preserves Claude thinking metadata when reasoning content is sent back to
|
|
217
|
+
* Anthropic in later turns.
|
|
218
|
+
*
|
|
219
|
+
* @category request
|
|
220
|
+
* @since 4.0.0
|
|
221
|
+
*/
|
|
134
222
|
export interface ReasoningPartOptions extends ProviderOptions {
|
|
135
223
|
readonly anthropic?: {
|
|
136
224
|
readonly info?: {
|
|
@@ -155,6 +243,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
155
243
|
} | null
|
|
156
244
|
}
|
|
157
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Anthropic-specific options for file prompt parts.
|
|
248
|
+
*
|
|
249
|
+
* **Details**
|
|
250
|
+
*
|
|
251
|
+
* Controls document metadata, citations, and prompt caching for files sent to
|
|
252
|
+
* Anthropic.
|
|
253
|
+
*
|
|
254
|
+
* @category request
|
|
255
|
+
* @since 4.0.0
|
|
256
|
+
*/
|
|
158
257
|
export interface FilePartOptions extends ProviderOptions {
|
|
159
258
|
readonly anthropic?: {
|
|
160
259
|
/**
|
|
@@ -174,12 +273,25 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
174
273
|
* Additional context about the document that will be forwarded to the
|
|
175
274
|
* large language model, but will not be used towards cited content.
|
|
176
275
|
*
|
|
177
|
-
*
|
|
276
|
+
* **When to use**
|
|
277
|
+
*
|
|
278
|
+
* Use when storing additional document metadata as text or stringified JSON.
|
|
178
279
|
*/
|
|
179
280
|
readonly documentContext?: string | null
|
|
180
281
|
} | null
|
|
181
282
|
}
|
|
182
283
|
|
|
284
|
+
/**
|
|
285
|
+
* Anthropic-specific options for tool call prompt parts.
|
|
286
|
+
*
|
|
287
|
+
* **Details**
|
|
288
|
+
*
|
|
289
|
+
* Carries Anthropic tool caller metadata, MCP metadata, and cache control for
|
|
290
|
+
* tool use blocks.
|
|
291
|
+
*
|
|
292
|
+
* @category request
|
|
293
|
+
* @since 4.0.0
|
|
294
|
+
*/
|
|
183
295
|
export interface ToolCallPartOptions extends ProviderOptions {
|
|
184
296
|
readonly anthropic?: {
|
|
185
297
|
readonly caller?: {
|
|
@@ -202,6 +314,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
202
314
|
} | null
|
|
203
315
|
}
|
|
204
316
|
|
|
317
|
+
/**
|
|
318
|
+
* Anthropic-specific options for tool result prompt parts.
|
|
319
|
+
*
|
|
320
|
+
* **Details**
|
|
321
|
+
*
|
|
322
|
+
* Controls Anthropic prompt caching for tool result content.
|
|
323
|
+
*
|
|
324
|
+
* @category request
|
|
325
|
+
* @since 4.0.0
|
|
326
|
+
*/
|
|
205
327
|
export interface ToolResultPartOptions extends ProviderOptions {
|
|
206
328
|
readonly anthropic?: {
|
|
207
329
|
/**
|
|
@@ -211,6 +333,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
211
333
|
} | null
|
|
212
334
|
}
|
|
213
335
|
|
|
336
|
+
/**
|
|
337
|
+
* Anthropic-specific options for tool approval request prompt parts.
|
|
338
|
+
*
|
|
339
|
+
* **Details**
|
|
340
|
+
*
|
|
341
|
+
* Controls prompt caching for human approval requests in conversations.
|
|
342
|
+
*
|
|
343
|
+
* @category request
|
|
344
|
+
* @since 4.0.0
|
|
345
|
+
*/
|
|
214
346
|
export interface ToolApprovalRequestPartOptions extends ProviderOptions {
|
|
215
347
|
readonly anthropic?: {
|
|
216
348
|
/**
|
|
@@ -220,15 +352,16 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
220
352
|
} | null
|
|
221
353
|
}
|
|
222
354
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
355
|
+
/**
|
|
356
|
+
* Anthropic-specific options for tool approval response prompt parts.
|
|
357
|
+
*
|
|
358
|
+
* **Details**
|
|
359
|
+
*
|
|
360
|
+
* Controls prompt caching for human approval responses in conversations.
|
|
361
|
+
*
|
|
362
|
+
* @category request
|
|
363
|
+
* @since 4.0.0
|
|
364
|
+
*/
|
|
232
365
|
export interface ToolApprovalResponsePartOptions extends ProviderOptions {
|
|
233
366
|
readonly anthropic?: {
|
|
234
367
|
/**
|
|
@@ -240,6 +373,17 @@ declare module "effect/unstable/ai/Prompt" {
|
|
|
240
373
|
}
|
|
241
374
|
|
|
242
375
|
declare module "effect/unstable/ai/Response" {
|
|
376
|
+
/**
|
|
377
|
+
* Anthropic metadata attached when a reasoning block begins.
|
|
378
|
+
*
|
|
379
|
+
* **Details**
|
|
380
|
+
*
|
|
381
|
+
* Includes Claude thinking metadata needed to continue reasoning-aware
|
|
382
|
+
* conversations.
|
|
383
|
+
*
|
|
384
|
+
* @category response
|
|
385
|
+
* @since 4.0.0
|
|
386
|
+
*/
|
|
243
387
|
export interface ReasoningStartPartMetadata extends ProviderMetadata {
|
|
244
388
|
readonly anthropic?: {
|
|
245
389
|
readonly info?: {
|
|
@@ -260,6 +404,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
260
404
|
} | null
|
|
261
405
|
}
|
|
262
406
|
|
|
407
|
+
/**
|
|
408
|
+
* Anthropic metadata attached to streaming reasoning deltas.
|
|
409
|
+
*
|
|
410
|
+
* **Details**
|
|
411
|
+
*
|
|
412
|
+
* Includes the signature for streamed Claude thinking content when available.
|
|
413
|
+
*
|
|
414
|
+
* @category response
|
|
415
|
+
* @since 4.0.0
|
|
416
|
+
*/
|
|
263
417
|
export interface ReasoningDeltaPartMetadata extends ProviderMetadata {
|
|
264
418
|
readonly anthropic?: {
|
|
265
419
|
readonly info?: {
|
|
@@ -273,6 +427,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
273
427
|
} | null
|
|
274
428
|
}
|
|
275
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Anthropic metadata attached to completed reasoning parts.
|
|
432
|
+
*
|
|
433
|
+
* **Details**
|
|
434
|
+
*
|
|
435
|
+
* Preserves Claude thinking or redacted thinking information for later turns.
|
|
436
|
+
*
|
|
437
|
+
* @category response
|
|
438
|
+
* @since 4.0.0
|
|
439
|
+
*/
|
|
276
440
|
export interface ReasoningPartMetadata extends ProviderMetadata {
|
|
277
441
|
readonly anthropic?: {
|
|
278
442
|
readonly info?: {
|
|
@@ -293,6 +457,17 @@ declare module "effect/unstable/ai/Response" {
|
|
|
293
457
|
} | null
|
|
294
458
|
}
|
|
295
459
|
|
|
460
|
+
/**
|
|
461
|
+
* Anthropic metadata attached to tool call response parts.
|
|
462
|
+
*
|
|
463
|
+
* **Details**
|
|
464
|
+
*
|
|
465
|
+
* Identifies Anthropic caller details and MCP tool metadata emitted by the
|
|
466
|
+
* provider.
|
|
467
|
+
*
|
|
468
|
+
* @category response
|
|
469
|
+
* @since 4.0.0
|
|
470
|
+
*/
|
|
296
471
|
export interface ToolCallPartMetadata extends ProviderMetadata {
|
|
297
472
|
readonly anthropic?: {
|
|
298
473
|
readonly caller?: {
|
|
@@ -311,6 +486,17 @@ declare module "effect/unstable/ai/Response" {
|
|
|
311
486
|
} | null
|
|
312
487
|
}
|
|
313
488
|
|
|
489
|
+
/**
|
|
490
|
+
* Anthropic metadata attached to tool result response parts.
|
|
491
|
+
*
|
|
492
|
+
* **Details**
|
|
493
|
+
*
|
|
494
|
+
* Identifies MCP tool metadata associated with provider-executed tool
|
|
495
|
+
* results.
|
|
496
|
+
*
|
|
497
|
+
* @category response
|
|
498
|
+
* @since 4.0.0
|
|
499
|
+
*/
|
|
314
500
|
export interface ToolResultPartMetadata extends ProviderMetadata {
|
|
315
501
|
readonly anthropic?: {
|
|
316
502
|
/**
|
|
@@ -325,6 +511,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
325
511
|
} | null
|
|
326
512
|
}
|
|
327
513
|
|
|
514
|
+
/**
|
|
515
|
+
* Anthropic metadata for document citations in model responses.
|
|
516
|
+
*
|
|
517
|
+
* **Details**
|
|
518
|
+
*
|
|
519
|
+
* Records the cited document span by character position or page number.
|
|
520
|
+
*
|
|
521
|
+
* @category response
|
|
522
|
+
* @since 4.0.0
|
|
523
|
+
*/
|
|
328
524
|
export interface DocumentSourcePartMetadata extends ProviderMetadata {
|
|
329
525
|
readonly anthropic?: {
|
|
330
526
|
readonly source: "document"
|
|
@@ -359,6 +555,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
359
555
|
} | null
|
|
360
556
|
}
|
|
361
557
|
|
|
558
|
+
/**
|
|
559
|
+
* Anthropic metadata for URL and web citations in model responses.
|
|
560
|
+
*
|
|
561
|
+
* **Details**
|
|
562
|
+
*
|
|
563
|
+
* Records cited URL text or web-search source freshness information.
|
|
564
|
+
*
|
|
565
|
+
* @category response
|
|
566
|
+
* @since 4.0.0
|
|
567
|
+
*/
|
|
362
568
|
export interface UrlSourcePartMetadata extends ProviderMetadata {
|
|
363
569
|
readonly anthropic?: {
|
|
364
570
|
readonly source: "url"
|
|
@@ -378,6 +584,17 @@ declare module "effect/unstable/ai/Response" {
|
|
|
378
584
|
} | null
|
|
379
585
|
}
|
|
380
586
|
|
|
587
|
+
/**
|
|
588
|
+
* Anthropic metadata attached to the finish part of a response.
|
|
589
|
+
*
|
|
590
|
+
* **Details**
|
|
591
|
+
*
|
|
592
|
+
* Includes container state, context management information, stop details, and
|
|
593
|
+
* token usage reported by Anthropic.
|
|
594
|
+
*
|
|
595
|
+
* @category response
|
|
596
|
+
* @since 4.0.0
|
|
597
|
+
*/
|
|
381
598
|
export interface FinishPartMetadata extends ProviderMetadata {
|
|
382
599
|
readonly anthropic?: {
|
|
383
600
|
readonly container: typeof Generated.BetaContainer.Encoded | null
|
|
@@ -387,6 +604,16 @@ declare module "effect/unstable/ai/Response" {
|
|
|
387
604
|
} | null
|
|
388
605
|
}
|
|
389
606
|
|
|
607
|
+
/**
|
|
608
|
+
* Anthropic metadata attached to error response parts.
|
|
609
|
+
*
|
|
610
|
+
* **Details**
|
|
611
|
+
*
|
|
612
|
+
* Includes the provider request identifier when Anthropic returns one.
|
|
613
|
+
*
|
|
614
|
+
* @category response
|
|
615
|
+
* @since 4.0.0
|
|
616
|
+
*/
|
|
390
617
|
export interface ErrorPartMetadata extends ProviderMetadata {
|
|
391
618
|
readonly anthropic?: {
|
|
392
619
|
requestId?: string | null
|
|
@@ -399,22 +626,44 @@ declare module "effect/unstable/ai/Response" {
|
|
|
399
626
|
// =============================================================================
|
|
400
627
|
|
|
401
628
|
/**
|
|
402
|
-
* Creates an Anthropic
|
|
629
|
+
* Creates an Anthropic model descriptor that can be provided with `Effect.provide`.
|
|
630
|
+
*
|
|
631
|
+
* **When to use**
|
|
632
|
+
*
|
|
633
|
+
* Use when you want an Anthropic Claude model value that carries provider and
|
|
634
|
+
* model metadata and can be supplied directly to an Effect program.
|
|
635
|
+
*
|
|
636
|
+
* @see {@link layer} for creating a `LanguageModel.LanguageModel` layer directly
|
|
637
|
+
* @see {@link make} for constructing the language model service effectfully
|
|
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
|
-
* Creates an Anthropic
|
|
649
|
+
* Creates an Anthropic `LanguageModel` service from a model identifier and optional request defaults.
|
|
650
|
+
*
|
|
651
|
+
* **When to use**
|
|
652
|
+
*
|
|
653
|
+
* Use when you need to construct a `LanguageModel.Service` value backed by
|
|
654
|
+
* `AnthropicClient` inside an Effect.
|
|
655
|
+
*
|
|
656
|
+
* **Details**
|
|
657
|
+
*
|
|
658
|
+
* The returned effect requires `AnthropicClient`. Request defaults from the
|
|
659
|
+
* `config` option are merged with any `Config` service in the context, with
|
|
660
|
+
* context values taking precedence.
|
|
661
|
+
*
|
|
662
|
+
* @see {@link layer} for providing the service as a `Layer`
|
|
663
|
+
* @see {@link model} for creating a model descriptor for `AiModel.provide`
|
|
415
664
|
*
|
|
416
|
-
* @since 1.0.0
|
|
417
665
|
* @category constructors
|
|
666
|
+
* @since 4.0.0
|
|
418
667
|
*/
|
|
419
668
|
export const make = Effect.fnUntraced(function*({ model, config: providerConfig }: {
|
|
420
669
|
readonly model: (string & {}) | Model
|
|
@@ -423,7 +672,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
423
672
|
const client = yield* AnthropicClient
|
|
424
673
|
|
|
425
674
|
const makeConfig: Effect.Effect<typeof Config.Service & { readonly model: string }> = Effect.gen(function*() {
|
|
426
|
-
const services = yield* Effect.
|
|
675
|
+
const services = yield* Effect.context<never>()
|
|
427
676
|
return { model, ...providerConfig, ...services.mapUnsafe.get(Config.key) }
|
|
428
677
|
})
|
|
429
678
|
|
|
@@ -469,6 +718,7 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
469
718
|
)
|
|
470
719
|
|
|
471
720
|
return yield* LanguageModel.make({
|
|
721
|
+
codecTransformer: toCodecAnthropic,
|
|
472
722
|
generateText: Effect.fnUntraced(function*(options) {
|
|
473
723
|
const config = yield* makeConfig
|
|
474
724
|
const toolNameMapper = new Tool.NameMapper(options.tools)
|
|
@@ -493,17 +743,23 @@ export const make = Effect.fnUntraced(function*({ model, config: providerConfig
|
|
|
493
743
|
return response
|
|
494
744
|
})
|
|
495
745
|
))
|
|
496
|
-
})
|
|
497
|
-
LanguageModel.CurrentCodecTransformer,
|
|
498
|
-
toCodecAnthropic
|
|
499
|
-
))
|
|
746
|
+
})
|
|
500
747
|
})
|
|
501
748
|
|
|
502
749
|
/**
|
|
503
750
|
* Creates a layer for the Anthropic language model.
|
|
504
751
|
*
|
|
505
|
-
*
|
|
752
|
+
* **When to use**
|
|
753
|
+
*
|
|
754
|
+
* Use when composing application layers and you want Anthropic to satisfy
|
|
755
|
+
* `LanguageModel.LanguageModel` while supplying `AnthropicClient` from another
|
|
756
|
+
* layer.
|
|
757
|
+
*
|
|
758
|
+
* @see {@link make} for constructing the language model service effectfully
|
|
759
|
+
* @see {@link model} for creating a model service directly
|
|
760
|
+
*
|
|
506
761
|
* @category layers
|
|
762
|
+
* @since 4.0.0
|
|
507
763
|
*/
|
|
508
764
|
export const layer = (options: {
|
|
509
765
|
readonly model: (string & {}) | Model
|
|
@@ -514,37 +770,107 @@ export const layer = (options: {
|
|
|
514
770
|
/**
|
|
515
771
|
* Provides config overrides for Anthropic language model operations.
|
|
516
772
|
*
|
|
517
|
-
*
|
|
773
|
+
* **When to use**
|
|
774
|
+
*
|
|
775
|
+
* Use to apply Anthropic request configuration to one effect without changing
|
|
776
|
+
* the model's default configuration.
|
|
777
|
+
*
|
|
778
|
+
* **Details**
|
|
779
|
+
*
|
|
780
|
+
* The overrides are merged with any existing `Config` service for the duration
|
|
781
|
+
* of the supplied effect. Fields in `overrides` take precedence over existing
|
|
782
|
+
* config, and the helper supports both `effect.pipe(withConfigOverride(overrides))`
|
|
783
|
+
* and `withConfigOverride(effect, overrides)`.
|
|
784
|
+
*
|
|
785
|
+
* @see {@link Config} for available Anthropic request configuration fields
|
|
786
|
+
*
|
|
518
787
|
* @category configuration
|
|
788
|
+
* @since 4.0.0
|
|
519
789
|
*/
|
|
520
790
|
export const withConfigOverride: {
|
|
521
791
|
/**
|
|
522
792
|
* Provides config overrides for Anthropic language model operations.
|
|
523
793
|
*
|
|
524
|
-
*
|
|
794
|
+
* **When to use**
|
|
795
|
+
*
|
|
796
|
+
* Use to apply Anthropic request configuration to one effect without changing
|
|
797
|
+
* the model's default configuration.
|
|
798
|
+
*
|
|
799
|
+
* **Details**
|
|
800
|
+
*
|
|
801
|
+
* The overrides are merged with any existing `Config` service for the duration
|
|
802
|
+
* of the supplied effect. Fields in `overrides` take precedence over existing
|
|
803
|
+
* config, and the helper supports both `effect.pipe(withConfigOverride(overrides))`
|
|
804
|
+
* and `withConfigOverride(effect, overrides)`.
|
|
805
|
+
*
|
|
806
|
+
* @see {@link Config} for available Anthropic request configuration fields
|
|
807
|
+
*
|
|
525
808
|
* @category configuration
|
|
809
|
+
* @since 4.0.0
|
|
526
810
|
*/
|
|
527
811
|
(overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
528
812
|
/**
|
|
529
813
|
* Provides config overrides for Anthropic language model operations.
|
|
530
814
|
*
|
|
531
|
-
*
|
|
815
|
+
* **When to use**
|
|
816
|
+
*
|
|
817
|
+
* Use to apply Anthropic request configuration to one effect without changing
|
|
818
|
+
* the model's default configuration.
|
|
819
|
+
*
|
|
820
|
+
* **Details**
|
|
821
|
+
*
|
|
822
|
+
* The overrides are merged with any existing `Config` service for the duration
|
|
823
|
+
* of the supplied effect. Fields in `overrides` take precedence over existing
|
|
824
|
+
* config, and the helper supports both `effect.pipe(withConfigOverride(overrides))`
|
|
825
|
+
* and `withConfigOverride(effect, overrides)`.
|
|
826
|
+
*
|
|
827
|
+
* @see {@link Config} for available Anthropic request configuration fields
|
|
828
|
+
*
|
|
532
829
|
* @category configuration
|
|
830
|
+
* @since 4.0.0
|
|
533
831
|
*/
|
|
534
832
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>
|
|
535
833
|
} = dual<
|
|
536
834
|
/**
|
|
537
835
|
* Provides config overrides for Anthropic language model operations.
|
|
538
836
|
*
|
|
539
|
-
*
|
|
837
|
+
* **When to use**
|
|
838
|
+
*
|
|
839
|
+
* Use to apply Anthropic request configuration to one effect without changing
|
|
840
|
+
* the model's default configuration.
|
|
841
|
+
*
|
|
842
|
+
* **Details**
|
|
843
|
+
*
|
|
844
|
+
* The overrides are merged with any existing `Config` service for the duration
|
|
845
|
+
* of the supplied effect. Fields in `overrides` take precedence over existing
|
|
846
|
+
* config, and the helper supports both `effect.pipe(withConfigOverride(overrides))`
|
|
847
|
+
* and `withConfigOverride(effect, overrides)`.
|
|
848
|
+
*
|
|
849
|
+
* @see {@link Config} for available Anthropic request configuration fields
|
|
850
|
+
*
|
|
540
851
|
* @category configuration
|
|
852
|
+
* @since 4.0.0
|
|
541
853
|
*/
|
|
542
854
|
(overrides: typeof Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
|
|
543
855
|
/**
|
|
544
856
|
* Provides config overrides for Anthropic language model operations.
|
|
545
857
|
*
|
|
546
|
-
*
|
|
858
|
+
* **When to use**
|
|
859
|
+
*
|
|
860
|
+
* Use to apply Anthropic request configuration to one effect without changing
|
|
861
|
+
* the model's default configuration.
|
|
862
|
+
*
|
|
863
|
+
* **Details**
|
|
864
|
+
*
|
|
865
|
+
* The overrides are merged with any existing `Config` service for the duration
|
|
866
|
+
* of the supplied effect. Fields in `overrides` take precedence over existing
|
|
867
|
+
* config, and the helper supports both `effect.pipe(withConfigOverride(overrides))`
|
|
868
|
+
* and `withConfigOverride(effect, overrides)`.
|
|
869
|
+
*
|
|
870
|
+
* @see {@link Config} for available Anthropic request configuration fields
|
|
871
|
+
*
|
|
547
872
|
* @category configuration
|
|
873
|
+
* @since 4.0.0
|
|
548
874
|
*/
|
|
549
875
|
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
550
876
|
>(2, (self, overrides) =>
|
|
@@ -962,21 +1288,37 @@ const prepareMessages = Effect.fnUntraced(
|
|
|
962
1288
|
// =============================================================================
|
|
963
1289
|
|
|
964
1290
|
/**
|
|
965
|
-
*
|
|
1291
|
+
* Encoded Anthropic custom tool definition that can be sent in a Messages API request.
|
|
1292
|
+
*
|
|
1293
|
+
* **When to use**
|
|
1294
|
+
*
|
|
1295
|
+
* Use when you need to type or inspect the provider-specific request payload for
|
|
1296
|
+
* a custom Anthropic tool.
|
|
1297
|
+
*
|
|
1298
|
+
* **Details**
|
|
1299
|
+
*
|
|
1300
|
+
* This type aliases the encoded `Generated.BetaTool` schema used for Effect
|
|
1301
|
+
* user-defined and dynamic tools after conversion. It contains the tool `name`,
|
|
1302
|
+
* optional `description`, and `input_schema`, plus Anthropic-specific fields
|
|
1303
|
+
* such as `strict` and `cache_control`.
|
|
1304
|
+
*
|
|
1305
|
+
* @see {@link AnthropicProviderDefinedTool} for the request shape used by Anthropic built-in provider tools
|
|
966
1306
|
*
|
|
967
|
-
* @since 1.0.0
|
|
968
1307
|
* @category tools
|
|
1308
|
+
* @since 4.0.0
|
|
969
1309
|
*/
|
|
970
1310
|
export type AnthropicUserDefinedTool = typeof Generated.BetaTool.Encoded
|
|
971
1311
|
|
|
972
1312
|
/**
|
|
973
1313
|
* Represents a provider-defined tool that can be passed to the Anthropic API.
|
|
974
1314
|
*
|
|
1315
|
+
* **Details**
|
|
1316
|
+
*
|
|
975
1317
|
* These include Anthropic's built-in tools like computer use, code execution,
|
|
976
1318
|
* web search, and text editing.
|
|
977
1319
|
*
|
|
978
|
-
* @since 1.0.0
|
|
979
1320
|
* @category tools
|
|
1321
|
+
* @since 4.0.0
|
|
980
1322
|
*/
|
|
981
1323
|
export type AnthropicProviderDefinedTool =
|
|
982
1324
|
| typeof Generated.BetaBashTool_20241022.Encoded
|
|
@@ -1033,9 +1375,9 @@ const prepareTools = Effect.fnUntraced(
|
|
|
1033
1375
|
const providerTools: Array<AnthropicProviderDefinedTool> = []
|
|
1034
1376
|
|
|
1035
1377
|
for (const tool of options.tools) {
|
|
1036
|
-
if (Tool.isUserDefined(tool)) {
|
|
1378
|
+
if (Tool.isUserDefined(tool) || Tool.isDynamic(tool)) {
|
|
1037
1379
|
const description = Tool.getDescription(tool)
|
|
1038
|
-
const input_schema = yield*
|
|
1380
|
+
const input_schema = yield* tryToolJsonSchema(tool, "prepareTools")
|
|
1039
1381
|
const toolStrict = Tool.getStrictMode(tool)
|
|
1040
1382
|
const strict = capabilities.supportsStructuredOutput
|
|
1041
1383
|
? (toolStrict ?? config.strictJsonSchema ?? true)
|
|
@@ -1248,7 +1590,7 @@ const buildHttpRequestDetails = (
|
|
|
1248
1590
|
method: request.method,
|
|
1249
1591
|
url: request.url,
|
|
1250
1592
|
urlParams: Array.from(request.urlParams),
|
|
1251
|
-
hash: request.hash,
|
|
1593
|
+
hash: Option.getOrUndefined(request.hash),
|
|
1252
1594
|
headers: Redactable.redact(request.headers) as Record<string, string>
|
|
1253
1595
|
})
|
|
1254
1596
|
|
|
@@ -2544,7 +2886,7 @@ const groupMessages = (prompt: Prompt.Prompt): Array<ContentGroup> => {
|
|
|
2544
2886
|
}
|
|
2545
2887
|
|
|
2546
2888
|
/**
|
|
2547
|
-
* Checks
|
|
2889
|
+
* Checks whether data is a URL (either a URL object or a URL string).
|
|
2548
2890
|
*/
|
|
2549
2891
|
const isUrlData = (
|
|
2550
2892
|
data: typeof Prompt.FilePart.Type["data"]
|
|
@@ -2702,7 +3044,11 @@ const getModelCapabilities = (modelId: string): ModelCapabilities => {
|
|
|
2702
3044
|
if (
|
|
2703
3045
|
modelId.includes("claude-sonnet-4-5") ||
|
|
2704
3046
|
modelId.includes("claude-opus-4-5") ||
|
|
2705
|
-
modelId.includes("claude-haiku-4-5")
|
|
3047
|
+
modelId.includes("claude-haiku-4-5") ||
|
|
3048
|
+
modelId.includes("claude-opus-4-6") ||
|
|
3049
|
+
modelId.includes("claude-sonnet-4-6") ||
|
|
3050
|
+
modelId.includes("claude-opus-4-7") ||
|
|
3051
|
+
modelId.includes("claude-opus-4-8")
|
|
2706
3052
|
) {
|
|
2707
3053
|
return {
|
|
2708
3054
|
maxOutputTokens: 64000,
|
|
@@ -2772,6 +3118,12 @@ const tryJsonSchema = <S extends Schema.Top>(schema: S, method: string) =>
|
|
|
2772
3118
|
catch: (error) => unsupportedSchemaError(error, method)
|
|
2773
3119
|
})
|
|
2774
3120
|
|
|
3121
|
+
const tryToolJsonSchema = <T extends Tool.Any | Tool.AnyDynamic>(tool: T, method: string) =>
|
|
3122
|
+
Effect.try({
|
|
3123
|
+
try: () => Tool.getJsonSchema(tool, { transformer: toCodecAnthropic }),
|
|
3124
|
+
catch: (error) => unsupportedSchemaError(error, method)
|
|
3125
|
+
})
|
|
3126
|
+
|
|
2775
3127
|
const getOutputFormat = Effect.fnUntraced(function*({ capabilities, options }: {
|
|
2776
3128
|
readonly capabilities: ModelCapabilities
|
|
2777
3129
|
readonly options: LanguageModel.ProviderOptions
|