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

capabilities.ts

97 lines3.6 KB

1import {
2 type AuthClientPresentationMetadata,
3 type DesktopSshEnvironmentBootstrap,
4 type DesktopSshEnvironmentTarget,
5 EnvironmentId,
6} from "@t3tools/contracts";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import type * as Option from "effect/Option";
10​
11import type { ConnectionAttemptError } from "../connection/model.ts";
12​
13export interface PreparedSshEnvironment {
14 readonly bootstrap: DesktopSshEnvironmentBootstrap;
15 readonly bearerToken: string;
17​
18export interface ProvisionedSshEnvironment extends PreparedSshEnvironment {
19 /** Settles the tunnel opened for this add after the save succeeds or fails. */
20 readonly finishTunnelClaim?: (retain: boolean) => Effect.Effect<void>;
21 readonly environmentId: EnvironmentId;
22 readonly label: string;
24​
25/** Stable for one signed-in session, including same-account token refreshes. */
26export interface CloudSessionIdentity {
27 readonly accountId: string;
29​
30export class CloudSession extends Context.Service<
31 CloudSession,
32 {
33 readonly identity: Effect.Effect<Option.Option<CloudSessionIdentity>>;
34 readonly clerkToken: Effect.Effect<string, ConnectionAttemptError>;
35 }
36>()("@t3tools/client-runtime/platform/capabilities/CloudSession") {}
37​
38export class RelayDeviceIdentity extends Context.Service<
39 RelayDeviceIdentity,
40 {
41 readonly deviceId: Effect.Effect<Option.Option<string>, ConnectionAttemptError>;
42 }
43>()("@t3tools/client-runtime/platform/capabilities/RelayDeviceIdentity") {}
44​
45/**
46 * A random id this client keeps across launches. Environments it connects to
47 * record its hash, so two environments opened from one device share an owner
48 * in analytics even when nobody is signed in.
49 */
50export class AnalyticsDevice extends Context.Reference<{
51 readonly id: Effect.Effect<Option.Option<string>>;
52}>("@t3tools/client-runtime/platform/capabilities/AnalyticsDevice", {
53 defaultValue: () => ({ id: Effect.succeedNone }),
54}) {}
55​
56export class ClientPresentation extends Context.Service<
57 ClientPresentation,
58 {
59 readonly metadata: AuthClientPresentationMetadata;
60 }
61>()("@t3tools/client-runtime/platform/capabilities/ClientPresentation") {}
62​
63export class PrimaryEnvironmentAuth extends Context.Service<
64 PrimaryEnvironmentAuth,
65 {
66 readonly bearerToken: Effect.Effect<Option.Option<string>, ConnectionAttemptError>;
67 }
68>()("@t3tools/client-runtime/platform/capabilities/PrimaryEnvironmentAuth") {}
69​
70export class SshEnvironmentGateway extends Context.Service<
71 SshEnvironmentGateway,
72 {
73 /**
74 * With `expectedEnvironmentId`, a server that is a different machine fails
75 * before its one-time pairing credential is spent.
76 */
77 readonly provision: (
78 target: DesktopSshEnvironmentTarget,
79 expectedEnvironmentId?: EnvironmentId,
80 ) => Effect.Effect<ProvisionedSshEnvironment, ConnectionAttemptError>;
81 /**
82 * Starts or reuses the remote server for a saved connection. Returns
83 * `bearerToken`, the token this client saved from an earlier pairing, while
84 * the host still accepts it with the standard scopes, and pairs again only
85 * when it does not, so a reconnect adds no session on the host.
86 */
87 readonly prepare: (input: {
88 readonly connectionId: string;
89 readonly expectedEnvironmentId: EnvironmentId;
90 readonly target: DesktopSshEnvironmentTarget;
91 readonly bearerToken?: string;
92 }) => Effect.Effect<PreparedSshEnvironment, ConnectionAttemptError>;
93 readonly disconnect: (
94 target: DesktopSshEnvironmentTarget,
95 ) => Effect.Effect<void, ConnectionAttemptError>;
96 }
97>()("@t3tools/client-runtime/platform/capabilities/SshEnvironmentGateway") {}