iterate.com / cloud

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

cloud / internal-packages / installer / worker / main.jsmain at aacbc74

// THE CLOUD WORKER: one origin for the wizard and its run (index.js, deploy-run.js), a deployment's
// enrolment and the operator's way in (access.js), and iterate's clients at GitHub, Google and X
// (internal-packages/cloud-clients), Cap'n Web AI Gateway lending (internal-packages/cloud-ai),
// the one MCP server for every deployment (mcp-relay.js), and
// the few operations the Cloud's app in the iterate mind may ask (service.js).
import cloudAi, { CloudUsageLedger, handles as cloudAiHandles } from "@iterate-com/cloud-ai";
import clients, { Bot, handles, Outbox, Routes } from "@iterate-com/cloud-clients";
import { WorkerEntrypoint } from "cloudflare:workers";
import { serviceCallerOf } from "iterate/principal";
import mcpRelay, { handles as mcpRelayHandles } from "./mcp-relay.js";
import { handle as service, handles as serviceHandles, serviceCall } from "./service.js";
import installer from "./index.js";

export { Deployer } from "./deploy-run.js";
export { Bot, CloudUsageLedger, Outbox, Routes };

/** The operations of service.js as `itx.services.cloud.<method>(…)`: service.js says who may
 *  call and what each method takes. */
export class Service extends WorkerEntrypoint {
  #call(caller, work) {
    return serviceCall(this.env, caller, serviceCallerOf, work);
  }
  /** `{ deployment, owner: { email, userId } }` → `{ installation, deployment, created }` */
  installationCreate(caller, args) {
    return this.#call(caller, (ops) => ops.create(args));
  }
  /** `{ deployment }` → the status */
  installationStatus(caller, named) {
    return this.#call(caller, (ops) => ops.status(named));
  }
  /** `{ deployment, choices }` → the status
   *  @param {unknown} caller @param {{ choices?: unknown } & Record<string, unknown>} [args] */
  installationChoose(caller, { choices, ...named } = {}) {
    return this.#call(caller, (ops) => ops.choose(named, choices));
  }
  /** `{ deployment, kind }` → `{ url, expiresAt }`: a single-use link for a button
   *  @param {unknown} caller @param {{ kind?: unknown } & Record<string, unknown>} [args] */
  installationLink(caller, { kind, ...named } = {}) {
    return this.#call(caller, (ops) => ops.link(named, { kind }));
  }
  /** `{ deployment }` → the status, after a look at the waits */
  installationPoke(caller, named) {
    return this.#call(caller, (ops) => ops.poke(named));
  }
  /** `{ deployment }` → `{ resources }` */
  installationResources(caller, named) {
    return this.#call(caller, (ops) => ops.resources(named));
  }
  /** `{ domain }` → `{ domain, enrolled, handedOver }`
   *  @param {unknown} caller @param {{ domain?: unknown }} [args] */
  deploymentAt(caller, { domain } = {}) {
    return this.#call(caller, (ops) => ops.deploymentAt(domain));
  }
  /** `{ deployment, domain }` → the status: a deployment made elsewhere, tied to the installation
   *  @param {unknown} caller @param {{ domain?: unknown } & Record<string, unknown>} [args] */
  installationDeployedAt(caller, { domain, ...named } = {}) {
    return this.#call(caller, (ops) => ops.deployedAt(named, { domain }));
  }
  /** `{ email }` → `{ email, installations }`: the ids of an owner's installations */
  ownerInstallations(caller, asked) {
    return this.#call(caller, (ops) => ops.ownerInstallations(asked));
  }
  /** `{ deployment }` → `{ ok, installation }` */
  installationForget(caller, named) {
    return this.#call(caller, (ops) => ops.forget(named));
  }
  /** `{ domain }` → `{ ok, domain, key }` */
  enrol(caller, asked) {
    return this.#call(caller, (ops) => ops.enrol(asked));
  }
  /** → `{ keys: [{ actor, publicKey, label, paths }] }` */
  cloudKey(caller) {
    return this.#call(caller, (ops) => ops.cloudKey());
  }
  /** `{ domain }` → `{ ok, domain }` */
  forgetEnrolment(caller, asked) {
    return this.#call(caller, (ops) => ops.forgetEnrolment(asked));
  }
  /** `{ deployment, summary }` → `{ ok, handover, sent }` */
  handover(caller, args) {
    return this.#call(caller, (ops) => ops.handover(args));
  }
}

export default {
  fetch: (request, env, ctx) => {
    const url = new URL(request.url);
    // The relay is a prototype: it answers only where the config turns it on and gives it a KV
    // namespace of its own (wrangler.jsonc). Elsewhere its paths answer 404, not the wizard's
    // page: an MCP client expects no HTML there.
    if (mcpRelayHandles(url))
      return env.MCP_RELAY === "on" && env.RELAY
        ? mcpRelay.fetch(request, env, ctx)
        : new Response("Not found\n", { status: 404 });
    // what the Cloud's app in the iterate mind may ask (service.js): a bearer, never a browser
    if (serviceHandles(url)) return service(request, env);
    // Cap'n Web AI Gateway lending for enrolled minds (internal-packages/cloud-ai)
    if (cloudAiHandles(url)) return cloudAi.fetch(request, env);
    return handles(url) ? clients.fetch(request, env) : installer.fetch(request, env);
  },
};