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

status.ts

153 lines5.3 KB

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 native "accept edits" mode, so T3 offers only the two it maps
28 // directly: :ask-me with promptUnmatched, and :unrestricted with allowAll.
29 supportedRuntimeModes: ["approval-required", "full-access"],
30} as const;
31​
32const FALLBACK_CAPABILITIES = museModelCapabilities();
33​
34export const makePendingMuseProvider = Effect.fn("makePendingMuseProvider")(function* (
35 settings: MuseSettings,
36) {
37 return buildServerProvider({
38 presentation: MUSE_PRESENTATION,
39 enabled: settings.enabled,
40 checkedAt: DateTime.formatIso(yield* DateTime.now),
41 models: providerModelsFromSettings([], settings.customModels, FALLBACK_CAPABILITIES),
42 slashCommands: settings.enabled ? [COMPACT_SLASH_COMMAND] : [],
43 probe: {
44 installed: false,
45 version: null,
46 status: "warning",
47 auth: { status: "unknown" },
48 message: settings.enabled
49 ? "Checking Muse Code CLI availability..."
50 : "Muse Code is disabled in T3 Code settings.",
51 },
52 });
53});
54​
55export const checkMuseProviderStatus = Effect.fn("checkMuseProviderStatus")(function* (
56 settings: MuseSettings,
57 environment?: NodeJS.ProcessEnv,
58 cwd?: string,
59 createHost: typeof createMuseSdkHost = createMuseSdkHost,
60) {
61 if (!settings.enabled) return yield* makePendingMuseProvider(settings);
62 const checkedAt = DateTime.formatIso(yield* DateTime.now);
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});