File size: 13,387 Bytes
691dc7c
e99df76
691dc7c
 
 
 
 
 
 
 
 
 
2e7b8ca
 
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
 
 
 
691dc7c
 
 
 
 
 
 
 
 
a9e27c0
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e99df76
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7872b8
691dc7c
 
 
 
 
 
 
 
 
 
f7872b8
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
691dc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from __future__ import annotations
"""Shareable local diagnostics, without starting or changing FCC."""

import importlib
import json
import platform
import re
import shutil
import subprocess
from typing import Any
from urllib.parse import urlsplit

import httpx

from free_claude_code.config.loader import ManagedConfigStore
from free_claude_code.config.paths import (
    claude_desktop_disconnect_path,
    codex_model_catalog_path,
)
from free_claude_code.config.provider_catalog import PROVIDER_CATALOG, ProviderAuthKind
from free_claude_code.config.server_urls import local_proxy_root_url
from free_claude_code.config.settings import Settings
from free_claude_code.core.version import package_version
from free_claude_code.harnesses import (
    claude_desktop_integration,
    claude_integration,
    codex_integration,
    jetbrains_acp_integration,
    vscode_chat_integration,
)
from free_claude_code.providers.github_copilot.auth import CopilotAuthManager
from free_claude_code.providers.openai_codex.auth import saved_connection_state
from free_claude_code.providers.runtime.config import has_provider_configuration
from free_claude_code.providers.runtime.discovery import (
    referenced_provider_ids,
    safe_discovery_failure_message,
)

RUNTIME_FIELDS = (
    "host",
    "port",
    "open_admin_browser",
    "proxy_auth_enabled",
    "provider_rate_limit",
    "provider_rate_window",
    "provider_max_concurrency",
    "provider_max_attempts",
    "http_read_timeout",
    "http_write_timeout",
    "http_connect_timeout",
    "log_level",
    "log_raw_api_payloads",
    "log_raw_sse_events",
    "log_api_error_tracebacks",
    "log_raw_messaging_content",
    "log_raw_cli_diagnostics",
    "log_messaging_error_details",
    "debug_platform_edits",
    "debug_subagent_stack",
    "voice_note_enabled",
    "whisper_device",
    "whisper_model",
    "messaging_platform",
    "messaging_rate_limit",
    "messaging_rate_window",
    "max_message_log_entries_per_chat",
)
ROUTING_FIELDS = (
    "model",
    "model_fable",
    "model_opus",
    "model_sonnet",
    "model_haiku",
    "model_fallbacks",
)
REASONING_FIELDS = (
    "reasoning_policy",
    "reasoning_fable",
    "reasoning_opus",
    "reasoning_sonnet",
    "reasoning_haiku",
)
WEB_FIELDS = (
    "enable_web_server_tools",
    "web_fetch_allowed_schemes",
    "web_fetch_allow_private_networks",
)
EXCLUDED_FIELDS = {
    "proxy_auth_token",
    "telegram_bot_token",
    "allowed_telegram_user_id",
    "discord_bot_token",
    "allowed_discord_channels",
    "allowed_dir",
    "cloudflare_account_id",
    "anthropic_workspace_id",
    "vertex_project_id",
} | {d.credential_attr for d in PROVIDER_CATALOG.values() if d.credential_attr}
URL_FIELDS = {
    attr
    for d in PROVIDER_CATALOG.values()
    for attr in (d.base_url_attr, d.proxy_attr)
    if attr
} | {"telegram_proxy_url"}
HARNESS_NAMES = (
    "claude",
    "codex",
    "pi",
    "opencode",
    "cline",
    "hermes",
    "dsh",
    "grok",
    "muse",
    "aider",
)
_VERSION = re.compile(
    r"(?<![\w./\\])v?(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?)(?![\w./\\+-])"
)


def classified_settings() -> set[str]:
    return (
        set(RUNTIME_FIELDS + ROUTING_FIELDS + REASONING_FIELDS + WEB_FIELDS)
        | EXCLUDED_FIELDS
        | URL_FIELDS
        | {"vertex_location", "custom_providers"}
    )


