-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.ts
More file actions
238 lines (221 loc) · 7.58 KB
/
bootstrap.ts
File metadata and controls
238 lines (221 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Seed apps/support.auto.dev and every readings dependency into the arest
* worker via POST /arest/parse.
*
* Tiers seed in dependency order — each tier's nouns become parser context
* for the next:
*
* 1. arest metamodel — Entity Type, Fact Type, Constraint, etc.
* 2. law-core — Authority hierarchy
* 3. us-law — federal + state statutes
* 4. apps/auto.dev — vehicle / plans / API vocabulary
* 5. apps/support.auto.dev — Support Request lifecycle (this app)
*
* Within a tier, files seed in parallel batches. Tier 1 metamodel files
* are sequential because each builds on the previous (core defines the
* primitives the rest reference).
*
* Usage: yarn seed (or: npx tsx bootstrap.ts)
*
* AUTO_DEV_API_KEY must be set in env or in ~/.claude/.env.
*/
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const API_KEY = (() => {
const fromEnv = process.env.AUTO_DEV_API_KEY?.trim()
if (fromEnv) return fromEnv
try {
const home = process.env.HOME || process.env.USERPROFILE || ''
const dotEnv = fs.readFileSync(path.join(home, '.claude', '.env'), 'utf-8')
return dotEnv.match(/AUTO_DEV_API_KEY=(.+)/)?.[1]?.trim() ?? ''
} catch {
return ''
}
})()
if (!API_KEY) {
console.error('AUTO_DEV_API_KEY not found in env or ~/.claude/.env')
process.exit(1)
}
const SEED_URL = 'https://api.auto.dev/arest/parse'
const REPOS = path.resolve(__dirname, '..', '..')
const APPS = path.resolve(__dirname, '..')
interface ParseResult {
domain: string
entities: number
nouns: number
readings: number
errors: string[]
}
async function seedOne(slug: string, text: string): Promise<ParseResult> {
let res: Response
try {
res = await fetch(SEED_URL, {
method: 'POST',
headers: { 'X-API-Key': API_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ domain: slug, text }),
})
} catch (e) {
return { domain: slug, entities: 0, nouns: 0, readings: 0, errors: [`network: ${e instanceof Error ? e.message : String(e)}`] }
}
const body = await res.json().catch(() => ({})) as any
if (!res.ok) {
return { domain: slug, entities: 0, nouns: 0, readings: 0, errors: [body?.error || body?.message || `HTTP ${res.status}`] }
}
// /api/parse returns { domains: [{ domain, entities, nouns, readings, errors }] }
const r = body?.domains?.[0] as ParseResult | undefined
if (!r) return { domain: slug, entities: 0, nouns: 0, readings: 0, errors: ['no result in response'] }
return r
}
function listMd(dir: string, opts: { recursive?: boolean } = {}): string[] {
if (!fs.existsSync(dir)) return []
const out: string[] = []
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, ent.name)
if (ent.isDirectory()) {
if (opts.recursive) out.push(...listMd(p, opts))
} else if (ent.name.endsWith('.md')) {
out.push(p)
}
}
return out.sort()
}
// .env at an app root carries FORML readings (Domain Connection facts with
// Secret References). Gitignored, so the actual key values live outside source
// control. Returns the path if it exists, empty otherwise.
function listEnv(dir: string): string[] {
const p = path.join(dir, '.env')
return fs.existsSync(p) ? [p] : []
}
function slugFor(p: string): string {
if (path.basename(p) === '.env') return path.basename(path.dirname(p)) + '-env'
return path.basename(p, '.md')
}
interface Tier {
label: string
files: string[]
parallel: number
}
async function seedTier(tier: Tier) {
console.log(`\n=== ${tier.label} (${tier.files.length} file${tier.files.length === 1 ? '' : 's'}) ===`)
const totals = { entities: 0, nouns: 0, readings: 0, errored: 0 }
if (tier.files.length === 0) {
console.log(' (no files)')
return totals
}
for (let i = 0; i < tier.files.length; i += tier.parallel) {
const batch = tier.files.slice(i, i + tier.parallel)
const results = await Promise.all(
batch.map((p) => seedOne(slugFor(p), fs.readFileSync(p, 'utf-8'))),
)
for (const r of results) {
totals.entities += r.entities
totals.nouns += r.nouns
totals.readings += r.readings
if (r.errors?.length) {
totals.errored += 1
console.log(` x ${r.domain.padEnd(28)} ${r.errors.join('; ')}`)
} else {
console.log(` o ${r.domain.padEnd(28)} entities=${r.entities} nouns=${r.nouns} readings=${r.readings}`)
}
}
}
return totals
}
async function main() {
// Tier 1 (arest metamodel — Entity Type, Fact Type, Constraint, Domain,
// Organization, App, etc.) is bundled into the engine WASM at build time
// via crates/arest/readings/, so we do not seed it. Set SEED_METAMODEL=1
// to override (useful when deploying a fresh arest worker that hasn't had
// its DEFS cells written yet).
const seedMetamodel = process.env.SEED_METAMODEL === '1'
const arestRel = (rel: string) => path.join(REPOS, 'arest', 'readings', rel)
const tiers: Tier[] = [
...(seedMetamodel
? ([
{
label: 'Tier 1 - arest metamodel (core)',
files: [
arestRel('core/core.md'),
arestRel('core/state.md'),
arestRel('core/instances.md'),
arestRel('core/validation.md'),
arestRel('core/evolution.md'),
arestRel('core/outcomes.md'),
].filter(fs.existsSync),
parallel: 1,
},
{
label: 'Tier 1 - arest metamodel (templates)',
files: [arestRel('templates/organizations.md'), arestRel('templates/agents.md')].filter(fs.existsSync),
parallel: 1,
},
{
label: 'Tier 1 - arest metamodel (ui)',
files: [
arestRel('ui/ui.md'),
arestRel('ui/components.md'),
arestRel('ui/design.md'),
arestRel('ui/monoview.md'),
].filter(fs.existsSync),
parallel: 1,
},
] satisfies Tier[])
: []),
{
label: 'Tier 2 - law-core',
files: listMd(path.join(REPOS, 'law-core', 'readings')),
parallel: 1,
},
{
label: 'Tier 3 - us-law (federal + statutory)',
files: [
...listMd(path.join(REPOS, 'us-law', 'readings')),
...listMd(path.join(REPOS, 'us-law', 'readings', 'statutory')),
],
parallel: 3,
},
{
label: 'Tier 3 - us-law (states)',
files: listMd(path.join(REPOS, 'us-law', 'readings', 'states'), { recursive: true }),
parallel: 3,
},
{
label: 'Tier 4 - apps/auto.dev (vocabulary)',
files: listMd(path.join(APPS, 'auto.dev')),
parallel: 3,
},
{
label: 'Tier 5 - apps/support.auto.dev',
files: [
...listMd(path.join(__dirname, 'readings')),
...listEnv(__dirname),
],
parallel: 3,
},
]
let grand = { entities: 0, nouns: 0, readings: 0, errored: 0, files: 0 }
for (const tier of tiers) {
grand.files += tier.files.length
const t = await seedTier(tier)
grand.entities += t.entities
grand.nouns += t.nouns
grand.readings += t.readings
grand.errored += t.errored
}
console.log('\n=== Totals ===')
console.log(` Files: ${grand.files}`)
console.log(` Entities: ${grand.entities}`)
console.log(` Nouns: ${grand.nouns}`)
console.log(` Readings: ${grand.readings}`)
if (grand.errored > 0) {
console.log(` Errored: ${grand.errored}`)
process.exit(1)
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})