eva-cli 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- eva/__init__.py +3 -0
- eva/budget.py +31 -0
- eva/cache/__init__.py +17 -0
- eva/cache/cache.py +40 -0
- eva/cache.py +19 -0
- eva/chat_session.py +5 -0
- eva/cli/__init__.py +3 -0
- eva/cli/app.py +851 -0
- eva/cli.py +5 -0
- eva/config/__init__.py +33 -0
- eva/config/config.py +129 -0
- eva/config.py +35 -0
- eva/context/__init__.py +19 -0
- eva/context/finder.py +5 -0
- eva/context/gitignore.py +5 -0
- eva/context/io.py +5 -0
- eva/context/tokenizer.py +5 -0
- eva/context/tree.py +5 -0
- eva/diagnostics.py +9 -0
- eva/git_ops.py +15 -0
- eva/indexing/__init__.py +18 -0
- eva/indexing/finder.py +42 -0
- eva/indexing/io.py +35 -0
- eva/indexing/repo_index.py +240 -0
- eva/indexing/tokenizer.py +37 -0
- eva/indexing/tree.py +60 -0
- eva/plugins/__init__.py +3 -0
- eva/prompts/__init__.py +36 -0
- eva/providers/__init__.py +150 -0
- eva/providers/gemini_provider.py +72 -0
- eva/providers/groq_provider.py +37 -0
- eva/providers/llamacpp_provider.py +69 -0
- eva/providers/ollama_provider.py +70 -0
- eva/providers/openai_compat.py +57 -0
- eva/providers/opencode_zen_provider.py +53 -0
- eva/providers/openrouter_provider.py +55 -0
- eva/replay/__init__.py +11 -0
- eva/replay/player.py +91 -0
- eva/replay/recorder.py +67 -0
- eva/router/__init__.py +25 -0
- eva/router/gemini_provider.py +5 -0
- eva/router/groq_provider.py +5 -0
- eva/router/openai_compat.py +5 -0
- eva/router/opencode_zen_provider.py +5 -0
- eva/router/openrouter_provider.py +5 -0
- eva/security/__init__.py +32 -0
- eva/security/audit.py +126 -0
- eva/security/redaction.py +80 -0
- eva/security/sandbox.py +55 -0
- eva/security/work_safety.py +85 -0
- eva/shell/__init__.py +3 -0
- eva/shell/runner.py +6 -0
- eva/telemetry/__init__.py +10 -0
- eva/telemetry/diagnostics.py +43 -0
- eva/telemetry/metrics.py +68 -0
- eva/ui/formatter.py +46 -0
- eva/ui/streaming.py +21 -0
- eva/work_safety.py +21 -0
- eva/workflows/__init__.py +39 -0
- eva/workflows/budget.py +139 -0
- eva/workflows/builtins/dependency_audit.yaml +12 -0
- eva/workflows/builtins/repo_health.yaml +16 -0
- eva/workflows/builtins/security_scan.yaml +12 -0
- eva/workflows/chat_session.py +54 -0
- eva/workflows/engine.py +267 -0
- eva/workspace/__init__.py +39 -0
- eva/workspace/git_ops.py +202 -0
- eva/workspace/gitignore.py +47 -0
- eva/workspace/session.py +184 -0
- eva_cli-3.0.0.dist-info/METADATA +344 -0
- eva_cli-3.0.0.dist-info/RECORD +74 -0
- eva_cli-3.0.0.dist-info/WHEEL +4 -0
- eva_cli-3.0.0.dist-info/entry_points.txt +2 -0
- eva_cli-3.0.0.dist-info/licenses/LICENSE +21 -0
eva/cli.py
ADDED
eva/config/__init__.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from eva.config.config import (
|
|
2
|
+
APP_NAME,
|
|
3
|
+
AppConfig,
|
|
4
|
+
GeneralConfig,
|
|
5
|
+
KeyringUnavailableError,
|
|
6
|
+
ProviderConfig,
|
|
7
|
+
clear_api_key,
|
|
8
|
+
get_api_key,
|
|
9
|
+
get_api_key_env_var,
|
|
10
|
+
get_config_dir,
|
|
11
|
+
get_config_file,
|
|
12
|
+
keyring_backend_available,
|
|
13
|
+
load_config,
|
|
14
|
+
save_config,
|
|
15
|
+
set_api_key,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"APP_NAME",
|
|
20
|
+
"AppConfig",
|
|
21
|
+
"GeneralConfig",
|
|
22
|
+
"KeyringUnavailableError",
|
|
23
|
+
"ProviderConfig",
|
|
24
|
+
"clear_api_key",
|
|
25
|
+
"get_api_key",
|
|
26
|
+
"get_api_key_env_var",
|
|
27
|
+
"get_config_dir",
|
|
28
|
+
"get_config_file",
|
|
29
|
+
"keyring_backend_available",
|
|
30
|
+
"load_config",
|
|
31
|
+
"save_config",
|
|
32
|
+
"set_api_key",
|
|
33
|
+
]
|
eva/config/config.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
import keyring
|
|
6
|
+
from keyring.errors import KeyringError, NoKeyringError
|
|
7
|
+
from platformdirs import user_config_dir
|
|
8
|
+
from pydantic import BaseModel, Field
|
|
9
|
+
|
|
10
|
+
APP_NAME = "eva"
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class KeyringUnavailableError(RuntimeError):
|
|
15
|
+
"""Raised when the host has no usable keyring backend."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get_config_dir() -> Path:
|
|
19
|
+
d = Path(user_config_dir(APP_NAME))
|
|
20
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
21
|
+
return d
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_config_file() -> Path:
|
|
25
|
+
return get_config_dir() / "config.toml"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ProviderConfig(BaseModel):
|
|
29
|
+
model: str
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class GeneralConfig(BaseModel):
|
|
33
|
+
default_provider: str = "openrouter"
|
|
34
|
+
fallback_enabled: bool = True
|
|
35
|
+
fallback_order: list[str] = Field(
|
|
36
|
+
default_factory=lambda: ["openrouter", "groq", "gemini", "opencode_zen", "ollama", "llamacpp"]
|
|
37
|
+
)
|
|
38
|
+
sandbox_risky_commands: bool = False
|
|
39
|
+
telemetry_enabled: bool = False
|
|
40
|
+
telemetry_export_endpoint: str | None = None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class AppConfig(BaseModel):
|
|
44
|
+
general: GeneralConfig = Field(default_factory=GeneralConfig)
|
|
45
|
+
providers: dict[str, ProviderConfig] = Field(
|
|
46
|
+
default_factory=lambda: {
|
|
47
|
+
"openrouter": ProviderConfig(model="openrouter/free"),
|
|
48
|
+
"groq": ProviderConfig(model="llama-3.1-8b-instant"),
|
|
49
|
+
"gemini": ProviderConfig(model="gemini-3-flash"),
|
|
50
|
+
"opencode_zen": ProviderConfig(model="big-pickle"),
|
|
51
|
+
"ollama": ProviderConfig(model="gemma4:12b"),
|
|
52
|
+
"llamacpp": ProviderConfig(model="default"),
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def load_config() -> AppConfig:
|
|
58
|
+
import tomli
|
|
59
|
+
|
|
60
|
+
config_file = get_config_file()
|
|
61
|
+
if not config_file.exists():
|
|
62
|
+
return AppConfig()
|
|
63
|
+
|
|
64
|
+
with open(config_file, "rb") as f:
|
|
65
|
+
data = tomli.load(f)
|
|
66
|
+
return AppConfig(**data)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def save_config(config: AppConfig):
|
|
70
|
+
import tomli_w
|
|
71
|
+
|
|
72
|
+
config_file = get_config_file()
|
|
73
|
+
with open(config_file, "wb") as f:
|
|
74
|
+
tomli_w.dump(config.model_dump(exclude_none=True), f)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def get_api_key_env_var(provider: str) -> str:
|
|
78
|
+
normalized = provider.upper().replace("-", "_")
|
|
79
|
+
return f"EVA_{normalized}_API_KEY"
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def get_api_key(provider: str) -> str | None:
|
|
83
|
+
env_key = os.getenv(get_api_key_env_var(provider))
|
|
84
|
+
if env_key:
|
|
85
|
+
return env_key
|
|
86
|
+
try:
|
|
87
|
+
return keyring.get_password(APP_NAME, provider)
|
|
88
|
+
except NoKeyringError:
|
|
89
|
+
logger.warning("No keyring backend is available for provider %s", provider)
|
|
90
|
+
return None
|
|
91
|
+
except KeyringError as exc:
|
|
92
|
+
logger.warning("Keyring lookup failed for provider %s: %s", provider, exc)
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def set_api_key(provider: str, key: str):
|
|
97
|
+
try:
|
|
98
|
+
keyring.set_password(APP_NAME, provider, key)
|
|
99
|
+
except NoKeyringError as exc:
|
|
100
|
+
env_var = get_api_key_env_var(provider)
|
|
101
|
+
raise KeyringUnavailableError(
|
|
102
|
+
"No OS keyring backend is available. Install/configure a keyring backend "
|
|
103
|
+
f"or set {env_var} in the environment for this provider."
|
|
104
|
+
) from exc
|
|
105
|
+
except KeyringError as exc:
|
|
106
|
+
raise KeyringUnavailableError(f"Keyring failed while storing the API key: {exc}") from exc
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def clear_api_key(provider: str):
|
|
110
|
+
try:
|
|
111
|
+
keyring.delete_password(APP_NAME, provider)
|
|
112
|
+
except keyring.errors.PasswordDeleteError:
|
|
113
|
+
pass # Key doesn't exist, which is fine
|
|
114
|
+
except NoKeyringError as exc:
|
|
115
|
+
raise KeyringUnavailableError("No OS keyring backend is available.") from exc
|
|
116
|
+
except KeyringError as exc:
|
|
117
|
+
raise KeyringUnavailableError(f"Keyring failed while deleting the API key: {exc}") from exc
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def keyring_backend_available() -> tuple[bool, str]:
|
|
121
|
+
try:
|
|
122
|
+
backend = keyring.get_keyring()
|
|
123
|
+
keyring.get_password(APP_NAME, "__doctor_probe__")
|
|
124
|
+
priority = getattr(backend, "priority", None)
|
|
125
|
+
return True, f"{backend.__class__.__module__}.{backend.__class__.__name__} priority={priority}"
|
|
126
|
+
except NoKeyringError as exc:
|
|
127
|
+
return False, str(exc)
|
|
128
|
+
except KeyringError as exc:
|
|
129
|
+
return False, str(exc)
|
eva/config.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Compatibility re-exports for config."""
|
|
2
|
+
|
|
3
|
+
from eva.config.config import (
|
|
4
|
+
APP_NAME,
|
|
5
|
+
AppConfig,
|
|
6
|
+
GeneralConfig,
|
|
7
|
+
KeyringUnavailableError,
|
|
8
|
+
ProviderConfig,
|
|
9
|
+
clear_api_key,
|
|
10
|
+
get_api_key,
|
|
11
|
+
get_api_key_env_var,
|
|
12
|
+
get_config_dir,
|
|
13
|
+
get_config_file,
|
|
14
|
+
keyring_backend_available,
|
|
15
|
+
load_config,
|
|
16
|
+
save_config,
|
|
17
|
+
set_api_key,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"APP_NAME",
|
|
22
|
+
"AppConfig",
|
|
23
|
+
"GeneralConfig",
|
|
24
|
+
"KeyringUnavailableError",
|
|
25
|
+
"ProviderConfig",
|
|
26
|
+
"clear_api_key",
|
|
27
|
+
"get_api_key",
|
|
28
|
+
"get_api_key_env_var",
|
|
29
|
+
"get_config_dir",
|
|
30
|
+
"get_config_file",
|
|
31
|
+
"keyring_backend_available",
|
|
32
|
+
"load_config",
|
|
33
|
+
"save_config",
|
|
34
|
+
"set_api_key",
|
|
35
|
+
]
|
eva/context/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Compatibility re-exports for eva.context."""
|
|
2
|
+
|
|
3
|
+
from eva.indexing.finder import find_files
|
|
4
|
+
from eva.indexing.io import ContextReadError, read_text_file_for_context
|
|
5
|
+
from eva.indexing.tokenizer import count_tokens, trim_context
|
|
6
|
+
from eva.indexing.tree import generate_tree
|
|
7
|
+
from eva.workspace.gitignore import ALWAYS_IGNORED_DIRS, get_gitignore_spec, is_ignored
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"ALWAYS_IGNORED_DIRS",
|
|
11
|
+
"ContextReadError",
|
|
12
|
+
"count_tokens",
|
|
13
|
+
"find_files",
|
|
14
|
+
"generate_tree",
|
|
15
|
+
"get_gitignore_spec",
|
|
16
|
+
"is_ignored",
|
|
17
|
+
"read_text_file_for_context",
|
|
18
|
+
"trim_context",
|
|
19
|
+
]
|
eva/context/finder.py
ADDED
eva/context/gitignore.py
ADDED
eva/context/io.py
ADDED
eva/context/tokenizer.py
ADDED
eva/context/tree.py
ADDED
eva/diagnostics.py
ADDED
eva/git_ops.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Compatibility re-exports for git_ops."""
|
|
2
|
+
|
|
3
|
+
from eva.workspace.git_ops import (
|
|
4
|
+
apply_diff_python_fallback,
|
|
5
|
+
apply_unified_diff,
|
|
6
|
+
extract_unified_diff,
|
|
7
|
+
run_git,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"apply_diff_python_fallback",
|
|
12
|
+
"apply_unified_diff",
|
|
13
|
+
"extract_unified_diff",
|
|
14
|
+
"run_git",
|
|
15
|
+
]
|
eva/indexing/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from eva.indexing.finder import find_files
|
|
2
|
+
from eva.indexing.io import ContextReadError, read_text_file_for_context
|
|
3
|
+
from eva.indexing.repo_index import DepGraph, ProjectStack, build_dep_graph, detect_stack
|
|
4
|
+
from eva.indexing.tokenizer import count_tokens, trim_context
|
|
5
|
+
from eva.indexing.tree import generate_tree
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"ContextReadError",
|
|
9
|
+
"DepGraph",
|
|
10
|
+
"ProjectStack",
|
|
11
|
+
"build_dep_graph",
|
|
12
|
+
"count_tokens",
|
|
13
|
+
"detect_stack",
|
|
14
|
+
"find_files",
|
|
15
|
+
"generate_tree",
|
|
16
|
+
"read_text_file_for_context",
|
|
17
|
+
"trim_context",
|
|
18
|
+
]
|
eva/indexing/finder.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import fnmatch
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
from collections.abc import Iterator
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from eva.workspace.gitignore import get_gitignore_spec, is_ignored
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
import eva_fastwalk
|
|
13
|
+
|
|
14
|
+
HAS_RUST = True
|
|
15
|
+
except ImportError:
|
|
16
|
+
HAS_RUST = False
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def find_files(root: str | Path, pattern: str) -> Iterator[Path]:
|
|
20
|
+
root_path = Path(root).resolve()
|
|
21
|
+
|
|
22
|
+
if HAS_RUST:
|
|
23
|
+
try:
|
|
24
|
+
results = eva_fastwalk.fast_find_files(str(root_path), pattern)
|
|
25
|
+
for r in results:
|
|
26
|
+
yield Path(r)
|
|
27
|
+
return
|
|
28
|
+
except (AttributeError, RuntimeError, TypeError, ValueError) as exc:
|
|
29
|
+
logger.debug("Rust fast_find_files error, falling back to Python: %s", exc)
|
|
30
|
+
|
|
31
|
+
spec = get_gitignore_spec(root_path)
|
|
32
|
+
|
|
33
|
+
for current_root, dirs, files in os.walk(root_path):
|
|
34
|
+
current_path = Path(current_root)
|
|
35
|
+
dirs[:] = [d for d in dirs if not is_ignored(current_path / d, root_path, spec)]
|
|
36
|
+
|
|
37
|
+
for filename in files:
|
|
38
|
+
path = current_path / filename
|
|
39
|
+
if is_ignored(path, root_path, spec):
|
|
40
|
+
continue
|
|
41
|
+
if fnmatch.fnmatch(path.name, pattern):
|
|
42
|
+
yield path
|
eva/indexing/io.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
MAX_CONTEXT_FILE_BYTES = 1_000_000
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ContextReadError(RuntimeError):
|
|
7
|
+
pass
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def read_text_file_for_context(path: Path, max_bytes: int = MAX_CONTEXT_FILE_BYTES) -> tuple[str, list[str]]:
|
|
11
|
+
"""Read a file for model context without crashing on invalid UTF-8.
|
|
12
|
+
|
|
13
|
+
Returns (text, warnings). Binary files are rejected because replacement text
|
|
14
|
+
tends to waste context and mislead the model.
|
|
15
|
+
"""
|
|
16
|
+
warnings: list[str] = []
|
|
17
|
+
|
|
18
|
+
if not path.exists():
|
|
19
|
+
raise ContextReadError(f"File {path} does not exist.")
|
|
20
|
+
if not path.is_file():
|
|
21
|
+
raise ContextReadError(f"{path} is not a regular file.")
|
|
22
|
+
|
|
23
|
+
size = path.stat().st_size
|
|
24
|
+
read_limit = min(size, max_bytes)
|
|
25
|
+
with open(path, "rb") as f:
|
|
26
|
+
data = f.read(read_limit + 1)
|
|
27
|
+
|
|
28
|
+
if b"\x00" in data:
|
|
29
|
+
raise ContextReadError(f"{path} appears to be binary; refusing to send it as text context.")
|
|
30
|
+
|
|
31
|
+
if size > max_bytes:
|
|
32
|
+
warnings.append(f"{path} is {size} bytes; only the first {max_bytes} bytes were read.")
|
|
33
|
+
data = data[:max_bytes]
|
|
34
|
+
|
|
35
|
+
return data.decode("utf-8", errors="replace"), warnings
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
import json
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from eva.workspace.gitignore import get_gitignore_spec, is_ignored
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class ProjectStack:
|
|
15
|
+
languages: list[str] = field(default_factory=list)
|
|
16
|
+
frameworks: list[str] = field(default_factory=list)
|
|
17
|
+
package_managers: list[str] = field(default_factory=list)
|
|
18
|
+
ci_configs: list[str] = field(default_factory=list)
|
|
19
|
+
test_frameworks: list[str] = field(default_factory=list)
|
|
20
|
+
dependencies: list[str] = field(default_factory=list)
|
|
21
|
+
|
|
22
|
+
def to_summary_string(self) -> str:
|
|
23
|
+
lines = []
|
|
24
|
+
if self.languages:
|
|
25
|
+
lines.append(f"Languages: {', '.join(self.languages)}")
|
|
26
|
+
if self.frameworks:
|
|
27
|
+
lines.append(f"Frameworks: {', '.join(self.frameworks)}")
|
|
28
|
+
if self.package_managers:
|
|
29
|
+
lines.append(f"Package Managers: {', '.join(self.package_managers)}")
|
|
30
|
+
if self.test_frameworks:
|
|
31
|
+
lines.append(f"Test Frameworks: {', '.join(self.test_frameworks)}")
|
|
32
|
+
if self.ci_configs:
|
|
33
|
+
lines.append(f"CI Configs: {', '.join(self.ci_configs)}")
|
|
34
|
+
if self.dependencies:
|
|
35
|
+
top_deps = self.dependencies[:15]
|
|
36
|
+
lines.append(f"Key Dependencies ({len(self.dependencies)} total): {', '.join(top_deps)}")
|
|
37
|
+
return "\n".join(lines) if lines else "No clear stack detected."
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class DepGraph:
|
|
42
|
+
nodes: dict[str, list[str]] = field(default_factory=dict)
|
|
43
|
+
|
|
44
|
+
def to_summary_string(self) -> str:
|
|
45
|
+
if not self.nodes:
|
|
46
|
+
return "Empty dependency graph."
|
|
47
|
+
lines = ["Module Import Graph:"]
|
|
48
|
+
for mod, imports in sorted(self.nodes.items()):
|
|
49
|
+
if imports:
|
|
50
|
+
lines.append(f" {mod} -> {', '.join(sorted(set(imports)))}")
|
|
51
|
+
else:
|
|
52
|
+
lines.append(f" {mod} (no local imports)")
|
|
53
|
+
return "\n".join(lines)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def detect_stack(root_dir: str | Path) -> ProjectStack:
|
|
57
|
+
root = Path(root_dir).resolve()
|
|
58
|
+
stack = ProjectStack()
|
|
59
|
+
|
|
60
|
+
if not root.is_dir():
|
|
61
|
+
return stack
|
|
62
|
+
|
|
63
|
+
# Detect CI configs
|
|
64
|
+
github_workflows = root / ".github" / "workflows"
|
|
65
|
+
if github_workflows.is_dir() and any(github_workflows.glob("*.y*ml")):
|
|
66
|
+
stack.ci_configs.append("GitHub Actions")
|
|
67
|
+
if (root / ".gitlab-ci.yml").exists():
|
|
68
|
+
stack.ci_configs.append("GitLab CI")
|
|
69
|
+
if (root / ".circleci").is_dir():
|
|
70
|
+
stack.ci_configs.append("CircleCI")
|
|
71
|
+
if (root / "Jenkinsfile").exists():
|
|
72
|
+
stack.ci_configs.append("Jenkins")
|
|
73
|
+
|
|
74
|
+
# Python stack detection
|
|
75
|
+
pyproject = root / "pyproject.toml"
|
|
76
|
+
reqs = root / "requirements.txt"
|
|
77
|
+
setup_py = root / "setup.py"
|
|
78
|
+
pipfile = root / "Pipfile"
|
|
79
|
+
|
|
80
|
+
has_python = pyproject.exists() or reqs.exists() or setup_py.exists() or pipfile.exists()
|
|
81
|
+
if not has_python and list(root.glob("*.py")):
|
|
82
|
+
has_python = True
|
|
83
|
+
|
|
84
|
+
if has_python:
|
|
85
|
+
stack.languages.append("Python")
|
|
86
|
+
if (root / "uv.lock").exists():
|
|
87
|
+
stack.package_managers.append("uv")
|
|
88
|
+
elif (root / "poetry.lock").exists():
|
|
89
|
+
stack.package_managers.append("poetry")
|
|
90
|
+
elif (root / "Pipfile.lock").exists():
|
|
91
|
+
stack.package_managers.append("pipenv")
|
|
92
|
+
else:
|
|
93
|
+
stack.package_managers.append("pip")
|
|
94
|
+
|
|
95
|
+
# Parse dependencies & frameworks
|
|
96
|
+
deps: set[str] = set()
|
|
97
|
+
if pyproject.exists():
|
|
98
|
+
try:
|
|
99
|
+
content = pyproject.read_text(encoding="utf-8", errors="ignore")
|
|
100
|
+
for line in content.splitlines():
|
|
101
|
+
clean = line.strip().strip('"').strip("'").lower()
|
|
102
|
+
for pkg in [
|
|
103
|
+
"django",
|
|
104
|
+
"flask",
|
|
105
|
+
"fastapi",
|
|
106
|
+
"pytest",
|
|
107
|
+
"torch",
|
|
108
|
+
"tensorflow",
|
|
109
|
+
"pydantic",
|
|
110
|
+
"typer",
|
|
111
|
+
"rich",
|
|
112
|
+
"ruff",
|
|
113
|
+
]:
|
|
114
|
+
if pkg in clean:
|
|
115
|
+
deps.add(pkg)
|
|
116
|
+
except OSError as exc:
|
|
117
|
+
logger.debug("Failed to parse %s: %s", pyproject, exc)
|
|
118
|
+
|
|
119
|
+
if reqs.exists():
|
|
120
|
+
try:
|
|
121
|
+
content = reqs.read_text(encoding="utf-8", errors="ignore")
|
|
122
|
+
for line in content.splitlines():
|
|
123
|
+
pkg = line.split("==")[0].split(">=")[0].split("<=")[0].strip().lower()
|
|
124
|
+
if pkg and not pkg.startswith("#"):
|
|
125
|
+
deps.add(pkg)
|
|
126
|
+
except OSError as exc:
|
|
127
|
+
logger.debug("Failed to parse %s: %s", reqs, exc)
|
|
128
|
+
|
|
129
|
+
for dep in deps:
|
|
130
|
+
stack.dependencies.append(dep)
|
|
131
|
+
if dep in {"django", "flask", "fastapi", "streamlit"} and dep.capitalize() not in stack.frameworks:
|
|
132
|
+
stack.frameworks.append(dep.capitalize())
|
|
133
|
+
if dep in {"pytest", "unittest"} and dep not in stack.test_frameworks:
|
|
134
|
+
stack.test_frameworks.append(dep)
|
|
135
|
+
|
|
136
|
+
if "pytest" not in stack.test_frameworks and (root / "conftest.py").exists() or (root / "tests").is_dir():
|
|
137
|
+
stack.test_frameworks.append("pytest")
|
|
138
|
+
|
|
139
|
+
# Node.js stack detection
|
|
140
|
+
pkg_json = root / "package.json"
|
|
141
|
+
if pkg_json.exists():
|
|
142
|
+
stack.languages.append("JavaScript/TypeScript")
|
|
143
|
+
if (root / "pnpm-lock.yaml").exists():
|
|
144
|
+
stack.package_managers.append("pnpm")
|
|
145
|
+
elif (root / "yarn.lock").exists():
|
|
146
|
+
stack.package_managers.append("yarn")
|
|
147
|
+
else:
|
|
148
|
+
stack.package_managers.append("npm")
|
|
149
|
+
|
|
150
|
+
try:
|
|
151
|
+
data = json.loads(pkg_json.read_text(encoding="utf-8", errors="ignore"))
|
|
152
|
+
all_deps = {**data.get("dependencies", {}), **data.get("devDependencies", {})}
|
|
153
|
+
for dep in all_deps:
|
|
154
|
+
stack.dependencies.append(dep)
|
|
155
|
+
if dep in {"react", "next", "vue", "express", "angular"}:
|
|
156
|
+
stack.frameworks.append(dep)
|
|
157
|
+
if dep in {"jest", "vitest", "mocha", "cypress"}:
|
|
158
|
+
stack.test_frameworks.append(dep)
|
|
159
|
+
except (OSError, ValueError) as exc:
|
|
160
|
+
logger.debug("Failed to parse %s: %s", pkg_json, exc)
|
|
161
|
+
|
|
162
|
+
# Rust stack detection
|
|
163
|
+
cargo_toml = root / "Cargo.toml"
|
|
164
|
+
if cargo_toml.exists():
|
|
165
|
+
stack.languages.append("Rust")
|
|
166
|
+
stack.package_managers.append("cargo")
|
|
167
|
+
stack.test_frameworks.append("cargo test")
|
|
168
|
+
|
|
169
|
+
# Go stack detection
|
|
170
|
+
go_mod = root / "go.mod"
|
|
171
|
+
if go_mod.exists():
|
|
172
|
+
stack.languages.append("Go")
|
|
173
|
+
stack.package_managers.append("go modules")
|
|
174
|
+
stack.test_frameworks.append("go test")
|
|
175
|
+
|
|
176
|
+
return stack
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def build_dep_graph(root_dir: str | Path) -> DepGraph:
|
|
180
|
+
"""Build a lightweight module-level import dependency graph for Python projects."""
|
|
181
|
+
root = Path(root_dir).resolve()
|
|
182
|
+
graph = DepGraph()
|
|
183
|
+
|
|
184
|
+
if not root.is_dir():
|
|
185
|
+
return graph
|
|
186
|
+
|
|
187
|
+
spec = get_gitignore_spec(root)
|
|
188
|
+
py_files = []
|
|
189
|
+
|
|
190
|
+
for current_root, dirs, files in os.walk(root):
|
|
191
|
+
cpath = Path(current_root)
|
|
192
|
+
dirs[:] = [d for d in dirs if not is_ignored(cpath / d, root, spec)]
|
|
193
|
+
for f in files:
|
|
194
|
+
p = cpath / f
|
|
195
|
+
if p.suffix == ".py" and not is_ignored(p, root, spec):
|
|
196
|
+
py_files.append(p)
|
|
197
|
+
|
|
198
|
+
# Collect module names
|
|
199
|
+
module_names: set[str] = set()
|
|
200
|
+
file_map: dict[Path, str] = {}
|
|
201
|
+
|
|
202
|
+
for p in py_files:
|
|
203
|
+
try:
|
|
204
|
+
rel = p.relative_to(root)
|
|
205
|
+
mod_parts = list(rel.parts)
|
|
206
|
+
if mod_parts[-1] == "__init__.py":
|
|
207
|
+
mod_parts.pop()
|
|
208
|
+
else:
|
|
209
|
+
mod_parts[-1] = p.stem
|
|
210
|
+
mod_name = ".".join(mod_parts)
|
|
211
|
+
if mod_name:
|
|
212
|
+
module_names.add(mod_name)
|
|
213
|
+
file_map[p] = mod_name
|
|
214
|
+
except ValueError:
|
|
215
|
+
continue
|
|
216
|
+
|
|
217
|
+
# Parse imports
|
|
218
|
+
for p, mod_name in file_map.items():
|
|
219
|
+
imports = set()
|
|
220
|
+
try:
|
|
221
|
+
source = p.read_text(encoding="utf-8", errors="ignore")
|
|
222
|
+
tree = ast.parse(source, filename=str(p))
|
|
223
|
+
for node in ast.walk(tree):
|
|
224
|
+
if isinstance(node, ast.Import):
|
|
225
|
+
for alias in node.names:
|
|
226
|
+
base = alias.name.split(".")[0]
|
|
227
|
+
if any(m.startswith(base) for m in module_names):
|
|
228
|
+
imports.add(alias.name)
|
|
229
|
+
elif isinstance(node, ast.ImportFrom) and node.module:
|
|
230
|
+
base = node.module.split(".")[0]
|
|
231
|
+
if any(m.startswith(base) for m in module_names):
|
|
232
|
+
imports.add(node.module)
|
|
233
|
+
except SyntaxError:
|
|
234
|
+
pass
|
|
235
|
+
except (OSError, ValueError) as exc:
|
|
236
|
+
logger.debug("Failed parsing imports in %s: %s", p, exc)
|
|
237
|
+
|
|
238
|
+
graph.nodes[mod_name] = sorted(imports)
|
|
239
|
+
|
|
240
|
+
return graph
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import tiktoken
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def count_tokens(text: str) -> int:
|
|
5
|
+
try:
|
|
6
|
+
encoding = tiktoken.get_encoding("cl100k_base")
|
|
7
|
+
return len(encoding.encode(text))
|
|
8
|
+
except (ValueError, KeyError, RuntimeError, TypeError):
|
|
9
|
+
# Fallback approximation: 1 token ~= 4 chars
|
|
10
|
+
return len(text) // 4
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def trim_context(text: str, max_tokens: int, keep: str = "head") -> str:
|
|
14
|
+
"""Trim text to a token budget.
|
|
15
|
+
|
|
16
|
+
keep="head" preserves the beginning of static context such as files.
|
|
17
|
+
keep="tail" preserves the end of rolling context such as chat history.
|
|
18
|
+
"""
|
|
19
|
+
if keep not in {"head", "tail"}:
|
|
20
|
+
raise ValueError("keep must be 'head' or 'tail'")
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
encoding = tiktoken.get_encoding("cl100k_base")
|
|
24
|
+
tokens = encoding.encode(text)
|
|
25
|
+
if len(tokens) <= max_tokens:
|
|
26
|
+
return text
|
|
27
|
+
if keep == "tail":
|
|
28
|
+
return "...[Context Trimmed]...\n" + encoding.decode(tokens[-max_tokens:])
|
|
29
|
+
return encoding.decode(tokens[:max_tokens]) + "\n...[Context Trimmed]..."
|
|
30
|
+
except (ValueError, KeyError, RuntimeError, TypeError):
|
|
31
|
+
# Fallback
|
|
32
|
+
max_chars = max_tokens * 4
|
|
33
|
+
if len(text) <= max_chars:
|
|
34
|
+
return text
|
|
35
|
+
if keep == "tail":
|
|
36
|
+
return "...[Context Trimmed]...\n" + text[-max_chars:]
|
|
37
|
+
return text[:max_chars] + "\n...[Context Trimmed]..."
|