Omarchy bar widget: show the active provider, and switch from it
A Quickshell plugin for the Omarchy shell (v4's bar is Quickshell, not Waybar). The icon is the mode; clicking it opens a panel that switches. State comes from watching health.json rather than polling the CLI, so the widget costs nothing while idle and a switch made in a terminal shows up in the bar on its own. state.json is the fallback, because it exists from install onward where health.json only appears once claude-mode has run. The marks are the providers' real logos, drawn as vector paths through QtQuick.Shapes rather than set as font glyphs - three of the four have no Nerd Font pictograph at all, and paths take the bar's foreground colour and follow the theme. Claude, OpenRouter and LM Studio from simple-icons, Z.AI from lobe-icons; trademarks belong to their owners. Zhipu's mark was rejected for Z.AI: it is a dense lattice that is unreadable below ~40px. Rendering them at 13px took three fixes. A layer rasterises the Shape at its own size and then scales the texture, so a 24px buffer minified to 13 resampled two pixels into one and the Claude burst lost rays; without the layer the scale is a transform on the geometry and rasterisation happens once, at final resolution. CurveRenderer replaces the tessellating default. And the size is forced odd, because a radially symmetric mark puts its vertical and horizontal arms on the centre line, which is a pixel centre at odd sizes and the seam between two pixels at even ones, where each arm splits its coverage and greys out. Measured against a cairo render at the same size, the result is now identical. Sizing is measured rather than guessed. Every stock glyph in this bar paints 11px of ink; the marks fill their box instead of carrying a font's padding, so the box is the smaller number. Each mark also carries an optical scale from two measurements of a 200px render - LM Studio's filled container covers 69% of its box against ~38% for the others, and Z.AI and OpenRouter are wide-but-short marks spanning ~84% of the box height. Choosing a target does not switch immediately. It runs the CLI's own preflight, and if that refuses, the panel names what is missing and offers the fix - a terminal for the hidden key prompt, a re-check once a server is up, or the server form. If sessions are running it lists them by terminal and directory, marks any mid-request, and asks whether to restart them, close them, or leave them. The session action is applied strictly after the write, since restarting first would only bring them back up on the provider just left. The server form edits an LM Studio preset's base URL and whether it needs an API key, reachable from the gear on any LM Studio preset row - per row, because two presets can point at two different machines - and from the failure card. The shipped local default is untouched unless it is changed.
This commit is contained in:
@@ -2,7 +2,10 @@
|
||||
|
||||
Switch Claude Code system-wide between **Anthropic**, **OpenRouter**, **Z.AI**, and a
|
||||
local **LM Studio** server — with named per-tier model presets.
|
||||
Windows / PowerShell 5.1, no external dependencies.
|
||||
|
||||
Windows / PowerShell 5.1 with no external dependencies, and a POSIX port for
|
||||
Linux and macOS that needs only python3 — including a theme-aware TUI and an
|
||||
Omarchy bar widget. See [Linux / Omarchy](#linux--omarchy).
|
||||
|
||||
```
|
||||
claude-mode # interactive menu
|
||||
@@ -110,6 +113,13 @@ claude-mode preset rm <name>
|
||||
claude-mode set-key [ref] store a key (hidden prompt, DPAPI)
|
||||
claude-mode models [filter] models available from the active provider
|
||||
claude-mode doctor verify auth, endpoint, model ids, stray env vars
|
||||
|
||||
claude-mode preflight <mode> [preset] can this mode actually serve? (no switch)
|
||||
claude-mode sessions running sessions, and which are mid-request
|
||||
claude-mode sessions --stop close them (asks first)
|
||||
claude-mode sessions --restart close and reopen each in its own directory
|
||||
claude-mode sessions --dry-run show what either would do, and do nothing
|
||||
claude-mode <mode> --force switch even when preflight says no
|
||||
```
|
||||
|
||||
Omitting the preset uses a **fixed** per-provider default, not "most recently
|
||||
@@ -119,9 +129,8 @@ used" — so `claude-mode openrouter` always means `default`.
|
||||
|
||||
| preset | provider | opus | sonnet | haiku | fable |
|
||||
|---|---|---|---|---|---|
|
||||
| `default` | openrouter | `deepseek/deepseek-v4-flash` | `qwen/qwen3.7-flash` | `openrouter/free` | `z-ai/glm-5.2` |
|
||||
| `cheap` | openrouter | `deepseek/deepseek-v4-pro` | `deepseek/deepseek-v4-flash` | `qwen/qwen3.7-flash` | `openai/gpt-5.6-luna-pro` |
|
||||
| `claude-via-or` | openrouter | `anthropic/claude-opus-5` | `anthropic/claude-sonnet-5` | `anthropic/claude-haiku-4.5` | `anthropic/claude-fable-5` |
|
||||
| `default` | openrouter | `deepseek/deepseek-v4-flash-0731` | `qwen/qwen3.7-flash` | `openrouter/free` | `z-ai/glm-5.2` |
|
||||
| `cheap` | openrouter | `deepseek/deepseek-v4-pro` | `deepseek/deepseek-v4-flash-0731` | `qwen/qwen3.7-flash` | `openai/gpt-5.6-luna-pro` |
|
||||
| `zai` | zai | `glm-5.2` | `glm-5.2` | `glm-4.7` | `glm-5.2` |
|
||||
| `lmstudio` | lmstudio | `kwaipilot_kat-coder-v2.5-dev` (all tiers) | | | |
|
||||
| `lmstudio-qwen` | lmstudio | `qwen3.6-35b-a3b-uncensored-heretic-native-mtp-preserved` (all tiers) | | | |
|
||||
@@ -130,6 +139,10 @@ Presets are plain JSON in `~/.claude-mode/presets/`. A preset declares its
|
||||
`provider`; `claude-mode lmstudio default` is rejected rather than silently
|
||||
pointing a local URL at remote model IDs.
|
||||
|
||||
To route the real Anthropic models through OpenRouter, copy one and repoint the
|
||||
tiers - `claude-mode preset new claude-via-or default`, then `preset set` each
|
||||
tier to `anthropic/claude-opus-5` and friends.
|
||||
|
||||
## Context windows and early auto-compaction
|
||||
|
||||
**Symptom:** switch to a gateway and the session starts auto-compacting almost
|
||||
@@ -151,7 +164,7 @@ CLAUDE_CODE_AUTO_COMPACT_WINDOW = <contextTokens>
|
||||
|
||||
| preset | contextTokens |
|
||||
|---|---|
|
||||
| `default`, `cheap`, `claude-via-or`, `zai` | 1,000,000 |
|
||||
| `default`, `cheap`, `zai` | 1,000,000 |
|
||||
| `lmstudio`, `lmstudio-qwen` | 262,144 |
|
||||
|
||||
`doctor` cross-checks the declared window against each tier's *actual* model
|
||||
@@ -316,6 +329,259 @@ can change.
|
||||
`extraEnv` key (Z.AI's timeouts, LM Studio's attribution header) is removed when
|
||||
you switch away — even though no other preset knows that key exists.
|
||||
|
||||
## Linux / Omarchy
|
||||
|
||||
The `linux/` tree is a POSIX port of the same design: one write to
|
||||
`~/.claude/settings.json`, secrets kept out of it, presets shared verbatim with
|
||||
the Windows build.
|
||||
|
||||
```bash
|
||||
cd claude-code-switcher
|
||||
bash linux/install.sh # --skip-key-prompt to install without storing a key
|
||||
```
|
||||
|
||||
Installs to `~/.claude-mode/`, symlinks `~/.local/bin/claude-mode`, and adds the
|
||||
`claude` wrapper to `~/.bashrc` / `~/.zshrc` between markers.
|
||||
|
||||
Secrets have no DPAPI equivalent here, so the vault picks the best backend
|
||||
available and says which one it chose: macOS Keychain, libsecret, `pass`, or a
|
||||
0600 file that is honestly labelled as unencrypted.
|
||||
|
||||
### The menu follows your desktop theme
|
||||
|
||||
The sixteen ANSI colour slots carry no guarantee about relative brightness, and
|
||||
monochrome themes exploit that. Under Omarchy's Solitude, slot 36 — headings —
|
||||
resolves to `#707070` and slot 31 — `FAIL` — to `#565d60`. Against `#cacccc`
|
||||
body text on a `#101315` ground that is 3.8:1 and 2.8:1 where the body text is
|
||||
11.6:1, so headings render as fine print and an error becomes the quietest
|
||||
thing on screen. Exactly backwards.
|
||||
|
||||
So when Omarchy is present, the palette is derived from the theme it publishes
|
||||
at `~/.local/state/omarchy/current/theme/colors.toml` instead. Every role is
|
||||
measured against the background it will actually be drawn on and lifted toward
|
||||
the foreground when it falls short, which keeps hue where the theme has any and
|
||||
falls back to weight where it does not:
|
||||
|
||||
| role | before (Solitude) | after |
|
||||
|---|---|---|
|
||||
| heading | 3.8:1 | 9.4:1 |
|
||||
| `FAIL` | 2.8:1 | 5.2:1 |
|
||||
| help text | 2.2:1 | 2.2:1 (recessive on purpose, floored) |
|
||||
|
||||
Light themes are handled by the same arithmetic — `mode` in `colors.toml` is
|
||||
authoritative, so `catppuccin-latte` and `flexoki-light` keep their accents
|
||||
rather than washing out.
|
||||
|
||||
Overrides: `CLAUDE_MODE_THEME=/path/to/colors.toml` points it elsewhere,
|
||||
`NO_COLOR` turns it off. Without Omarchy, or on a terminal that cannot do
|
||||
truecolor, it falls back to the ANSI slots with the two roles the slots get
|
||||
wrong corrected — bright red for `FAIL`, bold on headings.
|
||||
|
||||
### `claude-mode health`
|
||||
|
||||
Rewrites `~/.claude-mode/health.json`, the machine-readable mirror of the
|
||||
active configuration: mode, preset, model map, context window, key backend, and
|
||||
the switchable preset catalogue. No key material. A switch and a `status` both
|
||||
refresh it; the command exists for anything that wants to force it.
|
||||
|
||||
## Refusing a switch that would not work
|
||||
|
||||
A switch writes settings.json and is picked up by the *next* `claude` launch, so
|
||||
switching into a mode that cannot serve requests does not fail loudly — it
|
||||
succeeds, and every session started afterwards is broken in a way that points at
|
||||
Claude Code rather than at here.
|
||||
|
||||
LM Studio is the sharp case. Its token is an inline placeholder, so nothing
|
||||
about the switch needs the server to exist; point at a server that is not
|
||||
running and you get a config that looks perfectly healthy and answers nothing.
|
||||
|
||||
So the preconditions are checked before the write, not after:
|
||||
|
||||
| mode | checked | when it fails |
|
||||
|---|---|---|
|
||||
| `anthropic` | nothing to check | — |
|
||||
| `openrouter`, `zai` | a key exists for the preset's `keyRef`, and the helper is executable | `claude-mode set-key <ref>` |
|
||||
| `lmstudio` (anywhere) | the server answers, and accepts the credential the preset would send | see below |
|
||||
| any preset on a local base URL | same probe | start the server |
|
||||
| all | the preset exists and declares the provider being switched to | — |
|
||||
|
||||
The probe distinguishes four outcomes, because their remedies are opposites:
|
||||
|
||||
| result | means | remedy offered |
|
||||
|---|---|---|
|
||||
| `ok` | answered `/api/v0/models` or `/v1/models` | — |
|
||||
| `auth` | the server is up and refused the credential | store or fix the key, or turn auth on for the preset |
|
||||
| `notfound` | something is listening, but the API is not at that path | fix the base URL |
|
||||
| `refused` | nothing answered at all — down, asleep, DNS, TLS, timeout | start the server, or fix the address |
|
||||
|
||||
`claude-mode preflight <mode> [preset]` runs exactly these and prints the
|
||||
verdict as JSON without switching; it is what the bar widget calls before it
|
||||
offers to do anything. `--force` overrides the lot.
|
||||
|
||||
Remote *gateways* are not probed — OpenRouter or Z.AI being briefly unreachable
|
||||
is the network's problem and not worth blocking a config change over, where a
|
||||
missing key never fixes itself. LM Studio is probed wherever it lives, because
|
||||
an instance on a sleeping LAN box is exactly as absent as a loopback port with
|
||||
nothing behind it, and produces the identical silent breakage. Off-machine
|
||||
addresses get a longer timeout, not a pass.
|
||||
|
||||
### LM Studio somewhere other than this machine
|
||||
|
||||
It ships on loopback, but that is a default, not a constraint. A preset is just
|
||||
a base URL and an auth block, so all of these are the same two fields:
|
||||
|
||||
```bash
|
||||
claude-mode preset url lmstudio http://192.168.1.40:1234 # another box on the LAN
|
||||
claude-mode preset url lmstudio https://lms.example.net # through a tunnel or proxy
|
||||
claude-mode preset auth lmstudio key lmstudio # that server wants an API key
|
||||
claude-mode set-key lmstudio # store it (vault, not settings.json)
|
||||
claude-mode preset auth lmstudio none # back to the open-server default
|
||||
```
|
||||
|
||||
`auth none` writes LM Studio's inline placeholder token, which is not a secret
|
||||
and is what an unauthenticated server expects. `auth key` moves it to the vault
|
||||
like every other credential — a real key on a public address is a real key.
|
||||
Nothing here changes the shipped presets unless you ask it to; the local default
|
||||
stays exactly as it was.
|
||||
|
||||
## Sessions still on the old provider
|
||||
|
||||
Claude Code reads all of this once, at startup. A switch therefore does nothing
|
||||
to a session already running: it keeps talking to the old provider on the old
|
||||
key until it is restarted, which is the confusing part — the bar says one thing
|
||||
and the session in front of you is doing another. A session mid-request is worse
|
||||
off, because anthropic mode *deletes* the helper, so an in-flight turn can fail
|
||||
on its next tool call rather than stopping cleanly.
|
||||
|
||||
```
|
||||
claude-mode sessions
|
||||
running sessions (2)
|
||||
562250 pts/4 /home/smoido/Work working (this session - never touched)
|
||||
631644 pts/1 /home/smoido/Projects/api
|
||||
```
|
||||
|
||||
Sessions are found through `/proc/<pid>/exe`, which on Linux resolves to the
|
||||
real `claude` binary — a process-name match would sweep up every shell that
|
||||
merely mentions claude on its command line, including the one this is running
|
||||
from. Two things are then filtered out:
|
||||
|
||||
- **The calling session.** Killing the session that asked for the kill is not a
|
||||
thing anyone means, so it is listed and never signalled.
|
||||
- **Forks of a session.** A busy session spawns children off its own binary,
|
||||
and they inherit the same `exe`. Without excluding anything whose parent is
|
||||
itself claude, the count climbs and falls with how hard the machine is
|
||||
thinking — it read 2, 5, 11 and 40 on the same two sessions before this. A
|
||||
real session's parent is a terminal.
|
||||
|
||||
`working` is a sampled-CPU heuristic — two reads of `utime + stime` 300ms apart
|
||||
— so it is a good guess about which session is mid-turn, not a promise.
|
||||
|
||||
`--stop` sends SIGTERM (never SIGKILL; Claude Code writes out its transcript on
|
||||
the way down). `--restart` stops each session and reopens it — re-running the
|
||||
parent terminal's own command line where there is one, so the same terminal,
|
||||
flags and directory come back, and falling back to a fresh terminal in the
|
||||
session's directory otherwise. Interactive runs confirm first; `--yes` is for
|
||||
callers that have already asked, and `--dry-run` prints the plan and touches
|
||||
nothing.
|
||||
|
||||
## The Omarchy bar widget
|
||||
|
||||
An icon in the Omarchy top bar showing which provider the next `claude` launch
|
||||
will use, and a panel that switches it without a terminal.
|
||||
|
||||
```bash
|
||||
bash omarchy/install.sh
|
||||
```
|
||||
|
||||
Copies the plugin to `~/.config/omarchy/plugins/smoido.claude-mode/` and adds
|
||||
its id to the bar layout in `~/.config/omarchy/shell.json` (backed up first).
|
||||
Both hot-reload, so nothing needs restarting — except after a change to
|
||||
`Modes.js`, which the QML engine caches for the life of the process as a
|
||||
`.pragma library` (`omarchy restart shell`).
|
||||
|
||||
The icon is the mode, and it is the provider's own logo: the Claude burst, the
|
||||
OpenRouter arrow, the Z.AI Z, the LM Studio mark. They are drawn as vector paths
|
||||
with `QtQuick.Shapes` rather than set as font glyphs — three of the four have no
|
||||
Nerd Font pictograph at all, and being paths means they take the bar's
|
||||
foreground colour and follow the theme like everything else. Anything other than
|
||||
Anthropic takes the theme accent, so the bar stays quiet exactly when nothing
|
||||
unusual is configured.
|
||||
|
||||
Marks come from [simple-icons](https://simpleicons.org) (Claude, OpenRouter, LM
|
||||
Studio) and [lobe-icons](https://github.com/lobehub/lobe-icons) (Z.AI);
|
||||
trademarks belong to their owners. Each carries an optical scale factor, because
|
||||
equal nominal size is not equal apparent size — LM Studio's filled container
|
||||
covers 69% of its box against ~38% for the other three, and Z.AI and OpenRouter
|
||||
are wide-but-short marks whose ink spans only ~84% of the box height. The
|
||||
factors in `Modes.js` are the geometric mean of both corrections.
|
||||
|
||||
Three details keep them from looking ragged in a 13px slot, which is what every
|
||||
stock glyph in this bar measures:
|
||||
|
||||
- **No layer.** A layer rasterises the Shape at its own size and then scales the
|
||||
*texture*, so a 24px buffer minified to 13 resamples ~2 pixels into 1. Without
|
||||
one the scale is a transform on the geometry and rasterisation happens once,
|
||||
at final resolution.
|
||||
- **`Shape.CurveRenderer`** (Qt 6.6+) rasterises curves analytically instead of
|
||||
tessellating them into antialiased triangles. Measured against a cairo render
|
||||
of the same mark at the same size, the two now come out identical.
|
||||
- **An odd `iconSize`.** These marks are radially symmetric, so their vertical
|
||||
and horizontal arms sit on the centre line — which is a pixel *centre* at an
|
||||
odd size and the seam between two pixels at an even one, where each arm splits
|
||||
its coverage and greys out. Even values are rounded up.
|
||||
|
||||
- **Click** the icon for the panel: current mode, every mode with the active one
|
||||
ticked, and the model map behind a gateway. Picking a gateway unfolds its
|
||||
presets rather than switching blind; picking a preset starts the switch.
|
||||
|
||||
Choosing a target does not switch immediately — it runs the same two checks the
|
||||
CLI does, and both can stop it:
|
||||
|
||||
1. **Preflight.** If the mode has no key stored, or its server is not answering,
|
||||
the panel says which and offers the fix: *Store the key…* opens a terminal
|
||||
for the hidden prompt (a bar popup has nowhere to host one), *Check again*
|
||||
re-runs the preflight once the server is up, and *Server settings…* opens the
|
||||
form below. The gear on any LM Studio preset row opens the same form without
|
||||
waiting for a failure — per row, because two LM Studio presets can point at
|
||||
two different machines.
|
||||
|
||||
The form holds the base URL, a *Use local default* reset, and a switch for
|
||||
whether that server needs an API key. Saving rewrites the preset and drops
|
||||
straight back into the switch that was blocked.
|
||||
2. **Running sessions.** If any are running, the panel lists them by terminal
|
||||
and directory, marks any that are mid-request, and asks: *Switch and restart*,
|
||||
*Switch and close*, *Switch only*, or *Cancel*. The session action is applied
|
||||
strictly **after** the write — restarting first would only bring them back up
|
||||
on the provider you just left.
|
||||
- **Right-click** switches straight back to Anthropic.
|
||||
- **Middle-click** re-reads state.
|
||||
- **Hover** for mode, preset, and the opus/sonnet mapping.
|
||||
|
||||
State comes from watching `health.json`, not from polling the CLI, so the
|
||||
widget costs nothing while idle and a switch made in a terminal shows up in the
|
||||
bar on its own. A failed switch — a preset whose key was never stored is the
|
||||
common one — surfaces the CLI's own error in the panel rather than looking
|
||||
like a click that did nothing.
|
||||
|
||||
Per-instance settings in the `shell.json` layout entry:
|
||||
|
||||
| key | default | meaning |
|
||||
|---|---|---|
|
||||
| `showLabel` | `false` | show the preset name beside the icon as well |
|
||||
| `iconSize` | `13` | mark size in px; rounded up to odd (see below) |
|
||||
| `root` | `~/.claude-mode` | where claude-mode is installed |
|
||||
|
||||
Placement is `right`, before `omarchy.agents`; override with `CM_BAR_SECTION`
|
||||
and `CM_BAR_BEFORE` when installing. Moving it later is a normal
|
||||
`omarchy bar move smoido.claude-mode --section <section>`.
|
||||
|
||||
Uninstall:
|
||||
|
||||
```bash
|
||||
rm -rf ~/.config/omarchy/plugins/smoido.claude-mode
|
||||
# then remove the {"id": "smoido.claude-mode"} entry from ~/.config/omarchy/shell.json
|
||||
```
|
||||
|
||||
## Uninstall
|
||||
|
||||
```powershell
|
||||
|
||||
Reference in New Issue
Block a user