@codewizard-dt/bootstrap 5.3.0 → 6.0.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.
Files changed (93) hide show
  1. package/README.md +11 -3
  2. package/bin/cli.js +16 -0
  3. package/lib/hooks/README.md +124 -3
  4. package/lib/hooks/claude-settings-guard.js +8 -2
  5. package/lib/prompts/README.md +1 -1
  6. package/lib/prompts/migrate-wiki.md +7 -7
  7. package/lib/scripts/README.md +111 -10
  8. package/lib/scripts/bootstrap-prefs-tui.js +1222 -0
  9. package/lib/scripts/install-global.sh +137 -6
  10. package/lib/scripts/migrate-project.sh +52 -10
  11. package/lib/scripts/rename-wiki-ids.js +232 -0
  12. package/lib/scripts/templates/CLAUDE-wiki.md +6 -6
  13. package/lib/scripts/templates/bootstrap-prefs-schema.json +11 -2
  14. package/lib/scripts/templates/settings-sandbox-unattended.json +36 -0
  15. package/lib/scripts/templates/settings-sandbox.json +7 -0
  16. package/lib/scripts/templates/wiki/conventions.md +9 -9
  17. package/lib/scripts/templates/wiki/dashboard.html +527 -23
  18. package/lib/scripts/templates/wiki/index.md +2 -2
  19. package/lib/scripts/templates/wiki/work/bugs/lifecycle.md +1 -1
  20. package/lib/scripts/templates/wiki/work/decisions/index.md +12 -4
  21. package/lib/scripts/templates/wiki/work/decisions/lifecycle.md +11 -9
  22. package/lib/scripts/templates/wiki/work/requirements/index.md +12 -4
  23. package/lib/scripts/templates/wiki/work/requirements/lifecycle.md +8 -8
  24. package/lib/scripts/templates/wiki/work/roadmaps/index.md +11 -3
  25. package/lib/scripts/templates/wiki/work/roadmaps/lifecycle.md +7 -5
  26. package/lib/scripts/templates/wiki/work/tasks/index.md +1 -1
  27. package/lib/scripts/templates/wiki/work/tasks/lifecycle.md +16 -8
  28. package/lib/scripts/templates/wiki/work/uat/lifecycle.md +3 -3
  29. package/lib/scripts/wiki-dashboard-server.js +98 -0
  30. package/lib/skills/README.md +27 -22
  31. package/lib/skills/_shared/ARCHIVE-WORK-ITEM.md +103 -0
  32. package/lib/skills/_shared/ID-RESOLUTION.md +142 -0
  33. package/lib/skills/_shared/SKILL-BOILERPLATE.md +50 -0
  34. package/lib/skills/adr-assess/SKILL.md +112 -0
  35. package/lib/skills/{decision-create → adr-create}/SKILL.md +26 -26
  36. package/lib/skills/adr-deprecate/SKILL.md +190 -0
  37. package/lib/skills/adr-extract/SKILL.md +255 -0
  38. package/lib/skills/{decision-finalize → adr-finalize}/SKILL.md +38 -38
  39. package/lib/skills/{decision-next → adr-next}/SKILL.md +20 -19
  40. package/lib/skills/{decision-walkthrough → adr-walkthrough}/SKILL.md +23 -29
  41. package/lib/skills/architecture-audit/SKILL.md +1 -1
  42. package/lib/skills/autopilot/SKILL.md +110 -0
  43. package/lib/skills/bootstrap-config/SKILL.md +1 -1
  44. package/lib/skills/bug-assess/SKILL.md +3 -3
  45. package/lib/skills/bug-close/SKILL.md +13 -27
  46. package/lib/skills/{bug-file → bug-create}/SKILL.md +10 -14
  47. package/lib/skills/bug-triage/SKILL.md +5 -19
  48. package/lib/skills/demo/SKILL.md +1 -1
  49. package/lib/skills/extract-feature/SKILL.md +3 -3
  50. package/lib/skills/git-commit/SKILL.md +4 -4
  51. package/lib/skills/port-feature/SKILL.md +3 -3
  52. package/lib/skills/power-mode/SKILL.md +42 -49
  53. package/lib/skills/prd-assess/SKILL.md +105 -0
  54. package/lib/skills/{req-compile → prd-compile}/SKILL.md +6 -13
  55. package/lib/skills/{req-create → prd-create}/SKILL.md +19 -19
  56. package/lib/skills/prd-deprecate/SKILL.md +173 -0
  57. package/lib/skills/{req-finalize → prd-finalize}/SKILL.md +20 -29
  58. package/lib/skills/primer/SKILL.md +2 -2
  59. package/lib/skills/research/SKILL.md +3 -3
  60. package/lib/skills/roadmap-assess/SKILL.md +20 -20
  61. package/lib/skills/roadmap-create/SKILL.md +19 -29
  62. package/lib/skills/roadmap-next/SKILL.md +16 -28
  63. package/lib/skills/security-audit/SKILL.md +1 -1
  64. package/lib/skills/tackle/SKILL.md +13 -15
  65. package/lib/skills/{task-audit → task-assess}/SKILL.md +34 -44
  66. package/lib/skills/task-close/SKILL.md +73 -0
  67. package/lib/skills/{task-add → task-create}/SKILL.md +25 -24
  68. package/lib/skills/uat-assess/SKILL.md +124 -0
  69. package/lib/skills/uat-auto/SKILL.md +9 -7
  70. package/lib/skills/uat-auto-plus/SKILL.md +2 -2
  71. package/lib/skills/{uat-generate → uat-create}/SKILL.md +10 -12
  72. package/lib/skills/uat-skip/SKILL.md +5 -9
  73. package/lib/skills/uat-walk/SKILL.md +2 -2
  74. package/lib/skills/uat-walk/UAT-CORE.md +29 -16
  75. package/lib/skills/update-docs/SKILL.md +1 -1
  76. package/lib/skills/usage-report/SKILL.md +52 -0
  77. package/lib/skills/usage-report/scripts/pricing.json +22 -0
  78. package/lib/skills/usage-report/scripts/usage-report.js +518 -0
  79. package/lib/skills/whats-next/SKILL.md +177 -0
  80. package/lib/skills/wiki-archive/SKILL.md +12 -23
  81. package/lib/skills/wiki-lint/SKILL.md +23 -13
  82. package/lib/skills/wiki-query/SKILL.md +1 -1
  83. package/lib/skills/wiki-rotate-log/SKILL.md +1 -1
  84. package/lib/skills/wiki-tidy/SKILL.md +6 -9
  85. package/package.json +1 -1
  86. package/lib/scripts/templates/wiki/work/decisions/archive/index.md +0 -10
  87. package/lib/scripts/templates/wiki/work/requirements/archive/index.md +0 -10
  88. package/lib/scripts/templates/wiki/work/roadmaps/archive/index.md +0 -10
  89. package/lib/skills/req-extract-decisions/SKILL.md +0 -241
  90. package/lib/skills/req-retire/SKILL.md +0 -185
  91. package/lib/skills/req-update/SKILL.md +0 -265
  92. package/lib/skills/task-trash/SKILL.md +0 -115
  93. package/lib/skills/task-update/SKILL.md +0 -136