def _url_summary(value: str | None) -> dict[str, Any]:
    origin = None
    if value:
        try:
            parsed = urlsplit(value)
            if (
                parsed.scheme in {"http", "https", "socks5", "socks5h", "socks4"}
                and parsed.hostname
            ):
                host = parsed.hostname
                if ":" in host:
                    host = f"[{host}]"
                origin = f"{parsed.scheme}://{host}"
                if parsed.port is not None:
                    origin += f":{parsed.port}"
        except ValueError:
            pass
    return {"configured": bool(value), "origin": origin}


def settings_report(settings: Settings, accounts: dict[str, str]) -> dict[str, Any]:
    referenced = referenced_provider_ids(settings)
    providers = {}
    for name, descriptor in sorted(PROVIDER_CATALOG.items()):
        account = accounts.get(name, "unavailable")
        configured = has_provider_configuration(descriptor, settings)
        if descriptor.auth_kind is ProviderAuthKind.CONNECTED_ACCOUNT:
            configured = account == "connected" if account != "unavailable" else None
        row = {"configured": configured, "referenced": name in referenced}
        if descriptor.auth_kind is ProviderAuthKind.CONNECTED_ACCOUNT:
            row["saved_connection"] = account or "unavailable"
        for key, attr in (
            ("base_url", descriptor.base_url_attr),
            ("proxy", descriptor.proxy_attr),
        ):
            if attr:
                row[key] = _url_summary(getattr(settings, attr))
        if name == "vertex":
            row["location"] = settings.vertex_location
        providers[name] = row
    for definition in settings.custom_providers:
        providers[definition.provider_id] = {
            "display_name": definition.display_name,
            "configured": True,
            "referenced": definition.provider_id in referenced,
            "base_url": _url_summary(definition.base_url),
            "api_key_configured": bool(definition.api_keys),
            "api_format": definition.api_format,
            "reasoning_format": definition.reasoning_format,
            "reasoning_history_format": definition.reasoning_history_format,
            "model_ids": list(definition.model_ids),
        }
    runtime = {name: getattr(settings, name) for name in RUNTIME_FIELDS}
    runtime["telegram_proxy"] = _url_summary(settings.telegram_proxy_url)
    return {
        "providers": providers,
        "runtime": runtime,
        "model_routing": settings.model_dump(mode="json", include=set(ROUTING_FIELDS)),
        "reasoning": {name: getattr(settings, name) for name in REASONING_FIELDS},
        "web_tools": {name: getattr(settings, name) for name in WEB_FIELDS},
    }


def integration_report(settings: Settings) -> dict[str, Any]:
    url, token = local_proxy_root_url(settings), settings.proxy_auth_token
    readers = {
        "vscode-chat": lambda: vscode_chat_integration.status(
            vscode_chat_integration.config_path()
        ),
        "claude-vscode": lambda: claude_integration.configure(
            claude_integration.settings_path(),
            url,
            token,
        ),
        "codex": lambda: codex_integration.configure(
            codex_integration.config_path(), codex_model_catalog_path(), url
        ),
        "claude-desktop": lambda: claude_desktop_integration.configure(
            claude_desktop_integration.config_root(),
            url,
            token,
            disconnect_path=claude_desktop_disconnect_path(),
        ),
        "jetbrains-acp": lambda: jetbrains_acp_integration.configure(
            jetbrains_acp_integration.config_path(), url, token
        ),
    }
    result: dict[str, Any] = {}
    for name, read in readers.items():
        try:
            status = read()
            result[name] = {"status": "available", "connected": status["connected"]}
            if "disconnect_pending" in status:
                result[name]["disconnect_pending"] = status["disconnect_pending"]
        except (OSError, ValueError):
            result[name] = {"status": "unavailable", "connected": None}
    return result


def probe_harness(binary: str, args: tuple[str, ...]) -> dict[str, Any]:
    path = shutil.which(binary)
    result = {"installed": path is not None, "version": None, "status": "not_installed"}
    if path is None:
        return result
    try:
        probe = subprocess.run(
            [path, *args],
            stdin=subprocess.DEVNULL,
            capture_output=True,
            text=True,
            encoding="utf-8",
            errors="replace",
            timeout=5,
            check=False,
        )
        if probe.returncode != 0:
            result["status"] = "failed"
            return result
        output = probe.stdout.strip()
        if binary == "grok":
            payload = json.loads(output)
            output = (
                payload.get("currentVersion", "") if isinstance(payload, dict) else ""
            )
        else:
            output = f"{output}\n{probe.stderr}"
        match = _VERSION.search(output) if isinstance(output, str) else None
        result["version"] = match[1] if match else None
        result["status"] = "available" if match else "unrecognized_version"
    except subprocess.TimeoutExpired:
        result["status"] = "timeout"
    except OSError:
        result["status"] = "unavailable"
    except ValueError:
        result["status"] = "unrecognized_version"
    return result


