@weave-os/router 0.2.37 → 0.2.38

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/desktop-shim.js CHANGED
@@ -9,6 +9,7 @@
9
9
  "use strict";
10
10
 
11
11
  const { execFile } = require("node:child_process");
12
+ const crypto = require("node:crypto");
12
13
  const fs = require("node:fs");
13
14
  const http = require("node:http");
14
15
  const https = require("node:https");
@@ -43,10 +44,12 @@ function loadConfig(file) {
43
44
  }
44
45
  }
45
46
  return {
47
+ connectedMarker: `${file}.connected`,
46
48
  host: parsed.listen_host || "127.0.0.1",
47
49
  port: Number(parsed.listen_port ?? 47821),
48
50
  router: new URL(parsed.router_base_url),
49
51
  routerKey: typeof parsed.router_key === "string" ? parsed.router_key : "",
52
+ reportTimeoutMs: Number(parsed.report_timeout_ms ?? 5000),
50
53
  anthropic: new URL(parsed.anthropic_origin || `https://${INTERCEPT_HOST}`),
51
54
  cert: fs.readFileSync(parsed.cert_file),
52
55
  key: fs.readFileSync(parsed.key_file),
@@ -101,6 +104,47 @@ function pacFile(port) {
101
104
  `;
102
105
  }
103
106
 
107
+ // reportConnected tells the router, once per router key, that Desktop's traffic
108
+ // reached it. Desktop shares the Claude Code key, so the router can't tell a
109
+ // Desktop connection from the key's first use. The marker is written only after
110
+ // the router accepts the report, so a failed report is retried on a later call.
111
+ function reportConnected(cfg) {
112
+ if (!cfg.routerKey || cfg.connectedReport) return;
113
+ const fingerprint = crypto.createHash("sha256").update(cfg.routerKey).digest("hex");
114
+ try {
115
+ if (fs.readFileSync(cfg.connectedMarker, "utf8").trim() === fingerprint) {
116
+ cfg.connectedReport = "done";
117
+ return;
118
+ }
119
+ } catch {}
120
+ cfg.connectedReport = "pending";
121
+ const target = cfg.router;
122
+ const body = JSON.stringify({ action: "connected", harness: "claude_desktop" });
123
+ const client = target.protocol === "http:" ? http : https;
124
+ const req = client.request({
125
+ protocol: target.protocol,
126
+ hostname: target.hostname,
127
+ port: target.port || (target.protocol === "http:" ? 80 : 443),
128
+ method: "POST",
129
+ path: `${target.pathname.replace(/\/+$/, "")}/v1/client-events`,
130
+ headers: { "content-type": "application/json", "content-length": Buffer.byteLength(body), [ROUTER_KEY_HEADER]: cfg.routerKey },
131
+ timeout: cfg.reportTimeoutMs,
132
+ }, (res) => {
133
+ res.resume();
134
+ if (res.statusCode >= 200 && res.statusCode < 300) {
135
+ try {
136
+ fs.writeFileSync(cfg.connectedMarker, `${fingerprint}\n`, { mode: 0o600 });
137
+ } catch {}
138
+ cfg.connectedReport = "done";
139
+ } else {
140
+ cfg.connectedReport = undefined;
141
+ }
142
+ });
143
+ req.on("timeout", () => req.destroy(new Error("connected report timed out")));
144
+ req.on("error", () => { cfg.connectedReport = undefined; });
145
+ req.end(body);
146
+ }
147
+
104
148
  function forward(cfg, req, res) {
105
149
  const toRouter = routesToRouter(req);
106
150
  const target = toRouter ? cfg.router : cfg.anthropic;
@@ -123,6 +167,7 @@ function forward(cfg, req, res) {
123
167
  headers,
124
168
  },
125
169
  (upstreamRes) => {
170
+ if (toRouter && upstreamRes.statusCode >= 200 && upstreamRes.statusCode < 300) reportConnected(cfg);
126
171
  const out = {};
127
172
  for (const [name, value] of Object.entries(upstreamRes.headers)) {
128
173
  if (!HOP_BY_HOP.has(name)) out[name] = value;
package/install.sh CHANGED
@@ -602,8 +602,8 @@ normalize_name() {
602
602
 
603
603
  # resolve_user_email picks the email to plant in router request headers so the
604
604
  # router can attribute traffic to a person even on shared API keys. Priority:
605
- # explicit --email → WEAVE_USER_EMAIL env override → git config user.email →
606
- # interactive prompt (pre-filled with whatever we found). In --non-interactive
605
+ # explicit --email → WEAVE_USER_EMAIL env override → the email already installed
606
+ # for this target → git config user.email → interactive prompt (pre-filled with whatever we found). In --non-interactive
607
607
  # mode we never prompt, so unset/invalid means we ship no header (router treats
608
608
  # that as account_uuid-only, same as today). Echoes the validated email on stdout.
609
609
  resolve_user_email() {
@@ -626,6 +626,9 @@ resolve_user_email() {
626
626
  warn "WEAVE_USER_EMAIL=\"$WEAVE_USER_EMAIL\" is not a valid email; ignoring."
627
627
  fi
628
628
  fi
629
+ if [ -z "$candidate" ]; then
630
+ candidate="$(normalize_email "$(read_installed_header X-Weave-User-Email)")"
631
+ fi
629
632
  if [ -z "$candidate" ]; then
630
633
  local git_email
631
634
  git_email="$(git config --global user.email 2>/dev/null || true)"
@@ -1464,10 +1467,10 @@ install_lsp_servers() {
1464
1467
  }
1465
1468
 
1466
1469
  # resolve_user_name mirrors resolve_user_email but for display name. Priority:
1467
- # WEAVE_USER_NAME env override → git config user.name → empty. We don't
1468
- # prompt for name independently: if email prompting yielded nothing, name
1469
- # almost certainly will too, and a second prompt is noise. Echoes the
1470
- # validated name on stdout.
1470
+ # WEAVE_USER_NAME env override → installed name → git config user.name →
1471
+ # empty. We don't prompt for name independently: if email prompting yielded
1472
+ # nothing, name almost certainly will too, and a second prompt is noise. Echoes
1473
+ # the validated name on stdout.
1471
1474
  resolve_user_name() {
1472
1475
  local candidate=""
1473
1476
  if [ -n "${WEAVE_USER_NAME:-}" ]; then
@@ -1476,6 +1479,9 @@ resolve_user_name() {
1476
1479
  warn "WEAVE_USER_NAME=\"$WEAVE_USER_NAME\" is not a usable name; ignoring."
1477
1480
  fi
1478
1481
  fi
1482
+ if [ -z "$candidate" ]; then
1483
+ candidate="$(normalize_name "$(read_installed_header X-Weave-User-Name)")"
1484
+ fi
1479
1485
  if [ -z "$candidate" ]; then
1480
1486
  local git_name
1481
1487
  git_name="$(git config --global user.name 2>/dev/null || true)"
@@ -2739,6 +2745,61 @@ read_installed_key() {
2739
2745
  esac
2740
2746
  }
2741
2747
 
2748
+ # read_installed_header prints the value of identity header $1 (for example
2749
+ # X-Weave-User-Email) from this target's managed config, or nothing. Reading it
2750
+ # back keeps a re-run or `update` from dropping the identity an earlier install
2751
+ # planted when nothing else on the machine (git config, env) supplies one.
2752
+ read_installed_header() {
2753
+ local header="$1" candidate value=""
2754
+ case "$target" in
2755
+ claude)
2756
+ while IFS= read -r candidate; do
2757
+ [ -n "$candidate" ] || continue
2758
+ value="$(json_get "$candidate" '.env.ANTHROPIC_CUSTOM_HEADERS' \
2759
+ | sed -n "s/^[[:space:]]*${header}:[[:space:]]*//p" | head -n 1)"
2760
+ [ -n "$value" ] && break
2761
+ done <<<"$(models_key_file_order)"
2762
+ ;;
2763
+ codex)
2764
+ key_source_is_own "$codex_config_file" || return 0
2765
+ value="$(awk -v header="$header" '
2766
+ /^[[:space:]]*\[/ {
2767
+ in_provider = ($0 ~ /^[[:space:]]*\[[[:space:]]*model_providers[[:space:]]*\.[[:space:]]*weave[[:space:]]*(\.[^]]*)?\][[:space:]]*(#.*)?$/)
2768
+ next
2769
+ }
2770
+ in_provider && match($0, "\"?" header "\"?[[:space:]]*=[[:space:]]*\"([^\"\\\\]|\\\\.)*\"") {
2771
+ hdr = substr($0, RSTART, RLENGTH)
2772
+ sub(/^[^=]*=[[:space:]]*"/, "", hdr)
2773
+ sub(/"$/, "", hdr)
2774
+ # Undo toml_escape so the writer does not escape the value twice.
2775
+ value = ""
2776
+ for (i = 1; i <= length(hdr); i++) {
2777
+ c = substr(hdr, i, 1)
2778
+ if (c == "\\") { i++; c = substr(hdr, i, 1) }
2779
+ value = value c
2780
+ }
2781
+ print value
2782
+ exit
2783
+ }
2784
+ ' "$codex_config_file" 2>/dev/null || true)"
2785
+ ;;
2786
+ opencode)
2787
+ key_source_is_own "$opencode_config_file" || return 0
2788
+ value="$(json_get "$opencode_config_file" ".provider.weave.options.headers[\"$header\"]")"
2789
+ ;;
2790
+ pi)
2791
+ key_source_is_own "$pi_models_file" || return 0
2792
+ value="$(json_get "$pi_models_file" ".providers.weave.headers[\"$header\"]")"
2793
+ ;;
2794
+ copilot)
2795
+ key_source_is_own "$copilot_providers_file" || return 0
2796
+ value="$(jq -r --arg h "$header" '[(.providers // [])[] | select(.name == "weave") | .headers[$h] // empty][0] // empty' \
2797
+ "$copilot_providers_file" 2>/dev/null || true)"
2798
+ ;;
2799
+ esac
2800
+ printf '%s' "$value"
2801
+ }
2802
+
2742
2803
  # resolve_installed_base_url prints the router endpoint this Claude Code install
2743
2804
  # already points at, or nothing when none of its files carry a router-shaped
2744
2805
  # one. The parked sidecar comes first: `off` moves the router URL there and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weave-os/router",
3
- "version": "0.2.37",
3
+ "version": "0.2.38",
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",
package/uninstall.sh CHANGED
@@ -404,77 +404,6 @@ if [ -z "${WEAVE_UNINSTALL_ALL_CHILD:-}" ] && { [ "$target" = "claude" ] || [ "$
404
404
  exit 1
405
405
  fi
406
406
 
407
- # Claude Desktop runs through a local proxy service (see install.sh); remove the
408
- # service, its certificate and the stored router key.
409
- if [ "$target" = "claude-desktop" ]; then
410
- if [ "$scope" = "project" ]; then
411
- [ -z "${WEAVE_UNINSTALL_ALL_CHILD:-}" ] || exit 3
412
- err "--claude-desktop installs in user scope only; drop --scope project."
413
- exit 2
414
- fi
415
- desktop_shim_dir="${install_dir:-$HOME}/.weave-router/desktop-shim"
416
- desktop_shim_config="$desktop_shim_dir/config.json"
417
- desktop_shim_plist="$HOME/Library/LaunchAgents/ai.workweave.router-desktop-shim.plist"
418
- desktop_shim_unit="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/weave-router-desktop-shim.service"
419
- skip_if_not_installed "$desktop_shim_config" 'router_base_url'
420
- # The service files live under $HOME whatever --dir says; only remove the ones
421
- # that run this install's config.
422
- desktop_shim_config_xml="$(printf '%s' "$desktop_shim_config" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g')"
423
- if [ -f "$desktop_shim_plist" ] && grep -Fq "<string>$desktop_shim_config_xml</string>" "$desktop_shim_plist"; then
424
- launchctl bootout "gui/$(id -u)/ai.workweave.router-desktop-shim" >/dev/null 2>&1 || true
425
- rm -f "$desktop_shim_plist"
426
- ok "Removed $desktop_shim_plist"
427
- fi
428
- if [ -f "$desktop_shim_unit" ] && grep -Fq "\"$desktop_shim_config\"" "$desktop_shim_unit"; then
429
- systemctl --user disable --now weave-router-desktop-shim.service >/dev/null 2>&1 || true
430
- rm -f "$desktop_shim_unit"
431
- systemctl --user daemon-reload >/dev/null 2>&1 || true
432
- ok "Removed $desktop_shim_unit"
433
- fi
434
- # Undo the trusted certificate and the auto-proxy, but only where they are this install's.
435
- desktop_shim_pac="http://127.0.0.1:$(jq -r '.listen_port // 47821' "$desktop_shim_config" 2>/dev/null || echo 47821)/proxy.pac"
436
- case "$(uname -s)" in
437
- Darwin)
438
- if [ -f "$desktop_shim_dir/ca.pem" ] \
439
- && security find-certificate -c "Weave Router Claude Desktop proxy" "$HOME/Library/Keychains/login.keychain-db" >/dev/null 2>&1; then
440
- if ! security remove-trusted-cert "$desktop_shim_dir/ca.pem" \
441
- || ! security delete-certificate -Z "$(openssl x509 -noout -fingerprint -sha1 -in "$desktop_shim_dir/ca.pem" | sed 's/.*=//; s/://g')" \
442
- "$HOME/Library/Keychains/login.keychain-db" >/dev/null; then
443
- desktop_shim_ca_left="true"
444
- fi
445
- fi
446
- while IFS= read -r service; do
447
- if [ "$(networksetup -getautoproxyurl "$service" | sed -n 's/^URL: //p')" = "$desktop_shim_pac" ]; then
448
- sudo networksetup -setautoproxystate "$service" off
449
- fi
450
- done < <(networksetup -listallnetworkservices | tail -n +2 | grep -v '^\*')
451
- ;;
452
- Linux)
453
- desktop_shim_ca_store="/usr/local/share/ca-certificates/weave-router-claude-desktop.crt"
454
- if [ -f "$desktop_shim_ca_store" ] && cmp -s "$desktop_shim_dir/ca.pem" "$desktop_shim_ca_store"; then
455
- if ! { sudo rm -f "$desktop_shim_ca_store" && sudo update-ca-certificates --fresh >/dev/null; }; then
456
- desktop_shim_ca_left="true"
457
- fi
458
- fi
459
- if [ "$(gsettings get org.gnome.system.proxy autoconfig-url 2>/dev/null)" = "'$desktop_shim_pac'" ]; then
460
- gsettings set org.gnome.system.proxy mode 'none'
461
- gsettings reset org.gnome.system.proxy autoconfig-url
462
- fi
463
- ;;
464
- esac
465
- # Keep ca.pem and the config while the OS still trusts the CA, so a rerun can remove it.
466
- if [ "${desktop_shim_ca_left:-}" = "true" ]; then
467
- warn "Could not remove the proxy certificate from the OS trust store (a cancelled password prompt?). Rerun: npx @weave-os/router --uninstall --claude-desktop"
468
- elif [ -d "$desktop_shim_dir" ]; then
469
- refuse_if_symlink "$desktop_shim_dir"
470
- rm -rf "$desktop_shim_dir"
471
- rmdir "$(dirname "$desktop_shim_dir")" 2>/dev/null || true
472
- ok "Removed $desktop_shim_dir"
473
- fi
474
- info "Quit and reopen Claude Desktop so new sessions go direct to Anthropic."
475
- exit 0
476
- fi
477
-
478
407
  # ---------- lifecycle report ----------
479
408
  #
480
409
  # report_uninstall_event tells this install's router that the uninstall is
@@ -641,6 +570,84 @@ report_pi_uninstall_event() {
641
570
  report_uninstall_event pi "$endpoint" "$key"
642
571
  }
643
572
 
573
+ report_desktop_uninstall_event() {
574
+ local f="$1"
575
+ uninstall_file_is_own "$f" || return 0
576
+ report_uninstall_event claude_desktop "$(uninstall_json_get "$f" '.router_base_url')" "$(uninstall_json_get "$f" '.router_key')"
577
+ }
578
+
579
+ # Claude Desktop runs through a local proxy service (see install.sh); remove the
580
+ # service, its certificate and the stored router key.
581
+ if [ "$target" = "claude-desktop" ]; then
582
+ if [ "$scope" = "project" ]; then
583
+ [ -z "${WEAVE_UNINSTALL_ALL_CHILD:-}" ] || exit 3
584
+ err "--claude-desktop installs in user scope only; drop --scope project."
585
+ exit 2
586
+ fi
587
+ desktop_shim_dir="${install_dir:-$HOME}/.weave-router/desktop-shim"
588
+ desktop_shim_config="$desktop_shim_dir/config.json"
589
+ desktop_shim_plist="$HOME/Library/LaunchAgents/ai.workweave.router-desktop-shim.plist"
590
+ desktop_shim_unit="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/weave-router-desktop-shim.service"
591
+ skip_if_not_installed "$desktop_shim_config" 'router_base_url'
592
+ # The service files live under $HOME whatever --dir says; only remove the ones
593
+ # that run this install's config.
594
+ desktop_shim_config_xml="$(printf '%s' "$desktop_shim_config" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g')"
595
+ if [ -f "$desktop_shim_plist" ] && grep -Fq "<string>$desktop_shim_config_xml</string>" "$desktop_shim_plist"; then
596
+ launchctl bootout "gui/$(id -u)/ai.workweave.router-desktop-shim" >/dev/null 2>&1 || true
597
+ rm -f "$desktop_shim_plist"
598
+ ok "Removed $desktop_shim_plist"
599
+ fi
600
+ if [ -f "$desktop_shim_unit" ] && grep -Fq "\"$desktop_shim_config\"" "$desktop_shim_unit"; then
601
+ systemctl --user disable --now weave-router-desktop-shim.service >/dev/null 2>&1 || true
602
+ rm -f "$desktop_shim_unit"
603
+ systemctl --user daemon-reload >/dev/null 2>&1 || true
604
+ ok "Removed $desktop_shim_unit"
605
+ fi
606
+ # Undo the trusted certificate and the auto-proxy, but only where they are this install's.
607
+ desktop_shim_pac="http://127.0.0.1:$(jq -r '.listen_port // 47821' "$desktop_shim_config" 2>/dev/null || echo 47821)/proxy.pac"
608
+ case "$(uname -s)" in
609
+ Darwin)
610
+ if [ -f "$desktop_shim_dir/ca.pem" ] \
611
+ && security find-certificate -c "Weave Router Claude Desktop proxy" "$HOME/Library/Keychains/login.keychain-db" >/dev/null 2>&1; then
612
+ if ! security remove-trusted-cert "$desktop_shim_dir/ca.pem" \
613
+ || ! security delete-certificate -Z "$(openssl x509 -noout -fingerprint -sha1 -in "$desktop_shim_dir/ca.pem" | sed 's/.*=//; s/://g')" \
614
+ "$HOME/Library/Keychains/login.keychain-db" >/dev/null; then
615
+ desktop_shim_ca_left="true"
616
+ fi
617
+ fi
618
+ while IFS= read -r service; do
619
+ if [ "$(networksetup -getautoproxyurl "$service" | sed -n 's/^URL: //p')" = "$desktop_shim_pac" ]; then
620
+ sudo networksetup -setautoproxystate "$service" off
621
+ fi
622
+ done < <(networksetup -listallnetworkservices | tail -n +2 | grep -v '^\*')
623
+ ;;
624
+ Linux)
625
+ desktop_shim_ca_store="/usr/local/share/ca-certificates/weave-router-claude-desktop.crt"
626
+ if [ -f "$desktop_shim_ca_store" ] && cmp -s "$desktop_shim_dir/ca.pem" "$desktop_shim_ca_store"; then
627
+ if ! { sudo rm -f "$desktop_shim_ca_store" && sudo update-ca-certificates --fresh >/dev/null; }; then
628
+ desktop_shim_ca_left="true"
629
+ fi
630
+ fi
631
+ if [ "$(gsettings get org.gnome.system.proxy autoconfig-url 2>/dev/null)" = "'$desktop_shim_pac'" ]; then
632
+ gsettings set org.gnome.system.proxy mode 'none'
633
+ gsettings reset org.gnome.system.proxy autoconfig-url
634
+ fi
635
+ ;;
636
+ esac
637
+ # Keep ca.pem and the config while the OS still trusts the CA, so a rerun can remove it.
638
+ if [ "${desktop_shim_ca_left:-}" = "true" ]; then
639
+ warn "Could not remove the proxy certificate from the OS trust store (a cancelled password prompt?). Rerun: npx @weave-os/router --uninstall --claude-desktop"
640
+ elif [ -d "$desktop_shim_dir" ]; then
641
+ refuse_if_symlink "$desktop_shim_dir"
642
+ report_desktop_uninstall_event "$desktop_shim_config"
643
+ rm -rf "$desktop_shim_dir"
644
+ rmdir "$(dirname "$desktop_shim_dir")" 2>/dev/null || true
645
+ ok "Removed $desktop_shim_dir"
646
+ fi
647
+ info "Quit and reopen Claude Desktop so new sessions go direct to Anthropic."
648
+ exit 0
649
+ fi
650
+
644
651
  # Markers must stay in sync with install.sh. Keep verbatim.
645
652
  WEAVE_CODEX_BEGIN_MARKER="# >>> weave-router managed (do not edit between markers) >>>"
646
653
  WEAVE_CODEX_END_MARKER="# <<< weave-router managed <<<"