mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 22:02:38 +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 currentTabUrl: string | undefined;
|
||||||
let currentTabIndex: number | undefined;
|
let currentTabIndex: number | undefined;
|
||||||
|
|
||||||
// Track if agent is busy (streaming or executing tools)
|
|
||||||
let isAgentBusy = false;
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// HELPERS
|
// 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 generateTitle = (messages: AppMessage[]): string => {
|
||||||
const firstUserMsg = messages.find((m) => m.role === "user");
|
const firstUserMsg = messages.find((m) => m.role === "user");
|
||||||
if (!firstUserMsg || firstUserMsg.role !== "user") return "";
|
if (!firstUserMsg || firstUserMsg.role !== "user") return "";
|
||||||
|
|
@ -180,15 +151,6 @@ const createAgent = async (initialState?: Partial<AgentState>) => {
|
||||||
if (event.type === "state-update") {
|
if (event.type === "state-update") {
|
||||||
const messages = event.state.messages;
|
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
|
// Generate title after first successful response
|
||||||
if (!currentTitle && shouldSaveSession(messages)) {
|
if (!currentTitle && shouldSaveSession(messages)) {
|
||||||
currentTitle = generateTitle(messages);
|
currentTitle = generateTitle(messages);
|
||||||
|
|
@ -337,21 +299,21 @@ const renderApp = () => {
|
||||||
// TAB NAVIGATION TRACKING
|
// 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) => {
|
chrome.tabs.onUpdated.addListener((_tabId, changeInfo, tab) => {
|
||||||
// Only care about URL changes on the active tab
|
// Only care about URL changes on the active tab
|
||||||
if (changeInfo.url && tab.active && tab.url) {
|
if (changeInfo.url && tab.active && tab.url && agent) {
|
||||||
currentTabUrl = tab.url;
|
const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index);
|
||||||
currentTabIndex = tab.index;
|
agent.appendMessage(navMessage);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Listen for tab activation (user switches tabs)
|
// Listen for tab activation (user switches tabs)
|
||||||
chrome.tabs.onActivated.addListener(async (activeInfo) => {
|
chrome.tabs.onActivated.addListener(async (activeInfo) => {
|
||||||
const tab = await chrome.tabs.get(activeInfo.tabId);
|
const tab = await chrome.tabs.get(activeInfo.tabId);
|
||||||
if (tab.url) {
|
if (tab.url && agent) {
|
||||||
currentTabUrl = tab.url;
|
const navMessage = createNavigationMessage(tab.url, tab.title || "Untitled", tab.favIconUrl, tab.index);
|
||||||
currentTabIndex = tab.index;
|
agent.appendMessage(navMessage);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -381,10 +343,6 @@ async function initApp() {
|
||||||
chatPanel.onApiKeyRequired = async (provider: string) => {
|
chatPanel.onApiKeyRequired = async (provider: string) => {
|
||||||
return await ApiKeyPromptDialog.prompt(provider);
|
return await ApiKeyPromptDialog.prompt(provider);
|
||||||
};
|
};
|
||||||
chatPanel.onBeforeSend = async () => {
|
|
||||||
// Check for URL changes and insert nav message if needed
|
|
||||||
await checkAndInsertNavMessage();
|
|
||||||
};
|
|
||||||
chatPanel.additionalTools = [browserJavaScriptTool];
|
chatPanel.additionalTools = [browserJavaScriptTool];
|
||||||
|
|
||||||
// Initialize current tab state
|
// Initialize current tab state
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue