Use ChatGPT Codex for launcher questions
This commit is contained in:
parent
af95242b60
commit
4c6d610263
2 changed files with 40 additions and 94 deletions
|
|
@ -69,45 +69,20 @@
|
|||
# Instant-answer backend for the quickshell launcher on the desktops:
|
||||
# `printf '%s' "question" | ssh mediaserver qs-ask`.
|
||||
#
|
||||
# Runs through Claude Code so it draws on fred's existing subscription —
|
||||
# no API key, no separate billing. The trade is latency: Claude Code boots
|
||||
# a Node process per question, so this takes ~6s against the raw API's
|
||||
# ~1s. The launcher therefore shows its Wikipedia answer first and lets
|
||||
# this one supersede it when it lands.
|
||||
# Runs through Codex CLI using fred's ChatGPT subscription. Sign in on
|
||||
# the server as fred with `codex login` once; no API key is needed.
|
||||
# The launcher shows Wikipedia first and replaces it when this answers.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
# Every flag here is either isolation or trimming the invocation:
|
||||
# --safe-mode no CLAUDE.md, skills, hooks, plugins, MCP or
|
||||
# custom agents. Critical for correctness, not
|
||||
# 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.
|
||||
# Luna at low reasoning effort is enough for a short fact. The CLI runs
|
||||
# read-only, without web search, shell access, user config, exec rules,
|
||||
# or session persistence.
|
||||
#
|
||||
# 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
|
||||
# reads as "keep the Wikipedia answer". Every reason for that silence goes
|
||||
# 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.
|
||||
# call fails or the model doesn't know — silence means "keep Wikipedia".
|
||||
# Reasons go to STDERR and the journal (`journalctl -t qs-ask`).
|
||||
(pkgs.writeShellScriptBin "qs-ask" ''
|
||||
# Every line carries this script's pid, because the launcher fires a
|
||||
# fresh call per debounce and concurrent runs otherwise interleave with
|
||||
|
|
@ -124,45 +99,25 @@
|
|||
[ -z "$q" ] && { log "no question on stdin"; exit 0; }
|
||||
log "ask: $q"
|
||||
|
||||
# Claude Code derives its project state (transcripts, auto-memory) from
|
||||
# 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.
|
||||
# Keep repository instructions and project files out of this invocation.
|
||||
work="$HOME/.cache/qs-ask"
|
||||
${pkgs.coreutils}/bin/mkdir -p "$work" || { log "cannot create $work"; exit 0; }
|
||||
cd "$work" || { log "cannot enter $work"; exit 0; }
|
||||
|
||||
# MAX_THINKING_TOKENS=0 is the single biggest win here, worth ~2.4x.
|
||||
# Measured: Haiku defaults to extended thinking, and it spent 225 output
|
||||
# tokens and 1.9s deliberating before emitting an 80-character answer
|
||||
# about the height of a tower. With thinking off: 43 output tokens,
|
||||
# time-to-first-text 2657ms -> 1122ms, wall clock 5.7s -> 2.4s, same
|
||||
# answer. A one-line lookup has nothing to reason about.
|
||||
#
|
||||
# --name is not cosmetic: without it Claude Code fires a second,
|
||||
# concurrent `source=generate_session_title` request to invent a title
|
||||
# for a session --no-session-persistence then throws away. It costs no
|
||||
# wall time (both requests dispatch ~1ms apart), but it spent 507 input
|
||||
# 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.' \
|
||||
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" | \
|
||||
${pkgs.coreutils}/bin/timeout 30s ${pkgs.codex}/bin/codex exec \
|
||||
--model gpt-5.6-luna \
|
||||
--config model_reasoning_effort=low \
|
||||
--config web_search=disabled \
|
||||
--disable shell_tool \
|
||||
--sandbox read-only \
|
||||
--skip-git-repo-check \
|
||||
--ephemeral \
|
||||
--ignore-user-config \
|
||||
--ignore-rules \
|
||||
- \
|
||||
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:]]*$//')
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue