@weave-os/router 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +90 -0
- package/README.md +172 -0
- package/bin.js +68 -0
- package/cc-statusline.sh +951 -0
- package/codex-skills/disable-routing/SKILL.md +22 -0
- package/codex-skills/fm/SKILL.md +15 -0
- package/codex-skills/fm/scripts/emit.sh +8 -0
- package/codex-skills/force-model/SKILL.md +15 -0
- package/codex-skills/force-model/scripts/emit.sh +9 -0
- package/codex-skills/rf/SKILL.md +15 -0
- package/codex-skills/rf/scripts/emit.sh +7 -0
- package/codex-skills/router-feedback/SKILL.md +15 -0
- package/codex-skills/router-feedback/scripts/emit.sh +9 -0
- package/codex-skills/router-models/SKILL.md +51 -0
- package/codex-skills/router-off/SKILL.md +22 -0
- package/codex-skills/router-on/SKILL.md +22 -0
- package/codex-skills/router-status/SKILL.md +19 -0
- package/codex-skills/ufm/SKILL.md +14 -0
- package/codex-skills/ufm/scripts/emit.sh +3 -0
- package/codex-skills/unforce-model/SKILL.md +14 -0
- package/codex-skills/unforce-model/scripts/emit.sh +4 -0
- package/codex-status.sh +378 -0
- package/commands/beta.md +5 -0
- package/commands/fm.md +6 -0
- package/commands/force-model.md +6 -0
- package/commands/models.md +46 -0
- package/commands/rf.md +6 -0
- package/commands/router-feedback.md +6 -0
- package/commands/router-models.md +46 -0
- package/commands/router-off.md +10 -0
- package/commands/router-on.md +10 -0
- package/commands/router-session.md +15 -0
- package/commands/router-status.md +10 -0
- package/commands/ufm.md +5 -0
- package/commands/unforce-model.md +5 -0
- package/directives.tsv +13 -0
- package/install.sh +5092 -0
- package/opencode-weave/README.md +78 -0
- package/opencode-weave/package.json +18 -0
- package/opencode-weave/src/index.ts +655 -0
- package/package.json +79 -0
- package/pi-router/README.md +123 -0
- package/pi-router/package.json +25 -0
- package/pi-router/skills/install-lsps/SKILL.md +75 -0
- package/pi-router/skills/lsp-guide/SKILL.md +63 -0
- package/pi-router/src/beta.ts +21 -0
- package/pi-router/src/compaction.ts +188 -0
- package/pi-router/src/config.ts +293 -0
- package/pi-router/src/context-window.ts +12 -0
- package/pi-router/src/dispatch.ts +377 -0
- package/pi-router/src/force-model.ts +126 -0
- package/pi-router/src/index.ts +62 -0
- package/pi-router/src/lsp-broker.ts +255 -0
- package/pi-router/src/lsp-client.ts +435 -0
- package/pi-router/src/lsp-format.ts +230 -0
- package/pi-router/src/lsp-install.ts +215 -0
- package/pi-router/src/lsp-protocol.ts +128 -0
- package/pi-router/src/lsp-servers.ts +361 -0
- package/pi-router/src/lsp.ts +529 -0
- package/pi-router/src/metadata.ts +32 -0
- package/pi-router/src/pricing.generated.ts +91 -0
- package/pi-router/src/provider.ts +64 -0
- package/pi-router/src/routed-model.ts +182 -0
- package/pi-router/src/safety.ts +66 -0
- package/pi-router/src/savings.ts +191 -0
- package/pi-router/src/ui.ts +91 -0
- package/pi-router/src/wooly.ts +408 -0
- package/registry.sh +102 -0
- package/uninstall.sh +788 -0
package/cc-statusline.sh
ADDED
|
@@ -0,0 +1,951 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Claude Code statusline for the Weave Router. CC pipes a JSON blob on stdin
|
|
4
|
+
# whose `transcript_path` points at the JSONL log of the current session and
|
|
5
|
+
# whose `model.display_name` is the user's CC-side model selection. The
|
|
6
|
+
# router rewrites each request's `model` field before forwarding, so
|
|
7
|
+
# Anthropic/OpenAI/Google return `message.model = <routed>` in the SSE
|
|
8
|
+
# stream and CC stores that in the transcript verbatim. Per-turn savings
|
|
9
|
+
# come from comparing each turn's routed cost against what the user's
|
|
10
|
+
# selection would have cost on the same tokens. Works identically for
|
|
11
|
+
# local docker and the managed cloud router — no sidecar, no DB, no auth.
|
|
12
|
+
#
|
|
13
|
+
# Wire up by adding to ~/.claude/settings.json:
|
|
14
|
+
# { "statusLine": { "type": "command", "command": "/abs/path/to/cc-statusline.sh" } }
|
|
15
|
+
#
|
|
16
|
+
# Renders:
|
|
17
|
+
# WEAVE ROUTER — claude-sonnet-4-5 ← claude-opus-4-7 · saved $1.23 · 12.4k in / 3.1k out / 45.2k cached
|
|
18
|
+
#
|
|
19
|
+
# Pricing source of truth: internal/router/catalog. Input/output prices and
|
|
20
|
+
# cache-read multipliers are generated by cmd/genprices. Cache creation remains
|
|
21
|
+
# at 1.25× input pending TTL-aware pricing.
|
|
22
|
+
|
|
23
|
+
set -euo pipefail
|
|
24
|
+
|
|
25
|
+
# ---------- background self-refresh ----------
|
|
26
|
+
#
|
|
27
|
+
# Once every WEAVE_STATUSLINE_UPDATE_INTERVAL_DAYS (default 7), check
|
|
28
|
+
# raw.githubusercontent.com for a newer copy of this script and swap it in
|
|
29
|
+
# atomically. Runs in a forked subshell so the current Claude turn never
|
|
30
|
+
# blocks; the next turn picks up the new version. Applies to both user-scope
|
|
31
|
+
# (~/.weave/cc-statusline.sh) and project-scope (<repo>/.claude/cc-statusline.sh)
|
|
32
|
+
# installs — project teammates rate-limit independently because the stamp
|
|
33
|
+
# lives in their per-user cache dir, and on no-content-change days we skip
|
|
34
|
+
# the mv entirely so the repo working tree stays clean. When upstream does
|
|
35
|
+
# change, the first teammate's commit propagates the new version to the rest.
|
|
36
|
+
# A pricing-table miss also triggers a refresh off-schedule — see
|
|
37
|
+
# weave_refresh_on_price_miss below.
|
|
38
|
+
#
|
|
39
|
+
# Opt out entirely with `export WEAVE_STATUSLINE_UPDATE=0`. Override the
|
|
40
|
+
# source with `WEAVE_STATUSLINE_URL=...`, e.g. for self-hosters who fork.
|
|
41
|
+
#
|
|
42
|
+
# $1 is an optional stamp suffix, so a caller refreshing for a reason other than
|
|
43
|
+
# "the interval elapsed" rate-limits on its own clock rather than sharing the
|
|
44
|
+
# periodic check's budget. $2=1 drops the stamp again if the download fails, for
|
|
45
|
+
# callers recovering from a known-bad state where waiting out the full interval
|
|
46
|
+
# on a transient network error is worse than retrying next turn.
|
|
47
|
+
weave_self_refresh() {
|
|
48
|
+
local stamp_suffix="${1:-}"
|
|
49
|
+
local retry_on_fail="${2:-0}"
|
|
50
|
+
|
|
51
|
+
[ "${WEAVE_STATUSLINE_UPDATE:-1}" = "0" ] && return 0
|
|
52
|
+
command -v curl >/dev/null 2>&1 || return 0
|
|
53
|
+
|
|
54
|
+
local self="${BASH_SOURCE[0]:-$0}"
|
|
55
|
+
[ -f "$self" ] && [ -w "$self" ] || return 0
|
|
56
|
+
|
|
57
|
+
local interval_days="${WEAVE_STATUSLINE_UPDATE_INTERVAL_DAYS:-7}"
|
|
58
|
+
local interval_seconds=$(( interval_days * 86400 ))
|
|
59
|
+
|
|
60
|
+
# Stamp lives in the per-user cache dir, keyed by absolute script path so
|
|
61
|
+
# multiple repos (and the user-scope copy) rate-limit independently and no
|
|
62
|
+
# stray file ever lands inside a repo working tree.
|
|
63
|
+
local cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/weave-router"
|
|
64
|
+
mkdir -p "$cache_dir" 2>/dev/null || return 0
|
|
65
|
+
local script_slug
|
|
66
|
+
script_slug="$(printf '%s' "$self" | tr -c 'A-Za-z0-9._-' '_')"
|
|
67
|
+
local stamp="$cache_dir/checked-at${script_slug}${stamp_suffix}"
|
|
68
|
+
|
|
69
|
+
local now stamp_mtime
|
|
70
|
+
now="$(date +%s 2>/dev/null)" || return 0
|
|
71
|
+
if [ -f "$stamp" ]; then
|
|
72
|
+
# Try GNU `stat -c %Y` first; on macOS (BSD stat) -c isn't recognized
|
|
73
|
+
# and exits non-zero, so we fall through to `stat -f %m`. The reverse
|
|
74
|
+
# order is broken: GNU `stat -f` is `--file-system`, which silently
|
|
75
|
+
# succeeds with multi-line filesystem info instead of failing, leaving
|
|
76
|
+
# $stamp_mtime as garbage and disabling the rate-limit check entirely.
|
|
77
|
+
stamp_mtime="$(stat -c %Y "$stamp" 2>/dev/null || stat -f %m "$stamp" 2>/dev/null)" || stamp_mtime=0
|
|
78
|
+
else
|
|
79
|
+
stamp_mtime=0
|
|
80
|
+
fi
|
|
81
|
+
if [ -n "${stamp_mtime:-}" ] && [ "$stamp_mtime" -gt 0 ] \
|
|
82
|
+
&& [ $(( now - stamp_mtime )) -lt "$interval_seconds" ]; then
|
|
83
|
+
return 0
|
|
84
|
+
fi
|
|
85
|
+
|
|
86
|
+
# Touch the stamp BEFORE forking so concurrent statusline invocations
|
|
87
|
+
# (Claude calls us on every turn) don't all kick off downloads.
|
|
88
|
+
: > "$stamp" 2>/dev/null || return 0
|
|
89
|
+
|
|
90
|
+
local url="${WEAVE_STATUSLINE_URL:-https://raw.githubusercontent.com/weave-os/router/main/install/cc-statusline.sh}"
|
|
91
|
+
# $$ alone is not unique: two calls can run in one invocation (the periodic
|
|
92
|
+
# check and a pricing-miss retry both fire on a cold cache) and would then
|
|
93
|
+
# curl -o into the same path and mv over each other, installing a truncated
|
|
94
|
+
# script that can never self-heal. The stamp suffix is the right key — it is
|
|
95
|
+
# what makes two callers mutually exclusive in the first place, so callers
|
|
96
|
+
# that could overlap necessarily have different suffixes.
|
|
97
|
+
local tmp="${self}.tmp.$$${stamp_suffix}"
|
|
98
|
+
(
|
|
99
|
+
# Detach stdin (CC pipes JSON to us) so curl can't accidentally consume
|
|
100
|
+
# it, and silence all output so nothing leaks into the statusline.
|
|
101
|
+
exec </dev/null
|
|
102
|
+
if curl -fsSL --max-time 15 "$url" -o "$tmp" 2>/dev/null \
|
|
103
|
+
&& [ -s "$tmp" ] \
|
|
104
|
+
&& head -n 1 "$tmp" | grep -q '^#!.*bash' \
|
|
105
|
+
&& [ "$(wc -c < "$tmp")" -ge 1024 ]; then
|
|
106
|
+
# No-op when the download matches what's already on disk — keeps git
|
|
107
|
+
# status clean for project-scope teammates during a routine refresh.
|
|
108
|
+
if cmp -s "$tmp" "$self"; then
|
|
109
|
+
rm -f "$tmp"
|
|
110
|
+
else
|
|
111
|
+
chmod +x "$tmp" 2>/dev/null || true
|
|
112
|
+
mv "$tmp" "$self" 2>/dev/null || rm -f "$tmp"
|
|
113
|
+
fi
|
|
114
|
+
else
|
|
115
|
+
rm -f "$tmp"
|
|
116
|
+
# A download that never landed shouldn't spend the caller's whole
|
|
117
|
+
# interval; drop the stamp so the next turn can try again.
|
|
118
|
+
if [ "$retry_on_fail" = "1" ]; then
|
|
119
|
+
rm -f "$stamp"
|
|
120
|
+
fi
|
|
121
|
+
fi
|
|
122
|
+
) >/dev/null 2>&1 &
|
|
123
|
+
disown 2>/dev/null || true
|
|
124
|
+
return 0
|
|
125
|
+
}
|
|
126
|
+
weave_self_refresh 2>/dev/null || true
|
|
127
|
+
|
|
128
|
+
# ---------- background slash-command refresh ----------
|
|
129
|
+
#
|
|
130
|
+
# The /force-model, /router-* … wrappers under <install>/.claude/commands/ are
|
|
131
|
+
# written once at install time and then never touched again, so a user who
|
|
132
|
+
# doesn't re-run the installer keeps whatever set shipped that day. This script
|
|
133
|
+
# is the only thing Claude Code invokes on every turn, so it doubles as the
|
|
134
|
+
# refresh point for them: same rate limit, same detached fork, same
|
|
135
|
+
# content-diff no-op as the self-refresh above.
|
|
136
|
+
#
|
|
137
|
+
# Never clobber a wrapper the user edited. The only way to tell an edit from a
|
|
138
|
+
# stale copy is to remember what we last downloaded, so each canonical file is
|
|
139
|
+
# cached (unrendered) per install under the user cache dir and a wrapper is
|
|
140
|
+
# replaced only when its bytes still match that baseline. With no baseline yet
|
|
141
|
+
# we can't prove anything, so the first run only seeds the cache and a swap can
|
|
142
|
+
# happen from the next one on. install.sh seeds it too, so fresh installs skip
|
|
143
|
+
# that warm-up round.
|
|
144
|
+
#
|
|
145
|
+
# Never touch a wrapper git tracks either. A project-scope install writes the
|
|
146
|
+
# wrappers into the repo's own .claude/commands/, and unlike cc-statusline.sh
|
|
147
|
+
# the installer does not gitignore them — so an unattended weekly rewrite would
|
|
148
|
+
# surface as unexplained dirty files (and could ride along in someone's commit).
|
|
149
|
+
# Only the installer changes tracked files, and only when a human runs it.
|
|
150
|
+
#
|
|
151
|
+
# Opt out with WEAVE_COMMANDS_UPDATE=0 (WEAVE_STATUSLINE_UPDATE=0 disables this
|
|
152
|
+
# too, along with every other network path here). Override the source with
|
|
153
|
+
# WEAVE_COMMANDS_URL_BASE=..., e.g. for self-hosters who fork.
|
|
154
|
+
|
|
155
|
+
# weave_command_tracked_by_git returns 0 when $1 is a file git tracks. Used to
|
|
156
|
+
# leave repo-committed wrappers alone; no git (or no repo) means untracked.
|
|
157
|
+
weave_command_tracked_by_git() {
|
|
158
|
+
command -v git >/dev/null 2>&1 || return 1
|
|
159
|
+
git -C "$(dirname "$1")" ls-files --error-unmatch -- "$1" >/dev/null 2>&1
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
# weave_installed_command_names lists the wrappers this install may refresh.
|
|
163
|
+
# The statusline ships standalone (no registry.sh beside it), so the installed
|
|
164
|
+
# set — itself written from the registry — is the only source of truth here.
|
|
165
|
+
#
|
|
166
|
+
# Files written before ownership markers existed carry none, so matching on the
|
|
167
|
+
# marker alone would freeze every pre-marker install out of refreshes forever.
|
|
168
|
+
# List every wrapper instead and let the baseline comparison below decide: a
|
|
169
|
+
# file is replaced only when its bytes still match the last canonical copy, so
|
|
170
|
+
# a user-authored command is never touched whether or not it carries a marker.
|
|
171
|
+
weave_installed_command_names() {
|
|
172
|
+
local dir="$1" file
|
|
173
|
+
for file in "$dir"/*.md; do
|
|
174
|
+
[ -f "$file" ] || continue
|
|
175
|
+
printf '%s\n' "$(basename "$file" .md)"
|
|
176
|
+
done
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
# weave_render_command prints $1 with the installer's {{SCOPE}} placeholder
|
|
180
|
+
# replaced by $2, matching how install_slash_commands writes the same file.
|
|
181
|
+
# Trailing newlines are stripped on both sides of every comparison below.
|
|
182
|
+
weave_render_command() {
|
|
183
|
+
local body
|
|
184
|
+
body="$(cat "$1" 2>/dev/null)" || return 1
|
|
185
|
+
printf '%s' "${body//\{\{SCOPE\}\}/$2}"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
weave_sync_commands() {
|
|
189
|
+
[ "${WEAVE_STATUSLINE_UPDATE:-1}" = "0" ] && return 0
|
|
190
|
+
[ "${WEAVE_COMMANDS_UPDATE:-1}" = "0" ] && return 0
|
|
191
|
+
command -v curl >/dev/null 2>&1 || return 0
|
|
192
|
+
|
|
193
|
+
local self="${BASH_SOURCE[0]:-$0}" self_dir cmd_dir
|
|
194
|
+
self_dir="$(cd "$(dirname "$self")" 2>/dev/null && pwd -P)" || return 0
|
|
195
|
+
# User scope installs this script at <base>/.weave/, project and --dir at
|
|
196
|
+
# <base>/.claude/. Claude Code reads commands from <base>/.claude/commands in
|
|
197
|
+
# both layouts.
|
|
198
|
+
case "${self_dir##*/}" in
|
|
199
|
+
.weave) cmd_dir="${self_dir%/*}/.claude/commands" ;;
|
|
200
|
+
.claude) cmd_dir="$self_dir/commands" ;;
|
|
201
|
+
*) return 0 ;;
|
|
202
|
+
esac
|
|
203
|
+
[ -d "$cmd_dir" ] && [ -w "$cmd_dir" ] || return 0
|
|
204
|
+
|
|
205
|
+
local cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/weave-router"
|
|
206
|
+
local dir_slug
|
|
207
|
+
dir_slug="$(printf '%s' "$cmd_dir" | tr -c 'A-Za-z0-9._-' '_')"
|
|
208
|
+
# Baseline is keyed by install, not shared: two installs refresh on their own
|
|
209
|
+
# clocks, and a shared baseline updated by one would make the other's still-
|
|
210
|
+
# canonical wrappers look user-edited and freeze them forever.
|
|
211
|
+
local baseline_dir="$cache_dir/commands${dir_slug}"
|
|
212
|
+
local stamp="$cache_dir/checked-at${dir_slug}.commands"
|
|
213
|
+
mkdir -p "$baseline_dir" 2>/dev/null || return 0
|
|
214
|
+
|
|
215
|
+
local interval_days="${WEAVE_STATUSLINE_UPDATE_INTERVAL_DAYS:-7}"
|
|
216
|
+
local now stamp_mtime
|
|
217
|
+
now="$(date +%s 2>/dev/null)" || return 0
|
|
218
|
+
if [ -f "$stamp" ]; then
|
|
219
|
+
stamp_mtime="$(stat -c %Y "$stamp" 2>/dev/null || stat -f %m "$stamp" 2>/dev/null)" || stamp_mtime=0
|
|
220
|
+
else
|
|
221
|
+
stamp_mtime=0
|
|
222
|
+
fi
|
|
223
|
+
if [ -n "${stamp_mtime:-}" ] && [ "$stamp_mtime" -gt 0 ] \
|
|
224
|
+
&& [ $(( now - stamp_mtime )) -lt $(( interval_days * 86400 )) ]; then
|
|
225
|
+
return 0
|
|
226
|
+
fi
|
|
227
|
+
# Stamp before forking, same as the self-refresh: Claude Code calls us on
|
|
228
|
+
# every turn and concurrent invocations must not all start downloading.
|
|
229
|
+
: > "$stamp" 2>/dev/null || return 0
|
|
230
|
+
|
|
231
|
+
# router-off/on/status bake this install's scope selector into their npx
|
|
232
|
+
# line (upstream carries {{SCOPE}} there). Recover it from the installed copy
|
|
233
|
+
# so a project or --dir install keeps toggling its own config; when it can't
|
|
234
|
+
# be recovered those three are skipped rather than rewritten to point at the
|
|
235
|
+
# user-scope install.
|
|
236
|
+
local scope_args="" scope_known="false" off="$cmd_dir/router-off.md"
|
|
237
|
+
if [ -f "$off" ] && grep -Eq '^`npx @(workweave/router|weave-os/router) off --claude.*`$' "$off" 2>/dev/null; then
|
|
238
|
+
scope_known="true"
|
|
239
|
+
scope_args="$(sed -En 's#^`npx @(workweave/router|weave-os/router) off --claude(.*)`$#\2#p' "$off" | head -n 1)"
|
|
240
|
+
fi
|
|
241
|
+
|
|
242
|
+
local url_base="${WEAVE_COMMANDS_URL_BASE:-https://raw.githubusercontent.com/weave-os/router/main/install/commands}"
|
|
243
|
+
local name installed raw prev tmp new_body prev_body installed_body
|
|
244
|
+
(
|
|
245
|
+
# Detach stdin (CC pipes JSON to us) so curl can't consume it, and silence
|
|
246
|
+
# everything so no output leaks into the statusline.
|
|
247
|
+
exec </dev/null
|
|
248
|
+
while IFS= read -r name; do
|
|
249
|
+
installed="$cmd_dir/$name.md"
|
|
250
|
+
# Only ever refresh a wrapper that is already installed: a missing one
|
|
251
|
+
# was uninstalled or deliberately deleted, and resurrecting it would be
|
|
252
|
+
# a surprise. A symlink is user-owned; leave it alone. A git-tracked
|
|
253
|
+
# wrapper belongs to the repo — rewriting it would dirty a working tree
|
|
254
|
+
# nobody asked us to touch.
|
|
255
|
+
[ -f "$installed" ] || continue
|
|
256
|
+
[ -L "$installed" ] && continue
|
|
257
|
+
weave_command_tracked_by_git "$installed" && continue
|
|
258
|
+
|
|
259
|
+
raw="$baseline_dir/$name.md.tmp.$$"
|
|
260
|
+
curl -fsSL --max-time 15 "$url_base/$name.md" -o "$raw" 2>/dev/null || { rm -f "$raw"; continue; }
|
|
261
|
+
# Shape check: every wrapper opens with YAML front matter, so a 404 page
|
|
262
|
+
# or a truncated body can never be installed as a command.
|
|
263
|
+
if [ ! -s "$raw" ] || [ "$(head -n 1 "$raw")" != "---" ]; then
|
|
264
|
+
rm -f "$raw"
|
|
265
|
+
continue
|
|
266
|
+
fi
|
|
267
|
+
|
|
268
|
+
prev="$baseline_dir/$name.md"
|
|
269
|
+
if grep -q '{{SCOPE}}' "$raw" && [ "$scope_known" != "true" ]; then
|
|
270
|
+
mv "$raw" "$prev" 2>/dev/null || rm -f "$raw"
|
|
271
|
+
continue
|
|
272
|
+
fi
|
|
273
|
+
|
|
274
|
+
if [ -f "$prev" ]; then
|
|
275
|
+
new_body="$(weave_render_command "$raw" "$scope_args")"
|
|
276
|
+
prev_body="$(weave_render_command "$prev" "$scope_args")"
|
|
277
|
+
installed_body="$(cat "$installed" 2>/dev/null | sed '/^<!-- weave-router managed command: .* -->$/d')" || installed_body=""
|
|
278
|
+
if [ "$prev_body" = "$installed_body" ] && [ "$new_body" != "$installed_body" ]; then
|
|
279
|
+
tmp="$installed.tmp.$$"
|
|
280
|
+
if printf '%s\n<!-- weave-router managed command: %s -->' "$new_body" "$name" >"$tmp" 2>/dev/null; then
|
|
281
|
+
mv "$tmp" "$installed" 2>/dev/null || rm -f "$tmp"
|
|
282
|
+
else
|
|
283
|
+
rm -f "$tmp"
|
|
284
|
+
fi
|
|
285
|
+
fi
|
|
286
|
+
fi
|
|
287
|
+
mv "$raw" "$prev" 2>/dev/null || rm -f "$raw"
|
|
288
|
+
done <<EOF
|
|
289
|
+
$(weave_installed_command_names "$cmd_dir")
|
|
290
|
+
EOF
|
|
291
|
+
) >/dev/null 2>&1 &
|
|
292
|
+
disown 2>/dev/null || true
|
|
293
|
+
return 0
|
|
294
|
+
}
|
|
295
|
+
weave_sync_commands 2>/dev/null || true
|
|
296
|
+
|
|
297
|
+
# ---------- org "hide terminal surfaces" gate ----------
|
|
298
|
+
#
|
|
299
|
+
# When the org has hidden the router's terminal surfaces, the statusline
|
|
300
|
+
# renders nothing: this prints no output and exits 0, leaving the slot blank.
|
|
301
|
+
# The setting comes from GET /v1/display-settings with the install's own
|
|
302
|
+
# router key, but the foreground path NEVER touches the network: it decides
|
|
303
|
+
# solely from a per-install cache file (TTL WEAVE_DISPLAY_SETTINGS_TTL_SECONDS,
|
|
304
|
+
# default 1h), and a missing or stale cache fails open — the statusline
|
|
305
|
+
# renders normally. When the cache is missing or stale a detached background
|
|
306
|
+
# refresh re-fetches the setting and rewrites the cache atomically, so the
|
|
307
|
+
# next turn picks up the fresh value; a refresh failure simply leaves the
|
|
308
|
+
# cache stale, which keeps failing open rather than pinning the gate closed.
|
|
309
|
+
#
|
|
310
|
+
# Claude Code runs the statusline every turn, so several invocations can see a
|
|
311
|
+
# stale cache at once. Letting each fetch independently is not safe: the
|
|
312
|
+
# responses can land out of order, and the loser's mv would replace a newer
|
|
313
|
+
# setting with an older one AND stamp it fresh, pinning the gate on a stale
|
|
314
|
+
# value for a full TTL. Refreshes are therefore serialized per cache key on a
|
|
315
|
+
# mkdir mutex (atomic everywhere; flock is absent on macOS), held across both
|
|
316
|
+
# the fetch and the write. A refresh that finds the mutex held exits rather
|
|
317
|
+
# than queueing — the in-flight one is at least as fresh as anything it would
|
|
318
|
+
# fetch, so waiting only to overwrite it is the bug. The one path that is not
|
|
319
|
+
# strictly exclusive is reclaiming a lock whose holder died; see below.
|
|
320
|
+
weave_hidden_gate() {
|
|
321
|
+
command -v curl >/dev/null 2>&1 || return 1
|
|
322
|
+
command -v jq >/dev/null 2>&1 || return 1
|
|
323
|
+
|
|
324
|
+
local cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/weave-router"
|
|
325
|
+
mkdir -p "$cache_dir" 2>/dev/null || return 1
|
|
326
|
+
local self="${BASH_SOURCE[0]:-$0}"
|
|
327
|
+
local script_slug
|
|
328
|
+
script_slug="$(printf '%s' "$self" | tr -c 'A-Za-z0-9._-' '_')"
|
|
329
|
+
local cache="$cache_dir/display-settings${script_slug}"
|
|
330
|
+
local ttl="${WEAVE_DISPLAY_SETTINGS_TTL_SECONDS:-3600}"
|
|
331
|
+
|
|
332
|
+
local now mtime fresh="false"
|
|
333
|
+
now="$(date +%s 2>/dev/null)" || now=0
|
|
334
|
+
if [ -f "$cache" ]; then
|
|
335
|
+
mtime="$(stat -c %Y "$cache" 2>/dev/null || stat -f %m "$cache" 2>/dev/null)" || mtime=0
|
|
336
|
+
if [ -n "${mtime:-}" ] && [ "$mtime" -gt 0 ] && [ $(( now - mtime )) -lt "$ttl" ]; then
|
|
337
|
+
fresh="true"
|
|
338
|
+
fi
|
|
339
|
+
fi
|
|
340
|
+
|
|
341
|
+
# Foreground decision: only a fresh cache hides the surfaces. Anything else
|
|
342
|
+
# (no cache, stale cache, unreadable cache) renders normally so a slow or
|
|
343
|
+
# unreachable router can never stall a turn or wedge the statusline blank.
|
|
344
|
+
if [ "$fresh" = "true" ]; then
|
|
345
|
+
[ "$(cat "$cache" 2>/dev/null)" = "1" ]
|
|
346
|
+
return
|
|
347
|
+
fi
|
|
348
|
+
|
|
349
|
+
# Background refresh for the next invocation. Resolve the router base URL
|
|
350
|
+
# and key inside the subshell from the Claude Code settings the installer
|
|
351
|
+
# wrote: project/--dir installs put both under <base>/.claude alongside
|
|
352
|
+
# this script (key in settings.local.json), while a user-scope install
|
|
353
|
+
# lives under ~/.weave and reads ~/.claude. Resolve relative to the
|
|
354
|
+
# script's own location, falling back to user scope, so a project install
|
|
355
|
+
# never reads (or leaks) the user-scope key. ANTHROPIC_BASE_URL and
|
|
356
|
+
# WEAVE_ROUTER_BASE_URL may also be set in the environment. A file:// base
|
|
357
|
+
# URL is the offline/test seam: curl reads it as the response body
|
|
358
|
+
# directly, so the endpoint path is meaningless for it; real router URLs
|
|
359
|
+
# (https) get /v1/display-settings appended.
|
|
360
|
+
(
|
|
361
|
+
exec </dev/null
|
|
362
|
+
|
|
363
|
+
# Take the per-cache-key mutex, or bail. mkdir is the portable atomic
|
|
364
|
+
# test-and-set. A crashed holder would otherwise block refreshes forever,
|
|
365
|
+
# so a lock older than the fetch timeout is treated as abandoned and
|
|
366
|
+
# reclaimed. Releasing from a trap covers every exit path below.
|
|
367
|
+
lock="$cache.lock"
|
|
368
|
+
if ! mkdir "$lock" 2>/dev/null; then
|
|
369
|
+
lock_mtime="$(stat -c %Y "$lock" 2>/dev/null || stat -f %m "$lock" 2>/dev/null)" || lock_mtime=0
|
|
370
|
+
lock_now="$(date +%s 2>/dev/null)" || lock_now=0
|
|
371
|
+
if [ "${lock_mtime:-0}" -le 0 ] || [ $(( lock_now - lock_mtime )) -le 30 ]; then
|
|
372
|
+
exit 0
|
|
373
|
+
fi
|
|
374
|
+
# Reclaiming an abandoned lock must not be delete-then-recreate. With
|
|
375
|
+
# `rm -rf` + `mkdir`, refreshers that all see the same stale lock each
|
|
376
|
+
# delete the next one's freshly created directory, so many end up holding
|
|
377
|
+
# it at once (measured at 50 claimants: 4-11 concurrent holders) and their
|
|
378
|
+
# writes can land out of order. Renaming is atomic, so only one racer can
|
|
379
|
+
# move a given directory aside and the losers exit instead of clobbering
|
|
380
|
+
# the winner (same measurement: 1-2). It is not a perfect mutex — a
|
|
381
|
+
# straggler can still reclaim the new lock a winner just created, since
|
|
382
|
+
# nothing distinguishes it from the stale one — but real refreshes arrive
|
|
383
|
+
# one per turn rather than 50 at once, and the staleness threshold below
|
|
384
|
+
# is what bounds the rest.
|
|
385
|
+
dead="$lock.dead.$$"
|
|
386
|
+
mv "$lock" "$dead" 2>/dev/null || exit 0
|
|
387
|
+
rm -rf "$dead" 2>/dev/null
|
|
388
|
+
mkdir "$lock" 2>/dev/null || exit 0
|
|
389
|
+
fi
|
|
390
|
+
trap 'rmdir "$lock" 2>/dev/null' EXIT
|
|
391
|
+
|
|
392
|
+
self_dir="$(cd "$(dirname "$self")" 2>/dev/null && pwd)"
|
|
393
|
+
settings_base="$HOME"
|
|
394
|
+
case "$self_dir" in
|
|
395
|
+
*/.claude) settings_base="${self_dir%/.claude}" ;;
|
|
396
|
+
esac
|
|
397
|
+
settings="$settings_base/.claude/settings.json"
|
|
398
|
+
local_settings="$settings_base/.claude/settings.local.json"
|
|
399
|
+
base_url="${WEAVE_ROUTER_BASE_URL:-${ANTHROPIC_BASE_URL:-}}"
|
|
400
|
+
key="${WEAVE_ROUTER_KEY:-}"
|
|
401
|
+
if [ -z "$key" ] && [ -f "$settings" ]; then
|
|
402
|
+
key="$(jq -r '.env.ANTHROPIC_CUSTOM_HEADERS // "" | split("\n")[] | select(startswith("X-Weave-Router-Key:")) | sub("^X-Weave-Router-Key:[[:space:]]*";"")' "$settings" 2>/dev/null | head -n1)"
|
|
403
|
+
fi
|
|
404
|
+
if [ -z "$key" ] && [ -f "$local_settings" ]; then
|
|
405
|
+
key="$(jq -r '.env.ANTHROPIC_CUSTOM_HEADERS // "" | split("\n")[] | select(startswith("X-Weave-Router-Key:")) | sub("^X-Weave-Router-Key:[[:space:]]*";"")' "$local_settings" 2>/dev/null | head -n1)"
|
|
406
|
+
fi
|
|
407
|
+
if [ -z "$base_url" ] && [ -f "$settings" ]; then
|
|
408
|
+
base_url="$(jq -r '.env.ANTHROPIC_BASE_URL // empty' "$settings" 2>/dev/null)"
|
|
409
|
+
fi
|
|
410
|
+
[ -n "$base_url" ] && [ -n "$key" ] || exit 0
|
|
411
|
+
url="${base_url%/}"
|
|
412
|
+
case "$url" in
|
|
413
|
+
file://*) ;;
|
|
414
|
+
*) url="$url/v1/display-settings" ;;
|
|
415
|
+
esac
|
|
416
|
+
body="$(curl -fsS --max-time 5 -H "X-Weave-Router-Key: $key" "$url" 2>/dev/null)" || exit 0
|
|
417
|
+
hidden="$(printf '%s' "$body" | jq -r '.hide_terminal_surfaces // false' 2>/dev/null)"
|
|
418
|
+
tmp="$cache.tmp.$$"
|
|
419
|
+
if [ "$hidden" = "true" ]; then
|
|
420
|
+
printf '1' >"$tmp" 2>/dev/null && mv "$tmp" "$cache" 2>/dev/null
|
|
421
|
+
else
|
|
422
|
+
printf '0' >"$tmp" 2>/dev/null && mv "$tmp" "$cache" 2>/dev/null
|
|
423
|
+
fi
|
|
424
|
+
rm -f "$tmp" 2>/dev/null
|
|
425
|
+
) >/dev/null 2>&1 &
|
|
426
|
+
disown 2>/dev/null || true
|
|
427
|
+
return 1
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if weave_hidden_gate </dev/null; then
|
|
431
|
+
exit 0
|
|
432
|
+
fi
|
|
433
|
+
|
|
434
|
+
input="$(cat)"
|
|
435
|
+
transcript_path="$(printf '%s' "$input" | jq -r '.transcript_path // empty')"
|
|
436
|
+
# Prefer model.id over display_name: pricing keys + the routed model id in
|
|
437
|
+
# the transcript are canonical ids (e.g. claude-opus-4-7), while display_name
|
|
438
|
+
# is a human label ("Opus 4.7 (1M context)") that won't hit the pricing table,
|
|
439
|
+
# zeroing out savings. id passes through normalize_model cleanly.
|
|
440
|
+
selected_display="$(printf '%s' "$input" | jq -r '.model.id // .model.display_name // "?"')"
|
|
441
|
+
|
|
442
|
+
# Normalize a model id to a pricing-table key. CC + the decisions log carry
|
|
443
|
+
# two flavors of annotation we don't want in the lookup:
|
|
444
|
+
# * date suffix: claude-opus-4-7-20260101 → claude-opus-4-7
|
|
445
|
+
# * variant tag: claude-opus-4-7[1m] → claude-opus-4-7
|
|
446
|
+
# The 1M-context variant prices ~2× base for prompts >200k tokens, but for
|
|
447
|
+
# the "saved $X vs your selection" UX the base rate is the right comparison
|
|
448
|
+
# — we're measuring the model swap, not the context tier. Used below on the
|
|
449
|
+
# routed and requested model ids from the decisions log / transcript.
|
|
450
|
+
normalize_model() {
|
|
451
|
+
printf '%s' "$1" | sed -E 's/\[[^]]*\]$//; s/-[0-9]{8}$//'
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
# USD per 1k tokens. Generated from internal/observability/otel/pricing.go
|
|
455
|
+
# (USD/1M there, ÷1000 here) by cmd/genprices. Do not hand-edit — run
|
|
456
|
+
# `make generate` after updating pricing.go.
|
|
457
|
+
# BEGIN_GENERATED_PRICES
|
|
458
|
+
prices='{
|
|
459
|
+
"input": {
|
|
460
|
+
"claude-fable-5": 0.01,
|
|
461
|
+
"claude-fable-5-1": 0.01,
|
|
462
|
+
"claude-haiku-4-5": 0.001,
|
|
463
|
+
"claude-opus-4-0": 0.015,
|
|
464
|
+
"claude-opus-4-1": 0.015,
|
|
465
|
+
"claude-opus-4-5": 0.005,
|
|
466
|
+
"claude-opus-4-6": 0.005,
|
|
467
|
+
"claude-opus-4-7": 0.005,
|
|
468
|
+
"claude-opus-4-8": 0.005,
|
|
469
|
+
"claude-opus-5": 0.005,
|
|
470
|
+
"claude-sonnet-4-5": 0.003,
|
|
471
|
+
"claude-sonnet-4-6": 0.003,
|
|
472
|
+
"claude-sonnet-5": 0.003,
|
|
473
|
+
"deepseek/deepseek-v4-flash": 0.0001134,
|
|
474
|
+
"deepseek/deepseek-v4-pro": 0.00174,
|
|
475
|
+
"deepseek/deepseek-v4-pro-0813": 0.00174,
|
|
476
|
+
"gemini-2.0-flash": 0.0001,
|
|
477
|
+
"gemini-2.0-flash-lite": 0.000075,
|
|
478
|
+
"gemini-2.5-flash": 0.0003,
|
|
479
|
+
"gemini-2.5-flash-lite": 0.0001,
|
|
480
|
+
"gemini-2.5-pro": 0.00125,
|
|
481
|
+
"gemini-3-flash-preview": 0.0005,
|
|
482
|
+
"gemini-3-pro-preview": 0.002,
|
|
483
|
+
"gemini-3.1-flash-lite-preview": 0.0001,
|
|
484
|
+
"gemini-3.1-pro-preview": 0.002,
|
|
485
|
+
"gemini-3.5-flash": 0.0015,
|
|
486
|
+
"gemini-3.5-flash-lite": 0.0003,
|
|
487
|
+
"gemini-3.6-flash": 0.0015,
|
|
488
|
+
"gemini-3.7-flash": 0.0015,
|
|
489
|
+
"gemini-3.8-flash": 0.0015,
|
|
490
|
+
"google/gemma-4-26b-a4b-it": 0.00015,
|
|
491
|
+
"gpt-4.1": 0.002,
|
|
492
|
+
"gpt-4.1-mini": 0.0004,
|
|
493
|
+
"gpt-4.1-nano": 0.0001,
|
|
494
|
+
"gpt-4o": 0.0025,
|
|
495
|
+
"gpt-4o-mini": 0.00015,
|
|
496
|
+
"gpt-5": 0.0025,
|
|
497
|
+
"gpt-5-chat": 0.0025,
|
|
498
|
+
"gpt-5-mini": 0.0005,
|
|
499
|
+
"gpt-5-nano": 0.0001,
|
|
500
|
+
"gpt-5.4": 0.0025,
|
|
501
|
+
"gpt-5.4-mini": 0.00075,
|
|
502
|
+
"gpt-5.4-nano": 0.0002,
|
|
503
|
+
"gpt-5.4-pro": 0.03,
|
|
504
|
+
"gpt-5.5": 0.005,
|
|
505
|
+
"gpt-5.5-mini": 0.0005,
|
|
506
|
+
"gpt-5.5-nano": 0.00015,
|
|
507
|
+
"gpt-5.5-pro": 0.03,
|
|
508
|
+
"gpt-5.6-luna": 0.001,
|
|
509
|
+
"gpt-5.6-luna-pro": 0.001,
|
|
510
|
+
"gpt-5.6-sol": 0.005,
|
|
511
|
+
"gpt-5.6-sol-pro": 0.005,
|
|
512
|
+
"gpt-5.6-terra": 0.0025,
|
|
513
|
+
"grok-4.5": 0.002,
|
|
514
|
+
"grok-4.6": 0.002,
|
|
515
|
+
"minimax/minimax-m2.7": 0.0003,
|
|
516
|
+
"minimax/minimax-m3": 0.0003,
|
|
517
|
+
"mistralai/mistral-small-2603": 0.0002,
|
|
518
|
+
"moonshotai/kimi-k2.5": 0.0006,
|
|
519
|
+
"moonshotai/kimi-k2.6": 0.00095,
|
|
520
|
+
"moonshotai/kimi-k2.7": 0.00095,
|
|
521
|
+
"moonshotai/kimi-k3": 0.003,
|
|
522
|
+
"muse-spark-1.3": 0.00125,
|
|
523
|
+
"qwen/qwen3-235b-a22b-2507": 0.0002266,
|
|
524
|
+
"qwen/qwen3-30b-a3b-instruct-2507": 0.00015,
|
|
525
|
+
"qwen/qwen3-coder": 0.0009,
|
|
526
|
+
"qwen/qwen3-coder-next": 0.0005,
|
|
527
|
+
"qwen/qwen3-next-80b-a3b-instruct": 0.00015,
|
|
528
|
+
"qwen/qwen3.5-flash-02-23": 0.00005,
|
|
529
|
+
"qwen/qwen3.6-35b-a3b": 0.00015,
|
|
530
|
+
"qwen/qwen3.7-plus": 0.0004,
|
|
531
|
+
"qwen/qwen3.8-max": 0.002,
|
|
532
|
+
"xiaomi/mimo-v2.5-pro": 0.001,
|
|
533
|
+
"z-ai/glm-5": 0.001,
|
|
534
|
+
"z-ai/glm-5.1": 0.0014,
|
|
535
|
+
"z-ai/glm-5.2": 0.0014,
|
|
536
|
+
"z-ai/glm-5.3": 0.0014,
|
|
537
|
+
"z-ai/glm-5.3-flash": 0.00015
|
|
538
|
+
},
|
|
539
|
+
"output": {
|
|
540
|
+
"claude-fable-5": 0.05,
|
|
541
|
+
"claude-fable-5-1": 0.05,
|
|
542
|
+
"claude-haiku-4-5": 0.005,
|
|
543
|
+
"claude-opus-4-0": 0.075,
|
|
544
|
+
"claude-opus-4-1": 0.075,
|
|
545
|
+
"claude-opus-4-5": 0.025,
|
|
546
|
+
"claude-opus-4-6": 0.025,
|
|
547
|
+
"claude-opus-4-7": 0.025,
|
|
548
|
+
"claude-opus-4-8": 0.025,
|
|
549
|
+
"claude-opus-5": 0.025,
|
|
550
|
+
"claude-sonnet-4-5": 0.015,
|
|
551
|
+
"claude-sonnet-4-6": 0.015,
|
|
552
|
+
"claude-sonnet-5": 0.015,
|
|
553
|
+
"deepseek/deepseek-v4-flash": 0.0002791,
|
|
554
|
+
"deepseek/deepseek-v4-pro": 0.00348,
|
|
555
|
+
"deepseek/deepseek-v4-pro-0813": 0.00348,
|
|
556
|
+
"gemini-2.0-flash": 0.0004,
|
|
557
|
+
"gemini-2.0-flash-lite": 0.0003,
|
|
558
|
+
"gemini-2.5-flash": 0.0012,
|
|
559
|
+
"gemini-2.5-flash-lite": 0.0004,
|
|
560
|
+
"gemini-2.5-pro": 0.005,
|
|
561
|
+
"gemini-3-flash-preview": 0.002,
|
|
562
|
+
"gemini-3-pro-preview": 0.008,
|
|
563
|
+
"gemini-3.1-flash-lite-preview": 0.0004,
|
|
564
|
+
"gemini-3.1-pro-preview": 0.008,
|
|
565
|
+
"gemini-3.5-flash": 0.009,
|
|
566
|
+
"gemini-3.5-flash-lite": 0.0025,
|
|
567
|
+
"gemini-3.6-flash": 0.0075,
|
|
568
|
+
"gemini-3.7-flash": 0.0075,
|
|
569
|
+
"gemini-3.8-flash": 0.0075,
|
|
570
|
+
"google/gemma-4-26b-a4b-it": 0.0006,
|
|
571
|
+
"gpt-4.1": 0.008,
|
|
572
|
+
"gpt-4.1-mini": 0.0016,
|
|
573
|
+
"gpt-4.1-nano": 0.0004,
|
|
574
|
+
"gpt-4o": 0.01,
|
|
575
|
+
"gpt-4o-mini": 0.0006,
|
|
576
|
+
"gpt-5": 0.01,
|
|
577
|
+
"gpt-5-chat": 0.01,
|
|
578
|
+
"gpt-5-mini": 0.002,
|
|
579
|
+
"gpt-5-nano": 0.0004,
|
|
580
|
+
"gpt-5.4": 0.015,
|
|
581
|
+
"gpt-5.4-mini": 0.0045,
|
|
582
|
+
"gpt-5.4-nano": 0.00125,
|
|
583
|
+
"gpt-5.4-pro": 0.18,
|
|
584
|
+
"gpt-5.5": 0.03,
|
|
585
|
+
"gpt-5.5-mini": 0.0025,
|
|
586
|
+
"gpt-5.5-nano": 0.0006,
|
|
587
|
+
"gpt-5.5-pro": 0.18,
|
|
588
|
+
"gpt-5.6-luna": 0.006,
|
|
589
|
+
"gpt-5.6-luna-pro": 0.006,
|
|
590
|
+
"gpt-5.6-sol": 0.03,
|
|
591
|
+
"gpt-5.6-sol-pro": 0.03,
|
|
592
|
+
"gpt-5.6-terra": 0.015,
|
|
593
|
+
"grok-4.5": 0.006,
|
|
594
|
+
"grok-4.6": 0.006,
|
|
595
|
+
"minimax/minimax-m2.7": 0.0012,
|
|
596
|
+
"minimax/minimax-m3": 0.0012,
|
|
597
|
+
"mistralai/mistral-small-2603": 0.0006,
|
|
598
|
+
"moonshotai/kimi-k2.5": 0.003,
|
|
599
|
+
"moonshotai/kimi-k2.6": 0.004,
|
|
600
|
+
"moonshotai/kimi-k2.7": 0.004,
|
|
601
|
+
"moonshotai/kimi-k3": 0.015,
|
|
602
|
+
"muse-spark-1.3": 0.00425,
|
|
603
|
+
"qwen/qwen3-235b-a22b-2507": 0.0009064,
|
|
604
|
+
"qwen/qwen3-30b-a3b-instruct-2507": 0.0006,
|
|
605
|
+
"qwen/qwen3-coder": 0.0027,
|
|
606
|
+
"qwen/qwen3-coder-next": 0.0012,
|
|
607
|
+
"qwen/qwen3-next-80b-a3b-instruct": 0.0012,
|
|
608
|
+
"qwen/qwen3.5-flash-02-23": 0.00015,
|
|
609
|
+
"qwen/qwen3.6-35b-a3b": 0.001,
|
|
610
|
+
"qwen/qwen3.7-plus": 0.0016,
|
|
611
|
+
"qwen/qwen3.8-max": 0.006,
|
|
612
|
+
"xiaomi/mimo-v2.5-pro": 0.003,
|
|
613
|
+
"z-ai/glm-5": 0.0032,
|
|
614
|
+
"z-ai/glm-5.1": 0.0044,
|
|
615
|
+
"z-ai/glm-5.2": 0.0044,
|
|
616
|
+
"z-ai/glm-5.3": 0.0044,
|
|
617
|
+
"z-ai/glm-5.3-flash": 0.0005
|
|
618
|
+
},
|
|
619
|
+
"cache_read": {
|
|
620
|
+
"claude-fable-5": 0.1,
|
|
621
|
+
"claude-fable-5-1": 0.025,
|
|
622
|
+
"claude-haiku-4-5": 0.1,
|
|
623
|
+
"claude-opus-4-0": 0.1,
|
|
624
|
+
"claude-opus-4-1": 0.1,
|
|
625
|
+
"claude-opus-4-5": 0.1,
|
|
626
|
+
"claude-opus-4-6": 0.1,
|
|
627
|
+
"claude-opus-4-7": 0.1,
|
|
628
|
+
"claude-opus-4-8": 0.1,
|
|
629
|
+
"claude-opus-5": 0.1,
|
|
630
|
+
"claude-sonnet-4-5": 0.1,
|
|
631
|
+
"claude-sonnet-4-6": 0.1,
|
|
632
|
+
"claude-sonnet-5": 0.1,
|
|
633
|
+
"deepseek/deepseek-v4-flash": 0.2,
|
|
634
|
+
"deepseek/deepseek-v4-pro": 0.11494252873563218,
|
|
635
|
+
"deepseek/deepseek-v4-pro-0813": 0.11494252873563218,
|
|
636
|
+
"gemini-2.0-flash": 0.25,
|
|
637
|
+
"gemini-2.0-flash-lite": 0.25,
|
|
638
|
+
"gemini-2.5-flash": 0.1,
|
|
639
|
+
"gemini-2.5-flash-lite": 0.1,
|
|
640
|
+
"gemini-2.5-pro": 0.1,
|
|
641
|
+
"gemini-3-flash-preview": 0.1,
|
|
642
|
+
"gemini-3-pro-preview": 0.1,
|
|
643
|
+
"gemini-3.1-flash-lite-preview": 0.1,
|
|
644
|
+
"gemini-3.1-pro-preview": 0.1,
|
|
645
|
+
"gemini-3.5-flash": 0.1,
|
|
646
|
+
"gemini-3.5-flash-lite": 0.1,
|
|
647
|
+
"gemini-3.6-flash": 0.1,
|
|
648
|
+
"gemini-3.7-flash": 0.1,
|
|
649
|
+
"gemini-3.8-flash": 0.1,
|
|
650
|
+
"google/gemma-4-26b-a4b-it": 0.1,
|
|
651
|
+
"gpt-4.1": 0.25,
|
|
652
|
+
"gpt-4.1-mini": 0.25,
|
|
653
|
+
"gpt-4.1-nano": 0.25,
|
|
654
|
+
"gpt-4o": 0.5,
|
|
655
|
+
"gpt-4o-mini": 0.5,
|
|
656
|
+
"gpt-5": 0.1,
|
|
657
|
+
"gpt-5-chat": 0.1,
|
|
658
|
+
"gpt-5-mini": 0.1,
|
|
659
|
+
"gpt-5-nano": 0.1,
|
|
660
|
+
"gpt-5.4": 0.1,
|
|
661
|
+
"gpt-5.4-mini": 0.1,
|
|
662
|
+
"gpt-5.4-nano": 0.1,
|
|
663
|
+
"gpt-5.4-pro": 1,
|
|
664
|
+
"gpt-5.5": 0.1,
|
|
665
|
+
"gpt-5.5-mini": 0.1,
|
|
666
|
+
"gpt-5.5-nano": 0.1,
|
|
667
|
+
"gpt-5.5-pro": 1,
|
|
668
|
+
"gpt-5.6-luna": 0.1,
|
|
669
|
+
"gpt-5.6-luna-pro": 0.1,
|
|
670
|
+
"gpt-5.6-sol": 0.1,
|
|
671
|
+
"gpt-5.6-sol-pro": 0.1,
|
|
672
|
+
"gpt-5.6-terra": 0.1,
|
|
673
|
+
"grok-4.5": 0.25,
|
|
674
|
+
"grok-4.6": 0.25,
|
|
675
|
+
"minimax/minimax-m2.7": 0.2,
|
|
676
|
+
"minimax/minimax-m3": 0.2,
|
|
677
|
+
"mistralai/mistral-small-2603": 0.1,
|
|
678
|
+
"moonshotai/kimi-k2.5": 0.5,
|
|
679
|
+
"moonshotai/kimi-k2.6": 0.1684,
|
|
680
|
+
"moonshotai/kimi-k2.7": 0.2,
|
|
681
|
+
"moonshotai/kimi-k3": 0.1,
|
|
682
|
+
"muse-spark-1.3": 0.12,
|
|
683
|
+
"qwen/qwen3-235b-a22b-2507": 0.5,
|
|
684
|
+
"qwen/qwen3-30b-a3b-instruct-2507": 0.1684,
|
|
685
|
+
"qwen/qwen3-coder": 0.1684,
|
|
686
|
+
"qwen/qwen3-coder-next": 0.5,
|
|
687
|
+
"qwen/qwen3-next-80b-a3b-instruct": 0.5,
|
|
688
|
+
"qwen/qwen3.5-flash-02-23": 0.1,
|
|
689
|
+
"qwen/qwen3.6-35b-a3b": 0.1,
|
|
690
|
+
"qwen/qwen3.7-plus": 0.2,
|
|
691
|
+
"qwen/qwen3.8-max": 0.125,
|
|
692
|
+
"xiaomi/mimo-v2.5-pro": 0.1,
|
|
693
|
+
"z-ai/glm-5": 0.2,
|
|
694
|
+
"z-ai/glm-5.1": 0.18571428571428572,
|
|
695
|
+
"z-ai/glm-5.2": 0.18571428571428572,
|
|
696
|
+
"z-ai/glm-5.3": 0.18571428571428572,
|
|
697
|
+
"z-ai/glm-5.3-flash": 0.2
|
|
698
|
+
}
|
|
699
|
+
}'
|
|
700
|
+
# END_GENERATED_PRICES
|
|
701
|
+
|
|
702
|
+
routed=""
|
|
703
|
+
forced="false"
|
|
704
|
+
forced_model=""
|
|
705
|
+
session_savings=""
|
|
706
|
+
tot_in=0
|
|
707
|
+
tot_out=0
|
|
708
|
+
tot_cache_read=0
|
|
709
|
+
tot_cache_write=0
|
|
710
|
+
|
|
711
|
+
# Per-turn savings compare each turn's routed cost (priced from
|
|
712
|
+
# message.model in the transcript) against what the CC-side model selection
|
|
713
|
+
# (selected_display) would have cost on the same tokens. The selection
|
|
714
|
+
# isn't strictly the per-turn "requested" model — CC tags some background
|
|
715
|
+
# side-calls (compaction probes, title-gen) with a different model id —
|
|
716
|
+
# but for those the planner short-circuits to a hard pin and the savings
|
|
717
|
+
# math zeroes out anyway. Turns where routed == selection or where either
|
|
718
|
+
# model isn't in the pricing table emit 0 savings; the tokens clause
|
|
719
|
+
# always renders.
|
|
720
|
+
|
|
721
|
+
# Normalize the CC-side selection once for use in the jq math below.
|
|
722
|
+
requested_norm="$(normalize_model "$selected_display")"
|
|
723
|
+
|
|
724
|
+
if [[ -n "$transcript_path" && -f "$transcript_path" ]]; then
|
|
725
|
+
# macOS ships `tail -r`, GNU coreutils ships `tac`. Either works to walk the
|
|
726
|
+
# JSONL in reverse so we can grab the latest assistant turn.
|
|
727
|
+
if command -v tac >/dev/null 2>&1; then reverse=(tac); else reverse=(tail -r); fi
|
|
728
|
+
|
|
729
|
+
# CC stamps message.model = "<synthetic>" on assistant turns it generated
|
|
730
|
+
# locally (errored requests, cancellations, tool-only stubs) instead of a
|
|
731
|
+
# real model id. Show that as "failure" rather than leaking the internal
|
|
732
|
+
# sentinel into the statusline.
|
|
733
|
+
routed="$("${reverse[@]}" "$transcript_path" 2>/dev/null \
|
|
734
|
+
| jq -r 'select(.type=="assistant") | .message.model // empty' \
|
|
735
|
+
| head -n 1 || true)"
|
|
736
|
+
if [[ "$routed" == "<synthetic>" ]]; then
|
|
737
|
+
routed="failure"
|
|
738
|
+
else
|
|
739
|
+
routed="$(normalize_model "$routed")"
|
|
740
|
+
fi
|
|
741
|
+
|
|
742
|
+
# Detect an active /force-model pin from the router's synthetic ack turns —
|
|
743
|
+
# the only turns stamped message.model == "weave-router". The router emits
|
|
744
|
+
# one whenever a pin changes state:
|
|
745
|
+
# * /force-model → "force-model applied: <model> (<provider>) …"
|
|
746
|
+
# * /unforce-model → "force-model cleared …"
|
|
747
|
+
# * loop / no-progress break → "… clearing the session pin …" (expires the
|
|
748
|
+
# pin, including a user-forced one)
|
|
749
|
+
# * unrecognized model → "… isn't a recognized model · keeping
|
|
750
|
+
# automatic routing" — a NO-OP: the prior
|
|
751
|
+
# pin, if any, is left untouched
|
|
752
|
+
# * listing (historical) → "… pick a model by id …" / "no models are
|
|
753
|
+
# available to pin …" — also NO-OPs. The
|
|
754
|
+
# router no longer emits these, but a
|
|
755
|
+
# transcript written while it did still
|
|
756
|
+
# carries them, and they must not read as
|
|
757
|
+
# a clear or a live pin loses its [forced]
|
|
758
|
+
# These persist on disk (the ingress stripper only scrubs them from upstream
|
|
759
|
+
# requests). Classify each weave-router turn newest-first, skip the no-op
|
|
760
|
+
# "rejected" acks, and let the latest real state change decide: an "applied"
|
|
761
|
+
# marker means the session is pinned (and names the model); anything else
|
|
762
|
+
# (cleared / loop-break / no-progress) means automatic routing has resumed.
|
|
763
|
+
# Restricting to weave-router turns keeps a normal reply that merely quotes
|
|
764
|
+
# these phrases from flipping the tag. (A silent server-side TTL expiry emits
|
|
765
|
+
# no turn and so can't be reflected here — the pin TTL outlives a session.)
|
|
766
|
+
force_state="$("${reverse[@]}" "$transcript_path" 2>/dev/null \
|
|
767
|
+
| jq -r 'select(.type=="assistant" and .message.model=="weave-router")
|
|
768
|
+
| ([.message.content[]? | select(.type? == "text") | .text] | join(" ") | gsub("[\n\r]"; " ")) as $t
|
|
769
|
+
| if ($t | test("force-model applied:")) then "APPLIED " + ($t | capture("force-model applied: (?<m>[^ ]+)").m)
|
|
770
|
+
elif ($t | test("isn.t a recognized model")) then "REJECTED"
|
|
771
|
+
elif ($t | test("pick a model by id|no models are available to pin")) then "REJECTED"
|
|
772
|
+
else "CLEARED" end' 2>/dev/null \
|
|
773
|
+
| grep -m1 -v '^REJECTED$' || true)"
|
|
774
|
+
if [[ "$force_state" == APPLIED\ * ]]; then
|
|
775
|
+
forced="true"
|
|
776
|
+
forced_model="${force_state#APPLIED }"
|
|
777
|
+
fi
|
|
778
|
+
|
|
779
|
+
# Compute a session running total: savings across every assistant turn
|
|
780
|
+
# whose marker reports a requested ≠ routed swap, plus cumulative token
|
|
781
|
+
# counts across every assistant turn (rerouted or not — total work the
|
|
782
|
+
# session has done). cache_creation is priced at 1.25× input; cache_read
|
|
783
|
+
# uses each model's generated catalog multiplier. Both are no-ops when the
|
|
784
|
+
# provider does not return those fields.
|
|
785
|
+
#
|
|
786
|
+
# The marker regex tolerates the optional "(<provider>)" segment and a
|
|
787
|
+
# `[1m]` / `-YYYYMMDD` suffix on either model name so transcripts written
|
|
788
|
+
# against context-tiered or dated model ids still parse cleanly.
|
|
789
|
+
#
|
|
790
|
+
# Dedup note: CC writes one JSONL entry per *content block* in an
|
|
791
|
+
# assistant turn (text, text, tool_use → 3 entries), and every entry
|
|
792
|
+
# carries the same `message.usage`. Summing per-entry triple-counts the
|
|
793
|
+
# turn. We dedupe on (message.id, message.usage) before summing:
|
|
794
|
+
# * For native Anthropic upstreams message.id is unique per turn, so
|
|
795
|
+
# this collapses the content-block fan-out cleanly.
|
|
796
|
+
# * For non-Anthropic upstreams that round-trip through the router's
|
|
797
|
+
# translator, message.id can be a constant placeholder
|
|
798
|
+
# ("msg_translated"); usage still differs per turn (input_tokens
|
|
799
|
+
# grows), so the composite key keeps turns distinct. Two turns with
|
|
800
|
+
# byte-identical id AND usage would still collapse, but that's a
|
|
801
|
+
# genuine retry/duplicate we want to drop.
|
|
802
|
+
read -r session_savings tot_in tot_out tot_cache_read tot_cache_write < <(
|
|
803
|
+
jq -rs --argjson p "$prices" --arg requested "$requested_norm" '
|
|
804
|
+
[.[] | select(.type=="assistant")] |
|
|
805
|
+
unique_by([.message.id, .message.usage]) |
|
|
806
|
+
.[] |
|
|
807
|
+
.message as $m |
|
|
808
|
+
($m.model // "" | sub("\\[[^]]*\\]$"; "") | sub("-[0-9]{8}$"; "")) as $rm |
|
|
809
|
+
{
|
|
810
|
+
in: ($m.usage.input_tokens // 0),
|
|
811
|
+
out: ($m.usage.output_tokens // 0),
|
|
812
|
+
cwrt: ($m.usage.cache_creation_input_tokens // 0),
|
|
813
|
+
crd: ($m.usage.cache_read_input_tokens // 0)
|
|
814
|
+
} as $t |
|
|
815
|
+
(if $requested == "" or $requested == $rm then 0
|
|
816
|
+
else
|
|
817
|
+
($p.input[$rm] // null) as $rin | ($p.output[$rm] // null) as $rout |
|
|
818
|
+
($p.cache_read[$rm] // null) as $rcr |
|
|
819
|
+
($p.input[$requested] // null) as $sin | ($p.output[$requested] // null) as $sout |
|
|
820
|
+
($p.cache_read[$requested] // null) as $scr |
|
|
821
|
+
if ($rin == null or $rout == null or $rcr == null or $sin == null or $sout == null or $scr == null) then 0
|
|
822
|
+
else
|
|
823
|
+
(($t.in + 1.25 * $t.cwrt + $rcr * $t.crd) / 1000) as $routed_input_units |
|
|
824
|
+
(($t.in + 1.25 * $t.cwrt + $scr * $t.crd) / 1000) as $requested_input_units |
|
|
825
|
+
($t.out / 1000) as $output_units |
|
|
826
|
+
($routed_input_units * $rin + $output_units * $rout) as $routed_cost |
|
|
827
|
+
($requested_input_units * $sin + $output_units * $sout) as $requested_cost |
|
|
828
|
+
($requested_cost - $routed_cost)
|
|
829
|
+
end
|
|
830
|
+
end) as $savings |
|
|
831
|
+
"\($savings) \($t.in) \($t.out) \($t.crd) \($t.cwrt)"
|
|
832
|
+
' "$transcript_path" 2>/dev/null \
|
|
833
|
+
| awk 'BEGIN{s=0; i=0; o=0; r=0; w=0}
|
|
834
|
+
{s+=$1; i+=$2; o+=$3; r+=$4; w+=$5}
|
|
835
|
+
END{printf "%.4f %d %d %d %d\n", s, i, o, r, w}'
|
|
836
|
+
) || true
|
|
837
|
+
fi
|
|
838
|
+
|
|
839
|
+
# ---------- refresh when a model isn't in the pricing table ----------
|
|
840
|
+
#
|
|
841
|
+
# A model that shipped after this copy of the script has no price entry, so the
|
|
842
|
+
# jq guard above zeroes savings on every turn and the line reads "saved $0.00"
|
|
843
|
+
# — indistinguishable from "the router ran and didn't beat your selection". The
|
|
844
|
+
# periodic check heals that eventually, but a week late, and a model launch is
|
|
845
|
+
# exactly when the number gets looked at. Refresh off-schedule instead.
|
|
846
|
+
#
|
|
847
|
+
# Keyed on its own stamp per unpriced id: a model we never price (self-hosted,
|
|
848
|
+
# unrecognized) then costs one download per interval, not one per turn, and
|
|
849
|
+
# can't starve the periodic check.
|
|
850
|
+
weave_refresh_on_price_miss() {
|
|
851
|
+
local candidates="" m
|
|
852
|
+
for m in "$@"; do
|
|
853
|
+
case "$m" in
|
|
854
|
+
"" | "?" | failure | weave-router | "<synthetic>") continue ;;
|
|
855
|
+
esac
|
|
856
|
+
candidates="${candidates}${m}"$'\n'
|
|
857
|
+
done
|
|
858
|
+
[ -n "$candidates" ] || return 0
|
|
859
|
+
|
|
860
|
+
# A malformed $prices emits nothing here, which fails closed (no refresh)
|
|
861
|
+
# rather than re-downloading every turn.
|
|
862
|
+
local missing
|
|
863
|
+
missing="$(printf '%s' "$candidates" \
|
|
864
|
+
| jq -rR --argjson p "$prices" \
|
|
865
|
+
'select($p.input[.] == null or $p.output[.] == null)' 2>/dev/null \
|
|
866
|
+
| head -n 1)" || return 0
|
|
867
|
+
[ -n "$missing" ] || return 0
|
|
868
|
+
|
|
869
|
+
local model_slug
|
|
870
|
+
model_slug="$(printf '%s' "$missing" | tr -c 'A-Za-z0-9._-' '_')"
|
|
871
|
+
weave_self_refresh ".miss.${model_slug}" 1
|
|
872
|
+
}
|
|
873
|
+
weave_refresh_on_price_miss "$requested_norm" "$routed" 2>/dev/null || true
|
|
874
|
+
|
|
875
|
+
# Brand color (#FF6C47) on terminals that grok 24-bit truecolor — that's
|
|
876
|
+
# every modern one (iTerm2, Apple Terminal, vscode, ghostty, alacritty,
|
|
877
|
+
# wezterm, kitty). Falls back gracefully on any escape-stripping terminal.
|
|
878
|
+
brand=$'\033[38;2;255;108;71mWEAVE ROUTER\033[0m'
|
|
879
|
+
|
|
880
|
+
# Format helpers.
|
|
881
|
+
fmt_money() {
|
|
882
|
+
awk -v v="$1" 'BEGIN{
|
|
883
|
+
if (v == "" || v+0 == 0) { printf "$0.00"; exit }
|
|
884
|
+
if (v+0 < 0.005 && v+0 > -0.005){ printf "<$0.01"; exit }
|
|
885
|
+
if (v+0 < 0) { printf "-$%.2f", -v+0; exit }
|
|
886
|
+
printf "$%.2f", v
|
|
887
|
+
}'
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
fmt_tok() {
|
|
891
|
+
awk -v v="$1" 'BEGIN{
|
|
892
|
+
v = v+0
|
|
893
|
+
if (v >= 1000000) { printf "%.1fM", v/1000000; exit }
|
|
894
|
+
if (v >= 1000) { printf "%.1fk", v/1000; exit }
|
|
895
|
+
printf "%d", v
|
|
896
|
+
}'
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
# cache_read tokens are the cached portion of every prompt that the
|
|
900
|
+
# provider serves at 0.1× input price; cache_write tokens are the bytes
|
|
901
|
+
# that get newly cached on this turn at 1.25× input price. They behave
|
|
902
|
+
# completely differently both in cost and in what they tell the user
|
|
903
|
+
# about session-level efficiency, so we surface them separately rather
|
|
904
|
+
# than summing into a single "cached" number that conflates the two.
|
|
905
|
+
# Each clause is shown only when nonzero, so quiet sessions stay quiet.
|
|
906
|
+
tokens_clause=""
|
|
907
|
+
if [[ "$tot_in" -gt 0 || "$tot_out" -gt 0 || "$tot_cache_read" -gt 0 || "$tot_cache_write" -gt 0 ]]; then
|
|
908
|
+
tokens_clause=" · $(fmt_tok "$tot_in") in / $(fmt_tok "$tot_out") out"
|
|
909
|
+
if [[ "$tot_cache_read" -gt 0 ]]; then
|
|
910
|
+
tokens_clause+=" / $(fmt_tok "$tot_cache_read") cache read"
|
|
911
|
+
fi
|
|
912
|
+
if [[ "$tot_cache_write" -gt 0 ]]; then
|
|
913
|
+
tokens_clause+=" / $(fmt_tok "$tot_cache_write") cache write"
|
|
914
|
+
fi
|
|
915
|
+
fi
|
|
916
|
+
|
|
917
|
+
if [[ "$forced" == "true" ]]; then
|
|
918
|
+
# Session is pinned via /force-model. The "← selection · saved $X" clause
|
|
919
|
+
# describes automatic routing and would be misleading on a manual pin, so
|
|
920
|
+
# show the pinned model with a [forced] tag instead. forced_model comes from
|
|
921
|
+
# the marker; fall back to the routed/selected id if parsing came up empty.
|
|
922
|
+
forced_display="${forced_model:-${routed:-$selected_display}}"
|
|
923
|
+
printf '%s — %s [forced]%s' "$brand" "$forced_display" "$tokens_clause"
|
|
924
|
+
elif [[ "$routed" == "failure" ]]; then
|
|
925
|
+
# Latest turn was a CC-synthesized error stub — don't claim a routing
|
|
926
|
+
# swap or compute savings against a non-model.
|
|
927
|
+
printf '%s — %s%s' "$brand" "$routed" "$tokens_clause"
|
|
928
|
+
elif [[ -n "$routed" ]]; then
|
|
929
|
+
# Always show the savings clause, flooring a non-positive total at $0.00.
|
|
930
|
+
# session_savings is "0.0000" on fresh sessions or sessions where every
|
|
931
|
+
# turn routed back to the selected model, and can go negative when routing
|
|
932
|
+
# upgrades a turn to a pricier model (e.g. a sticky/hard-pinned side-call,
|
|
933
|
+
# or a hard turn escalated to opus). "saved -$X" would mislead, so clamp
|
|
934
|
+
# the display to $0.00 rather than dropping the clause — a $0.00 readout
|
|
935
|
+
# tells the user the router ran and simply didn't beat their selection.
|
|
936
|
+
# When the CC selection is unknown ("?" or empty) there's nothing to
|
|
937
|
+
# compare against, so drop the "← selection" arrow and just show routed.
|
|
938
|
+
display_savings="$session_savings"
|
|
939
|
+
if [[ -z "$display_savings" ]] \
|
|
940
|
+
|| awk -v v="$display_savings" 'BEGIN{exit !(v+0 < 0)}'; then
|
|
941
|
+
display_savings="0"
|
|
942
|
+
fi
|
|
943
|
+
if [[ -n "$selected_display" && "$selected_display" != "?" ]]; then
|
|
944
|
+
printf '%s — %s ← %s · saved %s%s' \
|
|
945
|
+
"$brand" "$routed" "$selected_display" "$(fmt_money "$display_savings")" "$tokens_clause"
|
|
946
|
+
else
|
|
947
|
+
printf '%s — %s%s' "$brand" "$routed" "$tokens_clause"
|
|
948
|
+
fi
|
|
949
|
+
else
|
|
950
|
+
printf '%s — %s%s' "$brand" "$selected_display" "$tokens_clause"
|
|
951
|
+
fi
|