sweagent 0.0.1

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 ADDED
@@ -0,0 +1,755 @@
1
+ <p align="center">
2
+ <h1 align="center">sweagent</h1>
3
+ <p align="center">
4
+ <strong>A multi-provider AI software engineering agent framework with tool calling.</strong>
5
+ </p>
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.
8
+ </p>
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://www.npmjs.com/package/sweagent"><img src="https://img.shields.io/npm/v/sweagent.svg?style=flat-square&color=0ea5e9&labelColor=0c4a6e" alt="npm version"></a>
13
+ <a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/TypeScript-5.7-3178c6?style=flat-square&logo=typescript&logoColor=white" alt="TypeScript"></a>
14
+ <a href="https://github.com/sijeeshmiziha/sweagent/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-8b5cf6?style=flat-square" alt="License"></a>
15
+ <a href="https://github.com/sijeeshmiziha/sweagent/pulls"><img src="https://img.shields.io/badge/PRs-Welcome-10b981?style=flat-square&labelColor=064e3b" alt="PRs Welcome"></a>
16
+ </p>
17
+
18
+ <p align="center">
19
+ <a href="#what-is-sweagent">What is sweagent?</a> •
20
+ <a href="#why-sweagent">Why sweagent?</a> •
21
+ <a href="#installation">Installation</a> •
22
+ <a href="#getting-started-tutorial">Tutorial</a> •
23
+ <a href="#architecture">Architecture</a> •
24
+ <a href="#api-reference">API Reference</a> •
25
+ <a href="#production-modules">Modules</a> •
26
+ <a href="#examples">Examples</a> •
27
+ <a href="#contributing">Contributing</a>
28
+ </p>
29
+
30
+ ---
31
+
32
+ ## Table of Contents
33
+
34
+ - [What is sweagent?](#what-is-sweagent)
35
+ - [Why sweagent?](#why-sweagent)
36
+ - [Features](#features)
37
+ - [Installation](#installation)
38
+ - [Getting Started Tutorial](#getting-started-tutorial)
39
+ - [Architecture](#architecture)
40
+ - [Engineering Deep Dive](#engineering-deep-dive)
41
+ - [API Reference](#api-reference)
42
+ - [Production Modules](#production-modules)
43
+ - [Examples](#examples)
44
+ - [Configuration Reference](#configuration-reference)
45
+ - [FAQ](#faq)
46
+ - [Troubleshooting](#troubleshooting)
47
+ - [Contributing](#contributing)
48
+ - [License](#license)
49
+
50
+ ---
51
+
52
+ ## What is sweagent?
53
+
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.
55
+
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.
57
+
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.
59
+
60
+ ```typescript
61
+ import { createModel, runAgent, defineTool } from 'sweagent';
62
+ import { z } from 'zod';
63
+
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}!` }),
70
+ });
71
+
72
+ const result = await runAgent({
73
+ model,
74
+ tools: [greetTool],
75
+ systemPrompt: 'You are a helpful assistant.',
76
+ input: 'Greet Alice',
77
+ });
78
+ console.log(result.output);
79
+ ```
80
+
81
+ ---
82
+
83
+ ## Why sweagent?
84
+
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.
86
+
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.
88
+
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.
90
+
91
+ ---
92
+
93
+ ## Features
94
+
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. |
105
+
106
+ ---
107
+
108
+ ## Installation
109
+
110
+ ### Step 1: Prerequisites
111
+
112
+ - **Node.js** >= 18.0.0
113
+ - **npm** >= 8.0.0 (or yarn, pnpm, bun)
114
+
115
+ ### Step 2: Install the package
116
+
117
+ **Using the package in your project:**
118
+
119
+ ```bash
120
+ npm install sweagent
121
+ ```
122
+
123
+ Or with yarn, pnpm, or bun:
124
+
125
+ ```bash
126
+ yarn add sweagent
127
+ pnpm add sweagent
128
+ bun add sweagent
129
+ ```
130
+
131
+ All AI provider SDKs (OpenAI, Anthropic, Google) are included; no extra packages are required.
132
+
133
+ **Contributing from source:**
134
+
135
+ ```bash
136
+ git clone https://github.com/sijeeshmiziha/sweagent.git
137
+ cd sweagent
138
+ npm install
139
+ ```
140
+
141
+ ### Step 3: Environment setup
142
+
143
+ Create a `.env` file in your project root (or export variables in your shell):
144
+
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=...
150
+ ```
151
+
152
+ ### Step 4: Verify installation
153
+
154
+ Run the hello-world example to confirm everything works.
155
+
156
+ **If you installed the package:** create a file `test-agent.mjs` (or `test-agent.ts` with tsx):
157
+
158
+ ```javascript
159
+ import { createModel, runAgent, helloWorldTool } from 'sweagent';
160
+ const model = createModel({ provider: 'openai', model: 'gpt-4o-mini' });
161
+ const result = await runAgent({
162
+ model,
163
+ tools: [helloWorldTool],
164
+ systemPrompt: 'You are helpful.',
165
+ input: 'Say hello',
166
+ });
167
+ console.log(result.output);
168
+ ```
169
+
170
+ Run it with `node --env-file=.env test-agent.mjs` (or `npx tsx --env-file=.env test-agent.ts`).
171
+
172
+ **If you cloned the repo:**
173
+
174
+ ```bash
175
+ npm run example -- examples/hello-world/01-hello-world.ts
176
+ ```
177
+
178
+ ---
179
+
180
+ ## Getting Started Tutorial
181
+
182
+ Progress from a simple model call to a full agent with tools and subagents.
183
+
184
+ ### Level 1: Model invocation
185
+
186
+ Create a model and get a completion:
187
+
188
+ ```typescript
189
+ import { createModel } from 'sweagent';
190
+
191
+ const model = createModel({
192
+ provider: 'openai',
193
+ model: 'gpt-4o-mini',
194
+ temperature: 0.7,
195
+ });
196
+
197
+ const response = await model.invoke([
198
+ { role: 'user', content: 'Explain TypeScript in one sentence.' },
199
+ ]);
200
+ console.log(response.text);
201
+ ```
202
+
203
+ ### Level 2: Custom tools
204
+
205
+ Define a type-safe tool with Zod:
206
+
207
+ ```typescript
208
+ import { defineTool } from 'sweagent';
209
+ import { z } from 'zod';
210
+
211
+ const calculatorTool = defineTool({
212
+ name: 'calculator',
213
+ description: 'Perform math calculations',
214
+ input: z.object({
215
+ expression: z.string().describe('Math expression to evaluate'),
216
+ }),
217
+ handler: async ({ expression }) => {
218
+ const result = eval(expression); // Use a safe math parser in production
219
+ return { result };
220
+ },
221
+ });
222
+ ```
223
+
224
+ ### Level 3: Agent loop
225
+
226
+ Run an agent that can call your tools:
227
+
228
+ ```typescript
229
+ import { runAgent, createModel, defineTool, createToolSet } from 'sweagent';
230
+ import { z } from 'zod';
231
+
232
+ const calculatorTool = defineTool({
233
+ name: 'calculator',
234
+ description: 'Perform math calculations',
235
+ input: z.object({ expression: z.string() }),
236
+ handler: async ({ expression }) => ({ result: String(eval(expression)) }),
237
+ });
238
+
239
+ const result = await runAgent({
240
+ model: createModel({ provider: 'openai', model: 'gpt-4o-mini' }),
241
+ tools: createToolSet({ calculator: calculatorTool }),
242
+ systemPrompt: 'You are a helpful assistant. Use the calculator when needed.',
243
+ input: 'What is 25 multiplied by 4?',
244
+ maxIterations: 10,
245
+ onStep: step => console.log(`Step ${step.iteration}:`, step.toolCalls ?? step.content),
246
+ });
247
+ console.log(result.output);
248
+ ```
249
+
250
+ ### Level 4: Subagents
251
+
252
+ Define subagents and expose them as tools to a parent agent:
253
+
254
+ ```typescript
255
+ import { defineSubagent, createSubagentToolSet, runAgent, createModel, createToolSet } from 'sweagent';
256
+
257
+ const researcher = defineSubagent({
258
+ name: 'researcher',
259
+ description: 'Research a topic and return a short summary',
260
+ systemPrompt: 'You are a researcher. Answer concisely.',
261
+ });
262
+ const subagentTools = createSubagentToolSet([researcher], { parentModel: model });
263
+ const tools = createToolSet({ ...otherTools, ...subagentTools });
264
+
265
+ const result = await runAgent({
266
+ model,
267
+ tools,
268
+ systemPrompt: 'You can delegate research to subagent_researcher.',
269
+ input: 'Research the history of TypeScript and summarize in 2 sentences.',
270
+ maxIterations: 10,
271
+ });
272
+ ```
273
+
274
+ ### Level 5: Production module (DB Designer)
275
+
276
+ Use the DB Designer orchestrator to generate a MongoDB schema from natural language:
277
+
278
+ ```typescript
279
+ import { runDbDesignerAgent } from 'sweagent';
280
+
281
+ const result = await runDbDesignerAgent({
282
+ input: 'E-commerce: users, orders, products. Admins manage products. Users place orders and have a profile.',
283
+ model: { provider: 'openai', model: 'gpt-4o-mini' },
284
+ maxIterations: 15,
285
+ });
286
+ console.log(result.output);
287
+ ```
288
+
289
+ ### Level 6: MCP integration
290
+
291
+ Extend `BaseMcpClient` to call MCP tools (e.g. HTTP or stdio server):
292
+
293
+ ```typescript
294
+ import { BaseMcpClient } from 'sweagent';
295
+
296
+ const config = BaseMcpClient.resolveConfig(
297
+ { url: 'https://your-mcp-server.example.com' },
298
+ { envPrefix: 'MCP' }
299
+ );
300
+ const client = new BaseMcpClient({ name: 'my-app', version: '1.0.0' }, config);
301
+ const result = await client.callTool('tool_name', { arg: 'value' });
302
+ ```
303
+
304
+ ---
305
+
306
+ ## Architecture
307
+
308
+ ### System overview
309
+
310
+ ```mermaid
311
+ graph TB
312
+ subgraph Client[Client Application]
313
+ App[Your App]
314
+ end
315
+
316
+ subgraph Sweagent[sweagent]
317
+ Models[Models]
318
+ Tools[Tools]
319
+ Agents[Agent Loop]
320
+ Subagents[Subagents]
321
+ Modules[DB Designer, React Builder]
322
+ end
323
+
324
+ subgraph Providers[AI Providers]
325
+ OpenAI[OpenAI]
326
+ Anthropic[Anthropic]
327
+ Google[Google]
328
+ end
329
+
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
341
+ ```
342
+
343
+ ### Agent execution flow
344
+
345
+ ```mermaid
346
+ sequenceDiagram
347
+ participant User
348
+ participant Agent
349
+ participant Model
350
+ participant Tools
351
+
352
+ User->>Agent: Input + Tools + System Prompt
353
+ loop Until Complete or Max Iterations
354
+ Agent->>Model: Messages + Tool Schemas
355
+ Model-->>Agent: Response (Text or Tool Calls)
356
+ alt Tool Calls Present
357
+ Agent->>Tools: Execute Tool Calls
358
+ Tools-->>Agent: Tool Results
359
+ Agent->>Agent: Append Results to Messages
360
+ else Final Answer
361
+ Agent-->>User: Output + Steps + Usage
362
+ end
363
+ end
364
+ ```
365
+
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
403
+
404
+ ### Problem decomposition: long-running agents
405
+
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.”
407
+
408
+ ### Incremental progress pattern
409
+
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.
411
+
412
+ ### Feature list approach
413
+
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.
415
+
416
+ ### Clean state principle
417
+
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.
419
+
420
+ ### Error hierarchy design
421
+
422
+ Errors are organized so you can handle them by type and preserve cause chains:
423
+
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
430
+
431
+ Each can wrap a `cause`. Use try/catch and `instanceof` to branch on the right level.
432
+
433
+ ### Provider adapter pattern
434
+
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.
436
+
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.
440
+
441
+ ---
442
+
443
+ ## API Reference
444
+
445
+ All public APIs are exported from the main package: `import { ... } from 'sweagent'`.
446
+
447
+ ### Models
448
+
449
+ **createModel(config)** – Create a model instance for the given provider.
450
+
451
+ ```typescript
452
+ import { createModel } from 'sweagent';
453
+
454
+ const model = createModel({
455
+ 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
459
+ maxOutputTokens?: number,
460
+ baseUrl?: string,
461
+ });
462
+
463
+ const response = await model.invoke(messages, { tools });
464
+ // response: { text, toolCalls, usage, finishReason }
465
+ ```
466
+
467
+ **Supported models (examples):**
468
+
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` |
474
+
475
+ **Vision:** `model.generateVision(prompt, images, options)` for image inputs.
476
+
477
+ ---
478
+
479
+ ### Tools
480
+
481
+ **defineTool(config)** – Define a type-safe tool with Zod schema and handler.
482
+
483
+ ```typescript
484
+ import { defineTool } from 'sweagent';
485
+ import { z } from 'zod';
486
+
487
+ const tool = defineTool({
488
+ name: 'my_tool',
489
+ description: 'What the tool does',
490
+ input: z.object({ key: z.string() }),
491
+ handler: async (parsed, context) => ({ result: parsed.key }),
492
+ });
493
+ ```
494
+
495
+ **createToolSet(tools)** – Build a record of tools for the agent (key = tool name).
496
+
497
+ **getTool(toolSet, name)** / **getTools(toolSet)** – Look up one or all tools.
498
+
499
+ **executeTool(tool, input, options)** – Run a single tool with input.
500
+
501
+ **executeToolByName(toolSet, name, input, options)** – Run by name; throws if tool missing.
502
+
503
+ **zodToJsonSchema(schema)** – Convert a Zod schema to JSON Schema (e.g. for MCP).
504
+
505
+ ---
506
+
507
+ ### Agents
508
+
509
+ **runAgent(config)** – Run the agent loop until the model returns no tool calls or max iterations is reached.
510
+
511
+ ```typescript
512
+ import { runAgent } from 'sweagent';
513
+
514
+ const result = await runAgent({
515
+ model,
516
+ tools: createToolSet({ ... }),
517
+ systemPrompt: string,
518
+ input: string,
519
+ maxIterations?: number, // default 10
520
+ onStep?: (step: AgentStep) => void,
521
+ });
522
+
523
+ // result: { output, steps, totalUsage, messages }
524
+ ```
525
+
526
+ **AgentStep** – `{ iteration, content?, toolCalls?, toolResults?, usage? }`.
527
+
528
+ **AgentResult** – `{ output, steps, totalUsage?, messages }`.
529
+
530
+ ---
531
+
532
+ ### Subagents
533
+
534
+ **defineSubagent(config)** – Define a subagent (name must be kebab-case).
535
+
536
+ ```typescript
537
+ import { defineSubagent } from 'sweagent';
538
+
539
+ const def = defineSubagent({
540
+ name: 'my-subagent',
541
+ description: 'What this subagent does',
542
+ systemPrompt: '...',
543
+ tools?: Record<string, Tool>,
544
+ model?: ModelConfig,
545
+ maxIterations?: number,
546
+ disallowedTools?: string[],
547
+ onStep?: (step) => void,
548
+ });
549
+ ```
550
+
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>`).
556
+
557
+ ---
558
+
559
+ ### MCP
560
+
561
+ **BaseMcpClient** – Base class for MCP clients. Lazy connection, `callTool(name, args)` for invocation.
562
+
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`.
566
+
567
+ ---
568
+
569
+ ### Errors
570
+
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. |
579
+
580
+ All accept an optional `cause` for chaining.
581
+
582
+ ---
583
+
584
+ ## Production Modules
585
+
586
+ ### DB Designer
587
+
588
+ Generates MongoDB-style project schemas (modules, fields, relationships) from natural language requirements.
589
+
590
+ **Tools:** `design_database`, `design_database_pro`, `redesign_database`, `validate_schema`
591
+ **Subagents:** `entity-analyzer` (structured analysis), `schema-refiner` (refinement/validation)
592
+
593
+ ```typescript
594
+ import { runDbDesignerAgent } from 'sweagent';
595
+
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),
601
+ });
602
+ console.log(result.output);
603
+ ```
604
+
605
+ ### React Builder
606
+
607
+ Generates frontend application config (app, modules, pages, fields, API hooks) from a GraphQL schema.
608
+
609
+ **Tools:** `generate_frontend`, `generate_feature_breakdown`, `validate_frontend_config`
610
+ **Subagents:** `graphql-analyzer`, `config-validator`
611
+
612
+ ```typescript
613
+ import { runReactBuilderAgent } from 'sweagent';
614
+
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);
621
+ ```
622
+
623
+ ---
624
+
625
+ ## Examples
626
+
627
+ The [examples directory](./examples/README.md) contains runnable scripts. Use the interactive launcher or run a file directly:
628
+
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 |
646
+
647
+ ---
648
+
649
+ ## Configuration Reference
650
+
651
+ ### Environment variables
652
+
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`) |
659
+
660
+ For examples: `PROVIDER`, `MODEL`, `AGENT_INPUT`, `REQUIREMENT`, `MAX_ITERATIONS` are used by example scripts.
661
+
662
+ ### ModelConfig
663
+
664
+ `provider`, `model`, `apiKey?`, `temperature?`, `maxOutputTokens?`, `baseUrl?`
665
+
666
+ ### AgentConfig
667
+
668
+ `model`, `tools`, `systemPrompt`, `input`, `maxIterations?`, `onStep?`
669
+
670
+ ---
671
+
672
+ ## FAQ
673
+
674
+ **Which AI provider should I use?**
675
+ All work well. Choose by existing infra and pricing. The API is the same.
676
+
677
+ **How do I handle rate limits?**
678
+ sweagent has no built-in rate limiting. Use a retry library (e.g. `p-retry`) around `model.invoke` or `runAgent` if needed.
679
+
680
+ **Can I use sweagent in the browser?**
681
+ Target is Node.js. For browsers, proxy API calls through your backend and keep keys server-side.
682
+
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 provider’s AI SDK binding) and register it in `createModel`.
685
+
686
+ ---
687
+
688
+ ## Troubleshooting
689
+
690
+ ### API key errors
691
+
692
+ **Invalid API key / Authentication failed**
693
+
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`.
696
+
697
+ ### Model not found
698
+
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.
701
+
702
+ ### Agent hits max iterations
703
+
704
+ - Increase `maxIterations` or simplify the task.
705
+ - Check that tools return clear, parseable results so the model can decide the next step.
706
+
707
+ ### Tool not found
708
+
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`).
710
+
711
+ ---
712
+
713
+ ## Contributing
714
+
715
+ We welcome contributions. See [CONTRIBUTING.md](CONTRIBUTING.md) for code style, testing, and PR process.
716
+
717
+ **Quick start for contributors:**
718
+
719
+ ```bash
720
+ git clone https://github.com/sijeeshmiziha/sweagent.git
721
+ cd sweagent
722
+ npm install
723
+ cp .env.example .env
724
+ # Add API keys to .env
725
+ npm test
726
+ npm run lint
727
+ npm run build
728
+ ```
729
+
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 |
739
+
740
+ **Support**
741
+
742
+ - [GitHub Issues](https://github.com/sijeeshmiziha/sweagent/issues) – Bugs and features
743
+ - [GitHub Discussions](https://github.com/sijeeshmiziha/sweagent/discussions) – Questions
744
+
745
+ ---
746
+
747
+ ## License
748
+
749
+ MIT License – see [LICENSE](LICENSE) for details.
750
+
751
+ ---
752
+
753
+ <p align="center">
754
+ Made with ❤️ by the CompilersLab team!
755
+ </p>