aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/ai/AiModule.qml
blob: a96f48478c10b777b9e1ad5bfcdcc7b8293e4a67 (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
// 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 Quickshell
import Quickshell.Io
import QtQuick
import "../.."

// Not always active: the stack is polled while the drawer is open and not
// otherwise. The poll's lifetime is the tile's lifetime; see AiTile.qml.
Module {
    id: mod

    name: "ai"
    label: "AI"
    icon: ""
    alwaysActive: false

    // Rebuilt from ai-state.sh on every poll, in the script's order. See the
    // script for the line protocol; the field order here must match it.
    property var engines: []
    property var apps: []

    readonly property var runningApps: apps.filter(a => a.running)

    // Something is holding VRAM: a loaded llama model, a running sd-server,
    // or an app.
    active: runningApps.length > 0
         || engines.some(e => e.up && (e.name === "sd" || e.model !== ""))

    readonly property var labels: ({
        llama: "llama.cpp",
        sd: "stable-diffusion.cpp",
        assistant: "Desktop assistant",
        chat: "Local chat",
        imggen: "imggen",
        fanfic: "Fanfictioner",
    })

    readonly property string assistantSh:
        Quickshell.env("HOME") + "/Programming/GIT/desktop-assistant/assistant.sh"

    // Each project's own control. Fanfictioner has no start, a run needs a
    // story file, and its stop needs the pid, see stop().
    readonly property var controls: ({
        assistant: { start: [assistantSh, "start"], stop: [assistantSh, "stop"] },
        chat: { start: ["llamachat", "--daemon"], stop: ["llamachat", "--quit"] },
        imggen: { start: ["imggen", "start"], stop: ["imggen", "stop"] },
    })

    function canStart(app) { return controls[app.name] !== undefined; }

    function start(app) {
        if (canStart(app)) exec(controls[app.name].start);
    }

    function stop(app) {
        if (app.name !== "fanfic") {
            exec(controls[app.name].stop);
            return;
        }
        // The sd-cli children first, then the script, by pid only: a name
        // match would take unrelated sd-cli and python processes with it.
        if (!/^[0-9]+$/.test(app.detail)) return;
        exec(["sh", "-c", 'pkill -TERM -P "$1"; kill -TERM "$1"', "sh", app.detail]);
    }

    // Detached, so a start outlives a quickshell reload. No exit code comes
    // back; the next poll shows whether it worked.
    function exec(cmd) {
        Quickshell.execDetached(cmd);
        repoll.restart();
    }

    property Timer repoll: Timer {
        interval: 1000
        onTriggered: mod.refresh()
    }

    // The tile is created when the drawer panel loads, grid or page, and
    // destroyed when it unloads, so its presence is the poll's on/off switch.
    property bool polling: false
    onPollingChanged: if (polling) refresh()

    function refresh() {
        stateProc.engines = [];
        stateProc.apps = [];
        stateProc.running = false;
        stateProc.running = true;
    }

    property Process stateProc: Process {
        property var engines: []
        property var apps: []
        command: ["bash", `${Quickshell.shellDir}/modules/ai/ai-state.sh`]
        stdout: SplitParser {
            onRead: line => {
                const f = line.split("\t");
                const v = s => s === "-" ? "" : s;
                if (f[0] === "engine" && f.length === 5)
                    stateProc.engines.push({
                        name: f[1], up: f[2] === "up", busy: f[3] === "busy", model: v(f[4]),
                    });
                else if (f[0] === "app" && f.length === 4)
                    stateProc.apps.push({
                        name: f[1], running: f[2] === "running", detail: v(f[3]),
                    });
            }
        }
        // A run that printed nothing (bash itself failing) keeps the last
        // state rather than blanking the page.
        onExited: {
            if (stateProc.engines.length + stateProc.apps.length === 0) return;
            mod.engines = stateProc.engines;
            mod.apps = stateProc.apps;
        }
    }

    property Timer pollTimer: Timer {
        interval: 3000
        repeat: true
        running: mod.polling
        onTriggered: mod.refresh()
    }

    tileContent: Component { AiTile { ai: mod } }

    page: Component {
        Page {
            title: "AI"
            AiPage { width: parent.width; ai: mod }
        }
    }
}