quickshell: Claude-backed launcher answers via the mediaserver

Adds qs-ask on the mediaserver — the Anthropic key lives there and nowhere
else, so neither desktop holds a credential and there's one place to
rotate it. The launcher pipes the question over the same SSH path the
server monitor already uses; the query goes over stdin because
`ssh host cmd arg` would re-parse arg through the remote shell.

Claude answers first, Wikipedia is the fallback. The script is the
feature gate: with no key it exits silently, which is the same signal as
a failed call or an UNKNOWN reply, so the Wikipedia path stays the
default with zero configuration. The card names whichever answered.

Haiku 4.5, no thinking and no effort (effort errors on Haiku) — a
one-sentence fact needs no reasoning tokens. ~$0.0005 per query.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
rope 2026-08-01 17:14:04 +01:00
parent 5c3d84d0c9
commit 958dc9b333
2 changed files with 140 additions and 6 deletions

View file

@ -247,6 +247,28 @@ in
'https://en.wikipedia.org/w/api.php?action=query&format=json&generator=search&gsrlimit=3&prop=extracts&exintro&explaintext&redirects=1'
'';
# Launcher LLM answers, via the mediaserver. The API key lives on the
# server ONLY — the desktops just pipe a question over the same SSH
# path the server monitor already uses, so no laptop ever holds a
# credential and there's one place to rotate it. The remote end is
# `qs-ask` in hosts/FredOS-Mediaserver.nix.
#
# The query goes over STDIN, not as an argv element: `ssh host cmd arg`
# re-parses arg through the remote shell, so a question containing
# quotes would break. stdin sidesteps remote quoting entirely.
#
# Prints nothing when the key is missing, the server is unreachable, the
# call fails, or the model says it doesn't know — each of those falls
# through to the Wikipedia answer.
askFetchScript = pkgs.writeShellScript "ask-fetch" ''
q="$1"
[ -z "$q" ] && exit 0
printf '%s' "$q" | ${pkgs.openssh}/bin/ssh \
-o BatchMode=yes -o ConnectTimeout=4 \
-o StrictHostKeyChecking=accept-new \
fred@10.0.0.1 qs-ask 2>/dev/null
'';
# cava in raw mode: one newline-terminated frame per tick, bars as
# semicolon-separated 0-100 ints — trivially parsed by SplitParser.
# Drives the media card's spectrum ring. Only spawned while the
@ -755,6 +777,7 @@ in
readonly property string wlCopy: "${pkgs.wl-clipboard}/bin/wl-copy"
readonly property string fd: "${pkgs.fd}/bin/fd"
readonly property string answerFetch: "${answerFetchScript}"
readonly property string askFetch: "${askFetchScript}"
readonly property string xdgOpen: "${pkgs.xdg-utils}/bin/xdg-open"
}
'';
@ -1683,7 +1706,9 @@ in
property string answerTitle: ""
property string answerUrl: ""
property string answerQuery: ""
property string answerSource: ""
property bool answerDropNext: false
property bool askDropNext: false
property var answerCache: ({})
readonly property var answerStop: [
@ -1746,19 +1771,38 @@ in
}
}
// Claude, tried first. The script is the feature gate: with
// no API key it exits immediately and prints nothing, which
// is the same signal as a failed call so the Wikipedia
// path stays the default with zero configuration.
Process {
id: askProc
stdout: StdioCollector {
onStreamFinished: launcherPanel.takeAsk(text)
}
}
function killAnswer() {
if (!answerProc.running) return;
answerDropNext = true;
answerProc.running = false;
}
function killAsk() {
if (!askProc.running) return;
askDropNext = true;
askProc.running = false;
}
function clearAnswer() {
answerTimer.stop();
killAnswer();
killAsk();
answerFact = "";
answerLead = "";
answerTitle = "";
answerUrl = "";
answerSource = "";
}
function runAnswer() {
@ -1773,16 +1817,51 @@ in
showAnswer(hit);
return;
}
// Claude first it answers the questions Wikipedia
// can't. Silence from it means "no key, unreachable, or
// doesn't know", and Wikipedia takes over.
killAsk();
askProc.command = [Commands.askFetch, q];
askProc.running = true;
}
function startWiki() {
killAnswer();
answerProc.command = [Commands.answerFetch, q];
answerProc.command = [Commands.answerFetch, answerQuery];
answerProc.running = true;
}
function takeAsk(raw) {
if (askDropNext) { askDropNext = false; return; }
const text = raw.trim();
if (text === "") {
startWiki();
return;
}
const res = {
title: "", // no article behind it
fact: text.slice(0, 400),
lead: "",
url: "", // nothing to open
source: "Claude"
};
cacheAnswer(res);
// Typing may have moved on while the call was out.
if (answerQuery !== answerKey(searchInput.text)) return;
showAnswer(res);
}
function cacheAnswer(res) {
if (Object.keys(answerCache).length > 40) answerCache = ({});
answerCache[answerQuery] = res;
}
function showAnswer(res) {
answerTitle = res.title;
answerFact = res.fact;
answerLead = res.lead;
answerUrl = res.url;
answerSource = res.source;
}
// Wikipedia hands back a whole intro; the sentence that
@ -1890,10 +1969,10 @@ in
fact: picked.fact,
lead: picked.lead,
url: title === "" ? ""
: "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_"))
: "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_")),
source: "Wikipedia"
};
if (Object.keys(answerCache).length > 40) answerCache = ({});
answerCache[answerQuery] = res;
cacheAnswer(res);
// Typing may have moved on while curl was out.
if (answerQuery !== answerKey(searchInput.text)) return;
showAnswer(res);
@ -2276,8 +2355,11 @@ in
anchors.rightMargin: 14
spacing: 4
// Article title Wikipedia only; a Claude
// answer has no page behind it.
Row {
spacing: 8
visible: launcherPanel.answerTitle !== ""
SIcon {
anchors.verticalCenter: parent.verticalCenter
text: "sparkles"
@ -2311,18 +2393,21 @@ in
wrapMode: Text.WordWrap
}
// Always say who answered the two sources
// carry very different trust.
SText {
text: "Wikipedia"
text: launcherPanel.answerSource
font.pixelSize: 10
color: Theme.base03
}
}
// Only Wikipedia answers have somewhere to go.
MouseArea {
anchors.fill: parent
enabled: launcherPanel.answerUrl !== ""
cursorShape: Qt.PointingHandCursor
onClicked: {
if (launcherPanel.answerUrl === "") return;
Quickshell.execDetached([Commands.xdgOpen, launcherPanel.answerUrl]);
launcherPanel.open = false;
}