@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 153 lines 4.62 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 vectorize from "@distilled.cloud/cloudflare/vectorize";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 test.provider(
16 "create and delete index with explicit dimensions",
17 (stack) =>
18 Effect.gen(function* () {
19 const { accountId } = yield* yield* CloudflareEnvironment;
20
21 yield* stack.destroy();
22
23 const index = yield* stack.deploy(
24 Cloudflare.Vectorize.Index("DefaultIndex", {
25 dimensions: 768,
26 metric: "cosine",
27 }),
28 );
29
30 expect(index.indexName).toBeDefined();
31 expect(index.dimensions).toEqual(768);
32 expect(index.metric).toEqual("cosine");
33
34 const actual = yield* vectorize.getIndex({
35 accountId,
36 indexName: index.indexName,
37 });
38 expect(actual.name).toEqual(index.indexName);
39 expect(actual.config?.dimensions).toEqual(768);
40
41 yield* stack.destroy();
42
43 yield* waitForDelete(accountId, index.indexName);
44 }).pipe(logLevel),
45 { tags: ["provider:cloudflare", "provider:cloudflare:vectorize", "live"] },
46 );
47
48 test.provider(
49 "create index from a preset",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* yield* CloudflareEnvironment;
53
54 yield* stack.destroy();
55
56 const index = yield* stack.deploy(
57 Cloudflare.Vectorize.Index("PresetIndex", {
58 preset: "@cf/baai/bge-base-en-v1.5",
59 description: "preset index",
60 }),
61 );
62
63 const actual = yield* vectorize.getIndex({
64 accountId,
65 indexName: index.indexName,
66 });
67 // bge-base resolves to 768 dimensions.
68 expect(actual.config?.dimensions).toEqual(768);
69 expect(index.description).toEqual("preset index");
70
71 yield* stack.destroy();
72
73 yield* waitForDelete(accountId, index.indexName);
74 }).pipe(logLevel),
75 { tags: ["provider:cloudflare", "provider:cloudflare:vectorize", "live"] },
76 );
77
78 test.provider(
79 "replaces index when dimensions change",
80 (stack) =>
81 Effect.gen(function* () {
82 const { accountId } = yield* yield* CloudflareEnvironment;
83
84 yield* stack.destroy();
85
86 const index = yield* stack.deploy(
87 Cloudflare.Vectorize.Index("ReplaceIndex", {
88 dimensions: 32,
89 metric: "cosine",
90 }),
91 );
92 expect(index.dimensions).toEqual(32);
93 expect(index.metric).toEqual("cosine");
94
95 const replaced = yield* stack.deploy(
96 Cloudflare.Vectorize.Index("ReplaceIndex", {
97 dimensions: 64,
98 metric: "euclidean",
99 }),
100 );
101
102 const actual = yield* vectorize.getIndex({
103 accountId,
104 indexName: replaced.indexName,
105 });
106 expect(actual.config?.dimensions).toEqual(64);
107 expect(actual.config?.metric).toEqual("euclidean");
108
109 yield* stack.destroy();
110
111 yield* waitForDelete(accountId, replaced.indexName);
112 }).pipe(logLevel),
113 { tags: ["provider:cloudflare", "provider:cloudflare:vectorize", "live"] },
114 );
115
116 test.provider(
117 "list enumerates the deployed index",
118 (stack) =>
119 Effect.gen(function* () {
120 const { accountId } = yield* yield* CloudflareEnvironment;
121
122 yield* stack.destroy();
123
124 const index = yield* stack.deploy(
125 Cloudflare.Vectorize.Index("ListIndex", {
126 dimensions: 768,
127 metric: "cosine",
128 }),
129 );
130
131 const provider = yield* Provider.findProvider(Cloudflare.Vectorize.Index);
132 const all = yield* provider.list();
133
134 expect(all.some((x) => x.indexName === index.indexName)).toBe(true);
135
136 yield* stack.destroy();
137
138 yield* waitForDelete(accountId, index.indexName);
139 }).pipe(logLevel),
140 { tags: ["provider:cloudflare", "provider:cloudflare:vectorize", "live"] },
141 );
142
143 const waitForDelete = (accountId: string, indexName: string) =>
144 vectorize.getIndex({ accountId, indexName }).pipe(
145 Effect.flatMap((index) =>
146 index.name === indexName ? Effect.fail({ _tag: "IndexNotDeleted" } as const) : Effect.void,
147 ),
148 Effect.catchTag(["NotFound", "Gone"], () => Effect.void),
149 Effect.retry({
150 while: (e) => e._tag === "IndexNotDeleted",
151 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
152 }),
153 );