1import * as GCP from "@/GCP";
2import { GcpEnvironment } from "@/GCP/Environment";
3import * as Test from "@/Test/Alchemy";
4import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 aiplatform.getProjectsLocationsReasoningEnginesSessions({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsReasoningEnginesSessions on a missing session fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 aiplatform.getProjectsLocationsReasoningEnginesSessions({
37 name: `projects/${project}/locations/us-central1/reasoningEngines/1234567890123456789/sessions/1234567890123456789`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create, update, and delete a reasoning engine session",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const engine = Effect.gen(function* () {
57 return yield* GCP.AIPlatform.ReasoningEngine("Agent", {
58 location: "us-central1",
59 displayName: "alchemy-session-engine",
60 spec: { agentFramework: "custom" },
61 });
62 });
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const { name: parent } = yield* engine;
67 return yield* GCP.AIPlatform.ReasoningEnginesSession("Chat", {
68 parent,
69 userId: "alchemy-user",
70 displayName: "support-chat",
71 labels: { env: "test" },
72 });
73 }),
74 );
75
76 expect(created.name).toContain("/sessions/");
77 expect(created.userId).toEqual("alchemy-user");
78 expect(created.labels).toMatchObject({ env: "test" });
79
80 const fetched =
81 yield* aiplatform.getProjectsLocationsReasoningEnginesSessions({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 const { name: parent } = yield* engine;
89 return yield* GCP.AIPlatform.ReasoningEnginesSession("Chat", {
90 parent,
91 sessionId: created.sessionId,
92 userId: "alchemy-user",
93 displayName: "support-chat-v2",
94 labels: { env: "prod" },
95 });
96 }),
97 );
98 expect(updated.name).toEqual(created.name);
99 expect(updated.displayName).toEqual("support-chat-v2");
100 expect(updated.labels).toMatchObject({ env: "prod" });
101
102 yield* stack.destroy();
103 const gone = yield* waitUntilGone(created.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel),
106 {
107 tags: ["provider:gcp", "provider:gcp:aiplatform", "live"],
108 timeout: 300_000,
109 },
110);