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

main
ts 130 lines 4.11 KB
Raw
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 dialogflow from "@distilled.cloud/gcp/dialogflow_v3";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { deleteAgent, ensureAgent, quotaTolerant } from "./parent.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const runLifecycle = !process.env.FAST;
16 const agentDisplayName = "alch-df-tool";
17
18 const waitUntilGone = (name: string) =>
19 dialogflow.getProjectsLocationsAgentsTools({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsAgentsTools on a missing tool fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 dialogflow.getProjectsLocationsAgentsTools({
38 name: `projects/${project}/locations/global/agents/missing/tools/missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel, quotaTolerant),
45 {
46 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
47 timeout: 90_000,
48 },
49 );
50
51 test.provider.skipIf(!runLifecycle)(
52 "create, update, and delete a tool",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56 yield* stack.destroy();
57
58 const agent = yield* ensureAgent(project, agentDisplayName);
59 const agentName = agent.name ?? "";
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Dialogflow.AgentsTool("Lookup", {
64 agent: agentName,
65 displayName: "lookup",
66 description: "Look up an order.",
67 functionSpec: {
68 inputSchema: {
69 type: "object",
70 properties: { orderId: { type: "string" } },
71 },
72 outputSchema: {
73 type: "object",
74 properties: { status: { type: "string" } },
75 },
76 },
77 });
78 }),
79 );
80
81 expect(created.name).toContain("/tools/");
82 expect(created.agent).toEqual(agentName);
83 expect(created.displayName).toEqual("lookup");
84 expect(created.description).toEqual("Look up an order.");
85 expect(created.functionSpec).toBeDefined();
86
87 const fetched = yield* dialogflow.getProjectsLocationsAgentsTools({
88 name: created.name,
89 });
90 expect(fetched.name).toEqual(created.name);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.Dialogflow.AgentsTool("Lookup", {
95 agent: agentName,
96 toolId: created.toolId,
97 displayName: "lookup",
98 description: "Look up an order by id.",
99 functionSpec: {
100 inputSchema: {
101 type: "object",
102 properties: {
103 orderId: { type: "string" },
104 includeHistory: { type: "boolean" },
105 },
106 },
107 outputSchema: {
108 type: "object",
109 properties: { status: { type: "string" } },
110 },
111 },
112 });
113 }),
114 );
115
116 expect(updated.name).toEqual(created.name);
117 expect(updated.description).toEqual("Look up an order by id.");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(created.name);
122 expect(gone).toEqual("gone");
123
124 yield* deleteAgent(agentName);
125 }).pipe(logLevel, quotaTolerant),
126 {
127 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
128 timeout: 120_000,
129 },
130 );