Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

7.5 KB
Raw Blame
import { readFile } from "node:fs/promises";
import { join } from "node:path";

import { channelEntries } from "@eve/catalog";

interface RegistryFile {
  path: string;
  target?: string;
}

interface RegistryItem {
  name: string;
  dependencies?: string[];
  files?: RegistryFile[];
  meta?: {
    eve?: {
      hidden?: boolean;
      setup?:
        | {
            command?: string;
            package?: string;
            bin?: string;
            args?: string[];
          }
        | Array<{
            command?: string;
            package?: string;
            bin?: string;
            args?: string[];
          }>;
    };
  };
}

interface Registry {
  items: RegistryItem[];
}

const registrySlugsByCatalogSlug: Readonly<Record<string, string>> = {
  eve: "web",
  linq: "linq",
  photon: "photon-imessage",
  "linear-agent": "linear",
};

const setupKindsByCatalogSlug: Readonly<Record<string, string>> = {
  slack: "slack",
  discord: "discord",
  github: "github",
  "linear-agent": "linear",
  eve: "web",
  linq: "linq",
  photon: "photon",
  teams: "teams",
};

const adapterDependenciesByCatalogSlug: Readonly<Record<string, string>> = {
  "chat-sdk-gchat": "@chat-adapter/gchat",
  "chat-sdk-whatsapp": "@chat-adapter/whatsapp",
  "chat-sdk-x": "@chat-adapter/x",
  "chat-sdk-messenger": "@chat-adapter/messenger",
  "chat-sdk-zernio": "@zernio/chat-sdk-adapter",
  "chat-sdk-velt": "@veltdev/chat-sdk-adapter",
  "chat-sdk-sendblue": "chat-adapter-sendblue",
  "chat-sdk-novu": "@novu/chat-sdk-adapter",
  "chat-sdk-liveblocks": "@liveblocks/chat-sdk-adapter",
  "chat-sdk-kapso": "@kapso/chat-adapter",
  "chat-sdk-dial": "@getdial/chat-sdk-adapter",
  "chat-sdk-agentphone": "@agentphone/chat-sdk-adapter",
  "chat-sdk-lark": "@larksuite/vercel-chat-adapter",
  "chat-sdk-beeper": "@beeper/chat-adapter-matrix",
  "chat-sdk-resend": "@resend/chat-sdk-adapter",
};

const targetSlugsByCatalogSlug: Readonly<Record<string, string>> = {
  "linear-agent": "linear",
  "chat-sdk-gchat": "gchat",
  "chat-sdk-whatsapp": "whatsapp",
  "chat-sdk-x": "x",
  "chat-sdk-messenger": "messenger",
  "chat-sdk-zernio": "zernio",
  "chat-sdk-velt": "velt",
  "chat-sdk-sendblue": "sendblue",
  "chat-sdk-novu": "novu",
  "chat-sdk-liveblocks": "liveblocks",
  "chat-sdk-kapso": "kapso",
  "chat-sdk-dial": "dial",
  "chat-sdk-agentphone": "agentphone",
  "chat-sdk-lark": "lark",
  "chat-sdk-beeper": "beeper",
  "chat-sdk-resend": "resend",
};

const fileValidationExemptCatalogSlugs = new Set(["chat-sdk-gmail"]);

// Adapters that can't edit a sent message; their templates must post each reply once.
const nonStreamingCatalogSlugs = new Set([
  "chat-sdk-agentphone",
  "chat-sdk-dial",
  "chat-sdk-kapso",
  "chat-sdk-messenger",
  "chat-sdk-resend",
  "chat-sdk-sendblue",
  "chat-sdk-whatsapp",
  "chat-sdk-x",
]);

const docsRoot = join(import.meta.dirname, "..");
const registry = JSON.parse(await readFile(join(docsRoot, "registry.json"), "utf8")) as Registry;
const channelItems = registry.items.filter((item) => item.name.startsWith("channel/"));

// Framework variants of Web Chat that `eve add channel/web` installs. They are
// one catalog channel, so they stay hidden and share the Web Chat setup.
const webChatVariantNames = new Set(["channel/web-tanstack"]);
const items = channelItems.filter((item) => !webChatVariantNames.has(item.name));
const webChatVariants = channelItems.filter((item) => webChatVariantNames.has(item.name));
const registryEntries = channelEntries().filter((entry) => entry.surfaces.registry);
const expectedSlugs = registryEntries.map(
  (entry) => registrySlugsByCatalogSlug[entry.slug] ?? entry.slug,
);
const actualSlugs = items.map((item) => item.name.slice("channel/".length));

