toolinterceptor

package
v0.260224.1130 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAnthropicBetaToolResultBlock

func CreateAnthropicBetaToolResultBlock(toolUseID, content string, isError bool) anthropic.BetaContentBlockParamUnion

CreateAnthropicBetaToolResultBlock creates an Anthropic beta tool_result content block

func CreateAnthropicToolResultBlock

func CreateAnthropicToolResultBlock(toolUseID, content string, isError bool) anthropic.ContentBlockParamUnion

CreateAnthropicToolResultBlock creates an Anthropic tool_result content block

func FetchCacheKey

func FetchCacheKey(url string) string

FetchCacheKey generates a cache key for fetch URLs

func FormatSearchResults

func FormatSearchResults(results []SearchResult) string

FormatResults formats search results as a JSON string for tool response

func IsFetchTool

func IsFetchTool(toolName string) bool

IsFetchTool checks if a tool name is a fetch tool alias

func IsSearchTool

func IsSearchTool(toolName string) bool

IsSearchTool checks if a tool name is a search tool alias

func SearchCacheKey

func SearchCacheKey(query string) string

SearchCacheKey generates a cache key for search queries

func ShouldInterceptTool

func ShouldInterceptTool(toolName string) bool

ShouldInterceptTool checks if a tool should be intercepted based on its name

func ShouldStripToolChoice

func ShouldStripToolChoice(req *openai.ChatCompletionNewParams) bool

ShouldStripToolChoice checks if tool_choice should be stripped (only contains search/fetch tools)

func StripSearchFetchToolsAnthropic

func StripSearchFetchToolsAnthropic(tools []anthropic.ToolUnionParam) []anthropic.ToolUnionParam

StripSearchFetchToolsAnthropic removes search/fetch tool definitions from Anthropic tools array

func StripSearchFetchToolsAnthropicBeta

func StripSearchFetchToolsAnthropicBeta(tools []anthropic.BetaToolUnionParam) []anthropic.BetaToolUnionParam

StripSearchFetchToolsAnthropicBeta removes search/fetch tool definitions from Anthropic beta tools array

func StripSearchFetchToolsOpenAI

func StripSearchFetchToolsOpenAI(req *openai.ChatCompletionNewParams) *openai.ChatCompletionNewParams

StripSearchFetchToolsOpenAI removes search/fetch tool definitions from OpenAI tools array. This is used when local interception is disabled but the provider doesn't support tools.

Types

type BraveSearchResponse

type BraveSearchResponse struct {
	Query struct {
		Original string `json:"original"`
	} `json:"query"`
	Web struct {
		Results []struct {
			Title       string `json:"title"`
			URL         string `json:"url"`
			Description string `json:"description"`
		} `json:"results"`
	} `json:"web"`
}

BraveSearchResponse represents the Brave Search API response

type Cache

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

Cache implements an in-memory cache for search and fetch results

func NewCache

func NewCache() *Cache

NewCache creates a new cache instance

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all entries from the cache

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get retrieves a cached entry if it exists and hasn't expired

func (*Cache) Set

func (c *Cache) Set(key string, result interface{}, contentType string)

Set stores a value in the cache with expiration

func (*Cache) Size

func (c *Cache) Size() int

Size returns the current number of cache entries

type CacheEntry

type CacheEntry struct {
	Result      interface{} // SearchResult[] or string (for fetch)
	ExpiresAt   time.Time
	ContentType string // "search" or "fetch"
}

CacheEntry represents a cached search or fetch result

type Config

type Config struct {
	SearchAPI  string // "brave", "google", or "duckduckgo"
	SearchKey  string // API key for search service (not needed for duckduckgo)
	MaxResults int    // Max search results (default: 10)

	// Proxy configuration
	ProxyURL string // HTTP proxy URL (e.g., "http://127.0.0.1:7897")

	// Fetch configuration
	MaxFetchSize int64 // Max content size for fetch in bytes (default: 1MB)
	FetchTimeout int64 // Fetch timeout in seconds (default: 30)
	MaxURLLength int   // Max URL length (default: 2000)
}

