The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | RelayManagedEndpointOrigin, |
| 3 | RelayManagedEndpointRuntimeConfig, |
| 4 | } from "@t3tools/contracts/relay"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | |
| 9 | import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; |
| 10 | |
| 11 | export const CLOUD_MINT_PUBLIC_KEY = "cloud-mint-ed25519-public-key"; |
| 12 | export const CLOUD_ENDPOINT_RUNTIME_CONFIG = "cloud-endpoint-runtime-config"; |
| 13 | export const CLOUD_ENDPOINT_CONFIRMED_ORIGIN = "cloud-endpoint-confirmed-origin"; |
| 14 | export const CLOUD_LINKED_USER_ID = "cloud-linked-user-id"; |
| 15 | export const RELAY_URL_SECRET = "cloud-relay-url"; |
| 16 | export const RELAY_ISSUER_SECRET = "cloud-relay-issuer"; |
| 17 | export const RELAY_ENVIRONMENT_CREDENTIAL_SECRET = "cloud-relay-environment-credential"; |
| 18 | export const PUBLISH_AGENT_ACTIVITY_SECRET = "cloud-publish-agent-activity"; |
| 19 | export const HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET = "cloud-hold-webhooks-while-offline"; |
| 20 | export const CLOUD_LINK_REVOKED = "cloud-link-revoked"; |
| 21 | |
| 22 | export const encodeEndpointRuntimeConfigJson = Schema.encodeEffect( |
| 23 | Schema.fromJsonString(RelayManagedEndpointRuntimeConfig), |
| 24 | ); |
| 25 | |
| 26 | export const decodeRuntimeConfig = Schema.decodeUnknownOption( |
| 27 | Schema.fromJsonString(RelayManagedEndpointRuntimeConfig), |
| 28 | ); |
| 29 | |
| 30 | export const ManagedEndpointConfirmedOrigin = Schema.Struct({ |
| 31 | config: RelayManagedEndpointRuntimeConfig, |
| 32 | origin: RelayManagedEndpointOrigin, |
| 33 | }); |
| 34 | |
| 35 | export const encodeConfirmedOriginJson = Schema.encodeEffect( |
| 36 | Schema.fromJsonString(ManagedEndpointConfirmedOrigin), |
| 37 | ); |
| 38 | |
| 39 | export const decodeConfirmedOrigin = Schema.decodeUnknownOption( |
| 40 | Schema.fromJsonString(ManagedEndpointConfirmedOrigin), |
| 41 | ); |
| 42 | |
| 43 | export function isAgentActivityPublishingEnabledValue(value: string | null): boolean { |
| 44 | return value === "true"; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Mirrors the per-publish gate in AgentAwarenessRelay, so the descriptor |
| 49 | capability never advertises publishing that the publisher would skip. |
| 50 | */ |
| 51 | export const readAgentActivityPublishingActive: ( |
| 52 | secrets: ServerSecretStore.ServerSecretStore["Service"], |
| 53 | ) => Effect.Effect<boolean> = Effect.fn("config.readAgentActivityPublishingActive")( |
| 54 | function* (secrets: ServerSecretStore.ServerSecretStore["Service"]) { |
| 55 | const readSecretString = (name: string) => |
| 56 | secrets |
| 57 | .get(name) |
| 58 | .pipe( |
| 59 | Effect.map((bytes) => |
| 60 | Option.isSome(bytes) ? new TextDecoder().decode(bytes.value) : null, |
| 61 | ), |
| 62 | ); |
| 63 | const [enabled, url, environmentCredential] = yield* Effect.all([ |
| 64 | readSecretString(PUBLISH_AGENT_ACTIVITY_SECRET), |
| 65 | readSecretString(RELAY_URL_SECRET), |
| 66 | readSecretString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET), |
| 67 | ]); |
| 68 | // Match the publisher, which treats empty strings as unconfigured. |
| 69 | return ( |
| 70 | isAgentActivityPublishingEnabledValue(enabled) && |
| 71 | url !== null && |
| 72 | url !== "" && |
| 73 | environmentCredential !== null && |
| 74 | environmentCredential !== "" |
| 75 | ); |
| 76 | }, |
| 77 | Effect.orElseSucceed(() => false), |
| 78 | ); |
| 79 | |
| 80 | const readSecretString = (name: string) => |
| 81 | ServerSecretStore.ServerSecretStore.pipe( |
| 82 | Effect.flatMap((secrets) => secrets.get(name)), |
| 83 | Effect.map((bytes) => |
| 84 | Option.isSome(bytes) && bytes.value.length > 0 ? new TextDecoder().decode(bytes.value) : null, |
| 85 | ), |
| 86 | Effect.orElseSucceed(() => null), |
| 87 | ); |
| 88 | |
| 89 | export const readRelayConnection = Effect.all([ |
| 90 | readSecretString(RELAY_URL_SECRET), |
| 91 | readSecretString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET), |
| 92 | ]).pipe( |
| 93 | Effect.map(([url, environmentCredential]) => |
| 94 | url && environmentCredential ? { url, environmentCredential } : null, |
| 95 | ), |
| 96 | ); |
| 97 | |
| 98 | export const readHoldWebhooksWhileOffline = readSecretString( |
| 99 | HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET, |
| 100 | ).pipe(Effect.map((value) => value === "true")); |