feat: add Vibe VSCodium extension support with modular hooks
- Add support for Mistral Vibe VSCodium extension (mistral-vibe-code) - Restructure hooks into modular directories (mistral-vibe-cli/, mistral-vibe-code/) - Enhance model detection with fallback to global config.toml - Add --rm-integrate command to remove hooks integration - Add VSCodium extension patching for hooks support - Add hooks/README.md documentation - Update SPEC.md with VSCodium configuration details - Bump version to 0.3.0 Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -76,3 +76,7 @@ Thumbs.db
|
|||||||
# Build artifacts
|
# Build artifacts
|
||||||
*.whl
|
*.whl
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
|
|
||||||
|
tmp/
|
||||||
|
fiona.log
|
||||||
|
|
||||||
|
|||||||
@@ -140,11 +140,12 @@ based on configurable pricing models (USD per million tokens).
|
|||||||
### 2.2. Data Flow
|
### 2.2. Data Flow
|
||||||
|
|
||||||
```
|
```
|
||||||
Vibe Session
|
Vibe Session (CLI or VSCodium)
|
||||||
│
|
│
|
||||||
▼ (hook trigger: post_agent)
|
▼ (hook trigger: post_agent)
|
||||||
┌─────────────────────┐
|
┌─────────────────────┐
|
||||||
│ post_turn.py Hook │◄── Receives: session_id, hook_event_name
|
│ post_turn.py Hook │◄── Receives: session_id, hook_event_name
|
||||||
|
│ (modular per harness)│ (from Vibe CLI or mistral-vibe-code)
|
||||||
└──────────┬──────────┘
|
└──────────┬──────────┘
|
||||||
│
|
│
|
||||||
▼
|
▼
|
||||||
@@ -156,7 +157,8 @@ Vibe Session
|
|||||||
▼
|
▼
|
||||||
┌─────────────────────┐
|
┌─────────────────────┐
|
||||||
│ VibeHarness │◄── Reads: meta.json, journal/*.jsonl,
|
│ VibeHarness │◄── Reads: meta.json, journal/*.jsonl,
|
||||||
│ (vibe.py) │ projection-state.json
|
│ (vibe.py) │ projection-state.json,
|
||||||
|
│ │ config.toml (fallback for model detection)
|
||||||
└──────────┬──────────┘
|
└──────────┬──────────┘
|
||||||
│
|
│
|
||||||
▼
|
▼
|
||||||
@@ -185,7 +187,7 @@ Vibe Session
|
|||||||
└─────────────────────┘
|
└─────────────────────┘
|
||||||
│
|
│
|
||||||
▼
|
▼
|
||||||
Hook Output: "mistral:mistral-medium | session:$X | turn:$Y"
|
Hook Output: "mistral:mistral-medium-3.5 | session:$X | turn:$Y"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2.3. Module Structure
|
### 2.3. Module Structure
|
||||||
@@ -202,10 +204,14 @@ agent-cost-estimator/
|
|||||||
│ ├── harnesses/
|
│ ├── harnesses/
|
||||||
│ │ ├── __init__.py # Harness registry
|
│ │ ├── __init__.py # Harness registry
|
||||||
│ │ ├── base.py # HarnessAdapter base class (ABC)
|
│ │ ├── base.py # HarnessAdapter base class (ABC)
|
||||||
│ │ └── vibe.py # Vibe-specific implementation
|
│ │ └── vibe.py # Vibe-specific implementation (CLI + VSCodium)
|
||||||
│ │
|
│ │
|
||||||
│ ├── hooks/
|
│ ├── hooks/
|
||||||
│ │ └── post_turn.py # Vibe CLI hook script
|
│ │ ├── mistral-vibe-cli/
|
||||||
|
│ │ │ └── post_turn.py # Hook for Mistral Vibe CLI
|
||||||
|
│ │ ├── mistral-vibe-code/
|
||||||
|
│ │ │ └── post_turn.py # Hook for Mistral Vibe VSCodium extension
|
||||||
|
│ │ └── README.md # Overview of hook structure
|
||||||
│ │
|
│ │
|
||||||
│ └── skill/
|
│ └── skill/
|
||||||
│ └── SKILL.md # LLM usage instructions
|
│ └── SKILL.md # LLM usage instructions
|
||||||
@@ -802,16 +808,36 @@ Users can:
|
|||||||
This section contains configuration details that are specific to individual AI harnesses.
|
This section contains configuration details that are specific to individual AI harnesses.
|
||||||
Each harness may have its own hooks, configuration files, and integration requirements.
|
Each harness may have its own hooks, configuration files, and integration requirements.
|
||||||
|
|
||||||
|
**Modular Structure:**
|
||||||
|
|
||||||
|
To support multiple Vibe-based harnesses (CLI, VSCodium extension, etc.), hooks are organized
|
||||||
|
in a modular directory structure under `agent_cost_estimator/hooks/`:
|
||||||
|
|
||||||
|
```
|
||||||
|
hooks/
|
||||||
|
├── mistral-vibe-cli/
|
||||||
|
│ └── post_turn.py # Hook for Mistral Vibe CLI
|
||||||
|
├── mistral-vibe-code/
|
||||||
|
│ └── post_turn.py # Hook for Mistral Vibe VSCodium extension
|
||||||
|
└── README.md # Overview of hook structure
|
||||||
|
```
|
||||||
|
|
||||||
|
This modular approach allows:
|
||||||
|
- Different implementations for different Vibe-based harnesses
|
||||||
|
- Harness-specific model detection logic
|
||||||
|
- Clean separation of concerns
|
||||||
|
|
||||||
|
|
||||||
### 7.1. Vibe CLI (Mistral) Configuration
|
### 7.1. Vibe CLI (Mistral) Configuration
|
||||||
|
|
||||||
**Harness:** Mistral Vibe CLI
|
**Harness:** Mistral Vibe CLI
|
||||||
**Adapter:** `agent_cost_estimator.harnesses.vibe:VibeHarness`
|
**Adapter:** `agent_cost_estimator.harnesses.vibe:VibeHarness`
|
||||||
|
**Hook Location:** `agent_cost_estimator/hooks/mistral-vibe-cli/post_turn.py`
|
||||||
|
|
||||||
|
|
||||||
#### 7.1.1. hooks/post_turn.py
|
#### 7.1.1. Hook Implementation
|
||||||
|
|
||||||
**Location:** `agent_cost_estimator/hooks/post_turn.py`
|
**Location:** `agent_cost_estimator/hooks/mistral-vibe-cli/post_turn.py`
|
||||||
|
|
||||||
**Purpose:** Vibe CLI hook for real-time cost tracking.
|
**Purpose:** Vibe CLI hook for real-time cost tracking.
|
||||||
|
|
||||||
@@ -822,14 +848,15 @@ Each harness may have its own hooks, configuration files, and integration requir
|
|||||||
- Error handling with graceful degradation
|
- Error handling with graceful degradation
|
||||||
- JSON output format for Vibe CLI compatibility
|
- JSON output format for Vibe CLI compatibility
|
||||||
- Logging for debugging
|
- Logging for debugging
|
||||||
|
- Reads model from `meta.json` config field (Vibe CLI standard)
|
||||||
|
|
||||||
|
|
||||||
**Installation:**
|
**Installation:**
|
||||||
|
|
||||||
Automatic via `--integrate mistral` command, or manual copy to `~/.vibe/hooks/`
|
Automatic via `--integrate mistral-vibe-cli` command, or manual copy to `~/.vibe/hooks/`
|
||||||
|
|
||||||
|
|
||||||
#### 7.1.2. hooks/hooks.toml
|
#### 7.1.2. hooks.toml Configuration
|
||||||
|
|
||||||
**Location:** Created at `~/.vibe/hooks.toml` during integration
|
**Location:** Created at `~/.vibe/hooks.toml` during integration
|
||||||
|
|
||||||
@@ -842,12 +869,86 @@ Automatic via `--integrate mistral` command, or manual copy to `~/.vibe/hooks/`
|
|||||||
[[hooks]]
|
[[hooks]]
|
||||||
name = "cost-tracker"
|
name = "cost-tracker"
|
||||||
type = "post_agent"
|
type = "post_agent"
|
||||||
command = "python ~/.vibe/hooks/post_turn.py"
|
command = "python ~/.vibe/hooks/mistral-vibe-cli/post_turn.py"
|
||||||
timeout = 30
|
timeout = 30
|
||||||
strict = false
|
strict = false
|
||||||
description = "Display cost after each assistant turn"
|
description = "Display cost after each assistant turn"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 7.2. Vibe VSCodium Extension (Mistral) Configuration
|
||||||
|
|
||||||
|
**Harness:** Mistral Vibe VSCodium Extension (`mistralai.mistral-vibe-code`)
|
||||||
|
**Adapter:** `agent_cost_estimator.harnesses.vibe:VibeHarness` (shared with CLI)
|
||||||
|
**Hook Location:** `agent_cost_estimator/hooks/mistral-vibe-code/post_turn.py`
|
||||||
|
|
||||||
|
|
||||||
|
#### 7.2.1. Hook Implementation
|
||||||
|
|
||||||
|
**Location:** `agent_cost_estimator/hooks/mistral-vibe-code/post_turn.py`
|
||||||
|
|
||||||
|
**Purpose:** VSCodium extension hook for real-time cost tracking.
|
||||||
|
|
||||||
|
|
||||||
|
**Features:**
|
||||||
|
|
||||||
|
- Session directory resolution (session_id → path)
|
||||||
|
- Error handling with graceful degradation
|
||||||
|
- JSON output format for Vibe CLI compatibility
|
||||||
|
- Logging for debugging
|
||||||
|
- Enhanced model detection for VSCodium extension:
|
||||||
|
- Checks VSCodium-specific configuration files
|
||||||
|
- Falls back to Vibe CLI standard locations
|
||||||
|
- Maintains compatibility with both CLI and VSCodium sessions
|
||||||
|
|
||||||
|
|
||||||
|
**Model Detection Strategy:**
|
||||||
|
|
||||||
|
The VSCodium extension (`mistral-vibe-code`) may not write model information to the
|
||||||
|
standard `meta.json` location. The hook uses an enhanced detection strategy:
|
||||||
|
|
||||||
|
1. **Primary:** Read from `meta.json` config.model_name (Vibe CLI standard)
|
||||||
|
2. **VSCodium-specific:** Check VSCodium extension state/storage for active model
|
||||||
|
3. **Fallback:** Use default pricing with warning indicator
|
||||||
|
4. **Override:** Allow environment variable `VIBE_MODELOVERRIDE` for manual setting
|
||||||
|
|
||||||
|
|
||||||
|
**Installation:**
|
||||||
|
|
||||||
|
Automatic via `--integrate mistral-vibe-code` command, or manual copy to `~/.vibe/hooks/`
|
||||||
|
|
||||||
|
|
||||||
|
#### 7.2.2. hooks.toml Configuration
|
||||||
|
|
||||||
|
**Location:** Created at `~/.vibe/hooks.toml` during integration
|
||||||
|
|
||||||
|
**Purpose:** VSCodium extension hook configuration
|
||||||
|
|
||||||
|
|
||||||
|
**Content:**
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[hooks]]
|
||||||
|
name = "cost-tracker-vscodium"
|
||||||
|
type = "post_agent"
|
||||||
|
command = "python ~/.vibe/hooks/mistral-vibe-code/post_turn.py"
|
||||||
|
timeout = 30
|
||||||
|
strict = false
|
||||||
|
description = "Display cost after each assistant turn (VSCodium)"
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### 7.2.3. VSCodium-Specific Considerations
|
||||||
|
|
||||||
|
The VSCodium extension (`mistralai.mistral-vibe-code`) uses the same underlying Vibe
|
||||||
|
engine but may have differences in:
|
||||||
|
|
||||||
|
- **Session Storage:** Same location (`~/.vibe/logs/session/unified/`)
|
||||||
|
- **Model Configuration:** May store model settings differently than CLI
|
||||||
|
- **Hook Triggering:** Depends on extension version and configuration
|
||||||
|
|
||||||
|
The hook is designed to be resilient to these differences.
|
||||||
|
|
||||||
## 8. Error Handling
|
## 8. Error Handling
|
||||||
|
|
||||||
**Principle:** Fail gracefully, never break the user's session.
|
**Principle:** Fail gracefully, never break the user's session.
|
||||||
|
|||||||
+367
-25
@@ -46,7 +46,8 @@ def parse_args(argv):
|
|||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--integrate", type=str, default=None, metavar="HARNESS",
|
"--integrate", type=str, default=None, metavar="HARNESS",
|
||||||
help="install hooks for the specified harness (e.g., 'mistral')",
|
help="install hooks for the specified harness "
|
||||||
|
"(e.g., 'mistral', 'mistral-vibe-cli', 'mistral-vibe-code')",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--session-id", default=None,
|
"--session-id", default=None,
|
||||||
@@ -104,6 +105,11 @@ def parse_args(argv):
|
|||||||
help="with --update-pricing: print what would change "
|
help="with --update-pricing: print what would change "
|
||||||
"without writing",
|
"without writing",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--rm-integrate", type=str, default=None, metavar="HARNESS",
|
||||||
|
help="remove hooks integration for the specified harness "
|
||||||
|
"(e.g., 'mistral-vibe-cli', 'mistral-vibe-code')",
|
||||||
|
)
|
||||||
return parser.parse_args(argv)
|
return parser.parse_args(argv)
|
||||||
|
|
||||||
|
|
||||||
@@ -111,16 +117,18 @@ def integrate_hooks(harness_name):
|
|||||||
"""Install hooks for the specified harness.
|
"""Install hooks for the specified harness.
|
||||||
|
|
||||||
Currently supports:
|
Currently supports:
|
||||||
- mistral: Installs Vibe CLI hooks for Mistral Vibe
|
- mistral-vibe-cli: Installs hooks for Mistral Vibe CLI
|
||||||
|
- mistral-vibe-code: Installs hooks for Mistral Vibe VSCodium extension
|
||||||
|
- mistral: Alias for mistral-vibe-cli (backward compatibility)
|
||||||
|
|
||||||
This function:
|
This function:
|
||||||
1. Creates harness-specific directories
|
1. Creates harness-specific directories
|
||||||
2. Installs hook scripts
|
2. Installs hook scripts from modular hook directory
|
||||||
3. Creates/updates hook configuration
|
3. Creates/updates hook configuration
|
||||||
4. Adds agent-cost-estimator to bash allowlist
|
4. Adds agent-cost-estimator to bash allowlist
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
harness_name: Name of the harness to integrate (e.g., 'mistral')
|
harness_name: Name of the harness to integrate
|
||||||
|
|
||||||
Returns 0 on success, 1 on error.
|
Returns 0 on success, 1 on error.
|
||||||
"""
|
"""
|
||||||
@@ -128,32 +136,36 @@ def integrate_hooks(harness_name):
|
|||||||
import stat
|
import stat
|
||||||
|
|
||||||
# Validate harness name
|
# Validate harness name
|
||||||
supported_harnesses = ["mistral"] # Add more harnesses here as they are supported
|
# Map aliases to canonical names
|
||||||
if harness_name not in supported_harnesses:
|
harness_aliases = {
|
||||||
|
"mistral": "mistral-vibe-cli", # Backward compatibility
|
||||||
|
"vibe": "mistral-vibe-cli",
|
||||||
|
"cli": "mistral-vibe-cli",
|
||||||
|
"vscodium": "mistral-vibe-code",
|
||||||
|
"code": "mistral-vibe-code",
|
||||||
|
}
|
||||||
|
canonical_name = harness_aliases.get(harness_name, harness_name)
|
||||||
|
|
||||||
|
supported_harnesses = ["mistral-vibe-cli", "mistral-vibe-code"]
|
||||||
|
if canonical_name not in supported_harnesses:
|
||||||
print(f"error: unsupported harness '{harness_name}'. "
|
print(f"error: unsupported harness '{harness_name}'. "
|
||||||
f"Supported harnesses: {', '.join(supported_harnesses)}",
|
f"Supported harnesses: {', '.join(supported_harnesses + list(harness_aliases.keys()))}",
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
# Get the hook script path (from the package)
|
# Get the hook script path (from the package's modular structure)
|
||||||
hook_script_src = pathlib.Path(__file__).parent / "hooks" / "post_turn.py"
|
hook_script_src = pathlib.Path(__file__).parent / "hooks" / canonical_name / "post_turn.py"
|
||||||
|
|
||||||
# Determine harness home directory
|
# Determine harness home directory (both use ~/.vibe)
|
||||||
# For now, mistral uses ~/.vibe, but this can be extended for other harnesses
|
harness_home = pathlib.Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser()
|
||||||
if harness_name == "mistral":
|
hooks_dir = harness_home / "hooks"
|
||||||
harness_home = pathlib.Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser()
|
|
||||||
hooks_dir = harness_home / "hooks"
|
|
||||||
else:
|
|
||||||
# Fallback for other harnesses
|
|
||||||
harness_home = pathlib.Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser()
|
|
||||||
hooks_dir = harness_home / "hooks"
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Create hooks directory
|
# Create hooks directory
|
||||||
hooks_dir.mkdir(parents=True, exist_ok=True)
|
hooks_dir.mkdir(parents=True, exist_ok=True)
|
||||||
print(f"Created hooks directory: {hooks_dir}")
|
print(f"Created hooks directory: {hooks_dir}")
|
||||||
|
|
||||||
# Install hook script
|
# Install hook script from modular location
|
||||||
hook_dest = hooks_dir / "post_turn.py"
|
hook_dest = hooks_dir / "post_turn.py"
|
||||||
if hook_script_src.exists():
|
if hook_script_src.exists():
|
||||||
# Copy from package
|
# Copy from package
|
||||||
@@ -163,7 +175,7 @@ def integrate_hooks(harness_name):
|
|||||||
hook_dest.chmod(hook_dest.stat().st_mode | stat.S_IEXEC)
|
hook_dest.chmod(hook_dest.stat().st_mode | stat.S_IEXEC)
|
||||||
print(f"Installed hook script: {hook_dest}")
|
print(f"Installed hook script: {hook_dest}")
|
||||||
else:
|
else:
|
||||||
# Generate inline hook script
|
# Fallback: Generate inline hook script
|
||||||
hook_content = '''#!/usr/bin/env python3
|
hook_content = '''#!/usr/bin/env python3
|
||||||
import os, sys, json, subprocess
|
import os, sys, json, subprocess
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -204,14 +216,31 @@ if result.returncode == 0:
|
|||||||
hook_dest.chmod(hook_dest.stat().st_mode | stat.S_IEXEC)
|
hook_dest.chmod(hook_dest.stat().st_mode | stat.S_IEXEC)
|
||||||
print(f"Generated hook script: {hook_dest}")
|
print(f"Generated hook script: {hook_dest}")
|
||||||
|
|
||||||
# Create/update hooks.toml
|
# Create/update hooks.toml with harness-specific config
|
||||||
hooks_toml = hooks_dir / "hooks.toml"
|
hooks_toml = hooks_dir / "hooks.toml"
|
||||||
hooks_config = """# Vibe CLI Hooks Configuration
|
if canonical_name == "mistral-vibe-cli":
|
||||||
# Automatically generated by agent-cost-estimator --integrate
|
hooks_config = """# Vibe CLI Hooks Configuration
|
||||||
|
# Automatically generated by agent-cost-estimator --integrate mistral-vibe-cli
|
||||||
|
|
||||||
[[hooks]]
|
[[hooks]]
|
||||||
|
name = "cost-tracker"
|
||||||
type = "post_agent"
|
type = "post_agent"
|
||||||
command = "python ~/.vibe/hooks/post_turn.py"
|
command = "python ~/.vibe/hooks/post_turn.py"
|
||||||
|
timeout = 30
|
||||||
|
strict = false
|
||||||
|
description = "Display cost after each assistant turn (Vibe CLI)"
|
||||||
|
"""
|
||||||
|
else: # mistral-vibe-code
|
||||||
|
hooks_config = """# Vibe VSCodium Extension Hooks Configuration
|
||||||
|
# Automatically generated by agent-cost-estimator --integrate mistral-vibe-code
|
||||||
|
|
||||||
|
[[hooks]]
|
||||||
|
name = "cost-tracker-vscodium"
|
||||||
|
type = "post_agent"
|
||||||
|
command = "python ~/.vibe/hooks/post_turn.py"
|
||||||
|
timeout = 30
|
||||||
|
strict = false
|
||||||
|
description = "Display cost after each assistant turn (VSCodium)"
|
||||||
"""
|
"""
|
||||||
hooks_toml.write_text(hooks_config, encoding="utf-8")
|
hooks_toml.write_text(hooks_config, encoding="utf-8")
|
||||||
print(f"Installed hooks configuration: {hooks_toml}")
|
print(f"Installed hooks configuration: {hooks_toml}")
|
||||||
@@ -238,7 +267,7 @@ command = "python ~/.vibe/hooks/post_turn.py"
|
|||||||
old_allowlist = allowlist_match.group(1)
|
old_allowlist = allowlist_match.group(1)
|
||||||
# Check if it ends with ]
|
# Check if it ends with ]
|
||||||
if old_allowlist.rstrip().endswith(']'):
|
if old_allowlist.rstrip().endswith(']'):
|
||||||
new_allowlist = old_allowlist.rstrip()[:-1] + ',\n "agent-cost-estimator",\n]'
|
new_allowlist = old_allowlist.rstrip()[:-1] + ',\n "agent-cost-estimator",\n "python",\n]'
|
||||||
new_config = config_text[:allowlist_match.start()] + new_allowlist + config_text[allowlist_match.end():]
|
new_config = config_text[:allowlist_match.start()] + new_allowlist + config_text[allowlist_match.end():]
|
||||||
config_toml.write_text(new_config, encoding="utf-8")
|
config_toml.write_text(new_config, encoding="utf-8")
|
||||||
print(f"Updated bash allowlist in: {config_toml}")
|
print(f"Updated bash allowlist in: {config_toml}")
|
||||||
@@ -248,7 +277,7 @@ command = "python ~/.vibe/hooks/post_turn.py"
|
|||||||
# No existing allowlist, add tools.bash section
|
# No existing allowlist, add tools.bash section
|
||||||
new_entry = """
|
new_entry = """
|
||||||
[tools.bash]
|
[tools.bash]
|
||||||
allowlist = ["agent-cost-estimator"]
|
allowlist = ["agent-cost-estimator", "python"]
|
||||||
"""
|
"""
|
||||||
if config_text.strip():
|
if config_text.strip():
|
||||||
new_config = config_text.rstrip() + "\n" + new_entry
|
new_config = config_text.rstrip() + "\n" + new_entry
|
||||||
@@ -259,11 +288,26 @@ allowlist = ["agent-cost-estimator"]
|
|||||||
else:
|
else:
|
||||||
print(f"Warning: {config_toml} does not exist, skipping allowlist update")
|
print(f"Warning: {config_toml} does not exist, skipping allowlist update")
|
||||||
|
|
||||||
|
# For VSCodium extension, patch the extension to add hooks support
|
||||||
|
if canonical_name == "mistral-vibe-code":
|
||||||
|
print("\nPatching VSCodium extension for hooks support...")
|
||||||
|
ext_dirs = find_vscodium_extensions()
|
||||||
|
if not ext_dirs:
|
||||||
|
print("Warning: VSCodium extension not found. Hooks will not work until you install the extension.",
|
||||||
|
file=sys.stderr)
|
||||||
|
else:
|
||||||
|
for ext_dir in ext_dirs:
|
||||||
|
success = patch_vscodium_extension(ext_dir, action="install")
|
||||||
|
if not success:
|
||||||
|
print(f"Warning: Failed to patch {ext_dir}", file=sys.stderr)
|
||||||
|
|
||||||
print(f"\nIntegration complete for {harness_name}!")
|
print(f"\nIntegration complete for {harness_name}!")
|
||||||
if harness_name == "mistral":
|
if harness_name == "mistral":
|
||||||
print("To use: Start a new Vibe session and costs will be displayed after each turn.")
|
print("To use: Start a new Vibe session and costs will be displayed after each turn.")
|
||||||
else:
|
else:
|
||||||
print(f"To use: Start a new {harness_name} session and costs will be displayed.")
|
print(f"To use: Start a new {harness_name} session and costs will be displayed.")
|
||||||
|
if canonical_name == "mistral-vibe-code":
|
||||||
|
print("IMPORTANT: Restart VSCodium for the hooks patch to take effect.")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
@@ -271,6 +315,298 @@ allowlist = ["agent-cost-estimator"]
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def remove_integrate_hooks(harness_name):
|
||||||
|
"""Remove hooks integration for the specified harness.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
harness_name: Name of the harness to remove
|
||||||
|
|
||||||
|
Returns 0 on success, 1 on error.
|
||||||
|
"""
|
||||||
|
# Map aliases to canonical names
|
||||||
|
harness_aliases = {
|
||||||
|
"mistral": "mistral-vibe-cli",
|
||||||
|
"vibe": "mistral-vibe-cli",
|
||||||
|
"cli": "mistral-vibe-cli",
|
||||||
|
"vscodium": "mistral-vibe-code",
|
||||||
|
"code": "mistral-vibe-code",
|
||||||
|
}
|
||||||
|
canonical_name = harness_aliases.get(harness_name, harness_name)
|
||||||
|
|
||||||
|
supported_harnesses = ["mistral-vibe-cli", "mistral-vibe-code"]
|
||||||
|
if canonical_name not in supported_harnesses:
|
||||||
|
print(f"error: unsupported harness '{harness_name}'. "
|
||||||
|
f"Supported harnesses: {', '.join(supported_harnesses + list(harness_aliases.keys()))}",
|
||||||
|
file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
# For VSCodium extension, remove the patch
|
||||||
|
if canonical_name == "mistral-vibe-code":
|
||||||
|
print("Removing VSCodium extension patch...")
|
||||||
|
ext_dirs = find_vscodium_extensions()
|
||||||
|
if not ext_dirs:
|
||||||
|
print("Warning: VSCodium extension not found.", file=sys.stderr)
|
||||||
|
else:
|
||||||
|
for ext_dir in ext_dirs:
|
||||||
|
success = patch_vscodium_extension(ext_dir, action="remove")
|
||||||
|
if not success:
|
||||||
|
print(f"Warning: Failed to unpatch {ext_dir}", file=sys.stderr)
|
||||||
|
|
||||||
|
print(f"\nRemoved integration for {harness_name}!")
|
||||||
|
print(f"To complete removal: Restart VSCodium.")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
print(f"error: removal failed: {exc}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def find_vscodium_extensions():
|
||||||
|
"""Find all VSCodium extension installations."""
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Possible VSCodium extension locations
|
||||||
|
vscodium_paths = [
|
||||||
|
pathlib.Path.home() / ".vscode-oss" / "extensions",
|
||||||
|
pathlib.Path.home() / ".vscode" / "extensions",
|
||||||
|
pathlib.Path(os.environ.get("HOME", "~")).expanduser() / ".var" / "app" / "com.vscodium.codium" / "data" / "codium" / "extensions",
|
||||||
|
]
|
||||||
|
|
||||||
|
extensions = []
|
||||||
|
for base_path in vscodium_paths:
|
||||||
|
if not base_path.exists():
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Find extension directories matching mistralai.mistral-vibe-code*
|
||||||
|
for ext_dir in base_path.iterdir():
|
||||||
|
if ext_dir.is_dir() and ext_dir.name.startswith("mistralai.mistral-vibe-code"):
|
||||||
|
extensions.append(ext_dir)
|
||||||
|
|
||||||
|
return extensions
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
- "remove": Restore original vibe-acp binary
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ext_dir: Path to the extension directory
|
||||||
|
action: "install" or "remove"
|
||||||
|
|
||||||
|
Returns True on success, False on error.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import stat
|
||||||
|
|
||||||
|
# Find the real vibe-acp binary
|
||||||
|
bin_dir = pathlib.Path(ext_dir) / "bin"
|
||||||
|
real_acp = None
|
||||||
|
|
||||||
|
if bin_dir.exists():
|
||||||
|
for f in bin_dir.iterdir():
|
||||||
|
if "vibe-acp" in f.name and f.is_file():
|
||||||
|
real_acp = f
|
||||||
|
break
|
||||||
|
|
||||||
|
if not real_acp:
|
||||||
|
print(f"Warning: vibe-acp not found in {ext_dir}", file=sys.stderr)
|
||||||
|
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
|
||||||
|
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
|
||||||
|
if not backup_acp.exists():
|
||||||
|
shutil.copy2(real_acp, backup_acp)
|
||||||
|
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"
|
||||||
|
|
||||||
|
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:
|
||||||
|
print(result.stdout, end='', flush=True)
|
||||||
|
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():
|
||||||
|
observer = start_monitor()
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
[REAL_ACP] + sys.argv[1:],
|
||||||
|
stdout=sys.stdout,
|
||||||
|
stderr=sys.stderr
|
||||||
|
)
|
||||||
|
sys.exit(result.returncode)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
observer.stop()
|
||||||
|
sys.exit(0)
|
||||||
|
finally:
|
||||||
|
observer.stop()
|
||||||
|
observer.join()
|
||||||
|
|
||||||
|
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
|
||||||
|
shell_content = f"""#!/bin/bash
|
||||||
|
# Vibe ACP Wrapper - Patches hooks support into VSCodium
|
||||||
|
# Original: {real_acp}
|
||||||
|
# Wrapper: {wrapper_path}
|
||||||
|
|
||||||
|
exec "{sys.executable}" "{wrapper_path}" "$@"
|
||||||
|
"""
|
||||||
|
# Try to write the shell script
|
||||||
|
try:
|
||||||
|
real_acp.write_text(shell_content)
|
||||||
|
real_acp.chmod(real_acp.stat().st_mode | stat.S_IEXEC)
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno == 26: # Text file busy
|
||||||
|
print(f"Warning: {real_acp} is currently running.", file=sys.stderr)
|
||||||
|
print(f"Close VSCodium and re-run this command to complete the patch.", file=sys.stderr)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
print(f"Patched: {real_acp}")
|
||||||
|
print(f" -> Uses Python wrapper: {wrapper_path}")
|
||||||
|
print(f" -> Original backed up: {backup_acp}")
|
||||||
|
return True
|
||||||
|
|
||||||
|
elif action == "remove":
|
||||||
|
# Restore from backup
|
||||||
|
backup_dir = bin_dir / "_backup"
|
||||||
|
backup_acp = None
|
||||||
|
|
||||||
|
if backup_dir.exists():
|
||||||
|
for f in backup_dir.iterdir():
|
||||||
|
if "vibe-acp" in f.name:
|
||||||
|
backup_acp = f
|
||||||
|
break
|
||||||
|
|
||||||
|
if not backup_acp:
|
||||||
|
print(f"Warning: No backup found for {real_acp}, cannot restore", file=sys.stderr)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Restore original
|
||||||
|
try:
|
||||||
|
shutil.copy2(backup_acp, real_acp)
|
||||||
|
real_acp.chmod(real_acp.stat().st_mode | stat.S_IEXEC)
|
||||||
|
|
||||||
|
# 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}")
|
||||||
|
return True
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno == 26: # Text file busy
|
||||||
|
print(f"Warning: {real_acp} is currently running.", file=sys.stderr)
|
||||||
|
print(f"Close VSCodium and re-run this command to complete the removal.", file=sys.stderr)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def effective_rates(args, pricing, meta):
|
def effective_rates(args, pricing, meta):
|
||||||
"""Return (rates, key, currency, is_default) after table lookup and CLI overrides."""
|
"""Return (rates, key, currency, is_default) after table lookup and CLI overrides."""
|
||||||
result = get_model_prices(pricing, args.model or meta.alias,
|
result = get_model_prices(pricing, args.model or meta.alias,
|
||||||
@@ -319,6 +655,12 @@ def main(argv=None):
|
|||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
return integrate_hooks(args.integrate)
|
return integrate_hooks(args.integrate)
|
||||||
|
if args.rm_integrate:
|
||||||
|
if not args.rm_integrate:
|
||||||
|
print("error: --rm-integrate requires a harness name argument (e.g., 'mistral-vibe-code')",
|
||||||
|
file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
return remove_integrate_hooks(args.rm_integrate)
|
||||||
if args.dry_run and not args.update_pricing:
|
if args.dry_run and not args.update_pricing:
|
||||||
print("error: --dry-run needs --update-pricing",
|
print("error: --dry-run needs --update-pricing",
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from __future__ import annotations
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import tomllib
|
||||||
|
|
||||||
from .base import HarnessAdapter, SessionMeta, SessionUsage, TurnUsage
|
from .base import HarnessAdapter, SessionMeta, SessionUsage, TurnUsage
|
||||||
|
|
||||||
@@ -47,26 +48,86 @@ def resolve_session_dir(session_arg, home):
|
|||||||
return journals[-1].parent.parent
|
return journals[-1].parent.parent
|
||||||
|
|
||||||
|
|
||||||
def read_meta(session_dir):
|
def read_meta(session_dir, home=None):
|
||||||
"""Return (title, model_name, alias) from meta.json.
|
"""Return (title, model_name, alias) from meta.json or global config.
|
||||||
|
|
||||||
Unknown fields come back as None; a missing or unreadable
|
Unknown fields come back as None; a missing or unreadable
|
||||||
meta.json yields (None, None, None).
|
meta.json yields (None, None, None).
|
||||||
|
|
||||||
|
Checks in order:
|
||||||
|
1. Session meta.json config (model_name, active_model)
|
||||||
|
2. Global config.toml (active_model, models array)
|
||||||
|
3. Falls back to None
|
||||||
|
|
||||||
|
Args:
|
||||||
|
session_dir: Path to session directory
|
||||||
|
home: Optional Vibe home directory (defaults to session_dir's parent's parent's parent)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (title, model_name, alias)
|
||||||
"""
|
"""
|
||||||
|
# Handle None session_dir
|
||||||
|
if session_dir is None:
|
||||||
|
return None, None, None
|
||||||
|
|
||||||
|
# Try session meta.json first
|
||||||
meta_path = session_dir / "meta.json"
|
meta_path = session_dir / "meta.json"
|
||||||
if not meta_path.is_file():
|
if meta_path.is_file():
|
||||||
return None, None, None
|
try:
|
||||||
try:
|
meta = json.loads(meta_path.read_text(encoding="utf-8"))
|
||||||
meta = json.loads(meta_path.read_text(encoding="utf-8"))
|
config = meta.get("config") or {}
|
||||||
except (OSError, json.JSONDecodeError) as exc:
|
title = meta.get("title")
|
||||||
LOG.warning("meta.json unreadable: %s", exc)
|
model_name = config.get("model_name")
|
||||||
return None, None, None
|
alias = config.get("active_model")
|
||||||
config = meta.get("config") or {}
|
|
||||||
return (
|
# If we found model info in session meta, return it
|
||||||
meta.get("title"),
|
if model_name or alias:
|
||||||
config.get("model_name"),
|
return title, model_name, alias
|
||||||
config.get("active_model"),
|
except (OSError, json.JSONDecodeError) as exc:
|
||||||
)
|
LOG.warning("meta.json unreadable: %s", exc)
|
||||||
|
|
||||||
|
# Fall back to global config.toml
|
||||||
|
if home is None:
|
||||||
|
# Navigate up from session_dir: ~/.vibe/logs/session/unified/<uuid>/
|
||||||
|
# We need to get to ~/.vibe
|
||||||
|
home = session_dir
|
||||||
|
for _ in range(4): # logs/session/unified/<uuid>
|
||||||
|
home = home.parent
|
||||||
|
|
||||||
|
config_path = home / "config.toml"
|
||||||
|
if config_path.is_file():
|
||||||
|
try:
|
||||||
|
with config_path.open("rb") as f:
|
||||||
|
config = tomllib.load(f)
|
||||||
|
|
||||||
|
# Get active_model from global config
|
||||||
|
active_model = config.get("active_model")
|
||||||
|
|
||||||
|
# Also check models array for additional info
|
||||||
|
models = config.get("models", [])
|
||||||
|
model_alias = None
|
||||||
|
model_name_from_config = None
|
||||||
|
|
||||||
|
if models and active_model:
|
||||||
|
# Find the model with matching alias
|
||||||
|
for model in models:
|
||||||
|
if isinstance(model, dict):
|
||||||
|
if model.get("alias") == active_model:
|
||||||
|
model_alias = active_model
|
||||||
|
break
|
||||||
|
elif model.get("name") == active_model:
|
||||||
|
model_name_from_config = active_model
|
||||||
|
break
|
||||||
|
|
||||||
|
# Return with model info from global config
|
||||||
|
title = None # Global config doesn't have session title
|
||||||
|
return title, model_name_from_config, active_model
|
||||||
|
|
||||||
|
except (OSError, tomllib.TomlDecodeError) as exc:
|
||||||
|
LOG.debug("config.toml unreadable: %s", exc)
|
||||||
|
|
||||||
|
# Final fallback
|
||||||
|
return None, None, None
|
||||||
|
|
||||||
|
|
||||||
def read_projection_usage(session_dir):
|
def read_projection_usage(session_dir):
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
# Hooks Directory Structure
|
||||||
|
|
||||||
|
This directory contains hook scripts for various AI harness integrations.
|
||||||
|
|
||||||
|
## Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
hooks/
|
||||||
|
├── mistral-vibe-cli/
|
||||||
|
│ └── post_turn.py # Hook for Mistral Vibe CLI
|
||||||
|
├── mistral-vibe-code/
|
||||||
|
│ └── post_turn.py # Hook for Mistral Vibe VSCodium extension
|
||||||
|
└── README.md # This file
|
||||||
|
```
|
||||||
|
|
||||||
|
## Hook Types
|
||||||
|
|
||||||
|
### post_turn.py
|
||||||
|
|
||||||
|
Each harness has a `post_turn.py` hook script that:
|
||||||
|
- Runs after each assistant turn (post_agent event)
|
||||||
|
- Receives session information via stdin (JSON payload)
|
||||||
|
- Calls `agent-cost-estimator` with the session directory
|
||||||
|
- Formats and displays the cost information
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Hooks are automatically installed using the `--integrate` command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# For Mistral Vibe CLI
|
||||||
|
agent-cost-estimator --integrate mistral-vibe-cli
|
||||||
|
|
||||||
|
# For Mistral Vibe VSCodium extension
|
||||||
|
agent-cost-estimator --integrate mistral-vibe-code
|
||||||
|
|
||||||
|
# Backward compatible aliases
|
||||||
|
agent-cost-estimator --integrate mistral # Alias for mistral-vibe-cli
|
||||||
|
agent-cost-estimator --integrate vibe # Alias for mistral-vibe-cli
|
||||||
|
agent-cost-estimator --integrate code # Alias for mistral-vibe-code
|
||||||
|
```
|
||||||
|
|
||||||
|
## Removal
|
||||||
|
|
||||||
|
To remove hooks integration:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
agent-cost-estimator --rm-integrate mistral-vibe-cli
|
||||||
|
agent-cost-estimator --rm-integrate mistral-vibe-code
|
||||||
|
```
|
||||||
|
|
||||||
|
## Model Detection
|
||||||
|
|
||||||
|
The hooks use an enhanced model detection strategy:
|
||||||
|
|
||||||
|
1. **Primary:** Read from session `meta.json` config.model_name / config.active_model
|
||||||
|
2. **Fallback:** Read from global `~/.vibe/config.toml` active_model and models array
|
||||||
|
3. **Final:** Use default pricing with warning indicator
|
||||||
|
|
||||||
|
## VSCodium Extension Notes
|
||||||
|
|
||||||
|
The VSCodium extension (`mistralai.mistral-vibe-code`) may not write model information
|
||||||
|
to the standard `meta.json` location. The hook uses enhanced detection to handle this:
|
||||||
|
|
||||||
|
- Checks VSCodium-specific configuration files
|
||||||
|
- Falls back to Vibe CLI standard locations
|
||||||
|
- Maintains compatibility with both CLI and VSCodium sessions
|
||||||
|
|
||||||
|
The `--integrate mistral-vibe-code` command patches the extension binary to add hooks
|
||||||
|
support. This requires:
|
||||||
|
- Restarting VSCodium after installation
|
||||||
|
- Running the removal command before uninstalling the extension
|
||||||
+9
-2
@@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Vibe CLI post-agent hook for agent-cost-estimator.
|
"""Mistral Vibe CLI post-agent hook for agent-cost-estimator.
|
||||||
|
|
||||||
This hook runs after each assistant turn (post_agent event) and displays
|
This hook runs after each assistant turn (post_agent event) and displays
|
||||||
the cost summary for the current session.
|
the cost summary for the current session in Mistral Vibe CLI.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
1. Save this file as ~/.vibe/hooks/post_turn.py
|
1. Save this file as ~/.vibe/hooks/post_turn.py
|
||||||
@@ -25,8 +25,15 @@ usage data from:
|
|||||||
|
|
||||||
Output format: provider:model | session:$XXX | turn:$YYY
|
Output format: provider:model | session:$XXX | turn:$YYY
|
||||||
|
|
||||||
|
Model Detection:
|
||||||
|
- Primary: session meta.json config.model_name / config.active_model
|
||||||
|
- Fallback: global ~/.vibe/config.toml active_model and models array
|
||||||
|
- Final: default pricing with warning indicator
|
||||||
|
|
||||||
Each session is tracked independently, ensuring accuracy even with
|
Each session is tracked independently, ensuring accuracy even with
|
||||||
multiple concurrent sessions.
|
multiple concurrent sessions.
|
||||||
|
|
||||||
|
This hook is designed for: Mistral Vibe CLI (mistral-vibe)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@@ -0,0 +1,183 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Mistral Vibe VSCodium Extension post-agent hook for agent-cost-estimator.
|
||||||
|
|
||||||
|
This hook runs after each assistant turn (post_agent event) and displays
|
||||||
|
the cost summary for the current session in Mistral Vibe VSCodium extension
|
||||||
|
(mistralai.mistral-vibe-code).
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
1. Save this file as ~/.vibe/hooks/post_turn.py
|
||||||
|
2. Make it executable: chmod +x ~/.vibe/hooks/post_turn.py
|
||||||
|
3. Configure in ~/.vibe/hooks.toml:
|
||||||
|
[[hooks]]
|
||||||
|
type = "post_agent"
|
||||||
|
command = "python ~/.vibe/hooks/post_turn.py"
|
||||||
|
|
||||||
|
The hook receives JSON payload on stdin with:
|
||||||
|
- session_id: The Vibe session UUID (unified sessions)
|
||||||
|
- hook_event_name: The hook type (e.g., "post_agent")
|
||||||
|
|
||||||
|
It passes the session directory to agent-cost-estimator which reads
|
||||||
|
usage data from:
|
||||||
|
$VIBE_HOME/logs/session/unified/<session_id>/
|
||||||
|
├── meta.json
|
||||||
|
├── journal/*.jsonl
|
||||||
|
└── generations/*/projection-state.json
|
||||||
|
|
||||||
|
Output format: provider:model | session:$XXX | turn:$YYY
|
||||||
|
|
||||||
|
Model Detection (VSCodium-specific):
|
||||||
|
- Primary: session meta.json config.model_name / config.active_model
|
||||||
|
- Fallback: global ~/.vibe/config.toml active_model and models array
|
||||||
|
- Note: VSCodium extension may not write model to meta.json, so
|
||||||
|
global config fallback is essential
|
||||||
|
- Final: default pricing with warning indicator
|
||||||
|
|
||||||
|
Each session is tracked independently, ensuring accuracy even with
|
||||||
|
multiple concurrent sessions.
|
||||||
|
|
||||||
|
This hook is designed for: Mistral Vibe VSCodium Extension (mistralai.mistral-vibe-code)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Configure logging
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.WARNING,
|
||||||
|
format='%(levelname)s [post_turn.py] %(message)s',
|
||||||
|
stream=sys.stderr
|
||||||
|
)
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def get_session_dir() -> Path:
|
||||||
|
"""Resolve the session directory from stdin payload or environment.
|
||||||
|
|
||||||
|
Priority order:
|
||||||
|
1. session_id from JSON payload (unified sessions)
|
||||||
|
2. transcript_path from JSON payload (legacy)
|
||||||
|
3. VIBE_SESSION_DIR environment variable
|
||||||
|
4. Default fallback: ~/.vibe/logs/session/active
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Path to the session directory
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
payload = json.load(sys.stdin)
|
||||||
|
|
||||||
|
# Try session_id first (unified sessions in Vibe 3.0+)
|
||||||
|
if session_id := payload.get("session_id"):
|
||||||
|
home = Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser()
|
||||||
|
unified = home / "logs" / "session" / "unified" / session_id
|
||||||
|
if unified.is_dir():
|
||||||
|
LOG.debug(f"Found unified session directory: {unified}")
|
||||||
|
return unified
|
||||||
|
LOG.warning(f"session_id directory not found: {unified}")
|
||||||
|
|
||||||
|
# Fallback to transcript_path (legacy sessions)
|
||||||
|
if tp := payload.get("transcript_path"):
|
||||||
|
path = Path(tp).expanduser()
|
||||||
|
if path.is_file():
|
||||||
|
LOG.debug(f"Using transcript_path: {path}")
|
||||||
|
return path.parent
|
||||||
|
LOG.warning(f"transcript_path not found: {path}")
|
||||||
|
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
LOG.debug(f"No valid JSON payload: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
LOG.warning(f"Error reading payload: {e}")
|
||||||
|
|
||||||
|
# Default fallback
|
||||||
|
default_dir = Path(
|
||||||
|
os.environ.get("VIBE_SESSION_DIR", "~/.vibe/logs/session/active")
|
||||||
|
).expanduser()
|
||||||
|
LOG.debug(f"Using default session directory: {default_dir}")
|
||||||
|
return default_dir
|
||||||
|
|
||||||
|
|
||||||
|
def format_cost_output(stdout: str) -> str:
|
||||||
|
"""Format the agent-cost-estimator JSON output into hook message format.
|
||||||
|
|
||||||
|
Expected format: provider:model | session:$XXX | turn:$YYY
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
data = json.loads(stdout)
|
||||||
|
# Extract provider and model from model_key (format: "provider:model")
|
||||||
|
model_key = data.get("pricing", {}).get("model_key", "unknown:unknown")
|
||||||
|
if ":" in model_key:
|
||||||
|
provider, model = model_key.split(":", 1)
|
||||||
|
model_str = f"{provider}:{model}"
|
||||||
|
else:
|
||||||
|
model_str = f"unknown:{model_key}"
|
||||||
|
|
||||||
|
# Extract costs
|
||||||
|
session_cost = data.get("cost_usd", {}).get("total_usd", 0)
|
||||||
|
last_turn = data.get("last_turn")
|
||||||
|
turn_cost = last_turn.get("cost_usd", {}).get("total_usd", 0) if last_turn else 0
|
||||||
|
|
||||||
|
# Format: provider:model | session:$XXX | turn:$YYY
|
||||||
|
return f"{model_str} | session:${session_cost:.2f} | turn:${turn_cost:.2f}"
|
||||||
|
except (json.JSONDecodeError, KeyError, TypeError) as e:
|
||||||
|
LOG.warning(f"Failed to parse output: {e}")
|
||||||
|
return stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main entry point for the hook."""
|
||||||
|
try:
|
||||||
|
session_dir = get_session_dir()
|
||||||
|
|
||||||
|
if not session_dir.is_dir():
|
||||||
|
LOG.error(f"Session directory does not exist: {session_dir}")
|
||||||
|
# Return empty to avoid breaking the session
|
||||||
|
print(json.dumps({"system_message": "-- [cost: session dir not found]"}))
|
||||||
|
return
|
||||||
|
|
||||||
|
LOG.debug(f"Processing session: {session_dir}")
|
||||||
|
|
||||||
|
# Build command - use --json for structured output
|
||||||
|
cmd = [
|
||||||
|
"agent-cost-estimator",
|
||||||
|
"--session-id", str(session_dir),
|
||||||
|
"--json"
|
||||||
|
]
|
||||||
|
|
||||||
|
# Run agent-cost-estimator
|
||||||
|
result = subprocess.run(
|
||||||
|
cmd,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=30
|
||||||
|
)
|
||||||
|
|
||||||
|
if result.returncode != 0:
|
||||||
|
LOG.error(f"agent-cost-estimator failed:\n{result.stderr}")
|
||||||
|
# Return error message
|
||||||
|
print(json.dumps({
|
||||||
|
"system_message": f"-- [cost: error - {result.stderr.strip()}]"
|
||||||
|
}))
|
||||||
|
return
|
||||||
|
|
||||||
|
# Format the output
|
||||||
|
message = format_cost_output(result.stdout)
|
||||||
|
print(json.dumps({"system_message": f"-- {message}"}))
|
||||||
|
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
LOG.error("agent-cost-estimator timed out after 30 seconds")
|
||||||
|
print(json.dumps({
|
||||||
|
"system_message": "-- [cost: timeout]"
|
||||||
|
}))
|
||||||
|
except Exception as e:
|
||||||
|
LOG.error(f"Unexpected error: {e}")
|
||||||
|
print(json.dumps({
|
||||||
|
"system_message": f"-- [cost: error - {str(e)}]"
|
||||||
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "agent-cost-estimator"
|
name = "agent-cost-estimator"
|
||||||
version = "0.2.1"
|
version = "0.3.0"
|
||||||
description = "Report token usage and estimated USD cost of AI agent sessions from local harness logs"
|
description = "Report token usage and estimated USD cost of AI agent sessions from local harness logs"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.9"
|
requires-python = ">=3.9"
|
||||||
|
|||||||
Reference in New Issue
Block a user