import time import threading import requests import os from dataclasses import dataclass, field from typing import Optional, Dict, Any, List from loguru import logger try: from . import config except ImportError: try: import config except ImportError: config = None # Re-export para compat: api.py faz `from .jev_client import get_jev_client, JEV_ENABLED` _SYSTEMONE_PATH = "/decisions" def _uniq(keys: list) -> list: out = [] for k in keys: k = (k or "").strip() if k and len(k) > 10 and k not in out: out.append(k) return out def _collect_api_keys(api_key: Optional[str] = None) -> list: """Chaves JEV/TypeSafe — JEV_API_KEY, JEV_API_KEY_2, TYPESAFE_API_KEY.""" keys: list = [] if api_key and str(api_key).strip(): keys.append(str(api_key).strip()) candidates = [ (getattr(config, "JEV_API_KEY", "") if config else ""), (getattr(config, "JEV_API_KEY_2", "") if config else ""), os.getenv("JEV_API_KEY", ""), os.getenv("JEV_API_KEY_2", ""), os.getenv("TYPESAFE_API_KEY", ""), ] return _uniq(keys + candidates) def _collect_named_keys(names: list) -> list: keys: list = [] for n in names: if config is not None: keys.append(getattr(config, n, "") or "") keys.append(os.getenv(n, "")) keys.append(os.getenv(n.lower(), "")) return _uniq(keys) _JEV_KEYS = _collect_api_keys() _TOKENRA_KEYS = _collect_named_keys(["TOKENRA_API_KEY"]) _OPENROUTER_KEYS = _collect_named_keys([ "OPENROUTER_API_KEY", "GITAKIRA_OPENROUTER_KEY", "GITAKIRA_OPENROUTER_API", "SANDEOBRAS_OPENROUTER_API", "SOFTEDGE_OPENROUTER_API", "JOSELENA_OPENROUTER_API", "FUGAKUSAYO_OPENROUTER_API", ]) _JEV_KEY = _JEV_KEYS[0] if _JEV_KEYS else "" JEV_ENABLED: bool = bool(_JEV_KEYS or _TOKENRA_KEYS or _OPENROUTER_KEYS) SYSTEMONE_PATH = _SYSTEMONE_PATH @dataclass class JEVResult: """Resultado tipado de uma decisão JEV (compat com .success/.data em api.py).""" success: bool data: Dict[str, Any] = field(default_factory=dict) error: Optional[str] = None elapsed_ms: float = 0.0 class JevClient: """Cliente da API TypeSafe System One (JEV). POST https://api.typesafe.ai/v1/systemone Body: {state, model, questions: {id: {type, instructions, criteria}}} Resp: {model, answers: {id: {...}}, usage} - noul -> {type: "noul", noul: 0..1} - choice -> {type: "choice", choice, probabilities, confidence} - score -> {type: "score", score, legend, probabilities, confidence} """ def __init__( self, api_key: Optional[str] = None, base_url: Optional[str] = None, model: Optional[str] = None, timeout: Optional[float] = None, ): self.api_keys = _collect_api_keys(api_key) self.api_key = self.api_keys[0] if self.api_keys else "" self._key_idx = 0 self.base_url = ( base_url or (getattr(config, "JEV_API_BASE_URL", "") if config else "") or "https://api.defapi.org/api/v1" ).rstrip("/") self.model = ( model or (getattr(config, "JEV_MODEL", "") if config else "") or "jev-latest" ) if timeout is None: timeout_ms = int(getattr(config, "JEV_TIMEOUT_MS", 5000) if config else 5000) timeout = max(1.0, timeout_ms / 1000.0) self.timeout = float(timeout) self.max_retries = int(getattr(config, "JEV_MAX_RETRIES", 2) if config else 2) self._lock = threading.Lock() self._stats = {"calls": 0, "ok": 0, "fail": 0, "avg_ms": 0.0} self._backends = self._build_backends(base_url, model, api_key) self._active_backend = 0 self.enabled = bool(self._backends) if self.enabled: bl = ", ".join(f"{b['base']}{b['path']}" for b in self._backends) logger.success( f"JEV System-One ativo: model={self.model} backends=[{bl}] " f"jev_keys={len(self.api_keys)} tokenra={len(_TOKENRA_KEYS)} openrouter={len(_OPENROUTER_KEYS)}" ) else: logger.warning("JEV desativado — nenhuma chave (JEV/TOKENRA/OPENROUTER)") def _build_backends(self, base_url, model, api_key) -> list: """Cadeia de backends JEV — tenta em ordem até um responder.""" jev_keys = self.api_keys primary_base = ( base_url or (getattr(config, "JEV_API_BASE_URL", "") if config else "") or "https://api.defapi.org/api/v1" ).rstrip("/") primary_model = ( model or (getattr(config, "JEV_MODEL", "") if config else "") or "jev-latest" ) primary_path = "/systemone" if "typesafe.ai" in primary_base else _SYSTEMONE_PATH # Defapi: jev-latest → 400; typesafe/jev-1.13 é o nome que funciona if "defapi" in primary_base and primary_model in ("jev-latest", "", "jev"): primary_model = "typesafe/jev-1.13" cands = [] _MODEL_ALTS = ["typesafe/jev-1.13", "jev-1.13.0", "jev-1.13", "jev-latest", "jev"] if jev_keys: cands.append({ "base": primary_base, "path": primary_path, "model": primary_model, "alt_models": [m for m in _MODEL_ALTS if m != primary_model], "keys": list(jev_keys), }) cands.append({ "base": "https://api.typesafe.ai/v1", "path": "/systemone", "model": "jev-latest", "alt_models": ["jev-1.13.0"], "keys": list(jev_keys), }) tr_keys = _uniq((_TOKENRA_KEYS or []) + jev_keys) if tr_keys: cands.append({ "base": "https://tokenra.io/v1", "path": "/decisions", "model": "jev-latest", "alt_models": ["jev-1.13.0"], "keys": tr_keys, }) or_keys = _uniq(_OPENROUTER_KEYS) if or_keys: cands.append({ "base": "https://openrouter.ai/api", "path": "/alpha/decisions", "model": "typesafe/jev-1.13", "alt_models": ["~typesafe/jev-latest"], "keys": or_keys, }) backends, seen = [], set() for c in cands: sig = (c["base"], c["path"], c["model"]) if sig not in seen and c["keys"]: seen.add(sig) backends.append(c) return backends def is_available(self) -> bool: return self.enabled def _headers(self, key: Optional[str] = None) -> Dict[str, str]: return { "Authorization": f"Bearer {key or self.api_key}", "Content-Type": "application/json", "Accept": "application/json", } def _backend_order(self) -> list: n = len(self._backends) if n == 0: return [] a = self._active_backend % n return [a] + [i for i in range(n) if i != a] def _decide_backend( self, backend: Dict[str, Any], state: str, questions: Dict[str, Any], deadline: float, t0: float, ) -> Optional[Dict[str, Any]]: url = f"{backend['base']}{backend['path']}" models = [backend["model"]] + [ m for m in (backend.get("alt_models") or []) if m != backend["model"] ] mi = 0 keys = backend["keys"] or [None] tried = set() key_i = 0 attempt = 0 while time.time() < deadline: attempt += 1 remaining = max(0.5, deadline - time.time()) payload = {"state": state, "model": models[mi], "questions": questions} try: resp = requests.post( url, headers=self._headers(keys[key_i]), json=payload, timeout=(2.0, remaining), ) except requests.exceptions.Timeout: logger.warning(f"JEV timeout {backend['base']} state={str(state)[:60]}") return None except Exception as e: logger.warning(f"JEV fail {backend['base']}: {e}") return None if resp.status_code in (429, 529) and attempt <= self.max_retries: wait = min(1.0, 0.25 * (2 ** (attempt - 1))) logger.warning(f"JEV {resp.status_code} @ {backend['base']} — retry {attempt}/{self.max_retries}") time.sleep(min(wait, max(0.0, deadline - time.time()))) continue elapsed = (time.time() - t0) * 1000 if resp.status_code in (401, 403): tried.add(key_i) nxt = next((i for i in range(len(keys)) if i not in tried), None) if nxt is not None and time.time() < deadline: key_i = nxt if keys[nxt]: self.api_key = keys[nxt] self._key_idx = 0 logger.warning( f"JEV {resp.status_code} @ {backend['base']} — próxima chave ({nxt + 1}/{len(keys)})" ) continue logger.warning( f"JEV {resp.status_code} @ {backend['base']} — backend sem chave válida, próximo backend" ) return None if resp.status_code == 404: logger.warning(f"JEV 404 @ {url} — endpoint errado, próximo backend") return None if resp.status_code == 400: body = (resp.text or "") if ("model" in body.lower() and ("not found" in body.lower() or "unknown" in body.lower() or "code\":1404" in body.replace(" ", ""))) and mi + 1 < len(models): logger.warning( f"JEV model not found @ {backend['base']} — {models[mi]} → {models[mi + 1]}" ) mi += 1 continue logger.warning(f"JEV 400 @ {backend['base']}: {body[:200]}") return None if resp.status_code == 402: logger.warning(f"JEV 402 @ {backend['base']} — sem créditos, próximo backend") return None if resp.status_code == 422: if mi + 1 < len(models): logger.warning( f"JEV 422 @ {backend['base']} — trocando modelo {models[mi]} → {models[mi + 1]}" ) mi += 1 continue logger.warning(f"JEV 422 @ {backend['base']}: {(resp.text or '')[:200]}") return None try: resp.raise_for_status() data = resp.json() except Exception as e: logger.warning(f"JEV parse fail @ {backend['base']}: {e}") return None if not isinstance(data, dict) or "answers" not in data: logger.warning(f"JEV resposta inesperada @ {backend['base']}: {str(data)[:200]}") return None if models[mi] != backend["model"]: logger.info(f"JEV modelo fixado @ {backend['base']}: {models[mi]}") backend["model"] = models[mi] logger.info( f"JEV decide ok {elapsed:.0f}ms @ {backend['base']} " f"answers={list((data.get('answers') or {}).keys())}" ) return data return None def decide( self, state: str, questions: Dict[str, Any], timeout: Optional[float] = None, ) -> Optional[Dict[str, Any]]: """Tenta a cadeia de backends JEV — retorna {model, answers, usage} ou None.""" if not self.enabled or not self._backends: return None req_timeout = float(timeout or self.timeout) t0 = time.time() deadline = t0 + req_timeout order = self._backend_order() for bi in order: data = self._decide_backend(self._backends[bi], state, questions, deadline, t0) if data is not None: if bi != self._active_backend: b = self._backends[bi] logger.info(f"JEV backend fixado: {b['base']}{b['path']} model={b['model']}") self._active_backend = bi with self._lock: self._stats["calls"] += 1 self._stats["ok"] += 1 n = self._stats["ok"] + self._stats["fail"] elapsed = (time.time() - t0) * 1000 self._stats["avg_ms"] = (self._stats["avg_ms"] * (n - 1) + elapsed) / n return data with self._lock: self._stats["calls"] += 1 self._stats["fail"] += 1 logger.error("JEV decide — todos os backends falharam") return None @staticmethod def _answer(data: Optional[Dict[str, Any]], key: str) -> Optional[Dict[str, Any]]: if not isinstance(data, dict): return None answers = data.get("answers") if not isinstance(answers, dict): return None ans = answers.get(key) return ans if isinstance(ans, dict) else None # ------------------------------------------------------------------ # Pré-classificação em lote (1 request para intent+emoção+moderação+roteamento) # ------------------------------------------------------------------ PRECLASSIFY_QUESTIONS: Dict[str, Any] = { "intent": { "type": "choice", "instructions": "Qual a intenção principal do utilizador nesta mensagem?", "criteria": { "question": "Pergunta que pede uma resposta/informação", "command": "Pedido para executar uma ação ou ferramenta", "greeting": "Saudação ou início de conversa", "farewell": "Despedida ou fim de conversa", "complaint": "Reclamação, frustração ou crítica", "compliment": "Elogio ou feedback positivo", "request_help": "Pedido de ajuda ou suporte", "share_info": "Partilha de informação sem pedido explícito", "joke": "Piada, brincadeira ou humor", "roleplay": "Pedido de roleplay ou simulação", "nsfw_request": "Pedido de conteúdo sexual/NSFW", "spam": "Spam, flood ou mensagem sem sentido", "provocation": "Provocação, ataques ou trolling", "other": "Outra intenção não listada", }, }, "emotion": { "type": "choice", "instructions": "Qual o estado emocional dominante do utilizador nesta mensagem?", "criteria": { "joy": "Alegria, entusiasmo, diversão", "sadness": "Tristeza, desânimo, perda", "anger": "Raiva, irritação, agressividade", "fear": "Medo, preocupação, ansiedade", "surprise": "Surpresa, choque, inesperado", "disgust": "Nojo, repulsa, aversão", "neutral": "Neutro, sem emoção marcada", "love": "Afeto, carinho, admiração", "anxiety": "Ansiedade, nervosismo, inquietação", "excitement": "Empolgação, antecipação, energia", }, }, "is_safe": { "type": "noul", "instructions": "A mensagem é segura para o bot responder sem restrições?", "criteria": { "true": "Conteúdo seguro, conversa normal", "false": "Ódio, assédio, violência, auto-lesão, sexual explícito, ilegal ou PII sensível", }, }, "route": { "type": "choice", "instructions": "Para qual caminho esta mensagem deve ser roteada?", "criteria": { "llm_general": "Conversa geral com LLM", "llm_coding": "Questão técnica ou programação", "llm_creative": "Criação de conteúdo, roleplay, história", "llm_emotional": "Apoio emocional, desabafo", "web_search": "Precisa de informação atual da web", "skill_execution": "Executar skill/ferramenta existente", "human_handoff": "Escalar para humano", "ignore": "Ignorar ou resposta mínima", }, }, "depth": { "type": "score", "instructions": ( "Classifique a profundidade de deliberação necessária, considerando " "TAMBÉM o histórico/contexto fornecido (não só a mensagem isolada). " "1 = System 1 puro (reflexo imediato), " "2 = System 1 predominante, " "3 = Equilíbrio System 1/2, " "4 = System 2 predominante, " "5 = System 2 puro (deliberação profunda). " "Follow-ups em conversa contínua sobre um tópico já em debate " "(ex: política, opinião, crise) exigem NO MÍNIMO 3-4 — a resposta " "DEVE continuar o raciocínio anterior, não tratar a pergunta isolada. " "Considere: complexidade, risco, ambiguidade, continuidade de tópico, " "necessidade de encadear com o que já foi dito." ), "criteria": [ "System 1 puro — reflexo imediato, resposta óbvia, sem deliberação, mensagem isolada sem histórico", "System 1 predominante — resposta simples, pouca análise, intuição, conversa casual sem profundidade", "Equilíbrio System 1/2 — raciocínio normal, follow-up em conversa que pede continuidade de tópico, alguma análise", "System 2 predominante — análise cuidada, tópico em debate (política/opinião/ética), múltiplos fatores, precisa encadear com histórico", "System 2 puro — deliberação profunda, decisão complexa/sensível, múltiplas perspectivas, crise ou dilema moral", ], }, "proactive": { "type": "noul", "instructions": ( "Deve Akira agir proativamente (sugerir próximo passo, oferecer ajuda " "não pedida, fazer pergunta follow-up) ou apenas responder ao que foi pedido?" ), "criteria": { "true": "Ação proativa seria útil e bem-vinda pelo contexto", "false": "Apenas responder ao pedido explícito — proatividade seria intrusiva", }, }, "hostility": { "type": "noul", "instructions": ( "A mensagem do utilizador mostra hostilidade, agressividade ou " "provocação dirigida ao bot ou a terceiros?" ), "criteria": { "true": "Hostilidade/ataque/provocação clara — exigir resposta firme e limites", "false": "Tom normal, frustração legítima ou desacordo sem agressão", }, }, } def preclassify( self, text: str, context: str = "", timeout: Optional[float] = None, ) -> Dict[str, JEVResult]: """Intent + emoção + moderação + roteamento numa única chamada System One.""" empty = { k: JEVResult(success=False, error="disabled_or_failed") for k in ("intent", "emotion", "moderation", "routing", "depth", "proactive", "hostility") } if not self.enabled or not text: return empty state = text.strip() if context: state = f"{state}\n\n[contexto]\n{context.strip()[:400]}" # Limite de segurança (docs: 64k tokens/req) — corta state muito longo if len(state) > 24000: state = state[:24000] t0 = time.time() data = self.decide(state, dict(self.PRECLASSIFY_QUESTIONS), timeout=timeout) elapsed = (time.time() - t0) * 1000 if not data: return empty out = dict(empty) intent_ans = self._answer(data, "intent") if intent_ans and intent_ans.get("choice"): out["intent"] = JEVResult( success=True, data={ "intent": intent_ans["choice"], "confidence": float(intent_ans.get("confidence") or 0.0), "probabilities": intent_ans.get("probabilities") or {}, }, elapsed_ms=elapsed, ) emotion_ans = self._answer(data, "emotion") if emotion_ans and emotion_ans.get("choice"): out["emotion"] = JEVResult( success=True, data={ "primary_emotion": emotion_ans["choice"], "confidence": float(emotion_ans.get("confidence") or 0.0), "probabilities": emotion_ans.get("probabilities") or {}, }, elapsed_ms=elapsed, ) safe_ans = self._answer(data, "is_safe") if safe_ans and "noul" in safe_ans: try: unsafe_p = 1.0 - float(safe_ans["noul"]) # noul = P(safe) is_safe = unsafe_p < 0.5 out["moderation"] = JEVResult( success=True, data={ "is_safe": is_safe, "categories": ["none"] if is_safe else ["flagged"], "severity": round(unsafe_p, 3), "action": "allow" if is_safe else "escalate", "confidence": round(max(unsafe_p, 1.0 - unsafe_p), 3), }, elapsed_ms=elapsed, ) except (TypeError, ValueError): pass route_ans = self._answer(data, "route") if route_ans and route_ans.get("choice"): out["routing"] = JEVResult( success=True, data={ "route_to": route_ans["choice"], "confidence": float(route_ans.get("confidence") or 0.0), "probabilities": route_ans.get("probabilities") or {}, }, elapsed_ms=elapsed, ) depth_ans = self._answer(data, "depth") if depth_ans and depth_ans.get("score") is not None: raw_score = float(depth_ans["score"]) # TypeSafe score is 0-indexed, convert to 1-5 scale depth_score = min(5.0, max(1.0, raw_score + 1.0)) if raw_score < 1.0 else min(5.0, raw_score) # 3 = equilíbrio → ainda System 1 (rápido); System 2 só a partir de 3.5 system_type = "System 2" if depth_score >= 3.5 else "System 1" out["depth"] = JEVResult( success=True, data={ "depth_score": depth_score, "system_type": system_type, "confidence": float(depth_ans.get("confidence") or 0.0), }, elapsed_ms=elapsed, ) proactive_ans = self._answer(data, "proactive") if proactive_ans and proactive_ans.get("noul") is not None: try: p = float(proactive_ans["noul"]) out["proactive"] = JEVResult( success=True, data={"proactive": p >= 0.5, "p_true": p, "confidence": float(proactive_ans.get("confidence") or 0.0)}, elapsed_ms=elapsed, ) except (TypeError, ValueError): pass hostility_ans = self._answer(data, "hostility") if hostility_ans and hostility_ans.get("noul") is not None: try: p = float(hostility_ans["noul"]) out["hostility"] = JEVResult( success=True, data={"hostile": p >= 0.5, "p_true": p, "confidence": float(hostility_ans.get("confidence") or 0.0)}, elapsed_ms=elapsed, ) except (TypeError, ValueError): pass return out # ------------------------------------------------------------------ # Wrappers individuais (compat com api.py — cada um = 1 pergunta) # ------------------------------------------------------------------ def classify_intent(self, text: str, context: str = "") -> JEVResult: state = f"{text}\n\n[contexto]\n{context}" if context else text q = self.PRECLASSIFY_QUESTIONS["intent"] data = self.decide(state, {"intent": q}) ans = self._answer(data, "intent") if ans and ans.get("choice"): return JEVResult( success=True, data={ "intent": ans["choice"], "confidence": float(ans.get("confidence") or 0.0), }, ) return JEVResult(success=False, error="no_answer") def classify_emotional_state(self, text: str, context: str = "") -> JEVResult: state = f"{text}\n\n[contexto]\n{context}" if context else text q = self.PRECLASSIFY_QUESTIONS["emotion"] data = self.decide(state, {"emotion": q}) ans = self._answer(data, "emotion") if ans and ans.get("choice"): return JEVResult( success=True, data={ "primary_emotion": ans["choice"], "confidence": float(ans.get("confidence") or 0.0), }, ) return JEVResult(success=False, error="no_answer") def moderate_content(self, text: str, context: str = "") -> JEVResult: state = f"{text}\n\n[contexto]\n{context}" if context else text p = self.noul( state, self.PRECLASSIFY_QUESTIONS["is_safe"]["instructions"], self.PRECLASSIFY_QUESTIONS["is_safe"]["criteria"]["true"], self.PRECLASSIFY_QUESTIONS["is_safe"]["criteria"]["false"], key="is_safe", ) if p is None: return JEVResult(success=False, error="no_answer") unsafe_p = 1.0 - p is_safe = unsafe_p < 0.5 return JEVResult( success=True, data={ "is_safe": is_safe, "categories": ["none"] if is_safe else ["flagged"], "severity": round(unsafe_p, 3), "action": "allow" if is_safe else "escalate", "confidence": round(max(unsafe_p, 1.0 - unsafe_p), 3), }, ) def decide_routing(self, text: str, context: str = "") -> JEVResult: state = f"{text}\n\n[contexto]\n{context}" if context else text q = self.PRECLASSIFY_QUESTIONS["route"] data = self.decide(state, {"route": q}) ans = self._answer(data, "route") if ans and ans.get("choice"): return JEVResult( success=True, data={ "route_to": ans["choice"], "confidence": float(ans.get("confidence") or 0.0), }, ) return JEVResult(success=False, error="no_answer") def stats(self) -> Dict[str, Any]: with self._lock: return dict(self._stats) def probe(self, timeout: float = 8.0) -> list: """Sonda cada backend com payload mínimo — diagnóstico HTTP detalhado.""" results = [] state = "ping" questions = {"ping": {"type": "noul", "instructions": "Is this a ping?"}} for b in self._backends: url = f"{b['base']}{b['path']}" models = [b["model"]] + [ m for m in (b.get("alt_models") or []) if m != b["model"] ] t0 = time.time() entry: Dict[str, Any] = { "url": url, "models_tried": [], "n_keys": len(b["keys"]), "status": None, "body": "", "error": "", "ms": 0, } for m in models: payload = {"state": state, "model": m, "questions": questions} try: resp = requests.post( url, headers=self._headers(b["keys"][0]), json=payload, timeout=(3.0, timeout), ) except Exception as e: entry["error"] = str(e)[:300] entry["models_tried"].append({"model": m, "status": None}) break entry["models_tried"].append({"model": m, "status": resp.status_code}) entry["status"] = resp.status_code entry["body"] = (resp.text or "")[:400] if resp.status_code < 400: entry["model_ok"] = m break body_l = (resp.text or "").lower() if not (("model" in body_l) and ("not found" in body_l or "unknown" in body_l or "1404" in body_l)): break entry["ms"] = int((time.time() - t0) * 1000) results.append(entry) return results # ------------------------------------------------------------------ # Métodos de baixo nível (usados por jev_questions.py) # ------------------------------------------------------------------ def noul( self, state: str, instructions: str, true_criteria: Optional[str] = None, false_criteria: Optional[str] = None, key: str = "noul", timeout: Optional[float] = None, ) -> Optional[float]: """ Pergunta noul (true/false) → probabilidade 0..1 (P(true)). Retorna float 0..1 ou None se falhar/timeout. """ if not self.enabled: return None try: q: Dict[str, Any] = {"type": "noul", "instructions": instructions} if true_criteria or false_criteria: q["criteria"] = { "true": true_criteria or "Afirmação correta", "false": false_criteria or "Afirmação incorreta", } data = self.decide(state, {key: q}, timeout=timeout) ans = self._answer(data, key) if ans and ans.get("noul") is not None: return float(ans["noul"]) except Exception as e: logger.debug(f"JEV noul({key}) error: {e}") return None def score( self, state: str, instructions: str, criteria: list, key: str = "score", timeout: Optional[float] = None, ) -> Optional[Dict[str, Any]]: """ Pergunta score (1..N) → {score, legend, confidence} Retorna dict ou None """ if not self.enabled: return None try: data = self.decide( state, {key: {"type": "score", "instructions": instructions, "criteria": criteria}}, timeout=timeout ) ans = self._answer(data, key) if ans and ans.get("score") is not None: return { "score": ans["score"], "legend": ans.get("legend"), "confidence": float(ans.get("confidence") or 0.0) } except Exception as e: logger.debug(f"JEV score({key}) error: {e}") return None def choice( self, state: str, instructions: str, criteria: Dict[str, str], key: str = "choice", timeout: Optional[float] = None, ) -> Optional[Dict[str, Any]]: """ Pergunta choice (enum) → {choice, probabilities, confidence} Retorna dict ou None """ if not self.enabled: return None try: data = self.decide( state, {key: {"type": "choice", "instructions": instructions, "criteria": criteria}}, timeout=timeout ) ans = self._answer(data, key) if ans and ans.get("choice"): return { "choice": ans["choice"], "probabilities": ans.get("probabilities", {}), "confidence": float(ans.get("confidence") or 0.0) } except Exception as e: logger.debug(f"JEV choice({key}) error: {e}") return None _jev_singleton: Optional[JevClient] = None _jev_lock = threading.Lock() def get_jev_client(api_key: Optional[str] = None) -> JevClient: global _jev_singleton with _jev_lock: if _jev_singleton is None: _jev_singleton = JevClient(api_key=api_key) return _jev_singleton def reset_jev_client(): global _jev_singleton with _jev_lock: _jev_singleton = None