Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 3 additions & 79 deletions ui/desktop/src/hooks/useChatStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {

import { createUserMessage, getCompactingMessage, getThinkingMessage } from '../types/message';
import { errorMessage } from '../utils/conversionUtils';
import { LocalMessageStorage } from '../utils/localMessageStorage';

const resultsCache = new Map<string, { messages: Message[]; session: Session }>();

Expand Down Expand Up @@ -165,8 +164,6 @@ export function useChatStream({
});
const [notifications, setNotifications] = useState<NotificationEvent[]>([]);
const abortControllerRef = useRef<AbortController | null>(null);
const [lastInteractionTime, setLastInteractionTime] = useState<number>(Date.now());
const [powerSaveTimeoutId, setPowerSaveTimeoutId] = useState<number | null>(null);

useEffect(() => {
if (session) {
Expand All @@ -183,42 +180,12 @@ export function useChatStream({
setNotifications((prev) => [...prev, notification]);
}, []);

const stopPowerSaveBlocker = useCallback(() => {
try {
window.electron.stopPowerSaveBlocker();
} catch (error) {
console.error('Failed to stop power save blocker:', error);
}

if (powerSaveTimeoutId) {
window.clearTimeout(powerSaveTimeoutId);
setPowerSaveTimeoutId(null);
}
}, [powerSaveTimeoutId]);

const onFinish = useCallback(
(error?: string): void => {
stopPowerSaveBlocker();

if (error) {
setSessionLoadError(error);
}

const timeSinceLastInteraction = Date.now() - lastInteractionTime;

if (timeSinceLastInteraction > 60000) {
try {
window.electron.showNotification({
title: 'Goose finished the task',
body: 'Click here for details',
});
} catch (error) {
console.error('Failed to show notification:', error);
}
} else {
console.log('useChatStream: Not showing notification (task took less than 2 seconds)');
}

const isNewSession = sessionId && sessionId.match(/^\d{8}_\d{6}$/);
if (isNewSession) {
console.log(
Expand All @@ -230,22 +197,9 @@ export function useChatStream({
setChatState(ChatState.Idle);
onStreamFinish();
},
[onStreamFinish, stopPowerSaveBlocker, lastInteractionTime, sessionId]
[onStreamFinish, sessionId]
);

useEffect(() => {
return () => {
if (powerSaveTimeoutId) {
window.clearTimeout(powerSaveTimeoutId);
}
try {
window.electron.stopPowerSaveBlocker();
} catch (error) {
console.error('Failed to stop power save blocker during cleanup:', error);
}
};
}, [powerSaveTimeoutId]);

// Load session on mount or sessionId change
useEffect(() => {
if (!sessionId) return;
Expand Down Expand Up @@ -306,29 +260,9 @@ export function useChatStream({
}

if (!userMessage.trim()) {
stopPowerSaveBlocker();
return;
}

LocalMessageStorage.addMessage(userMessage.trim());

try {
window.electron.startPowerSaveBlocker();
} catch (error) {
console.error('Failed to start power save blocker:', error);
}

setLastInteractionTime(Date.now());

const timeoutId = window.setTimeout(
() => {
console.warn('Power save blocker timeout - stopping automatically after 15 minutes');
stopPowerSaveBlocker();
},
15 * 60 * 1000
);
setPowerSaveTimeoutId(timeoutId);

if (messagesRef.current.length === 0) {
window.dispatchEvent(new CustomEvent('session-created'));
}
Expand Down Expand Up @@ -369,15 +303,7 @@ export function useChatStream({
}
}
},
[
sessionId,
session,
chatState,
updateMessages,
updateNotifications,
onFinish,
stopPowerSaveBlocker,
]
[sessionId, session, chatState, updateMessages, updateNotifications, onFinish]
);

const setRecipeUserParams = useCallback(
Expand Down Expand Up @@ -420,10 +346,8 @@ export function useChatStream({

const stopStreaming = useCallback(() => {
abortControllerRef.current?.abort();
setLastInteractionTime(Date.now());
stopPowerSaveBlocker();
setChatState(ChatState.Idle);
}, [stopPowerSaveBlocker]);
}, []);

const cached = resultsCache.get(sessionId);
const maybe_cached_messages = session ? messages : cached?.messages || [];
Expand Down
33 changes: 0 additions & 33 deletions ui/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2178,39 +2178,6 @@ async function appMain() {
}
});

ipcMain.handle('start-power-save-blocker', (event) => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window?.id;

if (windowId && !windowPowerSaveBlockers.has(windowId)) {
const blockerId = powerSaveBlocker.start('prevent-app-suspension');
windowPowerSaveBlockers.set(windowId, blockerId);
console.log(`[Main] Started power save blocker ${blockerId} for window ${windowId}`);
return true;
}

if (windowId && windowPowerSaveBlockers.has(windowId)) {
console.log(`[Main] Power save blocker already active for window ${windowId}`);
}

return false;
});

ipcMain.handle('stop-power-save-blocker', (event) => {
const window = BrowserWindow.fromWebContents(event.sender);
const windowId = window?.id;

if (windowId && windowPowerSaveBlockers.has(windowId)) {
const blockerId = windowPowerSaveBlockers.get(windowId)!;
powerSaveBlocker.stop(blockerId);
windowPowerSaveBlockers.delete(windowId);
console.log(`[Main] Stopped power save blocker ${blockerId} for window ${windowId}`);
return true;
}

return false;
});

// Handle metadata fetching from main process
ipcMain.handle('fetch-metadata', async (_event, url) => {
try {
Expand Down
4 changes: 0 additions & 4 deletions ui/desktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ type ElectronAPI = {
reloadApp: () => void;
checkForOllama: () => Promise<boolean>;
selectFileOrDirectory: (defaultPath?: string) => Promise<string | null>;
startPowerSaveBlocker: () => Promise<number>;
stopPowerSaveBlocker: () => Promise<void>;
getBinaryPath: (binaryName: string) => Promise<string>;
readFile: (directory: string) => Promise<FileResponse>;
writeFile: (directory: string, content: string) => Promise<boolean>;
Expand Down Expand Up @@ -164,8 +162,6 @@ const electronAPI: ElectronAPI = {
checkForOllama: () => ipcRenderer.invoke('check-ollama'),
selectFileOrDirectory: (defaultPath?: string) =>
ipcRenderer.invoke('select-file-or-directory', defaultPath),
startPowerSaveBlocker: () => ipcRenderer.invoke('start-power-save-blocker'),
stopPowerSaveBlocker: () => ipcRenderer.invoke('stop-power-save-blocker'),
getBinaryPath: (binaryName: string) => ipcRenderer.invoke('get-binary-path', binaryName),
readFile: (filePath: string) => ipcRenderer.invoke('read-file', filePath),
writeFile: (filePath: string, content: string) =>
Expand Down