Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(codex): detect npm-prefix installs after setup

cyh29e0c5dc471e
diff --git a/tests/test_ui_api.py b/tests/test_ui_api.py
index d95c1673f..ab0f7c570 100644
--- a/tests/test_ui_api.py
+++ b/tests/test_ui_api.py
@@ -57,6 +57,32 @@ def test_detect_cli_finds_npm_in_nvm(monkeypatch, tmp_path):
assert result["path"] == str(npm_path)
+def test_detect_cli_finds_codex_in_npm_global_prefix(monkeypatch, tmp_path):
+ npm_path = tmp_path / "tools" / "npm"
+ npm_path.parent.mkdir(parents=True, exist_ok=True)
+ npm_path.write_text("#!/bin/sh\n")
+ npm_path.chmod(0o755)
+
+ codex_path = tmp_path / ".npm-global" / "bin" / "codex"
+ codex_path.parent.mkdir(parents=True, exist_ok=True)
+ codex_path.write_text("#!/bin/sh\n")
+ codex_path.chmod(0o755)
+
+ class CompletedProcess:
+ returncode = 0
+ stdout = f"{tmp_path / '.npm-global'}\n"
+ stderr = ""
+
+ monkeypatch.setattr(api.Path, "home", lambda: tmp_path)
+ monkeypatch.setattr(api.shutil, "which", lambda binary: str(npm_path) if binary == "npm" else None)
+ monkeypatch.setattr(api.subprocess, "run", lambda *args, **kwargs: CompletedProcess())
+
+ result = api.detect_cli("codex")
+
+ assert result["found"] is True
+ assert result["path"] == str(codex_path)
+
+
def test_install_agent_returns_resolved_path(monkeypatch):
class CompletedProcess:
returncode = 0
@@ -106,3 +132,43 @@ def test_install_codex_uses_resolved_npm(monkeypatch):
assert calls[0][0] == ["/Users/test/.nvm/versions/node/v22.18.0/bin/npm", "install", "-g", "@openai/codex"]
assert calls[0][1]["PATH"].split(api.os.pathsep)[0] == "/Users/test/.nvm/versions/node/v22.18.0/bin"
assert result["path"] == "/Users/test/.nvm/versions/node/v22.18.0/bin/codex"
+
+
+def test_install_codex_detects_binary_via_npm_prefix(monkeypatch, tmp_path):
+ npm_path = tmp_path / "node" / "bin" / "npm"
+ npm_path.parent.mkdir(parents=True, exist_ok=True)
+ npm_path.write_text("#!/bin/sh\n")
+ npm_path.chmod(0o755)
+
+ prefix_path = tmp_path / ".npm-global"
+ codex_path = prefix_path / "bin" / "codex"
+ codex_path.parent.mkdir(parents=True, exist_ok=True)
+ codex_path.write_text("#!/bin/sh\n")
+ codex_path.chmod(0o755)
+
+ calls = []
+
+ class CompletedProcess:
+ def __init__(self, returncode=0, stdout="", stderr=""):
+ self.returncode = returncode
+ self.stdout = stdout
+ self.stderr = stderr
+
+ def fake_run(cmd, **kwargs):
+ calls.append((cmd, kwargs.get("env", {})))
+ if cmd == [str(npm_path), "install", "-g", "@openai/codex"]:
+ return CompletedProcess(stdout="installed")
+ if cmd == [str(npm_path), "config", "get", "prefix"]:
+ return CompletedProcess(stdout=f"{prefix_path}\n")
+ raise AssertionError(f"unexpected command: {cmd}")
+
+ monkeypatch.setattr(api.Path, "home", lambda: tmp_path)
+ monkeypatch.setattr(api.shutil, "which", lambda binary: str(npm_path) if binary == "npm" else None)
+ monkeypatch.setattr(api.subprocess, "run", fake_run)
+
+ result = api.install_agent("codex")
+
+ assert result["ok"] is True
+ assert result["path"] == str(codex_path)
+ assert calls[0][0] == [str(npm_path), "install", "-g", "@openai/codex"]
+ assert calls[0][1]["PATH"].split(api.os.pathsep)[0] == str(npm_path.parent)
diff --git a/vibe/api.py b/vibe/api.py
index 77ecba900..1d3ba66dd 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -53,6 +53,63 @@ def _nvm_binary_candidates(binary: str) -> list[Path]:
return candidates
+def _npm_global_binary_candidates(binary: str) -> list[Path]:
+ if not binary or binary == "npm":
+ return []
+
+ npm_paths: list[Path] = []
+ for candidate in _candidate_cli_paths("npm"):
+ if _is_executable_file(candidate) and candidate not in npm_paths:
+ npm_paths.append(candidate)
+
+ which_npm = shutil.which("npm")
+ if which_npm:
+ npm_candidate = Path(which_npm)
+ if npm_candidate not in npm_paths:
+ npm_paths.append(npm_candidate)
+
+ candidates: list[Path] = []
+ for npm_path in npm_paths:
+ try:
+ result = subprocess.run(
+ [str(npm_path), "config", "get", "prefix"],
+ capture_output=True,
+ text=True,
+ timeout=5,
+ env=_command_env_for(str(npm_path)),
+ )
+ except Exception:
+ continue
+
+ if result.returncode != 0:
+ continue
+
+ prefix = (result.stdout or "").strip().splitlines()
+ if not prefix:
+ continue
+
+ prefix_path = Path(os.path.expanduser(prefix[-1]))
+ derived_candidates = [
+ prefix_path / "bin" / binary,
+ prefix_path / binary,
+ prefix_path / "node_modules" / ".bin" / binary,
+ ]
+ if os.name == "nt":
+ derived_candidates.extend(
+ [
+ prefix_path / f"{binary}.cmd",
+ prefix_path / f"{binary}.exe",
+ prefix_path / "node_modules" / ".bin" / f"{binary}.cmd",
+ ]
+ )
+
+ for candidate in derived_candidates:
+ if candidate not in candidates:
+ candidates.append(candidate)
+
+ return candidates
+
+
def _candidate_cli_paths(binary: str) -> list[Path]:
if not binary:
return []
@@ -80,7 +137,7 @@ def _candidate_cli_paths(binary: str) -> list[Path]:
Path("/opt/homebrew/bin") / binary,
Path("/usr/local/bin") / binary,
]
- for candidate in common_candidates + _nvm_binary_candidates(binary):
+ for candidate in common_candidates + _nvm_binary_candidates(binary) + _npm_global_binary_candidates(binary):
if candidate not in candidates:
candidates.append(candidate)
@@ -1056,8 +1113,11 @@ def install_agent(name: str) -> dict:
output = result.stdout + ("\n" + result.stderr if result.stderr else "")
output = _truncate_output(output.strip())
if result.returncode == 0:
- logger.info("Agent %s installed successfully", name)
installed_path = resolve_cli_path(name)
+ if installed_path:
+ logger.info("Agent %s installed successfully at %s", name, installed_path)
+ else:
+ logger.warning("Agent %s install command succeeded but CLI path was not detected", name)
return {
"ok": True,
"message": f"{name} installed successfully",