utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package utils provides shared, reusable algorithms. This file implements a generic BM25 search engine.

Usage:

type MyDoc struct { ID string; Body string }

corpus := []MyDoc{...}
engine := bm25.New(corpus, func(d MyDoc) string {
    return d.ID + " " + d.Body
})
results := engine.Search("my query", 5)

Index

Constants

View Source
const (
	// DefaultBM25K1 is the term-frequency saturation factor (typical range 1.2–2.0).
	// Higher values give more weight to repeated terms.
	DefaultBM25K1 = 1.2

	// DefaultBM25B is the document-length normalization factor (0 = none, 1 = full).
	DefaultBM25B = 0.75
)
View Source
const ToolFeedbackContinuationHint = "Continuing the current task."

Variables

This section is empty.

Functions

func AllowConfiguredProxyFirstHop

func AllowConfiguredProxyFirstHop(req *http.Request, rt http.RoundTripper)

func AudioFormat

func AudioFormat(path string) (string, error)

func CalculateDefaultMaxContextRunes

func CalculateDefaultMaxContextRunes(contextWindow int) int

CalculateDefaultMaxContextRunes computes a default context limit based on the model's context window. Strategy: Use 75% of the context window and convert to rune estimate.

Token-to-rune conversion ratios (conservative estimates):

  • English: ~4 chars per token
  • Chinese: ~1.5-2 chars per token
  • Mixed: ~3 chars per token (used here for safety)

func CreateHTTPClient

func CreateHTTPClient(proxyURL string, timeout time.Duration) (*http.Client, error)

CreateHTTPClient creates an HTTP client with optional proxy support. If proxyURL is empty, it uses the system environment proxy settings. Supported proxy schemes: http, https, socks5, socks5h.

func CreateSafeHTTPClient

func CreateSafeHTTPClient(opts SafeHTTPClientOptions) (*http.Client, error)

func DerefStr

func DerefStr(s *string, fallback string) string

DerefStr dereferences a pointer to a string and returns the value or a fallback if the pointer is nil.

func DoRequestWithRetry

func DoRequestWithRetry(client *http.Client, req *http.Request) (*http.Response, error)

func DownloadFile

func DownloadFile(urlStr, filename string, opts DownloadOptions) string

DownloadFile downloads a file from URL to a local temp directory. Returns the local file path or empty string on error.

func DownloadFileSimple

func DownloadFileSimple(url, filename string) string

DownloadFileSimple is a simplified version of DownloadFile without options

func DownloadToFile

func DownloadToFile(ctx context.Context, client *http.Client, req *http.Request, maxBytes int64) (string, error)

DownloadToFile streams an HTTP response body to a temporary file in small chunks (~32KB), keeping peak memory usage constant regardless of file size.

Parameters:

  • ctx: context for cancellation/timeout
  • client: HTTP client to use (caller controls timeouts, transport, etc.)
  • req: fully prepared *http.Request (method, URL, headers, etc.)
  • maxBytes: maximum bytes to download; 0 means no limit

Returns the path to the temporary file. The caller is responsible for removing it when done (defer os.Remove(path)).

On any error the temp file is cleaned up automatically.

func ExtractZipFile

func ExtractZipFile(zipPath string, targetDir string) error

ExtractZipFile extracts a ZIP archive from disk to targetDir. It reads entries one at a time from disk, keeping memory usage minimal.

Security: rejects path traversal attempts and symlinks.

func FitToolFeedbackMessage

func FitToolFeedbackMessage(content string, maxLen int) string

FitToolFeedbackMessage keeps tool feedback within a single outbound message. It preserves the first line when possible and truncates the explanation body instead of letting the message be split into multiple chunks.

func FormatArgsJSON

func FormatArgsJSON(args map[string]any, prettyPrint, disableEscapeHTML bool) string

func FormatToolFeedbackMessage

func FormatToolFeedbackMessage(toolName, explanation, argsPreview string) string

FormatToolFeedbackMessage renders a tool feedback message for chat channels. It keeps the tool name on the first line for animation and can include both a human explanation and the serialized tool arguments in the body.

func HtmlToMarkdown

func HtmlToMarkdown(htmlStr string) (string, error)

func IsAudioFile

func IsAudioFile(filename, contentType string) bool

IsAudioFile checks if a file is an audio file based on its filename extension and content type.

func IsObviousPrivateHost

func IsObviousPrivateHost(
	host string,
	whitelist *PrivateHostWhitelist,
	allowPrivateHosts func() bool,
) bool

func IsPrivateOrRestrictedIP

func IsPrivateOrRestrictedIP(ip net.IP) bool

func MeasureContextRunes

func MeasureContextRunes(messages []providers.Message) int

MeasureContextRunes calculates the total rune count of a message list. Includes content, reasoning content, and estimates for tool calls.

func NewSafeDialContext

func NewSafeDialContext(
	dialer *net.Dialer,
	whitelist *PrivateHostWhitelist,
	allowPrivateHosts func() bool,
) func(context.Context, string, string) (net.Conn, error)

func ResolveMaxContextRunes

func ResolveMaxContextRunes(configValue, contextWindow int) int

ResolveMaxContextRunes determines the final MaxContextRunes value to use. Priority: explicit config > auto-calculate > conservative default

