Compare commits

...

13 Commits

Author SHA1 Message Date
chrome-storm-c442
8ff62d8f11 v1.9.42: full Grafana/Prometheus GUI & CLI improvements
Grafana tab:
- Datasources table (Name, Type, URL, Default)
- Open Grafana button (opens browser)
- Switch to AlertManager endpoint for real-time active alerts

Prometheus tab:
- Quick query buttons (up, CPU, Memory)
- Metrics browser popup with filter (loads all metric names)
- Rules section (recording + alerting rules Treeview)

CLI:
- --grafana-datasources ALIAS
- --prom-rules ALIAS

i18n: 28 new keys (EN/RU/ZH)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 10:23:33 -05:00
chrome-storm-c442
6a2db542c3 v1.9.41: fix Grafana/Prometheus tabs — pass server dict to clients
- GrafanaTab/PrometheusTab: use store.get_server(alias) to get server
  dict instead of passing (alias, store) to client constructors
- Fix PrometheusTab: targets() and alerts() method names + parse response
- Fix GrafanaTab: build dashboard URL from client.base_url

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 10:08:59 -05:00
chrome-storm-c442
d49fa9ce90 v1.9.40: Grafana basic auth support (user/password + api_token)
- Add user/password fields to Grafana in server dialog FIELD_MAP
- GrafanaClient: support basic auth when no api_token is set
- ssh.py: _grafana_request supports basic auth fallback

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 10:04:09 -05:00
IPGO Developer
df40af5632 v1.9.39: add Linux release to releases/linux/
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 14:44:30 +00:00
IPGO Developer
fb319afbd5 v1.9.39: Linux build with merged remote changes
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 14:43:01 +00:00
cosmic-frost-8b10
feff23fba9 fix: add PIL._tkinter_finder hidden import for Linux PyInstaller build
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 14:34:22 +00:00
chrome-storm-c442
c3124aeb7d v1.9.38: fix header controls overlay width hiding Setup tab
Remove relwidth=0.4 from header controls place() so buttons use
natural width and don't overlap rightmost tab buttons.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 09:06:52 -05:00
chrome-storm-c442
a7b4850c47 v1.9.37: align tab buttons with header controls on same Y-level
Removed separate header_bar, overlay header controls (language, update,
about) directly on tabview's tab row using place(). Tabs and controls
now sit in the same horizontal line — no vertical gap.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 09:01:16 -05:00
chrome-storm-c442
f5c91adac8 v1.9.36: document Claude Code image read crash bug & workarounds
- CLAUDE.md: add rule to never read images directly, only via Agent
- CLAUDE.md: add rule to never use chrome-devtools take_screenshot directly
- BUG_REPORT_CLAUDE_CODE_PNG_CRASH.md: full root cause analysis
- tools/patch_claude_code.js: v2 patcher with mapper media_type fix

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 08:31:27 -05:00
chrome-storm-c442
33e15827ce v1.9.35: eliminate CTkTabview internal top spacing to raise tabs
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 05:53:55 -05:00
chrome-storm-c442
73bcac8a55 v1.9.34: reduce header bar height and padding to raise tabview
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 05:52:23 -05:00
chrome-storm-c442
05706182df v1.9.33: raise tabview closer to header icons (remove top padding)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 05:50:20 -05:00
chrome-storm-c442
c4fae6a9c1 v1.9.32: full OFFLINE text on terminal overlay, font size 72
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 05:48:21 -05:00
19 changed files with 831 additions and 98 deletions

View File

@@ -0,0 +1,142 @@
# Bug Report: Claude Code CLI crashes when reading large image files
## Summary
The `Read` tool in Claude Code CLI fails when reading images larger than ~25K base64 tokens (~150KB file size). Small images work fine. The root cause is in the `DP1` image compression pipeline — when a large image goes through compression, the resulting API content block ends up with `source: {type: "base64"}` but **missing both `data` and `media_type` fields**. This causes an unrecoverable API 400 error.
## Environment
- **Claude Code CLI:** `@anthropic-ai/claude-code@2.1.70`
- **OS:** Windows 10 Pro for Workstations 10.0.19045
- **Node.js:** v24.13.1
- **sharp:** 0.34.5 (manually installed, works correctly)
## Root Cause Analysis
### The Size Threshold
Images are read by `Nv8()` which calls `q01()` to create the result. After `q01()`, a size check runs:
```javascript
if (Math.ceil($.file.base64.length * 0.125) > q) // q = Tv8() = 25000 tokens
```
- **Small images** (< ~150KB file / < 25K tokens base64): Skip `DP1`, return directly from `q01()`**WORKS**
- **Large images** (> ~150KB file / > 25K tokens base64): Enter `DP1` compression path → **CRASHES**
### What happens in the DP1 path
When the image exceeds the token limit, `DP1()` is called to compress it. `DP1` uses sharp to resize/recompress and returns `{base64, mediaType, originalSize}`. The code then returns:
```javascript
return {type: "image", file: {base64: H.base64, type: H.mediaType, originalSize: z}}
```
In isolation, this looks correct. `H.mediaType` is `"image/jpeg"` (from `vp6()` inside `DP1`).
### Where it actually breaks
The tool result mapper converts this to an API content block:
```javascript
case "image": return {
tool_use_id: q,
type: "tool_result",
content: [{
type: "image",
source: {type: "base64", data: A.file.base64, media_type: A.file.type}
}]
};
```
**However**, between the mapper output and the actual API request, the image content block gets **stripped**. The API receives:
```json
{"type": "image", "source": {"type": "base64"}}
```
Both `data` and `media_type` are absent. `JSON.stringify` silently drops `undefined` properties, so if both become `undefined` at any point, the serialized JSON omits them entirely.
### Evidence from transcript analysis
The session transcript (`.jsonl` output) captured the exact message content sent to the API:
```json
{
"type": "user",
"content": [{
"tool_use_id": "toolu_01NmuSjPErhBfbtoV8RBrJip",
"type": "tool_result",
"content": [{"type": "image", "source": {"type": "base64"}}]
}]
}
```
This confirms `data` and `media_type` are both missing at the API call level.
### The actual root cause (suspected)
The image data stripping likely occurs in the **message normalization/storage layer** between the tool result mapper and the API call. When conversation messages are stored in memory (the internal `D` array or conversation state), large base64 image data may be:
1. Stripped for memory efficiency
2. Moved to a separate image attachment store (referenced by `imagePasteIds`)
3. Lost during `structuredClone` or message serialization
The reconstruction step that should restore the image data before the API call **fails for tool_result image blocks**, possibly because it only handles top-level image blocks (from user pastes) but not images nested inside `tool_result.content[]`.
## Test Results
| File | Size | Base64 tokens | DP1 path | Result |
|------|------|---------------|----------|--------|
| photo.jpg | 25KB | ~4,250 | No | **Works** |
| test_tiny.png | 98B | ~16 | No | **Works** |
| test_medium.png | 751KB | ~125,000 | Yes | **Crashes** |
| screenshot_gui.png | 387KB | ~64,500 | Yes | **Crashes** |
## Severity: Critical
- **Session-killing:** corrupted message poisons the entire conversation context
- **No recovery:** every subsequent API call fails with 400
- **Affects subagents too:** Agent tool crashes, but main session survives
- **Size-dependent:** only images > ~150KB trigger the bug
## Patches Applied
### Patch 1: Nv8 try/catch wrapper (`PATCHED_NV8_SAFE_IMAGE_READ`)
Wraps the entire `Nv8` function in try/catch. On failure, returns a text error message instead of corrupted binary. Also adds `||"image/png"` fallback on `H.mediaType` in the DP1 path.
### Patch 2: Image mapper media_type fallback (`PATCHED_IMAGE_MEDIA_TYPE`)
Adds `||"image/png"` fallback to `media_type` in the tool result mapper. Prevents `undefined` from being serialized as absent field.
### Effectiveness
- Patches only work after restarting Claude Code (cli.js is loaded once at startup)
- Patches fix the `media_type` issue but may NOT fix the missing `data` issue
- The underlying cause (image data being stripped from stored messages) needs to be fixed upstream
## Patcher Tool
```bash
node tools/patch_claude_code.js # Apply all patches
node tools/patch_claude_code.js --check # Check status
node tools/patch_claude_code.js --revert # Revert to backup
```
After updating Claude Code (`npm update -g @anthropic-ai/claude-code`), re-run the patcher.
## Workarounds
1. **Use subagent for ALL image reading** — crashes in isolation, main session survives
2. **Resize large images before reading** — keep under ~150KB
3. **Read images only via Bash tool**`file screenshot.png` for metadata, avoid actual content
## Files Referenced
- **Patcher:** `tools/patch_claude_code.js`
- **CLI entry:** `node_modules/@anthropic-ai/claude-code/cli.js` (minified, ~13K lines)
- **Key functions:** `Nv8` (image reader), `DP1` (compressor), `q01` (result builder), `ig` (sharp wrapper), `mapToolResultToToolResultBlockParam` (API mapper)
## Report Info
- **Date:** 2026-03-06
- **Version:** Claude Code 2.1.70
- **Reproducible:** 100% on Windows with any image > ~150KB