if (JSON.stringify(actualSlugs) !== JSON.stringify(expectedSlugs)) {
  throw new Error(
    `Channel registry entries do not match the catalog.\nExpected: ${expectedSlugs.join(", ")}\nActual: ${actualSlugs.join(", ")}`,
  );
}

function declaredSetups(item: RegistryItem) {
  const declaredSetup = item.meta?.eve?.setup;
  return declaredSetup === undefined
    ? undefined
    : Array.isArray(declaredSetup)
      ? declaredSetup
      : [declaredSetup];
}

function assertDelegatesSetup(item: RegistryItem, setupKind: string): void {
  const expectedArgs = ["integration", "setup", setupKind];
  if (
    JSON.stringify(declaredSetups(item)) !==
    JSON.stringify([{ command: "eve", package: "eve", bin: "eve", args: expectedArgs }])
  ) {
    throw new Error(
      `Registry item "${item.name}" must delegate setup to eve integration setup ${setupKind}.`,
    );
  }
}

function assertWebChatItem(item: RegistryItem): void {
  if (
    item.dependencies?.some((dependency) => dependency === "ai" || dependency.startsWith("ai@"))
  ) {
    throw new Error(
      `Registry item "${item.name}" must preserve the agent's existing AI SDK dependency.`,
    );
  }
  if (item.files?.some((file) => file.target === "tsconfig.json")) {
    throw new Error(
      `Registry item "${item.name}" must let eve prepare tsconfig.json before shadcn installs files.`,
    );
  }
}

for (const name of webChatVariantNames) {
  const item = webChatVariants.find((candidate) => candidate.name === name);
  if (item === undefined) throw new Error(`apps/docs/registry.json must define ${name}.`);
  if (item.meta?.eve?.hidden !== true) {
    throw new Error(
      `Registry item "${name}" must be hidden; users install it through channel/web.`,
    );
  }
  assertWebChatItem(item);
  assertDelegatesSetup(item, "web");
}

for (const [index, item] of items.entries()) {
  const setups = declaredSetups(item);
  if (
    setups?.some(
      (setup) =>
        setup.command === undefined ||
        setup.package === undefined ||
        setup.bin === undefined ||
        setup.args === undefined,
    )
  ) {
    throw new Error(
      `Registry item "${item.name}" setup entries must declare command, package, bin, and args.`,
    );
  }

  const entry = registryEntries[index];
  if (entry === undefined) throw new Error(`Unexpected channel registry item "${item.name}".`);
  if (entry.slug === "eve") assertWebChatItem(item);

  const setupKind = setupKindsByCatalogSlug[entry.slug];
  if (setupKind !== undefined) {
    assertDelegatesSetup(item, setupKind);
    continue;
  }

  if (fileValidationExemptCatalogSlugs.has(entry.slug)) continue;

  const expectedPath = `registry/channels/${entry.slug}.ts`;
  const expectedTarget = `agent/channels/${targetSlugsByCatalogSlug[entry.slug] ?? entry.slug}.ts`;
  const file = item.files?.[0];
  if (item.files?.length !== 1 || file?.path !== expectedPath || file.target !== expectedTarget) {
    throw new Error(
      `Registry item "${item.name}" must write ${expectedPath} to ${expectedTarget}.`,
    );
  }
  const source = await readFile(join(docsRoot, expectedPath), "utf8");
  if (nonStreamingCatalogSlugs.has(entry.slug) && !source.includes("streaming: false")) {
    throw new Error(`Registry item "${item.name}" must disable unsupported streaming edits.`);
  }

  const adapterDependency = adapterDependenciesByCatalogSlug[entry.slug];
  if (entry.slug.startsWith("chat-sdk-") && adapterDependency === undefined) {
    throw new Error(`Registry item "${item.name}" has no adapter dependency mapping.`);
  }
  if (adapterDependency !== undefined) {
    const dependencies = item.dependencies ?? [];
    if (
      !dependencies.includes("chat") ||
      !dependencies.includes(adapterDependency) ||
      !dependencies.includes("@chat-adapter/state-memory")
    ) {
      throw new Error(
        `Registry item "${item.name}" must install Chat SDK, ${adapterDependency}, and its state adapter.`,
      );
    }
  }
}