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 * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; |
| 2 | import { ProviderCredentialError } from "@t3tools/provider-core/server/errors"; |
| 3 | import * as Digest from "effect/crypto/Digest"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | |
| 9 | import { resolveAttachmentPath } from "../attachmentStore.ts"; |
| 10 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 11 | import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
| 12 | import * as ServerConfig from "../config.ts"; |
| 13 | import * as PortScanner from "../preview/PortScanner.ts"; |
| 14 | import * as ServerLifecycleEvents from "../serverLifecycleEvents.ts"; |
| 15 | import * as ServerSettings from "../serverSettings.ts"; |
| 16 | import * as DocumentText from "./DocumentText.ts"; |
| 17 | import * as ModelManifest from "./ModelManifest.ts"; |
| 18 | import * as ProviderCredentialStore from "./ProviderCredentialStore.ts"; |
| 19 | |
| 20 | export const layer = Layer.effect( |
| 21 | ProviderHost.ProviderHost, |
| 22 | Effect.gen(function* () { |
| 23 | const config = yield* ServerConfig.ServerConfig; |
| 24 | const serverSettings = yield* ServerSettings.ServerSettingsService; |
| 25 | const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy; |
| 26 | const secrets = yield* ServerSecretStore.ServerSecretStore; |
| 27 | const digest = yield* Digest.Digest; |
| 28 | const fileSystem = yield* FileSystem.FileSystem; |
| 29 | // Optional so standalone provider layers in tests probe at once. |
| 30 | const lifecycleEvents = yield* Effect.serviceOption( |
| 31 | ServerLifecycleEvents.ServerLifecycleEvents, |
| 32 | ); |
| 33 | // Optional so hosts built without the preview layer still construct. |
| 34 | const portDiscovery = yield* Effect.serviceOption(PortScanner.PortDiscovery); |
| 35 | // Optional like the services above; without it the bundled policy still applies. |
| 36 | const manifest = yield* Effect.serviceOption(ModelManifest.ModelManifest); |
| 37 | const currentManifest = Option.match(manifest, { |
| 38 | onNone: () => Effect.succeed(ModelManifest.BUNDLED_MODEL_MANIFEST), |
| 39 | onSome: (service) => service.current, |
| 40 | }); |
| 41 | return ProviderHost.ProviderHost.of({ |
| 42 | compatibility: Effect.fn("ProviderHost.compatibility")(function* (driver, version) { |
| 43 | return ModelManifest.providerCompatibility(yield* currentManifest, driver, version); |
| 44 | }), |
| 45 | paths: { |
| 46 | cwd: config.cwd, |
| 47 | baseDir: config.baseDir, |
| 48 | stateDir: config.stateDir, |
| 49 | providerStatusCacheDir: config.providerStatusCacheDir, |
| 50 | attachmentsDir: config.attachmentsDir, |
| 51 | }, |
| 52 | settings: { |
| 53 | get: serverSettings.getSettings, |
| 54 | withSnapshot: serverSettings.withSettingsSnapshot, |
| 55 | changes: serverSettings.streamChanges, |
| 56 | subscribe: serverSettings.subscribeChanges, |
| 57 | }, |
| 58 | shouldRunBackgroundWork: backgroundPolicy.shouldRunScopeWork, |
| 59 | ...Option.match(lifecycleEvents, { |
| 60 | onNone: () => ({}), |
| 61 | onSome: (events) => ({ awaitServerReady: events.awaitReady }), |
| 62 | }), |
| 63 | ...Option.match(portDiscovery, { |
| 64 | onNone: () => ({}), |
| 65 | onSome: (discovery) => ({ trackAgent: discovery.trackAgent }), |
| 66 | }), |
| 67 | resolveAttachmentPath: (attachment) => |
| 68 | resolveAttachmentPath({ attachmentsDir: config.attachmentsDir, attachment }), |
| 69 | // DocumentText owns the path-resolution span. |
| 70 | resolveExtractedTextPath: Effect.fnUntraced( |
| 71 | function* (path) { |
| 72 | return yield* DocumentText.resolveExtractedTextPath(path); |
| 73 | }, |
| 74 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 75 | ), |
| 76 | credentials: Effect.fn("ProviderHost.credentials")(function (namespace, bindingId) { |
| 77 | return ProviderCredentialStore.make(namespace, bindingId).pipe( |
| 78 | Effect.map((store) => ({ |
| 79 | binding: store.binding, |
| 80 | get: store.get.pipe( |
| 81 | Effect.mapError((cause) => new ProviderCredentialError({ operation: "get", cause })), |
| 82 | ), |
| 83 | set: (credentials: Uint8Array) => |
| 84 | store |
| 85 | .set(credentials) |
| 86 | .pipe( |
| 87 | Effect.mapError( |
| 88 | (cause) => new ProviderCredentialError({ operation: "set", cause }), |
| 89 | ), |
| 90 | ), |
| 91 | remove: store.remove.pipe( |
| 92 | Effect.mapError( |
| 93 | (cause) => new ProviderCredentialError({ operation: "remove", cause }), |
| 94 | ), |
| 95 | ), |
| 96 | })), |
| 97 | Effect.provideService(ServerSecretStore.ServerSecretStore, secrets), |
| 98 | Effect.provideService(Digest.Digest, digest), |
| 99 | ); |
| 100 | }), |
| 101 | }); |
| 102 | }), |
| 103 | ); |