Open History to see the last commit
1import { ServerProviderModel } from "@t3tools/contracts";
2import { MuseSettings } from "../settings.ts";
3import { resolveSpawnCommand } from "@t3tools/shared/shell";
4import * as DateTime from "effect/DateTime";
5import * as Effect from "effect/Effect";
6import * as Option from "effect/Option";
7import * as Result from "effect/Result";
8import * as ChildProcess from "effect/process/ChildProcess";
9
10import { createMuseSdkHost, makeMuseEnvironment } from "./sdk.ts";
11import { parseMuseVersion } from "./maintenance.ts";
12import { discoverMuseModels, museModelCapabilities } from "./modelCatalog.ts";
13import {
14 buildServerProvider,
15 COMPACT_SLASH_COMMAND,
16 DEFAULT_TIMEOUT_MS,
17 isCommandMissingCause,
18 providerModelsFromSettings,
19 spawnAndCollect,
20 type ProviderProbeResult,
21} from "@t3tools/provider-core/server/snapshotProbe";
22
23const MUSE_PRESENTATION = {
24 displayName: "Muse Code",
25 showInteractionModeToggle: false,
26 reportsContextWindow: true,
27 // Muse has no accept-edits mode; map only :ask-me and :unrestricted.
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 const museEnvironment = environment ?? makeMuseEnvironment();
63 const snapshot = (probe: ProviderProbeResult, models: ReadonlyArray<ServerProviderModel> = []) =>
64 buildServerProvider({
65 presentation: MUSE_PRESENTATION,
66 enabled: true,
67 checkedAt,
68 models: providerModelsFromSettings(models, settings.customModels, FALLBACK_CAPABILITIES),
69 slashCommands: [COMPACT_SLASH_COMMAND],
70 probe,
71 });
72 const versionResult = yield* Effect.gen(function* () {
73 const spawnCommand = yield* resolveSpawnCommand(settings.binaryPath, ["--version"], {
74 env: museEnvironment,
75 });
76 return yield* spawnAndCollect(
77 settings.binaryPath,
78 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
79 env: museEnvironment,
80 shell: spawnCommand.shell,
81 ...(cwd ? { cwd } : {}),
82 }),
83 );
84 }).pipe(Effect.timeoutOption(DEFAULT_TIMEOUT_MS), Effect.result);
85
86 if (Result.isFailure(versionResult)) {
87 const missing = isCommandMissingCause(versionResult.failure);
88 return snapshot({
89 installed: !missing,
90 version: null,
91 status: "error",
92 auth: { status: "unknown" },
93 message: missing
94 ? "Muse Code CLI (`muse`) was not found. Install Muse Code and run `muse login` on this T3 server host."
95 : "Failed to execute Muse Code CLI. Check its binary path on this T3 server host.",
96 });
97 }
98 if (Option.isNone(versionResult.success)) {
99 return snapshot({
100 installed: true,
101 version: null,
102 status: "error",
103 auth: { status: "unknown" },
104 message: "Muse Code CLI version check timed out.",
105 });
106 }
107 const versionOutput = versionResult.success.value;
108 const version = parseMuseVersion(`${versionOutput.stdout}\n${versionOutput.stderr}`);
109 if (versionOutput.code !== 0) {
110 return snapshot({
111 installed: true,
112 version,
113 status: "error",
114 auth: { status: "unknown" },
115 message: "Muse Code CLI is installed but failed to run.",
116 });
117 }
118
119 const catalog = yield* discoverMuseModels(settings, museEnvironment, cwd, createHost).pipe(
120 Effect.scoped,
121 Effect.timeoutOption(12_000),
122 Effect.result,
123 );
124 if (Result.isFailure(catalog) || Option.isNone(catalog.success)) {
125 return snapshot({
126 installed: true,
127 version,
128 status: "error",
129 auth: { status: "unknown" },
130 message:
131 "Muse Code SDK could not read the model catalog. Check your Muse installation and run `muse login` on this T3 server host.",
132 });
133 }
134 const models = catalog.success.value;
135 // MSP has no auth signal and serves cached models while logged out; failures surface on first turn.
136 return snapshot(
137 {
138 installed: true,
139 version,
140 auth: { status: "unknown" },
141 ...(models.length > 0
142 ? { status: "ready" }
143 : {
144 status: "warning",
145 message:
146 "Muse Code returned no models. Run `muse login` on this T3 server host and refresh its status.",
147 }),
148 },
149 models,
150 );
151});