@workweave/router 0.1.8 → 0.2.1

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/install.sh CHANGED
@@ -1,35 +1,42 @@
1
1
  #!/usr/bin/env bash
2
2
  #
3
- # Weave Router installer for Claude Code, Codex, and opencode.
3
+ # Weave Router installer for Claude Code, Codex, opencode, and pi.
4
4
  #
5
- # Configures Claude Code (default), the OpenAI Codex CLI (`--codex`), or
6
- # opencode (`--opencode`) to permanently route through the Weave Router.
7
- # For Claude Code this writes the router base URL, router auth header,
8
- # and a status line into Claude Code's settings.json. For Codex it writes
9
- # a `model_providers.weave` entry plus `model_provider = "weave"` into
5
+ # Configures Claude Code (default), the OpenAI Codex CLI (`--codex`),
6
+ # opencode (`--opencode`), or pi (`--pi`) to permanently route through the
7
+ # Weave Router. For Claude Code this writes the router base URL, router auth
8
+ # header, and a status line into Claude Code's settings.json. For Codex it
9
+ # writes a `model_providers.weave` entry plus `model_provider = "weave"` into
10
10
  # ~/.codex/config.toml (managed block delimited by markers). For opencode
11
11
  # it merges a `provider.weave` block (anthropic-compatible) into
12
12
  # opencode.json — since the file is JSON, install/uninstall are structural
13
- # (jq) rather than marker-delimited.
13
+ # (jq) rather than marker-delimited. For pi it merges a `weave` provider into
14
+ # ~/.pi/agent/models.json, sets it as the default in settings.json, and adds
15
+ # the @workweave/router extension (which also adds a parallel subagent
16
+ # `dispatch` tool) — all structural (jq) merges.
14
17
  #
15
18
  # Two scopes (apply to all targets):
16
19
  # - user (default): ~/.claude/settings.json + ~/.weave/cc-statusline.sh
17
20
  # ~/.codex/config.toml (with --codex)
18
21
  # ~/.config/opencode/opencode.json (with --opencode)
22
+ # ~/.pi/agent/{models,settings}.json (with --pi)
19
23
  # - project: <repo>/.claude/settings.json + <repo>/.claude/cc-statusline.sh
20
24
  # <repo>/.codex/config.toml (with --codex)
21
25
  # <repo>/opencode.json (with --opencode)
26
+ # <repo>/.pi/ (run: PI_CODING_AGENT_DIR=<repo>/.pi pi) (with --pi)
22
27
  #
23
28
  # Or pass --dir to install into any directory:
24
29
  # - dir: <dir>/.claude/settings.json + <dir>/.claude/cc-statusline.sh
25
30
  # <dir>/.codex/config.toml (with --codex)
26
31
  # <dir>/opencode.json (with --opencode)
32
+ # <dir>/.pi/ (run: PI_CODING_AGENT_DIR=<dir>/.pi pi) (with --pi)
27
33
  #
28
34
  # Usage:
29
35
  # npx @workweave/router # interactive picker (Claude Code, Codex, opencode)
30
36
  # npx @workweave/router --claude # skip the picker, target Claude Code
31
37
  # npx @workweave/router --codex # skip the picker, target the OpenAI Codex CLI
32
38
  # npx @workweave/router --opencode # skip the picker, target opencode
39
+ # npx @workweave/router --pi # skip the picker, target pi
33
40
  # npx @workweave/router --scope project # commit-with-team install
34
41
  # npx @workweave/router --dir /tmp/my-sandbox # isolated throwaway install
35
42
  # npx @workweave/router --local # local router on localhost:8080
@@ -124,6 +131,7 @@ uninstall_cmd() {
124
131
  case "$target" in
125
132
  codex) cmd="$cmd --codex" ;;
126
133
  opencode) cmd="$cmd --opencode" ;;
134
+ pi) cmd="$cmd --pi" ;;
127
135
  esac
128
136
  [ "$scope" = "project" ] && cmd="$cmd --scope project"
