Skip to content

Commit 6458674

Browse files
authored
Merge pull request #13 from BjornMelin/feat/workflow-ai-sdk-streamdown-ai-elements
feat(workflow): integrate Vercel Workflow + AI Elements
2 parents 0efc392 + 4122b03 commit 6458674

155 files changed

Lines changed: 23079 additions & 275 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ APP_BASE_URL=
3030

3131
# Database (Neon Postgres)
3232
DATABASE_URL=
33+
# Optional: unpooled connection string (recommended for migrations/DDL).
34+
DATABASE_URL_UNPOOLED=
3335

3436
# Upstash Redis (cache + rate limit)
3537
UPSTASH_REDIS_REST_TOKEN=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ repomix-output.md
107107
.gemini/
108108
.claude/
109109
opensrc/
110+
config/mcporter.json
110111
# next-agents-md
111112
.next-docs/
112113
.env*.local

AGENTS.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ bun run fetch:models # Update AI model catalog (requires AI_GATEWAY_AP
3535
- TSDoc syntax enforcement (`tsdoc/syntax`)
3636
- JSDoc policy on exported APIs (warn-level for iteration speed)
3737

38+
## Import policy
39+
40+
- Do **not** switch to package barrel imports by default.
41+
- Barrel imports are allowed only for packages listed in `next.config.ts` `experimental.optimizePackageImports` (currently `radix-ui` and `lucide-react`).
42+
- For all other packages, use explicit/non-barrel imports.
43+
3844
## Drizzle + database
3945

4046
- `drizzle.config.ts` loads Next.js `.env*` files and requires `DATABASE_URL` for
@@ -58,8 +64,9 @@ bun run fetch:models # Update AI model catalog (requires AI_GATEWAY_AP
5864

5965
## AI Gateway
6066

61-
- Prefer `ai` package `gateway(...)` usage for model routing.
62-
- OpenAI-compatible base URL is `https://ai-gateway.vercel.sh/v1`.
67+
- Prefer the AI SDK's `gateway(...)` / `createGateway(...)` provider for model routing.
68+
- AI SDK Gateway provider base URL is `https://ai-gateway.vercel.sh/v3/ai` (default; configurable via `AI_GATEWAY_BASE_URL`).
69+
- OpenAI-compatible base URL is `https://ai-gateway.vercel.sh/v1` (used for OpenAI SDK compatibility and `GET /models`).
6370
- `bun run fetch:models` writes a model catalog JSON (default: `docs/ai-gateway-models.json`).
6471

6572
## Documentation standards

bun.lock

Lines changed: 749 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://ui.shadcn.com/schema.json",
3+
"aliases": {
4+
"components": "@/components",
5+
"hooks": "@/hooks",
6+
"lib": "@/lib",
7+
"ui": "@/components/ui",
8+
"utils": "@/lib/utils"
9+
},
10+
"iconLibrary": "lucide",
11+
"registries": {},
12+
"rsc": true,
13+
"rtl": false,
14+
"style": "new-york",
15+
"tailwind": {
16+
"baseColor": "zinc",
17+
"config": "",
18+
"css": "src/app/globals.css",
19+
"cssVariables": true,
20+
"prefix": ""
21+
},
22+
"tsx": true
23+
}

