| 1 | /** |
| 2 | * Scoped HTTP client customization for OpenRouter requests. |
| 3 | * |
| 4 | * @since 4.0.0 |
| 5 | */ |
| 6 | import * as Context from "effect/Context" |
| 7 | import * as Effect from "effect/Effect" |
| 8 | import { dual } from "effect/Function" |
| 9 | import type { HttpClient } from "effect/http/HttpClient" |
| 10 | |
| 11 | /** |
| 12 | * Scoped configuration read when executing OpenRouter requests. |
| 13 | * |
| 14 | * @see {@link withClientTransform} for scoping an HTTP client transformation |
| 15 | * |
| 16 | * @category services |
| 17 | * @since 4.0.0 |
| 18 | */ |
| 19 | export class OpenRouterConfig extends Context.Service< |
| 20 | OpenRouterConfig, |
| 21 | OpenRouterConfig.Service |
| 22 | >()("@effect/ai-openrouter/OpenRouterConfig") { |
| 23 | /** |
| 24 | * Gets the configured OpenRouter service from the current context when present. |
| 25 | * |
| 26 | * @since 4.0.0 |
| 27 | */ |
| 28 | static readonly getOrUndefined: Effect.Effect<typeof OpenRouterConfig.Service | undefined> = Effect.map( |
| 29 | Effect.context<never>(), |
| 30 | Context.getOrUndefined(OpenRouterConfig) |
| 31 | ) |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Types associated with the `OpenRouterConfig` context service. |
| 36 | * |
| 37 | * @since 4.0.0 |
| 38 | */ |
| 39 | export declare namespace OpenRouterConfig { |
| 40 | /** |
| 41 | * HTTP client configuration for generated methods and alpha Decisions requests. |
| 42 | * |
| 43 | * @category services |
| 44 | * @since 4.0.0 |
| 45 | */ |
| 46 | export interface Service { |
| 47 | readonly transformClient?: ((client: HttpClient) => HttpClient) | undefined |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Transforms the HTTP client for generated methods and alpha Decisions requests |
| 53 | * made by the supplied effect. Streaming chat completions ignore this transform. |
| 54 | * Replaces any existing scoped transform; compose them manually to apply both. |
| 55 | * |
| 56 | * @category configuration |
| 57 | * @since 4.0.0 |
| 58 | */ |
| 59 | export const withClientTransform: { |
| 60 | (transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> |
| 61 | <A, E, R>(self: Effect.Effect<A, E, R>, transform: (client: HttpClient) => HttpClient): Effect.Effect<A, E, R> |
| 62 | } = dual< |
| 63 | (transform: (client: HttpClient) => HttpClient) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>, |
| 64 | <A, E, R>(self: Effect.Effect<A, E, R>, transform: (client: HttpClient) => HttpClient) => Effect.Effect<A, E, R> |
| 65 | >( |
| 66 | 2, |
| 67 | (self, transformClient) => |
| 68 | Effect.flatMap( |
| 69 | OpenRouterConfig.getOrUndefined, |
| 70 | (config) => Effect.provideService(self, OpenRouterConfig, { ...config, transformClient }) |
| 71 | ) |
| 72 | ) |