The best way to control Claude Code, Codex and more from any computer or phone

main
ts 65 lines 2.9 KB
Raw
1 import type {
2 BackgroundScope,
3 ChatAttachment,
4 ServerSettings,
5 ServerSettingsError,
6 } from "@t3tools/contracts";
7 import * as Context from "effect/Context";
8 import type * as Effect from "effect/Effect";
9 import type * as Option from "effect/Option";
10 import type * as Scope from "effect/Scope";
11 import type * as Stream from "effect/Stream";
12
13 import type { ProviderCompatibilityLookup } from "./compatibility.ts";
14 import type { ProviderCredentialError } from "./errors.ts";
15
16 export interface ProviderHostPaths {
17 readonly cwd: string;
18 readonly baseDir: string;
19 readonly stateDir: string;
20 readonly providerStatusCacheDir: string;
21 /** Where chat attachments are stored; agents that sandbox file reads must be granted it. */
22 readonly attachmentsDir: string;
23 }
24
25 export interface ProviderCredentials {
26 /** Instances whose bindings are equal share these credentials. */
27 readonly binding: { readonly owner: "t3"; readonly key: string };
28 readonly get: Effect.Effect<Option.Option<Uint8Array>, ProviderCredentialError>;
29 readonly set: (credentials: Uint8Array) => Effect.Effect<void, ProviderCredentialError>;
30 readonly remove: Effect.Effect<void, ProviderCredentialError>;
31 }
32
33 export class ProviderHost extends Context.Service<
34 ProviderHost,
35 {
36 readonly paths: ProviderHostPaths;
37 readonly settings: {
38 readonly get: Effect.Effect<ServerSettings, ServerSettingsError>;
39 /** Runs `use` on a settings snapshot that no settings write can change until it finishes. */
40 readonly withSnapshot: <A, E, R>(
41 use: (settings: ServerSettings) => Effect.Effect<A, E, R>,
42 ) => Effect.Effect<A, E | ServerSettingsError, R>;
43 readonly changes: Stream.Stream<ServerSettings>;
44 /** Changes buffered from the moment the scoped subscription is acquired. */
45 readonly subscribe: Effect.Effect<Stream.Stream<ServerSettings>, never, Scope.Scope>;
46 };
47 readonly shouldRunBackgroundWork: (scope: BackgroundScope) => Effect.Effect<boolean>;
48 /** Completes once server startup finishes; first provider checks wait for it. */
49 readonly awaitServerReady?: Effect.Effect<void>;
50 /** Absolute attachment path, or null if the ID does not resolve inside attachmentsDir. */
51 readonly resolveAttachmentPath: (attachment: ChatAttachment) => string | null;
52 readonly resolveExtractedTextPath?: (path: string) => Effect.Effect<string | undefined>;
53 /**
54 * Counts a running agent for local server discovery, so listeners carrying its
55 * T3CODE_THREAD_ID show under its thread. Call the returned function when it stops.
56 */
57 readonly trackAgent?: () => () => void;
58 readonly credentials: (
59 namespace: string,
60 bindingId: string,
61 ) => Effect.Effect<ProviderCredentials>;
62 /** A driver declares its minimum version in the policy and asks here, not in a constant. */
63 readonly compatibility: ProviderCompatibilityLookup;
64 }
65 >()("@t3tools/provider-core/server/ProviderHost") {}