mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 09:01:14 +00:00
Simplify navigation tracking - insert message on every active tab URL change
- Remove complex agent busy state tracking - Remove checkAndInsertNavMessage helper and onBeforeSend logic - Directly insert navigation messages when tab URL changes (chrome.tabs.onUpdated) - Also insert when user switches to a different tab (chrome.tabs.onActivated) - Much simpler: every URL change = navigation message, no conditions - Fixes issue where browser_javascript navigation wasn't detected
This commit is contained in:
parent
6295025787
commit
4f11cc15db
1 changed files with 7 additions and 49 deletions
|
|
@ -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<typeof createNavigationMessage> | undefined;
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
if (messages[i].role === "navigation") {
|
||||
lastNavMessage = messages[i] as ReturnType<typeof createNavigationMessage>;
|
||||
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<AgentState>) => {
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue