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

main
ts 82 lines 3.31 KB
Raw
1 import * as Cache from "effect/Cache";
2 import * as Context from "effect/Context";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Exit from "effect/Exit";
6 import * as Layer from "effect/Layer";
7 import * as Option from "effect/Option";
8
9 import * as HostProcess from "@t3tools/shared/HostProcess";
10 import { SpawnExecutableResolution } from "@t3tools/shared/shell";
11
12 import { createMuseSdkHostEffect, MuseSdkError } from "./sdk.ts";
13
14 export class MuseUsageHome extends Context.Service<
15 MuseUsageHome,
16 {
17 /** None while discovery is still running and `awaitRefresh` is false. */
18 readonly resolve: (input: {
19 readonly binaryPath: string;
20 readonly environment: NodeJS.ProcessEnv;
21 readonly awaitRefresh: boolean;
22 }) => Effect.Effect<Option.Option<string | undefined>>;
23 }
24 >()("@t3tools/provider-muse/server/MuseUsageHome") {}
25
26 const make = Effect.gen(function* () {
27 const scope = yield* Effect.scope;
28 const cache = yield* Cache.makeWith(
29 Effect.fn("MuseUsageHome.initialize")(function* (input: {
30 readonly binaryPath: string;
31 readonly environment: NodeJS.ProcessEnv;
32 }) {
33 return yield* Effect.scoped(
34 Effect.gen(function* () {
35 const host = yield* Effect.acquireRelease(
36 createMuseSdkHostEffect({ ...input, readOnly: true, startupTimeoutMs: 5_000 }),
37 (host) =>
38 Effect.tryPromise({
39 try: () => host.close(),
40 catch: (cause) => new MuseSdkError({ operation: "close", cause }),
41 }).pipe(Effect.ignore),
42 { interruptible: true },
43 );
44 return host.initializeResult.museHome;
45 }),
46 ).pipe(Effect.orElseSucceed(() => undefined));
47 }),
48 {
49 capacity: 32,
50 // Retry unavailable installations, but keep resolved homes across scans.
51 timeToLive: (exit) =>
52 Exit.isSuccess(exit) && exit.value ? Duration.infinity : Duration.seconds(30),
53 },
54 );
55 const resolve: MuseUsageHome["Service"]["resolve"] = Effect.fn("MuseUsageHome.resolve")(
56 function* ({ binaryPath, environment, awaitRefresh }) {
57 // Cache keys compare structurally. Omitted and undefined variables both
58 // disappear from the child process environment and must share an entry.
59 const key = {
60 binaryPath,
61 environment: Object.fromEntries(
62 Object.entries(environment).filter(([, value]) => value !== undefined),
63 ),
64 };
65 const settled = yield* Cache.getSuccess(cache, key);
66 if (Option.isSome(settled)) return settled;
67 // Spawn failures surface asynchronously; without this a missing CLI would report as pending.
68 const resolveExecutable = yield* SpawnExecutableResolution;
69 if (resolveExecutable(binaryPath, yield* HostProcess.Platform, environment) === undefined) {
70 return Option.some(undefined);
71 }
72 if (awaitRefresh) return Option.some(yield* Cache.get(cache, key));
73 // Awaited in the layer scope so a departing caller cannot cancel discovery.
74 yield* Effect.forkIn(Cache.get(cache, key), scope, { startImmediately: true });
75 // A lookup that settled synchronously needs no refresh.
76 return yield* Cache.getSuccess(cache, key);
77 },
78 );
79 return MuseUsageHome.of({ resolve });
80 });
81
82 export const layer = Layer.effect(MuseUsageHome, make);