phoebus_os/installer/live/desktop/phoebus-assistant/AssistantBridge.qml
Phoebus 67fddf1299
Some checks are pending
ci / test (push) Waiting to run
feat(sys): review and enable scoped external overlays
2026-10-03 15:06:34 -07:00

188 lines
5.2 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"];
}
Timer {
interval: 100
repeat: false
running: proc.running && root.handshake === null
onTriggered: root.refreshHandshake(null)
}
function start() {
if (!proc.running)
proc.running = true;
}
function 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) {
console.log("assistant-bridge: unparsable line: " + text);
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) {
root.handshake = reply.result;
root.handshakeReady();
}
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) {
call("legacy_install", { application: application, backend: backend }, cb);
}
function legacyExecute(phrase, action, yes, cb) {
call("legacy_execute", { phrase: phrase, action: action, yes: yes }, cb);
}
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 = true;
root.crashed = false;
root.refreshHandshake(null);
}
onExited: exitCode => {
root.ready = false;
root.crashed = true;
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 + ")." });
} catch (e) {}
}
}
stdout: SplitParser {
onRead: line => root.handleLine(line)
}
stderr: SplitParser {
onRead: line => {
root.lastError = line;
console.log("assistant-bridge err: " + line);
}
}
}
}