utils

package
v1.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package utils provides utility functions for v2.

Package utils provides utility functions for v2.

Index

Constants

View Source
const (
	// DefaultHectorDir is the default directory for Hector data
	DefaultHectorDir = ".hector"

	// DefaultConfigFileName is the default config file name
	DefaultConfigFileName = "config.yaml"

	// DefaultDatabaseFileName is the default database file name
	DefaultDatabaseFileName = "hector.db"

	// DefaultAppsDir is the directory for app-specific data
	DefaultAppsDir = "apps"
)

Default paths for Hector files

Variables

This section is empty.

Functions

func DefaultConfigPath

func DefaultConfigPath() string

DefaultConfigPath returns the default config file path: .hector/config.yaml

func DefaultDatabasePath

func DefaultDatabasePath() string

DefaultDatabasePath returns the default database file path: .hector/hector.db

func EnsureHectorDir

func EnsureHectorDir(basePath string) (string, error)

EnsureHectorDir ensures the .hector directory exists at the given base path. If basePath is empty or ".", it creates ./.hector in the current directory. Otherwise, it creates {basePath}/.hector.

This is used by various facilities that need to store data in .hector: - Config file: ./.hector/config.yaml - Tasks database: ./.hector/hector.db - Document store index state: {sourcePath}/.hector/index_state_*.json - Checkpoints: {sourcePath}/.hector/checkpoints/ - Vector stores: {sourcePath}/.hector/vectors/

Returns the full path to the .hector directory and any error.

func EstimateTokens

func EstimateTokens(text string) int

EstimateTokens provides a quick token estimate without a counter. Uses ~4 characters per token as a rough approximation.

func GetAppDir added in v1.21.0

func GetAppDir(basePath, appID string) (string, error)

GetAppDir returns the directory path for a specific app: .hector/apps/<app_id> It ensures the directory exists.

func GetEncodingForModel

func GetEncodingForModel(model string) string

GetEncodingForModel returns the appropriate encoding name for a model.

func TemplateFuncs added in v1.15.0

func TemplateFuncs() template.FuncMap

TemplateFuncs returns common template helper functions used across Hector. These are useful for payload transformation in webhooks and notifications.

Types

type Message

type Message struct {
	Role    string
	Content string
}

Message represents a chat message for token counting.

type TokenCounter

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

TokenCounter provides token counting functionality using tiktoken. Token counting and estimation utilities.

func NewTokenCounter

func NewTokenCounter(model string) (*TokenCounter, error)

NewTokenCounter creates a new token counter for the given model.

func (*TokenCounter) Count

func (tc *TokenCounter) Count(text string) int

Count returns the number of tokens in the given text.

func (*TokenCounter) CountMessages

func (tc *TokenCounter) CountMessages(messages []Message) int

CountMessages returns the approximate token count for a list of messages. Uses the ChatML format estimation (3 tokens per message + role + content).

func (*TokenCounter) EstimateTokensForText

func (tc *TokenCounter) EstimateTokensForText(text string) int

EstimateTokensForText returns an estimate of tokens for text. If the counter is not initialized, falls back to character-based estimation.

func (*TokenCounter) FitWithinLimit

func (tc *TokenCounter) FitWithinLimit(messages []Message, maxTokens int) []Message

FitWithinLimit returns messages that fit within the token limit. It works backwards from the most recent message, keeping as many as fit. This preserves the most recent context which is typically most relevant.

func (*TokenCounter) GetModel

func (tc *TokenCounter) GetModel() string

GetModel returns the model name used by this counter.

Jump to

Keyboard shortcuts

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