The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const waitUntilGone = (name: string) => |
| 15 | aiplatform.getProjectsLocationsCachedContents({ name }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("2 seconds"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | test.provider( |
| 26 | "getProjectsLocationsCachedContents on a missing cache fails with a typed tag", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const { project } = yield* GcpEnvironment.current; |
| 30 | const parent = `projects/${project}/locations/us-central1`; |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const error = yield* Effect.flip( |
| 34 | aiplatform.getProjectsLocationsCachedContents({ |
| 35 | name: `${parent}/cachedContents/alchemy-aiplatform-missing`, |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toEqual("NotFound"); |
| 39 | const page = yield* aiplatform.listProjectsLocationsCachedContents({ |
| 40 | parent, |
| 41 | pageSize: 10, |
| 42 | }); |
| 43 | expect((page.cachedContents ?? []).map((item) => item.name)).not.toContain( |
| 44 | `${parent}/cachedContents/alchemy-aiplatform-missing`, |
| 45 | ); |
| 46 | |
| 47 | yield* stack.destroy(); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 51 | timeout: 90_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | // Explicit caching needs at least 1024 tokens of content. |
| 56 | const styleGuide = Array.from( |
| 57 | { length: 400 }, |
| 58 | (_, i) => `Rule ${i + 1}: answer question ${i + 1} in at most ${i + 2} words.`, |
| 59 | ).join("\n"); |
| 60 | |
| 61 | test.provider( |
| 62 | "create, update, and delete cached content", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | const { project } = yield* GcpEnvironment.current; |
| 66 | const parent = `projects/${project}/locations/us-central1`; |
| 67 | const model = `${parent}/publishers/google/models/gemini-2.5-flash`; |
| 68 | yield* stack.destroy(); |
| 69 | |
| 70 | const created = yield* stack.deploy( |
| 71 | Effect.gen(function* () { |
| 72 | return yield* GCP.AIPlatform.CachedContent("Style", { |
| 73 | location: "us-central1", |
| 74 | model, |
| 75 | ttl: "3600s", |
| 76 | displayName: "style-cache", |
| 77 | contents: [ |
| 78 | { |
| 79 | role: "user", |
| 80 | parts: [{ text: styleGuide }], |
| 81 | }, |
| 82 | ], |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.name).toContain("/cachedContents/"); |
| 88 | expect(created.model).toContain("gemini"); |
| 89 | expect(created.displayName).toEqual("style-cache"); |
| 90 | |
| 91 | const fetched = yield* aiplatform.getProjectsLocationsCachedContents({ |
| 92 | name: created.name, |
| 93 | }); |
| 94 | expect(fetched.name).toEqual(created.name); |
| 95 | |
| 96 | const updated = yield* stack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* GCP.AIPlatform.CachedContent("Style", { |
| 99 | location: "us-central1", |
| 100 | model, |
| 101 | ttl: "7200s", |
| 102 | displayName: "style-cache", |
| 103 | contents: [ |
| 104 | { |
| 105 | role: "user", |
| 106 | parts: [{ text: styleGuide }], |
| 107 | }, |
| 108 | ], |
| 109 | }); |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | expect(updated.name).toEqual(created.name); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | |
| 117 | const gone = yield* waitUntilGone(created.name); |
| 118 | expect(gone).toEqual("gone"); |
| 119 | }).pipe(logLevel), |
| 120 | { |
| 121 | tags: ["provider:gcp", "provider:gcp:aiplatform", "live"], |
| 122 | timeout: 300_000, |
| 123 | }, |
| 124 | ); |