@weave-os/router 0.2.18 → 0.2.20

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/README.md CHANGED
@@ -20,6 +20,7 @@ npx @weave-os/router --pi # skip the picker, target pi + Loom U
20
20
  npx @weave-os/router --scope project # per-repo install, commit settings.json (or .codex/ / opencode.json)
21
21
  npx @weave-os/router --local # self-hosted via docker-compose (localhost:8080)
22
22
  npx @weave-os/router --base-url https://router.acme.internal
23
+ npx @weave-os/router --email you@example.com # set identity without prompting
23
24
  npx @weave-os/router --non-interactive # reads $WEAVE_ROUTER_KEY, no prompts (defaults to claude)
24
25
  ```
25
26
 
@@ -33,8 +34,16 @@ npx @weave-os/router --claude # reuses the installed key
33
34
  npx @weave-os/router --codex # same for Codex, opencode, and pi
34
35
  npx @weave-os/router --claude --rotate-key # ignore it and prompt for a new one
35
36
  npx @weave-os/router update --claude # non-interactive refresh in place
37
+ npx @weave-os/router update --claude --context-window 1m # opt into Claude's 1M local context
36
38
  ```
37
39
 
40
+ Claude Code normally keeps a 200k local context budget when using a custom
41
+ router endpoint. The explicit `--context-window 1m` option selects the
42
+ supported `[1m]` model variant, preserves automatic compaction, and records
43
+ the prior model so `off` or uninstall can restore it. It is opt-in: longer
44
+ histories can increase token usage and cost, and the upstream provider still
45
+ needs to support the requested context size.
46
+
38
47
  For Claude Code the installed statusline and `/force-model`, `/router-*` slash
39
48
  commands also refresh themselves in the background about once a week (never
40
49
  overwriting a wrapper you edited). Opt out with `WEAVE_STATUSLINE_UPDATE=0`, or
@@ -118,25 +127,24 @@ Four install targets:
118
127
  every other selected model uses its WorkWeave deployment or BYOK credential.
119
128
  The block lives between begin/end markers
120
129
  so re-running the installer rewrites it cleanly and `--uninstall --codex`
121
- removes it without touching the rest of your config. Codex does not load
122
- third-party slash-command files; the installer provides native skills
123
- `$force-model` (`$fm`), `$unforce-model` (`$ufm`), and `$router-feedback`
124
- (`$rf`), each of which execs a local `scripts/emit.sh` that prints the same
125
- leading-space directive Claude Code uses (for example,
126
- ` /force-model gpt-5.6-terra`) — you can also type that form directly. It
127
- also installs `$router-status`, `$router-off`, `$router-on`, and
128
- `$router-models`, which call this installer's own verbs, plus a
129
- `$disable-routing` skill that returns the next Codex session to the default
130
- provider without logging out or deleting the router configuration. The
131
- managed lifecycle hooks also keep the latest routed model in the terminal
132
- title and emit a compact status message when the router reports a new route.
133
- - **opencode** (`--opencode`) — merges a `provider.weave` entry (backed by
134
- opencode's built-in `@ai-sdk/anthropic` provider) into
130
+ removes it without touching the rest of your config. Codex does not load
131
+ third-party slash-command files; the installer provides native skills
132
+ `$force-model` (`$fm`), `$unforce-model` (`$ufm`), and `$router-feedback`
133
+ (`$rf`), whose invocations are handled by the router directly. It also
134
+ installs `$router-status`, `$router-off`, `$router-on`, and `$router-models`,
135
+ which call this installer's own verbs, plus a `$disable-routing` skill that
136
+ returns the next Codex session to the default provider without logging out
137
+ or deleting the router configuration. The managed lifecycle hooks also keep
138
+ the latest routed model in the terminal title and emit a compact status
139
+ message when the router reports a new route.
140
+ - **opencode** (`--opencode`) — merges a Responses-format `provider.weave`
141
+ entry backed by `@ai-sdk/openai` into
135
142
  `~/.config/opencode/opencode.json` (or `<repo>/opencode.json` with
136
- `--scope project`). The router speaks the Anthropic Messages API
137
- natively, so opencode talks to it unmodified. Re-install rewrites only
138
- the managed `provider.weave` block; `--uninstall --opencode` strips it
139
- and leaves your other providers and settings alone.
143
+ `--scope project`). Installation activates `weave/auto` and parks any prior
144
+ default model so `off` and uninstall can restore it exactly. The bundled
145
+ plugin can attach ChatGPT and Claude plan credentials to the matching routed
146
+ turns. Re-install rewrites only the managed provider; unrelated providers,
147
+ MCP servers, agents, and plugins stay untouched.
140
148
  - **pi** (`--pi`) — registers the `weave` provider and installs this package as
