Use ChatGPT Codex for launcher questions

This commit is contained in:
rope 2026-09-17 17:49:24 +01:00
parent af95242b60
commit 4c6d610263
2 changed files with 40 additions and 94 deletions

View file

@ -69,45 +69,20 @@
# Instant-answer backend for the quickshell launcher on the desktops: # Instant-answer backend for the quickshell launcher on the desktops:
# `printf '%s' "question" | ssh mediaserver qs-ask`. # `printf '%s' "question" | ssh mediaserver qs-ask`.
# #
# Runs through Claude Code so it draws on fred's existing subscription — # Runs through Codex CLI using fred's ChatGPT subscription. Sign in on
# no API key, no separate billing. The trade is latency: Claude Code boots # the server as fred with `codex login` once; no API key is needed.
# a Node process per question, so this takes ~6s against the raw API's # The launcher shows Wikipedia first and replaces it when this answers.
# ~1s. The launcher therefore shows its Wikipedia answer first and lets
# this one supersede it when it lands.
# #
# Reads the question from stdin — passing it as an ssh argv element would # Reads the question from stdin — passing it as an ssh argv element would
# send it through the remote shell for a second round of word splitting. # send it through the remote shell for a second round of word splitting.
# #
# Every flag here is either isolation or trimming the invocation: # Luna at low reasoning effort is enough for a short fact. The CLI runs
# --safe-mode no CLAUDE.md, skills, hooks, plugins, MCP or # read-only, without web search, shell access, user config, exec rules,
# custom agents. Critical for correctness, not # or session persistence.
# just speed: fred's ~/.claude/CLAUDE.md tells
# Claude to answer in caveman mode, which would
# otherwise leak into launcher answers. Auth
# keeps working normally, which is what makes
# the subscription usable here.
# --no-session-persistence writes no transcript, so launcher questions
# never appear in /resume or any project history
# --tools "" no tools at all: nothing to load, and it
# cannot touch the filesystem or network
# --disable-slash-commands no skill resolution
# --strict-mcp-config ignore every MCP config (none supplied)
# --system-prompt replaces the full Claude Code system prompt
# with one line, rather than appending to it
# --permission-mode dontAsk never block waiting on a prompt nobody sees
# --model claude-haiku-4-5 smallest model — a one-line fact needs no more
#
# NOT --bare, which looks like the right flag and is a trap: it skips even
# more, but its auth is "strictly ANTHROPIC_API_KEY or apiKeyHelper (OAuth
# and keychain are never read)" — i.e. it cannot use the subscription.
# #
# STDOUT is the launcher's channel: the answer, or nothing at all if the # STDOUT is the launcher's channel: the answer, or nothing at all if the
# call fails or the model doesn't know — silence is what the launcher # call fails or the model doesn't know — silence means "keep Wikipedia".
# reads as "keep the Wikipedia answer". Every reason for that silence goes # Reasons go to STDERR and the journal (`journalctl -t qs-ask`).
# to STDERR instead, which the launcher discards and a human running
# `qs-ask` by hand can read — and to the journal, since over ssh nobody
# ever sees that STDERR. `journalctl -t qs-ask` is the only way to tell a
# blank card caused by UNKNOWN from one caused by a failed call.
(pkgs.writeShellScriptBin "qs-ask" '' (pkgs.writeShellScriptBin "qs-ask" ''
# Every line carries this script's pid, because the launcher fires a # Every line carries this script's pid, because the launcher fires a
# fresh call per debounce and concurrent runs otherwise interleave with # fresh call per debounce and concurrent runs otherwise interleave with
@ -124,45 +99,25 @@
[ -z "$q" ] && { log "no question on stdin"; exit 0; } [ -z "$q" ] && { log "no question on stdin"; exit 0; }
log "ask: $q" log "ask: $q"
# Claude Code derives its project state (transcripts, auto-memory) from # Keep repository instructions and project files out of this invocation.
# the working directory, so run from a dedicated empty one. Combined with
# --no-session-persistence and --safe-mode, a launcher question leaves no
# trace in the real projects' history or memory.
work="$HOME/.cache/qs-ask" work="$HOME/.cache/qs-ask"
${pkgs.coreutils}/bin/mkdir -p "$work" || { log "cannot create $work"; exit 0; } ${pkgs.coreutils}/bin/mkdir -p "$work" || { log "cannot create $work"; exit 0; }
cd "$work" || { log "cannot enter $work"; exit 0; } cd "$work" || { log "cannot enter $work"; exit 0; }
# MAX_THINKING_TOKENS=0 is the single biggest win here, worth ~2.4x. ans=$(printf 'Answer the question in one or two short sentences, under 240 characters. Lead with the specific fact asked for, including units. The query is often bare keywords rather than a question ("shadow hunter knife classic wow"); treat that as a request for the key facts about that thing. No preamble, caveats, markdown, or follow-up offers. Do not use tools. Reply with exactly UNKNOWN only when you genuinely do not know, or the answer depends on live data you do not have.\n\nQuestion: %s\n' "$q" | \
# Measured: Haiku defaults to extended thinking, and it spent 225 output ${pkgs.coreutils}/bin/timeout 30s ${pkgs.codex}/bin/codex exec \
# tokens and 1.9s deliberating before emitting an 80-character answer --model gpt-5.6-luna \
# about the height of a tower. With thinking off: 43 output tokens, --config model_reasoning_effort=low \
# time-to-first-text 2657ms -> 1122ms, wall clock 5.7s -> 2.4s, same --config web_search=disabled \
# answer. A one-line lookup has nothing to reason about. --disable shell_tool \
# --sandbox read-only \
# --name is not cosmetic: without it Claude Code fires a second, --skip-git-repo-check \
# concurrent `source=generate_session_title` request to invent a title --ephemeral \
# for a session --no-session-persistence then throws away. It costs no --ignore-user-config \
# wall time (both requests dispatch ~1ms apart), but it spent 507 input --ignore-rules \
# tokens against 153 for the real question. Naming the session up front - \
# removes it: one request instead of two, 660 input tokens -> 153.
#
# For the record, two things that are NOT the bottleneck, both measured:
# process startup (0.12s boot + 28ms to fire the request, so keeping a
# warm process saves nothing) and prompt size (184 input tokens).
ans=$(printf '%s' "$q" | ${pkgs.coreutils}/bin/env MAX_THINKING_TOKENS=0 \
${pkgs.claude-code}/bin/claude -p \
--safe-mode \
--no-session-persistence \
--tools "" \
--disable-slash-commands \
--strict-mcp-config \
--permission-mode dontAsk \
--output-format text \
--model claude-haiku-4-5 \
--name qs-ask \
--system-prompt 'Answer the question in one or two short sentences, under 240 characters. Lead with the specific fact asked for, including units. The query is often bare keywords rather than a question ("shadow hunter knife classic wow"); treat that as a request for the key facts about that thing, not as unanswerable. No preamble, no caveats, no markdown, no follow-up offers. Reply with exactly UNKNOWN only when you genuinely do not know, or the answer depends on live data you do not have.' \
2>/dev/null) \ 2>/dev/null) \
|| { log "claude exited non-zero (auth expired? run 'claude' once to log in)"; exit 0; } || { log "codex exited non-zero (check 'codex login status' as fred)"; exit 0; }
ans=$(printf '%s' "$ans" | ${pkgs.coreutils}/bin/tr -d '\r' | ${pkgs.gnused}/bin/sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') ans=$(printf '%s' "$ans" | ${pkgs.coreutils}/bin/tr -d '\r' | ${pkgs.gnused}/bin/sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')

View file

@ -344,19 +344,16 @@ in
'https://en.wikipedia.org/w/api.php?action=query&format=json&generator=search&gsrlimit=3&prop=extracts&exintro&explaintext&redirects=1' '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 # Launcher LLM answers, via the mediaserver. Codex uses fred's ChatGPT
# server ONLY — the desktops just pipe a question over the same SSH # login there; desktops only pipe questions over the existing SSH path.
# path the server monitor already uses, so no laptop ever holds a # The remote end is `qs-ask` in hosts/FredOS-Mediaserver.nix.
# 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` # 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 # re-parses arg through the remote shell, so a question containing
# quotes would break. stdin sidesteps remote quoting entirely. # quotes would break. stdin sidesteps remote quoting entirely.
# #
# Prints nothing when the key is missing, the server is unreachable, the # Prints nothing when login is missing, the server is unreachable, the
# call fails, or the model says it doesn't know — each of those falls # call fails, or the model says it doesn't know; Wikipedia then remains.
# through to the Wikipedia answer.
askFetchScript = pkgs.writeShellScript "ask-fetch" '' askFetchScript = pkgs.writeShellScript "ask-fetch" ''
q="$1" q="$1"
[ -z "$q" ] && exit 0 [ -z "$q" ] && exit 0
@ -367,8 +364,7 @@ in
''; '';
# Launcher weather. Neither of the other two answer paths can serve # Launcher weather. Neither of the other two answer paths can serve
# this: qs-ask runs with no tools at all, so Claude has no live data and # this: qs-ask has no live data, and Wikipedia answers "weather in kristiansand"
# correctly refuses, and Wikipedia answers "weather in kristiansand"
# with the city's founding date. open-meteo needs no key — geocode the # with the city's founding date. open-meteo needs no key — geocode the
# place name, then read current conditions at those coordinates. # place name, then read current conditions at those coordinates.
# #
@ -2025,8 +2021,8 @@ in
// the slow paths count: Wikipedia lands inside the debounce // the slow paths count: Wikipedia lands inside the debounce
// and would just flicker the spinner. // and would just flicker the spinner.
readonly property bool answerPending: askProc.running || wxProc.running readonly property bool answerPending: askProc.running || wxProc.running
readonly property string pendingSource: wxProc.running ? "open-meteo" : "Claude" readonly property string pendingSource: wxProc.running ? "open-meteo" : "ChatGPT"
// Claude answered the current query, so the slower-arriving // ChatGPT answered the current query, so the slower-arriving
// Wikipedia result must not overwrite it. // Wikipedia result must not overwrite it.
property bool askWon: false property bool askWon: false
property var answerCache: ({}) property var answerCache: ({})
@ -2116,10 +2112,8 @@ in
} }
} }
// Claude, tried first. The script is the feature gate: with // ChatGPT, tried alongside Wikipedia. Without a Codex login
// no API key it exits immediately and prints nothing, which // the script prints nothing and Wikipedia remains visible.
// is the same signal as a failed call — so the Wikipedia
// path stays the default with zero configuration.
Process { Process {
id: askProc id: askProc
stdout: StdioCollector { stdout: StdioCollector {
@ -2172,7 +2166,7 @@ in
return; return;
} }
answerQuery = q; answerQuery = q;
// Weather takes the open-meteo path alone: Claude has no // Weather takes the open-meteo path alone: ChatGPT has no
// network to look it up with, Wikipedia answers with the // network to look it up with, Wikipedia answers with the
// city's founding date, and the cache has no notion of // city's founding date, and the cache has no notion of
// age so it must not serve a temperature twice. // age so it must not serve a temperature twice.
@ -2191,10 +2185,7 @@ in
return; return;
} }
// Both at once. Wikipedia fills the card in well under a // Both at once. Wikipedia fills the card in well under a
// second; Claude takes ~6s through Claude Code and // second; ChatGPT supersedes it when Codex finishes.
// supersedes it on arrival. Racing them beats chaining:
// Claude-then-fallback would leave the card blank for
// six seconds on every question.
askWon = false; askWon = false;
killAsk(); killAsk();
askProc.command = [Commands.askFetch, q]; askProc.command = [Commands.askFetch, q];
@ -2218,7 +2209,7 @@ in
fact: text.slice(0, 400), fact: text.slice(0, 400),
lead: "", lead: "",
url: "", // nothing to open url: "", // nothing to open
source: "Claude" source: "ChatGPT"
}; };
cacheAnswer(res); cacheAnswer(res);
// Typing may have moved on while the call was out. // Typing may have moved on while the call was out.
@ -2365,7 +2356,7 @@ in
: "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_")), : "https://en.wikipedia.org/wiki/" + encodeURIComponent(title.replace(/ /g, "_")),
source: "Wikipedia" source: "Wikipedia"
}; };
// Claude already answered this query — don't overwrite // ChatGPT already answered this query — don't overwrite
// the better answer in the cache or on screen. // the better answer in the cache or on screen.
if (askWon) return; if (askWon) return;
cacheAnswer(res); cacheAnswer(res);
@ -2801,7 +2792,7 @@ in
anchors.rightMargin: 14 anchors.rightMargin: 14
spacing: 4 spacing: 4
// Article title — Wikipedia only; a Claude // Article title — Wikipedia only; a ChatGPT
// answer has no page behind it. // answer has no page behind it.
Row { Row {
spacing: 8 spacing: 8
@ -2842,12 +2833,12 @@ in
// Always say who answered — the two sources // Always say who answered — the two sources
// carry very different trust. The spinner // carry very different trust. The spinner
// runs for as long as Claude's ~6s call is // runs for as long as the Codex call is
// out, which is the only cue that a // out, which is the only cue that a
// Wikipedia answer already on screen is // Wikipedia answer already on screen is
// still provisional — and that a card that // still provisional — and that a card that
// never changes was Claude declining, not // never changes was ChatGPT declining or
// Claude being slow. // the call failing.
Row { Row {
spacing: 5 spacing: 5
SIcon { SIcon {