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

config.ts

100 lines3.6 KB

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";
20export const CLOUD_LINK_REVOKED = "cloud-link-revoked";
21​
22export const encodeEndpointRuntimeConfigJson = Schema.encodeEffect(
23 Schema.fromJsonString(RelayManagedEndpointRuntimeConfig),
24);
25​
26export const decodeRuntimeConfig = Schema.decodeUnknownOption(
27 Schema.fromJsonString(RelayManagedEndpointRuntimeConfig),
28);
29​
30export const ManagedEndpointConfirmedOrigin = Schema.Struct({
31 config: RelayManagedEndpointRuntimeConfig,
32 origin: RelayManagedEndpointOrigin,
33});
34​
35export const encodeConfirmedOriginJson = Schema.encodeEffect(
36 Schema.fromJsonString(ManagedEndpointConfirmedOrigin),
37);
38​
39export const decodeConfirmedOrigin = Schema.decodeUnknownOption(
40 Schema.fromJsonString(ManagedEndpointConfirmedOrigin),
41);
42​
43export function isAgentActivityPublishingEnabledValue(value: string | null): boolean {
44 return value === "true";
46​
47/**
48 * Mirrors the per-publish gate in AgentAwarenessRelay, so the descriptor
49 capability never advertises publishing that the publisher would skip.
50 */
51export 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​
80const 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​
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​
98export const readHoldWebhooksWhileOffline = readSecretString(
99 HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET,
100).pipe(Effect.map((value) => value === "true"));