The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolveProviderProbeTimeoutMs } from "@t3tools/provider-core/server/snapshotProbe"; |
| 2 | import type { SendUserTurnOptions } from "@muse-code/sdk"; |
| 3 | import { ModelCapabilities, ServerProviderModel } from "@t3tools/contracts"; |
| 4 | import { MuseSettings } from "../settings.ts"; |
| 5 | import { createModelCapabilities, getProviderOptionDescriptors } from "@t3tools/shared/model"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { |
| 9 | createMuseSdkHost, |
| 10 | createMuseSdkHostEffect, |
| 11 | MuseSdkError, |
| 12 | type MuseSdkHost, |
| 13 | } from "./sdk.ts"; |
| 14 | |
| 15 | export const resolveMuseStartupTimeoutMs = (environment?: NodeJS.ProcessEnv) => |
| 16 | resolveProviderProbeTimeoutMs("muse", 8_000, environment); |
| 17 | |
| 18 | type MuseReasoningEffort = NonNullable<SendUserTurnOptions<never>["reasoningEffort"]>; |
| 19 | |
| 20 | const REASONING_EFFORT_LABELS = { |
| 21 | none: "None", |
| 22 | minimal: "Minimal", |
| 23 | low: "Low", |
| 24 | medium: "Medium", |
| 25 | high: "High", |
| 26 | xhigh: "Extra High", |
| 27 | max: "Max", |
| 28 | ultra: "Ultra", |
| 29 | } satisfies Record<MuseReasoningEffort, string>; |
| 30 | |
| 31 | const isMuseReasoningEffort = (tier: string): tier is MuseReasoningEffort => |
| 32 | Object.hasOwn(REASONING_EFFORT_LABELS, tier); |
| 33 | |
| 34 | const FALLBACK_EFFORTS = ["low", "medium", "high", "xhigh", "max"]; |
| 35 | const FALLBACK_DEFAULT_EFFORT = "high"; |
| 36 | |
| 37 | const ModelCatalogEntry = Schema.Struct({ |
| 38 | modelId: Schema.NonEmptyString, |
| 39 | displayLabel: Schema.String, |
| 40 | providerId: Schema.String, |
| 41 | isDefault: Schema.Boolean, |
| 42 | variants: Schema.optional(Schema.Union([Schema.Array(Schema.String), Schema.Literal("unknown")])), |
| 43 | defaultReasoningEffort: Schema.optional(Schema.String), |
| 44 | reasoningEffortVariants: Schema.optional( |
| 45 | Schema.Array( |
| 46 | Schema.Struct({ tier: Schema.String, description: Schema.optional(Schema.String) }), |
| 47 | ), |
| 48 | ), |
| 49 | }); |
| 50 | const decodeModelList = Schema.decodeUnknownEffect( |
| 51 | Schema.Struct({ providerId: Schema.String, models: Schema.Array(ModelCatalogEntry) }), |
| 52 | ); |
| 53 | |
| 54 | export function museModelCapabilities( |
| 55 | model?: Pick< |
| 56 | typeof ModelCatalogEntry.Type, |
| 57 | "variants" | "defaultReasoningEffort" | "reasoningEffortVariants" |
| 58 | >, |
| 59 | ): ModelCapabilities { |
| 60 | const reported = model?.variants !== undefined && model.variants !== "unknown"; |
| 61 | const efforts = (reported ? model.variants : FALLBACK_EFFORTS).filter(isMuseReasoningEffort); |
| 62 | const preferredDefault = reported ? model?.defaultReasoningEffort : FALLBACK_DEFAULT_EFFORT; |
| 63 | const defaultValue = efforts.find((tier) => tier === preferredDefault) ?? efforts[0]; |
| 64 | if (!defaultValue) return createModelCapabilities({ optionDescriptors: [] }); |
| 65 | return createModelCapabilities({ |
| 66 | optionDescriptors: [ |
| 67 | { |
| 68 | id: "reasoningEffort", |
| 69 | label: "Reasoning", |
| 70 | type: "select", |
| 71 | currentValue: defaultValue, |
| 72 | options: efforts.map((id) => { |
| 73 | const description = model?.reasoningEffortVariants?.find( |
| 74 | (variant) => variant.tier === id, |
| 75 | )?.description; |
| 76 | return { |
| 77 | id, |
| 78 | label: REASONING_EFFORT_LABELS[id], |
| 79 | ...(id === defaultValue ? { isDefault: true } : {}), |
| 80 | ...(description ? { description } : {}), |
| 81 | }; |
| 82 | }), |
| 83 | }, |
| 84 | ], |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | export function resolveMuseReasoningEffort( |
| 89 | capabilities: ModelCapabilities | null | undefined, |
| 90 | effort: string | undefined, |
| 91 | ): string | undefined { |
| 92 | if (!capabilities) return effort; |
| 93 | const descriptor = getProviderOptionDescriptors({ |
| 94 | caps: capabilities, |
| 95 | selections: effort ? [{ id: "reasoningEffort", value: effort }] : undefined, |
| 96 | }).find((descriptor) => descriptor.id === "reasoningEffort"); |
| 97 | if (descriptor?.type !== "select") return undefined; |
| 98 | return descriptor.options.find((option) => option.id === descriptor.currentValue)?.id; |
| 99 | } |
| 100 | |
| 101 | class MuseCatalogError extends Schema.TaggedError<MuseCatalogError>()("MuseCatalogError", { |
| 102 | detail: Schema.String, |
| 103 | }) {} |
| 104 | |
| 105 | export const discoverMuseModels = Effect.fn("discoverMuseModels")(function* ( |
| 106 | settings: MuseSettings, |
| 107 | environment: NodeJS.ProcessEnv | undefined, |
| 108 | cwd?: string, |
| 109 | createHost: typeof createMuseSdkHost = createMuseSdkHost, |
| 110 | ) { |
| 111 | const host = yield* Effect.acquireRelease( |
| 112 | createMuseSdkHostEffect( |
| 113 | { |
| 114 | binaryPath: settings.binaryPath, |
| 115 | ...(environment ? { environment } : {}), |
| 116 | ...(cwd ? { cwd } : {}), |
| 117 | readOnly: true, |
| 118 | startupTimeoutMs: resolveMuseStartupTimeoutMs(environment), |
| 119 | }, |
| 120 | createHost, |
| 121 | ), |
| 122 | (host: MuseSdkHost) => Effect.promise(() => host.close()), |
| 123 | { interruptible: true }, |
| 124 | ); |
| 125 | const result = yield* Effect.tryPromise({ |
| 126 | try: () => host.connection.request("model/list", {}), |
| 127 | catch: (cause) => new MuseSdkError({ operation: "model/list", cause }), |
| 128 | }); |
| 129 | const catalog = yield* decodeModelList(result); |
| 130 | if (catalog.providerId !== "meta") { |
| 131 | return yield* new MuseCatalogError({ detail: "Muse returned a catalog for another provider." }); |
| 132 | } |
| 133 | const seen = new Set<string>(); |
| 134 | return catalog.models.flatMap((model): ServerProviderModel[] => { |
| 135 | if (model.providerId !== "meta" || seen.has(model.modelId)) return []; |
| 136 | seen.add(model.modelId); |
| 137 | const label = model.displayLabel.trim() || model.modelId; |
| 138 | const name = |
| 139 | label === model.modelId && label.startsWith("muse-") |
| 140 | ? label.replaceAll("-", " ").replace(/\b[a-z]/g, (letter) => letter.toUpperCase()) |
| 141 | : label; |
| 142 | return [ |
| 143 | { |
| 144 | slug: model.modelId, |
| 145 | name, |
| 146 | isCustom: false, |
| 147 | isDefault: model.isDefault, |
| 148 | capabilities: museModelCapabilities(model), |
| 149 | }, |
| 150 | ]; |
| 151 | }); |
| 152 | }); |