129
137
  [ -n "$install_dir" ] && cmd="$cmd --dir $(printf '%q' "$install_dir")"
@@ -149,6 +157,7 @@ print_banner() {
149
157
  case "$target" in
150
158
  codex) target_label="Codex installer" ;;
151
159
  opencode) target_label="opencode installer" ;;
160
+ pi) target_label="pi installer" ;;
152
161
  *) target_label="Claude Code installer" ;;
153
162
  esac
154
163
  printf '\n'
@@ -567,6 +576,12 @@ write_opencode_config() {
567
576
  # a startup error before the router ever sees a request. The router itself
568
577
  # ignores the value (auth runs off X-Weave-Router-Key); apiKey here is just
569
578
  # a placeholder that satisfies the SDK's "is auth configured" check.
579
+ # baseURL KEEPS its /v1 here — do NOT align it with the pi block, which is
580
+ # root. opencode's @ai-sdk/anthropic (Vercel) appends only /messages to
581
+ # baseURL (its default is api.anthropic.com/v1), so /v1 yields the correct
582
+ # /v1/messages. Dropping it would hit /messages and 404. (pi uses the official
583
+ # @anthropic-ai/sdk, which appends /v1/messages to a root baseURL — opposite
584
+ # convention.) Verified by install/pi-router/test/opencode_smoke.sh.
570
585
  local block
571
586
  block="$(jq -n \
572
587
  --arg url "$block_url/v1" \
@@ -611,6 +626,118 @@ write_opencode_config() {
611
626
  chmod 600 "$config_file"
612
627
  }
613
628
 
629
+ # write_pi_models_config merges a managed `weave` provider into pi's
630
+ # models.json (anthropic-compatible — the router speaks Anthropic Messages
631
+ # natively). The header set carries identity plus the main-loop routing knobs
632
+ # (quality bias); the @workweave/router extension re-registers the provider
633
+ # per process to flip those knobs for subagents/compaction. apiKey is the
634
+ # router key as well as a header — pi treats apiKey as required to consider auth
635
+ # configured, but the router authenticates off X-Weave-Router-Key
636
+ # (authHeader:false keeps Authorization free for BYOK). Re-running rewrites
637
+ # `.providers.weave` in place; uninstall strips it. chmod 600 — holds the key.
638
+ #
639
+ # Usage: write_pi_models_config <models_file> <base_url> <api_key> [user_email] [user_name]
640
+ write_pi_models_config() {
641
+ local config_file="$1"
642
+ local block_url="$2"
643
+ local block_key="$3"
644
+ local block_email="${4:-}"
645
+ local block_name="${5:-}"
646
+
647
+ # Identity + the main-loop (quality) routing knobs. Built piecewise so an
648
+ # empty email/name vanishes from the JSON entirely.
649
+ local headers_json
650
+ headers_json="$(jq -n \
651
+ --arg key "$block_key" \
652
+ --arg email "$block_email" \
653
+ --arg name "$block_name" '
654
+ {
655
+ "X-Weave-Router-Key": $key,
656
+ "X-App": "pi",
657
+ "x-weave-routing-marker": "off",
658
+ "x-weave-routing-alpha": "0.8",
659
+ "x-weave-routing-speed-weight": "0.05",
660
+ "x-weave-routing-output-cost-ratio": "0.5",
661
+ "x-weave-routing-expected-output-tokens": "3000"
662
+ }
663
+ | (if $email != "" then . + {"X-Weave-User-Email": $email} else . end)
664
+ | (if $name != "" then . + {"X-Weave-User-Name": $name } else . end)
665
+ ')"
666
+
667
+ # Headline models surfaced in pi's /model picker. The router re-routes every
668
+ # request regardless, so this list is UX; keep it Anthropic-shaped and in
669
+ # sync with @workweave/router's WEAVE_MODELS constant.
670
+ #
671
+ # baseUrl is the router ROOT (no /v1): pi's anthropic-messages provider uses
672
+ # @anthropic-ai/sdk, which appends /v1/messages itself. Unlike the codex block
673
+ # above (OpenAI-style, base ends in /v1), a /v1 suffix here would produce
674
+ # /v1/v1/messages and 404.
675
+ local block
676
+ block="$(jq -n \
677
+ --arg url "$block_url" \
678
+ --arg key "$block_key" \
679
+ --argjson headers "$headers_json" '
680
+ {
681
+ baseUrl: $url,
682
+ api: "anthropic-messages",
683
+ apiKey: $key,
684
+ authHeader: false,
685
+ headers: $headers,
686
+ models: [
687
+ { id: "claude-opus-4-8", name: "Claude Opus 4.8 (via Weave Router)", reasoning: true, input: ["text","image"], contextWindow: 200000, maxTokens: 64000 },
688
+ { id: "claude-opus-4-7", name: "Claude Opus 4.7 (via Weave Router)", reasoning: true, input: ["text","image"], contextWindow: 200000, maxTokens: 64000 },
689
+ { id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6 (via Weave Router)", reasoning: true, input: ["text","image"], contextWindow: 200000, maxTokens: 64000 },
690
+ { id: "claude-haiku-4-5", name: "Claude Haiku 4.5 (via Weave Router)", reasoning: true, input: ["text","image"], contextWindow: 200000, maxTokens: 32000 }
691
+ ]
692
+ }
693
+ ')"
694
+
695
+ # Overwrite provider.weave only; leave any other providers/models the user
696
+ # added untouched.
697
+ local merged
698
+ if [ -f "$config_file" ]; then
699
+ merged="$(jq --argjson block "$block" '.providers = ((.providers // {}) | .weave = $block)' "$config_file")"
700
+ else
701
+ merged="$(jq -n --argjson block "$block" '{ providers: { weave: $block } }')"
702
+ fi
703
+ printf '%s\n' "$merged" >"$config_file"
704
+ # 0600: the headers + apiKey hold the router key.
705
+ chmod 600 "$config_file"
706
+ }
707
+
708
+ # write_pi_settings_config makes the `weave` provider pi's default and loads the
709
+ # @workweave/router extension. defaultProvider is always set to "weave" — the
710
+ # installer's job is to route via Weave; uninstall reverts it. defaultModel is set
711
+ # only when unset (don't clobber a user's model pick). The npm package source is
712
+ # appended to `packages` idempotently — pi auto-installs missing packages on
713
+ # startup — and the legacy `npm:@workweave/pi-router` id (from before the
714
+ # extension was folded into @workweave/router) is dropped so a config from the
715
+ # old layout can't keep a dangling/duplicate entry. No secret lives here, so no
716
+ # chmod 600.
717
+ #
718
+ # Usage: write_pi_settings_config <settings_file>
719
+ write_pi_settings_config() {
720
+ local settings_file="$1"
721
+ local pkg="npm:@workweave/router"
722
+ local merged
723
+ if [ -f "$settings_file" ]; then
724
+ merged="$(jq --arg pkg "$pkg" '
725
+ (.packages //= [])
726
+ | (.packages -= ["npm:@workweave/pi-router"])
727
+ | (if (.packages | index($pkg)) then . else .packages += [$pkg] end)
728
+ | .defaultProvider = "weave"
729
+ | (if (.defaultModel // "") == "" then .defaultModel = "claude-sonnet-4-6" else . end)
730
+ ' "$settings_file")"
731
+ else
732
+ merged="$(jq -n --arg pkg "$pkg" '{
733
+ defaultProvider: "weave",
734
+ defaultModel: "claude-sonnet-4-6",
735
+ packages: [$pkg]
736
+ }')"
737
+ fi
738
+ printf '%s\n' "$merged" >"$settings_file"
739
+ }
740
+
614
741
  # resolve_user_name mirrors resolve_user_email but for display name. Priority:
615
742
  # WEAVE_USER_NAME env override → git config user.name → empty. We don't
616
743
  # prompt for name independently: if email prompting yielded nothing, name
@@ -716,6 +843,9 @@ while [ $# -gt 0 ]; do
716
843
  --opencode)
717
844
  target="opencode"; target_explicit="true"; shift
718
845
  ;;
846
+ --pi)
847
+ target="pi"; target_explicit="true"; shift
848
+ ;;
719
849
  --claude)
720
850
  # No-op selector for symmetry with --codex / --opencode. Useful in
721
851
  # pipelines that want to skip the interactive picker without depending
@@ -747,6 +877,14 @@ if [ "$mode" != "install" ]; then
747
877
  fi
748
878
  fi
749
879
 
880
+ # Toggle verbs (off/on/status) aren't implemented for pi — its config is a
881
+ # structural models.json/settings.json merge, reversed by the uninstaller
882
+ # rather than a single env/key line we can park and restore.
883
+ if [ "$mode" != "install" ] && [ "$target" = "pi" ]; then
884
+ err "toggle verbs (off/on/status) aren't supported for --pi. Use 'npx @workweave/router --uninstall --pi' to remove, or re-run the installer to refresh."
885
+ exit 2
886
+ fi
887
+
750
888
  if [ -z "$base_url" ]; then
751
889
  base_url="$DEFAULT_BASE_URL"
752
890
  fi
@@ -766,12 +904,14 @@ if [ "$target_explicit" = "false" ] && [ "$non_interactive" = "false" ] && [ -r
766
904
  printf " %s1)%s Claude Code %s— patches ~/.claude/settings.json (or <repo>/.claude/)%s\n" "$C_BRAND" "$C_RESET" "$C_DIM" "$C_RESET"
767
905
  printf " %s2)%s Codex %s— patches ~/.codex/config.toml (or <repo>/.codex/)%s\n" "$C_BRAND" "$C_RESET" "$C_DIM" "$C_RESET"
768
906
  printf " %s3)%s opencode %s— patches ~/.config/opencode/opencode.json (or <repo>/opencode.json)%s\n" "$C_BRAND" "$C_RESET" "$C_DIM" "$C_RESET"
769
- printf "Choose %s[1/2/3]%s (default %s1%s): " "$C_BOLD" "$C_RESET" "$C_BOLD" "$C_RESET"
907
+ printf " %s4)%s pi %s— patches ~/.pi/agent/models.json + settings.json (or <repo>/.pi/)%s\n" "$C_BRAND" "$C_RESET" "$C_DIM" "$C_RESET"
908
+ printf "Choose %s[1/2/3/4]%s (default %s1%s): " "$C_BOLD" "$C_RESET" "$C_BOLD" "$C_RESET"
770
909
  read -r target_choice </dev/tty || target_choice=""
771
910
  case "${target_choice:-1}" in
772
911
  1|""|claude|c|C) target="claude" ;;
773
912
  2|codex|x|X) target="codex" ;;
774
913
  3|opencode|o|O) target="opencode" ;;
914
+ 4|pi|p|P) target="pi" ;;
775
915
  *) err "invalid choice: $target_choice"; exit 2 ;;
776
916
  esac
777
917
  fi
@@ -809,6 +949,11 @@ if [ -z "$install_dir" ] && [ "$scope_explicit" = "false" ] && [ "$non_interacti
809
949
  scope_project_path="<repo>/opencode.json"
810
950
  scope_cli_label="opencode"
811
951
  ;;
952
+ pi)
953
+ scope_user_path="~/.pi/agent/"
954
+ scope_project_path="<repo>/.pi/"
955
+ scope_cli_label="pi"
956
+ ;;
812
957
  *)
813
958
  scope_user_path="~/.claude/"
814
959
  scope_project_path="<repo>/.claude/"
@@ -859,7 +1004,7 @@ fi
859
1004
  # Claude Code's settings.json and opencode's opencode.json patching both use
860
1005
  # jq to deep-merge / structurally rewrite JSON. Toggling those clients reads
861
1006
  # and rewrites the same JSON, so jq is required there too.
862
- if [ "$target" = "claude" ] || [ "$target" = "opencode" ]; then
1007
+ if [ "$target" = "claude" ] || [ "$target" = "opencode" ] || [ "$target" = "pi" ]; then
863
1008
  require_cmd jq "macOS: 'brew install jq' · Debian/Ubuntu: 'sudo apt install jq'"
