context

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package context provides context-building utilities for AI chat workflows. It supports manual file selection, token counting, and structured export of repository files for pasting into AI conversations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountTokens

func CountTokens(text string) int

CountTokens estimates the number of tokens in text using a word-based approximation. The multiplier (1.3) accounts for sub-word tokenisation used by models like GPT-4 (cl100k_base encoding). This is intentionally a simple heuristic — exact counts are not required for context budgeting.

Types

type Builder

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

Builder tracks a set of selected files and provides export functionality for AI chat workflows. All paths are validated against a repository root to prevent directory traversal.

func NewBuilder

func NewBuilder(root string) (*Builder, error)

NewBuilder creates a Builder anchored at the given repository root. The root is resolved to an absolute path; an error is returned if resolution fails.

func (*Builder) Add

func (b *Builder) Add(path string) error

Add reads the file at path and adds it to the context. Relative paths are resolved against the repo root. Paths that escape the root are rejected. Adding a file that is already present is a no-op.

func (*Builder) Clear

func (b *Builder) Clear()

Clear removes all files from the context.

func (*Builder) Export

func (b *Builder) Export() string

Export renders the context as structured markdown suitable for pasting into an AI chat window. Each file is preceded by a heading and wrapped in a fenced code block with the appropriate language tag.

func (*Builder) Files

func (b *Builder) Files() []ContextFile

Files returns a copy of the selected files.

func (*Builder) Remove

func (b *Builder) Remove(path string)

Remove removes a file from the context by its path. If the file is not present, this is a no-op.

func (*Builder) TotalTokens

func (b *Builder) TotalTokens() int

TotalTokens returns the sum of token counts across all selected files.

type ContextFile

type ContextFile struct {
	Path    string // repo-relative path
	Content string // raw file content
	Tokens  int    // estimated token count
}

ContextFile describes a single file selected for inclusion in the AI context window.

Jump to

Keyboard shortcuts

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