fixed reasoning
This commit is contained in:
@@ -18,7 +18,8 @@ interface PomodoroContextValue {
|
||||
const PomodoroContext = createContext<PomodoroContextValue | null>(null);
|
||||
|
||||
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 {
|
||||
return status?.status === "running" || status?.status === "paused";
|
||||
@@ -28,16 +29,28 @@ export function PomodoroProvider({ children }: { children: ReactNode }) {
|
||||
const [status, setStatus] = useState<PomodoroStatus | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const statusRef = useRef<PomodoroStatus | null>(null);
|
||||
const refreshInFlight = useRef<Promise<void> | null>(null);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
try {
|
||||
const data = await api.pomodoroStatus();
|
||||
statusRef.current = data;
|
||||
setStatus(data);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Ошибка загрузки таймера");
|
||||
if (refreshInFlight.current) {
|
||||
return refreshInFlight.current;
|
||||
}
|
||||
|
||||
const task = (async () => {
|
||||
try {
|
||||
const data = await api.pomodoroStatus();
|
||||
statusRef.current = data;
|
||||
setStatus(data);
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Ошибка загрузки таймера");
|
||||
} finally {
|
||||
refreshInFlight.current = null;
|
||||
}
|
||||
})();
|
||||
|
||||
refreshInFlight.current = task;
|
||||
return task;
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -46,19 +59,34 @@ export function PomodoroProvider({ children }: { children: ReactNode }) {
|
||||
let cancelled = false;
|
||||
let timeoutId: ReturnType<typeof setTimeout>;
|
||||
|
||||
const schedule = () => {
|
||||
const delay = isTimerActive(statusRef.current) ? POLL_ACTIVE_MS : POLL_IDLE_MS;
|
||||
timeoutId = setTimeout(async () => {
|
||||
if (cancelled) return;
|
||||
await refresh().catch(console.error);
|
||||
schedule();
|
||||
}, delay);
|
||||
const pollDelay = () => {
|
||||
if (document.hidden) return POLL_HIDDEN_MS;
|
||||
return isTimerActive(statusRef.current) ? POLL_ACTIVE_MS : POLL_IDLE_MS;
|
||||
};
|
||||
|
||||
const schedule = () => {
|
||||
timeoutId = setTimeout(async () => {
|
||||
if (cancelled) return;
|
||||
if (!document.hidden) {
|
||||
await refresh().catch(console.error);
|
||||
}
|
||||
schedule();
|
||||
}, pollDelay());
|
||||
};
|
||||
|
||||
const onVisibilityChange = () => {
|
||||
if (!document.hidden) {
|
||||
refresh().catch(console.error);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener("visibilitychange", onVisibilityChange);
|
||||
schedule();
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearTimeout(timeoutId);
|
||||
document.removeEventListener("visibilitychange", onVisibilityChange);
|
||||
};
|
||||
}, [refresh]);
|
||||
|
||||
|
||||
@@ -38,6 +38,9 @@ export default function Chat() {
|
||||
const [input, setInput] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [streaming, setStreaming] = useState("");
|
||||
const [pendingPhase, setPendingPhase] = useState<"thinking" | "preparing" | "generating">(
|
||||
"thinking",
|
||||
);
|
||||
const [liveNotices, setLiveNotices] = useState<string[]>([]);
|
||||
const bottomRef = useRef<HTMLDivElement>(null);
|
||||
const { status: pomodoroStatus, refresh: refreshPomodoro } = usePomodoro();
|
||||
@@ -71,7 +74,14 @@ export default function Chat() {
|
||||
}, [messages, streaming, liveNotices, loading]);
|
||||
|
||||
const waitingForStream = loading && !streaming;
|
||||
const pendingLabel = liveNotices.length > 0 ? "Обрабатываю…" : "Думаю…";
|
||||
const pendingLabel =
|
||||
liveNotices.length > 0
|
||||
? "Обрабатываю…"
|
||||
: pendingPhase === "preparing"
|
||||
? "Собираю контекст…"
|
||||
: pendingPhase === "generating"
|
||||
? "Генерирую ответ…"
|
||||
: "Думаю…";
|
||||
|
||||
useEffect(() => {
|
||||
const seq = pomodoroStatus?.cycle?.chat_notify_seq ?? 0;
|
||||
@@ -111,6 +121,7 @@ export default function Chat() {
|
||||
setInput("");
|
||||
setLoading(true);
|
||||
setStreaming("");
|
||||
setPendingPhase("thinking");
|
||||
setLiveNotices([]);
|
||||
|
||||
const tempUser: ChatMessage = {
|
||||
@@ -122,9 +133,19 @@ export default function Chat() {
|
||||
setMessages((prev) => [...prev, tempUser]);
|
||||
|
||||
try {
|
||||
let assistantText = "";
|
||||
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") {
|
||||
setStreaming((prev) => prev + chunk.data.content);
|
||||
assistantText += chunk.data.content;
|
||||
setStreaming(assistantText);
|
||||
}
|
||||
if (chunk.event === "notice") {
|
||||
setLiveNotices((prev) => [...prev, chunk.data.content]);
|
||||
@@ -136,10 +157,22 @@ export default function Chat() {
|
||||
refreshPomodoro();
|
||||
}
|
||||
if (chunk.event === "done") {
|
||||
await loadMessages(activeId);
|
||||
await loadSessions();
|
||||
setStreaming("");
|
||||
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") {
|
||||
throw new Error(chunk.data.message);
|
||||
|
||||
Reference in New Issue
Block a user