Open History to see the last commit
1import type { MuseSettings, ServerProviderModel } from "@t3tools/contracts";
2import { resolveSpawnCommand } from "@t3tools/shared/shell";
3import * as DateTime from "effect/DateTime";
4import * as Effect from "effect/Effect";
5import * as Option from "effect/Option";
6import * as Result from "effect/Result";
7import { ChildProcess } from "effect/process";
8
9import { createMuseSdkHost, makeMuseEnvironment } from "./museSdk.ts";
10import { parseMuseVersion } from "./museMaintenance.ts";
11import { discoverMuseModels, museModelCapabilities } from "./museModelCatalog.ts";
12import {
13 buildServerProvider,
14 COMPACT_SLASH_COMMAND,
15 DEFAULT_TIMEOUT_MS,
16 isCommandMissingCause,
17 providerModelsFromSettings,
18 spawnAndCollect,
19 type ProviderProbeResult,
20} from "./providerSnapshot.ts";
21
22const MUSE_PRESENTATION = {
23 displayName: "Muse Code",
24 showInteractionModeToggle: false,
25 reportsContextWindow: true,
26 // Muse has no native "accept edits" or reviewer-backed mode, so T3 offers only
27 // the two it maps directly: promptUnmatched and allowAll.
28 supportedRuntimeModes: ["approval-required", "full-access"],
29} as const;
30
31const FALLBACK_CAPABILITIES = museModelCapabilities();
32
33export const makePendingMuseProvider = Effect.fn("makePendingMuseProvider")(function* (
34 settings: MuseSettings,
35) {
36 return buildServerProvider({
37 presentation: MUSE_PRESENTATION,
38 enabled: settings.enabled,
39 checkedAt: DateTime.formatIso(yield* DateTime.now),
40 models: providerModelsFromSettings([], settings.customModels, FALLBACK_CAPABILITIES),
41 slashCommands: settings.enabled ? [COMPACT_SLASH_COMMAND] : [],
42 probe: {
43 installed: false,
44 version: null,
45 status: "warning",
46 auth: { status: "unknown" },
47 message: settings.enabled
48 ? "Checking Muse Code CLI availability..."
49 : "Muse Code is disabled in T3 Code settings.",
50 },
51 });
52});
53
54export const checkMuseProviderStatus = Effect.fn("checkMuseProviderStatus")(function* (
55 settings: MuseSettings,
56 environment?: NodeJS.ProcessEnv,
57 cwd?: string,
58 createHost: typeof createMuseSdkHost = createMuseSdkHost,
59) {
60 if (!settings.enabled) return yield* makePendingMuseProvider(settings);
61 const checkedAt = DateTime.formatIso(yield* DateTime.now);
62 // The driver passes a prepared environment; direct callers get the host's without META_API_KEY.
63 const museEnvironment = environment ?? makeMuseEnvironment();
64 const snapshot = (probe: ProviderProbeResult, models: ReadonlyArray<ServerProviderModel> = []) =>
65 buildServerProvider({
66 presentation: MUSE_PRESENTATION,
67 enabled: true,
68 checkedAt,
69 models: providerModelsFromSettings(models, settings.customModels, FALLBACK_CAPABILITIES),
70 slashCommands: [COMPACT_SLASH_COMMAND],
71 probe,
72 });
73 const versionResult = yield* Effect.gen(function* () {
74 const spawnCommand = yield* resolveSpawnCommand(settings.binaryPath, ["--version"], {
75 env: museEnvironment,
76 });
77 return yield* spawnAndCollect(
78 settings.binaryPath,
79 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
80 env: museEnvironment,
81 shell: spawnCommand.shell,
82 ...(cwd ? { cwd } : {}),
83 }),
84 );
85 }).pipe(Effect.timeoutOption(DEFAULT_TIMEOUT_MS), Effect.result);
86
87 if (Result.isFailure(versionResult)) {
88 const missing = isCommandMissingCause(versionResult.failure);
89 return snapshot({
90 installed: !missing,
91 version: null,
92 status: "error",
93 auth: { status: "unknown" },
94 message: missing
95 ? "Muse Code CLI (`muse`) was not found. Install Muse Code and run `muse login` on this T3 server host."
96 : "Failed to execute Muse Code CLI. Check its binary path on this T3 server host.",
97 });
98 }
99 if (Option.isNone(versionResult.success)) {
100 return snapshot({
101 installed: true,
102 version: null,
103 status: "error",
104 auth: { status: "unknown" },
105 message: "Muse Code CLI version check timed out.",
106 });
107 }
108 const versionOutput = versionResult.success.value;
109 const version = parseMuseVersion(`${versionOutput.stdout}\n${versionOutput.stderr}`);
110 if (versionOutput.code !== 0) {
111 return snapshot({
112 installed: true,
113 version,
114 status: "error",
115 auth: { status: "unknown" },
116 message: "Muse Code CLI is installed but failed to run.",
117 });
118 }
119
120 const catalog = yield* discoverMuseModels(settings, museEnvironment, cwd, createHost).pipe(
121 Effect.scoped,
122 Effect.timeoutOption(12_000),
123 Effect.result,
124 );
125 if (Result.isFailure(catalog) || Option.isNone(catalog.success)) {
126 return snapshot({
127 installed: true,
128 version,
129 status: "error",
130 auth: { status: "unknown" },
131 message:
132 "Muse Code SDK could not read the model catalog. Check your Muse installation and run `muse login` on this T3 server host.",
133 });
134 }
135 const models = catalog.success.value;
136 // MSP has no auth signal, and model/list answers from Muse's cache even when logged out.
137 // Auth failures surface on the first turn instead.
138 return snapshot(
139 {
140 installed: true,
141 version,
142 auth: { status: "unknown" },
143 ...(models.length > 0
144 ? { status: "ready" }
145 : {
146 status: "warning",
147 message:
148 "Muse Code returned no models. Run `muse login` on this T3 server host and refresh its status.",
149 }),
150 },
151 models,
152 );
153});