View File

@@ -4,6 +4,20 @@
ServerManager — **кроссплатформенное** Desktop GUI (CustomTkinter) для управления удалёнными серверами **любых типов**. Работает на **Windows и Linux**. Claude Code управляет серверами через скилл `/ssh`, зная только алиасы.
## КРИТИЧНО: Чтение изображений — ТОЛЬКО через Agent
**Claude Code CLI v2.1.70 баг:** Read tool крашит сессию на изображениях >150KB.
Большие изображения проходят через DP1-сжатие, где `data` и `media_type` теряются → API 400 → сессия мертва навсегда.
**ПРАВИЛА:**
- **НИКОГДА** не вызывать `Read` на .png/.jpg/.jpeg/.gif/.webp напрямую
- **НИКОГДА** не делать скриншоты через `mcp__chrome-devtools__take_screenshot` — результат тоже изображение, крашит так же
- **ВСЕГДА** читать изображения через `Agent` tool (субагент) — он упадёт изолированно, основная сессия выживет
- Маленькие (<150KB) работают, но **не рисковать** — всё через агента
- НЕ тестировать фиксы изображений в основном контексте даже после "исправления"
Подробности: `BUG_REPORT_CLAUDE_CODE_PNG_CRASH.md`
## КРОССПЛАТФОРМЕННОСТЬ — ОБЯЗАТЕЛЬНО
Приложение собирается и работает на **Windows** и **Linux**. При любых правках кода:

View File

