Insert navigation messages when agent finishes working (after tool execution)

- Track agent busy state: isStreaming OR pendingToolCalls.size > 0
- When agent transitions from busy to idle, check for URL changes
- Extract checkAndInsertNavMessage() helper function
- Call helper from both onBeforeSend (user submit) and agent state subscription (post-tool)
- Fixes issue where browser_javascript tool navigates page but no nav message inserted
- Navigation messages now appear after tools that change window.location.href
This commit is contained in:
Mario Zechner 2025-10-06 16:56:51 +02:00
parent d0cbca52b3
commit 6295025787
2 changed files with 43 additions and 21 deletions

View file

@ -75,9 +75,39 @@ 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 "";
@ -150,6 +180,15 @@ 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);
@ -343,25 +382,8 @@ async function initApp() {
return await ApiKeyPromptDialog.prompt(provider);
};
chatPanel.onBeforeSend = async () => {
// Get current tab info
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);
}
// Check for URL changes and insert nav message if needed
await checkAndInsertNavMessage();
};
chatPanel.additionalTools = [browserJavaScriptTool];

View file

@ -293,10 +293,10 @@ export class Agent {
this.patch({ isStreaming: false, streamMessage: null, pendingToolCalls: new Set<string>() });
this.abortController = undefined;
}
{
/*{
const { systemPrompt, model, messages } = this._state;
console.log("final state:", { systemPrompt, model, messages });
}
}*/
}
private patch(p: Partial<AgentState>): void {