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

main
ts 124 lines 4.14 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 cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { uploadChatTranscript } from "./transcript.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 transcriptBucketOf = (project: string) => `alchemy-cci-transcripts-${project}`;
16 const conversationId = "alchemy-cci-analyses-conv";
17 const conversationNameOf = (project: string) =>
18 `projects/${project}/locations/us-central1/conversations/${conversationId}`;
19
20 const waitUntilGone = (name: string) =>
21 cci.getProjectsLocationsConversationsAnalyses({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31 const ensureConversation = (project: string) =>
32 Effect.gen(function* () {
33 const transcriptBucket = transcriptBucketOf(project);
34 const conversationName = conversationNameOf(project);
35 yield* uploadChatTranscript(transcriptBucket);
36 const existing = yield* cci
37 .getProjectsLocationsConversations({
38 name: conversationName,
39 view: "BASIC",
40 })
41 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
42 if (existing !== undefined) return existing;
43 return yield* cci.createProjectsLocationsConversations({
44 parent: `projects/${project}/locations/us-central1`,
45 conversationId,
46 body: {
47 medium: "CHAT",
48 languageCode: "en-US",
49 labels: { "alchemy-test": "cci" },
50 dataSource: {
51 gcsSource: {
52 transcriptUri: `gs://${transcriptBucket}/transcript.json`,
53 },
54 },
55 },
56 });
57 });
58
59 const deleteConversation = (project: string) =>
60 cci
61 .deleteProjectsLocationsConversations({
62 name: conversationNameOf(project),
63 force: true,
64 })
65 .pipe(Effect.catchTag("NotFound", () => Effect.void));
66
67 test.provider(
68 "getProjectsLocationsConversationsAnalyses on a missing analysis fails with a typed tag",
69 (stack) =>
70 Effect.gen(function* () {
71 const { project } = yield* GcpEnvironment.current;
72 const conversationName = conversationNameOf(project);
73 yield* stack.destroy();
74
75 const error = yield* Effect.flip(
76 cci.getProjectsLocationsConversationsAnalyses({
77 name: `${conversationName}/analyses/missing`,
78 }),
79 );
80 expect(error._tag).toEqual("NotFound");
81
82 yield* stack.destroy();
83 }).pipe(logLevel),
84 {
85 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
86 timeout: 90_000,
87 },
88 );
89
90 test.provider.skipIf(!!process.env.FAST)(
91 "create and delete a conversation analysis",
92 (stack) =>
93 Effect.gen(function* () {
94 const { project } = yield* GcpEnvironment.current;
95 const conversationName = conversationNameOf(project);
96 yield* stack.destroy();
97 const conversation = yield* ensureConversation(project);
98
99 const created = yield* stack.deploy(
100 Effect.gen(function* () {
101 return yield* GCP.ContactCenterInsights.ConversationsAnalyses("Silence", {
102 parent: conversation.name ?? conversationName,
103 annotatorSelector: { runSilenceAnnotator: true },
104 });
105 }),
106 );
107
108 expect(created.name).toContain("/analyses/");
109
110 const fetched = yield* cci.getProjectsLocationsConversationsAnalyses({
111 name: created.name,
112 });
113 expect(fetched.name).toEqual(created.name);
114
115 yield* stack.destroy();
116 const gone = yield* waitUntilGone(created.name);
117 expect(gone).toEqual("gone");
118 yield* deleteConversation(project);
119 }).pipe(logLevel),
120 {
121 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
122 timeout: 120_000,
123 },
124 );