import QtQuick import Quickshell import Quickshell.Io // Shared state for every Quick Fix widget (one per bar): the conversation on // screen, whether a job runs, history. Jobs run in their own systemd user // units (helpers/quickfix.py), so a rebuild that restarts the shell does not // stop them; on start this reopens the chat when a job is running or its // result has not been seen yet. Item { id: root property var shell: null property string pluginId: "mx-quickfix" property var session: null property bool running: false property bool showHistory: false property var history: [] property string error: "" property var available: ({ claude: true, gpt: true, local: false }) property var remaining: ({}) property string provider: "" // Opened by a restart, not by the user: it does not take the keyboard. property bool passive: false // Open panels (one per screen at most). property int openPanels: 0 readonly property bool panelOpen: openPanels > 0 readonly property var last: session && session.messages.length ? session.messages[session.messages.length - 1] : null readonly property bool unseenFailure: !panelOpen && !!last && last.status === "failed" && pendingSeen readonly property string helper: decodeURIComponent(Qt.resolvedUrl("helpers/quickfix.py").toString().replace(/^file:\/\//, "")) // Bar settings -> helper options. function options(settings) { const s = settings || {} const list = value => String(value || "").split(/[,\n]/).map(v => v.trim()).filter(v => v) return { repositories: list(s.repositories), allow: list(s.allow), applyCommand: String(s.applyCommand || "").trim() ? ["sh", "-c", String(s.applyCommand)] : [], claudeModel: String(s.claudeModel || ""), gptModel: String(s.gptModel || ""), localUrl: String(s.localUrl || ""), localModel: String(s.localModel || "") } } // One helper call at a time; replies arrive in order. property var queue: [] function call(request, done) { queue = queue.concat([{ request: request, done: done || null }]) if (!job.running) next() } function next() { if (!queue.length) return job.current = queue[0] queue = queue.slice(1) job.command = ["python3", helper, JSON.stringify(job.current.request)] job.running = true } Process { id: job property var current: null stdout: StdioCollector { id: out } stderr: StdioCollector { id: err } onExited: function(code) { let result try { result = JSON.parse(out.text) } catch (e) { result = { ok: false, error: (err.text || "The helper failed").trim().split("\n").pop() } } const entry = job.current if (!result.ok) root.error = result.error || "Something went wrong" else if (entry && entry.request.op !== "state" && entry.request.op !== "providers") root.error = "" if (entry && entry.done) entry.done(result) Qt.callLater(root.next) } } function apply(result) { if (!result.ok || result.session === undefined) return const wasRunning = running session = result.session running = !!result.session && !!result.session.running if (wasRunning && !running) finished() } property bool pendingSeen: false function finished() { if (!session) return if (panelOpen) { call({ op: "seen", session: session.id }) pendingSeen = false return } // Closed while it worked: say how it went, then start fresh next time. pendingSeen = true const text = last ? last.text : "" Quickshell.execDetached(["notify-send", "-a", "Quick Fix", "-i", last && last.status === "failed" ? "dialog-error" : "dialog-information", last && last.status === "failed" ? "Quick fix failed" : "Quick fix done", text]) } // Settings of the widget that last used the service, for IPC calls. property var lastSettings: ({}) function send(text, settings) { text = String(text || "").trim() if (!text || running) return false lastSettings = settings || lastSettings const request = { op: "send", text: text, provider: provider || "auto", options: options(settings) } if (session && !showHistory) request.session = session.id showHistory = false // Shown at once; the helper's copy replaces it. const shown = session && request.session ? JSON.parse(JSON.stringify(session)) : { id: "", messages: [] } shown.messages.push({ role: "user", text: text }, { role: "agent", text: "", status: "running", provider: "" }) session = shown running = true call(request, result => { if (result.ok) apply(result) else { running = false; session = request.session ? session : null; if (request.session) refresh() } }) return true } function stop() { if (session && running) call({ op: "stop", session: session.id }) } function undo(run, settings) { if (!session || running) return running = true call({ op: "undo", session: session.id, run: run, options: options(settings) }, result => { if (result.ok) apply(result) else { running = false; refresh() } }) } function refresh() { call({ op: "state" }, result => { if (!result.ok) return // Another conversation may be on screen (history); only follow ours. if (result.session && (!session || session.id === result.session.id || result.running)) apply(result) else if (running && !result.running) { running = false } }) } function newChat() { showHistory = false if (!running) session = null error = "" } function openHistory() { showHistory = !showHistory if (showHistory) call({ op: "history" }, result => { if (result.ok) history = result.history }) } function load(id) { call({ op: "load", session: id }, result => { if (!result.ok) return showHistory = false if (!running) session = result.session }) } function clearHistory() { call({ op: "clearHistory" }, result => { if (result.ok) history = result.history }) } function checkProviders(settings) { lastSettings = settings || lastSettings call({ op: "providers", options: options(settings) }, result => { if (!result.ok) return available = result.available remaining = result.remaining }) } // A fresh start each time it opens, unless a job is still running. onPanelOpenChanged: { if (panelOpen) { if (pendingSeen && session) { call({ op: "seen", session: session.id }) pendingSeen = false if (!running) session = null } return } passive = false showHistory = false error = "" if (!running) session = null } Timer { interval: 1500 repeat: true running: root.running onTriggered: root.refresh() } // After a restart: reopen on a running job or an unseen result. property int summonTries: 0 Timer { id: summonRetry interval: 500 repeat: true onTriggered: { root.summonTries++ if (root.panelOpen || root.summonTries > 40) { stop(); return } if (root.shell && root.shell.summon(root.pluginId, "")) stop() } } Component.onCompleted: call({ op: "state" }, result => { if (!result.ok || !result.session || !(result.running || !result.seen)) return root.session = result.session root.running = result.running root.passive = true root.summonTries = 0 summonRetry.start() }) IpcHandler { target: "quickfix" function toggle(): void { if (root.shell) root.shell.toggle(root.pluginId, "") } // Sends to the open (or a new) conversation with the bar's settings. function send(text: string): bool { return root.send(text, root.lastSettings) } // Shows or hides History in the open chat. function history(): void { root.openHistory() } // Undoes the newest change in the conversation on screen. function undo(): bool { const run = root.session ? root.session.messages.slice().reverse().find(m => m.changed && !m.undone && m.run) : null if (!run || root.running) return false root.undo(run.run, root.lastSettings) return true } function status(): string { return JSON.stringify({ open: root.panelOpen, running: root.running, passive: root.passive, session: root.session ? root.session.id : "", error: root.error, messages: root.session ? root.session.messages.length : 0, last: root.last ? root.last.status + ": " + root.last.text : "" }) } } }