aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/assistant/AssistantPage.qml
blob: 297078443f4cb5490d9bdf56a6cc53b68fbad7f0 (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// 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. Every field and
// switch edits the module's draft; the header's Save writes it. Emptied, a
// field falls back to the daemon's default, which it shows as 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 reporting every keystroke, which the page turns into
    // a draft edit. It is not bound to value, which would reset the cursor
    // and eat a trailing space; it follows the value only when the draft
    // moves under it (Discard, an add). bad outlines a refused value.
    component Field: Rectangle {
        id: field
        property string value: ""
        property string placeholder: ""
        property bool bad: false
        signal edited(string text)
        signal accepted
        height: 34
        radius: 8
        color: Theme.surface
        border.width: field.bad ? 1 : 0
        border.color: Theme.red
        onValueChanged: if (ti.text.trim() !== field.value.trim()) ti.text = field.value
        onBadChanged: if (!field.bad) ti.text = field.value
        TextInput {
            activeFocusOnTab: true
            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 }
            Component.onCompleted: text = field.value
            onTextEdited: field.edited(text)
            onAccepted: field.accepted()
            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: Save, then restart it."
                    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" }

    Small {
        width: page.width
        wrapMode: Text.Wrap
        text: "The assistant answers with whichever model llama-server has loaded. Opening it loads "
            + "the preferred model if another one is in. Load swaps a model in now, for every client, "
            + "without changing the preference."
    }

    Small {
        visible: page.asst.models.length === 0
        text: "llama-server is not answering at " + page.asst.llm
        color: Theme.red
    }

    Repeater {
        model: page.asst.models

        delegate: Rectangle {
            id: modelRow
            required property var modelData
            readonly property bool loaded: modelData.status === "loaded"
            readonly property bool busy: page.asst.loading === modelData.id
            readonly property bool preferred: Prefs.assistantModel === modelData.id
            objectName: "model:" + modelData.id
            width: page.width
            height: 40
            radius: 8
            color: loaded ? Qt.alpha(Theme.accent, 0.14) : Qt.alpha(Theme.surface, 0.35)
            border.width: 1
            border.color: loaded ? Qt.alpha(Theme.accent, 0.5) : "transparent"

            Rectangle {
                id: dot
                anchors { left: parent.left; leftMargin: 12; verticalCenter: parent.verticalCenter }
                width: 8; height: 8; radius: 4
                color: modelRow.loaded ? Theme.green : modelRow.busy ? Theme.yellow : Theme.overlay
            }
            Text {
                id: modelName
                anchors { left: dot.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
                text: modelRow.modelData.id
                color: Theme.text
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2; bold: modelRow.loaded }
            }
            Small {
                anchors { left: modelName.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
                text: [modelRow.busy ? "loading…" : modelRow.modelData.status,
                       modelRow.preferred ? "preferred" : ""].filter(s => s).join("  ·  ")
                color: modelRow.preferred ? Theme.accent : Theme.subtext
            }
            // Two separate choices: which model the assistant prefers, kept,
            // and which one is loaded now, a swap for every client.
            Button {
                id: loadBtn
                anchors { right: parent.right; rightMargin: 6; verticalCenter: parent.verticalCenter }
                text: "Load"
                enabled: page.asst.loading === "" && !modelRow.loaded
                onClicked: page.asst.loadModel(modelRow.modelData.id)
            }
            Button {
                anchors { right: loadBtn.left; rightMargin: 6; verticalCenter: parent.verticalCenter }
                text: modelRow.preferred ? "Preferred" : "Prefer"
                enabled: !modelRow.preferred
                onClicked: Prefs.setAssistantModel(modelRow.modelData.id)
            }
        }
    }

    Small {
        width: page.width
        visible: page.asst.modelError !== ""
        wrapMode: Text.Wrap
        text: page.asst.modelError
        color: Theme.red
    }

    Labelled {
        objectName: "key:llm"
        label: "Server"
        Field {
            anchors.fill: parent
            value: page.v.llm ?? ""
            placeholder: page.d.llm ?? ""
            onEdited: 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"
            bad: page.asst.invalid["key:temperature"] ?? false
            onEdited: t => {
                const x = Number(t.trim());
                const ok = t.trim() === "" || (!isNaN(x) && x >= 0 && x <= 2);
                page.asst.markInvalid("key:temperature", !ok);
                if (ok) page.asst.setKey("temperature", t.trim() === "" ? null : String(x));
            }
        }
        Small {
            anchors { left: temp.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            text: "0 to 2; empty uses the llama-server preset's"
            color: temp.bad ? Theme.red : Theme.subtext
        }
    }

    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) : ""
            bad: page.asst.invalid["key:tool_rounds"] ?? false
            onEdited: t => {
                const n = Number(t.trim());
                const ok = t.trim() === "" || (Number.isInteger(n) && n >= 0 && n <= 20);
                page.asst.markInvalid("key:tool_rounds", !ok);
                if (ok) page.asst.setKey("tool_rounds", t.trim() === "" ? null : String(n));
            }
        }
        Small {
            anchors { left: rounds.right; leftMargin: 10; verticalCenter: parent.verticalCenter }
            text: "a whole number, 0 to 20: tool round trips per question"
            color: rounds.bad ? Theme.red : Theme.subtext
        }
    }

    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")
                    // Into the draft as it is typed; the default, or nothing,
                    // unsets the key rather than copying the default in.
                    onTextChanged: if (prompt.text !== page.shown("system"))
                        page.asst.setKey("system", prompt.text.trim() === "" || prompt.text === page.d.system
                                                   ? null : page.asst.quote(prompt.text))
                }
            }
        }

        Row {
            spacing: 8
            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.envKeys

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

            Text {
                id: envKey
                anchors { left: parent.left; verticalCenter: parent.verticalCenter }
                width: 120
                elide: Text.ElideRight
                text: envRow.modelData
                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: page.asst.envValue(envRow.modelData)
                onEdited: t => page.asst.setEnv(envRow.modelData, t)
            }
            Button {
                id: envDel
                anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                danger: true
                text: page.confirming === envRow.modelData ? "Confirm" : "Remove"
                onClicked: {
                    if (page.confirming === envRow.modelData) {
                        page.confirming = "";
                        page.asst.setEnv(envRow.modelData, null);
                    } else page.confirming = envRow.modelData;
                }
            }
        }
    }

    // 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"
            onEdited: t => page.newValue = t
            onAccepted: 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: page.asst.toolOn(toolRow.modelData)
                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: page.asst.toolOn(toolRow.modelData) ? 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
                }
            }
        }
    }
}