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
|
// Extracted from newOS4 index.html — SystemModule
class SystemModule {
constructor() {
this.commands = {};
this.terminal = null;
}
getHelpInfo() {
return {
'help': 'help [command] — Show all commands or usage for a specific command',
'echo': 'echo <text...> — Print text to the terminal',
'mem': 'mem — Show current memory usage',
'status': 'status — Display system status (network, files, modules, Ollama)',
'preview': 'preview <file.html> — Render an HTML file from the VFS in a preview pane',
'factory-reset': 'factory-reset [--confirm] — Wipe all webx11_ data (VFS, config, AI profile, mindmaps, themes, all localStorage)',
'reset-vfs': 'reset-vfs [--confirm] — Clear only the virtual filesystem (keeps settings, modules, AI profile)',
'logs': 'logs [n] [LEVEL] — Show last n log entries (optionally filtered by level: INFO/WARN/ERROR)',
'logrotate': 'logrotate — Rotate current.log to a dated archive (auto-cleans old)',
'health-check': 'health-check — Audit VFS size, modules, logs, trash, skills, tool failures',
'memory-map-export': 'memory-map-export — Generate memory map markdown from memory/index.json',
'font-size': 'font-size <8-32> — Set terminal font size (persists across sessions)',
'setfont': 'setfont <8-32> — Alias for font-size',
'smart-prompt': 'smart-prompt [on|off] — Toggle context-aware prompt (shows name, skills, sessions, memory, edits)',
'checkpoint': 'checkpoint [name] — Create a VFS snapshot (name defaults to ISO timestamp)',
'checkpoint-list': 'checkpoint-list — List all VFS checkpoints',
'rollback': 'rollback <name> — Restore VFS from a checkpoint',
'checkpoint-del': 'checkpoint-del <name> — Delete a VFS checkpoint',
'commands': 'commands — List all commands with module source and requirements status',
'check': 'check — Show overall system health including module requirements',
};
}
registerCommands() {
this.commands['help'] = (args) => this.handleHelp(args);
this.commands['echo'] = (args) => this.terminal.print(args.join(' '));
this.commands['mem'] = () => {
const mem = performance.memory ? (performance.memory.usedJSHeapSize / 1048576).toFixed(2) : 'N/A';
this.terminal.print(`Memory Usage: ${mem} MB`, 'info');
document.getElementById('header-status').innerText = `Mem: ${mem}MB`;
};
// Refresh memory display on boot
setTimeout(() => {
if (performance.memory) {
const mem = (performance.memory.usedJSHeapSize / 1048576).toFixed(1);
document.getElementById('header-status').innerText = `Mem: ${mem}MB`;
}
}, 500);
this.commands['status'] = () => {
this.terminal.print(`System: WebX11 ${window.NEWOS4_VERSION || 'v4.0'}`);
this.terminal.print(`Network: ${navigator.onLine ? 'Online' : 'Offline'}`);
this.terminal.print(`Files: ${this.terminal.fs.ls().length} items`);
this.terminal.print(`Modules: ${Object.keys(this.terminal.moduleManager.modules).length} loaded`);
this.terminal.print(`Ollama: ${this.terminal.ollamaManager.connected ? 'Connected' : 'Disconnected'}`);
};
this.commands['preview'] = (args) => this.terminal.openPreview(args);
this.commands['factory-reset'] = (args) => {
if (!args[0] || args[0] !== '--confirm') {
this.terminal.print('⚠️ FACTORY RESET will clear all data:', 'error');
this.terminal.print(' • Filesystem (all files + directories)', 'warning');
this.terminal.print(' • AI memory, patterns, user profile', 'warning');
this.terminal.print(' • Ollama endpoint, model selection', 'warning');
this.terminal.print(' • Terminal history, font size', 'warning');
this.terminal.print(' • All config, modules, packages', 'warning');
this.terminal.print(' • Mindmaps, notes, bookmarks', 'warning');
this.terminal.print(' • Themes, context profiles, servers', 'warning');
this.terminal.print(' • Chat settings, my data', 'warning');
this.terminal.print('Type: factory-reset --confirm to proceed', 'error');
return;
}
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i);
if (key && key.startsWith('webx11_')) localStorage.removeItem(key);
}
this.terminal.fs.root = { type: 'dir', content: {} };
this.terminal.fs.currentPath = [];
this.terminal.outputContainer.innerHTML = '';
this.terminal.print('✅ Factory reset complete. Reloading...', 'success');
setTimeout(() => location.reload(), 1000);
};
this.commands['reset-vfs'] = (args) => {
if (!args[0] || args[0] !== '--confirm') {
this.terminal.print('⚠️ RESET VFS will clear all files and directories:', 'warning');
this.terminal.print(' • Virtual filesystem (all files + directories)', 'warning');
this.terminal.print(' • VFS will be reset to empty root', 'warning');
this.terminal.print(' • All settings, modules, AI profile preserved', 'info');
this.terminal.print('Type: reset-vfs --confirm to proceed', 'error');
return;
}
this.terminal.fs.root = { type: 'dir', content: {} };
this.terminal.fs.currentPath = [];
this.terminal.fs.saveToStorage();
this.terminal.print('✅ VFS reset complete. Reloading...', 'success');
setTimeout(() => location.reload(), 1000);
};
this.commands['logs'] = (args) => {
const n = parseInt(args[0]) || 20;
const level = args[1] ? args[1].toUpperCase() : null;
const content = this.terminal.fs.readFile('/logs/current.log');
if (!content) { this.terminal.print('No logs yet', 'info'); return; }
let lines = content.split('\n').filter(l => l);
if (level) lines = lines.filter(l => l.includes('[' + level + ']'));
const tail = lines.slice(-n);
if (tail.length === 0) { this.terminal.print('No matching log entries', 'info'); return; }
this.terminal.print('--- Logs (last ' + tail.length + ') ---', 'info');
tail.forEach(l => this.terminal.print(l, l.includes('[ERROR]') ? 'error' : 'info'));
};
// Auto-rotate logs on startup (silently)
this._autoRotateLogs();
this.commands['logrotate'] = () => {
const rotated = this._autoRotateLogs(true);
if (rotated) {
this.terminal.print('Log rotated: ' + rotated, 'success');
} else {
this.terminal.print('Log rotation not needed (<50KB, <500 lines)', 'info');
}
};
this.commands['health-check'] = () => this._healthCheck();
this.commands['memory-map-export'] = () => this._memoryMapExport();
this.commands['font-size'] = (args) => {
if (!args.length || !args[0]) {
const cur = localStorage.getItem('webx11_font_size');
this.terminal.print(`Current font-size: ${cur || '16'}px`, 'info');
this.terminal.print('Usage: font-size <8-32>', 'info');
return;
}
const val = parseInt(args[0], 10);
if (isNaN(val) || val < 8 || val > 32) {
this.terminal.print('font-size must be between 8 and 32', 'error');
return;
}
localStorage.setItem('webx11_font_size', val.toString());
// Apply directly to body — bypasses CSS inheritance issues
document.body.style.fontSize = val + 'px';
document.getElementById('terminal-container').style.fontSize = val + 'px';
document.getElementById('command-input').style.fontSize = val + 'px';
this.terminal.print(`Font size set to ${val}px (saved) 👆`, 'success');
};
this.commands['setfont'] = this.commands['font-size'];
// Checkpoint commands (delegated to virtual-fs.js)
this.commands['checkpoint'] = (args) => {
try {
const name = args[0] || '';
this.terminal.fs.checkpoint(name);
const displayName = name || new Date().toISOString();
this.terminal.print('✅ Checkpoint created: ' + displayName, 'success');
} catch (e) {
this.terminal.print('Checkpoint failed: ' + e.message, 'error');
}
};
this.commands['checkpoint-list'] = () => {
try {
const list = this.terminal.fs.listCheckpoints();
if (!list || list.length === 0) {
this.terminal.print('No checkpoints found', 'info');
return;
}
this.terminal.print('📋 Checkpoints:', 'text-muted');
list.forEach(c => this.terminal.print(' • ' + c));
} catch (e) {
this.terminal.print('checkpoint-list failed: ' + e.message, 'error');
}
};
this.commands['checkpoint-del'] = (args) => {
if (!args[0]) return this.terminal.print('Usage: checkpoint-del <name>', 'warning');
try {
if (this.terminal.fs.deleteCheckpoint(args[0])) {
this.terminal.print('Deleted checkpoint: ' + args[0], 'success');
} else {
this.terminal.print('Checkpoint not found: ' + args[0], 'error');
}
} catch (e) {
this.terminal.print('checkpoint-del failed: ' + e.message, 'error');
}
};
this.commands['rollback'] = (args) => {
if (!args[0]) return this.terminal.print('Usage: rollback <name>', 'warning');
try {
if (this.terminal.fs.rollback(args[0])) {
this.terminal.print('↻ Rolled back to checkpoint: ' + args[0], 'success');
} else {
this.terminal.print('Rollback failed: checkpoint not found or invalid', 'error');
}
} catch (e) {
this.terminal.print('Rollback failed: ' + e.message, 'error');
}
};
}
_autoRotateLogs(force = false) {
try {
const content = this.terminal.fs.readFile('/logs/current.log') || '';
const lines = content.split('\n').filter(l => l);
const size = content.length;
if (!force && size < 51200 && lines.length < 500) return null;
const now = new Date();
const stamp = now.getFullYear() + '-' +
String(now.getMonth()+1).padStart(2,'0') + '-' +
String(now.getDate()).padStart(2,'0') + '_' +
String(now.getHours()).padStart(2,'0') + '00';
this.terminal.fs.writeFile('/logs/' + stamp + '.log', content);
this.terminal.fs.writeFile('/logs/current.log', '');
// Clean oldest rotated logs (>10)
const logsDir = this.terminal.fs.readDir('/logs');
if (logsDir && logsDir.content) {
const rotated = Object.keys(logsDir.content)
.filter(k => k.endsWith('.log') && k !== 'current.log')
.sort();
while (rotated.length > 10) {
const oldest = rotated.shift();
this.terminal.fs.deleteFile('/logs/' + oldest);
}
}
return stamp + '.log';
} catch (e) { return null; }
}
_healthCheck() {
const fs = this.terminal.fs;
let warnings = 0;
this.terminal.print('╔══════════════════════════════════════╗', 'info');
this.terminal.print('║ 🔍 SYSTEM HEALTH CHECK ║', 'info');
this.terminal.print('╚══════════════════════════════════════╝', 'info');
// VFS size
const dump = JSON.stringify(fs.root).length;
const vfsOk = dump < 524288;
this.terminal.print(`VFS size: ${(dump/1024).toFixed(1)}KB ${vfsOk ? '✅' : '⚠️ > 512KB'}`, vfsOk ? 'info' : 'warning');
if (!vfsOk) warnings++;
// Modules
const modTotal = Object.keys(this.terminal.moduleManager.modules || {}).length;
this.terminal.print(`Modules loaded: ${modTotal} ✅`, 'info');
// Logs
const logContent = fs.readFile('/logs/current.log') || '';
const logLines = logContent.split('\n').filter(l => l).length;
this.terminal.print(`Current log: ${logLines} lines`, logLines > 500 ? 'warning' : 'info');
if (logLines > 500) warnings++;
// Trash
const trashList = fs.listTrash ? fs.listTrash() : [];
this.terminal.print(`Trash items: ${trashList.length}`, 'info');
// Skills
const skillIdx = fs.fsReadJSON ? (fs.fsReadJSON('/skills/index.json') || {}) : {};
const skillCount = Object.keys(skillIdx).length;
this.terminal.print(`Skills: ${skillCount}`, skillCount > 0 ? 'info' : 'warning');
if (skillCount === 0) warnings++;
// Summary
this.terminal.print('─'.repeat(40), 'text-muted');
if (warnings === 0) {
this.terminal.print('All checks passed ✅', 'success');
} else {
this.terminal.print(`${warnings} warning(s) found`, 'warning');
}
}
_memoryMapExport() {
const fs = this.terminal.fs;
const idx = fs.fsReadJSON ? (fs.fsReadJSON('/memory/index.json') || {}) : null;
if (!idx) {
this.terminal.print('No memory index found at /memory/index.json', 'error');
return;
}
this.terminal.print('---\nname: adaptive-memory-map\ndescription: Auto-generated from memory/index.json\ntags: [memory, context, tracking, adaptation]\ncategory: system\n---\n', 'info');
for (const [k,v] of Object.entries(idx)) {
this.terminal.print(`## ${k}\n- Tasks: ${v.tasks || 0}\n- Updated: ${v.updated || 'unknown'}\n- Summary: ${(v.summary || '').substring(0,100)}...`, 'info');
}
this.terminal.print('(Not saved — render to file with memory-map-export > /memory/reference.md if desired)', 'text-muted');
}
handleHelp(args) {
const helpMap = this.terminal.moduleManager.getAllHelp();
// Single command lookup
if (args && args.length > 0) {
const cmd = args[0].toLowerCase();
const allCommands = this.terminal.moduleManager.getAllCommands();
if (allCommands[cmd]) {
const helpEntry = helpMap[cmd];
if (helpEntry) {
this.terminal.print(helpEntry, 'info');
} else {
this.terminal.print(`No help available for ${cmd}`, 'warning');
}
} else {
this.terminal.print(`No help available for ${cmd}`, 'warning');
}
return;
}
// Categorize all commands
const cats = {
files: ['ls','ll','cat','put','rm','mv','cp','mkdir','rmdir','cd','pwd','find','grep','head','tail','sort','wc','touch','tree','du','upload','download','vfs-export','vfs-import','edit','diff','trash','trash-dir','ls-trash','empty-trash'],
ai: ['ai','ask','ai-clear','ai-system','ai-context','ai-allow','ai-deny','ai-ignore','ai-stop','ai-model','ai-compress','debug'],
search: ['search','s','ddg','duck','fetch','web','extract'],
tools: ['calc','cron-every','cron-list','cron-stop','cron-clear','wpm','mindmap','theme','voice','alias','unalias',
'which','env','date','cal','yes','seq','base64','printf','cut','uniq','hostname','whoami','basename','dirname','sleep','expand','tutorial','gzip','gunzip','encrypt','decrypt','files','session-save','session-load','session-list','session-delete','session-export','session-import'],
system: ['help','clear','cls','echo','mem','status','font-size','setfont','smart-prompt','logs','preview','factory-reset','reset-vfs','export','unset','script','set','test','logrotate','health-check','memory-map-export','commands','check','checkpoint','checkpoint-list','rollback','checkpoint-del'],
net: ['server','servers','server-check','server-remove','monitor'],
mods: ['modules','listmods','loadmod','removemod','load-local','loadlocal','startup-modules'],
ollama: ['ollama','ollamaconnect','ollamadisconnect','ollamamodels','ollamasetmodel','ollamasetproxy','models','model-info','model-pull','model-delete','model-run'],
skills: ['skill-list','skill-view','skill-create','skill-patch','skill-delete','skill-export','skill-import','skill-sync','skill-pack-core'],
config: ['config','config-set','config-list','config-delete','config-export','config-reset'],
webx: ['webxai','wx','webx','context','contexts','context-new','context-delete','smart-ask','prompt','prompt-save','prompt-list','prompt-delete','prompt-var','prompt-vars'],
data: ['my','my-set','my-get','my-list','my-export','my-import','my-clear','export-mods','import-mods','backup','restore','share-mod'],
ext: ['chat','chat-setapi','chat-setmodel','chat-ask','webdav-config','webdav-status','webdav-ls','webdav-push','webdav-pull','webdav-backup','webdav-restore','webdav-backup-list','webdav-explorer','webdav-browser'],
gui: ['gui','gui-button','gui-input','gui-text','gui-clear','gui-run'],
vault: ['secret-set','secret-get','secret-list','secret-del','secret-lock','secret-unlock','secret-setpin','secret-status'],
};
const labels = {
files:'📁 Filesystem', ai:'🤖 AI', search:'🔍 Search & Web', tools:'🔧 Tools',
system:'⚙️ System', net:'📡 Network', mods:'🧩 Modules', ollama:'🦙 Ollama',
skills:'📚 Skills', config:'⚙️ Config', webx:'🌐 WebX', data:'💾 Data',
ext:'🧰 Extras', gui:'🖥️ GUI', vault:'🔒 Vault'
};
let result = '╔══════════════════════════════════════╗\n║ 🔷 WebX11 ' + (window.NEWOS4_VERSION || 'v4.0') + ' — All Commands ║\n╚══════════════════════════════════════╝\n\n';
const total = Object.keys(helpMap).length;
result += `📊 ${total} commands loaded\n\n`;
for (const [key, cmds] of Object.entries(cats)) {
const present = cmds.filter(c => helpMap[c]);
if (present.length === 0) continue;
result += labels[key] + '\n';
present.forEach(c => {
const desc = (helpMap[c] || c).split(' — ')[1] || '';
const short = desc.substring(0, 55);
result += ` ${c.padEnd(16)}${short}\n`;
});
result += '\n';
}
result += '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n help <command> → detailed usage\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━';
this.terminal.print(result.trim(), 'info');
}
}
|