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/cli/main.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Valkyrie CLI — main entry point.
|
|
3
|
+
|
|
4
|
+
This module defines the root Click command group and wires up all subcommands.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
|
|
11
|
+
from valkyrie import __version__
|
|
12
|
+
from valkyrie.core.console import console, print_banner, print_info, print_warning
|
|
13
|
+
|
|
14
|
+
_CLI_ALIASES = {
|
|
15
|
+
"c": "chat",
|
|
16
|
+
"m": "model",
|
|
17
|
+
"k": "keys",
|
|
18
|
+
"a": "auth",
|
|
19
|
+
"r": "rag",
|
|
20
|
+
"s": "status",
|
|
21
|
+
"ss": "session",
|
|
22
|
+
"in": "login",
|
|
23
|
+
"out": "logout",
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class _AliasedGroup(click.Group):
|
|
28
|
+
"""Click group supporting short aliases and unambiguous command prefixes."""
|
|
29
|
+
|
|
30
|
+
def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:
|
|
31
|
+
rv = super().get_command(ctx, cmd_name)
|
|
32
|
+
if rv is not None:
|
|
33
|
+
return rv
|
|
34
|
+
mapped = _CLI_ALIASES.get(cmd_name.lower())
|
|
35
|
+
if mapped:
|
|
36
|
+
return super().get_command(ctx, mapped)
|
|
37
|
+
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name.lower())]
|
|
38
|
+
if len(matches) == 1:
|
|
39
|
+
return super().get_command(ctx, matches[0])
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@click.group(cls=_AliasedGroup, invoke_without_command=True)
|
|
44
|
+
@click.version_option(__version__, prog_name="valkyrie")
|
|
45
|
+
@click.pass_context
|
|
46
|
+
def cli(ctx: click.Context) -> None:
|
|
47
|
+
"""Valkyrie — a precise, fast terminal AI workspace."""
|
|
48
|
+
ctx.ensure_object(dict)
|
|
49
|
+
if ctx.invoked_subcommand is None:
|
|
50
|
+
from valkyrie.auth.tokens import load_auth
|
|
51
|
+
from valkyrie.core.config import ValkyrieConfig
|
|
52
|
+
|
|
53
|
+
config = ValkyrieConfig.load()
|
|
54
|
+
if config.is_first_run and sys.stdin.isatty():
|
|
55
|
+
from valkyrie.cli.setup_wizard import run_setup_wizard
|
|
56
|
+
|
|
57
|
+
run_setup_wizard()
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
print_banner()
|
|
61
|
+
|
|
62
|
+
if config.is_first_run:
|
|
63
|
+
print_warning(
|
|
64
|
+
"First-run setup is incomplete. Run [bold]valkyrie setup[/bold] to configure."
|
|
65
|
+
)
|
|
66
|
+
else:
|
|
67
|
+
user = load_auth()
|
|
68
|
+
if user is None:
|
|
69
|
+
print_info(
|
|
70
|
+
"Running in local-only mode. Run [bold]valkyrie login[/bold] to enable sync."
|
|
71
|
+
)
|
|
72
|
+
else:
|
|
73
|
+
print_info(f"Signed in as [bold]{user.email}[/bold]")
|
|
74
|
+
|
|
75
|
+
console.print(
|
|
76
|
+
"\n Type [bold]valkyrie --help[/bold] to see available commands.\n",
|
|
77
|
+
style="dim",
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@cli.command()
|
|
82
|
+
def status() -> None:
|
|
83
|
+
"""Show current authentication and configuration status."""
|
|
84
|
+
from valkyrie.auth.tokens import load_auth
|
|
85
|
+
from valkyrie.core.config import ValkyrieConfig
|
|
86
|
+
from valkyrie.core.paths import get_config_dir
|
|
87
|
+
|
|
88
|
+
config_dir = get_config_dir()
|
|
89
|
+
config = ValkyrieConfig.load()
|
|
90
|
+
user = load_auth()
|
|
91
|
+
|
|
92
|
+
from valkyrie.core.console import print_kv, print_rule
|
|
93
|
+
|
|
94
|
+
console.print()
|
|
95
|
+
print_rule("Status")
|
|
96
|
+
console.print()
|
|
97
|
+
print_kv("Config directory", f"[dim]{config_dir}[/dim]")
|
|
98
|
+
|
|
99
|
+
if user is not None:
|
|
100
|
+
print_kv("Auth", f"[green]{user.email}[/green]")
|
|
101
|
+
else:
|
|
102
|
+
print_kv("Auth", "[yellow]not signed in[/yellow]")
|
|
103
|
+
|
|
104
|
+
provider_count = len(config.providers)
|
|
105
|
+
if provider_count > 0:
|
|
106
|
+
print_kv("Providers", f"[green]{provider_count} configured[/green]")
|
|
107
|
+
else:
|
|
108
|
+
print_kv("Providers", "[yellow]none[/yellow]")
|
|
109
|
+
|
|
110
|
+
assignments = config.models
|
|
111
|
+
assigned = sum(
|
|
112
|
+
1
|
|
113
|
+
for v in [
|
|
114
|
+
assignments.chat,
|
|
115
|
+
assignments.code,
|
|
116
|
+
assignments.planner,
|
|
117
|
+
assignments.vision,
|
|
118
|
+
assignments.summarization,
|
|
119
|
+
]
|
|
120
|
+
if v is not None
|
|
121
|
+
)
|
|
122
|
+
print_kv("Model roles", f"[dim]{assigned}/5 assigned[/dim]")
|
|
123
|
+
|
|
124
|
+
first_run_str = (
|
|
125
|
+
"[yellow]pending[/yellow]" if config.is_first_run else "[green]complete[/green]"
|
|
126
|
+
)
|
|
127
|
+
print_kv("First run", first_run_str)
|
|
128
|
+
|
|
129
|
+
from valkyrie.core.paths import get_sessions_db_path
|
|
130
|
+
from valkyrie.core.workspace import detect_workspace_root
|
|
131
|
+
|
|
132
|
+
ws_root = detect_workspace_root()
|
|
133
|
+
print_kv("Workspace", f"[dim]{ws_root}[/dim]")
|
|
134
|
+
print_kv("Vector DB", f"Pinecone ({config.pinecone.index_name})")
|
|
135
|
+
|
|
136
|
+
if get_sessions_db_path().exists():
|
|
137
|
+
from valkyrie.sessions.store import SessionStore
|
|
138
|
+
|
|
139
|
+
latest = SessionStore().get_latest_session(workspace_root=str(ws_root))
|
|
140
|
+
if latest is not None:
|
|
141
|
+
print_kv(
|
|
142
|
+
"Latest session",
|
|
143
|
+
f"[green]{latest.id}[/green] [dim]({latest.title})[/dim]",
|
|
144
|
+
)
|
|
145
|
+
else:
|
|
146
|
+
print_kv("Latest session", "[yellow]none in workspace[/yellow]")
|
|
147
|
+
else:
|
|
148
|
+
print_kv("Active session", "[yellow]none[/yellow]")
|
|
149
|
+
console.print()
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# ── Register subcommand groups ──────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
from valkyrie.cli.auth_commands import auth, login, logout # noqa: E402
|
|
155
|
+
from valkyrie.cli.chat_commands import chat # noqa: E402
|
|
156
|
+
from valkyrie.cli.keys_commands import keys # noqa: E402
|
|
157
|
+
from valkyrie.cli.model_commands import model # noqa: E402
|
|
158
|
+
from valkyrie.cli.rag_commands import rag # noqa: E402
|
|
159
|
+
from valkyrie.cli.session_commands import session # noqa: E402
|
|
160
|
+
from valkyrie.cli.setup_wizard import setup # noqa: E402
|
|
161
|
+
|
|
162
|
+
cli.add_command(auth)
|
|
163
|
+
cli.add_command(chat)
|
|
164
|
+
cli.add_command(keys)
|
|
165
|
+
cli.add_command(model)
|
|
166
|
+
cli.add_command(rag)
|
|
167
|
+
cli.add_command(session)
|
|
168
|
+
cli.add_command(setup)
|
|
169
|
+
cli.add_command(login)
|
|
170
|
+
cli.add_command(logout)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def main() -> None:
|
|
174
|
+
"""Entry point for the CLI."""
|
|
175
|
+
cli()
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
if __name__ == "__main__":
|
|
179
|
+
main()
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Model CLI commands — inspect catalogs, filter by provider, refresh live, and assign roles.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
|
|
9
|
+
import click
|
|
10
|
+
from rich import box
|
|
11
|
+
from rich.table import Table
|
|
12
|
+
from rich.text import Text
|
|
13
|
+
|
|
14
|
+
from valkyrie.core.config import ProviderConfig, ValkyrieConfig
|
|
15
|
+
from valkyrie.core.console import (
|
|
16
|
+
console,
|
|
17
|
+
print_error,
|
|
18
|
+
print_info,
|
|
19
|
+
print_kv,
|
|
20
|
+
print_rule,
|
|
21
|
+
print_success,
|
|
22
|
+
print_warning,
|
|
23
|
+
)
|
|
24
|
+
from valkyrie.providers.base import ASSIGNABLE_ROLES, ModelCapability, ModelInfo
|
|
25
|
+
from valkyrie.providers.registry import PROVIDERS, ModelRegistry
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _format_ctx(ctx: int | None) -> str:
|
|
29
|
+
"""Format context window token count, or 'unknown' if missing."""
|
|
30
|
+
if ctx is None:
|
|
31
|
+
return "unknown"
|
|
32
|
+
if ctx >= 1_000_000:
|
|
33
|
+
return f"{ctx / 1_000_000:.1f}M"
|
|
34
|
+
if ctx >= 1_000:
|
|
35
|
+
return f"{ctx // 1_000}k"
|
|
36
|
+
return str(ctx)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _render_provider_filter_bar(
|
|
40
|
+
registry: ModelRegistry,
|
|
41
|
+
active_provider: str | None,
|
|
42
|
+
) -> None:
|
|
43
|
+
"""Render an editorial provider pill bar showing available providers to filter by."""
|
|
44
|
+
counts = registry.provider_counts()
|
|
45
|
+
total = sum(counts.values())
|
|
46
|
+
bar = Text(" PROVIDERS ", style="grey62")
|
|
47
|
+
|
|
48
|
+
if active_provider is None:
|
|
49
|
+
bar.append(f" ALL ({total}) ", style="bold black on bright_cyan")
|
|
50
|
+
else:
|
|
51
|
+
bar.append(f"ALL ({total})", style="grey62")
|
|
52
|
+
|
|
53
|
+
for pkey in PROVIDERS:
|
|
54
|
+
cnt = counts.get(pkey, 0)
|
|
55
|
+
bar.append(" · ", style="grey35")
|
|
56
|
+
if active_provider == pkey:
|
|
57
|
+
bar.append(f" {pkey.upper()} ({cnt}) ", style="bold black on bright_cyan")
|
|
58
|
+
elif cnt > 0:
|
|
59
|
+
bar.append(f"{pkey} ({cnt})", style="bright_white")
|
|
60
|
+
else:
|
|
61
|
+
bar.append(f"{pkey} (0)", style="grey42")
|
|
62
|
+
|
|
63
|
+
console.print(bar)
|
|
64
|
+
console.print(
|
|
65
|
+
" [muted]Tip: filter anytime with [bold]--provider <name>[/bold] "
|
|
66
|
+
"or in chat with [bold]/model <provider>[/bold].[/muted]"
|
|
67
|
+
)
|
|
68
|
+
console.print()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@click.group(invoke_without_command=True)
|
|
72
|
+
@click.pass_context
|
|
73
|
+
def model(ctx: click.Context) -> None:
|
|
74
|
+
"""Inspect available models and manage role assignments."""
|
|
75
|
+
if ctx.invoked_subcommand is not None:
|
|
76
|
+
return
|
|
77
|
+
|
|
78
|
+
config = ValkyrieConfig.load()
|
|
79
|
+
registry = ModelRegistry.load_cache()
|
|
80
|
+
|
|
81
|
+
console.print()
|
|
82
|
+
print_rule("Model Assignments")
|
|
83
|
+
console.print()
|
|
84
|
+
|
|
85
|
+
for role in ASSIGNABLE_ROLES:
|
|
86
|
+
assigned = getattr(config.models, role, None)
|
|
87
|
+
display = f"[model]{assigned}[/model]" if assigned else "[muted]unassigned[/muted]"
|
|
88
|
+
print_kv(role, display)
|
|
89
|
+
|
|
90
|
+
console.print()
|
|
91
|
+
cached_count = len(registry.models)
|
|
92
|
+
if cached_count > 0:
|
|
93
|
+
counts = registry.provider_counts()
|
|
94
|
+
breakdown = " · ".join(f"{k}: {v}" for k, v in counts.items())
|
|
95
|
+
print_info(
|
|
96
|
+
f"Catalog: [bold]{cached_count}[/bold] models ([dim]{breakdown}[/dim])."
|
|
97
|
+
)
|
|
98
|
+
else:
|
|
99
|
+
print_info(
|
|
100
|
+
"No cached models yet. Run [bold]valkyrie model refresh[/bold] to fetch catalogs."
|
|
101
|
+
)
|
|
102
|
+
console.print()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@model.command(name="list")
|
|
106
|
+
@click.option(
|
|
107
|
+
"-p",
|
|
108
|
+
"--provider",
|
|
109
|
+
type=click.Choice(list(PROVIDERS.keys()), case_sensitive=False),
|
|
110
|
+
default=None,
|
|
111
|
+
help="Filter models to a specific provider (groq, gemini, nvidia).",
|
|
112
|
+
)
|
|
113
|
+
@click.option(
|
|
114
|
+
"-r",
|
|
115
|
+
"--role",
|
|
116
|
+
type=click.Choice(ASSIGNABLE_ROLES, case_sensitive=False),
|
|
117
|
+
default=None,
|
|
118
|
+
help="Only show models compatible with a role.",
|
|
119
|
+
)
|
|
120
|
+
@click.option(
|
|
121
|
+
"-c",
|
|
122
|
+
"--capability",
|
|
123
|
+
type=click.Choice([c.value for c in ModelCapability], case_sensitive=False),
|
|
124
|
+
default=None,
|
|
125
|
+
help="Filter by capability.",
|
|
126
|
+
)
|
|
127
|
+
@click.option(
|
|
128
|
+
"-s",
|
|
129
|
+
"--search",
|
|
130
|
+
default=None,
|
|
131
|
+
help="Filter models by name/ID substring.",
|
|
132
|
+
)
|
|
133
|
+
def list_cmd(
|
|
134
|
+
provider: str | None,
|
|
135
|
+
role: str | None,
|
|
136
|
+
capability: str | None,
|
|
137
|
+
search: str | None,
|
|
138
|
+
) -> None:
|
|
139
|
+
"""List models available under configured keys, optionally filtered by provider."""
|
|
140
|
+
registry = ModelRegistry.load_cache()
|
|
141
|
+
config = ValkyrieConfig.load()
|
|
142
|
+
|
|
143
|
+
console.print()
|
|
144
|
+
print_rule("Model Catalog")
|
|
145
|
+
console.print()
|
|
146
|
+
|
|
147
|
+
if not registry.models:
|
|
148
|
+
print_info("No cached models found.")
|
|
149
|
+
print_info("Run [bold]valkyrie model refresh[/bold] after adding a provider key.")
|
|
150
|
+
console.print()
|
|
151
|
+
return
|
|
152
|
+
|
|
153
|
+
selected_provider = provider.lower() if provider else None
|
|
154
|
+
_render_provider_filter_bar(registry, selected_provider)
|
|
155
|
+
|
|
156
|
+
cap_enum = ModelCapability(capability.lower()) if capability else None
|
|
157
|
+
models = registry.list_models(
|
|
158
|
+
provider=selected_provider,
|
|
159
|
+
capability=cap_enum,
|
|
160
|
+
role=role.lower() if role else None,
|
|
161
|
+
search=search,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
if not models:
|
|
165
|
+
print_warning("No models match the selected filters.")
|
|
166
|
+
console.print()
|
|
167
|
+
return
|
|
168
|
+
|
|
169
|
+
grouped: dict[str, list[ModelInfo]] = {}
|
|
170
|
+
for m in models:
|
|
171
|
+
grouped.setdefault(m.provider, []).append(m)
|
|
172
|
+
|
|
173
|
+
for pkey, items in grouped.items():
|
|
174
|
+
p_obj = PROVIDERS.get(pkey)
|
|
175
|
+
pname = p_obj.name if p_obj else pkey
|
|
176
|
+
table = Table(
|
|
177
|
+
title=f"[heading]◆ {pname}[/heading] [muted]({len(items)})[/muted]",
|
|
178
|
+
title_justify="left",
|
|
179
|
+
box=box.ROUNDED,
|
|
180
|
+
border_style="rule",
|
|
181
|
+
header_style="bold bright_white",
|
|
182
|
+
show_lines=False,
|
|
183
|
+
pad_edge=True,
|
|
184
|
+
)
|
|
185
|
+
table.add_column("#", style="grey50", justify="right", width=3)
|
|
186
|
+
table.add_column("Model", style="model", no_wrap=(console.width >= 78))
|
|
187
|
+
table.add_column("Capabilities", style="bright_cyan")
|
|
188
|
+
table.add_column("Ctx", justify="right", style="dim")
|
|
189
|
+
table.add_column("State", justify="right")
|
|
190
|
+
|
|
191
|
+
for idx, m in enumerate(items, start=1):
|
|
192
|
+
caps_str = " · ".join(c.value for c in m.capabilities) or "unknown"
|
|
193
|
+
ctx_str = _format_ctx(m.context_window)
|
|
194
|
+
if m.qualified_id == config.models.chat:
|
|
195
|
+
state_str = "[tag] DEFAULT [/tag]"
|
|
196
|
+
elif m.is_active:
|
|
197
|
+
state_str = "[success]ready[/success]"
|
|
198
|
+
else:
|
|
199
|
+
state_str = "[warning]retired[/warning]"
|
|
200
|
+
table.add_row(str(idx), m.qualified_id, caps_str, ctx_str, state_str)
|
|
201
|
+
|
|
202
|
+
console.print(table)
|
|
203
|
+
console.print()
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
@model.command()
|
|
207
|
+
@click.option(
|
|
208
|
+
"-p",
|
|
209
|
+
"--provider",
|
|
210
|
+
type=click.Choice(list(PROVIDERS.keys()), case_sensitive=False),
|
|
211
|
+
default=None,
|
|
212
|
+
help="Refresh only a specific provider.",
|
|
213
|
+
)
|
|
214
|
+
def refresh(provider: str | None) -> None:
|
|
215
|
+
"""Validate keys and fetch live model catalogs from providers."""
|
|
216
|
+
from valkyrie.vault.prompt import verify_and_prompt
|
|
217
|
+
from valkyrie.vault.store import get_key, vault_exists
|
|
218
|
+
|
|
219
|
+
console.print()
|
|
220
|
+
print_rule("Refresh Model Catalog")
|
|
221
|
+
console.print()
|
|
222
|
+
|
|
223
|
+
if not vault_exists():
|
|
224
|
+
print_error("No key vault found. Run [bold]valkyrie keys add[/bold] first.")
|
|
225
|
+
console.print()
|
|
226
|
+
return
|
|
227
|
+
|
|
228
|
+
password = verify_and_prompt(action="unlock API keys")
|
|
229
|
+
if password is None:
|
|
230
|
+
console.print()
|
|
231
|
+
return
|
|
232
|
+
|
|
233
|
+
target_providers = [provider.lower()] if provider else list(PROVIDERS.keys())
|
|
234
|
+
api_keys: dict[str, str] = {}
|
|
235
|
+
for pkey in target_providers:
|
|
236
|
+
key = get_key(password, pkey)
|
|
237
|
+
if key:
|
|
238
|
+
api_keys[pkey] = key
|
|
239
|
+
|
|
240
|
+
if not api_keys:
|
|
241
|
+
print_warning("No matching API keys found in vault.")
|
|
242
|
+
console.print()
|
|
243
|
+
return
|
|
244
|
+
|
|
245
|
+
print_info(f"Querying {len(api_keys)} provider(s)...")
|
|
246
|
+
registry = ModelRegistry.load_cache()
|
|
247
|
+
summary = asyncio.run(registry.refresh(api_keys))
|
|
248
|
+
|
|
249
|
+
config = ValkyrieConfig.load()
|
|
250
|
+
console.print()
|
|
251
|
+
for pkey, (valid, count) in summary.items():
|
|
252
|
+
p_obj = PROVIDERS[pkey]
|
|
253
|
+
if valid:
|
|
254
|
+
print_success(f"{p_obj.name:<15} {count} models discovered")
|
|
255
|
+
config.providers[pkey] = ProviderConfig(name=p_obj.name, enabled=True)
|
|
256
|
+
else:
|
|
257
|
+
print_error(f"{p_obj.name:<15} key validation failed or unreachable")
|
|
258
|
+
|
|
259
|
+
config.save()
|
|
260
|
+
console.print()
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
@model.command(name="set")
|
|
264
|
+
@click.argument("role", type=click.Choice(ASSIGNABLE_ROLES, case_sensitive=False))
|
|
265
|
+
@click.argument("model_id")
|
|
266
|
+
def set_cmd(role: str, model_id: str) -> None:
|
|
267
|
+
"""Assign a model (provider/model or unique model ID) to a role, or 'unassigned' to clear."""
|
|
268
|
+
role = role.lower()
|
|
269
|
+
config = ValkyrieConfig.load()
|
|
270
|
+
|
|
271
|
+
console.print()
|
|
272
|
+
print_rule("Set Role Assignment")
|
|
273
|
+
console.print()
|
|
274
|
+
|
|
275
|
+
if model_id.lower() == "unassigned":
|
|
276
|
+
setattr(config.models, role, None)
|
|
277
|
+
config.save()
|
|
278
|
+
print_success(f"Role [bold]{role}[/bold] cleared (unassigned).")
|
|
279
|
+
console.print()
|
|
280
|
+
return
|
|
281
|
+
|
|
282
|
+
registry = ModelRegistry.load_cache()
|
|
283
|
+
resolved_id = model_id
|
|
284
|
+
|
|
285
|
+
if "/" not in model_id and registry.models:
|
|
286
|
+
found, suggestions = registry.resolve_model(model_id)
|
|
287
|
+
if found is None:
|
|
288
|
+
print_error(f"Could not resolve '{model_id}' to a unique model.")
|
|
289
|
+
if suggestions:
|
|
290
|
+
print_info("Matches: " + ", ".join(m.qualified_id for m in suggestions[:5]))
|
|
291
|
+
console.print()
|
|
292
|
+
raise SystemExit(1)
|
|
293
|
+
resolved_id = found.qualified_id
|
|
294
|
+
|
|
295
|
+
if "/" not in resolved_id:
|
|
296
|
+
print_error(
|
|
297
|
+
"Model must be specified as <provider>/<model_id> "
|
|
298
|
+
"(e.g. groq/llama-3.3-70b-versatile)."
|
|
299
|
+
)
|
|
300
|
+
console.print()
|
|
301
|
+
raise SystemExit(1)
|
|
302
|
+
|
|
303
|
+
provider_key, _ = resolved_id.split("/", 1)
|
|
304
|
+
if provider_key not in PROVIDERS:
|
|
305
|
+
print_error(f"Unknown provider '{provider_key}'. Supported: {', '.join(PROVIDERS.keys())}.")
|
|
306
|
+
console.print()
|
|
307
|
+
raise SystemExit(1)
|
|
308
|
+
|
|
309
|
+
if registry.models:
|
|
310
|
+
model_info = registry.get_model(resolved_id)
|
|
311
|
+
if model_info is None:
|
|
312
|
+
print_error(f"Model '{resolved_id}' not found in cached catalog.")
|
|
313
|
+
print_info(
|
|
314
|
+
"Run [bold]valkyrie model list[/bold] to inspect available models, "
|
|
315
|
+
"or [bold]valkyrie model refresh[/bold]."
|
|
316
|
+
)
|
|
317
|
+
console.print()
|
|
318
|
+
raise SystemExit(1)
|
|
319
|
+
|
|
320
|
+
if not model_info.supports_role(role):
|
|
321
|
+
compatible = registry.list_models(role=role)
|
|
322
|
+
print_error(f"Model '{resolved_id}' does not support role '{role}'.")
|
|
323
|
+
if compatible:
|
|
324
|
+
suggestions_txt = ", ".join(m.qualified_id for m in compatible[:5])
|
|
325
|
+
print_info(f"Compatible models: {suggestions_txt}")
|
|
326
|
+
console.print()
|
|
327
|
+
raise SystemExit(1)
|
|
328
|
+
else:
|
|
329
|
+
print_warning("No cached catalog found; saving assignment without capability check.")
|
|
330
|
+
|
|
331
|
+
setattr(config.models, role, resolved_id)
|
|
332
|
+
config.save()
|
|
333
|
+
print_success(f"Assigned [model]{resolved_id}[/model] to [bold]{role}[/bold].")
|
|
334
|
+
console.print()
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""
|
|
2
|
+
RAG CLI commands — index workspace files, semantic search via Pinecone, and inspect status.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import click
|
|
8
|
+
|
|
9
|
+
from valkyrie.core.cards import render_tool_card
|
|
10
|
+
from valkyrie.core.config import ValkyrieConfig
|
|
11
|
+
from valkyrie.core.console import (
|
|
12
|
+
console,
|
|
13
|
+
print_error,
|
|
14
|
+
print_info,
|
|
15
|
+
print_kv,
|
|
16
|
+
print_rule,
|
|
17
|
+
print_success,
|
|
18
|
+
print_warning,
|
|
19
|
+
)
|
|
20
|
+
from valkyrie.core.workspace import detect_workspace_root
|
|
21
|
+
from valkyrie.rag.embeddings import resolve_pinecone_api_key, resolve_pinecone_config
|
|
22
|
+
from valkyrie.rag.pipeline import (
|
|
23
|
+
get_workspace_rag_store,
|
|
24
|
+
index_workspace_path,
|
|
25
|
+
search_workspace_rag,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@click.group()
|
|
30
|
+
def rag() -> None:
|
|
31
|
+
"""Index workspace documents and search with Pinecone DB (.env configured)."""
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@rag.command()
|
|
36
|
+
@click.argument("target", default=".")
|
|
37
|
+
def index(target: str) -> None:
|
|
38
|
+
"""Index a workspace file or directory into Pinecone DB."""
|
|
39
|
+
ws_root = detect_workspace_root()
|
|
40
|
+
config = ValkyrieConfig.load()
|
|
41
|
+
pc_key = resolve_pinecone_api_key(ws_root)
|
|
42
|
+
pc_cfg = resolve_pinecone_config(config, ws_root)
|
|
43
|
+
|
|
44
|
+
console.print()
|
|
45
|
+
print_rule("Pinecone Index")
|
|
46
|
+
console.print()
|
|
47
|
+
print_info(
|
|
48
|
+
f"Data transfer: workspace chunks from [bold]{target}[/bold] -> "
|
|
49
|
+
f"Pinecone index [accent]{pc_cfg.index_name}[/accent]"
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
summary = index_workspace_path(
|
|
54
|
+
ws_root,
|
|
55
|
+
target,
|
|
56
|
+
config=config,
|
|
57
|
+
pinecone_api_key=pc_key,
|
|
58
|
+
)
|
|
59
|
+
except Exception as exc:
|
|
60
|
+
print_error(f"Indexing failed: {exc}")
|
|
61
|
+
console.print()
|
|
62
|
+
raise SystemExit(1) from exc
|
|
63
|
+
|
|
64
|
+
render_tool_card(
|
|
65
|
+
tool="index",
|
|
66
|
+
status="ok",
|
|
67
|
+
target=target,
|
|
68
|
+
summary=(
|
|
69
|
+
f"Indexed {summary.files_indexed} files ({summary.chunks_upserted} chunks) "
|
|
70
|
+
f"into {summary.index_name} [{summary.namespace}] via {summary.embedding_backend}"
|
|
71
|
+
),
|
|
72
|
+
)
|
|
73
|
+
console.print()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@rag.command()
|
|
77
|
+
@click.argument("query")
|
|
78
|
+
@click.option("--top-k", default=5, show_default=True, help="Maximum results to return.")
|
|
79
|
+
def search(query: str, top_k: int) -> None:
|
|
80
|
+
"""Search indexed workspace documents in Pinecone with source citations."""
|
|
81
|
+
ws_root = detect_workspace_root()
|
|
82
|
+
config = ValkyrieConfig.load()
|
|
83
|
+
pc_key = resolve_pinecone_api_key(ws_root)
|
|
84
|
+
|
|
85
|
+
console.print()
|
|
86
|
+
print_rule(f"Pinecone Search · {query}")
|
|
87
|
+
console.print()
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
hits, backend = search_workspace_rag(
|
|
91
|
+
query,
|
|
92
|
+
ws_root,
|
|
93
|
+
config=config,
|
|
94
|
+
pinecone_api_key=pc_key,
|
|
95
|
+
top_k=top_k,
|
|
96
|
+
)
|
|
97
|
+
except Exception as exc:
|
|
98
|
+
print_error(f"Search failed: {exc}")
|
|
99
|
+
console.print()
|
|
100
|
+
raise SystemExit(1) from exc
|
|
101
|
+
|
|
102
|
+
if not hits:
|
|
103
|
+
print_warning("No matching chunks found. Run 'valkyrie rag index' first.")
|
|
104
|
+
console.print()
|
|
105
|
+
return
|
|
106
|
+
|
|
107
|
+
print_info(f"Query embedded with [dim]{backend}[/dim] -> {len(hits)} citation(s)")
|
|
108
|
+
console.print()
|
|
109
|
+
for hit in hits:
|
|
110
|
+
preview = " ".join(hit.text.splitlines()[:3])
|
|
111
|
+
if len(preview) > 200:
|
|
112
|
+
preview = preview[:197] + "..."
|
|
113
|
+
render_tool_card(
|
|
114
|
+
tool="citation",
|
|
115
|
+
status="ok",
|
|
116
|
+
target=hit.citation,
|
|
117
|
+
summary=f"similarity score: {hit.score:.3f}",
|
|
118
|
+
details=preview,
|
|
119
|
+
)
|
|
120
|
+
console.print()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@rag.command()
|
|
124
|
+
def status() -> None:
|
|
125
|
+
"""Show Pinecone retrieval configuration and workspace vector count."""
|
|
126
|
+
ws_root = detect_workspace_root()
|
|
127
|
+
config = ValkyrieConfig.load()
|
|
128
|
+
pc_key = resolve_pinecone_api_key(ws_root)
|
|
129
|
+
pc_cfg = resolve_pinecone_config(config, ws_root)
|
|
130
|
+
|
|
131
|
+
console.print()
|
|
132
|
+
print_rule("Pinecone RAG Status")
|
|
133
|
+
console.print()
|
|
134
|
+
print_kv("Workspace", str(ws_root))
|
|
135
|
+
print_kv("Storage", "Pinecone DB only (zero local vector storage)")
|
|
136
|
+
print_kv("Embedding mode", config.embedding_mode)
|
|
137
|
+
print_kv("Index name", pc_cfg.index_name)
|
|
138
|
+
print_kv("Cloud / Region", f"{pc_cfg.cloud} / {pc_cfg.region}")
|
|
139
|
+
print_kv(
|
|
140
|
+
"Pinecone backend",
|
|
141
|
+
"[green]connected (.env)[/green]" if pc_key else "[yellow]missing in .env[/yellow]",
|
|
142
|
+
)
|
|
143
|
+
if pc_key:
|
|
144
|
+
store = get_workspace_rag_store(
|
|
145
|
+
workspace_root=ws_root,
|
|
146
|
+
config=config,
|
|
147
|
+
pinecone_api_key=pc_key,
|
|
148
|
+
)
|
|
149
|
+
print_kv("Namespace", store.namespace)
|
|
150
|
+
try:
|
|
151
|
+
info = store.stats()
|
|
152
|
+
vec_str = str(info["vectors"]) if info["index_exists"] else "index not created yet"
|
|
153
|
+
print_kv("Vectors", vec_str)
|
|
154
|
+
except Exception as exc:
|
|
155
|
+
print_warning(f"Could not reach Pinecone: {exc}")
|
|
156
|
+
console.print()
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@rag.command()
|
|
160
|
+
@click.option("--yes", is_flag=True, help="Skip confirmation prompt.")
|
|
161
|
+
def clear(yes: bool) -> None:
|
|
162
|
+
"""Clear the current workspace's local or hosted index."""
|
|
163
|
+
ws_root = detect_workspace_root()
|
|
164
|
+
config = ValkyrieConfig.load()
|
|
165
|
+
pc_key = resolve_pinecone_api_key(ws_root)
|
|
166
|
+
|
|
167
|
+
console.print()
|
|
168
|
+
try:
|
|
169
|
+
store = get_workspace_rag_store(
|
|
170
|
+
workspace_root=ws_root,
|
|
171
|
+
config=config,
|
|
172
|
+
pinecone_api_key=pc_key,
|
|
173
|
+
)
|
|
174
|
+
except RuntimeError as exc:
|
|
175
|
+
print_error(str(exc))
|
|
176
|
+
console.print()
|
|
177
|
+
raise SystemExit(1) from exc
|
|
178
|
+
|
|
179
|
+
if not yes and not click.confirm(
|
|
180
|
+
f" Delete all indexed chunks in namespace '{store.namespace}'?", default=False
|
|
181
|
+
):
|
|
182
|
+
print_info("Cancelled.")
|
|
183
|
+
console.print()
|
|
184
|
+
return
|
|
185
|
+
|
|
186
|
+
if store.clear_workspace():
|
|
187
|
+
print_success(f"Cleared index namespace [dim]{store.namespace}[/dim].")
|
|
188
|
+
else:
|
|
189
|
+
print_info("The index is empty.")
|
|
190
|
+
console.print()
|
|
191
|
+
|