create-universal-ai-context 2.4.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.
- package/LICENSE +21 -21
- package/README.md +331 -294
- package/bin/create-ai-context.js +1507 -764
- package/lib/adapters/aider.js +131 -131
- package/lib/adapters/antigravity.js +205 -205
- package/lib/adapters/claude.js +397 -397
- package/lib/adapters/cline.js +125 -125
- package/lib/adapters/continue.js +138 -138
- package/lib/adapters/copilot.js +131 -131
- package/lib/adapters/index.js +78 -78
- package/lib/adapters/windsurf.js +138 -138
- package/lib/ai-context-generator.js +234 -234
- package/lib/ai-orchestrator.js +432 -432
- package/lib/call-tracer.js +444 -444
- package/lib/content-preservation.js +243 -243
- package/lib/cross-tool-sync/file-watcher.js +274 -274
- package/lib/cross-tool-sync/index.js +41 -40
- package/lib/cross-tool-sync/sync-manager.js +540 -512
- package/lib/cross-tool-sync/sync-service.js +297 -297
- package/lib/detector.js +726 -726
- package/lib/doc-discovery.js +741 -741
- package/lib/drift-checker.js +920 -920
- package/lib/environment-detector.js +239 -239
- package/lib/index.js +399 -399
- package/lib/install-hooks.js +82 -82
- package/lib/installer.js +419 -419
- package/lib/migrate.js +328 -328
- package/lib/placeholder.js +632 -632
- package/lib/prompts.js +341 -341
- package/lib/smart-merge.js +540 -540
- package/lib/spinner.js +60 -60
- package/lib/static-analyzer.js +729 -729
- package/lib/template-coordination.js +148 -148
- package/lib/template-populator.js +843 -843
- package/lib/template-renderer.js +392 -392
- package/lib/utils/fs-wrapper.js +79 -79
- package/lib/utils/path-utils.js +60 -60
- package/lib/validate.js +155 -155
- package/package.json +1 -1
- package/templates/AI_CONTEXT.md.template +245 -245
- package/templates/base/README.md +260 -257
- package/templates/base/RPI_WORKFLOW_PLAN.md +325 -320
- package/templates/base/agents/api-developer.md +76 -76
- package/templates/base/agents/context-engineer.md +525 -525
- package/templates/base/agents/core-architect.md +76 -76
- package/templates/base/agents/database-ops.md +76 -76
- package/templates/base/agents/deployment-ops.md +76 -76
- package/templates/base/agents/integration-hub.md +76 -76
- package/templates/base/analytics/README.md +114 -114
- package/templates/base/automation/config.json +58 -58
- package/templates/base/automation/generators/code-mapper.js +308 -308
- package/templates/base/automation/generators/index-builder.js +321 -321
- package/templates/base/automation/hooks/post-commit.sh +83 -83
- package/templates/base/automation/hooks/pre-commit.sh +103 -103
- package/templates/base/ci-templates/README.md +108 -108
- package/templates/base/ci-templates/github-actions/context-check.yml +144 -144
- package/templates/base/ci-templates/github-actions/validate-docs.yml +105 -105
- package/templates/base/commands/analytics.md +238 -238
- package/templates/base/commands/auto-sync.md +172 -172
- package/templates/base/commands/collab.md +194 -194
- package/templates/base/commands/context-optimize.md +226 -0
- package/templates/base/commands/help.md +485 -450
- package/templates/base/commands/rpi-implement.md +164 -115
- package/templates/base/commands/rpi-plan.md +147 -93
- package/templates/base/commands/rpi-research.md +145 -88
- package/templates/base/commands/session-resume.md +144 -144
- package/templates/base/commands/session-save.md +112 -112
- package/templates/base/commands/validate-all.md +77 -77
- package/templates/base/commands/verify-docs-current.md +86 -86
- package/templates/base/config/base.json +57 -57
- package/templates/base/config/environments/development.json +13 -13
- package/templates/base/config/environments/production.json +17 -17
- package/templates/base/config/environments/staging.json +13 -13
- package/templates/base/config/local.json.example +21 -21
- package/templates/base/context/.meta/generated-at.json +18 -18
- package/templates/base/context/ARCHITECTURE_SNAPSHOT.md +156 -156
- package/templates/base/context/CODE_TO_WORKFLOW_MAP.md +94 -94
- package/templates/base/context/FILE_OWNERSHIP.md +57 -57
- package/templates/base/context/INTEGRATION_POINTS.md +92 -92
- package/templates/base/context/KNOWN_GOTCHAS.md +195 -195
- package/templates/base/context/TESTING_MAP.md +95 -95
- package/templates/base/context/WORKFLOW_INDEX.md +129 -129
- package/templates/base/context/workflows/WORKFLOW_TEMPLATE.md +294 -294
- package/templates/base/indexes/agents/CAPABILITY_MATRIX.md +255 -255
- package/templates/base/indexes/agents/CATEGORY_INDEX.md +44 -44
- package/templates/base/indexes/code/CATEGORY_INDEX.md +38 -38
- package/templates/base/indexes/routing/CATEGORY_INDEX.md +39 -39
- package/templates/base/indexes/search/CATEGORY_INDEX.md +39 -39
- package/templates/base/indexes/workflows/CATEGORY_INDEX.md +38 -38
- package/templates/base/knowledge/README.md +98 -98
- package/templates/base/knowledge/sessions/README.md +88 -88
- package/templates/base/knowledge/sessions/TEMPLATE.md +150 -150
- package/templates/base/knowledge/shared/decisions/0001-adopt-context-engineering.md +144 -144
- package/templates/base/knowledge/shared/decisions/README.md +49 -49
- package/templates/base/knowledge/shared/decisions/TEMPLATE.md +123 -123
- package/templates/base/knowledge/shared/patterns/README.md +62 -62
- package/templates/base/knowledge/shared/patterns/TEMPLATE.md +120 -120
- package/templates/base/plans/PLAN_TEMPLATE.md +316 -250
- package/templates/base/research/RESEARCH_TEMPLATE.md +245 -153
- package/templates/base/schemas/agent.schema.json +141 -141
- package/templates/base/schemas/anchors.schema.json +54 -54
- package/templates/base/schemas/automation.schema.json +93 -93
- package/templates/base/schemas/command.schema.json +134 -134
- package/templates/base/schemas/hashes.schema.json +40 -40
- package/templates/base/schemas/manifest.schema.json +117 -117
- package/templates/base/schemas/plan.schema.json +136 -136
- package/templates/base/schemas/research.schema.json +115 -115
- package/templates/base/schemas/roles.schema.json +34 -34
- package/templates/base/schemas/session.schema.json +77 -77
- package/templates/base/schemas/settings.schema.json +244 -244
- package/templates/base/schemas/staleness.schema.json +53 -53
- package/templates/base/schemas/team-config.schema.json +42 -42
- package/templates/base/schemas/workflow.schema.json +126 -126
- package/templates/base/session/checkpoints/.gitkeep +2 -2
- package/templates/base/session/current/state.json +20 -20
- package/templates/base/session/history/.gitkeep +2 -2
- package/templates/base/settings.json +3 -3
- package/templates/base/standards/COMPATIBILITY.md +219 -219
- package/templates/base/standards/EXTENSION_GUIDELINES.md +280 -280
- package/templates/base/standards/QUALITY_CHECKLIST.md +211 -211
- package/templates/base/standards/README.md +66 -66
- package/templates/base/sync/anchors.json +6 -6
- package/templates/base/sync/hashes.json +6 -6
- package/templates/base/sync/staleness.json +10 -10
- package/templates/base/team/README.md +168 -168
- package/templates/base/team/config.json +79 -79
- package/templates/base/team/roles.json +145 -145
- package/templates/base/tools/bin/claude-context.js +151 -151
- package/templates/base/tools/lib/anchor-resolver.js +276 -276
- package/templates/base/tools/lib/config-loader.js +363 -363
- package/templates/base/tools/lib/detector.js +350 -350
- package/templates/base/tools/lib/diagnose.js +206 -206
- package/templates/base/tools/lib/drift-detector.js +373 -373
- package/templates/base/tools/lib/errors.js +199 -199
- package/templates/base/tools/lib/index.js +36 -36
- package/templates/base/tools/lib/init.js +192 -192
- package/templates/base/tools/lib/logger.js +230 -230
- package/templates/base/tools/lib/placeholder.js +201 -201
- package/templates/base/tools/lib/session-manager.js +354 -354
- package/templates/base/tools/lib/validate.js +521 -521
- package/templates/base/tools/package.json +49 -49
- package/templates/handlebars/aider-config.hbs +146 -80
- package/templates/handlebars/antigravity.hbs +377 -377
- package/templates/handlebars/claude.hbs +183 -183
- package/templates/handlebars/cline.hbs +62 -62
- package/templates/handlebars/continue-config.hbs +116 -116
- package/templates/handlebars/copilot.hbs +130 -130
- package/templates/handlebars/partials/gotcha-list.hbs +11 -11
- package/templates/handlebars/partials/header.hbs +3 -3
- package/templates/handlebars/partials/workflow-summary.hbs +16 -16
- package/templates/handlebars/windsurf-rules.hbs +69 -69
- package/templates/hooks/post-commit.hbs +28 -29
- package/templates/hooks/pre-commit.hbs +46 -46
package/lib/template-renderer.js
CHANGED
|
@@ -1,392 +1,392 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AI Context Engineering - Template Renderer
|
|
3
|
-
*
|
|
4
|
-
* Handlebars-based template rendering engine for generating
|
|
5
|
-
* context files for multiple AI tools.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const Handlebars = require('handlebars');
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
const { getToolCoordinationHeader, getToolCoordinationFooter, getUniversalContextReference } = require('./template-coordination');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Templates directory
|
|
15
|
-
*/
|
|
16
|
-
const TEMPLATES_DIR = path.join(__dirname, '..', 'templates', 'handlebars');
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Multi-file markers for Antigravity templates
|
|
20
|
-
*/
|
|
21
|
-
const FILE_START_MARKER = /\{\{\{fileStart\s+"([^"]+)"\}\}\}/g;
|
|
22
|
-
const FILE_END_MARKER = /\{\{\{fileEnd\}\}\}/g;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Register custom Handlebars helpers
|
|
26
|
-
*/
|
|
27
|
-
function registerHelpers() {
|
|
28
|
-
// Join array with delimiter
|
|
29
|
-
Handlebars.registerHelper('join', function(array, delimiter) {
|
|
30
|
-
if (!Array.isArray(array)) return '';
|
|
31
|
-
return array.join(delimiter || ', ');
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
// Get first N items from array
|
|
35
|
-
Handlebars.registerHelper('first', function(array, n) {
|
|
36
|
-
if (!Array.isArray(array)) return [];
|
|
37
|
-
return array.slice(0, n);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// Get length of array
|
|
41
|
-
Handlebars.registerHelper('length', function(array) {
|
|
42
|
-
if (!Array.isArray(array)) return 0;
|
|
43
|
-
return array.length;
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// Truncate string to max length
|
|
47
|
-
Handlebars.registerHelper('truncate', function(str, maxLength) {
|
|
48
|
-
if (typeof str !== 'string') return '';
|
|
49
|
-
if (str.length <= maxLength) return str;
|
|
50
|
-
return str.substring(0, maxLength) + '...';
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Slugify string for filenames
|
|
54
|
-
Handlebars.registerHelper('slugify', function(str) {
|
|
55
|
-
if (typeof str !== 'string') return '';
|
|
56
|
-
return str
|
|
57
|
-
.toLowerCase()
|
|
58
|
-
.replace(/[^a-z0-9]+/g, '-')
|
|
59
|
-
.replace(/^-|-$/g, '');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Concat strings
|
|
63
|
-
Handlebars.registerHelper('concat', function(...args) {
|
|
64
|
-
// Remove the Handlebars options object from args
|
|
65
|
-
args.pop();
|
|
66
|
-
return args.join('');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
// Uppercase string
|
|
70
|
-
Handlebars.registerHelper('uppercase', function(str) {
|
|
71
|
-
if (typeof str !== 'string') return '';
|
|
72
|
-
return str.toUpperCase();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// Lowercase string
|
|
76
|
-
Handlebars.registerHelper('lowercase', function(str) {
|
|
77
|
-
if (typeof str !== 'string') return '';
|
|
78
|
-
return str.toLowerCase();
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// Check if equal
|
|
82
|
-
Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
|
|
83
|
-
return (arg1 === arg2) ? options.fn(this) : options.inverse(this);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
// Check if not equal
|
|
87
|
-
Handlebars.registerHelper('ifNotEquals', function(arg1, arg2, options) {
|
|
88
|
-
return (arg1 !== arg2) ? options.fn(this) : options.inverse(this);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
// Date formatting
|
|
92
|
-
Handlebars.registerHelper('formatDate', function(date) {
|
|
93
|
-
if (!date) return new Date().toISOString();
|
|
94
|
-
return new Date(date).toISOString();
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// File markers for multi-file templates (Antigravity)
|
|
98
|
-
Handlebars.registerHelper('fileStart', function(filename) {
|
|
99
|
-
return new Handlebars.SafeString(`<<<FILE_START:${filename}>>>`);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
Handlebars.registerHelper('fileEnd', function() {
|
|
103
|
-
return new Handlebars.SafeString('<<<FILE_END>>>');
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Register partials from the partials directory
|
|
109
|
-
* @param {string} partialsDir - Path to partials directory
|
|
110
|
-
*/
|
|
111
|
-
function registerPartials(partialsDir) {
|
|
112
|
-
const defaultPartialsDir = partialsDir || path.join(TEMPLATES_DIR, 'partials');
|
|
113
|
-
|
|
114
|
-
if (!fs.existsSync(defaultPartialsDir)) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const files = fs.readdirSync(defaultPartialsDir)
|
|
119
|
-
.filter(f => f.endsWith('.hbs'));
|
|
120
|
-
|
|
121
|
-
for (const file of files) {
|
|
122
|
-
const name = path.basename(file, '.hbs');
|
|
123
|
-
const content = fs.readFileSync(path.join(defaultPartialsDir, file), 'utf-8');
|
|
124
|
-
Handlebars.registerPartial(name, content);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Initialize the template engine
|
|
130
|
-
*/
|
|
131
|
-
function initialize() {
|
|
132
|
-
registerHelpers();
|
|
133
|
-
registerPartials();
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Compile and render a template
|
|
138
|
-
* @param {string} templatePath - Path to the template file
|
|
139
|
-
* @param {object} context - Context data for rendering
|
|
140
|
-
* @returns {string} Rendered content
|
|
141
|
-
*/
|
|
142
|
-
function renderTemplate(templatePath, context) {
|
|
143
|
-
if (!fs.existsSync(templatePath)) {
|
|
144
|
-
throw new Error(`Template not found: ${templatePath}`);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const source = fs.readFileSync(templatePath, 'utf-8');
|
|
148
|
-
const template = Handlebars.compile(source, { noEscape: false });
|
|
149
|
-
return template(context);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Render a template by name (looks in templates/handlebars/)
|
|
154
|
-
* @param {string} templateName - Template name (without .hbs extension)
|
|
155
|
-
* @param {object} context - Context data for rendering
|
|
156
|
-
* @returns {string} Rendered content
|
|
157
|
-
*/
|
|
158
|
-
function renderTemplateByName(templateName, context) {
|
|
159
|
-
const templatePath = path.join(TEMPLATES_DIR, `${templateName}.hbs`);
|
|
160
|
-
return renderTemplate(templatePath, context);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Parse multi-file output from rendered template (for Antigravity)
|
|
165
|
-
* @param {string} rendered - Rendered template content with file markers
|
|
166
|
-
* @returns {object[]} Array of { filename, content } objects
|
|
167
|
-
*/
|
|
168
|
-
function parseMultiFileOutput(rendered) {
|
|
169
|
-
const files = [];
|
|
170
|
-
const parts = rendered.split('<<<FILE_START:');
|
|
171
|
-
|
|
172
|
-
for (let i = 1; i < parts.length; i++) {
|
|
173
|
-
const part = parts[i];
|
|
174
|
-
const endMarkerIndex = part.indexOf('>>>');
|
|
175
|
-
|
|
176
|
-
if (endMarkerIndex === -1) continue;
|
|
177
|
-
|
|
178
|
-
const filename = part.substring(0, endMarkerIndex);
|
|
179
|
-
const rest = part.substring(endMarkerIndex + 3);
|
|
180
|
-
|
|
181
|
-
const fileEndIndex = rest.indexOf('<<<FILE_END>>>');
|
|
182
|
-
const content = fileEndIndex !== -1
|
|
183
|
-
? rest.substring(0, fileEndIndex).trim()
|
|
184
|
-
: rest.trim();
|
|
185
|
-
|
|
186
|
-
files.push({ filename, content });
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return files;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Render a multi-file template (for Antigravity)
|
|
194
|
-
* @param {string} templatePath - Path to the template file
|
|
195
|
-
* @param {object} context - Context data for rendering
|
|
196
|
-
* @returns {object[]} Array of { filename, content } objects
|
|
197
|
-
*/
|
|
198
|
-
function renderMultiFileTemplate(templatePath, context) {
|
|
199
|
-
const rendered = renderTemplate(templatePath, context);
|
|
200
|
-
return parseMultiFileOutput(rendered);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Get available template names
|
|
205
|
-
* @returns {string[]} Array of template names
|
|
206
|
-
*/
|
|
207
|
-
function getAvailableTemplates() {
|
|
208
|
-
if (!fs.existsSync(TEMPLATES_DIR)) {
|
|
209
|
-
return [];
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return fs.readdirSync(TEMPLATES_DIR)
|
|
213
|
-
.filter(f => f.endsWith('.hbs') && !fs.statSync(path.join(TEMPLATES_DIR, f)).isDirectory())
|
|
214
|
-
.map(f => path.basename(f, '.hbs'));
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Build default context from analysis results
|
|
219
|
-
* @param {object} analysis - Analysis results from static analyzer
|
|
220
|
-
* @param {object} config - Configuration from CLI
|
|
221
|
-
* @param {string} toolName - AI tool name (claude, copilot, cline, antigravity)
|
|
222
|
-
* @returns {object} Context object for templates
|
|
223
|
-
*/
|
|
224
|
-
function buildContext(analysis, config, toolName = 'claude') {
|
|
225
|
-
const projectName = config.projectName || 'project';
|
|
226
|
-
const techStack = analysis.techStack || config.techStack || {};
|
|
227
|
-
const version = getPackageVersion();
|
|
228
|
-
|
|
229
|
-
return {
|
|
230
|
-
// Project identity
|
|
231
|
-
project: {
|
|
232
|
-
name: projectName,
|
|
233
|
-
description: config.description || `${projectName} application`,
|
|
234
|
-
tech_stack: techStack.languages || [],
|
|
235
|
-
primary_language: techStack.languages?.[0] || 'unknown'
|
|
236
|
-
},
|
|
237
|
-
|
|
238
|
-
// Architecture (from analysis)
|
|
239
|
-
architecture: {
|
|
240
|
-
pattern: analysis.architecture?.pattern || 'layered',
|
|
241
|
-
layers: (analysis.architecture?.layers || []).map(l => ({
|
|
242
|
-
name: l.name,
|
|
243
|
-
path: l.directories?.[0] || l.name + '/',
|
|
244
|
-
purpose: l.purpose || l.name
|
|
245
|
-
}))
|
|
246
|
-
},
|
|
247
|
-
|
|
248
|
-
// Workflows (from analysis)
|
|
249
|
-
workflows: (analysis.workflows || []).map(wf => ({
|
|
250
|
-
name: wf.name,
|
|
251
|
-
category: wf.category || 'general',
|
|
252
|
-
complexity: wf.complexity || 'MEDIUM',
|
|
253
|
-
description: wf.description || `Handles ${wf.name} functionality`,
|
|
254
|
-
files: wf.files || [],
|
|
255
|
-
entry_points: (wf.entryPoints || []).map(ep => ({
|
|
256
|
-
endpoint: ep.route || ep.name || 'unknown',
|
|
257
|
-
file: ep.file,
|
|
258
|
-
line: ep.line,
|
|
259
|
-
method: ep.method || 'GET',
|
|
260
|
-
purpose: ep.purpose || 'Entry point'
|
|
261
|
-
}))
|
|
262
|
-
})),
|
|
263
|
-
|
|
264
|
-
// Gotchas (empty by default, filled by AI or manual)
|
|
265
|
-
gotchas: [],
|
|
266
|
-
|
|
267
|
-
// Key files (from analysis)
|
|
268
|
-
key_files: {
|
|
269
|
-
entry_points: (analysis.entryPoints || []).map(ep => ep.file),
|
|
270
|
-
api_routes: [],
|
|
271
|
-
config: [],
|
|
272
|
-
models: []
|
|
273
|
-
},
|
|
274
|
-
|
|
275
|
-
// Commands (from tech stack)
|
|
276
|
-
commands: {
|
|
277
|
-
install: techStack.commands?.install || 'npm install',
|
|
278
|
-
dev: techStack.commands?.dev || 'npm run dev',
|
|
279
|
-
test: techStack.commands?.test || 'npm test',
|
|
280
|
-
build: techStack.commands?.build || 'npm run build'
|
|
281
|
-
},
|
|
282
|
-
|
|
283
|
-
// Environment
|
|
284
|
-
environment: {
|
|
285
|
-
required: [],
|
|
286
|
-
optional: []
|
|
287
|
-
},
|
|
288
|
-
|
|
289
|
-
// Critical constraints
|
|
290
|
-
critical_constraints: [],
|
|
291
|
-
|
|
292
|
-
// Metadata with tool-specific coordination header
|
|
293
|
-
metadata: {
|
|
294
|
-
header: getToolCoordinationHeader(toolName, version),
|
|
295
|
-
timestamp: new Date().toISOString(),
|
|
296
|
-
generator_version: version,
|
|
297
|
-
source_ref: projectName,
|
|
298
|
-
tool_name: toolName
|
|
299
|
-
},
|
|
300
|
-
|
|
301
|
-
// Tool coordination footer
|
|
302
|
-
coordination: {
|
|
303
|
-
footer: getToolCoordinationFooter(toolName),
|
|
304
|
-
universal_context: getUniversalContextReference()
|
|
305
|
-
},
|
|
306
|
-
|
|
307
|
-
// Registry (for federation compatibility)
|
|
308
|
-
registry: {
|
|
309
|
-
name: projectName,
|
|
310
|
-
submodule: projectName,
|
|
311
|
-
description: config.description || `${projectName} application`,
|
|
312
|
-
tech_stack: techStack.languages || [],
|
|
313
|
-
primary_language: techStack.languages?.[0] || 'unknown',
|
|
314
|
-
architecture: {
|
|
315
|
-
pattern: analysis.architecture?.pattern || 'layered',
|
|
316
|
-
layers: (analysis.architecture?.layers || []).map(l => l.name || l),
|
|
317
|
-
dependencies: {}
|
|
318
|
-
},
|
|
319
|
-
key_files: {
|
|
320
|
-
entry_points: (analysis.entryPoints || []).map(ep => ep.file),
|
|
321
|
-
api_routes: [],
|
|
322
|
-
core: [],
|
|
323
|
-
config: []
|
|
324
|
-
},
|
|
325
|
-
commands: {
|
|
326
|
-
install: techStack.commands?.install || 'npm install',
|
|
327
|
-
run: techStack.commands?.dev || 'npm run dev',
|
|
328
|
-
test: techStack.commands?.test || 'npm test',
|
|
329
|
-
build: techStack.commands?.build || 'npm run build'
|
|
330
|
-
},
|
|
331
|
-
environment: {
|
|
332
|
-
required: [],
|
|
333
|
-
optional: []
|
|
334
|
-
},
|
|
335
|
-
critical_constraints: [],
|
|
336
|
-
integrations: {
|
|
337
|
-
external: {},
|
|
338
|
-
internal: {}
|
|
339
|
-
}
|
|
340
|
-
},
|
|
341
|
-
|
|
342
|
-
// Submodule (for federation compatibility)
|
|
343
|
-
submodule: {
|
|
344
|
-
ports: {
|
|
345
|
-
default: 3000
|
|
346
|
-
}
|
|
347
|
-
},
|
|
348
|
-
|
|
349
|
-
// Cross-repo references (for federation)
|
|
350
|
-
cross_repos: [],
|
|
351
|
-
|
|
352
|
-
// Active task (for task dispatch)
|
|
353
|
-
active_task: null,
|
|
354
|
-
|
|
355
|
-
// Federation info
|
|
356
|
-
federation: {
|
|
357
|
-
is_federated: false,
|
|
358
|
-
subproject: null,
|
|
359
|
-
cross_repos: []
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
/**
|
|
365
|
-
* Get package version
|
|
366
|
-
* @returns {string} Package version
|
|
367
|
-
*/
|
|
368
|
-
function getPackageVersion() {
|
|
369
|
-
try {
|
|
370
|
-
const pkgPath = path.join(__dirname, '..', 'package.json');
|
|
371
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
372
|
-
return pkg.version || '2.0.0';
|
|
373
|
-
} catch {
|
|
374
|
-
return '2.0.0';
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// Initialize on module load
|
|
379
|
-
initialize();
|
|
380
|
-
|
|
381
|
-
module.exports = {
|
|
382
|
-
renderTemplate,
|
|
383
|
-
renderTemplateByName,
|
|
384
|
-
renderMultiFileTemplate,
|
|
385
|
-
parseMultiFileOutput,
|
|
386
|
-
buildContext,
|
|
387
|
-
registerPartials,
|
|
388
|
-
registerHelpers,
|
|
389
|
-
initialize,
|
|
390
|
-
getAvailableTemplates,
|
|
391
|
-
TEMPLATES_DIR
|
|
392
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* AI Context Engineering - Template Renderer
|
|
3
|
+
*
|
|
4
|
+
* Handlebars-based template rendering engine for generating
|
|
5
|
+
* context files for multiple AI tools.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const Handlebars = require('handlebars');
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const { getToolCoordinationHeader, getToolCoordinationFooter, getUniversalContextReference } = require('./template-coordination');
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Templates directory
|
|
15
|
+
*/
|
|
16
|
+
const TEMPLATES_DIR = path.join(__dirname, '..', 'templates', 'handlebars');
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Multi-file markers for Antigravity templates
|
|
20
|
+
*/
|
|
21
|
+
const FILE_START_MARKER = /\{\{\{fileStart\s+"([^"]+)"\}\}\}/g;
|
|
22
|
+
const FILE_END_MARKER = /\{\{\{fileEnd\}\}\}/g;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Register custom Handlebars helpers
|
|
26
|
+
*/
|
|
27
|
+
function registerHelpers() {
|
|
28
|
+
// Join array with delimiter
|
|
29
|
+
Handlebars.registerHelper('join', function(array, delimiter) {
|
|
30
|
+
if (!Array.isArray(array)) return '';
|
|
31
|
+
return array.join(delimiter || ', ');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Get first N items from array
|
|
35
|
+
Handlebars.registerHelper('first', function(array, n) {
|
|
36
|
+
if (!Array.isArray(array)) return [];
|
|
37
|
+
return array.slice(0, n);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Get length of array
|
|
41
|
+
Handlebars.registerHelper('length', function(array) {
|
|
42
|
+
if (!Array.isArray(array)) return 0;
|
|
43
|
+
return array.length;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Truncate string to max length
|
|
47
|
+
Handlebars.registerHelper('truncate', function(str, maxLength) {
|
|
48
|
+
if (typeof str !== 'string') return '';
|
|
49
|
+
if (str.length <= maxLength) return str;
|
|
50
|
+
return str.substring(0, maxLength) + '...';
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Slugify string for filenames
|
|
54
|
+
Handlebars.registerHelper('slugify', function(str) {
|
|
55
|
+
if (typeof str !== 'string') return '';
|
|
56
|
+
return str
|
|
57
|
+
.toLowerCase()
|
|
58
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
59
|
+
.replace(/^-|-$/g, '');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Concat strings
|
|
63
|
+
Handlebars.registerHelper('concat', function(...args) {
|
|
64
|
+
// Remove the Handlebars options object from args
|
|
65
|
+
args.pop();
|
|
66
|
+
return args.join('');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Uppercase string
|
|
70
|
+
Handlebars.registerHelper('uppercase', function(str) {
|
|
71
|
+
if (typeof str !== 'string') return '';
|
|
72
|
+
return str.toUpperCase();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Lowercase string
|
|
76
|
+
Handlebars.registerHelper('lowercase', function(str) {
|
|
77
|
+
if (typeof str !== 'string') return '';
|
|
78
|
+
return str.toLowerCase();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Check if equal
|
|
82
|
+
Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
|
|
83
|
+
return (arg1 === arg2) ? options.fn(this) : options.inverse(this);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Check if not equal
|
|
87
|
+
Handlebars.registerHelper('ifNotEquals', function(arg1, arg2, options) {
|
|
88
|
+
return (arg1 !== arg2) ? options.fn(this) : options.inverse(this);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Date formatting
|
|
92
|
+
Handlebars.registerHelper('formatDate', function(date) {
|
|
93
|
+
if (!date) return new Date().toISOString();
|
|
94
|
+
return new Date(date).toISOString();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// File markers for multi-file templates (Antigravity)
|
|
98
|
+
Handlebars.registerHelper('fileStart', function(filename) {
|
|
99
|
+
return new Handlebars.SafeString(`<<<FILE_START:${filename}>>>`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
Handlebars.registerHelper('fileEnd', function() {
|
|
103
|
+
return new Handlebars.SafeString('<<<FILE_END>>>');
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Register partials from the partials directory
|
|
109
|
+
* @param {string} partialsDir - Path to partials directory
|
|
110
|
+
*/
|
|
111
|
+
function registerPartials(partialsDir) {
|
|
112
|
+
const defaultPartialsDir = partialsDir || path.join(TEMPLATES_DIR, 'partials');
|
|
113
|
+
|
|
114
|
+
if (!fs.existsSync(defaultPartialsDir)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const files = fs.readdirSync(defaultPartialsDir)
|
|
119
|
+
.filter(f => f.endsWith('.hbs'));
|
|
120
|
+
|
|
121
|
+
for (const file of files) {
|
|
122
|
+
const name = path.basename(file, '.hbs');
|
|
123
|
+
const content = fs.readFileSync(path.join(defaultPartialsDir, file), 'utf-8');
|
|
124
|
+
Handlebars.registerPartial(name, content);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Initialize the template engine
|
|
130
|
+
*/
|
|
131
|
+
function initialize() {
|
|
132
|
+
registerHelpers();
|
|
133
|
+
registerPartials();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Compile and render a template
|
|
138
|
+
* @param {string} templatePath - Path to the template file
|
|
139
|
+
* @param {object} context - Context data for rendering
|
|
140
|
+
* @returns {string} Rendered content
|
|
141
|
+
*/
|
|
142
|
+
function renderTemplate(templatePath, context) {
|
|
143
|
+
if (!fs.existsSync(templatePath)) {
|
|
144
|
+
throw new Error(`Template not found: ${templatePath}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const source = fs.readFileSync(templatePath, 'utf-8');
|
|
148
|
+
const template = Handlebars.compile(source, { noEscape: false });
|
|
149
|
+
return template(context);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Render a template by name (looks in templates/handlebars/)
|
|
154
|
+
* @param {string} templateName - Template name (without .hbs extension)
|
|
155
|
+
* @param {object} context - Context data for rendering
|
|
156
|
+
* @returns {string} Rendered content
|
|
157
|
+
*/
|
|
158
|
+
function renderTemplateByName(templateName, context) {
|
|
159
|
+
const templatePath = path.join(TEMPLATES_DIR, `${templateName}.hbs`);
|
|
160
|
+
return renderTemplate(templatePath, context);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Parse multi-file output from rendered template (for Antigravity)
|
|
165
|
+
* @param {string} rendered - Rendered template content with file markers
|
|
166
|
+
* @returns {object[]} Array of { filename, content } objects
|
|
167
|
+
*/
|
|
168
|
+
function parseMultiFileOutput(rendered) {
|
|
169
|
+
const files = [];
|
|
170
|
+
const parts = rendered.split('<<<FILE_START:');
|
|
171
|
+
|
|
172
|
+
for (let i = 1; i < parts.length; i++) {
|
|
173
|
+
const part = parts[i];
|
|
174
|
+
const endMarkerIndex = part.indexOf('>>>');
|
|
175
|
+
|
|
176
|
+
if (endMarkerIndex === -1) continue;
|
|
177
|
+
|
|
178
|
+
const filename = part.substring(0, endMarkerIndex);
|
|
179
|
+
const rest = part.substring(endMarkerIndex + 3);
|
|
180
|
+
|
|
181
|
+
const fileEndIndex = rest.indexOf('<<<FILE_END>>>');
|
|
182
|
+
const content = fileEndIndex !== -1
|
|
183
|
+
? rest.substring(0, fileEndIndex).trim()
|
|
184
|
+
: rest.trim();
|
|
185
|
+
|
|
186
|
+
files.push({ filename, content });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return files;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Render a multi-file template (for Antigravity)
|
|
194
|
+
* @param {string} templatePath - Path to the template file
|
|
195
|
+
* @param {object} context - Context data for rendering
|
|
196
|
+
* @returns {object[]} Array of { filename, content } objects
|
|
197
|
+
*/
|
|
198
|
+
function renderMultiFileTemplate(templatePath, context) {
|
|
199
|
+
const rendered = renderTemplate(templatePath, context);
|
|
200
|
+
return parseMultiFileOutput(rendered);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Get available template names
|
|
205
|
+
* @returns {string[]} Array of template names
|
|
206
|
+
*/
|
|
207
|
+
function getAvailableTemplates() {
|
|
208
|
+
if (!fs.existsSync(TEMPLATES_DIR)) {
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return fs.readdirSync(TEMPLATES_DIR)
|
|
213
|
+
.filter(f => f.endsWith('.hbs') && !fs.statSync(path.join(TEMPLATES_DIR, f)).isDirectory())
|
|
214
|
+
.map(f => path.basename(f, '.hbs'));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Build default context from analysis results
|
|
219
|
+
* @param {object} analysis - Analysis results from static analyzer
|
|
220
|
+
* @param {object} config - Configuration from CLI
|
|
221
|
+
* @param {string} toolName - AI tool name (claude, copilot, cline, antigravity)
|
|
222
|
+
* @returns {object} Context object for templates
|
|
223
|
+
*/
|
|
224
|
+
function buildContext(analysis, config, toolName = 'claude') {
|
|
225
|
+
const projectName = config.projectName || 'project';
|
|
226
|
+
const techStack = analysis.techStack || config.techStack || {};
|
|
227
|
+
const version = getPackageVersion();
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
// Project identity
|
|
231
|
+
project: {
|
|
232
|
+
name: projectName,
|
|
233
|
+
description: config.description || `${projectName} application`,
|
|
234
|
+
tech_stack: techStack.languages || [],
|
|
235
|
+
primary_language: techStack.languages?.[0] || 'unknown'
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
// Architecture (from analysis)
|
|
239
|
+
architecture: {
|
|
240
|
+
pattern: analysis.architecture?.pattern || 'layered',
|
|
241
|
+
layers: (analysis.architecture?.layers || []).map(l => ({
|
|
242
|
+
name: l.name,
|
|
243
|
+
path: l.directories?.[0] || l.name + '/',
|
|
244
|
+
purpose: l.purpose || l.name
|
|
245
|
+
}))
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
// Workflows (from analysis)
|
|
249
|
+
workflows: (analysis.workflows || []).map(wf => ({
|
|
250
|
+
name: wf.name,
|
|
251
|
+
category: wf.category || 'general',
|
|
252
|
+
complexity: wf.complexity || 'MEDIUM',
|
|
253
|
+
description: wf.description || `Handles ${wf.name} functionality`,
|
|
254
|
+
files: wf.files || [],
|
|
255
|
+
entry_points: (wf.entryPoints || []).map(ep => ({
|
|
256
|
+
endpoint: ep.route || ep.name || 'unknown',
|
|
257
|
+
file: ep.file,
|
|
258
|
+
line: ep.line,
|
|
259
|
+
method: ep.method || 'GET',
|
|
260
|
+
purpose: ep.purpose || 'Entry point'
|
|
261
|
+
}))
|
|
262
|
+
})),
|
|
263
|
+
|
|
264
|
+
// Gotchas (empty by default, filled by AI or manual)
|
|
265
|
+
gotchas: [],
|
|
266
|
+
|
|
267
|
+
// Key files (from analysis)
|
|
268
|
+
key_files: {
|
|
269
|
+
entry_points: (analysis.entryPoints || []).map(ep => ep.file),
|
|
270
|
+
api_routes: [],
|
|
271
|
+
config: [],
|
|
272
|
+
models: []
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
// Commands (from tech stack)
|
|
276
|
+
commands: {
|
|
277
|
+
install: techStack.commands?.install || 'npm install',
|
|
278
|
+
dev: techStack.commands?.dev || 'npm run dev',
|
|
279
|
+
test: techStack.commands?.test || 'npm test',
|
|
280
|
+
build: techStack.commands?.build || 'npm run build'
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
// Environment
|
|
284
|
+
environment: {
|
|
285
|
+
required: [],
|
|
286
|
+
optional: []
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
// Critical constraints
|
|
290
|
+
critical_constraints: [],
|
|
291
|
+
|
|
292
|
+
// Metadata with tool-specific coordination header
|
|
293
|
+
metadata: {
|
|
294
|
+
header: getToolCoordinationHeader(toolName, version),
|
|
295
|
+
timestamp: new Date().toISOString(),
|
|
296
|
+
generator_version: version,
|
|
297
|
+
source_ref: projectName,
|
|
298
|
+
tool_name: toolName
|
|
299
|
+
},
|
|
300
|
+
|
|
301
|
+
// Tool coordination footer
|
|
302
|
+
coordination: {
|
|
303
|
+
footer: getToolCoordinationFooter(toolName),
|
|
304
|
+
universal_context: getUniversalContextReference()
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
// Registry (for federation compatibility)
|
|
308
|
+
registry: {
|
|
309
|
+
name: projectName,
|
|
310
|
+
submodule: projectName,
|
|
311
|
+
description: config.description || `${projectName} application`,
|
|
312
|
+
tech_stack: techStack.languages || [],
|
|
313
|
+
primary_language: techStack.languages?.[0] || 'unknown',
|
|
314
|
+
architecture: {
|
|
315
|
+
pattern: analysis.architecture?.pattern || 'layered',
|
|
316
|
+
layers: (analysis.architecture?.layers || []).map(l => l.name || l),
|
|
317
|
+
dependencies: {}
|
|
318
|
+
},
|
|
319
|
+
key_files: {
|
|
320
|
+
entry_points: (analysis.entryPoints || []).map(ep => ep.file),
|
|
321
|
+
api_routes: [],
|
|
322
|
+
core: [],
|
|
323
|
+
config: []
|
|
324
|
+
},
|
|
325
|
+
commands: {
|
|
326
|
+
install: techStack.commands?.install || 'npm install',
|
|
327
|
+
run: techStack.commands?.dev || 'npm run dev',
|
|
328
|
+
test: techStack.commands?.test || 'npm test',
|
|
329
|
+
build: techStack.commands?.build || 'npm run build'
|
|
330
|
+
},
|
|
331
|
+
environment: {
|
|
332
|
+
required: [],
|
|
333
|
+
optional: []
|
|
334
|
+
},
|
|
335
|
+
critical_constraints: [],
|
|
336
|
+
integrations: {
|
|
337
|
+
external: {},
|
|
338
|
+
internal: {}
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
// Submodule (for federation compatibility)
|
|
343
|
+
submodule: {
|
|
344
|
+
ports: {
|
|
345
|
+
default: 3000
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
// Cross-repo references (for federation)
|
|
350
|
+
cross_repos: [],
|
|
351
|
+
|
|
352
|
+
// Active task (for task dispatch)
|
|
353
|
+
active_task: null,
|
|
354
|
+
|
|
355
|
+
// Federation info
|
|
356
|
+
federation: {
|
|
357
|
+
is_federated: false,
|
|
358
|
+
subproject: null,
|
|
359
|
+
cross_repos: []
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Get package version
|
|
366
|
+
* @returns {string} Package version
|
|
367
|
+
*/
|
|
368
|
+
function getPackageVersion() {
|
|
369
|
+
try {
|
|
370
|
+
const pkgPath = path.join(__dirname, '..', 'package.json');
|
|
371
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
372
|
+
return pkg.version || '2.0.0';
|
|
373
|
+
} catch {
|
|
374
|
+
return '2.0.0';
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Initialize on module load
|
|
379
|
+
initialize();
|
|
380
|
+
|
|
381
|
+
module.exports = {
|
|
382
|
+
renderTemplate,
|
|
383
|
+
renderTemplateByName,
|
|
384
|
+
renderMultiFileTemplate,
|
|
385
|
+
parseMultiFileOutput,
|
|
386
|
+
buildContext,
|
|
387
|
+
registerPartials,
|
|
388
|
+
registerHelpers,
|
|
389
|
+
initialize,
|
|
390
|
+
getAvailableTemplates,
|
|
391
|
+
TEMPLATES_DIR
|
|
392
|
+
};
|