docs/architecture/adr/ADR-0005-orchestration-upstash-qstash-for-durable-workflows.md

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
ADR: 0005
3-
Title: Orchestration: Upstash QStash for durable workflows
3+
Title: Orchestration: Upstash QStash for background jobs (ingestion + fanout)
44
Status: Accepted
5-
Version: 0.3
5+
Version: 0.4
66
Date: 2026-02-03
77
Supersedes: []
88
Superseded-by: []
9-
Related: [ADR-0013, ADR-0012]
9+
Related: [ADR-0013, ADR-0012, ADR-0026]
1010
Tags: [architecture, reliability]
1111
References:
1212
- [QStash Next.js quickstart](https://upstash.com/docs/qstash/quickstarts/vercel-nextjs)
@@ -16,18 +16,26 @@ References:
1616
## Status
1717

1818
Accepted — 2026-01-30.
19+
Updated — 2026-02-03: re-scoped to background jobs (ingestion + fanout); interactive runs moved to ADR-0026.
1920

2021
## Description
2122

22-
Use QStash to execute multi-step runs durably and idempotently.
23+
Use QStash to execute **background jobs** durably and idempotently, with signed
24+
delivery, retries, and rate limiting.
25+
26+
This ADR no longer governs **interactive runs or chat streaming**. Those are
27+
handled by Vercel Workflow DevKit per [ADR-0026](./ADR-0026-orchestration-vercel-workflow-devkit-for-interactive-runs.md).
2328

2429
See [SPEC-0021](../spec/SPEC-0021-full-stack-finalization-fluid-compute-neon-upstash-ai-elements.md)
2530
for the cross-cutting “finalization” plan that ties QStash orchestration into
26-
run persistence, UI, and step execution patterns.
31+
ingestion, UI, and background step execution patterns.
2732

2833
## Context
2934

30-
Full spec generation and research can exceed route execution limits. Durable orchestration ensures work completes even if the user closes the browser. QStash provides serverless-friendly HTTP-based queueing, signature verification, and retries.
35+
Ingestion and other background jobs (extract/chunk/embed/index) can exceed route
36+
execution limits and should continue even if the user closes the browser. QStash
37+
provides serverless-friendly HTTP-based queueing, signature verification, and
38+
retries.
3139

3240
## Decision Drivers
3341

@@ -55,43 +63,46 @@ Full spec generation and research can exceed route execution limits. Durable orc
5563

5664
## Decision
5765

58-
We will use **Upstash QStash** to orchestrate run steps, verifying signatures and enforcing step idempotency.
66+
We will use **Upstash QStash** to orchestrate background jobs (primarily the ingestion pipeline), verifying signatures and enforcing idempotency.
5967

6068
## Constraints
6169

62-
- Step endpoints must verify QStash signatures.
70+
- Background job endpoints must verify QStash signatures.
6371
- Verification requires `QSTASH_CURRENT_SIGNING_KEY` and `QSTASH_NEXT_SIGNING_KEY`.
64-
- Steps must be idempotent per (runId, stepName).
72+
- Jobs must be idempotent (e.g., by `(fileId, stage)` and/or content hash).
6573
- Ensure at-least-once delivery does not duplicate artifacts.
6674

6775
## High-Level Architecture
6876

6977
```mermaid
7078
flowchart LR
71-
UI --> Runs[/src/app/api/runs/]
72-
Runs --> DB[(Neon)]
73-
Runs --> Q[(QStash)]
74-
Q --> Step[/src/app/api/jobs/run-step/]
75-
Step --> DB
76-
Step --> Tools[AI Gateway / Exa / Firecrawl / Vector]
79+
UI --> Upload[/src/app/api/upload/]
80+
Upload --> DB[(Neon)]
81+
Upload --> Q[(QStash)]
82+
Q --> Ingest[/src/app/api/jobs/ingest-file/]
83+
Ingest --> DB
84+
Ingest --> Tools[AI Gateway / Vector / Blob]
7785
```
7886

7987
## Related Requirements
8088

8189
### Functional Requirements
8290

83-
- **FR-010:** Durable run execution.
84-
- **FR-011:** Persist step status and outputs.
91+
- **FR-003:** Upload files to a project.
92+
- **FR-004:** Store original files durably.
93+
- **FR-005:** Extract text + structural metadata.
94+
- **FR-006:** Chunk extracted content.
95+
- **FR-007:** Embed + index chunks in vector store.
8596

8697
### Non-Functional Requirements
8798

8899
- **NFR-004:** Observability for each step.
89-
- **NFR-005:** Deterministic artifacts per step.
100+
- **NFR-006:** Cost controls for background processing.
90101

91102
### Performance Requirements
92103

93-
- **PR-004:** Runs survive disconnects.
94104
- **PR-005:** Idempotent steps.
105+
- **PR-003:** Ingest 10 MB PDF within target (excluding queue delay).
95106

96107
### Integration Requirements
97108

@@ -101,25 +112,34 @@ flowchart LR
101112

102113
### Architecture Overview
103114

104-
- A run is a DB record with ordered steps.
105-
- Each step handler acquires a DB lease, executes, writes outputs, and marks status.
115+
QStash is used to deliver background processing jobs (ingestion and related
116+
fanout tasks) reliably:
117+
118+
- upload endpoint publishes a QStash message referencing the file/project
119+
- ingestion worker verifies the signature, loads state from DB, and advances the pipeline
120+
- retries are expected; all operations must be idempotent
121+
122+
The ingestion pipeline details are defined in:
123+
124+
- [SPEC-0003](../spec/SPEC-0003-upload-ingestion-pipeline.md)
125+
- [SPEC-0021](../spec/SPEC-0021-full-stack-finalization-fluid-compute-neon-upstash-ai-elements.md)
106126

107127
### Implementation Details
108128

109-
- Store `attempt` and `error` in `run_steps`.
110-
- Provide a UI action to retry a failed step.
111-
- Cap maximum retries and enforce budgets.
129+
- Verify QStash signatures on every background job route.
130+
- Enforce idempotency at the file/content hash boundary (avoid double indexing).
131+
- Cap retries and enforce budgets to avoid runaway costs.
112132

113133
### File locations (target)
114134

115-
- `src/app/api/runs/route.ts`
116-
- `src/app/api/jobs/run-step/route.ts`
135+
- `src/app/api/upload/route.ts`
136+
- `src/app/api/jobs/ingest-file/route.ts`
117137

118138
## Testing
119139

120-
- Integration: replay same step twice yields identical artifacts.
121140
- Contract: unsigned requests rejected.
122-
- E2E: run completes after client disconnect.
141+
- Integration: replay same job twice yields the same DB/vector state (idempotent).
142+
- E2E: ingestion completes after client disconnect.
123143

124144
## Implementation Notes
125145

@@ -151,3 +171,4 @@ flowchart LR
151171
- **0.1 (2026-01-29)**: Initial version.
152172
- **0.2 (2026-01-30)**: Updated for current repo baseline (Bun, `src/` layout, CI).
153173
- **0.3 (2026-02-03)**: Linked to SPEC-0021 as the cross-cutting finalization spec.
174+
- **0.4 (2026-02-03)**: Re-scoped to background jobs (ingestion + fanout); interactive runs moved to ADR-0026.

docs/architecture/adr/ADR-0006-agent-runtime-ai-sdk-v6-toolloopagent-streaming-ui-responses.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
---
22
ADR: 0006
3-
Title: Agent runtime: AI SDK v6 ToolLoopAgent + streaming UI responses
3+
Title: Agent runtime: AI SDK v6 agents + streaming UI responses (Workflow DevKit durable sessions)
44
Status: Accepted
5-
Version: 0.3
5+
Version: 0.4
66
Date: 2026-02-03
77
Supersedes: []
88
Superseded-by: []
9-
Related: [ADR-0007, ADR-0011, ADR-0012]
9+
Related: [ADR-0007, ADR-0011, ADR-0012, ADR-0026]
1010
Tags: [agents, architecture]
1111
References:
1212
- [AI SDK agents](https://ai-sdk.dev/docs/agents/overview)
1313
- [ToolLoopAgent](https://ai-sdk.dev/docs/reference/ai-sdk-core/tool-loop-agent)
14-
- [createAgentUIStreamResponse](https://ai-sdk.dev/docs/reference/ai-sdk-core/create-agent-ui-stream-response)
14+
- [createUIMessageStreamResponse](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response)
15+
- [Workflow DevKit: DurableAgent](https://useworkflow.dev/docs/api-reference/workflow-ai/durable-agent)
16+
- [Workflow DevKit: Resumable streams](https://useworkflow.dev/docs/ai/resumable-streams)
1517
---
1618

1719
## Status
1820

1921
Accepted — 2026-01-30.
22+
Updated — 2026-02-03: aligned with Workflow DevKit durable sessions and resumable streaming (ADR-0026).
2023

2124
## Description
2225

@@ -26,9 +29,17 @@ See [SPEC-0021](../spec/SPEC-0021-full-stack-finalization-fluid-compute-neon-ups
2629
for the cross-cutting “finalization” plan that ties agent streaming into the
2730
workspace UI, retrieval, caching, and durable orchestration.
2831

32+
See [SPEC-0022](../spec/SPEC-0022-vercel-workflow-durable-runs-and-streaming-contracts.md)
33+
for the canonical Workflow DevKit integration (durable multi-turn sessions,
34+
resumable streams, hook endpoints).
35+
2936
## Context
3037

31-
The system requires multi-step reasoning and tool usage. AI SDK v6 provides ToolLoopAgent for iterative tool calls and createAgentUIStreamResponse for streaming message parts to the UI in Next.js Route Handlers.
38+
The system requires multi-step reasoning and tool usage. AI SDK v6 provides
39+
agent loops (ToolLoopAgent semantics) and UI message streaming primitives.
40+
For production-grade resumable streaming and multi-turn durability, we run the
41+
agent loop inside Workflow DevKit using `@workflow/ai` `DurableAgent` and stream
42+
via Workflow run streams (see [ADR-0026](./ADR-0026-orchestration-vercel-workflow-devkit-for-interactive-runs.md) for orchestration rationale).
3243

3344
## Decision Drivers
3445

@@ -56,7 +67,10 @@ The system requires multi-step reasoning and tool usage. AI SDK v6 provides Tool
5667

5768
## Decision
5869

59-
We will implement agents using **ToolLoopAgent** and stream UI output using **createAgentUIStreamResponse** in Route Handlers.
70+
We will implement agent loops using **AI SDK v6 agents** (ToolLoopAgent-style
71+
tool loops) and stream UI output using **AI SDK UI message streams**. In
72+
interactive/durable paths, we will execute these loops inside Workflow DevKit
73+
via `@workflow/ai` `DurableAgent` and expose resumable streams to clients.
6074

6175
## Constraints
6276

@@ -92,7 +106,7 @@ flowchart LR
92106
### Performance Requirements
93107

94108
- **PR-001:** fast streaming start.
95-
- **PR-004:** durable runs via QStash.
109+
- **PR-004:** durable runs and resumable streams (Workflow DevKit).
96110

97111
### Integration Requirements
98112

@@ -146,3 +160,4 @@ flowchart LR
146160
- **0.1 (2026-01-29)**: Initial version.
147161
- **0.2 (2026-01-30)**: Updated for current repo baseline (Bun, `src/` layout, CI).
148162
- **0.3 (2026-02-03)**: Linked to SPEC-0021 as the cross-cutting finalization spec.
163+
- **0.4 (2026-02-03)**: Updated for Workflow DevKit durable sessions + resumable streaming alignment.

docs/architecture/adr/ADR-0011-ui-ai-elements-streamdown-shadcn-ui-tailwind-v4.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ See [SPEC-0021](../spec/SPEC-0021-full-stack-finalization-fluid-compute-neon-ups
2828
for the cross-cutting “finalization” plan that specifies the workspace
2929
information architecture and the API contracts required by the UI.
3030

31+
See [SPEC-0023](../spec/SPEC-0023-ai-elements-workspace-ui-and-interaction-model.md)
32+
for the UI information architecture, component-vendoring plan, and interaction
33+
model (chat/runs/search/uploads).
34+
3135
## Context
3236

3337
Building chat and streaming UI primitives from scratch is slow and error-prone. AI Elements provides ready-made components designed for AI SDK message parts. Streamdown improves streaming markdown rendering. shadcn/ui provides accessible UI components aligned with Tailwind v4.
@@ -141,4 +145,4 @@ flowchart LR
141145

142146
- **0.1 (2026-01-29)**: Initial version.
143147
- **0.2 (2026-01-30)**: Updated for current repo baseline (Bun, `src/` layout, CI).
144-
- **0.3 (2026-02-03)**: Linked to SPEC-0021 as the cross-cutting finalization spec.
148+
- **0.3 (2026-02-03)**: Linked to SPEC-0021 as the cross-cutting finalization spec; added SPEC-0023 reference for UI/interaction model.

0 commit comments

Comments
 (0)