Open History to see the last commit
1import {
2 RelayManagedEndpointOrigin,
3 RelayManagedEndpointRuntimeConfig,
4} from "@t3tools/contracts/relay";
5import * as Effect from "effect/Effect";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8
9import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
10
11export const CLOUD_MINT_PUBLIC_KEY = "cloud-mint-ed25519-public-key";
12export const CLOUD_ENDPOINT_RUNTIME_CONFIG = "cloud-endpoint-runtime-config";
13export const CLOUD_ENDPOINT_CONFIRMED_ORIGIN = "cloud-endpoint-confirmed-origin";
14export const CLOUD_LINKED_USER_ID = "cloud-linked-user-id";
15export const RELAY_URL_SECRET = "cloud-relay-url";
16export const RELAY_ISSUER_SECRET = "cloud-relay-issuer";
17export const RELAY_ENVIRONMENT_CREDENTIAL_SECRET = "cloud-relay-environment-credential";
18export const PUBLISH_AGENT_ACTIVITY_SECRET = "cloud-publish-agent-activity";
19export const HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET = "cloud-hold-webhooks-while-offline";
20
21export const encodeEndpointRuntimeConfigJson = Schema.encodeEffect(
22 Schema.fromJsonString(RelayManagedEndpointRuntimeConfig),
23);
24
25export const decodeRuntimeConfig = Schema.decodeUnknownOption(
26 Schema.fromJsonString(RelayManagedEndpointRuntimeConfig),
27);
28
29export const ManagedEndpointConfirmedOrigin = Schema.Struct({
30 config: RelayManagedEndpointRuntimeConfig,
31 origin: RelayManagedEndpointOrigin,
32});
33
34export const encodeConfirmedOriginJson = Schema.encodeEffect(
35 Schema.fromJsonString(ManagedEndpointConfirmedOrigin),
36);
37
38export const decodeConfirmedOrigin = Schema.decodeUnknownOption(
39 Schema.fromJsonString(ManagedEndpointConfirmedOrigin),
40);
41
42export function isAgentActivityPublishingEnabledValue(value: string | null): boolean {
43 return value === "true";
44}
45
46/** Whether agent-activity publishes currently leave this environment: the
47 publish opt-in secret is enabled and the relay link credentials exist.
48 Mirrors the per-publish gate in AgentAwarenessRelay, so the descriptor
49 capability never advertises publishing that the publisher would skip. */
50export const readAgentActivityPublishingActive = (
51 secrets: ServerSecretStore.ServerSecretStore["Service"],
52): Effect.Effect<boolean> =>
53 Effect.gen(function* () {
54 const readSecretString = (name: string) =>
55 secrets
56 .get(name)
57 .pipe(
58 Effect.map((bytes) =>
59 Option.isSome(bytes) ? new TextDecoder().decode(bytes.value) : null,
60 ),
61 );
62 const [enabled, url, environmentCredential] = yield* Effect.all([
63 readSecretString(PUBLISH_AGENT_ACTIVITY_SECRET),
64 readSecretString(RELAY_URL_SECRET),
65 readSecretString(RELAY_ENVIRONMENT_CREDENTIAL_SECRET),
66 ]);
67 // Empty strings are as unconfigured as missing files: the publisher's
68 // truthiness gate skips them, so the capability must too.
69 return (
70 isAgentActivityPublishingEnabledValue(enabled) &&
71 url !== null &&
72 url !== "" &&
73 environmentCredential !== null &&
74 environmentCredential !== ""
75 );
76 }).pipe(Effect.orElseSucceed(() => false));
77
78/** A non-empty secret as text, or null when it is missing, empty or unreadable. */
79const readSecretString = (name: string) =>
80 ServerSecretStore.ServerSecretStore.pipe(
81 Effect.flatMap((secrets) => secrets.get(name)),
82 Effect.map((bytes) =>
83 Option.isSome(bytes) && bytes.value.length > 0 ? new TextDecoder().decode(bytes.value) : null,
84 ),
85 Effect.orElseSucceed(() => null),
86 );
87
88/** The relay URL and environment credential, or null when not linked to T3 Connect. */
89export 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/** Whether this environment opted in to T3 Connect holding webhooks while it is offline. */
99export const readHoldWebhooksWhileOffline = readSecretString(
100 HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET,
101).pipe(Effect.map((value) => value === "true"));