@weave-os/router 0.2.18 → 0.2.19
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/codex-toggle.sh +203 -0
- package/install.sh +318 -9
- package/package.json +2 -1
package/codex-toggle.sh
ADDED
|
@@ -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"
|
package/install.sh
CHANGED
|
@@ -552,6 +552,11 @@ weave_owns_codex_helper() {
|
|
|
552
552
|
weave_owned_codex_helpers() {
|
|
553
553
|
weave_owns_codex_helper "${codex_status_file:-}" '<!-- weave-router managed codex status -->' rewrite \
|
|
554
554
|
&& printf '%s\n' "$codex_status_file"
|
|
555
|
+
# delete, not rewrite: unlike the status helper, a symlink at the directive
|
|
556
|
+
# path is never ours even when its target carries the marker. Classifying it
|
|
557
|
+
# rewrite-owned made a reinstall follow the link and overwrite whatever it
|
|
558
|
+
# pointed at. install_codex_directive_script skips symlinks for the same
|
|
559
|
+
# reason, so the two stay consistent.
|
|
555
560
|
weave_owns_codex_helper "${codex_directive_file:-}" '<!-- weave-router managed codex directive -->' delete \
|
|
556
561
|
&& printf '%s\n' "$codex_directive_file"
|
|
557
562
|
return 0
|
|
@@ -662,6 +667,7 @@ write_codex_config() {
|
|
|
662
667
|
esc_name="$(toml_escape "$block_name")"
|
|
663
668
|
esc_url="$(toml_escape "$block_url")"
|
|
664
669
|
esc_status="$(toml_escape "$codex_status_file")"
|
|
670
|
+
esc_directive="$(toml_escape "$codex_directive_file")"
|
|
665
671
|
|
|
666
672
|
# Plant whichever identity values we have alongside the router key so the
|
|
667
673
|
# router can attribute Codex traffic to a person on shared keys. Build the
|
|
@@ -701,6 +707,25 @@ write_codex_config() {
|
|
|
701
707
|
hook_feature_line=""
|
|
702
708
|
fi
|
|
703
709
|
if [ "$codex_hooks_enabled" = "true" ]; then
|
|
710
|
+
# UserPromptSubmit is registered only when OUR hook is actually on disk.
|
|
711
|
+
# Existence alone is the wrong test: install_codex_directive_script leaves
|
|
712
|
+
# a user-owned file at that path untouched, and registering it would hand
|
|
713
|
+
# someone else's script every prompt. Registering a command that is not
|
|
714
|
+
# there is the other failure -- Codex then fails every prompt rather than
|
|
715
|
+
# every toggle -- so the file must exist and carry our marker. -L is
|
|
716
|
+
# checked first because -f follows the link: a symlink to a marked file is
|
|
717
|
+
# still not a file we may point Codex at.
|
|
718
|
+
local directive_hook_block=""
|
|
719
|
+
if [ ! -L "$codex_directive_file" ] && [ -f "$codex_directive_file" ] && grep -Fq '<!-- weave-router managed codex directive -->' "$codex_directive_file"; then
|
|
720
|
+
directive_hook_block="$(cat <<TOML
|
|
721
|
+
|
|
722
|
+
[[hooks.UserPromptSubmit]]
|
|
723
|
+
[[hooks.UserPromptSubmit.hooks]]
|
|
724
|
+
type = "command"
|
|
725
|
+
command = "${esc_directive}"
|
|
726
|
+
TOML
|
|
727
|
+
)"
|
|
728
|
+
fi
|
|
704
729
|
hook_block="$(cat <<TOML
|
|
705
730
|
|
|
706
731
|
[[hooks.SessionStart]]
|
|
@@ -713,7 +738,7 @@ command = "${esc_status}"
|
|
|
713
738
|
type = "command"
|
|
714
739
|
command = "${esc_status}"
|
|
715
740
|
TOML
|
|
716
|
-
)"
|
|
741
|
+
)${directive_hook_block}"
|
|
717
742
|
fi
|
|
718
743
|
local block
|
|
719
744
|
block="$(cat <<TOML
|
|
@@ -3702,11 +3727,12 @@ announce_done() {
|
|
|
3702
3727
|
# latest known routed model in the terminal title. The embedded copy keeps the
|
|
3703
3728
|
# standalone curl installer feature-complete; packaged installs use the
|
|
3704
3729
|
# canonical sibling asset.
|
|
3705
|
-
# remove_codex_directive_helper deletes the UserPromptSubmit helper
|
|
3706
|
-
#
|
|
3707
|
-
#
|
|
3708
|
-
# run a missing command on every prompt, so the
|
|
3709
|
-
# strip_weave_codex_hooks removes the registration; this
|
|
3730
|
+
# remove_codex_directive_helper deletes the UserPromptSubmit helper. Install no
|
|
3731
|
+
# longer calls it — install_codex_directive_script writes the local-toggle hook
|
|
3732
|
+
# in its place — but uninstall still does, and a registration left pointing at a
|
|
3733
|
+
# deleted script would make Codex run a missing command on every prompt, so the
|
|
3734
|
+
# two must go together. strip_weave_codex_hooks removes the registration; this
|
|
3735
|
+
# removes the file.
|
|
3710
3736
|
remove_codex_directive_helper() {
|
|
3711
3737
|
[ -e "$codex_directive_file" ] || return 0
|
|
3712
3738
|
# Skip, do not exit: a symlink here is the user's, and aborting would leave
|
|
@@ -4164,17 +4190,300 @@ CODEX_STATUS_EOF
|
|
|
4164
4190
|
ok "Codex status integration installed at $codex_status_file"
|
|
4165
4191
|
}
|
|
4166
4192
|
|
|
4193
|
+
# Install the UserPromptSubmit hook that answers the four local-config toggles
|
|
4194
|
+
# ($router-on/off/status, $disable-routing) without an inference turn.
|
|
4195
|
+
#
|
|
4196
|
+
# Only these four. Everything the router can answer it already answers itself,
|
|
4197
|
+
# and a hook that also intercepted those would re-open the duplicate-path
|
|
4198
|
+
# problem #1257 closed. What is left here cannot move server-side at all: they
|
|
4199
|
+
# rewrite this machine's config.toml, and $router-off in particular must keep
|
|
4200
|
+
# working when the router is the thing that is broken.
|
|
4201
|
+
#
|
|
4202
|
+
# Mirrors install_codex_status_script, with one addition: the {{SCOPE}} token
|
|
4203
|
+
# is substituted the same way install_slash_commands does it, because the
|
|
4204
|
+
# installed hook cannot discover at run time which install it belongs to.
|
|
4205
|
+
install_codex_directive_script() {
|
|
4206
|
+
local candidate directive_src=""
|
|
4207
|
+
for candidate in \
|
|
4208
|
+
"$script_dir/codex-toggle.sh" \
|
|
4209
|
+
"$script_dir/../codex-toggle.sh"
|
|
4210
|
+
do
|
|
4211
|
+
if [ -f "$candidate" ]; then
|
|
4212
|
+
directive_src="$candidate"
|
|
4213
|
+
break
|
|
4214
|
+
fi
|
|
4215
|
+
done
|
|
4216
|
+
# Every bail-out below is a skip, never a failure. Unlike the status helper,
|
|
4217
|
+
# this hook has a working fallback -- the toggle skills still shell out to
|
|
4218
|
+
# the same installer -- so refusing to install the router because of a stray
|
|
4219
|
+
# file would cost the user far more than the two turns the hook saves.
|
|
4220
|
+
#
|
|
4221
|
+
# No sibling asset means a standalone `curl | sh` install, which has only
|
|
4222
|
+
# this file. The embedded copy below keeps that path feature-complete;
|
|
4223
|
+
# install/tests/codex-toggle_test.sh diffs the two and fails on drift.
|
|
4224
|
+
local embedded_src=""
|
|
4225
|
+
if [ -z "$directive_src" ]; then
|
|
4226
|
+
embedded_src="$(mktemp -t weave-codex-toggle.XXXXXX)" || return 0
|
|
4227
|
+
cat >"$embedded_src" <<'CODEX_TOGGLE_EOF'
|
|
4228
|
+
#!/usr/bin/env bash
|
|
4229
|
+
# <!-- weave-router managed codex directive -->
|
|
4230
|
+
#
|
|
4231
|
+
# Codex UserPromptSubmit hook for the Weave Router's LOCAL config toggles.
|
|
4232
|
+
# Codex passes a JSON object on stdin carrying the RAW prompt text, before
|
|
4233
|
+
# `$skill` expansion and before any inference.
|
|
4234
|
+
#
|
|
4235
|
+
# Why this exists. The other directives ($fm, $ufm, $rf, $router-session,
|
|
4236
|
+
# a bare $router-models) are router state, and the router answers them itself
|
|
4237
|
+
# with no model turn. These four are not: they rewrite config.toml on this
|
|
4238
|
+
# machine, which a remote HTTP service cannot do. Left as skills they cost two
|
|
4239
|
+
# inference turns each -- one for the model to read the skill and decide to
|
|
4240
|
+
# exec, one to report the tool result.
|
|
4241
|
+
#
|
|
4242
|
+
# The second reason matters more than the cost. $router-off and
|
|
4243
|
+
# $disable-routing are what you reach for when the router is slow, broken, or
|
|
4244
|
+
# out of credits -- and as skills they need a model turn, which is served
|
|
4245
|
+
# THROUGH the router. The off switch depended on the thing it turns off. This
|
|
4246
|
+
# hook runs locally before any network call, so it works when the router does
|
|
4247
|
+
# not.
|
|
4248
|
+
#
|
|
4249
|
+
# Scope: only the four local toggles. Everything the router can answer is
|
|
4250
|
+
# deliberately absent -- a hook that intercepted those would re-open the
|
|
4251
|
+
# duplicate-path problem #1257 closed.
|
|
4252
|
+
#
|
|
4253
|
+
# FAIL OPEN. Every unexpected condition -- no jq, no npx, a malformed payload,
|
|
4254
|
+
# a command that errors -- must let the prompt through untouched. A directive
|
|
4255
|
+
# that silently swallows the user's prompt is far worse than one that does not
|
|
4256
|
+
# fire.
|
|
4257
|
+
|
|
4258
|
+
set -uo pipefail
|
|
4259
|
+
|
|
4260
|
+
# How long the toggle may take before the hook gives up and passes the prompt
|
|
4261
|
+
# through. npx resolves from cache after first use; the budget covers a cold
|
|
4262
|
+
# fetch without letting a dead network hang the turn indefinitely.
|
|
4263
|
+
#
|
|
4264
|
+
# Validated, not trusted. `[ "$waited" -ge "$seconds" ]` does not compare a
|
|
4265
|
+
# bound it cannot parse -- it errors -- so the cap would never fire and the
|
|
4266
|
+
# hook would hang until npx exited on its own. Two ways to get there: a
|
|
4267
|
+
# non-numeric value, and a digit-only value too large for the shell's integer
|
|
4268
|
+
# type ("[: 99999999999999999999: integer expression expected"). The length
|
|
4269
|
+
# bound covers the second; 4 digits is already ~2.7h, far past anything
|
|
4270
|
+
# meaningful for a prompt hook.
|
|
4271
|
+
#
|
|
4272
|
+
# The diagnostic goes to stderr, never stdout: stdout is the hook protocol
|
|
4273
|
+
# channel and any stray byte there corrupts the JSON. Codex discards stderr,
|
|
4274
|
+
# so this costs a user nothing and tells whoever set the variable why it was
|
|
4275
|
+
# ignored.
|
|
4276
|
+
WEAVE_TOGGLE_TIMEOUT_DEFAULT=45
|
|
4277
|
+
# Resolved into a local first: under `set -u`, ${#WEAVE_TOGGLE_TIMEOUT} on an
|
|
4278
|
+
# unset variable aborts the hook outright -- which would swallow every prompt,
|
|
4279
|
+
# the one failure this script must never have.
|
|
4280
|
+
weave_timeout="${WEAVE_TOGGLE_TIMEOUT:-$WEAVE_TOGGLE_TIMEOUT_DEFAULT}"
|
|
4281
|
+
weave_timeout_invalid=""
|
|
4282
|
+
case "$weave_timeout" in
|
|
4283
|
+
''|*[!0-9]*|0) weave_timeout_invalid=1 ;;
|
|
4284
|
+
*) [ "${#weave_timeout}" -gt 4 ] && weave_timeout_invalid=1 ;;
|
|
4285
|
+
esac
|
|
4286
|
+
if [ -n "$weave_timeout_invalid" ]; then
|
|
4287
|
+
printf 'weave-router: ignoring invalid WEAVE_TOGGLE_TIMEOUT=%s; using %ss\n' \
|
|
4288
|
+
"$weave_timeout" "$WEAVE_TOGGLE_TIMEOUT_DEFAULT" >&2
|
|
4289
|
+
weave_timeout="$WEAVE_TOGGLE_TIMEOUT_DEFAULT"
|
|
4290
|
+
fi
|
|
4291
|
+
WEAVE_TOGGLE_TIMEOUT="$weave_timeout"
|
|
4292
|
+
|
|
4293
|
+
# ---------- responses ----------
|
|
4294
|
+
|
|
4295
|
+
# pass_through hands the prompt to Codex unchanged. Every failure path ends here.
|
|
4296
|
+
pass_through() {
|
|
4297
|
+
printf '{"continue":true}\n'
|
|
4298
|
+
exit 0
|
|
4299
|
+
}
|
|
4300
|
+
|
|
4301
|
+
# block stops the turn before any inference and shows the text to the user.
|
|
4302
|
+
# Codex renders `reason`; nothing reaches the model.
|
|
4303
|
+
#
|
|
4304
|
+
# Setting only `decision` is deliberate: `continue:false` alongside it wins and
|
|
4305
|
+
# renders a bare "Hook stopped" with the reason discarded.
|
|
4306
|
+
block() {
|
|
4307
|
+
local reason="$1" payload
|
|
4308
|
+
payload="$(jq -cn --arg r "$reason" '{decision:"block", reason:$r}' 2>/dev/null)" \
|
|
4309
|
+
|| pass_through
|
|
4310
|
+
printf '%s\n' "$payload"
|
|
4311
|
+
exit 0
|
|
4312
|
+
}
|
|
4313
|
+
|
|
4314
|
+
command -v jq >/dev/null 2>&1 || pass_through
|
|
4315
|
+
|
|
4316
|
+
payload="$(cat)" || pass_through
|
|
4317
|
+
[ -n "$payload" ] || pass_through
|
|
4318
|
+
|
|
4319
|
+
prompt="$(jq -r '.prompt // ""' <<<"$payload" 2>/dev/null)" || pass_through
|
|
4320
|
+
|
|
4321
|
+
# Only a directive at the very start of the prompt is ours. Prose that merely
|
|
4322
|
+
# mentions `$router-off` in a sentence is the user's text, not a command.
|
|
4323
|
+
case "$prompt" in
|
|
4324
|
+
'$'*) ;;
|
|
4325
|
+
*) pass_through ;;
|
|
4326
|
+
esac
|
|
4327
|
+
|
|
4328
|
+
verb="${prompt#$}"
|
|
4329
|
+
# These four take no arguments. A prompt with a tail is not one of ours --
|
|
4330
|
+
# pass it through so the skill can explain itself rather than the hook eating
|
|
4331
|
+
# a message it cannot honour.
|
|
4332
|
+
case "$verb" in
|
|
4333
|
+
*[[:space:]]*) pass_through ;;
|
|
4334
|
+
esac
|
|
4335
|
+
|
|
4336
|
+
# ---------- directive table ----------
|
|
4337
|
+
#
|
|
4338
|
+
# Local-config toggles only. router-on/off/status flip this install's
|
|
4339
|
+
# config.toml; disable-routing is `off` plus the reversal hint below.
|
|
4340
|
+
case "$verb" in
|
|
4341
|
+
router-on) toggle="on" ;;
|
|
4342
|
+
router-off) toggle="off" ;;
|
|
4343
|
+
router-status) toggle="status" ;;
|
|
4344
|
+
disable-routing) toggle="off" ;;
|
|
4345
|
+
*) pass_through ;;
|
|
4346
|
+
esac
|
|
4347
|
+
|
|
4348
|
+
command -v npx >/dev/null 2>&1 || pass_through
|
|
4349
|
+
|
|
4350
|
+
# ---------- run ----------
|
|
4351
|
+
|
|
4352
|
+
# WEAVE_TOGGLE_TIMED_OUT is set by run_bounded instead of encoding the timeout
|
|
4353
|
+
# in the exit status. 124 is a status the child can legitimately return, and
|
|
4354
|
+
# reading that as "we timed out" would swallow the toggle's own error output.
|
|
4355
|
+
WEAVE_TOGGLE_TIMED_OUT=0
|
|
4356
|
+
|
|
4357
|
+
# run_bounded runs a command with a wall-clock cap. GNU `timeout` is not on
|
|
4358
|
+
# macOS, so this polls the child rather than depending on coreutils.
|
|
4359
|
+
#
|
|
4360
|
+
# The child runs in its own process group (`set -m` + `kill -- -PID`) because
|
|
4361
|
+
# npx execs node: signalling only the direct child leaves that node process
|
|
4362
|
+
# running after the hook has already passed the prompt through.
|
|
4363
|
+
run_bounded() {
|
|
4364
|
+
local seconds="$1" out="$2"; shift 2
|
|
4365
|
+
WEAVE_TOGGLE_TIMED_OUT=0
|
|
4366
|
+
|
|
4367
|
+
local had_monitor=0
|
|
4368
|
+
case "$-" in *m*) had_monitor=1 ;; esac
|
|
4369
|
+
set -m
|
|
4370
|
+
"$@" >"$out" 2>&1 &
|
|
4371
|
+
local pid=$!
|
|
4372
|
+
[ "$had_monitor" -eq 1 ] || set +m
|
|
4373
|
+
|
|
4374
|
+
local waited=0
|
|
4375
|
+
while kill -0 "$pid" 2>/dev/null; do
|
|
4376
|
+
if [ "$waited" -ge "$seconds" ]; then
|
|
4377
|
+
WEAVE_TOGGLE_TIMED_OUT=1
|
|
4378
|
+
# Negative pid = the whole group. Fall back to the bare pid if the
|
|
4379
|
+
# group is already gone, so a raced exit still gets reaped.
|
|
4380
|
+
kill -TERM -- "-$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null
|
|
4381
|
+
sleep 1
|
|
4382
|
+
kill -KILL -- "-$pid" 2>/dev/null || kill -KILL "$pid" 2>/dev/null
|
|
4383
|
+
wait "$pid" 2>/dev/null
|
|
4384
|
+
return 0
|
|
4385
|
+
fi
|
|
4386
|
+
sleep 1
|
|
4387
|
+
waited=$((waited + 1))
|
|
4388
|
+
done
|
|
4389
|
+
wait "$pid"
|
|
4390
|
+
}
|
|
4391
|
+
|
|
4392
|
+
out_file="$(mktemp -t weave-codex-toggle.XXXXXX)" || pass_through
|
|
4393
|
+
# shellcheck disable=SC2064 # expand out_file now; it never changes after this
|
|
4394
|
+
trap "rm -f '$out_file'" EXIT
|
|
4395
|
+
|
|
4396
|
+
# {{SCOPE}} is substituted at install time by the only code that knows whether
|
|
4397
|
+
# this install is user, project, or --dir scoped. It becomes literal argument
|
|
4398
|
+
# text (the installer printf %q-quotes any path), so there is nothing here to
|
|
4399
|
+
# word-split at runtime -- the braces are a template token, not an expansion.
|
|
4400
|
+
# shellcheck disable=SC1083
|
|
4401
|
+
run_bounded "$WEAVE_TOGGLE_TIMEOUT" "$out_file" \
|
|
4402
|
+
npx --package @weave-os/router -y -- weave-router "$toggle" --codex{{SCOPE}}
|
|
4403
|
+
status=$?
|
|
4404
|
+
|
|
4405
|
+
output="$(cat "$out_file" 2>/dev/null || true)"
|
|
4406
|
+
# Strip ANSI colour the installer emits when it thinks it has a terminal;
|
|
4407
|
+
# Codex renders `reason` as plain text.
|
|
4408
|
+
output="$(printf '%s' "$output" | sed -e 's/\x1b\[[0-9;]*m//g')"
|
|
4409
|
+
|
|
4410
|
+
# A failure is reported, not swallowed: passing through here would hand the
|
|
4411
|
+
# model a prompt the user meant as a command, and it would likely try the same
|
|
4412
|
+
# command again. Only a real timeout passes through -- the toggle never got to
|
|
4413
|
+
# finish, so the skill path is still the honest fallback.
|
|
4414
|
+
if [ "$WEAVE_TOGGLE_TIMED_OUT" -eq 1 ]; then
|
|
4415
|
+
pass_through
|
|
4416
|
+
fi
|
|
4417
|
+
|
|
4418
|
+
[ -n "$output" ] || output="weave-router $toggle --codex exited with status $status."
|
|
4419
|
+
|
|
4420
|
+
# Both trailers describe a change that actually happened, so they are gated on
|
|
4421
|
+
# a zero exit. A failed toggle rewrote no config; telling the user it takes
|
|
4422
|
+
# effect next launch would send them to restart Codex for nothing.
|
|
4423
|
+
if [ "$status" -eq 0 ] && [ "$verb" != "router-status" ]; then
|
|
4424
|
+
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."
|
|
4425
|
+
fi
|
|
4426
|
+
if [ "$status" -eq 0 ] && [ "$verb" = "disable-routing" ]; then
|
|
4427
|
+
output="$output"$'\n'"Reverse it with \$router-on."
|
|
4428
|
+
fi
|
|
4429
|
+
|
|
4430
|
+
block "$output"
|
|
4431
|
+
CODEX_TOGGLE_EOF
|
|
4432
|
+
directive_src="$embedded_src"
|
|
4433
|
+
fi
|
|
4434
|
+
if ! grep -Fq '<!-- weave-router managed codex directive -->' "$directive_src"; then
|
|
4435
|
+
warn "Codex directive hook has no ownership marker; skipping it. The \$router-on/off/status toggles still work as skills."
|
|
4436
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4437
|
+
return 0
|
|
4438
|
+
fi
|
|
4439
|
+
# A symlink at this path is never ours, whatever it points at and whether or
|
|
4440
|
+
# not the target carries our marker. Writing through one would let a
|
|
4441
|
+
# reinstall overwrite an arbitrary file outside the Codex directory, and a
|
|
4442
|
+
# DANGLING link is invisible to -e, so this test must be -L and must come
|
|
4443
|
+
# first. It skips rather than exits: refuse_if_symlink would abort the whole
|
|
4444
|
+
# installer, and this hook is optional -- the toggle skills still work.
|
|
4445
|
+
if [ -L "$codex_directive_file" ]; then
|
|
4446
|
+
warn "A symlink sits at $codex_directive_file; leaving it untouched and skipping the local-toggle hook. The \$router-on/off/status toggles still work as skills."
|
|
4447
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4448
|
+
return 0
|
|
4449
|
+
fi
|
|
4450
|
+
if [ "$scope" = "user" ] && [ -z "$install_dir" ]; then
|
|
4451
|
+
mkdir -p "$(dirname "$codex_directive_file")"
|
|
4452
|
+
fi
|
|
4453
|
+
if [ -e "$codex_directive_file" ] && { [ ! -f "$codex_directive_file" ] || ! grep -Fq '<!-- weave-router managed codex directive -->' "$codex_directive_file"; }; then
|
|
4454
|
+
warn "A user-owned Codex directive helper already exists at $codex_directive_file; leaving it untouched. The \$router-on/off/status toggles still work as skills."
|
|
4455
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4456
|
+
return 0
|
|
4457
|
+
fi
|
|
4458
|
+
|
|
4459
|
+
local scope_args=""
|
|
4460
|
+
if [ -n "$install_dir" ]; then
|
|
4461
|
+
scope_args=" --dir $(printf '%q' "$install_dir")"
|
|
4462
|
+
elif [ "$scope" = "project" ]; then
|
|
4463
|
+
scope_args=" --scope project"
|
|
4464
|
+
fi
|
|
4465
|
+
local body
|
|
4466
|
+
body="$(cat "$directive_src")" || { [ -n "$embedded_src" ] && rm -f "$embedded_src"; return 0; }
|
|
4467
|
+
[ -n "$embedded_src" ] && rm -f "$embedded_src"
|
|
4468
|
+
printf '%s\n' "${body//\{\{SCOPE\}\}/$scope_args}" >"$codex_directive_file" || return 0
|
|
4469
|
+
chmod 700 "$codex_directive_file"
|
|
4470
|
+
ok "Codex local-toggle hook installed at $codex_directive_file"
|
|
4471
|
+
}
|
|
4472
|
+
|
|
4167
4473
|
|
|
4168
4474
|
if [ "$target" = "codex" ]; then
|
|
4169
4475
|
if ! install_codex_status_script; then
|
|
4170
4476
|
err "Cannot install the Codex status helper safely; refusing to write hooks that could execute unowned code."
|
|
4171
4477
|
exit 1
|
|
4172
4478
|
fi
|
|
4479
|
+
# Before write_codex_config, which registers whatever this leaves on disk:
|
|
4480
|
+
# a registration pointing at a script that is not there yet makes Codex run
|
|
4481
|
+
# a missing command on every prompt. It never fails the install -- the
|
|
4482
|
+
# toggles fall back to their skills -- so its outcome is read off the file,
|
|
4483
|
+
# not off an exit status.
|
|
4484
|
+
install_codex_directive_script
|
|
4173
4485
|
write_codex_config "$codex_config_file" "$base_url" "$api_key" "$user_email" "$user_name"
|
|
4174
4486
|
ok "Codex config written to $codex_config_file"
|
|
4175
|
-
# Only once the registration is gone: a helper deleted while its registration
|
|
4176
|
-
# survives makes Codex run a missing command on every prompt.
|
|
4177
|
-
remove_codex_directive_helper
|
|
4178
4487
|
remove_obsolete_codex_prompt_wrappers "$codex_dir/prompts"
|
|
4179
4488
|
install_codex_prompt_skills
|
|
4180
4489
|
info "Codex router directives: begin the message with one space, e.g. ' /force-model gpt-5.6-terra'."
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weave-os/router",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.19",
|
|
4
4
|
"description": "One-command installer that points Claude Code, Codex, opencode, or pi at the Weave Router. For pi it also ships the routing extension, loaded via pi.extensions.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"weave-router": "bin.js",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"uninstall.sh",
|
|
21
21
|
"cc-statusline.sh",
|
|
22
22
|
"codex-status.sh",
|
|
23
|
+
"codex-toggle.sh",
|
|
23
24
|
"registry.sh",
|
|
24
25
|
"directives.tsv",
|
|
25
26
|
"commands/",
|