browser.go 22.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//go:build windows || darwin

package tools

import (
	"context"
	"fmt"
	"net/url"
	"regexp"
	"strings"
	"sync"
	"time"

	"github.com/ollama/ollama/app/ui/responses"
)

type PageType string

const (
	PageTypeSearchResults PageType = "initial_results"
	PageTypeWebpage       PageType = "webpage"
)

// DefaultViewTokens is the number of tokens to show to the model used when calling displayPage
const DefaultViewTokens = 1024

/*
The Browser tool provides web browsing capability for gpt-oss.
The model uses the tool by usually doing a search first and then choosing to either open a page,
find a term in a page, or do another search.

The tool optionally may open a URL directly - especially if one is passed in.

Each action is saved into an append-only page stack `responses.BrowserStateData` to keep
track of the history of the browsing session.

Each `Execute()` for a tool returns the full current state of the browser. ui.go manages the
browser state representation between the tool, ui, and db.

A new Browser object is created per request - the state is reconstructed by ui.go.
The initialization of the browser will receive a `responses.BrowserStateData` with the stitched history.
*/

// BrowserState manages the browsing session on a per-chat basis
type BrowserState struct {
	mu   sync.RWMutex
	Data *responses.BrowserStateData
}
type Browser struct {
	state *BrowserState
}

// State is only accessed in a single thread, as each chat has its own browser state
func (b *Browser) State() *responses.BrowserStateData {
	b.state.mu.RLock()
	defer b.state.mu.RUnlock()
	return b.state.Data
}

func (b *Browser) savePage(page *responses.Page) {
	b.state.Data.URLToPage[page.URL] = page
	b.state.Data.PageStack = append(b.state.Data.PageStack, page.URL)
}

func (b *Browser) getPageFromStack(url string) (*responses.Page, error) {
	page, ok := b.state.Data.URLToPage[url]
	if !ok {
		return nil, fmt.Errorf("page not found for url %s", url)
	}
	return page, nil
}

func NewBrowser(state *responses.BrowserStateData) *Browser {
	if state == nil {
		state = &responses.BrowserStateData{
			PageStack:  []string{},
			ViewTokens: DefaultViewTokens,
			URLToPage:  make(map[string]*responses.Page),
		}
	}
	b := &BrowserState{
		Data: state,
	}

	return &Browser{
		state: b,
	}
}

type BrowserSearch struct {
	Browser
	webSearch *BrowserWebSearch
}

// NewBrowserSearch creates a new browser search instance
func NewBrowserSearch(bb *Browser) *BrowserSearch {
	if bb == nil {
		bb = &Browser{
			state: &BrowserState{
				Data: &responses.BrowserStateData{
					PageStack:  []string{},
					ViewTokens: DefaultViewTokens,
					URLToPage:  make(map[string]*responses.Page),
				},
			},
		}
	}
	return &BrowserSearch{
		Browser:   *bb,
		webSearch: &BrowserWebSearch{},
	}
}

func (b *BrowserSearch) Name() string {
	return "browser.search"
}

func (b *BrowserSearch) Description() string {
	return "Search the web for information"
}

func (b *BrowserSearch) Prompt() string {
	return ""
}

func (b *BrowserSearch) Schema() map[string]any {
	return map[string]any{}
}

func (b *BrowserSearch) Execute(ctx context.Context, args map[string]any) (any, string, error) {
	query, ok := args["query"].(string)
	if !ok {
		return nil, "", fmt.Errorf("query parameter is required")
	}

	topn, ok := args["topn"].(int)
	if !ok {
		topn = 5
	}

	searchArgs := map[string]any{
		"queries":     []any{query},
		"max_results": topn,
	}

	result, err := b.webSearch.Execute(ctx, searchArgs)
	if err != nil {
		return nil, "", fmt.Errorf("search error: %w", err)
	}

	searchResponse, ok := result.(*WebSearchResponse)
	if !ok {
		return nil, "", fmt.Errorf("invalid search results format")
	}

	// Build main search results page that contains all search results
	searchResultsPage := b.buildSearchResultsPageCollection(query, searchResponse)
	b.savePage(searchResultsPage)
	cursor := len(b.state.Data.PageStack) - 1
	// cache result for each page
	for _, queryResults := range searchResponse.Results {
		for i, result := range queryResults {
			resultPage := b.buildSearchResultsPage(&result, i+1)
			// save to global only, do not add to visited stack
			b.state.Data.URLToPage[resultPage.URL] = resultPage
		}
	}

	page := searchResultsPage

	pageText, err := b.displayPage(page, cursor, 0, -1)
	if err != nil {
		return nil, "", fmt.Errorf("failed to display page: %w", err)
	}

	return b.state.Data, pageText, nil
}

