@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.
Files changed (69) hide show
  1. package/LICENSE +90 -0
  2. package/README.md +172 -0
  3. package/bin.js +68 -0
  4. package/cc-statusline.sh +951 -0
  5. package/codex-skills/disable-routing/SKILL.md +22 -0
  6. package/codex-skills/fm/SKILL.md +15 -0
  7. package/codex-skills/fm/scripts/emit.sh +8 -0
  8. package/codex-skills/force-model/SKILL.md +15 -0
  9. package/codex-skills/force-model/scripts/emit.sh +9 -0
  10. package/codex-skills/rf/SKILL.md +15 -0
  11. package/codex-skills/rf/scripts/emit.sh +7 -0
  12. package/codex-skills/router-feedback/SKILL.md +15 -0
  13. package/codex-skills/router-feedback/scripts/emit.sh +9 -0
  14. package/codex-skills/router-models/SKILL.md +51 -0
  15. package/codex-skills/router-off/SKILL.md +22 -0
  16. package/codex-skills/router-on/SKILL.md +22 -0
  17. package/codex-skills/router-status/SKILL.md +19 -0
  18. package/codex-skills/ufm/SKILL.md +14 -0
  19. package/codex-skills/ufm/scripts/emit.sh +3 -0
  20. package/codex-skills/unforce-model/SKILL.md +14 -0
  21. package/codex-skills/unforce-model/scripts/emit.sh +4 -0
  22. package/codex-status.sh +378 -0
  23. package/commands/beta.md +5 -0
  24. package/commands/fm.md +6 -0
  25. package/commands/force-model.md +6 -0
  26. package/commands/models.md +46 -0
  27. package/commands/rf.md +6 -0
  28. package/commands/router-feedback.md +6 -0
  29. package/commands/router-models.md +46 -0
  30. package/commands/router-off.md +10 -0
  31. package/commands/router-on.md +10 -0
  32. package/commands/router-session.md +15 -0
  33. package/commands/router-status.md +10 -0
  34. package/commands/ufm.md +5 -0
  35. package/commands/unforce-model.md +5 -0
  36. package/directives.tsv +13 -0
  37. package/install.sh +5092 -0
  38. package/opencode-weave/README.md +78 -0
  39. package/opencode-weave/package.json +18 -0
  40. package/opencode-weave/src/index.ts +655 -0
  41. package/package.json +79 -0
  42. package/pi-router/README.md +123 -0
  43. package/pi-router/package.json +25 -0
  44. package/pi-router/skills/install-lsps/SKILL.md +75 -0
  45. package/pi-router/skills/lsp-guide/SKILL.md +63 -0
  46. package/pi-router/src/beta.ts +21 -0
  47. package/pi-router/src/compaction.ts +188 -0
  48. package/pi-router/src/config.ts +293 -0
  49. package/pi-router/src/context-window.ts +12 -0
  50. package/pi-router/src/dispatch.ts +377 -0
  51. package/pi-router/src/force-model.ts +126 -0
  52. package/pi-router/src/index.ts +62 -0
  53. package/pi-router/src/lsp-broker.ts +255 -0
  54. package/pi-router/src/lsp-client.ts +435 -0
  55. package/pi-router/src/lsp-format.ts +230 -0
  56. package/pi-router/src/lsp-install.ts +215 -0
  57. package/pi-router/src/lsp-protocol.ts +128 -0
  58. package/pi-router/src/lsp-servers.ts +361 -0
  59. package/pi-router/src/lsp.ts +529 -0
  60. package/pi-router/src/metadata.ts +32 -0
  61. package/pi-router/src/pricing.generated.ts +91 -0
  62. package/pi-router/src/provider.ts +64 -0
  63. package/pi-router/src/routed-model.ts +182 -0
  64. package/pi-router/src/safety.ts +66 -0
  65. package/pi-router/src/savings.ts +191 -0
  66. package/pi-router/src/ui.ts +91 -0
  67. package/pi-router/src/wooly.ts +408 -0
  68. package/registry.sh +102 -0
  69. package/uninstall.sh +788 -0
