Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sdk.ts

245 lines9.6 KB

1// @effect-diagnostics globalTimers:off
2// The SDK owns process shutdown; native deadlines cover initialize before an Effect resource exists.
3// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.realPath is Node's JS realpath; museWorkspaceRoot needs the native one.
4import * as NodeFSP from "node:fs/promises";
5​
6import {
7 spawnMspConnection,
8 type Connection,
9 type ProcessExit,
10 type SpawnedMspConnection,
11} from "@muse-code/sdk";
12import type { RuntimeMode } from "@t3tools/contracts";
13import * as HostProcess from "@t3tools/shared/HostProcess";
14import { SpawnExecutableResolution } from "@t3tools/shared/shell";
15import * as Effect from "effect/Effect";
16​
17export interface MuseSdkHost {
18 readonly connection: Pick<
19 Connection,
20 | "command"
21 | "request"
22 | "mintCommandId"
23 | "onNotification"
24 | "onServerRequest"
25 | "onProtocolError"
26 | "closed"
27 >;
28 readonly initializeResult: Pick<SpawnedMspConnection["initializeResult"], "grantedCapabilities">;
29 readonly exited: Promise<ProcessExit>;
30 /** The last lines Muse wrote to stderr, for explaining an unexpected exit. */
31 readonly stderrTail?: () => ReadonlyArray<string>;
32 readonly close: () => Promise<void>;
34​
35/** The program that runs `binaryPath`, and the arguments that go before the binary's own. */
36export interface MuseLaunch {
37 readonly command: string;
38 readonly args: ReadonlyArray<string>;
40​
41export interface MuseSdkHostOptions {
42 readonly binaryPath: string;
43 /** Arguments before the serve arguments, for a launcher in front of the binary. */
44 readonly launchArgs?: ReadonlyArray<string>;
45 /** How to start `binaryPath`, from `museLaunch`; without it the binary path is run as is. */
46 readonly launch?: MuseLaunch;
47 readonly cwd?: string;
48 readonly environment?: NodeJS.ProcessEnv;
49 readonly runtimeMode?: RuntimeMode;
50 /** Disables shell and filesystem writes and workspace trust. */
51 readonly readOnly?: boolean;
52 /** Read-only generation needs durable logging for turn/item notifications (verified through 1.1.1). */
53 readonly sessionLogging?: boolean;
54 readonly signal?: AbortSignal;
55 readonly startupTimeoutMs?: number;
57​
58/** Preserve the host's CLI login while excluding the API-key override before copying values. */
59export function makeMuseEnvironment(
60 environment: NodeJS.ProcessEnv = process.env,
61): NodeJS.ProcessEnv {
62 return {
63 ...Object.fromEntries(
64 Object.keys(environment)
65 .filter((key) => key.toUpperCase() !== "META_API_KEY")
66 .map((key) => [key, environment[key]]),
67 ),
68 MUSE_NO_AUTO_UPDATE: "1",
69 };
71​
72/** Windows' verbatim form of a canonical path: `\\?\C:\…`, or `\\?\UNC\server\share\…`. */
73export function museVerbatimPath(path: string): string {
74 if (path.startsWith("\\\\?\\")) return path;
75 return path.startsWith("\\\\") ? `\\\\?\\UNC\\${path.slice(2)}` : `\\\\?\\${path}`;
77​
78/**
79 * A workspace root in the form `turn/start` accepts. On Windows Muse rejects
80 * anything but the verbatim canonical path, true case and long names
81 * included, so resolve with the native realpath, which unlike
82 * FileSystem.realPath fixes case and expands 8.3 names. Muse itself still
83 * starts in the plain path: the `muse.cmd` launcher runs under cmd.exe, which
84 * cannot use a verbatim directory.
85 */
86export const museWorkspaceRoot = Effect.fn("museWorkspaceRoot")(function* (path: string) {
87 if ((yield* HostProcess.Platform) !== "win32") return path;
88 const canonical = yield* Effect.tryPromise(() => NodeFSP.realpath(path)).pipe(
89 // A verbatim path takes `/` literally, so an unresolved root needs Windows separators.
90 Effect.orElseSucceed(() => path.replaceAll("/", "\\")),
91 );
92 return museVerbatimPath(canonical);
93});
94​
95/** A Windows environment variable; their names are case-insensitive there. */
96function windowsEnvironmentValue(environment: NodeJS.ProcessEnv, name: string) {
97 const key = Object.keys(environment).find(
98 (candidate) => candidate.toUpperCase() === name.toUpperCase(),
99 );
100 return key === undefined ? undefined : environment[key];
102​
103/**
104 * How to start Muse. The SDK spawns without a shell, which finds the `muse`
105 * launcher on PATH everywhere but Windows. There Node neither looks a bare
106 * `muse` up with PATHEXT (ENOENT) nor runs a `.cmd` file such as Muse's own
107 * launcher (EINVAL) without a shell, so resolve the binary like any other
108 * command and run a `.cmd` or `.bat` under cmd.exe. Node quotes a path with
109 * spaces, and inside those quotes cmd takes every character literally. A path
110 * without spaces is not quoted, so its metacharacters are escaped instead.
111 */
112export const museLaunch = Effect.fn("museLaunch")(function* (
113 binaryPath: string,
114 environment?: NodeJS.ProcessEnv,
115): Effect.fn.Return<MuseLaunch> {
116 if ((yield* HostProcess.Platform) !== "win32") return { command: binaryPath, args: [] };
117 const env = environment ?? (yield* HostProcess.Environment);
118 const resolveExecutable = yield* SpawnExecutableResolution;
119 const resolved = resolveExecutable(binaryPath, "win32", env) ?? binaryPath;
120 if (!/\.(?:cmd|bat)$/i.test(resolved)) return { command: resolved, args: [] };
121 const systemRoot = windowsEnvironmentValue(env, "SystemRoot");
122 return {
123 command:
124 windowsEnvironmentValue(env, "ComSpec") ??
125 (systemRoot ? `${systemRoot}\\System32\\cmd.exe` : "cmd.exe"),
126 // /d skips cmd's AutoRun commands, which could write into Muse's stdout. cmd /c drops
127 // the quotes from a command that starts with one and holds `&`, `@`, `^` or a
128 // parenthesis, so a quoted path follows a bare `@` (echo off), which keeps them.
129 args: [
130 "/d",
131 "/c",
132 ...(/\s/.test(resolved) ? ["@", resolved] : [resolved.replace(/[()&<>@^|]/g, "^$&")]),
133 ],
134 };
135});
136​
137export function museApprovalMode(runtimeMode: RuntimeMode) {
138 return runtimeMode === "full-access" ? "allowAll" : "promptUnmatched";
140​
141/** The `muse serve` arguments for a host: read-only hosts get no shell, writes, or workspace trust. */
142export function museServeArgs(
143 options: Pick<MuseSdkHostOptions, "readOnly" | "sessionLogging" | "runtimeMode">,
144) {
145 const args = ["serve"];
146 if (options.readOnly) {
147 args.push("--disable-shell", "--disable-write");
148 if (options.sessionLogging !== true) args.push("--no-session-log");
149 } else {
150 args.push("--trust-workspace");
151 if (options.runtimeMode === "full-access") args.push("--disable-sandbox");
152 }
153 return args;
155​
156/** What T3 sends in MSP `initialize`; only full hosts ask for session MCP servers. */
157export function museInitializeParams(readOnly = false) {
158 return {
159 clientInfo: { name: "t3_code", title: "T3 Code", version: "1" },
160 capabilities: { requestedCapabilities: readOnly ? [] : ["sessionMcp"] },
161 };
163​
164/** One SDK-owned process. The pre-handshake handle owns cleanup even when initialize never replies. */
165export async function createMuseSdkHost(
166 options: MuseSdkHostOptions,
167 spawn: typeof spawnMspConnection = spawnMspConnection,
168): Promise<MuseSdkHost> {
169 options.signal?.throwIfAborted();
170 const launch = options.launch ?? { command: options.binaryPath, args: [] };
171 const handshake = spawn({
172 command: launch.command,
173 args: [...launch.args, ...(options.launchArgs ?? []), ...museServeArgs(options)],
174 ...(options.cwd ? { cwd: options.cwd } : {}),
175 // Callers pass an environment already built with makeMuseEnvironment.
176 env: options.environment ?? makeMuseEnvironment(),
177 // A healthy host exits at once; this only bounds a hung one on close.
178 shutdownTimeoutMs: 10_000,
179 });
180 let closePromise: Promise<void> | undefined;
181 const close = () => {
182 options.signal?.removeEventListener("abort", onAbort);
183 return (closePromise ??= handshake.close().then(() => undefined));
184 };
185 let rejectStartup: (reason: unknown) => void = () => {};
186 const onAbort = () => {
187 rejectStartup(options.signal?.reason ?? new Error("Muse SDK startup aborted."));
188 void close().catch(() => {});
189 };
190 const interrupted = new Promise<never>((_resolve, reject) => {
191 rejectStartup = reject;
192 });
193 options.signal?.addEventListener("abort", onAbort, { once: true });
194 if (options.signal?.aborted) onAbort();
195 const timer = setTimeout(
196 () => rejectStartup(new Error("Muse SDK initialization timed out.")),
197 options.startupTimeoutMs ?? 20_000,
198 );
199 timer.unref();
200 try {
201 const host = await Promise.race([
202 handshake.initialize(museInitializeParams(options.readOnly)),
203 interrupted,
204 ]);
205 if (host.initializeResult.schema?.version !== 1) {
206 throw new Error("Muse SDK returned an unsupported protocol envelope version.");
207 }
208 // The SDK's fingerprint warning permits additive optional schema changes.
209 // Successful initialization, not fingerprint identity, determines readiness.
210 return {
211 connection: host.connection,
212 initializeResult: host.initializeResult,
213 exited: host.exited,
214 stderrTail: () => handshake.child.stderrTail,
215 close,
216 };
217 } catch (error) {
218 await close();
219 throw error;
220 } finally {
221 clearTimeout(timer);
222 }
224​
225/** Cancellation waits for native shutdown before callers release startup resources. */
226export const createMuseSdkHostEffect = Effect.fn("createMuseSdkHostEffect")(function* (
227 options: Omit<MuseSdkHostOptions, "signal">,
228 createHost: typeof createMuseSdkHost = createMuseSdkHost,
229) {
230 const launch = options.launch ?? (yield* museLaunch(options.binaryPath, options.environment));
231 let startup: Promise<MuseSdkHost> | undefined;
232 return yield* Effect.tryPromise((signal) => {
233 startup = createHost({ ...options, launch, signal });
234 return startup;
235 }).pipe(
236 Effect.onInterrupt(() =>
237 Effect.promise(async () => {
238 await startup?.then(
239 (host) => host.close(),
240 () => {},
241 );
242 }),
243 ),
244 );
245});