864
1009
  fi
865
1010
  # curl is only used by the install path's health/validate probes; toggles never
@@ -885,6 +1030,12 @@ case "$target" in
885
1030
  warn "Continuing — opencode.json will be written and will take effect once opencode is installed."
886
1031
  fi
887
1032
  ;;
1033
+ pi)
1034
+ if ! command -v pi >/dev/null 2>&1; then
1035
+ warn "'pi' not found on PATH. Install with 'npm install -g @mariozechner/pi-coding-agent', then re-run this script."
1036
+ warn "Continuing — models.json/settings.json will be written and take effect once pi is installed."
1037
+ fi
1038
+ ;;
888
1039
  esac
889
1040
 
890
1041
  script_dir="$(cd "$(dirname "$0")" 2>/dev/null && pwd || true)"
@@ -970,6 +1121,31 @@ elif [ "$target" = "codex" ]; then
970
1121
  fi
971
1122
 
972
1123
  mkdir -p "$codex_dir"
1124
+ elif [ "$target" = "pi" ]; then
1125
+ # pi reads ~/.pi/agent/ by default, so a plain `pi` picks up a user-scope
1126
+ # install with no env var. models.json is global-only in pi (there is no
1127
+ # project-level models file), so for project/--dir scope we point
1128
+ # PI_CODING_AGENT_DIR at a repo-local .pi that holds the whole config
1129
+ # (models.json + settings.json + key) — the same shape as Codex's CODEX_HOME.
1130
+ # The router key is embedded, so .pi goes in .gitignore for project scope.
1131
+ case "$scope" in
1132
+ user) pi_dir="$settings_base/.pi/agent" ;;
1133
+ project) pi_dir="$settings_base/.pi" ;;
1134
+ esac
1135
+ # --dir is a self-contained sandbox: flat .pi, launched via PI_CODING_AGENT_DIR.
1136
+ [ -n "$install_dir" ] && pi_dir="$install_dir/.pi"
1137
+ pi_models_file="$pi_dir/models.json"
1138
+ pi_settings_file="$pi_dir/settings.json"
1139
+ pi_key_file="$pi_dir/.weave_router_key"
1140
+
1141
+ if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
1142
+ refuse_if_symlink "$pi_dir"
1143
+ refuse_if_symlink "$pi_models_file"
1144
+ refuse_if_symlink "$pi_settings_file"
1145
+ refuse_if_symlink "$pi_key_file"
1146
+ fi
1147
+
1148
+ mkdir -p "$pi_dir"
973
1149
  else