141
149
  a pi extension. Stock pi then gets the Loom startup header, Wooly's animated
142
150
  mascot, the persistent actual-route display, cumulative session savings,
package/cc-statusline.sh CHANGED
@@ -281,8 +281,13 @@ weave_sync_commands() {
281
281
  installed_body="$(sed '/^<!-- weave-router managed command: .* -->$/d' "$installed" 2>/dev/null)" || installed_body=""
282
282
  if [ "$prev_body" = "$installed_body" ] && [ "$new_body" != "$installed_body" ]; then
283
283
  tmp="$installed.tmp.$$"
284
- if printf '%s\n<!-- weave-router managed command: %s -->' "$new_body" "$name" >"$tmp" 2>/dev/null; then
284
+ if printf '%s\n' "$new_body" >"$tmp" 2>/dev/null; then
285
285
  mv "$tmp" "$installed" 2>/dev/null || rm -f "$tmp"
286
+ ownership_file="$installed.weave-router"
287
+ if [ ! -L "$ownership_file" ]; then
288
+ printf 'weave-router managed command: %s\n%s\n' "$name" "$new_body" \
289
+ >"$ownership_file" 2>/dev/null || true
290
+ fi
286
291
  else
287
292
  rm -f "$tmp"
288
293
  fi
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env bash
2
+ # <!-- weave-router managed codex directive -->
3
+ #
4
+ # Codex UserPromptSubmit hook for the Weave Router's LOCAL config toggles.
5
+ # Codex passes a JSON object on stdin carrying the RAW prompt text, before
6
+ # `$skill` expansion and before any inference.
7
+ #
8
+ # Why this exists. The other directives ($fm, $ufm, $rf, $router-session,
9
+ # a bare $router-models) are router state, and the router answers them itself
10
+ # with no model turn. These four are not: they rewrite config.toml on this
11
+ # machine, which a remote HTTP service cannot do. Left as skills they cost two
12
+ # inference turns each -- one for the model to read the skill and decide to
13
+ # exec, one to report the tool result.
14
+ #
15
+ # The second reason matters more than the cost. $router-off and
16
+ # $disable-routing are what you reach for when the router is slow, broken, or
17
+ # out of credits -- and as skills they need a model turn, which is served
18
+ # THROUGH the router. The off switch depended on the thing it turns off. This
19
+ # hook runs locally before any network call, so it works when the router does
20
+ # not.
21
+ #
22
+ # Scope: only the four local toggles. Everything the router can answer is
23
+ # deliberately absent -- a hook that intercepted those would re-open the
24
+ # duplicate-path problem #1257 closed.
25
+ #
26
+ # FAIL OPEN. Every unexpected condition -- no jq, no npx, a malformed payload,
27
+ # a command that errors -- must let the prompt through untouched. A directive
28
+ # that silently swallows the user's prompt is far worse than one that does not
29
+ # fire.
30
+
31
+ set -uo pipefail
32
+
33
+ # How long the toggle may take before the hook gives up and passes the prompt
34
+ # through. npx resolves from cache after first use; the budget covers a cold
35
+ # fetch without letting a dead network hang the turn indefinitely.
36
+ #
37
+ # Validated, not trusted. `[ "$waited" -ge "$seconds" ]` does not compare a
38
+ # bound it cannot parse -- it errors -- so the cap would never fire and the
39
+ # hook would hang until npx exited on its own. Two ways to get there: a
40
+ # non-numeric value, and a digit-only value too large for the shell's integer
41
+ # type ("[: 99999999999999999999: integer expression expected"). The length
42
+ # bound covers the second; 4 digits is already ~2.7h, far past anything
43
+ # meaningful for a prompt hook.
44
+ #
45
+ # The diagnostic goes to stderr, never stdout: stdout is the hook protocol
46
+ # channel and any stray byte there corrupts the JSON. Codex discards stderr,
47
+ # so this costs a user nothing and tells whoever set the variable why it was
48
+ # ignored.
49
+ WEAVE_TOGGLE_TIMEOUT_DEFAULT=45
50
+ # Resolved into a local first: under `set -u`, ${#WEAVE_TOGGLE_TIMEOUT} on an
51
+ # unset variable aborts the hook outright -- which would swallow every prompt,
52
+ # the one failure this script must never have.
53
+ weave_timeout="${WEAVE_TOGGLE_TIMEOUT:-$WEAVE_TOGGLE_TIMEOUT_DEFAULT}"
54
+ weave_timeout_invalid=""
55
+ case "$weave_timeout" in
56
+ ''|*[!0-9]*|0) weave_timeout_invalid=1 ;;
57
+ *) [ "${#weave_timeout}" -gt 4 ] && weave_timeout_invalid=1 ;;
58
+ esac
59
+ if [ -n "$weave_timeout_invalid" ]; then
60
+ printf 'weave-router: ignoring invalid WEAVE_TOGGLE_TIMEOUT=%s; using %ss\n' \
61
+ "$weave_timeout" "$WEAVE_TOGGLE_TIMEOUT_DEFAULT" >&2
62
+ weave_timeout="$WEAVE_TOGGLE_TIMEOUT_DEFAULT"
63
+ fi
64
+ WEAVE_TOGGLE_TIMEOUT="$weave_timeout"
65
+
66
+ # ---------- responses ----------
67
+
68
+ # pass_through hands the prompt to Codex unchanged. Every failure path ends here.
69
+ pass_through() {
70
+ printf '{"continue":true}\n'
71
+ exit 0
72
+ }
73
+
74
+ # block stops the turn before any inference and shows the text to the user.
75
+ # Codex renders `reason`; nothing reaches the model.
76
+ #
77
+ # Setting only `decision` is deliberate: `continue:false` alongside it wins and
78
+ # renders a bare "Hook stopped" with the reason discarded.
79
+ block() {
80
+ local reason="$1" payload
81
+ payload="$(jq -cn --arg r "$reason" '{decision:"block", reason:$r}' 2>/dev/null)" \
82
+ || pass_through
83
+ printf '%s\n' "$payload"
84
+ exit 0
85
+ }
86
+
87
+ command -v jq >/dev/null 2>&1 || pass_through
88
+
89
+ payload="$(cat)" || pass_through
90
+ [ -n "$payload" ] || pass_through
91
+
92
+ prompt="$(jq -r '.prompt // ""' <<<"$payload" 2>/dev/null)" || pass_through
93
+
94
+ # Only a directive at the very start of the prompt is ours. Prose that merely
95
+ # mentions `$router-off` in a sentence is the user's text, not a command.
96
+ case "$prompt" in
97
+ '$'*) ;;
98
+ *) pass_through ;;
99
+ esac
100
+
101
+ verb="${prompt#$}"
102
+ # These four take no arguments. A prompt with a tail is not one of ours --
103
+ # pass it through so the skill can explain itself rather than the hook eating
104
+ # a message it cannot honour.
105
+ case "$verb" in
106
+ *[[:space:]]*) pass_through ;;
107
+ esac
108
+
109
+ # ---------- directive table ----------
110
+ #
111
+ # Local-config toggles only. router-on/off/status flip this install's
112
+ # config.toml; disable-routing is `off` plus the reversal hint below.
113
+ case "$verb" in
114
+ router-on) toggle="on" ;;
115
+ router-off) toggle="off" ;;
116
+ router-status) toggle="status" ;;
117
+ disable-routing) toggle="off" ;;
118
+ *) pass_through ;;
119
+ esac
120
+
121
+ command -v npx >/dev/null 2>&1 || pass_through
122
+
123
+ # ---------- run ----------
124
+
125
+ # WEAVE_TOGGLE_TIMED_OUT is set by run_bounded instead of encoding the timeout
126
+ # in the exit status. 124 is a status the child can legitimately return, and
127
+ # reading that as "we timed out" would swallow the toggle's own error output.
128
+ WEAVE_TOGGLE_TIMED_OUT=0
129
+
130
+ # run_bounded runs a command with a wall-clock cap. GNU `timeout` is not on
131
+ # macOS, so this polls the child rather than depending on coreutils.
132
+ #
133
+ # The child runs in its own process group (`set -m` + `kill -- -PID`) because
134
+ # npx execs node: signalling only the direct child leaves that node process
135
+ # running after the hook has already passed the prompt through.
136
+ run_bounded() {
137
+ local seconds="$1" out="$2"; shift 2
138
+ WEAVE_TOGGLE_TIMED_OUT=0
139
+
140
+ local had_monitor=0
141
+ case "$-" in *m*) had_monitor=1 ;; esac
142
+ set -m
143
+ "$@" >"$out" 2>&1 &
144
+ local pid=$!
145
+ [ "$had_monitor" -eq 1 ] || set +m
146
+
147
+ local waited=0
148
+ while kill -0 "$pid" 2>/dev/null; do
149
+ if [ "$waited" -ge "$seconds" ]; then
150
+ WEAVE_TOGGLE_TIMED_OUT=1
151
+ # Negative pid = the whole group. Fall back to the bare pid if the
152
+ # group is already gone, so a raced exit still gets reaped.
153
+ kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null
154
+ sleep 1
155
+ kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid" 2>/dev/null
156
+ wait "$pid" 2>/dev/null
157
+ return 0
158
+ fi
159
+ sleep 1
160
+ waited=$((waited + 1))
161
+ done
162
+ wait "$pid"
163
+ }
164
+
165
+ out_file="$(mktemp -t weave-codex-toggle.XXXXXX)" || pass_through
166
+ # shellcheck disable=SC2064 # expand out_file now; it never changes after this
167
+ trap "rm -f '$out_file'" EXIT
168
+
169
+ # {{SCOPE}} is substituted at install time by the only code that knows whether
170
+ # this install is user, project, or --dir scoped. It becomes literal argument
171
+ # text (the installer printf %q-quotes any path), so there is nothing here to
172
+ # word-split at runtime -- the braces are a template token, not an expansion.
173
+ # shellcheck disable=SC1083
174
+ run_bounded "$WEAVE_TOGGLE_TIMEOUT" "$out_file" \
175
+ npx --package @weave-os/router -y -- weave-router "$toggle" --codex{{SCOPE}}
176
+ status=$?
177
+
178
+ output="$(cat "$out_file" 2>/dev/null || true)"
179
+ # Strip ANSI colour the installer emits when it thinks it has a terminal;
180
+ # Codex renders `reason` as plain text.
181
+ output="$(printf '%s' "$output" | sed -e 's/\x1b\[[0-9;]*m//g')"
182
+
183
+ # A failure is reported, not swallowed: passing through here would hand the
184
+ # model a prompt the user meant as a command, and it would likely try the same
185
+ # command again. Only a real timeout passes through -- the toggle never got to
186
+ # finish, so the skill path is still the honest fallback.
187
+ if [ "$WEAVE_TOGGLE_TIMED_OUT" -eq 1 ]; then
188
+ pass_through
189
+ fi
190
+
191
+ [ -n "$output" ] || output="weave-router $toggle --codex exited with status $status."
192
+
193
+ # Both trailers describe a change that actually happened, so they are gated on
194
+ # a zero exit. A failed toggle rewrote no config; telling the user it takes
195
+ # effect next launch would send them to restart Codex for nothing.
196
+ if [ "$status" -eq 0 ] && [ "$verb" != "router-status" ]; then
197
+ output="$output"$'\n\n'"Takes effect on your next \`codex\` launch: Codex reads its provider config at startup, so this session keeps routing as it already was."
198
+ fi
199
+ if [ "$status" -eq 0 ] && [ "$verb" = "disable-routing" ]; then
200
+ output="$output"$'\n'"Reverse it with \$router-on."
201
+ fi
202
+
203
+ block "$output"