-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoobe.py
More file actions
163 lines (132 loc) · 6.15 KB
/
Copy pathoobe.py
File metadata and controls
163 lines (132 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
"""Minimal OOBE setup for ask-cli. Run once to configure LLM provider.
Dependencies: pip install cryptography
Usage: python oobe.py
"""
import os, json, sys
import requests
from pathlib import Path
from rich.console import Console
try:
from cryptography.fernet import Fernet
except ImportError:
print("[bold red]Error: cryptography package required.[/bold red]")
print("Install: pip install cryptography")
sys.exit(1)
import hashlib
import base64
console = Console()
# ── Encryption helpers ──────────────────────────────────────────────────
def _fernet_key():
"""Derive a Fernet key from hostname (stable per machine, no extra files)."""
return base64.urlsafe_b64encode(
hashlib.sha256(os.uname().nodename.encode()).digest()
)
def encrypt(v: str) -> str:
"""Encrypt a value (e.g. API key) using Fernet."""
return Fernet(_fernet_key()).encrypt(v.encode()).decode()
# ── Core logic ──────────────────────────────────────────────────────────
def try_router(host: str = "localhost", port: int = 9931) -> tuple[bool, dict | None]:
"""Probe for a running Llama.cpp router at the given host:port.
Returns (found, models_data) where models_data is the parsed JSON
if the endpoint responded with status 200.
"""
try:
r = requests.get(f"http://{host}:{port}/v1/models", timeout=2)
if r.status_code == 200:
return True, r.json()
except Exception:
pass
return False, None
def setup() -> None:
"""Interactive first-run configuration wizard."""
# Write to user directory to avoid Nix Store immutability conflicts
config_path = Path.home() / ".local" / "share" / "ask" / "config.json"
# Already configured?
if config_path.exists():
console.print("[dim]Config already exists at:[/dim]")
console.print(f"[cyan]{config_path}[/cyan]")
console.print("[dim]Run again to reconfigure (or delete the file).[/dim]")
return
console.print("\n[bold cyan]ask-cli first-run setup[/bold cyan]")
# ── 1. Auto-detect running router ──────────────────────────────────
console.print("\n[bold yellow]Checking for running Llama.cpp router...[/bold yellow]")
ok, models = try_router()
api_base = ""
api_key = ""
extra_provider_config = {}
if ok:
console.print(f"[bold green]✓ Found router at http://localhost:9931[/bold green]")
model_list = models.get("data", [])
console.print(f"[dim]Available models ({len(model_list)}):[/dim]")
for m in model_list:
status = "loaded"
if "status" in m and isinstance(m["status"], dict):
status = m["status"].get("value", "loaded")
model_id = m.get("id", "unknown-model")
icon = "✅" if status == "loaded" else "⏸"
console.print(f" {icon} {model_id} ({status})")
api_base = "http://localhost:9931/v1"
api_key = ""
else:
console.print("[bold yellow]⚠ No router found at http://localhost:9931[/bold yellow]")
ans = input("Would you like to configure auto-start for a local llama-server? (y/n): ").strip().lower()
if ans == 'y':
server_path = input("[bold]Path to llama-server binary[/bold] (e.g. llama-server): ").strip() or "llama-server"
models_dir = input("[bold]Path to models directory[/bold]: ").strip()
api_base = "http://localhost:9931/v1"
api_key = ""
extra_provider_config = {
"server_path": server_path,
"models_dir": models_dir
}
else:
console.print("Please provide your API connection details.\n")
api_base = input("[bold]API Base[/bold] "
"(default http://localhost:9931/v1): ").strip()
if not api_base:
api_base = "http://localhost:9931/v1"
api_key = input("[bold]API Key[/bold] "
"(press Enter if none): ").strip()
# ── 2. Save config ────────────────────────────────────────────────
config = {
"providers": {
"llama-cpp-router": {
"type": "router",
"api_base": api_base,
"api_key": encrypt(api_key) if api_key else "",
**extra_provider_config
}
},
"active_provider": "llama-cpp-router",
"timeout": 1000,
"max_turns": 100,
"max_result_chars": 10000,
"auto_approve_default": False,
"use_sandbox_default": False,
"search_rate_limit": 5,
"search_rate_delay": 5.0,
"search_max_concurrent": 1,
"search_retry_count": 3,
"search_retry_base_delay": 10.0,
"search_timeout": 30,
}
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(json.dumps(config, indent=2))
console.print(f"\n[bold green]✓ Saved to {config_path}[/bold green]")
# ── 3. Verify connection ──────────────────────────────────────────
if extra_provider_config:
console.print("[dim]Skipping connection test (server will be started automatically by ask-cli).[/dim]")
else:
headers = {
"Authorization": f"Bearer {api_key}"
} if api_key else {}
try:
r = requests.get(f"{api_base}/models", headers=headers, timeout=3)
r.raise_for_status()
console.print(f"[bold green]✓ Connected ({len(r.json()['data'])} models)[/bold green]")
except Exception as e:
console.print(f"[bold red]✗ Verification failed: {e}[/bold red]")
console.print("[dim]Edit config.json manually or re-run setup if needed.[/dim]")
if __name__ == "__main__":
setup()