974
1150
  # opencode discovers config in this order: $XDG_CONFIG_HOME/opencode/opencode.json
975
1151
  # (or ~/.config/opencode/opencode.json) for user scope, and opencode.json /
@@ -1581,6 +1757,74 @@ if [ "$target" = "opencode" ]; then
1581
1757
  exit 0
1582
1758
  fi
1583
1759
 
1760
+ # ---------- pi install path (dispatch + exit before the Claude-only writes) ----------
1761
+
1762
+ if [ "$target" = "pi" ]; then
1763
+ write_pi_models_config "$pi_models_file" "$base_url" "$api_key" "$user_email" "$user_name"
1764
+ ok "pi models config written to $pi_models_file"
1765
+ write_pi_settings_config "$pi_settings_file"
1766
+ ok "pi settings written to $pi_settings_file (provider weave + @workweave/router)"
1767
+
1768
+ if [ -n "$api_key" ]; then
1769
+ printf '%s\n' "$api_key" >"$pi_key_file"
1770
+ chmod 600 "$pi_key_file"
1771
+ ok "Router key written to $pi_key_file"
1772
+ fi
1773
+
1774
+ # Project scope: the repo-local .pi carries the router key, so keep it out of
1775
+ # git. Same reasoning as the Codex/opencode paths — base URL is shared, the
1776
+ # key is per-person.
1777
+ if [ "$scope" = "project" ] && [ -z "$install_dir" ] && [ -n "${git_root:-}" ]; then
1778
+ # Write the .gitignore in the directory that CONTAINS .pi (the chosen project
1779
+ # dir), not the git root: gitignore entries with a slash are anchored to the
1780
+ # .gitignore's own location, so a root-level ".pi/models.json" would NOT match
1781
+ # a nested <subdir>/.pi/ — leaking the router key. dirname "$pi_dir" == the
1782
+ # project dir (== git root when they're the same).
1783
+ gitignore="$(dirname "$pi_dir")/.gitignore"
1784
+ refuse_if_symlink "$gitignore"
1785
+ for entry in \
1786
+ ".pi/models.json" \
1787
+ ".pi/settings.json" \
1788
+ ".pi/.weave_router_key"
1789
+ do
1790
+ if [ ! -f "$gitignore" ] || ! grep -qxF "$entry" "$gitignore"; then
1791
+ printf '%s\n' "$entry" >>"$gitignore"
1792
+ fi
1793
+ done
1794
+ ok "Updated $gitignore (ignored repo-local .pi router config)"
1795
+ fi
1796
+
1797
+ # Post-install verification: same probes the Claude/Codex/opencode paths run.
1798
+ if [ "$quiet" != "true" ]; then
1799
+ if ! spin "Pinging $base_url/health" curl -fsS --max-time 5 "$base_url/health"; then
1800
+ warn "Could not reach $base_url/health within 5s. Settings are written; verify the router is running."
1801
+ fi
1802
+ fi
1803
+
1804
+ if [ -n "$api_key" ]; then
1805
+ validate_pi_key() {
1806
+ printf '%s: %s\n' "$router_key_header" "$api_key" \
1807
+ | curl -fsS --max-time 5 --header @- "$base_url/validate"
1808
+ }
1809
+ if ! spin "Validating API key" validate_pi_key; then
1810
+ warn "Router rejected the API key (check it matches the dashboard at $base_url/ui/)."
1811
+ fi
1812
+ fi
1813
+
1814
+ printf "\n"
1815
+ printf "%s✓%s %s%sWeave Router installed for pi.%s\n" \
1816
+ "$C_GREEN" "$C_RESET" "$C_BOLD" "$C_BRAND" "$C_RESET"
1817
+ # Billing note: pi normally draws on a Claude subscription (OAuth); routing
1818
+ # through the router switches to per-token billing on the router deployment
1819
+ # key (or BYOK). Surface it at install so the change isn't a surprise.
1820
+ info "pi now bills per token on the Weave Router key, not your Claude subscription."
1821
+ if [ "$scope" = "project" ] || [ -n "$install_dir" ]; then
1822
+ info "Run pi with PI_CODING_AGENT_DIR=$pi_dir pi so it picks up this config."
1823
+ fi
1824
+ print_uninstall_hint
1825
+ exit 0
1826
+ fi
1827
+
1584
1828
  # ---------- write the statusline script ----------
