Skip to content
Source
Sign inSign up

ProviderHost.ts

55 lines2.3 KB

1import type {
2 BackgroundScope,
3 ChatAttachment,
4 ServerSettings,
5 ServerSettingsError,
6} from "@t3tools/contracts";
7import * as Context from "effect/Context";
8import type * as Effect from "effect/Effect";
9import type * as Option from "effect/Option";
10import type * as Scope from "effect/Scope";
11import type * as Stream from "effect/Stream";
12​
13import type { ProviderCredentialError } from "./errors.ts";
14​
15export interface ProviderHostPaths {
16 readonly cwd: string;
17 readonly baseDir: string;
18 readonly stateDir: string;
19 readonly providerStatusCacheDir: string;
20 /** Where chat attachments are stored; agents that sandbox file reads must be granted it. */
21 readonly attachmentsDir: string;
23​
24export interface ProviderCredentials {
25 /** Instances whose bindings are equal share these credentials. */
26 readonly binding: { readonly owner: "t3"; readonly key: string };
27 readonly get: Effect.Effect<Option.Option<Uint8Array>, ProviderCredentialError>;
28 readonly set: (credentials: Uint8Array) => Effect.Effect<void, ProviderCredentialError>;
29 readonly remove: Effect.Effect<void, ProviderCredentialError>;
31​
32export class ProviderHost extends Context.Service<
33 ProviderHost,
34 {
35 readonly paths: ProviderHostPaths;
36 readonly settings: {
37 readonly get: Effect.Effect<ServerSettings, ServerSettingsError>;
38 /** Runs `use` on a settings snapshot that no settings write can change until it finishes. */
39 readonly withSnapshot: <A, E, R>(
40 use: (settings: ServerSettings) => Effect.Effect<A, E, R>,
41 ) => Effect.Effect<A, E | ServerSettingsError, R>;
42 readonly changes: Stream.Stream<ServerSettings>;
43 /** Changes buffered from the moment the scoped subscription is acquired. */
44 readonly subscribe: Effect.Effect<Stream.Stream<ServerSettings>, never, Scope.Scope>;
45 };
46 readonly shouldRunBackgroundWork: (scope: BackgroundScope) => Effect.Effect<boolean>;
47 /** Absolute attachment path, or null if the ID does not resolve inside attachmentsDir. */
48 readonly resolveAttachmentPath: (attachment: ChatAttachment) => string | null;
49 readonly resolveExtractedTextPath?: (path: string) => Effect.Effect<string | undefined>;
50 readonly credentials: (
51 namespace: string,
52 bindingId: string,
53 ) => Effect.Effect<ProviderCredentials>;
54 }
55>()("@t3tools/provider-core/server/ProviderHost") {}