Skip to content

armgit / kong

Public Imported from GitHub
0

The world's first agentic reverse engineer.

feat: add custom provider CLI flags, resolve_provider, and endpoint probe

Amruth0a1c0ce9776c
diff --git a/kong/__main__.py b/kong/__main__.py
index 568863d..1d0354b 100644
--- a/kong/__main__.py
+++ b/kong/__main__.py
@@ -24,7 +24,7 @@ from kong.banner import (
print_banner,
)
from kong.config import GhidraConfig, KongConfig, LLMConfig, LLMProvider, OutputConfig
-from kong.db import get_default_provider, get_enabled_providers, is_setup_complete, save_setup
+from kong.db import get_custom_config, get_default_provider, get_enabled_providers, is_setup_complete, save_setup
from kong.evals.harness import score as eval_score
from kong.ghidra.client import GhidraClient, GhidraClientError
from kong.llm.usage import TokenUsage
@@ -70,23 +70,50 @@ def create_llm_client(config: LLMConfig) -> LLMClient:
return AnthropicClient(model=model, api_key=config.api_key)
-def resolve_provider(cli_override: str | None = None) -> LLMProvider:
+def _int_or_none(value: str | None) -> int | None:
+ if value is None:
+ return None
+ try:
+ return int(value)
+ except ValueError:
+ return None
+
+
+def validate_base_url(url: str) -> str:
+ if not url.startswith(("http://", "https://")):
+ raise click.BadParameter(
+ f"base-url must start with http:// or https:// (got '{url}')"
+ )
+ return url.rstrip("/")
+
+
+def resolve_provider(cli_override: str | None = None, base_url: str | None = None) -> LLMProvider:
"""Pick the best available provider: CLI flag > DB default > any enabled key."""
+ if base_url and not cli_override:
+ return LLMProvider.CUSTOM
+
if cli_override:
provider = LLMProvider(cli_override)
+ if provider is LLMProvider.CUSTOM:
+ return provider
if check_api_key(provider):
return provider
+ env_var = _ENV_VARS.get(provider, "unknown")
console.print(
f"[yellow]Warning:[/yellow] --provider {provider.value} specified "
- f"but {_ENV_VARS[provider]} is not set."
+ f"but {env_var} is not set."
)
raise SystemExit(1)
default = get_default_provider()
+ if default is LLMProvider.CUSTOM:
+ return default
if default and check_api_key(default):
return default
for provider in get_enabled_providers():
+ if provider is LLMProvider.CUSTOM:
+ continue
if check_api_key(provider):
return provider
@@ -165,9 +192,13 @@ def _print_final_stats(supervisor: Supervisor, llm_client: LLMClient) -> None:
"--provider", "-p",
type=click.Choice([p.value for p in LLMProvider], case_sensitive=False),
default=None,
- help="LLM provider (anthropic or openai). Uses configured default if omitted.",
+ help="LLM provider (anthropic, openai, or custom).",
)
@click.option("--model", "-m", default=None, help="Override the LLM model name.")
[email protected]("--base-url", default=None, help="Custom OpenAI-compatible endpoint URL.")
[email protected]("--max-prompt-chars", type=int, default=None, help="Override prompt size limit.")
[email protected]("--max-chunk-functions", type=int, default=None, help="Override batch size limit.")
[email protected]("--max-output-tokens", type=int, default=None, help="Override output token limit.")
@click.pass_context
def analyze(
ctx: click.Context,
@@ -178,6 +209,10 @@ def analyze(
ghidra_dir: str | None,
provider: str | None,
model: str | None,
+ base_url: str | None,
+ max_prompt_chars: int | None,
+ max_chunk_functions: int | None,
+ max_output_tokens: int | None,
) -> None:
"""Analyze a binary with Kong's autonomous agent."""
if not is_setup_complete():
@@ -185,15 +220,54 @@ def analyze(
console.print("Run [bold cyan]kong setup[/bold cyan] first.")
raise SystemExit(1)
- llm_provider = resolve_provider(provider)
+ if base_url:
+ base_url = validate_base_url(base_url)
+ if provider and provider not in ("custom",):
+ console.print("[red]--base-url can only be used with --provider custom[/red]")
+ raise SystemExit(1)
+
+ llm_provider = resolve_provider(provider, base_url=base_url)
+
+ if llm_provider is LLMProvider.CUSTOM:
+ custom_db = get_custom_config()
+ base_url = base_url or custom_db.get("custom_base_url")
+ model = model or custom_db.get("custom_model")
+ if not model:
+ console.print("[red]--model is required for custom provider[/red]")
+ raise SystemExit(1)
+ if not base_url:
+ console.print("[red]--base-url is required for custom provider[/red]")
+ raise SystemExit(1)
+ max_prompt_chars = max_prompt_chars or _int_or_none(custom_db.get("custom_max_prompt_chars"))
+ max_chunk_functions = max_chunk_functions or _int_or_none(custom_db.get("custom_max_chunk_functions"))
+ max_output_tokens = max_output_tokens or _int_or_none(custom_db.get("custom_max_output_tokens"))
+
config = KongConfig(
ghidra=GhidraConfig(install_dir=ghidra_dir),
- llm=LLMConfig(provider=llm_provider, model=model),
+ llm=LLMConfig(
+ provider=llm_provider,
+ model=model,
+ base_url=base_url,
+ max_prompt_chars=max_prompt_chars,
+ max_chunk_functions=max_chunk_functions,
+ max_output_tokens=max_output_tokens,
+ ),
output=OutputConfig(directory=Path(output), formats=list(formats)),
headless=headless,
verbose=ctx.obj["verbose"],
)
+ from kong.llm.probe import probe_endpoint
+
+ if not probe_endpoint(config.llm):
+ console.print("[red]Could not connect to LLM endpoint.[/red]")
+ if llm_provider is LLMProvider.CUSTOM:
+ console.print(f"Ensure your server is running at {config.llm.base_url}")
+ raise SystemExit(1)
+
+ if llm_provider is LLMProvider.CUSTOM:
+ console.print("[dim]Cost tracking disabled for custom provider (token counts still recorded)[/dim]")
+
binary_path = Path(binary).resolve()
print_analyze_header(
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 9267231..3ca30aa 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -6,9 +6,11 @@ from unittest.mock import MagicMock, patch
from click.testing import CliRunner
-from kong.__main__ import cli, create_llm_client
+import click
+
+from kong.__main__ import cli, create_llm_client, resolve_provider, validate_base_url
from kong.config import LLMConfig, LLMProvider
-from kong.db import save_setup
+from kong.db import get_custom_config, save_setup
def _complete_setup(tmp_path, monkeypatch):
@@ -149,3 +151,42 @@ class TestCreateLLMClient:
config = LLMConfig(provider=LLMProvider.ANTHROPIC, model="claude-opus-4-6")
client = create_llm_client(config)
assert isinstance(client, AnthropicClient)
+
+
+class TestResolveProviderCustom:
+ def test_base_url_implies_custom(self, tmp_path, monkeypatch):
+ monkeypatch.setenv("KONG_CONFIG_DIR", str(tmp_path))
+ provider = resolve_provider(base_url="http://localhost:8000/v1")
+ assert provider is LLMProvider.CUSTOM
+
+ def test_explicit_custom_provider(self, tmp_path, monkeypatch):
+ monkeypatch.setenv("KONG_CONFIG_DIR", str(tmp_path))
+ provider = resolve_provider(cli_override="custom")
+ assert provider is LLMProvider.CUSTOM
+
+ def test_custom_skipped_in_fallback(self, tmp_path, monkeypatch):
+ monkeypatch.setenv("KONG_CONFIG_DIR", str(tmp_path))
+ monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-test")
+ save_setup(
+ enabled=[LLMProvider.CUSTOM, LLMProvider.ANTHROPIC],
+ default=LLMProvider.ANTHROPIC,
+ )
+ provider = resolve_provider()
+ assert provider is LLMProvider.ANTHROPIC
+
+
+class TestValidateBaseUrl:
+ def test_valid_http_url(self):
+ assert validate_base_url("http://localhost:8000/v1") == "http://localhost:8000/v1"
+
+ def test_valid_https_url(self):
+ assert validate_base_url("https://api.together.xyz/v1") == "https://api.together.xyz/v1"
+
+ def test_strips_trailing_slash(self):
+ assert validate_base_url("http://localhost:8000/v1/") == "http://localhost:8000/v1"
+
+ def test_rejects_missing_scheme(self):
+ import pytest
+
+ with pytest.raises(click.BadParameter):
+ validate_base_url("localhost:8000/v1")