diff --git a/.claude/rules/sim-api-contracts.md b/.claude/rules/sim-api-contracts.md index 643f31c65aa..9e8e90dc7c1 100644 --- a/.claude/rules/sim-api-contracts.md +++ b/.claude/rules/sim-api-contracts.md @@ -35,7 +35,7 @@ Placement: the annotation must immediately precede the call or cast; up to three ```ts // boundary-raw-fetch: streaming SSE chunks must be processed as they arrive -const response = await fetch(`/api/copilot/chat/stream?chatId=${chatId}`, { signal }) +const response = await fetch(`/api/mothership/chat/stream?streamId=${streamId}`, { signal }) ``` ```ts diff --git a/.cursor/rules/sim-api-contracts.mdc b/.cursor/rules/sim-api-contracts.mdc index 840e3bbbc23..483379b72a1 100644 --- a/.cursor/rules/sim-api-contracts.mdc +++ b/.cursor/rules/sim-api-contracts.mdc @@ -33,7 +33,7 @@ Placement: the annotation must immediately precede the call or cast; up to three ```ts // boundary-raw-fetch: streaming SSE chunks must be processed as they arrive -const response = await fetch(`/api/copilot/chat/stream?chatId=${chatId}`, { signal }) +const response = await fetch(`/api/mothership/chat/stream?streamId=${streamId}`, { signal }) ``` ```ts diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index f03ff816348..7bc4ac74449 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -5,7 +5,6 @@ /apps/sim/app/api/mothership/chat/ @simstudioai/mothership /apps/sim/app/api/mothership/chats/ @simstudioai/mothership /apps/sim/app/api/mothership/execute/ @simstudioai/mothership -/apps/sim/app/api/v1/copilot/chat/ @simstudioai/mothership # Server-side stream orchestration, persistence, and protocol. /apps/sim/lib/copilot/chat/ @simstudioai/mothership diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index c009be91430..4277061d49a 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -179,31 +179,34 @@ Access the application at [http://localhost:3000/](http://localhost:3000/) #### Using Local Models -To use local models with Sim: +To use local models with Sim, either point it at an Ollama server you already run or start one alongside the stack. -1. Install Ollama and pull models: +- **Ollama on your machine**: install it, pull a model, and point the app at the host: - ```bash - # Install Ollama (if not already installed) - curl -fsSL https://ollama.ai/install.sh | sh + ```bash + curl -fsSL https://ollama.ai/install.sh | sh + ollama pull gemma3:4b - # Pull a model (e.g., gemma3:4b) - ollama pull gemma3:4b - ``` + echo 'OLLAMA_URL=http://host.docker.internal:11434' >> .env + docker compose -f docker-compose.prod.yml up -d + ``` -2. Start Sim with local model support: + Ollama must listen on an interface the container can reach (`OLLAMA_HOST=0.0.0.0:11434`). For an Ollama server elsewhere, set `OLLAMA_URL` to its address instead. - ```bash - # With NVIDIA GPU support - docker compose --profile local-gpu -f docker-compose.ollama.yml up -d +- **Bundled Ollama**: layer `docker-compose.ollama.yml` on the production file, with exactly one of the `gpu` or `cpu` profiles, then pull the starter model once: - # Without GPU (CPU only) - docker compose --profile local-cpu -f docker-compose.ollama.yml up -d + ```bash + echo 'OLLAMA_URL=http://ollama:11434' >> .env - # If hosting on a server, update the environment variables in the docker-compose.prod.yml file - # to include the server's public IP then start again (OLLAMA_URL to i.e. http://1.1.1.1:11434) - docker compose -f docker-compose.prod.yml up -d - ``` + # With NVIDIA GPU support + docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile gpu up -d + + # Without GPU (CPU only) + docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile cpu up -d + + # Pull gemma3:4b into the running server + docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile setup run --rm model-setup + ``` ### Option 3: Manual Setup diff --git a/README.md b/README.md index e1bc33f0b7e..de4131f0e0b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@

Ask DeepWiki - Set Up with Cursor + Set Up with Cursor

