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

main
ts 144 lines 4.83 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-assess-conv";
17 const conversationNameOf = (project: string) =>
18 `projects/${project}/locations/us-central1/conversations/${conversationId}`;
19
20 const waitUntilGone = (name: string) =>
21 cci.getProjectsLocationsConversationsAssessments({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("1 second"),
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 "getProjectsLocationsConversationsAssessments on a missing assessment 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.getProjectsLocationsConversationsAssessments({
77 name: `${conversationName}/assessments/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, update, and delete a conversation assessment",
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.ConversationsAssessment("QA", {
102 parent: conversation.name ?? conversationName,
103 agentInfo: {
104 agentId: "agent-1",
105 displayName: "Ada",
106 agentType: "HUMAN_AGENT",
107 },
108 });
109 }),
110 );
111
112 expect(created.name).toContain("/assessments/");
113 expect(created.agentInfo?.displayName).toEqual("Ada");
114
115 const fetched = yield* cci.getProjectsLocationsConversationsAssessments({
116 name: created.name,
117 });
118 expect(fetched.name).toEqual(created.name);
119 expect(fetched.agentInfo?.displayName).toContain("alchemy-id=");
120
121 const updated = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* GCP.ContactCenterInsights.ConversationsAssessment("QA", {
124 parent: conversation.name ?? conversationName,
125 agentInfo: {
126 agentId: "agent-1",
127 displayName: "Bob",
128 agentType: "HUMAN_AGENT",
129 },
130 });
131 }),
132 );
133 expect(updated.agentInfo?.displayName).toEqual("Bob");
134
135 yield* stack.destroy();
136 const gone = yield* waitUntilGone(created.name);
137 expect(gone).toEqual("gone");
138 yield* deleteConversation(project);
139 }).pipe(logLevel),
140 {
141 tags: ["provider:gcp", "provider:gcp:contactcenterinsights", "live"],
142 timeout: 120_000,
143 },
144 );