phoebus_os/installer/live/desktop/common/Bridge.qml
Phoebus c16014ade8 feat(gui): select and validate OpenCode from the installer
Add an 'OpenCode agent' choice with server/password/provider/model fields,
a Test connection action, and a secret-free frontend state line the
harness asserts. Require exactly one installer session and assert the
current step and AI state instead of inferring from a screenshot. Add a
live-OpenCode acceptance make target and document the adapter, transport
policy, states and workflows (ADR 0011 updated).
2026-09-22 02:36:36 -07:00

176 lines
5.1 KiB
Bash

pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
// Typed JSON bridge to the privileged Rust installer. The frontend never
// scrapes terminal output: every call is a typed request and every reply is a
// typed JSON object. Secrets are written over the process stdin only.
Singleton {
id: root
// True once the bridge process is running and accepting requests.
property bool ready: false
property bool crashed: false
property string crashMessage: ""
property string lastError: ""
property var handshake: null
property var config: null
property var pending: ({})
property int nextId: 1
signal event(string name, var data)
signal availabilityChanged
signal handshakeReady
readonly property var bridgeCommand: {
const override = Quickshell.env("PHOEBUS_BRIDGE_CMD");
if (override && override.length > 0)
return override.split(" ").filter(part => part.length > 0);
// Absolute paths: Quickshell's Process execs the command directly and
// does not perform PATH lookup.
return ["/usr/bin/sudo", "-n", "/usr/local/bin/phoebus-installer", "--gui-bridge"];
}
// Retried until the first successful handshake, so a process-start race can
// never leave the frontend permanently stuck.
Timer {
interval: 400
repeat: true
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) {
const id = root.nextId++;
if (onDone)
root.pending[id] = onDone;
const request = Object.assign({ id: id, op: op }, payload || {});
const line = JSON.stringify(request) + "\n";
if (!proc.running) {
// Quickshell's Process.write() returns void, so it cannot be used
// to detect a failed write. Check the process state instead.
delete root.pending[id];
root.lastError = "backend not running";
if (onDone)
onDone({ ok: false, error: "the installer backend 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("bridge: unparsable line: " + text);
return;
}
if (msg.event === "progress") {
root.event("progress", msg);
return;
}
if (msg.id !== undefined && root.pending[msg.id]) {
const cb = root.pending[msg.id];
delete root.pending[msg.id];
cb(msg);
}
}
// Convenience wrappers.
function refreshHandshake(cb) {
call("handshake", {}, reply => {
if (reply.ok) {
root.handshake = reply.data;
root.handshakeReady();
}
if (cb)
cb(reply);
});
}
function listDisks(cb) {
call("disks", {}, cb);
}
function setConfig(cfg, cb) {
call("set_config", { config: cfg }, reply => {
if (reply.ok)
root.config = cfg;
if (cb)
cb(reply);
});
}
function setSecrets(secrets, cb) {
call("set_secrets", { secrets: secrets }, cb);
}
function validate(cb) {
call("validate", {}, cb);
}
function plan(cb) {
call("plan", {}, cb);
}
function destructivePreview(cb) {
call("destructive_preview", {}, cb);
}
function aiCheck(cb) {
call("ai_check", {}, cb);
}
function aiTest(cb) {
call("ai_test", {}, cb);
}
function approve(eraseText, finalConfirmed, cb) {
call("approve", { erase: eraseText, final_confirm: finalConfirmed }, cb);
}
function startInstall(planDigest, eraseText, cb) {
call("start_install", {
erase: eraseText,
final_confirm: true,
plan_digest: planDigest
}, cb);
}
function logTail(lines, cb) {
call("log_tail", { lines: lines }, cb);
}
Process {
id: proc
command: root.bridgeCommand
stdinEnabled: true
running: false
onStarted: {
root.ready = true;
root.crashed = false;
root.crashMessage = "";
root.availabilityChanged();
}
onExited: exitCode => {
root.ready = false;
root.crashed = true;
root.crashMessage = "The installer backend stopped (exit " + exitCode + ").";
root.availabilityChanged();
}
stdout: SplitParser {
onRead: line => root.handleLine(line)
}
stderr: SplitParser {
onRead: line => {
root.lastError = line;
console.log("phoebus-bridge: " + line);
}
}
}
}