Skip to content

Commit d28199a

Browse files
committed
feat(bots): fix asyncio error and add thread configuration
- Fix asyncio.run() error by using run_in_executor() for sync agent.chat() - Add reply_in_thread config option (default: False for inline replies) - Add thread_threshold for auto-threading long responses (default: 500 chars) - Add INFO logging for message received/sent flow
1 parent 0d3d6bb commit d28199a

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/praisonai-agents/praisonaiagents/bots/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class BotConfig:
4040
max_message_length: int = 4096
4141
retry_attempts: int = 3
4242
timeout: int = 30
43+
reply_in_thread: bool = False # Default to inline, set True for thread replies
44+
thread_threshold: int = 500 # Auto-thread responses longer than this (0 = disabled)
4345
metadata: Dict[str, Any] = field(default_factory=dict)
4446

4547
def to_dict(self) -> Dict[str, Any]:
@@ -57,6 +59,8 @@ def to_dict(self) -> Dict[str, Any]:
5759
"max_message_length": self.max_message_length,
5860
"retry_attempts": self.retry_attempts,
5961
"timeout": self.timeout,
62+
"reply_in_thread": self.reply_in_thread,
63+
"thread_threshold": self.thread_threshold,
6064
"metadata": self.metadata,
6165
}
6266

src/praisonai/praisonai/bots/slack.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,21 @@ async def handle_message(event, say):
159159

160160
if should_respond and self._agent:
161161
try:
162-
response = self._agent.chat(text)
163-
await self._send_long_message(say, response, thread_ts=event.get("thread_ts") or event.get("ts"))
162+
logger.info(f"Message received: {text[:100]}...")
163+
# Run sync agent.chat() in executor to avoid asyncio.run() conflicts
164+
loop = asyncio.get_event_loop()
165+
response = await loop.run_in_executor(None, self._agent.chat, text)
166+
logger.info(f"Response sent: {response[:100]}...")
167+
168+
# Determine if we should reply in thread
169+
thread_ts = None
170+
if self.config.reply_in_thread:
171+
thread_ts = event.get("thread_ts") or event.get("ts")
172+
elif self.config.thread_threshold > 0 and len(response) > self.config.thread_threshold:
173+
# Auto-thread long responses
174+
thread_ts = event.get("thread_ts") or event.get("ts")
175+
176+
await self._send_long_message(say, response, thread_ts=thread_ts)
164177
except Exception as e:
165178
logger.error(f"Agent error: {e}")
166179
await say(f"Error: {str(e)}")
@@ -176,8 +189,21 @@ async def handle_mention(event, say):
176189

177190
if self._agent:
178191
try:
179-
response = self._agent.chat(text)
180-
await self._send_long_message(say, response, thread_ts=event.get("thread_ts") or event.get("ts"))
192+
logger.info(f"@mention received: {text[:100]}...")
193+
# Run sync agent.chat() in executor to avoid asyncio.run() conflicts
194+
loop = asyncio.get_event_loop()
195+
response = await loop.run_in_executor(None, self._agent.chat, text)
196+
logger.info(f"Response sent: {response[:100]}...")
197+
198+
# Determine if we should reply in thread
199+
thread_ts = None
200+
if self.config.reply_in_thread:
201+
thread_ts = event.get("thread_ts") or event.get("ts")
202+
elif self.config.thread_threshold > 0 and len(response) > self.config.thread_threshold:
203+
# Auto-thread long responses
204+
thread_ts = event.get("thread_ts") or event.get("ts")
205+
206+
await self._send_long_message(say, response, thread_ts=thread_ts)
181207
except Exception as e:
182208
logger.error(f"Agent error: {e}")
183209
await say(f"Error: {str(e)}")

0 commit comments

Comments
 (0)