Personal AI, On Personal Devices
Python
feat: improve Slack messaging UX — progress updates, queue, clean formatting
Jon Saad-Falcon2ef6615184bd
diff --git a/src/openjarvis/agents/deep_research.py b/src/openjarvis/agents/deep_research.pyindex e806698d..5f37bce1 100644--- a/src/openjarvis/agents/deep_research.py+++ b/src/openjarvis/agents/deep_research.py@@ -141,7 +141,9 @@ abbreviations, related terms.- Research answers: cite as [source] title -- author. End with Sources.- Casual answers: no citations needed.- Concise unless detail is asked for.-- Use markdown (headers, tables, lists) for readability.+- Use plain text formatting that reads well in messaging apps. \+Avoid LaTeX, raw HTML, or complex markdown. Use simple lists, \+dashes, and line breaks instead of headers and tables when possible.- If nothing found, say so honestly and suggest alternatives."""diff --git a/src/openjarvis/server/agent_manager_routes.py b/src/openjarvis/server/agent_manager_routes.pyindex 5d542870..9338f1aa 100644--- a/src/openjarvis/server/agent_manager_routes.py+++ b/src/openjarvis/server/agent_manager_routes.py@@ -1421,27 +1421,97 @@ def create_agent_manager_router(tools=dr_tools,)+ import queue as _q+ import re as _re+ import threading as _thr++ _msg_queue: _q.Queue = _q.Queue()+ _processing = _thr.Event()++ def _strip_markdown(text: str) -> str:+ """Strip markdown for clean Slack messages."""+ # Remove headers+ text = _re.sub(r"^#{1,6}\s+", "", text, flags=_re.MULTILINE)+ # Remove bold/italic markers+ text = _re.sub(r"\*\*(.+?)\*\*", r"\1", text)+ text = _re.sub(r"\*(.+?)\*", r"\1", text)+ # Remove code blocks+ text = _re.sub(r"```[\s\S]*?```", "", text)+ text = _re.sub(r"`(.+?)`", r"\1", text)+ # Clean up extra whitespace+ text = _re.sub(r"\n{3,}", "\n\n", text)+ return text.strip()++ def _process_queue(say_fn):+ """Process queued messages sequentially."""+ while not _msg_queue.empty():+ text = _msg_queue.get()+ _processing.set()+ if dr_agent:+ try:+ result = dr_agent.run(text)+ reply = (+ result.content+ or "No results found."+ )+ except Exception as _exc:+ reply = f"Error: {_exc}"+ else:+ reply = (+ "Agent not available."+ )+ say_fn(_strip_markdown(reply))+ _processing.clear()+@bolt_app.event("message")def _handle_slack_dm(event, say):text = event.get("text", "")if not text:return- say("Message received! Researching now...")- if dr_agent:- try:- result = dr_agent.run(text)- reply = (- result.content- or "No results found."- )- except Exception as _exc:- reply = f"Error: {_exc}"- else:- reply = (- "Agent not available. "- "Please check server config."++ if _processing.is_set():+ # Already working on something+ _msg_queue.put(text)+ qsize = _msg_queue.qsize()+ say(+ "Message received! "+ f"Message {qsize + 1} in queue"+ f" of {qsize + 1}, "+ "will respond ASAP")- say(reply)+ return++ say(+ "Message received! "+ "Working on it now..."+ )+ _msg_queue.put(text)++ # Start progress updater+ _stop_progress = _thr.Event()++ def _progress_updates():+ mins = 0+ while not _stop_progress.is_set():+ _stop_progress.wait(60)+ if _stop_progress.is_set():+ break+ mins += 1+ if _processing.is_set():+ say(+ "Still working! "+ "Will reply ASAP"+ )++ pt = _thr.Thread(+ target=_progress_updates,+ daemon=True,+ )+ pt.start()++ # Process in current thread+ _process_queue(say)+ _stop_progress.set()sm_handler = SocketModeHandler(bolt_app, app_token,diff --git a/src/openjarvis/server/webhook_routes.py b/src/openjarvis/server/webhook_routes.pyindex e8f50479..0e4f1951 100644--- a/src/openjarvis/server/webhook_routes.py+++ b/src/openjarvis/server/webhook_routes.py@@ -45,6 +45,33 @@ def _validate_twilio_signature(return True+def _format_for_sms(text: str) -> str:+ """Strip markdown/LaTeX formatting for clean iMessage/SMS display."""+ import re++ # Remove markdown headers (## Header -> Header)+ text = re.sub(r"^#{1,6}\s+", "", text, flags=re.MULTILINE)+ # Remove bold/italic markers+ text = re.sub(r"\*{1,3}([^*]+)\*{1,3}", r"\1", text)+ text = re.sub(r"_{1,3}([^_]+)_{1,3}", r"\1", text)+ # Remove inline code backticks+ text = re.sub(r"`([^`]+)`", r"\1", text)+ # Remove code blocks+ text = re.sub(r"```[\s\S]*?```", "", text)+ # Remove LaTeX+ text = re.sub(r"\$\$[\s\S]*?\$\$", "", text)+ text = re.sub(r"\$[^$]+\$", "", text)+ # Remove markdown links [text](url) -> text+ text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text)+ # Remove image syntax + text = re.sub(r"!\[[^\]]*\]\([^)]+\)", "", text)+ # Convert markdown bullet lists to plain bullets+ text = re.sub(r"^[\s]*[-*]\s+", "- ", text, flags=re.MULTILINE)+ # Collapse excessive blank lines+ text = re.sub(r"\n{3,}", "\n\n", text)+ return text.strip()++def create_webhook_router(bridge: Any,twilio_auth_token: str = "",@@ -231,34 +258,58 @@ def create_webhook_router(logger.warning("No channel bridge — cannot process SendBlue msg")return Response("OK", status_code=200)+ # Message queue tracking (per-sender)+ _sendblue_queues = getattr(+ request.app.state, "_sendblue_queues", None+ )+ if _sendblue_queues is None:+ import threading as _th++ _sendblue_queues = {}+ _sendblue_queues["_lock"] = _th.Lock()+ request.app.state._sendblue_queues = _sendblue_queues+def _handle_and_reply() -> None:+ import threadingimport time as _time- # Immediate acknowledgment- if reply_channel:- reply_channel.send(- from_number,- "Message received! Researching your data now...",- )+ lock = _sendblue_queues["_lock"]- start = _time.monotonic()+ # Track queue depth for this sender+ with lock:+ q = _sendblue_queues.setdefault(+ from_number, {"pending": 0}+ )+ q["pending"] += 1+ position = q["pending"]- # Start a "still working" reminder in a separate thread- import threading+ # Immediate acknowledgment+ if reply_channel:+ if position > 1:+ reply_channel.send(+ from_number,+ f"Message received! Message {position} in"+ f" queue, will respond ASAP",+ )+ else:+ reply_channel.send(+ from_number,+ "Message received! Working on it now...",+ )+ # Periodic "still working" reminders every 60sdone_event = threading.Event()- def _send_delay_notice() -> None:- if not done_event.wait(45):- # 45s elapsed — send a heads-up+ def _send_reminders() -> None:+ while not done_event.wait(60):if reply_channel:reply_channel.send(from_number,- "Still working — complex query, hang tight...",+ "Still working! Will reply ASAP",)reminder = threading.Thread(- target=_send_delay_notice, daemon=True+ target=_send_reminders, daemon=True)reminder.start()@@ -268,10 +319,14 @@ def create_webhook_router()finally:done_event.set()+ with lock:+ q["pending"] = max(0, q["pending"] - 1)- # Send the agent's response back via SendBlue+ # Format response for clean text message displayif response and reply_channel:- reply_channel.send(from_number, response)+ reply_channel.send(+ from_number, _format_for_sms(response)+ )task = asyncio.create_task(asyncio.to_thread(_handle_and_reply))task.add_done_callback(_log_task_exception)