-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
executable file
·93 lines (73 loc) · 3.15 KB
/
monitor.py
File metadata and controls
executable file
·93 lines (73 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
Webhook listener that creates commits/tags when LiteLLM switches models.
Runs a simple Flask server to receive webhook events from LiteLLM proxy.
"""
import os
import subprocess
from datetime import datetime
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
# Config
DISCORD_WEBHOOK = os.getenv("DISCORD_WEBHOOK_URL")
GIT_AUTO_COMMIT = os.getenv("GIT_AUTO_COMMIT", "true").lower() == "true"
def git_commit_and_tag(model_name: str, event_type: str):
"""Create a commit and tag for model switch."""
try:
# Check if there are changes to commit
status = subprocess.run(
["git", "status", "--porcelain"],
capture_output=True, text=True, check=True
)
if not status.stdout.strip():
print(f"No changes to commit for {model_name}")
return
# Commit with descriptive message
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
commit_msg = f"Auto-commit before switch to {model_name}\n\nTimestamp: {timestamp}\nEvent: {event_type}"
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", commit_msg], check=True)
# Create lightweight tag
tag_name = f"model-{model_name}-{datetime.now().strftime('%Y%m%d-%H%M%S')}"
subprocess.run(["git", "tag", tag_name], check=True)
print(f"✓ Created commit and tag: {tag_name}")
return tag_name
except subprocess.CalledProcessError as e:
print(f"Git operation failed: {e}")
return None
def send_discord(message: str):
"""Send notification to Discord."""
if not DISCORD_WEBHOOK:
return
try:
requests.post(DISCORD_WEBHOOK, json={"content": message}, timeout=5)
except Exception as e:
print(f"Discord notification failed: {e}")
@app.route("/webhook", methods=["POST"])
def webhook():
"""Handle LiteLLM webhook events."""
data = request.json
# Extract model info
model = data.get("model", "unknown")
event_type = data.get("event_type", data.get("status", "unknown"))
print(f"\n📊 Webhook received: {event_type} | Model: {model}")
# On fallback or rate limit, create commit
if "fallback" in event_type.lower() or "rate_limit" in str(data).lower():
if GIT_AUTO_COMMIT:
tag = git_commit_and_tag(model, event_type)
# Notify
msg = f"🔄 **Model Switch Detected**\nSwitching to: `{model}`\nTag: `{tag}`"
send_discord(msg)
print(msg)
return jsonify({"status": "ok"}), 200
@app.route("/health", methods=["GET"])
def health():
"""Health check endpoint."""
return jsonify({"status": "healthy", "service": "claude-fallback-monitor"}), 200
if __name__ == "__main__":
print("🚀 Starting Claude Fallback Monitor...")
print(f" Auto-commit: {GIT_AUTO_COMMIT}")
print(f" Discord: {'Enabled' if DISCORD_WEBHOOK else 'Disabled'}")
print("\n Listening for webhooks on http://localhost:5000/webhook\n")
app.run(host="0.0.0.0", port=5000, debug=False)