aboutsummaryrefslogtreecommitdiff
path: root/modules/core/module-dynamicprompts.js
blob: ff8c5ea0ca5c6f15ad2d47fcfc65dbd6f544b616 (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
// Extracted from newOS4 index.html — DynamicPromptsModule
        class DynamicPromptsModule {
            constructor() {
                this.commands = {}; this.terminal = null;
                this.prompts = this.loadPrompts(); this.variables = {};
            }
            getHelpInfo() {
                return {
                    'prompt': 'prompt <name> [additional-text] — Use a saved prompt template',
                    'prompt-save': 'prompt-save <name> <template> — Save a new prompt template',
                    'prompt-list': 'prompt-list — List all saved prompt templates',
                    'prompt-delete': 'prompt-delete <name> — Delete a saved prompt',
                    'prompt-var': 'prompt-var <name> <value> — Set a template variable',
                    'prompt-vars': 'prompt-vars — List all template variables'
                };
            }
            loadPrompts() {
                try {
                    const saved = localStorage.getItem('webx11_prompts');
                    return saved ? JSON.parse(saved) : {};
                } catch (e) { return {}; }
            }
            savePrompts() {
                localStorage.setItem('webx11_prompts', JSON.stringify(this.prompts));
            }
            registerCommands() {
                this.commands['prompt'] = (args) => this.usePrompt(args);
                this.commands['prompt-save'] = (args) => this.savePrompt(args);
                this.commands['prompt-list'] = () => this.listPrompts();
                this.commands['prompt-delete'] = (args) => this.deletePrompt(args);
                this.commands['prompt-var'] = (args) => this.setVariable(args);
                this.commands['prompt-vars'] = () => this.listVariables();
            }
            usePrompt(args) {
                if (!args[0]) {
                    this.terminal.print("Usage: prompt <name> [additional-text]", "warning"); return;
                }
                const name = args[0], additional = args.slice(1).join(' ');
                if (!this.prompts[name]) {
                    this.terminal.print(`Prompt '${name}' not found`, "error"); return;
                }
                let template = this.prompts[name].template;
                Object.entries(this.variables).forEach(([k, v]) => {
                    template = template.replace(new RegExp(`\\{${k}\\}`, 'g'), v);
                });
                const finalPrompt = additional ? `${template} ${additional}` : template;
                this.terminal.print("=== Generated Prompt ===", "info");
                this.terminal.printHTML(`<div class="output-line ollama" style="margin-top: 5px;">
                    <button class="copy-btn" onclick="navigator.clipboard.writeText(this.parentElement.innerText.replace('Copy','').trim())">Copy</button>
                    ${finalPrompt}
                </div>`);
                this.terminal.print("\nUse with: ai <generated-prompt>", "info");
            }
            savePrompt(args) {
                if (args.length < 2) {
                    this.terminal.print("Usage: prompt-save <name> <template>", "warning"); return;
                }
                const name = args[0], template = args.slice(1).join(' ');
                this.prompts[name] = { template, created: new Date().toISOString() };
                this.savePrompts();
                this.terminal.print(`Prompt '${name}' saved`, "success");
            }
            listPrompts() {
                const names = Object.keys(this.prompts);
                if (names.length === 0) {
                    this.terminal.print("No saved prompts", "info"); return;
                }
                this.terminal.print("=== Saved Prompts ===", "info");
                names.forEach(name => {
                    const p = this.prompts[name];
                    this.terminal.print(`📝 ${name}: ${p.template.substring(0, 50)}...`);
                });
            }
            deletePrompt(args) {
                if (!args[0]) {
                    this.terminal.print("Usage: prompt-delete <name>", "warning"); return;
                }
                if (this.prompts[args[0]]) {
                    delete this.prompts[args[0]];
                    this.savePrompts();
                    this.terminal.print(`Prompt '${args[0]}' deleted`, "success");
                } else {
                    this.terminal.print(`Prompt '${args[0]}' not found`, "error");
                }
            }
            setVariable(args) {
                if (args.length < 2) {
                    this.terminal.print("Usage: prompt-var <name> <value>", "warning"); return;
                }
                const name = args[0], value = args.slice(1).join(' ');
                this.variables[name] = value;
                this.terminal.print(`Variable set: ${name} = ${value}`, "success");
            }
            listVariables() {
                const names = Object.keys(this.variables);
                if (names.length === 0) {
                    this.terminal.print("No variables set", "info"); return;
                }
                this.terminal.print("=== Variables ===", "info");
                names.forEach(name => { this.terminal.print(`${name}: ${this.variables[name]}`); });
            }
        }