webtool

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 19 Imported by: 0

README

webtool

webtool provides browser-backed web tools for the ds4 agent loop, matching the design of the upstream ds4 inference engine's browser helper (ds4_web.c).

It enables DeepSeek agent loops to search Google and visit web pages using a local Google Chrome or Chromium installation.

How it Works

  1. Auto-Detection: The package automatically searches standard locations on macOS and Linux for a Google Chrome or Chromium executable (or reads the DS4_CHROME environment variable).
  2. Process Management: It starts a visible Google Chrome process in the background configured with remote debugging enabled (--remote-debugging-port) and a dedicated user data directory (~/.ds4/browser).
  3. CDP over Raw WebSockets: It connects to Chrome using the Chrome DevTools Protocol (CDP). To remain zero-dependency, it uses a lightweight, custom RFC 6455 WebSocket client implementation built on top of standard Go sockets.
  4. Markdown Extraction: It navigates to pages, automatically bypasses Google Search consent forms, triggers lazy-loaded content by scrolling, and runs JavaScript-based Markdown extractors to compile clean structured text snapshots of pages.

Usage

You can initialize webtool.NewWebHelper and register its tools (GoogleSearchTool() and VisitPageTool()) directly into a ds4.ToolRegistry:

package main

import (
	"context"
	"fmt"
	"log"
	"strings"

	"github.com/NimbleMarkets/ds4go"
	"github.com/NimbleMarkets/ds4go/webtool"
)

func main() {
	// Initialize the helper
	helper := webtool.NewWebHelper(webtool.Config{
		Port: 9333,
		ConfirmApproval: func(message string) (bool, error) {
			// Web tools require explicit user confirmation to launch Chrome for the first time
			fmt.Print(message)
			var input string
			_, err := fmt.Scanln(&input)
			if err != nil {
				return false, err
			}
			input = strings.ToLower(strings.TrimSpace(input))
			return input == "y" || input == "yes", nil
		},
		Log: func(msg string) {
			log.Printf("[Browser Log] %s\n", msg)
		},
	})
	// Keep the browser running across tool execution rounds, but clean up on shutdown
	defer helper.Close()

	// Register tools
	reg := ds4.NewToolRegistry()
	reg.MustRegister(helper.GoogleSearchTool())
	reg.MustRegister(helper.VisitPageTool())

	// Configure and run the ds4 ToolLoop...
}

Available Tools

The following tools are exposed to the DeepSeek model:

  • Description: Search Google in a visible browser and return compact Markdown links.
  • Arguments:
    {
      "query": "search query string"
    }
    
visit_page
  • Description: Open a URL in a visible browser and return rendered page Markdown.
  • Arguments:
    {
      "url": "https://example.com"
    }
    

Configuration Options

The webtool.Config struct accepts the following configuration fields:

Field Type Description
HomeDir string Directory containing user configuration. Defaults to $HOME, or . if unset. The Chrome user profile is stored at HomeDir + "/.ds4/browser".
Port int Remote debugging port to use for the Chrome session. Defaults to 9333.
ChromePath string Explicit path to the Chrome or Chromium executable. If empty, the helper will search standard paths.
ConfirmApproval func(string) (bool, error) Required. Callback invoked to request permission from the user before starting Chrome for the first time.
Log func(string) Optional logger callback for receiving status messages from the browser manager.
SearchProvider string Search engine to use: "google", "duckduckgo", "bing", "yahoo", "yandex", "baidu", or "brave". Defaults to "google".

Documentation

Index

Constants

View Source
const GoogleSearchParametersSchema = `{
	"type": "object",
	"properties": {
		"query": {
			"type": "string"
		}
	},
	"required": [
		"query"
	]
}`
View Source
const VisitPageParametersSchema = `{
	"type": "object",
	"properties": {
		"url": {
			"type": "string"
		}
	},
	"required": [
		"url"
	]
}`

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// HomeDir is the directory containing user configuration.
	// Defaults to $HOME, or "." if unset.
	HomeDir string
	// Port is the debugging port to run Chrome on. Defaults to 9333.
	Port int
	// ChromePath is the path to the Chrome or Chromium executable.
	// If empty, auto-detection searches standard paths.
	ChromePath string
	// ConfirmApproval is called to prompt the user before starting Chrome for the first time.
	// If nil, starting Chrome will fail with an error.
	ConfirmApproval func(message string) (bool, error)
	// Log receives status messages from the helper.
	Log func(message string)
	// SearchProvider specifies the search engine to use.
	// Supported values: "google" (default), "duckduckgo", "bing".
	SearchProvider string
}

Config configures the WebHelper.

type WebHelper

type WebHelper struct {
	// contains filtered or unexported fields
}

WebHelper implements browser-backed web tools using Chrome DevTools Protocol.

func NewWebHelper

func NewWebHelper(cfg Config) *WebHelper

NewWebHelper creates a new WebHelper.

func (*WebHelper) Close

func (w *WebHelper) Close() error

Close terminates the spawned Chrome browser if it was started by this helper.

func (*WebHelper) GoogleSearch

func (w *WebHelper) GoogleSearch(ctx context.Context, query string) (string, error)

GoogleSearch searches Google (or the default configured SearchProvider) and returns search result Markdown links.

func (*WebHelper) GoogleSearchTool

func (w *WebHelper) GoogleSearchTool() ds4.Tool

GoogleSearchTool returns the Tool schema and handler for Google Search.

func (*WebHelper) Search

func (w *WebHelper) Search(ctx context.Context, query string, provider string) (string, error)

Search executes a web search using the specified provider and returns Markdown links. Supported providers: "google", "duckduckgo", "bing". If provider is empty, the default configured SearchProvider is used (falling back to "google").

func (*WebHelper) VisitPage

func (w *WebHelper) VisitPage(ctx context.Context, targetURL string) (string, error)

VisitPage navigates to a URL, optionally scrolls, and extracts its text layout.

func (*WebHelper) VisitPageTool

func (w *WebHelper) VisitPageTool() ds4.Tool

VisitPageTool returns the Tool schema and handler for visiting web pages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL