create-universal-ai-context 2.5.0 → 2.6.0-final

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 (153) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +331 -294
  3. package/bin/create-ai-context.js +1507 -775
  4. package/lib/adapters/aider.js +131 -131
  5. package/lib/adapters/antigravity.js +205 -205
  6. package/lib/adapters/claude.js +397 -397
  7. package/lib/adapters/cline.js +125 -125
  8. package/lib/adapters/continue.js +138 -138
  9. package/lib/adapters/copilot.js +131 -131
  10. package/lib/adapters/index.js +78 -78
  11. package/lib/adapters/windsurf.js +138 -138
  12. package/lib/ai-context-generator.js +234 -234
  13. package/lib/ai-orchestrator.js +432 -432
  14. package/lib/call-tracer.js +444 -444
  15. package/lib/content-preservation.js +243 -243
  16. package/lib/cross-tool-sync/file-watcher.js +274 -274
  17. package/lib/cross-tool-sync/index.js +41 -40
  18. package/lib/cross-tool-sync/sync-manager.js +540 -512
  19. package/lib/cross-tool-sync/sync-service.js +297 -297
  20. package/lib/detector.js +726 -726
  21. package/lib/doc-discovery.js +741 -741
  22. package/lib/drift-checker.js +920 -920
  23. package/lib/environment-detector.js +239 -239
  24. package/lib/index.js +399 -399
  25. package/lib/install-hooks.js +82 -82
  26. package/lib/installer.js +419 -419
  27. package/lib/migrate.js +328 -328
  28. package/lib/placeholder.js +632 -632
  29. package/lib/prompts.js +341 -341
  30. package/lib/smart-merge.js +540 -540
  31. package/lib/spinner.js +60 -60
  32. package/lib/static-analyzer.js +729 -729
  33. package/lib/template-coordination.js +148 -148
  34. package/lib/template-populator.js +843 -843
  35. package/lib/template-renderer.js +392 -392
  36. package/lib/utils/fs-wrapper.js +79 -79
  37. package/lib/utils/path-utils.js +60 -60
  38. package/lib/validate.js +155 -155
  39. package/package.json +1 -1
  40. package/templates/AI_CONTEXT.md.template +245 -245
  41. package/templates/base/README.md +260 -257
  42. package/templates/base/RPI_WORKFLOW_PLAN.md +325 -320
  43. package/templates/base/agents/api-developer.md +76 -76
  44. package/templates/base/agents/context-engineer.md +525 -525
  45. package/templates/base/agents/core-architect.md +76 -76
  46. package/templates/base/agents/database-ops.md +76 -76
  47. package/templates/base/agents/deployment-ops.md +76 -76
  48. package/templates/base/agents/integration-hub.md +76 -76
  49. package/templates/base/analytics/README.md +114 -114
  50. package/templates/base/automation/config.json +58 -58
  51. package/templates/base/automation/generators/code-mapper.js +308 -308
  52. package/templates/base/automation/generators/index-builder.js +321 -321
  53. package/templates/base/automation/hooks/post-commit.sh +83 -83
  54. package/templates/base/automation/hooks/pre-commit.sh +103 -103
  55. package/templates/base/ci-templates/README.md +108 -108
  56. package/templates/base/ci-templates/github-actions/context-check.yml +144 -144
  57. package/templates/base/ci-templates/github-actions/validate-docs.yml +105 -105
  58. package/templates/base/commands/analytics.md +238 -238
  59. package/templates/base/commands/auto-sync.md +172 -172
  60. package/templates/base/commands/collab.md +194 -194
  61. package/templates/base/commands/context-optimize.md +226 -0
  62. package/templates/base/commands/help.md +485 -450
  63. package/templates/base/commands/rpi-implement.md +164 -115
  64. package/templates/base/commands/rpi-plan.md +147 -93
  65. package/templates/base/commands/rpi-research.md +145 -88
  66. package/templates/base/commands/session-resume.md +144 -144
  67. package/templates/base/commands/session-save.md +112 -112
  68. package/templates/base/commands/validate-all.md +77 -77
  69. package/templates/base/commands/verify-docs-current.md +86 -86
  70. package/templates/base/config/base.json +57 -57
  71. package/templates/base/config/environments/development.json +13 -13
  72. package/templates/base/config/environments/production.json +17 -17
  73. package/templates/base/config/environments/staging.json +13 -13
  74. package/templates/base/config/local.json.example +21 -21
  75. package/templates/base/context/.meta/generated-at.json +18 -18
  76. package/templates/base/context/ARCHITECTURE_SNAPSHOT.md +156 -156
  77. package/templates/base/context/CODE_TO_WORKFLOW_MAP.md +94 -94
  78. package/templates/base/context/FILE_OWNERSHIP.md +57 -57
  79. package/templates/base/context/INTEGRATION_POINTS.md +92 -92
  80. package/templates/base/context/KNOWN_GOTCHAS.md +195 -195
  81. package/templates/base/context/TESTING_MAP.md +95 -95
  82. package/templates/base/context/WORKFLOW_INDEX.md +129 -129
  83. package/templates/base/context/workflows/WORKFLOW_TEMPLATE.md +294 -294
  84. package/templates/base/indexes/agents/CAPABILITY_MATRIX.md +255 -255
  85. package/templates/base/indexes/agents/CATEGORY_INDEX.md +44 -44
  86. package/templates/base/indexes/code/CATEGORY_INDEX.md +38 -38
  87. package/templates/base/indexes/routing/CATEGORY_INDEX.md +39 -39
  88. package/templates/base/indexes/search/CATEGORY_INDEX.md +39 -39
  89. package/templates/base/indexes/workflows/CATEGORY_INDEX.md +38 -38
  90. package/templates/base/knowledge/README.md +98 -98
  91. package/templates/base/knowledge/sessions/README.md +88 -88
  92. package/templates/base/knowledge/sessions/TEMPLATE.md +150 -150
  93. package/templates/base/knowledge/shared/decisions/0001-adopt-context-engineering.md +144 -144
  94. package/templates/base/knowledge/shared/decisions/README.md +49 -49
  95. package/templates/base/knowledge/shared/decisions/TEMPLATE.md +123 -123
  96. package/templates/base/knowledge/shared/patterns/README.md +62 -62
  97. package/templates/base/knowledge/shared/patterns/TEMPLATE.md +120 -120
  98. package/templates/base/plans/PLAN_TEMPLATE.md +316 -250
  99. package/templates/base/research/RESEARCH_TEMPLATE.md +245 -153
  100. package/templates/base/schemas/agent.schema.json +141 -141
  101. package/templates/base/schemas/anchors.schema.json +54 -54
  102. package/templates/base/schemas/automation.schema.json +93 -93
  103. package/templates/base/schemas/command.schema.json +134 -134
  104. package/templates/base/schemas/hashes.schema.json +40 -40
  105. package/templates/base/schemas/manifest.schema.json +117 -117
  106. package/templates/base/schemas/plan.schema.json +136 -136
  107. package/templates/base/schemas/research.schema.json +115 -115
  108. package/templates/base/schemas/roles.schema.json +34 -34
  109. package/templates/base/schemas/session.schema.json +77 -77
  110. package/templates/base/schemas/settings.schema.json +244 -244
  111. package/templates/base/schemas/staleness.schema.json +53 -53
  112. package/templates/base/schemas/team-config.schema.json +42 -42
  113. package/templates/base/schemas/workflow.schema.json +126 -126
  114. package/templates/base/session/checkpoints/.gitkeep +2 -2
  115. package/templates/base/session/current/state.json +20 -20
  116. package/templates/base/session/history/.gitkeep +2 -2
  117. package/templates/base/settings.json +3 -3
  118. package/templates/base/standards/COMPATIBILITY.md +219 -219
  119. package/templates/base/standards/EXTENSION_GUIDELINES.md +280 -280
  120. package/templates/base/standards/QUALITY_CHECKLIST.md +211 -211
  121. package/templates/base/standards/README.md +66 -66
  122. package/templates/base/sync/anchors.json +6 -6
  123. package/templates/base/sync/hashes.json +6 -6
  124. package/templates/base/sync/staleness.json +10 -10
  125. package/templates/base/team/README.md +168 -168
  126. package/templates/base/team/config.json +79 -79
  127. package/templates/base/team/roles.json +145 -145
  128. package/templates/base/tools/bin/claude-context.js +151 -151
  129. package/templates/base/tools/lib/anchor-resolver.js +276 -276
  130. package/templates/base/tools/lib/config-loader.js +363 -363
  131. package/templates/base/tools/lib/detector.js +350 -350
  132. package/templates/base/tools/lib/diagnose.js +206 -206
  133. package/templates/base/tools/lib/drift-detector.js +373 -373
  134. package/templates/base/tools/lib/errors.js +199 -199
  135. package/templates/base/tools/lib/index.js +36 -36
  136. package/templates/base/tools/lib/init.js +192 -192
  137. package/templates/base/tools/lib/logger.js +230 -230
  138. package/templates/base/tools/lib/placeholder.js +201 -201
  139. package/templates/base/tools/lib/session-manager.js +354 -354
  140. package/templates/base/tools/lib/validate.js +521 -521
  141. package/templates/base/tools/package.json +49 -49
  142. package/templates/handlebars/aider-config.hbs +146 -80
  143. package/templates/handlebars/antigravity.hbs +377 -377
  144. package/templates/handlebars/claude.hbs +183 -183
  145. package/templates/handlebars/cline.hbs +62 -62
  146. package/templates/handlebars/continue-config.hbs +116 -116
  147. package/templates/handlebars/copilot.hbs +130 -130
  148. package/templates/handlebars/partials/gotcha-list.hbs +11 -11
  149. package/templates/handlebars/partials/header.hbs +3 -3
  150. package/templates/handlebars/partials/workflow-summary.hbs +16 -16
  151. package/templates/handlebars/windsurf-rules.hbs +69 -69
  152. package/templates/hooks/post-commit.hbs +28 -29
  153. package/templates/hooks/pre-commit.hbs +46 -46
@@ -1,432 +1,432 @@
1
- /**
2
- * AI Context Engineering - AI Orchestrator
3
- *
4
- * Coordinates with @context-engineer agent for full AI-powered analysis.
5
- * Creates initialization requests and generates agent instructions.
6
- */
7
-
8
- const fs = require('fs');
9
- const path = require('path');
10
-
11
- /**
12
- * Context directory and file names
13
- */
14
- const AI_CONTEXT_DIR = '.ai-context';
15
- const AI_CONTEXT_FILE = 'AI_CONTEXT.md';
16
-
17
- /**
18
- * Marker file for pending initialization
19
- */
20
- const INIT_MARKER = '.init-pending';
21
-
22
- /**
23
- * Instruction file for agent
24
- */
25
- const INIT_REQUEST_FILE = 'INIT_REQUEST.md';
26
-
27
- /**
28
- * Progress tracking file
29
- */
30
- const PROGRESS_FILE = 'INIT_PROGRESS.json';
31
-
32
- /**
33
- * Create an initialization request marker
34
- * @param {string} contextDir - .ai-context directory path
35
- * @param {object} config - Configuration from CLI
36
- * @returns {object} Request object
37
- */
38
- function createInitializationRequest(contextDir, config) {
39
- const request = {
40
- version: '2.0.0',
41
- createdAt: new Date().toISOString(),
42
- projectName: config.projectName || path.basename(path.dirname(contextDir)),
43
- config: {
44
- techStack: config.techStack,
45
- features: config.features,
46
- installPlugin: config.installPlugin
47
- },
48
- phases: [
49
- { id: 'repository-analysis', name: 'Repository Analysis', status: 'pending', progress: 0 },
50
- { id: 'workflow-discovery', name: 'Workflow Discovery', status: 'pending', progress: 0 },
51
- { id: 'template-population', name: 'Template Population', status: 'pending', progress: 0 },
52
- { id: 'index-generation', name: 'Index Generation', status: 'pending', progress: 0 },
53
- { id: 'validation', name: 'Validation', status: 'pending', progress: 0 },
54
- { id: 'finalization', name: 'Finalization', status: 'pending', progress: 0 }
55
- ],
56
- status: 'pending',
57
- completedAt: null
58
- };
59
-
60
- const markerPath = path.join(contextDir, INIT_MARKER);
61
- fs.writeFileSync(markerPath, JSON.stringify(request, null, 2));
62
-
63
- return request;
64
- }
65
-
66
- /**
67
- * Generate agent instruction file with pre-analysis results
68
- * @param {string} contextDir - .ai-context directory path
69
- * @param {object} analysis - Pre-analysis results from static analyzer
70
- * @param {object} config - Configuration from CLI
71
- */
72
- function generateAgentInstructions(contextDir, analysis, config) {
73
- const projectName = config.projectName || path.basename(path.dirname(contextDir));
74
- const pkgVersion = getPackageVersion();
75
-
76
- const instructions = `# Context Engineering Initialization Request
77
-
78
- > **Auto-generated by \`create-ai-context\` v${pkgVersion}**
79
- > Generated: ${new Date().toISOString()}
80
-
81
- ## Status: Awaiting AI Completion
82
-
83
- The CLI has performed static pre-analysis and prepared the template structure.
84
- Full AI-powered context engineering is requested to complete initialization.
85
-
86
- ---
87
-
88
- ## Project Overview
89
-
90
- | Property | Value |
91
- |----------|-------|
92
- | **Project Name** | ${projectName} |
93
- | **Tech Stack** | ${analysis.techStack?.summary || 'Detected during analysis'} |
94
- | **Source Files** | ${analysis.sourceFiles || 'TBD'} |
95
- | **Lines of Code** | ${analysis.linesOfCode?.total || 'TBD'} |
96
-
97
- ---
98
-
99
- ## Pre-Analysis Results
100
-
101
- ### Entry Points Discovered (${analysis.entryPoints?.length || 0})
102
-
103
- ${formatEntryPoints(analysis.entryPoints)}
104
-
105
- ### Workflow Candidates (${analysis.workflows?.length || 0})
106
-
107
- ${formatWorkflows(analysis.workflows)}
108
-
109
- ### Architecture Layers
110
-
111
- ${formatLayers(analysis.architecture?.layers)}
112
-
113
- ### Dependencies (${analysis.dependencies?.length || 0})
114
-
115
- ${formatDependencies(analysis.dependencies)}
116
-
117
- ---
118
-
119
- ## Requested Actions
120
-
121
- Please complete the context engineering initialization by:
122
-
123
- ### Phase 1: Verify Pre-Analysis
124
- - [ ] Review discovered entry points for accuracy
125
- - [ ] Confirm workflow candidates are valid
126
- - [ ] Identify any missed entry points or workflows
127
-
128
- ### Phase 2: Deep Workflow Discovery
129
- - [ ] Trace call chains for each workflow (3 levels deep)
130
- - [ ] Identify database operations per workflow
131
- - [ ] Map external API calls per workflow
132
- - [ ] Document error handling patterns
133
-
134
- ### Phase 3: Template Population
135
- - [ ] Fill ARCHITECTURE_SNAPSHOT.md with real architecture
136
- - [ ] Create individual workflow files (8-15 total)
137
- - [ ] Populate CODE_TO_WORKFLOW_MAP.md with file references
138
- - [ ] Update WORKFLOW_INDEX.md with all workflows
139
-
140
- ### Phase 4: Index Generation
141
- - [ ] Generate proper category indexes
142
- - [ ] Create semantic anchors for key functions
143
- - [ ] Build reverse lookup indexes
144
-
145
- ### Phase 5: Validation
146
- - [ ] Verify line number accuracy (target: 60%+)
147
- - [ ] Check all internal links
148
- - [ ] Confirm no placeholder values remain
149
-
150
- ### Phase 6: Finalization
151
- - [ ] Generate summary report
152
- - [ ] Update AI_CONTEXT.md with project-specific info
153
- - [ ] Create maintenance schedule
154
-
155
- ---
156
-
157
- ## Quick Start Command
158
-
159
- \`\`\`bash
160
- @context-engineer "Complete initialization for ${projectName} using the pre-analysis in INIT_REQUEST.md"
161
- \`\`\`
162
-
163
- Or use the dedicated command:
164
-
165
- \`\`\`bash
166
- /rpi-research "Complete context engineering initialization"
167
- \`\`\`
168
-
169
- ---
170
-
171
- ## Files to Update
172
-
173
- | File | Action |
174
- |------|--------|
175
- | \`context/ARCHITECTURE_SNAPSHOT.md\` | Populate with real architecture |
176
- | \`context/WORKFLOW_INDEX.md\` | Add all discovered workflows |
177
- | \`context/CODE_TO_WORKFLOW_MAP.md\` | Map files to workflows |
178
- | \`context/workflows/*.md\` | Create individual workflow docs |
179
- | \`indexes/workflows/CATEGORY_INDEX.md\` | Generate category index |
180
- | \`../AI_CONTEXT.md\` | Finalize with project info |
181
-
182
- ---
183
-
184
- ## Pre-Analysis Data
185
-
186
- The following JSON contains the full pre-analysis for programmatic use:
187
-
188
- \`\`\`json
189
- ${JSON.stringify(analysis.summary || {}, null, 2)}
190
- \`\`\`
191
-
192
- ---
193
-
194
- *This file will be deleted automatically after successful initialization.*
195
- *Generated by create-ai-context v${pkgVersion}*
196
- `;
197
-
198
- const instructionPath = path.join(contextDir, INIT_REQUEST_FILE);
199
- fs.writeFileSync(instructionPath, instructions);
200
-
201
- return instructionPath;
202
- }
203
-
204
- /**
205
- * Format entry points for markdown
206
- * @param {object[]} entryPoints - Entry points array
207
- * @returns {string}
208
- */
209
- function formatEntryPoints(entryPoints) {
210
- if (!entryPoints || entryPoints.length === 0) {
211
- return '*No entry points discovered during pre-analysis. AI analysis will discover them.*\n';
212
- }
213
-
214
- const limited = entryPoints.slice(0, 15);
215
- let output = '| File | Line | Route | Method |\n|------|------|-------|--------|\n';
216
-
217
- for (const ep of limited) {
218
- output += `| \`${ep.file}\` | ${ep.line} | ${ep.route || '-'} | ${ep.method || '-'} |\n`;
219
- }
220
-
221
- if (entryPoints.length > 15) {
222
- output += `\n*...and ${entryPoints.length - 15} more entry points*\n`;
223
- }
224
-
225
- return output;
226
- }
227
-
228
- /**
229
- * Format workflows for markdown
230
- * @param {object[]} workflows - Workflows array
231
- * @returns {string}
232
- */
233
- function formatWorkflows(workflows) {
234
- if (!workflows || workflows.length === 0) {
235
- return '*No workflows discovered during pre-analysis. AI analysis will discover them.*\n';
236
- }
237
-
238
- let output = '| Workflow | Category | Complexity | Files | Confidence |\n|----------|----------|------------|-------|------------|\n';
239
-
240
- for (const wf of workflows) {
241
- output += `| **${wf.name}** | ${wf.category} | ${wf.complexity} | ${wf.fileCount || wf.files?.length || 0} | ${wf.confidence || '-'}% |\n`;
242
- }
243
-
244
- return output;
245
- }
246
-
247
- /**
248
- * Format architecture layers for markdown
249
- * @param {object[]} layers - Layers array
250
- * @returns {string}
251
- */
252
- function formatLayers(layers) {
253
- if (!layers || layers.length === 0) {
254
- return '*Architecture layers will be determined during AI analysis.*\n';
255
- }
256
-
257
- let output = '| Layer | Directories | Purpose |\n|-------|-------------|----------|\n';
258
-
259
- for (const layer of layers) {
260
- output += `| **${layer.name}** | ${layer.directories?.join(', ') || '-'} | ${layer.purpose || '-'} |\n`;
261
- }
262
-
263
- return output;
264
- }
265
-
266
- /**
267
- * Format dependencies for markdown
268
- * @param {object[]} dependencies - Dependencies array
269
- * @returns {string}
270
- */
271
- function formatDependencies(dependencies) {
272
- if (!dependencies || dependencies.length === 0) {
273
- return '*No dependencies detected.*\n';
274
- }
275
-
276
- // Group by ecosystem
277
- const byEcosystem = {};
278
- for (const dep of dependencies) {
279
- const eco = dep.ecosystem || 'unknown';
280
- if (!byEcosystem[eco]) byEcosystem[eco] = [];
281
- byEcosystem[eco].push(dep);
282
- }
283
-
284
- let output = '';
285
- for (const [ecosystem, deps] of Object.entries(byEcosystem)) {
286
- output += `\n**${ecosystem.toUpperCase()}** (${deps.length}):\n`;
287
- const topDeps = deps.slice(0, 10);
288
- output += topDeps.map(d => `- ${d.name}@${d.version}`).join('\n') + '\n';
289
- if (deps.length > 10) {
290
- output += `- *...and ${deps.length - 10} more*\n`;
291
- }
292
- }
293
-
294
- return output;
295
- }
296
-
297
- /**
298
- * Get package version
299
- * @returns {string}
300
- */
301
- function getPackageVersion() {
302
- try {
303
- const pkgPath = path.join(__dirname, '..', 'package.json');
304
- const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
305
- return pkg.version || '1.0.0';
306
- } catch (error) {
307
- // Silently fall back to default version
308
- return '1.0.0';
309
- }
310
- }
311
-
312
- /**
313
- * Check initialization status
314
- * @param {string} contextDir - .ai-context directory path
315
- * @returns {object}
316
- */
317
- function checkInitializationStatus(contextDir) {
318
- const markerPath = path.join(contextDir, INIT_MARKER);
319
-
320
- if (!fs.existsSync(markerPath)) {
321
- return { status: 'not-started', request: null };
322
- }
323
-
324
- try {
325
- const request = JSON.parse(fs.readFileSync(markerPath, 'utf-8'));
326
- const completedPhases = request.phases.filter(p => p.status === 'completed').length;
327
- const totalPhases = request.phases.length;
328
-
329
- return {
330
- status: completedPhases === totalPhases ? 'completed' : 'in-progress',
331
- progress: Math.round((completedPhases / totalPhases) * 100),
332
- completedPhases,
333
- totalPhases,
334
- request
335
- };
336
- } catch {
337
- return { status: 'error', request: null };
338
- }
339
- }
340
-
341
- /**
342
- * Update initialization progress
343
- * @param {string} contextDir - .ai-context directory path
344
- * @param {string} phaseId - Phase ID to update
345
- * @param {string} status - New status ('in-progress', 'completed', 'failed')
346
- * @param {number} progress - Progress percentage (0-100)
347
- */
348
- function updateInitializationProgress(contextDir, phaseId, status, progress = 0) {
349
- const markerPath = path.join(contextDir, INIT_MARKER);
350
-
351
- if (!fs.existsSync(markerPath)) {
352
- return false;
353
- }
354
-
355
- try {
356
- const request = JSON.parse(fs.readFileSync(markerPath, 'utf-8'));
357
- const phase = request.phases.find(p => p.id === phaseId);
358
-
359
- if (phase) {
360
- phase.status = status;
361
- phase.progress = progress;
362
- phase.updatedAt = new Date().toISOString();
363
- }
364
-
365
- // Check if all phases are completed
366
- const allCompleted = request.phases.every(p => p.status === 'completed');
367
- if (allCompleted) {
368
- request.status = 'completed';
369
- request.completedAt = new Date().toISOString();
370
- } else {
371
- request.status = 'in-progress';
372
- }
373
-
374
- fs.writeFileSync(markerPath, JSON.stringify(request, null, 2));
375
- return true;
376
- } catch {
377
- return false;
378
- }
379
- }
380
-
381
- /**
382
- * Mark initialization as complete and clean up
383
- * @param {string} contextDir - .ai-context directory path
384
- * @param {boolean} removeMarker - Whether to remove the marker file
385
- */
386
- function completeInitialization(contextDir, removeMarker = true) {
387
- const markerPath = path.join(contextDir, INIT_MARKER);
388
- const instructionPath = path.join(contextDir, INIT_REQUEST_FILE);
389
-
390
- if (fs.existsSync(markerPath)) {
391
- const request = JSON.parse(fs.readFileSync(markerPath, 'utf-8'));
392
- request.status = 'completed';
393
- request.completedAt = new Date().toISOString();
394
-
395
- // Archive to progress file
396
- const progressPath = path.join(contextDir, PROGRESS_FILE);
397
- fs.writeFileSync(progressPath, JSON.stringify(request, null, 2));
398
-
399
- if (removeMarker) {
400
- fs.unlinkSync(markerPath);
401
- }
402
- }
403
-
404
- // Remove instruction file
405
- if (removeMarker && fs.existsSync(instructionPath)) {
406
- fs.unlinkSync(instructionPath);
407
- }
408
- }
409
-
410
- /**
411
- * Check if initialization is pending
412
- * @param {string} contextDir - .ai-context directory path
413
- * @returns {boolean}
414
- */
415
- function isInitializationPending(contextDir) {
416
- const markerPath = path.join(contextDir, INIT_MARKER);
417
- return fs.existsSync(markerPath);
418
- }
419
-
420
- module.exports = {
421
- createInitializationRequest,
422
- generateAgentInstructions,
423
- checkInitializationStatus,
424
- updateInitializationProgress,
425
- completeInitialization,
426
- isInitializationPending,
427
- INIT_MARKER,
428
- INIT_REQUEST_FILE,
429
- PROGRESS_FILE,
430
- AI_CONTEXT_DIR,
431
- AI_CONTEXT_FILE
432
- };
1
+ /**
2
+ * AI Context Engineering - AI Orchestrator
3
+ *
4
+ * Coordinates with @context-engineer agent for full AI-powered analysis.
5
+ * Creates initialization requests and generates agent instructions.
6
+ */
7
+
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+ /**
12
+ * Context directory and file names
13
+ */
14
+ const AI_CONTEXT_DIR = '.ai-context';
15
+ const AI_CONTEXT_FILE = 'AI_CONTEXT.md';
16
+
17
+ /**
18
+ * Marker file for pending initialization
19
+ */
20
+ const INIT_MARKER = '.init-pending';
21
+
22
+ /**
23
+ * Instruction file for agent
24
+ */
25
+ const INIT_REQUEST_FILE = 'INIT_REQUEST.md';
26
+
27
+ /**
28
+ * Progress tracking file
29
+ */
30
+ const PROGRESS_FILE = 'INIT_PROGRESS.json';
31
+
32
+ /**
33
+ * Create an initialization request marker
34
+ * @param {string} contextDir - .ai-context directory path
35
+ * @param {object} config - Configuration from CLI
36
+ * @returns {object} Request object
37
+ */
38
+ function createInitializationRequest(contextDir, config) {
39
+ const request = {
40
+ version: '2.0.0',
41
+ createdAt: new Date().toISOString(),
42
+ projectName: config.projectName || path.basename(path.dirname(contextDir)),
43
+ config: {
44
+ techStack: config.techStack,
45
+ features: config.features,
46
+ installPlugin: config.installPlugin
47
+ },
48
+ phases: [
49
+ { id: 'repository-analysis', name: 'Repository Analysis', status: 'pending', progress: 0 },
50
+ { id: 'workflow-discovery', name: 'Workflow Discovery', status: 'pending', progress: 0 },
51
+ { id: 'template-population', name: 'Template Population', status: 'pending', progress: 0 },
52
+ { id: 'index-generation', name: 'Index Generation', status: 'pending', progress: 0 },
53
+ { id: 'validation', name: 'Validation', status: 'pending', progress: 0 },
54
+ { id: 'finalization', name: 'Finalization', status: 'pending', progress: 0 }
55
+ ],
56
+ status: 'pending',
57
+ completedAt: null
58
+ };
59
+
60
+ const markerPath = path.join(contextDir, INIT_MARKER);
61
+ fs.writeFileSync(markerPath, JSON.stringify(request, null, 2));
62
+
63
+ return request;
64
+ }
65
+
66
+ /**
67
+ * Generate agent instruction file with pre-analysis results
68
+ * @param {string} contextDir - .ai-context directory path
69
+ * @param {object} analysis - Pre-analysis results from static analyzer
70
+ * @param {object} config - Configuration from CLI
71
+ */
72
+ function generateAgentInstructions(contextDir, analysis, config) {
73
+ const projectName = config.projectName || path.basename(path.dirname(contextDir));
74
+ const pkgVersion = getPackageVersion();
75
+
76
+ const instructions = `# Context Engineering Initialization Request
77
+
78
+ > **Auto-generated by \`create-ai-context\` v${pkgVersion}**
79
+ > Generated: ${new Date().toISOString()}
80
+
81
+ ## Status: Awaiting AI Completion
82
+
83
+ The CLI has performed static pre-analysis and prepared the template structure.
84
+ Full AI-powered context engineering is requested to complete initialization.
85
+
86
+ ---
87
+
88
+ ## Project Overview
89
+
90
+ | Property | Value |
91
+ |----------|-------|
92
+ | **Project Name** | ${projectName} |
93
+ | **Tech Stack** | ${analysis.techStack?.summary || 'Detected during analysis'} |
94
+ | **Source Files** | ${analysis.sourceFiles || 'TBD'} |
95
+ | **Lines of Code** | ${analysis.linesOfCode?.total || 'TBD'} |
96
+
97
+ ---
98
+
99
+ ## Pre-Analysis Results
100
+
101
+ ### Entry Points Discovered (${analysis.entryPoints?.length || 0})
102
+
103
+ ${formatEntryPoints(analysis.entryPoints)}
104
+
105
+ ### Workflow Candidates (${analysis.workflows?.length || 0})
106
+
107
+ ${formatWorkflows(analysis.workflows)}
108
+
109
+ ### Architecture Layers
110
+
111
+ ${formatLayers(analysis.architecture?.layers)}
112
+
113
+ ### Dependencies (${analysis.dependencies?.length || 0})
114
+
115
+ ${formatDependencies(analysis.dependencies)}
116
+
117
+ ---
118
+
119
+ ## Requested Actions
120
+
121
+ Please complete the context engineering initialization by:
122
+
123
+ ### Phase 1: Verify Pre-Analysis
124
+ - [ ] Review discovered entry points for accuracy
125
+ - [ ] Confirm workflow candidates are valid
126
+ - [ ] Identify any missed entry points or workflows
127
+
128
+ ### Phase 2: Deep Workflow Discovery
129
+ - [ ] Trace call chains for each workflow (3 levels deep)
130
+ - [ ] Identify database operations per workflow
131
+ - [ ] Map external API calls per workflow
132
+ - [ ] Document error handling patterns
133
+
134
+ ### Phase 3: Template Population
135
+ - [ ] Fill ARCHITECTURE_SNAPSHOT.md with real architecture
136
+ - [ ] Create individual workflow files (8-15 total)
137
+ - [ ] Populate CODE_TO_WORKFLOW_MAP.md with file references
138
+ - [ ] Update WORKFLOW_INDEX.md with all workflows
139
+
140
+ ### Phase 4: Index Generation
141
+ - [ ] Generate proper category indexes
142
+ - [ ] Create semantic anchors for key functions
143
+ - [ ] Build reverse lookup indexes
144
+
145
+ ### Phase 5: Validation
146
+ - [ ] Verify line number accuracy (target: 60%+)
147
+ - [ ] Check all internal links
148
+ - [ ] Confirm no placeholder values remain
149
+
150
+ ### Phase 6: Finalization
151
+ - [ ] Generate summary report
152
+ - [ ] Update AI_CONTEXT.md with project-specific info
153
+ - [ ] Create maintenance schedule
154
+
155
+ ---
156
+
157
+ ## Quick Start Command
158
+
159
+ \`\`\`bash
160
+ @context-engineer "Complete initialization for ${projectName} using the pre-analysis in INIT_REQUEST.md"
161
+ \`\`\`
162
+
163
+ Or use the dedicated command:
164
+
165
+ \`\`\`bash
166
+ /rpi-research "Complete context engineering initialization"
167
+ \`\`\`
168
+
169
+ ---
170
+
171
+ ## Files to Update
172
+
173
+ | File | Action |
174
+ |------|--------|
175
+ | \`context/ARCHITECTURE_SNAPSHOT.md\` | Populate with real architecture |
176
+ | \`context/WORKFLOW_INDEX.md\` | Add all discovered workflows |
177
+ | \`context/CODE_TO_WORKFLOW_MAP.md\` | Map files to workflows |
178
+ | \`context/workflows/*.md\` | Create individual workflow docs |
179
+ | \`indexes/workflows/CATEGORY_INDEX.md\` | Generate category index |
180
+ | \`../AI_CONTEXT.md\` | Finalize with project info |
181
+
182
+ ---
183
+
184
+ ## Pre-Analysis Data
185
+
186
+ The following JSON contains the full pre-analysis for programmatic use:
187
+
188
+ \`\`\`json
189
+ ${JSON.stringify(analysis.summary || {}, null, 2)}
190
+ \`\`\`
191
+
192
+ ---
193
+
194
+ *This file will be deleted automatically after successful initialization.*
195
+ *Generated by create-ai-context v${pkgVersion}*
196
+ `;
197
+
198
+ const instructionPath = path.join(contextDir, INIT_REQUEST_FILE);
199
+ fs.writeFileSync(instructionPath, instructions);
200
+
201
+ return instructionPath;
202
+ }
203
+
204
+ /**
205
+ * Format entry points for markdown
206
+ * @param {object[]} entryPoints - Entry points array
207
+ * @returns {string}
208
+ */
209
+ function formatEntryPoints(entryPoints) {
210
+ if (!entryPoints || entryPoints.length === 0) {
211
+ return '*No entry points discovered during pre-analysis. AI analysis will discover them.*\n';
212
+ }
213
+
214
+ const limited = entryPoints.slice(0, 15);
215
+ let output = '| File | Line | Route | Method |\n|------|------|-------|--------|\n';
216
+
217
+ for (const ep of limited) {
218
+ output += `| \`${ep.file}\` | ${ep.line} | ${ep.route || '-'} | ${ep.method || '-'} |\n`;
219
+ }
220
+
221
+ if (entryPoints.length > 15) {
222
+ output += `\n*...and ${entryPoints.length - 15} more entry points*\n`;
223
+ }
224
+
225
+ return output;
226
+ }
227
+
228
+ /**
229
+ * Format workflows for markdown
230
+ * @param {object[]} workflows - Workflows array
231
+ * @returns {string}
232
+ */
233
+ function formatWorkflows(workflows) {
234
+ if (!workflows || workflows.length === 0) {
235
+ return '*No workflows discovered during pre-analysis. AI analysis will discover them.*\n';
236
+ }
237
+
238
+ let output = '| Workflow | Category | Complexity | Files | Confidence |\n|----------|----------|------------|-------|------------|\n';
239
+
240
+ for (const wf of workflows) {
241
+ output += `| **${wf.name}** | ${wf.category} | ${wf.complexity} | ${wf.fileCount || wf.files?.length || 0} | ${wf.confidence || '-'}% |\n`;
242
+ }
243
+
244
+ return output;
245
+ }
246
+
247
+ /**
248
+ * Format architecture layers for markdown
249
+ * @param {object[]} layers - Layers array
250
+ * @returns {string}
251
+ */
252
+ function formatLayers(layers) {
253
+ if (!layers || layers.length === 0) {
254
+ return '*Architecture layers will be determined during AI analysis.*\n';
255
+ }
256
+
257
+ let output = '| Layer | Directories | Purpose |\n|-------|-------------|----------|\n';
258
+
259
+ for (const layer of layers) {
260
+ output += `| **${layer.name}** | ${layer.directories?.join(', ') || '-'} | ${layer.purpose || '-'} |\n`;
261
+ }
262
+
263
+ return output;
264
+ }
265
+
266
+ /**
267
+ * Format dependencies for markdown
268
+ * @param {object[]} dependencies - Dependencies array
269
+ * @returns {string}
270
+ */
271
+ function formatDependencies(dependencies) {
272
+ if (!dependencies || dependencies.length === 0) {
273
+ return '*No dependencies detected.*\n';
274
+ }
275
+
276
+ // Group by ecosystem
277
+ const byEcosystem = {};
278
+ for (const dep of dependencies) {
279
+ const eco = dep.ecosystem || 'unknown';
280
+ if (!byEcosystem[eco]) byEcosystem[eco] = [];
281
+ byEcosystem[eco].push(dep);
282
+ }
283
+
284
+ let output = '';
285
+ for (const [ecosystem, deps] of Object.entries(byEcosystem)) {
286
+ output += `\n**${ecosystem.toUpperCase()}** (${deps.length}):\n`;
287
+ const topDeps = deps.slice(0, 10);
288
+ output += topDeps.map(d => `- ${d.name}@${d.version}`).join('\n') + '\n';
289
+ if (deps.length > 10) {
290
+ output += `- *...and ${deps.length - 10} more*\n`;
291
+ }
292
+ }
293
+
294
+ return output;
295
+ }
296
+
297
+ /**
298
+ * Get package version
299
+ * @returns {string}
300
+ */
301
+ function getPackageVersion() {
302
+ try {
303
+ const pkgPath = path.join(__dirname, '..', 'package.json');
304
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
305
+ return pkg.version || '1.0.0';
306
+ } catch (error) {
307
+ // Silently fall back to default version
308
+ return '1.0.0';
309
+ }
310
+ }
311
+
312
+ /**
313
+ * Check initialization status
314
+ * @param {string} contextDir - .ai-context directory path
315
+ * @returns {object}
316
+ */
317
+ function checkInitializationStatus(contextDir) {
318
+ const markerPath = path.join(contextDir, INIT_MARKER);
319
+
320
+ if (!fs.existsSync(markerPath)) {
321
+ return { status: 'not-started', request: null };
322
+ }
323
+
324
+ try {
325
+ const request = JSON.parse(fs.readFileSync(markerPath, 'utf-8'));
326
+ const completedPhases = request.phases.filter(p => p.status === 'completed').length;
327
+ const totalPhases = request.phases.length;
328
+
329
+ return {
330
+ status: completedPhases === totalPhases ? 'completed' : 'in-progress',
331
+ progress: Math.round((completedPhases / totalPhases) * 100),
332
+ completedPhases,
333
+ totalPhases,
334
+ request
335
+ };
336
+ } catch {
337
+ return { status: 'error', request: null };
338
+ }
339
+ }
340
+
341
+ /**
342
+ * Update initialization progress
343
+ * @param {string} contextDir - .ai-context directory path
344
+ * @param {string} phaseId - Phase ID to update
345
+ * @param {string} status - New status ('in-progress', 'completed', 'failed')
346
+ * @param {number} progress - Progress percentage (0-100)
347
+ */
348
+ function updateInitializationProgress(contextDir, phaseId, status, progress = 0) {
349
+ const markerPath = path.join(contextDir, INIT_MARKER);
350
+
351
+ if (!fs.existsSync(markerPath)) {
352
+ return false;
353
+ }
354
+
355
+ try {
356
+ const request = JSON.parse(fs.readFileSync(markerPath, 'utf-8'));
357
+ const phase = request.phases.find(p => p.id === phaseId);
358
+
359
+ if (phase) {
360
+ phase.status = status;
361
+ phase.progress = progress;
362
+ phase.updatedAt = new Date().toISOString();
363
+ }
364
+
365
+ // Check if all phases are completed
366
+ const allCompleted = request.phases.every(p => p.status === 'completed');
367
+ if (allCompleted) {
368
+ request.status = 'completed';
369
+ request.completedAt = new Date().toISOString();
370
+ } else {
371
+ request.status = 'in-progress';
372
+ }
373
+
374
+ fs.writeFileSync(markerPath, JSON.stringify(request, null, 2));
375
+ return true;
376
+ } catch {
377
+ return false;
378
+ }
379
+ }
380
+
381
+ /**
382
+ * Mark initialization as complete and clean up
383
+ * @param {string} contextDir - .ai-context directory path
384
+ * @param {boolean} removeMarker - Whether to remove the marker file
385
+ */
386
+ function completeInitialization(contextDir, removeMarker = true) {
387
+ const markerPath = path.join(contextDir, INIT_MARKER);
388
+ const instructionPath = path.join(contextDir, INIT_REQUEST_FILE);
389
+
390
+ if (fs.existsSync(markerPath)) {
391
+ const request = JSON.parse(fs.readFileSync(markerPath, 'utf-8'));
392
+ request.status = 'completed';
393
+ request.completedAt = new Date().toISOString();
394
+
395
+ // Archive to progress file
396
+ const progressPath = path.join(contextDir, PROGRESS_FILE);
397
+ fs.writeFileSync(progressPath, JSON.stringify(request, null, 2));
398
+
399
+ if (removeMarker) {
400
+ fs.unlinkSync(markerPath);
401
+ }
402
+ }
403
+
404
+ // Remove instruction file
405
+ if (removeMarker && fs.existsSync(instructionPath)) {
406
+ fs.unlinkSync(instructionPath);
407
+ }
408
+ }
409
+
410
+ /**
411
+ * Check if initialization is pending
412
+ * @param {string} contextDir - .ai-context directory path
413
+ * @returns {boolean}
414
+ */
415
+ function isInitializationPending(contextDir) {
416
+ const markerPath = path.join(contextDir, INIT_MARKER);
417
+ return fs.existsSync(markerPath);
418
+ }
419
+
420
+ module.exports = {
421
+ createInitializationRequest,
422
+ generateAgentInstructions,
423
+ checkInitializationStatus,
424
+ updateInitializationProgress,
425
+ completeInitialization,
426
+ isInitializationPending,
427
+ INIT_MARKER,
428
+ INIT_REQUEST_FILE,
429
+ PROGRESS_FILE,
430
+ AI_CONTEXT_DIR,
431
+ AI_CONTEXT_FILE
432
+ };