fix: Simplify VSCodium hooks patch to use VIBE_HOME environment variable
The Vibe core in vibe-acp automatically loads hooks from $VIBE_HOME/hooks/ when VIBE_HOME is set. This is a much simpler approach than the previous watchdog-based monitoring. The wrapper now: 1. Sets VIBE_HOME to ~/.vibe 2. Executes the original vibe-acp binary The original binary and its _internal directory are backed up to _backup/ to ensure PyInstaller runtime files are preserved. The Vibe core will then automatically execute post_agent hooks after each assistant turn, displaying the cost tracker. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
+34
-155
@@ -390,9 +390,13 @@ def patch_vscodium_extension(ext_dir, action="install"):
|
||||
"""Patch VSCodium extension to add hooks support.
|
||||
|
||||
Actions:
|
||||
- "install": Replace vibe-acp with wrapper that monitors for turns
|
||||
- "install": Replace vibe-acp with wrapper that sets VIBE_HOME
|
||||
- "remove": Restore original vibe-acp binary
|
||||
|
||||
The wrapper sets VIBE_HOME so the Vibe core loads hooks from ~/.vibe/hooks/.
|
||||
The Vibe core will then automatically execute post_agent hooks after each
|
||||
assistant turn, which will display the cost tracker.
|
||||
|
||||
Args:
|
||||
ext_dir: Path to the extension directory
|
||||
action: "install" or "remove"
|
||||
@@ -418,168 +422,33 @@ def patch_vscodium_extension(ext_dir, action="install"):
|
||||
return False
|
||||
|
||||
if action == "install":
|
||||
# Check for watchdog dependency
|
||||
try:
|
||||
import watchdog
|
||||
except ImportError:
|
||||
print("Error: 'watchdog' package is required for VSCodium hooks patching.", file=sys.stderr)
|
||||
print("Install it with: pip install watchdog", file=sys.stderr)
|
||||
return False
|
||||
|
||||
# Create backup directory
|
||||
# Save the original vibe-acp binary and its supporting files
|
||||
backup_dir = bin_dir / "_backup"
|
||||
backup_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Backup original if not already backed up
|
||||
backup_acp = backup_dir / real_acp.name
|
||||
|
||||
# Only backup if not already there
|
||||
if not backup_acp.exists():
|
||||
# Copy the vibe-acp binary
|
||||
shutil.copy2(real_acp, backup_acp)
|
||||
# Also copy the _internal directory which contains PyInstaller runtime files
|
||||
internal_dir = bin_dir / "_internal"
|
||||
backup_internal_dir = backup_dir / "_internal"
|
||||
if internal_dir.exists():
|
||||
if backup_internal_dir.exists():
|
||||
shutil.rmtree(backup_internal_dir)
|
||||
shutil.copytree(internal_dir, backup_internal_dir)
|
||||
print(f"Backed up: {real_acp} -> {backup_acp}")
|
||||
|
||||
# Generate wrapper script
|
||||
vibe_home_env = os.environ.get("VIBE_HOME", "~/.vibe")
|
||||
hooks_dir = pathlib.Path(vibe_home_env).expanduser() / "hooks"
|
||||
|
||||
wrapper_content = f"""#!/usr/bin/env python3
|
||||
\"\"\"Vibe ACP Wrapper with Hooks Support for VSCodium.\"\"\"
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import json
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
REAL_ACP = "{backup_acp}"
|
||||
HOOKS_DIR = Path("{hooks_dir}")
|
||||
HOOKS_CONFIG = HOOKS_DIR / "hooks.toml"
|
||||
|
||||
# Global reference to the vibe-acp process
|
||||
vibe_acp_process = None
|
||||
|
||||
class TurnCompletionHandler(FileSystemEventHandler):
|
||||
def on_created(self, event):
|
||||
self._check_journal(event)
|
||||
|
||||
def on_modified(self, event):
|
||||
self._check_journal(event)
|
||||
|
||||
def _check_journal(self, event):
|
||||
if not event.is_directory and "journal" in str(event.src_path) and event.src_path.endswith(".jsonl"):
|
||||
session_dir = Path(event.src_path).parent.parent
|
||||
self._run_hooks(session_dir)
|
||||
|
||||
def _run_hooks(self, session_dir):
|
||||
hook_script = HOOKS_DIR / "post_turn.py"
|
||||
if not hook_script.exists() or not HOOKS_CONFIG.exists():
|
||||
return
|
||||
|
||||
try:
|
||||
import tomllib
|
||||
with open(HOOKS_CONFIG, 'rb') as f:
|
||||
config = tomllib.load(f)
|
||||
except (ImportError, Exception):
|
||||
try:
|
||||
import tomli
|
||||
with open(HOOKS_CONFIG, 'rb') as f:
|
||||
config = tomli.load(f)
|
||||
except:
|
||||
return
|
||||
|
||||
hooks = config.get('hooks', [])
|
||||
post_agent_hooks = [h for h in hooks if h.get('type') == 'post_agent']
|
||||
if not post_agent_hooks:
|
||||
return
|
||||
|
||||
try:
|
||||
payload = {{"session_id": session_dir.name, "hook_event_name": "post_agent"}}
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(hook_script)],
|
||||
input=json.dumps(payload),
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=30
|
||||
)
|
||||
if result.stdout:
|
||||
# Send hook output to vibe-acp's stdin instead of printing to stdout
|
||||
# This prevents breaking the ACP protocol
|
||||
try:
|
||||
if vibe_acp_process and vibe_acp_process.stdin:
|
||||
vibe_acp_process.stdin.write(result.stdout)
|
||||
vibe_acp_process.stdin.flush()
|
||||
except Exception:
|
||||
# If we can't write to stdin, print to stderr for debugging
|
||||
print(result.stdout, end='', flush=True, file=sys.stderr)
|
||||
except Exception as e:
|
||||
print(f"Hook error: {{e}}", file=sys.stderr)
|
||||
|
||||
def start_monitor():
|
||||
observer = Observer()
|
||||
handler = TurnCompletionHandler()
|
||||
|
||||
vibe_logs_unified = Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser() / "logs" / "session" / "unified"
|
||||
vibe_logs_active = Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser() / "logs" / "session" / "active"
|
||||
|
||||
if vibe_logs_unified.exists():
|
||||
observer.schedule(handler, str(vibe_logs_unified), recursive=True)
|
||||
if vibe_logs_active.exists():
|
||||
observer.schedule(handler, str(vibe_logs_active), recursive=True)
|
||||
|
||||
observer.start()
|
||||
return observer
|
||||
|
||||
def main():
|
||||
global vibe_acp_process
|
||||
|
||||
observer = start_monitor()
|
||||
try:
|
||||
# Start the real vibe-acp with stdin as a pipe so we can write to it
|
||||
vibe_acp_process = subprocess.Popen(
|
||||
[REAL_ACP] + sys.argv[1:],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=sys.stdout,
|
||||
stderr=sys.stderr,
|
||||
bufsize=1,
|
||||
universal_newlines=True
|
||||
)
|
||||
|
||||
# Wait for vibe-acp to complete
|
||||
vibe_acp_process.wait()
|
||||
sys.exit(vibe_acp_process.returncode)
|
||||
except KeyboardInterrupt:
|
||||
if vibe_acp_process:
|
||||
vibe_acp_process.terminate()
|
||||
observer.stop()
|
||||
sys.exit(0)
|
||||
finally:
|
||||
observer.stop()
|
||||
observer.join()
|
||||
if vibe_acp_process:
|
||||
try:
|
||||
vibe_acp_process.stdin.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
"""
|
||||
|
||||
# Write Python wrapper
|
||||
wrapper_path = real_acp.with_suffix(".py")
|
||||
wrapper_path.write_text(wrapper_content)
|
||||
wrapper_path.chmod(wrapper_path.stat().st_mode | stat.S_IEXEC)
|
||||
|
||||
# Replace vibe-acp with shell script that runs the wrapper
|
||||
# Create shell wrapper that sets VIBE_HOME and runs the original vibe-acp
|
||||
vibe_home = str(pathlib.Path.home() / ".vibe")
|
||||
shell_content = f"""#!/bin/bash
|
||||
# Vibe ACP Wrapper - Patches hooks support into VSCodium
|
||||
# Original: {real_acp}
|
||||
# Wrapper: {wrapper_path}
|
||||
|
||||
exec "{sys.executable}" "{wrapper_path}" "$@"
|
||||
# Vibe ACP Wrapper - Sets VIBE_HOME for hooks support in VSCodium
|
||||
export VIBE_HOME="{vibe_home}"
|
||||
exec "{backup_acp}" "$@"
|
||||
"""
|
||||
# Try to write the shell script
|
||||
|
||||
# Write the wrapper script
|
||||
try:
|
||||
real_acp.write_text(shell_content)
|
||||
real_acp.chmod(real_acp.stat().st_mode | stat.S_IEXEC)
|
||||
@@ -592,7 +461,7 @@ exec "{sys.executable}" "{wrapper_path}" "$@"
|
||||
raise
|
||||
|
||||
print(f"Patched: {real_acp}")
|
||||
print(f" -> Uses Python wrapper: {wrapper_path}")
|
||||
print(f" -> Sets VIBE_HOME={vibe_home}")
|
||||
print(f" -> Original backed up: {backup_acp}")
|
||||
return True
|
||||
|
||||
@@ -613,15 +482,25 @@ exec "{sys.executable}" "{wrapper_path}" "$@"
|
||||
|
||||
# Restore original
|
||||
try:
|
||||
# Restore the vibe-acp binary
|
||||
shutil.copy2(backup_acp, real_acp)
|
||||
real_acp.chmod(real_acp.stat().st_mode | stat.S_IEXEC)
|
||||
|
||||
# Also restore the _internal directory if it exists in backup
|
||||
backup_internal_dir = backup_dir / "_internal"
|
||||
internal_dir = bin_dir / "_internal"
|
||||
if backup_internal_dir.exists():
|
||||
if internal_dir.exists():
|
||||
shutil.rmtree(internal_dir)
|
||||
shutil.copytree(backup_internal_dir, internal_dir)
|
||||
|
||||
# Remove Python wrapper if it exists
|
||||
wrapper_path = real_acp.with_suffix(".py")
|
||||
if wrapper_path.exists():
|
||||
wrapper_path.unlink()
|
||||
|
||||
print(f"Restored: {backup_acp} -> {real_acp}")
|
||||
print(f" -> Restored _internal directory")
|
||||
return True
|
||||
except OSError as e:
|
||||
if e.errno == 26: # Text file busy
|
||||
|
||||
Reference in New Issue
Block a user