package/uninstall.sh ADDED
@@ -0,0 +1,788 @@
1
+ #!/usr/bin/env bash
2
+ #
3
+ # Weave Router uninstaller for Claude Code, Codex, opencode, and pi.
4
+ #
5
+ # Default target is Claude Code: removes the env vars, statusLine, and local
6
+ # router auth that install.sh added; leaves the rest of settings.json
7
+ # untouched. Pass --codex to strip the managed [model_providers.weave]
8
+ # block (and the matching top-level `model_provider`) from Codex's
9
+ # config.toml — anything outside the markers is preserved. Pass --opencode
10
+ # to strip the `provider.weave` block (and the top-level `model` key when
11
+ # it points at the router) from opencode.json; other providers and user
12
+ # settings are preserved. Pass --pi to strip the `weave` provider from
13
+ # pi's models.json, drop @workweave/router from settings.json, revert the
14
+ # weave defaults, and remove the router key file.
15
+ #
16
+ # Usage:
17
+ # npx @workweave/router --uninstall # Claude Code, user scope
18
+ # npx @workweave/router --uninstall --codex # Codex, user scope
19
+ # npx @workweave/router --uninstall --opencode # opencode, user scope
20
+ # npx @workweave/router --uninstall --pi # pi, user scope
21
+ # npx @workweave/router --uninstall --scope project # run inside the repo
22
+ # npx @workweave/router --uninstall --dir /tmp/test # --dir alone (user scope, .weave/)
23
+ # npx @workweave/router --uninstall --scope project --dir /tmp # --dir + project scope (.claude/)
24
+
25
+ set -euo pipefail
26
+
27
+ scope="user"
28
+ scope_explicit="false"
29
+ install_dir=""
30
+ script_dir="$(cd "$(dirname "$0")" 2>/dev/null && pwd || true)"
31
+
32
+ # ---------- directive registry (embedded) ----------
33
+ #
34
+ # uninstall.sh is served standalone too (the npm bin runs it directly, and it
35
+ # can be piped), so it cannot rely on a sibling registry.sh. The data and
36
+ # helpers are embedded verbatim; install/tests/registry_test.sh asserts they
37
+ # never drift from the canonical files. Regenerate with `make embed-registry`.
38
+ WEAVE_REGISTRY_DATA=$(cat <<'WEAVE_REGISTRY_EOF'
39
+ # Weave Router directive registry
40
+ # canonical|aliases|capability|claude|codex|opencode|pi|cursor|adapter
41
+ # aliases are comma-separated; client columns are yes/no. adapter is the native asset kind.
42
+ force-model|fm|prompt|yes|yes|yes|yes|manual|command,skill
43
+ unforce-model|ufm|prompt|yes|yes|yes|yes|manual|command,skill
44
+ router-feedback|rf|prompt|yes|yes|yes|no|manual|command,skill
45
+ router-off||local-toggle|yes|yes|no|no|manual|command,skill
46
+ router-on||local-toggle|yes|yes|no|no|manual|command,skill
47
+ router-status||local-toggle|yes|yes|no|no|manual|command,skill
48
+ router-session||prompt|yes|no|no|no|manual|command
49
+ router-models|models|local-toggle|yes|yes|no|no|manual|command,skill
50
+ disable-routing||local-toggle|no|yes|no|no|manual|skill
51
+ beta||prompt|yes|no|no|yes|manual|command
52
+ WEAVE_REGISTRY_EOF
53
+ )
54
+
55
+ # >>> weave-router registry lib >>>
56
+ weave_registry_rows() {
57
+ if [ -n "${WEAVE_REGISTRY_DATA:-}" ]; then
58
+ printf '%s\n' "$WEAVE_REGISTRY_DATA" | awk -F '|' '!/^([[:space:]]*#|[[:space:]]*$)/ { print }'
59
+ return 0
60
+ fi
61
+ local registry="${WEAVE_REGISTRY_FILE:-}"
62
+ if [ -z "$registry" ]; then
63
+ local dir
64
+ dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd -P)" || return 1
65
+ registry="$dir/directives.tsv"
66
+ fi
67
+ [ -f "$registry" ] || return 1
68
+ awk -F '|' '!/^([[:space:]]*#|[[:space:]]*$)/ { print }' "$registry"
69
+ }
70
+
71
+ # weave_registry_names lists every canonical name and alias a client installs.
72
+ weave_registry_names() {
73
+ local target="$1" canonical aliases capability claude codex opencode pi cursor adapter
74
+ while IFS='|' read -r canonical aliases capability claude codex opencode pi cursor adapter; do
75
+ case "$target" in
76
+ claude) [ "$claude" = yes ] || continue ;; codex) [ "$codex" = yes ] || continue ;;
77
+ opencode) [ "$opencode" = yes ] || continue ;; pi) [ "$pi" = yes ] || continue ;;
78
+ cursor) [ "$cursor" = yes ] || continue ;;
79
+ esac
80
+ printf '%s\n' "$canonical"
81
+ [ -n "$aliases" ] && printf '%s\n' "$aliases" | tr ',' '\n'
82
+ done <<EOF
83
+ $(weave_registry_rows)
84
+ EOF
85
+ }
86
+
87
+ # weave_registry_skill_names lists the prompt directives a client exposes as a
88
+ # native skill. Local-config toggles are excluded: they mutate config this
89
+ # installer owns and are handled per target, not as a generic prompt.
90
+ weave_registry_skill_names() {
91
+ local target="$1" canonical aliases capability claude codex opencode pi cursor adapter
92
+ while IFS='|' read -r canonical aliases capability claude codex opencode pi cursor adapter; do
93
+ [ "$capability" = prompt ] || continue
94
+ case "$target" in
95
+ claude) [ "$claude" = yes ] || continue ;; codex) [ "$codex" = yes ] || continue ;;
96
+ opencode) [ "$opencode" = yes ] || continue ;; pi) [ "$pi" = yes ] || continue ;;
97
+ cursor) [ "$cursor" = yes ] || continue ;;
98
+ esac
99
+ printf '%s\n' "$canonical"
100
+ [ -n "$aliases" ] && printf '%s\n' "$aliases" | tr ',' '\n'
101
+ done <<EOF
102
+ $(weave_registry_rows)
103
+ EOF
104
+ }
105
+
106
+ # weave_registry_skill_assets lists every directive a client ships as a skill
107
+ # file, including local-config toggles such as Codex's disable-routing. Install
108
+ # and uninstall use this for file management; weave_registry_skill_names is the
109
+ # narrower prompt-only set used when generating prompt adapters.
110
+ weave_registry_skill_assets() {
111
+ local target="$1" canonical aliases capability claude codex opencode pi cursor adapter
112
+ while IFS='|' read -r canonical aliases capability claude codex opencode pi cursor adapter; do
113
+ case ",$adapter," in *,skill,*) ;; *) continue ;; esac
114
+ case "$target" in
115
+ claude) [ "$claude" = yes ] || continue ;; codex) [ "$codex" = yes ] || continue ;;
116
+ opencode) [ "$opencode" = yes ] || continue ;; pi) [ "$pi" = yes ] || continue ;;
117
+ cursor) [ "$cursor" = yes ] || continue ;;
118
+ esac
119
+ printf '%s\n' "$canonical"
120
+ [ -n "$aliases" ] && printf '%s\n' "$aliases" | tr ',' '\n'
121
+ done <<EOF
122
+ $(weave_registry_rows)
123
+ EOF
124
+ }
125
+
126
+ # weave_registry_canonical_for resolves a name or alias to its canonical
127
+ # directive, and fails when the name is not in the registry at all.
128
+ weave_registry_canonical_for() {
129
+ local wanted="$1" canonical aliases capability claude codex opencode pi cursor adapter alias
130
+ while IFS='|' read -r canonical aliases capability claude codex opencode pi cursor adapter; do
131
+ [ "$wanted" = "$canonical" ] && { printf '%s' "$canonical"; return 0; }
132
+ IFS=',' read -ra _aliases <<< "$aliases"
133
+ for alias in ${_aliases[@]+"${_aliases[@]}"}; do
134
+ [ "$wanted" = "$alias" ] && { printf '%s' "$canonical"; return 0; }
135
+ done
136
+ done <<EOF
137
+ $(weave_registry_rows)
138
+ EOF
139
+ return 1
140
+ }
141
+ # <<< weave-router registry lib <<<
142
+
143
+ target="claude"
144
+
145
+ err() { printf "\033[31merror:\033[0m %s\n" "$*" >&2; }
146
+ warn() { printf "\033[33mwarning:\033[0m %s\n" "$*" >&2; }
147
+ info() { printf "\033[36m==>\033[0m %s\n" "$*"; }
148
+ ok() { printf "\033[32m✓\033[0m %s\n" "$*"; }
149
+
150
+ # Refuse to write/delete through a symlink. Project scope reads paths from the
151
+ # user's git repo; a hostile checkout could ship `.claude/settings.json` (or
152
+ # `.claude/cc-statusline.sh`) as a symlink to e.g. `~/.ssh/authorized_keys`,
153
+ # and the uninstaller's `>` redirect or `rm` would silently follow that link.
154
+ refuse_if_symlink() {
155
+ local target="$1"
156
+ if [ -L "$target" ]; then
157
+ err "$target is a symlink (-> $(readlink "$target")). Refusing to operate on it."
158
+ exit 1
159
+ fi
160
+ }
161
+
162
+ while [ $# -gt 0 ]; do
163
+ case "$1" in
164
+ --scope)
165
+ scope="${2:-}"; shift 2
166
+ [ "$scope" = "user" ] || [ "$scope" = "project" ] || { err "--scope must be 'user' or 'project'"; exit 2; }
167
+ scope_explicit="true"
168
+ ;;
169
+ --dir)
170
+ install_dir="${2:-}"; shift 2
171
+ [ -n "$install_dir" ] || { err "--dir requires a path."; exit 2; }
172
+ ;;
173
+ --codex)
174
+ target="codex"; shift
175
+ ;;
176
+ --opencode)
177
+ target="opencode"; shift
178
+ ;;
179
+ --pi)
180
+ target="pi"; shift
181
+ ;;
182
+ --claude)
183
+ # No-op selector for symmetry with --codex / --opencode and install.sh's
184
+ # --claude. Lets `./install.sh --uninstall --claude` (which forwards
185
+ # remaining args here) succeed instead of hitting the unknown-flag
186
+ # catch-all.
187
+ target="claude"; shift
188
+ ;;
189
+ -h|--help)
190
+ # awk avoids GNU `head -n -<N>` (rejected by BSD head on macOS).
191
+ awk 'NR<2 { next } /^set -euo/ { exit } { sub(/^# ?/, ""); print }' "$0"
192
+ exit 0
193
+ ;;
194
+ *)
195
+ err "Unknown flag: $1. Run --help for usage."; exit 2
196
+ ;;
197
+ esac
198
+ done
199
+
200
+ if { [ "$target" = "claude" ] || [ "$target" = "opencode" ] || [ "$target" = "pi" ]; } && ! command -v jq >/dev/null 2>&1; then
201
+ err "jq is required for the $target uninstall path."
202
+ exit 1
203
+ fi
204
+
205
+ # Markers must stay in sync with install.sh. Keep verbatim.
206
+ WEAVE_CODEX_BEGIN_MARKER="# >>> weave-router managed (do not edit between markers) >>>"
207
+ WEAVE_CODEX_END_MARKER="# <<< weave-router managed <<<"
208
+
209
+ # strip_codex_block rewrites config.toml without the managed block and any
210
+ # top-level `model_provider = "weave"` that lived outside the markers (which
211
+ # can happen if the user copy-pasted our key into their own config). Other
212
+ # top-level model_provider values are preserved so we don't yank a user back
213
+ # into the OpenAI default when they meant to keep their own.
214
+ strip_codex_block() {
215
+ local config_file="$1"
216
+ local tmp; tmp="$(mktemp -t weave-codex-uninstall.XXXXXX)"
217
+ awk -v begin="$WEAVE_CODEX_BEGIN_MARKER" -v end="$WEAVE_CODEX_END_MARKER" '
218
+ $0 == begin { skip = 1; next }
219
+ $0 == end { skip = 0; next }
220
+ skip { next }
221
+ /^[[:space:]]*\[/ { in_section = 1 }
222
+ !in_section && /^[[:space:]]*model_provider[[:space:]]*=[[:space:]]*"weave"[[:space:]]*$/ { next }
223
+ { print }
224
+ ' "$config_file" >"$tmp"
225
+ mv "$tmp" "$config_file"
226
+ }
227
+
228
+ # ---------- opencode uninstall path ----------
229
+
230
+ if [ "$target" = "opencode" ]; then
231
+ # Resolve opencode_config_file based on scope/dir. Mirrors install.sh so
232
+ # an `install --opencode --scope project` is exactly reversed by an
233
+ # `uninstall --opencode --scope project`.
234
+ if [ -n "$install_dir" ]; then
235
+ install_dir="$(cd "$install_dir" 2>/dev/null && pwd || echo "$install_dir")"
236
+ opencode_dir="$install_dir"
237
+ refuse_if_symlink "$opencode_dir"
238
+ elif [ "$scope" = "user" ]; then
239
+ opencode_dir="${XDG_CONFIG_HOME:-$HOME/.config}/opencode"
240
+ else
241
+ # Project scope: same prompt + git-root fallback as install.sh.
242
+ project_dir=""
243
+ if [ "$scope_explicit" = "false" ] && [ -r /dev/tty ]; then
244
+ default_project_dir="$(pwd)"
245
+ printf "Project directory to uninstall from [default: %s]: " "$default_project_dir"
246
+ read -r project_dir_choice </dev/tty || project_dir_choice=""
247
+ project_dir="${project_dir_choice:-$default_project_dir}"
248
+ case "$project_dir" in
249
+ "~") project_dir="$HOME" ;;
250
+ "~/"*) project_dir="$HOME/${project_dir#~/}" ;;
251
+ esac
252
+ if [ ! -d "$project_dir" ]; then
253
+ err "Directory does not exist: $project_dir."
254
+ exit 1
255
+ fi
256
+ project_dir="$(cd "$project_dir" && pwd)"
257
+ fi
258
+ if [ -n "${project_dir:-}" ]; then
259
+ opencode_dir="$project_dir"
260
+ else
261
+ if ! git_root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
262
+ err "--scope project must be run inside a git repo, or use --dir <path>."
263
+ exit 1
264
+ fi
265
+ opencode_dir="$git_root"
266
+ fi
267
+ refuse_if_symlink "$opencode_dir"
268
+ fi
269
+ opencode_config_file="$opencode_dir/opencode.json"
270
+ refuse_if_symlink "$opencode_config_file"
271
+
272
+ # Canonicalize the plugin path exactly as install.sh did (`cd … && pwd`) so
273
+ # the `plugin` array entry matches on removal — a raw "$opencode_dir/…" string
274
+ # can differ (symlinks, trailing slash) and leave the entry behind.
275
+ if [ -d "$opencode_dir" ]; then
276
+ opencode_plugin="$(cd "$opencode_dir" && pwd)/.weave/opencode-weave.ts"
277
+ else
278
+ opencode_plugin="$opencode_dir/.weave/opencode-weave.ts"
279
+ fi
280
+ if [ -f "$opencode_config_file" ]; then
281
+ # Strip every managed provider (`weave`, the login-only `weave-claude`, and
282
+ # the legacy `weave-codex` from pre-upgrade installs), the managed plugin
283
+ # entry from the `plugin` array, and any router-pointing top-level model
284
+ # (the `weave/`, `weave-claude/`, and `weave-codex/` prefixes — otherwise a
285
+ # default survives and points at a deleted provider). Other providers,
286
+ # user-set models that don't reference the router, other plugins, and any
287
+ # unrelated keys are preserved.
288
+ cleaned="$(jq --arg plugin "$opencode_plugin" '
289
+ (if .provider.weave then del(.provider.weave) else . end)
290
+ | (if .provider["weave-claude"] then del(.provider["weave-claude"]) else . end)
291
+ | (if .provider["weave-codex"] then del(.provider["weave-codex"]) else . end)
292
+ | (if (.provider // {}) == {} then del(.provider) else . end)
293
+ | (if (.plugin | type) == "array" then .plugin -= [$plugin] else . end)
294
+ | (if (.plugin | type) == "array" and (.plugin | length) == 0 then del(.plugin) else . end)
295
+ | (if (.model // "" | tostring | (startswith("weave/") or startswith("weave-claude/") or startswith("weave-codex/"))) then del(.model) else . end)
296
+ ' "$opencode_config_file")"
297
+ printf '%s\n' "$cleaned" >"$opencode_config_file"
298
+
299
+ # If only the $schema marker remains (or the file is empty), drop the
300
+ # file entirely so we don't leave a one-key artifact.
301
+ remaining_keys="$(jq -r 'del(."$schema") | keys | length' "$opencode_config_file" 2>/dev/null || echo 0)"
302
+ if [ "$remaining_keys" = "0" ]; then
303
+ rm -f "$opencode_config_file"
304
+ ok "Removed empty $opencode_config_file"
305
+ else
306
+ ok "Cleaned $opencode_config_file"
307
+ fi
308
+ else
309
+ info "No opencode config at $opencode_config_file (already uninstalled?)"
310
+ fi
311
+
312
+ # Drop the bundled subscription plugin (no secrets; the config holds the key,
313
+ # opencode's own auth store holds the ChatGPT/Claude tokens). Remove the
314
+ # .weave/ dir only if it's left empty so we don't clobber an unrelated user dir.
315
+ if [ -f "$opencode_plugin" ]; then
316
+ refuse_if_symlink "$opencode_plugin"
317
+ rm -f "$opencode_plugin"
318
+ rmdir "$opencode_dir/.weave" 2>/dev/null || true
319
+ ok "Removed $opencode_plugin"
320
+ fi
321
+
322
+ # Drop the toggle parked sidecar (holds the parked router model when off).
323
+ opencode_parked="$opencode_dir/.weave-parked.json"
324
+ if [ -f "$opencode_parked" ]; then
325
+ refuse_if_symlink "$opencode_parked"
326
+ rm -f "$opencode_parked"
327
+ ok "Removed $opencode_parked"
328
+ fi
329
+
330
+ # Remove slash command wrapper files this installer owns. Install mirrors
331
+ # this split: project scope uses <repo>/.opencode/commands/, while user
332
+ # scope—including a user-style --dir install—uses the global XDG commands
333
+ # directory so opencode discovers the wrappers from any working directory.
334
+ remove_opencode_command_dir() {
335
+ local opencode_cmds_dir="$1" cmd cmd_file
336
+ if [ -d "$opencode_cmds_dir" ]; then
337
+ refuse_if_symlink "$opencode_cmds_dir"
338
+ while IFS= read -r cmd; do
339
+ cmd_file="$opencode_cmds_dir/$cmd.md"
340
+ if [ -f "$cmd_file" ]; then
341
+ if grep -Fq "<!-- weave-router managed command: $cmd -->" "$cmd_file"; then
342
+ refuse_if_symlink "$cmd_file"
343
+ rm -f "$cmd_file"
344
+ ok "Removed $cmd_file"
345
+ else
346
+ warn "Leaving user-owned opencode command at $cmd_file untouched."
347
+ fi
348
+ fi
349
+ done <<EOF
350
+ $(weave_registry_names opencode)
351
+ EOF
352
+ rmdir "$opencode_cmds_dir" 2>/dev/null || true
353
+ fi
354
+ }
355
+
356
+ if [ "$scope" = "project" ]; then
357
+ opencode_commands_dir="$opencode_dir/.opencode/commands"
358
+ else
359
+ opencode_commands_dir="${XDG_CONFIG_HOME:-$HOME/.config}/opencode/commands"
360
+ fi
361
+ remove_opencode_command_dir "$opencode_commands_dir"
362
+
363
+ # Also clean the two paths used by earlier installer revisions, but avoid
364
+ # visiting the canonical path twice. This keeps uninstall idempotent for
365
+ # existing project and --dir installations.
366
+ for legacy_dir in "$opencode_dir/commands" "$opencode_dir/.opencode/commands"; do
367
+ [ "$legacy_dir" = "$opencode_commands_dir" ] && continue
368
+ remove_opencode_command_dir "$legacy_dir"
369
+ done
370
+
371
+ if [ -n "$install_dir" ]; then
372
+ ok "Weave Router uninstalled from $install_dir (opencode)."
373
+ else
374
+ ok "Weave Router uninstalled (opencode, scope=$scope)."
375
+ fi
376
+ exit 0
377
+ fi
378
+
379
+ # ---------- pi uninstall path ----------
380
+
381
+ if [ "$target" = "pi" ]; then
382
+ # Resolve the pi agent dir based on scope/dir. Mirrors install.sh: user scope
383
+ # is pi's default ~/.pi/agent; project/--dir scope is a repo-local .pi.
384
+ if [ -n "$install_dir" ]; then
385
+ install_dir="$(cd "$install_dir" 2>/dev/null && pwd || echo "$install_dir")"
386
+ pi_dir="$install_dir/.pi"
387
+ refuse_if_symlink "$pi_dir"
388
+ elif [ "$scope" = "user" ]; then
389
+ pi_dir="$HOME/.pi/agent"
390
+ else
391
+ # Project scope: same prompt + git-root fallback as the opencode path.
392
+ project_dir=""
393
+ if [ "$scope_explicit" = "false" ] && [ -r /dev/tty ]; then
394
+ default_project_dir="$(pwd)"
395
+ printf "Project directory to uninstall from [default: %s]: " "$default_project_dir"
396
+ read -r project_dir_choice </dev/tty || project_dir_choice=""
397
+ project_dir="${project_dir_choice:-$default_project_dir}"
398
+ case "$project_dir" in
399
+ "~") project_dir="$HOME" ;;
400
+ "~/"*) project_dir="$HOME/${project_dir#~/}" ;;
401
+ esac
402
+ if [ ! -d "$project_dir" ]; then
403
+ err "Directory does not exist: $project_dir."
404
+ exit 1
405
+ fi
406
+ project_dir="$(cd "$project_dir" && pwd)"
407
+ fi
408
+ if [ -n "${project_dir:-}" ]; then
409
+ pi_dir="$project_dir/.pi"
410
+ else
411
+ if ! git_root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
412
+ err "--scope project must be run inside a git repo, or use --dir <path>."
413
+ exit 1
414
+ fi
415
+ pi_dir="$git_root/.pi"
416
+ fi
417
+ refuse_if_symlink "$pi_dir"
418
+ fi
419
+
420
+ pi_models_file="$pi_dir/models.json"
421
+ pi_settings_file="$pi_dir/settings.json"
422
+ pi_key_file="$pi_dir/.weave_router_key"
423
+ refuse_if_symlink "$pi_models_file"
424
+ refuse_if_symlink "$pi_settings_file"
425
+ refuse_if_symlink "$pi_key_file"
426
+
427
+ # models.json: drop provider.weave; remove the file if nothing else remains.
428
+ # Other providers/models the user added are preserved.
429
+ if [ -f "$pi_models_file" ]; then
430
+ cleaned="$(jq '
431
+ (if .providers.weave then del(.providers.weave) else . end)
432
+ | (if (.providers // {}) == {} then del(.providers) else . end)
433
+ ' "$pi_models_file")"
434
+ printf '%s\n' "$cleaned" >"$pi_models_file"
435
+ if [ "$(jq -r 'keys | length' "$pi_models_file" 2>/dev/null || echo 0)" = "0" ]; then
436
+ rm -f "$pi_models_file"
437
+ ok "Removed empty $pi_models_file"
438
+ else
439
+ ok "Cleaned $pi_models_file"
440
+ fi
441
+ else
442
+ info "No pi models config at $pi_models_file (already uninstalled?)"
443
+ fi
444
+
445
+ # settings.json: drop our package and revert defaults that still point at the
446
+ # router. Leaving defaultProvider="weave" after removing the provider would
447
+ # break pi startup, so reverting is the correct reverse of the install.
448
+ # defaultModel is reverted ONLY when defaultProvider was "weave" (the state
449
+ # install creates): install sets defaultModel only when it was empty, so a user
450
+ # who independently picked claude-sonnet-4-6 with their own provider keeps it.
451
+ if [ -f "$pi_settings_file" ]; then
452
+ cleaned="$(jq '
453
+ (if .packages then .packages -= ["npm:@workweave/router", "npm:@weave-os/router", "npm:@workweave/pi-router"] else . end)
454
+ | (if (.packages // []) == [] then del(.packages) else . end)
455
+ | (if .defaultProvider == "weave"
456
+ then del(.defaultProvider)
457
+ | (if .defaultModel == "claude-sonnet-4-6" then del(.defaultModel) else . end)
458
+ else . end)
459
+ ' "$pi_settings_file")"
460
+ printf '%s\n' "$cleaned" >"$pi_settings_file"
461
+ if [ "$(jq -r 'keys | length' "$pi_settings_file" 2>/dev/null || echo 0)" = "0" ]; then
462
+ rm -f "$pi_settings_file"
463
+ ok "Removed empty $pi_settings_file"
464
+ else
465
+ ok "Cleaned $pi_settings_file"
466
+ fi
467
+ else
468
+ info "No pi settings at $pi_settings_file (already uninstalled?)"
469
+ fi
470
+
471
+ if [ -f "$pi_key_file" ]; then
472
+ rm -f "$pi_key_file"
473
+ ok "Removed $pi_key_file"
474
+ fi
475
+
476
+ if [ -n "$install_dir" ]; then
477
+ ok "Weave Router uninstalled from $install_dir (pi)."
478
+ else
479
+ ok "Weave Router uninstalled (pi, scope=$scope)."
480
+ fi
481
+ exit 0
482
+ fi
483
+
484
+ # ---------- codex uninstall path ----------
485
+
486
+ if [ "$target" = "codex" ]; then
487
+ # Resolve codex_config_file based on scope/dir. Mirrors the install path so
488
+ # an `install --codex --scope project` is exactly reversed by an
489
+ # `uninstall --codex --scope project`.
490
+ if [ -n "$install_dir" ]; then
491
+ install_dir="$(cd "$install_dir" 2>/dev/null && pwd || echo "$install_dir")"
492
+ codex_dir="$install_dir/.codex"
493
+ refuse_if_symlink "$codex_dir"
494
+ elif [ "$scope" = "user" ]; then
495
+ codex_dir="$HOME/.codex"
496
+ else
497
+ # Project scope: same prompt + git-root fallback as install.sh.
498
+ project_dir=""
499
+ if [ "$scope_explicit" = "false" ] && [ -r /dev/tty ]; then
500
+ default_project_dir="$(pwd)"
501
+ printf "Project directory to uninstall from [default: %s]: " "$default_project_dir"
502
+ read -r project_dir_choice </dev/tty || project_dir_choice=""
503
+ project_dir="${project_dir_choice:-$default_project_dir}"
504
+ case "$project_dir" in
505
+ "~") project_dir="$HOME" ;;
506
+ "~/"*) project_dir="$HOME/${project_dir#~/}" ;;
507
+ esac
508
+ if [ ! -d "$project_dir" ]; then
509
+ err "Directory does not exist: $project_dir."
510
+ exit 1
511
+ fi
512
+ project_dir="$(cd "$project_dir" && pwd)"
513
+ fi
514
+ if [ -n "${project_dir:-}" ]; then
515
+ codex_dir="$project_dir/.codex"
516
+ else
517
+ if ! git_root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
518
+ err "--scope project must be run inside a git repo, or use --dir <path>."
519
+ exit 1
520
+ fi
521
+ codex_dir="$git_root/.codex"
522
+ fi
523
+ refuse_if_symlink "$codex_dir"
524
+ fi
525
+ codex_config_file="$codex_dir/config.toml"
526
+ if [ "$scope" = "user" ] && [ -z "$install_dir" ]; then
527
+ codex_status_file="$HOME/.weave/codex-status.sh"
528
+ else
529
+ codex_status_file="$codex_dir/weave-status.sh"
530
+ fi
531
+ codex_status_disabled_marker="$(dirname "$codex_status_file")/.weave-router-disabled"
532
+ refuse_if_symlink "$codex_config_file"
533
+ refuse_if_symlink "$codex_status_file"
534
+ refuse_if_symlink "$codex_status_disabled_marker"
535
+
536
+ if [ -f "$codex_config_file" ]; then
537
+ strip_codex_block "$codex_config_file"
538
+ # If the file now contains only whitespace/comments, leave it: the user
539
+ # may have other comments worth keeping. Truly empty files we delete so
540
+ # we don't leave a zero-byte artifact behind.
541
+ if [ ! -s "$codex_config_file" ]; then
542
+ rm -f "$codex_config_file"
543
+ ok "Removed empty $codex_config_file"
544
+ else
545
+ ok "Cleaned $codex_config_file"
546
+ fi
547
+ else
548
+ info "No Codex config at $codex_config_file (already uninstalled?)"
549
+ fi
550
+
551
+ # A user config with an existing [features] table receives the installer
552
+ # hook setting outside the managed block; remove only our tagged line.
553
+ if [ -f "$codex_config_file" ]; then
554
+ tmp="$(mktemp -t weave-codex-features-uninstall.XXXXXX)"
555
+ awk '$0 != "hooks = true # weave-router managed codex hooks" { print }' "$codex_config_file" >"$tmp"
556
+ mv "$tmp" "$codex_config_file"
557
+ fi
558
+
559
+ if [ -f "$codex_status_file" ]; then
560
+ if grep -Fq '<!-- weave-router managed codex status -->' "$codex_status_file"; then
561
+ rm -f "$codex_status_file"
562
+ rm -f "$codex_status_disabled_marker"
563
+ ok "Removed $codex_status_file"
564
+ rmdir "$(dirname "$codex_status_file")" 2>/dev/null || true
565
+ else
566
+ warn "Leaving user-owned Codex status helper at $codex_status_file untouched."
567
+ fi
568
+ fi
569
+
570
+ # Remove only the prompt files this installer owns; leave any user-authored
571
+ # entries in prompts/ alone. The dir itself is dropped only if empty after.
572
+ codex_prompts_dir="$codex_dir/prompts"
573
+ if [ -d "$codex_prompts_dir" ]; then
574
+ refuse_if_symlink "$codex_prompts_dir"
575
+ while IFS= read -r cmd; do
576
+ cmd_file="$codex_prompts_dir/$cmd.md"
577
+ if [ -f "$cmd_file" ]; then
578
+ refuse_if_symlink "$cmd_file"
579
+ rm -f "$cmd_file"
580
+ ok "Removed $cmd_file"
581
+ fi
582
+ done <<EOF
583
+ $(weave_registry_names codex)
584
+ EOF
585
+ rmdir "$codex_prompts_dir" 2>/dev/null || true
586
+ fi
587
+
588
+ # disable-routing is handled by the registry-driven loop below, alongside
589
+ # every other installer-owned skill.
590
+
591
+ # A symlinked skills/ or per-skill directory would let the marker check and
592
+ # the rm below reach a file outside the Codex tree entirely, so refuse the
593
+ # parents before touching anything beneath them — not just SKILL.md itself.
594
+ if [ -L "$codex_dir/skills" ]; then
595
+ warn "$codex_dir/skills is a symlink; leaving Codex skills untouched."
596
+ else
597
+ while IFS= read -r canonical; do
598
+ skill_dir="$codex_dir/skills/$canonical"
599
+ skill_file="$skill_dir/SKILL.md"
600
+ if [ -L "$skill_dir" ]; then
601
+ warn "$skill_dir is a symlink; leaving it untouched."
602
+ continue
603
+ fi
604
+ if [ -L "$skill_file" ]; then
605
+ warn "$skill_file is a symlink; leaving it untouched."
606
+ continue
607
+ fi
608
+ if [ -f "$skill_file" ]; then
609
+ if grep -Fq "<!-- weave-router managed $canonical skill -->" "$skill_file"; then
610
+ rm -f "$skill_file"
611
+ ok "Removed $skill_file"
612
+ # The prompt skills also ship scripts/emit.sh; leaving it behind
613
+ # strands a directory Codex still advertises as a skill.
614
+ emit_file="$skill_dir/scripts/emit.sh"
615
+ if [ ! -L "$skill_dir/scripts" ] && [ ! -L "$emit_file" ] && [ -f "$emit_file" ]; then
616
+ rm -f "$emit_file"
617
+ rmdir "$skill_dir/scripts" 2>/dev/null || true
618
+ fi
619
+ else
620
+ warn "Leaving user-owned Codex skill at $skill_file untouched."
621
+ fi
622
+ rmdir "$skill_dir" 2>/dev/null || true
623
+ fi
624
+ done <<EOF
625
+ $(weave_registry_skill_assets codex)
626
+ EOF
627
+ # Drop skills/ when nothing is left in it, matching how the prompts and
628
+ # Claude command paths clean up their now-empty directories. rmdir refuses
629
+ # a non-empty dir, so an unrelated user skill keeps it.
630
+ rmdir "$codex_dir/skills" 2>/dev/null || true
631
+ fi
632
+
633
+ if [ -n "$install_dir" ]; then
634
+ ok "Weave Router uninstalled from $install_dir (Codex)."
635
+ else
636
+ ok "Weave Router uninstalled (Codex, scope=$scope)."
637
+ fi
638
+ exit 0
639
+ fi
640
+
641
+ # ---------- claude uninstall path ----------
642
+
643
+ # Resolve the base directory. When --dir is given, use it directly.
644
+ if [ -n "$install_dir" ]; then
645
+ install_dir="$(cd "$install_dir" 2>/dev/null && pwd || echo "$install_dir")"
646
+ settings_file="$install_dir/.claude/settings.json"
647
+ local_settings_file=""
648
+ # --dir alone (scope defaults to "user") uses .weave/; --dir --scope project
649
+ # uses .claude/. Match the installer's scope-dependent statusline placement.
650
+ if [ "$scope" = "project" ]; then
651
+ statusline_file="$install_dir/.claude/cc-statusline.sh"
652
+ else
653
+ statusline_file="$install_dir/.weave/cc-statusline.sh"
654
+ fi
655
+ # Symlink containment: --dir paths come from a user-supplied directory that may
656
+ # be hostile. The later `>` redirect on settings_file and `rm -f` on the
657
+ # statusline script would otherwise follow links out of the directory.
658
+ refuse_if_symlink "$install_dir/.claude"
659
+ refuse_if_symlink "$settings_file"
660
+ refuse_if_symlink "$statusline_file"
661
+ elif [ "$scope" = "user" ]; then
662
+ settings_file="$HOME/.claude/settings.json"
663
+ local_settings_file=""
664
+ statusline_file="$HOME/.weave/cc-statusline.sh"
665
+ else
666
+ # Project scope without --dir: mirror install.sh — directory prompt only when
667
+ # scope_explicit is false (interactive install path); explicit --scope project
668
+ # uses the git root of CWD with no prompt.
669
+ project_dir=""
670
+ if [ "$scope_explicit" = "false" ] && [ -r /dev/tty ]; then
671
+ default_project_dir="$(pwd)"
672
+ printf "Project directory to uninstall from [default: %s]: " "$default_project_dir"
673
+ read -r project_dir_choice </dev/tty || project_dir_choice=""
674
+ project_dir="${project_dir_choice:-$default_project_dir}"
675
+ case "$project_dir" in
676
+ "~") project_dir="$HOME" ;;
677
+ "~/"*) project_dir="$HOME/${project_dir#~/}" ;;
678
+ esac
679
+ if [ ! -d "$project_dir" ]; then
680
+ err "Directory does not exist: $project_dir."
681
+ exit 1
682
+ fi
683
+ project_dir="$(cd "$project_dir" && pwd)"
684
+ fi
685
+ if [ -n "${project_dir:-}" ]; then
686
+ settings_base="$project_dir"
687
+ else
688
+ if ! git_root="$(git rev-parse --show-toplevel 2>/dev/null)"; then
689
+ err "--scope project must be run inside a git repo, or use --dir <path>."
690
+ exit 1
691
+ fi
692
+ settings_base="$git_root"
693
+ fi
694
+ settings_file="$settings_base/.claude/settings.json"
695
+ local_settings_file="$settings_base/.claude/settings.local.json"
696
+ statusline_file="$settings_base/.claude/cc-statusline.sh"
697
+ # Symlink containment: paths come from a git repo or user-supplied directory
698
+ # that may be hostile. The later `>` redirect on settings_file and `rm -f` on
699
+ # the scripts would otherwise follow links out of the repo.
700
+ refuse_if_symlink "$settings_base/.claude"
701
+ refuse_if_symlink "$settings_file"
702
+ refuse_if_symlink "$local_settings_file"
703
+ refuse_if_symlink "$statusline_file"
704
+ fi
705
+
706
+ if [ -f "$settings_file" ]; then
707
+ # Only remove keys we actually installed: scrub our env vars, and only
708
+ # delete `statusLine` / `apiKeyHelper` when they point at scripts this
709
+ # installer used in older versions. Otherwise an unrelated user-configured
710
+ # statusLine or apiKeyHelper would be silently clobbered.
711
+ cleaned="$(jq '
712
+ if .env then
713
+ .env |= (del(.ANTHROPIC_BASE_URL, .ANTHROPIC_AUTH_TOKEN, .ANTHROPIC_CUSTOM_HEADERS, .ENABLE_TOOL_SEARCH))
714
+ | (if (.env | length) == 0 then del(.env) else . end)
715
+ else . end
716
+ | (if (.statusLine.command // "" | tostring | endswith("cc-statusline.sh"))
717
+ then del(.statusLine) else . end)
718
+ | (if (.apiKeyHelper // "" | tostring | endswith("weave-key.sh"))
719
+ then del(.apiKeyHelper) else . end)
720
+ | (if ((.attribution.commit == "Co-Authored-By: Weave Router <router@workweave.ai>"
721
+ or .attribution.commit == "Co-Authored-By: Weave Router <noreply@workweave.ai>")
722
+ and .attribution.pr == "🤖 Generated with [Weave Router](https://router.workweave.ai)")
723
+ then del(.attribution) else . end)
724
+ ' "$settings_file")"
725
+ printf '%s\n' "$cleaned" >"$settings_file"
726
+ ok "Cleaned $settings_file"
727
+ else
728
+ info "No settings file at $settings_file (already uninstalled?)"
729
+ fi
730
+
731
+ if [ -n "$local_settings_file" ] && [ -f "$local_settings_file" ]; then
732
+ # ANTHROPIC_BASE_URL only lives here when a project install was toggled off
733
+ # (the off path overrides it to Anthropic in the local file). Project installs
734
+ # also keep WEAVE_ROUTER_BASE_URL here as a private endpoint marker beside the
735
+ # router key; scrub both so uninstall fully reverts the local config.
736
+ cleaned="$(jq '
737
+ if .env then
738
+ .env |= (del(.ANTHROPIC_BASE_URL, .ANTHROPIC_AUTH_TOKEN, .ANTHROPIC_CUSTOM_HEADERS, .ENABLE_TOOL_SEARCH, .WEAVE_ROUTER_BASE_URL))
739
+ | (if (.env | length) == 0 then del(.env) else . end)
740
+ else . end
741
+ | del(.apiKeyHelper)
742
+ ' "$local_settings_file")"
743
+ printf '%s\n' "$cleaned" >"$local_settings_file"
744
+ ok "Cleaned $local_settings_file"
745
+ fi
746
+
747
+ # Drop the toggle parked sidecar (carries the router key header when off).
748
+ parked_file="$(dirname "$settings_file")/.weave-parked.json"
749
+ if [ -f "$parked_file" ]; then
750
+ refuse_if_symlink "$parked_file"
751
+ rm -f "$parked_file"
752
+ ok "Removed $parked_file"
753
+ fi
754
+
755
+ if [ -f "$statusline_file" ]; then
756
+ rm -f "$statusline_file"
757
+ ok "Removed $statusline_file"
758
+ fi
759
+
760
+ # Remove only the slash command files this installer owns; leave any other
761
+ # files in commands/ alone. The directory itself stays if it still contains
762
+ # unrelated user commands.
763
+ commands_dir="$(dirname "$settings_file")/commands"
764
+ if [ -d "$commands_dir" ]; then
765
+ refuse_if_symlink "$commands_dir"
766
+ while IFS= read -r cmd; do
767
+ cmd_file="$commands_dir/$cmd.md"
768
+ if [ -f "$cmd_file" ]; then
769
+ refuse_if_symlink "$cmd_file"
770
+ if grep -Fq "<!-- weave-router managed command: $cmd -->" "$cmd_file"; then
771
+ rm -f "$cmd_file"
772
+ ok "Removed $cmd_file"
773
+ else
774
+ warn "Leaving user-owned Claude command at $cmd_file untouched."
775
+ fi
776
+ fi
777
+ done <<EOF
778
+ $(weave_registry_names claude)
779
+ EOF
780
+ # Clean up the dir only if we left nothing behind.
781
+ rmdir "$commands_dir" 2>/dev/null || true
782
+ fi
783
+
784
+ if [ -n "$install_dir" ]; then
785
+ ok "Weave Router uninstalled from $install_dir."
786
+ else
787
+ ok "Weave Router uninstalled (scope=$scope)."
788
+ fi