@@ -133,6 +133,7 @@ def build():
cmd_parts.extend([
"--hidden-import", "customtkinter",
"--hidden-import", "PIL",
"--hidden-import", "PIL._tkinter_finder",
"--hidden-import", "pyotp",
"--hidden-import", "pyte",
"--hidden-import", "psutil",

View File

@@ -20,19 +20,25 @@ class GrafanaClient:
Initialize the Grafana client.
Args:
server: dict with keys: ip, port, api_token, use_ssl
server: dict with keys: ip, port, api_token (or user+password), use_ssl
"""
self.ip: str = server["ip"]
self.port: int = int(server["port"])
self.api_token: str = server["api_token"]
self.api_token: str = server.get("api_token", "")
self.user: str = server.get("user", "")
self.password: str = server.get("password", "")
self.use_ssl: bool = bool(server.get("use_ssl", False))
scheme = "https" if self.use_ssl else "http"
self.base_url: str = f"{scheme}://{self.ip}:{self.port}"
self.headers: dict[str, str] = {
"Authorization": f"Bearer {self.api_token}",
"Content-Type": "application/json",
}
self.headers: dict[str, str] = {"Content-Type": "application/json"}
self.auth: tuple[str, str] | None = None
if self.api_token:
self.headers["Authorization"] = f"Bearer {self.api_token}"
elif self.user and self.password:
self.auth = (self.user, self.password)
self.timeout: int = 10
def _get(self, path: str, params: dict | None = None) -> Any:
@@ -42,7 +48,7 @@ class GrafanaClient:
url = f"{self.base_url}{path}"
log.debug("Grafana GET %s", url)
resp = requests.get(
url, headers=self.headers, params=params, timeout=self.timeout
url, headers=self.headers, params=params, auth=self.auth, timeout=self.timeout
)
resp.raise_for_status()
return resp.json()
@@ -54,7 +60,7 @@ class GrafanaClient:
url = f"{self.base_url}{path}"
log.debug("Grafana POST %s", url)
resp = requests.post(
url, headers=self.headers, json=json_data, timeout=self.timeout
url, headers=self.headers, json=json_data, auth=self.auth, timeout=self.timeout
)
resp.raise_for_status()
return resp.json()
@@ -132,6 +138,16 @@ class GrafanaClient:
log.error("Grafana list_alerts failed: %s", exc)
return []
def get_active_alerts(self) -> list[dict]:
"""List active (firing) alerts via AlertManager endpoint."""
try:
results = self._get("/api/alertmanager/grafana/api/v2/alerts")
log.info("Grafana: %d active alerts", len(results))
return results
except Exception as exc:
log.error("Grafana get_active_alerts failed: %s", exc)
return []
def list_datasources(self) -> list[dict]:
"""
List all datasources via GET /api/datasources.

View File

@@ -111,7 +111,7 @@ _EN = {
"term_connecting": "Connecting to {alias}...",
"term_connected": "Connected to {alias}",
"term_disconnected": "Disconnected",
"term_off": "OFF",
"term_off": "OFFLINE",
"ctx_disconnect": "Disconnect",
"term_click_to_connect": "Double-click to connect to {alias}",
"sftp_click_to_connect": "Double-click server to browse files",
@@ -429,6 +429,19 @@ _EN = {
"grafana_connected": "Connected to {alias}",
"grafana_no_dashboards": "No dashboards found",
"grafana_no_alerts": "No alerts",
"grafana_loading": "Loading...",
"grafana_loaded": "{dashboards} dashboards, {alerts} alerts, {datasources} datasources",
"grafana_no_server": "No server selected",
"grafana_open_browser": "Open Grafana",
"grafana_datasources": "Datasources",
"grafana_ds_name": "Name",
"grafana_ds_type": "Type",
"grafana_ds_default": "Default",
"grafana_dash_title": "Title",
"grafana_dash_folder": "Folder",
"grafana_alert_state": "State",
"grafana_alert_name": "Name",
"grafana_alert_severity": "Severity",
# Prometheus tab
"prom_refresh": "Refresh",
@@ -444,6 +457,23 @@ _EN = {
"prom_no_targets": "No targets",
"prom_no_alerts": "No alerts",
"prom_placeholder": "up",
"prom_loading": "Loading...",
"prom_loaded": "{targets} targets, {alerts} alerts, {rules} rules",
"prom_no_server": "No server selected",
"prom_executing": "Executing...",
"prom_results": "Results",
"prom_query_placeholder": "e.g. up, node_cpu_seconds_total",
"prom_metrics_browser": "Metrics",
"prom_filter_metrics": "Filter metrics...",
"prom_rules": "Rules",
"prom_rule_type": "Type",
"prom_rule_name": "Name",
"prom_rule_group": "Group",
"prom_rule_health": "Health",
"prom_target_job": "Job",
"prom_target_instance": "Instance",
"prom_target_health": "Health",
"prom_target_scrape": "Last Scrape",
# PowerShell tab
"ps_execute": "Execute",
@@ -638,7 +668,7 @@ _RU = {
"term_connecting": "Подключение к {alias}...",
"term_connected": "Подключено к {alias}",
"term_disconnected": "Отключено",
"term_off": "ОТКЛ",
"term_off": "ОТКЛЮЧЕНО",
"ctx_disconnect": "Отключиться",
"term_click_to_connect": "Двойной клик для подключения к {alias}",
"sftp_click_to_connect": "Двойной клик для просмотра файлов",
@@ -956,6 +986,19 @@ _RU = {
"grafana_connected": "Подключено к {alias}",
"grafana_no_dashboards": "Дашборды не найдены",
"grafana_no_alerts": "Нет оповещений",
"grafana_loading": "Загрузка...",
"grafana_loaded": "{dashboards} дашб., {alerts} оповещ., {datasources} источн.",
"grafana_no_server": "Сервер не выбран",
"grafana_open_browser": "Открыть Grafana",
"grafana_datasources": "Источники данных",
"grafana_ds_name": "Имя",
"grafana_ds_type": "Тип",
"grafana_ds_default": "По умолч.",
"grafana_dash_title": "Название",
"grafana_dash_folder": "Папка",
"grafana_alert_state": "Состояние",
"grafana_alert_name": "Имя",
"grafana_alert_severity": "Серьёзность",
# Prometheus tab
"prom_refresh": "Обновить",
@@ -971,6 +1014,23 @@ _RU = {
"prom_no_targets": "Нет целей",
"prom_no_alerts": "Нет оповещений",
"prom_placeholder": "up",
"prom_loading": "Загрузка...",
"prom_loaded": "{targets} целей, {alerts} оповещ., {rules} правил",
"prom_no_server": "Сервер не выбран",
"prom_executing": "Выполнение...",
"prom_results": "Результаты",
"prom_query_placeholder": "напр. up, node_cpu_seconds_total",
"prom_metrics_browser": "Метрики",
"prom_filter_metrics": "Фильтр метрик...",
"prom_rules": "Правила",
"prom_rule_type": "Тип",
"prom_rule_name": "Имя",
"prom_rule_group": "Группа",
"prom_rule_health": "Здоровье",
"prom_target_job": "Job",
"prom_target_instance": "Инстанс",
"prom_target_health": "Здоровье",
"prom_target_scrape": "Последний опрос",
# PowerShell tab
"ps_execute": "Выполнить",
@@ -1165,7 +1225,7 @@ _ZH = {
"term_connecting": "正在连接 {alias}...",
"term_connected": "已连接到 {alias}",
"term_disconnected": "已断开",
"term_off": "已断开",
"term_off": "未连接",
"ctx_disconnect": "断开连接",
"term_click_to_connect": "双击连接 {alias}",
"sftp_click_to_connect": "双击服务器浏览文件",
@@ -1483,6 +1543,19 @@ _ZH = {
"grafana_connected": "已连接到 {alias}",
"grafana_no_dashboards": "未找到仪表盘",
"grafana_no_alerts": "无告警",
"grafana_loading": "加载中...",
"grafana_loaded": "{dashboards}仪表盘, {alerts}告警, {datasources}数据源",
"grafana_no_server": "未选择服务器",
"grafana_open_browser": "打开Grafana",
"grafana_datasources": "数据源",
"grafana_ds_name": "名称",
"grafana_ds_type": "类型",
"grafana_ds_default": "默认",
"grafana_dash_title": "标题",
"grafana_dash_folder": "文件夹",
"grafana_alert_state": "状态",
"grafana_alert_name": "名称",
"grafana_alert_severity": "严重程度",
# Prometheus tab
"prom_refresh": "刷新",
@@ -1498,6 +1571,23 @@ _ZH = {
"prom_no_targets": "无目标",
"prom_no_alerts": "无告警",
"prom_placeholder": "up",
"prom_loading": "加载中...",
"prom_loaded": "{targets}目标, {alerts}告警, {rules}规则",
"prom_no_server": "未选择服务器",
"prom_executing": "执行中...",
"prom_results": "结果",
"prom_query_placeholder": "例如 up, node_cpu_seconds_total",
"prom_metrics_browser": "指标",
"prom_filter_metrics": "过滤指标...",
"prom_rules": "规则",
"prom_rule_type": "类型",
"prom_rule_name": "名称",
"prom_rule_group": "",
"prom_rule_health": "健康",
"prom_target_job": "任务",
"prom_target_instance": "实例",
"prom_target_health": "健康",
"prom_target_scrape": "最后抓取",
# PowerShell tab
"ps_execute": "执行",

View File

@@ -169,15 +169,13 @@ class App(ctk.CTk):
self._main_frame = ctk.CTkFrame(self._paned, fg_color="transparent")
self._paned.add(self._main_frame, minsize=500)
# Header bar (language + about)
header_bar = ctk.CTkFrame(self._main_frame, fg_color="transparent", height=40)
header_bar.pack(fill="x", padx=10, pady=(8, 0))
header_bar.pack_propagate(False)
# Header controls — overlay frame placed on top of tabview's tab row
self._header_controls = ctk.CTkFrame(self._main_frame, fg_color="transparent", height=30)
# Language selector
_lang_img = ctk_icon("globe", 18)
self._lang_icon = ctk.CTkLabel(
header_bar, text="" if _lang_img else "\U0001f310",
self._header_controls, text="" if _lang_img else "\U0001f310",
image=_lang_img, font=ctk.CTkFont(size=14), width=20,
)
self._lang_icon.pack(side="right", padx=(5, 0))
@@ -185,17 +183,17 @@ class App(ctk.CTk):
current_display = LANGUAGES.get(i18n.get_language(), "English")
self._lang_var = ctk.StringVar(value=current_display)
self.lang_menu = ctk.CTkOptionMenu(
header_bar, values=lang_values, variable=self._lang_var,
width=110, height=30, command=self._change_language
self._header_controls, values=lang_values, variable=self._lang_var,
width=110, height=26, command=self._change_language
)
self.lang_menu.pack(side="right", padx=(5, 0))
# Check Updates button
_sync_img = ctk_icon("refresh", 18)
self._update_check_btn = ctk.CTkButton(
header_bar, text="" if _sync_img else "\u21bb",
image=_sync_img, width=30, height=30,
corner_radius=15, fg_color="#6b7280", hover_color="#4b5563",
self._header_controls, text="" if _sync_img else "\u21bb",
image=_sync_img, width=26, height=26,
corner_radius=13, fg_color="#6b7280", hover_color="#4b5563",
command=self._check_updates_manual,
)
self._update_check_btn.pack(side="right", padx=(5, 0))
@@ -203,12 +201,12 @@ class App(ctk.CTk):
# About button
_info_img = ctk_icon("info", 18)
self.about_btn = ctk.CTkButton(
header_bar, text="" if _info_img else "",
image=_info_img, width=30, height=30,
corner_radius=15, fg_color="#6b7280", hover_color="#4b5563",
self._header_controls, text="" if _info_img else "",
image=_info_img, width=26, height=26,
corner_radius=13, fg_color="#6b7280", hover_color="#4b5563",
command=self._show_about
)
self.about_btn.pack(side="right", padx=(5, 5))
self.about_btn.pack(side="right", padx=(5, 0))
# Update banner (hidden by default)
self._update_banner = None
@@ -250,7 +248,14 @@ class App(ctk.CTk):
# Create new tabview
self.tabview = ctk.CTkTabview(self._main_frame, command=self._on_tab_changed)
self.tabview.pack(fill="both", expand=True, padx=10, pady=10)
self.tabview._outer_spacing = 0
self.tabview._outer_button_overhang = 0
self.tabview._configure_grid()
self.tabview.pack(fill="both", expand=True, padx=10, pady=(4, 10))
# Overlay header controls on top-right of tabview (same row as tab buttons)
self._header_controls.lift()
self._header_controls.place(in_=self.tabview, relx=1.0, y=0, anchor="ne")
for key in self._tab_keys:
self.tabview.add(_tab_label(key))

View File

@@ -20,7 +20,7 @@ FIELD_MAP = {
"mssql": ["user", "password", "database"],
"postgresql": ["user", "password", "database"],
"redis": ["password", "db_index", "use_ssl"],
"grafana": ["api_token", "use_ssl"],
"grafana": ["user", "password", "api_token", "use_ssl"],
"prometheus": ["use_ssl"],
"rdp": ["user", "password", "rdp_resolution", "rdp_quality", "rdp_clipboard", "rdp_drives", "rdp_printers"],
"vnc": ["password"],

View File

@@ -1,5 +1,5 @@
"""
Grafana tab — dashboards browser and alerts overview.
Grafana tab — dashboards browser, active alerts, and datasources overview.
"""
import threading
@@ -9,7 +9,7 @@ from tkinter import ttk
import customtkinter as ctk
from core.grafana_client import GrafanaClient
from core.i18n import t
from core.icons import icon_text, make_icon_button, reconfigure_icon_button
from core.icons import make_icon_button, reconfigure_icon_button
from gui.tabs.query_tab import apply_dark_scrollbar_style
@@ -25,18 +25,23 @@ class GrafanaTab(ctk.CTkFrame):
def _build_ui(self):
apply_dark_scrollbar_style()
# ── Header + Refresh ──
# ── Header + buttons ──
header_frame = ctk.CTkFrame(self, fg_color="transparent")
header_frame.pack(fill="x", padx=15, pady=(15, 5))
title = ctk.CTkLabel(header_frame, text=t("grafana_title"),
title = ctk.CTkLabel(header_frame, text="Grafana",
font=ctk.CTkFont(size=18, weight="bold"))
title.pack(side="left")
self._refresh_btn = make_icon_button(header_frame, "refresh", t("grafana_refresh"), width=110,
self._refresh_btn = make_icon_button(header_frame, "refresh", t("grafana_refresh"), width=100,
command=self._refresh)
self._refresh_btn.pack(side="right")
self._open_btn = make_icon_button(header_frame, "browser", t("grafana_open_browser"), width=130,
fg_color="#6b7280", hover_color="#4b5563",
command=self._open_grafana)
self._open_btn.pack(side="right", padx=(0, 5))
# ── Dashboards section ──
dash_label = ctk.CTkLabel(self, text=t("grafana_dashboards"),
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
@@ -47,7 +52,7 @@ class GrafanaTab(ctk.CTkFrame):
columns = ("uid", "title", "folder")
self._dash_tree = ttk.Treeview(dash_frame, columns=columns, show="headings",
selectmode="browse", height=8)
selectmode="browse", height=6)
self._dash_tree.heading("uid", text="UID")
self._dash_tree.heading("title", text=t("grafana_dash_title"))
self._dash_tree.heading("folder", text=t("grafana_dash_folder"))
@@ -59,7 +64,6 @@ class GrafanaTab(ctk.CTkFrame):
dash_scroll = ttk.Scrollbar(dash_frame, orient="vertical", command=self._dash_tree.yview, style="Dark.Vertical.TScrollbar")
dash_scroll.pack(side="right", fill="y")
self._dash_tree.configure(yscrollcommand=dash_scroll.set)
self._dash_tree.bind("<Double-1>", self._on_dashboard_click)
# ── Alerts section ──
@@ -72,7 +76,7 @@ class GrafanaTab(ctk.CTkFrame):
alert_columns = ("state", "name", "severity")
self._alerts_tree = ttk.Treeview(alerts_frame, columns=alert_columns, show="headings",
selectmode="browse", height=6)
selectmode="browse", height=5)
self._alerts_tree.heading("state", text=t("grafana_alert_state"))
self._alerts_tree.heading("name", text=t("grafana_alert_name"))
self._alerts_tree.heading("severity", text=t("grafana_alert_severity"))
@@ -85,6 +89,31 @@ class GrafanaTab(ctk.CTkFrame):
alerts_scroll.pack(side="right", fill="y")
self._alerts_tree.configure(yscrollcommand=alerts_scroll.set)
# ── Datasources section ──
ds_label = ctk.CTkLabel(self, text=t("grafana_datasources"),
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
ds_label.pack(fill="x", padx=15, pady=(10, 3))
ds_frame = ctk.CTkFrame(self, fg_color="transparent")
ds_frame.pack(fill="both", expand=True, padx=15, pady=(0, 5))
ds_columns = ("name", "type", "url", "default")
self._ds_tree = ttk.Treeview(ds_frame, columns=ds_columns, show="headings",
selectmode="browse", height=4)
self._ds_tree.heading("name", text=t("grafana_ds_name"))
self._ds_tree.heading("type", text=t("grafana_ds_type"))
self._ds_tree.heading("url", text="URL")
self._ds_tree.heading("default", text=t("grafana_ds_default"))
self._ds_tree.column("name", width=150, minwidth=100)
self._ds_tree.column("type", width=120, minwidth=80)
self._ds_tree.column("url", width=250, minwidth=120)
self._ds_tree.column("default", width=60, minwidth=40)
self._ds_tree.pack(side="left", fill="both", expand=True)
ds_scroll = ttk.Scrollbar(ds_frame, orient="vertical", command=self._ds_tree.yview, style="Dark.Vertical.TScrollbar")
ds_scroll.pack(side="right", fill="y")
self._ds_tree.configure(yscrollcommand=ds_scroll.set)
# ── Status bar ──
self._status_bar = ctk.CTkLabel(self, text=t("grafana_no_server"), anchor="w",
font=ctk.CTkFont(size=11), text_color="#9ca3af")
@@ -93,7 +122,6 @@ class GrafanaTab(ctk.CTkFrame):
# ── Public API ──
def set_server(self, alias: str | None):
"""Called when user selects a server in sidebar."""
self._current_alias = alias
self._client = None
self._dashboards.clear()
@@ -109,24 +137,26 @@ class GrafanaTab(ctk.CTkFrame):
def _refresh(self):
if not self._current_alias:
self._set_status(t("no_server_selected"), "#ef4444")
self._set_status(t("grafana_no_server"), "#ef4444")
return
self._refresh_btn.configure(state="disabled", text=t("grafana_loading"))
self._refresh_btn.configure(state="disabled")
self._set_status(t("grafana_loading"), "#ccaa00")
def _do():
try:
client = self._get_client()
dashboards = client.list_dashboards()
alerts = client.list_alerts()
alerts = client.get_active_alerts()
datasources = client.list_datasources()
self.after(0, lambda: self._populate_dashboards(dashboards))
self.after(0, lambda: self._populate_alerts(alerts))
self.after(0, lambda: self._populate_datasources(datasources))
self.after(0, lambda: self._set_status(
t("grafana_loaded").format(
dashboards=len(dashboards), alerts=len(alerts)
dashboards=len(dashboards), alerts=len(alerts),
datasources=len(datasources)
), "#22c55e"))
except Exception as e:
self.after(0, lambda: self._set_status(f"(error) {e}", "#ef4444"))
@@ -140,7 +170,10 @@ class GrafanaTab(ctk.CTkFrame):
def _get_client(self) -> GrafanaClient:
if self._client is None:
self._client = GrafanaClient(self._current_alias, self.store)
server = self.store.get_server(self._current_alias)
if not server:
raise ValueError(f"Server '{self._current_alias}' not found")
self._client = GrafanaClient(server)
return self._client
# ── Table population ──
@@ -157,28 +190,37 @@ class GrafanaTab(ctk.CTkFrame):
def _populate_alerts(self, alerts: list[dict]):
self._alerts_tree.delete(*self._alerts_tree.get_children())
for a in alerts:
state = a.get("state", a.get("status", "unknown"))
name = a.get("name", a.get("title", ""))
severity = a.get("severity", a.get("labels", {}).get("severity", ""))
status = a.get("status", {})
state = status.get("state", "unknown") if isinstance(status, dict) else str(status)
labels = a.get("labels", {})
name = labels.get("alertname", a.get("name", ""))
severity = labels.get("severity", "---")
tag = ""
if state in ("alerting", "firing"):
if state in ("active", "firing", "alerting"):
tag = "alerting"
elif state in ("ok", "normal", "inactive"):
elif state in ("suppressed", "resolved", "inactive"):
tag = "ok"
self._alerts_tree.insert("", "end", values=(state, name, severity), tags=(tag,))
# Color-code alert states
self._alerts_tree.tag_configure("alerting", foreground="#ef4444")
self._alerts_tree.tag_configure("ok", foreground="#22c55e")
def _populate_datasources(self, datasources: list[dict]):
self._ds_tree.delete(*self._ds_tree.get_children())
for ds in datasources:
name = ds.get("name", "")
ds_type = ds.get("type", "")
url = ds.get("url", "")
is_default = "Yes" if ds.get("isDefault", False) else ""
self._ds_tree.insert("", "end", values=(name, ds_type, url, is_default))
def _clear_tables(self):
self._dash_tree.delete(*self._dash_tree.get_children())
self._alerts_tree.delete(*self._alerts_tree.get_children())
self._ds_tree.delete(*self._ds_tree.get_children())
# ── Events ──
def _on_dashboard_click(self, _event):
"""Open dashboard URL in browser on double-click."""
selection = self._dash_tree.selection()
if not selection:
return
@@ -186,21 +228,26 @@ class GrafanaTab(ctk.CTkFrame):
uid = item["values"][0] if item["values"] else None
if not uid:
return
# Find the dashboard data to get the URL
for d in self._dashboards:
if d.get("uid") == uid:
url = d.get("url", "")
if url:
try:
client = self._get_client()
full_url = client.get_dashboard_url(url)
webbrowser.open(full_url)
webbrowser.open(f"{client.base_url}{url}")
except Exception:
# Fallback: just open relative URL
webbrowser.open(url)
break
def _open_grafana(self):
if not self._current_alias:
return
try:
client = self._get_client()
webbrowser.open(client.base_url)
except Exception:
pass
# ── Helpers ──
def _set_status(self, text: str, color: str = "#9ca3af"):

View File

@@ -1,5 +1,5 @@
"""
Prometheus tab — PromQL query executor, targets overview, and alerts.
Prometheus tab — PromQL query executor, targets overview, alerts, and rules.
"""
import threading
@@ -8,7 +8,7 @@ from tkinter import ttk
import customtkinter as ctk
from core.prometheus_client import PrometheusClient
from core.i18n import t
from core.icons import icon_text, make_icon_button, reconfigure_icon_button
from core.icons import make_icon_button, reconfigure_icon_button
from gui.tabs.query_tab import apply_dark_scrollbar_style
@@ -41,19 +41,35 @@ class PrometheusTab(ctk.CTkFrame):
command=self._execute_query)
self._exec_btn.pack(side="left")
# ── Quick query buttons ──
quick_frame = ctk.CTkFrame(self, fg_color="transparent")
quick_frame.pack(fill="x", padx=15, pady=(0, 5))
for label, query in [("up", "up"), ("CPU", "process_cpu_seconds_total"),
("Memory", "node_memory_MemFree_bytes")]:
btn = make_icon_button(quick_frame, "metrics", label, width=80,
fg_color="#6b7280", hover_color="#4b5563",
command=lambda q=query: self._run_quick(q))
btn.pack(side="left", padx=(0, 5))
self._metrics_btn = make_icon_button(quick_frame, "search", t("prom_metrics_browser"), width=100,
fg_color="#6b7280", hover_color="#4b5563",
command=self._open_metrics_browser)
self._metrics_btn.pack(side="left", padx=(0, 5))
# ── Query results ──
results_label = ctk.CTkLabel(self, text=t("prom_results"),
font=ctk.CTkFont(size=12, weight="bold"), anchor="w")
results_label.pack(fill="x", padx=15, pady=(10, 3))
results_label.pack(fill="x", padx=15, pady=(5, 3))
self._results_box = ctk.CTkTextbox(self, height=150,
self._results_box = ctk.CTkTextbox(self, height=120,
font=ctk.CTkFont(family="Consolas", size=12),
state="disabled")
self._results_box.pack(fill="x", padx=15, pady=(0, 5))
# ── Targets section ──
targets_header = ctk.CTkFrame(self, fg_color="transparent")
targets_header.pack(fill="x", padx=15, pady=(10, 3))
targets_header.pack(fill="x", padx=15, pady=(5, 3))
targets_label = ctk.CTkLabel(targets_header, text=t("prom_targets"),
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
@@ -68,7 +84,7 @@ class PrometheusTab(ctk.CTkFrame):
target_columns = ("job", "instance", "health", "last_scrape")
self._targets_tree = ttk.Treeview(targets_frame, columns=target_columns, show="headings",
selectmode="browse", height=6)
selectmode="browse", height=5)
self._targets_tree.heading("job", text=t("prom_target_job"))
self._targets_tree.heading("instance", text=t("prom_target_instance"))
self._targets_tree.heading("health", text=t("prom_target_health"))
@@ -88,13 +104,39 @@ class PrometheusTab(ctk.CTkFrame):
# ── Alerts section ──
alerts_label = ctk.CTkLabel(self, text=t("prom_alerts"),
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
alerts_label.pack(fill="x", padx=15, pady=(10, 3))
alerts_label.pack(fill="x", padx=15, pady=(5, 3))
self._alerts_box = ctk.CTkTextbox(self, height=100,
self._alerts_box = ctk.CTkTextbox(self, height=80,
font=ctk.CTkFont(family="Consolas", size=12),
state="disabled")
self._alerts_box.pack(fill="x", padx=15, pady=(0, 5))
# ── Rules section ──
rules_label = ctk.CTkLabel(self, text=t("prom_rules"),
font=ctk.CTkFont(size=14, weight="bold"), anchor="w")
rules_label.pack(fill="x", padx=15, pady=(5, 3))
rules_frame = ctk.CTkFrame(self, fg_color="transparent")
rules_frame.pack(fill="both", expand=True, padx=15, pady=(0, 5))
rules_columns = ("type", "name", "group", "health")
self._rules_tree = ttk.Treeview(rules_frame, columns=rules_columns, show="headings",
selectmode="browse", height=5)
self._rules_tree.heading("type", text=t("prom_rule_type"))
self._rules_tree.heading("name", text=t("prom_rule_name"))
self._rules_tree.heading("group", text=t("prom_rule_group"))
self._rules_tree.heading("health", text=t("prom_rule_health"))
self._rules_tree.column("type", width=80, minwidth=60)
self._rules_tree.column("name", width=250, minwidth=120)
self._rules_tree.column("group", width=150, minwidth=80)
self._rules_tree.column("health", width=80, minwidth=60)
self._rules_tree.pack(side="left", fill="both", expand=True)
rules_scroll = ttk.Scrollbar(rules_frame, orient="vertical", command=self._rules_tree.yview,
style="Dark.Vertical.TScrollbar")
rules_scroll.pack(side="right", fill="y")
self._rules_tree.configure(yscrollcommand=rules_scroll.set)
# ── Status bar ──
self._status_bar = ctk.CTkLabel(self, text=t("prom_no_server"), anchor="w",
font=ctk.CTkFont(size=11), text_color="#9ca3af")
@@ -103,7 +145,6 @@ class PrometheusTab(ctk.CTkFrame):
# ── Public API ──
def set_server(self, alias: str | None):
"""Called when user selects a server in sidebar."""
self._current_alias = alias
self._client = None
self._clear_all()
@@ -114,6 +155,13 @@ class PrometheusTab(ctk.CTkFrame):
else:
self._set_status(t("prom_no_server"), "#9ca3af")
# ── Quick query ──
def _run_quick(self, query: str):
self._query_entry.delete(0, "end")
self._query_entry.insert(0, query)
self._execute_query()
# ── PromQL execution ──
def _execute_query(self):
@@ -121,7 +169,7 @@ class PrometheusTab(ctk.CTkFrame):
if not query:
return
if not self._current_alias:
self._set_results(t("no_server_selected"))
self._set_results(t("prom_no_server"))
return
self._exec_btn.configure(state="disabled")
@@ -141,11 +189,9 @@ class PrometheusTab(ctk.CTkFrame):
threading.Thread(target=_do, daemon=True).start()
def _format_query_result(self, result: dict) -> str:
"""Format Prometheus query API response for display."""
status = result.get("status", "unknown")
if status != "success":
error = result.get("error", "Unknown error")
return f"Error: {error}"
return f"Error: {result.get('error', 'Unknown error')}"
data = result.get("data", {})
result_type = data.get("resultType", "")
@@ -166,7 +212,7 @@ class PrometheusTab(ctk.CTkFrame):
elif result_type == "matrix":
values = item.get("values", [])
lines.append(f"{{{metric_str}}}")
for ts, val in values[-10:]: # Show last 10 points
for ts, val in values[-10:]:
lines.append(f" @{ts} => {val}")
if len(values) > 10:
lines.append(f" ... ({len(values)} total points)")
@@ -175,28 +221,105 @@ class PrometheusTab(ctk.CTkFrame):
return "\n".join(lines)
# ── Refresh targets & alerts ──
# ── Metrics browser ──
def _open_metrics_browser(self):
if not self._current_alias:
self._set_status(t("prom_no_server"), "#ef4444")
return
self._metrics_btn.configure(state="disabled")
def _do():
try:
client = self._get_client()
resp = client._get("/api/v1/label/__name__/values")
metrics = resp.get("data", [])
self.after(0, lambda: self._show_metrics_popup(metrics))
except Exception as e:
self.after(0, lambda: self._set_status(f"(error) {e}", "#ef4444"))
finally:
self.after(0, lambda: self._metrics_btn.configure(state="normal"))
threading.Thread(target=_do, daemon=True).start()
def _show_metrics_popup(self, metrics: list[str]):
popup = ctk.CTkToplevel(self)
popup.title(t("prom_metrics_browser"))
popup.geometry("450x500")
popup.transient(self.winfo_toplevel())
filter_entry = ctk.CTkEntry(popup, placeholder_text=t("prom_filter_metrics"))
filter_entry.pack(fill="x", padx=10, pady=(10, 5))
listbox_frame = ctk.CTkFrame(popup, fg_color="transparent")
listbox_frame.pack(fill="both", expand=True, padx=10, pady=(0, 10))
tree = ttk.Treeview(listbox_frame, columns=("metric",), show="headings",
selectmode="browse")
tree.heading("metric", text="Metric Name")
tree.column("metric", width=400)
tree.pack(side="left", fill="both", expand=True)
scroll = ttk.Scrollbar(listbox_frame, orient="vertical", command=tree.yview,
style="Dark.Vertical.TScrollbar")
scroll.pack(side="right", fill="y")
tree.configure(yscrollcommand=scroll.set)
all_metrics = sorted(metrics)
def populate(filter_text=""):
tree.delete(*tree.get_children())
for m in all_metrics:
if filter_text.lower() in m.lower():
tree.insert("", "end", values=(m,))
def on_filter(*_):
populate(filter_entry.get())
filter_entry.bind("<KeyRelease>", on_filter)
def on_select(event):
sel = tree.selection()
if sel:
metric = tree.item(sel[0])["values"][0]
self._query_entry.delete(0, "end")
self._query_entry.insert(0, metric)
popup.destroy()
tree.bind("<Double-1>", on_select)
populate()
filter_entry.focus_set()
# ── Refresh targets, alerts & rules ──
def _refresh_all(self):
if not self._current_alias:
self._set_status(t("no_server_selected"), "#ef4444")
self._set_status(t("prom_no_server"), "#ef4444")
return
self._refresh_btn.configure(state="disabled", text=t("prom_loading"))
self._refresh_btn.configure(state="disabled")
self._set_status(t("prom_loading"), "#ccaa00")
def _do():
try:
client = self._get_client()
targets = client.get_targets()
alerts = client.get_alerts()
targets_resp = client.targets()
targets = targets_resp.get("data", {}).get("activeTargets", [])
alerts_resp = client.alerts()
alerts = alerts_resp.get("data", {}).get("alerts", [])
rules_resp = client.rules()
rule_groups = rules_resp.get("data", {}).get("groups", [])
self.after(0, lambda: self._populate_targets(targets))
self.after(0, lambda: self._populate_alerts(alerts))
self.after(0, lambda: self._populate_rules(rule_groups))
rule_count = sum(len(g.get("rules", [])) for g in rule_groups)
self.after(0, lambda: self._set_status(
t("prom_loaded").format(
targets=len(targets), alerts=len(alerts)
targets=len(targets), alerts=len(alerts), rules=rule_count
), "#22c55e"))
except Exception as e:
self.after(0, lambda: self._set_status(f"(error) {e}", "#ef4444"))
@@ -210,7 +333,10 @@ class PrometheusTab(ctk.CTkFrame):
def _get_client(self) -> PrometheusClient:
if self._client is None:
self._client = PrometheusClient(self._current_alias, self.store)
server = self.store.get_server(self._current_alias)
if not server:
raise ValueError(f"Server '{self._current_alias}' not found")
self._client = PrometheusClient(server)
return self._client
# ── Table population ──
@@ -218,28 +344,19 @@ class PrometheusTab(ctk.CTkFrame):
def _populate_targets(self, targets: list[dict]):
self._targets_tree.delete(*self._targets_tree.get_children())
for target in targets:
job = target.get("labels", {}).get("job", "")
instance = target.get("labels", {}).get("instance", "")
job = target.get("labels", {}).get("job", "---")
instance = target.get("labels", {}).get("instance", "---")
health = target.get("health", "unknown")
last_scrape = target.get("lastScrape", "")
tag = ""
if health == "up":
tag = "up"
elif health == "down":
tag = "down"
last_scrape = target.get("lastScrape", "---")
tag = "up" if health == "up" else ("down" if health == "down" else "")
self._targets_tree.insert("", "end",
values=(job, instance, health, last_scrape),
tags=(tag,))
values=(job, instance, health, last_scrape), tags=(tag,))
self._targets_tree.tag_configure("up", foreground="#22c55e")
self._targets_tree.tag_configure("down", foreground="#ef4444")
def _populate_alerts(self, alerts: list[dict]):
self._alerts_box.configure(state="normal")
self._alerts_box.delete("1.0", "end")
if not alerts:
self._alerts_box.insert("1.0", t("prom_no_alerts"))
else:
@@ -247,12 +364,25 @@ class PrometheusTab(ctk.CTkFrame):
for a in alerts:
name = a.get("labels", {}).get("alertname", a.get("name", "unknown"))
state = a.get("state", "unknown")
severity = a.get("labels", {}).get("severity", "")
severity = a.get("labels", {}).get("severity", "---")
lines.append(f"[{state.upper()}] {name} (severity: {severity})")
self._alerts_box.insert("1.0", "\n".join(lines))
self._alerts_box.configure(state="disabled")
def _populate_rules(self, groups: list[dict]):
self._rules_tree.delete(*self._rules_tree.get_children())
for group in groups:
group_name = group.get("name", "")
for rule in group.get("rules", []):
rtype = rule.get("type", "")
name = rule.get("name", "")
health = rule.get("health", "")
tag = "up" if health == "ok" else ("down" if health == "err" else "")
self._rules_tree.insert("", "end",
values=(rtype, name, group_name, health), tags=(tag,))
self._rules_tree.tag_configure("up", foreground="#22c55e")
self._rules_tree.tag_configure("down", foreground="#ef4444")
# ── Helpers ──
def _set_results(self, text: str):
@@ -263,6 +393,7 @@ class PrometheusTab(ctk.CTkFrame):
def _clear_all(self):
self._targets_tree.delete(*self._targets_tree.get_children())
self._rules_tree.delete(*self._rules_tree.get_children())
self._set_results("")
self._alerts_box.configure(state="normal")
self._alerts_box.delete("1.0", "end")

Binary file not shown.

Binary file not shown.

232
tools/patch_claude_code.js Normal file
View File

@@ -0,0 +1,232 @@
#!/usr/bin/env node
/**
* Patcher for Claude Code CLI — fixes image reading crash on Windows.
*
* Root cause: In some code paths, `media_type` field is undefined when
* constructing image content blocks for the API. JSON.stringify omits
* undefined values, so the field is absent from the request body.
* The API returns 400 "media_type: Field required" which permanently
* poisons the conversation context and kills the session.
*
* This patcher:
* 1. Installs `sharp` into claude-code's node_modules (if missing)
* 2. Patches the Nv8 (image reader) function to gracefully handle errors
* 3. Patches the image mapper to guarantee media_type is always present
*
* Usage:
* node tools/patch_claude_code.js # apply patch
* node tools/patch_claude_code.js --check # check status only
* node tools/patch_claude_code.js --revert # revert patch
*
* Safe to run multiple times — idempotent.
*/
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
// Find claude-code installation
function findClaudeCodeDir() {
const npmGlobal = execSync("npm root -g", { encoding: "utf8" }).trim();
const claudeDir = path.join(npmGlobal, "@anthropic-ai", "claude-code");
if (fs.existsSync(path.join(claudeDir, "cli.js"))) return claudeDir;
// Fallback: try common paths
const fallbacks = [
path.join(process.env.APPDATA || "", "npm", "node_modules", "@anthropic-ai", "claude-code"),
path.join(process.env.HOME || "", ".npm-global", "lib", "node_modules", "@anthropic-ai", "claude-code"),
"/usr/local/lib/node_modules/@anthropic-ai/claude-code",
];
for (const dir of fallbacks) {
if (fs.existsSync(path.join(dir, "cli.js"))) return dir;
}
return null;
}
// Check if sharp is installed
function isSharpInstalled(claudeDir) {
try {
const sharpDir = path.join(claudeDir, "node_modules", "sharp");
return fs.existsSync(sharpDir);
} catch {
return false;
}
}
// Install sharp
function installSharp(claudeDir) {
console.log("[*] Installing sharp...");
try {
execSync("npm install sharp", { cwd: claudeDir, encoding: "utf8", stdio: "pipe" });
console.log("[+] sharp installed successfully");
return true;
} catch (e) {
console.error("[-] Failed to install sharp:", e.message);
return false;
}
}
const PATCH_MARKER = "/* PATCHED_NV8_SAFE_IMAGE_READ */";
const MAPPER_PATCH_MARKER = "/* PATCHED_IMAGE_MEDIA_TYPE */";
function readCliJs(claudeDir) {
return fs.readFileSync(path.join(claudeDir, "cli.js"), "utf8");
}
function writeCliJs(claudeDir, code) {
// Backup first
const backupPath = path.join(claudeDir, "cli.js.bak");
if (!fs.existsSync(backupPath)) {
fs.copyFileSync(path.join(claudeDir, "cli.js"), backupPath);
console.log("[+] Backup created: cli.js.bak");
}
fs.writeFileSync(path.join(claudeDir, "cli.js"), code, "utf8");
}
function isPatched(code) {
return code.includes(PATCH_MARKER);
}
function isMapperPatched(code) {
return code.includes(MAPPER_PATCH_MARKER);
}
// Patch 1: Nv8 safety wrapper (try/catch around image reader)
function applyNv8Patch(code) {
if (code.includes(PATCH_MARKER)) {
console.log("[=] Nv8 safety patch already applied");
return code;
}
const ORIGINAL_NV8_SIGNATURE = "async function Nv8(A,q=Tv8(),K){let Y=await X1().readFileBytes(A,K)";
const idx = code.indexOf(ORIGINAL_NV8_SIGNATURE);
if (idx === -1) {
console.error("[-] Could not find Nv8 function signature in cli.js");
console.error(" Claude Code may have been updated.");
return code;
}
const endMarker = "}var Ns9";
const endIdx = code.indexOf(endMarker, idx);
if (endIdx === -1) {
console.error("[-] Could not find end of Nv8 function");
return code;
}
const patchedNv8 = `${PATCH_MARKER}async function Nv8(A,q=Tv8(),K){try{let Y=await X1().readFileBytes(A,K),z=Y.length;if(z===0)throw Error("Image file is empty: "+A);let w=kp6(Y),_=w.split("/")[1]||"png",$;try{let H=await ig(Y,z,_);$=q01(H.buffer,H.mediaType,z,H.dimensions)}catch(H){$6(H);$=q01(Y,_,z)}if(Math.ceil($.file.base64.length*0.125)>q)try{let H=await DP1(Y,q,w);return{type:"image",file:{base64:H.base64,type:H.mediaType||"image/png",originalSize:z}}}catch(H){$6(H);try{let j=await Promise.resolve().then(()=>q6(yN8(),1)),M=await(j.default||j)(Y).resize(400,400,{fit:"inside",withoutEnlargement:!0}).jpeg({quality:20}).toBuffer();return q01(M,"jpeg",z)}catch(j){return $6(j),q01(Y,_,z)}}return $}catch(_err){return{type:"text",file:{content:"[Error reading image: "+_err.message+"] File: "+A,totalLines:1}}}}`;
code = code.slice(0, idx) + patchedNv8 + code.slice(endIdx + 1);
console.log("[+] Nv8 safety patch applied");
return code;
}
// Patch 2: Image mapper — guarantee media_type is always a valid string
// This is the ROOT CAUSE fix: A.file.type can be undefined in some code paths,
// and JSON.stringify silently drops undefined fields, causing API 400 error.
function applyMapperPatch(code) {
if (code.includes(MAPPER_PATCH_MARKER)) {
console.log("[=] Image mapper patch already applied");
return code;
}
const ORIGINAL_MAPPER = 'case"image":return{tool_use_id:q,type:"tool_result",content:[{type:"image",source:{type:"base64",data:A.file.base64,media_type:A.file.type}}]}';
const idx = code.indexOf(ORIGINAL_MAPPER);
if (idx === -1) {
console.error("[-] Could not find image mapper in cli.js");
console.error(" Claude Code may have been updated.");
return code;
}
// Patched version: fallback media_type to "image/png" if undefined
const PATCHED_MAPPER = `${MAPPER_PATCH_MARKER}case"image":return{tool_use_id:q,type:"tool_result",content:[{type:"image",source:{type:"base64",data:A.file.base64,media_type:A.file.type||"image/png"}}]}`;
code = code.slice(0, idx) + PATCHED_MAPPER + code.slice(idx + ORIGINAL_MAPPER.length);
console.log("[+] Image mapper patched — media_type guaranteed non-empty");
return code;
}
function revertPatch(claudeDir) {
const backupPath = path.join(claudeDir, "cli.js.bak");
if (!fs.existsSync(backupPath)) {
console.error("[-] No backup found at cli.js.bak");
return false;
}
fs.copyFileSync(backupPath, path.join(claudeDir, "cli.js"));
console.log("[+] Reverted to original cli.js from backup");
return true;
}
// Main
function main() {
const args = process.argv.slice(2);
const checkOnly = args.includes("--check");
const revert = args.includes("--revert");
console.log("=== Claude Code Image Read Patcher v2 ===\n");
const claudeDir = findClaudeCodeDir();
if (!claudeDir) {
console.error("[-] Claude Code installation not found!");
console.error(" Install it with: npm install -g @anthropic-ai/claude-code");
process.exit(1);
}
console.log("[*] Found Claude Code at:", claudeDir);
// Read package version
try {
const pkg = JSON.parse(fs.readFileSync(path.join(claudeDir, "package.json"), "utf8"));
console.log("[*] Version:", pkg.version);
} catch {}
// Check sharp
const hasSharp = isSharpInstalled(claudeDir);
console.log("[*] sharp module:", hasSharp ? "installed" : "MISSING");
// Check patch status
const code = readCliJs(claudeDir);
const nv8Patched = isPatched(code);
const mapperPatched = isMapperPatched(code);
console.log("[*] Nv8 safety patch:", nv8Patched ? "applied" : "not applied");
console.log("[*] Image mapper patch:", mapperPatched ? "applied" : "not applied");
if (checkOnly) {
const fullyProtected = hasSharp && nv8Patched && mapperPatched;
const status = fullyProtected ? "FULLY PROTECTED" :
(mapperPatched ? "PROTECTED (mapper fix applied)" : "VULNERABLE");
console.log("\nStatus:", status);
process.exit(fullyProtected ? 0 : 1);
}
if (revert) {
revertPatch(claudeDir);
process.exit(0);
}
console.log("");
// Step 1: Install sharp
if (!hasSharp) {
if (!installSharp(claudeDir)) {
console.error("\n[-] Could not install sharp. Applying safety patches anyway...");
}
} else {
console.log("[=] sharp already installed, skipping");
}
// Step 2: Apply Nv8 safety patch
let patchedCode = applyNv8Patch(readCliJs(claudeDir));
// Step 3: Apply image mapper patch (ROOT CAUSE FIX)
patchedCode = applyMapperPatch(patchedCode);
writeCliJs(claudeDir, patchedCode);
console.log("\n=== Done! Claude Code is now protected against image read crashes ===");
console.log("Patches applied:");
console.log(" 1. Nv8 try/catch — prevents binary leak on image read failure");
console.log(" 2. Image mapper — guarantees media_type is always present (ROOT FIX)");
console.log("\nNote: After updating Claude Code (npm update -g @anthropic-ai/claude-code),");
console.log(" re-run this patcher to reapply the fixes.");
}
main();

View File

@@ -1511,16 +1511,19 @@ def _grafana_request(server: dict, endpoint: str) -> dict:
import requests
host = server["ip"]
port = server.get("port", 3000)
protocol = "https" if server.get("ssl", False) else "http"
protocol = "https" if server.get("use_ssl", server.get("ssl", False)) else "http"
base_url = server.get("base_url", f"{protocol}://{host}:{port}")
api_key = server.get("api_key", server.get("password", ""))
api_token = server.get("api_token", server.get("api_key", ""))
headers = {}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
auth = None
if api_token:
headers["Authorization"] = f"Bearer {api_token}"
elif server.get("user") and server.get("password"):
auth = (server["user"], server["password"])
url = f"{base_url.rstrip('/')}/api/{endpoint.lstrip('/')}"
resp = requests.get(url, headers=headers, timeout=15, verify=server.get("ssl_verify", True))
resp = requests.get(url, headers=headers, auth=auth, timeout=15, verify=server.get("ssl_verify", True))
resp.raise_for_status()
return resp.json()
@@ -1566,6 +1569,25 @@ def grafana_alerts(server: dict):
print(f"\n({len(rows)} alert{'s' if len(rows) != 1 else ''})")
def grafana_datasources(server: dict):
"""List Grafana datasources."""
data = _grafana_request(server, "datasources")
if not data:
print("(no datasources found)")
return
headers = ["Name", "Type", "URL", "Default"]
rows = []
for ds in data:
rows.append([
ds.get("name", ""),
ds.get("type", ""),
ds.get("url", ""),
"yes" if ds.get("isDefault", False) else "",
])
_print_table(headers, rows)
print(f"\n({len(rows)} datasource{'s' if len(rows) != 1 else ''})")
# ── Prometheus commands ───────────────────────────────
def _prom_request(server: dict, endpoint: str, params: dict = None) -> dict:
@@ -1678,6 +1700,29 @@ def prom_alerts(server: dict):
print(f"\n({len(rows)} alert{'s' if len(rows) != 1 else ''})")
def prom_rules(server: dict):
"""List Prometheus rules (recording + alerting)."""
data = _prom_request(server, "rules")
groups = data.get("data", {}).get("groups", [])
if not groups:
print("(no rules)")
return
headers = ["Type", "Name", "Group", "Health", "Query/Expr"]
rows = []
for group in groups:
gname = group.get("name", "")
for rule in group.get("rules", []):
rows.append([
rule.get("type", ""),
rule.get("name", ""),
gname,
rule.get("health", ""),
(rule.get("query", rule.get("expr", "")))[:60],
])
_print_table(headers, rows)
print(f"\n({len(rows)} rule{'s' if len(rows) != 1 else ''} in {len(groups)} group{'s' if len(groups) != 1 else ''})")
# ── WinRM commands ────────────────────────────────────
def _get_winrm_session(server: dict):
@@ -1831,6 +1876,11 @@ def main():
alias = _resolve_alias(sys.argv[2], servers)
grafana_alerts(servers[alias])
sys.exit(0)
if cmd == "--grafana-datasources" and len(sys.argv) >= 3:
_, servers = load_servers()
alias = _resolve_alias(sys.argv[2], servers)
grafana_datasources(servers[alias])
sys.exit(0)
# ── Prometheus commands ──
if cmd == "--prom-query" and len(sys.argv) >= 4:
@@ -1848,6 +1898,11 @@ def main():
alias = _resolve_alias(sys.argv[2], servers)
prom_alerts(servers[alias])
sys.exit(0)
if cmd == "--prom-rules" and len(sys.argv) >= 3:
_, servers = load_servers()
alias = _resolve_alias(sys.argv[2], servers)
prom_rules(servers[alias])
sys.exit(0)
# ── WinRM commands ──
if cmd == "--ps" and len(sys.argv) >= 4:

View File

@@ -1,6 +1,6 @@
"""Version info for ServerManager."""
__version__ = "1.9.30"
__version__ = "1.9.42"
__app_name__ = "ServerManager"
__author__ = "aibot777"
__description__ = "Desktop GUI for managing remote servers"