1585
1829
 
1586
1830
  cat > "$statusline_file" << 'STATUSLINE_EOF'
package/package.json CHANGED
@@ -1,16 +1,22 @@
1
1
  {
2
2
  "name": "@workweave/router",
3
- "version": "0.1.8",
4
- "description": "One-command installer that points Claude Code at the Weave Router.",
3
+ "version": "0.2.1",
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"
7
7
  },
8
+ "pi": {
9
+ "extensions": [
10
+ "./pi-router/src/index.ts"
11
+ ]
12
+ },
8
13
  "files": [
9
14
  "bin.js",
10
15
  "install.sh",
11
16
  "uninstall.sh",
12
17
  "cc-statusline.sh",
13
18
  "commands/",
19
+ "pi-router/",
14
20
  "README.md",
15
21
  "LICENSE"
16
22
  ],
@@ -20,6 +26,20 @@
20
26
  "engines": {
21
27
  "node": ">=18"
22
28
  },
29
+ "peerDependencies": {
30
+ "@mariozechner/pi-agent-core": "*",
31
+ "@mariozechner/pi-ai": "*",
32
+ "@mariozechner/pi-coding-agent": "*",
33
+ "@mariozechner/pi-tui": "*",
34
+ "typebox": "*"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "@mariozechner/pi-agent-core": { "optional": true },
38
+ "@mariozechner/pi-ai": { "optional": true },
39
+ "@mariozechner/pi-coding-agent": { "optional": true },
40
+ "@mariozechner/pi-tui": { "optional": true },
41
+ "typebox": { "optional": true }
42
+ },
23
43
  "repository": {
24
44
  "type": "git",
25
45
  "url": "git+https://github.com/workweave/router.git",
@@ -33,6 +53,8 @@
33
53
  "claude",
34
54
  "claude-code",
35
55
  "anthropic",
36
- "llm-router"
56
+ "llm-router",
57
+ "pi",
58
+ "pi-package"
37
59
  ]
