mirror of https://github.com/kortix-ai/suna.git
fix agent run status not updating
This commit is contained in:
parent
0250e786cd
commit
001cfacb01
|
@ -204,11 +204,12 @@ async def stop_agent_run(agent_run_id: str, error_message: Optional[str] = None)
|
||||||
|
|
||||||
# Update the agent run status in the database
|
# Update the agent run status in the database
|
||||||
update_success = await update_agent_run_status(
|
update_success = await update_agent_run_status(
|
||||||
client, agent_run_id, final_status, error=error_message, responses=all_responses
|
client, agent_run_id, final_status, error=error_message
|
||||||
)
|
)
|
||||||
|
|
||||||
if not update_success:
|
if not update_success:
|
||||||
logger.error(f"Failed to update database status for stopped/failed run {agent_run_id}")
|
logger.error(f"Failed to update database status for stopped/failed run {agent_run_id}")
|
||||||
|
raise HTTPException(status_code=500, detail="Failed to update agent run status in database")
|
||||||
|
|
||||||
# Send STOP signal to the global control channel
|
# Send STOP signal to the global control channel
|
||||||
global_control_channel = f"agent_run:{agent_run_id}:control"
|
global_control_channel = f"agent_run:{agent_run_id}:control"
|
||||||
|
|
|
@ -197,26 +197,25 @@ class SandboxWebSearchTool(SandboxToolsBase):
|
||||||
|
|
||||||
logging.info(f"Processing {len(url_list)} URLs: {url_list}")
|
logging.info(f"Processing {len(url_list)} URLs: {url_list}")
|
||||||
|
|
||||||
# Process each URL and collect results
|
# Process each URL concurrently and collect results
|
||||||
results = []
|
tasks = [self._scrape_single_url(url) for url in url_list]
|
||||||
for url in url_list:
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
try:
|
|
||||||
# Add protocol if missing
|
# Process results, handling exceptions
|
||||||
if not (url.startswith('http://') or url.startswith('https://')):
|
processed_results = []
|
||||||
url = 'https://' + url
|
for i, result in enumerate(results):
|
||||||
logging.info(f"Added https:// protocol to URL: {url}")
|
if isinstance(result, Exception):
|
||||||
|
logging.error(f"Error processing URL {url_list[i]}: {str(result)}")
|
||||||
# Scrape this URL
|
processed_results.append({
|
||||||
result = await self._scrape_single_url(url)
|
"url": url_list[i],
|
||||||
results.append(result)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Error processing URL {url}: {str(e)}")
|
|
||||||
results.append({
|
|
||||||
"url": url,
|
|
||||||
"success": False,
|
"success": False,
|
||||||
"error": str(e)
|
"error": str(result)
|
||||||
})
|
})
|
||||||
|
else:
|
||||||
|
processed_results.append(result)
|
||||||
|
|
||||||
|
results = processed_results
|
||||||
|
|
||||||
|
|
||||||
# Summarize results
|
# Summarize results
|
||||||
successful = sum(1 for r in results if r.get("success", False))
|
successful = sum(1 for r in results if r.get("success", False))
|
||||||
|
@ -255,6 +254,12 @@ class SandboxWebSearchTool(SandboxToolsBase):
|
||||||
"""
|
"""
|
||||||
Helper function to scrape a single URL and return the result information.
|
Helper function to scrape a single URL and return the result information.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# # Add protocol if missing
|
||||||
|
# if not (url.startswith('http://') or url.startswith('https://')):
|
||||||
|
# url = 'https://' + url
|
||||||
|
# logging.info(f"Added https:// protocol to URL: {url}")
|
||||||
|
|
||||||
logging.info(f"Scraping single URL: {url}")
|
logging.info(f"Scraping single URL: {url}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -272,7 +277,7 @@ class SandboxWebSearchTool(SandboxToolsBase):
|
||||||
|
|
||||||
# Use longer timeout and retry logic for more reliability
|
# Use longer timeout and retry logic for more reliability
|
||||||
max_retries = 3
|
max_retries = 3
|
||||||
timeout_seconds = 120
|
timeout_seconds = 30
|
||||||
retry_count = 0
|
retry_count = 0
|
||||||
|
|
||||||
while retry_count < max_retries:
|
while retry_count < max_retries:
|
||||||
|
|
|
@ -227,7 +227,7 @@ async def run_agent_background(
|
||||||
all_responses = [json.loads(r) for r in all_responses_json]
|
all_responses = [json.loads(r) for r in all_responses_json]
|
||||||
|
|
||||||
# Update DB status
|
# Update DB status
|
||||||
await update_agent_run_status(client, agent_run_id, final_status, error=error_message, responses=all_responses)
|
await update_agent_run_status(client, agent_run_id, final_status, error=error_message)
|
||||||
|
|
||||||
# Publish final control signal (END_STREAM or ERROR)
|
# Publish final control signal (END_STREAM or ERROR)
|
||||||
control_signal = "END_STREAM" if final_status == "completed" else "ERROR" if final_status == "failed" else "STOP"
|
control_signal = "END_STREAM" if final_status == "completed" else "ERROR" if final_status == "failed" else "STOP"
|
||||||
|
@ -264,7 +264,7 @@ async def run_agent_background(
|
||||||
all_responses = [error_response] # Use the error message we tried to push
|
all_responses = [error_response] # Use the error message we tried to push
|
||||||
|
|
||||||
# Update DB status
|
# Update DB status
|
||||||
await update_agent_run_status(client, agent_run_id, "failed", error=f"{error_message}\n{traceback_str}", responses=all_responses)
|
await update_agent_run_status(client, agent_run_id, "failed", error=f"{error_message}\n{traceback_str}")
|
||||||
|
|
||||||
# Publish ERROR signal
|
# Publish ERROR signal
|
||||||
try:
|
try:
|
||||||
|
@ -347,7 +347,6 @@ async def update_agent_run_status(
|
||||||
agent_run_id: str,
|
agent_run_id: str,
|
||||||
status: str,
|
status: str,
|
||||||
error: Optional[str] = None,
|
error: Optional[str] = None,
|
||||||
responses: Optional[list[any]] = None # Expects parsed list of dicts
|
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""
|
"""
|
||||||
Centralized function to update agent run status.
|
Centralized function to update agent run status.
|
||||||
|
@ -362,9 +361,7 @@ async def update_agent_run_status(
|
||||||
if error:
|
if error:
|
||||||
update_data["error"] = error
|
update_data["error"] = error
|
||||||
|
|
||||||
if responses:
|
|
||||||
# Ensure responses are stored correctly as JSONB
|
|
||||||
update_data["responses"] = responses
|
|
||||||
|
|
||||||
# Retry up to 3 times
|
# Retry up to 3 times
|
||||||
for retry in range(3):
|
for retry in range(3):
|
||||||
|
|
Loading…
Reference in New Issue