fix: make AI parser async to prevent Discord heartbeat blocking

This commit is contained in:
2026-02-16 12:24:13 -06:00
parent 9cc2f19ce8
commit a53187d6a9
3 changed files with 14 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ Config-driven via ai_config.json. Supports:
- Conversation context for multi-turn interactions
"""
import asyncio
import json
import os
import re
@@ -37,8 +38,8 @@ def _extract_json_from_text(text):
return None
def _call_llm(system_prompt, user_prompt):
"""Call OpenAI-compatible API and return the response text."""
def _call_llm_sync(system_prompt, user_prompt):
"""Synchronous LLM call - runs in thread pool to avoid blocking."""
try:
response = client.chat.completions.create(
model=AI_CONFIG["model"],
@@ -64,7 +65,12 @@ def _call_llm(system_prompt, user_prompt):
return None
def parse(user_input, interaction_type, retry_count=0, errors=None, history=None):
async def _call_llm(system_prompt, user_prompt):
"""Async wrapper for LLM call - prevents Discord heartbeat blocking."""
return await asyncio.to_thread(_call_llm_sync, system_prompt, user_prompt)
async def parse(user_input, interaction_type, retry_count=0, errors=None, history=None):
"""
Parse user input into structured JSON using LLM.
@@ -111,14 +117,14 @@ def parse(user_input, interaction_type, retry_count=0, errors=None, history=None
f"\n\nPrevious attempt had errors: {errors}\nPlease fix and try again."
)
response_text = _call_llm(prompt_config["system"], user_prompt)
response_text = await _call_llm(prompt_config["system"], user_prompt)
if not response_text:
return {"error": "AI service unavailable", "user_input": user_input}
try:
parsed = json.loads(response_text)
except json.JSONDecodeError:
return parse(
return await parse(
user_input,
interaction_type,
retry_count + 1,