context

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package context provides functionality for loading and managing .context/ files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(content []byte) int

EstimateTokens provides a rough token count estimate for content.

Uses a simple heuristic of ~4 characters per token for English text. This is a conservative estimate for Claude/GPT-style tokenizers that tends to slightly overestimate, which is safer for budgeting.

Parameters:

  • content: Byte slice to estimate tokens for

Returns:

  • int: Estimated token count (0 for empty content)

func EstimateTokensString

func EstimateTokensString(s string) int

EstimateTokensString estimates tokens for a string.

Convenience wrapper around EstimateTokens for string input.

Parameters:

  • s: String to estimate tokens for

Returns:

  • int: Estimated token count

func Exists

func Exists(dir string) bool

Exists checks if a context directory exists.

If dir is empty, it uses the configured context directory.

Parameters:

  • dir: Directory path to check, or empty string for default

Returns:

  • bool: True if the directory exists and is a directory

Types

type Context

type Context struct {
	Dir         string
	Files       []FileInfo
	TotalTokens int
	TotalSize   int64
}

Context represents the loaded context from a .context/ directory.

Fields:

  • Dir: Path to the context directory
  • Files: All loaded context files with their metadata
  • TotalTokens: Sum of estimated tokens across all files
  • TotalSize: Sum of file sizes in bytes

func Load

func Load(dir string) (*Context, error)

Load reads all context files from the specified directory.

If dir is empty, it uses the configured context directory from .contextrc, CTX_DIR environment variable, or the default ".context".

Parameters:

  • dir: Directory path to load from, or empty string for default

Returns:

  • *Context: Loaded context with files, token counts, and metadata
  • error: NotFoundError if directory doesn't exist, or other IO errors

type FileInfo

type FileInfo struct {
	Name    string
	Path    string
	Size    int64
	ModTime time.Time
	Content []byte
	IsEmpty bool
	Tokens  int
	Summary string
}

FileInfo represents metadata about a context file.

Fields:

  • Name: Filename (e.g., "TASKS.md")
  • Path: Full path to the file
  • Size: File size in bytes
  • ModTime: Last modification time
  • Content: Raw file content
  • IsEmpty: True if the file has no meaningful content (only headers/whitespace)
  • Tokens: Estimated token count for the content
  • Summary: Brief description generated from the content

type NotFoundError

type NotFoundError struct {
	Dir string
}

NotFoundError is returned when the context directory doesn't exist.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

Error implements the error interface for NotFoundError.

Returns:

  • string: Error message including the missing directory path

Jump to

Keyboard shortcuts

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