diff --git a/apps/docs/content/docs/platform/self-hosting/docker.mdx b/apps/docs/content/docs/platform/self-hosting/docker.mdx index 3666d5fa8d2..11135d005ec 100644 --- a/apps/docs/content/docs/platform/self-hosting/docker.mdx +++ b/apps/docs/content/docs/platform/self-hosting/docker.mdx @@ -145,30 +145,64 @@ docker compose -f docker-compose.prod.yml up -d Container `localhost` is the container itself. The default `http://localhost:11434` does not reach an Ollama server on the host. -### Development Ollama stack +### Bundled Ollama -`docker-compose.ollama.yml` builds the app, realtime, and migrations from source. It does not bundle Redis or cron. Without Redis, pub/sub is process-local and CLI pairing approval has no fallback; idempotency and execution-progress storage can fall back to PostgreSQL. Without a scheduler, background jobs do not run. +`docker-compose.ollama.yml` is an overlay on `docker-compose.prod.yml`: it adds an Ollama server to the production stack and points the app at it, so the stack keeps the published images, the required secrets, Redis, and cron. Ollama is reachable only inside the Compose network, as `ollama:11434`. -Use a server profile together with `setup`; `setup` alone starts no Ollama server and the model setup waits: +Add the address to `.env` first. The overlay refuses to start without it, and keeping it in `.env` means a later run of `docker-compose.prod.yml` alone still reaches Ollama: ```bash -# NVIDIA GPU -docker compose -f docker-compose.ollama.yml --profile gpu --profile setup up -d +OLLAMA_URL=http://ollama:11434 +``` + +Start the stack with exactly one server profile: + +```bash +# NVIDIA GPU (requires the NVIDIA Container Toolkit) +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile gpu up -d # CPU only -docker compose -f docker-compose.ollama.yml --profile cpu --profile setup up -d +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile cpu up -d +``` + +Pull the starter model, `gemma3:4b`, once the server is running: + +```bash +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile setup run --rm model-setup ``` -Setup pulls `gemma3:4b`. To pull another model: +To pull another model, exec into the server for your profile: ```bash # GPU profile -docker compose -f docker-compose.ollama.yml exec ollama ollama pull llama3.2 +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile gpu exec ollama ollama pull llama3.2 # CPU profile -docker compose -f docker-compose.ollama.yml exec ollama-cpu ollama pull llama3.2 +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile cpu exec ollama-cpu ollama pull llama3.2 ``` +#### Migrating from the previous Ollama stack + +The previous `docker-compose.ollama.yml` was a standalone stack with the fixed project name `sim-with-ollama`, so its database and models live in the `sim-with-ollama_postgres_data` and `sim-with-ollama_ollama_data` volumes. The overlay takes its project name from `docker-compose.prod.yml`, and started as-is it creates empty volumes instead. To keep your data: + +1. From the repository directory, stop the old stack without `-v`, so its volumes are kept. The old file is gone, so name the project instead: + + ```bash + docker compose -p sim-with-ollama down + ``` + +2. Add the old project name and database password to `.env`, so the overlay and the `sim-setup` CLI both resolve the same project and volumes. The old stack's password defaulted to `postgres` unless you set one: + + ```bash + COMPOSE_PROJECT_NAME=sim-with-ollama + POSTGRES_PASSWORD=postgres + OLLAMA_URL=http://ollama:11434 + ``` + +3. Start the stack with the overlay and your profile, as above. + +Ollama is no longer published on host port `11434`. Reach it with `docker compose exec`, as in the model pull commands above. + ## LM Studio Start [LM Studio's local server](https://lmstudio.ai/docs/developer/core/server), load a model, enable **Serve on Local Network**, and [configure API authentication](https://lmstudio.ai/docs/developer/core/authentication). Save these values beside your Compose file: @@ -184,13 +218,14 @@ Recreate the app using the file that started your installation: ```bash COMPOSE_FILE=docker-compose.prod.yml -# For a source-built stack, use docker-compose.local.yml or docker-compose.ollama.yml. +# For a source-built stack, use docker-compose.local.yml. With bundled Ollama, +# add the overlay: -f docker-compose.prod.yml -f docker-compose.ollama.yml docker compose -f "$COMPOSE_FILE" up -d --force-recreate simstudio ``` ## The `sim-setup` CLI -Invoke commands as `bunx sim-setup ` unless the package is installed globally. Run `doctor`, `config`, and the wizard from the configuration directory. Lifecycle commands discover running production/local Compose stacks, source checkouts, and Helm releases; Ollama-stack installs are not managed. +Invoke commands as `bunx sim-setup ` unless the package is installed globally. Run `doctor`, `config`, and the wizard from the configuration directory. Lifecycle commands discover running production/local Compose stacks, source checkouts, and Helm releases. A stack started with the Ollama overlay is managed as a production install, through `docker-compose.prod.yml` alone: the CLI acts on the Sim services and leaves the Ollama container untouched. | Command | Behavior | | --- | --- | diff --git a/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx b/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx index 31f471f901e..277229e234e 100644 --- a/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx +++ b/apps/docs/content/docs/platform/self-hosting/environment-variables.mdx @@ -260,7 +260,7 @@ Configure at least one. Every configured provider stays active and is tried in o | `RESUME_EXECUTION_CONCURRENCY_LIMIT` | `50` | Trigger.dev resume task concurrency | | `EGRESS_ALLOWED_HOSTS` | unset | Comma-separated hostnames outbound requests may reach on a private network. Leading wildcard allowed, e.g. `host.docker.internal,*.svc.cluster.local`. Not honored for URLs harvested from content or a third-party response, nor for an HTTP block's `proxyUrl` | | `EGRESS_ALLOWED_IP_RANGES` | unset | Comma-separated CIDRs or IPs outbound requests may reach on a private network, e.g. `10.0.0.0/8`. Same exclusions | -| `ALLOW_PRIVATE_DATABASE_HOSTS` | unset | **Deprecated.** Vouches for the entire private address space, for database, cache, and mail connector hosts only. Replace it with the two settings above naming specific destinations | +| `ALLOW_PRIVATE_DATABASE_HOSTS` | unset | **Removed.** Has no effect; the app logs an error at startup if it is still set. Name each database, cache, or mail host in `EGRESS_ALLOWED_HOSTS` instead; a host reached by IP address, including any IPv6 literal (which `EGRESS_ALLOWED_HOSTS` rejects), takes a single-address entry such as `10.0.0.5/32` in `EGRESS_ALLOWED_IP_RANGES`. Do not allowlist whole private ranges, which would widen what workflow HTTP blocks can reach, or `localhost`, where Sim's own database and Redis listen | | `DOMAIN_VERIFICATION_NAMESERVERS` | `1.1.1.1,8.8.8.8` | Comma-separated nameserver IPs that SSO domain verification sends its TXT lookup to. Set it when the network only allows DNS to an internal resolver. Entries that are not IP addresses are ignored | A malformed entry in either allowlist stops the app at startup with a message naming the setting. See [the SSRF boundary](/platform/self-hosting/security#the-ssrf-boundary) for the accepted syntax and for what the allowlist does and does not reach. diff --git a/apps/docs/content/docs/platform/self-hosting/observability.mdx b/apps/docs/content/docs/platform/self-hosting/observability.mdx index f69ffec0c0d..7a308463faf 100644 --- a/apps/docs/content/docs/platform/self-hosting/observability.mdx +++ b/apps/docs/content/docs/platform/self-hosting/observability.mdx @@ -71,7 +71,7 @@ Deploy the PII service, configure its internal network path, and enable the stor ## Metrics - The default app and realtime images **do not expose a `/metrics` endpoint**. The chart's `monitoring.serviceMonitor` option exists for builds that do — enabling it against the stock images produces a ServiceMonitor that scrapes nothing. + The app and realtime images **do not expose a `/metrics` endpoint**, and the Helm chart ships no ServiceMonitor. There is nothing for Prometheus to scrape from the Sim pods directly. Until an application metrics endpoint ships, build alerting from the signals that do exist: diff --git a/apps/docs/content/docs/platform/self-hosting/security.mdx b/apps/docs/content/docs/platform/self-hosting/security.mdx index d1cab05df81..5cfb65dc7a5 100644 --- a/apps/docs/content/docs/platform/self-hosting/security.mdx +++ b/apps/docs/content/docs/platform/self-hosting/security.mdx @@ -271,7 +271,7 @@ EGRESS_ALLOWED_HOSTS=host.docker.internal Review these compatibility cases when upgrading: -- **`ALLOW_PRIVATE_DATABASE_HOSTS`** still works, but it is deprecated and logs a warning at startup. It vouches for the whole private address space, loopback included, for database, cache, and mail connector hosts. Replace it with `EGRESS_ALLOWED_HOSTS` or `EGRESS_ALLOWED_IP_RANGES` naming the hosts you actually use. +- **`ALLOW_PRIVATE_DATABASE_HOSTS`** has been removed and no longer has any effect; the app logs an error at startup if it is still set. Name the specific database, cache, and mail hosts you use in `EGRESS_ALLOWED_HOSTS`. A host you reach by IP address needs a single-address entry in `EGRESS_ALLOWED_IP_RANGES` instead (for example `10.0.0.5/32`, or `fd00::5/128`); IPv6 literals in particular must go there, since `EGRESS_ALLOWED_HOSTS` rejects them. Do not allowlist whole private ranges in its place, since the allowlist also applies to workflow HTTP blocks and that would widen what every workflow author can reach, and do not allowlist `localhost`, where Sim's own database and Redis listen. - **1Password Connect** on a private, non-loopback address, and an **MCP server** on a private address or reached through a DNS name that points at loopback, are no longer reachable implicitly. Name them. - **`ALLOWED_MCP_DOMAINS`** governs which domains may be used; it no longer disables the address check, so an MCP server on a private address needs the allowlist too. - **Content fetches** — an image URL, a file imported by URL, an MCP OAuth endpoint on a different origin than the MCP server itself — never use the allowlist. Those destinations have to be publicly routable. (An SSO OIDC *discovery* URL is a configured endpoint and does use the allowlist; the endpoints named *inside* the discovery document are validated as content when Sim registers the provider, so an internal IdP endpoint is refused there and cannot be allowlisted — provide the endpoints explicitly to have them validated as configured endpoints instead.) diff --git a/apps/docs/content/docs/platform/self-hosting/troubleshooting.mdx b/apps/docs/content/docs/platform/self-hosting/troubleshooting.mdx index 8630a836884..8924875a920 100644 --- a/apps/docs/content/docs/platform/self-hosting/troubleshooting.mdx +++ b/apps/docs/content/docs/platform/self-hosting/troubleshooting.mdx @@ -7,7 +7,7 @@ Start with the checks for your deployment type, then follow the symptom below. ## Run `sim-setup doctor` -On a production or local-development Compose install, or a source checkout, run the built-in checker first. It does not detect the Ollama stack. It checks configuration and common dependency failures. It reads env files, so it is not a Kubernetes tool — on Helm, skip to the checks below. +On a production or local-development Compose install (including one running the Ollama overlay), or a source checkout, run the built-in checker first. It checks configuration and common dependency failures. It reads env files, so it is not a Kubernetes tool — on Helm, skip to the checks below. ```bash bunx sim-setup doctor @@ -193,12 +193,12 @@ OLLAMA_URL=http://host.docker.internal:11434 Sim identifies dynamically discovered LM Studio and vLLM models by their `vllm/` prefix. If the endpoint is unavailable and you manually enter the raw LM Studio model identifier, Sim treats that unknown identifier as an Ollama model. -1. Set `VLLM_BASE_URL`, and `VLLM_API_KEY` too if you enabled LM Studio's API authentication — without the key, discovery requests omit the bearer token and fail. No Compose file sets either for you — `docker-compose.ollama.yml` only sets `OLLAMA_URL` — so add them to the `.env` file next to your Compose file — `simstudio` declares `env_file: .env` in every shipped file — then recreate the service and confirm it landed: +1. Set `VLLM_BASE_URL`, and `VLLM_API_KEY` too if you enabled LM Studio's API authentication — without the key, discovery requests omit the bearer token and fail. No Compose file sets either for you, so add them to the `.env` file next to your Compose file — `simstudio` declares `env_file: .env` in every shipped file — then recreate the service and confirm it landed: - Use the Compose file that started your installation — `docker-compose.prod.yml` for the documented production setup, `docker-compose.local.yml` for a source-built stack, or `docker-compose.ollama.yml` if you started from the Ollama stack: + Use the Compose file that started your installation — `docker-compose.prod.yml` for the documented production setup (also when you run the Ollama overlay on top of it) or `docker-compose.local.yml` for a source-built stack: ```bash - COMPOSE_FILE=docker-compose.prod.yml # or docker-compose.local.yml, docker-compose.ollama.yml + COMPOSE_FILE=docker-compose.prod.yml # or docker-compose.local.yml docker compose -f "$COMPOSE_FILE" up -d --force-recreate simstudio docker compose -f "$COMPOSE_FILE" exec simstudio printenv VLLM_BASE_URL ``` diff --git a/apps/docs/content/docs/platform/self-hosting/upgrades.mdx b/apps/docs/content/docs/platform/self-hosting/upgrades.mdx index 18557eb2671..828d54e0e76 100644 --- a/apps/docs/content/docs/platform/self-hosting/upgrades.mdx +++ b/apps/docs/content/docs/platform/self-hosting/upgrades.mdx @@ -138,8 +138,8 @@ kubectl logs -n simstudio deploy/sim-app -c migrations --tail=100 ```bash bunx sim-setup update -# The file that started your install. The Ollama stack is not managed by -# the CLI at all — see below. +# The file that started your install. A stack running the Ollama overlay is a +# docker-compose.prod.yml install — see below. COMPOSE_FILE=docker-compose.prod.yml # COMPOSE_FILE=docker-compose.local.yml docker compose -f "$COMPOSE_FILE" logs migrations @@ -160,19 +160,18 @@ Inspect the result with `bunx sim-setup logs`, which targets whichever Compose f Before updating a pinned production install, replace `SIM_VERSION` in `.env` with the chosen release tag. The update command pulls that configured tag; it does not automatically advance a pin. -The CLI detects only those two files. An install started from `docker-compose.ollama.yml` is invisible to it: `update`, `logs`, and `status` report no install, or — in a source checkout that also carries per-application env files — report that checkout's dev install instead. Upgrade that stack directly: +The CLI detects only those two files. A stack started with the `docker-compose.ollama.yml` overlay is detected as a `docker-compose.prod.yml` install, and `update` upgrades its Sim services through that file alone, keeping `OLLAMA_URL` from `.env`. It does not touch the Ollama container. To refresh the Ollama image as well, run the update with both files: ```bash -# That file builds the app, realtime server, and migrator from source, so the -# upgrade is a checkout plus a rebuild — pulling only refreshes Ollama and -# Postgres. The Ollama service is profile-scoped, so pass the profile you -# installed with or Compose silently skips it. +# The Ollama service is profile-scoped, so pass the profile you installed +# with or Compose silently skips it. PROFILE=gpu # or cpu -git pull -docker compose -f docker-compose.ollama.yml --profile "$PROFILE" build --pull -docker compose -f docker-compose.ollama.yml --profile "$PROFILE" up -d +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile "$PROFILE" pull +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile "$PROFILE" up -d ``` +A stack started from the previous, standalone `docker-compose.ollama.yml` ran under the project name `sim-with-ollama`. Upgrading it to the overlay without carrying that name over starts Sim on an empty database: stop it with `docker compose -p sim-with-ollama down` (no `-v`), then set `COMPOSE_PROJECT_NAME=sim-with-ollama` and its `POSTGRES_PASSWORD` (`postgres` unless you set one) in `.env` before starting the overlay. See [Migrating from the previous Ollama stack](/platform/self-hosting/docker#migrating-from-the-previous-ollama-stack). Ollama is no longer published on host port `11434`; use `docker compose exec`. + `sim-setup update` refuses two install kinds outright rather than doing something surprising: @@ -214,17 +213,18 @@ helm rollback sim -n simstudio docker compose -f docker-compose.prod.yml up -d ``` -The local and Ollama stacks build the app from source and ignore `SIM_VERSION`, so roll those back by checking out the previous tag and rebuilding with the file — and, for the Ollama stack, the profile — that started the install: +A stack running the Ollama overlay rolls back the same way: it reads `SIM_VERSION` from `.env` like any production install. Pass both files and your profile so Compose manages the Ollama service in the same run: ```bash -git checkout "" +PROFILE=gpu # or cpu +docker compose -f docker-compose.prod.yml -f docker-compose.ollama.yml --profile "$PROFILE" up -d +``` -# Local stack -docker compose -f docker-compose.local.yml up -d --build +The local stack builds the app from source and ignores `SIM_VERSION`, so roll it back by checking out the previous tag and rebuilding: -# Ollama stack — the profile you installed with -PROFILE=gpu # or cpu -docker compose -f docker-compose.ollama.yml --profile "$PROFILE" up -d --build +```bash +git checkout "" +docker compose -f docker-compose.local.yml up -d --build ``` Never roll back below v0.9.33 once account tokens are sealed: those releases cannot read them, and every connected account would have to be reconnected. See [Account token sealing](/platform/self-hosting/security#account-token-sealing). diff --git a/apps/docs/scripts/fixtures/content-baseline.json b/apps/docs/scripts/fixtures/content-baseline.json index 8b0dea6725e..4382245ee9c 100644 --- a/apps/docs/scripts/fixtures/content-baseline.json +++ b/apps/docs/scripts/fixtures/content-baseline.json @@ -172631,10 +172631,15 @@ "depth": 3 }, { - "title": "Development Ollama stack", - "url": "#development-ollama-stack", + "title": "Bundled Ollama", + "url": "#bundled-ollama", "depth": 3 }, + { + "title": "Migrating from the previous Ollama stack", + "url": "#migrating-from-the-previous-ollama-stack", + "depth": 4 + }, { "title": "LM Studio", "url": "#lm-studio", @@ -172698,105 +172703,133 @@ "meta": null, "sha256": "a68c0e57699fd1b6173497de2b48e69da088d4f060252545cc6073e05474d543", "bytes": 14, - "line": 16 + "line": 20 }, { "lang": "bash", "meta": null, "sha256": "fc4c41bd42c49d3d1e01e1c5b3bea596499a608d6e8b477667c20c94e6392569", "bytes": 55, - "line": 24 + "line": 28 }, { "lang": "bash", "meta": null, "sha256": "8458fd352d5aa0c3bd2d0e4d4e362b72556f9a812206a9b1b311a17df776cffb", "bytes": 370, - "line": 37 + "line": 41 }, { "lang": "bash", "meta": null, "sha256": "895e38a2340e9bc60c742d7b2950ffadbe99a44f47bdb55f94e9c32363f4b126", "bytes": 196, - "line": 76 + "line": 80 }, { "lang": "bash", "meta": null, "sha256": "9a9a03e2cc32aa7b9e9e7b7f44c93923586a33c987a8eac7bd82d393295dafd1", "bytes": 54, - "line": 94 + "line": 98 }, { "lang": "text", "meta": null, "sha256": "57272f564e9f8df27666b97601aa83289d7ca6df712285cf1170be56c945b7c4", "bytes": 208, - "line": 104 + "line": 108 }, { "lang": "bash", "meta": null, "sha256": "6c7c578d5d5d42812a8dd29e3fde30d7282e4f02890c4fffd2055e2086982252", "bytes": 44, - "line": 130 + "line": 134 }, { "lang": "bash", "meta": null, "sha256": "3428ca92c1894f3f15ac85ae0637c969c0c004b29b60bc9fe1393b2c397d245f", "bytes": 47, - "line": 136 + "line": 140 }, { "lang": "bash", "meta": null, - "sha256": "1117f8e0bcc0f62773b181db811dc4845abe41db6430f97a8844b0e6d14a26a0", - "bytes": 184, - "line": 150 + "sha256": "c04933e97d11eb1d70a6dc9bc4dd84de6567b91a5df4028d29f1fb0c7f9248d0", + "bytes": 30, + "line": 154 }, { "lang": "bash", "meta": null, - "sha256": "29b413408a4d2f9af144351258c1ebc324273879ac991b3f2ae63a25402611d1", - "bytes": 186, + "sha256": "55ae5c7b92a538490f5b42e5be4f11dfd7fd43e6b60dc23c24a14adf9228457f", + "bytes": 246, "line": 160 }, + { + "lang": "bash", + "meta": null, + "sha256": "e3bbbc664b20d8e444548efdc9528e1156c0d4da50868bb87f01d2797894d785", + "bytes": 107, + "line": 170 + }, + { + "lang": "bash", + "meta": null, + "sha256": "0f620ad171fa8f8a7e4ee5e12a860c218ab48598886e69486f7562d983874f1e", + "bytes": 268, + "line": 176 + }, + { + "lang": "bash", + "meta": null, + "sha256": "7e1c423747795ea24732770b89f38ddeb4f942ebdf25770f23887c66266f0a32", + "bytes": 38, + "line": 190 + }, + { + "lang": "bash", + "meta": null, + "sha256": "894aecda7cae7ed3cd0bf0de86f635db75dd378b32f60e2e496b85b81c78c92a", + "bytes": 94, + "line": 196 + }, { "lang": "bash", "meta": null, "sha256": "83e50e313f3b2f086dc8a52ee3716a6a9e5ed6b44cd94b9a6e10e1f65f48bf9e", "bytes": 84, - "line": 172 + "line": 210 }, { "lang": "bash", "meta": null, - "sha256": "f0e3637c851af3d40ba1ed3c076002b7d966292fefc7e25410e757ea24f91a96", - "bytes": 190, - "line": 181 + "sha256": "38232bb8c4e43050f0ec71cbe8a154cb86b9fd02bc63b7cc4f370243129861bd", + "bytes": 257, + "line": 219 }, { "lang": "bash", "meta": null, "sha256": "d5bd2d83b2af1f760af5560bb09c66eefc40e39cf52352e1001cfad9a3c4eac4", "bytes": 95, - "line": 215 + "line": 254 }, { "lang": "bash", "meta": null, "sha256": "2f058f5665db6f9b709c6d84e196109453b1be9f9af60eee056f98d1a90fc076", "bytes": 92, - "line": 221 + "line": 260 }, { "lang": "bash", "meta": null, "sha256": "a5f21e5199143694b8a6b0048a1cad17a98186f489694b49deacc108edf1fa7b", "bytes": 21, - "line": 229 + "line": 268 } ], "links": [ @@ -173125,6 +173158,11 @@ "url": "#integration-credentials", "depth": 2 }, + { + "title": "Keyless cloud credentials", + "url": "#workload-identity", + "depth": 3 + }, { "title": "Access control", "url": "#access-control", @@ -173135,6 +173173,11 @@ "url": "#code-execution", "depth": 2 }, + { + "title": "Preview features", + "url": "#preview-features", + "depth": 2 + }, { "title": "Chat and PII", "url": "#chat--pii", @@ -173221,21 +173264,21 @@ "meta": null, "sha256": "8212e97007004681330f0d78618939abbc4d7a29283580552542a576245c0826", "bytes": 79, - "line": 272 + "line": 295 }, { "lang": "bash", "meta": null, "sha256": "0e794f3491fc1402990b65869622ebe68febe19c0331544891e8eeb01e8249f3", "bytes": 129, - "line": 283 + "line": 306 }, { "lang": "text", "meta": null, "sha256": "a45ddc97179f78324455bd2d0f25917282f721b3962c68e7455d9917e32379f5", "bytes": 852, - "line": 306 + "line": 329 } ], "links": [ @@ -174797,28 +174840,28 @@ "meta": null, "sha256": "4b151527ca8880f0091cad081ede9c3080ddbeb58ffa79751cbd8794fa4fd2d3", "bytes": 110, - "line": 31 + "line": 35 }, { "lang": "bash", "meta": null, "sha256": "07b6e155fe047c59e93c5fbf63e3e4eb331eeb2705f5074e1e5028acbda9f1c0", "bytes": 220, - "line": 49 + "line": 53 }, { "lang": "bash", "meta": null, "sha256": "0dd8aa17d989b63d622ef45a99189f5149672f5052866c9178b9579b853eb28d", "bytes": 59, - "line": 55 + "line": 59 }, { "lang": "yaml", "meta": null, "sha256": "076126c0931b3e1fd1f9f73389fd2302f9d6c806f45809410ae273a51923618c", "bytes": 80, - "line": 125 + "line": 129 } ], "links": [ @@ -175732,70 +175775,70 @@ "meta": null, "sha256": "d2548fced54a0f26df513f23562741d37cf411569e6070c0e5e83af42dea923f", "bytes": 30, - "line": 94 + "line": 98 }, { "lang": "yaml", "meta": null, "sha256": "4b119b9923af4a285af84dfe3ff39b5a42278ddbda2d450a0b9da7afeb27fbfd", "bytes": 129, - "line": 102 + "line": 106 }, { "lang": "yaml", "meta": null, "sha256": "1286f5b957d8aae8c5e5ba37b8deaa088e40642bd303c6ba0a4a4e6ef1a8c658", "bytes": 295, - "line": 115 + "line": 119 }, { "lang": "yaml", "meta": null, "sha256": "1d6790fb0ffae5ee4bde0dfe5b5fe40f091c241a4936378b37cd299aa107ff7c", "bytes": 58, - "line": 135 + "line": 139 }, { "lang": "bash", "meta": null, "sha256": "5380d663506380272ec502d0cceaef927d173f2836daec02c78c0fc892769919", "bytes": 79, - "line": 162 + "line": 166 }, { "lang": "bash", "meta": null, "sha256": "4fd1ce391cf85e5093c6348b4d57dac12c1302939037c308314bd8e12c260b38", "bytes": 117, - "line": 240 + "line": 244 }, { "lang": "bash", "meta": null, "sha256": "68d150e3ac8a0f2ab0a0a8e7b7f000dd0b4eeb533f074057fc3f0272e314b060", "bytes": 41, - "line": 258 + "line": 262 }, { "lang": "yaml", "meta": null, "sha256": "97a9e97bd237a84b46a8dd55ddde2685442b763d4b00279d9b4d6db05883cfae", "bytes": 110, - "line": 292 + "line": 296 }, { "lang": "yaml", "meta": null, "sha256": "dd2274ef28334de3fd41b204f57e8af3c3fba12c3af4457c44234c72da35b56c", "bytes": 551, - "line": 305 + "line": 309 }, { "lang": "yaml", "meta": null, "sha256": "a6efcc859a8531bfec1e7434aded7fbbacd567afe8875f4bdee025f428f267d2", "bytes": 137, - "line": 349 + "line": 353 } ], "links": [ @@ -175955,133 +175998,133 @@ "meta": null, "sha256": "95f834e604341dd3a4a7dbe6d3e03ba145a0ad5be37a545e49cb8c2650d38542", "bytes": 140, - "line": 8 + "line": 12 }, { "lang": "bash", "meta": null, "sha256": "f53db8ab234bd8bc5fcc3d58410b791feda6c3ee388860a2cfe3af51534c2ae3", "bytes": 229, - "line": 18 + "line": 22 }, { "lang": "bash", "meta": null, "sha256": "fa72373581093369f53d01fe1fb8c39b3d718591d0ee29ee6e3e7bb14752cca0", "bytes": 318, - "line": 28 + "line": 32 }, { "lang": "bash", "meta": null, "sha256": "3cecb71d565f222bc89348c8063e2a7bf072a97fda47d8f28777dd03ffdfe02f", "bytes": 167, - "line": 46 + "line": 50 }, { "lang": "bash", "meta": null, "sha256": "689d5e0f169abc2f9a9fd1c5ef618ca9aa8257cc3fae3f15c70ec3e81e387f5a", "bytes": 65, - "line": 56 + "line": 60 }, { "lang": "yaml", "meta": null, "sha256": "d376cd2a53779eeac8931cbad3f40e5ebb4f8ef1ca1e8b232f86beee2b5ba471", "bytes": 48, - "line": 63 + "line": 67 }, { "lang": "bash", "meta": null, "sha256": "0297fdeeaaad204b960ec9384c842991c7e6ef5a6fa46a3c0c37c512cd95b6b0", "bytes": 107, - "line": 71 + "line": 75 }, { "lang": "bash", "meta": null, "sha256": "b8210a5f0b455614cd2657e2a4fc4e7d77a0c02524b56b77722f69c559391507", "bytes": 33, - "line": 84 + "line": 88 }, { "lang": "bash", "meta": null, "sha256": "236c9446662bbb3595b6501e5d963453498a6064c7a66078dd437baadf3713b8", "bytes": 204, - "line": 115 + "line": 119 }, { "lang": "yaml", "meta": null, "sha256": "c54df79de3f702f739f9fe5c288c3a9fcc348197d33272df3187cf74b5ad8117", "bytes": 197, - "line": 134 + "line": 138 }, { "lang": "yaml", "meta": null, "sha256": "7b4b8ea041d0669f220e56eeabcd0c5c906738646a647de5bb1a548983ffc2d3", "bytes": 280, - "line": 144 + "line": 148 }, { "lang": "bash", "meta": null, "sha256": "4fd1ce391cf85e5093c6348b4d57dac12c1302939037c308314bd8e12c260b38", "bytes": 117, - "line": 167 + "line": 171 }, { "lang": "bash", "meta": null, "sha256": "7a9aac79c13c638a232f991fc97e9076d7080ec7ac75d80c60eb780b42e1d4bd", "bytes": 76, - "line": 183 + "line": 187 }, { "lang": "bash", "meta": null, - "sha256": "0d0f8b183f3e48befc0b8261da1923fe9c81eb5189ae2fb0dc887c37d1ccc256", - "bytes": 234, - "line": 196 + "sha256": "ce7ef86fc4fbeab2fdae583b7e60a64259680ac3374b92e8c7641601ca35af93", + "bytes": 207, + "line": 200 }, { "lang": "bash", "meta": null, "sha256": "8560a7167d89cfcd9241ccda8ad1ed21f36e8dc5f108714518fe306aadbd5222", "bytes": 61, - "line": 227 + "line": 231 }, { "lang": "bash", "meta": null, "sha256": "a69235481ad1a0208b7ab7a563c83664dce5cc1ca5ec486ae08b9c10c194ca94", "bytes": 108, - "line": 235 + "line": 239 }, { "lang": "bash", "meta": null, "sha256": "341e83bdf0029a50800e322164b960fd942b1344aa38b563aea95a5640ca103b", "bytes": 190, - "line": 242 + "line": 246 }, { "lang": "bash", "meta": null, "sha256": "62bb3e351950d9f5d7f8ed10bfae6fa61dddb02dcfbaa1a5a27f805d3c861df9", "bytes": 13, - "line": 269 + "line": 273 }, { "lang": "bash", "meta": null, "sha256": "7b2c4142afb1d9e4d4609bd3746ddf70d89307eda5cc804cf471c5527dbc3f3d", "bytes": 65, - "line": 274 + "line": 278 } ], "links": [ @@ -176334,105 +176377,112 @@ "meta": null, "sha256": "f93d9b9699d57eef6c5baedda45e01aca67135784f44f13492672284f604eebb", "bytes": 89, - "line": 16 + "line": 20 }, { "lang": "yaml", "meta": null, "sha256": "7f57b5468e8035e34ab3e919ee30de9aee9f7a41af1592288193a23cfbcad3cf", "bytes": 206, - "line": 29 + "line": 33 }, { "lang": "yaml", "meta": null, "sha256": "5f0525543906c1d87f4f437be4b0e00016854267457f750a26617e34128ea749", "bytes": 38, - "line": 43 + "line": 47 }, { "lang": "bash", "meta": null, "sha256": "3b3f1fe93cf8b3eb76bfc4aed0b77a9d0059f6a80de9d20471ea892d53836094", "bytes": 26, - "line": 54 + "line": 58 }, { "lang": "bash", "meta": null, "sha256": "2b11cb46a00678505e7f39b61358c1196e726b0096f84acb6944ffcdb9b4ff33", "bytes": 456, - "line": 84 + "line": 88 }, { "lang": "bash", "meta": null, "sha256": "63638fd6cf4e99f972edffff928949b04fc63d6b75d83402057156c4b2265b30", "bytes": 128, - "line": 112 + "line": 116 }, { "lang": "bash", "meta": null, "sha256": "a55a5ea9ad954b5ebd159b620c3fecb10a5262d3c5c6224a8ce670a32880c70f", "bytes": 112, - "line": 121 + "line": 125 }, { "lang": "bash", "meta": null, "sha256": "e91ed481dfd9e3b64d81b10df809bdd9b40543ff74d2d5cc32858912ffc85555", "bytes": 130, - "line": 127 + "line": 131 }, { "lang": "bash", "meta": null, - "sha256": "0c51dd7a84c6fbfe2ce344c3e969a3d57f607e58ee0c94f923fd551188b65553", - "bytes": 253, - "line": 135 + "sha256": "ad878e0b3ad9eca63a1d754f876211eebdd440c656ee0d3587aaa68ade3700bb", + "bytes": 275, + "line": 139 }, { "lang": "bash", "meta": null, - "sha256": "f1d627bf3b9c3825fcbeae8f48a08f66f2c8711685d489af48885267ed43dd84", - "bytes": 455, - "line": 161 + "sha256": "ff1bd014f6d6327ed60b482b030a73278e8d7829940aa009687b6005698f5917", + "bytes": 327, + "line": 165 }, { "lang": "bash", "meta": null, "sha256": "f06da1c6caa3599e7e0498042c61dad4cb65c3fd390f1ca66f08d2d5d58f6b6a", "bytes": 30, - "line": 202 + "line": 205 }, { "lang": "bash", "meta": null, "sha256": "2eeb472e1b9a01898d9b9f49f52fa6176917f9eb9e5ead4240b36a8061a47d26", "bytes": 245, - "line": 206 + "line": 209 }, { "lang": "bash", "meta": null, - "sha256": "8b7420174e74e7eb17a2424902eed512fb73a00e55931ee604b97c318c93fc22", - "bytes": 261, - "line": 215 + "sha256": "8446fa16e456c1c785b8888bcfc62395fc23e75a40795178bd70a276e26bcd36", + "bytes": 119, + "line": 218 + }, + { + "lang": "bash", + "meta": null, + "sha256": "eaa8f9247177aab7ff05a6ee64cc60f48e8a6905fd3b4773b52568fc14e14ed0", + "bytes": 94, + "line": 225 }, { "lang": "bash", "meta": null, "sha256": "689d5e0f169abc2f9a9fd1c5ef618ca9aa8257cc3fae3f15c70ec3e81e387f5a", "bytes": 65, - "line": 241 + "line": 245 }, { "lang": "bash", "meta": null, "sha256": "e25438beeab6ee8d89c44a1d2e8c5b1d0f48973c6167530dc7b2ec0e7057a9c7", "bytes": 98, - "line": 245 + "line": 249 } ], "links": [ diff --git a/apps/sim/app/api/copilot/chat/abort/route.ts b/apps/sim/app/api/copilot/chat/abort/route.ts deleted file mode 100644 index 64fcf3a5a28..00000000000 --- a/apps/sim/app/api/copilot/chat/abort/route.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { NextRequest } from 'next/server' -import { copilotChatAbortContract } from '@/lib/api/contracts/copilot' -import { - defineInternalJsonRoute, - internalOrchestrationErrorPolicy, - internalRateLimits, - internalSessionAuth, -} from '@/lib/api/server/routes' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { abortRun, runControlOperations } from '@/lib/mothership/request/application/controls' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' - -const handler = defineInternalJsonRoute({ - contract: copilotChatAbortContract, - auth: internalSessionAuth, - operation: runControlOperations.abort, - rateLimit: internalRateLimits.none({ - reason: - 'Authenticated lifecycle control; Stop and steering must remain available while a run is active.', - }), - errorPolicy: internalOrchestrationErrorPolicy, - mapInput: ({ body }) => body, - useCase: abortRun, -}) - -export const POST = (request: NextRequest, context?: Parameters[1]) => - withIncomingGoSpan(request.headers, TraceSpan.CopilotChatAbortStream, undefined, () => - handler(request, context) - ) diff --git a/apps/sim/app/api/copilot/chat/resources/route.ts b/apps/sim/app/api/copilot/chat/resources/route.ts deleted file mode 100644 index c05e09cdd80..00000000000 --- a/apps/sim/app/api/copilot/chat/resources/route.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { - addCopilotChatResourceContract, - removeCopilotChatResourceContract, - reorderCopilotChatResourcesContract, -} from '@/lib/api/contracts/copilot' -import { - defineInternalJsonRoute, - internalOrchestrationErrorPolicy, - internalRateLimits, - internalSessionAuth, -} from '@/lib/api/server/routes' -import { changeChatResources } from '@/lib/mothership/chat/application/change-resources' -import { presentChatResourceForBrowser } from '@/lib/mothership/resources/presentation' -import type { MothershipResource } from '@/lib/mothership/resources/types' - -const policy = { - auth: internalSessionAuth, - operation: changeChatResources.operation, - rateLimit: internalRateLimits.none({ - reason: 'Personal chat layout updates have no separate rate bucket.', - }), - errorPolicy: internalOrchestrationErrorPolicy, - useCase: changeChatResources, - present: ({ resources }: { resources: MothershipResource[] }) => ({ - success: true as const, - resources: resources.map(presentChatResourceForBrowser), - }), -} - -export const POST = defineInternalJsonRoute({ - ...policy, - contract: addCopilotChatResourceContract, - mapInput: ({ body }) => ({ - chatId: body.chatId, - change: { - kind: 'upsert' as const, - resources: [ - { ...body.resource, ...(body.clearViewId ? { clearViewId: true as const } : {}) }, - ], - }, - }), -}) - -export const PATCH = defineInternalJsonRoute({ - ...policy, - contract: reorderCopilotChatResourcesContract, - mapInput: ({ body }) => ({ - chatId: body.chatId, - change: { kind: 'reorder' as const, resources: body.resources }, - }), -}) - -export const DELETE = defineInternalJsonRoute({ - ...policy, - contract: removeCopilotChatResourceContract, - mapInput: ({ body }) => ({ - chatId: body.chatId, - change: { - kind: 'remove' as const, - resources: [ - { - type: body.resourceType, - id: body.resourceId, - workspaceId: body.workspaceId, - owner: body.owner, - }, - ], - }, - }), -}) diff --git a/apps/sim/app/api/copilot/chat/stop/route.ts b/apps/sim/app/api/copilot/chat/stop/route.ts deleted file mode 100644 index 33ccee4e0fd..00000000000 --- a/apps/sim/app/api/copilot/chat/stop/route.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { createLogger } from '@sim/logger' -import { generateId } from '@sim/utils/id' -import { type NextRequest, NextResponse } from 'next/server' -import { copilotChatStopContract } from '@/lib/api/contracts/copilot' -import { parseRequest } from '@/lib/api/server' -import { getSession } from '@/lib/auth' -import { withRouteHandler } from '@/lib/core/utils/with-route-handler' -import { getAccessibleCopilotChatAuth } from '@/lib/mothership/chat/lifecycle' -import { - normalizeMessage, - type PersistedMessage, - withStoppedContentBlock, -} from '@/lib/mothership/chat/persisted-message' -import { - finalizeAssistantTurn, - readStoppedAssistantMessage, -} from '@/lib/mothership/chat/terminal-state' -import { publishChatStatusChanged } from '@/lib/mothership/chat-status' -import { - CopilotChatFinalizeOutcome, - CopilotStopOutcome, -} from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' - -const logger = createLogger('CopilotChatStopAPI') - -// POST /api/copilot/chat/stop — persists partial assistant content -// when the user stops mid-stream. Lock release is handled by the -// aborted server stream unwinding, not this handler. -export const POST = withRouteHandler((req: NextRequest) => - withIncomingGoSpan(req.headers, TraceSpan.CopilotChatStopStream, undefined, async (span) => { - try { - const session = await getSession() - if (!session?.user?.id) { - span.setAttribute(TraceAttr.CopilotStopOutcome, CopilotStopOutcome.Unauthorized) - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) - } - - const parsed = await parseRequest(copilotChatStopContract, req, {}) - if (!parsed.success) { - span.setAttribute(TraceAttr.CopilotStopOutcome, CopilotStopOutcome.ValidationError) - return parsed.response - } - const { chatId, streamId, content, contentBlocks, requestId } = parsed.data.body - const chat = await getAccessibleCopilotChatAuth(chatId, session.user.id, { - principal: { kind: 'session', userId: session.user.id, sessionId: session.session.id }, - }) - if (!chat) return NextResponse.json({ success: true }) - span.setAttributes({ - [TraceAttr.ChatId]: chatId, - [TraceAttr.StreamId]: streamId, - [TraceAttr.UserId]: session.user.id, - [TraceAttr.CopilotStopContentLength]: content.length, - [TraceAttr.CopilotStopBlocksCount]: contentBlocks?.length ?? 0, - ...(requestId ? { [TraceAttr.RequestId]: requestId } : {}), - }) - - const hasContent = content.trim().length > 0 - const hasBlocks = Array.isArray(contentBlocks) && contentBlocks.length > 0 - const assistantBlocks = hasBlocks - ? contentBlocks - : hasContent - ? [{ type: 'text', channel: 'assistant', content }] - : [] - const assistantMessage: PersistedMessage | null = - hasContent || hasBlocks - ? withStoppedContentBlock( - normalizeMessage({ - id: generateId(), - role: 'assistant', - content, - timestamp: new Date().toISOString(), - contentBlocks: assistantBlocks, - ...(requestId ? { requestId } : {}), - }) - ) - : await readStoppedAssistantMessage(streamId, chatId, session.user.id) - /** The run owner retains the full response if replay was trimmed or has not started. */ - if (!assistantMessage) return NextResponse.json({ success: true }) - const result = await finalizeAssistantTurn({ - chatId, - userId: session.user.id, - userMessageId: streamId, - assistantMessage, - streamMarkerPolicy: 'active-or-cleared', - preferServerReplay: hasContent || hasBlocks, - }) - span.setAttribute(TraceAttr.CopilotStopAppendedAssistant, result.appendedAssistant) - const stopOutcome = !result.found - ? CopilotStopOutcome.ChatNotFound - : result.updated || result.outcome === CopilotChatFinalizeOutcome.AssistantAlreadyPersisted - ? CopilotStopOutcome.Persisted - : CopilotStopOutcome.NoMatchingRow - const shouldPublishCompleted = - result.updated || result.outcome === CopilotChatFinalizeOutcome.AssistantAlreadyPersisted - - if (shouldPublishCompleted) { - publishChatStatusChanged(chat, { - chatId, - type: 'completed', - streamId, - }) - } - - span.setAttribute(TraceAttr.CopilotStopOutcome, stopOutcome) - return NextResponse.json({ success: true }) - } catch (error) { - logger.error('Error stopping chat stream:', error) - span.setAttribute(TraceAttr.CopilotStopOutcome, CopilotStopOutcome.InternalError) - return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) - } - }) -) diff --git a/apps/sim/app/api/copilot/chat/stream/route.ts b/apps/sim/app/api/copilot/chat/stream/route.ts deleted file mode 100644 index d214fb471a2..00000000000 --- a/apps/sim/app/api/copilot/chat/stream/route.ts +++ /dev/null @@ -1,623 +0,0 @@ -import { type Context, context as otelContext, type Span, trace } from '@opentelemetry/api' -import type { SessionPrincipal } from '@sim/auth/principal' -import { createLogger } from '@sim/logger' -import { getErrorMessage } from '@sim/utils/errors' -import { sleep } from '@sim/utils/helpers' -import { type NextRequest, NextResponse } from 'next/server' -import { copilotChatStreamContract } from '@/lib/api/contracts/copilot' -import { parseRequest } from '@/lib/api/server' -import { - InternalUnauthenticatedError, - internalOrchestrationErrorPolicy, - internalSessionAuth, -} from '@/lib/api/server/routes' -import { encodeSSEComment } from '@/lib/core/utils/sse' -import { withRouteHandler } from '@/lib/core/utils/with-route-handler' -import { - MOTHERSHIP_DESKTOP_EXECUTOR_HEADER, - MOTHERSHIP_STREAM_REPLAY_HEADER, -} from '@/lib/mothership/constants' -import { - MothershipStreamV1CompletionStatus, - MothershipStreamV1EventType, -} from '@/lib/mothership/generated/mothership-stream-v1' -import { - CopilotResumeOutcome, - CopilotTransport, -} from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { readChatStream } from '@/lib/mothership/request/application/recover-stream' -import { contextFromRequestHeaders } from '@/lib/mothership/request/go/propagation' -import { getCopilotTracer, markSpanForError } from '@/lib/mothership/request/otel' -import { - createEvent, - encodeSSEEnvelope, - findReplayGap, - forwardRunReplay, - holdStreamViewer, - isTerminalStreamStatus, - openRunReplay, - RunReplayUnavailableError, - readEvents, - readFilePreviewSessions, - readRingPosition, - replayGapTerminal, - ringCanServe, - SSE_RESPONSE_HEADERS, -} from '@/lib/mothership/request/session' -import { toReplayEnvelope, toStreamBatchEvent } from '@/lib/mothership/request/session/types' - -export const maxDuration = 3600 - -const logger = createLogger('CopilotChatStreamAPI') -const POLL_INTERVAL_MS = 250 -const POLL_INTERVAL_MAX_MS = 2_000 -const REPLAY_KEEPALIVE_INTERVAL_MS = 15_000 -/** How often a tail that is still flushing events checks that its ring can serve it. */ -const RING_CHECK_EVERY_BUSY_POLLS = 8 -/** - * One replay response stays open at most this long, inside the route's `maxDuration`. - * A run still going at the cap is not over: the response ends without a terminal - * event and the client re-attaches from its cursor. - */ -const MAX_STREAM_MS = 60 * 60 * 1000 - 60_000 - -/** - * Whether ring events read after `cursor` start right after it. The ring can trim its - * head between a gap check and the read, and a read that starts later would silently - * skip part of the turn. - */ -function startsAfterCursor(events: readonly { seq: number }[], cursor: string): boolean { - return events.length === 0 || events[0].seq <= Number(cursor || '0') + 1 -} - -function extractCanonicalRequestId(value: unknown): string { - return typeof value === 'string' && value.length > 0 ? value : '' -} - -function extractRunRequestId(run: { requestContext?: unknown } | null | undefined): string { - if (!run || typeof run.requestContext !== 'object' || run.requestContext === null) { - return '' - } - const requestContext = run.requestContext as Record - return ( - extractCanonicalRequestId(requestContext.requestId) || - extractCanonicalRequestId(requestContext.simRequestId) - ) -} - -function extractEnvelopeRequestId(envelope: { trace?: { requestId?: unknown } }): string { - return extractCanonicalRequestId(envelope.trace?.requestId) -} - -function buildResumeTerminalEnvelopes(options: { - streamId: string - afterCursor: string - status: MothershipStreamV1CompletionStatus - message?: string - code: string - reason?: string - requestId?: string -}) { - const baseSeq = Number(options.afterCursor || '0') - const seq = Number.isFinite(baseSeq) ? baseSeq : 0 - const envelopes: ReturnType[] = [] - const rid = options.requestId ?? '' - - if (options.status === MothershipStreamV1CompletionStatus.error) { - envelopes.push( - createEvent({ - streamId: options.streamId, - cursor: String(seq + 1), - seq: seq + 1, - requestId: rid, - type: MothershipStreamV1EventType.error, - payload: { - message: options.message || 'Stream recovery failed before completion.', - code: options.code, - }, - }) - ) - } - - envelopes.push( - createEvent({ - streamId: options.streamId, - cursor: String(seq + envelopes.length + 1), - seq: seq + envelopes.length + 1, - requestId: rid, - type: MothershipStreamV1EventType.complete, - payload: { - status: options.status, - ...(options.reason ? { reason: options.reason } : {}), - }, - }) - ) - - return envelopes -} - -export const GET = withRouteHandler(async (request: NextRequest) => { - let principal: SessionPrincipal - try { - principal = await internalSessionAuth.authenticate() - } catch (error) { - if (error instanceof InternalUnauthenticatedError) - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) - throw error - } - const authenticatedUserId = principal.userId - - const parsed = await parseRequest(copilotChatStreamContract, request, {}) - if (!parsed.success) return parsed.response - const { streamId, after: afterCursor, batch: batchMode, source } = parsed.data.query - - if (!streamId) { - return NextResponse.json({ error: 'streamId is required' }, { status: 400 }) - } - - // Root span for the whole resume/reconnect request. In stream mode the - // work happens inside `ReadableStream.start`, which the Node runtime - // invokes after this function returns and OUTSIDE the AsyncLocalStorage - // scope installed by `startActiveSpan`. We therefore start the span - // manually, capture its context, and re-enter that context inside the - // stream callback so every nested `withCopilotSpan` / `withDbSpan` call - // attaches to this root. - // - // `contextFromRequestHeaders` extracts the W3C `traceparent` the - // client echoed (set via `streamTraceparentRef` on Sim's chat POST - // response), so the resume span becomes a child of the original - // chat's `gen_ai.agent.execute` trace instead of a disconnected - // new root. On reconnects after page reload (client ref was wiped) - // the header is absent and extraction leaves the ambient context - // alone → the resume span becomes its own root. Same as pre- - // linking behavior; no regression. - const incomingContext = contextFromRequestHeaders(request.headers) - const rootSpan = getCopilotTracer().startSpan( - TraceSpan.CopilotResumeRequest, - { - attributes: { - [TraceAttr.CopilotTransport]: batchMode ? CopilotTransport.Batch : CopilotTransport.Stream, - [TraceAttr.StreamId]: streamId, - [TraceAttr.UserId]: authenticatedUserId, - [TraceAttr.CopilotResumeAfterCursor]: afterCursor || '0', - }, - }, - incomingContext - ) - const rootContext = trace.setSpan(incomingContext, rootSpan) - - try { - return await otelContext.with(rootContext, () => - handleResumeRequestBody({ - request, - streamId, - afterCursor, - batchMode, - fromLog: source === 'log', - principal, - rootSpan, - rootContext, - }) - ) - } catch (err) { - markSpanForError(rootSpan, err) - rootSpan.end() - const errorResponse = - internalOrchestrationErrorPolicy.project(err) ?? internalOrchestrationErrorPolicy.unhandled!() - return NextResponse.json(errorResponse.body, { - status: errorResponse.status, - headers: errorResponse.headers, - }) - } -}) - -async function handleResumeRequestBody({ - request, - streamId, - afterCursor, - batchMode, - fromLog, - principal, - rootSpan, - rootContext, -}: { - request: NextRequest - streamId: string - afterCursor: string - batchMode: boolean - /** The reader's cursor came from a log re-sync, so the ring never serves it. */ - fromLog: boolean - principal: SessionPrincipal - rootSpan: Span - rootContext: Context -}) { - const readRun = () => - readChatStream.execute({ - principal, - input: { streamId }, - }) - const run = await readRun() - logger.info('[Resume] Stream lookup', { - streamId, - afterCursor, - batchMode, - hasRun: !!run, - runStatus: run?.status, - }) - if (!run) { - rootSpan.setAttribute(TraceAttr.CopilotResumeOutcome, CopilotResumeOutcome.StreamNotFound) - rootSpan.end() - return NextResponse.json({ error: 'Stream not found' }, { status: 404 }) - } - rootSpan.setAttribute(TraceAttr.CopilotRunStatus, run.status) - - if (batchMode) { - const afterSeq = afterCursor || '0' - const [gap, events, previewSessions] = await Promise.all([ - fromLog ? null : findReplayGap(streamId, afterSeq, extractRunRequestId(run)), - readEvents(streamId, afterSeq), - readFilePreviewSessions(streamId).catch((error) => { - logger.warn('Failed to read preview sessions for stream batch', { - streamId, - error: getErrorMessage(error), - }) - return [] - }), - ]) - // A reader the ring cannot serve, or whose next event it trimmed after the gap check, - // is re-synced from the worker log by the live tail. - const batchEvents = - fromLog || gap || !startsAfterCursor(events, afterSeq) ? [] : events.map(toStreamBatchEvent) - logger.info('[Resume] Batch response', { - streamId, - afterCursor: afterSeq, - eventCount: batchEvents.length, - previewSessionCount: previewSessions.length, - runStatus: run.status, - }) - rootSpan.setAttributes({ - [TraceAttr.CopilotResumeOutcome]: CopilotResumeOutcome.BatchDelivered, - [TraceAttr.CopilotResumeEventCount]: batchEvents.length, - [TraceAttr.CopilotResumePreviewSessionCount]: previewSessions.length, - }) - rootSpan.end() - return NextResponse.json({ - success: true, - events: batchEvents, - previewSessions, - status: run.status, - ...(run.chatId ? { chatId: run.chatId } : {}), - ...(run.desktopDeviceId ? { desktopToolsOnDevice: true } : {}), - }) - } - - const startTime = Date.now() - let totalEventsFlushed = 0 - let pollIterations = 0 - - /** - * A reader the ring cannot serve is re-synced from the worker's durable log for the - * rest of this response, never handed back to the ring: the log and the ring have - * no shared position to join on. The header tells the client to rebuild the turn - * from an empty response, since the replay's cursors restart at 1. - */ - const ringGap = fromLog - ? null - : await findReplayGap(streamId, afterCursor || '0', extractRunRequestId(run)) - // A finished run whose buffer expired answers its terminal; its transcript is persisted. - const gap = - ringGap && !(ringGap.latestSeq <= 0 && isTerminalStreamStatus(run.status)) ? ringGap : null - const resyncFromLog = fromLog || gap !== null - let replayBody: ReadableStream | null = null - /** Releases the worker's replay once this response ends; the request signal may never fire. */ - const replayAbort = new AbortController() - const replaySignal = AbortSignal.any([request.signal, replayAbort.signal]) - if (resyncFromLog && run.chatId) { - try { - replayBody = await openRunReplay({ - streamId, - chatId: run.chatId, - userId: principal.userId, - signal: replaySignal, - }) - } catch (error) { - if (!(error instanceof RunReplayUnavailableError)) throw error - logger.warn('Run replay unavailable; the client will retry', { - streamId, - error: getErrorMessage(error), - }) - markSpanForError(rootSpan, error) - rootSpan.end() - return NextResponse.json({ error: 'Stream replay is unavailable' }, { status: 503 }) - } - } - - const stream = new ReadableStream({ - async start(controller) { - // Re-enter the root OTel context so any `withCopilotSpan` call below - // (inside flushEvents/replayGapTerminal/etc.) parents under - // copilot.resume.request instead of becoming an orphan. - return otelContext.with(rootContext, () => startInner(controller)) - }, - }) - - async function startInner(controller: ReadableStreamDefaultController) { - let cursor = afterCursor || '0' - let controllerClosed = false - let sawTerminalEvent = false - let currentRequestId = extractRunRequestId(run) - let lastWriteTime = Date.now() - // Stamp the logical request id + chat id on the resume root as soon - // as we resolve them from the run row, so TraceQL joins work on - // resume legs the same way they do on the original POST. - if (currentRequestId) { - rootSpan.setAttribute(TraceAttr.RequestId, currentRequestId) - rootSpan.setAttribute(TraceAttr.SimRequestId, currentRequestId) - } - if (run?.chatId) { - rootSpan.setAttribute(TraceAttr.ChatId, run.chatId) - } - - const closeController = () => { - if (controllerClosed) return - controllerClosed = true - try { - controller.close() - } catch { - // Controller already closed by runtime/client - } - } - - const enqueueEvent = (payload: unknown) => { - if (controllerClosed) return false - try { - controller.enqueue(encodeSSEEnvelope(payload)) - lastWriteTime = Date.now() - return true - } catch { - controllerClosed = true - return false - } - } - - const enqueueComment = (comment: string) => { - if (controllerClosed) return false - try { - controller.enqueue(encodeSSEComment(comment)) - lastWriteTime = Date.now() - return true - } catch { - controllerClosed = true - return false - } - } - - const abortListener = () => { - controllerClosed = true - } - request.signal.addEventListener('abort', abortListener, { once: true }) - // An attached reader is a chat view that starts this turn's workflow tools. - const releaseViewer = await holdStreamViewer(streamId) - - /** Delivers the ring's events after the cursor, or returns null if it trimmed the next one. */ - const flushEvents = async (): Promise => { - const events = await readEvents(streamId, cursor) - if (!startsAfterCursor(events, cursor)) { - logger.warn('Replay ring trimmed past a reader cursor', { streamId, cursor }) - return null - } - if (events.length > 0) { - logger.debug('[Resume] Flushing events', { - streamId, - afterCursor: cursor, - eventCount: events.length, - }) - } - for (const envelope of events) { - if (!enqueueEvent(toReplayEnvelope(envelope))) { - break - } - totalEventsFlushed += 1 - cursor = envelope.stream.cursor ?? String(envelope.seq) - currentRequestId = extractEnvelopeRequestId(envelope) || currentRequestId - if (envelope.type === MothershipStreamV1EventType.complete) { - sawTerminalEvent = true - } - } - return events.length - } - - const emitTerminalIfMissing = ( - status: MothershipStreamV1CompletionStatus, - options?: { message?: string; code: string; reason?: string } - ) => { - if (controllerClosed || sawTerminalEvent) { - return - } - for (const envelope of buildResumeTerminalEnvelopes({ - streamId, - afterCursor: cursor, - status, - message: options?.message, - code: options?.code ?? 'resume_terminal', - reason: options?.reason, - requestId: currentRequestId, - })) { - if (!enqueueEvent(envelope)) { - break - } - cursor = envelope.stream.cursor ?? String(envelope.seq) - if (envelope.type === MothershipStreamV1EventType.complete) { - sawTerminalEvent = true - } - } - } - - /** Forwards the worker's replay, keeping the response alive while it waits. */ - const streamRunReplay = async (body: ReadableStream) => { - const keepalive = setInterval(() => { - if (Date.now() - lastWriteTime < REPLAY_KEEPALIVE_INTERVAL_MS) return - if (!enqueueComment('keepalive')) replayAbort.abort() - }, REPLAY_KEEPALIVE_INTERVAL_MS) - try { - const end = await forwardRunReplay({ - body, - streamId, - signal: replaySignal, - write: (envelope) => { - if (!enqueueEvent(envelope)) return false - totalEventsFlushed += 1 - cursor = envelope.stream.cursor ?? cursor - if (envelope.type === MothershipStreamV1EventType.complete) sawTerminalEvent = true - return true - }, - readRunStatus: async () => (await readRun().catch(() => null))?.status ?? null, - isClosed: () => controllerClosed, - deadlineAt: startTime + MAX_STREAM_MS, - }) - logger.info('[Resume] Run replay ended', { streamId, end, eventCount: totalEventsFlushed }) - } finally { - clearInterval(keepalive) - replayAbort.abort() - } - } - - try { - enqueueComment('accepted') - - if (replayBody) { - await streamRunReplay(replayBody) - return - } - if (resyncFromLog) { - const position = gap ?? (await readRingPosition(streamId, cursor)) - const terminal = await replayGapTerminal(streamId, position, currentRequestId) - for (const envelope of terminal.envelopes) { - if (!enqueueEvent(envelope)) { - break - } - cursor = envelope.stream.cursor ?? String(envelope.seq) - currentRequestId = extractEnvelopeRequestId(envelope) || currentRequestId - if (envelope.type === MothershipStreamV1EventType.complete) { - sawTerminalEvent = true - } - } - return - } - - let lastFlushed = await flushEvents() - if (lastFlushed === null) return - - let pollDelayMs = POLL_INTERVAL_MS - while (!controllerClosed && Date.now() - startTime < MAX_STREAM_MS) { - pollIterations += 1 - const currentRun = await readRun().catch((err) => { - logger.warn('Failed to poll latest run for stream', { - streamId, - error: getErrorMessage(err), - }) - return null - }) - if (!currentRun) { - emitTerminalIfMissing(MothershipStreamV1CompletionStatus.error, { - message: 'The stream could not be recovered because its run metadata is unavailable.', - code: 'resume_run_unavailable', - reason: 'run_unavailable', - }) - break - } - // The ring lost its head, restarted or expired under this live tail; the re-attach - // re-syncs, and a finished run answers its terminal instead. Only a quiet ring can - // restart or be re-sent into by a recovery, so a busy tail checks every few polls. - const checkRing = lastFlushed === 0 || pollIterations % RING_CHECK_EVERY_BUSY_POLLS === 0 - if ( - checkRing && - !isTerminalStreamStatus(currentRun.status) && - !ringCanServe(await readRingPosition(streamId, cursor)) - ) { - logger.warn('Replay ring can no longer serve a live tail', { streamId, cursor }) - break - } - - currentRequestId = extractRunRequestId(currentRun) || currentRequestId - - const flushed = await flushEvents() - if (flushed === null) break - lastFlushed = flushed - /* Adaptive tail: 4 Hz only while events are actually flowing; a quiet stream - decays toward the cap so an attached client doesn't hammer Postgres + Redis - at 4 Hz for up to an hour. Any flushed event snaps back to full rate. */ - pollDelayMs = - flushed > 0 ? POLL_INTERVAL_MS : Math.min(pollDelayMs * 2, POLL_INTERVAL_MAX_MS) - - if (controllerClosed) { - break - } - if (isTerminalStreamStatus(currentRun.status)) { - emitTerminalIfMissing(currentRun.status, { - message: - currentRun.status === MothershipStreamV1CompletionStatus.error - ? typeof currentRun.error === 'string' - ? currentRun.error - : 'The recovered stream ended with an error.' - : undefined, - code: 'resume_terminal_status', - reason: 'terminal_status', - }) - break - } - - if (request.signal.aborted) { - controllerClosed = true - break - } - - if (Date.now() - lastWriteTime >= REPLAY_KEEPALIVE_INTERVAL_MS) { - enqueueComment('keepalive') - } - - await sleep(pollDelayMs) - } - } catch (error) { - if (!controllerClosed && !request.signal.aborted) { - logger.warn('Stream replay failed', { - streamId, - error: getErrorMessage(error), - }) - emitTerminalIfMissing(MothershipStreamV1CompletionStatus.error, { - message: 'The stream replay failed before completion.', - code: 'resume_internal', - reason: 'stream_replay_failed', - }) - } - markSpanForError(rootSpan, error) - } finally { - releaseViewer() - request.signal.removeEventListener('abort', abortListener) - // Read before closing: closing the controller here is this route ending, not the client. - const clientDisconnected = controllerClosed - closeController() - rootSpan.setAttributes({ - [TraceAttr.CopilotResumeOutcome]: sawTerminalEvent - ? CopilotResumeOutcome.TerminalDelivered - : clientDisconnected - ? CopilotResumeOutcome.ClientDisconnected - : CopilotResumeOutcome.EndedWithoutTerminal, - [TraceAttr.CopilotResumeEventCount]: totalEventsFlushed, - [TraceAttr.CopilotResumePollIterations]: pollIterations, - [TraceAttr.CopilotResumeDurationMs]: Date.now() - startTime, - }) - rootSpan.end() - } - } - - return new Response(stream, { - headers: { - ...SSE_RESPONSE_HEADERS, - ...(replayBody ? { [MOTHERSHIP_STREAM_REPLAY_HEADER]: 'log' } : {}), - ...(run.desktopDeviceId ? { [MOTHERSHIP_DESKTOP_EXECUTOR_HEADER]: 'device' } : {}), - }, - }) -} diff --git a/apps/sim/app/api/copilot/chat/abort/route.test.ts b/apps/sim/app/api/mothership/chat/abort/route.test.ts similarity index 99% rename from apps/sim/app/api/copilot/chat/abort/route.test.ts rename to apps/sim/app/api/mothership/chat/abort/route.test.ts index e3a516df4d7..9a14a247f78 100644 --- a/apps/sim/app/api/copilot/chat/abort/route.test.ts +++ b/apps/sim/app/api/mothership/chat/abort/route.test.ts @@ -74,7 +74,7 @@ vi.mock('@/lib/core/application/workspace-authorization', () => workspaceAuthori vi.mock('@/lib/core/application/organization-authorization', () => organizationAuthorizationMock) import { OrchestrationError } from '@/lib/core/orchestration/types' -import { POST } from '@/app/api/copilot/chat/abort/route' +import { POST } from '@/app/api/mothership/chat/abort/route' const { mockAuthorizeWorkspaceOperation: mockAuthorize } = workspaceAuthorizationMockFns const { mockGetActivelyBannedUserIds: mockBannedUsers } = authBanMockFns @@ -94,7 +94,7 @@ function abortRequest() { return createMockRequest('POST', { streamId: 'stream-1', chatId: 'chat-1' }) } -describe('POST /api/copilot/chat/abort', () => { +describe('POST /api/mothership/chat/abort', () => { beforeEach(() => { mockChatContext.mockResolvedValue({ chatId: 'chat-1', diff --git a/apps/sim/app/api/mothership/chat/abort/route.ts b/apps/sim/app/api/mothership/chat/abort/route.ts index 25cd9eb42b8..b10d43b2550 100644 --- a/apps/sim/app/api/mothership/chat/abort/route.ts +++ b/apps/sim/app/api/mothership/chat/abort/route.ts @@ -1,12 +1,29 @@ import type { NextRequest } from 'next/server' -import { mothershipChatAbortEnvelopeSchema } from '@/lib/api/contracts/mothership-chats' -import { withRouteHandler } from '@/lib/core/utils/with-route-handler' -import { validateShimEnvelope } from '@/lib/mothership/request/http' -import { POST as copilotAbortPost } from '@/app/api/copilot/chat/abort/route' +import { mothershipChatAbortContract } from '@/lib/api/contracts/mothership-chats' +import { + defineInternalJsonRoute, + internalOrchestrationErrorPolicy, + internalRateLimits, + internalSessionAuth, +} from '@/lib/api/server/routes' +import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { abortRun, runControlOperations } from '@/lib/mothership/request/application/controls' +import { withIncomingGoSpan } from '@/lib/mothership/request/otel' -export const POST = withRouteHandler(async (request: NextRequest) => { - const invalid = await validateShimEnvelope(request, mothershipChatAbortEnvelopeSchema) - if (invalid) return invalid - - return copilotAbortPost(request, undefined) +const handler = defineInternalJsonRoute({ + contract: mothershipChatAbortContract, + auth: internalSessionAuth, + operation: runControlOperations.abort, + rateLimit: internalRateLimits.none({ + reason: + 'Authenticated lifecycle control; Stop and steering must remain available while a run is active.', + }), + errorPolicy: internalOrchestrationErrorPolicy, + mapInput: ({ body }) => body, + useCase: abortRun, }) + +export const POST = (request: NextRequest, context?: Parameters[1]) => + withIncomingGoSpan(request.headers, TraceSpan.CopilotChatAbortStream, undefined, () => + handler(request, context) + ) diff --git a/apps/sim/app/api/mothership/chat/resources/route.ts b/apps/sim/app/api/mothership/chat/resources/route.ts index dbb169f5e3d..897fc8c71ad 100644 --- a/apps/sim/app/api/mothership/chat/resources/route.ts +++ b/apps/sim/app/api/mothership/chat/resources/route.ts @@ -1,36 +1,70 @@ -import type { NextRequest, NextResponse } from 'next/server' -import { mothershipChatResourceEnvelopeSchema } from '@/lib/api/contracts/mothership-chats' -import { withRouteHandler } from '@/lib/core/utils/with-route-handler' -import { validateShimEnvelope } from '@/lib/mothership/request/http' import { - DELETE as copilotResourcesDelete, - PATCH as copilotResourcesPatch, - POST as copilotResourcesPost, -} from '@/app/api/copilot/chat/resources/route' + addMothershipChatResourceContract, + removeMothershipChatResourceContract, + reorderMothershipChatResourcesContract, +} from '@/lib/api/contracts/mothership-chats' +import { + defineInternalJsonRoute, + internalOrchestrationErrorPolicy, + internalRateLimits, + internalSessionAuth, +} from '@/lib/api/server/routes' +import { changeChatResources } from '@/lib/mothership/chat/application/change-resources' +import { presentChatResourceForBrowser } from '@/lib/mothership/resources/presentation' +import type { MothershipResource } from '@/lib/mothership/resources/types' -async function validateResourceRequestEnvelope(request: NextRequest): Promise { - const invalid = await validateShimEnvelope(request, mothershipChatResourceEnvelopeSchema) - if (invalid) return invalid - return null +const policy = { + auth: internalSessionAuth, + operation: changeChatResources.operation, + rateLimit: internalRateLimits.none({ + reason: 'Personal chat layout updates have no separate rate bucket.', + }), + errorPolicy: internalOrchestrationErrorPolicy, + useCase: changeChatResources, + present: ({ resources }: { resources: MothershipResource[] }) => ({ + success: true as const, + resources: resources.map(presentChatResourceForBrowser), + }), } -export const POST = withRouteHandler(async (request: NextRequest) => { - const validationResponse = await validateResourceRequestEnvelope(request) - if (validationResponse) return validationResponse - - return copilotResourcesPost(request, undefined) +export const POST = defineInternalJsonRoute({ + ...policy, + contract: addMothershipChatResourceContract, + mapInput: ({ body }) => ({ + chatId: body.chatId, + change: { + kind: 'upsert' as const, + resources: [ + { ...body.resource, ...(body.clearViewId ? { clearViewId: true as const } : {}) }, + ], + }, + }), }) -export const PATCH = withRouteHandler(async (request: NextRequest) => { - const validationResponse = await validateResourceRequestEnvelope(request) - if (validationResponse) return validationResponse - - return copilotResourcesPatch(request, undefined) +export const PATCH = defineInternalJsonRoute({ + ...policy, + contract: reorderMothershipChatResourcesContract, + mapInput: ({ body }) => ({ + chatId: body.chatId, + change: { kind: 'reorder' as const, resources: body.resources }, + }), }) -export const DELETE = withRouteHandler(async (request: NextRequest) => { - const validationResponse = await validateResourceRequestEnvelope(request) - if (validationResponse) return validationResponse - - return copilotResourcesDelete(request, undefined) +export const DELETE = defineInternalJsonRoute({ + ...policy, + contract: removeMothershipChatResourceContract, + mapInput: ({ body }) => ({ + chatId: body.chatId, + change: { + kind: 'remove' as const, + resources: [ + { + type: body.resourceType, + id: body.resourceId, + workspaceId: body.workspaceId, + owner: body.owner, + }, + ], + }, + }), }) diff --git a/apps/sim/app/api/mothership/chat/route.ts b/apps/sim/app/api/mothership/chat/route.ts index 74f5c159b9f..6374e006909 100644 --- a/apps/sim/app/api/mothership/chat/route.ts +++ b/apps/sim/app/api/mothership/chat/route.ts @@ -3,7 +3,7 @@ import { mothershipChatPostEnvelopeSchema } from '@/lib/api/contracts/mothership import { getSession } from '@/lib/auth' import { withRouteHandler } from '@/lib/core/utils/with-route-handler' import { handleUnifiedChatPost } from '@/lib/mothership/chat/post' -import { validateShimEnvelope } from '@/lib/mothership/request/http' +import { validateChatPostEnvelope } from '@/lib/mothership/request/http' /** * Caps this response on serverless hosts only; the Node server bounds nothing with it. @@ -18,7 +18,7 @@ export const POST = withRouteHandler(async (request: NextRequest) => { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } - const invalid = await validateShimEnvelope(request, mothershipChatPostEnvelopeSchema) + const invalid = await validateChatPostEnvelope(request, mothershipChatPostEnvelopeSchema) if (invalid) return invalid return handleUnifiedChatPost(request) diff --git a/apps/sim/app/api/copilot/chat/stop/route.test.ts b/apps/sim/app/api/mothership/chat/stop/route.test.ts similarity index 98% rename from apps/sim/app/api/copilot/chat/stop/route.test.ts rename to apps/sim/app/api/mothership/chat/stop/route.test.ts index 806741d9f82..55c50d7cd43 100644 --- a/apps/sim/app/api/copilot/chat/stop/route.test.ts +++ b/apps/sim/app/api/mothership/chat/stop/route.test.ts @@ -32,7 +32,7 @@ vi.mock('@/lib/mothership/chat/messages-store', () => mothershipChatMessagesMock vi.mock('@/lib/mothership/chat-status', () => mothershipChatStatusMock) import { getLatestRunForStream } from '@/lib/mothership/async-runs/repository' -import { POST } from '@/app/api/copilot/chat/stop/route' +import { POST } from '@/app/api/mothership/chat/stop/route' const mockGetAccessibleChat = mothershipChatLifecycleMockFns.mockGetAccessibleCopilotChatAuth const mockAppendCopilotChatMessages = mothershipChatMessagesMockFns.mockAppendCopilotChatMessages @@ -46,7 +46,7 @@ mothershipAsyncRunsMockFns.mockGetLatestRunForStream.mockResolvedValue({ const stopRequest = (request: NextRequest) => POST(request, undefined) function createRequest(body: Record) { - return new NextRequest('http://localhost:3000/api/copilot/chat/stop', { + return new NextRequest('http://localhost:3000/api/mothership/chat/stop', { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' }, @@ -66,7 +66,7 @@ function mockReads(opts: { dbChainMockFns.limit.mockResolvedValueOnce(opts.last ? [opts.last] : []) } -describe('copilot chat stop route', () => { +describe('mothership chat stop route', () => { beforeEach(() => { // Drain the once-queue (clearAllMocks/resetDbChainMock don't), then restore defaults. dbChainMockFns.limit.mockReset() diff --git a/apps/sim/app/api/mothership/chat/stop/route.ts b/apps/sim/app/api/mothership/chat/stop/route.ts index 6b40e8093e1..8cfe51b65d1 100644 --- a/apps/sim/app/api/mothership/chat/stop/route.ts +++ b/apps/sim/app/api/mothership/chat/stop/route.ts @@ -1,12 +1,116 @@ -import type { NextRequest } from 'next/server' -import { mothershipChatStopEnvelopeSchema } from '@/lib/api/contracts/mothership-chats' +import { createLogger } from '@sim/logger' +import { generateId } from '@sim/utils/id' +import { type NextRequest, NextResponse } from 'next/server' +import { mothershipChatStopContract } from '@/lib/api/contracts/mothership-chats' +import { parseRequest } from '@/lib/api/server' +import { getSession } from '@/lib/auth' import { withRouteHandler } from '@/lib/core/utils/with-route-handler' -import { validateShimEnvelope } from '@/lib/mothership/request/http' -import { POST as copilotStopPost } from '@/app/api/copilot/chat/stop/route' +import { getAccessibleCopilotChatAuth } from '@/lib/mothership/chat/lifecycle' +import { + normalizeMessage, + type PersistedMessage, + withStoppedContentBlock, +} from '@/lib/mothership/chat/persisted-message' +import { + finalizeAssistantTurn, + readStoppedAssistantMessage, +} from '@/lib/mothership/chat/terminal-state' +import { publishChatStatusChanged } from '@/lib/mothership/chat-status' +import { + CopilotChatFinalizeOutcome, + CopilotStopOutcome, +} from '@/lib/mothership/generated/trace-attribute-values-v1' +import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' +import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { withIncomingGoSpan } from '@/lib/mothership/request/otel' -export const POST = withRouteHandler(async (request: NextRequest) => { - const invalid = await validateShimEnvelope(request, mothershipChatStopEnvelopeSchema) - if (invalid) return invalid +const logger = createLogger('CopilotChatStopAPI') - return copilotStopPost(request, undefined) -}) +/** + * Persists partial assistant content when the user stops mid-stream. Lock release is handled by + * the aborted server stream unwinding, not this handler. + */ +export const POST = withRouteHandler((req: NextRequest) => + withIncomingGoSpan(req.headers, TraceSpan.CopilotChatStopStream, undefined, async (span) => { + try { + const session = await getSession() + if (!session?.user?.id) { + span.setAttribute(TraceAttr.CopilotStopOutcome, CopilotStopOutcome.Unauthorized) + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } + + const parsed = await parseRequest(mothershipChatStopContract, req, {}) + if (!parsed.success) { + span.setAttribute(TraceAttr.CopilotStopOutcome, CopilotStopOutcome.ValidationError) + return parsed.response + } + const { chatId, streamId, content, contentBlocks, requestId } = parsed.data.body + const chat = await getAccessibleCopilotChatAuth(chatId, session.user.id, { + principal: { kind: 'session', userId: session.user.id, sessionId: session.session.id }, + }) + if (!chat) return NextResponse.json({ success: true }) + span.setAttributes({ + [TraceAttr.ChatId]: chatId, + [TraceAttr.StreamId]: streamId, + [TraceAttr.UserId]: session.user.id, + [TraceAttr.CopilotStopContentLength]: content.length, + [TraceAttr.CopilotStopBlocksCount]: contentBlocks?.length ?? 0, + ...(requestId ? { [TraceAttr.RequestId]: requestId } : {}), + }) + + const hasContent = content.trim().length > 0 + const hasBlocks = Array.isArray(contentBlocks) && contentBlocks.length > 0 + const assistantBlocks = hasBlocks + ? contentBlocks + : hasContent + ? [{ type: 'text', channel: 'assistant', content }] + : [] + const assistantMessage: PersistedMessage | null = + hasContent || hasBlocks + ? withStoppedContentBlock( + normalizeMessage({ + id: generateId(), + role: 'assistant', + content, + timestamp: new Date().toISOString(), + contentBlocks: assistantBlocks, + ...(requestId ? { requestId } : {}), + }) + ) + : await readStoppedAssistantMessage(streamId, chatId, session.user.id) + /** The run owner retains the full response if replay was trimmed or has not started. */ + if (!assistantMessage) return NextResponse.json({ success: true }) + const result = await finalizeAssistantTurn({ + chatId, + userId: session.user.id, + userMessageId: streamId, + assistantMessage, + streamMarkerPolicy: 'active-or-cleared', + preferServerReplay: hasContent || hasBlocks, + }) + span.setAttribute(TraceAttr.CopilotStopAppendedAssistant, result.appendedAssistant) + const stopOutcome = !result.found + ? CopilotStopOutcome.ChatNotFound + : result.updated || result.outcome === CopilotChatFinalizeOutcome.AssistantAlreadyPersisted + ? CopilotStopOutcome.Persisted + : CopilotStopOutcome.NoMatchingRow + const shouldPublishCompleted = + result.updated || result.outcome === CopilotChatFinalizeOutcome.AssistantAlreadyPersisted + + if (shouldPublishCompleted) { + publishChatStatusChanged(chat, { + chatId, + type: 'completed', + streamId, + }) + } + + span.setAttribute(TraceAttr.CopilotStopOutcome, stopOutcome) + return NextResponse.json({ success: true }) + } catch (error) { + logger.error('Error stopping chat stream:', error) + span.setAttribute(TraceAttr.CopilotStopOutcome, CopilotStopOutcome.InternalError) + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) + } + }) +) diff --git a/apps/sim/app/api/copilot/chat/stream/desktop-executor.integration.ts b/apps/sim/app/api/mothership/chat/stream/desktop-executor.integration.ts similarity index 95% rename from apps/sim/app/api/copilot/chat/stream/desktop-executor.integration.ts rename to apps/sim/app/api/mothership/chat/stream/desktop-executor.integration.ts index b58def23553..337dca557c7 100644 --- a/apps/sim/app/api/copilot/chat/stream/desktop-executor.integration.ts +++ b/apps/sim/app/api/mothership/chat/stream/desktop-executor.integration.ts @@ -31,7 +31,7 @@ import { eq } from 'drizzle-orm' import { NextRequest } from 'next/server' import { closeRedisConnection } from '@/lib/core/config/redis' import { MOTHERSHIP_DESKTOP_EXECUTOR_HEADER } from '@/lib/mothership/constants' -import { GET as streamGET } from '@/app/api/copilot/chat/stream/route' +import { GET as streamGET } from '@/app/api/mothership/chat/stream/route' const userId = generateId() const workspaceId = generateId() @@ -56,7 +56,7 @@ async function finishedRun(desktopDeviceId: string | null): Promise { function reconnect(streamId: string, batch: boolean) { return streamGET( new NextRequest( - `http://localhost:3000/api/copilot/chat/stream?streamId=${streamId}&after=0${batch ? '&batch=true' : ''}` + `http://localhost:3000/api/mothership/chat/stream?streamId=${streamId}&after=0${batch ? '&batch=true' : ''}` ), { params: Promise.resolve({}) } ) diff --git a/apps/sim/app/api/copilot/chat/stream/route.test.ts b/apps/sim/app/api/mothership/chat/stream/route.test.ts similarity index 88% rename from apps/sim/app/api/copilot/chat/stream/route.test.ts rename to apps/sim/app/api/mothership/chat/stream/route.test.ts index 06f25004aa4..969b82368da 100644 --- a/apps/sim/app/api/copilot/chat/stream/route.test.ts +++ b/apps/sim/app/api/mothership/chat/stream/route.test.ts @@ -52,17 +52,17 @@ vi.mock('@/lib/mothership/request/session', () => ({ }, })) -import { GET as routeGET } from './route' +import { GET as routeGET } from '@/app/api/mothership/chat/stream/route' const GET = (request: NextRequest) => routeGET(request, { params: Promise.resolve({}) }) async function readAllChunks(response: Response): Promise { const reader = response.body?.getReader() - expect(reader).toBeTruthy() + if (!reader) throw new Error('Expected a streaming response body') const chunks: string[] = [] while (true) { - const { done, value } = await reader!.read() + const { done, value } = await reader.read() if (done) { break } @@ -71,7 +71,7 @@ async function readAllChunks(response: Response): Promise { return chunks } -describe('copilot chat stream replay route', () => { +describe('mothership chat stream replay route', () => { afterEach(() => { vi.useRealTimers() }) @@ -91,7 +91,9 @@ describe('copilot chat stream replay route', () => { new OrchestrationError('not_found', 'Chat not found') ) const response = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&batch=true') + new NextRequest( + 'http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&batch=true' + ) ) expect(response.status).toBe(404) expect(readEvents).not.toHaveBeenCalled() @@ -120,7 +122,7 @@ describe('copilot chat stream replay route', () => { const response = await GET( new NextRequest( - 'http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0&batch=true' + 'http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0&batch=true' ) ) @@ -160,14 +162,18 @@ describe('copilot chat stream replay route', () => { }) const response = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0') + new NextRequest( + 'http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0' + ) ) const chunks = await readAllChunks(response) if (elapsedMs >= 3_600_000) { expect(chunks.join('')).not.toContain('"type":"complete"') const reattached = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0') + new NextRequest( + 'http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0' + ) ) chunks.push(...(await readAllChunks(reattached))) } @@ -193,7 +199,7 @@ describe('copilot chat stream replay route', () => { .mockResolvedValueOnce(null) const response = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0') + new NextRequest('http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0') ) const chunks = await readAllChunks(response) @@ -216,7 +222,7 @@ describe('copilot chat stream replay route', () => { }) const response = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=7') + new NextRequest('http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=7') ) const body = readAllChunks(response) await vi.advanceTimersByTimeAsync(61 * 60 * 1000) @@ -251,7 +257,7 @@ describe('copilot chat stream replay route', () => { ]) const response = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0') + new NextRequest('http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0') ) const text = (await readAllChunks(response)).join('') @@ -277,7 +283,7 @@ describe('copilot chat stream replay route', () => { const response = await GET( new NextRequest( - 'http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0&batch=true' + 'http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0&batch=true' ) ) @@ -302,7 +308,7 @@ describe('copilot chat stream replay route', () => { .mockResolvedValue([event(5, 'past a trimmed gap')]) const response = await GET( - new NextRequest('http://localhost:3000/api/copilot/chat/stream?streamId=stream-1&after=0') + new NextRequest('http://localhost:3000/api/mothership/chat/stream?streamId=stream-1&after=0') ) const text = (await readAllChunks(response)).join('') diff --git a/apps/sim/app/api/mothership/chat/stream/route.ts b/apps/sim/app/api/mothership/chat/stream/route.ts index 65233f6cebc..15b35846352 100644 --- a/apps/sim/app/api/mothership/chat/stream/route.ts +++ b/apps/sim/app/api/mothership/chat/stream/route.ts @@ -1,13 +1,626 @@ -import type { NextRequest } from 'next/server' -import { mothershipChatStreamQuerySchema } from '@/lib/api/contracts/mothership-chats' -import { validationErrorResponse } from '@/lib/api/server' -import { GET as copilotStreamGet } from '@/app/api/copilot/chat/stream/route' - -export function GET(request: NextRequest) { - const validation = mothershipChatStreamQuerySchema.safeParse( - Object.fromEntries(request.nextUrl.searchParams.entries()) +import { type Context, context as otelContext, type Span, trace } from '@opentelemetry/api' +import type { SessionPrincipal } from '@sim/auth/principal' +import { createLogger } from '@sim/logger' +import { getErrorMessage } from '@sim/utils/errors' +import { sleep } from '@sim/utils/helpers' +import { type NextRequest, NextResponse } from 'next/server' +import { mothershipChatStreamContract } from '@/lib/api/contracts/mothership-chats' +import { parseRequest } from '@/lib/api/server' +import { + InternalUnauthenticatedError, + internalErrorResponse, + internalOrchestrationErrorPolicy, + internalSessionAuth, +} from '@/lib/api/server/routes' +import { encodeSSEComment } from '@/lib/core/utils/sse' +import { withRouteHandler } from '@/lib/core/utils/with-route-handler' +import { + MOTHERSHIP_DESKTOP_EXECUTOR_HEADER, + MOTHERSHIP_STREAM_REPLAY_HEADER, +} from '@/lib/mothership/constants' +import { + MothershipStreamV1CompletionStatus, + MothershipStreamV1EventType, +} from '@/lib/mothership/generated/mothership-stream-v1' +import { + CopilotResumeOutcome, + CopilotTransport, +} from '@/lib/mothership/generated/trace-attribute-values-v1' +import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' +import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { readChatStream } from '@/lib/mothership/request/application/recover-stream' +import { contextFromRequestHeaders } from '@/lib/mothership/request/go/propagation' +import { getCopilotTracer, markSpanForError } from '@/lib/mothership/request/otel' +import { + createEvent, + encodeSSEEnvelope, + findReplayGap, + forwardRunReplay, + holdStreamViewer, + isTerminalStreamStatus, + openRunReplay, + RunReplayUnavailableError, + readEvents, + readFilePreviewSessions, + readRingPosition, + replayGapTerminal, + ringCanServe, + SSE_RESPONSE_HEADERS, +} from '@/lib/mothership/request/session' +import { toReplayEnvelope, toStreamBatchEvent } from '@/lib/mothership/request/session/types' + +export const maxDuration = 3600 + +const logger = createLogger('CopilotChatStreamAPI') +const POLL_INTERVAL_MS = 250 +const POLL_INTERVAL_MAX_MS = 2_000 +const REPLAY_KEEPALIVE_INTERVAL_MS = 15_000 +/** How often a tail that is still flushing events checks that its ring can serve it. */ +const RING_CHECK_EVERY_BUSY_POLLS = 8 +/** + * One replay response stays open at most this long, inside the route's `maxDuration`. + * A run still going at the cap is not over: the response ends without a terminal + * event and the client re-attaches from its cursor. + */ +const MAX_STREAM_MS = 60 * 60 * 1000 - 60_000 + +/** + * Whether ring events read after `cursor` start right after it. The ring can trim its + * head between a gap check and the read, and a read that starts later would silently + * skip part of the turn. + */ +function startsAfterCursor(events: readonly { seq: number }[], cursor: string): boolean { + return events.length === 0 || events[0].seq <= Number(cursor || '0') + 1 +} + +function extractCanonicalRequestId(value: unknown): string { + return typeof value === 'string' && value.length > 0 ? value : '' +} + +function extractRunRequestId(run: { requestContext?: unknown } | null | undefined): string { + if (!run || typeof run.requestContext !== 'object' || run.requestContext === null) { + return '' + } + const requestContext = run.requestContext as Record + return ( + extractCanonicalRequestId(requestContext.requestId) || + extractCanonicalRequestId(requestContext.simRequestId) ) - if (!validation.success) return validationErrorResponse(validation.error) +} + +function extractEnvelopeRequestId(envelope: { trace?: { requestId?: unknown } }): string { + return extractCanonicalRequestId(envelope.trace?.requestId) +} + +function buildResumeTerminalEnvelopes(options: { + streamId: string + afterCursor: string + status: MothershipStreamV1CompletionStatus + message?: string + code: string + reason?: string + requestId?: string +}) { + const baseSeq = Number(options.afterCursor || '0') + const seq = Number.isFinite(baseSeq) ? baseSeq : 0 + const envelopes: ReturnType[] = [] + const rid = options.requestId ?? '' + + if (options.status === MothershipStreamV1CompletionStatus.error) { + envelopes.push( + createEvent({ + streamId: options.streamId, + cursor: String(seq + 1), + seq: seq + 1, + requestId: rid, + type: MothershipStreamV1EventType.error, + payload: { + message: options.message || 'Stream recovery failed before completion.', + code: options.code, + }, + }) + ) + } + + envelopes.push( + createEvent({ + streamId: options.streamId, + cursor: String(seq + envelopes.length + 1), + seq: seq + envelopes.length + 1, + requestId: rid, + type: MothershipStreamV1EventType.complete, + payload: { + status: options.status, + ...(options.reason ? { reason: options.reason } : {}), + }, + }) + ) + + return envelopes +} + +export const GET = withRouteHandler(async (request: NextRequest) => { + let principal: SessionPrincipal + try { + principal = await internalSessionAuth.authenticate() + } catch (error) { + if (error instanceof InternalUnauthenticatedError) + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + throw error + } + const authenticatedUserId = principal.userId + + const parsed = await parseRequest(mothershipChatStreamContract, request, {}) + if (!parsed.success) return parsed.response + const { streamId, after: afterCursor, batch: batchMode, source } = parsed.data.query + + if (!streamId) { + return NextResponse.json({ error: 'streamId is required' }, { status: 400 }) + } + + // Root span for the whole resume/reconnect request. In stream mode the + // work happens inside `ReadableStream.start`, which the Node runtime + // invokes after this function returns and OUTSIDE the AsyncLocalStorage + // scope installed by `startActiveSpan`. We therefore start the span + // manually, capture its context, and re-enter that context inside the + // stream callback so every nested `withCopilotSpan` / `withDbSpan` call + // attaches to this root. + // + // `contextFromRequestHeaders` extracts the W3C `traceparent` the + // client echoed (set via `streamTraceparentRef` on Sim's chat POST + // response), so the resume span becomes a child of the original + // chat's `gen_ai.agent.execute` trace instead of a disconnected + // new root. On reconnects after page reload (client ref was wiped) + // the header is absent and extraction leaves the ambient context + // alone → the resume span becomes its own root. Same as pre- + // linking behavior; no regression. + const incomingContext = contextFromRequestHeaders(request.headers) + const rootSpan = getCopilotTracer().startSpan( + TraceSpan.CopilotResumeRequest, + { + attributes: { + [TraceAttr.CopilotTransport]: batchMode ? CopilotTransport.Batch : CopilotTransport.Stream, + [TraceAttr.StreamId]: streamId, + [TraceAttr.UserId]: authenticatedUserId, + [TraceAttr.CopilotResumeAfterCursor]: afterCursor || '0', + }, + }, + incomingContext + ) + const rootContext = trace.setSpan(incomingContext, rootSpan) + + try { + return await otelContext.with(rootContext, () => + handleResumeRequestBody({ + request, + streamId, + afterCursor, + batchMode, + fromLog: source === 'log', + principal, + rootSpan, + rootContext, + }) + ) + } catch (err) { + markSpanForError(rootSpan, err) + rootSpan.end() + const errorResponse = + internalOrchestrationErrorPolicy.project(err) ?? + internalOrchestrationErrorPolicy.unhandled?.() ?? + internalErrorResponse(500, { error: 'Internal server error' }) + return NextResponse.json(errorResponse.body, { + status: errorResponse.status, + headers: errorResponse.headers, + }) + } +}) + +async function handleResumeRequestBody({ + request, + streamId, + afterCursor, + batchMode, + fromLog, + principal, + rootSpan, + rootContext, +}: { + request: NextRequest + streamId: string + afterCursor: string + batchMode: boolean + /** The reader's cursor came from a log re-sync, so the ring never serves it. */ + fromLog: boolean + principal: SessionPrincipal + rootSpan: Span + rootContext: Context +}) { + const readRun = () => + readChatStream.execute({ + principal, + input: { streamId }, + }) + const run = await readRun() + logger.info('[Resume] Stream lookup', { + streamId, + afterCursor, + batchMode, + hasRun: !!run, + runStatus: run?.status, + }) + if (!run) { + rootSpan.setAttribute(TraceAttr.CopilotResumeOutcome, CopilotResumeOutcome.StreamNotFound) + rootSpan.end() + return NextResponse.json({ error: 'Stream not found' }, { status: 404 }) + } + rootSpan.setAttribute(TraceAttr.CopilotRunStatus, run.status) + + if (batchMode) { + const afterSeq = afterCursor || '0' + const [gap, events, previewSessions] = await Promise.all([ + fromLog ? null : findReplayGap(streamId, afterSeq, extractRunRequestId(run)), + readEvents(streamId, afterSeq), + readFilePreviewSessions(streamId).catch((error) => { + logger.warn('Failed to read preview sessions for stream batch', { + streamId, + error: getErrorMessage(error), + }) + return [] + }), + ]) + // A reader the ring cannot serve, or whose next event it trimmed after the gap check, + // is re-synced from the worker log by the live tail. + const batchEvents = + fromLog || gap || !startsAfterCursor(events, afterSeq) ? [] : events.map(toStreamBatchEvent) + logger.info('[Resume] Batch response', { + streamId, + afterCursor: afterSeq, + eventCount: batchEvents.length, + previewSessionCount: previewSessions.length, + runStatus: run.status, + }) + rootSpan.setAttributes({ + [TraceAttr.CopilotResumeOutcome]: CopilotResumeOutcome.BatchDelivered, + [TraceAttr.CopilotResumeEventCount]: batchEvents.length, + [TraceAttr.CopilotResumePreviewSessionCount]: previewSessions.length, + }) + rootSpan.end() + return NextResponse.json({ + success: true, + events: batchEvents, + previewSessions, + status: run.status, + ...(run.chatId ? { chatId: run.chatId } : {}), + ...(run.desktopDeviceId ? { desktopToolsOnDevice: true } : {}), + }) + } + + const startTime = Date.now() + let totalEventsFlushed = 0 + let pollIterations = 0 + + /** + * A reader the ring cannot serve is re-synced from the worker's durable log for the + * rest of this response, never handed back to the ring: the log and the ring have + * no shared position to join on. The header tells the client to rebuild the turn + * from an empty response, since the replay's cursors restart at 1. + */ + const ringGap = fromLog + ? null + : await findReplayGap(streamId, afterCursor || '0', extractRunRequestId(run)) + // A finished run whose buffer expired answers its terminal; its transcript is persisted. + const gap = + ringGap && !(ringGap.latestSeq <= 0 && isTerminalStreamStatus(run.status)) ? ringGap : null + const resyncFromLog = fromLog || gap !== null + let replayBody: ReadableStream | null = null + /** Releases the worker's replay once this response ends; the request signal may never fire. */ + const replayAbort = new AbortController() + const replaySignal = AbortSignal.any([request.signal, replayAbort.signal]) + if (resyncFromLog && run.chatId) { + try { + replayBody = await openRunReplay({ + streamId, + chatId: run.chatId, + userId: principal.userId, + signal: replaySignal, + }) + } catch (error) { + if (!(error instanceof RunReplayUnavailableError)) throw error + logger.warn('Run replay unavailable; the client will retry', { + streamId, + error: getErrorMessage(error), + }) + markSpanForError(rootSpan, error) + rootSpan.end() + return NextResponse.json({ error: 'Stream replay is unavailable' }, { status: 503 }) + } + } + + const stream = new ReadableStream({ + async start(controller) { + // Re-enter the root OTel context so any `withCopilotSpan` call below + // (inside flushEvents/replayGapTerminal/etc.) parents under + // copilot.resume.request instead of becoming an orphan. + return otelContext.with(rootContext, () => startInner(controller)) + }, + }) + + async function startInner(controller: ReadableStreamDefaultController) { + let cursor = afterCursor || '0' + let controllerClosed = false + let sawTerminalEvent = false + let currentRequestId = extractRunRequestId(run) + let lastWriteTime = Date.now() + // Stamp the logical request id + chat id on the resume root as soon + // as we resolve them from the run row, so TraceQL joins work on + // resume legs the same way they do on the original POST. + if (currentRequestId) { + rootSpan.setAttribute(TraceAttr.RequestId, currentRequestId) + rootSpan.setAttribute(TraceAttr.SimRequestId, currentRequestId) + } + if (run?.chatId) { + rootSpan.setAttribute(TraceAttr.ChatId, run.chatId) + } + + const closeController = () => { + if (controllerClosed) return + controllerClosed = true + try { + controller.close() + } catch { + // Controller already closed by runtime/client + } + } + + const enqueueEvent = (payload: unknown) => { + if (controllerClosed) return false + try { + controller.enqueue(encodeSSEEnvelope(payload)) + lastWriteTime = Date.now() + return true + } catch { + controllerClosed = true + return false + } + } + + const enqueueComment = (comment: string) => { + if (controllerClosed) return false + try { + controller.enqueue(encodeSSEComment(comment)) + lastWriteTime = Date.now() + return true + } catch { + controllerClosed = true + return false + } + } + + const abortListener = () => { + controllerClosed = true + } + request.signal.addEventListener('abort', abortListener, { once: true }) + // An attached reader is a chat view that starts this turn's workflow tools. + const releaseViewer = await holdStreamViewer(streamId) + + /** Delivers the ring's events after the cursor, or returns null if it trimmed the next one. */ + const flushEvents = async (): Promise => { + const events = await readEvents(streamId, cursor) + if (!startsAfterCursor(events, cursor)) { + logger.warn('Replay ring trimmed past a reader cursor', { streamId, cursor }) + return null + } + if (events.length > 0) { + logger.debug('[Resume] Flushing events', { + streamId, + afterCursor: cursor, + eventCount: events.length, + }) + } + for (const envelope of events) { + if (!enqueueEvent(toReplayEnvelope(envelope))) { + break + } + totalEventsFlushed += 1 + cursor = envelope.stream.cursor ?? String(envelope.seq) + currentRequestId = extractEnvelopeRequestId(envelope) || currentRequestId + if (envelope.type === MothershipStreamV1EventType.complete) { + sawTerminalEvent = true + } + } + return events.length + } + + const emitTerminalIfMissing = ( + status: MothershipStreamV1CompletionStatus, + options?: { message?: string; code: string; reason?: string } + ) => { + if (controllerClosed || sawTerminalEvent) { + return + } + for (const envelope of buildResumeTerminalEnvelopes({ + streamId, + afterCursor: cursor, + status, + message: options?.message, + code: options?.code ?? 'resume_terminal', + reason: options?.reason, + requestId: currentRequestId, + })) { + if (!enqueueEvent(envelope)) { + break + } + cursor = envelope.stream.cursor ?? String(envelope.seq) + if (envelope.type === MothershipStreamV1EventType.complete) { + sawTerminalEvent = true + } + } + } + + /** Forwards the worker's replay, keeping the response alive while it waits. */ + const streamRunReplay = async (body: ReadableStream) => { + const keepalive = setInterval(() => { + if (Date.now() - lastWriteTime < REPLAY_KEEPALIVE_INTERVAL_MS) return + if (!enqueueComment('keepalive')) replayAbort.abort() + }, REPLAY_KEEPALIVE_INTERVAL_MS) + try { + const end = await forwardRunReplay({ + body, + streamId, + signal: replaySignal, + write: (envelope) => { + if (!enqueueEvent(envelope)) return false + totalEventsFlushed += 1 + cursor = envelope.stream.cursor ?? cursor + if (envelope.type === MothershipStreamV1EventType.complete) sawTerminalEvent = true + return true + }, + readRunStatus: async () => (await readRun().catch(() => null))?.status ?? null, + isClosed: () => controllerClosed, + deadlineAt: startTime + MAX_STREAM_MS, + }) + logger.info('[Resume] Run replay ended', { streamId, end, eventCount: totalEventsFlushed }) + } finally { + clearInterval(keepalive) + replayAbort.abort() + } + } + + try { + enqueueComment('accepted') + + if (replayBody) { + await streamRunReplay(replayBody) + return + } + if (resyncFromLog) { + const position = gap ?? (await readRingPosition(streamId, cursor)) + const terminal = await replayGapTerminal(streamId, position, currentRequestId) + for (const envelope of terminal.envelopes) { + if (!enqueueEvent(envelope)) { + break + } + cursor = envelope.stream.cursor ?? String(envelope.seq) + currentRequestId = extractEnvelopeRequestId(envelope) || currentRequestId + if (envelope.type === MothershipStreamV1EventType.complete) { + sawTerminalEvent = true + } + } + return + } + + let lastFlushed = await flushEvents() + if (lastFlushed === null) return + + let pollDelayMs = POLL_INTERVAL_MS + while (!controllerClosed && Date.now() - startTime < MAX_STREAM_MS) { + pollIterations += 1 + const currentRun = await readRun().catch((err) => { + logger.warn('Failed to poll latest run for stream', { + streamId, + error: getErrorMessage(err), + }) + return null + }) + if (!currentRun) { + emitTerminalIfMissing(MothershipStreamV1CompletionStatus.error, { + message: 'The stream could not be recovered because its run metadata is unavailable.', + code: 'resume_run_unavailable', + reason: 'run_unavailable', + }) + break + } + // The ring lost its head, restarted or expired under this live tail; the re-attach + // re-syncs, and a finished run answers its terminal instead. Only a quiet ring can + // restart or be re-sent into by a recovery, so a busy tail checks every few polls. + const checkRing = lastFlushed === 0 || pollIterations % RING_CHECK_EVERY_BUSY_POLLS === 0 + if ( + checkRing && + !isTerminalStreamStatus(currentRun.status) && + !ringCanServe(await readRingPosition(streamId, cursor)) + ) { + logger.warn('Replay ring can no longer serve a live tail', { streamId, cursor }) + break + } + + currentRequestId = extractRunRequestId(currentRun) || currentRequestId + + const flushed = await flushEvents() + if (flushed === null) break + lastFlushed = flushed + /* Adaptive tail: 4 Hz only while events are actually flowing; a quiet stream + decays toward the cap so an attached client doesn't hammer Postgres + Redis + at 4 Hz for up to an hour. Any flushed event snaps back to full rate. */ + pollDelayMs = + flushed > 0 ? POLL_INTERVAL_MS : Math.min(pollDelayMs * 2, POLL_INTERVAL_MAX_MS) + + if (controllerClosed) { + break + } + if (isTerminalStreamStatus(currentRun.status)) { + emitTerminalIfMissing(currentRun.status, { + message: + currentRun.status === MothershipStreamV1CompletionStatus.error + ? typeof currentRun.error === 'string' + ? currentRun.error + : 'The recovered stream ended with an error.' + : undefined, + code: 'resume_terminal_status', + reason: 'terminal_status', + }) + break + } + + if (request.signal.aborted) { + controllerClosed = true + break + } + + if (Date.now() - lastWriteTime >= REPLAY_KEEPALIVE_INTERVAL_MS) { + enqueueComment('keepalive') + } + + await sleep(pollDelayMs) + } + } catch (error) { + if (!controllerClosed && !request.signal.aborted) { + logger.warn('Stream replay failed', { + streamId, + error: getErrorMessage(error), + }) + emitTerminalIfMissing(MothershipStreamV1CompletionStatus.error, { + message: 'The stream replay failed before completion.', + code: 'resume_internal', + reason: 'stream_replay_failed', + }) + } + markSpanForError(rootSpan, error) + } finally { + releaseViewer() + request.signal.removeEventListener('abort', abortListener) + // Read before closing: closing the controller here is this route ending, not the client. + const clientDisconnected = controllerClosed + closeController() + rootSpan.setAttributes({ + [TraceAttr.CopilotResumeOutcome]: sawTerminalEvent + ? CopilotResumeOutcome.TerminalDelivered + : clientDisconnected + ? CopilotResumeOutcome.ClientDisconnected + : CopilotResumeOutcome.EndedWithoutTerminal, + [TraceAttr.CopilotResumeEventCount]: totalEventsFlushed, + [TraceAttr.CopilotResumePollIterations]: pollIterations, + [TraceAttr.CopilotResumeDurationMs]: Date.now() - startTime, + }) + rootSpan.end() + } + } - return copilotStreamGet(request, undefined) + return new Response(stream, { + headers: { + ...SSE_RESPONSE_HEADERS, + ...(replayBody ? { [MOTHERSHIP_STREAM_REPLAY_HEADER]: 'log' } : {}), + ...(run.desktopDeviceId ? { [MOTHERSHIP_DESKTOP_EXECUTOR_HEADER]: 'device' } : {}), + }, + }) } diff --git a/apps/sim/app/api/v1/copilot/chat/route.ts b/apps/sim/app/api/v1/copilot/chat/route.ts deleted file mode 100644 index 970cbab2a81..00000000000 --- a/apps/sim/app/api/v1/copilot/chat/route.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { NextResponse } from 'next/server' -import { withRouteHandler } from '@/lib/core/utils/with-route-handler' - -/** - * POST /api/v1/copilot/chat - * - * Deprecated: the v1 headless copilot chat API has been removed. The endpoint - * returns 410 Gone for all callers. - * - * permission-group-exempt: none — the route authenticates nobody and reaches no - * resource, so there is no workspace in which to resolve a group. The - * counterpart `chat.send` declares `copilot.use`; when this surface returns - * anything but 410 again, it declares that capability. - */ -export const POST = withRouteHandler(async () => - NextResponse.json( - { - success: false, - error: 'The v1 copilot chat API has been deprecated and is no longer available.', - }, - { status: 410, headers: { 'Cache-Control': 'no-store' } } - ) -) diff --git a/apps/sim/app/playground/page.tsx b/apps/sim/app/playground/page.tsx deleted file mode 100644 index fa5a295eacd..00000000000 --- a/apps/sim/app/playground/page.tsx +++ /dev/null @@ -1,1079 +0,0 @@ -'use client' - -import { useState, useSyncExternalStore } from 'react' -import { - Avatar, - AvatarFallback, - AvatarImage, - Badge, - BubbleChatClose, - BubbleChatPreview, - Button, - Checkbox, - ChevronDown, - ChipButtonGroup, - ChipButtonGroupItem, - ChipDatePicker, - Code, - Combobox, - Connections, - Cursor, - DocumentAttachment, - Download, - Duplicate, - Expand, - Eye, - FolderCode, - FolderPlus, - Hand, - HexSimple, - Input, - Key as KeyIcon, - Label, - Layout, - Library, - Loader, - Modal, - ModalBody, - ModalContent, - ModalDescription, - ModalFooter, - ModalHeader, - ModalTabs, - ModalTabsContent, - ModalTabsList, - ModalTabsTrigger, - ModalTrigger, - MoreHorizontal, - NoWrap, - PanelLeft, - Play, - PlayOutline, - Popover, - PopoverBackButton, - PopoverContent, - PopoverFolder, - PopoverItem, - PopoverScrollArea, - PopoverSearch, - PopoverSection, - PopoverTrigger, - Redo, - Rocket, - Slider, - Switch, - Table, - TableBody, - TableCaption, - TableCell, - TableFooter, - TableHead, - TableHeader, - TableRow, - TagInput, - type TagItem, - Textarea, - TimePicker, - Tooltip, - Trash, - toast, - Undo, - Wrap, - ZoomIn, - ZoomOut, -} from '@sim/emcn' -import { ArrowLeft, Folder, Moon, Sun } from '@sim/emcn/icons' -import { notFound, useRouter } from 'next/navigation' -import { env, isTruthy } from '@/lib/core/config/env' - -function Section({ title, children }: { title: string; children: React.ReactNode }) { - return ( -

