aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-smartctx.js
blob: 2b4712d746e945070744c887e7064c27f8966af6 (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
// Extracted from newOS4 index.html — SmartContextModule
        class SmartContextModule {
            constructor() {
                this.commands = {}; this.terminal = null;
                this.contextProfiles = this.loadProfiles(); this.activeProfile = null;
            }
            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));
            }
            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.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.";
                if (this.activeProfile && this.contextProfiles[this.activeProfile]) {
                    systemPrompt = this.contextProfiles[this.activeProfile].systemPrompt;
                    this.terminal.print(`Using context: ${this.contextProfiles[this.activeProfile].name}`, "info");
                }
                const fullPrompt = `${systemPrompt}\n\nUser: ${args.join(' ')}`;
                await this.terminal.ollamaManager.chat(fullPrompt);
            }
        }