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

main
ts 83 lines 2.68 KB
Raw
1 import { unwrapRpcHandlers } from "alchemy/Local/RpcSerialization";
2 import type { RpcProxyApi } from "alchemy/Local/RpcServer";
3 import { encodeSessionEnvironment, SESSION_ENV_PARAM } from "alchemy/Local/RpcServerEnvironment";
4 import { layerServer, RpcSpawner } from "alchemy/Local/RpcSpawner";
5 import { PlatformServices, runMain } from "alchemy/Util/PlatformServices";
6 import { newWebSocketRpcSession } from "capnweb";
7 import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as FetchHttpClient from "effect/http/FetchHttpClient";
10 import * as HttpBody from "effect/http/HttpBody";
11 import * as HttpClient from "effect/http/HttpClient";
12 import * as Layer from "effect/Layer";
13
14 const program = Effect.gen(function* () {
15 const fs = yield* FileSystem.FileSystem;
16 const input = yield* Effect.sync(
17 () =>
18 JSON.parse(process.env.COMMAND_FIXTURES!) as {
19 home: string;
20 ready: string;
21 commands: Array<{ command: string; env: Record<string, string> }>;
22 },
23 );
24 const spawner = yield* RpcSpawner;
25 const http = yield* HttpClient.HttpClient;
26 const wsUrl = yield* http
27 .post(spawner.url, {
28 body: yield* HttpBody.json({
29 serverEntryUrl: new URL("../../../src/Local/Sidecar.ts", import.meta.url).href,
30 }),
31 })
32 .pipe(Effect.flatMap((response) => response.text));
33 const url = new URL(wsUrl);
34 url.searchParams.set(
35 SESSION_ENV_PARAM,
36 encodeSessionEnvironment({
37 alchemyContext: {
38 dotAlchemy: input.home,
39 updateStateStore: false,
40 dev: true,
41 adopt: false,
42 },
43 stack: { name: "CommandShutdown", stage: "test" },
44 }),
45 );
46 const session = yield* Effect.sync(() => newWebSocketRpcSession<RpcProxyApi>(url.toString()));
47 const wrapped = yield* Effect.promise(
48 () =>
49 session.getProvider(
50 "Command.Dev",
51 new URL("../../../src/Command/Local.ts", import.meta.url).href,
52 ) as ReturnType<RpcProxyApi["getProvider"]>,
53 );
54 const provider = unwrapRpcHandlers(wrapped, []);
55 yield* Effect.forEach(
56 input.commands,
57 (news, index) =>
58 provider.reconcile({
59 id: `Command${index}`,
60 fqn: `Command${index}`,
61 instanceId: `Command${index}`,
62 news,
63 olds: undefined,
64 output: undefined,
65 session: {} as never,
66 bindings: [],
67 }),
68 { concurrency: "unbounded" },
69 );
70 yield* fs.writeFileString(input.ready, "ready");
71 yield* Effect.never;
72 });
73 program.pipe(
74 Effect.provide(
75 Layer.mergeAll(
76 layerServer({ profile: undefined, envFile: undefined }).pipe(Layer.provide(PlatformServices)),
77 PlatformServices,
78 FetchHttpClient.layer,
79 ),
80 ),
81 Effect.scoped,
82 runMain,
83 );