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

main
ts 85 lines 3.42 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Cause from "effect/Cause";
3 import * as Effect from "effect/Effect";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Stream from "effect/Stream";
6 import { TaskApi, TaskRpcs } from "./schema.ts";
7 import { ApiBackend, ApiSidecar, RpcBackend, RpcSidecar } from "./targets.ts";
8
9 /**
10 * Public service that calls each target through a typed client: Effect
11 * RPC and HttpApi, over a sidecar and over a private service.
12 */
13 export default class EffectApiCaller extends GCP.Run.Service<EffectApiCaller>()(
14 "EffectApiCaller",
15 { main: import.meta.url, location: "us-central1", invokerIamDisabled: true },
16 Effect.gen(function* () {
17 const rpcSidecar = yield* GCP.Run.bindContainer(RpcSidecar, { schema: TaskRpcs });
18 const apiSidecar = yield* GCP.Run.bindContainer(ApiSidecar, { api: TaskApi });
19 const rpcService = yield* GCP.Run.InvokeService(RpcBackend, { schema: TaskRpcs });
20 const apiService = yield* GCP.Run.InvokeService(ApiBackend, { api: TaskApi });
21
22 const missingTag = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) =>
23 effect.pipe(
24 Effect.map(() => "found"),
25 Effect.catch((error) => Effect.succeed(error._tag)),
26 );
27
28 return {
29 fetch: Effect.gen(function* () {
30 // Each call reports its own failure, so a broken transport names itself.
31 const report = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
32 effect.pipe(
33 Effect.catchCause((cause) =>
34 Effect.succeed({
35 failed: Cause.pretty(cause),
36 detail: JSON.stringify(Cause.squash(cause)),
37 }),
38 ),
39 );
40 return yield* HttpServerResponse.json({
41 rpcSidecar: yield* report(
42 Effect.all({
43 task: rpcSidecar.getTask({ id: "t1" }).pipe(Effect.map((task) => task.title)),
44 missing: missingTag(rpcSidecar.getTask({ id: "nope" })),
45 counts: rpcSidecar.countTasks({ to: 3 }).pipe(
46 Stream.runCollect,
47 Effect.map((counts) => [...counts]),
48 ),
49 }),
50 ),
51 apiSidecar: yield* report(
52 Effect.all({
53 task: apiSidecar.Tasks.getTask({ params: { id: "t1" } }).pipe(
54 Effect.map((task) => task.title),
55 ),
56 missing: missingTag(apiSidecar.Tasks.getTask({ params: { id: "nope" } })),
57 }),
58 ),
59 rpcService: yield* report(
60 Effect.all({
61 task: rpcService.getTask({ id: "t1" }).pipe(Effect.map((task) => task.title)),
62 missing: missingTag(rpcService.getTask({ id: "nope" })),
63 counts: rpcService.countTasks({ to: 3 }).pipe(
64 Stream.runCollect,
65 Effect.map((counts) => [...counts]),
66 ),
67 }),
68 ),
69 apiService: yield* report(
70 Effect.all({
71 task: apiService.Tasks.getTask({ params: { id: "t1" } }).pipe(
72 Effect.map((task) => task.title),
73 ),
74 missing: missingTag(apiService.Tasks.getTask({ params: { id: "nope" } })),
75 }),
76 ),
77 });
78 }).pipe(
79 Effect.catchCause((cause) =>
80 Effect.succeed(HttpServerResponse.text(Cause.pretty(cause), { status: 500 })),
81 ),
82 ),
83 };
84 }).pipe(Effect.provide(GCP.Run.InvokeServiceHttp)),
85 ) {}