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

main
ts 157 lines 5.41 KB
Raw
1 /**
2 * HTTP client for Cloudflare Clef decisions through Workers AI REST.
3 *
4 * @stability unstable
5 * @since 4.0.0
6 */
7 import * as AiError from "effect/ai/AiError"
8 import * as Config from "effect/Config"
9 import * as Context from "effect/Context"
10 import * as Effect from "effect/Effect"
11 import { flow, identity } from "effect/Function"
12 import * as HttpClient from "effect/http/HttpClient"
13 import * as HttpClientRequest from "effect/http/HttpClientRequest"
14 import * as HttpClientResponse from "effect/http/HttpClientResponse"
15 import * as Layer from "effect/Layer"
16 import * as Redacted from "effect/Redacted"
17 import * as Schema from "effect/Schema"
18 import * as CloudflareSchema from "./CloudflareSchema.ts"
19 import * as Errors from "./internal/errors.ts"
20
21 /**
22 * Low-level Cloudflare decision operations.
23 *
24 * @stability unstable
25 * @category services
26 * @since 4.0.0
27 */
28 export interface Service {
29 readonly client: HttpClient.HttpClient
30 readonly createDecisions: (
31 request: typeof CloudflareSchema.DecisionsRequest.Encoded
32 ) => Effect.Effect<typeof CloudflareSchema.DecisionsResponse.Type, AiError.AiError>
33 }
34
35 /**
36 * Cloudflare Workers AI client service.
37 *
38 * @stability unstable
39 * @category services
40 * @since 4.0.0
41 */
42 export class CloudflareClient
43 extends Context.Service<CloudflareClient, Service>()("@effect/ai-cloudflare/CloudflareClient")
44 {}
45
46 /**
47 * Account, authentication, AI Gateway, API base URL, and HTTP customization options.
48 *
49 * @stability unstable
50 * @category options
51 * @since 4.0.0
52 */
53 export interface Options {
54 readonly accountId: string
55 readonly apiKey: Redacted.Redacted<string>
56 readonly gatewayId?: string | undefined
57 readonly apiUrl?: string | undefined
58 readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
59 }
60
61 const decodeResponse = HttpClientResponse.schemaBodyJson(Schema.Union([
62 Schema.Struct({ success: Schema.Literal(true), result: CloudflareSchema.DecisionsResponse }),
63 Schema.Struct({ success: Schema.Literal(false) })
64 ]))
65
66 /**
67 * Builds a Cloudflare client without automatic retries.
68 *
69 * @stability unstable
70 * @category constructors
71 * @since 4.0.0
72 */
73 export const make = Effect.fnUntraced(
74 function*(options: Options): Effect.fn.Return<Service, never, HttpClient.HttpClient> {
75 const base = yield* HttpClient.HttpClient
76 const client = base.pipe(
77 HttpClient.mapRequest(flow(
78 HttpClientRequest.prependUrl(options.apiUrl ?? "https://api.cloudflare.com/client/v4"),
79 HttpClientRequest.bearerToken(Redacted.value(options.apiKey)),
80 HttpClientRequest.acceptJson,
81 options.gatewayId === undefined
82 ? identity
83 : HttpClientRequest.setHeader("cf-aig-gateway-id", options.gatewayId)
84 )),
85 HttpClient.filterStatusOk,
86 options.transformClient ?? identity
87 )
88 const path = `/accounts/${encodeURIComponent(options.accountId)}/ai/run/@cf/cloudflare/`
89 return CloudflareClient.of({
90 client,
91 createDecisions: (payload) =>
92 HttpClientRequest.bodyJson(HttpClientRequest.post(path + payload.model), payload).pipe(
93 Effect.mapError((error) =>
94 AiError.make({
95 module: "CloudflareClient",
96 method: "createDecisions",
97 reason: new AiError.InvalidRequestError({ description: String(error) })
98 })
99 ),
100 Effect.flatMap(client.execute),
101 Effect.flatMap((response) =>
102 decodeResponse(response).pipe(
103 Effect.flatMap((body) => body.success ? Effect.succeed(body.result) : Errors.mapResponseError(response))
104 )
105 ),
106 Effect.catchTags({
107 HttpClientError: Errors.mapHttpClientError,
108 SchemaError: (error) =>
109 Effect.fail(AiError.make({
110 module: "CloudflareClient",
111 method: "createDecisions",
112 reason: AiError.InvalidResponseError.fromSchemaError(error)
113 }))
114 })
115 )
116 })
117 }
118 )
119
120 /**
121 * Provides a Cloudflare client from explicit options.
122 *
123 * @stability unstable
124 * @category layers
125 * @since 4.0.0
126 */
127 export const layer = (options: Options): Layer.Layer<CloudflareClient, never, HttpClient.HttpClient> =>
128 Layer.effect(CloudflareClient, make(options))
129
130 /**
131 * Provides a client from configuration. It reads `CLOUDFLARE_ACCOUNT_ID`, `CLOUDFLARE_API_TOKEN`, and the optional
132 * `CLOUDFLARE_AI_GATEWAY_ID` by default.
133 *
134 * @stability unstable
135 * @category layers
136 * @since 4.0.0
137 */
138 export const layerConfig = (options?: {
139 readonly accountId?: Config.Config<string> | undefined
140 readonly apiKey?: Config.Config<Redacted.Redacted<string>> | undefined
141 readonly gatewayId?: Config.Config<string> | undefined
142 readonly apiUrl?: Config.Config<string> | undefined
143 readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
144 }): Layer.Layer<CloudflareClient, Config.ConfigError, HttpClient.HttpClient> =>
145 Layer.effect(
146 CloudflareClient,
147 Effect.gen(function*() {
148 return yield* make({
149 accountId: yield* (options?.accountId ?? Config.String("CLOUDFLARE_ACCOUNT_ID")),
150 apiKey: yield* (options?.apiKey ?? Config.Redacted("CLOUDFLARE_API_TOKEN")),
151 gatewayId: yield* (options?.gatewayId ??
152 Config.String("CLOUDFLARE_AI_GATEWAY_ID").pipe(Config.withDefault(undefined))),
153 apiUrl: options?.apiUrl ? yield* options.apiUrl : undefined,
154 transformClient: options?.transformClient
155 })
156 })
157 )