The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { DesktopSshEnvironmentTargetSchema, EnvironmentId } from "@t3tools/contracts"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | import { |
| 6 | BearerConnectionTarget, |
| 7 | PrimaryConnectionTarget, |
| 8 | RelayConnectionTarget, |
| 9 | SshConnectionTarget, |
| 10 | type ConnectionTarget, |
| 11 | } from "./model.ts"; |
| 12 | |
| 13 | const ConnectionProfileBase = { |
| 14 | connectionId: Schema.String, |
| 15 | environmentId: EnvironmentId, |
| 16 | label: Schema.String, |
| 17 | }; |
| 18 | |
| 19 | export class BearerConnectionProfile extends Schema.TaggedClass<BearerConnectionProfile>()( |
| 20 | "BearerConnectionProfile", |
| 21 | { |
| 22 | ...ConnectionProfileBase, |
| 23 | httpBaseUrl: Schema.String, |
| 24 | wsBaseUrl: Schema.String, |
| 25 | learned: Schema.optionalKey(Schema.Literal(true)), |
| 26 | authorization: Schema.optionalKey(Schema.Literal("t3-connect")), |
| 27 | /** |
| 28 | * "tailscale" on a learned route the server found on its Tailscale |
| 29 | * interface. A bare 100.64.0.0/10 address could belong to any VPN. |
| 30 | */ |
| 31 | network: Schema.optionalKey(Schema.Literal("tailscale")), |
| 32 | }, |
| 33 | ) {} |
| 34 | |
| 35 | export class SshConnectionProfile extends Schema.TaggedClass<SshConnectionProfile>()( |
| 36 | "SshConnectionProfile", |
| 37 | { |
| 38 | ...ConnectionProfileBase, |
| 39 | target: DesktopSshEnvironmentTargetSchema, |
| 40 | }, |
| 41 | ) {} |
| 42 | |
| 43 | export const ConnectionProfile = Schema.Union([BearerConnectionProfile, SshConnectionProfile]); |
| 44 | export type ConnectionProfile = typeof ConnectionProfile.Type; |
| 45 | |
| 46 | export interface ConnectionRoute { |
| 47 | readonly target: ConnectionTarget; |
| 48 | readonly profile: Option.Option<ConnectionProfile>; |
| 49 | } |
| 50 | |
| 51 | export interface ConnectionCatalogEntry { |
| 52 | readonly target: ConnectionTarget; |
| 53 | readonly profile: Option.Option<ConnectionProfile>; |
| 54 | readonly alternateRoutes?: ReadonlyArray<ConnectionRoute>; |
| 55 | readonly enabled: boolean; |
| 56 | readonly unsupportedReason?: string; |
| 57 | readonly serverUpdateRequired?: boolean; |
| 58 | } |
| 59 | |
| 60 | export class BearerConnectionCredential extends Schema.TaggedClass<BearerConnectionCredential>()( |
| 61 | "BearerConnectionCredential", |
| 62 | { |
| 63 | token: Schema.String, |
| 64 | }, |
| 65 | ) {} |
| 66 | |
| 67 | /** A token bound to this device's DPoP key, which the host renews while the device uses it. */ |
| 68 | export class DpopConnectionCredential extends Schema.TaggedClass<DpopConnectionCredential>()( |
| 69 | "DpopConnectionCredential", |
| 70 | { |
| 71 | accessToken: Schema.String, |
| 72 | expiresAtEpochMs: Schema.Number, |
| 73 | }, |
| 74 | ) {} |
| 75 | |
| 76 | export const ConnectionCredential = Schema.Union([ |
| 77 | BearerConnectionCredential, |
| 78 | DpopConnectionCredential, |
| 79 | ]); |
| 80 | export type ConnectionCredential = typeof ConnectionCredential.Type; |
| 81 | |
| 82 | export class PrimaryConnectionRegistration extends Schema.TaggedClass<PrimaryConnectionRegistration>()( |
| 83 | "PrimaryConnectionRegistration", |
| 84 | { |
| 85 | target: PrimaryConnectionTarget, |
| 86 | }, |
| 87 | ) {} |
| 88 | |
| 89 | export class RelayConnectionRegistration extends Schema.TaggedClass<RelayConnectionRegistration>()( |
| 90 | "RelayConnectionRegistration", |
| 91 | { |
| 92 | target: RelayConnectionTarget, |
| 93 | }, |
| 94 | ) {} |
| 95 | |
| 96 | export class BearerConnectionRegistration extends Schema.TaggedClass<BearerConnectionRegistration>()( |
| 97 | "BearerConnectionRegistration", |
| 98 | { |
| 99 | target: BearerConnectionTarget, |
| 100 | profile: BearerConnectionProfile, |
| 101 | credential: ConnectionCredential, |
| 102 | }, |
| 103 | ) {} |
| 104 | |
| 105 | export class SshConnectionRegistration extends Schema.TaggedClass<SshConnectionRegistration>()( |
| 106 | "SshConnectionRegistration", |
| 107 | { |
| 108 | target: SshConnectionTarget, |
| 109 | profile: SshConnectionProfile, |
| 110 | /** The token from pairing over SSH, which later connects reuse. */ |
| 111 | credential: BearerConnectionCredential, |
| 112 | }, |
| 113 | ) {} |
| 114 | |
| 115 | export const ConnectionRegistration = Schema.Union([ |
| 116 | RelayConnectionRegistration, |
| 117 | BearerConnectionRegistration, |
| 118 | SshConnectionRegistration, |
| 119 | ]); |
| 120 | export type ConnectionRegistration = typeof ConnectionRegistration.Type; |
| 121 | |
| 122 | /** |
| 123 | * The primary stays on same-origin cookie auth |
| 124 | * (`PrimaryConnectionRegistration`); secondary local backends live on a |
| 125 | * separate loopback origin and authenticate with a bearer token minted from |
| 126 | * their bootstrap credential (`BearerConnectionRegistration`). |
| 127 | */ |
| 128 | export const PlatformConnectionRegistration = Schema.Union([ |
| 129 | PrimaryConnectionRegistration, |
| 130 | BearerConnectionRegistration, |
| 131 | ]); |
| 132 | export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type; |
| 133 | |
| 134 | export function connectionRegistrationCatalogEntry( |
| 135 | registration: ConnectionRegistration | PrimaryConnectionRegistration, |
| 136 | ): ConnectionCatalogEntry { |
| 137 | switch (registration._tag) { |
| 138 | case "PrimaryConnectionRegistration": |
| 139 | case "RelayConnectionRegistration": |
| 140 | return { |
| 141 | target: registration.target, |
| 142 | profile: Option.none(), |
| 143 | enabled: true, |
| 144 | }; |
| 145 | case "BearerConnectionRegistration": |
| 146 | case "SshConnectionRegistration": |
| 147 | return { |
| 148 | target: registration.target, |
| 149 | profile: Option.some(registration.profile), |
| 150 | enabled: true, |
| 151 | }; |
| 152 | } |
| 153 | } |