func SanitizeFilename

func SanitizeFilename(filename string) string

SanitizeFilename removes potentially dangerous characters from a filename and returns a safe version for local filesystem storage.

func SanitizeMessageContent

func SanitizeMessageContent(input string) string

SanitizeMessageContent removes Unicode control characters, format characters (RTL overrides, zero-width characters), and other non-graphic characters that could confuse an LLM or cause display issues in the agent UI.

func SetDisableTruncation

func SetDisableTruncation(enabled bool)

SetDisableTruncation globally enables or disables string truncation

func ToolCallExplanationDuplicatesContent

func ToolCallExplanationDuplicatesContent(content string, toolCalls []providers.ToolCall) bool

func Truncate

func Truncate(s string, maxLen int) string

Truncate returns a truncated version of s with at most maxLen runes. Handles multi-byte Unicode characters properly. If the string is truncated, "..." is appended to indicate truncation.

func TruncateContextSmart

func TruncateContextSmart(messages []providers.Message, maxRunes int) []providers.Message

TruncateContextSmart intelligently truncates message history to fit within maxRunes.

Strategy:

  1. Always preserve system messages (they define the agent's behavior)
  2. Keep the most recent messages (they contain current context)
  3. Drop older middle messages when necessary
  4. Insert a truncation notice to inform the LLM

Returns the truncated message list.

func ValidateSafeHTTPURL

func ValidateSafeHTTPURL(urlStr string, whitelist *PrivateHostWhitelist, allowPrivateHosts func() bool) error

func ValidateSkillIdentifier

func ValidateSkillIdentifier(identifier string) error

ValidateSkillIdentifier validates that the given skill identifier (slug or registry name) is non-empty and does not contain path separators ("/", "\\") or ".." for security.

func VisibleToolCallArgumentsPreview

func VisibleToolCallArgumentsPreview(tc providers.ToolCall, maxLen int) string

func VisibleToolCallNameAndArguments

func VisibleToolCallNameAndArguments(tc providers.ToolCall) (string, string)

Types

type BM25Engine

type BM25Engine[T any] struct {
	// contains filtered or unexported fields
}

BM25Engine is a BM25 search engine over a generic corpus. T is the document type; the caller supplies a TextFunc that extracts the searchable text from each document.

The engine precomputes its index once at construction time and reuses it for subsequent searches. If the corpus content changes, construct a new engine.

func NewBM25Engine

func NewBM25Engine[T any](corpus []T, textFunc func(T) string, opts ...BM25Option) *BM25Engine[T]

NewBM25Engine creates a BM25Engine for the given corpus.

  • corpus : slice of documents of any type T.
  • textFunc : function that returns the searchable text for a document.
  • opts : optional tuning (WithK1, WithB).

The corpus slice is referenced, not copied. Callers must not mutate it concurrently with Search().

func (*BM25Engine[T]) Search

func (e *BM25Engine[T]) Search(query string, topK int) []BM25Result[T]

Search ranks the corpus against query and returns the top-k results. Returns an empty slice (not nil) when there are no matches.

Complexity: O(|Q|×avgPostingLen + candidates × log k) per search after the one-time indexing work performed by NewBM25Engine.

type BM25Option

type BM25Option func(*bm25Config)

BM25Option is a functional option to configure a BM25Engine.

func WithB

func WithB(b float64) BM25Option

WithB overrides the document-length normalization factor (default 0.75).

func WithK1

func WithK1(k1 float64) BM25Option

WithK1 overrides the term-frequency saturation constant (default 1.2).

type BM25Result

type BM25Result[T any] struct {
	Document T
	Score    float32
}

BM25Result is a single ranked result from a Search call.

type DownloadOptions

type DownloadOptions struct {
	Timeout             time.Duration
	ExtraHeaders        map[string]string
	LoggerPrefix        string
	ProxyURL            string
	BlockPrivateTargets bool
}

DownloadOptions holds optional parameters for downloading files

type PrivateHostWhitelist

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

func NewPrivateHostWhitelist

func NewPrivateHostWhitelist(entries []string) (*PrivateHostWhitelist, error)

func (*PrivateHostWhitelist) Contains

func (w *PrivateHostWhitelist) Contains(ip net.IP) bool

type SafeHTTPClientOptions

type SafeHTTPClientOptions struct {
	ProxyURL             string
	Timeout              time.Duration
	PrivateHostWhitelist []string
	AllowPrivateHosts    func() bool
	MaxRedirects         int
}

type VisibleToolCall

type VisibleToolCall struct {
	ID           string                       `json:"id,omitempty"`
	Type         string                       `json:"type,omitempty"`
	Function     *VisibleToolCallFunction     `json:"function,omitempty"`
	ExtraContent *VisibleToolCallExtraContent `json:"extra_content,omitempty"`
}

func BuildVisibleToolCalls

func BuildVisibleToolCalls(
	toolCalls []providers.ToolCall,
	maxArgsLen int,
) []VisibleToolCall

type VisibleToolCallExtraContent

type VisibleToolCallExtraContent struct {
	ToolFeedbackExplanation string `json:"tool_feedback_explanation,omitempty"`
}

type VisibleToolCallFunction

type VisibleToolCallFunction struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

Jump to

Keyboard shortcuts

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