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

main
ts 87 lines 2.62 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import * as Output from "alchemy/Output";
4 import * as Effect from "effect/Effect";
5 import * as Redacted from "effect/Redacted";
6
7 export const CHAT_TRANSCRIPT = JSON.stringify({
8 entries: [
9 {
10 start_timestamp_usec: 1_000_000,
11 text: "Hello, how can I help you today?",
12 role: "AGENT",
13 user_id: 1,
14 },
15 {
16 start_timestamp_usec: 5_000_000,
17 text: "I want to check my billing.",
18 role: "CUSTOMER",
19 user_id: 2,
20 },
21 ],
22 });
23
24 export const uploadChatTranscript = (bucketName: string) =>
25 Effect.gen(function* () {
26 const env = yield* GcpEnvironment.current;
27 const token = Redacted.value(env.accessToken);
28 const project = env.project;
29 yield* Effect.tryPromise({
30 try: () =>
31 fetch(
32 `https://storage.googleapis.com/storage/v1/b?project=${encodeURIComponent(project)}`,
33 {
34 method: "POST",
35 headers: {
36 Authorization: `Bearer ${token}`,
37 "Content-Type": "application/json",
38 },
39 body: JSON.stringify({
40 name: bucketName,
41 location: "US-CENTRAL1",
42 }),
43 },
44 ),
45 catch: (error) => new Error(`transcript bucket create failed: ${String(error)}`),
46 });
47 const response = yield* Effect.tryPromise({
48 try: () =>
49 fetch(
50 `https://storage.googleapis.com/upload/storage/v1/b/${bucketName}/o?uploadType=media&name=transcript.json`,
51 {
52 method: "POST",
53 headers: {
54 Authorization: `Bearer ${token}`,
55 "Content-Type": "application/json",
56 },
57 body: CHAT_TRANSCRIPT,
58 },
59 ),
60 catch: (error) => new Error(`transcript upload failed: ${String(error)}`),
61 });
62 if (!response.ok) {
63 return yield* Effect.die(new Error(`transcript upload failed with HTTP ${response.status}`));
64 }
65 });
66
67 /**
68 * A bucket holding {@link CHAT_TRANSCRIPT}, declared as stack resources.
69 * Conversations must name a GCS transcript as their data source.
70 */
71 export const ChatTranscript = Effect.gen(function* () {
72 const bucket = yield* GCP.Storage.Bucket("Transcripts", {
73 location: "US-CENTRAL1",
74 forceDestroy: true,
75 });
76 const object = yield* GCP.Storage.Object("Transcript", {
77 bucketName: bucket.bucketName,
78 key: "transcript.json",
79 content: CHAT_TRANSCRIPT,
80 contentType: "application/json",
81 });
82 return {
83 gcsSource: {
84 transcriptUri: Output.interpolate`gs://${bucket.bucketName}/${object.key}`,
85 },
86 };
87 });