fixed reasoning

This commit is contained in:
2026-06-10 14:56:18 +03:00
parent 89158930ee
commit e9762d7921
7 changed files with 143 additions and 23 deletions
+9 -1
View File
@@ -52,4 +52,12 @@ async def send_message(
async for chunk in service.stream_response(session_id, payload.content): async for chunk in service.stream_response(session_id, payload.content):
yield chunk 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",
},
)
+18 -1
View File
@@ -37,6 +37,18 @@ MAX_HISTORY_MESSAGES = 40
logger = logging.getLogger(__name__) 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( async def _extract_memory_background(
session_id: int, session_id: int,
user_text: str, user_text: str,
@@ -166,7 +178,12 @@ class ChatService:
return return
self._save_message(session_id, "user", user_text) 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] = [] streamed_reply_parts: list[str] = []
for _ in range(MAX_TOOL_ROUNDS): for _ in range(MAX_TOOL_ROUNDS):
+12 -1
View File
@@ -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('apparent_temperature_c')}°C), "
f"{cur.get('conditions')}, ветер {cur.get('wind_speed_kmh')} км/ч." 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.") lines.append("Вопросы «что на улице» / «будет ли дождь» — get_weather.")
return "\n".join(lines) return "\n".join(lines)
+21 -1
View File
@@ -1,3 +1,4 @@
import time
from typing import Any from typing import Any
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@@ -8,9 +9,28 @@ from app.projects.service import ProjectService
MAX_PROJECTS_IN_CONTEXT = 20 MAX_PROJECTS_IN_CONTEXT = 20
MAX_OPEN_PER_PROJECT = 8 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() settings = get_settings()
service = ProjectService(db) service = ProjectService(db)
+3
View File
@@ -643,7 +643,10 @@ async def execute_tool(
elif name == "get_pomodoro_history": elif name == "get_pomodoro_history":
result = pomodoro.history(limit=arguments.get("limit", 10)) result = pomodoro.history(limit=arguments.get("limit", 10))
elif name == "sync_taiga_projects": elif name == "sync_taiga_projects":
from app.projects.context import invalidate_projects_snapshot_cache
result = projects.sync_taiga_projects() result = projects.sync_taiga_projects()
invalidate_projects_snapshot_cache()
elif name == "list_taiga_projects": elif name == "list_taiga_projects":
result = projects.list_projects() result = projects.list_projects()
elif name == "list_taiga_tasks": elif name == "list_taiga_tasks":
+36 -8
View File
@@ -18,7 +18,8 @@ interface PomodoroContextValue {
const PomodoroContext = createContext<PomodoroContextValue | null>(null); const PomodoroContext = createContext<PomodoroContextValue | null>(null);
const POLL_ACTIVE_MS = 1000; const POLL_ACTIVE_MS = 1000;
const POLL_IDLE_MS = 30000; const POLL_IDLE_MS = 60000;
const POLL_HIDDEN_MS = 120000;
function isTimerActive(status: PomodoroStatus | null): boolean { function isTimerActive(status: PomodoroStatus | null): boolean {
return status?.status === "running" || status?.status === "paused"; return status?.status === "running" || status?.status === "paused";
@@ -28,8 +29,14 @@ export function PomodoroProvider({ children }: { children: ReactNode }) {
const [status, setStatus] = useState<PomodoroStatus | null>(null); const [status, setStatus] = useState<PomodoroStatus | null>(null);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const statusRef = useRef<PomodoroStatus | null>(null); const statusRef = useRef<PomodoroStatus | null>(null);
const refreshInFlight = useRef<Promise<void> | null>(null);
const refresh = useCallback(async () => { const refresh = useCallback(async () => {
if (refreshInFlight.current) {
return refreshInFlight.current;
}
const task = (async () => {
try { try {
const data = await api.pomodoroStatus(); const data = await api.pomodoroStatus();
statusRef.current = data; statusRef.current = data;
@@ -37,7 +44,13 @@ export function PomodoroProvider({ children }: { children: ReactNode }) {
setError(null); setError(null);
} catch (err) { } catch (err) {
setError(err instanceof Error ? err.message : "Ошибка загрузки таймера"); setError(err instanceof Error ? err.message : "Ошибка загрузки таймера");
} finally {
refreshInFlight.current = null;
} }
})();
refreshInFlight.current = task;
return task;
}, []); }, []);
useEffect(() => { useEffect(() => {
@@ -46,19 +59,34 @@ export function PomodoroProvider({ children }: { children: ReactNode }) {
let cancelled = false; let cancelled = false;
let timeoutId: ReturnType<typeof setTimeout>; let timeoutId: ReturnType<typeof setTimeout>;
const schedule = () => { const pollDelay = () => {
const delay = isTimerActive(statusRef.current) ? POLL_ACTIVE_MS : POLL_IDLE_MS; if (document.hidden) return POLL_HIDDEN_MS;
timeoutId = setTimeout(async () => { return isTimerActive(statusRef.current) ? POLL_ACTIVE_MS : POLL_IDLE_MS;
if (cancelled) return;
await refresh().catch(console.error);
schedule();
}, delay);
}; };
const schedule = () => {
timeoutId = setTimeout(async () => {
if (cancelled) return;
if (!document.hidden) {
await refresh().catch(console.error);
}
schedule(); schedule();
}, pollDelay());
};
const onVisibilityChange = () => {
if (!document.hidden) {
refresh().catch(console.error);
}
};
document.addEventListener("visibilitychange", onVisibilityChange);
schedule();
return () => { return () => {
cancelled = true; cancelled = true;
clearTimeout(timeoutId); clearTimeout(timeoutId);
document.removeEventListener("visibilitychange", onVisibilityChange);
}; };
}, [refresh]); }, [refresh]);
+37 -4
View File
@@ -38,6 +38,9 @@ export default function Chat() {
const [input, setInput] = useState(""); const [input, setInput] = useState("");
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [streaming, setStreaming] = useState(""); const [streaming, setStreaming] = useState("");
const [pendingPhase, setPendingPhase] = useState<"thinking" | "preparing" | "generating">(
"thinking",
);
const [liveNotices, setLiveNotices] = useState<string[]>([]); const [liveNotices, setLiveNotices] = useState<string[]>([]);
const bottomRef = useRef<HTMLDivElement>(null); const bottomRef = useRef<HTMLDivElement>(null);
const { status: pomodoroStatus, refresh: refreshPomodoro } = usePomodoro(); const { status: pomodoroStatus, refresh: refreshPomodoro } = usePomodoro();
@@ -71,7 +74,14 @@ export default function Chat() {
}, [messages, streaming, liveNotices, loading]); }, [messages, streaming, liveNotices, loading]);
const waitingForStream = loading && !streaming; const waitingForStream = loading && !streaming;
const pendingLabel = liveNotices.length > 0 ? "Обрабатываю…" : "Думаю…"; const pendingLabel =
liveNotices.length > 0
? "Обрабатываю…"
: pendingPhase === "preparing"
? "Собираю контекст…"
: pendingPhase === "generating"
? "Генерирую ответ…"
: "Думаю…";
useEffect(() => { useEffect(() => {
const seq = pomodoroStatus?.cycle?.chat_notify_seq ?? 0; const seq = pomodoroStatus?.cycle?.chat_notify_seq ?? 0;
@@ -111,6 +121,7 @@ export default function Chat() {
setInput(""); setInput("");
setLoading(true); setLoading(true);
setStreaming(""); setStreaming("");
setPendingPhase("thinking");
setLiveNotices([]); setLiveNotices([]);
const tempUser: ChatMessage = { const tempUser: ChatMessage = {
@@ -122,9 +133,19 @@ export default function Chat() {
setMessages((prev) => [...prev, tempUser]); setMessages((prev) => [...prev, tempUser]);
try { try {
let assistantText = "";
for await (const chunk of api.sendMessage(activeId, text)) { for await (const chunk of api.sendMessage(activeId, text)) {
if (chunk.event === "status") {
if (chunk.data.phase === "preparing") {
setPendingPhase("preparing");
}
if (chunk.data.phase === "generating") {
setPendingPhase("generating");
}
}
if (chunk.event === "token") { if (chunk.event === "token") {
setStreaming((prev) => prev + chunk.data.content); assistantText += chunk.data.content;
setStreaming(assistantText);
} }
if (chunk.event === "notice") { if (chunk.event === "notice") {
setLiveNotices((prev) => [...prev, chunk.data.content]); setLiveNotices((prev) => [...prev, chunk.data.content]);
@@ -136,10 +157,22 @@ export default function Chat() {
refreshPomodoro(); refreshPomodoro();
} }
if (chunk.event === "done") { if (chunk.event === "done") {
await loadMessages(activeId);
await loadSessions();
setStreaming(""); setStreaming("");
setLiveNotices([]); setLiveNotices([]);
setLoading(false);
if (assistantText.trim()) {
setMessages((prev) => [
...prev,
{
id: Date.now(),
role: "assistant",
content: assistantText,
created_at: new Date().toISOString(),
},
]);
}
void loadMessages(activeId);
void loadSessions();
} }
if (chunk.event === "error") { if (chunk.event === "error") {
throw new Error(chunk.data.message); throw new Error(chunk.data.message);