-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
594 lines (472 loc) · 18.7 KB
/
Copy pathapp.py
File metadata and controls
594 lines (472 loc) · 18.7 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
"""
FastAPI entry point for NIRA.
Run with:
uvicorn app:app --reload
Endpoints:
GET /health -> {"status": "ok"}
POST /chat -> {"reply": "..."} (non-streaming)
POST /chat/stream -> SSE stream of state + reply (for the UI)
/ -> the built React UI, if ui/dist exists
"""
from __future__ import annotations
import asyncio
import json
import os
import threading
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from fastapi.staticfiles import StaticFiles
from loguru import logger
from pydantic import BaseModel
from speech.tts import speak_onnx
from core import runtime
from core.assistant import Assistant
from core import router as tool_router
from core.oauth import router as oauth_router
BASE_DIR = Path(__file__).resolve().parent
app = FastAPI(title="NIRA", version="0.3.1")
app.include_router(oauth_router)
# CORS: allow the deployed Vercel frontend (and local dev) to call this API.
# Locked to known origins so we don't open the API to any site.
_ALLOWED_ORIGINS = [
"https://nira-ai-assistant.vercel.app",
"http://localhost:5173",
"http://127.0.0.1:5173",
"http://localhost:8000",
"http://127.0.0.1:8000",
# Custom production frontend domain (if set via env).
(os.getenv("WEB_ORIGIN") or "").rstrip("/"),
]
app.add_middleware(
CORSMiddleware,
allow_origins=_ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def _user_context(request, call_next):
"""Set the per-request local-user identity for OAuth connections.
The SPA sends `X-User-Id` (Firebase anonymous uid, or a `web-<uuid>`
fallback). Each local NIRA profile then gets its OWN Spotify/Google/etc.
tokens instead of all sharing one 'default' account.
"""
from core.oauth_store import CURRENT_USER
uid = (request.headers.get("X-User-Id") or "").strip() or "default"
token = CURRENT_USER.set(uid)
try:
return await call_next(request)
finally:
CURRENT_USER.reset(token)
@app.on_event("startup")
def _init_providers() -> None:
"""Build the LLM provider list from env keys and seed the active model."""
from ai.providers import build_providers
from ai.provider import MultiProviderClient
providers = build_providers()
runtime.set_providers(providers)
# Seed the active model only if it isn't already a valid scoped id
# ("provider|model"). Empty/free-style ids get replaced by the first
# provider's first model so multi-provider fallback always has a start.
current = runtime.get_model()
if not current or "|" not in current:
client = MultiProviderClient(providers, current, 0.7)
if client.model:
runtime.set_model(client.model)
class ChatRequest(BaseModel):
message: str
session_id: str = "default"
# Conversation history is owned by the CLIENT (Firestore, per anonymous
# Firebase UID) — the server no longer keeps a shared SQLite store. The
# client sends the prior turns so the model has context, keeping memory
# private to each device/user and resetting cleanly when Firestore is empty.
history: list[dict] = []
name: str = ""
class ChatResponse(BaseModel):
reply: str
class ModelSelectRequest(BaseModel):
model: str
class NameRequest(BaseModel):
name: str
session_id: str = "default"
class CustomProvider(BaseModel):
name: str
base_url: str
api_key: str = ""
models: list[str] = []
class CustomName(BaseModel):
name: str
class ToolKey(BaseModel):
"""An external-tool API key (Google, GitHub, Spotify, ...)."""
name: str
api_key: str = ""
extra: dict = {}
class FeatureState(BaseModel):
"""Enable/disable a Quick-Action feature (persisted locally)."""
name: str
enabled: bool = True
class ToolRunRequest(BaseModel):
"""Run a registered tool directly (no LLM involved)."""
name: str
arguments: dict = {}
def tool_keys_path() -> Path:
return BASE_DIR / "config" / "tool_keys.json"
def _load_tool_keys() -> dict:
p = tool_keys_path()
if not p.exists():
return {}
try:
return json.loads(p.read_text(encoding="utf-8")) or {}
except (ValueError, OSError):
return {}
def _save_tool_keys(d: dict) -> None:
p = tool_keys_path()
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(d, indent=2), encoding="utf-8")
@app.get("/")
def root() -> dict:
return {"status": "ok", "service": "nira-ai-backend"}
@app.get("/health")
def health() -> dict:
return {"status": "ok"}
@app.get("/models")
def models(refresh: bool = False) -> dict:
"""List free, tool-capable models (scoped id/name/context_length) for the UI."""
free = runtime.free_models(refresh=refresh)
# De-duplicate by id — provider/model ids are already globally scoped as
# "provider|model", so id alone is the correct uniqueness key. Jan's
# /v1/models can otherwise return the same id with a differing provider
# field, which a (provider, id) key would fail to collapse.
seen = set()
deduped = []
for m in free:
mid = m.get("id")
if not mid or mid in seen:
continue
seen.add(mid)
deduped.append(m)
# Group by provider for a cleaner dropdown.
providers = sorted({m.get("provider") for m in deduped if m.get("provider")})
return {"current": runtime.get_model(), "models": deduped, "providers": providers}
@app.post("/models/select")
def select_model(req: ModelSelectRequest) -> dict:
"""Switch the active model at runtime. Must be a known free model id."""
free_ids = {m["id"] for m in runtime.free_models(refresh=False)}
if req.model not in free_ids:
raise HTTPException(
status_code=400,
detail=f"Model '{req.model}' is not a known free model.",
)
runtime.set_model(req.model)
return {"current": runtime.get_model()}
@app.get("/providers")
def providers() -> dict:
"""Which LLM providers are configured (key present) + which are active."""
from ai.providers import build_providers
built = build_providers()
configured = {p.name for p in built}
return {
"configured": sorted(configured),
"active": [p.name for p in runtime.providers()],
}
@app.get("/providers/custom")
def list_custom() -> dict:
from ai.providers import _load_custom
return {"custom": _load_custom()}
@app.post("/providers/custom")
def add_custom(req: "CustomProvider") -> dict:
"""Add (or replace) a custom/local OpenAI-compatible provider.
Body: {name, base_url, api_key?, models?[]}. Persisted to
config/custom_providers.json and loaded into the live runtime.
"""
from ai.providers import _load_custom, save_custom
name = (req.name or "").strip()
base = (req.base_url or "").strip()
if not name or not base:
raise HTTPException(status_code=400, detail="name and base_url are required")
items = _load_custom()
items = [c for c in items if c.get("name") != name]
items.append(
{
"name": name,
"base_url": base,
"api_key": (req.api_key or "").strip(),
"models": [m.strip() for m in (req.models or []) if m.strip()],
}
)
save_custom(items)
runtime.rebuild_providers()
return {"ok": True, "name": name, "active": [p.name for p in runtime.providers()]}
@app.delete("/providers/custom")
def delete_custom(req: "CustomName") -> dict:
"""Remove a custom provider by name."""
from ai.providers import _load_custom, save_custom
name = req.name or ""
items = [c for c in _load_custom() if c.get("name") != name]
save_custom(items)
runtime.rebuild_providers()
return {"ok": True, "removed": name}
@app.get("/tools/keys")
def list_tool_keys() -> dict:
"""List configured external-tool keys (values masked for safety)."""
d = _load_tool_keys()
out = {}
for k, v in d.items():
if isinstance(v, dict):
has = bool((v.get("api_key") or "").strip())
out[k] = {
"configured": has,
"extra": v.get("extra", {}),
}
else:
out[k] = {"configured": bool(str(v).strip()), "extra": {}}
return {"keys": out}
@app.post("/tools/keys")
def set_tool_key(req: "ToolKey") -> dict:
"""Save (or replace) an external-tool API key. Persisted to
config/tool_keys.json (gitignored)."""
name = req.name.strip()
if not name:
raise HTTPException(status_code=400, detail="name is required")
d = _load_tool_keys()
# Preserve extra fields for keys that need more than a single token.
prev = d.get(name, {}) if isinstance(d.get(name), dict) else {}
d[name] = {
"api_key": (req.api_key or "").strip(),
"extra": {**prev.get("extra", {}), **(req.extra or {})},
}
_save_tool_keys(d)
return {"ok": True, "name": name, "configured": bool(d[name]["api_key"])}
@app.delete("/tools/keys")
def delete_tool_key(req: "CustomName") -> dict:
"""Remove an external-tool API key by name."""
name = req.name.strip()
d = _load_tool_keys()
if name in d:
del d[name]
_save_tool_keys(d)
return {"ok": True, "removed": name}
def features_path() -> Path:
return BASE_DIR / "config" / "features.json"
def _load_features() -> dict:
p = features_path()
if not p.exists():
return {}
try:
return json.loads(p.read_text(encoding="utf-8")) or {}
except (ValueError, OSError):
return {}
def _save_features(d: dict) -> None:
p = features_path()
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(d, indent=2), encoding="utf-8")
@app.get("/features")
def get_features() -> dict:
"""Return the set of enabled Quick-Action features."""
return _load_features()
@app.post("/features")
def set_feature(req: "FeatureState") -> dict:
"""Enable/disable a Quick-Action feature (persisted locally)."""
name = (req.name or "").strip()
if not name:
raise HTTPException(status_code=400, detail="name is required")
d = _load_features()
d[name] = bool(req.enabled)
_save_features(d)
return {"ok": True, "name": name, "enabled": d[name]}
# NOTE: chat sessions + message history are now owned by the CLIENT and
# stored in Firebase Firestore (per anonymous UID). The server no longer
# keeps a shared SQLite sessions table — these endpoints were removed so
# memory is private per user and resets when Firestore is empty.
@app.post("/tools/run")
def run_tool(req: "ToolRunRequest") -> dict:
"""Execute a registered tool DIRECTLY, without an LLM.
This is the model-independent execution path: slash commands and the
command menu call this so tasks still run even when the chosen model is
down, rate-limited, or too weak to call tools itself.
"""
name = (req.name or "").strip()
if not name:
raise HTTPException(status_code=400, detail="tool name is required")
if name not in tool_router.REGISTRY:
raise HTTPException(status_code=404, detail=f"unknown tool: {name}")
result = tool_router.execute(name, req.arguments or {})
return {"ok": True, "tool": name, "result": result}
@app.get("/status")
def status() -> dict:
"""Provider/model capability snapshot for diagnostics (no network)."""
providers = runtime.providers()
return {
"model": runtime.get_model(),
"providers": [p.name for p in providers],
"free_model_count": len(runtime.free_models(refresh=False)),
}
@app.get("/desktop")
def desktop() -> dict:
"""Live desktop snapshot: installed apps, open windows, running apps, and
browser tabs.
Uses the Windows system-enumeration tools (pywin32/psutil). Returns
structured lists so the UI can render them and let NIRA act on them —
a reliable "desktop awareness" view that doesn't depend on screen capture.
"""
import platform as _platform
from tools.windows import list_installed_apps, list_windows, list_open_apps, list_chrome_tabs
plat = _platform.system().lower()
if plat == "darwin":
device = "macos"
elif plat == "linux":
device = "linux"
elif plat == "windows":
device = "windows"
else:
device = plat or "unknown"
return {
"ok": True,
"platform": device,
"installed": list_installed_apps(),
"windows": list_windows(),
"apps": list_open_apps(),
"tabs": list_chrome_tabs(),
}
class DeviceAppsRequest(BaseModel):
device: str = "unknown" # android | ios | windows | macos | linux | web
apps: list = [] # list of app names (or {name, ...} dicts)
source: str = "client" # how the list was obtained
@app.post("/device/apps")
def device_apps(req: DeviceAppsRequest) -> dict:
"""Receive an app list reported by any client device.
The Windows desktop tools can only enumerate apps locally. To make
"list apps / list installed apps" work on Android, iOS, and any
non-Windows PC, the client reports what it can see (device type + app
names) and the tools read from this shared store. No local enumeration
required on the server.
"""
from core.device_apps import report_apps
report_apps(req.device, req.apps, req.source)
return {"ok": True, "device": req.device, "count": len(req.apps or [])}
@app.get("/device/apps")
def device_apps_get() -> dict:
"""Return the latest device-reported app list (diagnostics)."""
from core.device_apps import get_apps
return {"ok": True, **get_apps()}
class DesktopActionRequest(BaseModel):
action: str # "focus" | "close"
kind: str # "window" | "tab"
title: str = ""
exe: str = ""
query: str = ""
@app.post("/desktop/action")
def desktop_action(req: DesktopActionRequest) -> dict:
"""Focus, close, or open a window / tab / installed app."""
from tools.windows import (
focus_window,
close_window,
focus_browser_tab,
close_browser_tab,
open_installed_app,
)
a = (req.action or "").lower()
k = (req.kind or "").lower()
if k == "installed":
if a == "open":
msg = open_installed_app(req.title)
else:
raise HTTPException(status_code=400, detail="unknown action")
elif k == "window":
if a == "focus":
msg = focus_window(req.title, req.exe)
elif a == "close":
msg = close_window(req.title, req.exe)
else:
raise HTTPException(status_code=400, detail="unknown action")
elif k == "tab":
q = req.query or req.title
if a == "focus":
msg = focus_browser_tab(q)
elif a == "close":
msg = close_browser_tab(q)
else:
raise HTTPException(status_code=400, detail="unknown action")
else:
raise HTTPException(status_code=400, detail="unknown kind")
return {"ok": True, "message": msg}
@app.post("/prefs/name")
def set_name(req: NameRequest) -> dict:
"""Deprecated: name now lives in Firebase Firestore (anonymous UID).
Kept as a no-op so old clients don't error; the UI persists the name to
Firestore instead of the server.
"""
name = (req.name or "").strip()
if not name:
raise HTTPException(status_code=400, detail="name is required")
return {"ok": True, "name": name, "note": "persisted client-side (Firestore)"}
@app.post("/speak")
def speak(req: ChatRequest) -> "Response | dict":
"""Server-side TTS: return WAV audio for the given text.
Uses Kokoro (natural) with a Piper fallback. Returns a WAV audio response
when the model weights are present; otherwise a clear error so the client
can fall back to browser speech synthesis instead of failing silently.
"""
from fastapi.responses import Response
if not req.message or not req.message.strip():
raise HTTPException(status_code=400, detail="message is required")
# Server-side TTS (large Kokoro/Piper model) was REMOVED to keep the
# free-tier instance under its 512MB RAM ceiling (a 325MB model + Python
# OOM-killed the process). NIRA's voice now runs CLIENT-SIDE via
# ResponsiveVoice ("UK English Male"). This endpoint is kept for
# compatibility but returns 404 so the client uses its own voice.
return {
"error": "Server TTS is disabled. NIRA speaks from the browser "
"(ResponsiveVoice UK English Male).",
"client_voice": True,
}
@app.post("/chat", response_model=ChatResponse)
def chat(req: ChatRequest) -> ChatResponse:
if not req.message or not req.message.strip():
raise HTTPException(status_code=400, detail="message is required")
assistant = Assistant(
session_id=req.session_id, history=req.history, name=req.name
)
reply = assistant.ask(req.message)
return ChatResponse(reply=reply)
@app.post("/chat/stream")
async def chat_stream(req: ChatRequest):
"""Server-Sent Events stream of assistant state + final reply.
Emits events: meta, state(thinking|executing|speaking|idle|error),
tool_result, message, error. The React UI uses these to drive the
AI-core ring so it reflects what the assistant is actually doing.
"""
if not req.message or not req.message.strip():
raise HTTPException(status_code=400, detail="message is required")
assistant = Assistant(
session_id=req.session_id, history=req.history, name=req.name
)
loop = asyncio.get_running_loop()
event_q: "asyncio.Queue" = asyncio.Queue()
def worker() -> None:
try:
for ev in assistant.stream(req.message):
asyncio.run_coroutine_threadsafe(event_q.put(ev), loop)
except Exception: # noqa: BLE001 - never kill the stream silently
logger.exception("stream worker failed")
finally:
asyncio.run_coroutine_threadsafe(event_q.put(None), loop)
# Run the (blocking, sync) assistant in a thread so the event loop stays free.
threading.Thread(target=worker, daemon=True).start()
async def event_source():
while True:
ev = await event_q.get()
if ev is None:
break
yield f"data: {json.dumps(ev)}\n\n"
return StreamingResponse(event_source(), media_type="text/event-stream")
# Serve extracted app icons statically (generated at runtime into ui/public/icons).
_icons = BASE_DIR / "ui" / "public" / "icons"
if _icons.is_dir():
app.mount("/icons", StaticFiles(directory=str(_icons)), name="icons")
# Serve the built React UI (run `npm run build` in ui/) if it is present.
_dist = BASE_DIR / "ui" / "dist"
if _dist.is_dir():
app.mount("/", StaticFiles(directory=str(_dist), html=True), name="ui")