-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-tier.js
More file actions
executable file
·796 lines (695 loc) · 31.2 KB
/
opencode-tier.js
File metadata and controls
executable file
·796 lines (695 loc) · 31.2 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#!/usr/bin/env node
/**
* opencode-tier v3.1.0
* ─────────────────────
* Smart autonomous model tier switcher for OpenCode.
*
* Proactively monitors your Go credit consumption and automatically
* switches between premium/free model tiers based on real-time budget
* analysis. No user intervention needed — set it and forget it.
*
* Cross-platform: Linux, macOS, Windows.
* Zero npm dependencies: pure Node.js built-ins.
*
* Usage:
* opencode-tier <command>
*
* Commands:
* ultimate 🟣 Professional free models (GitHub + Google + Zen)
* green 🟢 Full paid power (deepseek-v4-pro)
* yellow 🟡 Balanced mid-range (deepseek-v4-flash)
* orange 🟠 Economy (cheap paid + free mix)
* red 🔴 Survival (free Zen only — big-pickle)
* auto 🤖 Auto-detect and apply best tier based on budget
* watch [N] 👁 Continuous monitoring (checks every N min, default: 5)
* status 📊 Show current configuration and budget
* providers 🔌 Show connected AI providers
* setup 🛠 Interactive setup wizard
* install 📦 Install auto-switching service + commands
* uninstall 🗑️ Remove service and commands
* help ℹ️ Show this message
*/
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
// ─── ANSI Colors (zero deps) ──────────────────────────────────────────────
const isTTY = process.stdout.isTTY && !process.env.CI;
const C = {
reset: isTTY ? '\x1b[0m' : '',
bold: isTTY ? '\x1b[1m' : '',
dim: isTTY ? '\x1b[2m' : '',
green: isTTY ? '\x1b[32m' : '',
yellow: isTTY ? '\x1b[33m' : '',
red: isTTY ? '\x1b[31m' : '',
blue: isTTY ? '\x1b[34m' : '',
magenta: isTTY ? '\x1b[35m' : '',
cyan: isTTY ? '\x1b[36m' : '',
white: isTTY ? '\x1b[37m' : '',
};
const icons = {
check: '\u2713',
cross: '\u2717',
arrow: '\u2192',
star: '\u2605',
bullet: '\u25CF',
};
// ─── Imports ───────────────────────────────────────────────────────────────
const tiers = require('./lib/tiers');
const config = require('./lib/config');
const budget = require('./lib/budget');
const providers = require('./lib/providers');
const scheduler = require('./lib/scheduler');
const notify = require('./lib/notify');
const alert = require('./lib/alert');
// ─── Helpers ───────────────────────────────────────────────────────────────
function clr(text, code) {
return code ? `${code}${text}${C.reset}` : text;
}
function header(text, subtext) {
console.log('');
console.log(` ${C.bold}╭────────────────────────────────────────────╮${C.reset}`);
console.log(` ${C.bold}│${C.reset} ${clr(icons.star, C.yellow)} ${clr(text, C.bold)}${' '.repeat(Math.max(1, 39 - text.length))}${C.bold}│${C.reset}`);
if (subtext) {
console.log(` ${C.bold}│${C.reset} ${clr(subtext, C.dim)}${' '.repeat(Math.max(1, 35 - subtext.length))}${C.bold}│${C.reset}`);
}
console.log(` ${C.bold}╰────────────────────────────────────────────╯${C.reset}`);
console.log('');
}
function box(items) {
// items: array of { label, value, color? }
const maxLabel = Math.max(...items.map(i => i.label.length));
for (const item of items) {
const pad = ' '.repeat(maxLabel - item.label.length);
const val = item.color ? clr(item.value, item.color) : item.value;
console.log(` ${clr(item.label + ':', C.dim)} ${pad}${val}`);
}
}
function success(msg) {
console.log(` ${clr(icons.check, C.green)} ${msg}`);
}
function warn(msg) {
console.log(` ${clr('!', C.yellow)} ${msg}`);
}
function error(msg) {
console.log(` ${clr(icons.cross, C.red)} ${msg}`);
}
// ─── Commands ──────────────────────────────────────────────────────────────
/**
* Apply a tier by name.
* @param {string} tierKey
* @param {Object} [opts]
* @param {boolean} [opts.silent] - Suppress output
* @param {'auto'|'manual'} [opts.source] - Who initiated (default: 'manual')
*/
function cmdApplyTier(tierKey, { silent, source } = {}) {
const tierDef = tiers.TIERS[tierKey];
if (!tierDef) {
error(`Unknown tier: ${tierKey}`);
process.exit(1);
}
try {
const cfg = config.readConfig();
// Track manual overrides (even when tier unchanged — records user intent)
if (source !== 'auto') {
config.recordManualOverride(tierKey);
}
// Skip write if config already matches this tier (redundant write prevention)
if (config.configAlreadyMatchesTier(cfg, tierDef)) {
if (!silent) {
console.log('');
console.log(` ${tierDef.icon} ${clr('Already on ' + tierDef.label + ' tier', C.dim)}`);
console.log(` No config changes needed.`);
console.log('');
}
return;
}
config.applyTierToConfig(cfg, tierDef);
const backup = config.writeConfig(cfg);
config.logSwitch(tierDef.label);
if (source === 'auto') {
config.recordAutoSwitch(tierKey);
}
// Send tier change notification
alert.notifyTierChange(tierKey, tierDef, source || 'manual');
if (!silent) {
console.log('');
console.log(` ${tierDef.icon} ${clr('TIER ' + tierDef.label, C.bold)}`);
console.log(` ${clr(tierDef.description, C.dim)}`);
console.log(` ${clr(tierDef.costRange, C.dim)}`);
console.log('');
console.log(` ${clr('Agent models:', C.dim)}`);
for (const [agent, model] of Object.entries(tierDef.agents)) {
console.log(` ${clr(agent.padEnd(14), C.cyan)} ${model}`);
}
console.log('');
success(`Backup saved: ${pathRelative(backup)}`);
console.log(` ${clr(icons.arrow, C.blue)} Changes take effect on ${clr('next OpenCode session', C.bold)}.`);
console.log(` Running sessions keep their current model.`);
console.log('');
}
} catch (err) {
error(`Failed to apply tier: ${err.message}`);
process.exit(1);
}
}
/**
* Auto-detect best tier based on budget analysis.
*/
function cmdAuto({ yes } = {}) {
header('TIER AUTO', 'Budget-based tier selection');
// Step 1: Detect providers
const prov = providers.detectProviders();
box([
{ label: 'OpenCode Go', value: prov.opencodeGo ? 'connected' : 'not connected', color: prov.opencodeGo ? C.green : C.dim },
{ label: 'GitHub Copilot', value: prov.github ? 'connected' : 'not connected', color: prov.github ? C.green : C.dim },
{ label: 'Google Gemini', value: prov.google ? prov.googleKeyHint : 'not found', color: prov.google ? C.green : C.yellow },
]);
console.log('');
// Step 2: Check if we can use Ultimate (all free providers)
if (prov.github && prov.google) {
success('All free providers available!');
console.log(` ${clr('Ultimate tier gives you professional models at $0/day.', C.dim)}`);
console.log('');
if (!yes) {
console.log(` ${clr(icons.arrow, C.blue)} Run ${clr('opencode-tier ultimate', C.bold)} to use free pro models.`);
console.log('');
} else {
return cmdApplyTier('ultimate', { silent: true, source: 'auto' });
}
}
// Step 3: Analyze budget
console.log(` ${clr('Analyzing budget...', C.dim)}`);
let analysis;
try {
analysis = budget.analyze();
} catch (err) {
error(`Budget analysis failed: ${err.message}`);
console.log(` ${clr('Falling back to provider-based recommendation.', C.dim)}`);
console.log('');
const recTier = providers.bestTierForProviders(prov);
console.log(` ${clr(icons.arrow, C.blue)} Recommended: ${clr(recTier.toUpperCase(), C.bold)}`);
console.log('');
return cmdApplyTier(recTier, { silent: yes, source: 'auto' });
}
if (analysis.error) {
warn(analysis.error);
console.log(` ${clr('Using provider-based recommendation instead.', C.dim)}`);
const recTier = providers.bestTierForProviders(prov);
console.log(` ${clr(icons.arrow, C.blue)} Recommended: ${clr(recTier.toUpperCase(), C.bold)}`);
console.log('');
if (!yes) {
if (!process.stdin.isTTY) {
// Non-TTY: skip confirmation (same as --yes)
return cmdApplyTier(recTier, { silent: true, source: 'auto' });
}
// Interactive confirm
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.question(` Apply tier ${clr(recTier.toUpperCase(), C.bold)}? [Y/n] `, (answer) => {
rl.close();
if (answer.toLowerCase() === 'n') {
console.log(` ${clr('Cancelled.', C.dim)}`);
return;
}
cmdApplyTier(recTier, { source: 'auto' });
});
return;
}
return cmdApplyTier(recTier, { silent: yes, source: 'auto' });
}
// Step 4: Show budget breakdown
const { urgency, dailyCost, totalCost, breakdown } = analysis;
console.log('');
box([
{ label: 'Daily spend', value: `$${dailyCost.toFixed(2)}` },
{ label: 'Total spend', value: `$${totalCost.toFixed(2)}` },
{ label: '', value: '' },
{ label: '5h limit', value: `${breakdown.score5h}%`, color: breakdown.score5h >= 80 ? C.red : breakdown.score5h >= 50 ? C.yellow : C.green },
{ label: 'Weekly limit', value: `${breakdown.scoreWeekly}%`, color: breakdown.scoreWeekly >= 80 ? C.red : breakdown.scoreWeekly >= 50 ? C.yellow : C.green },
{ label: 'Monthly limit', value: `${breakdown.scoreMonthly}%`, color: breakdown.scoreMonthly >= 80 ? C.red : breakdown.scoreMonthly >= 50 ? C.yellow : C.green },
]);
console.log('');
// Step 5: Select and apply tier
// Detect current tier for hysteresis (prevents oscillation on re-upgrade)
let currentTier;
try { currentTier = tiers.detectTier(config.readConfig().agent); } catch { currentTier = null; }
const { key: suggestedKey, tier: suggestedTier } = tiers.selectTierForUrgency(urgency, { hasGo: prov.opencodeGo, currentTier, skipMidTiers: true });
const urgencyColor = urgency >= 60 ? C.red : urgency >= 30 ? C.yellow : C.green;
console.log(` Urgency: ${clr(`${urgency}%`, urgencyColor)}`);
console.log(` Tier: ${suggestedTier.icon} ${clr(suggestedKey.toUpperCase(), C.bold)} ${clr(suggestedTier.description, C.dim)}`);
console.log('');
// Preemptive budget warnings
if (analysis.breakdown) {
alert.checkAndWarn(urgency, analysis.breakdown);
}
// Imminent boundary detection — warn user to save BEFORE tier switches
if (urgency !== null && urgency !== undefined) {
const boundaryAlert = alert.checkImminentBoundary(urgency);
if (boundaryAlert.imminent) {
console.log(` ${clr('⚠', C.yellow)} Tier change to ${clr(boundaryAlert.tier, C.bold)} approaching.`);
console.log(` ${clr('SAVE YOUR WORK before the switch triggers.', C.yellow)}`);
console.log('');
}
}
// Check cooldown / manual override before switching
const autoCheck = config.shouldAutoSwitch(urgency, suggestedKey);
if (!autoCheck.allowed) {
warn(`Auto-switch blocked: ${autoCheck.reason}`);
console.log(` ${clr('Run manually to override:', C.dim)} opencode-tier ${suggestedKey}`);
console.log('');
return;
}
// Emergency: clear manual override if urgency is critical
if (urgency >= config.EMERGENCY_URGENCY) {
config.clearManualOverride();
warn(`Emergency mode: urgency ${urgency}% — overriding manual settings`);
console.log('');
}
if (yes) {
success(`Auto-selected ${suggestedKey.toUpperCase()} (${clr(`--yes`, C.dim)})`);
console.log('');
return cmdApplyTier(suggestedKey, { silent: true, source: 'auto' });
}
if (!process.stdin.isTTY) {
// Non-TTY: skip confirmation (same as --yes)
success(`Auto-selected ${suggestedKey.toUpperCase()} (non-interactive)`);
console.log('');
return cmdApplyTier(suggestedKey, { silent: true, source: 'auto' });
}
// Interactive
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.question(` Apply tier ${clr(suggestedKey.toUpperCase(), C.bold)}? [Y/n/tier-name] `, (answer) => {
rl.close();
const ans = answer.trim().toLowerCase();
if (ans === '' || ans === 'y' || ans === 'yes') {
return cmdApplyTier(suggestedKey, { source: 'auto' });
}
if (tiers.TIERS[ans]) {
return cmdApplyTier(ans);
}
if (ans === 'n' || ans === 'no') {
console.log(` ${clr('Cancelled. No changes made.', C.dim)}`);
return;
}
console.log(` ${clr('Invalid response. No changes made.', C.yellow)}`);
});
}
/**
* Continuous monitoring (watch mode).
*/
function cmdWatch(interval) {
const intervalMin = parseInt(interval, 10) || 5;
header('TIER WATCH', `Auto-adjusting every ${intervalMin} min`);
console.log(` ${clr('Monitoring in background. Press Ctrl+C to stop.', C.dim)}`);
console.log(` ${clr('Tier changes apply on next OpenCode session.', C.dim)}`);
console.log('');
let lastTier = null;
scheduler.startPolling(intervalMin, () => {
try {
const analysis = budget.analyze();
if (analysis.urgency === null || analysis.urgency === undefined) {
warn('Budget data unavailable — skipping check');
return;
}
// Detect current tier from config (not lastTier in memory — respects manual changes)
let cfg;
try { cfg = config.readConfig(); } catch { /* stale config, use lastTier fallback */ }
const currentTier = cfg ? tiers.detectTier(cfg.agent) : lastTier;
const { key } = tiers.selectTierForUrgency(analysis.urgency, { currentTier, skipMidTiers: true });
const tierDef = tiers.TIERS[key];
// Preemptive budget warnings (sent via desktop notification)
if (analysis.breakdown) {
alert.checkAndWarn(analysis.urgency, analysis.breakdown);
}
// Imminent boundary detection — warn user to save BEFORE tier switches
if (analysis.urgency !== null && analysis.urgency !== undefined) {
alert.checkImminentBoundary(analysis.urgency);
}
// Check cooldown / manual override
if (key !== lastTier) {
const autoCheck = config.shouldAutoSwitch(analysis.urgency, key);
if (!autoCheck.allowed) {
const ts = new Date().toLocaleTimeString();
console.log(` [${clr(ts, C.dim)}] ${clr('Skipping:', C.yellow)} ${autoCheck.reason}`);
return;
}
// Emergency override
if (analysis.urgency >= config.EMERGENCY_URGENCY) {
config.clearManualOverride();
}
const ts = new Date().toLocaleTimeString();
if (lastTier !== null) {
console.log(` [${clr(ts, C.dim)}] ${clr('Budget:', C.dim)} ${analysis.urgency}% ${clr(icons.arrow, C.blue)} Switching to ${tierDef.icon} ${clr(key.toUpperCase(), C.bold)}`);
cmdApplyTier(key, { silent: true, source: 'auto' });
} else {
console.log(` [${clr(ts, C.dim)}] ${clr('Initial:', C.dim)} ${analysis.urgency}% → ${tierDef.icon} ${clr(key.toUpperCase(), C.bold)}`);
const currentTier = tiers.detectTier(config.readConfig().agent);
if (currentTier !== key) {
cmdApplyTier(key, { silent: true, source: 'auto' });
}
}
}
lastTier = key;
} catch (err) {
const ts = new Date().toLocaleTimeString();
console.log(` [${clr(ts, C.dim)}] ${clr('Watch error:', C.red)} ${err.message}`);
}
});
}
/**
* Show status.
*/
function cmdStatus() {
header('TIER STATUS', 'Current configuration & budget');
// Current config
let cfg;
try {
cfg = config.readConfig();
} catch (err) {
error(`Cannot read config: ${err.message}`);
process.exit(1);
}
const currentTier = tiers.detectTier(cfg.agent);
const tierDef = tiers.TIERS[currentTier];
const icon = tierDef ? tierDef.icon : clr('?', C.yellow);
console.log(` ${icon} ${clr('Tier: ' + (tierDef ? tierDef.label : currentTier.toUpperCase()), C.bold)}`);
if (tierDef) {
console.log(` ${clr(tierDef.description, C.dim)}`);
console.log(` ${clr(tierDef.costRange, C.dim)}`);
}
console.log('');
// Agent models
console.log(` ${clr('Agent models:', C.dim)}`);
for (const agentName of tiers.KNOWN_AGENTS) {
if (cfg.agent && cfg.agent[agentName]) {
const model = cfg.agent[agentName].model || '(default)';
console.log(` ${clr(agentName.padEnd(14), C.cyan)} ${model}`);
}
}
console.log('');
// Providers
const prov = providers.detectProviders();
console.log(` ${clr('Providers:', C.dim)}`);
console.log(` ${clr('Go:'.padEnd(14), C.dim)} ${prov.opencodeGo ? clr(icons.check + ' connected', C.green) : clr('not connected', C.dim)}`);
console.log(` ${clr('GitHub:'.padEnd(14), C.dim)} ${prov.github ? clr(icons.check + ' connected', C.green) : clr('not connected', C.dim)}`);
console.log(` ${clr('Google:'.padEnd(14), C.dim)} ${prov.google ? clr(icons.check + ' ' + prov.googleKeyHint, C.green) : clr('not configured', C.yellow)}`);
console.log('');
// Budget
try {
const analysis = budget.analyze();
if (analysis.urgency !== null) {
console.log(` ${clr('Budget:', C.dim)}`);
console.log(` ${clr('Daily:'.padEnd(14), C.dim)} $${analysis.dailyCost.toFixed(2)}`);
console.log(` ${clr('Total:'.padEnd(14), C.dim)} $${analysis.totalCost.toFixed(2)}`);
const uc = analysis.urgency >= 60 ? C.red : analysis.urgency >= 30 ? C.yellow : C.green;
console.log(` ${clr('Urgency:'.padEnd(14), C.dim)} ${clr(analysis.urgency + '%', uc)}`);
if (analysis.breakdown) {
console.log(` ${clr('5h limit:'.padEnd(14), C.dim)} ${analysis.breakdown.score5h}%`);
console.log(` ${clr('Weekly:'.padEnd(14), C.dim)} ${analysis.breakdown.scoreWeekly}%`);
console.log(` ${clr('Monthly:'.padEnd(14), C.dim)} ${analysis.breakdown.scoreMonthly}%`);
}
}
} catch {
console.log(` ${clr('Budget:', C.dim)} ${clr('(opencode stats unavailable)', C.yellow)}`);
}
console.log('');
// Service status
const svc = scheduler.checkServiceStatus();
if (svc.installed) {
success(`Auto-switching: ${svc.method} — ${svc.details}`);
} else {
console.log(` ${clr('Auto-switching:', C.dim)} ${clr('not installed', C.dim)}`);
console.log(` Run ${clr('opencode-tier install', C.bold)} to enable automatic tier switching.`);
}
console.log('');
// Notifications
const notif = notify.checkAvailability();
console.log(` ${clr('Notifications:', C.dim)}`);
console.log(` ${clr('Desktop:'.padEnd(14), C.dim)} ${notif.available ? clr(icons.check + ' ' + notif.method, C.green) : clr('not available', C.yellow)}`);
// Manual override status
if (config.hasActiveManualOverride()) {
const override = config.state.get('manualOverride');
console.log(` ${clr('Override:'.padEnd(14), C.dim)} ${clr('manual ' + override.tier.toUpperCase() + ' (respected until ' + new Date(override.timestamp + config.MANUAL_OVERRIDE_GRACE_MS).toLocaleTimeString() + ')', C.yellow)}`);
}
console.log('');
// Alert status
const warnLevel = alert.getHighestWarningLevel();
if (warnLevel) {
console.log(` ${clr('Alerts:', C.dim)} Last warning at ${clr(warnLevel + '%', warnLevel >= 90 ? C.red : warnLevel >= 80 ? C.yellow : C.yellow)} urgency`);
console.log('');
}
// Recent switches
const logs = config.tailLog(5);
if (logs.length > 0) {
console.log(` ${clr('Recent switches:', C.dim)}`);
for (const line of logs) {
console.log(` ${clr(line, C.dim)}`);
}
console.log('');
}
}
/**
* Show providers.
*/
function cmdProviders() {
header('PROVIDERS', 'Connected AI service status');
const prov = providers.detectProviders();
const steps = providers.setupGuide(prov);
console.log(` ${prov.opencodeGo ? clr(icons.check, C.green) : clr(icons.cross, C.red)} OpenCode Go ${prov.opencodeGo ? '(connected)' : clr('(not connected — paid plan)', C.dim)}`);
console.log(` ${clr(icons.check, C.green)} OpenCode Zen (always available — free tier)`);
console.log(` ${prov.github ? clr(icons.check, C.green) : clr(icons.cross, C.yellow)} GitHub Copilot ${prov.github ? '(connected — FREE via Education)' : clr('(not connected — FREE via .edu email)', C.dim)}`);
console.log(` ${prov.google ? clr(icons.check, C.green) : clr(icons.cross, C.yellow)} Google Gemini ${prov.google ? '(' + prov.googleKeyHint + ' — FREE tier)' : clr('(not configured — FREE via API key)', C.dim)}`);
console.log('');
// Recommendation
const best = providers.bestTierForProviders(prov);
console.log(` ${clr('Best tier:', C.bold)} ${tiers.TIERS[best] ? tiers.TIERS[best].icon + ' ' : ''}${best.toUpperCase()}`);
console.log('');
// Setup guide if missing providers
if (steps.length > 0) {
console.log(` ${clr('Setup steps:', C.bold)}`);
for (const line of steps) {
console.log(` ${line}`);
}
}
}
/**
* Setup wizard.
*/
function cmdSetup() {
header('SETUP', 'Connect free providers for Ultimate tier');
const prov = providers.detectProviders();
console.log(` ${clr('This wizard helps you connect FREE AI providers', C.bold)}`);
console.log(` ${clr('to unlock professional-grade models at $0/day.', C.dim)}`);
console.log('');
// Step 1: GitHub Copilot
console.log(` ${clr('Step 1: GitHub Copilot (FREE via Education)', C.bold)}`);
console.log(` ${clr('─────────────────────────────────────────', C.dim)}`);
if (prov.github) {
success('Already connected!');
} else {
console.log(` 1. Verify your educational status at:`);
console.log(` ${clr('https://github.com/settings/education/benefits', C.cyan)}`);
console.log(` Use your university email (.edu or @icesi.edu.co)`);
console.log('');
console.log(` 2. In OpenCode TUI, run: ${clr('/connect', C.bold)}`);
console.log(` Select ${clr('GitHub Copilot', C.cyan)} and follow the OAuth flow`);
console.log('');
console.log(` 3. After connecting, you get these models for FREE:`);
console.log(` ${clr('github/claude-3.5-sonnet', C.cyan)} — best for complex tasks`);
console.log(` ${clr('github/gpt-4o', C.cyan)} — best for general coding`);
console.log('');
}
// Step 2: Google Gemini
console.log(` ${clr('Step 2: Google Gemini API (FREE tier)', C.bold)}`);
console.log(` ${clr('──────────────────────────────────────────', C.dim)}`);
if (prov.google) {
success('API key found: ' + prov.googleKeyHint);
} else {
console.log(` 1. Go to ${clr('https://aistudio.google.com/', C.cyan)}`);
console.log(` 2. Generate a free API key (no credit card needed)`);
console.log(` 3. Configure it:`);
console.log('');
console.log(` ${clr('# Option A: Environment variable', C.bold)}`);
console.log(` echo 'export GOOGLE_API_KEY=your_key_here' >> ~/.bashrc`);
console.log(` source ~/.bashrc`);
console.log('');
console.log(` ${clr('# Option B: OpenCode .env file', C.bold)}`);
console.log(` echo 'GOOGLE_API_KEY=your_key_here' > ~/.config/opencode/.env`);
console.log('');
}
// Step 3: Verify
console.log(` ${clr('Step 3: Verify and activate', C.bold)}`);
console.log(` ${clr('────────────────────────────', C.dim)}`);
console.log(` After connecting providers:`);
// Check if we can recommend ultimate
const updatedProv = providers.detectProviders();
if (updatedProv.github && updatedProv.google) {
console.log(` ${clr('Run:', C.bold)} opencode-tier ultimate`);
console.log(` ${clr('→ Professional models at $0/day!', C.green)}`);
} else {
console.log(` ${clr('Run:', C.bold)} opencode-tier auto`);
console.log(` ${clr('→ Let the system pick the best tier.', C.dim)}`);
}
console.log('');
console.log(` ${clr('Run:', C.bold)} opencode-tier providers`);
console.log(` ${clr('→ Verify your connections.', C.dim)}`);
console.log('');
console.log(` ${clr('Run:', C.bold)} opencode-tier install`);
console.log(` ${clr('→ Enable automatic switching (every 30 min).', C.dim)}`);
console.log('');
}
/**
* Install service + commands.
*/
function cmdInstall() {
header('INSTALL', 'Setting up auto-switching');
const scriptPath = pathRelative(require.resolve('./opencode-tier'));
console.log(` Script: ${clr(scriptPath, C.dim)}`);
console.log('');
// Install scheduler service
console.log(` ${clr('Installing scheduler...', C.dim)}`);
const svc = scheduler.installService(scriptPath);
if (svc.success) {
success(svc.message);
} else {
warn(svc.message);
console.log(` ${clr('Falling back to in-process polling.', C.yellow)}`);
console.log(` ${clr('Run "opencode-tier watch" for continuous monitoring.', C.dim)}`);
}
console.log('');
// Install OpenCode commands
console.log(` ${clr('Installing /tier commands for OpenCode TUI...', C.dim)}`);
const cmdFiles = scheduler.installCommands(scriptPath);
success(`Installed ${cmdFiles.length} commands:`);
for (const file of cmdFiles) {
console.log(` ${clr(file, C.cyan)}`);
}
console.log('');
console.log(` ${clr('You can now use these commands in OpenCode TUI:', C.dim)}`);
console.log(` ${clr('/tier-status', C.bold)} — show current tier`);
console.log(` ${clr('/tier', C.bold)} — switch tiers`);
console.log(` ${clr('/tier-auto', C.bold)} — auto-switch`);
console.log(` ${clr('/tier-watch', C.bold)} — start monitoring`);
console.log(` ${clr('/tier-providers', C.bold)} — show providers`);
console.log('');
success('Installation complete!');
console.log('');
}
/**
* Uninstall service.
*/
function cmdUninstall() {
header('UNINSTALL', 'Removing auto-switching');
const svc = scheduler.uninstallService();
if (svc.success) {
success(svc.message);
} else {
warn(svc.message);
}
// Remove commands
try {
const cmdDir = path.join(os.homedir(), '.config', 'opencode', 'commands');
for (const cmd of ['tier-status.md', 'tier-auto.md', 'tier.md', 'tier-watch.md', 'tier-providers.md']) {
try { fs.unlinkSync(path.join(cmdDir, cmd)); } catch { /* ignore */ }
}
success('Commands removed from OpenCode TUI');
} catch { /* ignore */ }
console.log('');
console.log(` ${clr('To stop a running watch process, press Ctrl+C in that terminal.', C.dim)}`);
console.log('');
}
/**
* Show help.
*/
function cmdHelp() {
header('OPENCODE-TIER', 'v3.1.0 — Autonomous tier switcher + alerts');
console.log(` ${clr('USAGE', C.bold)}`);
console.log(` opencode-tier <command>`);
console.log('');
console.log(` ${clr('COMMANDS', C.bold)}`);
console.log(` ultimate ${clr('Professional free models (GitHub + Google + Zen)', C.dim)}`);
console.log(` green ${clr('Full paid power — deepseek-v4-pro, qwen3.6-plus', C.dim)}`);
console.log(` yellow ${clr('Balanced mid-range — deepseek-v4-flash, minimax', C.dim)}`);
console.log(` orange ${clr('Economy — cheap paid + free Zen mix', C.dim)}`);
console.log(` red ${clr('Survival — free Zen models (qwen + deepseek + minimax)', C.dim)}`);
console.log(` auto ${clr('Auto-detect best tier based on budget', C.dim)}`);
console.log(` watch [N] ${clr('Continuous monitoring (checks every N min)', C.dim)}`);
console.log(` status ${clr('Show current configuration and budget', C.dim)}`);
console.log(` providers ${clr('Show connected AI providers', C.dim)}`);
console.log(` setup ${clr('Interactive setup wizard', C.dim)}`);
console.log(` install ${clr('Install auto-switching service + commands', C.dim)}`);
console.log(` uninstall ${clr('Remove service and commands', C.dim)}`);
console.log('');
console.log(` ${clr('TIERS (daily cost)', C.bold)}`);
console.log(` ${clr('🟣 Ultimate', C.magenta)} $0/day (GitHub Edu + Google Gemini + Zen free)`);
console.log(` ${clr('🟢 Green', C.green)} $6-12/day (Go paid models, full power)`);
console.log(` ${clr('🟡 Yellow', C.yellow)} $2-5/day (Go mid-range)`);
console.log(` ${clr('🟠 Orange', C.yellow)} $0.30-1/day (Go cheap + free mix)`);
console.log(` ${clr('🔴 Red', C.red)} $0/day (Zen free — distributed across 4 models)`);
console.log('');
console.log(` ${clr('EXAMPLES', C.bold)}`);
console.log(` opencode-tier auto ${clr('# Interactive budget check', C.dim)}`);
console.log(` opencode-tier auto --yes ${clr('# Hands-free auto switch', C.dim)}`);
console.log(` opencode-tier watch ${clr('# Monitor and auto-adjust', C.dim)}`);
console.log(` opencode-tier ultimate ${clr('# Activate free pro models', C.dim)}`);
console.log(` opencode-tier install ${clr('# Set up persistent service', C.dim)}`);
console.log('');
console.log(` ${clr('INFO', C.bold)}`);
console.log(` Changes take effect on ${clr('NEXT', C.bold)} OpenCode session`);
console.log(` Config backups: ~/.local/share/opencode-tier/`);
console.log(` Docs: https://github.com/NickRivers1983/opencode-tier`);
console.log('');
}
// ─── Utilities ─────────────────────────────────────────────────────────────
function pathRelative(absPath) {
const home = os.homedir();
if (absPath.startsWith(home)) return '~' + absPath.slice(home.length);
return absPath;
}
// ─── Main ───────────────────────────────────────────────────────────────────
function main() {
const args = process.argv.slice(2);
const cmd = args[0] || 'help';
// Check prerequisites
try {
require.resolve('./lib/tiers');
} catch {
error('opencode-tier modules not found. Run from the package directory.');
process.exit(1);
}
switch (cmd) {
case 'ultimate':
return cmdApplyTier('ultimate');
case 'green':
case 'yellow':
case 'orange':
case 'red':
return cmdApplyTier(cmd);
case 'auto':
return cmdAuto({ yes: args.includes('--yes') || args.includes('-y') });
case 'watch':
return cmdWatch(args[1]);
case 'status':
case 'st':
return cmdStatus();
case 'providers':
case 'connect':
return cmdProviders();
case 'setup':
return cmdSetup();
case 'install':
return cmdInstall();
case 'uninstall':
return cmdUninstall();
case 'help':
case '--help':
case '-h':
case '':
return cmdHelp();
default:
error(`Unknown command: ${cmd}`);
console.log(` Run ${clr('opencode-tier help', C.bold)} for usage.`);
process.exit(1);
}
}
main();