fixed reasoning
This commit is contained in:
@@ -52,4 +52,12 @@ async def send_message(
|
||||
async for chunk in service.stream_response(session_id, payload.content):
|
||||
yield chunk
|
||||
|
||||
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
||||
return StreamingResponse(
|
||||
event_stream(),
|
||||
media_type="text/event-stream",
|
||||
headers={
|
||||
"Cache-Control": "no-cache",
|
||||
"Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
},
|
||||
)
|
||||
|
||||
@@ -37,6 +37,18 @@ MAX_HISTORY_MESSAGES = 40
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _build_messages_for_session(session_id: int) -> list[dict[str, Any]]:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
service = ChatService(db)
|
||||
session = service.get_session(session_id)
|
||||
if not session:
|
||||
return []
|
||||
return service._build_messages(session)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
async def _extract_memory_background(
|
||||
session_id: int,
|
||||
user_text: str,
|
||||
@@ -166,7 +178,12 @@ class ChatService:
|
||||
return
|
||||
|
||||
self._save_message(session_id, "user", user_text)
|
||||
messages = self._build_messages(session)
|
||||
yield self._sse("status", {"phase": "preparing"})
|
||||
messages = await asyncio.to_thread(_build_messages_for_session, session_id)
|
||||
if not messages:
|
||||
yield self._sse("error", {"message": "Session not found"})
|
||||
return
|
||||
yield self._sse("status", {"phase": "generating"})
|
||||
streamed_reply_parts: list[str] = []
|
||||
|
||||
for _ in range(MAX_TOOL_ROUNDS):
|
||||
|
||||
@@ -137,6 +137,17 @@ def format_weather_snapshot(data: dict[str, Any] | None = None) -> str:
|
||||
f"(ощущается {cur.get('apparent_temperature_c')}°C), "
|
||||
f"{cur.get('conditions')}, ветер {cur.get('wind_speed_kmh')} км/ч."
|
||||
)
|
||||
lines.append(client.rain_summary(hours_ahead=6))
|
||||
hourly = snapshot.get("hourly") or []
|
||||
rainy_hours = []
|
||||
for hour in hourly:
|
||||
prob = hour.get("precipitation_probability")
|
||||
precip = hour.get("precipitation_mm") or 0
|
||||
if (prob is not None and prob >= 40) or precip > 0:
|
||||
time_str = (hour.get("time") or "")[11:16]
|
||||
rainy_hours.append(f"{time_str} ({prob}% вероятность, {precip} мм)")
|
||||
if rainy_hours:
|
||||
lines.append("Ожидаются осадки: " + ", ".join(rainy_hours[:6]))
|
||||
else:
|
||||
lines.append("Существенных осадков в ближайшие часы не ожидается.")
|
||||
lines.append("Вопросы «что на улице» / «будет ли дождь» — get_weather.")
|
||||
return "\n".join(lines)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -8,9 +9,28 @@ from app.projects.service import ProjectService
|
||||
|
||||
MAX_PROJECTS_IN_CONTEXT = 20
|
||||
MAX_OPEN_PER_PROJECT = 8
|
||||
PROJECTS_CACHE_SEC = 120
|
||||
|
||||
_cache: dict[str, Any] = {"data": None, "expires_at": 0.0}
|
||||
|
||||
|
||||
def get_projects_snapshot(db: Session) -> dict[str, Any]:
|
||||
def invalidate_projects_snapshot_cache() -> None:
|
||||
_cache["data"] = None
|
||||
_cache["expires_at"] = 0.0
|
||||
|
||||
|
||||
def get_projects_snapshot(db: Session, *, force: bool = False) -> dict[str, Any]:
|
||||
now = time.time()
|
||||
if not force and _cache["data"] is not None and now < _cache["expires_at"]:
|
||||
return _cache["data"]
|
||||
|
||||
snapshot = _fetch_projects_snapshot(db)
|
||||
_cache["data"] = snapshot
|
||||
_cache["expires_at"] = now + PROJECTS_CACHE_SEC
|
||||
return snapshot
|
||||
|
||||
|
||||
def _fetch_projects_snapshot(db: Session) -> dict[str, Any]:
|
||||
settings = get_settings()
|
||||
service = ProjectService(db)
|
||||
|
||||
|
||||
@@ -643,7 +643,10 @@ async def execute_tool(
|
||||
elif name == "get_pomodoro_history":
|
||||
result = pomodoro.history(limit=arguments.get("limit", 10))
|
||||
elif name == "sync_taiga_projects":
|
||||
from app.projects.context import invalidate_projects_snapshot_cache
|
||||
|
||||
result = projects.sync_taiga_projects()
|
||||
invalidate_projects_snapshot_cache()
|
||||
elif name == "list_taiga_projects":
|
||||
result = projects.list_projects()
|
||||
elif name == "list_taiga_tasks":
|
||||
|
||||
Reference in New Issue
Block a user