| 1 | /** |
| 2 | * The `AnthropicConfig` module lets a workflow temporarily customize the HTTP |
| 3 | * client used by generated Anthropic requests. It is used by |
| 4 | * `AnthropicClient` when request helpers run, so code can add middleware, |
| 5 | * logging, or other client changes without rebuilding the client layer. |
| 6 | * |
| 7 | * @since 4.0.0 |
| 8 | */ |
| 9 | import * as Context from "effect/Context" |
| 10 | import * as Effect from "effect/Effect" |
| 11 | import { dual } from "effect/Function" |
| 12 | import type { HttpClient } from "effect/http/HttpClient" |
| 13 | |
| 14 | /** |
| 15 | * Service tag for Anthropic client configuration overrides, such as transformations applied to the generated HTTP client. |
| 16 | * |
| 17 | * **When to use** |
| 18 | * |
| 19 | * Use when you need to provide or read Anthropic client configuration through |
| 20 | * Effect's context from a layer or integration. |
| 21 | * |
| 22 | * @see {@link withClientTransform} for scoping an HTTP client transformation |
| 23 | * |
| 24 | * @category services |
| 25 | * @since 4.0.0 |
| 26 | */ |
| 27 | export class AnthropicConfig extends Context.Service< |
| 28 | AnthropicConfig, |
| 29 | AnthropicConfig.Service |
| 30 | >()("@effect/ai-anthropic/AnthropicConfig") { |
| 31 | /** |
| 32 | * Gets the configured Anthropic service from the current context when present. |
| 33 | * |
| 34 | * @since 4.0.0 |
| 35 | */ |
| 36 | static readonly getOrUndefined: Effect.Effect<typeof AnthropicConfig.Service | undefined> = Effect.map( |
| 37 | Effect.context<never>(), |
| 38 | Context.getOrUndefined(AnthropicConfig) |
| 39 | ) |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Namespace containing types associated with the `AnthropicConfig` service. |
| 44 | * |
| 45 | * @since 4.0.0 |
| 46 | */ |
| 47 | export declare namespace AnthropicConfig { |
| 48 | /** |
| 49 | * Configuration provided through `AnthropicConfig`. |
| 50 | * |
| 51 | * **Details** |
| 52 | * |
| 53 | * Use `transformClient` to wrap or replace the `HttpClient` used by generated Anthropic API requests. |
| 54 | * |
| 55 | * @category services |
| 56 | * @since 4.0.0 |
| 57 | */ |
| 58 | export interface Service { |
| 59 | readonly transformClient?: ((client: HttpClient) => HttpClient) | undefined |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Runs an effect with an `AnthropicConfig` override that transforms the underlying `HttpClient` used by generated Anthropic requests. |
| 65 | * |
| 66 | * **When to use** |
| 67 | * |
| 68 | * Use when you need to apply a temporary `HttpClient` transformation, such as adding middleware or logging, to a |
| 69 | * specific scope of an effectful program. |
| 70 | * |
| 71 | * @category configuration |
| 72 | * @since 4.0.0 |
| 73 | */ |
| 74 | export const withClientTransform: { |
| 75 | (transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> |
| 76 | <A, E, R>(self: Effect.Effect<A, E, R>, transform: (client: HttpClient) => HttpClient): Effect.Effect<A, E, R> |
| 77 | } = dual(2, <A, E, R>( |
| 78 | self: Effect.Effect<A, E, R>, |
| 79 | transformClient: (client: HttpClient) => HttpClient |
| 80 | ) => |
| 81 | Effect.flatMap( |
| 82 | AnthropicConfig.getOrUndefined, |
| 83 | (config) => Effect.provideService(self, AnthropicConfig, { ...config, transformClient }) |
| 84 | )) |