Config represents the tool interceptor configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default configuration

type DuckDuckGoInstantAnswerAPIResponse

type DuckDuckGoInstantAnswerAPIResponse struct {
	Abstract       string `json:"Abstract"`
	AbstractText   string `json:"AbstractText"`
	AbstractSource string `json:"AbstractSource"`
	AbstractURL    string `json:"AbstractURL"`
	Image          string `json:"Image"`
	Heading        string `json:"Heading"`
	Answer         string `json:"Answer"`
	AnswerType     string `json:"AnswerType"`
	Definition     string `json:"Definition"`
	DefinitionURL  string `json:"DefinitionURL"`
	RelatedTopics  []struct {
		Text     string `json:"Text"`
		FirstURL string `json:"FirstURL"`
	} `json:"RelatedTopics"`
	Results []struct {
		Text     string `json:"Text"`
		FirstURL string `json:"FirstURL"`
	} `json:"Results"`
	// Infobox can be either an object or empty string, so we use interface{}
	Infobox interface{} `json:"Infobox"`
}

DuckDuckGoInstantAnswerAPIResponse represents the DuckDuckGo Instant Answer API response

type FetchHandler

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

FetchHandler handles web content fetching and extraction

func NewFetchHandler

func NewFetchHandler(cache *Cache) *FetchHandler

NewFetchHandler creates a new fetch handler

func NewFetchHandlerWithConfig

func NewFetchHandlerWithConfig(cache *Cache, config *Config) *FetchHandler

NewFetchHandlerWithConfig creates a new fetch handler with custom configuration

func (*FetchHandler) FetchAndExtract

func (h *FetchHandler) FetchAndExtract(targetURL string) (string, error)

FetchAndExtract fetches a URL and extracts the main content

type FetchRequest

type FetchRequest struct {
	URL string `json:"url"`
}

FetchRequest represents a fetch tool call parameters

type HandlerType

type HandlerType string

HandlerType represents the type of tool handler

const (
	HandlerTypeSearch HandlerType = "internal_search"
	HandlerTypeFetch  HandlerType = "internal_fetch"
	HandlerTypeNone   HandlerType = ""
)

func MatchToolAlias

func MatchToolAlias(toolName string) (HandlerType, bool)

MatchToolAlias checks if a tool name matches any known alias and returns the handler type

type Interceptor

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

Interceptor handles tool interception and execution

func NewInterceptor

func NewInterceptor(globalConfig *typ.ToolInterceptorConfig) *Interceptor

NewInterceptor creates a new tool interceptor with global configuration

func (*Interceptor) ExecuteTool

func (i *Interceptor) ExecuteTool(provider *typ.Provider, toolName string, argsJSON string) ToolResult

ExecuteTool executes a tool by name with JSON arguments (public method for server use)

func (*Interceptor) FetchAndExtract

func (i *Interceptor) FetchAndExtract(url string) (string, error)

FetchAndExtract fetches a URL and extracts the main content

func (*Interceptor) GetConfigForProvider

func (i *Interceptor) GetConfigForProvider(provider *typ.Provider) *typ.ToolInterceptorConfig

GetConfigForProvider returns the effective config for a specific provider

func (*Interceptor) InterceptAnthropicBetaRequest

func (i *Interceptor) InterceptAnthropicBetaRequest(provider *typ.Provider, req *anthropic.BetaMessageNewParams) (intercepted bool, results []ToolResult, modifiedTools []anthropic.BetaToolUnionParam)

InterceptAnthropicBetaRequest intercepts tool calls in an Anthropic beta request

func (*Interceptor) InterceptAnthropicRequest

