From cd6eaff7cbf0e3fc8011683d42bc50607e357b2a Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Mon, 26 Jan 2026 00:52:35 -0300 Subject: [PATCH] comms: normalize atlasbot replies --- services/comms/atlasbot-deployment.yaml | 4 ++- services/comms/scripts/atlasbot/bot.py | 34 ++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/services/comms/atlasbot-deployment.yaml b/services/comms/atlasbot-deployment.yaml index 278a008..c2bc108 100644 --- a/services/comms/atlasbot-deployment.yaml +++ b/services/comms/atlasbot-deployment.yaml @@ -16,7 +16,7 @@ spec: labels: app: atlasbot annotations: - checksum/atlasbot-configmap: manual-atlasbot-4 + checksum/atlasbot-configmap: manual-atlasbot-5 vault.hashicorp.com/agent-inject: "true" vault.hashicorp.com/role: "comms" vault.hashicorp.com/agent-inject-secret-turn-secret: "kv/data/atlas/comms/turn-shared-secret" @@ -75,6 +75,8 @@ spec: value: http://victoria-metrics-single-server.monitoring.svc.cluster.local:8428 - name: BOT_USER value: atlasbot + - name: BOT_MENTIONS + value: atlasbot - name: OLLAMA_URL value: http://chat-ai-gateway.bstein-dev-home.svc.cluster.local/ - name: OLLAMA_MODEL diff --git a/services/comms/scripts/atlasbot/bot.py b/services/comms/scripts/atlasbot/bot.py index e8bd1a8..3da93ba 100644 --- a/services/comms/scripts/atlasbot/bot.py +++ b/services/comms/scripts/atlasbot/bot.py @@ -71,6 +71,8 @@ METRIC_HINT_WORDS = { "latency", } +CODE_FENCE_RE = re.compile(r"^```(?:json)?\\s*(.*?)\\s*```$", re.DOTALL) + def _tokens(text: str) -> list[str]: toks = [t.lower() for t in TOKEN_RE.findall(text or "")] return [t for t in toks if t not in STOPWORDS and len(t) >= 2] @@ -442,6 +444,35 @@ def vm_cluster_snapshot() -> str: parts.append(pr) return "\n".join(parts).strip() +def _strip_code_fence(text: str) -> str: + cleaned = (text or "").strip() + match = CODE_FENCE_RE.match(cleaned) + if match: + return match.group(1).strip() + return cleaned + +def _normalize_reply(value: Any) -> str: + if isinstance(value, dict): + for key in ("content", "response", "reply", "message"): + if key in value: + return _normalize_reply(value[key]) + for v in value.values(): + if isinstance(v, (str, dict, list)): + return _normalize_reply(v) + return json.dumps(value, ensure_ascii=False) + if isinstance(value, list): + parts = [_normalize_reply(item) for item in value] + return " ".join(p for p in parts if p) + if value is None: + return "" + text = _strip_code_fence(str(value)) + if text.startswith("{") and text.endswith("}"): + try: + return _normalize_reply(json.loads(text)) + except Exception: + return text + return text + # Conversation state. history = collections.defaultdict(list) # (room_id, sender|None) -> list[str] (short transcript) @@ -511,7 +542,8 @@ def ollama_reply(hist_key, prompt: str, *, context: str) -> str: r = request.Request(OLLAMA_URL, data=json.dumps(payload).encode(), headers=headers) with request.urlopen(r, timeout=20) as resp: data = json.loads(resp.read().decode()) - reply = data.get("message") or data.get("response") or data.get("reply") or "I'm here to help." + raw_reply = data.get("message") or data.get("response") or data.get("reply") or data + reply = _normalize_reply(raw_reply) or "I'm here to help." history[hist_key].append(f"Atlas: {reply}") return reply except Exception: