-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply-optimization.js
More file actions
92 lines (76 loc) · 2.49 KB
/
apply-optimization.js
File metadata and controls
92 lines (76 loc) · 2.49 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
#!/usr/bin/env node
/**
* Apply Optimization - Auto-apply cost optimization to OpenClaw
*/
const fs = require('fs');
const path = require('path');
class OptimizationApplier {
constructor() {
const home = process.env.HOME || process.env.USERPROFILE;
this.configPath = path.join(home, '.openclaw', 'openclaw.json');
}
load() {
try {
return JSON.parse(fs.readFileSync(this.configPath, 'utf8'));
} catch (e) {
console.error(`❌ Failed to load config: ${e.message}`);
process.exit(1);
}
}
apply(config) {
if (!config.agents) config.agents = {};
if (!config.agents.defaults) config.agents.defaults = {};
// Set Haiku primary, Sonnet fallback
config.agents.defaults.model = {
primary: 'anthropic/claude-haiku-4-5',
fallbacks: ['anthropic/claude-sonnet-4-20250514']
};
if (!config.agents.defaults.models) config.agents.defaults.models = {};
config.agents.defaults.models['anthropic/claude-haiku-4-5'] = {};
// Update subagents
if (!config.agents.defaults.subagents) config.agents.defaults.subagents = {};
config.agents.defaults.subagents.model = {
primary: 'anthropic/claude-haiku-4-5',
fallbacks: ['anthropic/claude-sonnet-4-20250514']
};
return config;
}
backup() {
const ts = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = `${this.configPath}.backup.${ts}`;
try {
fs.copyFileSync(this.configPath, backupPath);
console.log(`✅ Backup: ${backupPath}`);
return backupPath;
} catch (e) {
console.warn(`⚠️ Backup failed: ${e.message}`);
return null;
}
}
save(config) {
try {
fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2));
console.log(`✅ Optimized config saved`);
return true;
} catch (e) {
console.error(`❌ Failed to save: ${e.message}`);
return false;
}
}
run() {
console.log('🚀 Applying cost optimization to OpenClaw...\n');
this.backup();
const config = this.load();
const optimized = this.apply(config);
if (!this.save(optimized)) process.exit(1);
console.log('\n📊 Applied:');
console.log(' • Primary: Haiku 4.5 (67% savings)');
console.log(' • Fallback: Sonnet 4.5');
console.log('\n💰 Expected: 50-95% cost reduction');
console.log('\n✨ Restart gateway: openclaw gateway restart\n');
}
}
if (require.main === module) {
new OptimizationApplier().run();
}
module.exports = OptimizationApplier;