-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-builder.js
More file actions
136 lines (122 loc) · 3.54 KB
/
context-builder.js
File metadata and controls
136 lines (122 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
/**
* orchestrator-lite/context-builder.js
*
* Build layered context for agent execution
* layers: contract -> repo facts -> references -> prior artifacts
*
* Integrates with OpenClaw tools: read, memory_search, memory_get
*/
const fs = require('fs');
const path = require('path');
function approxTokens(text) {
return Math.ceil(String(text || '').length / 4);
}
function buildContext(options = {}) {
const totalBudget = options.totalTokens || 4000;
const contract = options.contract || {};
// Layer 1: Task Contract (already provided)
const contractTokens = approxTokens(JSON.stringify(contract));
// Layer 2: Repo Facts
const repoFacts = [];
if (options.projectPath && fs.existsSync(options.projectPath)) {
try {
const packageJsonPath = path.join(options.projectPath, 'package.json');
if (fs.existsSync(packageJsonPath)) {
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
repoFacts.push(`Package: ${pkg.name || 'unknown'}`);
if (pkg.dependencies) {
const deps = Object.keys(pkg.dependencies);
if (deps.length > 0) {
repoFacts.push(`Dependencies: ${deps.slice(0, 5).join(', ')}`);
}
}
}
} catch (e) {
// Ignore errors
}
}
// Layer 3: References (optional)
const references = options.references || [];
// Layer 4: Prior Artifacts (optional)
const priorArtifacts = [];
if (options.outputPath && fs.existsSync(options.outputPath)) {
try {
const files = fs.readdirSync(options.outputPath);
const recentFiles = files
.filter(f => {
const fullPath = path.join(options.outputPath, f);
const stat = fs.statSync(fullPath);
const oneHourAgo = Date.now() - 3600000;
return stat.mtime > oneHourAgo;
})
.slice(0, 3)
.map(f => path.basename(f));
priorArtifacts.push(...recentFiles);
} catch (e) {
// Ignore errors
}
}
// Build context object
const context = {
contract,
repoFacts,
references,
priorArtifacts
};
const manifest = {
totalBudgetTokens: totalBudget,
usedTokens: contractTokens + approxTokens(repoFacts.join('\n')) + approxTokens(references.join('\n')) + approxTokens(priorArtifacts.join('\n')),
layers: [
{
name: 'contract',
tokens: contractTokens,
truncated: false
},
{
name: 'repo_facts',
tokens: approxTokens(repoFacts.join('\n')),
truncated: false,
count: repoFacts.length
},
{
name: 'references',
tokens: approxTokens(references.join('\n')),
truncated: false,
count: references.length
},
{
name: 'prior_artifacts',
tokens: approxTokens(priorArtifacts.join('\n')),
truncated: false,
count: priorArtifacts.length
}
]
};
return {
context,
manifest
};
}
module.exports = {
buildContext,
approxTokens
};
if (require.main === module) {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Usage: node context-builder.js "task"');
console.log('');
console.log('Options:');
console.log(' --projectPath <path>');
console.log(' --totalTokens <number>');
console.log(' --references <comma-separated>');
console.log(' --outputPath <path>');
process.exit(0);
}
const result = buildContext({
projectPath: args[0],
totalTokens: parseInt(args[1]) || 4000
});
console.log(JSON.stringify(result, null, 2));
}