The best way to control Claude Code, Codex and more from any computer or phone

main
ts 91 lines 2.66 KB
Raw
1 /**
2 * The `OpenAiConfig` module lets a workflow temporarily customize the HTTP
3 * client used by OpenAI-compatible request helpers. Model, embedding, and
4 * tool-calling code can use this scoped configuration to add middleware,
5 * instrumentation, or routing without rebuilding the client layer.
6 *
7 * @stability unstable
8 * @since 4.0.0
9 */
10 import * as Context from "effect/Context"
11 import * as Effect from "effect/Effect"
12 import { dual } from "effect/Function"
13 import type { HttpClient } from "effect/http/HttpClient"
14
15 /**
16 * Context service for OpenAI-compatible client configuration in the current
17 * Effect scope.
18 *
19 * **When to use**
20 *
21 * Use as the context service for scoped OpenAI-compatible client configuration
22 * and HTTP client transforms.
23 *
24 * @see {@link withClientTransform} for scoping an HTTP client transformation
25 *
26 * @stability unstable
27 * @category services
28 * @since 4.0.0
29 */
30 export class OpenAiConfig extends Context.Service<
31 OpenAiConfig,
32 OpenAiConfig.Service
33 >()("@effect/ai-openai-compat/OpenAiConfig") {
34 /**
35 * Gets the configured OpenAI-compatible service from the current context when present.
36 *
37 * @since 4.0.0
38 */
39 static readonly getOrUndefined: Effect.Effect<typeof OpenAiConfig.Service | undefined> = Effect.map(
40 Effect.context<never>(),
41 Context.getOrUndefined(OpenAiConfig)
42 )
43 }
44
45 /**
46 * Types associated with the `OpenAiConfig` context service.
47 *
48 * @stability unstable
49 * @since 4.0.0
50 */
51 export declare namespace OpenAiConfig {
52 /**
53 * Configuration consumed by OpenAI-compatible clients when they build or
54 * resolve the underlying HTTP client.
55 *
56 * @category services
57 * @since 4.0.0
58 */
59 export interface Service {
60 readonly transformClient?: ((client: HttpClient) => HttpClient) | undefined
61 }
62 }
63
64 /**
65 * Provides an HTTP client transform for the supplied effect.
66 *
67 * **When to use**
68 *
69 * Use to add provider-specific OpenAI-compatible HTTP behavior, such as
70 * headers, retries, instrumentation, or proxy routing.
71 *
72 * **Details**
73 *
74 * OpenAI-compatible provider services read the transform from the
75 * `OpenAiConfig` context.
76 *
77 * @stability unstable
78 * @category configuration
79 * @since 4.0.0
80 */
81 export const withClientTransform: {
82 (transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
83 <A, E, R>(self: Effect.Effect<A, E, R>, transform: (client: HttpClient) => HttpClient): Effect.Effect<A, E, R>
84 } = dual(2, <A, E, R>(
85 self: Effect.Effect<A, E, R>,
86 transformClient: (client: HttpClient) => HttpClient
87 ) =>
88 Effect.flatMap(
89 OpenAiConfig.getOrUndefined,
90 (config) => Effect.provideService(self, OpenAiConfig, { ...config, transformClient })
91 ))