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

main
ts 822 lines 27.3 KB
Raw
1 import * as aiGateway from "@distilled.cloud/cloudflare/ai-gateway";
2 import { deepEqual, isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { isResourceOfType, Resource } from "alchemy/Resource";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import * as Stream from "effect/Stream";
9 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
10 import type { Providers } from "../Providers.ts";
11
12 export type GatewayRateLimitingTechnique = "fixed" | "sliding";
13
14 export type GatewayLogManagementStrategy = "STOP_INSERTING" | "DELETE_OLDEST";
15
16 export type GatewayDlp =
17 | {
18 /**
19 * Action to take when a DLP profile matches.
20 */
21 action: "BLOCK" | "FLAG";
22 /**
23 * Whether DLP is enabled.
24 */
25 enabled: boolean;
26 /**
27 * DLP profile identifiers to apply.
28 */
29 profiles: string[];
30 }
31 | {
32 /**
33 * Whether DLP is enabled.
34 */
35 enabled: boolean;
36 /**
37 * DLP policies to apply.
38 */
39 policies: {
40 /**
41 * DLP policy identifier.
42 */
43 id: string;
44 /**
45 * Action to take when the policy matches.
46 */
47 action: "FLAG" | "BLOCK";
48 /**
49 * Request or response phases checked by the policy.
50 */
51 check: ("REQUEST" | "RESPONSE")[];
52 /**
53 * Whether the policy is enabled.
54 */
55 enabled: boolean;
56 /**
57 * DLP profile identifiers to apply.
58 */
59 profiles: string[];
60 }[];
61 };
62
63 export type GatewayOtel = {
64 /**
65 * Authorization header value for the OpenTelemetry endpoint.
66 */
67 authorization?: string;
68 /**
69 * Additional headers sent to the OpenTelemetry endpoint.
70 */
71 headers: Record<string, unknown>;
72 /**
73 * OpenTelemetry endpoint URL.
74 */
75 url: string;
76 /**
77 * Payload encoding sent to the OpenTelemetry endpoint.
78 * @default "json"
79 */
80 contentType?: "json" | "protobuf";
81 };
82
83 export type GatewayStripe = {
84 /**
85 * Authorization header value for Stripe usage events.
86 */
87 authorization: string;
88 /**
89 * Stripe usage event payload definitions.
90 */
91 usageEvents: {
92 /**
93 * Usage event payload.
94 */
95 payload: string;
96 }[];
97 };
98
99 /**
100 * A single spend-limit rule. Caps cumulative spend (in cents) routed through
101 * the gateway over a rolling `window` (in seconds), optionally scoped to a set
102 * of models or providers.
103 */
104 export type GatewaySpendLimitRule = {
105 /**
106 * Spend cap for this rule. The amount is in cents (`limitType: "cost"`).
107 */
108 limit: number;
109 /**
110 * The kind of limit. Only `"cost"` is currently supported.
111 */
112 limitType: "cost";
113 /**
114 * Rolling window over which `limit` accumulates. Accepts an Effect
115 * `Duration.Input` — e.g. `"1 minute"`, `"1 day"`, or a `Duration` — and is
116 * sent to Cloudflare as whole seconds.
117 */
118 window: Duration.Input;
119 /**
120 * Stable identifier for the rule. Cloudflare assigns one if omitted; pass
121 * it back to update an existing rule in place.
122 */
123 id?: string;
124 /**
125 * Whether this rule is enforced.
126 *
127 * @default true
128 */
129 enabled?: boolean;
130 /**
131 * Arbitrary metadata attached to the rule.
132 */
133 metadata?: Record<string, unknown>;
134 /**
135 * Restrict the rule to a set of model ids.
136 */
137 model?: { mode: "filter"; values: string[] };
138 /**
139 * Restrict the rule to a set of providers.
140 */
141 provider?: { mode: "filter"; values: string[] };
142 /**
143 * Enforcement algorithm — `fixed` resets on the window boundary, `sliding`
144 * tracks a rolling window.
145 */
146 technique?: GatewayRateLimitingTechnique;
147 };
148
149 /**
150 * Per-gateway spend limits — Cloudflare's replacement for the deprecated
151 * account-level AI Gateway spending limit. Attach cost caps directly to a
152 * gateway via {@link GatewayProps.spendLimits}.
153 *
154 * @see https://developers.cloudflare.com/ai-gateway/features/spend-limits/
155 */
156 export type GatewaySpendLimits = {
157 /**
158 * Whether spend limiting is enabled for the gateway.
159 */
160 enabled?: boolean;
161 /**
162 * The cost-cap rules applied to requests routed through the gateway.
163 */
164 rules?: GatewaySpendLimitRule[];
165 };
166
167 export type GatewayProps = {
168 /**
169 * Gateway identifier. If omitted, a unique ID will be generated.
170 *
171 * Must be 1-64 characters and match Cloudflare's AI Gateway ID pattern:
172 * lowercase letters, numbers, underscores, and hyphens.
173 *
174 * @default ${app}-${stage}-${id}
175 */
176 id?: string;
177 /**
178 * Whether cached responses are invalidated when a request changes.
179 *
180 * @default false
181 */
182 cacheInvalidateOnUpdate?: boolean;
183 /**
184 * Cache time-to-live in seconds. Set to `null` to disable caching.
185 *
186 * @default null
187 */
188 cacheTtl?: number | null;
189 /**
190 * Whether AI Gateway stores request logs.
191 *
192 * @default true
193 */
194 collectLogs?: boolean;
195 /**
196 * Rate limiting interval in seconds. Set to `null` to disable rate limiting.
197 *
198 * @default null
199 */
200 rateLimitingInterval?: number | null;
201 /**
202 * Maximum requests allowed during the rate limiting interval. Set to `null`
203 * to disable rate limiting.
204 *
205 * @default null
206 */
207 rateLimitingLimit?: number | null;
208 /**
209 * Rate limiting algorithm.
210 *
211 * @default "fixed"
212 */
213 rateLimitingTechnique?: GatewayRateLimitingTechnique;
214 /**
215 * Whether gateway authentication is enabled. Cloudflare requires it on new
216 * gateways. When omitted, a new gateway enables it and an existing one
217 * keeps its current setting.
218 */
219 authentication?: boolean;
220 /**
221 * DLP configuration. The installed distilled Cloudflare client applies this
222 * through the update API after gateway creation.
223 */
224 dlp?: GatewayDlp;
225 /**
226 * Whether this gateway is the account default.
227 */
228 isDefault?: boolean;
229 /**
230 * Maximum number of log entries to retain.
231 */
232 logManagement?: number | null;
233 /**
234 * Strategy used when retained logs reach `logManagement`.
235 */
236 logManagementStrategy?: GatewayLogManagementStrategy | null;
237 /**
238 * Whether Logpush is enabled for this gateway.
239 */
240 logpush?: boolean;
241 /**
242 * Public key used for Logpush encryption.
243 */
244 logpushPublicKey?: string | null;
245 /**
246 * OpenTelemetry export configuration.
247 */
248 otel?: GatewayOtel[] | null;
249 /**
250 * Store identifier used by the gateway.
251 */
252 storeId?: string | null;
253 /**
254 * Stripe usage export configuration.
255 */
256 stripe?: GatewayStripe | null;
257 /**
258 * Per-gateway spend limits (Cloudflare's replacement for the deprecated
259 * account-level spending limit). Applied through the update API after
260 * gateway creation.
261 */
262 spendLimits?: GatewaySpendLimits | null;
263 /**
264 * Whether Zero Data Retention is enabled.
265 */
266 zdr?: boolean;
267 };
268
269 export type Gateway = Resource<
270 "Cloudflare.AI.Gateway",
271 GatewayProps,
272 {
273 gatewayId: string;
274 accountId: string;
275 cacheInvalidateOnUpdate: boolean;
276 cacheTtl: number | null;
277 collectLogs: boolean;
278 createdAt: string;
279 modifiedAt: string;
280 rateLimitingInterval: number | null;
281 rateLimitingLimit: number | null;
282 rateLimitingTechnique: GatewayRateLimitingTechnique;
283 authentication: boolean;
284 dlp: GatewayDlp | undefined;
285 isDefault: boolean;
286 logManagement: number;
287 logManagementStrategy: GatewayLogManagementStrategy;
288 logpush: boolean;
289 logpushPublicKey: string | undefined;
290 otel: GatewayOtel[] | undefined;
291 storeId: string;
292 stripe: GatewayStripe | undefined;
293 spendLimits: GatewaySpendLimits | undefined;
294 zdr: boolean;
295 },
296 never,
297 Providers
298 >;
299
300 // Cloudflare's AI Gateway API uses 0 to mean "disabled" for cache TTL and
301 // rate limiting fields. Normalize back to null so user-facing semantics
302 // match what was passed in.
303 const nullIfZero = (value: number | null | undefined): number | null =>
304 value == null || value === 0 ? null : value;
305
306 export const isAiGateway = (value: unknown): value is Gateway =>
307 isResourceOfType(value, "Cloudflare.AI.Gateway");
308
309 /**
310 * A Cloudflare.AI. Gateway for observability, caching, rate limiting, and
311 * governance across AI provider requests.
312 *
313 * AI Gateway gives your application a stable gateway ID and account-scoped
314 * endpoint that can route model requests through Cloudflare. Once bound to a
315 * Worker, `aiGateway.model({...})` returns an `effect/ai`
316 * `LanguageModel` Layer so you use the standard `generateText` / `streamText`
317 * APIs — provider-agnostic, with caching, rate limiting, retries, and a
318 * unified request log handled by the gateway.
319 * ### Creating a Gateway
320 * **Example:** Basic gateway
321 * ```typescript
322 * const gateway = yield* Cloudflare.AI.Gateway("Gateway");
323 * ```
324 *
325 * **Example:** Gateway with caching and rate limiting
326 * ```typescript
327 * const gateway = yield* Cloudflare.AI.Gateway("Gateway", {
328 * id: "my-gateway",
329 * cacheTtl: 300,
330 * cacheInvalidateOnUpdate: true,
331 * rateLimitingInterval: 60,
332 * rateLimitingLimit: 100,
333 * rateLimitingTechnique: "sliding",
334 * });
335 * ```
336 *
337 * ### Logging
338 * **Example:** Gateway with log retention
339 * ```typescript
340 * const gateway = yield* Cloudflare.AI.Gateway("Gateway", {
341 * collectLogs: true,
342 * logManagement: 10000,
343 * logManagementStrategy: "STOP_INSERTING",
344 * });
345 * ```
346 *
347 * ### Binding into a Worker
348 * **Example:** Bind the gateway and provide the runtime layer
349 * `Cloudflare.AI.QueryGateway(gateway)` returns a typed, Effect-native client during the
350 * Worker's Init phase. Provide `Cloudflare.AI.QueryGatewayBinding` once at the
351 * bottom of the Init layer chain so every `QueryGateway(...)` resolves at runtime.
352 * ```typescript
353 * import * as Cloudflare from "@alchemy.run/cloudflare";
354 * import * as Effect from "effect/Effect";
355 * import { Gateway } from "./Gateway.ts";
356 *
357 * export default class Api extends Cloudflare.Worker<Api>()(
358 * "Api",
359 * { main: import.meta.url },
360 * Effect.gen(function* () {
361 * const aiGateway = yield* Cloudflare.AI.QueryGateway(Gateway);
362 *
363 * return {
364 * fetch: Effect.gen(function* () {
365 * // …routes
366 * }),
367 * };
368 * }).pipe(Effect.provide(Cloudflare.AI.QueryGatewayBinding)),
369 * ) {}
370 * ```
371 *
372 * ### Building a LanguageModel
373 * **Example:** `aiGateway.model(...)` -> Effect AI `LanguageModel`
374 * Call `aiGateway.model({...})` with a Workers AI model id. It returns a
375 * `Layer<LanguageModel, never, RuntimeContext>` directly — no API key and no
376 * `Layer.unwrap`, since the binding handles auth and the gateway URL. Build it
377 * in the Init phase; construction is pure.
378 * ```typescript
379 * const aiGateway = yield* Cloudflare.AI.QueryGateway(Gateway);
380 *
381 * const languageModel = aiGateway.model({
382 * model: "@cf/meta/llama-3.1-8b-instruct",
383 * parameters: { temperature: 0.7, maxTokens: 1024 },
384 * });
385 * ```
386 *
387 * ### Generating Text
388 * **Example:** Generate text on a route
389 * Provide the `languageModel` layer to the handler and call
390 * `LanguageModel.generateText` like any other Effect. `Effect.orDie` collapses
391 * `AiError` to a defect (a 500); use `Effect.catchTag("AiError", …)` for typed
392 * handling instead.
393 * ```typescript
394 * import { LanguageModel } from "effect/ai";
395 * import * as HttpServerResponse from "effect/http/HttpServerResponse";
396 *
397 * fetch: Effect.gen(function* () {
398 * const response = yield* LanguageModel.generateText({
399 * prompt: "Say hello.",
400 * }).pipe(Effect.orDie);
401 * return yield* HttpServerResponse.json({
402 * text: response.text,
403 * usage: {
404 * inputTokens: response.usage.inputTokens.total,
405 * outputTokens: response.usage.outputTokens.total,
406 * },
407 * });
408 * }).pipe(Effect.provide(languageModel));
409 * ```
410 *
411 * ### Streaming Text
412 * **Example:** Stream tokens as Server-Sent Events
413 * `LanguageModel.streamText` returns a `Stream` of typed response parts.
414 * `Stream.provide(languageModel)` keeps the model available for the whole
415 * stream lifetime; pipe through `Sse.encode` for an SSE response.
416 * ```typescript
417 * import { LanguageModel } from "effect/ai";
418 * import * as Stream from "effect/Stream";
419 * import * as Sse from "effect/encoding/Sse";
420 * import * as HttpServerResponse from "effect/http/HttpServerResponse";
421 *
422 * const stream = LanguageModel.streamText({ prompt }).pipe(
423 * Stream.provide(languageModel),
424 * Sse.encode,
425 * );
426 * return HttpServerResponse.stream(stream, {
427 * headers: {
428 * "content-type": "text/event-stream",
429 * "cache-control": "no-cache",
430 * "x-accel-buffering": "no",
431 * },
432 * });
433 * ```
434 *
435 * ### Tuning the Gateway
436 * **Example:** Production-grade caching, rate limits, and DLP
437 * Every prop maps to an in-place update — no replacement, no downtime.
438 * ```typescript
439 * export const Gateway = Cloudflare.AI.Gateway("Gateway", {
440 * id: "prod-gateway",
441 * cacheTtl: 300,
442 * cacheInvalidateOnUpdate: true,
443 * rateLimitingInterval: 60,
444 * rateLimitingLimit: 100,
445 * rateLimitingTechnique: "sliding",
446 * collectLogs: true,
447 * logManagement: 100_000,
448 * logManagementStrategy: "DELETE_OLDEST",
449 * authentication: true,
450 * });
451 * ```
452 *
453 * ### Spend Limits
454 * **Example:** Cap cost per rolling window
455 * Per-gateway spend limits replace the deprecated account-level spending
456 * limit. Each rule caps cumulative cost (in cents) over a rolling `window`
457 * (in seconds), optionally scoped to specific models or providers.
458 * ```typescript
459 * const gateway = yield* Cloudflare.AI.Gateway("Gateway", {
460 * spendLimits: {
461 * enabled: true,
462 * rules: [
463 * { limitType: "cost", limit: 500_00, window: "1 day" }, // $500/day
464 * ],
465 * },
466 * });
467 * ```
468 *
469 * @resource
470 * @product AI Gateway
471 * @category AI
472 */
473 export const Gateway = Resource<Gateway>("Cloudflare.AI.Gateway", {
474 aliases: ["Cloudflare.AiGateway"],
475 });
476
477 export const GatewayResourceProvider = () =>
478 Provider.succeed(Gateway, {
479 stables: ["gatewayId", "accountId"],
480 diff: Effect.fn(function* ({ id, olds = {}, news = {}, output }) {
481 if (!isResolved(news)) return undefined;
482 const { accountId } = yield* yield* CloudflareEnvironment;
483
484 const next = yield* desired(id, news);
485 const oldGatewayId = output?.gatewayId ?? (yield* createGatewayId(id, olds.id));
486 // Auto-generated ids are engine-owned: the deployed id stays
487 // authoritative even if the generator would name this id differently
488 // today. Only an explicit user-provided id can force a replace.
489 const newGatewayId = news.id ?? oldGatewayId;
490 if ((output?.accountId ?? accountId) !== accountId || oldGatewayId !== newGatewayId) {
491 return { action: "replace" } as const;
492 }
493
494 const oldMutable = mutable(output ?? ((yield* desired(id, olds)) as Gateway["Attributes"]));
495 const nextMutable = mutable({
496 ...next,
497 authentication: next.authentication ?? oldMutable.authentication,
498 } as Gateway["Attributes"]);
499 if (!deepEqual(oldMutable, nextMutable)) {
500 return { action: "update" } as const;
501 }
502 }),
503 reconcile: Effect.fn(function* ({ id, news = {}, output }) {
504 const { accountId } = yield* yield* CloudflareEnvironment;
505
506 const acct = output?.accountId ?? accountId;
507 const gatewayId = output?.gatewayId ?? (yield* createGatewayId(id, news.id));
508
509 // Observe — fetch the gateway's current state. The Cloudflare API
510 // returns 404 when the gateway is missing, which we tolerate so the
511 // reconciler can fall through to create.
512 const observed = yield* aiGateway
513 .getAiGateway({
514 accountId: acct,
515 id: gatewayId,
516 })
517 .pipe(Effect.catchTag("GatewayNotFound", () => Effect.undefined));
518
519 // Ensure — create if missing. Tolerate `GatewayAlreadyExists` for
520 // idempotency: a peer reconciler may have created it concurrently,
521 // or state persistence may have failed after a previous create.
522 if (observed === undefined) {
523 // Prefer the deployed id: regenerating would target a different
524 // resource if the generator's output for this id ever drifts.
525 const request = { ...(yield* createRequest(id, news)), id: gatewayId };
526 yield* aiGateway
527 .createAiGateway(request)
528 .pipe(
529 Effect.catchTag("GatewayAlreadyExists", () =>
530 aiGateway.getAiGateway({ accountId: acct, id: request.id }),
531 ),
532 );
533 }
534
535 // Sync — the Cloudflare.AI. Gateway update API is a full PATCH that
536 // overwrites all mutable fields. We always apply the desired shape
537 // so adoption, drift, and routine updates all converge.
538 // Prefer the deployed id: regenerating would target a different
539 // resource if the generator's output for this id ever drifts.
540 const update = {
541 ...(yield* updateRequest(id, news, acct, observed?.authentication)),
542 id: gatewayId,
543 };
544 const gateway = yield* aiGateway.updateAiGateway(update);
545 return mapGateway(gateway, acct);
546 }),
547 delete: Effect.fn(function* ({ output }) {
548 yield* aiGateway
549 .deleteAiGateway({
550 accountId: output.accountId,
551 id: output.gatewayId,
552 })
553 .pipe(Effect.catchTag("GatewayNotFound", () => Effect.void));
554 }),
555 read: Effect.fn(function* ({ id, olds, output }) {
556 const { accountId } = yield* yield* CloudflareEnvironment;
557
558 const gatewayId = output?.gatewayId ?? (yield* createGatewayId(id, olds?.id));
559 const acct = output?.accountId ?? accountId;
560 return yield* aiGateway
561 .getAiGateway({
562 accountId: acct,
563 id: gatewayId,
564 })
565 .pipe(
566 Effect.map((gateway) => mapGateway(gateway, acct)),
567 Effect.catchTag("GatewayNotFound", () => Effect.undefined),
568 );
569 }),
570 // AI Gateways are account-scoped; the list API returns the full gateway
571 // object for every gateway in the account in one paginated collection.
572 // Exhaustively paginate and hydrate each item into the same Attributes
573 // shape `read` produces (via `mapGateway`).
574 list: Effect.fn(function* () {
575 const { accountId } = yield* yield* CloudflareEnvironment;
576 return yield* aiGateway.listAiGateways.pages({ accountId }).pipe(
577 Stream.runCollect,
578 Effect.map((chunk) =>
579 Array.from(chunk).flatMap((page) =>
580 (page.result ?? []).map((gateway) => mapGateway(gateway, accountId)),
581 ),
582 ),
583 );
584 }),
585 });
586
587 const createGatewayId = (id: string, gatewayId: string | undefined) =>
588 Effect.gen(function* () {
589 if (gatewayId) return gatewayId;
590 return yield* createPhysicalName({
591 id,
592 maxLength: 64,
593 lowercase: true,
594 });
595 });
596 const desired = (id: string, props: GatewayProps | undefined) =>
597 Effect.gen(function* () {
598 return {
599 gatewayId: yield* createGatewayId(id, props?.id),
600 cacheInvalidateOnUpdate: props?.cacheInvalidateOnUpdate ?? false,
601 cacheTtl: props?.cacheTtl ?? null,
602 collectLogs: props?.collectLogs ?? true,
603 rateLimitingInterval: props?.rateLimitingInterval ?? null,
604 rateLimitingLimit: props?.rateLimitingLimit ?? null,
605 rateLimitingTechnique: props?.rateLimitingTechnique ?? "fixed",
606 // Unmanaged when omitted: Cloudflare requires it on new gateways, and
607 // an existing gateway keeps its current setting.
608 authentication: props?.authentication,
609 dlp: props?.dlp ?? undefined,
610 isDefault: props?.isDefault ?? false,
611 logManagement: props?.logManagement ?? 100_000,
612 logManagementStrategy: props?.logManagementStrategy ?? "STOP_INSERTING",
613 logpush: props?.logpush ?? false,
614 logpushPublicKey: props?.logpushPublicKey ?? undefined,
615 otel: props?.otel ?? undefined,
616 storeId: props?.storeId ?? "",
617 stripe: props?.stripe ?? undefined,
618 spendLimits: resolveSpendLimits(props?.spendLimits),
619 zdr: props?.zdr ?? false,
620 };
621 });
622
623 // Resolve the user-facing spend limits into the API request shape: each rule's
624 // `window` is decoded from an Effect `Duration.Input` (e.g. `"1 day"`) into
625 // whole seconds. Done once here so both the create/update request body and the
626 // diff baseline carry seconds (matching what the API echoes back), keeping the
627 // reconciler's diff stable.
628 const resolveSpendLimits = (spendLimits: GatewaySpendLimits | null | undefined) => {
629 if (spendLimits == null) return undefined;
630 return {
631 enabled: spendLimits.enabled,
632 rules: spendLimits.rules?.map((rule) => ({
633 ...rule,
634 window: Math.round(Duration.toSeconds(rule.window)),
635 })),
636 };
637 };
638
639 const mapGateway = (
640 gateway:
641 | aiGateway.GetAiGatewayResponse
642 | aiGateway.CreateAiGatewayResponse
643 | aiGateway.UpdateAiGatewayResponse,
644 accountId: string,
645 ): Gateway["Attributes"] => ({
646 gatewayId: gateway.id,
647 accountId,
648 // accountTag: gateway.accountTag ?? undefined,
649 // internalId: gateway.internalId ?? undefined,
650 cacheInvalidateOnUpdate: gateway.cacheInvalidateOnUpdate,
651 cacheTtl: nullIfZero(gateway.cacheTtl),
652 collectLogs: gateway.collectLogs,
653 createdAt: gateway.createdAt,
654 modifiedAt: gateway.modifiedAt,
655 rateLimitingInterval: nullIfZero(gateway.rateLimitingInterval),
656 rateLimitingLimit: nullIfZero(gateway.rateLimitingLimit),
657 rateLimitingTechnique: gateway.rateLimitingTechnique ?? "fixed",
658 authentication: gateway.authentication ?? false,
659 // Distilled widened generated string enums to open unions (`string & {}`).
660 dlp: (gateway.dlp ?? undefined) as GatewayDlp | undefined,
661 isDefault: gateway.isDefault ?? false,
662 logManagement: gateway.logManagement ?? 100_000,
663 logManagementStrategy: gateway.logManagementStrategy ?? "STOP_INSERTING",
664 logpush: gateway.logpush ?? false,
665 logpushPublicKey: gateway.logpushPublicKey ?? undefined,
666 // The wire shape uses explicit nulls and an open content-type union —
667 // normalize into our prop shape so attributes diff cleanly against props.
668 otel: gateway.otel?.map((o): GatewayOtel => ({
669 url: o.url,
670 headers: o.headers,
671 ...(o.authorization != null ? { authorization: o.authorization } : {}),
672 ...(o.contentType != null ? { contentType: o.contentType as "json" | "protobuf" } : {}),
673 })),
674 storeId: gateway.storeId ?? "",
675 stripe: gateway.stripe ?? undefined,
676 spendLimits: normalizeSpendLimits(gateway.spendLimits),
677 zdr: gateway.zdr ?? false,
678 });
679
680 // Normalize spend limits into the user-facing shape and drop server-managed
681 // noise so attributes diff cleanly against props. Cloudflare assigns a stable
682 // `id` to each rule and echoes back an explicit `enabled` flag — fold the
683 // per-rule `enabled` default (`true`) and `null`s away so a gateway the user
684 // configured without ids converges to a no-op instead of perpetual drift.
685 const normalizeSpendLimits = (
686 spendLimits:
687 | {
688 enabled?: boolean | null;
689 rules?:
690 | {
691 limit: number;
692 limitType: "cost";
693 window: number;
694 id?: string | null;
695 enabled?: boolean | null;
696 metadata?: Record<string, unknown> | null;
697 model?: { mode: "filter"; values: string[] } | null;
698 provider?: { mode: "filter"; values: string[] } | null;
699 technique?: string | null;
700 }[]
701 | null;
702 }
703 | null
704 | undefined,
705 ): GatewaySpendLimits | undefined => {
706 if (spendLimits == null) return undefined;
707 return {
708 enabled: spendLimits.enabled ?? false,
709 rules: (spendLimits.rules ?? []).map((rule): GatewaySpendLimitRule => ({
710 limit: rule.limit,
711 limitType: rule.limitType,
712 window: rule.window,
713 enabled: rule.enabled ?? true,
714 ...(rule.id != null ? { id: rule.id } : {}),
715 ...(rule.metadata != null && Object.keys(rule.metadata).length > 0
716 ? { metadata: rule.metadata }
717 : {}),
718 ...(rule.model != null ? { model: rule.model } : {}),
719 ...(rule.provider != null ? { provider: rule.provider } : {}),
720 ...(rule.technique != null
721 ? { technique: rule.technique as GatewayRateLimitingTechnique }
722 : {}),
723 })),
724 };
725 };
726
727 const mutable = (gateway: Gateway["Attributes"]) => ({
728 cacheInvalidateOnUpdate: gateway.cacheInvalidateOnUpdate,
729 cacheTtl: gateway.cacheTtl,
730 collectLogs: gateway.collectLogs,
731 rateLimitingInterval: gateway.rateLimitingInterval,
732 rateLimitingLimit: gateway.rateLimitingLimit,
733 rateLimitingTechnique: gateway.rateLimitingTechnique,
734 authentication: gateway.authentication,
735 dlp: gateway.dlp,
736 isDefault: gateway.isDefault,
737 logManagement: gateway.logManagement,
738 logManagementStrategy: gateway.logManagementStrategy,
739 logpush: gateway.logpush,
740 logpushPublicKey: gateway.logpushPublicKey,
741 otel: gateway.otel,
742 storeId: gateway.storeId,
743 stripe: gateway.stripe,
744 spendLimits: spendLimitsForDiff(gateway.spendLimits),
745 zdr: gateway.zdr,
746 });
747
748 // Compare spend limits by their user-meaningful shape: drop the
749 // server-assigned rule `id` (absent on freshly-declared props) and apply the
750 // per-rule `enabled` default so desired (from props) and observed (from the
751 // API) converge to a no-op when semantically equal.
752 const spendLimitsForDiff = (spendLimits: GatewaySpendLimits | undefined) => {
753 if (spendLimits == null) return undefined;
754 return {
755 enabled: spendLimits.enabled ?? false,
756 rules: (spendLimits.rules ?? []).map((rule) => ({
757 limit: rule.limit,
758 limitType: rule.limitType,
759 window: rule.window,
760 enabled: rule.enabled ?? true,
761 ...(rule.metadata != null && Object.keys(rule.metadata).length > 0
762 ? { metadata: rule.metadata }
763 : {}),
764 ...(rule.model != null ? { model: rule.model } : {}),
765 ...(rule.provider != null ? { provider: rule.provider } : {}),
766 ...(rule.technique != null ? { technique: rule.technique } : {}),
767 })),
768 };
769 };
770 const createRequest = Effect.fn(function* (id: string, props: GatewayProps | undefined) {
771 const next = yield* desired(id, props);
772 const { accountId } = yield* yield* CloudflareEnvironment;
773
774 return {
775 accountId,
776 id: next.gatewayId,
777 cacheInvalidateOnUpdate: next.cacheInvalidateOnUpdate,
778 cacheTtl: next.cacheTtl,
779 collectLogs: next.collectLogs,
780 rateLimitingInterval: next.rateLimitingInterval,
781 rateLimitingLimit: next.rateLimitingLimit,
782 rateLimitingTechnique: next.rateLimitingTechnique,
783 // Cloudflare rejects new gateways without authentication (code 7001).
784 authentication: next.authentication ?? true,
785 logManagement: next.logManagement,
786 logManagementStrategy: next.logManagementStrategy,
787 logpush: next.logpush,
788 logpushPublicKey: next.logpushPublicKey,
789 zdr: next.zdr,
790 } satisfies aiGateway.CreateAiGatewayRequest;
791 });
792
793 const updateRequest = Effect.fn(function* (
794 id: string,
795 props: GatewayProps | undefined,
796 accountId: string,
797 observedAuthentication: boolean | null | undefined,
798 ) {
799 const next = yield* desired(id, props);
800 return {
801 accountId,
802 id: next.gatewayId,
803 cacheInvalidateOnUpdate: next.cacheInvalidateOnUpdate,
804 cacheTtl: next.cacheTtl,
805 collectLogs: next.collectLogs,
806 rateLimitingInterval: next.rateLimitingInterval,
807 rateLimitingLimit: next.rateLimitingLimit,
808 rateLimitingTechnique: next.rateLimitingTechnique,
809 // The update is a full PATCH: an omitted prop keeps what the gateway has.
810 authentication: next.authentication ?? observedAuthentication ?? true,
811 dlp: next.dlp,
812 logManagement: next.logManagement,
813 logManagementStrategy: next.logManagementStrategy,
814 logpush: next.logpush,
815 logpushPublicKey: next.logpushPublicKey,
816 otel: next.otel,
817 storeId: next.storeId,
818 stripe: next.stripe,
819 spendLimits: next.spendLimits,
820 zdr: next.zdr,
821 } satisfies aiGateway.UpdateAiGatewayRequest;
822 });