sweagent 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  <p align="center">
2
2
  <h1 align="center">sweagent</h1>
3
3
  <p align="center">
4
- <strong>A multi-provider AI software engineering agent framework with tool calling.</strong>
4
+ <strong>The Deep Thinking layer that makes Cursor, Claude Code, and Codex 10x more effective.</strong>
5
5
  </p>
6
6
  <p align="center">
7
- Build intelligent agents that execute tools and integrate with OpenAI, Anthropic, and Google. TypeScript-first, type-safe tools, subagent orchestration, and MCP support.
7
+ Deep, multi-stage reasoning before a single line of code is written. Domain-specialized agent pipelines generate structured blueprints -- requirements, data models, API contracts, auth flows, and architecture specs -- through iterative LLM calls and sub-agent decomposition. Hand the result to your coding agent.
8
8
  </p>
9
9
  </p>
10
10
 
@@ -16,13 +16,20 @@
16
16
  </p>
17
17
 
18
18
  <p align="center">
19
- <a href="#what-is-sweagent">What is sweagent?</a> •
19
+ <a href="#the-problem">The Problem</a> •
20
+ <a href="#use-with-cursor-claude-code-and-codex">Use with Coding Agents</a> •
20
21
  <a href="#why-sweagent">Why sweagent?</a> •
22
+ <a href="#deep-reasoning-philosophy">Deep Reasoning</a> •
23
+ <a href="#how-it-works">How It Works</a> •
24
+ <a href="#features">Features</a> •
25
+ <a href="#planning-pipeline">Planning Pipeline</a> •
26
+ <a href="#full-pipeline">Full Pipeline</a> •
27
+ <a href="#domain-agent-modules">Modules</a> •
28
+ <a href="#getting-started">Getting Started</a> •
21
29
  <a href="#installation">Installation</a> •
22
- <a href="#getting-started-tutorial">Tutorial</a> •
23
30
  <a href="#architecture">Architecture</a> •
24
31
  <a href="#api-reference">API Reference</a> •
25
- <a href="#production-modules">Modules</a> •
32
+ <a href="#reference">Reference</a> •
26
33
  <a href="#examples">Examples</a> •
27
34
  <a href="#contributing">Contributing</a>
28
35
  </p>
@@ -31,165 +38,828 @@
31
38
 
32
39
  ## Table of Contents
33
40
 
