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, 04:45 UTC
| 1 | import { |
| 2 | AntigravitySettings, |
| 3 | CodexSettings, |
| 4 | ProviderDriverKind, |
| 5 | type ProviderInstallCancelInput, |
| 6 | type ProviderInstallState, |
| 7 | type ProviderInstanceId, |
| 8 | ProviderSetupError, |
| 9 | type ProviderSetupInput, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import { resolveCommandPath } from "@t3tools/shared/shell"; |
| 12 | import * as Context from "effect/Context"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Layer from "effect/Layer"; |
| 15 | import * as FileSystem from "effect/FileSystem"; |
| 16 | import * as Path from "effect/Path"; |
| 17 | import * as Option from "effect/Option"; |
| 18 | import * as Schema from "effect/Schema"; |
| 19 | import * as Stream from "effect/Stream"; |
| 20 | |
| 21 | import * as CodexInstallation from "./CodexInstallation.ts"; |
| 22 | import * as ServerSettings from "../serverSettings.ts"; |
| 23 | import * as AntigravityInstallation from "./AntigravityInstallation.ts"; |
| 24 | import { deriveProviderInstanceConfigMap } from "./ProviderInstanceRegistryHydration.ts"; |
| 25 | import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts"; |
| 26 | import * as ProviderRegistry from "./ProviderRegistry.ts"; |
| 27 | import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; |
| 28 | |
| 29 | const ANTIGRAVITY = ProviderDriverKind.make("antigravity"); |
| 30 | const hasBinaryPath = Schema.is(Schema.Struct({ binaryPath: Schema.String })); |
| 31 | const decodeCodexSettings = Schema.decodeUnknownEffect(CodexSettings); |
| 32 | const decodeAntigravitySettings = Schema.decodeUnknownEffect(AntigravitySettings); |
| 33 | |
| 34 | export class ProviderInstallation extends Context.Service< |
| 35 | ProviderInstallation, |
| 36 | { |
| 37 | readonly start: ( |
| 38 | input: ProviderSetupInput, |
| 39 | ) => Effect.Effect<ProviderInstallState, ProviderSetupError>; |
| 40 | readonly cancel: ( |
| 41 | input: ProviderInstallCancelInput, |
| 42 | ) => Effect.Effect<ProviderInstallState, ProviderSetupError>; |
| 43 | readonly subscribe: ( |
| 44 | input: ProviderSetupInput, |
| 45 | ) => Stream.Stream<ProviderInstallState, ProviderSetupError>; |
| 46 | readonly remove: ( |
| 47 | input: ProviderSetupInput, |
| 48 | ) => Effect.Effect<ProviderInstallState, ProviderSetupError>; |
| 49 | } |
| 50 | >()("t3/provider/providerInstallation") {} |
| 51 | |
| 52 | const make = Effect.fn("makeProviderInstallation")(function* () { |
| 53 | const antigravityInstallation = yield* AntigravityInstallation.AntigravityInstallation; |
| 54 | const codexInstallation = yield* CodexInstallation.CodexInstallation; |
| 55 | const instances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; |
| 56 | const providers = yield* ProviderRegistry.ProviderRegistry; |
| 57 | const settings = yield* ServerSettings.ServerSettingsService; |
| 58 | const fileSystem = yield* FileSystem.FileSystem; |
| 59 | const path = yield* Path.Path; |
| 60 | const scope = yield* Effect.scope; |
| 61 | // A start during a running install joins it, so one operation is checked once whichever instance asked. |
| 62 | let accountCheckOperation: string | undefined; |
| 63 | |
| 64 | const readEntries = Effect.fn("ProviderInstallation.readEntries")(function* ( |
| 65 | instanceId: ProviderInstanceId, |
| 66 | operation: string, |
| 67 | ) { |
| 68 | const current = yield* settings.getSettings.pipe( |
| 69 | Effect.mapError( |
| 70 | () => |
| 71 | new ProviderSetupError({ |
| 72 | instanceId, |
| 73 | operation, |
| 74 | detail: "Could not read provider installation settings.", |
| 75 | }), |
| 76 | ), |
| 77 | ); |
| 78 | return deriveProviderInstanceConfigMap(current); |
| 79 | }); |
| 80 | |
| 81 | const requireInstance = Effect.fn("ProviderInstallation.requireInstance")(function* ( |
| 82 | instanceId: ProviderInstanceId, |
| 83 | operation: string, |
| 84 | managedOnly = false, |
| 85 | ) { |
| 86 | const instance = yield* instances.getInstance(instanceId); |
| 87 | const isCodex = instance?.driverKind === ProviderDriverKind.make("codex"); |
| 88 | if (instance?.driverKind !== ANTIGRAVITY && !isCodex) { |
| 89 | return yield* new ProviderSetupError({ |
| 90 | instanceId, |
| 91 | operation, |
| 92 | detail: "Managed installation is not available for this provider instance.", |
| 93 | }); |
| 94 | } |
| 95 | const installation = isCodex ? codexInstallation : antigravityInstallation; |
| 96 | const entries = yield* readEntries(instanceId, operation); |
| 97 | const invalidConfig = () => |
| 98 | new ProviderSetupError({ |
| 99 | instanceId, |
| 100 | operation, |
| 101 | detail: "The provider instance configuration is invalid.", |
| 102 | }); |
| 103 | const config = isCodex |
| 104 | ? yield* decodeCodexSettings(entries[instanceId]?.config ?? {}).pipe( |
| 105 | Effect.mapError(invalidConfig), |
| 106 | ) |
| 107 | : yield* decodeAntigravitySettings(entries[instanceId]?.config ?? {}).pipe( |
| 108 | Effect.mapError(invalidConfig), |
| 109 | ); |
| 110 | if (isCodex && (!("setupMode" in config) || config.setupMode !== "managed")) { |
| 111 | return yield* new ProviderSetupError({ |
| 112 | instanceId, |
| 113 | operation, |
| 114 | detail: "Choose managed setup to install Codex in T3 Code.", |
| 115 | }); |
| 116 | } |
| 117 | if (managedOnly && config.binaryPath && (!isCodex || config.binaryPath !== "codex")) { |
| 118 | return yield* new ProviderSetupError({ |
| 119 | instanceId, |
| 120 | operation, |
| 121 | detail: |
| 122 | "This instance uses a custom executable. Clear its binary path to manage installation in T3 Code.", |
| 123 | }); |
| 124 | } |
| 125 | return { installation, driver: instance.driverKind }; |
| 126 | }); |
| 127 | |
| 128 | const failure = |
| 129 | (instanceId: ProviderInstanceId) => |
| 130 | ( |
| 131 | error: |
| 132 | | AntigravityInstallation.AntigravityInstallationError |
| 133 | | CodexInstallation.CodexInstallationError, |
| 134 | ) => |
| 135 | new ProviderSetupError({ instanceId, operation: error.operation, detail: error.detail }); |
| 136 | |
| 137 | // Every instance without a custom executable runs the one managed runtime, so an install |
| 138 | // replaces the runtime behind all of their model lists, not only the instance that asked. |
| 139 | const checkManagedRuntimeInstances = Effect.fn( |
| 140 | "ProviderInstallation.checkManagedRuntimeInstances", |
| 141 | )(function* (instanceId: ProviderInstanceId) { |
| 142 | const entries = yield* readEntries(instanceId, "install"); |
| 143 | for (const instance of yield* instances.listInstances) { |
| 144 | if (instance.driverKind !== ANTIGRAVITY || !instance.enabled || !instance.refreshModels) { |
| 145 | continue; |
| 146 | } |
| 147 | const config = yield* decodeAntigravitySettings( |
| 148 | entries[instance.instanceId]?.config ?? {}, |
| 149 | ).pipe(Effect.option); |
| 150 | if (Option.isNone(config) || config.value.binaryPath) continue; |
| 151 | yield* instance.refreshModels().pipe(Effect.ignore({ log: true })); |
| 152 | yield* providers.refreshInstance(instance.instanceId); |
| 153 | } |
| 154 | }); |
| 155 | |
| 156 | const start = Effect.fn("ProviderInstallation.start")(function* (input: ProviderSetupInput) { |
| 157 | const { installation, driver } = yield* requireInstance(input.instanceId, "install", true); |
| 158 | const started = yield* installation.start.pipe(Effect.mapError(failure(input.instanceId))); |
| 159 | if ( |
| 160 | driver === ANTIGRAVITY && |
| 161 | started.operationId !== null && |
| 162 | accountCheckOperation !== started.operationId |
| 163 | ) { |
| 164 | accountCheckOperation = started.operationId; |
| 165 | // Installation outlives the request and may finish while every client is disconnected. |
| 166 | yield* installation.changes.pipe( |
| 167 | Stream.filter( |
| 168 | (state) => |
| 169 | state.operationId !== started.operationId || |
| 170 | state.phase === "succeeded" || |
| 171 | state.phase === "failed" || |
| 172 | state.phase === "cancelled", |
| 173 | ), |
| 174 | Stream.runHead, |
| 175 | Effect.flatMap((result) => |
| 176 | Option.isSome(result) && |
| 177 | result.value.operationId === started.operationId && |
| 178 | result.value.phase === "succeeded" |
| 179 | ? checkManagedRuntimeInstances(input.instanceId) |
| 180 | : Effect.void, |
| 181 | ), |
| 182 | Effect.ignore({ log: true }), |
| 183 | Effect.forkIn(scope), |
| 184 | ); |
| 185 | } |
| 186 | return started; |
| 187 | }); |
| 188 | |
| 189 | const cancel = Effect.fn("ProviderInstallation.cancel")(function* ( |
| 190 | input: ProviderInstallCancelInput, |
| 191 | ) { |
| 192 | const { installation } = yield* requireInstance(input.instanceId, "cancel-install"); |
| 193 | return yield* installation |
| 194 | .cancel(input.operationId) |
| 195 | .pipe(Effect.mapError(failure(input.instanceId))); |
| 196 | }); |
| 197 | |
| 198 | // Watching an installation is when its owner can act on an update, so the |
| 199 | // lookup runs then, in the background, unless update checks are off. |
| 200 | const subscribe = (input: ProviderSetupInput) => |
| 201 | Stream.unwrap( |
| 202 | requireInstance(input.instanceId, "observe-install").pipe( |
| 203 | Effect.tap(({ installation }) => |
| 204 | settings.getSettings.pipe( |
| 205 | Effect.flatMap((current) => |
| 206 | current.enableProviderUpdateChecks |
| 207 | ? Effect.forkDetach(installation.checkForUpdate) |
| 208 | : Effect.void, |
| 209 | ), |
| 210 | Effect.ignore, |
| 211 | ), |
| 212 | ), |
| 213 | Effect.map(({ installation }) => installation.changes), |
| 214 | ), |
| 215 | ); |
| 216 | |
| 217 | const remove = Effect.fn("ProviderInstallation.remove")(function* (input: ProviderSetupInput) { |
| 218 | const { installation, driver } = yield* requireInstance( |
| 219 | input.instanceId, |
| 220 | "remove-install", |
| 221 | true, |
| 222 | ); |
| 223 | const entries = yield* readEntries(input.instanceId, "remove-install"); |
| 224 | const protectedPaths = yield* Effect.forEach( |
| 225 | Object.values(entries), |
| 226 | Effect.fnUntraced(function* (entry) { |
| 227 | if (!hasBinaryPath(entry.config) || !entry.config.binaryPath.trim()) { |
| 228 | return []; |
| 229 | } |
| 230 | const binaryPath = entry.config.binaryPath.trim(); |
| 231 | const resolved = yield* resolveCommandPath(binaryPath, { |
| 232 | env: yield* mergeProviderInstanceEnvironment(entry.environment), |
| 233 | }).pipe( |
| 234 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 235 | Effect.provideService(Path.Path, path), |
| 236 | ); |
| 237 | return resolved === null ? [binaryPath] : [binaryPath, resolved]; |
| 238 | }), |
| 239 | ); |
| 240 | yield* installation |
| 241 | .remove(protectedPaths.flat()) |
| 242 | .pipe(Effect.mapError(failure(input.instanceId))); |
| 243 | const allInstances = yield* instances.listInstances; |
| 244 | yield* Effect.forEach( |
| 245 | allInstances.filter((instance) => instance.driverKind === driver), |
| 246 | (instance) => providers.refreshInstance(instance.instanceId), |
| 247 | { discard: true }, |
| 248 | ); |
| 249 | return yield* installation.state; |
| 250 | }); |
| 251 | |
| 252 | return { start, cancel, subscribe, remove }; |
| 253 | }); |
| 254 | |
| 255 | export const layer = Layer.effect(ProviderInstallation, make()); |