@@ -0,0 +1,518 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ /**
5
+ * usage-report.js — offline token/cost rollup over Claude Code session transcripts
6
+ *
7
+ * Purpose: parses ~/.claude/projects/<encoded-cwd>/**\/*.jsonl (main sessions and
8
+ * <session-id>/subagents/agent-*.jsonl) and prints ONE JSON document on stdout:
9
+ * token and estimated-cost totals per model, per skill, and per day. No network,
10
+ * no writes outside stdout; rendering a human summary is the SKILL.md's job.
11
+ * Usage: node usage-report.js [--project <path>] [--all-projects]
12
+ * [--since YYYY-MM-DD] [--until YYYY-MM-DD] [--pretty]
13
+ * Default scope is process.cwd(). Unknown flag, missing flag value, or bad date
14
+ * exits 2 with usage on stderr. No transcripts at all is NOT an error: valid
15
+ * zero-total JSON, exit 0.
16
+ * Env (tests): USAGE_REPORT_PROJECTS_DIR overrides ~/.claude/projects;
17
+ * USAGE_REPORT_SKILLS_DIR overrides ~/.claude/skills (declaredModel lookup).
18
+ * Counting rules: only type:"assistant" entries with message.usage count;
19
+ * "<synthetic>" models and unparseable lines are skipped (the latter counted in
20
+ * meta.skippedLines). Streaming writes one API response several times, and fork
21
+ * context copies it across files, so records are deduped GLOBALLY on
22
+ * message.id|requestId (fallback message.id; neither -> once per file:line).
23
+ * Attribution: a turn belongs to the most recently invoked skill in its session
24
+ * timeline (Skill tool_use or <command-name>/x</command-name> user string) until
25
+ * the next invocation; before any invocation -> "(none)". Subagent files map to
26
+ * the PARENT session (the directory name above subagents/). This is a heuristic.
27
+ * Cost: tokens x pricing.json (USD per million tokens, longest model-id prefix
28
+ * wins). A model with no price entry has cost null and is listed in
29
+ * meta.unpricedModels. A byModel/bySkill/byDay row's cost is null only when every
30
+ * turn in it is unpriced; totals.cost sums priced turns only.
31
+ *
32
+ * JSON contract (schemaVersion 1 — bump on any breaking change; TASK-0109 reads it):
33
+ * { schemaVersion, generatedAt,
34
+ * scope:{project,encodedDir,allProjects} // project/encodedDir null when allProjects
35
+ * range:{since,until}, // ISO strings or null; until is end-of-day
36
+ * totals:{input,output,cacheCreation,cacheRead,cost,turns},
37
+ * byModel:[{model,tier,input,output,cacheCreation,cacheRead,cost,turns}],
38
+ * bySkill:[{skill,declaredModel,declaredFamily,modelCheck,models:{<model>:turns},
39
+ * input,output,cacheCreation,cacheRead,cost,turns}],
40
+ * // declaredFamily: haiku|sonnet|opus|fable|unknown|null (null = none/"inherit").
41
+ * // modelCheck: match|mismatch|undeclared|unknown — compares FAMILIES, not
42
+ * // exact ids; mismatch only when a known observed family differs. Additive
43
+ * // (still schemaVersion 1); consumers display it rather than re-comparing.
44
+ * byDay:[{date,cost,turns}],
45
+ * meta:{files,skippedLines,unpricedModels,pricingAsOf,attribution} }
46
+ * byModel/bySkill sort by cost desc (null last); byDay by date asc.
47
+ */
48
+
49
+ const fs = require('node:fs');
50
+ const path = require('node:path');
51
+ const os = require('node:os');
52
+ const readline = require('node:readline');
53
+
54
+ const NONE_SKILL = '(none)';
55
+ const USAGE = 'usage: node usage-report.js [--project <path>] [--all-projects] ' +
56
+ '[--since YYYY-MM-DD] [--until YYYY-MM-DD] [--pretty]\n';
57
+
58
+ function encodeProjectDir(absPath) {
59
+ return absPath.replace(/[^A-Za-z0-9]/g, '-');
60
+ }
61
+
62
+ function projectsRoot() {
63
+ return process.env.USAGE_REPORT_PROJECTS_DIR || path.join(os.homedir(), '.claude', 'projects');
64
+ }
65
+
66
+ function skillsRoot() {
67
+ return process.env.USAGE_REPORT_SKILLS_DIR || path.join(os.homedir(), '.claude', 'skills');
68
+ }
69
+
70
+ function listTranscripts(dir) {
71
+ const out = [];
72
+ const walk = (d) => {
73
+ let entries;
74
+ try {
75
+ entries = fs.readdirSync(d, { withFileTypes: true });
76
+ } catch (_) {
77
+ return;
78
+ }
79
+ for (const e of entries) {
80
+ const full = path.join(d, e.name);
81
+ if (e.isDirectory()) {
82
+ if (e.name !== 'tool-results') walk(full);
83
+ } else if (e.isFile() && e.name.endsWith('.jsonl')) {
84
+ out.push(full);
85
+ }
86
+ }
87
+ };
88
+ walk(dir);
89
+ return out.sort();
90
+ }
91
+
92
+ function sessionForFile(file) {
93
+ const parent = path.dirname(file);
94
+ if (path.basename(parent) === 'subagents') return path.basename(path.dirname(parent));
95
+ return path.basename(file, '.jsonl');
96
+ }
97
+
98
+ function num(x) {
99
+ return typeof x === 'number' && Number.isFinite(x) ? x : 0;
100
+ }
101
+
102
+ const SLASH_RE = /<command-name>\s*\/?([^<\s]+)\s*<\/command-name>/;
103
+
104
+ async function parseTranscript(file) {
105
+ const session = sessionForFile(file);
106
+ const isSubagent = path.basename(path.dirname(file)) === 'subagents';
107
+ const records = [];
108
+ const events = [];
109
+ let skipped = 0;
110
+ let lineNo = 0;
111
+
112
+ const rl = readline.createInterface({
113
+ input: fs.createReadStream(file, { encoding: 'utf8' }),
114
+ crlfDelay: Infinity,
115
+ });
116
+
117
+ for await (const raw of rl) {
118
+ lineNo++;
119
+ const text = raw.trim();
120
+ if (!text) continue;
121
+ let entry;
122
+ try {
123
+ entry = JSON.parse(text);
124
+ } catch (_) {
125
+ skipped++;
126
+ continue;
127
+ }
128
+ if (!entry || typeof entry !== 'object') {
129
+ skipped++;
130
+ continue;
131
+ }
132
+ const message = entry.message && typeof entry.message === 'object' ? entry.message : null;
133
+ if (!message) continue;
134
+
135
+ if (entry.type === 'assistant') {
136
+ if (Array.isArray(message.content)) {
137
+ for (const block of message.content) {
138
+ if (block && block.type === 'tool_use' && block.name === 'Skill' &&
139
+ block.input && typeof block.input.skill === 'string' && block.input.skill) {
140
+ events.push({
141
+ session, ts: entry.timestamp, skill: block.input.skill.replace(/^\//, ''),
142
+ key: block.id ? `tu:${block.id}` : null,
143
+ });
144
+ }
145
+ }
146
+ }
147
+ const usage = message.usage;
148
+ if (usage && typeof usage === 'object' && message.model !== '<synthetic>') {
149
+ const cc = usage.cache_creation && typeof usage.cache_creation === 'object' ? usage.cache_creation : null;
150
+ const cacheCreation = num(usage.cache_creation_input_tokens);
151
+ records.push({
152
+ file, line: lineNo, session, isSubagent,
153
+ id: typeof message.id === 'string' ? message.id : null,
154
+ requestId: typeof entry.requestId === 'string' ? entry.requestId : null,
155
+ model: typeof message.model === 'string' ? message.model : 'unknown',
156
+ ts: typeof entry.timestamp === 'string' ? entry.timestamp : null,
157
+ input: num(usage.input_tokens),
158
+ output: num(usage.output_tokens),
159
+ cacheCreation,
160
+ cacheCreation1h: cc ? Math.min(num(cc.ephemeral_1h_input_tokens), cacheCreation) : 0,
161
+ cacheRead: num(usage.cache_read_input_tokens),
162
+ });
163
+ }
164
+ } else if (entry.type === 'user' && typeof message.content === 'string') {
165
+ const m = SLASH_RE.exec(message.content);
166
+ if (m) {
167
+ events.push({
168
+ session, ts: entry.timestamp, skill: m[1],
169
+ key: entry.uuid ? `uu:${entry.uuid}` : null,
170
+ });
171
+ }
172
+ }
173
+ }
174
+ return { file, records, events, skipped };
175
+ }
176
+
177
+ function dedupe(records) {
178
+ const seen = new Map();
179
+ for (const r of records) {
180
+ let key;
181
+ if (r.id && r.requestId) key = `${r.id}|${r.requestId}`;
182
+ else if (r.id) key = `id:${r.id}`;
183
+ else key = `line:${r.file}:${r.line}`;
184
+ seen.set(key, r);
185
+ }
186
+ return [...seen.values()];
187
+ }
188
+
189
+ function attributeSkills(records, events) {
190
+ const seenKeys = new Set();
191
+ const bySession = new Map();
192
+ for (const ev of events) {
193
+ if (typeof ev.ts !== 'string' || Number.isNaN(Date.parse(ev.ts))) continue;
194
+ if (ev.key) {
195
+ if (seenKeys.has(ev.key)) continue;
196
+ seenKeys.add(ev.key);
197
+ }
198
+ if (!bySession.has(ev.session)) bySession.set(ev.session, []);
199
+ bySession.get(ev.session).push({ t: Date.parse(ev.ts), skill: ev.skill });
200
+ }
201
+ for (const list of bySession.values()) list.sort((a, b) => a.t - b.t);
202
+
203
+ return records.map((r) => {
204
+ let skill = NONE_SKILL;
205
+ const list = bySession.get(r.session);
206
+ const t = r.ts ? Date.parse(r.ts) : NaN;
207
+ if (list && !Number.isNaN(t)) {
208
+ let lo = 0;
209
+ let hi = list.length - 1;
210
+ let found = -1;
211
+ while (lo <= hi) {
212
+ const mid = (lo + hi) >> 1;
213
+ if (list[mid].t <= t) {
214
+ found = mid;
215
+ lo = mid + 1;
216
+ } else {
217
+ hi = mid - 1;
218
+ }
219
+ }
220
+ if (found >= 0) skill = list[found].skill;
221
+ }
222
+ return { ...r, skill };
223
+ });
224
+ }
225
+
226
+ function matchPricing(model, table) {
227
+ const keys = Object.keys(table.models || {}).sort((a, b) => b.length - a.length);
228
+ for (const k of keys) {
229
+ if (model.startsWith(k)) return table.models[k];
230
+ }
231
+ return null;
232
+ }
233
+
234
+ function tierOf(model, table) {
235
+ const p = matchPricing(model, table);
236
+ if (p && p.tier) return p.tier;
237
+ const m = model.toLowerCase();
238
+ if (m.includes('haiku')) return 'haiku';
239
+ if (m.includes('sonnet')) return 'sonnet';
240
+ if (m.includes('opus')) return 'opus';
241
+ return 'other';
242
+ }
243
+
244
+ const MODEL_FAMILIES = ['haiku', 'sonnet', 'opus', 'fable'];
245
+
246
+ // Family of a model alias or id ("sonnet", "claude-sonnet-5", "sonnet[1m]" -> "sonnet"),
247
+ // used to compare a skill's declared model to the observed ones by tier rather than
248
+ // by exact string. null = nothing to compare ("inherit", empty, non-string);
249
+ // "unknown" = a model string outside the known families (incl. "opusplan", which
250
+ // switches models mid-session).
251
+ function familyOf(model) {
252
+ if (typeof model !== 'string') return null;
253
+ const m = model.trim().toLowerCase();
254
+ if (!m || m === 'inherit') return null;
255
+ if (m.startsWith('opusplan')) return 'unknown';
256
+ return MODEL_FAMILIES.find((f) => m.includes(f)) || 'unknown';
257
+ }
258
+
259
+ // Declared-vs-observed verdict for one skill row. "mismatch" only when the declared
260
+ // family is known and an observed model of a known, different family ran; unknown
261
+ // observed models are ignored. "undeclared" = no declared tier (missing/inherit);
262
+ // "unknown" = declared or observed families cannot be compared.
263
+ function modelCheckOf(declaredModel, observedModels) {
264
+ const declared = familyOf(declaredModel);
265
+ if (declared === null) return 'undeclared';
266
+ if (declared === 'unknown') return 'unknown';
267
+ const observed = new Set(observedModels.map(familyOf).filter((f) => f && f !== 'unknown'));
268
+ if (observed.size === 0) return 'unknown';
269
+ return [...observed].some((f) => f !== declared) ? 'mismatch' : 'match';
270
+ }
271
+
272
+ function price(record, table) {
273
+ const p = matchPricing(record.model, table);
274
+ if (!p) return null;
275
+ const w1h = Math.min(record.cacheCreation1h || 0, record.cacheCreation);
276
+ const w5m = record.cacheCreation - w1h;
277
+ const rate1h = typeof p.cacheWrite1h === 'number' ? p.cacheWrite1h : p.cacheWrite;
278
+ return (
279
+ record.input * p.input +
280
+ record.output * p.output +
281
+ w5m * p.cacheWrite +
282
+ w1h * rate1h +
283
+ record.cacheRead * p.cacheRead
284
+ ) / 1e6;
285
+ }
286
+
287
+ function readDeclaredModel(skill) {
288
+ if (!/^[A-Za-z0-9_.-]+$/.test(skill) || skill === '.' || skill === '..') return null;
289
+ let text;
290
+ try {
291
+ text = fs.readFileSync(path.join(skillsRoot(), skill, 'SKILL.md'), 'utf8');
292
+ } catch (_) {
293
+ return null;
294
+ }
295
+ const fm = /^---\r?\n([\s\S]*?)\r?\n---/.exec(text);
296
+ if (!fm) return null;
297
+ const m = /^model:[ \t]*(.*?)[ \t]*$/m.exec(fm[1]);
298
+ if (!m) return null;
299
+ const val = m[1].replace(/^["']|["']$/g, '').trim();
300
+ return val || null;
301
+ }
302
+
303
+ function round6(n) {
304
+ return Math.round(n * 1e6) / 1e6;
305
+ }
306
+
307
+ function newBucket() {
308
+ return { input: 0, output: 0, cacheCreation: 0, cacheRead: 0, cost: 0, priced: 0, turns: 0 };
309
+ }
310
+
311
+ function addTo(b, r) {
312
+ b.input += r.input;
313
+ b.output += r.output;
314
+ b.cacheCreation += r.cacheCreation;
315
+ b.cacheRead += r.cacheRead;
316
+ b.turns += 1;
317
+ if (typeof r.cost === 'number') {
318
+ b.cost += r.cost;
319
+ b.priced += 1;
320
+ }
321
+ }
322
+
323
+ function finish(b) {
324
+ const { priced, ...rest } = b;
325
+ return { ...rest, cost: b.turns > 0 && priced === 0 ? null : round6(b.cost) };
326
+ }
327
+
328
+ function byCostDesc(a, b) {
329
+ if (a.cost === null && b.cost !== null) return 1;
330
+ if (b.cost === null && a.cost !== null) return -1;
331
+ if (a.cost !== b.cost) return b.cost - a.cost;
332
+ return b.turns - a.turns;
333
+ }
334
+
335
+ // Records are expected to carry `cost` (number|null), `tier`, and `skill`, as set by main().
336
+ function aggregate(records, opts = {}) {
337
+ const declared = opts.declaredModel || readDeclaredModel;
338
+ const totals = newBucket();
339
+ const models = new Map();
340
+ const skills = new Map();
341
+ const days = new Map();
342
+
343
+ for (const r of records) {
344
+ addTo(totals, r);
345
+
346
+ if (!models.has(r.model)) models.set(r.model, { tier: r.tier || 'other', b: newBucket() });
347
+ addTo(models.get(r.model).b, r);
348
+
349
+ const skillName = r.skill || NONE_SKILL;
350
+ if (!skills.has(skillName)) skills.set(skillName, { b: newBucket(), models: {} });
351
+ const s = skills.get(skillName);
352
+ addTo(s.b, r);
353
+ s.models[r.model] = (s.models[r.model] || 0) + 1;
354
+
355
+ if (r.ts) {
356
+ const date = r.ts.slice(0, 10);
357
+ if (!days.has(date)) days.set(date, newBucket());
358
+ addTo(days.get(date), r);
359
+ }
360
+ }
361
+
362
+ const totalsOut = finish(totals);
363
+ totalsOut.cost = round6(totals.cost);
364
+
365
+ const byModel = [...models.entries()]
366
+ .map(([model, v]) => ({ model, tier: v.tier, ...finish(v.b) }))
367
+ .sort(byCostDesc);
368
+
369
+ const bySkill = [...skills.entries()]
370
+ .map(([skill, v]) => {
371
+ const declaredModel = skill === NONE_SKILL ? null : declared(skill);
372
+ return {
373
+ skill,
374
+ declaredModel,
375
+ declaredFamily: familyOf(declaredModel),
376
+ modelCheck: modelCheckOf(declaredModel, Object.keys(v.models)),
377
+ models: v.models,
378
+ ...finish(v.b),
379
+ };
380
+ })
381
+ .sort(byCostDesc);
382
+
383
+ const byDay = [...days.entries()]
384
+ .map(([date, b]) => ({ date, cost: finish(b).cost, turns: b.turns }))
385
+ .sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0));
386
+
387
+ return { totals: totalsOut, byModel, bySkill, byDay };
388
+ }
389
+
390
+ function isValidDate(s) {
391
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) return false;
392
+ const d = new Date(`${s}T00:00:00.000Z`);
393
+ return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === s;
394
+ }
395
+
396
+ function parseArgs(argv) {
397
+ const opts = { project: null, allProjects: false, since: null, until: null, pretty: false };
398
+ for (let i = 0; i < argv.length; i++) {
399
+ const a = argv[i];
400
+ if (a === '--all-projects') opts.allProjects = true;
401
+ else if (a === '--pretty') opts.pretty = true;
402
+ else if (a === '--project' || a === '--since' || a === '--until') {
403
+ const v = argv[i + 1];
404
+ if (v === undefined || v.startsWith('--')) return { error: `missing value for ${a}` };
405
+ i++;
406
+ if (a === '--project') opts.project = v;
407
+ else if (!isValidDate(v)) return { error: `${a} expects YYYY-MM-DD, got "${v}"` };
408
+ else opts[a.slice(2)] = v;
409
+ } else {
410
+ return { error: `unknown argument: ${a}` };
411
+ }
412
+ }
413
+ return { opts };
414
+ }
415
+
416
+ async function main(argv) {
417
+ const parsed = parseArgs(argv);
418
+ if (parsed.error) {
419
+ process.stderr.write(`usage-report: ${parsed.error}\n${USAGE}`);
420
+ return 2;
421
+ }
422
+ const opts = parsed.opts;
423
+
424
+ let table;
425
+ try {
426
+ table = JSON.parse(fs.readFileSync(path.join(__dirname, 'pricing.json'), 'utf8'));
427
+ } catch (e) {
428
+ process.stderr.write(`usage-report: cannot load pricing.json: ${e.message}\n`);
429
+ return 1;
430
+ }
431
+
432
+ const root = projectsRoot();
433
+ let projectAbs = null;
434
+ let encodedDir = null;
435
+ let scanDir;
436
+ if (opts.allProjects) {
437
+ scanDir = root;
438
+ } else {
439
+ projectAbs = path.resolve(opts.project || process.cwd());
440
+ encodedDir = encodeProjectDir(projectAbs);
441
+ scanDir = path.join(root, encodedDir);
442
+ }
443
+
444
+ const files = listTranscripts(scanDir);
445
+ let records = [];
446
+ let events = [];
447
+ let skippedLines = 0;
448
+ for (const f of files) {
449
+ const p = await parseTranscript(f);
450
+ records = records.concat(p.records);
451
+ events = events.concat(p.events);
452
+ skippedLines += p.skipped;
453
+ }
454
+
455
+ records = dedupe(records);
456
+ if (opts.since || opts.until) {
457
+ records = records.filter((r) => {
458
+ if (!r.ts) return false;
459
+ const d = r.ts.slice(0, 10);
460
+ return (!opts.since || d >= opts.since) && (!opts.until || d <= opts.until);
461
+ });
462
+ }
463
+ records = attributeSkills(records, events).map((r) => ({
464
+ ...r, cost: price(r, table), tier: tierOf(r.model, table),
465
+ }));
466
+
467
+ const agg = aggregate(records);
468
+ const unpriced = [...new Set(records.filter((r) => r.cost === null).map((r) => r.model))].sort();
469
+
470
+ const report = {
471
+ schemaVersion: 1,
472
+ generatedAt: new Date().toISOString(),
473
+ scope: { project: projectAbs, encodedDir, allProjects: opts.allProjects },
474
+ range: {
475
+ since: opts.since ? `${opts.since}T00:00:00.000Z` : null,
476
+ until: opts.until ? `${opts.until}T23:59:59.999Z` : null,
477
+ },
478
+ totals: agg.totals,
479
+ byModel: agg.byModel,
480
+ bySkill: agg.bySkill,
481
+ byDay: agg.byDay,
482
+ meta: {
483
+ files: files.length,
484
+ skippedLines,
485
+ unpricedModels: unpriced,
486
+ pricingAsOf: table.asOf,
487
+ attribution: 'last-invoked-skill',
488
+ },
489
+ };
490
+
491
+ process.stdout.write(JSON.stringify(report, null, opts.pretty ? 2 : 0) + '\n');
492
+ return 0;
493
+ }
494
+
495
+ module.exports = {
496
+ encodeProjectDir,
497
+ listTranscripts,
498
+ parseTranscript,
499
+ dedupe,
500
+ attributeSkills,
501
+ price,
502
+ tierOf,
503
+ familyOf,
504
+ modelCheckOf,
505
+ aggregate,
506
+ readDeclaredModel,
507
+ main,
508
+ };
509
+
510
+ if (require.main === module) {
511
+ main(process.argv.slice(2)).then(
512
+ (code) => { process.exitCode = code; },
513
+ (err) => {
514
+ process.stderr.write(`usage-report: ${err && err.message ? err.message : err}\n`);
515
+ process.exitCode = 1;
516
+ },
517
+ );
518
+ }