phoebus_os/installer/live/desktop/phoebus-assistant/AssistantBridge.qml
Phoebus 3b219fc851
Some checks failed
ci / test (push) Has been cancelled
fix(agents): harden both user-level agent paths
2026-10-12 07:15:31 -07:00

220 lines
6.5 KiB
Bash

pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
// Typed JSON-RPC bridge to `sys --assistant-bridge`.
// Shares the exact same deterministic and AI admin broker with the CLI.
Singleton {
id: root
property bool ready: false
property bool crashed: false
property string lastError: ""
property var handshake: null
property var pending: ({})
property int nextId: 1
signal event(string name, var data)
signal handshakeReady()
readonly property var bridgeCommand: {
const override = Quickshell.env("PHOEBUS_ASSISTANT_BRIDGE_CMD");
if (override && override.length > 0)
return override.split(" ").filter(part => part.length > 0);
return ["/usr/local/bin/sys", "--assistant-bridge"];
}
property bool stoppedByUser: false
property int restartAttempts: 0
Timer {
id: restartTimer
interval: 1000 * Math.max(1, root.restartAttempts)
onTriggered: {
if (!root.stoppedByUser && !proc.running)
proc.running = true;
}
}
Timer {
interval: 60000
running: root.ready && proc.running
onTriggered: root.restartAttempts = 0
}
function start() {
root.stoppedByUser = false;
root.restartAttempts = 0;
if (!proc.running)
proc.running = true;
}
function stop() {
root.stoppedByUser = true;
restartTimer.stop();
if (proc.running)
proc.running = false;
}
function call(op, payload, onDone, onProgress) {
const id = root.nextId++;
if (onDone)
root.pending[id] = { done: onDone, progress: onProgress || null };
const request = Object.assign({ id: id, op: op }, payload || {});
const line = JSON.stringify(request) + "\n";
if (!proc.running) {
delete root.pending[id];
root.lastError = "backend not running";
if (onDone)
onDone({ ok: false, error: "The assistant broker is not running." });
return;
}
proc.write(line);
}
function handleLine(line) {
const text = ("" + line).trim();
if (text.length === 0)
return;
let msg;
try {
msg = JSON.parse(text);
} catch (e) {
root.lastError = "The assistant returned an invalid protocol message.";
return;
}
if (msg.id !== undefined && root.pending[msg.id]) {
const callback = root.pending[msg.id];
if (msg.event === "progress") {
if (callback.progress)
callback.progress(msg.progress || {});
return;
}
delete root.pending[msg.id];
callback.done(msg);
}
}
function refreshHandshake(cb) {
call("handshake", {}, reply => {
if (reply.ok && reply.result && reply.result.protocol_version === 1) {
root.handshake = reply.result;
root.ready = true;
root.handshakeReady();
} else {
root.lastError = reply.error || "The assistant protocol is incompatible.";
root.crashed = true;
}
if (cb)
cb(reply);
});
}
function ask(query, backend, cb, onProgress) {
call("ask", { request: query, backend: backend }, cb, onProgress);
}
function prepareOverlay(atom, repository, cb, onProgress) {
call("overlay_prepare", { application: atom, repository: repository }, cb, onProgress);
}
function execute(planId, digest, yes, cb, onProgress) {
call("execute", { plan_id: planId, digest: digest, yes: yes }, cb, onProgress);
}
function legacyInstall(application, backend, cb, onProgress) {
call("legacy_install", { application: application, backend: backend }, cb, onProgress);
}
function selectCatalogSource(packageId, backend, cb, onProgress) {
call("select_catalog_source", { application: packageId, backend: backend }, cb, onProgress);
}
function legacyExecute(phrase, action, yes, cb) {
call("legacy_execute", { phrase: phrase, action: action, yes: yes }, cb);
}
function prepareBuild(action, cb, onProgress) {
call("prepare_build", { action: action }, cb, onProgress);
}
function getUpdates(cb) {
call("updates", {}, cb);
}
function getProactiveAlerts(cb) {
call("proactive_alerts", {}, cb);
}
function snoozeAlerts(durationSeconds, cb) {
call("proactive_snooze", { duration_seconds: durationSeconds }, cb);
}
function dismissAlert(fingerprint, cb) {
call("proactive_dismiss", { fingerprint: fingerprint }, cb);
}
function getPreferences(cb) {
call("get_preferences", {}, cb);
}
function setPreferences(prefs, cb) {
call("set_preferences", { preferences: prefs }, cb);
}
function getHistory(cb) {
call("history", {}, cb);
}
function clearConversation(cb) {
call("clear_conversation", {}, cb);
}
function undo(snapshot, cb) {
call("undo", { snapshot: snapshot, yes: true }, cb);
}
Process {
id: proc
command: root.bridgeCommand
stdinEnabled: true
running: false
onStarted: {
root.ready = false;
root.handshake = null;
root.crashed = false;
root.refreshHandshake(null);
}
onExited: exitCode => {
root.ready = false;
root.crashed = !root.stoppedByUser;
root.handshake = null;
const ids = Object.keys(root.pending);
for (let i = 0; i < ids.length; i++) {
const cb = root.pending[ids[i]];
delete root.pending[ids[i]];
try {
cb.done({ ok: false, error: "The assistant bridge stopped (exit " + exitCode + "). Check History before retrying an action that was executing." });
} catch (e) {}
}
// Restore the connection only; never replay pending requests.
if (!root.stoppedByUser && root.restartAttempts < 3) {
root.restartAttempts++;
restartTimer.restart();
}
}
stdout: SplitParser {
onRead: line => root.handleLine(line)
}
stderr: SplitParser {
onRead: line => {
root.lastError = ("" + line).slice(0, 1024);
}
}
}
}