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

main
ts 97 lines 2.88 KB
Raw
1 /**
2 * The `OpenAiConfig` module lets a workflow temporarily customize the HTTP
3 * client used by `@effect/ai-openai` request helpers. OpenAI client, language
4 * model, and embedding code read this scoped transform when they execute
5 * provider calls.
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 scoped OpenAI configuration used by provider operations.
17 *
18 * **When to use**
19 *
20 * Use to provide scoped OpenAI client configuration, such as an HTTP client
21 * transform, to OpenAI provider operations without passing it through each call.
22 *
23 * @see {@link withClientTransform} for scoping an HTTP client transformation
24 *
25 * @stability unstable
26 * @category services
27 * @since 4.0.0
28 */
29 export class OpenAiConfig extends Context.Service<
30 OpenAiConfig,
31 OpenAiConfig.Service
32 >()("@effect/ai-openai/OpenAiConfig") {
33 /**
34 * Gets the configured OpenAI service from the current context when present.
35 *
36 * @since 4.0.0
37 */
38 static readonly getOrUndefined: Effect.Effect<typeof OpenAiConfig.Service | undefined> = Effect.map(
39 Effect.context<never>(),
40 Context.getOrUndefined(OpenAiConfig)
41 )
42 }
43
44 /**
45 * Types used by the `OpenAiConfig` context service.
46 *
47 * @stability unstable
48 * @since 4.0.0
49 */
50 export declare namespace OpenAiConfig {
51 /**
52 * Configuration values read by OpenAI provider operations when executing
53 * 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 * Provides a scoped transform for the OpenAI HTTP client used by provider
65 * operations.
66 *
67 * **When to use**
68 *
69 * Use when you need temporary OpenAI HTTP client customization for a single
70 * effect or workflow without rebuilding the client layer.
71 *
72 * **Details**
73 *
74 * Supports both data-first and data-last forms. The transform is stored in the
75 * scoped `OpenAiConfig` service and read by OpenAI provider operations while
76 * running the supplied effect.
77 *
78 * **Gotchas**
79 *
80 * If a transform is already present in the scoped config, this helper replaces
81 * it. Compose transforms manually when both should apply.
82 *
83 * @stability unstable
84 * @category configuration
85 * @since 4.0.0
86 */
87 export const withClientTransform: {
88 (transform: (client: HttpClient) => HttpClient): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
89 <A, E, R>(self: Effect.Effect<A, E, R>, transform: (client: HttpClient) => HttpClient): Effect.Effect<A, E, R>
90 } = dual(2, <A, E, R>(
91 self: Effect.Effect<A, E, R>,
92 transformClient: (client: HttpClient) => HttpClient
93 ) =>
94 Effect.flatMap(
95 OpenAiConfig.getOrUndefined,
96 (config) => Effect.provideService(self, OpenAiConfig, { ...config, transformClient })
97 ))