aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/assistant/AssistantPage.qml
blob: e5d76c884c515de36f310992c00e5425769003ad (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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import QtQuick
import QtQuick.Controls
import "../.."

// The view over AssistantModule: the model settings, the tools' environment,
// the tools themselves, and the restart that applies them. A field saves on
// Enter; emptied, it falls back to the daemon's default, which it shows as
// its placeholder.
Column {
    id: page
    spacing: 8

    // The module. Not named `am`, its id (it would bind to itself).
    required property QtObject asst

    readonly property var v: asst.values
    readonly property var d: asst.defaults

    // What the daemon would run with: the file's value, else the default.
    function shown(key) {
        const x = page.v[key] !== undefined ? page.v[key] : page.d[key];
        return x === undefined || x === null ? "" : String(x);
    }

    component Small: Text {
        color: Theme.subtext
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
    }

    component Heading: Text {
        topPadding: 14
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize; bold: true }
        color: Theme.text
    }

    // A one-line input that saves on Enter and undoes on Escape; edited
    // reports every keystroke, for the add row.
    component Field: Rectangle {
        id: field
        property string value: ""
        property string placeholder: ""
        signal commit(string text)
        signal edited(string text)
        // Commit on Enter even when unchanged: the add row's fields track
        // their value as they are typed, so they never differ from it.
        property bool live: false
        height: 34
        radius: 8
        color: Theme.surface
        TextInput {
            id: ti
            anchors { fill: parent; leftMargin: 10; rightMargin: 10 }
            verticalAlignment: TextInput.AlignVCenter
            clip: true
            selectByMouse: true
            color: Theme.text
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
            text: field.value
            onTextEdited: field.edited(text)
            onAccepted: if (field.live || text !== field.value) field.commit(text)
            Keys.onEscapePressed: event => {
                if (text !== field.value) text = field.value;
                else event.accepted = false;
            }
            Text {
                anchors.verticalCenter: parent.verticalCenter
                width: parent.width
                elide: Text.ElideRight
                visible: parent.text === ""
                text: field.placeholder
                color: Theme.overlay
                font: parent.font
            }
        }
    }

    // A labelled row: label on the left, the control filling the rest.
    component Labelled: Item {
        property alias label: lbl.text
        default property alias control: slot.data
        width: page.width
        height: 34
        Text {
            id: lbl
            anchors { left: parent.left; verticalCenter: parent.verticalCenter }
            width: 120
            color: Theme.subtext
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        }
        Item {
            id: slot
            anchors { left: lbl.right; leftMargin: 8; right: parent.right; top: parent.top; bottom: parent.bottom }
        }
    }

    // --- restart ---

    Rectangle {
        width: page.width
        height: bar.implicitHeight + 20
        radius: 10
        color: page.asst.pending ? Qt.alpha(Theme.accent, 0.16) : Qt.alpha(Theme.surface, 0.35)
        border.width: 1
        border.color: page.asst.pending ? Qt.alpha(Theme.accent, 0.5) : Qt.alpha(Theme.text, 0.08)

        Column {
            id: bar
            anchors { left: parent.left; right: parent.right; verticalCenter: parent.verticalCenter; margins: 12 }
            spacing: 6

            Item {
                width: parent.width
                height: 30
                Text {
                    anchors { left: parent.left; right: restartBtn.left; rightMargin: 8; verticalCenter: parent.verticalCenter }
                    wrapMode: Text.Wrap
                    text: page.asst.pending ? "Saved. The assistant reads these at startup: restart it to apply."
                                            : "The assistant reads these at startup."
                    color: Theme.text
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
                }
                Button {
                    id: restartBtn
                    anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                    text: page.asst.restarting ? "Restarting…" : "Restart assistant"
                    enabled: !page.asst.restarting
                    onClicked: page.asst.restart()
                }
            }

            Small {
                width: parent.width
                visible: page.asst.status !== ""
                wrapMode: Text.Wrap
                text: page.asst.status
                color: page.asst.status === "Restarted." ? Theme.green : Theme.red
            }
        }
    }

    Text {
        width: page.width
        visible: page.asst.error !== ""
        wrapMode: Text.Wrap
        text: page.asst.error
        color: Theme.red
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
    }

    // --- model ---

    Heading { text: "Model" }

    Labelled {
        objectName: "key:llm"
        label: "Server"
        Field {
            anchors.fill: parent
            value: page.v.llm ?? ""
            placeholder: page.d.llm ?? ""
            onCommit: t => page.asst.setKey("llm", t.trim() ? page.asst.quote(t.trim()) : null)
        }
    }

    Labelled {
        objectName: "key:temperature"
        label: "Temperature"
        Field {
            id: temp
            anchors { left: parent.left; top: parent.top; bottom: parent.bottom }
            width: 120
            value: page.v.temperature !== undefined ? String(page.v.temperature) : ""
            placeholder: "the model's preset"
            onCommit: t => {
                const x = Number(t.trim());
                if (t.trim() === "") page.asst.setKey("temperature", null);
                else if (!isNaN(x) && x >= 0 && x <= 2) page.asst.setKey("temperature", String(x));
                else page.asst.error = "Temperature is a number from 0 to 2.";
            }
        }
        Small {
            anchors { left: temp.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            text: "0 to 2; empty uses the llama-server preset's"
        }
    }

    Labelled {
        objectName: "key:tool_rounds"
        label: "Tool rounds"
        Field {
            id: rounds
            anchors { left: parent.left; top: parent.top; bottom: parent.bottom }
            width: 120
            value: page.v.tool_rounds !== undefined ? String(page.v.tool_rounds) : ""
            placeholder: page.d.tool_rounds !== undefined ? String(page.d.tool_rounds) : ""
            onCommit: t => {
                const n = Number(t.trim());
                if (t.trim() === "") page.asst.setKey("tool_rounds", null);
                else if (Number.isInteger(n) && n >= 0 && n <= 20) page.asst.setKey("tool_rounds", String(n));
                else page.asst.error = "Tool rounds is a whole number from 0 to 20.";
            }
        }
        Small {
            anchors { left: rounds.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            text: "tool round trips per question"
        }
    }

    Labelled {
        objectName: "key:thinking"
        label: "Thinking"
        Switch {
            id: think
            anchors.verticalCenter: parent.verticalCenter
            checked: page.shown("thinking") !== "false"
            onToggled: page.asst.setKey("thinking", checked ? "true" : "false")
        }
        Small {
            anchors { left: think.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            text: "off skips the model's reasoning: a faster first word"
        }
    }

    // The prompt is long and multi-line, so it gets its own box and an
    // explicit Save rather than Enter.
    Column {
        objectName: "key:system"
        width: page.width
        spacing: 6

        Text {
            text: "System prompt"
            color: Theme.subtext
            font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
        }

        Rectangle {
            width: parent.width
            height: 130
            radius: 8
            color: Theme.surface

            ScrollView {
                anchors { fill: parent; margins: 8 }
                TextArea {
                    id: prompt
                    wrapMode: TextEdit.Wrap
                    selectByMouse: true
                    color: Theme.text
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
                    background: null
                    padding: 0
                    text: page.shown("system")
                }
            }
        }

        Row {
            spacing: 8
            Button {
                text: "Save prompt"
                enabled: prompt.text.trim() !== "" && prompt.text !== page.shown("system")
                onClicked: page.asst.setKey("system", prompt.text === page.d.system ? null
                                                      : page.asst.quote(prompt.text))
            }
            Button {
                text: "Use the default"
                enabled: page.v.system !== undefined
                onClicked: page.asst.setKey("system", null)
            }
            Small {
                anchors.verticalCenter: parent.verticalCenter
                text: page.v.system !== undefined ? "custom" : "default"
            }
        }
    }

    // --- env ---

    Heading { text: "Tool environment" }
    Small {
        width: page.width
        wrapMode: Text.Wrap
        text: "Added to every tool's environment: instance URLs, key file paths."
    }

    property string confirming: ""

    Repeater {
        model: page.asst.envList

        delegate: Item {
            id: envRow
            required property var modelData
            objectName: "env:" + modelData.key
            width: page.width
            height: 34

            Text {
                id: envKey
                anchors { left: parent.left; verticalCenter: parent.verticalCenter }
                width: 120
                elide: Text.ElideRight
                text: envRow.modelData.key
                color: Theme.subtext
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
            }
            Field {
                anchors { left: envKey.right; leftMargin: 8; right: envDel.left; rightMargin: 8; top: parent.top; bottom: parent.bottom }
                value: envRow.modelData.value
                onCommit: t => page.asst.setEnv(envRow.modelData.key, t)
            }
            Button {
                id: envDel
                anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                danger: true
                text: page.confirming === envRow.modelData.key ? "Confirm" : "Remove"
                onClicked: {
                    if (page.confirming === envRow.modelData.key) {
                        page.confirming = "";
                        page.asst.setEnv(envRow.modelData.key, null);
                    } else page.confirming = envRow.modelData.key;
                }
            }
        }
    }

    // The add row: a key, a value, Add.
    property string newKey: ""
    property string newValue: ""
    readonly property bool keyOk: /^[A-Za-z_][A-Za-z0-9_]*$/.test(page.newKey)
                                  && !page.asst.envList.some(e => e.key === page.newKey)
    function addEnv() {
        if (!page.keyOk) return;
        page.asst.setEnv(page.newKey, page.newValue);
        page.newKey = "";
        page.newValue = "";
    }

    Item {
        width: page.width
        height: 34
        Field {
            id: keyField
            anchors { left: parent.left; top: parent.top; bottom: parent.bottom }
            width: 120
            value: page.newKey
            placeholder: "NEW_KEY"
            onEdited: t => page.newKey = t
        }
        Field {
            anchors { left: keyField.right; leftMargin: 8; right: addBtn.left; rightMargin: 8; top: parent.top; bottom: parent.bottom }
            value: page.newValue
            placeholder: "value"
            live: true
            onEdited: t => page.newValue = t
            onCommit: page.addEnv()
        }
        Button {
            id: addBtn
            anchors { right: parent.right; verticalCenter: parent.verticalCenter }
            text: "Add"
            enabled: page.keyOk
            onClicked: page.addEnv()
        }
    }

    // --- tools ---

    Heading { text: "Tools" }
    Small {
        width: page.width
        wrapMode: Text.Wrap
        text: "What the model may call. Off renames the file to .toml.off, which the daemon skips."
    }

    Repeater {
        model: page.asst.tools

        delegate: Item {
            id: toolRow
            required property var modelData
            objectName: "tool:" + modelData.name
            width: page.width
            height: Math.max(40, toolText.implicitHeight + 10)

            Switch {
                id: toolSw
                anchors { left: parent.left; verticalCenter: parent.verticalCenter }
                checked: toolRow.modelData.on
                onToggled: page.asst.toggleTool(toolRow.modelData)
            }
            Column {
                id: toolText
                anchors { left: toolSw.right; leftMargin: 10; right: parent.right; verticalCenter: parent.verticalCenter }
                Text {
                    text: toolRow.modelData.name
                    color: toolRow.modelData.on ? Theme.text : Theme.subtext
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2; bold: true }
                }
                Small {
                    width: parent.width
                    wrapMode: Text.Wrap
                    text: toolRow.modelData.description
                }
            }
        }
    }
}