@contractspec/app.cli-contractspec 1.44.0

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,658 @@
1
+ # @contractspec/app.cli-contractspec
2
+
3
+ Website: https://contractspec.io/
4
+
5
+
6
+ **Stabilize your AI-generated code** — Define contracts once, generate consistent code across API, DB, UI, and events. Safe regeneration. No lock-in.
7
+
8
+ CLI tool for creating, building, and validating contract specifications.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ bun add -D @contractspec/app.cli-contractspec
14
+ ```
15
+
16
+ ## Quick Start
17
+
18
+ ```bash
19
+ # Create a new contract spec interactively
20
+ contractspec create
21
+
22
+ # Create with AI assistance
23
+ contractspec create --ai
24
+
25
+ # Build implementation from spec
26
+ contractspec build src/contracts/mySpec.ts
27
+
28
+ # Validate a spec
29
+ contractspec validate src/contracts/mySpec.ts
30
+ ```
31
+
32
+ ## Commands
33
+
34
+ ### `contractspec list`
35
+
36
+ List all contract specifications in the project with filtering options.
37
+
38
+ **Options:**
39
+ - `--pattern <pattern>` - File pattern to search (glob)
40
+ - `--deep` - Load spec modules to extract richer metadata (executes spec modules)
41
+ - `--type <type>` - Filter by spec type (operation, event, presentation, etc.)
42
+ - `--owner <owner>` - Filter by owner
43
+ - `--tag <tag>` - Filter by tag
44
+ - `--stability <level>` - Filter by stability (experimental, beta, stable, deprecated)
45
+ - `--json` - Output as JSON for scripting
46
+
47
+ **Examples:**
48
+
49
+ ```bash
50
+ # List all specs
51
+ contractspec list
52
+
53
+ # Filter by type and stability
54
+ contractspec list --type operation --stability stable
55
+
56
+ # Find specs by owner
57
+ contractspec list --owner @team-platform
58
+
59
+ # JSON output for scripting
60
+ contractspec list --json
61
+ ```
62
+
63
+ ### `contractspec watch`
64
+
65
+ Watch contract specifications and auto-regenerate on changes.
66
+
67
+ **Options:**
68
+ - `--pattern <pattern>` - File pattern to watch (default: `**/*.contracts.ts`)
69
+ - `--build` - Auto-run build command on changes
70
+ - `--validate` - Auto-run validate command on changes
71
+ - `--on-start <mode>` - Run action on startup: none|validate|build|both (default: none)
72
+ - `--continue-on-error` - Do not exit on build/validate errors
73
+ - `--debounce <ms>` - Debounce delay in milliseconds (default: 500)
74
+
75
+ **Examples:**
76
+
77
+ ```bash
78
+ # Watch for changes and auto-build
79
+ contractspec watch --build
80
+
81
+ # Watch and auto-validate
82
+ contractspec watch --validate
83
+
84
+ # Custom pattern and debounce
85
+ contractspec watch --pattern 'src/**/*.ts' --debounce 1000
86
+ ```
87
+
88
+ ### `contractspec sync`
89
+
90
+ Sync contracts by building all discovered specs.
91
+
92
+ **Options:**
93
+ - `--pattern <pattern>` - File pattern to search (glob)
94
+ - `--buckets <buckets>` - Optional output buckets (comma-separated). Builds repeat into `./generated/<bucket>/`
95
+ - `--surfaces <surfaces>` - (deprecated) Alias for `--buckets`
96
+ - `--validate` - Validate each spec before building (spec-only)
97
+ - `--dry-run` - Show what would be synced without making changes
98
+
99
+ **Examples:**
100
+
101
+ ```bash
102
+ # Sync all surfaces
103
+ contractspec sync
104
+
105
+ # Sync specific surfaces
106
+ contractspec sync --surfaces api,ui
107
+
108
+ # Preview changes
109
+ contractspec sync --dry-run
110
+ ```
111
+
112
+ ### `contractspec clean`
113
+
114
+ Clean generated files and build artifacts.
115
+
116
+ **Options:**
117
+ - `--dry-run` - Show what would be deleted without deleting
118
+ - `--generated-only` - Only clean generated directories (generated/, dist/, .turbo/, outputDir artifacts)
119
+ - `--older-than <days>` - Only clean files older than specified days
120
+ - `--force` - Skip confirmation prompts
121
+ - `--git-clean` - Use `git clean -fdx` for comprehensive cleanup (requires confirmation or `--force`)
122
+
123
+ **Examples:**
124
+
125
+ ```bash
126
+ # Clean all generated files
127
+ contractspec clean
128
+
129
+ # Preview cleanup
130
+ contractspec clean --dry-run
131
+
132
+ # Clean only old files
133
+ contractspec clean --older-than 30
134
+
135
+ # Use git clean
136
+ contractspec clean --git-clean
137
+ ```
138
+
139
+ ### `contractspec deps`
140
+
141
+ Analyze contract dependencies and relationships.
142
+
143
+ **Options:**
144
+ - `--pattern <pattern>` - File pattern to search (glob)
145
+ - `--entry <name>` - Focus on a specific contract name
146
+ - `--format <format>` - text|json|dot (default: text)
147
+ - `--graph` - (deprecated) Same as `--format dot`
148
+ - `--circular` - Find circular dependencies
149
+ - `--missing` - Find missing dependencies
150
+ - `--json` - (deprecated) Same as `--format json`
151
+
152
+ **Examples:**
153
+
154
+ ```bash
155
+ # Show dependency overview
156
+ contractspec deps
157
+
158
+ # Analyze specific contract
159
+ contractspec deps --entry user.signup
160
+
161
+ # Find circular dependencies
162
+ contractspec deps --circular
163
+
164
+ # Generate graphviz graph
165
+ contractspec deps --format dot > deps.dot
166
+ ```
167
+
168
+ ### `contractspec diff`
169
+
170
+ Compare contract specifications and show differences.
171
+
172
+ **Arguments:**
173
+ - `<spec1> <spec2>` - Two spec files to compare
174
+
175
+ **Options:**
176
+ - `--breaking` - Only show breaking changes
177
+ - `--semantic` - Show semantic differences (not just text)
178
+ - `--json` - Output as JSON for scripting
179
+ - `--baseline <ref>` - Compare with git reference (branch/commit)
180
+
181
+ **Examples:**
182
+
183
+ ```bash
184
+ # Compare two specs
185
+ contractspec diff spec1.ts spec2.ts
186
+
187
+ # Compare with git branch
188
+ contractspec diff spec.ts spec.ts --baseline main
189
+
190
+ # Show only breaking changes
191
+ contractspec diff spec1.ts spec2.ts --breaking
192
+
193
+ # Semantic comparison
194
+ contractspec diff spec1.ts spec2.ts --semantic
195
+ ```
196
+
197
+ ### `contractspec openapi`
198
+
199
+ Import/Export OpenAPI specifications.
200
+
201
+ #### `contractspec openapi import`
202
+
203
+ Import an OpenAPI specification and generate ContractSpec models or other schema formats.
204
+
205
+ **Options:**
206
+ - `--file <path>` - Path to OpenAPI file (json/yaml) or URL
207
+ - `--output <dir>` - Output directory
208
+ - `--schema-format <format>` - Output schema format: `contractspec` (default), `zod`, `json-schema`, `graphql`
209
+ - `--prefix <prefix>` - Prefix for generated models
210
+
211
+ **Examples:**
212
+
213
+ ```bash
214
+ # Import as ContractSpec (default)
215
+ contractspec openapi import --file api.json --output ./models
216
+
217
+ # Import as Zod schemas
218
+ contractspec openapi import --file api.json --output ./zod --schema-format zod
219
+
220
+ # Import as GraphQL types
221
+ contractspec openapi import --file api.json --output ./gql --schema-format graphql
222
+ ```
223
+
224
+ ### `contractspec create`
225
+
226
+ Interactive wizard to create contract specifications.
227
+
228
+ **Options:**
229
+ - `--type <type>` - Spec type: operation, event, presentation, form, feature
230
+ - `--ai` - Use AI to generate spec from description
231
+ - `--provider <provider>` - AI provider: claude, openai, ollama, custom
232
+ - `--model <model>` - AI model to use
233
+
234
+ **Examples:**
235
+
236
+ ```bash
237
+ # Interactive wizard
238
+ contractspec create
239
+
240
+ # Create operation spec with AI
241
+ contractspec create --type operation --ai
242
+
243
+ # Use local Ollama model
244
+ contractspec create --ai --provider ollama --model codellama
245
+ ```
246
+
247
+ ### `contractspec build`
248
+
249
+ Generate implementation code from contract specs using AI agents with automatic fallbacks.
250
+
251
+ **Options:**
252
+ - `--output-dir <dir>` - Output directory (default: ./generated)
253
+ - `--provider <provider>` - AI provider: claude, openai, ollama, custom
254
+ - `--model <model>` - AI model to use
255
+ - `--agent-mode <mode>` - Agent mode: simple, cursor, claude-code, openai-codex
256
+ - `--no-tests` - Skip test generation
257
+ - `--no-agent` - Disable AI (use basic templates)
258
+
259
+ > ℹ️ The build command automatically orchestrates between the selected agent and lightweight templates. If an agent becomes unavailable (missing API key, Cursor not running, etc.) the CLI falls back to deterministic templates so the build never blocks.
260
+
261
+ **Examples:**
262
+
263
+ ```bash
264
+ # Generate handler from operation spec (default: simple agent)
265
+ contractspec build src/contracts/signup.contracts.ts
266
+
267
+ # Use Claude Code agent for high-quality production code
268
+ contractspec build src/contracts/signup.contracts.ts --agent-mode claude-code
269
+
270
+ # Use OpenAI Codex for algorithmic implementations
271
+ contractspec build src/contracts/optimizer.contracts.ts --agent-mode openai-codex
272
+
273
+ # Generate without AI (basic templates only)
274
+ contractspec build src/contracts/simple.contracts.ts --no-agent
275
+ ```
276
+
277
+ ### `contractspec validate`
278
+
279
+ Validate contract specifications and optionally verify implementations against specs using AI.
280
+
281
+ **Options:**
282
+ - `--check-implementation` - Validate implementation against spec using AI
283
+ - `--implementation-path <path>` - Path to implementation (auto-detected if not specified)
284
+ - `--agent-mode <mode>` - Agent mode for validation: simple, claude-code, openai-codex
285
+ - `--check-handlers` - Verify handler implementations exist
286
+ - `--check-tests` - Verify test coverage
287
+ - `-i, --interactive` - Interactive mode - prompt for what to validate
288
+
289
+ > ℹ️ When no validation scope is provided, the CLI prompts you to choose between spec-only validation or full spec + implementation validation. In non-interactive environments it defaults to spec-only. Use `--check-implementation` to skip the prompt.
290
+
291
+ **Examples:**
292
+
293
+ ```bash
294
+ # Validate spec structure only
295
+ contractspec validate src/contracts/signup.contracts.ts
296
+
297
+ # Validate implementation against spec with AI
298
+ contractspec validate src/contracts/signup.contracts.ts --check-implementation
299
+
300
+ # Interactive validation with Claude Code agent
301
+ contractspec validate src/contracts/signup.contracts.ts -i --agent-mode claude-code
302
+
303
+ # Validate specific implementation file
304
+ contractspec validate src/contracts/signup.contracts.ts \
305
+ --check-implementation \
306
+ --implementation-path src/handlers/signup.handler.ts \
307
+ --agent-mode claude-code
308
+ ```
309
+
310
+ ### `contractspec ci`
311
+
312
+ Run all validation checks for CI/CD pipelines with machine-readable output formats.
313
+
314
+ **Options:**
315
+ - `--pattern <glob>` - Glob pattern for spec discovery
316
+ - `--format <format>` - Output format: text, json, sarif (default: text)
317
+ - `--output <file>` - Write results to file
318
+ - `--fail-on-warnings` - Exit with code 2 on warnings
319
+ - `--skip <checks>` - Skip specific checks (comma-separated)
320
+ - `--checks <checks>` - Only run specific checks (comma-separated)
321
+ - `--check-handlers` - Include handler implementation checks
322
+ - `--check-tests` - Include test coverage checks
323
+ - `--verbose` - Verbose output
324
+
325
+ **Available Checks:**
326
+ - `structure` - Spec structure validation (meta, io, policy)
327
+ - `integrity` - Contract integrity (orphaned specs, broken refs)
328
+ - `deps` - Dependency analysis (circular deps, missing refs)
329
+ - `doctor` - Installation health (skips AI in CI)
330
+ - `handlers` - Handler implementation existence
331
+ - `tests` - Test file existence
332
+
333
+ **Exit Codes:**
334
+ - `0` - All checks passed
335
+ - `1` - Errors found
336
+ - `2` - Warnings found (with `--fail-on-warnings`)
337
+
338
+ **Examples:**
339
+
340
+ ```bash
341
+ # Run all CI checks
342
+ contractspec ci
343
+
344
+ # Output as JSON for parsing
345
+ contractspec ci --format json
346
+
347
+ # Output SARIF for GitHub Code Scanning
348
+ contractspec ci --format sarif --output results.sarif
349
+
350
+ # Skip doctor checks
351
+ contractspec ci --skip doctor
352
+
353
+ # Run only structure and integrity checks
354
+ contractspec ci --checks structure,integrity
355
+
356
+ # Fail on warnings
357
+ contractspec ci --fail-on-warnings
358
+ ```
359
+
360
+ For comprehensive CI/CD integration guides (GitHub Actions, GitLab CI, Jenkins, AWS CodeBuild, etc.), see [docs/ci-cd.md](./docs/ci-cd.md).
361
+
362
+ ## Configuration
363
+
364
+ Create a `.contractsrc.json` file in your project root:
365
+
366
+ ```json
367
+ {
368
+ "aiProvider": "claude",
369
+ "aiModel": "claude-3-7-sonnet-20250219",
370
+ "agentMode": "claude-code",
371
+ "outputDir": "./src",
372
+ "conventions": {
373
+ "operations": "interactions/commands|queries",
374
+ "events": "events",
375
+ "presentations": "presentations",
376
+ "forms": "forms"
377
+ },
378
+ "defaultOwners": ["@team"],
379
+ "defaultTags": ["auto-generated"]
380
+ }
381
+ ```
382
+
383
+ ### Agent Modes
384
+
385
+ The CLI supports multiple AI agent modes for different use cases:
386
+
387
+ - **simple** (default) - Direct LLM API calls, fast and straightforward
388
+ - **cursor** - Leverages Windsurf/Cursor agentic capabilities (requires Cursor environment)
389
+ - **claude-code** - Uses Claude with extended thinking, best for production code and validation
390
+ - **openai-codex** - Uses GPT-4o/o1 models, excellent for algorithms and optimization
391
+
392
+ See [AGENT_MODES.md](./AGENT_MODES.md) for detailed comparison and usage guide.
393
+
394
+ ### AI Providers
395
+
396
+ #### Claude (Anthropic)
397
+
398
+ ```bash
399
+ export ANTHROPIC_API_KEY=your-key-here
400
+ contractspec create --ai --provider claude
401
+ ```
402
+
403
+ #### OpenAI
404
+
405
+ ```bash
406
+ export OPENAI_API_KEY=your-key-here
407
+ contractspec create --ai --provider openai --model gpt-4o
408
+ ```
409
+
410
+ #### Ollama (Local)
411
+
412
+ Install Ollama and pull a model:
413
+
414
+ ```bash
415
+ ollama pull codellama
416
+ contractspec create --ai --provider ollama --model codellama
417
+ ```
418
+
419
+ #### Custom OpenAI-Compatible Endpoint
420
+
421
+ ```bash
422
+ contractspec create --ai --provider custom \
423
+ --endpoint https://your-llm.com/v1 \
424
+ --model your-model-name
425
+ ```
426
+
427
+ Set via environment:
428
+
429
+ ```bash
430
+ export CONTRACTSPEC_LLM_ENDPOINT=https://your-llm.com/v1
431
+ export CONTRACTSPEC_LLM_API_KEY=your-key
432
+ export CONTRACTSPEC_LLM_MODEL=your-model
433
+ ```
434
+
435
+ ### Supported Local Models (Ollama)
436
+
437
+ - `codellama` - Code generation (recommended)
438
+ - `llama3.1` - General purpose
439
+ - `mistral` - Fast, good quality
440
+ - `deepseek-coder` - Specialized for code
441
+
442
+ ## Examples
443
+
444
+ ### Create a Command Spec
445
+
446
+ ```bash
447
+ contractspec create --type operation
448
+ # Follow prompts:
449
+ # - Name: user.signup
450
+ # - Kind: command
451
+ # - Description: Start user signup flow
452
+ # etc.
453
+ ```
454
+
455
+ ### Build Handler from Spec
456
+
457
+ Given a spec file `src/contracts/signup.contracts.ts`:
458
+
459
+ ```bash
460
+ contractspec build src/contracts/signup.contracts.ts
461
+ # Generates:
462
+ # - src/handlers/signup.handler.ts
463
+ # - src/handlers/signup.handler.test.ts
464
+ ```
465
+
466
+ ### Multi-Provider Workflow
467
+
468
+ ```bash
469
+ # Draft with free local model
470
+ contractspec create --ai --provider ollama
471
+
472
+ # Refine with Claude for production
473
+ contractspec build src/contracts/draft.ts --provider claude
474
+ ```
475
+
476
+ ## Advanced Usage
477
+
478
+ ### Multi-Provider and Multi-Agent Workflows
479
+
480
+ Combine different providers and agents for optimal results:
481
+
482
+ ```bash
483
+ # Draft specs with free local model
484
+ contractspec create --ai --provider ollama --model codellama
485
+
486
+ # Generate production code with Claude Code agent
487
+ contractspec build src/contracts/user-signup.contracts.ts \
488
+ --agent-mode claude-code
489
+
490
+ # Validate implementation with AI
491
+ contractspec validate src/contracts/user-signup.contracts.ts \
492
+ --check-implementation \
493
+ --agent-mode claude-code
494
+
495
+ # Use OpenAI for algorithmic code
496
+ contractspec build src/contracts/search-algorithm.contracts.ts \
497
+ --agent-mode openai-codex
498
+ ```
499
+
500
+ ### Environment Variables
501
+
502
+ ```bash
503
+ # API Keys
504
+ export ANTHROPIC_API_KEY=your-key-here
505
+ export OPENAI_API_KEY=your-key-here
506
+
507
+ # Agent configuration
508
+ export CONTRACTSPEC_AGENT_MODE=claude-code
509
+ export CONTRACTSPEC_AI_PROVIDER=claude
510
+ export CONTRACTSPEC_AI_MODEL=claude-3-7-sonnet-20250219
511
+
512
+ # Custom provider
513
+ export CONTRACTSPEC_LLM_ENDPOINT=https://your-llm.com/v1
514
+ export CONTRACTSPEC_LLM_API_KEY=your-api-key
515
+ ```
516
+
517
+ ### CI/CD Integration
518
+
519
+ ```yaml
520
+ # .github/workflows/contracts.yml
521
+ name: Validate Contracts
522
+
523
+ on: [push, pull_request]
524
+
525
+ jobs:
526
+ validate:
527
+ runs-on: ubuntu-latest
528
+ steps:
529
+ - uses: actions/checkout@v6
530
+ - uses: oven-sh/setup-bun@v2
531
+ - run: bun install
532
+ - run: contractspec validate 'src/contracts/**/*.ts'
533
+ ```
534
+
535
+ ### Project Structure
536
+
537
+ Recommended organization:
538
+
539
+ ```
540
+ src/
541
+ ├── contracts/ # Contract specs
542
+ │ ├── events/
543
+ │ │ └── user-created.event.ts
544
+ │ ├── interactions/
545
+ │ │ ├── commands/
546
+ │ │ │ └── user-signup.contracts.ts
547
+ │ │ └── queries/
548
+ │ │ └── user-get-profile.contracts.ts
549
+ │ └── presentations/
550
+ │ └── user-profile-card.presentation.ts
551
+ ├── handlers/ # Generated handlers
552
+ ├── components/ # Generated components
553
+ └── forms/ # Generated forms
554
+ ```
555
+
556
+ ## Troubleshooting
557
+
558
+ ### "Provider not available" error
559
+
560
+ **Claude:**
561
+ ```bash
562
+ export ANTHROPIC_API_KEY=sk-ant-...
563
+ ```
564
+
565
+ **OpenAI:**
566
+ ```bash
567
+ export OPENAI_API_KEY=sk-...
568
+ ```
569
+
570
+ **Ollama:**
571
+ ```bash
572
+ # Install Ollama from https://ollama.ai
573
+ ollama serve
574
+ ollama pull codellama
575
+ ```
576
+
577
+ ### Slow generation
578
+
579
+ For faster local generation, use smaller models:
580
+ ```bash
581
+ contractspec create --ai --provider ollama --model codellama:7b
582
+ ```
583
+
584
+ For cloud, use faster models:
585
+ ```bash
586
+ contractspec build spec.ts --provider openai --model gpt-3.5-turbo
587
+ ```
588
+
589
+ ### Import errors in generated code
590
+
591
+ Make sure `@contractspec/lib.contracts` and `@contractspec/lib.schema` are installed:
592
+ ```bash
593
+ bunadd @contractspec/lib.contracts @contractspec/lib.schema
594
+ ```
595
+
596
+ ## Contributing
597
+
598
+ Contributions welcome! Please:
599
+
600
+ 1. Follow existing code style
601
+ 2. Add tests for new features
602
+ 3. Update documentation
603
+ 4. Ensure all tests pass: `buntest`
604
+
605
+ ## Agent Modes Deep Dive
606
+
607
+ ### When to Use Each Mode
608
+
609
+ **Simple Mode** - Default, good for:
610
+ - Rapid prototyping
611
+ - Basic implementations
612
+ - Quick iterations
613
+ - CI/CD pipelines (fast)
614
+
615
+ **Cursor Mode** - Best for:
616
+ - Working in Windsurf/Cursor IDE
617
+ - Complex, iterative development
618
+ - Context-aware code generation
619
+
620
+ **Claude Code Mode** - Best for:
621
+ - Production-quality implementations
622
+ - Critical business logic
623
+ - Comprehensive code validation
624
+ - Detailed code reviews
625
+
626
+ **OpenAI Codex Mode** - Best for:
627
+ - Algorithmic problems
628
+ - Performance optimization
629
+ - Mathematical computations
630
+ - Complex data transformations
631
+
632
+ For more details, see [AGENT_MODES.md](./AGENT_MODES.md).
633
+
634
+ ## Roadmap
635
+
636
+ - [x] AI-powered code generation
637
+ - [x] Multiple agent modes (simple, cursor, claude-code, openai-codex)
638
+ - [x] AI-powered implementation validation
639
+ - [x] Contract listing and discovery (`contractspec list`)
640
+ - [x] Watch mode for auto-regeneration (`contractspec watch`)
641
+ - [x] Multi-surface synchronization (`contractspec sync`)
642
+ - [x] Cleanup utilities (`contractspec clean`)
643
+ - [x] Dependency analysis (`contractspec deps`)
644
+ - [x] Contract diffing and comparison (`contractspec diff`)
645
+ - [x] CI/CD integration (`contractspec ci`) with SARIF/JSON output
646
+ - [x] GitHub Action for CI/CD
647
+ - [ ] Form spec generation
648
+ - [ ] Feature spec bundling
649
+ - [ ] Handler signature checking in validation
650
+ - [ ] Test coverage validation
651
+ - [ ] Interactive spec editing
652
+ - [ ] GraphQL schema generation
653
+ - [ ] OpenAPI/Swagger export
654
+
655
+ ## License
656
+
657
+ MIT
658
+