def harness_report() -> dict[str, Any]:
    result = {}
    for name in HARNESS_NAMES:
        spec = importlib.import_module(f"free_claude_code.cli.launchers.{name}").SPEC
        args = (
            tuple(spec.compatibility_check.args)
            if spec.compatibility_check
            else ("--version",)
        )
        result[name] = probe_harness(spec.binary_name, args)
    return result


def model_discovery_report(settings: Settings) -> dict[str, Any]:
    """Read a safe snapshot from an existing local server without refreshing it."""
    unavailable = {"status": "unavailable", "source": "running_server"}
    try:
        url = local_proxy_root_url(settings)
        if urlsplit(url).hostname not in {"127.0.0.1", "localhost", "::1"}:
            return unavailable
        response = httpx.get(
            f"{url}/admin/api/status",
            timeout=2.0,
            trust_env=False,
            follow_redirects=False,
        )
        response.raise_for_status()
        payload = response.json()
    except (httpx.HTTPError, httpx.InvalidURL, ValueError):
        return unavailable
    if not isinstance(payload, dict) or payload.get("status") != "running":
        return unavailable
    startup = payload.get("startup")
    if not isinstance(startup, dict):
        return unavailable
    states, errors = startup.get("providers"), startup.get("provider_errors")
    if not isinstance(states, dict) or not isinstance(errors, dict):
        return unavailable
    providers = {}
    for provider_id, state in states.items():
        if provider_id not in PROVIDER_CATALOG and not re.fullmatch(
            r"custom_[a-f0-9]{32}", provider_id
        ):
            continue
        if state not in ("starting", "ready", "failed", "unavailable"):
            continue
        row = {"state": state}
        if state == "failed":
            row["message"] = safe_discovery_failure_message(errors.get(provider_id))
        providers[provider_id] = row
    return {"status": "available", "source": "running_server", "providers": providers}


def collect_report() -> dict[str, Any]:
    report = {
        "version": package_version(),
        "system": {
            "os": platform.system(),
            "release": platform.release(),
            "architecture": platform.machine(),
            "python_version": platform.python_version(),
            "python_implementation": platform.python_implementation(),
        },
        "configuration": {
            "source": "saved_config_with_process_overrides",
            "status": "available",
        },
        "providers": None,
        "runtime": None,
        "model_routing": None,
        "reasoning": None,
        "web_tools": None,
        "integrations": None,
        "harnesses": {},
        "model_discovery": {"status": "unavailable", "source": "running_server"},
        "errors": [],
    }
    store = ManagedConfigStore()
    try:
        if not store.path.is_file():
            raise FileNotFoundError
        settings = store.read().settings
    except FileNotFoundError:
        report["configuration"]["status"] = "missing"
    except ValueError:
        report["configuration"]["status"] = "invalid"
    except OSError:
        report["configuration"]["status"] = "unavailable"
    else:
        accounts = {"openai": saved_connection_state()}
        copilot = CopilotAuthManager().status().state.value
        accounts["github_copilot"] = (
            copilot if copilot in {"connected", "disconnected"} else "unavailable"
        )
        report.update(settings_report(settings, accounts))
        report["model_discovery"] = model_discovery_report(settings)
        report["integrations"] = integration_report(settings)
        for name, state in accounts.items():
            if state not in {"connected", "disconnected"}:
                report["errors"].append(
                    {"section": f"providers.{name}", "code": "unavailable"}
                )
        for name, status in report["integrations"].items():
            if status["status"] != "available":
                report["errors"].append(
                    {"section": f"integrations.{name}", "code": "unavailable"}
                )
    if report["configuration"]["status"] != "available":
        report["errors"].append(
            {"section": "configuration", "code": report["configuration"]["status"]}
        )
    report["harnesses"] = harness_report()
    return report