@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, 06:00 UTC

main
ts 182 lines 7.22 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as aiGateway from "@distilled.cloud/cloudflare/ai-gateway";
6 import * as Provider from "alchemy/Provider";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import { MinimumLogLevel } from "effect/References";
11 import * as Schedule from "effect/Schedule";
12 import { testName } from "../../names.ts";
13
14 const { test } = Test.make({ providers: Cloudflare.providers() });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 // A gateway allows only one provider config per (providerSlug, alias). The
19 // two cases in this file run concurrently (test.provider), so they each get
20 // their OWN gateway to avoid colliding on the same slug+alias.
21 const LIFECYCLE_GATEWAY_ID = testName("alchemy-test-aigw-pc");
22 const LIST_GATEWAY_ID = testName("alchemy-test-aigw-pc-list");
23 const PROVIDER_SLUG = "openai";
24 const ALIAS = "default";
25 // Cloudflare requires the Secrets Store secret backing a provider config to
26 // be named exactly `{gatewayId}_{providerSlug}_{alias}`.
27 const secretName = (gatewayId: string) => `${gatewayId}_${PROVIDER_SLUG}_${ALIAS}`;
28 const SECRET_VALUE = "sk-alchemy-test-provider-config-1234567890";
29
30 class ProviderConfigStillExists extends Data.TaggedError("ProviderConfigStillExists") {}
31
32 // There is no get endpoint for provider configs — verify through the list.
33 // A missing parent gateway returns an empty list, so this also covers
34 // post-destroy verification.
35 const expectGone = (accountId: string, gatewayId: string, providerConfigId: string) =>
36 aiGateway.listProviderConfigs({ accountId, gatewayId, perPage: 50 }).pipe(
37 Effect.flatMap((page) =>
38 page.result.some((c) => c.id === providerConfigId)
39 ? Effect.fail(new ProviderConfigStillExists())
40 : Effect.void,
41 ),
42 Effect.retry({
43 while: (e): e is ProviderConfigStillExists => e instanceof ProviderConfigStillExists,
44 schedule: Schedule.max([Schedule.exponential("250 millis"), Schedule.recurs(10)]),
45 }),
46 );
47
48 test.provider(
49 "create, noop, replace, delete a BYOK provider config",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* yield* CloudflareEnvironment;
53
54 yield* stack.destroy();
55
56 const program = (rateLimit?: number) =>
57 Effect.gen(function* () {
58 // Cloudflare allows a single Secrets Store per account — the
59 // resource adopts the existing one and never deletes it.
60 const store = yield* Cloudflare.SecretsStore.Store("PcStore");
61 const gateway = yield* Cloudflare.AI.Gateway("PcGateway", {
62 id: LIFECYCLE_GATEWAY_ID,
63 // BYOK resolution requires the gateway to reference the store.
64 storeId: store.storeId,
65 });
66 const secret = yield* Cloudflare.SecretsStore.Secret("PcSecret", {
67 store,
68 name: secretName(LIFECYCLE_GATEWAY_ID),
69 value: Redacted.make(SECRET_VALUE),
70 scopes: ["ai_gateway"],
71 });
72 const config = yield* Cloudflare.AI.GatewayProvider("Byok", {
73 gatewayId: gateway.gatewayId,
74 providerSlug: PROVIDER_SLUG,
75 alias: ALIAS,
76 secretId: secret.secretId,
77 defaultConfig: true,
78 ...(rateLimit !== undefined && { rateLimit, rateLimitPeriod: 60 }),
79 });
80 return { secret, config };
81 });
82
83 const initial = yield* stack.deploy(program());
84
85 expect(initial.config.providerConfigId).toBeDefined();
86 expect(initial.config.accountId).toEqual(accountId);
87 expect(initial.config.gatewayId).toEqual(LIFECYCLE_GATEWAY_ID);
88 expect(initial.config.providerSlug).toEqual(PROVIDER_SLUG);
89 expect(initial.config.alias).toEqual(ALIAS);
90 expect(initial.config.secretId).toEqual(initial.secret.secretId);
91 expect(initial.config.defaultConfig).toBe(true);
92 expect(initial.config.rateLimit).toBeUndefined();
93
94 // Verify out-of-band via the API.
95 const live = yield* aiGateway.listProviderConfigs({
96 accountId,
97 gatewayId: LIFECYCLE_GATEWAY_ID,
98 perPage: 50,
99 });
100 const liveConfig = live.result.find((c) => c.id === initial.config.providerConfigId);
101 expect(liveConfig).toBeDefined();
102 expect(liveConfig!.alias).toEqual(ALIAS);
103 expect(liveConfig!.providerSlug).toEqual(PROVIDER_SLUG);
104 expect(liveConfig!.secretId).toEqual(initial.secret.secretId);
105
106 // Redeploying identical props is a no-op (still the same config).
107 const noop = yield* stack.deploy(program());
108 expect(noop.config.providerConfigId).toEqual(initial.config.providerConfigId);
109
110 // Provider configs have no update API — adding a rate limit is a
111 // delete-first replacement (same provider slug + alias).
112 const limited = yield* stack.deploy(program(100));
113 expect(limited.config.providerConfigId).not.toEqual(initial.config.providerConfigId);
114 expect(limited.config.rateLimit).toEqual(100);
115 expect(limited.config.rateLimitPeriod).toEqual(60);
116
117 // The replaced config is gone.
118 yield* expectGone(accountId, LIFECYCLE_GATEWAY_ID, initial.config.providerConfigId);
119
120 yield* stack.destroy();
121
122 yield* expectGone(accountId, LIFECYCLE_GATEWAY_ID, limited.config.providerConfigId);
123 }).pipe(logLevel),
124 {
125 tags: [
126 "provider:cloudflare",
127 "provider:cloudflare:ai",
128 "provider:cloudflare:secretsstore",
129 "live",
130 ],
131 },
132 );
133
134 test.provider(
135 "list enumerates the deployed provider config",
136 (stack) =>
137 Effect.gen(function* () {
138 yield* stack.destroy();
139
140 const deployed = yield* stack.deploy(
141 Effect.gen(function* () {
142 const store = yield* Cloudflare.SecretsStore.Store("PcListStore");
143 const gateway = yield* Cloudflare.AI.Gateway("PcListGateway", {
144 id: LIST_GATEWAY_ID,
145 storeId: store.storeId,
146 });
147 const secret = yield* Cloudflare.SecretsStore.Secret("PcListSecret", {
148 store,
149 name: secretName(LIST_GATEWAY_ID),
150 value: Redacted.make(SECRET_VALUE),
151 scopes: ["ai_gateway"],
152 });
153 return yield* Cloudflare.AI.GatewayProvider("ByokList", {
154 gatewayId: gateway.gatewayId,
155 providerSlug: PROVIDER_SLUG,
156 alias: ALIAS,
157 secretId: secret.secretId,
158 defaultConfig: true,
159 });
160 }),
161 );
162
163 const provider = yield* Provider.findProvider(Cloudflare.AI.GatewayProvider);
164 const all = yield* provider.list();
165
166 expect(all.some((c) => c.providerConfigId === deployed.providerConfigId)).toBe(true);
167 const found = all.find((c) => c.providerConfigId === deployed.providerConfigId)!;
168 expect(found.gatewayId).toEqual(LIST_GATEWAY_ID);
169 expect(found.providerSlug).toEqual(PROVIDER_SLUG);
170 expect(found.alias).toEqual(ALIAS);
171
172 yield* stack.destroy();
173 }).pipe(logLevel),
174 {
175 tags: [
176 "provider:cloudflare",
177 "provider:cloudflare:ai",
178 "provider:cloudflare:secretsstore",
179 "live",
180 ],
181 },
182 );