func (i *Interceptor) InterceptAnthropicRequest(provider *typ.Provider, req *anthropic.MessageNewParams) (intercepted bool, results []ToolResult, modifiedTools []anthropic.ToolUnionParam)

InterceptAnthropicRequest intercepts tool calls in an Anthropic request

func (*Interceptor) InterceptOpenAIRequest

func (i *Interceptor) InterceptOpenAIRequest(provider *typ.Provider, req *openai.ChatCompletionNewParams) (intercepted bool, results []ToolResult, modifiedTools []openai.ChatCompletionToolUnionParam)

InterceptOpenAIRequest intercepts tool calls in an OpenAI request Returns: - intercepted: true if any tools were intercepted - results: tool results to inject back - modifiedTools: tools that were not intercepted (to forward to provider)

func (*Interceptor) IsEnabledForProvider

func (i *Interceptor) IsEnabledForProvider(provider *typ.Provider) bool

IsEnabledForProvider checks if interceptor is enabled for a specific provider

func (*Interceptor) PrepareAnthropicBetaRequest

func (i *Interceptor) PrepareAnthropicBetaRequest(provider *typ.Provider, originalReq *anthropic.BetaMessageNewParams) (modifiedReq *anthropic.BetaMessageNewParams, hasPreInjectedResults bool)

PrepareAnthropicBetaRequest pre-processes an Anthropic beta request before sending to provider Returns: - modifiedReq: the request with tools stripped and pre-injected results - hasPreInjectedResults: whether tool results were injected

func (*Interceptor) PrepareAnthropicRequest

func (i *Interceptor) PrepareAnthropicRequest(provider *typ.Provider, originalReq *anthropic.MessageNewParams) (modifiedReq *anthropic.MessageNewParams, hasPreInjectedResults bool)

PrepareAnthropicRequest pre-processes an Anthropic request before sending to provider Returns: - modifiedReq: the request with tools stripped and pre-injected results - hasPreInjectedResults: whether tool results were injected

func (*Interceptor) PrepareOpenAIRequest

func (i *Interceptor) PrepareOpenAIRequest(provider *typ.Provider, originalReq *openai.ChatCompletionNewParams) (modifiedReq *openai.ChatCompletionNewParams, hasPreInjectedResults bool)

PrepareOpenAIRequest pre-processes an OpenAI request before sending to provider Returns: - modifiedReq: the request with tools stripped and pre-injected results - hasPreInjectedResults: whether tool results were injected

func (*Interceptor) SearchWithConfig

func (i *Interceptor) SearchWithConfig(query string, count int, config *Config) ([]SearchResult, error)

SearchWithConfig executes a search with the given configuration

type SearchHandler

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

SearchHandler handles web search requests

func NewSearchHandler

func NewSearchHandler(config *Config, cache *Cache) *SearchHandler

NewSearchHandler creates a new search handler

func (*SearchHandler) Search

func (h *SearchHandler) Search(query string, count int) ([]SearchResult, error)

Search executes a web search query using the handler's configured API

func (*SearchHandler) SearchWithConfig

func (h *SearchHandler) SearchWithConfig(query string, count int, config *Config) ([]SearchResult, error)

SearchWithConfig executes a web search query using a specific config This allows provider-specific overrides at runtime

type SearchRequest

type SearchRequest struct {
	Query string `json:"query"`
	Count int    `json:"count,omitempty"` // Number of results to return
}

SearchRequest represents a search tool call parameters

type SearchResult

type SearchResult struct {
	Title   string `json:"title"`
	URL     string `json:"url"`
	Snippet string `json:"snippet"`
}

SearchResult represents a single search result from the search API

type ToolResult

type ToolResult struct {
	ToolCallID string // OpenAI: tool_call_id, Anthropic: tool_use_id
	Content    string // JSON string or plain text result
	Error      string // Error message if execution failed
	IsError    bool   // True if the result is an error
}

ToolResult represents the result of a tool execution

Jump to

Keyboard shortcuts

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