38
60
  }
@@ -0,0 +1,70 @@
1
+ # Weave Router pi extension
2
+
3
+ > Bundled inside the [`@workweave/router`](https://www.npmjs.com/package/@workweave/router) package — not published separately. `src/` here is the source of truth; `npm run prepack` copies it into the package.
4
+
5
+ A [pi](https://pi.dev) extension that routes every request through the
6
+ [WorkWeave Router](https://github.com/workweave/router) — a trained, per-request
7
+ LLM proxy that picks the most cost-efficient model that still solves each task.
8
+
9
+ Installed automatically by the Weave Router installer:
10
+
11
+ ```bash
12
+ WEAVE_ROUTER_KEY=rk_… npx -y @workweave/router --pi # user scope
13
+ WEAVE_ROUTER_KEY=rk_… npx -y @workweave/router --pi --local # local router (http://localhost:8080)
14
+ ```
15
+
16
+ That writes `~/.pi/agent/models.json` (the `weave` provider), adds
17
+ `npm:@workweave/router` to `~/.pi/agent/settings.json` `packages`, and stores
18
+ the key in `~/.pi/agent/.weave_router_key`. pi auto-installs `@workweave/router`
19
+ from npm on next start and loads this extension via its `pi.extensions` field.
20
+
21
+ ## What it does
22
+
23
+ - **Automatic model selection.** All pi traffic flows through the router, which
24
+ selects the model per request. You don't pick a model — the router does.
25
+ - **Per-process routing bias.** Static `x-weave-routing-*` knob headers bias the
26
+ router: quality on the main loop, speed + cheap on subagents, cheapest on
27
+ compaction.
28
+ - **Sticky sessions.** `metadata.user_id = "pi:<sessionId>"` pins the main loop
29
+ to one model for the session; subagents get their own pins.
30
+ - **`dispatch` tool — parallel, context-isolated subagents.** pi has none
31
+ natively. `dispatch` spawns child `pi` processes (read-only by default), runs
32
+ them concurrently, and returns only each subagent's final answer — intermediate
33
+ tool output stays in the child, so the main context stays small.
34
+ - **Routed-model display.** Shows the model the router actually chose
35
+ (`x-router-model`) in the status bar, and opts the request out of the router's
36
+ in-band routing badge (`X-Weave-Routing-Marker: off`) — pi can't render that
37
+ separate marker text block inline, and the status bar already conveys the model.
38
+ - **Safety backstop.** Blocks a few catastrophic shell commands (`rm -rf /`,
39
+ `mkfs`, `dd of=/dev/…`, fork bombs, force-push to main). Disable with
40
+ `WEAVE_NO_SAFETY=1`.
41
+
42
+ ## Configuration (environment)
43
+
44
+ | Variable | Default | Purpose |
45
+ |---|---|---|
46
+ | `WEAVE_ROUTER_URL` | `http://localhost:8080` | Router base URL (children inherit it) |
47
+ | `WEAVE_ROUTER_KEY` | — | Router key (else read from `.weave_router_key`) |
48
+ | `WEAVE_ROUTER_KEY_FILE` | `<agentDir>/.weave_router_key` | Override key file path |
49
+ | `WEAVE_USER_EMAIL` / `WEAVE_USER_NAME` | from `git config` | Identity headers for attribution |
50
+ | `WEAVE_PI_SUBAGENT_MODEL` | `claude-sonnet-4-6` | `weave/<model>` handle children launch with (router re-routes) |
51
+ | `WEAVE_PI_DISPATCH_CONCURRENCY` | `4` | Max concurrent subagents |
52
+ | `WEAVE_PI_SUBAGENT_TIMEOUT_MS` | `600000` | Per-subagent timeout |
53
+ | `WEAVE_PI_ALLOW_SUBAGENT_TOOLS` | unset | `1` lets `dispatch` grant subagents write/exec tools (bash, write, edit); default strips them |
54
+ | `WEAVE_ROUTING_ALPHA` / `…_SPEED_WEIGHT` / `…_OUTPUT_COST_RATIO` / `…_EXPECTED_OUTPUT_TOKENS` | role preset | Override individual routing knobs (main process only — children always use their role preset) |
55
+ | `WEAVE_NO_SAFETY` | unset | `1` disables the catastrophic-bash gate |
56
+ | `WEAVE_CHEAP_COMPACTION` | unset | `1` enables the (experimental) cheap-compaction path |
57
+
58
+ Internal: `WEAVE_PI_SUBAGENT=1` and `WEAVE_PI_SUBAGENT_ID` are set by `dispatch`
59
+ on child processes; don't set them yourself.
60
+
61
+ ## Billing
62
+
63
+ Routing through the router switches pi from Claude **subscription OAuth** to
64
+ **per-token** billing on the router deployment's key (or your BYOK key). BYOK
65
+ skips cross-provider failover; deployment-key billing is the default.
66
+
67
+ ## Notes
68
+
69
+ - Cheap compaction is currently a reserved flag — the handler defers to pi's
70
+ built-in compaction until the router-routed path is validated end-to-end.
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@workweave/router-pi-extension",
3
+ "private": true,
4
+ "type": "module",
5
+ "description": "The pi routing extension bundled into @workweave/router. NOT published separately: src/ is copied into the @workweave/router package at prepack and loaded via that package's pi.extensions. This manifest just marks the sources as ESM and declares the pi peer deps for local typecheck.",
6
+ "peerDependencies": {
7
+ "@mariozechner/pi-agent-core": "*",
8
+ "@mariozechner/pi-ai": "*",
9
+ "@mariozechner/pi-coding-agent": "*",
10
+ "@mariozechner/pi-tui": "*",
11
+ "typebox": "*"
12
+ }
13
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * EXPERIMENTAL, off by default (enable with WEAVE_CHEAP_COMPACTION=1).
3
+ *
4
+ * Intent: run context compaction through the cheapest routing knobs. The
5
+ * catch is that compaction bypasses provider hooks and reuses the session's
6
+ * static provider headers (the main loop's quality knobs), and
7
+ * `session_before_compact` is the only per-turn lever. Producing a fully
8
+ * router-routed cheap CompactionResult here is not yet validated, so this
9
+ * handler currently defers to pi's built-in compaction (returns no override)
10
+ * and only reserves the flag + surfaces that it's active. Promote to a real
11
+ * cheap path once validated end-to-end.
12
+ */
13
+
14
+ import type { ExtensionAPI, ExtensionContext, SessionBeforeCompactEvent } from "@mariozechner/pi-coding-agent";
15
+
16
+ export function registerCheapCompaction(pi: ExtensionAPI): void {
17
+ let announced = false;
18
+ pi.on("session_before_compact", (_event: SessionBeforeCompactEvent, ctx: ExtensionContext) => {
19
+ if (ctx.hasUI && !announced) {
20
+ announced = true;
21
+ ctx.ui.setStatus("weave-compaction", "cheap compaction: experimental (using built-in)");
22
+ }
23
+ // Defer to built-in compaction until the router-routed cheap path is validated.
24
+ return undefined;
25
+ });
26
+ }