release: wire /api/chat/workflow to Vercel Workflow (#581)

Merged
SW
6mo ago
+1478-94

Summary

Release of PR #581 — wires `POST /api/chat/workflow` to a durable Vercel Workflow agent loop. Replaces the stub UIMessage stream from #579 with real `wrun_` workflow runs + real LLM-driven streaming via `@ai-sdk/gateway`.

PR 3 of 4 for the open-agents → api chat-workflow port.

What's now live

  • `POST /api/chat/workflow` returns a real durable workflow stream (no tools yet — those come in PR 4)
  • Resume / 409 / conflict resolution via `reconcileExistingActiveStream` + `maybeResumeChatStream`
  • CAS-before-start ordering closes the race where two concurrent requests could bill the model twice
  • Auto-title from first user message (truncated to 80 chars including suffix)
  • Lifecycle activity refresh so long-running responses don't look idle
  • Workflow files at `app/lib/workflows/` (`runAgentWorkflow`, `runAgentStep`)
  • Generic Supabase helpers + domain wrappers: `updateChat`, `upsertChatMessage`, `selectChatMessages` (plumbing) + `compareAndSetChatActiveStreamId` (domain CAS)

Verification

  • ✅ Full suite 2955/2955 pass, lint clean
  • ✅ E2E verified on preview: real `wrun_` run-id, real LLM streaming, full SSE protocol matches `sandbox.recoupable.com`
  • ✅ All review feedback addressed (sweetman structural + cubic P1/P2) — see PR #581 thread

🤖 Generated with Claude Code


Summary by cubic

Wires POST /api/chat/workflow to a durable Vercel Workflow that streams real LLM responses with wrun_ run IDs via @ai-sdk/gateway. Adds safe resume/409 handling and CAS-before-start to prevent duplicate billing.

  • New Features

    • Streams from a real workflow: starts runAgentWorkflow (single turn for now) and returns SSE with x-workflow-run-id.
    • Resume or 409: maybeResumeChatStream resumes live runs or returns conflict when uncertain; otherwise starts fresh.
    • Safe start: claims chats.active_stream_id with a pending-<uuid> before start(), then promotes to the real run id.
    • Message persistence: saves the latest user message and bumps updated_at before queueing; auto-titles from the first user message (80 chars max, with …).
    • Model selection: uses chat.model_id when set, else defaults to anthropic/claude-haiku-4.5.
    • Agent instructions: centralized agentCustomInstructions with workspace file link formatting and Recoup API guidance.
  • Bug Fixes

    • No duplicate runs on races: CAS-before-start ordering; on promote loss, getRun(runId).cancel() is awaited.
    • Reliable conflict semantics: transient workflow/api failures no longer clear active_stream_id; failed CAS-clear returns 409.
    • Clear DB outcomes: Supabase helpers return discriminated results to distinguish “race lost” from “DB error”.
    • Stream writer stability: acquire/release a single writer per step to avoid lock leaks.
    • Precise title truncation: exactly 80 chars including the suffix.

Summary by CodeRabbit

Release Notes

  • New Features
    • Implemented durable chat workflow system with persistent message storage for improved chat continuity
    • Added workspace file linking in chat responses for better code navigation
    • Enabled resumption of interrupted chat workflows to prevent message loss
    • Enhanced agent with improved API usage guidance and resource recommendations

Review Change Stack