@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 230 lines 8.83 KB
Raw
1 import * as workers from "@distilled.cloud/cloudflare/workers";
2 import * as Provider from "alchemy/Provider";
3 import { Resource } from "alchemy/Resource";
4 import * as Effect from "effect/Effect";
5 import * as Predicate from "effect/Predicate";
6 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
7 import type { Providers } from "../Providers.ts";
8
9 const TypeId = "Cloudflare.Workers.AccountSetting" as const;
10 type TypeId = typeof TypeId;
11
12 export type AccountSettingProps = {
13 /**
14 * Default usage model applied to new Workers in this account (e.g.
15 * `"standard"`). Mostly legacy since Workers Standard pricing — new
16 * accounts only support `"standard"`.
17 *
18 * Mutable — updated in place. When omitted, the account's current value
19 * is left untouched.
20 * @default keep the account's current value
21 */
22 defaultUsageModel?: string;
23 /**
24 * Whether Green Compute is enabled for the account. When enabled,
25 * scheduled (cron) Workers run only on hardware powered by renewable
26 * energy.
27 *
28 * Mutable — updated in place. When omitted, the account's current value
29 * is left untouched.
30 * @default keep the account's current value
31 */
32 greenCompute?: boolean;
33 };
34
35 export type AccountSettingAttributes = {
36 /** The Cloudflare account these settings belong to. */
37 accountId: string;
38 /** Resolved default usage model for the account. */
39 defaultUsageModel: string | undefined;
40 /** Resolved Green Compute flag for the account. */
41 greenCompute: boolean | undefined;
42 /**
43 * The `defaultUsageModel` the account had before Alchemy first managed
44 * this singleton. Restored on destroy.
45 */
46 initialDefaultUsageModel: string | undefined;
47 /**
48 * The `greenCompute` flag the account had before Alchemy first managed
49 * this singleton. Restored on destroy.
50 */
51 initialGreenCompute: boolean | undefined;
52 };
53
54 export type AccountSetting = Resource<
55 TypeId,
56 AccountSettingProps,
57 AccountSettingAttributes,
58 never,
59 Providers
60 >;
61
62 /**
63 * The account-wide Workers settings singleton
64 * (`/accounts/{account_id}/workers/account-settings`): the default usage
65 * model for new Workers and the Green Compute flag for scheduled Workers.
66 *
67 * This is a singleton — it always exists on every account with Cloudflare
68 * defaults, so this resource never creates or deletes anything physical.
69 * Reconcile PUTs the settings when the observed values differ from the
70 * desired ones; destroy restores the values the account had before Alchemy
71 * first managed it (captured as `initialDefaultUsageModel` /
72 * `initialGreenCompute`).
73 * ### Managing account settings
74 * **Example:** Enable Green Compute for scheduled Workers
75 * ```typescript
76 * yield* Cloudflare.Workers.AccountSetting("GreenCompute", {
77 * greenCompute: true,
78 * });
79 * ```
80 *
81 * **Example:** Pin the default usage model
82 * ```typescript
83 * yield* Cloudflare.Workers.AccountSetting("UsageModel", {
84 * defaultUsageModel: "standard",
85 * greenCompute: false,
86 * });
87 * ```
88 *
89 * @see https://developers.cloudflare.com/api/resources/workers/subresources/account_settings/
90 *
91 * @resource
92 * @product Workers
93 * @category Workers & Compute
94 */
95 export const AccountSetting = Resource<AccountSetting>(TypeId);
96
97 /**
98 * Returns true if the given value is a AccountSetting resource.
99 */
100 export const isAccountSetting = (value: unknown): value is AccountSetting =>
101 Predicate.hasProperty(value, "Type") && value.Type === TypeId;
102
103 export const AccountSettingProvider = () =>
104 Provider.succeed(AccountSetting, {
105 nuke: { singleton: true },
106 stables: ["accountId", "initialDefaultUsageModel", "initialGreenCompute"],
107
108 diff: Effect.fn(function* ({ output }) {
109 const { accountId } = yield* yield* CloudflareEnvironment;
110 // The singleton's identity is the account it lives on.
111 if (output !== undefined && output.accountId !== accountId) {
112 return { action: "replace" } as const;
113 }
114 return undefined;
115 }),
116
117 read: Effect.fn(function* ({ output }) {
118 const { accountId } = yield* yield* CloudflareEnvironment;
119 const acct = output?.accountId ?? accountId;
120 const observed = yield* workers.getAccountSetting({ accountId: acct });
121 // The settings singleton always exists with Cloudflare defaults —
122 // there is nothing to "own", so a cold read adopts freely (never
123 // `Unowned`). The observed values at adoption time become the
124 // initial values restored on destroy.
125 return toAttributes(
126 acct,
127 observed,
128 output !== undefined
129 ? output.initialDefaultUsageModel
130 : (observed.defaultUsageModel ?? undefined),
131 output !== undefined ? output.initialGreenCompute : (observed.greenCompute ?? undefined),
132 );
133 }),
134
135 // Account-scoped singleton: there is no collection API — the settings
136 // object always exists once per account. Read the single live object and
137 // return it as a one-element array, exactly mirroring `read` (a cold
138 // observation where the observed values are the initial values).
139 list: Effect.fn(function* () {
140 const { accountId } = yield* yield* CloudflareEnvironment;
141 const observed = yield* workers.getAccountSetting({ accountId });
142 return [
143 toAttributes(
144 accountId,
145 observed,
146 observed.defaultUsageModel ?? undefined,
147 observed.greenCompute ?? undefined,
148 ),
149 ];
150 }),
151
152 reconcile: Effect.fn(function* ({ news, output }) {
153 const { accountId } = yield* yield* CloudflareEnvironment;
154
155 // 1. Observe — the singleton always exists; read its live values.
156 const observed = yield* workers.getAccountSetting({ accountId });
157
158 // 2. Capture — the pre-management values, restored on destroy.
159 // `output` (including an adoption read) already carries them;
160 // otherwise this is our first touch and the observed values are
161 // the account's originals.
162 const initialDefaultUsageModel =
163 output !== undefined
164 ? output.initialDefaultUsageModel
165 : (observed.defaultUsageModel ?? undefined);
166 const initialGreenCompute =
167 output !== undefined ? output.initialGreenCompute : (observed.greenCompute ?? undefined);
168
169 // 3. Sync — desired = news merged over observed (unspecified props
170 // keep the account's current value); PUT only when dirty.
171 const desired = {
172 defaultUsageModel: news.defaultUsageModel ?? observed.defaultUsageModel ?? undefined,
173 greenCompute: news.greenCompute ?? observed.greenCompute ?? undefined,
174 };
175 const dirty =
176 (news.defaultUsageModel !== undefined &&
177 (observed.defaultUsageModel ?? undefined) !== news.defaultUsageModel) ||
178 (news.greenCompute !== undefined &&
179 (observed.greenCompute ?? undefined) !== news.greenCompute);
180 if (!dirty) {
181 return toAttributes(accountId, observed, initialDefaultUsageModel, initialGreenCompute);
182 }
183
184 yield* workers.putAccountSetting({ accountId, ...desired });
185 // 4. Return — the PUT is authoritative for what we just wrote. We do
186 // NOT re-read here: `getAccountSetting` is eventually consistent and
187 // can briefly echo the pre-write values right after a PUT (observed
188 // under concurrent load), which would surface a stale `greenCompute`
189 // in the reconcile output. The values we sent are the new state.
190 return {
191 accountId,
192 defaultUsageModel: desired.defaultUsageModel,
193 greenCompute: desired.greenCompute,
194 initialDefaultUsageModel,
195 initialGreenCompute,
196 };
197 }),
198
199 delete: Effect.fn(function* ({ output }) {
200 const { accountId, initialDefaultUsageModel, initialGreenCompute } = output;
201 // There is no DELETE API — destroy restores the pre-management
202 // values. Skip the call when the live values already match
203 // (idempotent re-delete after a crashed run).
204 const observed = yield* workers.getAccountSetting({ accountId });
205 const dirty =
206 (initialDefaultUsageModel !== undefined &&
207 (observed.defaultUsageModel ?? undefined) !== initialDefaultUsageModel) ||
208 (initialGreenCompute !== undefined &&
209 (observed.greenCompute ?? undefined) !== initialGreenCompute);
210 if (!dirty) return;
211 yield* workers.putAccountSetting({
212 accountId,
213 defaultUsageModel: initialDefaultUsageModel ?? observed.defaultUsageModel ?? undefined,
214 greenCompute: initialGreenCompute ?? observed.greenCompute ?? undefined,
215 });
216 }),
217 });
218
219 const toAttributes = (
220 accountId: string,
221 observed: workers.GetAccountSettingResponse,
222 initialDefaultUsageModel: string | undefined,
223 initialGreenCompute: boolean | undefined,
224 ): AccountSettingAttributes => ({
225 accountId,
226 defaultUsageModel: observed.defaultUsageModel ?? undefined,
227 greenCompute: observed.greenCompute ?? undefined,
228 initialDefaultUsageModel,
229 initialGreenCompute,
230 });