diff --git a/README.md b/README.md index a2c862c01..722802a55 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,26 @@ The local backend (`start_local.sh`) accepts requests from any `http://localhost ### Projects and Tangent (beta) -**Projects** groups pipelines, runs and documents, and every project belongs to a workspace. **Tangent** turns a project into a chat with an agent that can build, run and debug its pipelines. Both are beta features and are off by default. Turn them on under **Settings → Beta Features** once you have a backend that supports them: +**Projects** groups pipelines, runs and documents, and every project belongs to a workspace. **Tangent** turns a project into a chat with an agent that can build, run and debug its pipelines. Both are beta features and are off by default. Turn them on under **Settings → Beta Features** once you have what they need. -- **Projects** needs a Tangle backend that includes the Projects API (workspaces and projects). The released open-source backend does not include it yet. The backend also needs at least one active workspace. An admin can create one with **Create Workspace** in the backend's API docs (`/docs`). -- **Tangent** needs Projects, a running [Tangent shell](https://github.com/TangleML/tangent-shell), and the app started with `VITE_ENABLE_TANGENT=true`; without that variable the Tangent toggle stays locked. In development the app looks for the shell at `http://localhost:5173`. Sessions use the agent bundle named in the workspace's data as `tangentBundleId`, or `tangle` when it isn't set; the open-source shell ships `tangle-oss`, so a workspace that uses it sets `"tangentBundleId": "tangle-oss"`. `pnpm oss:dev` sets up and starts the shell next to the app. +**Projects** needs a Tangle backend that includes the Projects API (workspaces and projects) and has at least one active workspace. The released open-source backend does not include it yet. The backend's [`neurips-2026` branch](https://github.com/TangleML/tangle/tree/neurips-2026) does, and creates a "Default" workspace the first time it starts. On other backends, an admin can create a workspace with **Create Workspace** in the backend's API docs (`/docs`), for example with the body `{"name": "Default", "data": {"tangentBundleId": "tangle-oss"}}` for the open-source Tangent shell. + +**Tangent** needs all of the following: + +- Projects, set up as above. +- Saved pipelines: the app started with `VITE_REMOTE_PIPELINES=true`, on a backend that supports them (the `neurips-2026` branch does). The pipelines a project links to are then stored by the backend, where Tangent's agent can read and change them, instead of in your browser. +- A running [Tangent shell](https://github.com/TangleML/tangent-shell). In development the app looks for it at `http://localhost:5173`, where the shell's `start_local.sh` serves it. +- A workspace that names the shell's agent bundle. Sessions start with the bundle set as `tangentBundleId` in the workspace's data, or `tangle` when it isn't set. The open-source shell ships `tangle-oss`, so its workspace needs `"data": {"tangentBundleId": "tangle-oss"}`. The `neurips-2026` backend's "Default" workspace and the one `pnpm oss:dev` creates already have it. +- An LLM for the agent: `OPENAI_API_KEY` or `ANTHROPIC_API_KEY`, or both `PI_PROXY_URL` and `PI_PROXY_API_KEY` for an LLM gateway, set where the Tangent shell runs. See [Connect an LLM](https://github.com/TangleML/tangent-shell#connect-an-llm) in the shell's README. +- The app started with `VITE_ENABLE_TANGENT=true`. Without it the Tangent toggle stays locked. + +To set it all up at once, clone [tangent-shell](https://github.com/TangleML/tangent-shell) next to `tangle-ui`, start the backend, and run: + +```bash +OPENAI_API_KEY=sk-... pnpm oss:dev # or ANTHROPIC_API_KEY=sk-ant-... +``` + +`pnpm oss:dev` installs the app's dependencies, starts Tangent with the shell's `start_local.sh` (which installs the `pi` agent binary and hands it your key), creates a workspace that names the `tangle-oss` bundle if the backend has none (and warns about workspaces that don't name a bundle), writes `VITE_ENABLE_TANGENT=true` and `VITE_REMOTE_PIPELINES=true` into `.env`, and starts the app at `http://localhost:3000`. It expects the backend at `http://localhost:8000`; set `TANGLE_API_URL` to use another address. On Windows, run it in WSL or Git Bash. ### AI provider configuration @@ -80,7 +96,7 @@ development server after changes, or rebuild the frontend for deployment. | `VITE_BACKEND_API_URL` | For backend features | URL of your Tangle backend (e.g. `http://127.0.0.1:8000`). | | `TANGLE_UI_PORT` | No | Port for the local Vite dev server and Playwright base URL. Defaults to `3000`; set this in `.env`, `.env.local`, or your shell to use a different local port. | | `VITE_ENABLE_TANGENT` | For Tangent | Set to `true` to unlock the Tangent beta toggle in Settings → Beta Features. See [Projects and Tangent (beta)](#projects-and-tangent-beta). | -| `VITE_REMOTE_PIPELINES` | No | Opt in to remote pipeline storage with `true` on a backend that supports saved pipelines. Unset or `false` keeps new pipelines, imports, and copies in browser storage. Without a configured backend, storage stays local even when enabled. | +| `VITE_REMOTE_PIPELINES` | For Tangent | Opt in to remote pipeline storage with `true` on a backend that supports saved pipelines. Unset or `false` keeps new pipelines, imports, and copies in browser storage. Without a configured backend, storage stays local even when enabled. | | `VITE_ARTIFACT_RETENTION_DAYS` | No | Number of days artifacts are stored by your backend. When set, the UI shows artifact expiry dates and warns when artifacts may no longer be available. | | `VITE_BUGSNAG_API_KEY` | No | Bugsnag API key. Required alongside `VITE_TANGLE_ENV` to enable error reporting. | | `VITE_TANGLE_ENV` | No | Release stage passed to Bugsnag (e.g. `development`, `staging`, `production`). | diff --git a/package.json b/package.json index b29594f49..fe1b51be7 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "type": "module", "scripts": { "start": "vite", - "oss:dev": "tsx scripts/dev-oss.ts", + "oss:dev": "pnpm install && tsx scripts/dev-oss.ts", "build": "vite build && tsc", "build:ghpages": "VITE_BASE_URL=/tangle-ui/ VITE_GITHUB_PAGES=true vite build --config vite.config.ghpages.js && tsc", "build:hf": "VITE_REQUIRE_AUTHORIZATION=true VITE_HUGGING_FACE_AUTHORIZATION=true VITE_BASE_URL=/ VITE_GITHUB_PAGES=true vite build && tsc", diff --git a/scripts/dev-oss.ts b/scripts/dev-oss.ts index ea39593b5..9376aa62d 100644 --- a/scripts/dev-oss.ts +++ b/scripts/dev-oss.ts @@ -2,21 +2,23 @@ // // One-shot local OSS dev environment: Tangle + Tangent. // -// Orchestrates four stages: -// 1. Set up and run Tangent (the tangent-shell sibling repo). -// 2. Seed the `tangle-oss` agent bundle into Tangent's marketplace. -// 3. Ensure the Tangle API has an active workspace pointing at local Tangent. -// 4. Launch tangle-ui (dev); turn Projects and Tangent on under Settings → Beta Features. +// Orchestrates three stages: +// 1. Run Tangent (the tangent-shell sibling repo) through its own +// start_local.sh, which installs `pi`, seeds the `tangle-oss` agent bundle +// and wires LLM credentials into tangent-shell/.env. +// 2. Ensure the Tangle API has an active workspace pointing at local Tangent +// and naming its bundle. +// 3. Launch tangle-ui (dev); turn Projects and Tangent on under Settings → Beta Features. // // Tangent runs in the background; tangle-ui runs in the foreground. Ctrl-C (or // any exit) tears the background Tangent processes down. // -// All ports and paths are env-overridable; see the Config block below. Secrets -// (PI_PROXY_URL / PI_PROXY_API_KEY) are read from the environment and written -// into tangent-shell/.env so Tangent's `pi` agents can reach an LLM proxy. +// All ports and paths are env-overridable; see the Config block below. LLM +// credentials (OPENAI_API_KEY / ANTHROPIC_API_KEY, or PI_PROXY_URL with +// PI_PROXY_API_KEY for a gateway) pass through to start_local.sh. // -// Cross-platform: runs on macOS, Linux, and Windows via tsx. Usage: -// PI_PROXY_URL=https://proxy... PI_PROXY_API_KEY=sk-... pnpm oss:dev +// Needs bash for start_local.sh, so on Windows run it in WSL or Git Bash. Usage: +// OPENAI_API_KEY=sk-... pnpm oss:dev import { type ChildProcess, spawn } from "node:child_process"; import fs from "node:fs/promises"; @@ -52,7 +54,8 @@ const WORKSPACE_CREATE_BODY = env.WORKSPACE_CREATE_BODY ?? ""; const PI_PROXY_URL = env.PI_PROXY_URL ?? ""; const PI_PROXY_API_KEY = env.PI_PROXY_API_KEY ?? ""; -const HEALTH_TIMEOUT = Number(env.HEALTH_TIMEOUT ?? "120"); +// The first start installs Tangent's dependencies and `pi` before it serves. +const HEALTH_TIMEOUT = Number(env.HEALTH_TIMEOUT ?? "300"); const info = (msg: string) => console.log(`\x1b[1;34m[dev-oss]\x1b[0m ${msg}`); const warn = (msg: string) => @@ -140,16 +143,30 @@ function spawnBackground( command: string, args: string[], cwd: string, + extraEnv?: Record, ): ChildProcess { return spawn([command, ...args].join(" "), { cwd, stdio: "inherit", shell: true, detached: !isWindows, - env: { ...process.env }, + env: { ...process.env, ...extraEnv }, }); } +function portOf(url: string): string { + const parsed = new URL(url); + return parsed.port || (parsed.protocol === "https:" ? "443" : "80"); +} + +function hasLlmCredentials(): boolean { + return Boolean( + env.OPENAI_API_KEY || + env.ANTHROPIC_API_KEY || + (PI_PROXY_URL && PI_PROXY_API_KEY), + ); +} + function commandExists(cmd: string): Promise { return new Promise((resolve) => { const probe = isWindows ? `where ${cmd}` : `command -v ${cmd}`; @@ -241,6 +258,25 @@ function extractTotalCount(data: unknown): number { return 0; } +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null; +} + +function findWorkspacesWithoutBundle(data: unknown): string[] { + if (!isRecord(data) || !Array.isArray(data.workspaces)) { + return []; + } + return data.workspaces + .filter(isRecord) + .filter((workspace) => { + const bundleId = isRecord(workspace.data) + ? workspace.data.tangentBundleId + : undefined; + return typeof bundleId !== "string" || bundleId.trim().length === 0; + }) + .map((workspace) => String(workspace.name ?? workspace.id)); +} + async function promptYesNo(question: string): Promise { if (!process.stdin.isTTY) { return false; @@ -284,14 +320,16 @@ async function preflight(): Promise { await ensureTangentRepo(); - if (!(await commandExists("pi")) && !env.PI_BIN) { + if (isWindows && !(await commandExists("bash"))) { + err("Tangent's start_local.sh needs bash. Run this in WSL or Git Bash."); + process.exit(1); + } + if (!hasLlmCredentials()) { warn( - "'pi' agent binary not on PATH (set PI_BIN). Tangent will boot but agents cannot respond.", + "No LLM credentials in the environment, so Tangent's agents can't answer unless tangent-shell/.env already has some.", ); - } - if (!PI_PROXY_URL || !PI_PROXY_API_KEY) { warn( - "PI_PROXY_URL / PI_PROXY_API_KEY not set. Tangent agents need an LLM proxy to reply.", + "Re-run with OPENAI_API_KEY or ANTHROPIC_API_KEY, or with both PI_PROXY_URL and PI_PROXY_API_KEY for an LLM gateway.", ); } } @@ -304,33 +342,15 @@ async function startTangent(): Promise { return; } - const envFile = path.join(TANGENT_DIR, ".env"); - if (!(await pathExists(envFile, "file"))) { - info(`Creating ${envFile} from .env.example`); - await fs.copyFile(path.join(TANGENT_DIR, ".env.example"), envFile); - } - await setEnvVar(envFile, "TANGLE_API_URL", TANGLE_API_URL); - await setEnvVar(envFile, "TANGLE_BASE_URL", TANGLE_API_URL); - await setEnvVar(envFile, "VITE_DEFAULT_SESSION_BUNDLE_ID", BUNDLE_ID); - if (PI_PROXY_URL) { - await setEnvVar(envFile, "PI_PROXY_URL", PI_PROXY_URL); - } - if (PI_PROXY_API_KEY) { - await setEnvVar(envFile, "PI_PROXY_API_KEY", PI_PROXY_API_KEY); - } - - info("Installing Tangent dependencies (pnpm install)..."); - await run("pnpm", ["install"], TANGENT_DIR); - - // Stage 2 (seed): idempotent -- a no-op once the marketplace is populated, and - // duplicate ZIPs are skipped. Seeds every examples// bundle, incl. tangle-oss. - info(`Seeding agent bundles (incl. ${BUNDLE_ID})...`); - await run("pnpm", ["seed"], TANGENT_DIR); - info( - `Starting Tangent (pnpm dev): server ${TANGENT_SERVER_URL}, web ${TANGENT_WEB_URL}`, + `Starting Tangent (start_local.sh): server ${TANGENT_SERVER_URL}, web ${TANGENT_WEB_URL}`, ); - tangentProc = spawnBackground("pnpm", ["dev"], TANGENT_DIR); + tangentProc = spawnBackground("bash", ["start_local.sh"], TANGENT_DIR, { + WEB_PORT: portOf(TANGENT_WEB_URL), + SERVER_PORT: portOf(TANGENT_SERVER_URL), + TANGLE_API_URL, + VITE_DEFAULT_SESSION_BUNDLE_ID: BUNDLE_ID, + }); tangentProc.on("exit", (code, signal) => { tangentExit = { code, signal }; tangentProc = null; @@ -342,7 +362,7 @@ async function startTangent(): Promise { } const status = tangentExit.signal ?? tangentExit.code; return ( - `Tangent (pnpm dev) exited (${status}) before becoming healthy; see its logs above. ` + + `Tangent (start_local.sh) exited (${status}) before becoming healthy; see its logs above. ` + `A common cause is a native module (e.g. better-sqlite3) built for a different Node.js ` + `version. Try: (cd "${TANGENT_DIR}" && pnpm rebuild)` ); @@ -376,10 +396,13 @@ async function ensureWorkspace(): Promise { } let count = 0; + let workspacesWithoutBundle: string[] = []; try { const res = await fetch(`${TANGLE_API_URL}/api/workspaces/`); if (res.ok) { - count = extractTotalCount(await res.json()); + const data: unknown = await res.json(); + count = extractTotalCount(data); + workspacesWithoutBundle = findWorkspacesWithoutBundle(data); } } catch { count = 0; @@ -390,6 +413,14 @@ async function ensureWorkspace(): Promise { info( `In dev, tangle-ui falls back to ${TANGENT_WEB_URL} when a workspace has no tangentBaseUrl.`, ); + if (workspacesWithoutBundle.length > 0) { + warn( + `These workspaces don't set tangentBundleId in their data, so their sessions ask for the "tangle" bundle: ${workspacesWithoutBundle.join(", ")}.`, + ); + warn( + `Tangent here has "${BUNDLE_ID}". Set "tangentBundleId": "${BUNDLE_ID}" in a workspace's data to use it.`, + ); + } return; } @@ -399,7 +430,7 @@ async function ensureWorkspace(): Promise { name: WORKSPACE_NAME, description: "Local OSS workspace", is_active: true, - data: { tangentBaseUrl: TANGENT_WEB_URL }, + data: { tangentBaseUrl: TANGENT_WEB_URL, tangentBundleId: BUNDLE_ID }, }); info( @@ -449,9 +480,9 @@ async function startUi(): Promise { // canEnable gate); the Projects and Tangent flags themselves default to off. await setEnvVar(envFile, "VITE_ENABLE_TANGENT", "true"); await setEnvVar(envFile, "VITE_BACKEND_API_URL", TANGLE_API_URL); - - info("Installing tangle-ui dependencies (pnpm install)..."); - await run("pnpm", ["install"], UI_DIR); + // Tangent's agent works on the pipelines a project links to, so they have to + // live on the backend rather than in this browser's storage. + await setEnvVar(envFile, "VITE_REMOTE_PIPELINES", "true"); info( `Starting tangle-ui at http://localhost:${TANGLE_UI_PORT} (dev). Ctrl-C stops everything.`,