@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 168 lines 5.92 KB
Raw
1 import { resolveProviderProbeTimeoutMs } from "@t3tools/provider-core/server/snapshotProbe";
2 import { ServerProviderModel } from "@t3tools/contracts";
3 import { MuseSettings } from "../settings.ts";
4 import { resolveSpawnCommand } from "@t3tools/shared/shell";
5 import * as DateTime from "effect/DateTime";
6 import * as Effect from "effect/Effect";
7 import * as Option from "effect/Option";
8 import * as Result from "effect/Result";
9 import * as ChildProcess from "effect/process/ChildProcess";
10
11 import { createMuseSdkHost, makeMuseEnvironment } from "./sdk.ts";
12 import { parseMuseVersion } from "./maintenance.ts";
13 import {
14 discoverMuseModels,
15 museModelCapabilities,
16 resolveMuseStartupTimeoutMs,
17 } from "./modelCatalog.ts";
18 import {
19 buildServerProvider,
20 COMPACT_SLASH_COMMAND,
21 DEFAULT_TIMEOUT_MS,
22 isCommandMissingCause,
23 parseCliVersionResult,
24 providerModelsFromSettings,
25 providerProbeEnvironment,
26 spawnAndCollect,
27 type ProviderProbeResult,
28 } from "@t3tools/provider-core/server/snapshotProbe";
29
30 const MUSE_PRESENTATION = {
31 displayName: "Muse Code",
32 showInteractionModeToggle: false,
33 reportsContextWindow: true,
34 // Muse has no accept-edits mode; map only :ask-me and :unrestricted.
35 supportedRuntimeModes: ["approval-required", "full-access"],
36 } as const;
37
38 const FALLBACK_CAPABILITIES = museModelCapabilities();
39
40 export const makePendingMuseProvider = Effect.fn("makePendingMuseProvider")(function* (
41 settings: MuseSettings,
42 ) {
43 return buildServerProvider({
44 presentation: MUSE_PRESENTATION,
45 enabled: settings.enabled,
46 checkedAt: DateTime.formatIso(yield* DateTime.now),
47 models: providerModelsFromSettings([], settings.customModels, FALLBACK_CAPABILITIES),
48 slashCommands: settings.enabled ? [COMPACT_SLASH_COMMAND] : [],
49 probe: {
50 installed: false,
51 version: null,
52 status: "warning",
53 auth: { status: "unknown" },
54 message: settings.enabled
55 ? "Checking Muse Code CLI availability..."
56 : "Muse Code is disabled in T3 Code settings.",
57 },
58 });
59 });
60
61 export const checkMuseProviderStatus = Effect.fn("checkMuseProviderStatus")(function* (
62 settings: MuseSettings,
63 environment?: NodeJS.ProcessEnv,
64 cwd?: string,
65 createHost: typeof createMuseSdkHost = createMuseSdkHost,
66 ) {
67 if (!settings.enabled) return yield* makePendingMuseProvider(settings);
68 const checkedAt = DateTime.formatIso(yield* DateTime.now);
69 const museEnvironment = providerProbeEnvironment(environment ?? makeMuseEnvironment());
70 const snapshot = (probe: ProviderProbeResult, models: ReadonlyArray<ServerProviderModel> = []) =>
71 buildServerProvider({
72 presentation: MUSE_PRESENTATION,
73 enabled: true,
74 checkedAt,
75 models: providerModelsFromSettings(models, settings.customModels, FALLBACK_CAPABILITIES),
76 slashCommands: [COMPACT_SLASH_COMMAND],
77 probe,
78 });
79 const versionResult = yield* Effect.gen(function* () {
80 const spawnCommand = yield* resolveSpawnCommand(settings.binaryPath, ["--version"], {
81 env: museEnvironment,
82 });
83 return yield* spawnAndCollect(
84 settings.binaryPath,
85 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
86 env: museEnvironment,
87 shell: spawnCommand.shell,
88 ...(cwd ? { cwd } : {}),
89 }),
90 );
91 }).pipe(
92 Effect.timeoutOption(
93 resolveProviderProbeTimeoutMs("muse", DEFAULT_TIMEOUT_MS, museEnvironment),
94 ),
95 Effect.result,
96 );
97
98 if (Result.isFailure(versionResult)) {
99 const missing = isCommandMissingCause(versionResult.failure);
100 return snapshot({
101 installed: !missing,
102 version: null,
103 status: "error",
104 auth: { status: "unknown" },
105 message: missing
106 ? "Muse Code CLI (`muse`) was not found. Install Muse Code and run `muse login` on this T3 server host."
107 : "Failed to execute Muse Code CLI. Check its binary path on this T3 server host.",
108 });
109 }
110 if (Option.isNone(versionResult.success)) {
111 return snapshot({
112 installed: true,
113 version: null,
114 status: "error",
115 auth: { status: "unknown" },
116 message: "Muse Code CLI version check timed out.",
117 });
118 }
119 const versionOutput = versionResult.success.value;
120 const version = parseCliVersionResult(versionOutput, parseMuseVersion);
121 if (versionOutput.code !== 0) {
122 return snapshot({
123 installed: true,
124 version,
125 status: "error",
126 auth: { status: "unknown" },
127 // T3 runs Muse with MUSE_NO_AUTO_UPDATE=1, so a launcher whose runtime was never fetched
128 // (common after a mise upgrade) refuses to install it; a manual run completes setup.
129 message: versionOutput.stderr.includes("installed binary is missing")
130 ? "Muse Code has not finished installing. Run `muse --version` on this T3 server host, then refresh its status."
131 : "Muse Code CLI is installed but failed to run.",
132 });
133 }
134
135 const catalog = yield* discoverMuseModels(settings, museEnvironment, cwd, createHost).pipe(
136 Effect.scoped,
137 // Startup shares the override, so the catalog keeps its 4 seconds on top of it.
138 Effect.timeoutOption(resolveMuseStartupTimeoutMs(museEnvironment) + 4_000),
139 Effect.result,
140 );
141 if (Result.isFailure(catalog) || Option.isNone(catalog.success)) {
142 return snapshot({
143 installed: true,
144 version,
145 status: "error",
146 auth: { status: "unknown" },
147 message:
148 "Muse Code SDK could not read the model catalog. Check your Muse installation and run `muse login` on this T3 server host.",
149 });
150 }
151 const models = catalog.success.value;
152 // MSP has no auth signal and serves cached models while logged out; failures surface on first turn.
153 return snapshot(
154 {
155 installed: true,
156 version,
157 auth: { status: "unknown" },
158 ...(models.length > 0
159 ? { status: "ready" }
160 : {
161 status: "warning",
162 message:
163 "Muse Code returned no models. Run `muse login` on this T3 server host and refresh its status.",
164 }),
165 },
166 models,
167 );
168 });