aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-smartctx.js
blob: 81929f8c0e0fc7aa805e3396ec4b91cb28cd8239 (plain)
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
// 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 <name> <displayName> <systemPrompt> — Create a new context profile',
                    'context-delete': 'context-delete <name> — Delete a context profile',
                    'smart-ask': 'smart-ask <question> — 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 <name> <displayName> <systemPrompt>", "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 <name>", "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 <your-question>", "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);
                }
            }
        }