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, 03:37 UTC
| 1 | import { runAtomCommand } from "@t3tools/client-runtime/state/runtime"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import { providerClients } from "@t3tools/client-runtime/provider-clients"; |
| 4 | import { |
| 5 | type EnvironmentId, |
| 6 | type ModelSelection, |
| 7 | type ProjectId, |
| 8 | ProviderDriverKind, |
| 9 | ProviderInstanceId, |
| 10 | type ServerProvider, |
| 11 | type ServerProviderModel, |
| 12 | type ServerSettings, |
| 13 | type TextGeneratorConfig, |
| 14 | type TextGeneratorKind, |
| 15 | type TextGeneratorModel, |
| 16 | } from "@t3tools/contracts"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import { msg, useMessages } from "gt-react"; |
| 19 | import * as Option from "effect/Option"; |
| 20 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 21 | |
| 22 | import { |
| 23 | deriveProviderInstanceEntries, |
| 24 | type ProviderInstanceEntry, |
| 25 | providerInstanceEntriesFor, |
| 26 | } from "./providerInstances"; |
| 27 | import { m, type MessageResolver } from "./i18n/messages"; |
| 28 | import { appAtomRegistry } from "./rpc/atomRegistry"; |
| 29 | import { formatEnvironmentQueryError } from "./state/query"; |
| 30 | import { serverEnvironment } from "./state/server"; |
| 31 | |
| 32 | export interface TextGeneratorPreset { |
| 33 | readonly id: "openai" | "openrouter" | "anthropic" | "custom"; |
| 34 | readonly label: string; |
| 35 | readonly kind: TextGeneratorKind; |
| 36 | /** Empty for a custom server, whose URL the user enters. */ |
| 37 | readonly baseUrl: string; |
| 38 | } |
| 39 | |
| 40 | export const TEXT_GENERATOR_PRESETS: ReadonlyArray<TextGeneratorPreset> = [ |
| 41 | { id: "openai", label: "OpenAI", kind: "openaiCompatible", baseUrl: "https://api.openai.com/v1" }, |
| 42 | { |
| 43 | id: "openrouter", |
| 44 | label: "OpenRouter", |
| 45 | kind: "openaiCompatible", |
| 46 | baseUrl: "https://openrouter.ai/api/v1", |
| 47 | }, |
| 48 | { |
| 49 | id: "anthropic", |
| 50 | label: "Anthropic", |
| 51 | kind: "anthropic", |
| 52 | baseUrl: "https://api.anthropic.com/v1", |
| 53 | }, |
| 54 | { |
| 55 | id: "custom", |
| 56 | label: msg("Custom (OpenAI-compatible)"), |
| 57 | kind: "openaiCompatible", |
| 58 | baseUrl: "", |
| 59 | }, |
| 60 | ]; |
| 61 | |
| 62 | export function textGeneratorDisplayName( |
| 63 | config: TextGeneratorConfig, |
| 64 | resolve: MessageResolver = m, |
| 65 | ): string { |
| 66 | if (config.label) return config.label; |
| 67 | const preset = TEXT_GENERATOR_PRESETS.find( |
| 68 | (candidate) => candidate.baseUrl === config.baseUrl && candidate.kind === config.kind, |
| 69 | ); |
| 70 | if (preset) return resolve(preset.label); |
| 71 | try { |
| 72 | return new URL(config.baseUrl).host; |
| 73 | } catch { |
| 74 | return config.baseUrl; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * The service a text generator reaches, which picks its picker icon. It stands in for the driver |
| 80 | * kind in picker entries only, so generators of one service share an icon and get instance badges. |
| 81 | */ |
| 82 | export function textGeneratorBrand(config: Pick<TextGeneratorConfig, "kind" | "baseUrl">) { |
| 83 | if (config.kind === "anthropic") return ProviderDriverKind.make("anthropic"); |
| 84 | let url: URL | null = null; |
| 85 | try { |
| 86 | url = new URL(config.baseUrl); |
| 87 | } catch {} |
| 88 | const host = url?.hostname ?? ""; |
| 89 | if (host === "openrouter.ai" || host.endsWith(".openrouter.ai")) { |
| 90 | return ProviderDriverKind.make("openrouter"); |
| 91 | } |
| 92 | if (host === "api.openai.com") return ProviderDriverKind.make("openai"); |
| 93 | if (url?.port === "11434" || host.includes("ollama")) return ProviderDriverKind.make("ollama"); |
| 94 | return ProviderDriverKind.make("openaiCompatible"); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * A new text generator id. Selections name a text generator by the same id as a provider |
| 99 | * instance, so it must not match a configured instance, a driver's default instance, or |
| 100 | * another text generator. |
| 101 | */ |
| 102 | export function nextTextGeneratorId( |
| 103 | base: string, |
| 104 | settings: Pick<ServerSettings, "providerInstances" | "textGenerators">, |
| 105 | providers: ReadonlyArray<Pick<ServerProvider, "instanceId">>, |
| 106 | ): ProviderInstanceId { |
| 107 | const taken = new Set<string>([ |
| 108 | ...Object.keys(settings.providerInstances), |
| 109 | ...Object.keys(settings.textGenerators), |
| 110 | ...providers.map((provider) => provider.instanceId), |
| 111 | ...providerClients.definitions.map((definition) => definition.driverKind), |
| 112 | ]); |
| 113 | let id = base; |
| 114 | for (let suffix = 2; taken.has(id); suffix += 1) { |
| 115 | id = `${base}-${suffix}`; |
| 116 | } |
| 117 | return ProviderInstanceId.make(id); |
| 118 | } |
| 119 | |
| 120 | type ModelsKey = readonly [ |
| 121 | environmentId: EnvironmentId, |
| 122 | id: ProviderInstanceId, |
| 123 | // The connection settings, so editing them asks again instead of reusing a stale list. |
| 124 | kind: TextGeneratorKind, |
| 125 | baseUrl: string, |
| 126 | apiKey: TextGeneratorConfig["apiKey"], |
| 127 | ]; |
| 128 | |
| 129 | const refreshCountAtom = Atom.family((key: string) => |
| 130 | Atom.make(0).pipe(Atom.keepAlive, Atom.withLabel(`text-generator-refresh:${key}`)), |
| 131 | ); |
| 132 | |
| 133 | const textGeneratorModelsAtom = Atom.family((key: string) => { |
| 134 | const [environmentId, id] = JSON.parse(key) as ModelsKey; |
| 135 | return Atom.make((get) => { |
| 136 | // Read again after a refresh; the server's cache already holds the new list by then. |
| 137 | get(refreshCountAtom(`${environmentId}:${id}`)); |
| 138 | return serverEnvironment.listTextGeneratorModels |
| 139 | .run(get.registry, { environmentId, input: { id } }) |
| 140 | .pipe( |
| 141 | Effect.map((result) => result.models), |
| 142 | Effect.catchCause((cause) => Effect.fail(formatEnvironmentQueryError(cause))), |
| 143 | ); |
| 144 | }).pipe(Atom.setIdleTTL(5 * 60_000), Atom.withLabel(`text-generator-models:${key}`)); |
| 145 | }); |
| 146 | |
| 147 | const textGeneratorCatalogAtom = Atom.family((keys: string) => |
| 148 | Atom.make((get) => |
| 149 | (JSON.parse(keys) as ReadonlyArray<string>).map((key) => get(textGeneratorModelsAtom(key))), |
| 150 | ).pipe(Atom.withLabel(`text-generator-catalog:${keys}`)), |
| 151 | ); |
| 152 | |
| 153 | /** |
| 154 | * Has open pickers ask the server again. Needed after a saved key changes: clients only see the |
| 155 | * redaction marker, so the cache key does not change with it. |
| 156 | */ |
| 157 | export function rereadTextGeneratorModels(environmentId: EnvironmentId, id: ProviderInstanceId) { |
| 158 | const atom = refreshCountAtom(`${environmentId}:${id}`); |
| 159 | appAtomRegistry.set(atom, appAtomRegistry.get(atom) + 1); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Asks the API for its model list again, skipping the server's cache, then has open pickers |
| 164 | * read it. Resolves to the model count, or the error to show. |
| 165 | */ |
| 166 | export async function refreshTextGeneratorModels( |
| 167 | environmentId: EnvironmentId, |
| 168 | id: ProviderInstanceId, |
| 169 | ): Promise<{ readonly count: number } | { readonly error: string }> { |
| 170 | const result = await runAtomCommand( |
| 171 | appAtomRegistry, |
| 172 | serverEnvironment.listTextGeneratorModels, |
| 173 | { environmentId, input: { id, refresh: true } }, |
| 174 | { reportFailure: false }, |
| 175 | ); |
| 176 | rereadTextGeneratorModels(environmentId, id); |
| 177 | return result._tag === "Success" |
| 178 | ? { count: result.value.models.length } |
| 179 | : { error: formatEnvironmentQueryError(result.cause) }; |
| 180 | } |
| 181 | |
| 182 | /** Lists the models unsaved settings reach, to check them before saving. */ |
| 183 | export async function checkTextGeneratorConfig( |
| 184 | environmentId: EnvironmentId, |
| 185 | id: ProviderInstanceId, |
| 186 | config: TextGeneratorConfig, |
| 187 | ): Promise<{ readonly models: ReadonlyArray<TextGeneratorModel> } | { readonly error: string }> { |
| 188 | const result = await runAtomCommand( |
| 189 | appAtomRegistry, |
| 190 | serverEnvironment.listTextGeneratorModels, |
| 191 | { environmentId, input: { id, config } }, |
| 192 | { reportFailure: false }, |
| 193 | ); |
| 194 | return result._tag === "Success" |
| 195 | ? { models: result.value.models } |
| 196 | : { error: formatEnvironmentQueryError(result.cause) }; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Has a saved generator's model answer a tiny prompt. Some APIs list models without checking the |
| 201 | * key, so only this proves the key, endpoint, and model together. Resolves to the error, if any. |
| 202 | */ |
| 203 | export async function testTextGeneratorModel( |
| 204 | environmentId: EnvironmentId, |
| 205 | id: ProviderInstanceId, |
| 206 | model: string, |
| 207 | ): Promise<string | null> { |
| 208 | const result = await runAtomCommand( |
| 209 | appAtomRegistry, |
| 210 | serverEnvironment.listTextGeneratorModels, |
| 211 | { environmentId, input: { id, test: true, model } }, |
| 212 | { reportFailure: false }, |
| 213 | ); |
| 214 | return result._tag === "Success" ? null : formatEnvironmentQueryError(result.cause); |
| 215 | } |
| 216 | |
| 217 | /** A saved text generator's model list, shared with the pickers. */ |
| 218 | export function useTextGeneratorModels( |
| 219 | environmentId: EnvironmentId, |
| 220 | id: ProviderInstanceId, |
| 221 | config: TextGeneratorConfig, |
| 222 | ): AsyncResult.AsyncResult<ReadonlyArray<TextGeneratorModel>, string> { |
| 223 | return useAtomValue( |
| 224 | textGeneratorModelsAtom( |
| 225 | JSON.stringify([ |
| 226 | environmentId, |
| 227 | id, |
| 228 | config.kind, |
| 229 | config.baseUrl, |
| 230 | config.apiKey, |
| 231 | ] satisfies ModelsKey), |
| 232 | ), |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | function toProviderModel(model: TextGeneratorModel): ServerProviderModel { |
| 237 | return { slug: model.id, name: model.name, isCustom: false, capabilities: null }; |
| 238 | } |
| 239 | |
| 240 | /** Picker stand-in for a text generator. It never leaves the settings pickers. */ |
| 241 | function textGeneratorSnapshot( |
| 242 | id: ProviderInstanceId, |
| 243 | config: TextGeneratorConfig, |
| 244 | result: AsyncResult.AsyncResult<ReadonlyArray<TextGeneratorModel>, string>, |
| 245 | selectedModels: ReadonlyArray<string>, |
| 246 | resolve: MessageResolver, |
| 247 | ): ServerProvider { |
| 248 | const discovered = AsyncResult.isSuccess(result) ? result.value.map(toProviderModel) : []; |
| 249 | const missing = [...new Set(selectedModels)] |
| 250 | .filter((slug) => !discovered.some((model) => model.slug === slug)) |
| 251 | .map((slug): ServerProviderModel => ({ |
| 252 | slug, |
| 253 | name: slug, |
| 254 | isCustom: false, |
| 255 | capabilities: null, |
| 256 | })); |
| 257 | const failure = Option.getOrUndefined(AsyncResult.error(result)); |
| 258 | return { |
| 259 | instanceId: id, |
| 260 | driver: textGeneratorBrand(config), |
| 261 | displayName: textGeneratorDisplayName(config, resolve), |
| 262 | supportsTextGeneration: true, |
| 263 | enabled: config.enabled, |
| 264 | installed: true, |
| 265 | version: null, |
| 266 | status: failure === undefined ? "ready" : "error", |
| 267 | ...(failure === undefined ? {} : { message: failure }), |
| 268 | auth: { status: "unknown" }, |
| 269 | checkedAt: new Date(0).toISOString(), |
| 270 | models: [...discovered, ...missing], |
| 271 | slashCommands: [], |
| 272 | skills: [], |
| 273 | }; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * The environment's text generation providers plus its enabled text generators, for the pickers |
| 278 | * that may write text. Text generators replace a provider instance with the same id, as the |
| 279 | * server does. Selections listed in `selections` keep their model while the list loads. |
| 280 | */ |
| 281 | export function useTextGenerationTargets(input: { |
| 282 | readonly environmentId: EnvironmentId | null; |
| 283 | readonly settings: Pick< |
| 284 | ServerSettings, |
| 285 | "providerInstances" | "projectSettingsOverrides" | "textGenerators" |
| 286 | >; |
| 287 | readonly providers: ReadonlyArray<ServerProvider>; |
| 288 | readonly projectId: ProjectId | null; |
| 289 | readonly selections: ReadonlyArray<ModelSelection | null | undefined>; |
| 290 | }): { |
| 291 | readonly providers: ReadonlyArray<ServerProvider>; |
| 292 | readonly entries: ReadonlyArray<ProviderInstanceEntry>; |
| 293 | } { |
| 294 | const { environmentId, settings } = input; |
| 295 | const resolve = useMessages(); |
| 296 | const generators = |
| 297 | environmentId === null |
| 298 | ? [] |
| 299 | : ( |
| 300 | Object.entries(settings.textGenerators) as Array< |
| 301 | [ProviderInstanceId, TextGeneratorConfig] |
| 302 | > |
| 303 | ) |
| 304 | .filter(([, config]) => config.enabled) |
| 305 | .map(([id, config]) => ({ |
| 306 | id, |
| 307 | config, |
| 308 | key: JSON.stringify([ |
| 309 | environmentId, |
| 310 | id, |
| 311 | config.kind, |
| 312 | config.baseUrl, |
| 313 | config.apiKey, |
| 314 | ] satisfies ModelsKey), |
| 315 | })); |
| 316 | const results = useAtomValue( |
| 317 | textGeneratorCatalogAtom(JSON.stringify(generators.map((generator) => generator.key))), |
| 318 | ); |
| 319 | const generatorProviders = generators.map(({ id, config }, index) => |
| 320 | textGeneratorSnapshot( |
| 321 | id, |
| 322 | config, |
| 323 | results[index] ?? AsyncResult.initial(), |
| 324 | input.selections.flatMap((selection) => |
| 325 | selection?.instanceId === id && selection.model ? [selection.model] : [], |
| 326 | ), |
| 327 | resolve, |
| 328 | ), |
| 329 | ); |
| 330 | const providers = input.providers.filter( |
| 331 | (provider) => !Object.hasOwn(settings.textGenerators, provider.instanceId), |
| 332 | ); |
| 333 | return { |
| 334 | providers: [...providers, ...generatorProviders], |
| 335 | // Project provider overrides do not apply to text generators, so their entries skip them. |
| 336 | entries: [ |
| 337 | ...providerInstanceEntriesFor(providers, settings, input.projectId), |
| 338 | ...deriveProviderInstanceEntries(generatorProviders), |
| 339 | ], |
| 340 | }; |
| 341 | } |