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

main
ts 111 lines 4.72 KB
Raw
1 import type {
2 SourceControlAppAccess,
3 SourceControlProviderKind,
4 SourceControlSignInError,
5 SourceControlSignInPrompt,
6 SourceControlSignInResult,
7 } from "@t3tools/contracts";
8 import type * as Digest from "effect/crypto/Digest";
9 import * as Effect from "effect/Effect";
10 import type * as FileSystem from "effect/FileSystem";
11 import type { HttpClient, HttpClientError, HttpClientResponse } from "effect/http";
12 import * as Layer from "effect/Layer";
13 import type * as Scope from "effect/Scope";
14
15 import type { SourceControlDefinition } from "../shared/definition.ts";
16
17 import type { SourceControlProviderDiscoverySpec } from "./discovery.ts";
18 import type { IssueTrackerApi } from "./IssueTracker.ts";
19 import type { PullRequestProviderApi } from "./PullRequestProvider.ts";
20 import type * as SourceControlHost from "./SourceControlHost.ts";
21 import type * as SourceControlProvider from "./SourceControlProvider.ts";
22 import type * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
23
24 export interface SourceControlMediaRequest {
25 /** A `fetchUrl` this host's definition returned from `mediaUrl`. */
26 readonly url: string;
27 /** Request headers to forward, such as `range`. */
28 readonly headers: Readonly<Record<string, string>>;
29 /** `owner/name` of the change request the media is in, else of the checkout it was shown in. */
30 readonly repository?: string;
31 }
32
33 /** The server side of a definition's `credentials.appSignIn`. */
34 export interface SourceControlSignIn {
35 /** Replaces any sign-in already pending for `host`; the driver finishes it on its own. */
36 readonly start: (
37 host: string,
38 ) => Effect.Effect<SourceControlSignInPrompt, SourceControlSignInError>;
39 /** The outcome of the latest sign-in `start` began for `host`, waiting for it if pending. */
40 readonly wait: (
41 host: string,
42 ) => Effect.Effect<SourceControlSignInResult, SourceControlSignInError>;
43 /** Ends the sign-in pending for `host`; its `wait` fails as cancelled. */
44 readonly cancel: (host: string) => Effect.Effect<void>;
45 /** Whether the sign-in reaches `owner/name`, so clients can suggest installing the app there. */
46 readonly access: (
47 host: string,
48 repository: string,
49 ) => Effect.Effect<SourceControlAppAccess["status"]>;
50 }
51
52 export interface SourceControlDriverInstance {
53 readonly sourceControl: SourceControlProvider.SourceControlProvider["Service"];
54 readonly discovery: SourceControlProviderDiscoverySpec;
55 /** Null for a host whose pull requests this build cannot read. */
56 readonly pullRequests: PullRequestProviderApi | null;
57 /** Absent for a host whose issues this build cannot read. */
58 readonly issues?: IssueTrackerApi;
59 /**
60 * Fetches media the definition's `mediaUrl` claimed, with the credential the host serves it
61 * to. Null when the fetch went nowhere a response could come from, such as a redirect loop.
62 */
63 readonly fetchMedia?: (
64 request: SourceControlMediaRequest,
65 ) => Effect.Effect<
66 HttpClientResponse.HttpClientResponse | null,
67 HttpClientError.HttpClientError,
68 Scope.Scope
69 >;
70 readonly signIn?: SourceControlSignIn;
71 /** Whether the server's account on `host` starred `repository` (`owner/name`). */
72 readonly isRepositoryStarred?: (host: string, repository: string) => Effect.Effect<boolean>;
73 /** Stars `repository` as the server's account on `host`; false when the forge refused. */
74 readonly starRepository?: (host: string, repository: string) => Effect.Effect<boolean>;
75 }
76
77 /** What the server gives every driver. A driver builds anything else it needs itself. */
78 export type SourceControlDriverServices =
79 | SourceControlHost.SourceControlHost
80 | SourceControlRateLimit.SourceControlRateLimit
81 | HttpClient.HttpClient
82 | FileSystem.FileSystem
83 | Digest.Digest;
84
85 export interface SourceControlDriver {
86 readonly kind: SourceControlProviderKind;
87 readonly definition: SourceControlDefinition;
88 /**
89 * Run once per server, in a scope that lasts as long as the server. State a host shares across
90 * everything that talks to it, such as GitHub's quota reserve and rate-limit pause, lives in
91 * what this builds, so building it twice would split that state.
92 */
93 readonly make: Effect.Effect<
94 SourceControlDriverInstance,
95 never,
96 SourceControlDriverServices | Scope.Scope
97 >;
98 }
99
100 export const defineSourceControlDriver = (driver: SourceControlDriver): SourceControlDriver =>
101 driver;
102
103 /**
104 * `make` over a host's own `services`, built into the caller's scope so they live as long as the
105 * instance that holds them.
106 */
107 export const withOwnServices = <A, R, ROut, RIn>(
108 make: Effect.Effect<A, never, R>,
109 services: Layer.Layer<ROut, never, RIn>,
110 ): Effect.Effect<A, never, Exclude<R, ROut> | RIn | Scope.Scope> =>
111 Layer.build(services).pipe(Effect.flatMap((context) => make.pipe(Effect.provide(context))));