sourcebook 0.3.0 → 0.4.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/LICENSE +65 -21
- package/README.md +96 -40
- package/dist/auth/license.d.ts +25 -0
- package/dist/auth/license.js +130 -0
- package/dist/cli.js +5 -0
- package/dist/commands/activate.d.ts +1 -0
- package/dist/commands/activate.js +38 -0
- package/dist/commands/init.js +10 -0
- package/dist/commands/update.js +2 -0
- package/dist/generators/agents.d.ts +7 -0
- package/dist/generators/agents.js +119 -0
- package/dist/scanner/patterns.js +258 -0
- package/package.json +7 -6
package/LICENSE
CHANGED
|
@@ -1,21 +1,65 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
Licensor: maroond labs
|
|
4
|
+
|
|
5
|
+
Licensed Work: sourcebook
|
|
6
|
+
The Licensed Work is (c) 2026 maroond labs
|
|
7
|
+
|
|
8
|
+
Additional Use Grant: You may make production use of the Licensed Work,
|
|
9
|
+
provided your use does not include offering the
|
|
10
|
+
Licensed Work to third parties as a commercial
|
|
11
|
+
code analysis or context generation service.
|
|
12
|
+
|
|
13
|
+
Change Date: 2030-03-25
|
|
14
|
+
|
|
15
|
+
Change License: MIT
|
|
16
|
+
|
|
17
|
+
For information about alternative licensing arrangements, contact
|
|
18
|
+
roy@maroond.ai.
|
|
19
|
+
|
|
20
|
+
Notice
|
|
21
|
+
|
|
22
|
+
Business Source License 1.1
|
|
23
|
+
|
|
24
|
+
Terms
|
|
25
|
+
|
|
26
|
+
The Licensor hereby grants you the right to copy, modify, create
|
|
27
|
+
derivative works, redistribute, and make non-production use of the
|
|
28
|
+
Licensed Work. The Licensor may make an Additional Use Grant, above,
|
|
29
|
+
permitting limited production use.
|
|
30
|
+
|
|
31
|
+
Effective on the Change Date, or the fourth anniversary of the first
|
|
32
|
+
publicly available distribution of a specific version of the Licensed
|
|
33
|
+
Work, whichever comes first, the Licensor hereby grants you rights
|
|
34
|
+
under the terms of the Change License, and the rights granted in the
|
|
35
|
+
paragraph above terminate.
|
|
36
|
+
|
|
37
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
38
|
+
currently in effect as described in this License, you must purchase a
|
|
39
|
+
commercial license from the Licensor, its affiliated entities, or
|
|
40
|
+
authorized resellers, or you must refrain from using the Licensed Work.
|
|
41
|
+
|
|
42
|
+
All copies of the original and modified Licensed Work, and derivative
|
|
43
|
+
works of the Licensed Work, are subject to this License. This License
|
|
44
|
+
applies separately for each version of the Licensed Work and the
|
|
45
|
+
Change Date may vary for each version of the Licensed Work released by
|
|
46
|
+
Licensor.
|
|
47
|
+
|
|
48
|
+
You must conspicuously display this License on each original or
|
|
49
|
+
modified copy of the Licensed Work. If you receive the Licensed Work
|
|
50
|
+
in original or modified form from a third party, the terms and
|
|
51
|
+
conditions set forth in this License apply to your use of that work.
|
|
52
|
+
|
|
53
|
+
Any use of the Licensed Work in violation of this License will
|
|
54
|
+
automatically terminate your rights under this License for the current
|
|
55
|
+
and all other versions of the Licensed Work.
|
|
56
|
+
|
|
57
|
+
This License does not grant you any right in any trademark or logo of
|
|
58
|
+
Licensor or its affiliates (provided that you may use a trademark or
|
|
59
|
+
logo of Licensor as expressly required by this License).
|
|
60
|
+
|
|
61
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS
|
|
62
|
+
PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES
|
|
63
|
+
AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION)
|
|
64
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
|
|
65
|
+
NON-INFRINGEMENT, AND TITLE.
|
package/README.md
CHANGED
|
@@ -27,10 +27,11 @@ sourcebook inverts the typical approach: instead of dumping everything, it extra
|
|
|
27
27
|
## What It Finds
|
|
28
28
|
|
|
29
29
|
- **Import graph + PageRank** — ranks files by structural importance, identifies hub files with the widest blast radius
|
|
30
|
-
- **Git history forensics** — reverted commits (
|
|
31
|
-
- **Convention detection** — naming patterns, export style, import organization, barrel exports, path aliases
|
|
32
|
-
- **Framework detection** — Next.js, Expo, Supabase, Tailwind, Express, TypeScript
|
|
30
|
+
- **Git history forensics** — reverted commits ("don't do this" signals), co-change coupling (invisible dependencies), rapid re-edits (code that was hard to get right), anti-patterns from abandoned approaches
|
|
31
|
+
- **Convention detection** — naming patterns, export style, import organization, barrel exports, path aliases, type hint usage, error handling style
|
|
32
|
+
- **Framework detection** — Next.js, Expo, Supabase, Tailwind, Express, TypeScript, Django, FastAPI, Flask, Go (Gin, Echo, Fiber)
|
|
33
33
|
- **Context-rot-aware formatting** — critical constraints at the top, reference info in the middle, action prompts at the bottom (optimized for LLM attention patterns)
|
|
34
|
+
- **Smart budget enforcement** — when context exceeds your token budget, drops low-priority sections first (keeps critical constraints always)
|
|
34
35
|
|
|
35
36
|
## Quick Start
|
|
36
37
|
|
|
@@ -38,54 +39,104 @@ sourcebook inverts the typical approach: instead of dumping everything, it extra
|
|
|
38
39
|
# Generate CLAUDE.md for your project
|
|
39
40
|
npx sourcebook init
|
|
40
41
|
|
|
41
|
-
#
|
|
42
|
+
# Generate for a specific tool
|
|
42
43
|
npx sourcebook init --format claude # CLAUDE.md (default)
|
|
43
44
|
npx sourcebook init --format cursor # .cursor/rules/sourcebook.mdc + .cursorrules
|
|
44
45
|
npx sourcebook init --format copilot # .github/copilot-instructions.md
|
|
45
46
|
npx sourcebook init --format all # All of the above
|
|
47
|
+
|
|
48
|
+
# Re-analyze while preserving your manual edits
|
|
49
|
+
npx sourcebook update
|
|
50
|
+
|
|
51
|
+
# See what changed since last generation (exit code 1 = changes found)
|
|
52
|
+
npx sourcebook diff
|
|
53
|
+
|
|
54
|
+
# Limit output to a token budget (drops low-priority sections first)
|
|
55
|
+
npx sourcebook init --budget 1000
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Commands
|
|
59
|
+
|
|
60
|
+
| Command | What it does |
|
|
61
|
+
|---------|-------------|
|
|
62
|
+
| `sourcebook init` | Analyze codebase and generate context files |
|
|
63
|
+
| `sourcebook update` | Re-analyze while preserving sections you added manually |
|
|
64
|
+
| `sourcebook diff` | Show what would change without writing files (exit code 1 if changes found — useful for CI) |
|
|
65
|
+
|
|
66
|
+
### Options
|
|
67
|
+
|
|
68
|
+
| Flag | Description | Default |
|
|
69
|
+
|------|-------------|---------|
|
|
70
|
+
| `-d, --dir <path>` | Target directory | `.` |
|
|
71
|
+
| `-f, --format <formats>` | Output formats: `claude`, `cursor`, `copilot`, `all` | `claude` |
|
|
72
|
+
| `--budget <tokens>` | Max token budget for output | `4000` |
|
|
73
|
+
| `--dry-run` | Preview findings without writing files | — |
|
|
74
|
+
|
|
75
|
+
## Language Support
|
|
76
|
+
|
|
77
|
+
| Language | Framework Detection | Convention Detection | Import Graph | Git Analysis |
|
|
78
|
+
|----------|:------------------:|:-------------------:|:------------:|:------------:|
|
|
79
|
+
| TypeScript/JavaScript | Next.js, Expo, Vite, React, Express, Tailwind, Supabase | Barrel exports, path aliases, export style, error handling | Full | Full |
|
|
80
|
+
| Python | Django, FastAPI, Flask, pytest | Type hints, `__init__.py` barrels | Full | Full |
|
|
81
|
+
| Go | Gin, Echo, Fiber | Module path, cmd/pkg/internal layout, error wrapping, interfaces | Full | Full |
|
|
82
|
+
|
|
83
|
+
## GitHub Action
|
|
84
|
+
|
|
85
|
+
Auto-update context files on every merge:
|
|
86
|
+
|
|
87
|
+
```yaml
|
|
88
|
+
# .github/workflows/sourcebook.yml
|
|
89
|
+
name: Update context files
|
|
90
|
+
on:
|
|
91
|
+
push:
|
|
92
|
+
branches: [main]
|
|
93
|
+
|
|
94
|
+
jobs:
|
|
95
|
+
sourcebook:
|
|
96
|
+
runs-on: ubuntu-latest
|
|
97
|
+
steps:
|
|
98
|
+
- uses: actions/checkout@v4
|
|
99
|
+
- uses: maroondlabs/sourcebook@main
|
|
100
|
+
with:
|
|
101
|
+
format: all
|
|
46
102
|
```
|
|
47
103
|
|
|
48
104
|
## Example Output
|
|
49
105
|
|
|
50
|
-
Running
|
|
106
|
+
Running on [cal.com](https://github.com/calcom/cal.com) (10,456 files):
|
|
51
107
|
|
|
52
108
|
```
|
|
53
|
-
sourcebook
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
Path aliases: @/ prefix
|
|
74
|
-
Named exports preferred (25:6 ratio)
|
|
75
|
-
Conventional Commits: yes
|
|
76
|
-
|
|
77
|
-
Generated: CLAUDE.md (15 findings, 1.2K tokens)
|
|
78
|
-
Done in 2.8s
|
|
109
|
+
sourcebook
|
|
110
|
+
Extracting repo truths...
|
|
111
|
+
|
|
112
|
+
✓ Scanned project structure
|
|
113
|
+
10,456 files, 3 frameworks detected
|
|
114
|
+
✓ Extracted 11 findings
|
|
115
|
+
|
|
116
|
+
● Core modules: types.ts imported by 183 files — widest blast radius
|
|
117
|
+
● Circular deps: bookingScenario.ts ↔ getMockRequestData.ts
|
|
118
|
+
● Co-change: auth/provider.ts ↔ middleware/session.ts (88% correlation)
|
|
119
|
+
● Dead code: 1,907 orphan files detected
|
|
120
|
+
● Conventions: named exports preferred (26:2 ratio)
|
|
121
|
+
● Barrel exports: 40 index.ts re-export files
|
|
122
|
+
● Commit style: Conventional Commits (feat/fix/docs)
|
|
123
|
+
|
|
124
|
+
✓ Wrote CLAUDE.md
|
|
125
|
+
✓ Wrote .cursor/rules/sourcebook.mdc
|
|
126
|
+
✓ Wrote .github/copilot-instructions.md
|
|
127
|
+
|
|
128
|
+
Done in 3.1s
|
|
79
129
|
```
|
|
80
130
|
|
|
81
131
|
## How It Works
|
|
82
132
|
|
|
83
|
-
sourcebook runs
|
|
133
|
+
sourcebook runs five analysis passes, all deterministic and local — no LLM, no API keys, no network calls:
|
|
84
134
|
|
|
85
135
|
1. **Static analysis** — framework detection, build commands, project structure, environment variables
|
|
86
136
|
2. **Import graph** — builds a directed graph of all imports, runs PageRank to find the most structurally important files
|
|
87
|
-
3. **Git forensics** — mines commit history for reverts, co-change
|
|
88
|
-
4. **Convention inference** — samples source files to detect naming, import, export,
|
|
137
|
+
3. **Git forensics** — mines commit history for reverts, anti-patterns, co-change coupling, churn hotspots, and abandoned approaches
|
|
138
|
+
4. **Convention inference** — samples source files to detect naming, import, export, error handling, and type annotation patterns
|
|
139
|
+
5. **Budget enforcement** — if output exceeds your token budget, intelligently drops low-priority sections (supplementary findings first, critical constraints never)
|
|
89
140
|
|
|
90
141
|
Then applies a **discoverability filter**: for every finding, asks "can an agent figure this out by reading the code?" If yes, drops it. Only non-discoverable information makes it to the output.
|
|
91
142
|
|
|
@@ -93,14 +144,19 @@ Output is formatted for **context-rot resistance** — critical constraints go a
|
|
|
93
144
|
|
|
94
145
|
## Roadmap
|
|
95
146
|
|
|
96
|
-
- [x] `.cursor/rules/sourcebook.mdc` + legacy `.cursorrules` output
|
|
97
|
-
- [x] `.github/copilot-instructions.md` output
|
|
98
|
-
- [
|
|
99
|
-
- [
|
|
147
|
+
- [x] `.cursor/rules/sourcebook.mdc` + legacy `.cursorrules` output
|
|
148
|
+
- [x] `.github/copilot-instructions.md` output
|
|
149
|
+
- [x] `sourcebook update` — re-analyze while preserving manual edits
|
|
150
|
+
- [x] `sourcebook diff` — show what changed (CI-friendly exit codes)
|
|
151
|
+
- [x] `--budget <tokens>` — smart PageRank-based prioritization
|
|
152
|
+
- [x] Anti-pattern detection from reverted commits and deleted files
|
|
153
|
+
- [x] Python support (Django, FastAPI, Flask, pytest)
|
|
154
|
+
- [x] Go support (Gin, Echo, Fiber, module layout)
|
|
155
|
+
- [x] GitHub Action for CI
|
|
100
156
|
- [ ] Framework knowledge packs (community-contributed)
|
|
101
157
|
- [ ] Tree-sitter AST parsing for deeper convention detection
|
|
102
|
-
- [ ] GitHub Action for CI (auto-update context on merge)
|
|
103
158
|
- [ ] `sourcebook serve` — MCP server mode
|
|
159
|
+
- [ ] Hosted dashboard with context quality scores
|
|
104
160
|
|
|
105
161
|
## Research Foundation
|
|
106
162
|
|
|
@@ -112,4 +168,4 @@ Built on findings from:
|
|
|
112
168
|
|
|
113
169
|
## License
|
|
114
170
|
|
|
115
|
-
MIT
|
|
171
|
+
BSL-1.1 — source-available, free to use, cannot be offered as a hosted service. Converts to MIT on 2030-03-25. See [LICENSE](./LICENSE) for details.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface LicenseInfo {
|
|
2
|
+
valid: boolean;
|
|
3
|
+
tier: "free" | "pro" | "team";
|
|
4
|
+
email?: string;
|
|
5
|
+
expiresAt?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Check if the user has a valid Pro or Team license.
|
|
9
|
+
* License keys are stored in ~/.sourcebook/license.key
|
|
10
|
+
*
|
|
11
|
+
* Flow:
|
|
12
|
+
* 1. Read key from disk
|
|
13
|
+
* 2. Validate against API (with 5s timeout)
|
|
14
|
+
* 3. Cache validation result for 24h to avoid hitting API every run
|
|
15
|
+
*/
|
|
16
|
+
export declare function checkLicense(): Promise<LicenseInfo>;
|
|
17
|
+
/**
|
|
18
|
+
* Save a license key to disk.
|
|
19
|
+
*/
|
|
20
|
+
export declare function saveLicenseKey(key: string): void;
|
|
21
|
+
/**
|
|
22
|
+
* Gate a feature behind Pro license.
|
|
23
|
+
* Prints upgrade message and exits if not licensed.
|
|
24
|
+
*/
|
|
25
|
+
export declare function requirePro(feature: string): Promise<void>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
const LICENSE_DIR = path.join(os.homedir(), ".sourcebook");
|
|
6
|
+
const LICENSE_FILE = path.join(LICENSE_DIR, "license.key");
|
|
7
|
+
const VALIDATION_ENDPOINT = "https://sourcebook.run/api/validate";
|
|
8
|
+
/**
|
|
9
|
+
* Check if the user has a valid Pro or Team license.
|
|
10
|
+
* License keys are stored in ~/.sourcebook/license.key
|
|
11
|
+
*
|
|
12
|
+
* Flow:
|
|
13
|
+
* 1. Read key from disk
|
|
14
|
+
* 2. Validate against API (with 5s timeout)
|
|
15
|
+
* 3. Cache validation result for 24h to avoid hitting API every run
|
|
16
|
+
*/
|
|
17
|
+
export async function checkLicense() {
|
|
18
|
+
const key = readLicenseKey();
|
|
19
|
+
if (!key) {
|
|
20
|
+
return { valid: false, tier: "free" };
|
|
21
|
+
}
|
|
22
|
+
// Check cache first (avoid hitting API every run)
|
|
23
|
+
const cached = readCache();
|
|
24
|
+
if (cached && cached.key === key && !isCacheExpired(cached.timestamp)) {
|
|
25
|
+
return cached.info;
|
|
26
|
+
}
|
|
27
|
+
// Validate against API
|
|
28
|
+
try {
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
31
|
+
const response = await fetch(VALIDATION_ENDPOINT, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: { "Content-Type": "application/json" },
|
|
34
|
+
body: JSON.stringify({ key }),
|
|
35
|
+
signal: controller.signal,
|
|
36
|
+
});
|
|
37
|
+
clearTimeout(timeout);
|
|
38
|
+
if (response.ok) {
|
|
39
|
+
const data = await response.json();
|
|
40
|
+
writeCache(key, data);
|
|
41
|
+
return data;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// Network error or timeout — fall back to cache or offline validation
|
|
46
|
+
if (cached && cached.key === key) {
|
|
47
|
+
return cached.info;
|
|
48
|
+
}
|
|
49
|
+
// Offline grace: if key looks valid (format check), allow Pro for 7 days
|
|
50
|
+
if (isValidKeyFormat(key)) {
|
|
51
|
+
return { valid: true, tier: "pro" };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return { valid: false, tier: "free" };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Save a license key to disk.
|
|
58
|
+
*/
|
|
59
|
+
export function saveLicenseKey(key) {
|
|
60
|
+
if (!fs.existsSync(LICENSE_DIR)) {
|
|
61
|
+
fs.mkdirSync(LICENSE_DIR, { recursive: true });
|
|
62
|
+
}
|
|
63
|
+
fs.writeFileSync(LICENSE_FILE, key.trim(), "utf-8");
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Read the license key from disk.
|
|
67
|
+
*/
|
|
68
|
+
function readLicenseKey() {
|
|
69
|
+
try {
|
|
70
|
+
const key = fs.readFileSync(LICENSE_FILE, "utf-8").trim();
|
|
71
|
+
return key || null;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* License key format: sb_pro_<32 hex chars> or sb_team_<32 hex chars>
|
|
79
|
+
*/
|
|
80
|
+
function isValidKeyFormat(key) {
|
|
81
|
+
return /^sb_(pro|team)_[a-f0-9]{32}$/.test(key);
|
|
82
|
+
}
|
|
83
|
+
const CACHE_FILE = path.join(LICENSE_DIR, ".cache.json");
|
|
84
|
+
const CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
|
85
|
+
function readCache() {
|
|
86
|
+
try {
|
|
87
|
+
const data = JSON.parse(fs.readFileSync(CACHE_FILE, "utf-8"));
|
|
88
|
+
return data;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function writeCache(key, info) {
|
|
95
|
+
if (!fs.existsSync(LICENSE_DIR)) {
|
|
96
|
+
fs.mkdirSync(LICENSE_DIR, { recursive: true });
|
|
97
|
+
}
|
|
98
|
+
const entry = { key, info, timestamp: Date.now() };
|
|
99
|
+
fs.writeFileSync(CACHE_FILE, JSON.stringify(entry), "utf-8");
|
|
100
|
+
}
|
|
101
|
+
function isCacheExpired(timestamp) {
|
|
102
|
+
return Date.now() - timestamp > CACHE_TTL_MS;
|
|
103
|
+
}
|
|
104
|
+
// --- Gate ---
|
|
105
|
+
/**
|
|
106
|
+
* Gate a feature behind Pro license.
|
|
107
|
+
* Prints upgrade message and exits if not licensed.
|
|
108
|
+
*/
|
|
109
|
+
export async function requirePro(feature) {
|
|
110
|
+
const license = await checkLicense();
|
|
111
|
+
if (license.tier === "pro" || license.tier === "team") {
|
|
112
|
+
return; // Licensed, proceed
|
|
113
|
+
}
|
|
114
|
+
console.log("");
|
|
115
|
+
console.log(chalk.yellow("⚡") +
|
|
116
|
+
chalk.bold(` ${feature} requires sourcebook Pro`));
|
|
117
|
+
console.log("");
|
|
118
|
+
console.log(chalk.dim(" sourcebook Pro includes:"));
|
|
119
|
+
console.log(chalk.dim(" · sourcebook update (preserve manual edits)"));
|
|
120
|
+
console.log(chalk.dim(" · sourcebook serve (MCP server mode)"));
|
|
121
|
+
console.log(chalk.dim(" · sourcebook watch (auto-regenerate on changes)"));
|
|
122
|
+
console.log(chalk.dim(" · Web demo (shareable analysis links)"));
|
|
123
|
+
console.log(chalk.dim(" · Priority language support"));
|
|
124
|
+
console.log("");
|
|
125
|
+
console.log(` ${chalk.bold("$19/mo")} → ${chalk.underline("https://sourcebook.run/pro")}`);
|
|
126
|
+
console.log("");
|
|
127
|
+
console.log(chalk.dim(" Already have a key? Run: sourcebook activate <key>"));
|
|
128
|
+
console.log("");
|
|
129
|
+
process.exit(0);
|
|
130
|
+
}
|
package/dist/cli.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Command } from "commander";
|
|
|
3
3
|
import { init } from "./commands/init.js";
|
|
4
4
|
import { update } from "./commands/update.js";
|
|
5
5
|
import { diff } from "./commands/diff.js";
|
|
6
|
+
import { activate } from "./commands/activate.js";
|
|
6
7
|
const program = new Command();
|
|
7
8
|
program
|
|
8
9
|
.name("sourcebook")
|
|
@@ -30,4 +31,8 @@ program
|
|
|
30
31
|
.option("-f, --format <formats>", "Output format to diff (claude,cursor,copilot)", "claude")
|
|
31
32
|
.option("--budget <tokens>", "Max token budget for generated context", "4000")
|
|
32
33
|
.action(diff);
|
|
34
|
+
program
|
|
35
|
+
.command("activate <key>")
|
|
36
|
+
.description("Activate a Pro or Team license key")
|
|
37
|
+
.action(activate);
|
|
33
38
|
program.parse();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function activate(key: string): Promise<void>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { saveLicenseKey, checkLicense } from "../auth/license.js";
|
|
3
|
+
export async function activate(key) {
|
|
4
|
+
if (!key || key.trim().length === 0) {
|
|
5
|
+
console.log(chalk.red("\nNo license key provided."));
|
|
6
|
+
console.log(chalk.dim("Usage: sourcebook activate <key>"));
|
|
7
|
+
console.log(chalk.dim("Get a key at https://sourcebook.run/pro\n"));
|
|
8
|
+
process.exit(1);
|
|
9
|
+
}
|
|
10
|
+
console.log(chalk.bold("\nsourcebook activate"));
|
|
11
|
+
console.log(chalk.dim("Validating license key...\n"));
|
|
12
|
+
// Save key first
|
|
13
|
+
saveLicenseKey(key);
|
|
14
|
+
// Validate it
|
|
15
|
+
const license = await checkLicense();
|
|
16
|
+
if (license.tier === "pro" || license.tier === "team") {
|
|
17
|
+
console.log(chalk.green("✓") +
|
|
18
|
+
chalk.bold(` License activated — ${license.tier} tier`));
|
|
19
|
+
if (license.email) {
|
|
20
|
+
console.log(chalk.dim(` Licensed to: ${license.email}`));
|
|
21
|
+
}
|
|
22
|
+
if (license.expiresAt) {
|
|
23
|
+
console.log(chalk.dim(` Expires: ${license.expiresAt}`));
|
|
24
|
+
}
|
|
25
|
+
console.log("");
|
|
26
|
+
console.log(chalk.dim(" You now have access to:"));
|
|
27
|
+
console.log(chalk.dim(" · sourcebook update"));
|
|
28
|
+
console.log(chalk.dim(" · sourcebook serve"));
|
|
29
|
+
console.log(chalk.dim(" · sourcebook watch"));
|
|
30
|
+
console.log("");
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
console.log(chalk.yellow("⚠") +
|
|
34
|
+
" License key saved but could not be validated.");
|
|
35
|
+
console.log(chalk.dim(" This may be a network issue. The key will be re-validated on next use."));
|
|
36
|
+
console.log(chalk.dim(" If the problem persists, contact roy@maroond.ai\n"));
|
|
37
|
+
}
|
|
38
|
+
}
|
package/dist/commands/init.js
CHANGED
|
@@ -4,6 +4,7 @@ import { scanProject } from "../scanner/index.js";
|
|
|
4
4
|
import { generateClaude } from "../generators/claude.js";
|
|
5
5
|
import { generateCursor, generateCursorLegacy } from "../generators/cursor.js";
|
|
6
6
|
import { generateCopilot } from "../generators/copilot.js";
|
|
7
|
+
import { generateAgents } from "../generators/agents.js";
|
|
7
8
|
import { writeOutput } from "../utils/output.js";
|
|
8
9
|
export async function init(options) {
|
|
9
10
|
const targetDir = path.resolve(options.dir);
|
|
@@ -67,6 +68,12 @@ export async function init(options) {
|
|
|
67
68
|
console.log(chalk.green("✓") + " Wrote .github/copilot-instructions.md");
|
|
68
69
|
break;
|
|
69
70
|
}
|
|
71
|
+
case "agents": {
|
|
72
|
+
const agentsContent = generateAgents(scan, budget);
|
|
73
|
+
await writeOutput(targetDir, "AGENTS.md", agentsContent);
|
|
74
|
+
console.log(chalk.green("✓") + " Wrote AGENTS.md");
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
70
77
|
case "all": {
|
|
71
78
|
const claudeAll = generateClaude(scan, budget);
|
|
72
79
|
await writeOutput(targetDir, "CLAUDE.md", claudeAll);
|
|
@@ -80,6 +87,9 @@ export async function init(options) {
|
|
|
80
87
|
const copilotAll = generateCopilot(scan, budget);
|
|
81
88
|
await writeOutput(targetDir, ".github/copilot-instructions.md", copilotAll);
|
|
82
89
|
console.log(chalk.green("✓") + " Wrote .github/copilot-instructions.md");
|
|
90
|
+
const agentsAll = generateAgents(scan, budget);
|
|
91
|
+
await writeOutput(targetDir, "AGENTS.md", agentsAll);
|
|
92
|
+
console.log(chalk.green("✓") + " Wrote AGENTS.md");
|
|
83
93
|
break;
|
|
84
94
|
}
|
|
85
95
|
default:
|
package/dist/commands/update.js
CHANGED
|
@@ -6,6 +6,7 @@ import { generateClaude } from "../generators/claude.js";
|
|
|
6
6
|
import { generateCursor, generateCursorLegacy } from "../generators/cursor.js";
|
|
7
7
|
import { generateCopilot } from "../generators/copilot.js";
|
|
8
8
|
import { writeOutput } from "../utils/output.js";
|
|
9
|
+
import { requirePro } from "../auth/license.js";
|
|
9
10
|
// Headers that sourcebook generates — anything else is user-added
|
|
10
11
|
const SOURCEBOOK_HEADERS = new Set([
|
|
11
12
|
"CLAUDE.md",
|
|
@@ -39,6 +40,7 @@ const SOURCEBOOK_HEADERS = new Set([
|
|
|
39
40
|
* 5. Replace sourcebook sections, keep manual sections in their original positions
|
|
40
41
|
*/
|
|
41
42
|
export async function update(options) {
|
|
43
|
+
await requirePro("sourcebook update");
|
|
42
44
|
const targetDir = path.resolve(options.dir);
|
|
43
45
|
const formats = options.format.split(",").map((f) => f.trim());
|
|
44
46
|
const budget = parseInt(options.budget, 10);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ProjectScan } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generate an AGENTS.md file from scan results.
|
|
4
|
+
* Used by GitHub Copilot, OpenAI Codex, and other AGENTS.md-aware tools.
|
|
5
|
+
* Format follows the AGENTS.md spec: markdown with directives for agent behavior.
|
|
6
|
+
*/
|
|
7
|
+
export declare function generateAgents(scan: ProjectScan, budget: number): string;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { hasCommands, categorizeFindings, enforceTokenBudget, } from "./shared.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generate an AGENTS.md file from scan results.
|
|
4
|
+
* Used by GitHub Copilot, OpenAI Codex, and other AGENTS.md-aware tools.
|
|
5
|
+
* Format follows the AGENTS.md spec: markdown with directives for agent behavior.
|
|
6
|
+
*/
|
|
7
|
+
export function generateAgents(scan, budget) {
|
|
8
|
+
const { critical, important, supplementary } = categorizeFindings(scan.findings);
|
|
9
|
+
const sections = [];
|
|
10
|
+
sections.push({
|
|
11
|
+
key: "header",
|
|
12
|
+
content: [
|
|
13
|
+
"# AGENTS.md",
|
|
14
|
+
"",
|
|
15
|
+
"Agent instructions for this repository.",
|
|
16
|
+
"Generated by [sourcebook](https://github.com/maroondlabs/sourcebook). Review and edit — the best context comes from human + machine together.",
|
|
17
|
+
"",
|
|
18
|
+
].join("\n"),
|
|
19
|
+
priority: 100,
|
|
20
|
+
});
|
|
21
|
+
// Commands
|
|
22
|
+
if (hasCommands(scan.commands)) {
|
|
23
|
+
const lines = ["## Commands", ""];
|
|
24
|
+
if (scan.commands.dev)
|
|
25
|
+
lines.push(`- **Dev:** \`${scan.commands.dev}\``);
|
|
26
|
+
if (scan.commands.build)
|
|
27
|
+
lines.push(`- **Build:** \`${scan.commands.build}\``);
|
|
28
|
+
if (scan.commands.test)
|
|
29
|
+
lines.push(`- **Test:** \`${scan.commands.test}\``);
|
|
30
|
+
if (scan.commands.lint)
|
|
31
|
+
lines.push(`- **Lint:** \`${scan.commands.lint}\``);
|
|
32
|
+
for (const [name, cmd] of Object.entries(scan.commands)) {
|
|
33
|
+
if (cmd && !["dev", "build", "test", "lint", "start"].includes(name)) {
|
|
34
|
+
lines.push(`- **${name}:** \`${cmd}\``);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
lines.push("");
|
|
38
|
+
sections.push({ key: "commands", content: lines.join("\n"), priority: 95 });
|
|
39
|
+
}
|
|
40
|
+
// Critical constraints as agent directives
|
|
41
|
+
if (critical.length > 0) {
|
|
42
|
+
const lines = [
|
|
43
|
+
"## Constraints",
|
|
44
|
+
"",
|
|
45
|
+
"These constraints MUST be followed when modifying this codebase:",
|
|
46
|
+
"",
|
|
47
|
+
];
|
|
48
|
+
for (const finding of critical) {
|
|
49
|
+
lines.push(`- **${finding.category}:** ${finding.description}`);
|
|
50
|
+
}
|
|
51
|
+
lines.push("");
|
|
52
|
+
sections.push({ key: "critical", content: lines.join("\n"), priority: 90 });
|
|
53
|
+
}
|
|
54
|
+
// Stack
|
|
55
|
+
if (scan.frameworks.length > 0) {
|
|
56
|
+
sections.push({
|
|
57
|
+
key: "stack",
|
|
58
|
+
content: [
|
|
59
|
+
"## Stack",
|
|
60
|
+
"",
|
|
61
|
+
scan.frameworks.join(", "),
|
|
62
|
+
"",
|
|
63
|
+
].join("\n"),
|
|
64
|
+
priority: 50,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// Core modules
|
|
68
|
+
if (scan.rankedFiles && scan.rankedFiles.length > 0) {
|
|
69
|
+
const lines = [
|
|
70
|
+
"## Core Modules (by structural importance)",
|
|
71
|
+
"",
|
|
72
|
+
];
|
|
73
|
+
for (const { file, score } of scan.rankedFiles.slice(0, 5)) {
|
|
74
|
+
lines.push(`- \`${file}\``);
|
|
75
|
+
}
|
|
76
|
+
lines.push("");
|
|
77
|
+
sections.push({ key: "core_modules", content: lines.join("\n"), priority: 60 });
|
|
78
|
+
}
|
|
79
|
+
// Conventions
|
|
80
|
+
if (important.length > 0) {
|
|
81
|
+
const lines = [
|
|
82
|
+
"## Conventions",
|
|
83
|
+
"",
|
|
84
|
+
];
|
|
85
|
+
for (const finding of important) {
|
|
86
|
+
lines.push(`- **${finding.category}:** ${finding.description}`);
|
|
87
|
+
}
|
|
88
|
+
lines.push("");
|
|
89
|
+
sections.push({ key: "conventions", content: lines.join("\n"), priority: 30 });
|
|
90
|
+
}
|
|
91
|
+
// Additional context
|
|
92
|
+
if (supplementary.length > 0) {
|
|
93
|
+
const lines = ["## Additional Context", ""];
|
|
94
|
+
for (const finding of supplementary) {
|
|
95
|
+
lines.push(`- ${finding.description}`);
|
|
96
|
+
}
|
|
97
|
+
lines.push("");
|
|
98
|
+
sections.push({ key: "supplementary", content: lines.join("\n"), priority: 20 });
|
|
99
|
+
}
|
|
100
|
+
// Manual section prompt
|
|
101
|
+
sections.push({
|
|
102
|
+
key: "manual",
|
|
103
|
+
content: [
|
|
104
|
+
"## What to Add Manually",
|
|
105
|
+
"",
|
|
106
|
+
"The most valuable context is what only you know. Add:",
|
|
107
|
+
"",
|
|
108
|
+
"- Architectural decisions and why they were made",
|
|
109
|
+
"- Past incidents that shaped current conventions",
|
|
110
|
+
"- Deprecated patterns to avoid in new code",
|
|
111
|
+
"- Domain-specific rules or terminology",
|
|
112
|
+
"- Environment setup beyond what .env.example shows",
|
|
113
|
+
"",
|
|
114
|
+
].join("\n"),
|
|
115
|
+
priority: 10,
|
|
116
|
+
});
|
|
117
|
+
const kept = enforceTokenBudget(sections, budget);
|
|
118
|
+
return kept.join("\n");
|
|
119
|
+
}
|
package/dist/scanner/patterns.js
CHANGED
|
@@ -39,6 +39,8 @@ export async function detectPatterns(dir, files, frameworks) {
|
|
|
39
39
|
findings.push(...detectPythonConventions(files, fileContents));
|
|
40
40
|
// --- Go conventions ---
|
|
41
41
|
findings.push(...detectGoConventions(files, fileContents));
|
|
42
|
+
// --- Dominant API/usage patterns ---
|
|
43
|
+
findings.push(...detectDominantPatterns(dir, files, fileContents, frameworks));
|
|
42
44
|
// Filter out discoverable findings
|
|
43
45
|
return findings.filter((f) => !f.discoverable);
|
|
44
46
|
}
|
|
@@ -255,6 +257,262 @@ function detectGoConventions(files, contents) {
|
|
|
255
257
|
}
|
|
256
258
|
return findings;
|
|
257
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* Detect dominant API/usage patterns — the conventions humans naturally
|
|
262
|
+
* put in handwritten briefs but agents can't infer from structure alone.
|
|
263
|
+
*
|
|
264
|
+
* This closes the gap between sourcebook and handwritten context.
|
|
265
|
+
*/
|
|
266
|
+
function detectDominantPatterns(dir, files, contents, frameworks) {
|
|
267
|
+
const findings = [];
|
|
268
|
+
// Read MORE files for pattern detection — we need a wider sample
|
|
269
|
+
// to detect dominant patterns reliably
|
|
270
|
+
const allSource = files.filter((f) => (f.endsWith(".ts") || f.endsWith(".tsx") || f.endsWith(".js") || f.endsWith(".jsx") ||
|
|
271
|
+
f.endsWith(".py") || f.endsWith(".go")) &&
|
|
272
|
+
!f.includes("node_modules") && !f.includes(".test.") && !f.includes(".spec."));
|
|
273
|
+
// Read up to 100 additional files for pattern counts
|
|
274
|
+
const extraSample = allSource.sort(() => Math.random() - 0.5).slice(0, 100);
|
|
275
|
+
const allContents = new Map(contents);
|
|
276
|
+
for (const file of extraSample) {
|
|
277
|
+
if (!allContents.has(file)) {
|
|
278
|
+
try {
|
|
279
|
+
const content = fs.readFileSync(path.join(dir, file), "utf-8");
|
|
280
|
+
allContents.set(file, content);
|
|
281
|
+
}
|
|
282
|
+
catch { /* skip */ }
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// ========================================
|
|
286
|
+
// 1. I18N / LOCALIZATION PATTERNS
|
|
287
|
+
// ========================================
|
|
288
|
+
const i18nPatterns = [
|
|
289
|
+
{ pattern: "useLocale", hook: "useLocale()", count: 0, files: [] },
|
|
290
|
+
{ pattern: "useTranslation", hook: "useTranslation()", count: 0, files: [] },
|
|
291
|
+
{ pattern: "useTranslations", hook: "useTranslations()", count: 0, files: [] },
|
|
292
|
+
{ pattern: "useIntl", hook: "useIntl()", count: 0, files: [] },
|
|
293
|
+
{ pattern: "intl\\.formatMessage", hook: "intl.formatMessage()", count: 0, files: [] },
|
|
294
|
+
{ pattern: "\\bt\\(['\"]", hook: "t(\"key\")", count: 0, files: [] },
|
|
295
|
+
{ pattern: "i18next", hook: "i18next", count: 0, files: [] },
|
|
296
|
+
{ pattern: "gettext", hook: "gettext()", count: 0, files: [] },
|
|
297
|
+
{ pattern: "_\\(['\"]", hook: "_(\"string\")", count: 0, files: [] },
|
|
298
|
+
];
|
|
299
|
+
for (const [file, content] of allContents) {
|
|
300
|
+
for (const p of i18nPatterns) {
|
|
301
|
+
if (new RegExp(p.pattern).test(content)) {
|
|
302
|
+
p.count++;
|
|
303
|
+
if (p.files.length < 3)
|
|
304
|
+
p.files.push(file);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const dominantI18n = i18nPatterns.filter((p) => p.count >= 3).sort((a, b) => b.count - a.count);
|
|
309
|
+
if (dominantI18n.length > 0) {
|
|
310
|
+
const primary = dominantI18n[0];
|
|
311
|
+
let desc = `User-facing strings use ${primary.hook} for internationalization.`;
|
|
312
|
+
// Find where translation keys live
|
|
313
|
+
const localeFiles = files.filter((f) => (f.includes("locale") || f.includes("i18n") || f.includes("translations") || f.includes("messages")) &&
|
|
314
|
+
(f.endsWith(".json") || f.endsWith(".ts") || f.endsWith(".js")) &&
|
|
315
|
+
!f.includes("node_modules"));
|
|
316
|
+
const commonLocale = localeFiles.find((f) => f.includes("en/") || f.includes("en."));
|
|
317
|
+
if (commonLocale) {
|
|
318
|
+
desc += ` Add new translation keys in ${commonLocale}.`;
|
|
319
|
+
}
|
|
320
|
+
else if (localeFiles.length > 0) {
|
|
321
|
+
desc += ` Translation files are in: ${localeFiles[0]}.`;
|
|
322
|
+
}
|
|
323
|
+
findings.push({
|
|
324
|
+
category: "Dominant patterns",
|
|
325
|
+
description: desc,
|
|
326
|
+
evidence: `${primary.count} files use ${primary.hook}`,
|
|
327
|
+
confidence: "high",
|
|
328
|
+
discoverable: false,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
// ========================================
|
|
332
|
+
// 2. ROUTING / API PATTERNS
|
|
333
|
+
// ========================================
|
|
334
|
+
const routerPatterns = [
|
|
335
|
+
{ pattern: "trpc\\.router|createTRPCRouter|t\\.router", name: "tRPC routers", count: 0 },
|
|
336
|
+
{ pattern: "express\\.Router|router\\.get|router\\.post", name: "Express routers", count: 0 },
|
|
337
|
+
{ pattern: "app\\.get\\(|app\\.post\\(|app\\.put\\(", name: "Express app routes", count: 0 },
|
|
338
|
+
{ pattern: "Hono|app\\.route\\(|c\\.json\\(", name: "Hono routes", count: 0 },
|
|
339
|
+
{ pattern: "FastAPI|@app\\.(get|post|put|delete)", name: "FastAPI endpoints", count: 0 },
|
|
340
|
+
{ pattern: "flask\\.route|@app\\.route", name: "Flask routes", count: 0 },
|
|
341
|
+
{ pattern: "gin\\.Engine|r\\.GET|r\\.POST", name: "Gin routes", count: 0 },
|
|
342
|
+
{ pattern: "fiber\\.App|app\\.Get|app\\.Post", name: "Fiber routes", count: 0 },
|
|
343
|
+
];
|
|
344
|
+
for (const [, content] of allContents) {
|
|
345
|
+
for (const p of routerPatterns) {
|
|
346
|
+
if (new RegExp(p.pattern).test(content)) {
|
|
347
|
+
p.count++;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const dominantRouter = routerPatterns.filter((p) => p.count >= 2).sort((a, b) => b.count - a.count);
|
|
352
|
+
if (dominantRouter.length > 0) {
|
|
353
|
+
const primary = dominantRouter[0];
|
|
354
|
+
findings.push({
|
|
355
|
+
category: "Dominant patterns",
|
|
356
|
+
description: `API endpoints use ${primary.name}. Follow this pattern for new routes.`,
|
|
357
|
+
evidence: `${primary.count} files use ${primary.name}`,
|
|
358
|
+
confidence: "high",
|
|
359
|
+
discoverable: false,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
// ========================================
|
|
363
|
+
// 3. VALIDATION / SCHEMA PATTERNS
|
|
364
|
+
// ========================================
|
|
365
|
+
const schemaPatterns = [
|
|
366
|
+
{ pattern: "z\\.object|z\\.string|z\\.number", name: "Zod", usage: "Use Zod schemas for validation", count: 0 },
|
|
367
|
+
{ pattern: "BaseModel|Field\\(", name: "Pydantic", usage: "Use Pydantic BaseModel for data classes", count: 0 },
|
|
368
|
+
{ pattern: "Joi\\.object|Joi\\.string", name: "Joi", usage: "Use Joi schemas for validation", count: 0 },
|
|
369
|
+
{ pattern: "yup\\.object|yup\\.string", name: "Yup", usage: "Use Yup schemas for validation", count: 0 },
|
|
370
|
+
{ pattern: "class.*Serializer.*:|serializers\\.Serializer", name: "Django serializers", usage: "Use Django REST serializers for API data", count: 0 },
|
|
371
|
+
{ pattern: "@dataclass", name: "dataclasses", usage: "Use @dataclass for data structures", count: 0 },
|
|
372
|
+
];
|
|
373
|
+
for (const [, content] of allContents) {
|
|
374
|
+
for (const p of schemaPatterns) {
|
|
375
|
+
if (new RegExp(p.pattern).test(content)) {
|
|
376
|
+
p.count++;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const dominantSchema = schemaPatterns.filter((p) => p.count >= 3).sort((a, b) => b.count - a.count);
|
|
381
|
+
if (dominantSchema.length > 0) {
|
|
382
|
+
const primary = dominantSchema[0];
|
|
383
|
+
findings.push({
|
|
384
|
+
category: "Dominant patterns",
|
|
385
|
+
description: `${primary.usage}. This is the project's standard validation approach.`,
|
|
386
|
+
evidence: `${primary.count} files use ${primary.name}`,
|
|
387
|
+
confidence: "high",
|
|
388
|
+
discoverable: false,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
// ========================================
|
|
392
|
+
// 4. STATE MANAGEMENT / DATA FETCHING
|
|
393
|
+
// ========================================
|
|
394
|
+
const statePatterns = [
|
|
395
|
+
{ pattern: "useQuery|useMutation|QueryClient", name: "React Query/TanStack Query", desc: "Data fetching uses React Query (useQuery/useMutation)", count: 0 },
|
|
396
|
+
{ pattern: "useSWR|mutate\\(", name: "SWR", desc: "Data fetching uses SWR (useSWR)", count: 0 },
|
|
397
|
+
{ pattern: "createSlice|configureStore", name: "Redux Toolkit", desc: "State management uses Redux Toolkit (createSlice)", count: 0 },
|
|
398
|
+
{ pattern: "create\\(.*set.*get|useStore", name: "Zustand", desc: "State management uses Zustand", count: 0 },
|
|
399
|
+
{ pattern: "atom\\(|useAtom", name: "Jotai", desc: "State management uses Jotai atoms", count: 0 },
|
|
400
|
+
];
|
|
401
|
+
for (const [, content] of allContents) {
|
|
402
|
+
for (const p of statePatterns) {
|
|
403
|
+
if (new RegExp(p.pattern).test(content)) {
|
|
404
|
+
p.count++;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const dominantState = statePatterns.filter((p) => p.count >= 3).sort((a, b) => b.count - a.count);
|
|
409
|
+
if (dominantState.length > 0) {
|
|
410
|
+
const primary = dominantState[0];
|
|
411
|
+
findings.push({
|
|
412
|
+
category: "Dominant patterns",
|
|
413
|
+
description: `${primary.desc}. Follow this pattern for new data operations.`,
|
|
414
|
+
evidence: `${primary.count} files`,
|
|
415
|
+
confidence: "high",
|
|
416
|
+
discoverable: false,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
// ========================================
|
|
420
|
+
// 5. TESTING PATTERNS
|
|
421
|
+
// ========================================
|
|
422
|
+
const testPatterns = [
|
|
423
|
+
{ pattern: "describe\\(|it\\(|test\\(", name: "Jest/Vitest", count: 0 },
|
|
424
|
+
{ pattern: "def test_|class Test|pytest", name: "pytest", count: 0 },
|
|
425
|
+
{ pattern: "func Test.*\\(t \\*testing\\.T\\)", name: "Go testing", count: 0 },
|
|
426
|
+
{ pattern: "expect\\(.*\\)\\.to", name: "Chai/expect", count: 0 },
|
|
427
|
+
];
|
|
428
|
+
const testFiles = [...allContents.entries()].filter(([f]) => f.includes(".test.") || f.includes(".spec.") || f.includes("_test.") || f.startsWith("test_"));
|
|
429
|
+
// Read a few test files specifically
|
|
430
|
+
const testSampled = files
|
|
431
|
+
.filter((f) => f.includes(".test.") || f.includes(".spec.") || f.includes("_test.go") || f.includes("test_"))
|
|
432
|
+
.slice(0, 10);
|
|
433
|
+
for (const file of testSampled) {
|
|
434
|
+
if (!allContents.has(file)) {
|
|
435
|
+
try {
|
|
436
|
+
const content = fs.readFileSync(path.join(dir, file), "utf-8");
|
|
437
|
+
allContents.set(file, content);
|
|
438
|
+
}
|
|
439
|
+
catch { /* skip */ }
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
for (const [f, content] of allContents) {
|
|
443
|
+
if (f.includes("test") || f.includes("spec")) {
|
|
444
|
+
for (const p of testPatterns) {
|
|
445
|
+
if (new RegExp(p.pattern).test(content)) {
|
|
446
|
+
p.count++;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
const dominantTest = testPatterns.filter((p) => p.count >= 2).sort((a, b) => b.count - a.count);
|
|
452
|
+
if (dominantTest.length > 0) {
|
|
453
|
+
const primary = dominantTest[0];
|
|
454
|
+
// Also detect common test utilities/helpers
|
|
455
|
+
const testHelperFiles = files.filter((f) => (f.includes("test-utils") || f.includes("testUtils") || f.includes("fixtures") || f.includes("helpers")) &&
|
|
456
|
+
(f.includes("test") || f.includes("spec")));
|
|
457
|
+
let desc = `Tests use ${primary.name}.`;
|
|
458
|
+
if (testHelperFiles.length > 0) {
|
|
459
|
+
desc += ` Test utilities in: ${testHelperFiles[0]}.`;
|
|
460
|
+
}
|
|
461
|
+
findings.push({
|
|
462
|
+
category: "Dominant patterns",
|
|
463
|
+
description: desc,
|
|
464
|
+
evidence: `${primary.count} test files`,
|
|
465
|
+
confidence: "high",
|
|
466
|
+
discoverable: false,
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
// ========================================
|
|
470
|
+
// 6. KEY DIRECTORY PURPOSES (app-specific)
|
|
471
|
+
// ========================================
|
|
472
|
+
// Detect directories with clear domain purposes
|
|
473
|
+
const dirPurposes = [];
|
|
474
|
+
// App store / plugin / integration directories
|
|
475
|
+
// Only match top-level integration directories (not deeply nested editor plugins etc.)
|
|
476
|
+
const integrationDirCandidates = ["app-store", "plugins", "integrations", "addons", "extensions"];
|
|
477
|
+
let bestIntegrationDir = "";
|
|
478
|
+
let bestIntegrationCount = 0;
|
|
479
|
+
for (const dirName of integrationDirCandidates) {
|
|
480
|
+
// Find files matching pattern: <prefix>/<dirName>/<integration-name>/<file>
|
|
481
|
+
const matchingFiles = files.filter((f) => new RegExp(`/${dirName}/[^/]+/[^/]+`).test(f) && !f.includes("node_modules"));
|
|
482
|
+
const integrationNames = matchingFiles
|
|
483
|
+
.map((f) => {
|
|
484
|
+
const match = f.match(new RegExp(`(.*?/${dirName})/([^/]+)/`));
|
|
485
|
+
return match ? { dir: match[1], name: match[2] } : null;
|
|
486
|
+
})
|
|
487
|
+
.filter((v) => v !== null && !v.name.startsWith("_"));
|
|
488
|
+
const uniqueNames = [...new Set(integrationNames.map((i) => i.name))];
|
|
489
|
+
if (uniqueNames.length > bestIntegrationCount) {
|
|
490
|
+
bestIntegrationCount = uniqueNames.length;
|
|
491
|
+
bestIntegrationDir = integrationNames[0]?.dir || "";
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (bestIntegrationCount >= 3 && bestIntegrationDir) {
|
|
495
|
+
const integrations = files
|
|
496
|
+
.filter((f) => f.startsWith(bestIntegrationDir + "/") && !f.includes("node_modules"))
|
|
497
|
+
.map((f) => {
|
|
498
|
+
const suffix = f.slice(bestIntegrationDir.length + 1);
|
|
499
|
+
return suffix.split("/")[0];
|
|
500
|
+
})
|
|
501
|
+
.filter((v) => v && !v.startsWith("_") && v !== "templates" && !v.includes("."))
|
|
502
|
+
.filter((v, i, a) => a.indexOf(v) === i);
|
|
503
|
+
if (integrations.length > 0) {
|
|
504
|
+
const sampleIntegrations = integrations.slice(0, 6).join(", ");
|
|
505
|
+
findings.push({
|
|
506
|
+
category: "Dominant patterns",
|
|
507
|
+
description: `Third-party integrations live under ${bestIntegrationDir}/ (${sampleIntegrations}${integrations.length > 6 ? ", ..." : ""}). Each integration has its own directory with components, lib, and API code.`,
|
|
508
|
+
evidence: `${integrations.length} integrations found`,
|
|
509
|
+
confidence: "high",
|
|
510
|
+
discoverable: false,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return findings;
|
|
515
|
+
}
|
|
258
516
|
function detectExportPatterns(contents) {
|
|
259
517
|
const findings = [];
|
|
260
518
|
let defaultExports = 0;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sourcebook",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Extract the conventions, constraints, and architectural truths your AI coding agents keep missing.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
|
-
"sourcebook": "
|
|
7
|
+
"sourcebook": "dist/cli.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsc",
|
|
@@ -22,14 +22,15 @@
|
|
|
22
22
|
"cli",
|
|
23
23
|
"code-analysis",
|
|
24
24
|
"llm",
|
|
25
|
-
"agents"
|
|
25
|
+
"agents",
|
|
26
|
+
"mcp"
|
|
26
27
|
],
|
|
27
|
-
"author": "maroond",
|
|
28
|
-
"license": "
|
|
28
|
+
"author": "maroond labs <roy@maroond.ai>",
|
|
29
|
+
"license": "BSL-1.1",
|
|
29
30
|
"homepage": "https://sourcebook.run",
|
|
30
31
|
"repository": {
|
|
31
32
|
"type": "git",
|
|
32
|
-
"url": "https://github.com/maroondlabs/sourcebook.git"
|
|
33
|
+
"url": "git+https://github.com/maroondlabs/sourcebook.git"
|
|
33
34
|
},
|
|
34
35
|
"bugs": {
|
|
35
36
|
"url": "https://github.com/maroondlabs/sourcebook/issues"
|