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

peerPairingCode.ts

95 lines3.7 KB

1/**
2 * A one-use pairing code for linking, minted on the environment being linked to with the
3 * user's own session there, so a link card can be answered without fetching a code by hand.
4 * It carries exactly what an agent sign-in with the chosen access needs.
5 */
6import { type AuthMcpClientAccess, mcpClientScopes } from "@t3tools/contracts";
7import * as Data from "effect/Data";
8import * as Effect from "effect/Effect";
9import type * as HttpClient from "effect/http/HttpClient";
10import * as Option from "effect/Option";
11import type { Atom } from "effect/reactivity";
12import * as SubscriptionRef from "effect/SubscriptionRef";
13​
14import * as RemoteEnvironmentAuthorization from "../authorization/service.ts";
15import type { PreparedConnection } from "../connection/model.ts";
16import type { EnvironmentRegistry } from "../connection/registry.ts";
17import * as EnvironmentSupervisor from "../connection/supervisor.ts";
18import * as ManagedRelay from "../relay/managedRelay.ts";
19import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
20import { createEnvironmentCommand } from "./runtime.ts";
21​
22const MINT_TIMEOUT_MS = 8_000;
23const PEER_PAIRING_CODE_LABEL = "T3 Code link";
24​
25/** Minting failed; the user can still paste a code from the other environment. */
26export class PeerPairingCodeError extends Data.TaggedError("PeerPairingCodeError")<{
27 readonly reason: "not_allowed" | "unavailable";
28 readonly cause: unknown;
29}> {
30 override get message(): string {
31 return this.reason === "not_allowed"
32 ? "Your session there cannot create pairing codes. Paste a code from there instead."
33 : "Could not get a pairing code from there. Paste a code from there instead.";
34 }
36​
37const mintPeerPairingCode = Effect.fn("clientRuntime.state.mintPeerPairingCode")(function* (
38 prepared: PreparedConnection,
39 access: AuthMcpClientAccess,
40): Effect.fn.Return<string, PeerPairingCodeError, HttpClient.HttpClient> {
41 const signer = yield* Effect.serviceOption(ManagedRelay.ManagedRelayDpopSigner);
42 const remoteAuthorization = yield* Effect.serviceOption(
43 RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization,
44 );
45 const minted = yield* executeAuthenticatedEnvironmentHttpRequest({
46 prepared,
47 signer,
48 remoteAuthorization,
49 group: "auth",
50 method: "POST",
51 url: (urls) => urls.pairingCredential(),
52 timeoutMs: MINT_TIMEOUT_MS,
53 request: ({ client, headers }) =>
54 client.pairingCredential({
55 headers,
56 payload: { label: PEER_PAIRING_CODE_LABEL, scopes: mcpClientScopes(access) },
57 }),
58 }).pipe(
59 Effect.mapError(
60 (cause) =>
61 new PeerPairingCodeError({
62 reason:
63 cause._tag === "EnvironmentScopeRequiredError" ||
64 cause._tag === "EnvironmentOperationForbiddenError"
65 ? "not_allowed"
66 : "unavailable",
67 cause,
68 }),
69 ),
70 );
71 return minted.credential;
72});
73​
74/**
75 * Mints a code on the environment it runs in. Each run mints its own: a code
76 * is one-use, so two overlapping answers must never share one.
77 */
78export function createPeerPairingCodeCommand<R, E>(
79 runtime: Atom.AtomRuntime<EnvironmentRegistry | HttpClient.HttpClient | R, E>,
80) {
81 return createEnvironmentCommand(runtime, {
82 label: "environment-data:peer-pairing-code:mint",
83 execute: Effect.fn(function* (access: AuthMcpClientAccess) {
84 const supervisor = yield* EnvironmentSupervisor.EnvironmentSupervisor;
85 const prepared = yield* SubscriptionRef.get(supervisor.prepared);
86 if (Option.isNone(prepared)) {
87 return yield* new PeerPairingCodeError({
88 reason: "unavailable",
89 cause: "The environment is not connected.",
90 });
91 }
92 return yield* mintPeerPairingCode(prepared.value, access);
93 }),
94 });