Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

3.1 KB
Raw Blame
import { spawnSync } from "node:child_process";
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
import { existsSync } from "node:fs";
import { dirname, resolve } from "node:path";

const root = resolve(import.meta.dirname, "..");
if (existsSync(resolve(root, ".env.local"))) process.loadEnvFile(resolve(root, ".env.local"));
const connection = process.env.DUCKDB_CONNECTION || "./data/jaffle_shop.duckdb";
if (!connection.startsWith("md:"))
  await mkdir(dirname(resolve(root, connection)), { recursive: true });
const env = {
  ...process.env,
  DUCKDB_CONNECTION: connection.startsWith("md:") ? connection : resolve(root, connection),
  UV_PROJECT_ENVIRONMENT:
    process.env.UV_PROJECT_ENVIRONMENT ?? resolve(root, "node_modules/.dbt-venv"),
};
for (const command of [["build"], ["docs", "generate"]]) {
  const result = spawnSync(
    "uv",
    ["run", "--python", "3.13", "--", "dbt", ...command, "--profiles-dir", "."],
    {
      cwd: resolve(root, "dbt"),
      env,
      stdio: "inherit",
      timeout: 120_000,
    },
  );
  if (result.error) throw result.error;
  if (result.status !== 0) process.exit(result.status ?? 1);
}
const manifest = JSON.parse(await readFile(resolve(root, "dbt/target/manifest.json"), "utf8"));
const warehouseCatalog = JSON.parse(
  await readFile(resolve(root, "dbt/target/catalog.json"), "utf8"),
);
const models = Object.values(manifest.nodes)
  .filter((node) => node.resource_type === "model" && node.tags.includes("company_brain"))
  .map((node) => {
    if (node.config.materialized !== "table" || node.schema !== "main") {
      throw new Error("Exposed models must be materialized tables in the main schema.");
    }
    if (!/^[a-z][a-z0-9_]*$/.test(node.alias)) throw new Error("Use snake_case relation names.");
    const columns = warehouseCatalog.nodes[node.unique_id]?.columns;
    if (!columns) throw new Error(`Missing catalog columns for ${node.name}.`);
    return {
      name: node.name,
      relation: `main.${node.alias}`,
      description: node.description,
      sql: node.compiled_code,
      meta: node.config.meta,
      dependsOn: node.depends_on.nodes
        .map((id) => manifest.nodes[id])
        .filter((dependency) => dependency?.resource_type === "model")
        .map((dependency) => dependency.name),
      columns: Object.values(columns).map((column) => ({
        name: column.name,
        type: column.type,
        description: node.columns[column.name]?.description ?? "",
      })),
    };
  })
  .sort((a, b) => a.name.localeCompare(b.name));
if (models.length === 0) throw new Error("Tag at least one analytics model with company_brain.");
const catalogPath = resolve(root, "agent/lib/catalog.ts");
await mkdir(dirname(catalogPath), { recursive: true });
await writeFile(
  `${catalogPath}.tmp`,
  `// Generated by pnpm data:build from the successful dbt build.\nexport default ${JSON.stringify({ builtAt: manifest.metadata.generated_at, models }, null, 2)};\n`,
);
await rename(`${catalogPath}.tmp`, catalogPath);
console.log(
  `Published ${models.length} models to agent/lib/catalog.ts. Rebuild/restart the agent to load them.`,
);