iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / src / apps / start-app-config.tsmain at d68d7e1

// start-app-config.ts — THE CONFIGURATION OF AN APP ON TOP of the platform: agents, notes, admin
// and voice, TanStack Start apps that are each an OAuth client of the platform and nothing
// else — but notes, which a mind's config worker serves under its hosts, on the host's sign-in. The platform's mechanism (iterate/app-config): one `ITERATE_APP` object, and any key set alone
// as an `ITERATE_APP__*` var merged on top. iterate/docs' Docs app keeps its own copy of this file, with two fields (`denyZones`, `pkgPrNewRef`) that it reads and no app here does.
//
//   { urls: { os, agents, notes, admin, voice, kit } }
//
// scripts/lib/start-app.ts writes it from envs.ts as the Worker's one `ITERATE_APP` var
// (`startAppWorkerConfig`), and a per-PR preview swaps in the same PR's origins
// (`startAppPreviewConfig`). The apps hold no secrets, so nothing comes from Doppler. Local dev
// starts from prd's and names a local platform, or a local app, in the app's gitignored
// `.dev.vars`:
//
//   ITERATE_APP__URLS__OS=http://localhost:8788
//   ITERATE_APP__URLS__VOICE=http://localhost:5174

import { z } from "zod";
import { optionalOrigin, parseAppConfigVars } from "iterate/app-config";

/** THE SCHEMA. `urls` names the platform and every first-party app (scripts/lib/start-app.ts
 *  `FIRST_PARTY_APPS` is typed against it, so an app added there is added here). */
export const StartAppConfig = z.object({
  urls: z
    .object({
      /** THE PLATFORM this app signs in against: the default issuer, and `/api` on it the
       *  resource. Blank for an app hosted apart from any mind, which a person points at a
       *  mind by its address (packages/agents-app/scripts/standalone.ts). */
      os: optionalOrigin,
      // THE FIRST-PARTY APPS, what a link from one app to another follows. The same environment's
      // as `os`. Blank ⇒ no link: a per-PR preview names only the apps its run deploys, because
      // production does not know the preview's minds.
      agents: optionalOrigin,
      /** read by no app: nothing links to Notes, which is served under each mind's hosts. It
       *  is here because `FIRST_PARTY_APPS` is typed against `urls`, which gives a per-PR preview
       *  its Notes Worker. */
      notes: optionalOrigin,
      admin: optionalOrigin,
      voice: optionalOrigin,
      /** Kit, the voice boards' installer, deployed from iterate/kit (envs.ts `kitWorkers`), not
       *  by this repository's app scripts. Prd's and main on dev's; a per-PR preview has none. */
      kit: optionalOrigin,
    })
    .prefault({}),
});

export type StartAppConfig = z.output<typeof StartAppConfig>;

const startAppConfigByEnv = new WeakMap<object, StartAppConfig>();

/** The configuration of the isolate `env` belongs to (`import { env } from "cloudflare:workers"`):
 *  parsed on first use, then the same object every time. A malformed field throws HERE, on the
 *  first request, naming the field. */
export function startAppConfigOf(env: object): StartAppConfig {
  let config = startAppConfigByEnv.get(env);
  if (!config) {
    config = parseAppConfigVars(env, StartAppConfig, { prefix: "ITERATE_APP" });
    startAppConfigByEnv.set(env, config);
  }
  return config;
}