The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 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 | * @stability unstable |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | /** @effect-diagnostics preferSchemaOverJson:skip-file */ |
| 12 | import * as AiError from "effect/ai/AiError" |
| 13 | import { toCodecAnthropic } from "effect/ai/AnthropicStructuredOutput" |
| 14 | import * as IdGenerator from "effect/ai/IdGenerator" |
| 15 | import * as LanguageModel from "effect/ai/LanguageModel" |
| 16 | import * as AiModel from "effect/ai/Model" |
| 17 | import type * as Prompt from "effect/ai/Prompt" |
| 18 | import type * as Response from "effect/ai/Response" |
| 19 | import * as Tool from "effect/ai/Tool" |
| 20 | import * as Arr from "effect/Array" |
| 21 | import * as Context from "effect/Context" |
| 22 | import * as DateTime from "effect/DateTime" |
| 23 | import * as Effect from "effect/Effect" |
| 24 | import * as Base64 from "effect/encoding/Base64" |
| 25 | import { dual } from "effect/Function" |
| 26 | import type * as HttpClientResponse from "effect/http/HttpClientResponse" |
| 27 | import * as Layer from "effect/Layer" |
| 28 | import * as Predicate from "effect/Predicate" |
| 29 | import * as Redactable from "effect/Redactable" |
| 30 | import * as Schema from "effect/Schema" |
| 31 | import * as SchemaAST from "effect/SchemaAST" |
| 32 | import * as Stream from "effect/Stream" |
| 33 | import type { Span } from "effect/Tracer" |
| 34 | import type { Mutable, Simplify } from "effect/Types" |
| 35 | import { AnthropicClient, type MessageStreamEvent } from "./AnthropicClient.ts" |
| 36 | import { addGenAIAnnotations } from "./AnthropicTelemetry.ts" |
| 37 | import * as AnthropicTool from "./AnthropicTool.ts" |
| 38 | import type * as Generated from "./Generated.ts" |
| 39 | import * as InternalUtilities from "./internal/utilities.ts" |
| 40 | |
| 41 | /** |
| 42 | * Known Anthropic Claude model identifiers exposed by the generated Anthropic schema. |
| 43 | * |
| 44 | * @stability unstable |
| 45 | * @category models |
| 46 | * @since 4.0.0 |
| 47 | */ |
| 48 | export type Model = Extract<(typeof Generated.Model)["members"][number], { readonly literal: string }>["literal"] |
| 49 | |
| 50 | // ============================================================================= |
| 51 | // Configuration |
| 52 | // ============================================================================= |
| 53 | |
| 54 | /** |
| 55 | * Context service for Anthropic language model configuration. |
| 56 | * |
| 57 | * **When to use** |
| 58 | * |
| 59 | * Use when you need scoped Anthropic model request defaults or per-operation |
| 60 | * overrides from Effect context. |
| 61 | * |
| 62 | * **Details** |
| 63 | * |
| 64 | * The service stores request fields that are merged into Anthropic Messages API |
| 65 | * requests. Scoped configuration overrides defaults supplied to `model`, |
| 66 | * `make`, or `layer`. |
| 67 | * |
| 68 | * @stability unstable |
| 69 | * @category services |
| 70 | * @since 4.0.0 |
| 71 | */ |
| 72 | export class Config extends Context.Service< |
| 73 | Config, |
| 74 | Simplify< |
| 75 | & Partial< |
| 76 | Omit< |
| 77 | typeof Generated.BetaCreateMessageParams.Encoded, |
| 78 | "messages" | "output_config" | "tools" | "tool_choice" | "stream" |
| 79 | > |
| 80 | > |
| 81 | & { |
| 82 | readonly output_config?: { |
| 83 | readonly effort?: "low" | "medium" | "high" | null |
| 84 | } |
| 85 | /** |
| 86 | * Disables Claude's ability to use multiple tools to respond to a query. |
| 87 | */ |
| 88 | readonly disableParallelToolCalls?: boolean | undefined |
| 89 | /** |
| 90 | * Whether the model supports native structured outputs. |
| 91 | * |
| 92 | * Overrides automatic capability detection based on the model identifier. |
| 93 | */ |
| 94 | readonly structuredOutputs?: boolean | undefined |
| 95 | /** |
| 96 | * Overrides model detection for mid-conversation system messages. |
| 97 | * Set to `false` to send all instructions in the top-level `system` field. |
| 98 | * |
| 99 | * @see https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages |
| 100 | */ |
| 101 | readonly midConversationSystemMessages?: boolean | undefined |
| 102 | /** |
| 103 | * Whether to use strict JSON schema validation for tool calls. |
| 104 | * |
| 105 | * **Details** |
| 106 | * |
| 107 | * Only applies to models that support structured outputs. Defaults to |
| 108 | * `true` when structured outputs are supported. |
| 109 | */ |
| 110 | readonly strictJsonSchema?: boolean | undefined |
| 111 | } |
| 112 | > |
| 113 | >()("@effect/ai-anthropic/AnthropicLanguageModel/Config") {} |
| 114 | |
| 115 | // ============================================================================= |
| 116 | // Provider Options / Metadata |
| 117 | // ============================================================================= |
| 118 | |
| 119 | declare module "effect/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 | * @stability unstable |
| 129 | * @category models |
| 130 | * @since 4.0.0 |
| 131 | */ |
| 132 | export interface SystemMessageOptions extends ProviderOptions { |
| 133 | readonly anthropic?: { |
| 134 | /** |
| 135 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 136 | */ |
| 137 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 138 | } | null |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Anthropic-specific options for user messages. |
| 143 | * |
| 144 | * **Details** |
| 145 | * |
| 146 | * These options are used when translating user messages into Anthropic |
| 147 | * request content. |
| 148 | * |
| 149 | * @stability unstable |
| 150 | * @category models |
| 151 | * @since 4.0.0 |
| 152 | */ |
| 153 | export interface UserMessageOptions extends ProviderOptions { |
| 154 | readonly anthropic?: { |
| 155 | /** |
| 156 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 157 | */ |
| 158 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 159 | } | null |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Anthropic-specific options for assistant messages. |
| 164 | * |
| 165 | * **Details** |
| 166 | * |
| 167 | * These options are used when replaying assistant messages in Anthropic |
| 168 | * conversation history. |
| 169 | * |
| 170 | * @stability unstable |
| 171 | * @category models |
| 172 | * @since 4.0.0 |
| 173 | */ |
| 174 | export interface AssistantMessageOptions extends ProviderOptions { |
| 175 | readonly anthropic?: { |
| 176 | /** |
| 177 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 178 | */ |
| 179 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 180 | } | null |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Anthropic-specific options for tool messages. |
| 185 | * |
| 186 | * **Details** |
| 187 | * |
| 188 | * These options are used when converting tool results into Anthropic user |
| 189 | * content blocks. |
| 190 | * |
| 191 | * @stability unstable |
| 192 | * @category models |
| 193 | * @since 4.0.0 |
| 194 | */ |
| 195 | export interface ToolMessageOptions extends ProviderOptions { |
| 196 | readonly anthropic?: { |
| 197 | /** |
| 198 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 199 | */ |
| 200 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 201 | } | null |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Anthropic-specific options for text prompt parts. |
| 206 | * |
| 207 | * **When to use** |
| 208 | * |
| 209 | * Use when you use these options to control how text blocks are sent to Anthropic. |
| 210 | * |
| 211 | * @stability unstable |
| 212 | * @category models |
| 213 | * @since 4.0.0 |
| 214 | */ |
| 215 | export interface TextPartOptions extends ProviderOptions { |
| 216 | readonly anthropic?: { |
| 217 | /** |
| 218 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 219 | */ |
| 220 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 221 | } | null |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Anthropic-specific options for reasoning prompt parts. |
| 226 | * |
| 227 | * **Details** |
| 228 | * |
| 229 | * Preserves Claude thinking metadata when reasoning content is sent back to |
| 230 | * Anthropic in later turns. |
| 231 | * |
| 232 | * @stability unstable |
| 233 | * @category models |
| 234 | * @since 4.0.0 |
| 235 | */ |
| 236 | export interface ReasoningPartOptions extends ProviderOptions { |
| 237 | readonly anthropic?: { |
| 238 | readonly info?: { |
| 239 | readonly type: "thinking" |
| 240 | /** |
| 241 | * Thinking content as an encrypted string, which is used to verify |
| 242 | * that thinking content was indeed generated by Anthropic's API. |
| 243 | */ |
| 244 | readonly signature: typeof Generated.ResponseThinkingBlock.fields.thinking.Encoded |
| 245 | } | { |
| 246 | readonly type: "redacted_thinking" |
| 247 | /** |
| 248 | * Thinking content which was flagged by Anthropic's safety systems, and |
| 249 | * was therefore encrypted. |
| 250 | */ |
| 251 | readonly redactedData: typeof Generated.ResponseRedactedThinkingBlock.fields.data.Encoded |
| 252 | } | null |
| 253 | /** |
| 254 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 255 | */ |
| 256 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 257 | } | null |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Anthropic-specific options for file prompt parts. |
| 262 | * |
| 263 | * **Details** |
| 264 | * |
| 265 | * Controls document metadata, citations, and prompt caching for files sent to |
| 266 | * Anthropic. |
| 267 | * |
| 268 | * @stability unstable |
| 269 | * @category models |
| 270 | * @since 4.0.0 |
| 271 | */ |
| 272 | export interface FilePartOptions extends ProviderOptions { |
| 273 | readonly anthropic?: { |
| 274 | /** |
| 275 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 276 | */ |
| 277 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 278 | /** |
| 279 | * Whether or not citations should be enabled for the file part. |
| 280 | */ |
| 281 | readonly citations?: typeof Generated.RequestCitationsConfig.Encoded | null |
| 282 | /** |
| 283 | * A custom title to provide to the document. If omitted, the file part's |
| 284 | * `fileName` property will be used. |
| 285 | */ |
| 286 | readonly documentTitle?: string | null |
| 287 | /** |
| 288 | * Additional context about the document that will be forwarded to the |
| 289 | * large language model, but will not be used towards cited content. |
| 290 | * |
| 291 | * **When to use** |
| 292 | * |
| 293 | * Use when storing additional document metadata as text or stringified JSON. |
| 294 | */ |
| 295 | readonly documentContext?: string | null |
| 296 | } | null |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Anthropic-specific options for tool call prompt parts. |
| 301 | * |
| 302 | * **Details** |
| 303 | * |
| 304 | * Carries Anthropic tool caller metadata, MCP metadata, and cache control for |
| 305 | * tool use blocks. |
| 306 | * |
| 307 | * @stability unstable |
| 308 | * @category models |
| 309 | * @since 4.0.0 |
| 310 | */ |
| 311 | export interface ToolCallPartOptions extends ProviderOptions { |
| 312 | readonly anthropic?: { |
| 313 | readonly caller?: { |
| 314 | readonly type: string |
| 315 | readonly toolId?: string | null |
| 316 | } | null |
| 317 | /** |
| 318 | * Contains details about the MCP tool that was called. |
| 319 | */ |
| 320 | readonly mcp_tool?: { |
| 321 | /** |
| 322 | * The name of the MCP server |
| 323 | */ |
| 324 | readonly server: string |
| 325 | } | null |
| 326 | /** |
| 327 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 328 | */ |
| 329 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 330 | } | null |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Anthropic-specific options for tool result prompt parts. |
| 335 | * |
| 336 | * **Details** |
| 337 | * |
| 338 | * Carries Anthropic MCP metadata and controls prompt caching for tool result |
| 339 | * content. |
| 340 | * |
| 341 | * @stability unstable |
| 342 | * @category models |
| 343 | * @since 4.0.0 |
| 344 | */ |
| 345 | export interface ToolResultPartOptions extends ProviderOptions { |
| 346 | readonly anthropic?: { |
| 347 | /** |
| 348 | * Contains details about the MCP tool that produced the result. |
| 349 | */ |
| 350 | readonly mcp_tool?: { |
| 351 | /** |
| 352 | * The name of the MCP server |
| 353 | */ |
| 354 | readonly server: string |
| 355 | } | null |
| 356 | /** |
| 357 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 358 | */ |
| 359 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 360 | } | null |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Anthropic-specific options for tool approval request prompt parts. |
| 365 | * |
| 366 | * **Details** |
| 367 | * |
| 368 | * Controls prompt caching for human approval requests in conversations. |
| 369 | * |
| 370 | * @stability unstable |
| 371 | * @category models |
| 372 | * @since 4.0.0 |
| 373 | */ |
| 374 | export interface ToolApprovalRequestPartOptions extends ProviderOptions { |
| 375 | readonly anthropic?: { |
| 376 | /** |
| 377 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 378 | */ |
| 379 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 380 | } | null |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Anthropic-specific options for tool approval response prompt parts. |
| 385 | * |
| 386 | * **Details** |
| 387 | * |
| 388 | * Controls prompt caching for human approval responses in conversations. |
| 389 | * |
| 390 | * @stability unstable |
| 391 | * @category models |
| 392 | * @since 4.0.0 |
| 393 | */ |
| 394 | export interface ToolApprovalResponsePartOptions extends ProviderOptions { |
| 395 | readonly anthropic?: { |
| 396 | /** |
| 397 | * A breakpoint which marks the end of reusable content eligible for caching. |
| 398 | */ |
| 399 | readonly cacheControl?: typeof Generated.CacheControlEphemeral.Encoded | null |
| 400 | } | null |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | declare module "effect/ai/Response" { |
| 405 | /** |
| 406 | * Anthropic metadata attached when a reasoning block begins. |
| 407 | * |
| 408 | * **Details** |
| 409 | * |
| 410 | * Includes Claude thinking metadata needed to continue reasoning-aware |
| 411 | * conversations. |
| 412 | * |
| 413 | * @stability unstable |
| 414 | * @category models |
| 415 | * @since 4.0.0 |
| 416 | */ |
| 417 | export interface ReasoningStartPartMetadata extends ProviderMetadata { |
| 418 | readonly anthropic?: { |
| 419 | readonly info?: { |
| 420 | readonly type: "thinking" |
| 421 | /** |
| 422 | * Thinking content as an encrypted string, which is used to verify |
| 423 | * that thinking content was indeed generated by Anthropic's API. |
| 424 | */ |
| 425 | readonly signature: typeof Generated.ResponseThinkingBlock.fields.thinking.Encoded |
| 426 | } | { |
| 427 | readonly type: "redacted_thinking" |
| 428 | /** |
| 429 | * Thinking content which was flagged by Anthropic's safety systems, and |
| 430 | * was therefore encrypted. |
| 431 | */ |
| 432 | readonly redactedData: typeof Generated.ResponseRedactedThinkingBlock.fields.data.Encoded |
| 433 | } | null |
| 434 | } | null |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Anthropic metadata attached to streaming reasoning deltas. |
| 439 | * |
| 440 | * **Details** |
| 441 | * |
| 442 | * Includes the signature for streamed Claude thinking content when available. |
| 443 | * |
| 444 | * @stability unstable |
| 445 | * @category models |
| 446 | * @since 4.0.0 |
| 447 | */ |
| 448 | export interface ReasoningDeltaPartMetadata extends ProviderMetadata { |
| 449 | readonly anthropic?: { |
| 450 | readonly info?: { |
| 451 | readonly type: "thinking" |
| 452 | /** |
| 453 | * Thinking content as an encrypted string, which is used to verify |
| 454 | * that thinking content was indeed generated by Anthropic's API. |
| 455 | */ |
| 456 | readonly signature: typeof Generated.ResponseThinkingBlock.fields.thinking.Encoded |
| 457 | } | null |
| 458 | } | null |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Anthropic metadata attached to completed reasoning parts. |
| 463 | * |
| 464 | * **Details** |
| 465 | * |
| 466 | * Preserves Claude thinking or redacted thinking information for later turns. |
| 467 | * |
| 468 | * @stability unstable |
| 469 | * @category models |
| 470 | * @since 4.0.0 |
| 471 | */ |
| 472 | export interface ReasoningPartMetadata extends ProviderMetadata { |
| 473 | readonly anthropic?: { |
| 474 | readonly info?: { |
| 475 | readonly type: "thinking" |
| 476 | /** |
| 477 | * Thinking content as an encrypted string, which is used to verify |
| 478 | * that thinking content was indeed generated by Anthropic's API. |
| 479 | */ |
| 480 | readonly signature: typeof Generated.ResponseThinkingBlock.fields.thinking.Encoded |
| 481 | } | { |
| 482 | readonly type: "redacted_thinking" |
| 483 | /** |
| 484 | * Thinking content which was flagged by Anthropic's safety systems, and |
| 485 | * was therefore encrypted. |
| 486 | */ |
| 487 | readonly redactedData: typeof Generated.ResponseRedactedThinkingBlock.fields.data.Encoded |
| 488 | } | null |
| 489 | } | null |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Anthropic metadata attached to tool call response parts. |
| 494 | * |
| 495 | * **Details** |
| 496 | * |
| 497 | * Identifies Anthropic caller details and MCP tool metadata emitted by the |
| 498 | * provider. |
| 499 | * |
| 500 | * @stability unstable |
| 501 | * @category models |
| 502 | * @since 4.0.0 |
| 503 | */ |
| 504 | export interface ToolCallPartMetadata extends ProviderMetadata { |
| 505 | readonly anthropic?: { |
| 506 | readonly caller?: { |
| 507 | readonly type: string |
| 508 | readonly toolId?: string | null |
| 509 | } | null |
| 510 | /** |
| 511 | * Contains details about the MCP tool that was called. |
| 512 | */ |
| 513 | readonly mcp_tool?: { |
| 514 | /** |
| 515 | * The name of the MCP server |
| 516 | */ |
| 517 | readonly server: string |
| 518 | } | null |
| 519 | } | null |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Anthropic metadata attached to tool result response parts. |
| 524 | * |
| 525 | * **Details** |
| 526 | * |
| 527 | * Identifies MCP tool metadata associated with provider-executed tool |
| 528 | * results. |
| 529 | * |
| 530 | * @stability unstable |
| 531 | * @category models |
| 532 | * @since 4.0.0 |
| 533 | */ |
| 534 | export interface ToolResultPartMetadata extends ProviderMetadata { |
| 535 | readonly anthropic?: { |
| 536 | /** |
| 537 | * Contains details about the MCP tool that was called. |
| 538 | */ |
| 539 | readonly mcp_tool?: { |
| 540 | /** |
| 541 | * The name of the MCP server |
| 542 | */ |
| 543 | readonly server: string |
| 544 | } | null |
| 545 | } | null |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Anthropic metadata for document citations in model responses. |
| 550 | * |
| 551 | * **Details** |
| 552 | * |
| 553 | * Records the cited document span by character position or page number. |
| 554 | * |
| 555 | * @stability unstable |
| 556 | * @category models |
| 557 | * @since 4.0.0 |
| 558 | */ |
| 559 | export interface DocumentSourcePartMetadata extends ProviderMetadata { |
| 560 | readonly anthropic?: { |
| 561 | readonly source: "document" |
| 562 | readonly type: "char_location" |
| 563 | /** |
| 564 | * The text that was cited in the response. |
| 565 | */ |
| 566 | readonly citedText: string |
| 567 | /** |
| 568 | * The 0-indexed starting position of the characters that were cited. |
| 569 | */ |
| 570 | readonly startCharIndex: number |
| 571 | /** |
| 572 | * The exclusive ending position of the characters that were cited. |
| 573 | */ |
| 574 | readonly endCharIndex: number |
| 575 | } | { |
| 576 | readonly source: "document" |
| 577 | readonly type: "page_location" |
| 578 | /** |
| 579 | * The text that was cited in the response. |
| 580 | */ |
| 581 | readonly citedText: string |
| 582 | /** |
| 583 | * The 1-indexed starting page of pages that were cited. |
| 584 | */ |
| 585 | readonly startPageNumber: number |
| 586 | /** |
| 587 | * The exclusive ending position of the pages that were cited. |
| 588 | */ |
| 589 | readonly endPageNumber: number |
| 590 | } | null |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Anthropic metadata for URL and web citations in model responses. |
| 595 | * |
| 596 | * **Details** |
| 597 | * |
| 598 | * Records cited URL text or web-search source freshness information. |
| 599 | * |
| 600 | * @stability unstable |
| 601 | * @category models |
| 602 | * @since 4.0.0 |
| 603 | */ |
| 604 | export interface UrlSourcePartMetadata extends ProviderMetadata { |
| 605 | readonly anthropic?: { |
| 606 | readonly source: "url" |
| 607 | /** |
| 608 | * Up to 150 characters of the text content that was referenced from the |
| 609 | * URL source material. |
| 610 | */ |
| 611 | readonly citedText: string |
| 612 | /** |
| 613 | * An internal reference that must be passed back to the Anthropic API |
| 614 | * during multi-turn conversations. |
| 615 | */ |
| 616 | readonly encryptedIndex: string |
| 617 | } | { |
| 618 | readonly source: "web" |
| 619 | readonly pageAge: string | null |
| 620 | } | null |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Anthropic metadata attached to the finish part of a response. |
| 625 | * |
| 626 | * **Details** |
| 627 | * |
| 628 | * Includes container state, context management information, stop details, and |
| 629 | * token usage reported by Anthropic. |
| 630 | * |
| 631 | * @stability unstable |
| 632 | * @category models |
| 633 | * @since 4.0.0 |
| 634 | */ |
| 635 | export interface FinishPartMetadata extends ProviderMetadata { |
| 636 | readonly anthropic?: { |
| 637 | readonly container: typeof Generated.BetaContainer.Encoded | null |
| 638 | readonly contextManagement: typeof Generated.BetaResponseContextManagement.Encoded | null |
| 639 | readonly stopSequence: string | null |
| 640 | readonly usage: typeof Generated.BetaMessage.Encoded["usage"] | null |
| 641 | } | null |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Anthropic metadata attached to error response parts. |
| 646 | * |
| 647 | * **Details** |
| 648 | * |
| 649 | * Includes the provider request identifier when Anthropic returns one. |
| 650 | * |
| 651 | * @stability unstable |
| 652 | * @category models |
| 653 | * @since 4.0.0 |
| 654 | */ |
| 655 | export interface ErrorPartMetadata extends ProviderMetadata { |
| 656 | readonly anthropic?: { |
| 657 | requestId?: string | null |
| 658 | } | null |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Anthropic metadata attached to response metadata parts. |
| 663 | * |
| 664 | * **Details** |
| 665 | * |
| 666 | * Fallback blocks are stored in `anthropic.fallback`; the part's `modelId` |
| 667 | * identifies the fallback model. |
| 668 | * |
| 669 | * @stability unstable |
| 670 | * @category models |
| 671 | * @since 4.0.3 |
| 672 | */ |
| 673 | export interface ResponseMetadataPartMetadata extends ProviderMetadata { |
| 674 | readonly anthropic?: { |
| 675 | readonly fallback?: typeof Generated.BetaResponseFallbackBlock.Encoded |
| 676 | } | null |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | // ============================================================================= |
| 681 | // Language Model |
| 682 | // ============================================================================= |
| 683 | |
| 684 | /** |
| 685 | * Creates an Anthropic model descriptor that can be provided with `Effect.provide`. |
| 686 | * |
| 687 | * **When to use** |
| 688 | * |
| 689 | * Use when you want an Anthropic Claude model value that carries provider and |
| 690 | * model metadata and can be supplied directly to an Effect program. |
| 691 | * |
| 692 | * @see {@link layer} for creating a `LanguageModel.LanguageModel` layer directly |
| 693 | * @see {@link make} for constructing the language model service effectfully |
| 694 | * |
| 695 | * @stability unstable |
| 696 | * @category constructors |
| 697 | * @since 4.0.0 |
| 698 | */ |
| 699 | export const model = ( |
| 700 | model: (string & {}) | Model, |
| 701 | config?: Omit<typeof Config.Service, "model"> |
| 702 | ): AiModel.Model<"anthropic", LanguageModel.LanguageModel, AnthropicClient> => |
| 703 | AiModel.make("anthropic", model, layer({ model, config })) |
| 704 | |
| 705 | /** |
| 706 | * Creates an Anthropic `LanguageModel` service from a model identifier and optional request defaults. |
| 707 | * |
| 708 | * **When to use** |
| 709 | * |
| 710 | * Use when you need to construct a `LanguageModel` value backed by |
| 711 | * `AnthropicClient` inside an Effect. |
| 712 | * |
| 713 | * **Details** |
| 714 | * |
| 715 | * The returned effect requires `AnthropicClient`. Request defaults from the |
| 716 | * `config` option are merged with any `Config` service in the context, with |
| 717 | * context values taking precedence. |
| 718 | * |
| 719 | * @see {@link layer} for providing the service as a `Layer` |
| 720 | * @see {@link model} for creating a model descriptor for `AiModel.provide` |
| 721 | * |
| 722 | * @stability unstable |
| 723 | * @category constructors |
| 724 | * @since 4.0.0 |
| 725 | */ |
| 726 | export const make = Effect.fnUntraced(function*({ model, config: providerConfig }: { |
| 727 | readonly model: (string & {}) | Model |
| 728 | readonly config?: Omit<typeof Config.Service, "model"> | undefined |
| 729 | }): Effect.fn.Return<LanguageModel.LanguageModel, never, AnthropicClient> { |
| 730 | const client = yield* AnthropicClient |
| 731 | |
| 732 | const makeConfig: Effect.Effect<typeof Config.Service & { readonly model: string }> = Effect.contextWith((services) => |
| 733 | Effect.succeed({ |
| 734 | model, |
| 735 | ...providerConfig, |
| 736 | ...Context.getOrUndefined(services, Config) |
| 737 | }) |
| 738 | ) |
| 739 | |
| 740 | const makeRequest = Effect.fnUntraced( |
| 741 | function*<Tools extends ReadonlyArray<Tool.Any>>({ config, options, toolNameMapper }: { |
| 742 | readonly config: typeof Config.Service & { readonly model: string } |
| 743 | readonly options: LanguageModel.ProviderOptions |
| 744 | readonly toolNameMapper: Tool.NameMapper<Tools> |
| 745 | }): Effect.fn.Return<{ |
| 746 | readonly params: typeof Generated.BetaMessagesPostParams.Encoded |
| 747 | readonly payload: typeof Generated.BetaCreateMessageParams.Encoded |
| 748 | }, AiError.AiError> { |
| 749 | const betas = new Set<string>() |
| 750 | const capabilities = getConfigCapabilities(config) |
| 751 | const { messages, system } = yield* prepareMessages({ betas, capabilities, options, toolNameMapper }) |
| 752 | const outputFormat = yield* getOutputFormat({ capabilities, options }) |
| 753 | const { tools, toolChoice } = yield* prepareTools({ betas, capabilities, config, options, toolNameMapper }) |
| 754 | const params: Mutable<typeof Generated.BetaMessagesPostParams.Encoded> = {} |
| 755 | if (betas.size > 0) { |
| 756 | params["anthropic-beta"] = Array.from(betas).join(",") |
| 757 | } |
| 758 | const { |
| 759 | disableParallelToolCalls: _, |
| 760 | output_config, |
| 761 | strictJsonSchema: _strictJsonSchema, |
| 762 | structuredOutputs: _structuredOutputs, |
| 763 | midConversationSystemMessages: _midConversationSystemMessages, |
| 764 | ...requestConfig |
| 765 | } = config |
| 766 | const payload: Mutable<typeof Generated.BetaCreateMessageParams.Encoded> = { |
| 767 | ...requestConfig, |
| 768 | max_tokens: requestConfig.max_tokens ?? capabilities.maxOutputTokens, |
| 769 | messages, |
| 770 | ...(Predicate.isNotUndefined(system) ? { system } : undefined), |
| 771 | ...(Predicate.isNotUndefined(tools) ? { tools } : undefined), |
| 772 | ...(Predicate.isNotUndefined(toolChoice) ? { tool_choice: toolChoice } : undefined) |
| 773 | } |
| 774 | const outputConfig: Mutable<typeof Generated.BetaCreateMessageParams.Encoded["output_config"]> = {} |
| 775 | if (Predicate.isNotUndefined(outputFormat)) { |
| 776 | outputConfig.format = outputFormat |
| 777 | } |
| 778 | if (Predicate.isNotUndefined(output_config?.effort)) { |
| 779 | outputConfig.effort = output_config.effort |
| 780 | } |
| 781 | if (Object.keys(outputConfig).length > 0) { |
| 782 | payload.output_config = outputConfig |
| 783 | } |
| 784 | return { params, payload } |
| 785 | } |
| 786 | ) |
| 787 | |
| 788 | return yield* LanguageModel.make({ |
| 789 | codecTransformer: toCodecAnthropic, |
| 790 | supportsSystemMessagesInHistory: Effect.map( |
| 791 | makeConfig, |
| 792 | (config) => getConfigCapabilities(config).supportsMidConversationSystemMessages |
| 793 | ), |
| 794 | generateText: Effect.fnUntraced(function*(options) { |
| 795 | const config = yield* makeConfig |
| 796 | const toolNameMapper = new Tool.NameMapper(options.tools) |
| 797 | const request = yield* makeRequest({ config, options, toolNameMapper }) |
| 798 | annotateRequest(options.span, request.payload) |
| 799 | const [rawResponse, response] = yield* client.createMessage(request) |
| 800 | annotateResponse(options.span, rawResponse) |
| 801 | return yield* makeResponse({ options, rawResponse, response, toolNameMapper }) |
| 802 | }), |
| 803 | streamText: Effect.fnUntraced(function*(options) { |
| 804 | const config = yield* makeConfig |
| 805 | const toolNameMapper = new Tool.NameMapper(options.tools) |
| 806 | const request = yield* makeRequest({ config, options, toolNameMapper }) |
| 807 | annotateRequest(options.span, request.payload) |
| 808 | const [response, stream] = yield* client.createMessageStream(request) |
| 809 | return yield* makeStreamResponse({ stream, response, options, toolNameMapper }) |
| 810 | }, (effect, options) => |
| 811 | effect.pipe( |
| 812 | Stream.unwrap, |
| 813 | Stream.map((response) => { |
| 814 | annotateStreamResponse(options.span, response) |
| 815 | return response |
| 816 | }) |
| 817 | )) |
| 818 | }) |
| 819 | }) |
| 820 | |
| 821 | /** |
| 822 | * Creates a layer for the Anthropic language model. |
| 823 | * |
| 824 | * **When to use** |
| 825 | * |
| 826 | * Use when composing application layers and you want Anthropic to satisfy |
| 827 | * `LanguageModel.LanguageModel` while supplying `AnthropicClient` from another |
| 828 | * layer. |
| 829 | * |
| 830 | * @see {@link make} for constructing the language model service effectfully |
| 831 | * @see {@link model} for creating a model service directly |
| 832 | * |
| 833 | * @stability unstable |
| 834 | * @category layers |
| 835 | * @since 4.0.0 |
| 836 | */ |
| 837 | export const layer = (options: { |
| 838 | readonly model: (string & {}) | Model |
| 839 | readonly config?: Omit<typeof Config.Service, "model"> | undefined |
| 840 | }): Layer.Layer<LanguageModel.LanguageModel, never, AnthropicClient> => |
| 841 | Layer.effect(LanguageModel.LanguageModel, make(options)) |
| 842 | |
| 843 | /** |
| 844 | * Provides config overrides for Anthropic language model operations. |
| 845 | * |
| 846 | * **When to use** |
| 847 | * |
| 848 | * Use to apply Anthropic request configuration to one effect without changing |
| 849 | * the model's default configuration. |
| 850 | * |
| 851 | * **Details** |
| 852 | * |
| 853 | * The overrides are merged with any existing `Config` service for the duration |
| 854 | * of the supplied effect. Fields in `overrides` take precedence over existing |
| 855 | * config, and the helper supports both `effect.pipe(withConfigOverride(overrides))` |
| 856 | * and `withConfigOverride(effect, overrides)`. |
| 857 | * |
| 858 | * @see {@link Config} for available Anthropic request configuration fields |
| 859 | * |
| 860 | * @stability unstable |
| 861 | * @category configuration |
| 862 | * @since 4.0.0 |
| 863 | */ |
| 864 | export const withConfigOverride: { |
| 865 | (overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>> |
| 866 | <A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>> |
| 867 | } = dual< |
| 868 | ( |
| 869 | overrides: typeof Config.Service |
| 870 | ) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>, |
| 871 | <A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>> |
| 872 | >(2, (self, overrides) => |
| 873 | Effect.flatMap( |
| 874 | Effect.serviceOption(Config), |
| 875 | (config) => |
| 876 | Effect.provideService(self, Config, { |
| 877 | ...(config._tag === "Some" ? config.value : {}), |
| 878 | ...overrides |
| 879 | }) |
| 880 | )) |
| 881 | |
| 882 | // ============================================================================= |
| 883 | // Prompt Conversion |
| 884 | // ============================================================================= |
| 885 | |
| 886 | const prepareMessages = Effect.fnUntraced( |
| 887 | function*<Tools extends ReadonlyArray<Tool.Any>>({ betas, capabilities, options, toolNameMapper }: { |
| 888 | readonly betas: Set<string> |
| 889 | readonly capabilities: ModelCapabilities |
| 890 | readonly options: LanguageModel.ProviderOptions |
| 891 | readonly toolNameMapper: Tool.NameMapper<Tools> |
| 892 | }): Effect.fn.Return<{ |
| 893 | readonly system: ReadonlyArray<typeof Generated.BetaRequestTextBlock.Encoded> | undefined |
| 894 | readonly messages: ReadonlyArray<typeof Generated.BetaInputMessage.Encoded> |
| 895 | }, AiError.AiError> { |
| 896 | const groups = groupMessages(options.prompt) |
| 897 | |
| 898 | // A system message in history must directly follow a user turn and directly |
| 899 | // precede an assistant turn or the end of the prompt, so later instructions |
| 900 | // are held until the next assistant turn. If any of them cannot be placed |
| 901 | // that way, every instruction goes in the top-level `system` field instead: |
| 902 | // inline instructions override top-level ones, so mixing would reorder them. |
| 903 | const inlineSystem = capabilities.supportsMidConversationSystemMessages && |
| 904 | groups.every((group, i) => |
| 905 | i === 0 || group.type !== "system" || groups[i - 1].type === "user" || groups[i + 1]?.type === "user" |
| 906 | ) |
| 907 | |
| 908 | const system: Array<typeof Generated.BetaRequestTextBlock.Encoded> = [] |
| 909 | const messages: Array<typeof Generated.BetaInputMessage.Encoded> = [] |
| 910 | let pendingSystem: Array<typeof Generated.BetaRequestTextBlock.Encoded> = [] |
| 911 | |
| 912 | for (let i = 0; i < groups.length; i++) { |
| 913 | const group = groups[i] |
| 914 | const isLastGroup = i === groups.length - 1 |
| 915 | |
| 916 | switch (group.type) { |
| 917 | case "system": { |
| 918 | const target = i === 0 || !inlineSystem ? system : pendingSystem |
| 919 | for (const message of group.messages) { |
| 920 | target.push({ |
| 921 | type: "text", |
| 922 | text: message.content, |
| 923 | cache_control: getCacheControl(message) |
| 924 | }) |
| 925 | } |
| 926 | break |
| 927 | } |
| 928 | |
| 929 | case "user": { |
| 930 | const content: Array<typeof Generated.BetaInputContentBlock.Encoded> = [] |
| 931 | |
| 932 | for (const message of group.messages) { |
| 933 | switch (message.role) { |
| 934 | case "user": { |
| 935 | for (let j = 0; j < message.content.length; j++) { |
| 936 | const part = message.content[j] |
| 937 | const isLastPart = j === message.content.length - 1 |
| 938 | |
| 939 | // Attempt to get the cache control from the part first. If |
| 940 | // the part does not have cache control defined and we are |
| 941 | // evaluating the last part for this message, also check the |
| 942 | // message for cache control. |
| 943 | const cacheControl = getCacheControl(part) ?? ( |
| 944 | isLastPart ? getCacheControl(message) : null |
| 945 | ) |
| 946 | |
| 947 | switch (part.type) { |
| 948 | case "text": { |
| 949 | content.push({ |
| 950 | type: "text", |
| 951 | text: part.text, |
| 952 | cache_control: cacheControl |
| 953 | }) |
| 954 | break |
| 955 | } |
| 956 | |
| 957 | case "file": { |
| 958 | content.push(yield* prepareFilePart(part, cacheControl, betas)) |
| 959 | break |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | break |
| 964 | } |
| 965 | |
| 966 | case "tool": { |
| 967 | for (let j = 0; j < message.content.length; j++) { |
| 968 | const part = message.content[j] |
| 969 | |
| 970 | // Skip evaluation of tool approval parts |
| 971 | if (part.type === "tool-approval-response") { |
| 972 | continue |
| 973 | } |
| 974 | |
| 975 | const isLastPart = j === message.content.length - 1 |
| 976 | |
| 977 | // Attempt to get the cache control from the part first. If |
| 978 | // the part does not have cache control defined and we are |
| 979 | // evaluating the last part for this message, also check the |
| 980 | // message for cache control. |
| 981 | const cacheControl = getCacheControl(part) ?? ( |
| 982 | isLastPart ? getCacheControl(message) : null |
| 983 | ) |
| 984 | |
| 985 | content.push({ |
| 986 | type: "tool_result", |
| 987 | tool_use_id: part.id, |
| 988 | content: Predicate.isUndefined(part.modelOutput) |
| 989 | ? typeof part.result === "string" ? part.result : JSON.stringify(part.result) |
| 990 | : yield* prepareToolResultContent(part.modelOutput, betas), |
| 991 | is_error: part.isFailure, |
| 992 | cache_control: cacheControl |
| 993 | }) |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | messages.push({ role: "user", content }) |
| 1000 | |
| 1001 | break |
| 1002 | } |
| 1003 | |
| 1004 | case "assistant": { |
| 1005 | if (pendingSystem.length > 0) { |
| 1006 | messages.push({ role: "system", content: pendingSystem }) |
| 1007 | pendingSystem = [] |
| 1008 | } |
| 1009 | |
| 1010 | const content: Array<typeof Generated.BetaInputContentBlock.Encoded> = [] |
| 1011 | const mcpToolIds = new Set<string>() |
| 1012 | |
| 1013 | for (let j = 0; j < group.messages.length; j++) { |
| 1014 | const message = group.messages[j] |
| 1015 | const isLastMessage = j === group.messages.length - 1 |
| 1016 | const messageStart = content.length |
| 1017 | |
| 1018 | for (let k = 0; k < message.content.length; k++) { |
| 1019 | const part = message.content[k] |
| 1020 | |
| 1021 | if (part.type === "file" || part.type === "tool-approval-request") { |
| 1022 | continue |
| 1023 | } |
| 1024 | |
| 1025 | const isLastPart = k === message.content.length - 1 |
| 1026 | const partStart = content.length |
| 1027 | |
| 1028 | switch (part.type) { |
| 1029 | case "text": { |
| 1030 | content.push({ |
| 1031 | type: "text", |
| 1032 | // Anthropic does not allow trailing whitespace in assistant |
| 1033 | // content blocks |
| 1034 | text: isLastGroup && isLastMessage && isLastPart |
| 1035 | ? part.text.trim() |
| 1036 | : part.text |
| 1037 | }) |
| 1038 | break |
| 1039 | } |
| 1040 | case "reasoning": { |
| 1041 | // TODO: make sending reasoning configurable |
| 1042 | const info = part.options.anthropic?.info |
| 1043 | if (Predicate.isNotNullish(info)) { |
| 1044 | if (info.type === "thinking") { |
| 1045 | content.push({ |
| 1046 | type: "thinking", |
| 1047 | thinking: part.text, |
| 1048 | signature: info.signature |
| 1049 | }) |
| 1050 | } else { |
| 1051 | content.push({ |
| 1052 | type: "redacted_thinking", |
| 1053 | data: info.redactedData |
| 1054 | }) |
| 1055 | } |
| 1056 | } |
| 1057 | break |
| 1058 | } |
| 1059 | |
| 1060 | case "tool-call": { |
| 1061 | if (part.providerExecuted) { |
| 1062 | const toolName = toolNameMapper.getProviderName(part.name) |
| 1063 | |
| 1064 | const isMcpTool = Predicate.isNotNullish(part.options.anthropic?.mcp_tool) |
| 1065 | |
| 1066 | if (isMcpTool) { |
| 1067 | const { server } = part.options.anthropic.mcp_tool |
| 1068 | |
| 1069 | mcpToolIds.add(part.id) |
| 1070 | |
| 1071 | content.push({ |
| 1072 | type: "mcp_tool_use", |
| 1073 | id: part.id, |
| 1074 | name: part.name, |
| 1075 | input: part.params as any, |
| 1076 | server_name: server |
| 1077 | }) |
| 1078 | } else if ( |
| 1079 | toolName === "code_execution" && |
| 1080 | Predicate.hasProperty(part.params, "type") && |
| 1081 | ( |
| 1082 | part.params.type === "bash_code_execution" || |
| 1083 | part.params.type === "text_editor_code_execution" |
| 1084 | ) |
| 1085 | ) { |
| 1086 | content.push({ |
| 1087 | type: "server_tool_use", |
| 1088 | id: part.id, |
| 1089 | name: part.params.type, |
| 1090 | input: part.params as any |
| 1091 | }) |
| 1092 | } else if ( |
| 1093 | // code execution 20250825 programmatic tool calling: |
| 1094 | // Strip the fake 'programmatic-tool-call' type before sending to Anthropic |
| 1095 | toolName === "code_execution" && |
| 1096 | Predicate.hasProperty(part.params, "type") && |
| 1097 | part.params.type === "programmatic-tool-call" |
| 1098 | ) { |
| 1099 | const { type: _type, ...params } = part.params |
| 1100 | content.push({ |
| 1101 | type: "server_tool_use", |
| 1102 | id: part.id, |
| 1103 | name: toolName, |
| 1104 | input: params as any |
| 1105 | }) |
| 1106 | } else { |
| 1107 | if ( |
| 1108 | // code execution 20250522 |
| 1109 | toolName === "code_execution" || |
| 1110 | toolName === "tool_search_tool_regex" || |
| 1111 | toolName === "tool_search_tool_bm25" || |
| 1112 | toolName === "web_fetch" || |
| 1113 | toolName === "web_search" |
| 1114 | ) { |
| 1115 | content.push({ |
| 1116 | type: "server_tool_use", |
| 1117 | id: part.id, |
| 1118 | name: toolName, |
| 1119 | input: part.params as any |
| 1120 | }) |
| 1121 | } |
| 1122 | } |
| 1123 | } else { |
| 1124 | // Extract caller info from provider options for programmatic tool calling |
| 1125 | const options = part.options.anthropic |
| 1126 | const caller = Predicate.isNotNullish(options?.caller) |
| 1127 | ? ( |
| 1128 | options.caller.type === "code_execution_20250825" && |
| 1129 | Predicate.isNotNullish(options.caller.toolId) |
| 1130 | ) |
| 1131 | ? { |
| 1132 | type: "code_execution_20250825", |
| 1133 | tool_id: options.caller.toolId |
| 1134 | } as const |
| 1135 | : options.caller.type === "direct" |
| 1136 | ? { |
| 1137 | type: "direct" |
| 1138 | } as const |
| 1139 | : undefined |
| 1140 | : undefined |
| 1141 | |
| 1142 | content.push({ |
| 1143 | type: "tool_use", |
| 1144 | id: part.id, |
| 1145 | name: toolNameMapper.getProviderName(part.name), |
| 1146 | input: part.params as any, |
| 1147 | ...(Predicate.isNotUndefined(caller) ? { caller } : undefined) |
| 1148 | }) |
| 1149 | } |
| 1150 | |
| 1151 | break |
| 1152 | } |
| 1153 | |
| 1154 | case "tool-result": { |
| 1155 | const toolName = toolNameMapper.getProviderName(part.name) |
| 1156 | |
| 1157 | if (mcpToolIds.has(part.id)) { |
| 1158 | content.push({ |
| 1159 | type: "mcp_tool_result", |
| 1160 | tool_use_id: part.id, |
| 1161 | is_error: part.isFailure, |
| 1162 | content: part.result as any |
| 1163 | }) |
| 1164 | break |
| 1165 | } |
| 1166 | |
| 1167 | if (toolName === "code_execution" && Predicate.hasProperty(part.result, "type")) { |
| 1168 | if (part.result.type === "code_execution_result") { |
| 1169 | content.push({ |
| 1170 | type: "code_execution_tool_result", |
| 1171 | tool_use_id: part.id, |
| 1172 | content: part.result as any |
| 1173 | }) |
| 1174 | } else if ( |
| 1175 | part.result.type === "bash_code_execution_result" || |
| 1176 | part.result.type === "bash_code_execution_tool_result_error" |
| 1177 | ) { |
| 1178 | content.push({ |
| 1179 | type: "bash_code_execution_tool_result", |
| 1180 | tool_use_id: part.id, |
| 1181 | content: part.result as any |
| 1182 | }) |
| 1183 | } else if ( |
| 1184 | part.result.type === "text_editor_code_execution_tool_result" || |
| 1185 | part.result.type === "text_editor_code_execution_tool_result_error" |
| 1186 | ) { |
| 1187 | content.push({ |
| 1188 | type: "text_editor_code_execution_tool_result", |
| 1189 | tool_use_id: part.id, |
| 1190 | content: part.result as any |
| 1191 | }) |
| 1192 | } |
| 1193 | break |
| 1194 | } |
| 1195 | |
| 1196 | if (toolName === "web_fetch") { |
| 1197 | content.push({ |
| 1198 | type: "web_fetch_tool_result", |
| 1199 | tool_use_id: part.id, |
| 1200 | content: part.result as any |
| 1201 | }) |
| 1202 | break |
| 1203 | } |
| 1204 | |
| 1205 | if (toolName === "web_search") { |
| 1206 | content.push({ |
| 1207 | type: "web_search_tool_result", |
| 1208 | tool_use_id: part.id, |
| 1209 | content: part.result as any |
| 1210 | }) |
| 1211 | break |
| 1212 | } |
| 1213 | |
| 1214 | if ( |
| 1215 | toolName === "tool_search_tool_regex" || |
| 1216 | toolName === "tool_search_tool_bm25" |
| 1217 | ) { |
| 1218 | content.push({ |
| 1219 | type: "tool_search_tool_result", |
| 1220 | tool_use_id: part.id, |
| 1221 | content: Array.isArray(part.result) |
| 1222 | ? { type: "tool_search_tool_search_result", tool_references: part.result } |
| 1223 | : part.result as any |
| 1224 | }) |
| 1225 | break |
| 1226 | } |
| 1227 | |
| 1228 | break |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | setLastCacheControl(content, partStart, getCacheControl(part)) |
| 1233 | } |
| 1234 | |
| 1235 | setLastCacheControl(content, messageStart, getCacheControl(message)) |
| 1236 | } |
| 1237 | |
| 1238 | messages.push({ role: "assistant", content }) |
| 1239 | |
| 1240 | break |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | if (pendingSystem.length > 0) { |
| 1246 | messages.push({ role: "system", content: pendingSystem }) |
| 1247 | } |
| 1248 | |
| 1249 | return { |
| 1250 | system: system.length > 0 ? system : undefined, |
| 1251 | messages |
| 1252 | } |
| 1253 | } |
| 1254 | ) |
| 1255 | |
| 1256 | // ============================================================================= |
| 1257 | // Tool Conversion |
| 1258 | // ============================================================================= |
| 1259 | |
| 1260 | /** |
| 1261 | * Encoded Anthropic custom tool definition that can be sent in a Messages API request. |
| 1262 | * |
| 1263 | * **When to use** |
| 1264 | * |
| 1265 | * Use when you need to type or inspect the provider-specific request payload for |
| 1266 | * a custom Anthropic tool. |
| 1267 | * |
| 1268 | * **Details** |
| 1269 | * |
| 1270 | * This type aliases the encoded `Generated.BetaTool` schema used for Effect |
| 1271 | * user-defined and dynamic tools after conversion. It contains the tool `name`, |
| 1272 | * optional `description`, and `input_schema`, plus Anthropic-specific fields |
| 1273 | * such as `strict` and `cache_control`. |
| 1274 | * |
| 1275 | * @see {@link AnthropicProviderDefinedTool} for the request shape used by Anthropic built-in provider tools |
| 1276 | * |
| 1277 | * @stability unstable |
| 1278 | * @category tools |
| 1279 | * @since 4.0.0 |
| 1280 | */ |
| 1281 | export type AnthropicUserDefinedTool = typeof Generated.BetaTool.Encoded |
| 1282 | |
| 1283 | /** |
| 1284 | * Represents a provider-defined tool that can be passed to the Anthropic API. |
| 1285 | * |
| 1286 | * **Details** |
| 1287 | * |
| 1288 | * These include Anthropic's built-in tools like computer use, code execution, |
| 1289 | * web search, and text editing. |
| 1290 | * |
| 1291 | * @stability unstable |
| 1292 | * @category tools |
| 1293 | * @since 4.0.0 |
| 1294 | */ |
| 1295 | export type AnthropicProviderDefinedTool = |
| 1296 | | typeof Generated.BetaBashTool_20241022.Encoded |
| 1297 | | typeof Generated.BetaBashTool_20250124.Encoded |
| 1298 | | typeof Generated.BetaCodeExecutionTool_20250522.Encoded |
| 1299 | | typeof Generated.BetaCodeExecutionTool_20250825.Encoded |
| 1300 | | typeof Generated.BetaComputerUseTool_20241022.Encoded |
| 1301 | | typeof Generated.BetaComputerUseTool_20250124.Encoded |
| 1302 | | typeof Generated.BetaComputerUseTool_20251124.Encoded |
| 1303 | | typeof Generated.BetaMemoryTool_20250818.Encoded |
| 1304 | | typeof Generated.BetaTextEditor_20241022.Encoded |
| 1305 | | typeof Generated.BetaTextEditor_20250124.Encoded |
| 1306 | | typeof Generated.BetaTextEditor_20250429.Encoded |
| 1307 | | typeof Generated.BetaTextEditor_20250728.Encoded |
| 1308 | | typeof Generated.BetaToolSearchToolBM25_20251119.Encoded |
| 1309 | | typeof Generated.BetaToolSearchToolRegex_20251119.Encoded |
| 1310 | | typeof Generated.BetaWebFetchTool_20250910.Encoded |
| 1311 | | typeof Generated.BetaWebSearchTool_20250305.Encoded |
| 1312 | |
| 1313 | const prepareToolResultContent = Effect.fnUntraced(function*( |
| 1314 | modelOutput: ReadonlyArray<Prompt.ToolModelOutputPart>, |
| 1315 | betas: Set<string> |
| 1316 | ): Effect.fn.Return< |
| 1317 | | string |
| 1318 | | Array< |
| 1319 | | typeof Generated.BetaRequestTextBlock.Encoded |
| 1320 | | typeof Generated.BetaRequestImageBlock.Encoded |
| 1321 | | typeof Generated.BetaRequestDocumentBlock.Encoded |
| 1322 | >, |
| 1323 | AiError.AiError |
| 1324 | > { |
| 1325 | if (modelOutput.length === 0) { |
| 1326 | return "" |
| 1327 | } |
| 1328 | const content: Array< |
| 1329 | | typeof Generated.BetaRequestTextBlock.Encoded |
| 1330 | | typeof Generated.BetaRequestImageBlock.Encoded |
| 1331 | | typeof Generated.BetaRequestDocumentBlock.Encoded |
| 1332 | > = [] |
| 1333 | for (const part of modelOutput) { |
| 1334 | const cacheControl = getCacheControl(part) |
| 1335 | content.push( |
| 1336 | part.type === "text" |
| 1337 | ? { type: "text", text: part.text, cache_control: cacheControl } |
| 1338 | : yield* prepareFilePart(part, cacheControl, betas) |
| 1339 | ) |
| 1340 | } |
| 1341 | return content |
| 1342 | }) |
| 1343 | |
| 1344 | const getBase64Data = (data: string | Uint8Array): string => |
| 1345 | typeof data === "string" ? data.replace(/^data:[^;]+;base64,/, "") : Base64.encode(data) |
| 1346 | |
| 1347 | const prepareFilePart = Effect.fnUntraced(function*( |
| 1348 | part: Prompt.FilePart, |
| 1349 | cacheControl: typeof Generated.CacheControlEphemeral.Encoded | null, |
| 1350 | betas: Set<string> |
| 1351 | ): Effect.fn.Return< |
| 1352 | typeof Generated.BetaRequestImageBlock.Encoded | typeof Generated.BetaRequestDocumentBlock.Encoded, |
| 1353 | AiError.AiError |
| 1354 | > { |
| 1355 | if (part.mediaType.startsWith("image/")) { |
| 1356 | const mediaType: typeof Generated.Base64ImageSource.Type["media_type"] = |
| 1357 | (part.mediaType === "image/*" ? "image/jpeg" : part.mediaType) as any |
| 1358 | |
| 1359 | const source = isUrlData(part.data) |
| 1360 | ? { type: "url", url: getUrlString(part.data) } as const |
| 1361 | : { type: "base64", media_type: mediaType, data: getBase64Data(part.data) } as const |
| 1362 | |
| 1363 | return { type: "image", source, cache_control: cacheControl } |
| 1364 | } |
| 1365 | |
| 1366 | if (part.mediaType === "application/pdf" || part.mediaType === "text/plain") { |
| 1367 | betas.add("pdfs-2024-09-25") |
| 1368 | |
| 1369 | const enableCitations = areCitationsEnabled(part) |
| 1370 | const documentOptions = getDocumentMetadata(part) |
| 1371 | |
| 1372 | const source = isUrlData(part.data) |
| 1373 | ? { |
| 1374 | type: "url", |
| 1375 | url: getUrlString(part.data) |
| 1376 | } as const |
| 1377 | : part.mediaType === "application/pdf" |
| 1378 | ? { type: "base64", media_type: "application/pdf", data: getBase64Data(part.data) } as const |
| 1379 | : { |
| 1380 | type: "text", |
| 1381 | media_type: "text/plain", |
| 1382 | data: typeof part.data === "string" ? part.data : new TextDecoder().decode(part.data) |
| 1383 | } as const |
| 1384 | |
| 1385 | return { |
| 1386 | type: "document", |
| 1387 | source, |
| 1388 | title: documentOptions?.title ?? part.fileName ?? null, |
| 1389 | cache_control: cacheControl, |
| 1390 | ...(documentOptions?.context ? { context: documentOptions.context } : undefined), |
| 1391 | ...(enableCitations ? { citations: { enabled: true } } : undefined) |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | return yield* new AiError.AiError({ |
| 1396 | module: "AnthropicLanguageModel", |
| 1397 | method: "prepareMessages", |
| 1398 | reason: new AiError.InvalidUserInputError({ |
| 1399 | description: `Detected unsupported media type for file: '${part.mediaType}'` |
| 1400 | }) |
| 1401 | }) |
| 1402 | }) |
| 1403 | |
| 1404 | const prepareTools = Effect.fnUntraced( |
| 1405 | function*<Tools extends ReadonlyArray<Tool.Any>>({ betas, capabilities, config, options, toolNameMapper }: { |
| 1406 | readonly betas: Set<string> |
| 1407 | readonly capabilities: ModelCapabilities |
| 1408 | readonly config: typeof Config.Service |
| 1409 | readonly options: LanguageModel.ProviderOptions |
| 1410 | readonly toolNameMapper: Tool.NameMapper<Tools> |
| 1411 | }): Effect.fn.Return<{ |
| 1412 | readonly tools: ReadonlyArray<AnthropicUserDefinedTool | AnthropicProviderDefinedTool> | undefined |
| 1413 | readonly toolChoice: typeof Generated.BetaToolChoice.Encoded | undefined |
| 1414 | }, AiError.AiError> { |
| 1415 | // Return a JSON response tool when using non-native structured outputs |
| 1416 | if (options.responseFormat.type === "json" && !capabilities.supportsStructuredOutput) { |
| 1417 | const input_schema = yield* tryJsonSchema(options.responseFormat.schema, "prepareTools") |
| 1418 | const userDescription = SchemaAST.resolveDescription(options.responseFormat.schema.ast) |
| 1419 | const description = Predicate.isNotUndefined(userDescription) ? `${userDescription} - ` : "" |
| 1420 | return { |
| 1421 | tools: [{ |
| 1422 | name: options.responseFormat.objectName, |
| 1423 | description: `${description}You MUST respond with a JSON object.`, |
| 1424 | input_schema: input_schema as any |
| 1425 | }], |
| 1426 | toolChoice: { |
| 1427 | type: "tool", |
| 1428 | name: options.responseFormat.objectName, |
| 1429 | disable_parallel_tool_use: true |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (options.tools.length === 0 || options.toolChoice === "none") { |
| 1435 | return { tools: undefined, toolChoice: undefined } |
| 1436 | } |
| 1437 | |
| 1438 | const userTools: Array<AnthropicUserDefinedTool> = [] |
| 1439 | const providerTools: Array<AnthropicProviderDefinedTool> = [] |
| 1440 | |
| 1441 | for (const tool of options.tools) { |
| 1442 | if (Tool.isUserDefined(tool) || Tool.isDynamic(tool)) { |
| 1443 | const description = Tool.getDescription(tool) |
| 1444 | const toolStrict = Tool.getStrictMode(tool) |
| 1445 | const strict = capabilities.supportsStructuredOutput |
| 1446 | ? (toolStrict ?? config.strictJsonSchema ?? true) |
| 1447 | : undefined |
| 1448 | const input_schema = yield* tryToolJsonSchema(tool, strict === true, "prepareTools") |
| 1449 | |
| 1450 | userTools.push({ |
| 1451 | name: tool.name, |
| 1452 | input_schema: input_schema as any, |
| 1453 | ...(Predicate.isNotUndefined(description) ? { description } : undefined), |
| 1454 | ...(Predicate.isNotUndefined(strict) ? { strict } : undefined), |
| 1455 | ...(Context.get(tool.annotations, AnthropicTool.DeferLoading) ? { defer_loading: true } : undefined) |
| 1456 | }) |
| 1457 | |
| 1458 | if (capabilities.supportsStructuredOutput === true) { |
| 1459 | betas.add("structured-outputs-2025-11-13") |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | if (Tool.isProviderDefined(tool)) { |
| 1464 | const providerTool = tool as AnthropicTool.AnthropicTool |
| 1465 | switch (providerTool.id) { |
| 1466 | case "anthropic.bash_20241022": { |
| 1467 | betas.add("computer-use-2024-10-22") |
| 1468 | providerTools.push({ name: "bash", type: "bash_20241022" }) |
| 1469 | break |
| 1470 | } |
| 1471 | |
| 1472 | case "anthropic.bash_20250124": { |
| 1473 | betas.add("computer-use-2025-01-24") |
| 1474 | providerTools.push({ name: "bash", type: "bash_20250124" }) |
| 1475 | break |
| 1476 | } |
| 1477 | |
| 1478 | case "anthropic.code_execution_20250522": { |
| 1479 | betas.add("code-execution-2025-05-22") |
| 1480 | providerTools.push({ name: "code_execution", type: "code_execution_20250522" }) |
| 1481 | break |
| 1482 | } |
| 1483 | |
| 1484 | case "anthropic.code_execution_20250825": { |
| 1485 | betas.add("code-execution-2025-08-25") |
| 1486 | providerTools.push({ name: "code_execution", type: "code_execution_20250825" }) |
| 1487 | break |
| 1488 | } |
| 1489 | |
| 1490 | case "anthropic.computer_use_20241022": { |
| 1491 | betas.add("computer-use-2024-10-22") |
| 1492 | providerTools.push({ |
| 1493 | name: "computer", |
| 1494 | type: "computer_20241022", |
| 1495 | display_height_px: providerTool.args.displayHeightPx, |
| 1496 | display_width_px: providerTool.args.displayWidthPx, |
| 1497 | display_number: providerTool.args.displayNumber ?? null |
| 1498 | }) |
| 1499 | break |
| 1500 | } |
| 1501 | |
| 1502 | case "anthropic.computer_20250124": { |
| 1503 | betas.add("computer-use-2025-01-24") |
| 1504 | providerTools.push({ |
| 1505 | name: "computer", |
| 1506 | type: "computer_20250124", |
| 1507 | display_height_px: providerTool.args.displayHeightPx, |
| 1508 | display_width_px: providerTool.args.displayWidthPx, |
| 1509 | display_number: providerTool.args.displayNumber ?? null |
| 1510 | }) |
| 1511 | break |
| 1512 | } |
| 1513 | |
| 1514 | case "anthropic.computer_20251124": { |
| 1515 | betas.add("computer-use-2025-11-24") |
| 1516 | providerTools.push({ |
| 1517 | name: "computer", |
| 1518 | type: "computer_20251124", |
| 1519 | display_height_px: providerTool.args.displayHeightPx, |
| 1520 | display_width_px: providerTool.args.displayWidthPx, |
| 1521 | display_number: providerTool.args.displayNumber ?? null, |
| 1522 | enable_zoom: providerTool.args.enableZoom ?? false |
| 1523 | }) |
| 1524 | break |
| 1525 | } |
| 1526 | |
| 1527 | case "anthropic.memory_20250818": { |
| 1528 | betas.add("context-management-2025-06-27") |
| 1529 | providerTools.push({ name: "memory", type: "memory_20250818" }) |
| 1530 | break |
| 1531 | } |
| 1532 | |
| 1533 | case "anthropic.text_editor_20241022": { |
| 1534 | betas.add("computer-use-2024-10-22") |
| 1535 | providerTools.push({ name: "str_replace_editor", type: "text_editor_20241022" }) |
| 1536 | break |
| 1537 | } |
| 1538 | |
| 1539 | case "anthropic.text_editor_20250124": { |
| 1540 | betas.add("computer-use-2025-01-24") |
| 1541 | providerTools.push({ name: "str_replace_editor", type: "text_editor_20250124" }) |
| 1542 | break |
| 1543 | } |
| 1544 | |
| 1545 | case "anthropic.text_editor_20250429": { |
| 1546 | betas.add("computer-use-2025-01-24") |
| 1547 | providerTools.push({ name: "str_replace_based_edit_tool", type: "text_editor_20250429" }) |
| 1548 | break |
| 1549 | } |
| 1550 | |
| 1551 | case "anthropic.text_editor_20250728": { |
| 1552 | providerTools.push({ |
| 1553 | name: "str_replace_based_edit_tool", |
| 1554 | type: "text_editor_20250728", |
| 1555 | max_characters: providerTool.args.max_characters ?? null |
| 1556 | }) |
| 1557 | break |
| 1558 | } |
| 1559 | |
| 1560 | case "anthropic.tool_search_tool_bm25_20251119": { |
| 1561 | betas.add("advanced-tool-use-2025-11-20") |
| 1562 | providerTools.push({ name: "tool_search_tool_bm25", type: "tool_search_tool_bm25_20251119" }) |
| 1563 | break |
| 1564 | } |
| 1565 | |
| 1566 | case "anthropic.tool_search_tool_regex_20251119": { |
| 1567 | providerTools.push({ name: "tool_search_tool_regex", type: "tool_search_tool_regex_20251119" }) |
| 1568 | break |
| 1569 | } |
| 1570 | |
| 1571 | case "anthropic.web_search_20250305": { |
| 1572 | providerTools.push({ |
| 1573 | name: "web_search", |
| 1574 | type: "web_search_20250305", |
| 1575 | max_uses: providerTool.args.maxUses ?? null, |
| 1576 | allowed_domains: providerTool.args.allowedDomains ?? null, |
| 1577 | blocked_domains: providerTool.args.blockedDomains ?? null, |
| 1578 | user_location: Predicate.isNotUndefined(providerTool.args.userLocation) |
| 1579 | ? { |
| 1580 | type: providerTool.args.userLocation.type, |
| 1581 | region: providerTool.args.userLocation.region ?? null, |
| 1582 | city: providerTool.args.userLocation.city ?? null, |
| 1583 | country: providerTool.args.userLocation.country ?? null, |
| 1584 | timezone: providerTool.args.userLocation.timezone ?? null |
| 1585 | } |
| 1586 | : null |
| 1587 | }) |
| 1588 | break |
| 1589 | } |
| 1590 | |
| 1591 | case "anthropic.web_fetch_20250910": { |
| 1592 | betas.add("web-fetch-2025-09-10") |
| 1593 | providerTools.push({ |
| 1594 | name: "web_fetch", |
| 1595 | type: "web_fetch_20250910", |
| 1596 | max_uses: providerTool.args.maxUses ?? null, |
| 1597 | allowed_domains: providerTool.args.allowedDomains ?? null, |
| 1598 | blocked_domains: providerTool.args.blockedDomains ?? null, |
| 1599 | citations: providerTool.args.citations ?? null, |
| 1600 | max_content_tokens: providerTool.args.maxContentTokens ?? null |
| 1601 | }) |
| 1602 | break |
| 1603 | } |
| 1604 | |
| 1605 | default: { |
| 1606 | return yield* AiError.make({ |
| 1607 | module: "AnthropicLanguageModel", |
| 1608 | method: "prepareTools", |
| 1609 | reason: new AiError.InvalidUserInputError({ |
| 1610 | description: `Received request to call unknown provider-defined tool '${tool.name}'` |
| 1611 | }) |
| 1612 | }) |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | const tools = [...userTools, ...providerTools] |
| 1619 | let toolChoice: Mutable<typeof Generated.BetaToolChoice.Encoded> | undefined = undefined |
| 1620 | |
| 1621 | if (options.toolChoice === "auto") { |
| 1622 | toolChoice = { type: "auto" } |
| 1623 | } else if (options.toolChoice === "required") { |
| 1624 | toolChoice = { type: "any" } |
| 1625 | } else if ("tool" in options.toolChoice) { |
| 1626 | toolChoice = { type: "tool", name: toolNameMapper.getProviderName(options.toolChoice.tool) } |
| 1627 | } else { |
| 1628 | toolChoice = { type: options.toolChoice.mode === "required" ? "any" : "auto" } |
| 1629 | } |
| 1630 | |
| 1631 | if ( |
| 1632 | Predicate.isNotUndefined(config.disableParallelToolCalls) && |
| 1633 | Predicate.isNotUndefined(toolChoice) && |
| 1634 | toolChoice.type !== "none" |
| 1635 | ) { |
| 1636 | toolChoice.disable_parallel_tool_use = config.disableParallelToolCalls |
| 1637 | } |
| 1638 | |
| 1639 | return { |
| 1640 | tools, |
| 1641 | toolChoice |
| 1642 | } |
| 1643 | } |
| 1644 | ) |
| 1645 | |
| 1646 | // ============================================================================= |
| 1647 | // HTTP Details |
| 1648 | // ============================================================================= |
| 1649 | |
| 1650 | const buildHttpResponseDetails = ( |
| 1651 | response: HttpClientResponse.HttpClientResponse |
| 1652 | ): typeof Response.HttpResponseDetails.Type => ({ |
| 1653 | status: response.status, |
| 1654 | headers: Redactable.redact(response.headers) as Record<string, string> |
| 1655 | }) |
| 1656 | |
| 1657 | // ============================================================================= |
| 1658 | // Response Conversion |
| 1659 | // ============================================================================= |
| 1660 | |
| 1661 | const makeResponse = Effect.fnUntraced( |
| 1662 | function*<Tools extends ReadonlyArray<Tool.Any>>({ |
| 1663 | options, |
| 1664 | rawResponse, |
| 1665 | response, |
| 1666 | toolNameMapper |
| 1667 | }: { |
| 1668 | readonly options: LanguageModel.ProviderOptions |
| 1669 | readonly rawResponse: Generated.BetaMessage |
| 1670 | readonly response: HttpClientResponse.HttpClientResponse |
| 1671 | readonly toolNameMapper: Tool.NameMapper<Tools> |
| 1672 | }): Effect.fn.Return< |
| 1673 | Array<Response.PartEncoded>, |
| 1674 | AiError.AiError, |
| 1675 | IdGenerator.IdGenerator |
| 1676 | > { |
| 1677 | const parts: Array<Response.PartEncoded> = [] |
| 1678 | const mcpToolCalls: Map<string, Response.ToolCallPartEncoded> = new Map() |
| 1679 | const serverToolCalls: Map<string, string> = new Map() |
| 1680 | const citableDocuments = extractCitableDocuments(options.prompt) |
| 1681 | const responseFormat = options.responseFormat |
| 1682 | const hasStructuredOutputTool = responseFormat.type === "json" && |
| 1683 | rawResponse.content.some((part) => part.type === "tool_use" && part.name === responseFormat.objectName) |
| 1684 | |
| 1685 | parts.push({ |
| 1686 | type: "response-metadata", |
| 1687 | id: rawResponse.id, |
| 1688 | modelId: rawResponse.model, |
| 1689 | timestamp: DateTime.formatIso(yield* DateTime.now) |
| 1690 | }) |
| 1691 | |
| 1692 | for (const part of rawResponse.content) { |
| 1693 | switch (part.type) { |
| 1694 | case "fallback": { |
| 1695 | parts.push({ |
| 1696 | type: "response-metadata", |
| 1697 | modelId: part.to.model, |
| 1698 | timestamp: DateTime.formatIso(yield* DateTime.now), |
| 1699 | metadata: { anthropic: { fallback: part } } |
| 1700 | }) |
| 1701 | break |
| 1702 | } |
| 1703 | case "text": { |
| 1704 | // The response tool supplies the JSON payload. Accompanying prose |
| 1705 | // must not be concatenated with it during structured output decoding. |
| 1706 | if (hasStructuredOutputTool) { |
| 1707 | break |
| 1708 | } |
| 1709 | |
| 1710 | parts.push({ |
| 1711 | type: "text", |
| 1712 | text: part.text |
| 1713 | }) |
| 1714 | |
| 1715 | if (Predicate.isNotNullish(part.citations)) { |
| 1716 | for (const citation of part.citations) { |
| 1717 | const source = yield* processCitation(citation, citableDocuments) |
| 1718 | if (Predicate.isNotUndefined(source)) { |
| 1719 | parts.push(source) |
| 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | break |
| 1725 | } |
| 1726 | |
| 1727 | case "thinking": { |
| 1728 | const metadata = { |
| 1729 | info: { type: "thinking", signature: part.signature } |
| 1730 | } as const |
| 1731 | |
| 1732 | parts.push({ |
| 1733 | type: "reasoning", |
| 1734 | text: part.thinking, |
| 1735 | metadata: { anthropic: metadata } |
| 1736 | }) |
| 1737 | break |
| 1738 | } |
| 1739 | |
| 1740 | case "redacted_thinking": { |
| 1741 | const metadata = { |
| 1742 | info: { type: "redacted_thinking", redactedData: part.data } |
| 1743 | } as const |
| 1744 | |
| 1745 | parts.push({ |
| 1746 | type: "reasoning", |
| 1747 | text: "", |
| 1748 | metadata: { anthropic: metadata } |
| 1749 | }) |
| 1750 | break |
| 1751 | } |
| 1752 | |
| 1753 | case "tool_use": { |
| 1754 | // When the `"json"` response format is requested, the JSON we need |
| 1755 | // is returned by a tool call injected into the request |
| 1756 | if (responseFormat.type === "json" && part.name === responseFormat.objectName) { |
| 1757 | parts.push({ |
| 1758 | type: "text", |
| 1759 | text: JSON.stringify(part.input) |
| 1760 | }) |
| 1761 | } else { |
| 1762 | // Extract caller info if present |
| 1763 | const caller = (part as any).caller |
| 1764 | const callerInfo = Predicate.isNotNullish(caller) |
| 1765 | ? { |
| 1766 | type: caller.type, |
| 1767 | toolId: "tool_id" in caller ? caller.tool_id : null |
| 1768 | } |
| 1769 | : undefined |
| 1770 | |
| 1771 | // Map the provider wire name (e.g. "memory") back to the tool's |
| 1772 | // custom name (e.g. "AnthropicMemory") that the toolkit is keyed by |
| 1773 | const toolName = toolNameMapper.getCustomName(part.name) |
| 1774 | const params = yield* transformToolCallParams(options, toolName, part.input) |
| 1775 | |
| 1776 | parts.push({ |
| 1777 | type: "tool-call", |
| 1778 | id: part.id, |
| 1779 | name: toolName, |
| 1780 | params, |
| 1781 | ...(Predicate.isNotUndefined(callerInfo) |
| 1782 | ? { metadata: { anthropic: { caller: callerInfo } } } |
| 1783 | : undefined) |
| 1784 | }) |
| 1785 | } |
| 1786 | |
| 1787 | break |
| 1788 | } |
| 1789 | |
| 1790 | case "server_tool_use": { |
| 1791 | const toolName = toolNameMapper.getCustomName(part.name) |
| 1792 | |
| 1793 | if ( |
| 1794 | part.name === "bash_code_execution" || |
| 1795 | part.name === "text_editor_code_execution" |
| 1796 | ) { |
| 1797 | parts.push({ |
| 1798 | type: "tool-call", |
| 1799 | id: part.id, |
| 1800 | name: toolName, |
| 1801 | params: { type: part.name, ...part.input }, |
| 1802 | providerExecuted: true |
| 1803 | }) |
| 1804 | } else if ( |
| 1805 | part.name === "code_execution" || |
| 1806 | part.name === "web_fetch" || |
| 1807 | part.name === "web_search" |
| 1808 | ) { |
| 1809 | const toolParams: Record<string, unknown> = { ...part.input } |
| 1810 | |
| 1811 | // Inject `type: "programmatic-tool-call"` when the input parameters |
| 1812 | // has the format `{ code: ... }` |
| 1813 | if ( |
| 1814 | part.name === "code_execution" && |
| 1815 | Predicate.hasProperty(part.input, "code") && |
| 1816 | !Predicate.hasProperty(part.input, "type") |
| 1817 | ) { |
| 1818 | toolParams.type = "programmatic-tool-call" |
| 1819 | } |
| 1820 | |
| 1821 | parts.push({ |
| 1822 | type: "tool-call", |
| 1823 | id: part.id, |
| 1824 | name: toolName, |
| 1825 | params: toolParams, |
| 1826 | providerExecuted: true |
| 1827 | }) |
| 1828 | } else if ( |
| 1829 | part.name === "tool_search_tool_bm25" || |
| 1830 | part.name === "tool_search_tool_regex" |
| 1831 | ) { |
| 1832 | serverToolCalls.set(part.id, part.name) |
| 1833 | parts.push({ |
| 1834 | type: "tool-call", |
| 1835 | id: part.id, |
| 1836 | name: toolName, |
| 1837 | params: part.input, |
| 1838 | providerExecuted: true |
| 1839 | }) |
| 1840 | } |
| 1841 | |
| 1842 | break |
| 1843 | } |
| 1844 | |
| 1845 | case "mcp_tool_use": { |
| 1846 | const toolCall: Response.ToolCallPartEncoded = { |
| 1847 | type: "tool-call", |
| 1848 | id: part.id, |
| 1849 | name: part.name, |
| 1850 | params: part.input, |
| 1851 | providerExecuted: true, |
| 1852 | metadata: { anthropic: { mcp_tool: { server: part.server_name } } } |
| 1853 | } |
| 1854 | |
| 1855 | mcpToolCalls.set(part.id, toolCall) |
| 1856 | |
| 1857 | parts.push(toolCall) |
| 1858 | |
| 1859 | break |
| 1860 | } |
| 1861 | |
| 1862 | case "mcp_tool_result": { |
| 1863 | const toolCall = mcpToolCalls.get(part.tool_use_id) |
| 1864 | const mcpMetadata = toolCall?.metadata?.anthropic?.mcp_tool |
| 1865 | |
| 1866 | if (Predicate.isNotUndefined(toolCall)) { |
| 1867 | parts.push({ |
| 1868 | type: "tool-result", |
| 1869 | id: part.tool_use_id, |
| 1870 | name: toolCall.name, |
| 1871 | isFailure: part.is_error, |
| 1872 | result: part.content, |
| 1873 | metadata: { |
| 1874 | anthropic: { |
| 1875 | ...(Predicate.isNotNullish(mcpMetadata) |
| 1876 | ? { mcp_tool: mcpMetadata } : |
| 1877 | undefined) |
| 1878 | } |
| 1879 | } |
| 1880 | }) |
| 1881 | } |
| 1882 | |
| 1883 | break |
| 1884 | } |
| 1885 | |
| 1886 | // Code Execution 20250522 |
| 1887 | case "code_execution_tool_result": { |
| 1888 | const toolName = toolNameMapper.getCustomName("code_execution") |
| 1889 | |
| 1890 | if (part.content.type === "code_execution_result") { |
| 1891 | parts.push({ |
| 1892 | type: "tool-result", |
| 1893 | id: part.tool_use_id, |
| 1894 | name: toolName, |
| 1895 | isFailure: false, |
| 1896 | result: part.content |
| 1897 | }) |
| 1898 | } else { |
| 1899 | parts.push({ |
| 1900 | type: "tool-result", |
| 1901 | id: part.tool_use_id, |
| 1902 | name: toolName, |
| 1903 | isFailure: true, |
| 1904 | result: part.content |
| 1905 | }) |
| 1906 | } |
| 1907 | |
| 1908 | break |
| 1909 | } |
| 1910 | |
| 1911 | // Code Execution 20250825 |
| 1912 | case "bash_code_execution_tool_result": |
| 1913 | case "text_editor_code_execution_tool_result": { |
| 1914 | const toolName = toolNameMapper.getCustomName("code_execution") |
| 1915 | |
| 1916 | if ( |
| 1917 | part.content.type === "bash_code_execution_tool_result_error" || |
| 1918 | part.content.type === "text_editor_code_execution_tool_result_error" |
| 1919 | ) { |
| 1920 | parts.push({ |
| 1921 | type: "tool-result", |
| 1922 | id: part.tool_use_id, |
| 1923 | name: toolName, |
| 1924 | isFailure: true, |
| 1925 | result: part.content |
| 1926 | }) |
| 1927 | } else { |
| 1928 | parts.push({ |
| 1929 | type: "tool-result", |
| 1930 | id: part.tool_use_id, |
| 1931 | name: toolName, |
| 1932 | isFailure: false, |
| 1933 | result: part.content |
| 1934 | }) |
| 1935 | } |
| 1936 | |
| 1937 | break |
| 1938 | } |
| 1939 | |
| 1940 | case "tool_search_tool_result": { |
| 1941 | let providerName = serverToolCalls.get(part.tool_use_id) |
| 1942 | |
| 1943 | if (Predicate.isUndefined(providerName)) { |
| 1944 | const bm25Name = toolNameMapper.getCustomName("tool_search_tool_bm25") |
| 1945 | const regexName = toolNameMapper.getCustomName("tool_search_tool_regex") |
| 1946 | |
| 1947 | if (bm25Name !== "tool_search_tool_bm25") { |
| 1948 | providerName = "tool_search_tool_bm25" |
| 1949 | } else if (regexName !== "tool_search_tool_regex") { |
| 1950 | providerName = "tool_search_tool_regex" |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | const toolName = toolNameMapper.getCustomName(providerName!) |
| 1955 | |
| 1956 | if (part.content.type === "tool_search_tool_search_result") { |
| 1957 | parts.push({ |
| 1958 | type: "tool-result", |
| 1959 | id: part.tool_use_id, |
| 1960 | name: toolName, |
| 1961 | isFailure: false, |
| 1962 | result: part.content.tool_references, |
| 1963 | providerExecuted: true |
| 1964 | }) |
| 1965 | } else { |
| 1966 | parts.push({ |
| 1967 | type: "tool-result", |
| 1968 | id: part.tool_use_id, |
| 1969 | name: toolName, |
| 1970 | isFailure: true, |
| 1971 | result: part.content, |
| 1972 | providerExecuted: true |
| 1973 | }) |
| 1974 | } |
| 1975 | |
| 1976 | break |
| 1977 | } |
| 1978 | |
| 1979 | case "web_fetch_tool_result": { |
| 1980 | const toolName = toolNameMapper.getCustomName("web_fetch") |
| 1981 | |
| 1982 | if (part.content.type === "web_fetch_result") { |
| 1983 | citableDocuments.push({ |
| 1984 | title: part.content.content.title ?? part.content.url, |
| 1985 | mediaType: part.content.content.source.media_type |
| 1986 | }) |
| 1987 | |
| 1988 | parts.push({ |
| 1989 | type: "tool-result", |
| 1990 | id: part.tool_use_id, |
| 1991 | name: toolName, |
| 1992 | isFailure: false, |
| 1993 | result: part.content |
| 1994 | }) |
| 1995 | } else { |
| 1996 | parts.push({ |
| 1997 | type: "tool-result", |
| 1998 | id: part.tool_use_id, |
| 1999 | name: toolName, |
| 2000 | isFailure: true, |
| 2001 | result: part.content |
| 2002 | }) |
| 2003 | } |
| 2004 | |
| 2005 | break |
| 2006 | } |
| 2007 | |
| 2008 | case "web_search_tool_result": { |
| 2009 | const toolName = toolNameMapper.getCustomName("web_search") |
| 2010 | |
| 2011 | if (Predicate.hasProperty(part.content, "type")) { |
| 2012 | parts.push({ |
| 2013 | type: "tool-result", |
| 2014 | id: part.tool_use_id, |
| 2015 | name: toolName, |
| 2016 | isFailure: true, |
| 2017 | result: part.content, |
| 2018 | providerExecuted: true |
| 2019 | }) |
| 2020 | } else { |
| 2021 | const idGenerator = yield* IdGenerator.IdGenerator |
| 2022 | |
| 2023 | parts.push({ |
| 2024 | type: "tool-result", |
| 2025 | id: part.tool_use_id, |
| 2026 | name: toolName, |
| 2027 | isFailure: false, |
| 2028 | result: part.content, |
| 2029 | providerExecuted: true |
| 2030 | }) |
| 2031 | |
| 2032 | const content = part.content as ReadonlyArray<Generated.BetaResponseWebSearchResultBlock> |
| 2033 | |
| 2034 | for (const result of content) { |
| 2035 | const id = yield* idGenerator.generateId() |
| 2036 | |
| 2037 | parts.push({ |
| 2038 | type: "source", |
| 2039 | sourceType: "url", |
| 2040 | id, |
| 2041 | url: result.url, |
| 2042 | title: result.title, |
| 2043 | metadata: { anthropic: { source: "web", pageAge: result.page_age } } |
| 2044 | }) |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | break |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | // Anthropic always returns a non-null `stop_reason` for non-streaming responses |
| 2054 | const finishReason = InternalUtilities.resolveFinishReason( |
| 2055 | rawResponse.stop_reason!, |
| 2056 | options.responseFormat.type === "json" |
| 2057 | ) |
| 2058 | |
| 2059 | const inputTokens = rawResponse.usage.input_tokens |
| 2060 | const outputTokens = rawResponse.usage.output_tokens |
| 2061 | const cacheWriteTokens = rawResponse.usage.cache_creation_input_tokens ?? 0 |
| 2062 | const cacheReadTokens = rawResponse.usage.cache_read_input_tokens ?? 0 |
| 2063 | |
| 2064 | parts.push({ |
| 2065 | type: "finish", |
| 2066 | reason: finishReason, |
| 2067 | usage: { |
| 2068 | inputTokens: { |
| 2069 | uncached: inputTokens, |
| 2070 | total: inputTokens + cacheWriteTokens + cacheReadTokens, |
| 2071 | cacheRead: cacheReadTokens, |
| 2072 | cacheWrite: cacheWriteTokens |
| 2073 | }, |
| 2074 | outputTokens: { |
| 2075 | total: outputTokens, |
| 2076 | text: undefined, |
| 2077 | reasoning: undefined |
| 2078 | } |
| 2079 | }, |
| 2080 | response: buildHttpResponseDetails(response), |
| 2081 | metadata: { |
| 2082 | anthropic: { |
| 2083 | container: rawResponse.container ?? null, |
| 2084 | contextManagement: rawResponse.context_management ?? null, |
| 2085 | usage: rawResponse.usage, |
| 2086 | stopSequence: rawResponse.stop_sequence |
| 2087 | } |
| 2088 | } |
| 2089 | }) |
| 2090 | |
| 2091 | return parts |
| 2092 | } |
| 2093 | ) |
| 2094 | |
| 2095 | const makeStreamResponse = Effect.fnUntraced( |
| 2096 | function*<Tools extends ReadonlyArray<Tool.Any>>({ |
| 2097 | stream, |
| 2098 | response, |
| 2099 | options, |
| 2100 | toolNameMapper |
| 2101 | }: { |
| 2102 | readonly stream: Stream.Stream<MessageStreamEvent, AiError.AiError> |
| 2103 | readonly response: HttpClientResponse.HttpClientResponse |
| 2104 | readonly options: LanguageModel.ProviderOptions |
| 2105 | readonly toolNameMapper: Tool.NameMapper<Tools> |
| 2106 | }): Effect.fn.Return< |
| 2107 | Stream.Stream<Response.StreamPartEncoded, AiError.AiError, IdGenerator.IdGenerator>, |
| 2108 | AiError.AiError |
| 2109 | > { |
| 2110 | const citableDocuments = extractCitableDocuments(options.prompt) |
| 2111 | |
| 2112 | let container: typeof Generated.BetaContainer.Encoded | null = null |
| 2113 | let contextManagement: typeof Generated.BetaResponseContextManagement.Encoded | null = null |
| 2114 | let finishReason: Response.FinishReason = "unknown" |
| 2115 | let stopSequence: string | null = null |
| 2116 | let rawUsage: typeof Generated.BetaMessage.Encoded["usage"] | null = null |
| 2117 | const mcpToolCalls: Map<string, Response.ToolCallPartEncoded> = new Map() |
| 2118 | const serverToolCalls: Map<string, string> = new Map() |
| 2119 | const contentBlocks: Map< |
| 2120 | number, |
| 2121 | | { |
| 2122 | readonly type: "text" |
| 2123 | } |
| 2124 | | { |
| 2125 | readonly type: "reasoning" |
| 2126 | } |
| 2127 | | { |
| 2128 | readonly type: "tool-call" |
| 2129 | readonly id: string |
| 2130 | readonly name: string |
| 2131 | params: string |
| 2132 | firstDelta: boolean |
| 2133 | readonly providerName?: string | undefined |
| 2134 | readonly providerExecuted?: boolean | undefined |
| 2135 | readonly caller?: { type: string; toolId: string | null } | undefined |
| 2136 | } |
| 2137 | > = new Map() |
| 2138 | const usage: Mutable<{ |
| 2139 | inputTokens: number |
| 2140 | outputTokens: number |
| 2141 | cacheReadInputTokens: number |
| 2142 | cacheWriteInputTokens: number |
| 2143 | }> = { |
| 2144 | inputTokens: 0, |
| 2145 | outputTokens: 0, |
| 2146 | cacheReadInputTokens: 0, |
| 2147 | cacheWriteInputTokens: 0 |
| 2148 | } |
| 2149 | |
| 2150 | let blockType: typeof Generated.BetaContentBlockStartEvent.Encoded["content_block"]["type"] | undefined = undefined |
| 2151 | |
| 2152 | return stream.pipe( |
| 2153 | Stream.mapEffect(Effect.fnUntraced(function*(event) { |
| 2154 | const parts: Array<Response.StreamPartEncoded> = [] |
| 2155 | |
| 2156 | switch (event.type) { |
| 2157 | case "message_start": { |
| 2158 | rawUsage = { ...event.message.usage } |
| 2159 | usage.inputTokens = event.message.usage.input_tokens |
| 2160 | usage.cacheReadInputTokens = event.message.usage.cache_read_input_tokens ?? 0 |
| 2161 | usage.cacheWriteInputTokens = event.message.usage.cache_creation_input_tokens ?? 0 |
| 2162 | |
| 2163 | if (Predicate.isNotNullish(event.message.container)) { |
| 2164 | container = event.message.container |
| 2165 | } |
| 2166 | |
| 2167 | if (Predicate.isNotNull(event.message.stop_sequence)) { |
| 2168 | stopSequence = event.message.stop_sequence |
| 2169 | } |
| 2170 | |
| 2171 | if (Predicate.isNotNull(event.message.stop_reason)) { |
| 2172 | finishReason = InternalUtilities.resolveFinishReason(event.message.stop_reason) |
| 2173 | } |
| 2174 | |
| 2175 | parts.push({ |
| 2176 | type: "response-metadata", |
| 2177 | id: event.message.id, |
| 2178 | modelId: event.message.model, |
| 2179 | timestamp: DateTime.formatIso(yield* DateTime.now) |
| 2180 | }) |
| 2181 | |
| 2182 | // Process pre-populated content blocks |
| 2183 | if (Predicate.isNotNullish(event.message.content) && event.message.content.length > 0) { |
| 2184 | for (let i = 0; i < event.message.content.length; i++) { |
| 2185 | const part = event.message.content[i] |
| 2186 | |
| 2187 | if (part.type === "tool_use") { |
| 2188 | const callerInfo = Predicate.isNotUndefined(part.caller) |
| 2189 | ? { |
| 2190 | type: part.caller.type, |
| 2191 | toolId: "tool_id" in part.caller ? part.caller.tool_id : null |
| 2192 | } |
| 2193 | : undefined |
| 2194 | |
| 2195 | // Map the provider wire name (e.g. "memory") back to the |
| 2196 | // tool's custom name (e.g. "AnthropicMemory") that the toolkit |
| 2197 | // is keyed by |
| 2198 | const toolName = toolNameMapper.getCustomName(part.name) |
| 2199 | |
| 2200 | parts.push({ |
| 2201 | type: "tool-params-start", |
| 2202 | id: part.id, |
| 2203 | name: toolName |
| 2204 | }) |
| 2205 | |
| 2206 | parts.push({ |
| 2207 | type: "tool-params-delta", |
| 2208 | id: part.id, |
| 2209 | delta: JSON.stringify(part.input ?? {}) |
| 2210 | }) |
| 2211 | |
| 2212 | parts.push({ |
| 2213 | type: "tool-params-end", |
| 2214 | id: part.id |
| 2215 | }) |
| 2216 | |
| 2217 | const params = yield* transformToolCallParams(options, toolName, part.input) |
| 2218 | |
| 2219 | parts.push({ |
| 2220 | type: "tool-call", |
| 2221 | id: part.id, |
| 2222 | name: toolName, |
| 2223 | params, |
| 2224 | ...(Predicate.isNotUndefined(callerInfo) |
| 2225 | ? { metadata: { anthropic: { caller: callerInfo } } } |
| 2226 | : undefined) |
| 2227 | }) |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | break |
| 2233 | } |
| 2234 | |
| 2235 | case "message_delta": { |
| 2236 | rawUsage = { ...rawUsage, ...event.usage } as any |
| 2237 | |
| 2238 | if ( |
| 2239 | Predicate.isNotNullish(event.usage.input_tokens) && |
| 2240 | usage.inputTokens !== event.usage.input_tokens |
| 2241 | ) { |
| 2242 | usage.inputTokens = event.usage.input_tokens |
| 2243 | } |
| 2244 | usage.outputTokens = event.usage.output_tokens |
| 2245 | |
| 2246 | if ( |
| 2247 | Predicate.isNotNullish(event.usage.cache_read_input_tokens) && |
| 2248 | usage.cacheReadInputTokens !== event.usage.cache_read_input_tokens |
| 2249 | ) { |
| 2250 | usage.cacheReadInputTokens = event.usage.cache_read_input_tokens |
| 2251 | } |
| 2252 | if ( |
| 2253 | Predicate.isNotNullish(event.usage.cache_creation_input_tokens) && |
| 2254 | usage.cacheWriteInputTokens !== event.usage.cache_creation_input_tokens |
| 2255 | ) { |
| 2256 | usage.cacheWriteInputTokens = event.usage.cache_creation_input_tokens |
| 2257 | } |
| 2258 | |
| 2259 | if (Predicate.isNotNullish(event.delta.container)) { |
| 2260 | container = event.delta.container |
| 2261 | } |
| 2262 | |
| 2263 | if (Predicate.isNotNullish(event.context_management)) { |
| 2264 | contextManagement = event.context_management |
| 2265 | } |
| 2266 | |
| 2267 | if (Predicate.isNotNull(event.delta.stop_reason)) { |
| 2268 | finishReason = InternalUtilities.resolveFinishReason(event.delta.stop_reason) |
| 2269 | } |
| 2270 | |
| 2271 | if (Predicate.isNotNull(event.delta.stop_sequence)) { |
| 2272 | stopSequence = event.delta.stop_sequence |
| 2273 | } |
| 2274 | |
| 2275 | break |
| 2276 | } |
| 2277 | |
| 2278 | case "message_stop": { |
| 2279 | const metadata: Response.FinishPartMetadata = { |
| 2280 | anthropic: { |
| 2281 | container, |
| 2282 | contextManagement, |
| 2283 | stopSequence, |
| 2284 | usage: rawUsage |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | parts.push({ |
| 2289 | type: "finish", |
| 2290 | reason: finishReason, |
| 2291 | usage: { |
| 2292 | inputTokens: { |
| 2293 | uncached: usage.inputTokens, |
| 2294 | total: usage.inputTokens + usage.cacheWriteInputTokens + usage.cacheReadInputTokens, |
| 2295 | cacheRead: usage.cacheReadInputTokens, |
| 2296 | cacheWrite: usage.cacheWriteInputTokens |
| 2297 | }, |
| 2298 | outputTokens: { |
| 2299 | total: usage.outputTokens, |
| 2300 | text: undefined, |
| 2301 | reasoning: undefined |
| 2302 | } |
| 2303 | }, |
| 2304 | response: buildHttpResponseDetails(response), |
| 2305 | metadata |
| 2306 | }) |
| 2307 | |
| 2308 | break |
| 2309 | } |
| 2310 | |
| 2311 | case "content_block_start": { |
| 2312 | blockType = event.content_block.type |
| 2313 | |
| 2314 | switch (event.content_block.type) { |
| 2315 | case "text": { |
| 2316 | contentBlocks.set(event.index, { type: "text" }) |
| 2317 | |
| 2318 | parts.push({ |
| 2319 | type: "text-start", |
| 2320 | id: event.index.toString() |
| 2321 | }) |
| 2322 | |
| 2323 | break |
| 2324 | } |
| 2325 | |
| 2326 | case "thinking": { |
| 2327 | contentBlocks.set(event.index, { type: "reasoning" }) |
| 2328 | |
| 2329 | parts.push({ |
| 2330 | type: "reasoning-start", |
| 2331 | id: event.index.toString() |
| 2332 | }) |
| 2333 | |
| 2334 | break |
| 2335 | } |
| 2336 | |
| 2337 | case "redacted_thinking": { |
| 2338 | contentBlocks.set(event.index, { type: "reasoning" }) |
| 2339 | |
| 2340 | const metadata: Response.ReasoningStartPartMetadata = { |
| 2341 | anthropic: { |
| 2342 | info: { |
| 2343 | type: "redacted_thinking", |
| 2344 | redactedData: event.content_block.data |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | parts.push({ |
| 2350 | type: "reasoning-start", |
| 2351 | id: event.index.toString(), |
| 2352 | metadata |
| 2353 | }) |
| 2354 | |
| 2355 | break |
| 2356 | } |
| 2357 | |
| 2358 | case "tool_use": { |
| 2359 | const part = event.content_block |
| 2360 | |
| 2361 | const caller = Predicate.isNotUndefined(part.caller) |
| 2362 | ? { |
| 2363 | type: part.caller.type, |
| 2364 | toolId: "tool_id" in part.caller ? part.caller.tool_id : null |
| 2365 | } |
| 2366 | : undefined |
| 2367 | |
| 2368 | const hasParams = Object.keys(part.input).length > 0 |
| 2369 | const initialParams = hasParams ? JSON.stringify(part.input) : "" |
| 2370 | // Map the provider wire name (e.g. "memory") back to the tool's |
| 2371 | // custom name (e.g. "AnthropicMemory") that the toolkit is keyed |
| 2372 | // by. The mapped name flows to the finalized tool-call via |
| 2373 | // `contentBlock.name` on `content_block_stop`. |
| 2374 | const toolName = toolNameMapper.getCustomName(part.name) |
| 2375 | contentBlocks.set(event.index, { |
| 2376 | type: "tool-call", |
| 2377 | id: part.id, |
| 2378 | name: toolName, |
| 2379 | params: initialParams, |
| 2380 | firstDelta: initialParams.length > 0, |
| 2381 | ...(Predicate.isNotUndefined(caller) ? { caller } : undefined) |
| 2382 | }) |
| 2383 | |
| 2384 | parts.push({ |
| 2385 | type: "tool-params-start", |
| 2386 | id: part.id, |
| 2387 | name: toolName |
| 2388 | }) |
| 2389 | |
| 2390 | break |
| 2391 | } |
| 2392 | |
| 2393 | case "server_tool_use": { |
| 2394 | const part = event.content_block |
| 2395 | |
| 2396 | if ( |
| 2397 | part.name === "code_execution" || |
| 2398 | part.name === "bash_code_execution" || |
| 2399 | part.name === "text_editor_code_execution" || |
| 2400 | part.name === "web_fetch" || |
| 2401 | part.name === "web_search" |
| 2402 | ) { |
| 2403 | const toolName = toolNameMapper.getCustomName( |
| 2404 | part.name === "bash_code_execution" || part.name === "text_editor_code_execution" |
| 2405 | ? "code_execution" |
| 2406 | : part.name |
| 2407 | ) |
| 2408 | |
| 2409 | contentBlocks.set(event.index, { |
| 2410 | type: "tool-call", |
| 2411 | id: part.id, |
| 2412 | name: toolName, |
| 2413 | params: "", |
| 2414 | firstDelta: true, |
| 2415 | providerName: part.name, |
| 2416 | providerExecuted: true |
| 2417 | }) |
| 2418 | |
| 2419 | parts.push({ |
| 2420 | type: "tool-params-start", |
| 2421 | id: part.id, |
| 2422 | name: toolName, |
| 2423 | providerExecuted: true |
| 2424 | }) |
| 2425 | } else if ( |
| 2426 | part.name === "tool_search_tool_bm25" || |
| 2427 | part.name === "tool_search_tool_regex" |
| 2428 | ) { |
| 2429 | serverToolCalls.set(part.id, part.name) |
| 2430 | |
| 2431 | const toolName = toolNameMapper.getCustomName(part.name) |
| 2432 | |
| 2433 | contentBlocks.set(event.index, { |
| 2434 | type: "tool-call", |
| 2435 | id: part.id, |
| 2436 | name: toolName, |
| 2437 | params: "", |
| 2438 | firstDelta: true, |
| 2439 | providerName: part.name, |
| 2440 | providerExecuted: true |
| 2441 | }) |
| 2442 | |
| 2443 | parts.push({ |
| 2444 | type: "tool-params-start", |
| 2445 | id: part.id, |
| 2446 | name: toolName, |
| 2447 | providerExecuted: true |
| 2448 | }) |
| 2449 | } |
| 2450 | |
| 2451 | break |
| 2452 | } |
| 2453 | |
| 2454 | case "web_fetch_tool_result": { |
| 2455 | const part = event.content_block |
| 2456 | const toolName = toolNameMapper.getCustomName("web_fetch") |
| 2457 | |
| 2458 | if (part.content.type === "web_fetch_result") { |
| 2459 | citableDocuments.push({ |
| 2460 | title: part.content.content.title ?? part.content.url, |
| 2461 | mediaType: part.content.content.source.media_type |
| 2462 | }) |
| 2463 | |
| 2464 | parts.push({ |
| 2465 | type: "tool-result", |
| 2466 | id: part.tool_use_id, |
| 2467 | name: toolName, |
| 2468 | isFailure: false, |
| 2469 | result: part.content |
| 2470 | }) |
| 2471 | } else { |
| 2472 | parts.push({ |
| 2473 | type: "tool-result", |
| 2474 | id: part.tool_use_id, |
| 2475 | name: toolName, |
| 2476 | isFailure: true, |
| 2477 | result: part.content |
| 2478 | }) |
| 2479 | } |
| 2480 | |
| 2481 | break |
| 2482 | } |
| 2483 | |
| 2484 | case "web_search_tool_result": { |
| 2485 | const part = event.content_block |
| 2486 | const toolName = toolNameMapper.getCustomName("web_search") |
| 2487 | |
| 2488 | if (Predicate.hasProperty(part.content, "type")) { |
| 2489 | parts.push({ |
| 2490 | type: "tool-result", |
| 2491 | id: part.tool_use_id, |
| 2492 | name: toolName, |
| 2493 | isFailure: true, |
| 2494 | result: part.content, |
| 2495 | providerExecuted: true |
| 2496 | }) |
| 2497 | } else { |
| 2498 | const idGenerator = yield* IdGenerator.IdGenerator |
| 2499 | |
| 2500 | parts.push({ |
| 2501 | type: "tool-result", |
| 2502 | id: part.tool_use_id, |
| 2503 | name: toolName, |
| 2504 | isFailure: false, |
| 2505 | result: part.content, |
| 2506 | providerExecuted: true |
| 2507 | }) |
| 2508 | |
| 2509 | const content = part.content as ReadonlyArray<Generated.BetaResponseWebSearchResultBlock> |
| 2510 | |
| 2511 | for (const result of content) { |
| 2512 | const id = yield* idGenerator.generateId() |
| 2513 | |
| 2514 | parts.push({ |
| 2515 | type: "source", |
| 2516 | sourceType: "url", |
| 2517 | id, |
| 2518 | url: result.url, |
| 2519 | title: result.title, |
| 2520 | metadata: { anthropic: { source: "web", pageAge: result.page_age } } |
| 2521 | }) |
| 2522 | } |
| 2523 | } |
| 2524 | break |
| 2525 | } |
| 2526 | |
| 2527 | case "code_execution_tool_result": { |
| 2528 | const part = event.content_block |
| 2529 | const toolName = toolNameMapper.getCustomName("code_execution") |
| 2530 | |
| 2531 | if (part.content.type === "code_execution_result") { |
| 2532 | parts.push({ |
| 2533 | type: "tool-result", |
| 2534 | id: part.tool_use_id, |
| 2535 | name: toolName, |
| 2536 | isFailure: false, |
| 2537 | result: part.content |
| 2538 | }) |
| 2539 | } else { |
| 2540 | parts.push({ |
| 2541 | type: "tool-result", |
| 2542 | id: part.tool_use_id, |
| 2543 | name: toolName, |
| 2544 | isFailure: true, |
| 2545 | result: part.content |
| 2546 | }) |
| 2547 | } |
| 2548 | break |
| 2549 | } |
| 2550 | |
| 2551 | case "bash_code_execution_tool_result": |
| 2552 | case "text_editor_code_execution_tool_result": { |
| 2553 | const part = event.content_block |
| 2554 | const toolName = toolNameMapper.getCustomName("code_execution") |
| 2555 | |
| 2556 | if ( |
| 2557 | part.content.type === "bash_code_execution_tool_result_error" || |
| 2558 | part.content.type === "text_editor_code_execution_tool_result_error" |
| 2559 | ) { |
| 2560 | parts.push({ |
| 2561 | type: "tool-result", |
| 2562 | id: part.tool_use_id, |
| 2563 | name: toolName, |
| 2564 | isFailure: true, |
| 2565 | result: part.content |
| 2566 | }) |
| 2567 | } else { |
| 2568 | parts.push({ |
| 2569 | type: "tool-result", |
| 2570 | id: part.tool_use_id, |
| 2571 | name: toolName, |
| 2572 | isFailure: false, |
| 2573 | result: part.content |
| 2574 | }) |
| 2575 | } |
| 2576 | break |
| 2577 | } |
| 2578 | |
| 2579 | case "tool_search_tool_result": { |
| 2580 | const part = event.content_block |
| 2581 | let providerName = serverToolCalls.get(part.tool_use_id) |
| 2582 | |
| 2583 | if (Predicate.isUndefined(providerName)) { |
| 2584 | const bm25Name = toolNameMapper.getCustomName("tool_search_tool_bm25") |
| 2585 | const regexName = toolNameMapper.getCustomName("tool_search_tool_regex") |
| 2586 | |
| 2587 | if (bm25Name !== "tool_search_tool_bm25") { |
| 2588 | providerName = "tool_search_tool_bm25" |
| 2589 | } else if (regexName !== "tool_search_tool_regex") { |
| 2590 | providerName = "tool_search_tool_regex" |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | const toolName = toolNameMapper.getCustomName(providerName!) |
| 2595 | |
| 2596 | if (part.content.type === "tool_search_tool_search_result") { |
| 2597 | parts.push({ |
| 2598 | type: "tool-result", |
| 2599 | id: part.tool_use_id, |
| 2600 | name: toolName, |
| 2601 | isFailure: false, |
| 2602 | result: part.content.tool_references, |
| 2603 | providerExecuted: true |
| 2604 | }) |
| 2605 | } else { |
| 2606 | parts.push({ |
| 2607 | type: "tool-result", |
| 2608 | id: part.tool_use_id, |
| 2609 | name: toolName, |
| 2610 | isFailure: true, |
| 2611 | result: part.content, |
| 2612 | providerExecuted: true |
| 2613 | }) |
| 2614 | } |
| 2615 | break |
| 2616 | } |
| 2617 | |
| 2618 | case "mcp_tool_use": { |
| 2619 | const part = event.content_block |
| 2620 | |
| 2621 | const toolCall: Response.ToolCallPartEncoded = { |
| 2622 | type: "tool-call", |
| 2623 | id: part.id, |
| 2624 | name: part.name, |
| 2625 | params: part.input, |
| 2626 | providerExecuted: true, |
| 2627 | metadata: { anthropic: { mcp_tool: { server: part.server_name } } } |
| 2628 | } |
| 2629 | |
| 2630 | mcpToolCalls.set(part.id, toolCall) |
| 2631 | |
| 2632 | parts.push(toolCall) |
| 2633 | |
| 2634 | break |
| 2635 | } |
| 2636 | |
| 2637 | case "mcp_tool_result": { |
| 2638 | const part = event.content_block |
| 2639 | const toolCall = mcpToolCalls.get(part.tool_use_id) |
| 2640 | const mcpMetadata = toolCall?.metadata?.anthropic?.mcp_tool |
| 2641 | |
| 2642 | if (Predicate.isNotUndefined(toolCall)) { |
| 2643 | parts.push({ |
| 2644 | type: "tool-result", |
| 2645 | id: part.tool_use_id, |
| 2646 | name: toolCall.name, |
| 2647 | isFailure: part.is_error, |
| 2648 | result: part.content, |
| 2649 | metadata: { |
| 2650 | anthropic: { |
| 2651 | ...(Predicate.isNotNullish(mcpMetadata) |
| 2652 | ? { mcp_tool: mcpMetadata } : |
| 2653 | undefined) |
| 2654 | } |
| 2655 | } |
| 2656 | }) |
| 2657 | } |
| 2658 | |
| 2659 | break |
| 2660 | } |
| 2661 | |
| 2662 | case "fallback": { |
| 2663 | parts.push({ |
| 2664 | type: "response-metadata", |
| 2665 | modelId: event.content_block.to.model, |
| 2666 | timestamp: DateTime.formatIso(yield* DateTime.now), |
| 2667 | metadata: { anthropic: { fallback: event.content_block } } |
| 2668 | }) |
| 2669 | |
| 2670 | break |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | break |
| 2675 | } |
| 2676 | |
| 2677 | case "content_block_delta": { |
| 2678 | const part = event.delta |
| 2679 | |
| 2680 | switch (part.type) { |
| 2681 | case "text_delta": { |
| 2682 | parts.push({ |
| 2683 | type: "text-delta", |
| 2684 | id: event.index.toString(), |
| 2685 | delta: part.text |
| 2686 | }) |
| 2687 | |
| 2688 | break |
| 2689 | } |
| 2690 | |
| 2691 | case "thinking_delta": { |
| 2692 | parts.push({ |
| 2693 | type: "reasoning-delta", |
| 2694 | id: event.index.toString(), |
| 2695 | delta: part.thinking |
| 2696 | }) |
| 2697 | |
| 2698 | break |
| 2699 | } |
| 2700 | |
| 2701 | case "signature_delta": { |
| 2702 | if (blockType === "thinking") { |
| 2703 | parts.push({ |
| 2704 | type: "reasoning-delta", |
| 2705 | id: event.index.toString(), |
| 2706 | delta: "", |
| 2707 | metadata: { |
| 2708 | anthropic: { |
| 2709 | info: { |
| 2710 | type: "thinking", |
| 2711 | signature: part.signature |
| 2712 | } |
| 2713 | } |
| 2714 | } |
| 2715 | }) |
| 2716 | } |
| 2717 | |
| 2718 | break |
| 2719 | } |
| 2720 | |
| 2721 | case "input_json_delta": { |
| 2722 | let delta = part.partial_json |
| 2723 | |
| 2724 | // Skip empty deltas |
| 2725 | if (delta.length === 0) { |
| 2726 | break |
| 2727 | } |
| 2728 | |
| 2729 | const contentBlock = contentBlocks.get(event.index) |
| 2730 | |
| 2731 | // Skip invalid deltas |
| 2732 | if (Predicate.isUndefined(contentBlock)) { |
| 2733 | break |
| 2734 | } |
| 2735 | |
| 2736 | // Skip non-tool-call deltas |
| 2737 | if (contentBlock.type !== "tool-call") { |
| 2738 | break |
| 2739 | } |
| 2740 | |
| 2741 | if ( |
| 2742 | contentBlock.firstDelta && |
| 2743 | (contentBlock.providerName === "bash_code_execution" || |
| 2744 | contentBlock.providerName === "text_editor_code_execution") |
| 2745 | ) { |
| 2746 | delta = `{"type":${JSON.stringify(contentBlock.providerName)},${delta.substring(1)}` |
| 2747 | } |
| 2748 | |
| 2749 | parts.push({ |
| 2750 | type: "tool-params-delta", |
| 2751 | id: contentBlock.id, |
| 2752 | delta |
| 2753 | }) |
| 2754 | |
| 2755 | contentBlock.params += delta |
| 2756 | contentBlock.firstDelta = false |
| 2757 | |
| 2758 | break |
| 2759 | } |
| 2760 | |
| 2761 | case "citations_delta": { |
| 2762 | const source = yield* processCitation(part.citation, citableDocuments) |
| 2763 | |
| 2764 | if (Predicate.isNotUndefined(source)) { |
| 2765 | parts.push(source) |
| 2766 | } |
| 2767 | |
| 2768 | break |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | break |
| 2773 | } |
| 2774 | |
| 2775 | case "content_block_stop": { |
| 2776 | const contentBlock = contentBlocks.get(event.index) |
| 2777 | |
| 2778 | if (Predicate.isNotUndefined(contentBlock)) { |
| 2779 | switch (contentBlock.type) { |
| 2780 | case "text": { |
| 2781 | parts.push({ |
| 2782 | type: "text-end", |
| 2783 | id: event.index.toString() |
| 2784 | }) |
| 2785 | |
| 2786 | break |
| 2787 | } |
| 2788 | |
| 2789 | case "reasoning": { |
| 2790 | parts.push({ |
| 2791 | type: "reasoning-end", |
| 2792 | id: event.index.toString() |
| 2793 | }) |
| 2794 | |
| 2795 | break |
| 2796 | } |
| 2797 | |
| 2798 | case "tool-call": { |
| 2799 | parts.push({ |
| 2800 | type: "tool-params-end", |
| 2801 | id: contentBlock.id |
| 2802 | }) |
| 2803 | |
| 2804 | // For code execution, inject the `programmatic-tool-call` type |
| 2805 | // when the input format is `{ code }` |
| 2806 | let finalParams = contentBlock.params.length === 0 ? "{}" : contentBlock.params |
| 2807 | |
| 2808 | if (contentBlock.providerName === "code_execution") { |
| 2809 | // @effect-diagnostics-next-line tryCatchInEffectGen:off |
| 2810 | try { |
| 2811 | const params = Tool.unsafeSecureJsonParse(finalParams) |
| 2812 | if (Predicate.hasProperty(params, "code") && !Predicate.hasProperty(params, "type")) { |
| 2813 | finalParams = JSON.stringify({ type: "programmatic-tool-call", ...params }) |
| 2814 | } |
| 2815 | } catch { |
| 2816 | // Ignore errors and use original tool call parameters |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | const toolParams = yield* Effect.try({ |
| 2821 | try: () => Tool.unsafeSecureJsonParse(finalParams), |
| 2822 | catch: (cause) => |
| 2823 | AiError.make({ |
| 2824 | module: "AnthropicLanguageModel", |
| 2825 | method: "makeStreamResponse", |
| 2826 | reason: new AiError.ToolParameterValidationError({ |
| 2827 | toolName: contentBlock.name, |
| 2828 | description: `Failed to securely JSON parse tool parameters: ${cause}` |
| 2829 | }) |
| 2830 | }) |
| 2831 | }) |
| 2832 | |
| 2833 | const params = contentBlock.providerExecuted === true |
| 2834 | ? toolParams |
| 2835 | : yield* transformToolCallParams(options, contentBlock.name, toolParams) |
| 2836 | |
| 2837 | parts.push({ |
| 2838 | type: "tool-call", |
| 2839 | id: contentBlock.id, |
| 2840 | name: contentBlock.name, |
| 2841 | params, |
| 2842 | ...(Predicate.isNotUndefined(contentBlock.providerExecuted) |
| 2843 | ? { providerExecuted: contentBlock.providerExecuted } |
| 2844 | : undefined), |
| 2845 | ...(Predicate.isNotUndefined(contentBlock.caller) |
| 2846 | ? { metadata: { anthropic: { caller: contentBlock.caller } } } |
| 2847 | : undefined) |
| 2848 | }) |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | contentBlocks.delete(event.index) |
| 2853 | } |
| 2854 | |
| 2855 | blockType = undefined |
| 2856 | |
| 2857 | break |
| 2858 | } |
| 2859 | |
| 2860 | case "error": { |
| 2861 | parts.push({ |
| 2862 | type: "error", |
| 2863 | error: event.error, |
| 2864 | metadata: { anthropic: { requestId: event.request_id } } |
| 2865 | }) |
| 2866 | |
| 2867 | break |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | return parts |
| 2872 | })), |
| 2873 | Stream.flattenIterable |
| 2874 | ) |
| 2875 | } |
| 2876 | ) |
| 2877 | |
| 2878 | // ============================================================================= |
| 2879 | // Telemetry |
| 2880 | // ============================================================================= |
| 2881 | |
| 2882 | const annotateRequest = ( |
| 2883 | span: Span, |
| 2884 | request: typeof Generated.BetaCreateMessageParams.Encoded |
| 2885 | ): void => { |
| 2886 | addGenAIAnnotations(span, { |
| 2887 | provider: { name: "anthropic" }, |
| 2888 | operation: { name: "chat" }, |
| 2889 | request: { |
| 2890 | model: request.model, |
| 2891 | temperature: request.temperature, |
| 2892 | topK: request.top_k, |
| 2893 | topP: request.top_p, |
| 2894 | maxTokens: request.max_tokens, |
| 2895 | stopSequences: Arr.ensure(request.stop_sequences).filter( |
| 2896 | Predicate.isNotNullish |
| 2897 | ) |
| 2898 | } |
| 2899 | }) |
| 2900 | } |
| 2901 | |
| 2902 | const annotateResponse = (span: Span, response: Generated.BetaMessage): void => { |
| 2903 | const cacheRead = response.usage.cache_read_input_tokens ?? 0 |
| 2904 | const cacheWrite = response.usage.cache_creation_input_tokens ?? 0 |
| 2905 | addGenAIAnnotations(span, { |
| 2906 | response: { |
| 2907 | id: response.id, |
| 2908 | model: response.model, |
| 2909 | finishReasons: response.stop_reason ? [response.stop_reason] : undefined |
| 2910 | }, |
| 2911 | usage: { |
| 2912 | inputTokens: response.usage.input_tokens + cacheRead + cacheWrite, |
| 2913 | outputTokens: response.usage.output_tokens |
| 2914 | } |
| 2915 | }) |
| 2916 | annotateCacheUsage(span, cacheRead, cacheWrite) |
| 2917 | } |
| 2918 | |
| 2919 | const annotateStreamResponse = (span: Span, part: Response.StreamPartEncoded) => { |
| 2920 | if (part.type === "response-metadata") { |
| 2921 | addGenAIAnnotations(span, { |
| 2922 | response: { |
| 2923 | id: part.id, |
| 2924 | model: part.modelId |
| 2925 | } |
| 2926 | }) |
| 2927 | } |
| 2928 | if (part.type === "finish") { |
| 2929 | addGenAIAnnotations(span, { |
| 2930 | response: { |
| 2931 | finishReasons: [part.reason] |
| 2932 | }, |
| 2933 | usage: { |
| 2934 | inputTokens: part.usage.inputTokens.total, |
| 2935 | outputTokens: part.usage.outputTokens.total |
| 2936 | } |
| 2937 | }) |
| 2938 | annotateCacheUsage(span, part.usage.inputTokens.cacheRead, part.usage.inputTokens.cacheWrite) |
| 2939 | } |
| 2940 | } |
| 2941 | |
| 2942 | // Cache counts are included in `gen_ai.usage.input_tokens`. |
| 2943 | const annotateCacheUsage = (span: Span, cacheRead: number | undefined, cacheWrite: number | undefined): void => { |
| 2944 | if (Predicate.isNotNullish(cacheRead)) span.attribute("gen_ai.usage.cache_read.input_tokens", cacheRead) |
| 2945 | if (Predicate.isNotNullish(cacheWrite)) span.attribute("gen_ai.usage.cache_write.input_tokens", cacheWrite) |
| 2946 | } |
| 2947 | |
| 2948 | // ============================================================================= |
| 2949 | // Internal Utilities |
| 2950 | // ============================================================================= |
| 2951 | |
| 2952 | type ContentGroup = SystemMessageGroup | AssistantMessageGroup | UserMessageGroup |
| 2953 | |
| 2954 | interface SystemMessageGroup { |
| 2955 | readonly type: "system" |
| 2956 | readonly messages: Array<Prompt.SystemMessage> |
| 2957 | } |
| 2958 | |
| 2959 | interface AssistantMessageGroup { |
| 2960 | readonly type: "assistant" |
| 2961 | readonly messages: Array<Prompt.AssistantMessage> |
| 2962 | } |
| 2963 | |
| 2964 | interface UserMessageGroup { |
| 2965 | readonly type: "user" |
| 2966 | readonly messages: Array<Prompt.ToolMessage | Prompt.UserMessage> |
| 2967 | } |
| 2968 | |
| 2969 | const groupMessages = (prompt: Prompt.Prompt): Array<ContentGroup> => { |
| 2970 | const messages: Array<ContentGroup> = [] |
| 2971 | let current: ContentGroup | undefined = undefined |
| 2972 | for (const message of prompt.content) { |
| 2973 | switch (message.role) { |
| 2974 | case "system": { |
| 2975 | if (current?.type !== "system") { |
| 2976 | current = { type: "system", messages: [] } |
| 2977 | messages.push(current) |
| 2978 | } |
| 2979 | current.messages.push(message) |
| 2980 | break |
| 2981 | } |
| 2982 | case "assistant": { |
| 2983 | if (current?.type !== "assistant") { |
| 2984 | current = { type: "assistant", messages: [] } |
| 2985 | messages.push(current) |
| 2986 | } |
| 2987 | current.messages.push(message) |
| 2988 | break |
| 2989 | } |
| 2990 | case "tool": |
| 2991 | case "user": { |
| 2992 | if (current?.type !== "user") { |
| 2993 | current = { type: "user", messages: [] } |
| 2994 | messages.push(current) |
| 2995 | } |
| 2996 | current.messages.push(message) |
| 2997 | break |
| 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | return messages |
| 3002 | } |
| 3003 | |
| 3004 | /** |
| 3005 | * Checks whether data is a URL (either a URL object or a URL string). |
| 3006 | */ |
| 3007 | const isUrlData = ( |
| 3008 | data: typeof Prompt.FilePart.Type["data"] |
| 3009 | ): data is URL => data instanceof URL || isUrlString(data) |
| 3010 | |
| 3011 | const isUrlString = (data: typeof Prompt.FilePart.Type["data"]): boolean => |
| 3012 | typeof data === "string" && /^https?:\/\//i.test(data) |
| 3013 | |
| 3014 | const getUrlString = (data: string | URL): string => data instanceof URL ? data.toString() : data |
| 3015 | |
| 3016 | const getCacheControl = ( |
| 3017 | part: |
| 3018 | | Prompt.SystemMessage |
| 3019 | | Prompt.UserMessage |
| 3020 | | Prompt.AssistantMessage |
| 3021 | | Prompt.ToolMessage |
| 3022 | | Prompt.UserMessagePart |
| 3023 | | Prompt.AssistantMessagePart |
| 3024 | | Prompt.ToolMessagePart |
| 3025 | ): typeof Generated.CacheControlEphemeral.Encoded | null => part.options.anthropic?.cacheControl ?? null |
| 3026 | |
| 3027 | // Skip blocks that cannot carry cache_control (thinking, MCP tool listing and |
| 3028 | // fallback blocks) without crossing the part/message boundary. |
| 3029 | // Preserve existing part-level breakpoints over message-level ones. |
| 3030 | const setLastCacheControl = ( |
| 3031 | content: Array<typeof Generated.BetaInputContentBlock.Encoded>, |
| 3032 | start: number, |
| 3033 | cacheControl: typeof Generated.CacheControlEphemeral.Encoded | null |
| 3034 | ): void => { |
| 3035 | if (cacheControl === null) { |
| 3036 | return |
| 3037 | } |
| 3038 | for (let i = content.length - 1; i >= start; i--) { |
| 3039 | const block = content[i] |
| 3040 | if ( |
| 3041 | block.type === "thinking" || block.type === "redacted_thinking" || block.type === "mcp_tool_listing" || |
| 3042 | block.type === "fallback" |
| 3043 | ) { |
| 3044 | continue |
| 3045 | } |
| 3046 | if (Predicate.isNullish(block.cache_control)) { |
| 3047 | content[i] = { ...block, cache_control: cacheControl } |
| 3048 | } |
| 3049 | return |
| 3050 | } |
| 3051 | } |
| 3052 | |
| 3053 | const getDocumentMetadata = (part: Prompt.FilePart): { |
| 3054 | readonly title: string | null |
| 3055 | readonly context: string | null |
| 3056 | } | null => { |
| 3057 | const options = part.options.anthropic |
| 3058 | if (Predicate.isNotUndefined(options)) { |
| 3059 | return { |
| 3060 | title: options?.documentTitle ?? null, |
| 3061 | context: options?.documentContext ?? null |
| 3062 | } |
| 3063 | } |
| 3064 | return null |
| 3065 | } |
| 3066 | |
| 3067 | const areCitationsEnabled = (part: Prompt.FilePart): boolean => part.options.anthropic?.citations?.enabled ?? false |
| 3068 | |
| 3069 | const isCitationPart = (part: Prompt.UserMessagePart | Prompt.ToolModelOutputPart): part is Prompt.FilePart => |
| 3070 | part.type === "file" && (part.mediaType === "application/pdf" || part.mediaType === "text/plain") |
| 3071 | ? areCitationsEnabled(part) |
| 3072 | : false |
| 3073 | |
| 3074 | interface CitableDocument { |
| 3075 | readonly title: string |
| 3076 | readonly fileName?: string | undefined |
| 3077 | readonly mediaType: string |
| 3078 | } |
| 3079 | |
| 3080 | const extractCitableDocuments = (prompt: Prompt.Prompt): Array<CitableDocument> => { |
| 3081 | const citableDocuments: Array<CitableDocument> = [] |
| 3082 | |
| 3083 | const addCitableDocument = (part: Prompt.UserMessagePart | Prompt.ToolModelOutputPart) => { |
| 3084 | if (isCitationPart(part)) { |
| 3085 | citableDocuments.push({ |
| 3086 | title: part.fileName ?? "Untitled Document", |
| 3087 | fileName: part.fileName, |
| 3088 | mediaType: part.mediaType |
| 3089 | }) |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | for (const message of prompt.content) { |
| 3094 | if (message.role === "user") { |
| 3095 | message.content.forEach(addCitableDocument) |
| 3096 | } else if (message.role === "tool") { |
| 3097 | for (const part of message.content) { |
| 3098 | if (part.type === "tool-result") { |
| 3099 | part.modelOutput?.forEach(addCitableDocument) |
| 3100 | } |
| 3101 | } |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | return citableDocuments |
| 3106 | } |
| 3107 | |
| 3108 | const processCitation = Effect.fnUntraced( |
| 3109 | function*( |
| 3110 | citation: |
| 3111 | | Generated.ResponseCharLocationCitation |
| 3112 | | Generated.ResponsePageLocationCitation |
| 3113 | | Generated.ResponseContentBlockLocationCitation |
| 3114 | | Generated.ResponseWebSearchResultLocationCitation |
| 3115 | | Generated.ResponseSearchResultLocationCitation, |
| 3116 | citableDocuments: ReadonlyArray<CitableDocument> |
| 3117 | ): Effect.fn.Return< |
| 3118 | Response.DocumentSourcePartEncoded | Response.UrlSourcePartEncoded | undefined, |
| 3119 | never, |
| 3120 | IdGenerator.IdGenerator |
| 3121 | > { |
| 3122 | const idGenerator = yield* IdGenerator.IdGenerator |
| 3123 | |
| 3124 | if (citation.type === "page_location" || citation.type === "char_location") { |
| 3125 | const citedDocument = citableDocuments[citation.document_index] |
| 3126 | if (Predicate.isNotUndefined(citedDocument)) { |
| 3127 | const id = yield* idGenerator.generateId() |
| 3128 | const metadata = citation.type === "char_location" |
| 3129 | ? { |
| 3130 | source: "document", |
| 3131 | type: citation.type, |
| 3132 | citedText: citation.cited_text, |
| 3133 | startCharIndex: citation.start_char_index, |
| 3134 | endCharIndex: citation.end_char_index |
| 3135 | } as const |
| 3136 | : { |
| 3137 | source: "document", |
| 3138 | type: citation.type, |
| 3139 | citedText: citation.cited_text, |
| 3140 | startPageNumber: citation.start_page_number, |
| 3141 | endPageNumber: citation.end_page_number |
| 3142 | } as const |
| 3143 | |
| 3144 | return { |
| 3145 | type: "source", |
| 3146 | sourceType: "document", |
| 3147 | id, |
| 3148 | mediaType: citedDocument.mediaType, |
| 3149 | title: citation.document_title ?? citedDocument.title, |
| 3150 | ...(Predicate.isNotUndefined(citedDocument.fileName) |
| 3151 | ? { fileName: citedDocument.fileName } |
| 3152 | : undefined), |
| 3153 | metadata: { anthropic: metadata } |
| 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | if (citation.type === "web_search_result_location") { |
| 3159 | const id = yield* idGenerator.generateId() |
| 3160 | |
| 3161 | const metadata = { |
| 3162 | source: "url", |
| 3163 | citedText: citation.cited_text, |
| 3164 | encryptedIndex: citation.encrypted_index |
| 3165 | } as const |
| 3166 | |
| 3167 | return { |
| 3168 | type: "source", |
| 3169 | sourceType: "url", |
| 3170 | id, |
| 3171 | url: citation.url, |
| 3172 | title: citation.title ?? "Untitled", |
| 3173 | metadata: { anthropic: metadata } |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | return undefined |
| 3178 | } |
| 3179 | ) |
| 3180 | |
| 3181 | interface ModelCapabilities { |
| 3182 | readonly maxOutputTokens: number |
| 3183 | readonly supportsStructuredOutput: boolean |
| 3184 | readonly supportsMidConversationSystemMessages: boolean |
| 3185 | } |
| 3186 | |
| 3187 | const getConfigCapabilities = (config: typeof Config.Service & { readonly model: string }): ModelCapabilities => { |
| 3188 | const capabilities = getModelCapabilities(config.model) |
| 3189 | return { |
| 3190 | ...capabilities, |
| 3191 | supportsStructuredOutput: config.structuredOutputs ?? capabilities.supportsStructuredOutput, |
| 3192 | supportsMidConversationSystemMessages: config.midConversationSystemMessages ?? |
| 3193 | capabilities.supportsMidConversationSystemMessages |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | /** |
| 3198 | * Returns model defaults, optimistically assuming modern capabilities for unknown IDs. |
| 3199 | * |
| 3200 | * @see https://docs.claude.com/en/docs/about-claude/models/overview#model-comparison-table |
| 3201 | * @see https://platform.claude.com/docs/en/build-with-claude/structured-outputs |
| 3202 | * @see https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages |
| 3203 | */ |
| 3204 | const getModelCapabilities = (modelId: string): ModelCapabilities => { |
| 3205 | if ( |
| 3206 | modelId.includes("claude-sonnet-4-5") || |
| 3207 | modelId.includes("claude-opus-4-5") || |
| 3208 | modelId.includes("claude-haiku-4-5") |
| 3209 | ) { |
| 3210 | return { |
| 3211 | maxOutputTokens: 64000, |
| 3212 | supportsStructuredOutput: true, |
| 3213 | supportsMidConversationSystemMessages: false |
| 3214 | } |
| 3215 | } else if (modelId.includes("claude-opus-4-1")) { |
| 3216 | return { |
| 3217 | maxOutputTokens: 32000, |
| 3218 | supportsStructuredOutput: true, |
| 3219 | supportsMidConversationSystemMessages: false |
| 3220 | } |
| 3221 | } else if ( |
| 3222 | modelId.includes("claude-sonnet-4-0") || |
| 3223 | modelId.includes("claude-sonnet-4-20250514") || |
| 3224 | modelId.includes("claude-3-7-sonnet") |
| 3225 | ) { |
| 3226 | return { |
| 3227 | maxOutputTokens: 64000, |
| 3228 | supportsStructuredOutput: false, |
| 3229 | supportsMidConversationSystemMessages: false |
| 3230 | } |
| 3231 | } else if ( |
| 3232 | modelId.includes("claude-opus-4-0") || |
| 3233 | modelId.includes("claude-opus-4-20250514") |
| 3234 | ) { |
| 3235 | return { |
| 3236 | maxOutputTokens: 32000, |
| 3237 | supportsStructuredOutput: false, |
| 3238 | supportsMidConversationSystemMessages: false |
| 3239 | } |
| 3240 | } else if (modelId.includes("claude-3-5-haiku")) { |
| 3241 | return { |
| 3242 | maxOutputTokens: 8192, |
| 3243 | supportsStructuredOutput: false, |
| 3244 | supportsMidConversationSystemMessages: false |
| 3245 | } |
| 3246 | } else if (modelId.includes("claude-3-")) { |
| 3247 | return { |
| 3248 | maxOutputTokens: 4096, |
| 3249 | supportsStructuredOutput: false, |
| 3250 | supportsMidConversationSystemMessages: false |
| 3251 | } |
| 3252 | } else { |
| 3253 | return { |
| 3254 | maxOutputTokens: 128000, |
| 3255 | supportsStructuredOutput: true, |
| 3256 | supportsMidConversationSystemMessages: !( |
| 3257 | modelId.includes("claude-opus-4-6") || |
| 3258 | modelId.includes("claude-opus-4-7") || |
| 3259 | modelId.includes("claude-sonnet-4-6") || |
| 3260 | modelId.includes("claude-mythos-preview") || |
| 3261 | // Match Sonnet 5, excluding minor versions such as 5.5. |
| 3262 | /claude-sonnet-5(?!-\d\b)/.test(modelId) |
| 3263 | ) |
| 3264 | } |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | const unsupportedSchemaError = (error: unknown, method: string): AiError.AiError => |
| 3269 | AiError.make({ |
| 3270 | module: "AnthropicLanguageModel", |
| 3271 | method, |
| 3272 | reason: new AiError.UnsupportedSchemaError({ |
| 3273 | description: error instanceof Error ? error.message : String(error) |
| 3274 | }) |
| 3275 | }) |
| 3276 | |
| 3277 | const tryJsonSchema = <S extends Schema.Constraint>(schema: S, method: string) => |
| 3278 | Effect.try({ |
| 3279 | try: () => Tool.getJsonSchemaFromSchema(schema, { transformer: toCodecAnthropic }), |
| 3280 | catch: (error) => unsupportedSchemaError(error, method) |
| 3281 | }) |
| 3282 | |
| 3283 | const toolParametersError = (tool: Tool.Any, error: unknown, method: string): AiError.AiError => |
| 3284 | unsupportedSchemaError( |
| 3285 | `Tool "${tool.name}" parameters: ${error instanceof Error ? error.message : String(error)}. ` + |
| 3286 | "For a tool without parameters, omit `parameters` or use `Tool.EmptyParams`", |
| 3287 | method |
| 3288 | ) |
| 3289 | |
| 3290 | const getToolJsonSchema = (tool: Tool.Any | Tool.AnyDynamic, strict: boolean) => { |
| 3291 | try { |
| 3292 | return Tool.getJsonSchema(tool, { transformer: toCodecAnthropic }) |
| 3293 | } catch (error) { |
| 3294 | if (strict) { |
| 3295 | throw error |
| 3296 | } |
| 3297 | // Anthropic's schema restrictions (e.g. no recursion) only apply to strict tools |
| 3298 | return Tool.getJsonSchema(tool, { transformer: LanguageModel.defaultCodecTransformer }) |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | const tryToolJsonSchema = <T extends Tool.Any | Tool.AnyDynamic>(tool: T, strict: boolean, method: string) => |
| 3303 | Effect.try({ |
| 3304 | try: () => getToolJsonSchema(tool, strict), |
| 3305 | catch: (error) => toolParametersError(tool, error, method) |
| 3306 | }).pipe(Effect.filterOrFail( |
| 3307 | // A raw JSON Schema given to a dynamic tool is sent unchanged |
| 3308 | (jsonSchema) => jsonSchema.type === "object" || (Tool.isDynamic(tool) && tool.jsonSchema !== undefined), |
| 3309 | (jsonSchema) => |
| 3310 | toolParametersError( |
| 3311 | tool, |
| 3312 | `expected a JSON Schema with type "object", received ${JSON.stringify(jsonSchema)}`, |
| 3313 | method |
| 3314 | ) |
| 3315 | )) |
| 3316 | |
| 3317 | const getOutputFormat = Effect.fnUntraced(function*({ capabilities, options }: { |
| 3318 | readonly capabilities: ModelCapabilities |
| 3319 | readonly options: LanguageModel.ProviderOptions |
| 3320 | }): Effect.fn.Return<typeof Generated.JsonOutputFormat.Encoded | undefined, AiError.AiError> { |
| 3321 | if (options.responseFormat.type === "json" && capabilities.supportsStructuredOutput) { |
| 3322 | const jsonSchema = yield* tryJsonSchema(options.responseFormat.schema, "getOutputFormat") |
| 3323 | return { |
| 3324 | type: "json_schema", |
| 3325 | schema: jsonSchema as any |
| 3326 | } |
| 3327 | } |
| 3328 | return undefined |
| 3329 | }) |
| 3330 | |
| 3331 | const getToolParamsCodec = <S extends Schema.Constraint>(schema: S) => { |
| 3332 | try { |
| 3333 | return toCodecAnthropic(schema).codec |
| 3334 | } catch { |
| 3335 | // The request used the plain JSON Schema (non-strict tools only), which |
| 3336 | // describes the JSON codec of the parameters |
| 3337 | return Schema.toCodecJson(schema) |
| 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | const transformToolCallParams = Effect.fnUntraced(function*( |
| 3342 | options: LanguageModel.ProviderOptions, |
| 3343 | toolName: string, |
| 3344 | toolParams: unknown |
| 3345 | ): Effect.fn.Return<unknown, AiError.AiError> { |
| 3346 | const tool = options.tools.find((tool) => tool.name === toolName) |
| 3347 | |
| 3348 | if (Predicate.isUndefined(tool)) { |
| 3349 | if (options.toolCallValidation === "deferred") { |
| 3350 | return toolParams |
| 3351 | } |
| 3352 | return yield* AiError.make({ |
| 3353 | module: "AnthropicLanguageModel", |
| 3354 | method: "makeResponse", |
| 3355 | reason: new AiError.ToolNotFoundError({ |
| 3356 | toolName, |
| 3357 | availableTools: options.tools.map((tool) => tool.name) |
| 3358 | }) |
| 3359 | }) |
| 3360 | } |
| 3361 | |
| 3362 | const codec = getToolParamsCodec(tool.parametersSchema) |
| 3363 | |
| 3364 | // Normalize valid parameters; leave invalid ones for Toolkit. |
| 3365 | return yield* ( |
| 3366 | Schema.decodeEffect(codec)(toolParams) as Effect.Effect<unknown, Schema.SchemaError> |
| 3367 | ).pipe( |
| 3368 | Effect.flatMap((decoded) => |
| 3369 | Schema.encodeUnknownEffect(tool.parametersSchema)(decoded) as Effect.Effect<unknown, Schema.SchemaError> |
| 3370 | ), |
| 3371 | Effect.orElseSucceed(() => toolParams) |
| 3372 | ) |
| 3373 | }) |