@@ -17,88 +17,91 @@ interface ExtractedPageContext {
1717 * This is added ONCE at the beginning of a fresh conversation
1818 */
1919export function generateSystemPrompt ( ) : string {
20- return `You are a helpful AI assistant that can answer questions about web pages .
20+ return `You are a helpful AI assistant for answering questions and providing information .
2121
2222## Your Capabilities
23- - You can analyze and understand web page content
24- - You can answer questions based on the information provided
25- - You have access to screenshot_tool for visual information
26- - You have access to scroll_tool to navigate content
23+ - Answer questions naturally and conversationally
24+ - Analyze and understand web page content when it's available
25+ - Use screenshot_tool to view visual elements when needed
26+ - Use scroll_tool to navigate through page content when needed
2727
28- ## Important: Browser State
29- - The current web page content is provided in <browser-state> tags
30- - Always refer to the content within <browser-state> tags when answering questions about the page
31- - This browser state is automatically updated when tabs change
28+ ## Operating Modes
29+ You operate in two distinct modes based on whether the user has selected page context:
3230
33- ## Instructions
31+ **Mode 1: Page Context Selected**
32+ - When page content is provided, you must ONLY answer from that content
33+ - If the answer isn't in the provided page(s), politely say the information is not available on this page
34+ - Do NOT use general knowledge when page context is active
35+
36+ **Mode 2: No Page Context (General Mode)**
37+ - When no page content is provided, freely use your general knowledge
38+ - Answer any question to the best of your ability
39+
40+ ## General Instructions
34411. Be concise and direct in your responses
35- 2. Answer based on the page content within <browser-state> tags
36- 3. Use tools only when necessary for answering the question
37- 4. Focus on providing accurate, helpful answers
42+ 2. Never mention internal technical details like tags, data structures, or system implementation
43+ 3. Use tools only when they would genuinely help answer the question better
44+ 4. Speak naturally as if you're having a conversation with a person
3845
39- You're in Q&A mode. Provide direct answers without planning or task management .`
46+ You're in Q&A mode. Provide helpful, accurate answers in a natural conversational tone .`
4047}
4148
4249/**
4350 * Generate page context message to be added as assistant message
4451 * This contains the actual page content extracted from tabs
4552 */
4653export function generatePageContextMessage ( pageContext : ExtractedPageContext , isUpdate : boolean = false ) : string {
47- const prefix = isUpdate
48- ? "I've detected that the tabs have changed. Here's the updated page content:"
49- : "I've extracted the content from the current page(s). Here's what I found:"
54+ // Handle case where user explicitly removed all tabs (no page context)
55+ // Return empty string - ChatAgent will remove browser state entirely
56+ if ( pageContext . tabs . length === 0 ) {
57+ return ''
58+ }
5059
60+ // No verbose announcements - just provide the content cleanly
5161 if ( pageContext . isSingleTab ) {
52- return generateSingleTabContext ( pageContext . tabs [ 0 ] , prefix )
62+ return generateSingleTabContext ( pageContext . tabs [ 0 ] )
5363 } else {
54- return generateMultiTabContext ( pageContext . tabs , prefix )
64+ return generateMultiTabContext ( pageContext . tabs )
5565 }
5666}
5767
5868/**
5969 * Generate context message for single tab
6070 */
61- function generateSingleTabContext ( tab : ExtractedPageContext [ 'tabs' ] [ 0 ] , prefix : string ) : string {
62- return `${ prefix }
63-
64- **Page: ${ tab . title } **
71+ function generateSingleTabContext ( tab : ExtractedPageContext [ 'tabs' ] [ 0 ] ) : string {
72+ return `<browser-state>
73+ Page: ${ tab . title }
6574URL: ${ tab . url }
6675
67- ## Content:
68- ${ tab . text } `
76+ ${ tab . text }
77+ </browser-state>
78+
79+ IMPORTANT: The user has selected this page as context. You must ONLY answer questions based on the content above. If a question cannot be answered using this page's content, politely inform the user that the information is not available on this page. Do not use your general knowledge to answer questions unrelated to this page.`
6980}
7081
7182/**
7283 * Generate context message for multiple tabs
7384 */
74- function generateMultiTabContext ( tabs : ExtractedPageContext [ 'tabs' ] , prefix : string ) : string {
85+ function generateMultiTabContext ( tabs : ExtractedPageContext [ 'tabs' ] ) : string {
7586 const tabSections = tabs . map ( ( tab , index ) => `
76- ** Tab ${ index + 1 } : ${ tab . title } **
87+ Tab ${ index + 1 } : ${ tab . title }
7788URL: ${ tab . url }
7889
7990${ tab . text } `) . join ( '\n\n---\n' )
8091
81- return `${ prefix }
92+ return `<browser-state>
93+ ${ tabSections }
94+ </browser-state>
8295
83- I'm analyzing ${ tabs . length } tabs:
84-
85- ${ tabSections } `
96+ IMPORTANT: The user has selected these ${ tabs . length } pages as context. You must ONLY answer questions based on the content above. If a question cannot be answered using these pages' content, politely inform the user that the information is not available on the selected pages. Do not use your general knowledge to answer questions unrelated to these pages.`
8697}
8798
8899/**
89- * Generate task prompt that wraps the user's query
90- * This tells the LLM to refer to the BrowserState content
100+ * Generate task prompt for the user's query
101+ * Simply returns the query without wrapper - the agent naturally uses available context
91102 */
92103export function generateTaskPrompt ( query : string , contextJustExtracted : boolean ) : string {
93- if ( contextJustExtracted ) {
94- // Context was just extracted and added above
95- return `Based on the page content in the <browser-state> tags above, please answer the following question:
96-
97- "${ query } "`
98- } else {
99- // Context already exists from previous extraction
100- return `Using the page content from the <browser-state> tags, please answer:
101-
102- "${ query } "`
103- }
104+ // Return the query directly without any wrapper
105+ // The agent will naturally reference browser-state content when relevant
106+ return query
104107}
0 commit comments