34
- - [What is sweagent?](#what-is-sweagent)
41
+ - [The Problem](#the-problem)
42
+ - [Use with Cursor, Claude Code, and Codex](#use-with-cursor-claude-code-and-codex)
35
43
  - [Why sweagent?](#why-sweagent)
44
+ - [Deep Reasoning Philosophy](#deep-reasoning-philosophy)
45
+ - [How It Works](#how-it-works)
36
46
  - [Features](#features)
47
+ - [Planning Pipeline](#planning-pipeline)
48
+ - [Full Pipeline](#full-pipeline)
49
+ - [Domain Agent Modules](#domain-agent-modules)
50
+ - [Getting Started](#getting-started)
37
51
  - [Installation](#installation)
38
- - [Getting Started Tutorial](#getting-started-tutorial)
39
52
  - [Architecture](#architecture)
40
- - [Engineering Deep Dive](#engineering-deep-dive)
41
53
  - [API Reference](#api-reference)
42
- - [Production Modules](#production-modules)
54
+ - [Reference](#reference)
43
55
  - [Examples](#examples)
44
- - [Configuration Reference](#configuration-reference)
45
- - [FAQ](#faq)
46
- - [Troubleshooting](#troubleshooting)
47
56
  - [Contributing](#contributing)
48
57
  - [License](#license)
49
58
 
50
59
  ---
51
60
 
52
- ## What is sweagent?
61
+ ## The Problem
53
62
 
54
- **sweagent** is a multi-provider AI software engineering agent framework for TypeScript and Node.js. It gives you a single, consistent API to build agents that call tools, delegate to subagents, and run across OpenAI, Anthropic, and Google—with no provider lock-in.
63
+ AI coding agents -- Cursor, Claude Code, Codex -- are powerful executors, but they fail at planning. Hand one a vague requirement and it guesses a tech stack, skips data modeling, forgets auth, and produces half-finished code. Enterprise teams need the same rigor from AI that they expect from senior engineers: structured discovery, explicit requirements, deliberate design, and traceable decisions.
55
64
 
56
- The framework is built around three pillars: **models** (unified provider abstraction), **tools** (Zod-validated, type-safe tool definitions), and **agents** (an iterative loop that invokes the model, executes tool calls, and feeds results back until the task is done). You can add **subagents** so a parent agent delegates subtasks to specialized child agents, and plug in **MCP** (Model Context Protocol) servers for external capabilities. Production-ready modules like **DB Designer** (MongoDB schema from natural language) and **React Builder** (frontend config from GraphQL) show how to orchestrate tools and subagents for real workflows.
65
+ **Without sweagent**, a coding agent receives `"build a task manager"` and immediately starts writing code:
57
66
 
58
- Whether you are prototyping a coding assistant or shipping a multi-step AI pipeline, sweagent keeps the same mental model: create a model, define tools, run an agent. All provider SDKs are included; set your API keys and go.
67
+ - Picks a random framework (maybe Express, maybe Fastify, who knows)
68
+ - Invents a database schema on the fly, misses relationships
69
+ - Forgets authentication entirely
70
+ - Skips error handling and edge cases
71
+ - Produces something that sort-of runs but needs a rewrite
72
+
73
+ **With sweagent**, the coding agent receives a structured blueprint before writing a single line:
74
+
75
+ - **11-section markdown plan** with tech stack, data models, API routes, auth flow, implementation order, edge cases, and testing checklist
76
+ - **Structured JSON requirements** with actors, user flows, stories, and module breakdowns
77
+ - **Database schemas** with exact field types, relationships, indexes, and validation rules
78
+ - **API contracts** with endpoints, methods, request/response shapes, and auth requirements
79
+ - **Frontend architecture** with pages, components, routing, and state management
80
+
81
+ Each pipeline walks through structured stages -- discovery, analysis, design, synthesis -- not a single LLM call. The result is a professional-grade artifact that a coding agent can execute step-by-step, or that a human architect can review and approve.
59
82
 
60
83
  ```typescript
61
- import { createModel, runAgent, defineTool } from 'sweagent';
62
- import { z } from 'zod';
84
+ import { runPlanningAgent } from 'sweagent';
63
85
 
64
- const model = createModel({ provider: 'openai', model: 'gpt-4o-mini' });
65
- const greetTool = defineTool({
66
- name: 'greet',
67
- description: 'Greet someone',
68
- input: z.object({ name: z.string() }),
69
- handler: async ({ name }) => ({ message: `Hello, ${name}!` }),
86
+ const result = await runPlanningAgent({
87
+ input: 'Task manager app with user auth, task CRUD, assignments, and a dashboard',
88
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
70
89
  });
71
90
 
72
- const result = await runAgent({
73
- model,
74
- tools: [greetTool],
75
- systemPrompt: 'You are a helpful assistant.',
76
- input: 'Greet Alice',
91
+ console.log('Plan is implementation-ready. Hand it to your coding agent.');
92
+ console.log(result.output); // Full markdown blueprint
93
+ ```
94
+
95
+ TypeScript-first, built on the Vercel AI SDK, ships with all provider SDKs (OpenAI, Anthropic, Google). Set your API keys and go.
96
+
97
+ ---
98
+
99
+ ## Use with Cursor, Claude Code, and Codex
100
+
101
+ Coding agents are powerful executors -- but they build faster and better when they start from a structured plan instead of a vague prompt. sweagent generates the blueprints; your coding agent implements them.
102
+
103
+ ```mermaid
104
+ flowchart LR
105
+ Requirement["Your idea"] --> sweagent["sweagent"]
106
+ sweagent --> Plan["plan.md / JSON spec"]
107
+ Plan --> Cursor["Cursor"]
108
+ Plan --> ClaudeCode["Claude Code"]
109
+ Plan --> Codex["Codex"]
110
+ Cursor --> Code["Production code"]
111
+ ClaudeCode --> Code
112
+ Codex --> Code
113
+ ```
114
+
115
+ ### With Cursor
116
+
117
+ Generate a plan, save it to your project, and reference it in Cursor chat or `.cursor/rules/`:
118
+
119
+ ```typescript
120
+ import { runPlanningAgent } from 'sweagent';
121
+ import { writeFileSync } from 'fs';
122
+
123
+ const result = await runPlanningAgent({
124
+ input: 'E-commerce with users, products, cart, checkout, admin dashboard',
125
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
77
126
  });
78
- console.log(result.output);
127
+
128
+ writeFileSync('plan.md', result.output);
129
+ // Open plan.md in Cursor and say: "Implement this plan step by step"
130
+ // Or copy plan.md to .cursor/rules/ so every agent session uses it as context
131
+ ```
132
+
133
+ ### With Claude Code
134
+
135
+ Generate a plan and save it as `CLAUDE.md` or a reference file. Claude Code automatically reads `CLAUDE.md` for project context:
136
+
137
+ ```typescript
138
+ import { runPlanningAgent } from 'sweagent';
139
+ import { writeFileSync } from 'fs';
140
+
141
+ const result = await runPlanningAgent({
142
+ input: 'SaaS dashboard with multi-tenancy, billing, and analytics',
143
+ model: { provider: 'anthropic', model: 'claude-sonnet-4-20250514' },
144
+ });
145
+
146
+ // Option 1: Save as CLAUDE.md for automatic context
147
+ writeFileSync('CLAUDE.md', `# Implementation Plan\n\n${result.output}`);
148
+
149
+ // Option 2: Save as plan.md and reference it
150
+ writeFileSync('plan.md', result.output);
151
+ // Then tell Claude Code: "Read plan.md and implement phase 1"
152
+ ```
153
+
154
+ ### With Codex
155
+
156
+ Codex works best with structured, machine-readable specs. Use the Requirement Gatherer or Data Modeler for JSON output:
157
+
158
+ ```typescript
159
+ import { runRequirementGathererAgent } from 'sweagent';
160
+ import { writeFileSync } from 'fs';
161
+
162
+ const result = await runRequirementGathererAgent({
163
+ input: 'Task manager with teams, Kanban boards, and time tracking',
164
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
165
+ maxIterations: 15,
166
+ });
167
+
168
+ // Structured JSON with actors, flows, stories, modules, DB schema, API design
169
+ writeFileSync('requirements.json', result.output);
170
+ // Feed requirements.json to Codex as context for implementation
79
171
  ```
80
172
 
173
+ ### Why this works
174
+
175
+ Without sweagent, a coding agent receives "build a task manager" and immediately starts guessing. With sweagent, it receives:
176
+
177
+ - **11-section markdown plan** with tech stack, data models, API routes, auth flow, implementation order, edge cases, and testing checklist
178
+ - **Structured JSON requirements** with actors, user flows, stories, and module breakdowns
179
+ - **Database schemas** with exact field types, relationships, indexes, and validation rules
180
+ - **API contracts** with endpoints, methods, request/response shapes, and auth requirements
181
+ - **Frontend architecture** with pages, components, routing, and state management
182
+
183
+ The coding agent stops guessing and starts executing a professional-grade blueprint.
184
+
81
185
  ---
82
186
 
83
187
  ## Why sweagent?
84
188
 
85
- Long-running AI agents face a core challenge: they work in discrete sessions, and each new session starts with no memory of the last. That leads to agents trying to do too much in one go (and leaving half-finished work), or declaring the job done too early. We designed sweagent so you can build **effective harnesses** for such agents.
189
+ ### 1. Domain-specialized agents, not generic wrappers
190
+
191
+ Each module is a self-contained agent pipeline purpose-built for its domain. The Data Modeler doesn't reuse the Planning Agent's prompts -- it has its own `entity-analyzer` and `schema-refiner` sub-agents, its own tools (`design_schema`, `validate_data_model`), and its own output schema. The React Builder has a `graphql-analyzer` and `config-validator`. Every domain gets the specialized treatment it deserves.
192
+
193
+ ### 2. Multi-stage pipelines with structured outputs
194
+
195
+ Every domain agent progresses through deliberate stages -- discovery, requirements, design, synthesis -- with dedicated LLM calls at each step. The Planning pipeline makes 8+ sequential LLM calls across 4 stages. The Requirement Gatherer produces structured JSON with actors, user flows, stories, and module breakdowns. No single-shot prompt engineering; each stage builds on the last and produces traceable, reviewable intermediate results.
196
+
197
+ ### 3. Sub-agent orchestration for complex domains
198
+
199
+ When a domain is too complex for a single agent, sweagent delegates to specialized sub-agents. The Data Modeler orchestrator spawns an `entity-analyzer` to extract entities and relationships, then a `schema-refiner` to normalize and validate the schema. The React Builder uses a `graphql-analyzer` to parse the schema and a `config-validator` to verify the output. Sub-agents run in isolation with their own context, tools, and models -- then return condensed results to the orchestrator.
200
+
201
+ ### 4. Enterprise-quality output, not bullet points
86
202
 
87
- The design follows a two-fold pattern. First, **initializer-style setup**: scaffold the environment with clear artifacts—feature lists, progress files, init scripts—so every run knows what “done” looks like and what’s left to do. Second, **incremental coding agents**: each session is prompted to make bounded progress, then leave the codebase in a clean, documented state (e.g. via commits and progress updates). That way the next session can read git history and progress files, get its bearings quickly, and continue without re-discovering the project.
203
+ Plans include tech stack decisions, data models with field-level detail, API routes with request/response shapes, phased implementation order, edge cases, and testing checklists. Requirement documents include actors with permissions, user flows with steps, user stories with acceptance criteria, and module breakdowns with CRUD operations. DB schemas include field types, relationships, indexes, and validation rules. These are blueprints, not summaries.
88
204
 
89
- sweagent’s architecture reflects this. **Modular tools** let you split capabilities into small, testable units. **Subagent delegation** lets a parent agent hand off analysis or refinement to specialized children. **Structured orchestration prompts** (as in the DB Designer and React Builder modules) spell out when to analyze, when to generate, and when to validate. **Typed schemas** (Zod everywhere) keep tool inputs and outputs predictable and safe. Under the hood, we use a **provider adapter** over the Vercel AI SDK so you can swap OpenAI, Anthropic, or Google without changing your agent code, and a **layered error hierarchy** so failures are easy to trace and handle.
205
+ ### 5. Provider-agnostic model layer
206
+
207
+ Your agent code stays the same whether you're using GPT-4o, Claude, or Gemini. One `createModel()` call, one interface, zero provider lock-in. Switch models in config, not in code.
208
+
209
+ ### 6. Incremental progress across sessions
210
+
211
+ Long-running agents fail when they lose context. sweagent encodes patterns for structured progress tracking: feature lists with pass/fail status, progress files, and clean-state principles so each session picks up exactly where the last one left off.
90
212
 
91
213
  ---
92
214
 
93
- ## Features
215
+ ## Deep Reasoning Philosophy
94
216
 
95
- | Feature | Description |
96
- |--------|-------------|
97
- | **Multi-Provider** | Unified API for OpenAI (GPT-4o), Anthropic (Claude), and Google (Gemini). One codebase, switch providers via config. |
98
- | **Type-Safe Tools** | Define tools with Zod schemas; full type inference and validation before execution. |
99
- | **Agent Framework** | Iterative agent loop with tool calling, step callbacks, and configurable max iterations. |
100
- | **Subagent Orchestration** | Parent agents delegate to child agents via tools; optional tool inheritance and isolated models. |
101
- | **MCP Protocol** | Connect to Model Context Protocol servers over HTTP or stdio. Lazy connection, typed tool invocation. |
102
- | **Production Modules** | DB Designer (MongoDB schema from requirements), React Builder (frontend config from GraphQL). |
103
- | **Vision** | Image inputs supported via `model.generateVision()` for vision-capable models. |
104
- | **Zero Extra Deps** | All provider SDKs included; set API keys and run. |
217
+ Coding agents generate code fast. But speed without depth produces fragile, incomplete software. sweagent applies deep reasoning -- structured, multi-stage, decomposed -- so your coding agent receives a blueprint that has been thoroughly worked through before implementation begins.
218
+
219
+ ### Multi-Stage Reasoning
220
+
221
+ sweagent never produces one-shot answers. The Planning Agent progresses through four deliberate stages -- discovery, requirements, design, synthesis -- with dedicated LLM calls at each step. Each stage consumes the output of the previous one, building context incrementally rather than cramming everything into a single prompt. Requirements inform design decisions, design decisions shape API contracts, and API contracts feed the implementation order.
222
+
223
+ ```mermaid
224
+ flowchart LR
225
+ Input["Requirement"] --> Discovery["Discovery"]
226
+ Discovery --> Requirements["Requirements"]
227
+ Requirements --> SubReq1["entity-analyzer"]
228
+ Requirements --> SubReq2["page-planner"]
229
+ Requirements --> SubReq3["flow-designer"]
230
+ SubReq1 --> Design["Design"]
231
+ SubReq2 --> Design
232
+ SubReq3 --> Design
233
+ Design --> SubDes1["endpoint-analyzer"]
234
+ Design --> SubDes2["contract-designer"]
235
+ SubDes1 --> Synthesis["Synthesis"]
236
+ SubDes2 --> Synthesis
237
+ Synthesis --> Output["Blueprint"]
238
+ ```
239
+
240
+ ### Sub-Agent Decomposition
241
+
242
+ When a domain is too complex for a single agent, sweagent delegates to specialized sub-agents that run in isolation with their own context, tools, and models. The Data Modeler spawns an `entity-analyzer` to extract entities, a `relationship-mapper` for cardinality, and a `schema-refiner` to normalize the result. The React Builder uses a `graphql-analyzer` to parse schema structure and a `config-validator` to check the output. Each sub-agent returns condensed results to its orchestrator, keeping context windows focused and reasoning sharp.
243
+
244
+ ### Inference-Time Depth
245
+
246
+ Unlike single-prompt planning tools, sweagent deliberately spends more inference-time compute for higher-quality output. The Planning Agent alone makes 12+ LLM calls across 4 stages and 7 sub-agents. The Data Modeler adds 3 more sub-agent calls. The full pipeline chains 7+ domain agents end-to-end, each with its own multi-call pipeline. This is not a design accident -- more structured reasoning steps produce measurably better blueprints than a single large prompt, the same way a senior engineer produces better architecture by working through each layer separately rather than designing everything at once.
105
247
 
106
248
  ---
107
249
 
108
- ## Installation
250
+ ## How It Works
109
251
 
110
- ### Step 1: Prerequisites
252
+ sweagent is not a single agent. It is a system of domain-specialized agent pipelines organized across the full software planning lifecycle. Each module can run independently, or you can chain them into a full-stack specification pipeline.
111
253
 
112
- - **Node.js** >= 18.0.0
113
- - **npm** >= 8.0.0 (or yarn, pnpm, bun)
254
+ ```mermaid
255
+ flowchart TB
256
+ subgraph input [Your Idea]
257
+ Idea["Natural language requirement"]
258
+ end
114
259
 
115
- ### Step 2: Install the package
260
+ subgraph discovery [Discovery Layer]
261
+ ReqGatherer["Requirement Gatherer"]
262
+ Planning["Planning Agent"]
263
+ end
116
264
 
117
- **Using the package in your project:**
265
+ subgraph bridge [Bridge]
266
+ FromReqs["from-requirements"]
267
+ end
118
268
 
119
- ```bash
120
- npm install sweagent
269
+ subgraph specialists [Specialist Architects]
270
+ DataModeler["Data Modeler"]
271
+ ApiDesigner["API Designer"]
272
+ AuthDesigner["Auth Designer"]
273
+ BackendArch["Backend Architect"]
274
+ FrontendArch["Frontend Architect"]
275
+ end
276
+
277
+ subgraph builders [Framework Builders]
278
+ ExpressBuilder["Express Builder"]
279
+ ApolloBuilder["Apollo Builder"]
280
+ ReactBuilder["React Builder"]
281
+ NextjsBuilder["Next.js Builder"]
282
+ end
283
+
284
+ subgraph execution [Execution]
285
+ ExecPlanner["Execution Planner"]
286
+ CodingAgent["Cursor / Claude Code / Codex"]
287
+ end
288
+
289
+ Idea --> ReqGatherer
290
+ Idea --> Planning
291
+ ReqGatherer -->|"FinalRequirement JSON"| FromReqs
292
+ FromReqs -->|"PlanningContext"| Planning
293
+ ReqGatherer --> DataModeler
294
+ DataModeler --> ApiDesigner
295
+ ApiDesigner --> AuthDesigner
296
+ AuthDesigner --> BackendArch
297
+ ApiDesigner --> FrontendArch
298
+ BackendArch -->|"Express selected"| ExpressBuilder
299
+ BackendArch -->|"Apollo selected"| ApolloBuilder
300
+ FrontendArch -->|"React selected"| ReactBuilder
301
+ FrontendArch -->|"Next.js selected"| NextjsBuilder
302
+ Planning --> ExecPlanner
303
+ ExecPlanner --> CodingAgent
304
+ builders --> CodingAgent
305
+ specialists --> CodingAgent
121
306
  ```
122
307
 
123
- Or with yarn, pnpm, or bun:
308
+ ### Three usage modes
124
309
 
125
- ```bash
126
- yarn add sweagent
127
- pnpm add sweagent
128
- bun add sweagent
310
+ **1. Quick plan** -- Planning Agent standalone. One call, one markdown plan. Best for getting a coding agent started fast.
311
+
312
+ **2. Structured requirements** -- Requirement Gatherer produces typed JSON (actors, flows, stories, modules, database, API). Feed the JSON to specialist modules (Data Modeler, API Designer, etc.) for detailed specs per layer. Use `runPlanningFromRequirements` to bridge requirement-gatherer output into the planning pipeline, skipping redundant discovery stages.
313
+
314
+ **3. Full pipeline** -- Chain all agents together. Each agent's output feeds the next: requirements -> data model -> API design -> auth -> backend architecture -> frontend architecture. Save all specs and hand the directory to your coding agent.
315
+
316
+ ---
317
+
318
+ ## Features
319
+
320
+ | Feature | Description |
321
+ | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
322
+ | **Domain Agent Modules** | Planning, Requirement Gatherer, Data Modeler, API Designer, Auth Designer, Backend Architect, Express Builder, Apollo Builder, Frontend Architect, React Builder, Next.js Builder, Execution Planner, and Hello World template -- each a self-contained pipeline. |
323
+ | **Multi-Stage Pipelines** | Every domain agent progresses through structured stages (discovery, requirements, design, synthesis) with dedicated LLM calls at each step. No single-shot prompts. |
324
+ | **Sub-Agent Orchestration** | Complex domains delegate to specialized sub-agents (`entity-analyzer`, `schema-refiner`, `graphql-analyzer`, `config-validator`) that run in isolation and return condensed results. |
325
+ | **Structured Outputs** | Requirements as typed JSON (actors, flows, stories, modules). DB schemas with field-level detail. Frontend configs with pages, hooks, and branding. Plans with 11 sections. |
326
+ | **Multi-Provider Models** | Unified API for OpenAI, Anthropic, and Google. One `createModel()` call, zero provider lock-in. |
327
+ | **Type-Safe Tools** | Define tools with Zod schemas; full type inference and validation before execution. Minimal, workflow-oriented tool sets. |
328
+ | **Agent Framework** | Iterative agent loop with tool calling, step callbacks, and configurable max iterations. |
329
+ | **MCP Protocol** | Connect to Model Context Protocol servers over HTTP or stdio. Lazy connection, typed tool invocation. |
330
+ | **Vision** | Image inputs via `model.generateVision()` for vision-capable models. |
331
+ | **Zero Extra Deps** | All provider SDKs (OpenAI, Anthropic, Google) included. Set API keys and run. |
332
+
333
+ ---
334
+
335
+ ## Planning Pipeline
336
+
337
+ The planning module is the centerpiece for AI coding agents. It turns a natural-language project description into a structured, implementation-ready markdown plan through four stages.
338
+
339
+ ### How it works
340
+
341
+ ```mermaid
342
+ flowchart LR
343
+ Input["User Requirement"] --> Discovery["Discovery"]
344
+ Discovery --> Requirements["Requirements"]
345
+ Requirements --> Design["Design"]
346
+ Design --> Synthesis["Synthesis"]
347
+ Synthesis --> Output["plan.md"]
129
348
  ```
130
349
 
131
- All AI provider SDKs (OpenAI, Anthropic, Google) are included; no extra packages are required.
350
+ ### Stages
132
351
 
133
- **Contributing from source:**
352
+ | Stage | What it produces | Sections |
353
+ | ---------------- | -------------------------------------------------- | ------------------------------------------------------------------- |
354
+ | **Discovery** | Understands the project, asks clarifying questions | Project overview |
355
+ | **Requirements** | 4 sequential LLM calls to flesh out the spec | Tech stack, feature decisions, data models, pages/routes, auth flow |
356
+ | **Design** | 2 sequential LLM calls for technical design | API routes, implementation details |
357
+ | **Synthesis** | Assembles the final plan | Implementation order, edge cases, testing checklist |
134
358
 
135
- ```bash
136
- git clone https://github.com/sijeeshmiziha/sweagent.git
137
- cd sweagent
138
- npm install
359
+ ### Output
360
+
361
+ The plan is a markdown document with these sections:
362
+
363
+ - **Overview** -- project scope and goals
364
+ - **Tech Stack** -- languages, frameworks, database, auth approach
365
+ - **Feature Decisions** -- what to build and what to defer
366
+ - **Data Models** -- schemas, relationships, fields
367
+ - **Pages and Routes** -- frontend structure
368
+ - **Authentication Flow** -- auth strategy and implementation
369
+ - **API Routes** -- endpoints, methods, request/response shapes
370
+ - **Implementation Details** -- architecture decisions, file structure
371
+ - **Execution Plan** -- phased implementation order
372
+ - **Edge Cases** -- error handling, boundary conditions
373
+ - **Testing Checklist** -- what to verify at each phase
374
+
375
+ ### Two modes
376
+
377
+ **One-shot mode** -- pass a requirement, get a plan:
378
+
379
+ ```typescript
380
+ import { runPlanningAgent } from 'sweagent';
381
+
382
+ const result = await runPlanningAgent({
383
+ input: 'Fitness app with workouts, nutrition tracking, and social features',
384
+ model: { provider: 'anthropic', model: 'claude-sonnet-4-20250514' },
385
+ });
386
+ console.log(result.output); // Full plan markdown
139
387
  ```
140
388
 
141
- ### Step 3: Environment setup
389
+ **Interactive chat mode** -- multi-turn conversation where you refine the plan:
142
390
 
143
- Create a `.env` file in your project root (or export variables in your shell):
391
+ ```typescript
392
+ import { processPlanningChat } from 'sweagent';
393
+ import type { PlanningContext } from 'sweagent';
144
394
 
145
- ```bash
146
- # At least one provider API key is required
147
- OPENAI_API_KEY=sk-...
148
- ANTHROPIC_API_KEY=sk-ant-...
149
- GOOGLE_GENERATIVE_AI_API_KEY=...
395
+ let context: PlanningContext | null = null;
396
+
397
+ // Turn 1: describe the project
398
+ const turn1 = await processPlanningChat('Build a task manager with teams', context, {
399
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
400
+ });
401
+ context = turn1.context;
402
+ console.log(turn1.message); // Assistant asks clarifying questions
403
+ console.log(turn1.pendingQuestions); // ["What auth provider?", ...]
404
+
405
+ // Turn 2: answer and advance
406
+ const turn2 = await processPlanningChat('Use NextAuth with GitHub OAuth', context, {
407
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
408
+ });
409
+ context = turn2.context;
410
+
411
+ // Continue until turn.planMarkdown is set (plan complete)
150
412
  ```
151
413
 
152
- ### Step 4: Verify installation
414
+ ### Requirements to plan (the bridge)
153
415
 
154
- Run the hello-world example to confirm everything works.
416
+ When you already have structured requirements from the Requirement Gatherer, `runPlanningFromRequirements` converts the `FinalRequirement` JSON into a `PlanningContext` pre-filled at the design stage, skipping the redundant discovery and requirements stages:
155
417
 
156
- **If you installed the package:** create a file `test-agent.mjs` (or `test-agent.ts` with tsx):
418
+ ```typescript
419
+ import { runRequirementGathererAgent, runPlanningFromRequirements } from 'sweagent';
157
420
 
158
- ```javascript
159
- import { createModel, runAgent, helloWorldTool } from 'sweagent';
160
- const model = createModel({ provider: 'openai', model: 'gpt-4o-mini' });
161
- const result = await runAgent({
421
+ const model = { provider: 'openai', model: 'gpt-4o-mini' } as const;
422
+
423
+ // Step 1: Gather structured requirements (actors, flows, stories, modules, DB)
424
+ const reqResult = await runRequirementGathererAgent({
425
+ input: 'Task manager with teams, Kanban boards, and time tracking',
162
426
  model,
163
- tools: [helloWorldTool],
164
- systemPrompt: 'You are helpful.',
165
- input: 'Say hello',
427
+ maxIterations: 15,
166
428
  });
167
- console.log(result.output);
429
+
430
+ // Step 2: Convert requirements into a planning context and generate the plan
431
+ // Skips discovery + requirements stages; begins at design
432
+ const planResult = await runPlanningFromRequirements({
433
+ requirement: JSON.parse(reqResult.output),
434
+ model,
435
+ });
436
+
437
+ console.log(planResult.output); // Full implementation plan as markdown
168
438
  ```
169
439
 
170
- Run it with `node --env-file=.env test-agent.mjs` (or `npx tsx --env-file=.env test-agent.ts`).
440
+ ---
441
+
442
+ ## Full Pipeline
171
443
 
172
- **If you cloned the repo:**
444
+ Chain multiple agents together to go from a vague idea to implementation-ready specs for every layer of your stack. Each agent's output feeds the next.
173
445
 
174
- ```bash
175
- npm run example -- examples/hello-world/01-hello-world.ts
446
+ ```typescript
447
+ import {
448
+ runRequirementGathererAgent,
449
+ runDataModelerAgent,
450
+ runApiDesignerAgent,
451
+ runAuthDesignerAgent,
452
+ runBackendArchitectAgent,
453
+ runFrontendArchitectAgent,
454
+ runExecutionPlannerAgent,
455
+ } from 'sweagent';
456
+ import { writeFileSync } from 'fs';
457
+
458
+ const model = { provider: 'openai', model: 'gpt-4o-mini' } as const;
459
+
460
+ // Step 1: Gather structured requirements
461
+ const requirements = await runRequirementGathererAgent({
462
+ input: 'Project management SaaS with teams, Kanban boards, time tracking, and billing',
463
+ model,
464
+ maxIterations: 15,
465
+ });
466
+
467
+ // Step 2: Design the data model from requirements
468
+ const dataModel = await runDataModelerAgent({
469
+ input: `Design a data model based on these requirements:\n${requirements.output}`,
470
+ model,
471
+ maxIterations: 15,
472
+ });
473
+
474
+ // Step 3: Design the API from the data model
475
+ const apiDesign = await runApiDesignerAgent({
476
+ input: `Design REST API for this data model:\n${dataModel.output}`,
477
+ model,
478
+ maxIterations: 15,
479
+ });
480
+
481
+ // Step 4: Design auth from the requirements and API
482
+ const authDesign = await runAuthDesignerAgent({
483
+ input: `Design auth for this project:\nRequirements: ${requirements.output}\nAPI: ${apiDesign.output}`,
484
+ model,
485
+ maxIterations: 15,
486
+ });
487
+
488
+ // Step 5: Plan backend architecture
489
+ const backendDesign = await runBackendArchitectAgent({
490
+ input: `Design backend:\nData model: ${dataModel.output}\nAPI: ${apiDesign.output}\nAuth: ${authDesign.output}`,
491
+ model,
492
+ maxIterations: 15,
493
+ });
494
+
495
+ // Step 6: Plan frontend architecture
496
+ const frontendDesign = await runFrontendArchitectAgent({
497
+ input: `Design frontend:\nAPI: ${apiDesign.output}\nRequirements: ${requirements.output}`,
498
+ model,
499
+ maxIterations: 15,
500
+ });
501
+
502
+ // Save all specs for your coding agent
503
+ writeFileSync('specs/requirements.json', requirements.output);
504
+ writeFileSync('specs/data-model.json', dataModel.output);
505
+ writeFileSync('specs/api-design.json', apiDesign.output);
506
+ writeFileSync('specs/auth-design.json', authDesign.output);
507
+ writeFileSync('specs/backend-design.json', backendDesign.output);
508
+ writeFileSync('specs/frontend-design.json', frontendDesign.output);
509
+
510
+ // Now hand the specs/ directory to Cursor, Claude Code, or Codex
511
+ // "Implement the backend using specs/backend-design.json and specs/data-model.json"
176
512
  ```
177
513
 
514
+ You can also run individual agents standalone -- each works independently with natural-language input. The pipeline approach gives you maximum control over each design decision.
515
+
178
516
  ---
179
517
 
180
- ## Getting Started Tutorial
518
+ ## Domain Agent Modules
519
+
520
+ Each module is a self-contained domain agent with its own orchestrator, pipeline stages, tools, sub-agents, and output format. All are exported from the main package.
521
+
522
+ | Stage | Agent | Sub-Agents | Output |
523
+ | ------------------- | ----------------------------- | ---------------------------------------------------------- | ------------------------------------------------- |
524
+ | **Planning** | `runPlanningAgent` | -- | Implementation-ready markdown plan (11 sections) |
525
+ | **Requirements** | `runRequirementGathererAgent` | -- | Structured JSON (actors, flows, stories, modules) |
526
+ | **Data Modeling** | `runDataModelerAgent` | `entity-analyzer`, `relationship-mapper`, `schema-refiner` | MongoDB/PostgreSQL schemas |
527
+ | **API Design** | `runApiDesignerAgent` | `endpoint-analyzer`, `contract-designer` | REST and/or GraphQL API design |
528
+ | **Auth Design** | `runAuthDesignerAgent` | `security-analyzer`, `flow-designer` | Auth strategy, flows, middleware, RBAC |
529
+ | **Backend Arch.** | `runBackendArchitectAgent` | `framework-selector`, `service-planner` | Backend architecture (Express/Apollo) |
530
+ | **Express Builder** | `runExpressBuilderAgent` | `route-generator`, `middleware-configurator` | Express.js REST API config |
531
+ | **Apollo Builder** | `runApolloBuilderAgent` | `schema-generator`, `resolver-planner` | Apollo GraphQL subgraph config |
532
+ | **Frontend Arch.** | `runFrontendArchitectAgent` | `page-planner`, `component-analyzer`, `framework-selector` | Frontend architecture (React/Next.js) |
533
+ | **React Builder** | `runReactBuilderAgent` | `graphql-analyzer`, `config-validator` | React + Vite app config from GraphQL |
534
+ | **Next.js Builder** | `runNextjsBuilderAgent` | `route-planner`, `api-route-generator` | Next.js App Router config |
535
+ | **Execution Plan** | `runExecutionPlannerAgent` | `edge-case-analyzer`, `testing-strategist` | Phased implementation plan |
536
+ | **Hello World** | `runAgent` + `helloWorldTool` | -- | Template module for custom agents |
181
537
 
182
- Progress from a simple model call to a full agent with tools and subagents.
538
+ ---
183
539
 
184
- ### Level 1: Model invocation
540
+ ### Planning Agent
541
+
542
+ Turns a natural-language project description into an implementation-ready markdown plan through 4 stages and 12+ LLM calls. Covers tech stack, data models, API routes, implementation order, edge cases, and testing checklists.
543
+
544
+ | Attribute | Detail |
545
+ | ----------------- | ----------------------------------------------------------------------- |
546
+ | **Stages** | Discovery, Requirements (4 LLM calls), Design (2 LLM calls), Synthesis |
547
+ | **Sub-Agents** | -- |
548
+ | **Tools** | -- (pipeline stages, not tool-based) |
549
+ | **Output Format** | Markdown plan (11 sections) |
550
+ | **Modes** | One-shot (`runPlanningAgent`), interactive chat (`processPlanningChat`) |
551
+
552
+ ```typescript
553
+ import { runPlanningAgent } from 'sweagent';
554
+
555
+ const result = await runPlanningAgent({
556
+ input: 'E-commerce: users, orders, products. Admins manage products.',
557
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
558
+ });
559
+ console.log(result.output); // Full markdown blueprint
560
+ ```
185
561
 
186
- Create a model and get a completion:
562
+ See [Planning Pipeline](#planning-pipeline) for stage-by-stage details.
563
+
564
+ ---
565
+
566
+ ### Requirement Gatherer Agent
567
+
568
+ Produces structured JSON requirements -- not prose. Unlike the Planning module (markdown output), the Requirement Gatherer extracts typed data that downstream systems can consume programmatically.
569
+
570
+ | Attribute | Detail |
571
+ | ----------------- | ------------------------------------------------------------------------------------- |
572
+ | **Stages** | Discovery, Requirements, Design, Synthesis |
573
+ | **Sub-Agents** | -- |
574
+ | **Tools** | Stage-specific tools |
575
+ | **Output Format** | Structured JSON |
576
+ | **Schemas** | Actors, User Flows, User Stories, Modules, Database Design, API Design |
577
+ | **Modes** | One-shot (`runRequirementGathererAgent`), interactive chat (`processRequirementChat`) |
578
+
579
+ **Output structure:** Actors (with permissions), User Flows (step-by-step sequences), User Stories (with acceptance criteria), Modules (with CRUD operations), Database Design (schemas, relationships), API Design (REST/GraphQL endpoints).
580
+
581
+ ```typescript
582
+ import { runRequirementGathererAgent } from 'sweagent';
583
+
584
+ const result = await runRequirementGathererAgent({
585
+ input: 'Project management tool with teams and Kanban boards',
586
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
587
+ maxIterations: 15,
588
+ });
589
+ // result.output: structured JSON with actors, flows, stories, modules
590
+ ```
591
+
592
+ ---
593
+
594
+ ### Data Modeler Agent
595
+
596
+ Designs data models for MongoDB or PostgreSQL with entities, fields, indexes, and relationships. Uses three sub-agents for entity analysis, relationship mapping, and schema refinement.
597
+
598
+ | Attribute | Detail |
599
+ | ----------------- | ---------------------------------------------------------------------------- |
600
+ | **Pattern** | Orchestrator with sub-agents |
601
+ | **Sub-Agents** | `entity-analyzer`, `relationship-mapper`, `schema-refiner` |
602
+ | **Tools** | `design_schema`, `design_schema_pro`, `refine_schema`, `validate_data_model` |
603
+ | **Output Format** | Data model JSON (entities, fields, indexes, relationships) |
604
+ | **Databases** | MongoDB, PostgreSQL |
605
+
606
+ ```typescript
607
+ import { runDataModelerAgent } from 'sweagent';
608
+
609
+ const result = await runDataModelerAgent({
610
+ input: 'SaaS platform with organizations, users, projects, and billing',
611
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
612
+ maxIterations: 15,
613
+ });
614
+ // result.output: DataModelDesign JSON with entities, fields, indexes, relationships
615
+ ```
616
+
617
+ ---
618
+
619
+ ### API Designer Agent
620
+
621
+ Designs REST and/or GraphQL APIs from data models, producing endpoint definitions with request/response contracts, auth requirements, and operation details.
622
+
623
+ | Attribute | Detail |
624
+ | ----------------- | ------------------------------------------------------------------------------------------------------- |
625
+ | **Pattern** | Orchestrator with sub-agents |
626
+ | **Sub-Agents** | `endpoint-analyzer` (derives endpoints from data model), `contract-designer` (designs request/response) |
627
+ | **Tools** | `design_api`, `design_api_pro`, `validate_api` |
628
+ | **Output Format** | API design JSON (REST endpoints and/or GraphQL operations) |
629
+
630
+ ```typescript
631
+ import { runApiDesignerAgent } from 'sweagent';
632
+
633
+ const result = await runApiDesignerAgent({
634
+ input: 'Design REST API for a task manager with users, projects, and tasks',
635
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
636
+ maxIterations: 15,
637
+ });
638
+ // result.output: ApiDesign JSON with REST endpoints and/or GraphQL operations
639
+ ```
640
+
641
+ ---
642
+
643
+ ### Auth Designer Agent
644
+
645
+ Designs authentication and authorization systems with strategies, flows, middleware, roles, and security policies.
646
+
647
+ | Attribute | Detail |
648
+ | ----------------- | ------------------------------------------------------------------------------------------ |
649
+ | **Pattern** | Orchestrator with sub-agents |
650
+ | **Sub-Agents** | `security-analyzer` (analyzes security requirements), `flow-designer` (designs auth flows) |
651
+ | **Tools** | `design_auth`, `validate_auth` |
652
+ | **Output Format** | Auth design JSON (strategy, flows, middleware, roles, policies) |
653
+
654
+ ```typescript
655
+ import { runAuthDesignerAgent } from 'sweagent';
656
+
657
+ const result = await runAuthDesignerAgent({
658
+ input: 'JWT auth with email/password, Google OAuth, role-based access (admin, member)',
659
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
660
+ maxIterations: 15,
661
+ });
662
+ // result.output: AuthDesign JSON with strategy, flows, middleware, roles, policies
663
+ ```
664
+
665
+ ---
666
+
667
+ ### Backend Architect Agent
668
+
669
+ Plans backend architecture including framework selection, services, middleware, routes, and folder structure. Routes to Express Builder or Apollo Builder based on framework choice.
670
+
671
+ | Attribute | Detail |
672
+ | ----------------- | ------------------------------------------------------------------------------- |
673
+ | **Pattern** | Orchestrator with sub-agents |
674
+ | **Sub-Agents** | `framework-selector`, `service-planner` |
675
+ | **Tools** | `design_backend`, `validate_backend` |
676
+ | **Output Format** | Backend design JSON (framework, services, middleware, routes, folder structure) |
677
+ | **Frameworks** | Express, Apollo, or both |
678
+
679
+ ```typescript
680
+ import { runBackendArchitectAgent } from 'sweagent';
681
+
682
+ const result = await runBackendArchitectAgent({
683
+ input: 'REST API backend with user auth, CRUD operations, and file uploads',
684
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
685
+ maxIterations: 15,
686
+ });
687
+ // result.output: BackendDesign JSON with framework, services, middleware, routes
688
+ ```
689
+
690
+ ---
691
+
692
+ ### Express Builder Agent
693
+
694
+ Generates Express.js REST API configuration with routers, models, middleware, and environment variables.
695
+
696
+ | Attribute | Detail |
697
+ | ----------------- | ----------------------------------------------------------- |
698
+ | **Pattern** | Orchestrator with sub-agents |
699
+ | **Sub-Agents** | `route-generator`, `middleware-configurator` |
700
+ | **Tools** | `generate_express`, `scaffold_express`, `validate_express` |
701
+ | **Output Format** | Express config JSON (routers, models, middleware, env vars) |
702
+
703
+ ```typescript
704
+ import { runExpressBuilderAgent } from 'sweagent';
705
+
706
+ const result = await runExpressBuilderAgent({
707
+ input: 'Express API for e-commerce with products, orders, and user auth',
708
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
709
+ maxIterations: 15,
710
+ });
711
+ // result.output: ExpressConfig JSON with routers, models, middleware, env vars
712
+ ```
713
+
714
+ ---
715
+
716
+ ### Apollo Builder Agent
717
+
718
+ Generates Apollo GraphQL subgraph configuration with modules, types, resolvers, datasources, and Federation v2 support.
719
+
720
+ | Attribute | Detail |
721
+ | ----------------- | --------------------------------------------------------------------- |
722
+ | **Pattern** | Orchestrator with sub-agents |
723
+ | **Sub-Agents** | `schema-generator`, `resolver-planner` |
724
+ | **Tools** | `generate_subgraph`, `scaffold_subgraph`, `validate_subgraph` |
725
+ | **Output Format** | Apollo subgraph config JSON (modules, types, operations, datasources) |
726
+
727
+ ```typescript
728
+ import { runApolloBuilderAgent } from 'sweagent';
729
+
730
+ const result = await runApolloBuilderAgent({
731
+ input: 'Apollo subgraph for a task manager with users, projects, and tasks',
732
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
733
+ maxIterations: 15,
734
+ });
735
+ // result.output: SubgraphConfig JSON with modules, types, operations, datasources
736
+ ```
737
+
738
+ ---
739
+
740
+ ### Frontend Architect Agent
741
+
742
+ Plans frontend architecture including pages, components, routing, and state management. Routes to React Builder or Next.js Builder based on framework selection.
743
+
744
+ | Attribute | Detail |
745
+ | ----------------- | ------------------------------------------------------------------- |
746
+ | **Pattern** | Orchestrator with sub-agents |
747
+ | **Sub-Agents** | `page-planner`, `component-analyzer`, `framework-selector` |
748
+ | **Output Format** | Frontend design JSON (pages, components, state management, routing) |
749
+ | **Frameworks** | React + Vite, Next.js |
750
+
751
+ ```typescript
752
+ import { runFrontendArchitectAgent } from 'sweagent';
753
+
754
+ const result = await runFrontendArchitectAgent({
755
+ input: 'Dashboard app with analytics, settings, and user management pages',
756
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
757
+ maxIterations: 15,
758
+ });
759
+ // result.output: FrontendDesign JSON with pages, components, state management, routing
760
+ ```
761
+
762
+ ---
763
+
764
+ ### React Builder Agent
765
+
766
+ Generates complete frontend application configuration from a GraphQL schema. A `graphql-analyzer` sub-agent parses the schema structure, and a `config-validator` sub-agent verifies the output. Produces app config, modules, pages, fields, and API hooks.
767
+
768
+ | Attribute | Detail |
769
+ | ----------------- | ----------------------------------------------------------------------------------------------------------- |
770
+ | **Pattern** | Orchestrator with sub-agents |
771
+ | **Sub-Agents** | `graphql-analyzer` (parses GraphQL schema structure), `config-validator` (validates frontend config output) |
772
+ | **Tools** | `generate_frontend`, `generate_feature_breakdown`, `validate_frontend_config` |
773
+ | **Output Format** | React app config JSON (app, modules, pages, fields, API hooks, branding) |
774
+ | **Schemas** | App config, User config, Page config, Field config, Branding |
775
+
776
+ ```typescript
777
+ import { runReactBuilderAgent } from 'sweagent';
778
+
779
+ const result = await runReactBuilderAgent({
780
+ input: 'GraphQL schema: type User { id: ID! name: String! } type Task { ... }',
781
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
782
+ maxIterations: 15,
783
+ });
784
+ // result.output: frontend config JSON with pages, fields, hooks, branding
785
+ ```
786
+
787
+ ---
788
+
789
+ ### Next.js Builder Agent
790
+
791
+ Generates Next.js App Router configuration with pages, layouts, API routes, server actions, and middleware.
792
+
793
+ | Attribute | Detail |
794
+ | ----------------- | ---------------------------------------------------------------------------- |
795
+ | **Pattern** | Orchestrator with sub-agents |
796
+ | **Sub-Agents** | `route-planner`, `api-route-generator` |
797
+ | **Tools** | `generate_nextjs`, `validate_nextjs` |
798
+ | **Output Format** | Next.js config JSON (pages, layouts, API routes, server actions, middleware) |
799
+
800
+ ```typescript
801
+ import { runNextjsBuilderAgent } from 'sweagent';
802
+
803
+ const result = await runNextjsBuilderAgent({
804
+ input: 'Next.js app for project management with teams, tasks, and dashboards',
805
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
806
+ maxIterations: 15,
807
+ });
808
+ // result.output: NextjsConfig JSON with pages, layouts, API routes, server actions
809
+ ```
810
+
811
+ ---
812
+
813
+ ### Execution Planner Agent
814
+
815
+ Creates phased implementation execution plans from plan sections, with edge case analysis and testing checklists.
816
+
817
+ | Attribute | Detail |
818
+ | ----------------- | --------------------------------------------------------------------------------------- |
819
+ | **Pattern** | Orchestrator with sub-agents |
820
+ | **Sub-Agents** | `edge-case-analyzer`, `testing-strategist` |
821
+ | **Tools** | `create_execution_plan`, `validate_execution_plan` |
822
+ | **Output Format** | Execution plan JSON (phases, edge cases, testing checklist, security/performance notes) |
823
+
824
+ ```typescript
825
+ import { runExecutionPlannerAgent } from 'sweagent';
826
+
827
+ const result = await runExecutionPlannerAgent({
828
+ input: 'Create execution plan for the task manager project',
829
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
830
+ maxIterations: 15,
831
+ });
832
+ // result.output: ExecutionPlan JSON with phases, edge cases, testing checklist
833
+ ```
834
+
835
+ ---
836
+
837
+ ### Hello World (Template)
838
+
839
+ Minimal example module with a single greeting tool. Use as a starting point when building your own domain agent module.
840
+
841
+ ```typescript
842
+ import { createModel, runAgent, helloWorldTool } from 'sweagent';
843
+
844
+ const result = await runAgent({
845
+ model: createModel({ provider: 'openai', model: 'gpt-4o-mini' }),
846
+ tools: [helloWorldTool],
847
+ systemPrompt: 'You are helpful.',
848
+ input: 'Say hello',
849
+ });
850
+ ```
851
+
852
+ ---
853
+
854
+ ## Getting Started
855
+
856
+ ### Level 1: Model invocation
187
857
 
188
858
  ```typescript
189
859
  import { createModel } from 'sweagent';
190
860
 
191
861
  const model = createModel({
192
- provider: 'openai',
862
+ provider: 'openai', // 'openai' | 'anthropic' | 'google'
193
863
  model: 'gpt-4o-mini',
194
864
  temperature: 0.7,
195
865
  });
@@ -200,9 +870,7 @@ const response = await model.invoke([
200
870
  console.log(response.text);
201
871
  ```
202
872
 
203
- ### Level 2: Custom tools
204
-
205
- Define a type-safe tool with Zod:
873
+ ### Level 2: Define tools
206
874
 
207
875
  ```typescript
208
876
  import { defineTool } from 'sweagent';
@@ -223,8 +891,6 @@ const calculatorTool = defineTool({
223
891
 
224
892
  ### Level 3: Agent loop
225
893
 
226
- Run an agent that can call your tools:
227
-
228
894
  ```typescript
229
895
  import { runAgent, createModel, defineTool, createToolSet } from 'sweagent';
230
896
  import { z } from 'zod';
@@ -249,10 +915,16 @@ console.log(result.output);
249
915
 
250
916
  ### Level 4: Subagents
251
917
 
252
- Define subagents and expose them as tools to a parent agent:
253
-
254
918
  ```typescript
255
- import { defineSubagent, createSubagentToolSet, runAgent, createModel, createToolSet } from 'sweagent';
919
+ import {
920
+ defineSubagent,
921
+ createSubagentToolSet,
922
+ runAgent,
923
+ createModel,
924
+ createToolSet,
925
+ } from 'sweagent';
926
+
927
+ const model = createModel({ provider: 'openai', model: 'gpt-4o-mini' });
256
928
 
257
929
  const researcher = defineSubagent({
258
930
  name: 'researcher',
@@ -260,7 +932,7 @@ const researcher = defineSubagent({
260
932
  systemPrompt: 'You are a researcher. Answer concisely.',
261
933
  });
262
934
  const subagentTools = createSubagentToolSet([researcher], { parentModel: model });
263
- const tools = createToolSet({ ...otherTools, ...subagentTools });
935
+ const tools = createToolSet({ ...subagentTools });
264
936
 
265
937
  const result = await runAgent({
266
938
  model,
@@ -271,25 +943,23 @@ const result = await runAgent({
271
943
  });
272
944
  ```
273
945
 
274
- ### Level 5: Production module (DB Designer)
946
+ ### Level 5: Planning pipeline
275
947
 
276
- Use the DB Designer orchestrator to generate a MongoDB schema from natural language:
948
+ Generate an implementation plan for a coding agent:
277
949
 
278
950
  ```typescript
279
- import { runDbDesignerAgent } from 'sweagent';
951
+ import { runPlanningAgent } from 'sweagent';
280
952
 
281
- const result = await runDbDesignerAgent({
282
- input: 'E-commerce: users, orders, products. Admins manage products. Users place orders and have a profile.',
953
+ const result = await runPlanningAgent({
954
+ input: 'E-commerce site: users, products, cart, checkout, admin dashboard',
283
955
  model: { provider: 'openai', model: 'gpt-4o-mini' },
284
- maxIterations: 15,
285
956
  });
286
- console.log(result.output);
957
+
958
+ console.log(result.output); // Full markdown blueprint
287
959
  ```
288
960
 
289
961
  ### Level 6: MCP integration
290
962
 
291
- Extend `BaseMcpClient` to call MCP tools (e.g. HTTP or stdio server):
292
-
293
963
  ```typescript
294
964
  import { BaseMcpClient } from 'sweagent';
295
965
 
@@ -303,6 +973,64 @@ const result = await client.callTool('tool_name', { arg: 'value' });
303
973
 
304
974
  ---
305
975
 
976
+ ## Installation
977
+
978
+ ### Prerequisites
979
+
980
+ - **Node.js** >= 18.0.0
981
+ - **npm** >= 8.0.0 (or yarn, pnpm, bun)
982
+
983
+ ### Install
984
+
985
+ ```bash
986
+ npm install sweagent
987
+ ```
988
+
989
+ Or with yarn, pnpm, or bun:
990
+
991
+ ```bash
992
+ yarn add sweagent
993
+ pnpm add sweagent
994
+ bun add sweagent
995
+ ```
996
+
997
+ All AI provider SDKs (OpenAI, Anthropic, Google) are included; no extra packages needed.
998
+
999
+ ### From source
1000
+
1001
+ ```bash
1002
+ git clone https://github.com/sijeeshmiziha/sweagent.git
1003
+ cd sweagent
1004
+ npm install
1005
+ ```
1006
+
1007
+ ### Environment setup
1008
+
1009
+ Create a `.env` file in your project root:
1010
+
1011
+ ```bash
1012
+ # At least one provider API key is required
1013
+ OPENAI_API_KEY=sk-...
1014
+ ANTHROPIC_API_KEY=sk-ant-...
1015
+ GOOGLE_GENERATIVE_AI_API_KEY=...
1016
+ ```
1017
+
1018
+ ### Verify
1019
+
1020
+ ```bash
1021
+ # If installed as a package
1022
+ echo 'import { createModel, runAgent, helloWorldTool } from "sweagent";
1023
+ const model = createModel({ provider: "openai", model: "gpt-4o-mini" });
1024
+ const result = await runAgent({ model, tools: [helloWorldTool], systemPrompt: "You are helpful.", input: "Say hello" });
1025
+ console.log(result.output);' > test.mjs
1026
+ node --env-file=.env test.mjs
1027
+
1028
+ # If cloned from source
1029
+ npm run example -- examples/hello-world/01-hello-world.ts
1030
+ ```
1031
+
1032
+ ---
1033
+
306
1034
  ## Architecture
307
1035
 
308
1036
  ### System overview
@@ -310,37 +1038,86 @@ const result = await client.callTool('tool_name', { arg: 'value' });
310
1038
  ```mermaid
311
1039
  graph TB
312
1040
  subgraph Client[Client Application]
313
- App[Your App]
1041
+ App["Your App / Cursor / Claude Code / Codex"]
314
1042
  end
315
1043
 
316
- subgraph Sweagent[sweagent]
317
- Models[Models]
318
- Tools[Tools]
319
- Agents[Agent Loop]
320
- Subagents[Subagents]
321
- Modules[DB Designer, React Builder]
1044
+ subgraph DomainAgents[Domain Agent Modules]
1045
+ Planning["Planning"]
1046
+ ReqGatherer["Requirement Gatherer"]
1047
+ DataModeler["Data Modeler"]
1048
+ ApiDesigner["API Designer"]
1049
+ AuthDesigner["Auth Designer"]
1050
+ BackendArch["Backend Architect"]
1051
+ ExpressBuilder["Express Builder"]
1052
+ ApolloBuilder["Apollo Builder"]
1053
+ FrontendArch["Frontend Architect"]
1054
+ ReactBuilder["React Builder"]
1055
+ NextjsBuilder["Next.js Builder"]
1056
+ ExecPlanner["Execution Planner"]
1057
+ end
1058
+
1059
+ subgraph Framework[Shared Framework]
1060
+ Models["Model Abstraction"]
1061
+ ToolFW["Tool Framework"]
1062
+ AgentLoop["Agent Loop"]
1063
+ SubAgentOrch["Sub-Agent Orchestration"]
1064
+ MCP["MCP Protocol"]
322
1065
  end
323
1066
 
324
1067
  subgraph Providers[AI Providers]
325
- OpenAI[OpenAI]
326
- Anthropic[Anthropic]
327
- Google[Google]
1068
+ OpenAI["OpenAI"]
1069
+ Anthropic["Anthropic"]
1070
+ Google["Google"]
328
1071
  end
329
1072
 
330
- App --> Models
331
- App --> Tools
332
- App --> Agents
333
- App --> Modules
334
- Agents --> Models
335
- Agents --> Tools
336
- Agents --> Subagents
337
- Modules --> Agents
338
- Models --> OpenAI
339
- Models --> Anthropic
340
- Models --> Google
1073
+ App --> DomainAgents
1074
+ DomainAgents --> Framework
1075
+ Framework --> Providers
341
1076
  ```
342
1077
 
343
- ### Agent execution flow
1078
+ ### Domain agent pipeline flow
1079
+
1080
+ Each domain agent follows a structured pipeline. The Planning Agent is representative:
1081
+
1082
+ ```mermaid
1083
+ flowchart LR
1084
+ Input["User Requirement"] --> Discovery["Discovery Stage"]
1085
+ Discovery --> Requirements["Requirements Stage"]
1086
+ Requirements --> Design["Design Stage"]
1087
+ Design --> Synthesis["Synthesis Stage"]
1088
+ Synthesis --> Plan["Structured Output"]
1089
+ Plan --> Validate["LLM Validator"]
1090
+ Validate --> Output["Validated Result"]
1091
+ ```
1092
+
1093
+ ### Orchestrator with sub-agents
1094
+
1095
+ Domain agents like Data Modeler and React Builder delegate to specialized sub-agents:
1096
+
1097
+ ```mermaid
1098
+ sequenceDiagram
1099
+ participant User
1100
+ participant Orchestrator
1101
+ participant Model
1102
+ participant Tools
1103
+ participant SubAgent1 as entity-analyzer
1104
+ participant SubAgent2 as schema-refiner
1105
+
1106
+ User->>Orchestrator: Natural-language requirement
1107
+ Orchestrator->>Model: Messages + Domain Tools
1108
+ Model-->>Orchestrator: Tool call design_database
1109
+ Orchestrator->>Tools: Execute design_database
1110
+ Tools-->>Orchestrator: Initial schema
1111
+ Orchestrator->>SubAgent1: Analyze entities and relationships
1112
+ SubAgent1-->>Orchestrator: Structured entity analysis
1113
+ Orchestrator->>SubAgent2: Refine and validate schema
1114
+ SubAgent2-->>Orchestrator: Validated schema
1115
+ Orchestrator->>Model: Compile final output
1116
+ Model-->>Orchestrator: Final result
1117
+ Orchestrator-->>User: Production-grade schema
1118
+ ```
1119
+
1120
+ ### Agent execution loop
344
1121
 
345
1122
  ```mermaid
346
1123
  sequenceDiagram
@@ -363,80 +1140,44 @@ sequenceDiagram
363
1140
  end
364
1141
  ```
365
1142
 
366
- ### Subagent delegation
367
-
368
- ```mermaid
369
- sequenceDiagram
370
- participant User
371
- participant ParentAgent
372
- participant ParentModel
373
- participant SubagentTool
374
- participant ChildAgent
375
- participant ChildModel
376
-
377
- User->>ParentAgent: Input
378
- ParentAgent->>ParentModel: Messages + Tools (incl. subagent_*)
379
- ParentModel-->>ParentAgent: Tool call subagent_researcher
380
- ParentAgent->>SubagentTool: Execute prompt
381
- SubagentTool->>ChildAgent: runAgent(systemPrompt, prompt)
382
- ChildAgent->>ChildModel: Messages
383
- ChildModel-->>ChildAgent: Response
384
- ChildAgent-->>SubagentTool: output
385
- SubagentTool-->>ParentAgent: Tool result
386
- ParentAgent->>ParentModel: Append tool result, continue
387
- ```
388
-
389
- ### Module orchestrator (DB Designer)
390
-
391
- ```mermaid
392
- flowchart LR
393
- Input[User Requirement] --> Analyze[entity-analyzer subagent]
394
- Analyze --> Design[design_database tool]
395
- Design --> Refine[schema-refiner subagent]
396
- Refine --> Validate[validate_schema tool]
397
- Validate --> Output[Schema JSON]
398
- ```
399
-
400
- ---
401
-
402
- ## Engineering Deep Dive
1143
+ ### Engineering Deep Dive
403
1144
 
404
- ### Problem decomposition: long-running agents
1145
+ #### The problem: long-running coding agents
405
1146
 
406
- Agents that work across many steps or sessions tend to fail in two ways: they either try to do too much in one shot (and leave partial, undocumented work), or they assume the project is done too early. To make progress across sessions, each run needs a way to **get up to speed** quickly and to **leave a clean state** for the next run. That implies structured artifacts: feature lists, progress files, and clear prompts that say “do one thing, then commit and document.”
1147
+ Coding agents that work across many steps or sessions fail in two ways: they try to do too much in one shot (leaving partial, undocumented work), or they declare the job done too early. Each new session starts with no memory of the last. To make progress across sessions, each run needs a way to get up to speed quickly and leave a clean state for the next run.
407
1148
 
408
- ### Incremental progress pattern
1149
+ #### Incremental progress pattern
409
1150
 
410
- We structure agents so that each session does **bounded work**: one feature or one clear subtask. The agent is prompted to update a progress file and to commit (or at least document) what it did. The next session reads progress and git history, chooses the next unfinished item, and continues. That avoids one-shotting the whole project and reduces the chance of the agent declaring victory prematurely.
1151
+ Each session does bounded work: one feature or one clear subtask. The agent updates a progress file and commits what it did. The next session reads progress and git history, chooses the next unfinished item, and continues. This avoids one-shotting the whole project and reduces premature completion.
411
1152
 
412
- ### Feature list approach
1153
+ #### Feature list approach
413
1154
 
414
- A structured list of requirements (e.g. in JSON) with a status per item gives the agent a clear definition of done.” Agents are instructed to only mark items passing after verification. That keeps scope explicit and makes it easier to resume across context windows.
1155
+ A structured list of requirements (e.g. in JSON) with a status per item gives the agent a clear definition of "done." Agents only mark items passing after verification, keeping scope explicit and making it easy to resume across context windows.
415
1156
 
416
- ### Clean state principle
1157
+ #### Clean state principle
417
1158
 
418
- Every session should end with code that is buildable, documented, and easy to continue from. In practice that means: no half-implemented features left broken, no stray debug code, and clear commit messages or progress notes. The framework doesn’t enforce this by itself; the orchestration prompts (e.g. in DB Designer and React Builder) encode these expectations.
1159
+ Every session should end with code that is buildable, documented, and easy to continue from. No half-implemented features, no stray debug code, clear commit messages or progress notes. The orchestration prompts in production modules encode these expectations.
419
1160
 
420
- ### Error hierarchy design
1161
+ ### Provider adapter pattern
421
1162
 
422
- Errors are organized so you can handle them by type and preserve cause chains:
1163
+ Models are created via `createModel({ provider, model, ... })`. A shared AI SDK adapter wraps the Vercel AI SDK's `generateText` and normalizes messages, tool schemas, and responses. Each provider has a thin factory that passes the correct `LanguageModel` into this adapter. Provider-specific logic stays in one place; everything else is provider-agnostic.
423
1164
 
424
- - **LibraryError** base for all library errors
425
- - **ModelError** – model invocation failures (provider, API key, etc.)
426
- - **ToolError** – tool execution or “tool not found”
427
- - **ValidationError** – schema validation failures
428
- - **AgentError** – agent hit max iterations without finishing
429
- - **SubagentError** – subagent configuration or execution failure
1165
+ ### Tool execution safety
430
1166
 
431
- Each can wrap a `cause`. Use try/catch and `instanceof` to branch on the right level.
1167
+ Inputs are validated with Zod before any tool runs. Invalid input produces a **ToolError** with the parse error; the handler is never called with bad data. Handler errors are caught and rethrown as **ToolError** with the original error as cause. The agent loop receives structured tool results (including error payloads) so the model can see failures and retry or adjust.
432
1168
 
433
- ### Provider adapter pattern
1169
+ ### Error hierarchy
434
1170
 
435
- Models are created via `createModel({ provider, model, ... })`. Under the hood, a shared **AI SDK adapter** (`createAIModel`) wraps the Vercel AI SDK’s `generateText` and normalizes messages, tool schemas, and responses. Each provider (OpenAI, Anthropic, Google) has a thin factory that passes the correct `LanguageModel` into this adapter. That keeps provider-specific logic in one place and keeps the rest of the stack provider-agnostic.
1171
+ | Class | When |
1172
+ | ------------------- | ------------------------------------------------ |
1173
+ | **LibraryError** | Base; all others extend it. |
1174
+ | **ModelError** | Model creation or invoke failed. |
1175
+ | **ToolError** | Tool not found or tool execution failed. |
1176
+ | **ValidationError** | Zod validation failed. |
1177
+ | **AgentError** | Agent reached max iterations without completing. |
1178
+ | **SubagentError** | Subagent config or run failed. |
436
1179
 
437
- ### Tool execution safety
438
-
439
- Before any tool runs, inputs are validated with Zod. Invalid input produces a **ToolError** with the parse error; the handler is never called with bad data. Handler errors are caught and rethrown as **ToolError** with the original error as cause. The agent loop receives structured tool results (including error payloads) so the model can see failures and retry or adjust.
1180
+ All accept an optional `cause` for chaining.
440
1181
 
441
1182
  ---
442
1183
 
@@ -446,16 +1187,16 @@ All public APIs are exported from the main package: `import { ... } from 'sweage
446
1187
 
447
1188
  ### Models
448
1189
 
449
- **createModel(config)** Create a model instance for the given provider.
1190
+ **createModel(config)** -- Create a model instance.
450
1191
 
451
1192
  ```typescript
452
1193
  import { createModel } from 'sweagent';
453
1194
 
454
1195
  const model = createModel({
455
1196
  provider: 'openai' | 'anthropic' | 'google',
456
- model: string, // e.g. 'gpt-4o', 'gpt-4o-mini', 'claude-3-5-sonnet-20241022'
457
- apiKey?: string, // Uses env var by default (OPENAI_API_KEY, etc.)
458
- temperature?: number, // 0–2, default varies by provider
1197
+ model: string, // e.g. 'gpt-4o', 'claude-sonnet-4-20250514'
1198
+ apiKey?: string, // Uses env var by default
1199
+ temperature?: number,
459
1200
  maxOutputTokens?: number,
460
1201
  baseUrl?: string,
461
1202
  });
@@ -466,11 +1207,11 @@ const response = await model.invoke(messages, { tools });
466
1207
 
467
1208
  **Supported models (examples):**
468
1209
 
469
- | Provider | Models |
470
- | --------- | ------ |
471
- | OpenAI | `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo` |
472
- | Anthropic | `claude-3-5-sonnet-20241022`, `claude-3-opus-20240229` |
473
- | Google | `gemini-1.5-pro`, `gemini-1.5-flash` |
1210
+ | Provider | Models |
1211
+ | --------- | ---------------------------------------------------- |
1212
+ | OpenAI | `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo` |
1213
+ | Anthropic | `claude-sonnet-4-20250514`, `claude-3-opus-20240229` |
1214
+ | Google | `gemini-1.5-pro`, `gemini-1.5-flash` |
474
1215
 
475
1216
  **Vision:** `model.generateVision(prompt, images, options)` for image inputs.
476
1217
 
@@ -478,7 +1219,7 @@ const response = await model.invoke(messages, { tools });
478
1219
 
479
1220
  ### Tools
480
1221
 
481
- **defineTool(config)** Define a type-safe tool with Zod schema and handler.
1222
+ **defineTool(config)** -- Define a type-safe tool with Zod schema and handler.
482
1223
 
483
1224
  ```typescript
484
1225
  import { defineTool } from 'sweagent';
@@ -492,21 +1233,21 @@ const tool = defineTool({
492
1233
  });
493
1234
  ```
494
1235
 
495
- **createToolSet(tools)** Build a record of tools for the agent (key = tool name).
1236
+ **createToolSet(tools)** -- Build a record of tools for the agent (key = tool name).
496
1237
 
497
- **getTool(toolSet, name)** / **getTools(toolSet)** Look up one or all tools.
1238
+ **getTool(toolSet, name)** / **getTools(toolSet)** -- Look up one or all tools.
498
1239
 
499
- **executeTool(tool, input, options)** Run a single tool with input.
1240
+ **executeTool(tool, input, options)** -- Run a single tool with input.
500
1241
 
501
- **executeToolByName(toolSet, name, input, options)** Run by name; throws if tool missing.
1242
+ **executeToolByName(toolSet, name, input, options)** -- Run by name; throws if tool missing.
502
1243
 
503
- **zodToJsonSchema(schema)** Convert a Zod schema to JSON Schema (e.g. for MCP).
1244
+ **zodToJsonSchema(schema)** -- Convert a Zod schema to JSON Schema (e.g. for MCP).
504
1245
 
505
1246
  ---
506
1247
 
507
1248
  ### Agents
508
1249
 
509
- **runAgent(config)** Run the agent loop until the model returns no tool calls or max iterations is reached.
1250
+ **runAgent(config)** -- Run the agent loop until the model returns no tool calls or max iterations is reached.
510
1251
 
511
1252
  ```typescript
512
1253
  import { runAgent } from 'sweagent';
@@ -523,15 +1264,11 @@ const result = await runAgent({
523
1264
  // result: { output, steps, totalUsage, messages }
524
1265
  ```
525
1266
 
526
- **AgentStep** – `{ iteration, content?, toolCalls?, toolResults?, usage? }`.
527
-
528
- **AgentResult** – `{ output, steps, totalUsage?, messages }`.
529
-
530
1267
  ---
531
1268
 
532
1269
  ### Subagents
533
1270
 
534
- **defineSubagent(config)** Define a subagent (name must be kebab-case).
1271
+ **defineSubagent(config)** -- Define a subagent (name must be kebab-case).
535
1272
 
536
1273
  ```typescript
537
1274
  import { defineSubagent } from 'sweagent';
@@ -548,165 +1285,181 @@ const def = defineSubagent({
548
1285
  });
549
1286
  ```
550
1287
 
551
- **runSubagent(definition, input, options)** Run the subagent in isolation.
552
-
553
- **createSubagentTool(definition, options)** – Expose one subagent as a tool (input: `{ prompt }`).
554
-
555
- **createSubagentToolSet(definitions, options)** – Build a record of subagent tools (`subagent_<name>`).
1288
+ **runSubagent(definition, input, options)** -- Run the subagent in isolation.
556
1289
 
557
- ---
558
-
559
- ### MCP
560
-
561
- **BaseMcpClient** – Base class for MCP clients. Lazy connection, `callTool(name, args)` for invocation.
1290
+ **createSubagentTool(definition, options)** -- Expose one subagent as a tool.
562
1291
 
563
- **BaseMcpClient.resolveConfig(options, resolveOpts)** Build config from options and env (e.g. `MCP_URL`, `MCP_COMMAND`, `MCP_ARGS`).
564
-
565
- **Transports** – HTTP and stdio transports are used internally; extend `BaseMcpClient` and pass config with `url` or `command` + `args`.
1292
+ **createSubagentToolSet(definitions, options)** -- Build a record of subagent tools (`subagent_<name>`).
566
1293
 
567
1294
  ---
568
1295
 
569
- ### Errors
1296
+ ### Planning
570
1297
 
571
- | Class | When |
572
- | ----- | ---- |
573
- | **LibraryError** | Base; all others extend it. |
574
- | **ModelError** | Model creation or invoke failed. |
575
- | **ToolError** | Tool not found or tool execution failed. |
576
- | **ValidationError** | Zod validation failed. |
577
- | **AgentError** | Agent reached max iterations without completing. |
578
- | **SubagentError** | Subagent config or run failed. |
1298
+ **runPlanningAgent(config)** -- One-shot mode: single input, auto-advances through all stages, returns plan markdown.
579
1299
 
580
- All accept an optional `cause` for chaining.
1300
+ ```typescript
1301
+ import { runPlanningAgent } from 'sweagent';
581
1302
 
582
- ---
1303
+ const result = await runPlanningAgent({
1304
+ input: string,
1305
+ model?: ModelConfig,
1306
+ maxIterations?: number,
1307
+ onStep?: (step: AgentStep) => void,
1308
+ logger?: Logger,
1309
+ });
1310
+ // result: AgentResult { output, steps, totalUsage, messages }
1311
+ ```
583
1312
 
584
- ## Production Modules
1313
+ **processPlanningChat(userMessage, context, config)** -- Multi-turn chat mode. Pass `null` context on the first turn.
585
1314
 
586
- ### DB Designer
1315
+ ```typescript
1316
+ import { processPlanningChat } from 'sweagent';
587
1317
 
588
- Generates MongoDB-style project schemas (modules, fields, relationships) from natural language requirements.
1318
+ const result = await processPlanningChat(userMessage, context, {
1319
+ model?: ModelConfig,
1320
+ maxIterations?: number,
1321
+ onStep?: (step: AgentStep) => void,
1322
+ logger?: Logger,
1323
+ });
1324
+ // result: PlanChatTurnResult { message, context, pendingQuestions, planMarkdown }
1325
+ ```
589
1326
 
590
- **Tools:** `design_database`, `design_database_pro`, `redesign_database`, `validate_schema`
591
- **Subagents:** `entity-analyzer` (structured analysis), `schema-refiner` (refinement/validation)
1327
+ **runPlanningFromRequirements(config)** -- Convert requirement-gatherer output to a plan, skipping redundant stages.
592
1328
 
593
1329
  ```typescript
594
- import { runDbDesignerAgent } from 'sweagent';
1330
+ import { runPlanningFromRequirements } from 'sweagent';
595
1331
 
596
- const result = await runDbDesignerAgent({
597
- input: 'E-commerce: users, orders, products. Admins manage products.',
598
- model: { provider: 'openai', model: 'gpt-4o-mini' },
599
- maxIterations: 15,
600
- onStep: step => console.log(step),
1332
+ const result = await runPlanningFromRequirements({
1333
+ requirement: FinalRequirement, // JSON from requirement-gatherer
1334
+ model?: ModelConfig,
1335
+ onStep?: (step: AgentStep) => void,
1336
+ logger?: Logger,
601
1337
  });
602
- console.log(result.output);
1338
+ // result: AgentResult { output (plan markdown), steps, totalUsage, messages }
603
1339
  ```
604
1340
 
605
- ### React Builder
1341
+ **assemblePlan(projectName, sections)** -- Assemble `PlanSections` into a single markdown string.
606
1342
 
607
- Generates frontend application config (app, modules, pages, fields, API hooks) from a GraphQL schema.
1343
+ **writePlanToFile(markdown, outputPath)** -- Write plan markdown to a file.
608
1344
 
609
- **Tools:** `generate_frontend`, `generate_feature_breakdown`, `validate_frontend_config`
610
- **Subagents:** `graphql-analyzer`, `config-validator`
1345
+ **PlanningContextBuilder** -- Fluent builder for `PlanningContext`:
611
1346
 
612
1347
  ```typescript
613
- import { runReactBuilderAgent } from 'sweagent';
1348
+ import { createPlanningContextBuilder } from 'sweagent';
614
1349
 
615
- const result = await runReactBuilderAgent({
616
- input: 'GraphQL schema: type User { id: ID! name: String } ...',
617
- model: { provider: 'openai', model: 'gpt-4o-mini' },
618
- maxIterations: 15,
619
- });
620
- console.log(result.output);
1350
+ const context = createPlanningContextBuilder()
1351
+ .withStage('requirements')
1352
+ .withProjectDescription('Task manager app')
1353
+ .withSections({ overview: '## Overview\n...' })
1354
+ .build();
621
1355
  ```
622
1356
 
623
1357
  ---
624
1358
 
625
- ## Examples
1359
+ ### MCP
626
1360
 
627
- The [examples directory](./examples/README.md) contains runnable scripts. Use the interactive launcher or run a file directly:
1361
+ **BaseMcpClient** -- Base class for MCP clients. Lazy connection, `callTool(name, args)` for invocation.
628
1362
 
629
- ```bash
630
- # Interactive launcher (pick folder and example, then provide inputs)
631
- npm run example:interactive
632
-
633
- # Run a specific example (from repo root)
634
- npm run example -- examples/core/01-basic-model.ts
635
- npm run example -- examples/hello-world/01-hello-world.ts
636
- npm run example -- examples/db-designer/01-db-designer-agent.ts
637
- npm run example -- examples/react-builder/01-react-builder-agent.ts
638
- ```
639
-
640
- | Group | Examples | Description |
641
- | ----- | -------- | ----------- |
642
- | **Core** | 01 Basic Model, 02 All Providers, 03 Tool Calling, 04 Multi-Tool Agent, 05 Subagents | Models, tools, agents, subagents |
643
- | **Hello World** | 01 Hello World | Minimal agent with greeting tool |
644
- | **DB Designer** | 01 DB Designer Agent | MongoDB schema from natural language |
645
- | **React Builder** | 01 React Builder Agent | Frontend config from GraphQL |
1363
+ **BaseMcpClient.resolveConfig(options, resolveOpts)** -- Build config from options and env (e.g. `MCP_URL`, `MCP_COMMAND`, `MCP_ARGS`).
646
1364
 
647
1365
  ---
648
1366
 
649
- ## Configuration Reference
1367
+ ### Errors
650
1368
 
651
- ### Environment variables
1369
+ | Class | When |
1370
+ | ------------------- | ------------------------------------------------ |
1371
+ | **LibraryError** | Base; all others extend it. |
1372
+ | **ModelError** | Model creation or invoke failed. |
1373
+ | **ToolError** | Tool not found or tool execution failed. |
1374
+ | **ValidationError** | Zod validation failed. |
1375
+ | **AgentError** | Agent reached max iterations without completing. |
1376
+ | **SubagentError** | Subagent config or run failed. |
1377
+
1378
+ All accept an optional `cause` for chaining.
652
1379
 
653
- | Variable | Purpose |
654
- | -------- | ------- |
655
- | `OPENAI_API_KEY` | OpenAI API key |
656
- | `ANTHROPIC_API_KEY` | Anthropic API key |
657
- | `GOOGLE_GENERATIVE_AI_API_KEY` | Google AI API key |
658
- | `MCP_URL` / `MCP_COMMAND` / `MCP_ARGS` | MCP client (when using `resolveConfig`) |
1380
+ ---
659
1381
 
660
- For examples: `PROVIDER`, `MODEL`, `AGENT_INPUT`, `REQUIREMENT`, `MAX_ITERATIONS` are used by example scripts.
1382
+ ## Reference
661
1383
 
662
- ### ModelConfig
1384
+ ### Environment variables
663
1385
 
664
- `provider`, `model`, `apiKey?`, `temperature?`, `maxOutputTokens?`, `baseUrl?`
1386
+ | Variable | Purpose |
1387
+ | -------------------------------------- | -------------------------------------------------------------- |
1388
+ | `OPENAI_API_KEY` | OpenAI API key |
1389
+ | `ANTHROPIC_API_KEY` | Anthropic API key |
1390
+ | `GOOGLE_GENERATIVE_AI_API_KEY` | Google AI API key |
1391
+ | `MCP_URL` / `MCP_COMMAND` / `MCP_ARGS` | MCP client (when using `resolveConfig`) |
1392
+ | `PROVIDER` | Default provider for examples |
1393
+ | `MODEL` | Default model for examples |
1394
+ | `REQUIREMENT` | Project requirement for planning/requirement-gatherer examples |
1395
+ | `MAX_ITERATIONS` | Max agent iterations for examples |
665
1396
 
666
- ### AgentConfig
1397
+ ### Config types
667
1398
 
668
- `model`, `tools`, `systemPrompt`, `input`, `maxIterations?`, `onStep?`
1399
+ - **ModelConfig** -- `provider`, `model`, `apiKey?`, `temperature?`, `maxOutputTokens?`, `baseUrl?`
1400
+ - **AgentConfig** -- `model`, `tools`, `systemPrompt`, `input`, `maxIterations?`, `onStep?`
1401
+ - **PlanningAgentConfig** -- `input`, `model?`, `maxIterations?`, `onStep?`, `logger?`
1402
+ - **PlanFromRequirementsConfig** -- `requirement`, `model?`, `onStep?`, `logger?`
669
1403
 
670
- ---
1404
+ ### FAQ
671
1405
 
672
- ## FAQ
1406
+ **Which AI provider should I use?**
1407
+ All work well. Choose by existing infrastructure and pricing. The API is the same regardless of provider.
673
1408
 
674
- **Which AI provider should I use?**
675
- All work well. Choose by existing infra and pricing. The API is the same.
1409
+ **Can I use this with Claude Code / Codex / Cursor?**
1410
+ Yes -- this is the primary use case. See [Use with Cursor, Claude Code, and Codex](#use-with-cursor-claude-code-and-codex) for detailed integration guides with code examples. In short: generate a plan or structured spec with sweagent, save it to a file, and reference it in your coding agent. For Cursor, save to `.cursor/rules/` or paste into chat. For Claude Code, save as `CLAUDE.md` or reference with `@plan.md`. For Codex, feed the structured JSON as context.
676
1411
 
677
- **How do I handle rate limits?**
1412
+ **How do I handle rate limits?**
678
1413
  sweagent has no built-in rate limiting. Use a retry library (e.g. `p-retry`) around `model.invoke` or `runAgent` if needed.
679
1414
 
680
- **Can I use sweagent in the browser?**
1415
+ **Can I use sweagent in the browser?**
681
1416
  Target is Node.js. For browsers, proxy API calls through your backend and keep keys server-side.
682
1417
 
683
- **How do I add a new provider?**
684
- Implement a factory that returns a model conforming to the internal `Model` interface (e.g. via `createAIModel` and the providers AI SDK binding) and register it in `createModel`.
1418
+ **How do I add a new provider?**
1419
+ Implement a factory that returns a model conforming to the internal `Model` interface (e.g. via `createAIModel` and the provider's AI SDK binding) and register it in `createModel`.
685
1420
 
686
- ---
1421
+ ### Troubleshooting
687
1422
 
688
- ## Troubleshooting
1423
+ **API key errors** -- Ensure the key is set: `echo $OPENAI_API_KEY` (or the relevant env var). If using `.env`, load it: `tsx --env-file=.env your-script.ts` or `node --env-file=.env your-script.js`.
689
1424
 
690
- ### API key errors
1425
+ **Model not found** -- Use the exact model id for the provider (e.g. `gpt-4o-mini`, `claude-sonnet-4-20250514`). Confirm your account has access to that model.
691
1426
 
692
- **Invalid API key / Authentication failed**
1427
+ **Agent hits max iterations** -- Increase `maxIterations` or simplify the task. Check that tools return clear, parseable results so the model can decide the next step.
693
1428
 
694
- - Ensure the key is set: `echo $OPENAI_API_KEY` (or the relevant env var).
695
- - If using `.env`, load it: `tsx --env-file=.env your-script.ts` or `node --env-file=.env your-script.js`.
1429
+ **Tool not found** -- Tools must be in the same object passed to `runAgent` under the name the model uses (e.g. `createToolSet({ calculator: calculatorTool })` means the model calls `calculator`).
696
1430
 
697
- ### Model not found
1431
+ ---
698
1432
 
699
- - Use the exact model id for the provider (e.g. `gpt-4o-mini`, `claude-3-5-sonnet-20241022`).
700
- - Confirm your account has access to that model.
1433
+ ## Examples
701
1434
 
702
- ### Agent hits max iterations
1435
+ The [examples directory](./examples/) contains runnable scripts organized by domain agent. Use the interactive launcher or run a file directly:
703
1436
 
704
- - Increase `maxIterations` or simplify the task.
705
- - Check that tools return clear, parseable results so the model can decide the next step.
1437
+ ```bash
1438
+ # Interactive launcher -- pick a domain agent, then an example
1439
+ npm run example:interactive
706
1440
 
707
- ### Tool not found
1441
+ # Run a specific domain agent example
1442
+ npm run example -- examples/planning/01-planning-agent.ts
1443
+ npm run example -- examples/data-modeler/01-data-modeler-agent.ts
1444
+ npm run example -- examples/react-builder/01-react-builder-agent.ts
1445
+ ```
708
1446
 
709
- - Tools must be in the same object passed to `runAgent` under the name the model uses (e.g. `createToolSet({ calculator: calculatorTool })` → model calls `calculator`).
1447
+ | Domain Agent | Examples | What it produces |
1448
+ | ------------------------ | ----------------------------------------------------- | ------------------------------------------------------------------------ |
1449
+ | **Planning** | 01 Planning Agent | Implementation-ready markdown plan through 4-stage pipeline |
1450
+ | **Requirement Gatherer** | 01 Requirement Gatherer Agent | Structured JSON requirements (actors, flows, stories, modules) |
1451
+ | **Data Modeler** | 01 Data Modeler Agent | MongoDB/PostgreSQL data model with entities, fields, indexes |
1452
+ | **API Designer** | 01 API Designer Agent | REST/GraphQL API design with endpoints and contracts |
1453
+ | **Auth Designer** | 01 Auth Designer Agent | Auth strategy, flows, middleware, roles, and policies |
1454
+ | **Backend Architect** | 01 Backend Architect Agent | Backend architecture with framework selection and services |
1455
+ | **Express Builder** | 01 Express Builder Agent | Express.js config with routers, models, and middleware |
1456
+ | **Apollo Builder** | 01 Apollo Builder Agent | Apollo GraphQL subgraph config with types and resolvers |
1457
+ | **Frontend Architect** | 01 Frontend Architect Agent | Frontend architecture with pages, components, and routing |
1458
+ | **React Builder** | 01 React Builder Agent | Frontend config via `graphql-analyzer` and `config-validator` sub-agents |
1459
+ | **Next.js Builder** | 01 Next.js Builder Agent | Next.js App Router config with pages, layouts, and API routes |
1460
+ | **Execution Planner** | 01 Execution Planner Agent | Phased implementation plan with edge cases and testing checklist |
1461
+ | **Core Framework** | 01-05: Model, Providers, Tools, Multi-Tool, Subagents | Models, tools, agent loop, sub-agent delegation |
1462
+ | **Hello World** | 01 Hello World | Minimal agent with greeting tool (module template) |
710
1463
 
711
1464
  ---
712
1465
 
@@ -727,29 +1480,23 @@ npm run lint
727
1480
  npm run build
728
1481
  ```
729
1482
 
730
- | Command | Description |
731
- | ------- | ----------- |
732
- | `npm run dev` | Watch build |
733
- | `npm test` | Unit tests |
734
- | `npm run test:integration` | Integration tests |
735
- | `npm run test:all` | All tests |
736
- | `npm run lint` / `npm run lint:fix` | ESLint |
737
- | `npm run typecheck` | TypeScript |
738
- | `npm run build` | Production build |
1483
+ | Command | Description |
1484
+ | ----------------------------------- | ----------------- |
1485
+ | `npm run dev` | Watch build |
1486
+ | `npm test` | Unit tests |
1487
+ | `npm run test:integration` | Integration tests |
1488
+ | `npm run test:all` | All tests |
1489
+ | `npm run lint` / `npm run lint:fix` | ESLint |
1490
+ | `npm run typecheck` | TypeScript |
1491
+ | `npm run build` | Production build |
739
1492
 
740
1493
  **Support**
741
1494
 
742
- - [GitHub Issues](https://github.com/sijeeshmiziha/sweagent/issues) Bugs and features
743
- - [GitHub Discussions](https://github.com/sijeeshmiziha/sweagent/discussions) Questions
1495
+ - [GitHub Issues](https://github.com/sijeeshmiziha/sweagent/issues) -- Bugs and features
1496
+ - [GitHub Discussions](https://github.com/sijeeshmiziha/sweagent/discussions) -- Questions
744
1497
 
745
1498
  ---
746
1499
 
747
1500
  ## License
748
1501
 
749
- MIT License see [LICENSE](LICENSE) for details.
750
-
751
- ---
752
-
753
- <p align="center">
754
- Made with ❤️ by the CompilersLab team!
755
- </p>
1502
+ MIT License -- see [LICENSE](LICENSE) for details.