func (b *Browser) buildSearchResultsPageCollection(query string, results *WebSearchResponse) *responses.Page {
	page := &responses.Page{
		URL:       "search_results_" + query,
		Title:     query,
		Links:     make(map[int]string),
		FetchedAt: time.Now(),
	}

	var textBuilder strings.Builder
	linkIdx := 0

	// Add the header lines to match format
	textBuilder.WriteString("\n")                 // L0: empty
	textBuilder.WriteString("URL: \n")            // L1: URL: (empty for search)
	textBuilder.WriteString("# Search Results\n") // L2: # Search Results
	textBuilder.WriteString("\n")                 // L3: empty

	for _, queryResults := range results.Results {
		for _, result := range queryResults {
			domain := result.URL
			if u, err := url.Parse(result.URL); err == nil && u.Host != "" {
				domain = u.Host
				domain = strings.TrimPrefix(domain, "www.")
			}

			linkFormat := fmt.Sprintf("* 【%d†%s†%s】", linkIdx, result.Title, domain)
			textBuilder.WriteString(linkFormat)

			numChars := min(len(result.Content.FullText), 400)
			snippet := strings.TrimSpace(result.Content.FullText[:numChars])
			textBuilder.WriteString(snippet)
			textBuilder.WriteString("\n")

			page.Links[linkIdx] = result.URL
			linkIdx++
		}
	}

	page.Text = textBuilder.String()
	page.Lines = wrapLines(page.Text, 80)

	return page
}

func (b *Browser) buildSearchResultsPage(result *WebSearchResult, linkIdx int) *responses.Page {
	page := &responses.Page{
		URL:       result.URL,
		Title:     result.Title,
		Links:     make(map[int]string),
		FetchedAt: time.Now(),
	}

	var textBuilder strings.Builder

	// Format the individual result page (only used when no full text is available)
	linkFormat := fmt.Sprintf("【%d†%s】", linkIdx, result.Title)
	textBuilder.WriteString(linkFormat)
	textBuilder.WriteString("\n")
	textBuilder.WriteString(fmt.Sprintf("URL: %s\n", result.URL))
	numChars := min(len(result.Content.FullText), 300)
	textBuilder.WriteString(result.Content.FullText[:numChars])
	textBuilder.WriteString("\n\n")

	// Only store link and snippet if we won't be processing full text later
	// (full text processing will handle all links consistently)
	if result.Content.FullText == "" {
		page.Links[linkIdx] = result.URL
	}

	// Use full text if available, otherwise use snippet
	if result.Content.FullText != "" {
		// Prepend the URL line to the full text
		page.Text = fmt.Sprintf("URL: %s\n%s", result.URL, result.Content.FullText)
		// Process markdown links in the full text
		processedText, processedLinks := processMarkdownLinks(page.Text)
		page.Text = processedText
		page.Links = processedLinks
	} else {
		page.Text = textBuilder.String()
	}

	page.Lines = wrapLines(page.Text, 80)

	return page
}

// getEndLoc calculates the end location for viewport based on token limits
func (b *Browser) getEndLoc(loc, numLines, totalLines int, lines []string) int {
	if numLines <= 0 {
		// Auto-calculate based on viewTokens
		txt := b.joinLinesWithNumbers(lines[loc:])

		// If text is very short, no need to truncate (at least 1 char per token)
		if len(txt) > b.state.Data.ViewTokens {
			// Simple heuristic: approximate token counting
			// Typical token is ~4 characters, but can be up to 128 chars
			maxCharsPerToken := 128

			// upper bound for text to analyze
			upperBound := min((b.state.Data.ViewTokens+1)*maxCharsPerToken, len(txt))
			textToAnalyze := txt[:upperBound]

			// Simple approximation: count tokens as ~4 chars each
			// This is less accurate than tiktoken but more performant
			approxTokens := len(textToAnalyze) / 4

			if approxTokens > b.state.Data.ViewTokens {
				// Find the character position at viewTokens
				endIdx := min(b.state.Data.ViewTokens*4, len(txt))

				// Count newlines up to that position to get line count
				numLines = strings.Count(txt[:endIdx], "\n") + 1
			} else {
				numLines = totalLines
			}
		} else {
			numLines = totalLines
		}
	}

	return min(loc+numLines, totalLines)
}

