refactor(stt): unifie le jeton Ghostfolio — source client unique, appel côté serveur (#41)

Avant : jeton Ghostfolio en double (config client pour l'intent vocal + secret k8s pour
le contexte serveur). Désormais une SEULE source — la config client — transmise au
serveur, qui fait tous les appels Ghostfolio (un seul code, lecture seule).

Serveur :
- sources.py : cœur _ghostfolio_fetch(token) partagé ; ghostfolio_block (contexte) et
  ghostfolio_phrase (intent) acceptent un jeton par requête (priorité client > env).
- app.py : /v1/ask accepte `secrets` (jetons client) passés aux sources ; nouvel
  endpoint POST /v1/portfolio {token} → valeur du portefeuille.
- bump 0.4.0 → 0.5.0.

Client :
- api.py : ServerClient.secrets transmis dans /v1/ask ; méthode portfolio() → /v1/portfolio.
- cli.py : _make_client renseigne secrets["ghostfolio_token"] depuis [ghostfolio].
- ui/app.py : l'intent « combien ghostfolio » appelle client.portfolio (serveur).
- supprime portal/ghostfolio.py (client) — l'appel API vit côté serveur.
- config : [ghostfolio] = juste access_token (transmis au serveur).
- bump 0.12.0 → 0.13.0.

Le secret k8s STT_GHOSTFOLIO_TOKEN devient un simple fallback. Validé bout-en-bout
contre une instance serveur locale + Ghostfolio réel : « …vaut environ 16 520 EUR. »

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ALI YESILKAYA 2026-06-21 03:28:52 +02:00 committed by GitHub
parent ae4d5bd20e
commit e49b4fc3ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 121 additions and 113 deletions

View file

@ -1,3 +1,3 @@
"""STT-server — orchestrateur AI in-cluster pour les clients STT."""
__version__ = "0.4.0"
__version__ = "0.5.0"

View file

@ -23,7 +23,7 @@ from stt_server.contexts import CONTEXTS, assemble, get_context
from stt_server.knowledge import Knowledge
from stt_server.longterm import LongTermMemory
from stt_server.memory import SessionStore
from stt_server.sources import fetch_blocks
from stt_server.sources import fetch_blocks, ghostfolio_phrase
log = logging.getLogger("stt_server")
# uvicorn ne configure que ses propres loggers : on attache notre handler en INFO
@ -59,6 +59,11 @@ class AskRequest(BaseModel):
model: str | None = None # alias LiteLLM ; défaut serveur si absent
session_id: str | None = None # mémoire court-terme : fil de conversation
context: str | None = None # contexte présélectionné (funk/ghostfolio/…)
secrets: dict | None = None # jetons client (ex. {"ghostfolio_token": …})
class PortfolioRequest(BaseModel):
token: str | None = None # jeton Ghostfolio fourni par le client (sinon env)
class AskReply(BaseModel):
@ -98,6 +103,18 @@ async def v1_memory_health() -> dict:
return await longterm.health()
@app.post("/v1/portfolio")
async def v1_portfolio(req: PortfolioRequest) -> dict:
"""Valeur du portefeuille Ghostfolio (intent vocal client → serveur).
Le serveur fait l'appel Ghostfolio avec le jeton fourni par le client (sinon le
jeton serveur `STT_GHOSTFOLIO_TOKEN`). Un seul code Ghostfolio, lecture seule.
"""
async with httpx.AsyncClient() as c:
summary = await ghostfolio_phrase(c, req.token)
return {"summary": summary}
@app.post("/v1/reset")
async def v1_reset(req: AskRequest) -> dict:
if req.session_id:
@ -130,7 +147,7 @@ async def v1_ask(req: AskRequest, background: BackgroundTasks) -> AskReply:
live = tuple(s for s in ctx.sources if s != "docs")
if live:
async with httpx.AsyncClient() as c:
blocks = await fetch_blocks(c, live)
blocks = await fetch_blocks(c, live, secrets=req.secrets)
t_recall = time.perf_counter()
system, ctx_debug = assemble(ctx, blocks=blocks, docs=docs, memories=memories)
try:

View file

@ -34,11 +34,19 @@ async def _prom_query(client: httpx.AsyncClient, expr: str) -> list[tuple[dict,
return out
async def ghostfolio_block(client: httpx.AsyncClient) -> str:
def _money(v) -> str:
return f"{round(float(v)):,}".replace(",", " ")
async def _ghostfolio_fetch(client: httpx.AsyncClient, token: str | None):
"""Récupère le portefeuille → (value, currency, holdings) ou une str d'erreur.
`token` : jeton fourni par le client (prioritaire) ; à défaut, jeton serveur (env).
"""
base = settings.ghostfolio_url.rstrip("/")
token = settings.ghostfolio_token
token = token or settings.ghostfolio_token
if not token:
return "Données indisponibles : jeton Ghostfolio non configuré côté serveur."
return "Données indisponibles : aucun jeton Ghostfolio (ni client, ni serveur)."
try:
r = await client.post(
f"{base}/api/v1/auth/anonymous",
@ -58,27 +66,47 @@ async def ghostfolio_block(client: httpx.AsyncClient) -> str:
summary = data.get("summary", {}) or {}
val = (summary.get("currentValueInBaseCurrency")
or summary.get("currentValue") or summary.get("netWorth"))
# quelques positions principales (par valeur), si présentes
cur = summary.get("baseCurrency") or "EUR"
holdings = data.get("holdings", {}) or {}
tops = sorted(
(h for h in holdings.values() if isinstance(h, dict)),
key=lambda h: h.get("valueInBaseCurrency") or 0, reverse=True,
)[:5]
lines = []
if val is not None:
lines.append(f"Valeur actuelle du portefeuille : {round(float(val)):,} (devise de base).".replace(",", " "))
for h in tops:
name = h.get("name") or h.get("symbol") or "?"
hv = h.get("valueInBaseCurrency")
if hv:
lines.append(f"- {name} : {round(float(hv)):,}".replace(",", " "))
return "\n".join(lines) if lines else "Aucune donnée de portefeuille renvoyée."
return (val, cur, holdings)
except httpx.HTTPError:
return "Données indisponibles : Ghostfolio injoignable."
except (ValueError, KeyError):
return "Données indisponibles : réponse Ghostfolio inattendue."
async def ghostfolio_block(client: httpx.AsyncClient, token: str | None = None) -> str:
"""Bloc de contexte (valeur + positions principales)."""
res = await _ghostfolio_fetch(client, token)
if isinstance(res, str):
return res
val, cur, holdings = res
tops = sorted(
(h for h in holdings.values() if isinstance(h, dict)),
key=lambda h: h.get("valueInBaseCurrency") or 0, reverse=True,
)[:5]
lines = []
if val is not None:
lines.append(f"Valeur actuelle du portefeuille : {_money(val)} {cur}.")
for h in tops:
name = h.get("name") or h.get("symbol") or "?"
hv = h.get("valueInBaseCurrency")
if hv:
lines.append(f"- {name} : {_money(hv)}")
return "\n".join(lines) if lines else "Aucune donnée de portefeuille renvoyée."
async def ghostfolio_phrase(client: httpx.AsyncClient, token: str | None = None) -> str:
"""Phrase parlable (intent vocal « combien sur mon ghostfolio »)."""
res = await _ghostfolio_fetch(client, token)
if isinstance(res, str):
return res
val, cur, _ = res
if val is None:
return "Je n'ai pas trouvé la valeur du portefeuille dans la réponse de Ghostfolio."
return f"Ton portefeuille Ghostfolio vaut environ {_money(val)} {cur}."
async def alerts_block(client: httpx.AsyncClient) -> str:
base = settings.alertmanager_url.rstrip("/")
try:
@ -153,15 +181,30 @@ _FETCHERS = {
}
async def fetch_blocks(client: httpx.AsyncClient, sources: tuple[str, ...]) -> list[tuple[str, str]]:
"""Récupère les blocs live demandés (hors « docs », géré par le RAG). Best-effort."""
async def fetch_blocks(
client: httpx.AsyncClient,
sources: tuple[str, ...],
secrets: dict | None = None,
) -> list[tuple[str, str]]:
"""Récupère les blocs live demandés (hors « docs », géré par le RAG). Best-effort.
`secrets` : jetons fournis par le client (ex. {"ghostfolio_token": }) priorité
sur les jetons serveur (env). Permet d'unifier la source du jeton côté client.
"""
import asyncio
secrets = secrets or {}
wanted = [s for s in sources if s in _FETCHERS]
if not wanted:
return []
def _call(s):
if s == "ghostfolio":
return ghostfolio_block(client, secrets.get("ghostfolio_token"))
return _FETCHERS[s](client)
results = await asyncio.gather(
*(_FETCHERS[s](client) for s in wanted), return_exceptions=True
*(_call(s) for s in wanted), return_exceptions=True
)
out: list[tuple[str, str]] = []
for src, res in zip(wanted, results):