-

- {title} -

-
{children}
-
- ) -} - -function VariantRow({ label, children }: { label: string; children: React.ReactNode }) { - return ( -
- {label} -
{children}
-
- ) -} - -const SAMPLE_CODE = `function greet(name) { - console.log("Hello, " + name); - return { success: true }; -}` - -const SAMPLE_PYTHON = `def greet(name): - print(f"Hello, {name}") - return {"success": True}` - -const COMBOBOX_OPTIONS = [ - { label: 'Option 1', value: 'opt1' }, - { label: 'Option 2', value: 'opt2' }, - { label: 'Option 3', value: 'opt3' }, -] - -const DARK_MODE_EVENT = 'playground:dark-mode-change' - -const subscribeToDarkMode = (onStoreChange: () => void) => { - window.addEventListener(DARK_MODE_EVENT, onStoreChange) - return () => window.removeEventListener(DARK_MODE_EVENT, onStoreChange) -} - -const getDarkModeSnapshot = () => document.documentElement.classList.contains('dark') -const getServerDarkModeSnapshot = () => false - -export default function PlaygroundPage() { - const router = useRouter() - const [comboboxValue, setComboboxValue] = useState('') - const [switchValue, setSwitchValue] = useState(false) - const [checkboxValue, setCheckboxValue] = useState(false) - const [sliderValue, setSliderValue] = useState([50]) - const [timeValue, setTimeValue] = useState('09:30') - const isDarkMode = useSyncExternalStore( - subscribeToDarkMode, - getDarkModeSnapshot, - getServerDarkModeSnapshot - ) - const [buttonGroupValue, setButtonGroupValue] = useState('curl') - const [dateValue, setDateValue] = useState('') - const [dateRangeStart, setDateRangeStart] = useState('') - const [dateRangeEnd, setDateRangeEnd] = useState('') - const [tagItems, setTagItems] = useState([ - { value: 'user@example.com', isValid: true }, - { value: 'invalid-email', isValid: false, error: 'Invalid email format' }, - ]) - - const toggleDarkMode = () => { - document.documentElement.classList.toggle('dark') - window.dispatchEvent(new Event(DARK_MODE_EVENT)) - } - - if (!isTruthy(env.NEXT_PUBLIC_ENABLE_PLAYGROUND)) { - notFound() - } - - return ( - <> - -
-
- - - - - Go back - -
-
- - - - - {isDarkMode ? 'Light mode' : 'Dark mode'} - -
-
-
-

EMCN Component Playground

-

- All emcn UI components and their variants -

-
- - {/* Toast */} -
- - - - - - - - - - - - - - - - - - - - - - - - - -
- - {/* Button */} -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - cURL - Python - JavaScript - - - - - Option 1 - Option 2 - - - - - Option 1 - Option 2 - - - - - Option 1 - Option 2 - - - - - Only Option - - -
- - {/* Badge */} -
- - Default - - - Outline - - - Type - - - Green - - With Dot - - - - Red - - With Dot - - - - Blue - - With Dot - - - - Blue Secondary - - - Purple - - - Orange - - - Amber - - - Teal - - - Cyan - - - Gray - - - Gray Secondary - - - Small - Medium - Large - -
- - {/* Input */} -
- - - - - - -
- - {/* TagInput */} -
- -
- { - const isValid = value.includes('@') && value.includes('.') - setTagItems((prev) => [...prev, { value, isValid }]) - return isValid - }} - onRemove={(_, index) => { - setTagItems((prev) => prev.filter((_, i) => i !== index)) - }} - placeholder='Enter emails...' - placeholderWithTags='Add another' - /> -
-
- -
- true} - onRemove={() => {}} - placeholder='Add tags' - placeholderWithTags='Add another' - triggerKeys={['Enter', ',']} - /> -
-
- -
- false} - onRemove={() => {}} - placeholder='Disabled input' - disabled - /> -
-
-
- - {/* Textarea */} -
-