// Extracted from newOS4 index.html — SmartContextModule
class SmartContextModule {
constructor() {
this.commands = {}; this.terminal = null;
this.contextProfiles = this.loadProfiles(); this.activeProfile = this.loadActiveProfile();
}
getHelpInfo() {
return {
'context': 'context [name] — Set or view current context profile',
'contexts': 'contexts — List all available context profiles',
'context-new': 'context-new — Create a new context profile',
'context-delete': 'context-delete — Delete a context profile',
'smart-ask': 'smart-ask — Ask a question using the active context'
};
}
loadProfiles() {
try {
const saved = localStorage.getItem('webx11_context_profiles');
return saved ? JSON.parse(saved) : this.getDefaultProfiles();
} catch (e) { return this.getDefaultProfiles(); }
}
saveProfiles() {
localStorage.setItem('webx11_context_profiles', JSON.stringify(this.contextProfiles));
}
loadActiveProfile() {
try {
const saved = localStorage.getItem('webx11_active_context_profile');
if (saved && this.contextProfiles[saved]) return saved;
} catch (e) {}
return null;
}
saveActiveProfile() {
try {
if (this.activeProfile) {
localStorage.setItem('webx11_active_context_profile', this.activeProfile);
} else {
localStorage.removeItem('webx11_active_context_profile');
}
} catch (e) {}
}
getDefaultProfiles() {
return {
'coding': { name: 'Coding Assistant', systemPrompt: "You are an expert programmer.", temperature: 0.7 },
'debug': { name: 'Debug Helper', systemPrompt: "You are a debugging expert.", temperature: 0.5 },
'creative': { name: 'Creative Writer', systemPrompt: "You are a creative writer.", temperature: 0.9 },
'technical': { name: 'Technical Docs', systemPrompt: "You are a technical writer.", temperature: 0.3 },
'chat': { name: 'Casual Chat', systemPrompt: "You are a friendly assistant.", temperature: 0.8 }
};
}
registerCommands() {
this.commands['context'] = (args) => this.setContext(args);
this.commands['contexts'] = () => this.listContexts();
this.commands['context-new'] = (args) => this.createContext(args);
this.commands['context-delete'] = (args) => this.deleteContext(args);
this.commands['smart-ask'] = (args) => this.smartAsk(args);
}
setContext(args) {
if (!args[0]) {
this.terminal.print(`Current context: ${this.activeProfile || 'none'}`, "info"); return;
}
if (this.contextProfiles[args[0]]) {
this.activeProfile = args[0];
this.saveActiveProfile();
this.terminal.print(`Context set to: ${this.contextProfiles[args[0]].name}`, "success");
} else {
this.terminal.print(`Profile '${args[0]}' not found`, "error");
}
}
listContexts() {
this.terminal.print("=== Context Profiles ===", "info");
Object.entries(this.contextProfiles).forEach(([key, profile]) => {
const active = key === this.activeProfile ? ' ✅' : '';
this.terminal.print(`${key}: ${profile.name}${active}`);
});
}
createContext(args) {
if (args.length < 3) {
this.terminal.print("Usage: context-new ", "warning"); return;
}
const name = args[0], displayName = args[1], systemPrompt = args.slice(2).join(' ');
this.contextProfiles[name] = { name: displayName, systemPrompt, temperature: 0.7 };
this.saveProfiles();
this.terminal.print(`Context profile '${name}' created`, "success");
}
deleteContext(args) {
if (!args[0]) {
this.terminal.print("Usage: context-delete ", "warning"); return;
}
if (this.contextProfiles[args[0]]) {
delete this.contextProfiles[args[0]];
this.saveProfiles();
if (this.activeProfile === args[0]) { this.activeProfile = null; }
this.terminal.print(`Context profile '${args[0]}' deleted`, "success");
} else {
this.terminal.print(`Profile '${args[0]}' not found`, "error");
}
}
async smartAsk(args) {
if (!args[0]) {
this.terminal.print("Usage: smart-ask ", "warning"); return;
}
let systemPrompt = "You are a helpful assistant.";
let temperature = 0.7;
if (this.activeProfile && this.contextProfiles[this.activeProfile]) {
const profile = this.contextProfiles[this.activeProfile];
systemPrompt = profile.systemPrompt;
temperature = profile.temperature || 0.7;
this.terminal.print(`Using context: ${profile.name} (temp: ${temperature})`, "info");
}
// Use agentic loop via the AI assistant module for tool-enabled responses
const aiModule = this.terminal?.moduleManager?.modules?.WebX11AIAssistantModule;
if (aiModule) {
// Set temperature on ollamaManager modelParams temporarily
const om = this.terminal.ollamaManager;
const savedTemp = om.modelParams ? om.modelParams.temperature : undefined;
if (om.modelParams) {
om.modelParams.temperature = temperature;
}
try {
const messages = [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: args.join(' ') }
];
const result = await om.chatWithTools(messages, aiModule.getToolDefinitions());
if (result && result.message) {
this.terminal.printAIResponse(result.message.content || '(no response)');
} else if (result && result.error) {
this.terminal.print(`⚠️ AI error: ${result.error}`, 'error');
}
} finally {
if (om.modelParams && savedTemp !== undefined) {
om.modelParams.temperature = savedTemp;
}
}
} else {
// Fallback: simple chat with temperature
const om = this.terminal.ollamaManager;
const fullPrompt = `${systemPrompt}\n\nUser: ${args.join(' ')}`;
await om.chat(fullPrompt);
}
}
}