Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderHostLive.ts

93 lines3.9 KB

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