// joinLinesWithNumbers creates a string with line numbers, matching Python's join_lines
func (b *Browser) joinLinesWithNumbers(lines []string) string {
	var builder strings.Builder
	var hadZeroLine bool
	for i, line := range lines {
		if i == 0 {
			builder.WriteString("L0:\n")
			hadZeroLine = true
		}
		if hadZeroLine {
			builder.WriteString(fmt.Sprintf("L%d: %s\n", i+1, line))
		} else {
			builder.WriteString(fmt.Sprintf("L%d: %s\n", i, line))
		}
	}
	return builder.String()
}

// processMarkdownLinks finds all markdown links in the text and replaces them with the special format
// Returns the processed text and a map of link IDs to URLs
func processMarkdownLinks(text string) (string, map[int]string) {
	links := make(map[int]string)

	// Always start from 0 for consistent numbering across all pages
	linkID := 0

	// First, handle multi-line markdown links by joining them
	// This regex finds markdown links that might be split across lines
	multiLinePattern := regexp.MustCompile(`\[([^\]]+)\]\s*\n\s*\(([^)]+)\)`)
	text = multiLinePattern.ReplaceAllStringFunc(text, func(match string) string {
		// Replace newlines with spaces in the match
		cleaned := strings.ReplaceAll(match, "\n", " ")
		// Remove extra spaces
		cleaned = regexp.MustCompile(`\s+`).ReplaceAllString(cleaned, " ")
		return cleaned
	})

	// Now process all markdown links (including the cleaned multi-line ones)
	linkPattern := regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`)

	processedText := linkPattern.ReplaceAllStringFunc(text, func(match string) string {
		matches := linkPattern.FindStringSubmatch(match)
		if len(matches) != 3 {
			return match
		}

		linkText := strings.TrimSpace(matches[1])
		linkURL := strings.TrimSpace(matches[2])

		// Extract domain from URL
		domain := linkURL
		if u, err := url.Parse(linkURL); err == nil && u.Host != "" {
			domain = u.Host
			// Remove www. prefix if present
			domain = strings.TrimPrefix(domain, "www.")
		}

		// Create the formatted link
		formatted := fmt.Sprintf("【%d†%s†%s】", linkID, linkText, domain)

		// Store the link
		links[linkID] = linkURL
		linkID++

		return formatted
	})

	return processedText, links
}

func wrapLines(text string, width int) []string {
	if width <= 0 {
		width = 80
	}

	lines := strings.Split(text, "\n")
	var wrapped []string

	for _, line := range lines {
		if line == "" {
			// Preserve empty lines
			wrapped = append(wrapped, "")
		} else if len(line) <= width {
			wrapped = append(wrapped, line)
		} else {
			// Word wrapping while preserving whitespace structure
			words := strings.Fields(line)
			if len(words) == 0 {
				// Line with only whitespace
				wrapped = append(wrapped, line)
				continue
			}

			currentLine := ""
			for _, word := range words {
				// Check if adding this word would exceed width
				testLine := currentLine
				if testLine != "" {
					testLine += " "
				}
				testLine += word

				if len(testLine) > width && currentLine != "" {
					// Current line would be too long, wrap it
					wrapped = append(wrapped, currentLine)
					currentLine = word
				} else {
					// Add word to current line
					if currentLine != "" {
						currentLine += " "
					}
					currentLine += word
				}
			}

			// Add any remaining content
			if currentLine != "" {
				wrapped = append(wrapped, currentLine)
			}
		}
	}

	return wrapped
}

// displayPage formats and returns the page display for the model
func (b *Browser) displayPage(page *responses.Page, cursor, loc, numLines int) (string, error) {
	totalLines := len(page.Lines)

	if loc >= totalLines {
		return "", fmt.Errorf("invalid location: %d (max: %d)", loc, totalLines-1)
	}

	// get viewport end location
	endLoc := b.getEndLoc(loc, numLines, totalLines, page.Lines)

	var displayBuilder strings.Builder
	displayBuilder.WriteString(fmt.Sprintf("[%d] %s", cursor, page.Title))
	if page.URL != "" {
		displayBuilder.WriteString(fmt.Sprintf("(%s)\n", page.URL))
	} else {
		displayBuilder.WriteString("\n")
	}
	displayBuilder.WriteString(fmt.Sprintf("**viewing lines [%d - %d] of %d**\n\n", loc, endLoc-1, totalLines-1))

	// Content with line numbers
	var hadZeroLine bool
	for i := loc; i < endLoc; i++ {
		if i == 0 {
			displayBuilder.WriteString("L0:\n")
			hadZeroLine = true
		}
		if hadZeroLine {
			displayBuilder.WriteString(fmt.Sprintf("L%d: %s\n", i+1, page.Lines[i]))
		} else {
			displayBuilder.WriteString(fmt.Sprintf("L%d: %s\n", i, page.Lines[i]))
		}
	}

	return displayBuilder.String(), nil
}

type BrowserOpen struct {
	Browser
	crawlPage *BrowserCrawler
}

func NewBrowserOpen(bb *Browser) *BrowserOpen {
	if bb == nil {
		bb = &Browser{
			state: &BrowserState{
				Data: &responses.BrowserStateData{
					PageStack:  []string{},
					ViewTokens: DefaultViewTokens,
					URLToPage:  make(map[string]*responses.Page),
				},
			},
		}
	}
	return &BrowserOpen{
		Browser:   *bb,
		crawlPage: &BrowserCrawler{},
	}
}

func (b *BrowserOpen) Name() string {
	return "browser.open"
}

func (b *BrowserOpen) Description() string {
	return "Open a link in the browser"
}

func (b *BrowserOpen) Prompt() string {
	return ""
}

func (b *BrowserOpen) Schema() map[string]any {
	return map[string]any{}
}

func (b *BrowserOpen) Execute(ctx context.Context, args map[string]any) (any, string, error) {
	// Get cursor parameter first
	cursor := -1
	if c, ok := args["cursor"].(float64); ok {
		cursor = int(c)
	} else if c, ok := args["cursor"].(int); ok {
		cursor = c
	}

	// Get loc parameter
	loc := 0
	if l, ok := args["loc"].(float64); ok {
		loc = int(l)
	} else if l, ok := args["loc"].(int); ok {
		loc = l
	}

	// Get num_lines parameter
	numLines := -1
	if n, ok := args["num_lines"].(float64); ok {
		numLines = int(n)
	} else if n, ok := args["num_lines"].(int); ok {
		numLines = n
	}

	// get page from cursor
	var page *responses.Page
	if cursor >= 0 {
		if cursor >= len(b.state.Data.PageStack) {
			return nil, "", fmt.Errorf("cursor %d is out of range (pageStack length: %d)", cursor, len(b.state.Data.PageStack))
		}
		var err error
		page, err = b.getPageFromStack(b.state.Data.PageStack[cursor])
		if err != nil {
			return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
		}
	} else {
		// get last page
		if len(b.state.Data.PageStack) != 0 {
			pageURL := b.state.Data.PageStack[len(b.state.Data.PageStack)-1]
			var err error
			page, err = b.getPageFromStack(pageURL)
			if err != nil {
				return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
			}
		}
	}

	// Try to get id as string (URL) first
	if url, ok := args["id"].(string); ok {
		// Check if we already have this page cached
		if existingPage, ok := b.state.Data.URLToPage[url]; ok {
			// Use cached page
			b.savePage(existingPage)
			// Always update cursor to point to the newly added page
			cursor = len(b.state.Data.PageStack) - 1
			pageText, err := b.displayPage(existingPage, cursor, loc, numLines)
			if err != nil {
				return nil, "", fmt.Errorf("failed to display page: %w", err)
			}
			return b.state.Data, pageText, nil
		}

		// Page not in cache, need to crawl it
		if b.crawlPage == nil {
			b.crawlPage = &BrowserCrawler{}
		}
		crawlResponse, err := b.crawlPage.Execute(ctx, map[string]any{
			"urls":   []any{url},
			"latest": false,
		})
		if err != nil {
			return nil, "", fmt.Errorf("failed to crawl URL %s: %w", url, err)
		}

		newPage, err := b.buildPageFromCrawlResult(url, crawlResponse)
		if err != nil {
			return nil, "", fmt.Errorf("failed to build page from crawl result: %w", err)
		}

		// Need to fall through if first search is directly an open command - no existing page
		b.savePage(newPage)
		// Always update cursor to point to the newly added page
		cursor = len(b.state.Data.PageStack) - 1
		pageText, err := b.displayPage(newPage, cursor, loc, numLines)
		if err != nil {
			return nil, "", fmt.Errorf("failed to display page: %w", err)
		}
		return b.state.Data, pageText, nil
	}

	// Try to get id as integer (link ID from current page)
	if id, ok := args["id"].(float64); ok {
		if page == nil {
			return nil, "", fmt.Errorf("no current page to resolve link from")
		}
		idInt := int(id)
		pageURL, ok := page.Links[idInt]
		if !ok {
			return nil, "", fmt.Errorf("invalid link id %d", idInt)
		}

		// Check if we have the linked page cached
		newPage, ok := b.state.Data.URLToPage[pageURL]
		if !ok {
			if b.crawlPage == nil {
				b.crawlPage = &BrowserCrawler{}
			}
			crawlResponse, err := b.crawlPage.Execute(ctx, map[string]any{
				"urls":   []any{pageURL},
				"latest": false,
			})
			if err != nil {
				return nil, "", fmt.Errorf("failed to crawl URL %s: %w", pageURL, err)
			}

			// Create new page from crawl result
			newPage, err = b.buildPageFromCrawlResult(pageURL, crawlResponse)
			if err != nil {
				return nil, "", fmt.Errorf("failed to build page from crawl result: %w", err)
			}
		}

		// Add to history stack regardless of cache status
		b.savePage(newPage)

		// Always update cursor to point to the newly added page
		cursor = len(b.state.Data.PageStack) - 1
		pageText, err := b.displayPage(newPage, cursor, loc, numLines)
		if err != nil {
			return nil, "", fmt.Errorf("failed to display page: %w", err)
		}
		return b.state.Data, pageText, nil
	}

	// If no id provided, just display current page
	if page == nil {
		return nil, "", fmt.Errorf("no current page to display")
	}
	// Only add to PageStack without updating URLToPage
	b.state.Data.PageStack = append(b.state.Data.PageStack, page.URL)
	cursor = len(b.state.Data.PageStack) - 1

	pageText, err := b.displayPage(page, cursor, loc, numLines)
	if err != nil {
		return nil, "", fmt.Errorf("failed to display page: %w", err)
	}
	return b.state.Data, pageText, nil
}

// buildPageFromCrawlResult creates a Page from crawl API results
func (b *Browser) buildPageFromCrawlResult(requestedURL string, crawlResponse *CrawlResponse) (*responses.Page, error) {
	// Initialize page with defaults
	page := &responses.Page{
		URL:       requestedURL,
		Title:     requestedURL,
		Text:      "",
		Links:     make(map[int]string),
		FetchedAt: time.Now(),
	}

	// Process crawl results - the API returns results grouped by URL
	for url, urlResults := range crawlResponse.Results {
		if len(urlResults) > 0 {
			// Get the first result for this URL
			result := urlResults[0]

			// Extract content
			if result.Content.FullText != "" {
				page.Text = result.Content.FullText
			}

			// Extract title if available
			if result.Title != "" {
				page.Title = result.Title
			}

			// Update URL to the actual URL from results
			page.URL = url

			// Extract links if available from extras
			for i, link := range result.Extras.Links {
				if link.Href != "" {
					page.Links[i] = link.Href
				} else if link.URL != "" {
					page.Links[i] = link.URL
				}
			}

			// Only process the first URL's results
			break
		}
	}

	// If no text was extracted, set a default message
	if page.Text == "" {
		page.Text = "No content could be extracted from this page."
	} else {
		// Prepend the URL line to match Python implementation
		page.Text = fmt.Sprintf("URL: %s\n%s", page.URL, page.Text)
	}

	// Process markdown links in the text
	processedText, processedLinks := processMarkdownLinks(page.Text)
	page.Text = processedText
	page.Links = processedLinks

	// Wrap lines for display
	page.Lines = wrapLines(page.Text, 80)

	return page, nil
}

type BrowserFind struct {
	Browser
}

func NewBrowserFind(bb *Browser) *BrowserFind {
	return &BrowserFind{
		Browser: *bb,
	}
}

func (b *BrowserFind) Name() string {
	return "browser.find"
}

func (b *BrowserFind) Description() string {
	return "Find a term in the browser"
}

func (b *BrowserFind) Prompt() string {
	return ""
}

func (b *BrowserFind) Schema() map[string]any {
	return map[string]any{}
}

func (b *BrowserFind) Execute(ctx context.Context, args map[string]any) (any, string, error) {
	pattern, ok := args["pattern"].(string)
	if !ok {
		return nil, "", fmt.Errorf("pattern parameter is required")
	}

	// Get cursor parameter if provided, default to current page
	cursor := -1
	if c, ok := args["cursor"].(float64); ok {
		cursor = int(c)
	}

	// Get the page to search in
	var page *responses.Page
	if cursor == -1 {
		// Use current page
		if len(b.state.Data.PageStack) == 0 {
			return nil, "", fmt.Errorf("no pages to search in")
		}
		var err error
		page, err = b.getPageFromStack(b.state.Data.PageStack[len(b.state.Data.PageStack)-1])
		if err != nil {
			return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
		}
	} else {
		// Use specific cursor
		if cursor < 0 || cursor >= len(b.state.Data.PageStack) {
			return nil, "", fmt.Errorf("cursor %d is out of range [0-%d]", cursor, len(b.state.Data.PageStack)-1)
		}
		var err error
		page, err = b.getPageFromStack(b.state.Data.PageStack[cursor])
		if err != nil {
			return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
		}
	}

	if page == nil {
		return nil, "", fmt.Errorf("page not found")
	}

	// Create find results page
	findPage := b.buildFindResultsPage(pattern, page)

	// Add the find results page to state
	b.savePage(findPage)
	newCursor := len(b.state.Data.PageStack) - 1

	pageText, err := b.displayPage(findPage, newCursor, 0, -1)
	if err != nil {
		return nil, "", fmt.Errorf("failed to display page: %w", err)
	}

	return b.state.Data, pageText, nil
}

func (b *Browser) buildFindResultsPage(pattern string, page *responses.Page) *responses.Page {
	findPage := &responses.Page{
		Title:     fmt.Sprintf("Find results for text: `%s` in `%s`", pattern, page.Title),
		Links:     make(map[int]string),
		FetchedAt: time.Now(),
	}

	findPage.URL = fmt.Sprintf("find_results_%s", pattern)

	var textBuilder strings.Builder
	matchIdx := 0
	maxResults := 50
	numShowLines := 4
	patternLower := strings.ToLower(pattern)

	// Search through the page lines following the reference algorithm
	var resultChunks []string
	lineIdx := 0

	for lineIdx < len(page.Lines) {
		line := page.Lines[lineIdx]
		lineLower := strings.ToLower(line)

		if !strings.Contains(lineLower, patternLower) {
			lineIdx++
			continue
		}

		// Build snippet context
		endLine := min(lineIdx+numShowLines, len(page.Lines))

		var snippetBuilder strings.Builder
		for j := lineIdx; j < endLine; j++ {
			snippetBuilder.WriteString(page.Lines[j])
			if j < endLine-1 {
				snippetBuilder.WriteString("\n")
			}
		}
		snippet := snippetBuilder.String()

		// Format the match
		linkFormat := fmt.Sprintf("【%d†match at L%d】", matchIdx, lineIdx)
		resultChunk := fmt.Sprintf("%s\n%s", linkFormat, snippet)
		resultChunks = append(resultChunks, resultChunk)

		if len(resultChunks) >= maxResults {
			break
		}

		matchIdx++
		lineIdx += numShowLines
	}

	// Build final display text
	if len(resultChunks) > 0 {
		textBuilder.WriteString(strings.Join(resultChunks, "\n\n"))
	}

	if matchIdx == 0 {
		findPage.Text = fmt.Sprintf("No `find` results for pattern: `%s`", pattern)
	} else {
		findPage.Text = textBuilder.String()
	}

	findPage.Lines = wrapLines(findPage.Text, 80)
	return findPage
}