diff --git a/packages/browser-extension/src/sidepanel.ts b/packages/browser-extension/src/sidepanel.ts index c1edbc13..a52f822e 100644 --- a/packages/browser-extension/src/sidepanel.ts +++ b/packages/browser-extension/src/sidepanel.ts @@ -75,39 +75,10 @@ let agentUnsubscribe: (() => void) | undefined; let currentTabUrl: string | undefined; let currentTabIndex: number | undefined; -// Track if agent is busy (streaming or executing tools) -let isAgentBusy = false; - // ============================================================================ // HELPERS // ============================================================================ -/** - * Check if URL changed and insert navigation message if needed - */ -const checkAndInsertNavMessage = async () => { - if (!agent) return; - - const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); - if (!tab?.url) return; - - // Find last navigation message in messages (reverse loop) - const messages = agent.state.messages; - let lastNavMessage: ReturnType | undefined; - for (let i = messages.length - 1; i >= 0; i--) { - if (messages[i].role === "navigation") { - lastNavMessage = messages[i] as ReturnType; - break; - } - } - - // Only insert if URL or tab changed - if (lastNavMessage?.url !== tab.url || lastNavMessage?.tabIndex !== tab.index) { - const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index); - agent.appendMessage(navMessage); - } -}; - const generateTitle = (messages: AppMessage[]): string => { const firstUserMsg = messages.find((m) => m.role === "user"); if (!firstUserMsg || firstUserMsg.role !== "user") return ""; @@ -180,15 +151,6 @@ const createAgent = async (initialState?: Partial) => { if (event.type === "state-update") { const messages = event.state.messages; - // Track agent busy state (streaming or executing tools) - const wasBusy = isAgentBusy; - isAgentBusy = event.state.isStreaming || event.state.pendingToolCalls.size > 0; - - // If agent just finished being busy, check for URL changes and insert nav message - if (wasBusy && !isAgentBusy) { - checkAndInsertNavMessage(); - } - // Generate title after first successful response if (!currentTitle && shouldSaveSession(messages)) { currentTitle = generateTitle(messages); @@ -337,21 +299,21 @@ const renderApp = () => { // TAB NAVIGATION TRACKING // ============================================================================ -// Listen for tab updates to track current tab state (don't insert messages yet) +// Listen for tab updates and insert navigation messages immediately chrome.tabs.onUpdated.addListener((_tabId, changeInfo, tab) => { // Only care about URL changes on the active tab - if (changeInfo.url && tab.active && tab.url) { - currentTabUrl = tab.url; - currentTabIndex = tab.index; + if (changeInfo.url && tab.active && tab.url && agent) { + const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index); + agent.appendMessage(navMessage); } }); // Listen for tab activation (user switches tabs) chrome.tabs.onActivated.addListener(async (activeInfo) => { const tab = await chrome.tabs.get(activeInfo.tabId); - if (tab.url) { - currentTabUrl = tab.url; - currentTabIndex = tab.index; + if (tab.url && agent) { + const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index); + agent.appendMessage(navMessage); } }); @@ -381,10 +343,6 @@ async function initApp() { chatPanel.onApiKeyRequired = async (provider: string) => { return await ApiKeyPromptDialog.prompt(provider); }; - chatPanel.onBeforeSend = async () => { - // Check for URL changes and insert nav message if needed - await checkAndInsertNavMessage(); - }; chatPanel.additionalTools = [browserJavaScriptTool]; // Initialize current tab state