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 | ModelCapabilities, |
| 3 | TrimmedNonEmptyString, |
| 4 | type ProviderDriverKind, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import * as Clock from "effect/Clock"; |
| 7 | import * as Context from "effect/Context"; |
| 8 | import * as DateTime from "effect/DateTime"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as Layer from "effect/Layer"; |
| 12 | import * as Path from "effect/Path"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | import * as Semaphore from "effect/Semaphore"; |
| 15 | import { HttpClient, HttpClientResponse } from "effect/http"; |
| 16 | |
| 17 | import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite"; |
| 18 | import { ServerConfig } from "../config.ts"; |
| 19 | import * as ServerSettings from "../serverSettings.ts"; |
| 20 | import { hasValidClaudeManifestAdapters } from "./ClaudeModelManifest.ts"; |
| 21 | import bundledManifestJson from "./model-manifest.json" with { type: "json" }; |
| 22 | import { |
| 23 | ProviderCompatibilityPolicy, |
| 24 | resolveProviderCompatibility, |
| 25 | } from "./providerCompatibility.ts"; |
| 26 | import * as ModelCatalog from "@t3tools/provider-core/server/ModelCatalog"; |
| 27 | |
| 28 | const MODEL_MANIFEST_URL = |
| 29 | "https://raw.githubusercontent.com/pingdotgg/t3code/main/apps/server/src/provider/model-manifest.json"; |
| 30 | |
| 31 | const MANIFEST_TTL_MS = 60 * 60 * 1000; |
| 32 | |
| 33 | /** GitHub's raw CDN caches about this long, so a shorter window buys nothing. */ |
| 34 | const MANIFEST_ON_DEMAND_TTL_MS = 5 * 60 * 1000; |
| 35 | |
| 36 | /** Back off failed fetches so offline health checks do not each wait on the network. */ |
| 37 | const MANIFEST_RETRY_MS = 5 * 60 * 1000; |
| 38 | |
| 39 | const FETCH_TIMEOUT_MS = 10_000; |
| 40 | const MODEL_NEW_BADGE_DURATION_MS = 7 * 24 * 60 * 60 * 1000; |
| 41 | |
| 42 | const ManifestModelStatus = Schema.Literals(["current", "legacy"]); |
| 43 | |
| 44 | const ManifestModelProfile = Schema.Struct({ |
| 45 | capabilities: Schema.optional(ModelCapabilities), |
| 46 | adapter: Schema.optional(Schema.Unknown), |
| 47 | }); |
| 48 | |
| 49 | const ManifestProviderModel = Schema.Struct({ |
| 50 | slug: TrimmedNonEmptyString, |
| 51 | name: TrimmedNonEmptyString, |
| 52 | shortName: Schema.optional(TrimmedNonEmptyString), |
| 53 | subProvider: Schema.optional(TrimmedNonEmptyString), |
| 54 | aliases: Schema.optional(Schema.Array(TrimmedNonEmptyString)), |
| 55 | status: ManifestModelStatus, |
| 56 | /** ISO date or timestamp of the model's public release, as published by its provider. */ |
| 57 | releasedAt: Schema.optional(Schema.String), |
| 58 | profile: Schema.optional(TrimmedNonEmptyString), |
| 59 | adapter: Schema.optional(Schema.Unknown), |
| 60 | }); |
| 61 | |
| 62 | const ManifestProviderCatalog = Schema.Struct({ |
| 63 | defaults: Schema.optional( |
| 64 | Schema.Struct({ |
| 65 | chat: Schema.optional(TrimmedNonEmptyString), |
| 66 | }), |
| 67 | ), |
| 68 | profiles: Schema.Record(Schema.String, ManifestModelProfile), |
| 69 | models: Schema.Array(ManifestProviderModel), |
| 70 | }); |
| 71 | |
| 72 | const ModelManifestEnvelopeSchema = Schema.Struct({ |
| 73 | version: Schema.Literal(1), |
| 74 | /** Older disk caches must not override bundled manifests; absent dates rank below any dated bundle. */ |
| 75 | updatedAt: Schema.optional(Schema.String), |
| 76 | compatibility: Schema.optional(Schema.Array(ProviderCompatibilityPolicy)), |
| 77 | currentModels: Schema.Record(Schema.String, Schema.Array(Schema.String)), |
| 78 | providers: Schema.optional(Schema.Record(Schema.String, ManifestProviderCatalog)), |
| 79 | }); |
| 80 | |
| 81 | const hasValidProviderCatalogReferences = ( |
| 82 | manifest: typeof ModelManifestEnvelopeSchema.Type, |
| 83 | ): boolean => |
| 84 | Object.values(manifest.providers ?? {}).every((catalog) => { |
| 85 | const slugs = new Set<string>(); |
| 86 | const modelsAreValid = catalog.models.every((model) => { |
| 87 | if (slugs.has(model.slug)) return false; |
| 88 | slugs.add(model.slug); |
| 89 | return model.profile === undefined || catalog.profiles[model.profile] !== undefined; |
| 90 | }); |
| 91 | return ( |
| 92 | modelsAreValid && (catalog.defaults?.chat === undefined || slugs.has(catalog.defaults.chat)) |
| 93 | ); |
| 94 | }); |
| 95 | |
| 96 | const ModelManifestSchema = ModelManifestEnvelopeSchema.pipe( |
| 97 | Schema.check( |
| 98 | Schema.makeFilter(hasValidProviderCatalogReferences, { |
| 99 | expected: "unique model slugs and existing model and profile references", |
| 100 | }), |
| 101 | Schema.makeFilter(hasValidClaudeManifestAdapters, { |
| 102 | expected: "valid Claude adapter metadata", |
| 103 | }), |
| 104 | ), |
| 105 | ); |
| 106 | export type ModelManifestData = typeof ModelManifestSchema.Type; |
| 107 | |
| 108 | const decodeManifest = Schema.decodeUnknownEffect(ModelManifestSchema); |
| 109 | |
| 110 | export const BUNDLED_MODEL_MANIFEST: ModelManifestData = |
| 111 | Schema.decodeUnknownSync(ModelManifestSchema)(bundledManifestJson); |
| 112 | |
| 113 | /** A manifest with no policy for this driver and release falls back to the bundled one. */ |
| 114 | export const providerCompatibility = ( |
| 115 | manifest: ModelManifestData, |
| 116 | driver: ProviderDriverKind, |
| 117 | version: string | null, |
| 118 | ) => |
| 119 | resolveProviderCompatibility(manifest.compatibility, driver, version) ?? |
| 120 | resolveProviderCompatibility(BUNDLED_MODEL_MANIFEST.compatibility, driver, version); |
| 121 | |
| 122 | function manifestUpdatedAtMs(manifest: ModelManifestData): number { |
| 123 | if (manifest.updatedAt === undefined) return 0; |
| 124 | const parsed = Date.parse(manifest.updatedAt); |
| 125 | return Number.isNaN(parsed) ? 0 : parsed; |
| 126 | } |
| 127 | |
| 128 | export function resolveProviderCatalog( |
| 129 | manifest: ModelManifestData, |
| 130 | driverKind: ProviderDriverKind, |
| 131 | nowMs = DateTime.toEpochMillis(DateTime.nowUnsafe()), |
| 132 | ): ModelCatalog.ProviderCatalog | null { |
| 133 | const catalog = manifest.providers?.[driverKind]; |
| 134 | if (!catalog) return null; |
| 135 | |
| 136 | const seen = new Set<string>(); |
| 137 | const models: Array<ModelCatalog.ProviderCatalogModel> = []; |
| 138 | for (const entry of catalog.models) { |
| 139 | if (seen.has(entry.slug)) return null; |
| 140 | seen.add(entry.slug); |
| 141 | |
| 142 | const profile = entry.profile ? catalog.profiles[entry.profile] : undefined; |
| 143 | if (entry.profile && !profile) return null; |
| 144 | |
| 145 | const releasedAtMs = entry.releasedAt === undefined ? NaN : Date.parse(entry.releasedAt); |
| 146 | const showNewBadge = |
| 147 | entry.status === "current" && |
| 148 | nowMs >= releasedAtMs && |
| 149 | nowMs - releasedAtMs < MODEL_NEW_BADGE_DURATION_MS; |
| 150 | |
| 151 | models.push({ |
| 152 | slug: entry.slug, |
| 153 | name: entry.name, |
| 154 | ...(entry.shortName ? { shortName: entry.shortName } : {}), |
| 155 | ...(entry.subProvider ? { subProvider: entry.subProvider } : {}), |
| 156 | ...(entry.aliases ? { aliases: entry.aliases } : {}), |
| 157 | ...(showNewBadge ? { badge: "new" as const } : {}), |
| 158 | status: entry.status, |
| 159 | capabilities: profile?.capabilities ?? null, |
| 160 | adapter: entry.adapter, |
| 161 | profileAdapter: profile?.adapter, |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | if (catalog.defaults?.chat !== undefined && !seen.has(catalog.defaults.chat)) return null; |
| 166 | |
| 167 | return { models, defaultChatModel: catalog.defaults?.chat }; |
| 168 | } |
| 169 | |
| 170 | const ManifestCacheFile = Schema.Struct({ |
| 171 | fetchedAtMs: Schema.Number, |
| 172 | manifest: ModelManifestSchema, |
| 173 | }); |
| 174 | const decodeManifestCache = Schema.decodeUnknownEffect( |
| 175 | Schema.fromJsonString( |
| 176 | ManifestCacheFile as unknown as Schema.Codec<typeof ManifestCacheFile.Type>, |
| 177 | ), |
| 178 | ); |
| 179 | export const encodeManifestCache = Schema.encodeEffect( |
| 180 | Schema.fromJsonString( |
| 181 | ManifestCacheFile as unknown as Schema.Codec<typeof ManifestCacheFile.Type>, |
| 182 | ), |
| 183 | ); |
| 184 | |
| 185 | export class ModelManifest extends Context.Service< |
| 186 | ModelManifest, |
| 187 | { |
| 188 | /** Snapshot classification reads this, so it never waits on the network. */ |
| 189 | readonly current: Effect.Effect<ModelManifestData>; |
| 190 | readonly refresh: Effect.Effect<ModelManifestData>; |
| 191 | readonly forceRefresh: Effect.Effect<ModelManifestData>; |
| 192 | /** For a user looking at the model list: tolerates far less staleness than `refresh`. */ |
| 193 | readonly refreshOnDemand: Effect.Effect<ModelManifestData>; |
| 194 | /** Process-shared fetches must survive the triggering instance's teardown. */ |
| 195 | readonly refreshInBackground: Effect.Effect<void>; |
| 196 | } |
| 197 | >()("t3/provider/ModelManifest") {} |
| 198 | |
| 199 | const BundledOnlyModelManifest: ModelManifest["Service"] = { |
| 200 | current: Effect.succeed(BUNDLED_MODEL_MANIFEST), |
| 201 | refresh: Effect.succeed(BUNDLED_MODEL_MANIFEST), |
| 202 | forceRefresh: Effect.succeed(BUNDLED_MODEL_MANIFEST), |
| 203 | refreshOnDemand: Effect.succeed(BUNDLED_MODEL_MANIFEST), |
| 204 | refreshInBackground: Effect.void, |
| 205 | }; |
| 206 | |
| 207 | const layerBundledOnly = Layer.succeed(ModelManifest, BundledOnlyModelManifest); |
| 208 | |
| 209 | export const make = Effect.gen(function* () { |
| 210 | const fileSystem = yield* FileSystem.FileSystem; |
| 211 | const path = yield* Path.Path; |
| 212 | const config = yield* ServerConfig; |
| 213 | const settingsService = yield* ServerSettings.ServerSettingsService; |
| 214 | const httpClient = yield* HttpClient.HttpClient; |
| 215 | const serviceScope = yield* Effect.scope; |
| 216 | |
| 217 | const cachePath = path.join(config.stateDir, "model-manifest.json"); |
| 218 | let manifest = BUNDLED_MODEL_MANIFEST; |
| 219 | let fetchedAtMs: number | null = null; |
| 220 | let lastAttemptMs: number | null = null; |
| 221 | const refreshSemaphore = yield* Semaphore.make(1); |
| 222 | |
| 223 | // Only refresh takes the fetch semaphore; current must not wait on network I/O. |
| 224 | const ensureDiskCacheLoaded = yield* Effect.cached( |
| 225 | Effect.gen(function* () { |
| 226 | const fromDisk = yield* fileSystem.readFileString(cachePath).pipe( |
| 227 | Effect.flatMap((raw) => decodeManifestCache(raw)), |
| 228 | Effect.catchCause(() => Effect.succeed(null)), |
| 229 | ); |
| 230 | if (fromDisk === null) return; |
| 231 | if (manifestUpdatedAtMs(BUNDLED_MODEL_MANIFEST) > manifestUpdatedAtMs(fromDisk.manifest)) { |
| 232 | return; |
| 233 | } |
| 234 | manifest = fromDisk.manifest; |
| 235 | fetchedAtMs = fromDisk.fetchedAtMs; |
| 236 | }), |
| 237 | ); |
| 238 | |
| 239 | const refresh = Effect.fn("ModelManifest.refresh")(function* ( |
| 240 | force = false, |
| 241 | ttlMs = MANIFEST_TTL_MS, |
| 242 | ) { |
| 243 | yield* ensureDiskCacheLoaded; |
| 244 | const now = yield* Clock.currentTimeMillis; |
| 245 | // Expire future timestamps after clock rollback; disk caches cross restarts, ruling out monotonic time. |
| 246 | const isWithin = (sinceMs: number | null, windowMs: number) => |
| 247 | sinceMs !== null && now >= sinceMs && now - sinceMs < windowMs; |
| 248 | if (!force && isWithin(fetchedAtMs, ttlMs)) return manifest; |
| 249 | if (!force && isWithin(lastAttemptMs, MANIFEST_RETRY_MS)) return manifest; |
| 250 | |
| 251 | const settings = yield* settingsService.getSettings.pipe( |
| 252 | Effect.catchCause(() => Effect.succeed(null)), |
| 253 | ); |
| 254 | if (settings !== null && !settings.enableProviderUpdateChecks) return manifest; |
| 255 | |
| 256 | lastAttemptMs = now; |
| 257 | const fetched = yield* httpClient.get(MODEL_MANIFEST_URL).pipe( |
| 258 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 259 | Effect.flatMap((response) => response.json), |
| 260 | Effect.flatMap((json) => decodeManifest(json)), |
| 261 | Effect.timeout(FETCH_TIMEOUT_MS), |
| 262 | Effect.catchCause(() => Effect.succeed(null)), |
| 263 | ); |
| 264 | // Reject stale CDN edits just like stale disk caches to preserve bundled version gates. |
| 265 | if (fetched === null || manifestUpdatedAtMs(fetched) < manifestUpdatedAtMs(manifest)) { |
| 266 | return manifest; |
| 267 | } |
| 268 | |
| 269 | manifest = fetched; |
| 270 | fetchedAtMs = now; |
| 271 | yield* encodeManifestCache({ fetchedAtMs: now, manifest: fetched }).pipe( |
| 272 | Effect.flatMap((contents) => writeFileStringAtomically({ filePath: cachePath, contents })), |
| 273 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 274 | Effect.provideService(Path.Path, path), |
| 275 | Effect.ignoreCause, |
| 276 | ); |
| 277 | return manifest; |
| 278 | }); |
| 279 | |
| 280 | const guardedRefresh = refreshSemaphore.withPermits(1)(refresh()); |
| 281 | |
| 282 | return ModelManifest.of({ |
| 283 | current: ensureDiskCacheLoaded.pipe(Effect.map(() => manifest)), |
| 284 | refresh: guardedRefresh, |
| 285 | forceRefresh: refreshSemaphore.withPermits(1)(refresh(true)), |
| 286 | refreshOnDemand: refreshSemaphore.withPermits(1)(refresh(false, MANIFEST_ON_DEMAND_TTL_MS)), |
| 287 | refreshInBackground: Effect.forkIn(guardedRefresh, serviceScope).pipe(Effect.asVoid), |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | export const layer = Layer.effect(ModelManifest, make); |
| 292 | |
| 293 | export const layerTest = Layer.suspend(() => |
| 294 | layerModelCatalog.pipe(Layer.provideMerge(layerBundledOnly)), |
| 295 | ); |
| 296 | |
| 297 | export const layerModelCatalog = Layer.effect( |
| 298 | ModelCatalog.ModelCatalog, |
| 299 | Effect.gen(function* () { |
| 300 | const manifest = yield* ModelManifest; |
| 301 | return ModelCatalog.ModelCatalog.of({ |
| 302 | current: Effect.fn("ModelCatalog.current")(function* (driverKind: ProviderDriverKind) { |
| 303 | const data = yield* manifest.current; |
| 304 | const nowMs = yield* Clock.currentTimeMillis; |
| 305 | return resolveProviderCatalog(data, driverKind, nowMs) ?? undefined; |
| 306 | }), |
| 307 | bundled: (driverKind) => |
| 308 | resolveProviderCatalog(BUNDLED_MODEL_MANIFEST, driverKind) ?? undefined, |
| 309 | refreshInBackground: manifest.refreshInBackground, |
| 310 | }); |
| 311 | }), |
| 312 | ); |