valkyria-cli 0.1.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.
- valkyria_cli-0.1.0.dist-info/METADATA +117 -0
- valkyria_cli-0.1.0.dist-info/RECORD +55 -0
- valkyria_cli-0.1.0.dist-info/WHEEL +4 -0
- valkyria_cli-0.1.0.dist-info/entry_points.txt +3 -0
- valkyrie/__init__.py +5 -0
- valkyrie/auth/__init__.py +21 -0
- valkyrie/auth/account.py +245 -0
- valkyrie/auth/firebase_config.py +28 -0
- valkyrie/auth/login_page.py +177 -0
- valkyrie/auth/server.py +145 -0
- valkyrie/auth/tokens.py +196 -0
- valkyrie/chat/__init__.py +37 -0
- valkyrie/chat/clients.py +431 -0
- valkyrie/chat/commands.py +229 -0
- valkyrie/chat/engine.py +1458 -0
- valkyrie/chat/messages.py +108 -0
- valkyrie/chat/renderer.py +122 -0
- valkyrie/cli/__init__.py +2 -0
- valkyrie/cli/auth_commands.py +61 -0
- valkyrie/cli/chat_commands.py +139 -0
- valkyrie/cli/keys_commands.py +275 -0
- valkyrie/cli/main.py +179 -0
- valkyrie/cli/model_commands.py +334 -0
- valkyrie/cli/rag_commands.py +191 -0
- valkyrie/cli/session_commands.py +247 -0
- valkyrie/cli/setup_wizard.py +510 -0
- valkyrie/core/__init__.py +2 -0
- valkyrie/core/cards.py +55 -0
- valkyrie/core/config.py +84 -0
- valkyrie/core/console.py +122 -0
- valkyrie/core/paths.py +68 -0
- valkyrie/core/workspace.py +87 -0
- valkyrie/providers/__init__.py +23 -0
- valkyrie/providers/base.py +78 -0
- valkyrie/providers/gemini.py +141 -0
- valkyrie/providers/groq.py +102 -0
- valkyrie/providers/nvidia.py +95 -0
- valkyrie/providers/registry.py +233 -0
- valkyrie/rag/__init__.py +32 -0
- valkyrie/rag/chunker.py +75 -0
- valkyrie/rag/embeddings.py +227 -0
- valkyrie/rag/files.py +163 -0
- valkyrie/rag/pinecone_store.py +191 -0
- valkyrie/rag/pipeline.py +170 -0
- valkyrie/sessions/__init__.py +9 -0
- valkyrie/sessions/store.py +544 -0
- valkyrie/tools/__init__.py +6 -0
- valkyrie/tools/local_fs.py +423 -0
- valkyrie/tools/registry.py +100 -0
- valkyrie/tools/web_search.py +73 -0
- valkyrie/vault/__init__.py +26 -0
- valkyrie/vault/crypto.py +119 -0
- valkyrie/vault/prompt.py +77 -0
- valkyrie/vault/store.py +207 -0
- valkyrie/vault/sync.py +140 -0
valkyrie/core/console.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Console and theme — sharp editorial terminal design.
|
|
3
|
+
|
|
4
|
+
A restrained obsidian/slate grid, electric cyan functional accent, confident typography,
|
|
5
|
+
and zero decorative fluff. Adapts gracefully to narrow terminals (< 80 columns).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from rich import box
|
|
9
|
+
from rich.console import Console
|
|
10
|
+
from rich.panel import Panel
|
|
11
|
+
from rich.text import Text
|
|
12
|
+
from rich.theme import Theme
|
|
13
|
+
|
|
14
|
+
# ── Editorial terminal palette ──────────────────────────────────────────────
|
|
15
|
+
VALKYRIE_THEME = Theme(
|
|
16
|
+
{
|
|
17
|
+
# Core hierarchy
|
|
18
|
+
"title": "bold bright_white",
|
|
19
|
+
"subtitle": "dim white",
|
|
20
|
+
"heading": "bold bright_cyan",
|
|
21
|
+
"body": "white",
|
|
22
|
+
"dim": "dim white",
|
|
23
|
+
"muted": "grey62",
|
|
24
|
+
# Status
|
|
25
|
+
"success": "bold bright_green",
|
|
26
|
+
"warning": "bold bright_yellow",
|
|
27
|
+
"error": "bold bright_red",
|
|
28
|
+
"info": "bright_cyan",
|
|
29
|
+
# Accent — electric cyan & subtle chrome
|
|
30
|
+
"accent": "bold bright_cyan",
|
|
31
|
+
"accent.dim": "cyan",
|
|
32
|
+
# UI elements
|
|
33
|
+
"rule": "grey35",
|
|
34
|
+
"prompt": "bold bright_white",
|
|
35
|
+
"prompt.arrow": "bright_cyan",
|
|
36
|
+
"key": "bold white",
|
|
37
|
+
"value": "white",
|
|
38
|
+
"tag": "bold black on bright_cyan",
|
|
39
|
+
"tag.dim": "bold bright_cyan",
|
|
40
|
+
# Model & provider
|
|
41
|
+
"model": "bold bright_magenta",
|
|
42
|
+
"provider": "bold bright_blue",
|
|
43
|
+
# Code
|
|
44
|
+
"code": "bright_white on grey11",
|
|
45
|
+
}
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# ── Shared console instance ────────────────────────────────────────────────
|
|
49
|
+
console = Console(theme=VALKYRIE_THEME, highlight=False)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def render_meter(ratio: float, *, width: int = 12) -> str:
|
|
53
|
+
"""Render a compact visual meter bar using block glyphs."""
|
|
54
|
+
clamped = max(0.0, min(1.0, ratio))
|
|
55
|
+
filled = round(clamped * width)
|
|
56
|
+
empty = width - filled
|
|
57
|
+
bar_color = (
|
|
58
|
+
"bright_red"
|
|
59
|
+
if clamped >= 0.85
|
|
60
|
+
else ("bright_yellow" if clamped >= 0.65 else "bright_cyan")
|
|
61
|
+
)
|
|
62
|
+
return f"[{bar_color}]{'█' * filled}[/{bar_color}][grey30]{'░' * empty}[/grey30]"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def print_banner() -> None:
|
|
66
|
+
"""Print the Valkyrie editorial masthead, adaptive to terminal width."""
|
|
67
|
+
from valkyrie import __version__
|
|
68
|
+
|
|
69
|
+
console.print()
|
|
70
|
+
header = Text()
|
|
71
|
+
header.append("◆ ", style="bold bright_cyan")
|
|
72
|
+
header.append("VALKYRIE", style="bold bright_white")
|
|
73
|
+
header.append(f" v{__version__}", style="grey62")
|
|
74
|
+
if console.width >= 64:
|
|
75
|
+
header.append(" · ", style="grey35")
|
|
76
|
+
header.append("TERMINAL-FIRST AI WORKSPACE", style="bold bright_cyan")
|
|
77
|
+
header.append("\n")
|
|
78
|
+
header.append(" Sharp mind. Crisp edges. Zero fluff.", style="italic grey70")
|
|
79
|
+
|
|
80
|
+
console.print(
|
|
81
|
+
Panel(
|
|
82
|
+
header,
|
|
83
|
+
box=box.ROUNDED,
|
|
84
|
+
border_style="bright_cyan",
|
|
85
|
+
padding=(0, 2),
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def print_rule(title: str = "") -> None:
|
|
91
|
+
"""Print a sharp horizontal rule with optional section label."""
|
|
92
|
+
if title:
|
|
93
|
+
console.rule(f" [heading]◆ {title}[/heading] ", style="rule")
|
|
94
|
+
else:
|
|
95
|
+
console.rule(style="rule")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def print_success(msg: str) -> None:
|
|
99
|
+
"""Print a success message."""
|
|
100
|
+
console.print(f" [success]◆[/success] {msg}")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def print_warning(msg: str) -> None:
|
|
104
|
+
"""Print a warning message."""
|
|
105
|
+
console.print(f" [warning]▲[/warning] {msg}")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def print_error(msg: str) -> None:
|
|
109
|
+
"""Print an error message."""
|
|
110
|
+
console.print(f" [error]✕[/error] {msg}")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def print_info(msg: str) -> None:
|
|
114
|
+
"""Print an info message."""
|
|
115
|
+
console.print(f" [info]▸[/info] {msg}")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def print_kv(key: str, value: str, indent: int = 2) -> None:
|
|
119
|
+
"""Print a key-value pair with consistent editorial alignment."""
|
|
120
|
+
pad = " " * indent
|
|
121
|
+
key_w = 14 if console.width < 72 else 20
|
|
122
|
+
console.print(f"{pad}[muted]{key:<{key_w}}[/muted] [value]{value}[/value]")
|
valkyrie/core/paths.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Filesystem paths — config, data, and cache directories.
|
|
3
|
+
|
|
4
|
+
Everything lives under ~/.valkyrie/ by default. The XDG_CONFIG_HOME
|
|
5
|
+
and XDG_DATA_HOME environment variables are respected if set.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
_APP_DIR_NAME = ".valkyrie"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def get_config_dir() -> Path:
|
|
15
|
+
"""Return the config directory, creating it if needed."""
|
|
16
|
+
xdg = os.environ.get("XDG_CONFIG_HOME")
|
|
17
|
+
base = Path(xdg) / "valkyrie" if xdg else Path.home() / _APP_DIR_NAME
|
|
18
|
+
base.mkdir(parents=True, exist_ok=True)
|
|
19
|
+
return base
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_data_dir() -> Path:
|
|
23
|
+
"""Return the data directory (sessions, indexes), creating it if needed."""
|
|
24
|
+
xdg = os.environ.get("XDG_DATA_HOME")
|
|
25
|
+
base = Path(xdg) / "valkyrie" if xdg else get_config_dir() / "data"
|
|
26
|
+
base.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
return base
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_cache_dir() -> Path:
|
|
31
|
+
"""Return the cache directory, creating it if needed."""
|
|
32
|
+
xdg = os.environ.get("XDG_CACHE_HOME")
|
|
33
|
+
base = Path(xdg) / "valkyrie" if xdg else get_config_dir() / "cache"
|
|
34
|
+
base.mkdir(parents=True, exist_ok=True)
|
|
35
|
+
return base
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def get_vault_path() -> Path:
|
|
39
|
+
"""Return the path to the encrypted vault file."""
|
|
40
|
+
return get_config_dir() / "vault.enc"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def get_auth_path() -> Path:
|
|
44
|
+
"""Return the path to the auth token file."""
|
|
45
|
+
return get_config_dir() / "auth.json"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def get_config_path() -> Path:
|
|
49
|
+
"""Return the path to the main config file."""
|
|
50
|
+
return get_config_dir() / "config.json"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def get_models_cache_path() -> Path:
|
|
54
|
+
"""Return the path to the cached provider model catalog."""
|
|
55
|
+
return get_cache_dir() / "models.json"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def get_sessions_db_path() -> Path:
|
|
59
|
+
"""Return the path to the sessions database."""
|
|
60
|
+
return get_data_dir() / "sessions.db"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def ensure_dirs() -> None:
|
|
64
|
+
"""Create all required directories."""
|
|
65
|
+
get_config_dir()
|
|
66
|
+
get_data_dir()
|
|
67
|
+
get_cache_dir()
|
|
68
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Workspace detection and scoped file discovery.
|
|
3
|
+
|
|
4
|
+
Detection order from cwd upward:
|
|
5
|
+
1. Nearest directory containing a `.valkyrie` marker
|
|
6
|
+
2. Nearest directory containing a `.git` directory/file
|
|
7
|
+
3. Fallback to the launch directory itself
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
_IGNORED_DIRS = {
|
|
16
|
+
".git",
|
|
17
|
+
".hg",
|
|
18
|
+
".svn",
|
|
19
|
+
".venv",
|
|
20
|
+
"venv",
|
|
21
|
+
"__pycache__",
|
|
22
|
+
"node_modules",
|
|
23
|
+
".mypy_cache",
|
|
24
|
+
".ruff_cache",
|
|
25
|
+
".pytest_cache",
|
|
26
|
+
"dist",
|
|
27
|
+
"build",
|
|
28
|
+
".eggs",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def detect_workspace_root(start_dir: Path | None = None) -> Path:
|
|
33
|
+
"""Detect the workspace root starting from start_dir (defaults to cwd)."""
|
|
34
|
+
start = (start_dir or Path.cwd()).resolve()
|
|
35
|
+
candidates = [start, *start.parents]
|
|
36
|
+
|
|
37
|
+
for directory in candidates:
|
|
38
|
+
if (directory / ".valkyrie").exists():
|
|
39
|
+
return directory
|
|
40
|
+
|
|
41
|
+
for directory in candidates:
|
|
42
|
+
if (directory / ".git").exists():
|
|
43
|
+
return directory
|
|
44
|
+
|
|
45
|
+
return start
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def resolve_workspace_path(workspace_root: Path, raw_path: str) -> Path | None:
|
|
49
|
+
"""
|
|
50
|
+
Resolve a path relative to workspace_root, rejecting escapes outside the root.
|
|
51
|
+
|
|
52
|
+
Returns the resolved Path if inside workspace_root, or None if invalid/outside.
|
|
53
|
+
"""
|
|
54
|
+
root = workspace_root.resolve()
|
|
55
|
+
cleaned = raw_path.strip()
|
|
56
|
+
if not cleaned:
|
|
57
|
+
return None
|
|
58
|
+
target = Path(cleaned)
|
|
59
|
+
candidate = target.resolve() if target.is_absolute() else (root / target).resolve()
|
|
60
|
+
try:
|
|
61
|
+
candidate.relative_to(root)
|
|
62
|
+
except ValueError:
|
|
63
|
+
return None
|
|
64
|
+
return candidate
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def list_workspace_files(workspace_root: Path, *, max_files: int = 2000) -> list[str]:
|
|
68
|
+
"""
|
|
69
|
+
Return sorted relative POSIX paths inside workspace_root, skipping ignored dirs.
|
|
70
|
+
"""
|
|
71
|
+
root = workspace_root.resolve()
|
|
72
|
+
if not root.exists():
|
|
73
|
+
return []
|
|
74
|
+
|
|
75
|
+
results: list[str] = []
|
|
76
|
+
for dirpath, dirnames, filenames in os.walk(root):
|
|
77
|
+
dirnames[:] = sorted(
|
|
78
|
+
d for d in dirnames if d not in _IGNORED_DIRS and not d.endswith(".egg-info")
|
|
79
|
+
)
|
|
80
|
+
rel_dir = Path(dirpath).relative_to(root)
|
|
81
|
+
for fname in sorted(filenames):
|
|
82
|
+
rel_path = (rel_dir / fname).as_posix() if str(rel_dir) != "." else fname
|
|
83
|
+
results.append(rel_path)
|
|
84
|
+
if len(results) >= max_files:
|
|
85
|
+
return results
|
|
86
|
+
return results
|
|
87
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Provider module — model discovery, capabilities, and unified registry."""
|
|
2
|
+
|
|
3
|
+
from valkyrie.providers.base import (
|
|
4
|
+
ASSIGNABLE_ROLES,
|
|
5
|
+
ModelCapability,
|
|
6
|
+
ModelInfo,
|
|
7
|
+
Provider,
|
|
8
|
+
)
|
|
9
|
+
from valkyrie.providers.registry import (
|
|
10
|
+
PROVIDERS,
|
|
11
|
+
ModelRegistry,
|
|
12
|
+
get_provider,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"ASSIGNABLE_ROLES",
|
|
17
|
+
"ModelCapability",
|
|
18
|
+
"ModelInfo",
|
|
19
|
+
"ModelRegistry",
|
|
20
|
+
"PROVIDERS",
|
|
21
|
+
"Provider",
|
|
22
|
+
"get_provider",
|
|
23
|
+
]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Provider abstraction — base protocol for all model providers.
|
|
3
|
+
|
|
4
|
+
Each provider must be able to validate its API key, fetch a catalog
|
|
5
|
+
of available models, and tag each model with its capabilities.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from enum import StrEnum
|
|
12
|
+
from typing import Protocol
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ModelCapability(StrEnum):
|
|
16
|
+
"""Capabilities a model can have, mapped to assignment roles."""
|
|
17
|
+
|
|
18
|
+
CHAT = "chat"
|
|
19
|
+
CODE = "code"
|
|
20
|
+
VISION = "vision"
|
|
21
|
+
EMBEDDINGS = "embeddings"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Roles that models can be assigned to
|
|
25
|
+
ASSIGNABLE_ROLES = ["chat", "code", "planner", "vision", "summarization", "embeddings"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class ModelInfo:
|
|
30
|
+
"""Information about a single model from a provider."""
|
|
31
|
+
|
|
32
|
+
id: str # Provider's model ID (e.g. "llama-3.3-70b-versatile")
|
|
33
|
+
name: str # Display name
|
|
34
|
+
provider: str # Provider key (e.g. "groq")
|
|
35
|
+
capabilities: list[ModelCapability] = field(default_factory=list)
|
|
36
|
+
context_window: int | None = None # Max tokens
|
|
37
|
+
description: str = ""
|
|
38
|
+
is_active: bool = True
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def qualified_id(self) -> str:
|
|
42
|
+
"""Full provider/model identifier (e.g. 'groq/llama-3.3-70b-versatile')."""
|
|
43
|
+
return f"{self.provider}/{self.id}"
|
|
44
|
+
|
|
45
|
+
def supports_role(self, role: str) -> bool:
|
|
46
|
+
"""Check if this model can serve a given role."""
|
|
47
|
+
if role == "embeddings":
|
|
48
|
+
return ModelCapability.EMBEDDINGS in self.capabilities
|
|
49
|
+
if role == "vision":
|
|
50
|
+
return ModelCapability.VISION in self.capabilities
|
|
51
|
+
# chat, code, planner, summarization — any text model works
|
|
52
|
+
return (
|
|
53
|
+
ModelCapability.CHAT in self.capabilities
|
|
54
|
+
or ModelCapability.CODE in self.capabilities
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Provider(Protocol):
|
|
59
|
+
"""Protocol that all providers must implement."""
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def name(self) -> str:
|
|
63
|
+
"""Human-readable provider name."""
|
|
64
|
+
...
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def key(self) -> str:
|
|
68
|
+
"""Provider key (e.g. 'groq')."""
|
|
69
|
+
...
|
|
70
|
+
|
|
71
|
+
async def validate_key(self, api_key: str) -> bool:
|
|
72
|
+
"""Validate that the API key is working. Returns True if valid."""
|
|
73
|
+
...
|
|
74
|
+
|
|
75
|
+
async def fetch_models(self, api_key: str) -> list[ModelInfo]:
|
|
76
|
+
"""Fetch the catalog of available models for this key."""
|
|
77
|
+
...
|
|
78
|
+
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Gemini provider — Google's Gemini models.
|
|
3
|
+
|
|
4
|
+
API docs: https://ai.google.dev/api/models
|
|
5
|
+
Models endpoint: GET https://generativelanguage.googleapis.com/v1beta/models
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
from valkyrie.providers.base import ModelCapability, ModelInfo
|
|
15
|
+
|
|
16
|
+
GEMINI_API_BASE = "https://generativelanguage.googleapis.com/v1beta"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
_NON_CHAT_KEYWORDS = (
|
|
20
|
+
"-tts",
|
|
21
|
+
"-transcribe",
|
|
22
|
+
"lyria",
|
|
23
|
+
"imagen",
|
|
24
|
+
"-image",
|
|
25
|
+
"nano-banana",
|
|
26
|
+
"robotics",
|
|
27
|
+
"computer-use",
|
|
28
|
+
"veo-",
|
|
29
|
+
"aqa",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
_VISION_PREFIXES = (
|
|
33
|
+
"gemini-4",
|
|
34
|
+
"gemini-3",
|
|
35
|
+
"gemini-2",
|
|
36
|
+
"gemini-1.5",
|
|
37
|
+
"gemini-omni",
|
|
38
|
+
"gemini-flash-latest",
|
|
39
|
+
"gemini-pro-latest",
|
|
40
|
+
"gemini-pro-vision",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class GeminiProvider:
|
|
45
|
+
"""Google Gemini provider."""
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def name(self) -> str:
|
|
49
|
+
return "Google Gemini"
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def key(self) -> str:
|
|
53
|
+
return "gemini"
|
|
54
|
+
|
|
55
|
+
async def validate_key(self, api_key: str) -> bool:
|
|
56
|
+
"""Validate by listing models."""
|
|
57
|
+
try:
|
|
58
|
+
async with httpx.AsyncClient() as client:
|
|
59
|
+
resp = await client.get(
|
|
60
|
+
f"{GEMINI_API_BASE}/models",
|
|
61
|
+
params={"key": api_key},
|
|
62
|
+
timeout=10.0,
|
|
63
|
+
)
|
|
64
|
+
return resp.status_code == 200
|
|
65
|
+
except httpx.HTTPError:
|
|
66
|
+
return False
|
|
67
|
+
|
|
68
|
+
async def fetch_models(self, api_key: str) -> list[ModelInfo]:
|
|
69
|
+
"""Fetch Gemini model catalog."""
|
|
70
|
+
all_models: list[ModelInfo] = []
|
|
71
|
+
page_token: str | None = None
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
async with httpx.AsyncClient() as client:
|
|
75
|
+
while True:
|
|
76
|
+
params: dict[str, str] = {"key": api_key, "pageSize": "100"}
|
|
77
|
+
if page_token:
|
|
78
|
+
params["pageToken"] = page_token
|
|
79
|
+
|
|
80
|
+
resp = await client.get(
|
|
81
|
+
f"{GEMINI_API_BASE}/models",
|
|
82
|
+
params=params,
|
|
83
|
+
timeout=10.0,
|
|
84
|
+
)
|
|
85
|
+
resp.raise_for_status()
|
|
86
|
+
data = resp.json()
|
|
87
|
+
|
|
88
|
+
for item in data.get("models", []):
|
|
89
|
+
model = self._parse_model(item)
|
|
90
|
+
if model is not None:
|
|
91
|
+
all_models.append(model)
|
|
92
|
+
|
|
93
|
+
page_token = data.get("nextPageToken")
|
|
94
|
+
if not page_token:
|
|
95
|
+
break
|
|
96
|
+
|
|
97
|
+
except httpx.HTTPError:
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
return all_models
|
|
101
|
+
|
|
102
|
+
def _parse_model(self, item: dict[str, Any]) -> ModelInfo | None:
|
|
103
|
+
"""Parse a single model entry from the Gemini API."""
|
|
104
|
+
# Model name is like "models/gemini-3.8-flash"
|
|
105
|
+
full_name: str = item.get("name", "")
|
|
106
|
+
model_id = full_name.removeprefix("models/")
|
|
107
|
+
if not model_id:
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
lower_id = model_id.lower()
|
|
111
|
+
methods: list[str] = item.get("supportedGenerationMethods", [])
|
|
112
|
+
caps: list[ModelCapability] = []
|
|
113
|
+
|
|
114
|
+
if "embedContent" in methods:
|
|
115
|
+
caps.append(ModelCapability.EMBEDDINGS)
|
|
116
|
+
elif "generateContent" in methods or "streamGenerateContent" in methods:
|
|
117
|
+
# Skip specialized audio/music/image/TTS/robotics endpoints from text chat
|
|
118
|
+
if any(kw in lower_id for kw in _NON_CHAT_KEYWORDS):
|
|
119
|
+
return None
|
|
120
|
+
|
|
121
|
+
caps.append(ModelCapability.CHAT)
|
|
122
|
+
caps.append(ModelCapability.CODE)
|
|
123
|
+
|
|
124
|
+
if any(lower_id.startswith(prefix) for prefix in _VISION_PREFIXES):
|
|
125
|
+
caps.append(ModelCapability.VISION)
|
|
126
|
+
|
|
127
|
+
if not caps:
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
display_name: str = item.get("displayName", model_id)
|
|
131
|
+
ctx = item.get("inputTokenLimit")
|
|
132
|
+
|
|
133
|
+
return ModelInfo(
|
|
134
|
+
id=model_id,
|
|
135
|
+
name=display_name,
|
|
136
|
+
provider="gemini",
|
|
137
|
+
capabilities=caps,
|
|
138
|
+
context_window=ctx,
|
|
139
|
+
description=item.get("description", ""),
|
|
140
|
+
)
|
|
141
|
+
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Groq provider — fast inference for open models.
|
|
3
|
+
|
|
4
|
+
API docs: https://console.groq.com/docs/api-reference
|
|
5
|
+
Models endpoint: GET https://api.groq.com/openai/v1/models
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import httpx
|
|
11
|
+
|
|
12
|
+
from valkyrie.providers.base import ModelCapability, ModelInfo
|
|
13
|
+
|
|
14
|
+
GROQ_API_BASE = "https://api.groq.com/openai/v1"
|
|
15
|
+
|
|
16
|
+
# Known capability hints for Groq models
|
|
17
|
+
_VISION_MODELS = {
|
|
18
|
+
"llama-3.2-11b-vision-preview",
|
|
19
|
+
"llama-3.2-90b-vision-preview",
|
|
20
|
+
"meta-llama/llama-4-scout-17b-16e-instruct",
|
|
21
|
+
}
|
|
22
|
+
_EMBEDDING_MODELS: set[str] = set() # Groq doesn't currently offer embeddings
|
|
23
|
+
_NON_CHAT_PREFIXES = (
|
|
24
|
+
"whisper-",
|
|
25
|
+
"canopylabs/orpheus",
|
|
26
|
+
"meta-llama/llama-prompt-guard",
|
|
27
|
+
"meta-llama/llama-guard",
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class GroqProvider:
|
|
32
|
+
"""Groq inference provider."""
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def name(self) -> str:
|
|
36
|
+
return "Groq"
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def key(self) -> str:
|
|
40
|
+
return "groq"
|
|
41
|
+
|
|
42
|
+
async def validate_key(self, api_key: str) -> bool:
|
|
43
|
+
"""Validate by listing models."""
|
|
44
|
+
try:
|
|
45
|
+
async with httpx.AsyncClient() as client:
|
|
46
|
+
resp = await client.get(
|
|
47
|
+
f"{GROQ_API_BASE}/models",
|
|
48
|
+
headers={"Authorization": f"Bearer {api_key}"},
|
|
49
|
+
timeout=10.0,
|
|
50
|
+
)
|
|
51
|
+
return resp.status_code == 200
|
|
52
|
+
except httpx.HTTPError:
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
async def fetch_models(self, api_key: str) -> list[ModelInfo]:
|
|
56
|
+
"""Fetch Groq model catalog."""
|
|
57
|
+
try:
|
|
58
|
+
async with httpx.AsyncClient() as client:
|
|
59
|
+
resp = await client.get(
|
|
60
|
+
f"{GROQ_API_BASE}/models",
|
|
61
|
+
headers={"Authorization": f"Bearer {api_key}"},
|
|
62
|
+
timeout=10.0,
|
|
63
|
+
)
|
|
64
|
+
resp.raise_for_status()
|
|
65
|
+
data = resp.json()
|
|
66
|
+
except httpx.HTTPError:
|
|
67
|
+
return []
|
|
68
|
+
|
|
69
|
+
models: list[ModelInfo] = []
|
|
70
|
+
for item in data.get("data", []):
|
|
71
|
+
model_id: str = item.get("id", "")
|
|
72
|
+
if not model_id:
|
|
73
|
+
continue
|
|
74
|
+
if any(model_id.lower().startswith(prefix) for prefix in _NON_CHAT_PREFIXES):
|
|
75
|
+
continue
|
|
76
|
+
|
|
77
|
+
# Determine capabilities
|
|
78
|
+
caps: list[ModelCapability] = []
|
|
79
|
+
if model_id in _VISION_MODELS:
|
|
80
|
+
caps.append(ModelCapability.VISION)
|
|
81
|
+
caps.append(ModelCapability.CHAT)
|
|
82
|
+
elif model_id in _EMBEDDING_MODELS:
|
|
83
|
+
caps.append(ModelCapability.EMBEDDINGS)
|
|
84
|
+
else:
|
|
85
|
+
caps.append(ModelCapability.CHAT)
|
|
86
|
+
caps.append(ModelCapability.CODE)
|
|
87
|
+
|
|
88
|
+
ctx = item.get("context_window")
|
|
89
|
+
|
|
90
|
+
models.append(
|
|
91
|
+
ModelInfo(
|
|
92
|
+
id=model_id,
|
|
93
|
+
name=model_id,
|
|
94
|
+
provider="groq",
|
|
95
|
+
capabilities=caps,
|
|
96
|
+
context_window=ctx,
|
|
97
|
+
is_active=item.get("active", True),
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
return models
|
|
102
|
+
|