Fix: Websocket communication misunderstanding error

This commit is contained in:
ziesorx 2025-09-13 00:19:41 +07:00
parent 9967bff6dc
commit 42a8325faf
8 changed files with 1109 additions and 63 deletions

View file

@ -227,6 +227,7 @@ class ModelManager:
async def _get_model_path(self, model_url: str, model_id: str) -> str:
"""
Get local path for a model, downloading if necessary.
Uses model_id subfolder structure: models/{model_id}/
Args:
model_url: URL or local path to model
@ -246,14 +247,18 @@ class ModelManager:
if parsed.scheme == 'file':
return parsed.path
# For HTTP/HTTPS URLs, download to cache
# For HTTP/HTTPS URLs, download to cache with model_id subfolder
if parsed.scheme in ['http', 'https']:
# Create model_id subfolder structure
model_dir = os.path.join(self.models_dir, str(model_id))
os.makedirs(model_dir, exist_ok=True)
# Generate cache filename
filename = os.path.basename(parsed.path)
if not filename:
filename = f"{model_id}.mpta"
filename = f"model_{model_id}.mpta"
cache_path = os.path.join(self.models_dir, filename)
cache_path = os.path.join(model_dir, filename)
# Check if already cached
if os.path.exists(cache_path):
@ -261,7 +266,7 @@ class ModelManager:
return cache_path
# Download model
logger.info(f"Downloading model from {model_url}")
logger.info(f"Downloading model {model_id} from {model_url}")
await self._download_model(model_url, cache_path)
return cache_path
@ -270,7 +275,7 @@ class ModelManager:
async def _download_model(self, url: str, destination: str) -> None:
"""
Download a model file from URL.
Download a model file from URL with enhanced HTTP request logging.
Args:
url: URL to download from
@ -278,9 +283,20 @@ class ModelManager:
"""
import aiohttp
import aiofiles
import time
# Import HTTP logger
from ..utils.logging_utils import get_http_logger
http_logger = get_http_logger()
start_time = time.time()
correlation_id = None
try:
async with aiohttp.ClientSession() as session:
# Log request start
correlation_id = http_logger.log_request_start("GET", url)
async with session.get(url) as response:
response.raise_for_status()
@ -293,22 +309,39 @@ class ModelManager:
# Download to temporary file first
temp_path = f"{destination}.tmp"
downloaded = 0
last_progress_log = 0
async with aiofiles.open(temp_path, 'wb') as f:
async for chunk in response.content.iter_chunked(8192):
await f.write(chunk)
downloaded += len(chunk)
# Log progress
if total_size and downloaded % (1024 * 1024) == 0:
# Log progress at 10% intervals
if total_size and downloaded > 0:
progress = (downloaded / total_size) * 100
logger.info(f"Download progress: {progress:.1f}%")
if progress >= last_progress_log + 10 and progress <= 100:
logger.info(f"Download progress: {progress:.1f}%")
http_logger.log_download_progress(
downloaded, total_size, progress, correlation_id
)
last_progress_log = progress
# Move to final destination
os.rename(temp_path, destination)
# Log successful completion
duration_ms = (time.time() - start_time) * 1000
http_logger.log_request_end(
response.status, downloaded, duration_ms, correlation_id
)
logger.info(f"Model downloaded successfully to {destination}")
except Exception as e:
# Log failed completion
if correlation_id:
duration_ms = (time.time() - start_time) * 1000
http_logger.log_request_end(500, None, duration_ms, correlation_id)
# Clean up temporary file if exists
temp_path = f"{destination}.tmp"
if os.path.exists(temp_path):