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/indexing/tree.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from eva.workspace.gitignore import get_gitignore_spec, is_ignored
|
|
5
|
+
|
|
6
|
+
logger = logging.getLogger(__name__)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _build_tree(dir_path: Path, root_path: Path, spec, prefix: str = "") -> str:
|
|
10
|
+
if is_ignored(dir_path, root_path, spec):
|
|
11
|
+
return ""
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
entries = sorted(dir_path.iterdir(), key=lambda x: (x.is_file(), x.name))
|
|
15
|
+
except PermissionError:
|
|
16
|
+
return ""
|
|
17
|
+
|
|
18
|
+
# Filter ignored
|
|
19
|
+
valid_entries = []
|
|
20
|
+
for entry in entries:
|
|
21
|
+
if not is_ignored(entry, root_path, spec):
|
|
22
|
+
valid_entries.append(entry)
|
|
23
|
+
|
|
24
|
+
tree_str = ""
|
|
25
|
+
for i, entry in enumerate(valid_entries):
|
|
26
|
+
is_last = i == len(valid_entries) - 1
|
|
27
|
+
connector = "└── " if is_last else "├── "
|
|
28
|
+
|
|
29
|
+
tree_str += f"{prefix}{connector}{entry.name}\n"
|
|
30
|
+
|
|
31
|
+
if entry.is_dir():
|
|
32
|
+
extension = " " if is_last else "│ "
|
|
33
|
+
tree_str += _build_tree(entry, root_path, spec, prefix + extension)
|
|
34
|
+
|
|
35
|
+
return tree_str
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
try:
|
|
39
|
+
import eva_fastwalk
|
|
40
|
+
|
|
41
|
+
HAS_RUST = True
|
|
42
|
+
except ImportError:
|
|
43
|
+
HAS_RUST = False
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def generate_tree(path: str | Path) -> str:
|
|
47
|
+
root_path = Path(path).resolve()
|
|
48
|
+
|
|
49
|
+
if HAS_RUST:
|
|
50
|
+
try:
|
|
51
|
+
return eva_fastwalk.fast_generate_tree(str(root_path))
|
|
52
|
+
except (AttributeError, RuntimeError, TypeError, ValueError) as exc:
|
|
53
|
+
logger.debug("Rust fast_generate_tree error, falling back to Python: %s", exc)
|
|
54
|
+
|
|
55
|
+
spec = get_gitignore_spec(root_path)
|
|
56
|
+
|
|
57
|
+
if not root_path.is_dir():
|
|
58
|
+
return f"{root_path.name}\n"
|
|
59
|
+
|
|
60
|
+
return f"{root_path.name}/\n" + _build_tree(root_path, root_path, spec)
|
eva/plugins/__init__.py
ADDED
eva/prompts/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
ASK_SYSTEM_PROMPT = """You are Eva, a terminal AI assistant.
|
|
2
|
+
Be concise, accurate, and direct. Output valid Markdown. Do not wrap everything in code blocks unless it's code.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
EXPLAIN_SYSTEM_PROMPT = """You are Eva, a terminal AI assistant.
|
|
6
|
+
Your task is to explain the provided code, file, or concept clearly and concisely.
|
|
7
|
+
Highlight key mechanics, entry points, and edge cases. Use Markdown.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
CHAT_SYSTEM_PROMPT = """You are Eva, a terminal AI assistant running as a REPL.
|
|
11
|
+
Keep answers extremely concise unless asked for detail.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
ANALYZE_SYSTEM_PROMPT = """You are Eva, a terminal AI assistant.
|
|
15
|
+
Your task is to analyze the provided terminal output or text data.
|
|
16
|
+
Explain what it means, highlight any errors, warnings, or important findings, and provide actionable next steps if applicable. Use Markdown.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
WORK_SYSTEM_PROMPT = """You are Eva, a terminal AI assistant.
|
|
20
|
+
Your task is to convert the user's request into a single valid shell command.
|
|
21
|
+
If the command typically requires elevated or root privileges (such as raw network scanning with nmap, system administration, package management, mounting, etc.), prepend `sudo ` to the command.
|
|
22
|
+
Output ONLY the raw command, with no markdown formatting, no explanation, and no backticks.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
EDIT_SYSTEM_PROMPT = """You are Eva, a terminal AI coding assistant.
|
|
26
|
+
Produce a strictly valid unified diff that implements the user's requested change against the provided file context.
|
|
27
|
+
Output ONLY the unified diff. Do not include markdown fences, prose, or commands.
|
|
28
|
+
You MUST include valid hunk headers with line numbers (e.g., @@ -1,4 +1,5 @@).
|
|
29
|
+
Prefix filenames with a/ and b/ (e.g., --- a/file.py
|
|
30
|
+
+++ b/file.py).
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
COMMIT_SYSTEM_PROMPT = """You are Eva, a terminal AI coding assistant.
|
|
34
|
+
Write a concise conventional commit message for the provided git diff.
|
|
35
|
+
Output only the commit message.
|
|
36
|
+
"""
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import time
|
|
3
|
+
from collections.abc import Iterator
|
|
4
|
+
from typing import Protocol
|
|
5
|
+
|
|
6
|
+
from eva.config import AppConfig
|
|
7
|
+
from eva.security.redaction import redact_secrets
|
|
8
|
+
from eva.telemetry.metrics import record_provider_metric
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class RateLimitError(Exception):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ServerError(Exception):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class AuthError(Exception):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class QuotaExhaustedError(Exception):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Provider(Protocol):
|
|
30
|
+
name: str
|
|
31
|
+
max_rpm: int
|
|
32
|
+
max_rpd: int
|
|
33
|
+
|
|
34
|
+
def generate_stream(
|
|
35
|
+
self, system_prompt: str, user_prompt: str, context: str, config: AppConfig
|
|
36
|
+
) -> Iterator[str]: ...
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# Provider registry
|
|
40
|
+
_PROVIDERS: dict[str, Provider] = {}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def register_provider(provider: Provider):
|
|
44
|
+
_PROVIDERS[provider.name] = provider
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def get_provider(name: str) -> Provider | None:
|
|
48
|
+
return _PROVIDERS.get(name)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def dispatch(
|
|
52
|
+
system_prompt: str, user_prompt: str, context: str, config: AppConfig, pinned_provider: str | None = None
|
|
53
|
+
) -> Iterator[str]:
|
|
54
|
+
if pinned_provider:
|
|
55
|
+
provider = get_provider(pinned_provider)
|
|
56
|
+
if not provider:
|
|
57
|
+
yield f"Error: Provider '{pinned_provider}' not found."
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
logger.debug("Dispatching to pinned provider %s", pinned_provider)
|
|
62
|
+
yield from call_provider(provider, system_prompt, user_prompt, context, config)
|
|
63
|
+
except QuotaExhaustedError as e:
|
|
64
|
+
logger.warning("Pinned provider %s exhausted: %s", pinned_provider, e)
|
|
65
|
+
yield f"\n[Eva Error] {e!s}"
|
|
66
|
+
except Exception as e:
|
|
67
|
+
logger.exception("Pinned provider %s failed", pinned_provider)
|
|
68
|
+
yield f"\n[Eva Error] Pinned provider {pinned_provider} failed: {e!s}"
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
# Unpinned: try default, then fallback
|
|
72
|
+
order = [config.general.default_provider]
|
|
73
|
+
if config.general.fallback_enabled:
|
|
74
|
+
for p in config.general.fallback_order:
|
|
75
|
+
if p not in order:
|
|
76
|
+
order.append(p)
|
|
77
|
+
|
|
78
|
+
failures: list[str] = []
|
|
79
|
+
|
|
80
|
+
for i, p_name in enumerate(order):
|
|
81
|
+
provider = get_provider(p_name)
|
|
82
|
+
if not provider:
|
|
83
|
+
failures.append(f"{p_name}: provider not registered")
|
|
84
|
+
logger.warning("Provider %s is configured but not registered", p_name)
|
|
85
|
+
continue
|
|
86
|
+
|
|
87
|
+
try:
|
|
88
|
+
logger.debug("Dispatching to provider %s", p_name)
|
|
89
|
+
yielded_any = False
|
|
90
|
+
for chunk in call_provider(provider, system_prompt, user_prompt, context, config):
|
|
91
|
+
yielded_any = True
|
|
92
|
+
yield chunk
|
|
93
|
+
return # Success
|
|
94
|
+
except QuotaExhaustedError as e:
|
|
95
|
+
if yielded_any:
|
|
96
|
+
yield f"\n[Eva Error] Provider {p_name} failed mid-stream: {e!s}"
|
|
97
|
+
return
|
|
98
|
+
failures.append(f"{p_name}: {type(e).__name__}: {e}")
|
|
99
|
+
logger.info("Provider %s skipped: %s", p_name, e)
|
|
100
|
+
continue
|
|
101
|
+
except AuthError as e:
|
|
102
|
+
if yielded_any:
|
|
103
|
+
yield f"\n[Eva Error] Provider {p_name} failed mid-stream: {e!s}"
|
|
104
|
+
return
|
|
105
|
+
failures.append(f"{p_name}: {type(e).__name__}: {e}")
|
|
106
|
+
logger.warning("Provider %s auth failed: %s", p_name, e)
|
|
107
|
+
except Exception as e:
|
|
108
|
+
if yielded_any:
|
|
109
|
+
yield f"\n[Eva Error] Provider {p_name} failed mid-stream: {e!s}"
|
|
110
|
+
return
|
|
111
|
+
failures.append(f"{p_name}: {type(e).__name__}: {e}")
|
|
112
|
+
logger.exception("Provider %s failed", p_name)
|
|
113
|
+
|
|
114
|
+
detail = "; ".join(failures) if failures else "no providers were available"
|
|
115
|
+
yield f"\n[Eva Error] All available providers failed or exhausted their local budget. Failures: {detail}"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# Import budget quota check after dispatch is defined so circular imports are avoided
|
|
119
|
+
from eva.workflows.budget import check_and_increment
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def call_provider(
|
|
123
|
+
provider: Provider, system_prompt: str, user_prompt: str, context: str, config: AppConfig
|
|
124
|
+
) -> Iterator[str]:
|
|
125
|
+
if not check_and_increment(provider.name, provider.max_rpm, provider.max_rpd):
|
|
126
|
+
logger.info("Local budget exhausted for provider %s", provider.name)
|
|
127
|
+
record_provider_metric(provider.name, 0.0, False, error_type="QuotaExhaustedError", config=config)
|
|
128
|
+
raise QuotaExhaustedError(
|
|
129
|
+
f"Local budget exhausted for {provider.name}. RPM: {provider.max_rpm}, RPD: {provider.max_rpd}"
|
|
130
|
+
)
|
|
131
|
+
safe_user_prompt = redact_secrets(user_prompt)
|
|
132
|
+
safe_context = redact_secrets(context)
|
|
133
|
+
start_t = time.time()
|
|
134
|
+
try:
|
|
135
|
+
yield from provider.generate_stream(system_prompt, safe_user_prompt, safe_context, config)
|
|
136
|
+
duration = time.time() - start_t
|
|
137
|
+
record_provider_metric(provider.name, duration, True, config=config)
|
|
138
|
+
except Exception as exc:
|
|
139
|
+
duration = time.time() - start_t
|
|
140
|
+
record_provider_metric(provider.name, duration, False, error_type=type(exc).__name__, config=config)
|
|
141
|
+
raise
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# Auto-register providers
|
|
145
|
+
import eva.providers.gemini_provider
|
|
146
|
+
import eva.providers.groq_provider
|
|
147
|
+
import eva.providers.llamacpp_provider
|
|
148
|
+
import eva.providers.ollama_provider
|
|
149
|
+
import eva.providers.opencode_zen_provider
|
|
150
|
+
import eva.providers.openrouter_provider # noqa: F401
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
from collections.abc import Iterator
|
|
2
|
+
|
|
3
|
+
from google import genai
|
|
4
|
+
from google.genai import errors
|
|
5
|
+
|
|
6
|
+
from eva.config import AppConfig, get_api_key
|
|
7
|
+
from eva.indexing.tokenizer import trim_context
|
|
8
|
+
from eva.providers import AuthError, Provider, RateLimitError, ServerError, register_provider
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class GeminiProvider(Provider):
|
|
12
|
+
name = "gemini"
|
|
13
|
+
max_rpm = 15
|
|
14
|
+
max_rpd = 1500
|
|
15
|
+
|
|
16
|
+
def generate_stream(self, system_prompt: str, user_prompt: str, context: str, config: AppConfig) -> Iterator[str]:
|
|
17
|
+
api_key = get_api_key(self.name)
|
|
18
|
+
if not api_key:
|
|
19
|
+
raise AuthError(f"Missing API key for {self.name}. Use: eva config set-key {self.name}")
|
|
20
|
+
|
|
21
|
+
client = genai.Client(api_key=api_key)
|
|
22
|
+
|
|
23
|
+
provider_config = config.providers.get(self.name)
|
|
24
|
+
model = provider_config.model if provider_config else "gemini-3-flash"
|
|
25
|
+
|
|
26
|
+
trimmed_context = trim_context(context, max_tokens=1000000) if context else ""
|
|
27
|
+
|
|
28
|
+
prompt = ""
|
|
29
|
+
if trimmed_context:
|
|
30
|
+
prompt += f"Context:\n{trimmed_context}\n\n"
|
|
31
|
+
prompt += user_prompt
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
response = client.models.generate_content_stream(
|
|
35
|
+
model=model,
|
|
36
|
+
contents=prompt,
|
|
37
|
+
config=genai.types.GenerateContentConfig(
|
|
38
|
+
system_instruction=system_prompt,
|
|
39
|
+
),
|
|
40
|
+
)
|
|
41
|
+
for chunk in response:
|
|
42
|
+
if chunk.text:
|
|
43
|
+
yield chunk.text
|
|
44
|
+
except errors.APIError as e:
|
|
45
|
+
if e.code == 401 or e.code == 403:
|
|
46
|
+
raise AuthError("Invalid API key for Gemini.")
|
|
47
|
+
if e.code == 429:
|
|
48
|
+
raise RateLimitError("Rate limited by Gemini.")
|
|
49
|
+
if e.code in [502, 503]:
|
|
50
|
+
raise ServerError("Gemini server error.")
|
|
51
|
+
raise
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_models() -> list[dict]:
|
|
55
|
+
api_key = get_api_key("gemini")
|
|
56
|
+
if not api_key:
|
|
57
|
+
return []
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
client = genai.Client(api_key=api_key)
|
|
61
|
+
models = []
|
|
62
|
+
for model in client.models.list():
|
|
63
|
+
name = getattr(model, "name", "")
|
|
64
|
+
display_name = getattr(model, "display_name", name)
|
|
65
|
+
if name:
|
|
66
|
+
models.append({"id": name, "name": display_name})
|
|
67
|
+
return models
|
|
68
|
+
except (errors.APIError, AttributeError, KeyError, ValueError, RuntimeError):
|
|
69
|
+
return []
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
register_provider(GeminiProvider())
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from eva.config import get_api_key
|
|
4
|
+
from eva.providers import register_provider
|
|
5
|
+
from eva.providers.openai_compat import OpenAICompatibleProvider
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_models() -> list[dict]:
|
|
11
|
+
api_key = get_api_key("groq")
|
|
12
|
+
if not api_key:
|
|
13
|
+
return []
|
|
14
|
+
import httpx
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
resp = httpx.get(
|
|
18
|
+
"https://api.groq.com/openai/v1/models", headers={"Authorization": f"Bearer {api_key}"}, timeout=5.0
|
|
19
|
+
)
|
|
20
|
+
if resp.status_code == 200:
|
|
21
|
+
return resp.json().get("data", [])
|
|
22
|
+
logger.warning("Groq model list fetch failed with HTTP %s", resp.status_code)
|
|
23
|
+
except (httpx.HTTPError, KeyError, ValueError, RuntimeError) as exc:
|
|
24
|
+
logger.warning("Groq model list fetch failed: %s", exc)
|
|
25
|
+
return []
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class GroqProvider(OpenAICompatibleProvider):
|
|
29
|
+
name = "groq"
|
|
30
|
+
base_url = "https://api.groq.com/openai/v1"
|
|
31
|
+
max_rpm = 30
|
|
32
|
+
max_rpd = 1000
|
|
33
|
+
default_model = "llama-3.1-8b-instant"
|
|
34
|
+
max_context_tokens = 7000
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
register_provider(GroqProvider())
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from collections.abc import Iterator
|
|
4
|
+
|
|
5
|
+
from eva.config import AppConfig, get_api_key
|
|
6
|
+
from eva.providers import register_provider
|
|
7
|
+
from eva.providers.openai_compat import OpenAICompatibleProvider
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def get_models() -> list[dict]:
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
host = os.getenv("LLAMACPP_HOST", "http://localhost:8080")
|
|
16
|
+
try:
|
|
17
|
+
resp = httpx.get(f"{host}/v1/models", timeout=5.0)
|
|
18
|
+
if resp.status_code == 200:
|
|
19
|
+
return resp.json().get("data", [])
|
|
20
|
+
except (httpx.HTTPError, KeyError, ValueError, RuntimeError) as exc:
|
|
21
|
+
logger.warning("llama.cpp model list fetch failed: %s", exc)
|
|
22
|
+
return []
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class LlamaCppProvider(OpenAICompatibleProvider):
|
|
26
|
+
name = "llamacpp"
|
|
27
|
+
base_url = os.getenv("LLAMACPP_HOST", "http://localhost:8080") + "/v1"
|
|
28
|
+
max_rpm = 999
|
|
29
|
+
max_rpd = 9999
|
|
30
|
+
default_model = "default"
|
|
31
|
+
max_context_tokens = 8000
|
|
32
|
+
|
|
33
|
+
def generate_stream(self, system_prompt: str, user_prompt: str, context: str, config: AppConfig) -> Iterator[str]:
|
|
34
|
+
api_key = get_api_key(self.name) or "llamacpp"
|
|
35
|
+
import openai
|
|
36
|
+
from openai import OpenAI
|
|
37
|
+
|
|
38
|
+
from eva.indexing.tokenizer import trim_context
|
|
39
|
+
from eva.providers import AuthError, RateLimitError, ServerError
|
|
40
|
+
|
|
41
|
+
base_url = os.getenv("LLAMACPP_HOST", "http://localhost:8080").rstrip("/") + "/v1"
|
|
42
|
+
client = OpenAI(base_url=base_url, api_key=api_key)
|
|
43
|
+
|
|
44
|
+
provider_config = config.providers.get(self.name)
|
|
45
|
+
model = self._resolve_model(provider_config.model if provider_config else self.default_model)
|
|
46
|
+
|
|
47
|
+
trimmed_context = trim_context(context, max_tokens=self.max_context_tokens) if context else ""
|
|
48
|
+
|
|
49
|
+
messages = [{"role": "system", "content": system_prompt}]
|
|
50
|
+
if trimmed_context:
|
|
51
|
+
messages.append({"role": "user", "content": f"Context:\n{trimmed_context}"})
|
|
52
|
+
messages.append({"role": "user", "content": user_prompt})
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
stream = client.chat.completions.create(model=model, messages=messages, stream=True)
|
|
56
|
+
for chunk in stream:
|
|
57
|
+
if chunk.choices and chunk.choices[0].delta.content:
|
|
58
|
+
yield chunk.choices[0].delta.content
|
|
59
|
+
except openai.AuthenticationError:
|
|
60
|
+
raise AuthError(f"Invalid API key for {self.name}.")
|
|
61
|
+
except openai.RateLimitError:
|
|
62
|
+
raise RateLimitError(f"Rate limited by {self.name}.")
|
|
63
|
+
except openai.APIConnectionError as e:
|
|
64
|
+
raise ServerError(f"llama.cpp server unavailable at {base_url}: {e}")
|
|
65
|
+
except openai.APIStatusError as e:
|
|
66
|
+
raise ServerError(f"llama.cpp server error: {e}")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
register_provider(LlamaCppProvider())
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from collections.abc import Iterator
|
|
4
|
+
|
|
5
|
+
from eva.config import AppConfig, get_api_key
|
|
6
|
+
from eva.providers import register_provider
|
|
7
|
+
from eva.providers.openai_compat import OpenAICompatibleProvider
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def get_models() -> list[dict]:
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
host = os.getenv("OLLAMA_HOST", "http://localhost:11434")
|
|
16
|
+
try:
|
|
17
|
+
resp = httpx.get(f"{host}/api/tags", timeout=5.0)
|
|
18
|
+
if resp.status_code == 200:
|
|
19
|
+
models_raw = resp.json().get("models", [])
|
|
20
|
+
return [{"id": m.get("name"), "name": m.get("name")} for m in models_raw if m.get("name")]
|
|
21
|
+
except (httpx.HTTPError, KeyError, ValueError, RuntimeError) as exc:
|
|
22
|
+
logger.warning("Ollama model list fetch failed: %s", exc)
|
|
23
|
+
return []
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class OllamaProvider(OpenAICompatibleProvider):
|
|
27
|
+
name = "ollama"
|
|
28
|
+
base_url = os.getenv("OLLAMA_HOST", "http://localhost:11434") + "/v1"
|
|
29
|
+
max_rpm = 999
|
|
30
|
+
max_rpd = 9999
|
|
31
|
+
default_model = "gemma4:12b"
|
|
32
|
+
max_context_tokens = 8000
|
|
33
|
+
|
|
34
|
+
def generate_stream(self, system_prompt: str, user_prompt: str, context: str, config: AppConfig) -> Iterator[str]:
|
|
35
|
+
api_key = get_api_key(self.name) or "ollama"
|
|
36
|
+
import openai
|
|
37
|
+
from openai import OpenAI
|
|
38
|
+
|
|
39
|
+
from eva.indexing.tokenizer import trim_context
|
|
40
|
+
from eva.providers import AuthError, RateLimitError, ServerError
|
|
41
|
+
|
|
42
|
+
base_url = os.getenv("OLLAMA_HOST", "http://localhost:11434").rstrip("/") + "/v1"
|
|
43
|
+
client = OpenAI(base_url=base_url, api_key=api_key)
|
|
44
|
+
|
|
45
|
+
provider_config = config.providers.get(self.name)
|
|
46
|
+
model = self._resolve_model(provider_config.model if provider_config else self.default_model)
|
|
47
|
+
|
|
48
|
+
trimmed_context = trim_context(context, max_tokens=self.max_context_tokens) if context else ""
|
|
49
|
+
|
|
50
|
+
messages = [{"role": "system", "content": system_prompt}]
|
|
51
|
+
if trimmed_context:
|
|
52
|
+
messages.append({"role": "user", "content": f"Context:\n{trimmed_context}"})
|
|
53
|
+
messages.append({"role": "user", "content": user_prompt})
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
stream = client.chat.completions.create(model=model, messages=messages, stream=True)
|
|
57
|
+
for chunk in stream:
|
|
58
|
+
if chunk.choices and chunk.choices[0].delta.content:
|
|
59
|
+
yield chunk.choices[0].delta.content
|
|
60
|
+
except openai.AuthenticationError:
|
|
61
|
+
raise AuthError(f"Invalid API key for {self.name}.")
|
|
62
|
+
except openai.RateLimitError:
|
|
63
|
+
raise RateLimitError(f"Rate limited by {self.name}.")
|
|
64
|
+
except openai.APIConnectionError as e:
|
|
65
|
+
raise ServerError(f"Ollama server unavailable at {base_url}: {e}")
|
|
66
|
+
except openai.APIStatusError as e:
|
|
67
|
+
raise ServerError(f"Ollama server error: {e}")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
register_provider(OllamaProvider())
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from collections.abc import Iterator
|
|
3
|
+
|
|
4
|
+
import openai
|
|
5
|
+
from openai import OpenAI
|
|
6
|
+
|
|
7
|
+
from eva.config import AppConfig, get_api_key
|
|
8
|
+
from eva.indexing.tokenizer import trim_context
|
|
9
|
+
from eva.providers import AuthError, Provider, RateLimitError, ServerError
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class OpenAICompatibleProvider(Provider):
|
|
15
|
+
"""Base for providers that expose an OpenAI-compatible chat completions API."""
|
|
16
|
+
|
|
17
|
+
name: str
|
|
18
|
+
base_url: str
|
|
19
|
+
max_rpm: int
|
|
20
|
+
max_rpd: int
|
|
21
|
+
default_model: str
|
|
22
|
+
max_context_tokens: int = 4000
|
|
23
|
+
|
|
24
|
+
def generate_stream(self, system_prompt: str, user_prompt: str, context: str, config: AppConfig) -> Iterator[str]:
|
|
25
|
+
api_key = get_api_key(self.name)
|
|
26
|
+
if not api_key:
|
|
27
|
+
raise AuthError(f"Missing API key for {self.name}. Use: eva config set-key {self.name}")
|
|
28
|
+
|
|
29
|
+
client = OpenAI(base_url=self.base_url, api_key=api_key)
|
|
30
|
+
|
|
31
|
+
provider_config = config.providers.get(self.name)
|
|
32
|
+
model = self._resolve_model(provider_config.model if provider_config else self.default_model)
|
|
33
|
+
|
|
34
|
+
trimmed_context = trim_context(context, max_tokens=self.max_context_tokens) if context else ""
|
|
35
|
+
|
|
36
|
+
messages = [{"role": "system", "content": system_prompt}]
|
|
37
|
+
if trimmed_context:
|
|
38
|
+
messages.append({"role": "user", "content": f"Context:\n{trimmed_context}"})
|
|
39
|
+
messages.append({"role": "user", "content": user_prompt})
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
stream = client.chat.completions.create(model=model, messages=messages, stream=True)
|
|
43
|
+
for chunk in stream:
|
|
44
|
+
if chunk.choices and chunk.choices[0].delta.content:
|
|
45
|
+
yield chunk.choices[0].delta.content
|
|
46
|
+
except openai.AuthenticationError:
|
|
47
|
+
raise AuthError(f"Invalid API key for {self.name}.")
|
|
48
|
+
except openai.RateLimitError:
|
|
49
|
+
raise RateLimitError(f"Rate limited by {self.name}.")
|
|
50
|
+
except openai.APIStatusError as e:
|
|
51
|
+
if e.status_code in [502, 503]:
|
|
52
|
+
raise ServerError(f"{self.name} server error.")
|
|
53
|
+
raise
|
|
54
|
+
|
|
55
|
+
def _resolve_model(self, config_model: str) -> str:
|
|
56
|
+
"""Override in subclasses to implement dynamic model resolution."""
|
|
57
|
+
return config_model
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from eva.cache import get_cache
|
|
6
|
+
from eva.providers import register_provider
|
|
7
|
+
from eva.providers.openai_compat import OpenAICompatibleProvider
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
MODEL_CACHE_KEY = "models:opencode_zen:free"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_free_models() -> list[dict]:
|
|
14
|
+
with get_cache() as cache:
|
|
15
|
+
cached = cache.get(MODEL_CACHE_KEY)
|
|
16
|
+
if cached:
|
|
17
|
+
return cached
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
resp = httpx.get("https://opencode.ai/zen/v1/models", timeout=5.0)
|
|
21
|
+
if resp.status_code == 200:
|
|
22
|
+
data = resp.json().get("data", [])
|
|
23
|
+
free_models = [m for m in data if "free" in m.get("id", "").lower() or m.get("id") == "big-pickle"]
|
|
24
|
+
if free_models:
|
|
25
|
+
with get_cache() as cache:
|
|
26
|
+
cache.set(MODEL_CACHE_KEY, free_models, expire=12 * 3600)
|
|
27
|
+
return free_models
|
|
28
|
+
logger.warning("OpenCode Zen model list fetch failed with HTTP %s", resp.status_code)
|
|
29
|
+
except (httpx.HTTPError, KeyError, ValueError, RuntimeError) as exc:
|
|
30
|
+
logger.warning("OpenCode Zen model list fetch failed: %s", exc)
|
|
31
|
+
return []
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class OpenCodeZenProvider(OpenAICompatibleProvider):
|
|
35
|
+
name = "opencode_zen"
|
|
36
|
+
base_url = "https://opencode.ai/zen/v1"
|
|
37
|
+
max_rpm = 10
|
|
38
|
+
max_rpd = 100
|
|
39
|
+
default_model = "big-pickle"
|
|
40
|
+
max_context_tokens = 4000
|
|
41
|
+
|
|
42
|
+
def _resolve_model(self, config_model: str) -> str:
|
|
43
|
+
free_models = get_free_models()
|
|
44
|
+
if free_models:
|
|
45
|
+
# check if config_model is in the free models
|
|
46
|
+
for m in free_models:
|
|
47
|
+
if m["id"] == config_model:
|
|
48
|
+
return config_model
|
|
49
|
+
return free_models[0]["id"]
|
|
50
|
+
return config_model
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
register_provider(OpenCodeZenProvider())
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from eva.cache import get_cache
|
|
6
|
+
from eva.providers import register_provider
|
|
7
|
+
from eva.providers.openai_compat import OpenAICompatibleProvider
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
MODEL_CACHE_KEY = "models:openrouter:free"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_free_models() -> list[dict]:
|
|
14
|
+
with get_cache() as cache:
|
|
15
|
+
cached = cache.get(MODEL_CACHE_KEY)
|
|
16
|
+
if cached:
|
|
17
|
+
return cached
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
resp = httpx.get("https://openrouter.ai/api/v1/models", timeout=5.0)
|
|
21
|
+
if resp.status_code == 200:
|
|
22
|
+
data = resp.json().get("data", [])
|
|
23
|
+
# Filter prompt pricing = 0
|
|
24
|
+
free_models = [m for m in data if m.get("pricing", {}).get("prompt") in ("0", "0.0", 0, 0.0)]
|
|
25
|
+
if free_models:
|
|
26
|
+
with get_cache() as cache:
|
|
27
|
+
cache.set(MODEL_CACHE_KEY, free_models, expire=12 * 3600)
|
|
28
|
+
return free_models
|
|
29
|
+
logger.warning("OpenRouter model list fetch failed with HTTP %s", resp.status_code)
|
|
30
|
+
except (httpx.HTTPError, KeyError, ValueError, RuntimeError) as exc:
|
|
31
|
+
logger.warning("OpenRouter model list fetch failed: %s", exc)
|
|
32
|
+
return []
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class OpenRouterProvider(OpenAICompatibleProvider):
|
|
36
|
+
name = "openrouter"
|
|
37
|
+
base_url = "https://openrouter.ai/api/v1"
|
|
38
|
+
max_rpm = 20
|
|
39
|
+
max_rpd = 50
|
|
40
|
+
default_model = "openrouter/free"
|
|
41
|
+
max_context_tokens = 4000
|
|
42
|
+
|
|
43
|
+
def _resolve_model(self, config_model: str) -> str:
|
|
44
|
+
if config_model != "openrouter/free":
|
|
45
|
+
return config_model
|
|
46
|
+
|
|
47
|
+
free_models = get_free_models()
|
|
48
|
+
if free_models:
|
|
49
|
+
return free_models[0]["id"]
|
|
50
|
+
|
|
51
|
+
logger.warning("No OpenRouter free model list available; falling back to openrouter/auto")
|
|
52
|
+
return "openrouter/auto"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
register_provider(OpenRouterProvider())
|
eva/replay/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from eva.replay.player import display_replay_session, list_replay_sessions, load_replay_session
|
|
2
|
+
from eva.replay.recorder import get_replays_dir, get_session_dir, record_replay_event
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"display_replay_session",
|
|
6
|
+
"get_replays_dir",
|
|
7
|
+
"get_session_dir",
|
|
8
|
+
"list_replay_sessions",
|
|
9
|
+
"load_replay_session",
|
|
10
|
+
"record_replay_event",
|
|
11
|
+
]
|