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

capabilities.ts

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