context

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanExecuteTool

func CanExecuteTool(turns []llm.Message, toolName string) bool

CanExecuteTool checks if a tool can be executed given current state

func CleanContent

func CleanContent(content string) string

CleanContent removes both turn number and runtime notice from content

func CountTurnsSinceTaskStart

func CountTurnsSinceTaskStart(turns []llm.Message) int

CountTurnsSinceTaskStart counts turns since the last Tasks.Start

func ExtractTurnNumber

func ExtractTurnNumber(content string) int

ExtractTurnNumber extracts the turn number from a message content Returns 0 if no turn number found

func GetPendingDiff

func GetPendingDiff(turns []llm.Message) *string

GetPendingDiff returns the checkpoint ID if there's a pending diff awaiting accept/decline

func GetTaskCheckpoint

func GetTaskCheckpoint(turns []llm.Message) *string

GetTaskCheckpoint returns the checkpoint ID for the current task

func GetTaskDepth

func GetTaskDepth(turns []llm.Message) int

GetTaskDepth returns the current task nesting depth

func HasUnfinishedTaskInHistory

func HasUnfinishedTaskInHistory(turns []llm.Message) bool

HasUnfinishedTaskInHistory checks if the most recent Tasks.Start tool result has "task started" (meaning we're still working on it)

func StripRuntimeNotice

func StripRuntimeNotice(content string) string

StripRuntimeNotice removes the runtime notice suffix from content

func StripTurnNumber

func StripTurnNumber(content string) string

StripTurnNumber removes the turn number prefix from content

Types

type InternalMeta

type InternalMeta struct {
	CheckpointID string `json:"checkpoint_id,omitempty"` // checkpoint at task start (for rollback)
	HasChanges   bool   `json:"has_changes,omitempty"`   // true if task made file changes
}

InternalMeta contains metadata for context tool operations

type Manager

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

Manager handles context tools git repo for conversation branch tracking

func NewManager

func NewManager(contextDir string, checkpointMgr *checkpoint.Manager) (*Manager, error)

NewManager creates a context tools manager whose git repository lives in contextDir. That directory belongs to the session, so a task started in one turn is still open in the next.

func (*Manager) AppendMessage

func (m *Manager) AppendMessage(msg llm.Message) error

AppendMessage appends a single message to turns.jsonl

func (*Manager) AppendMessages

func (m *Manager) AppendMessages(messages []llm.Message) error

AppendMessages appends multiple messages to turns.jsonl (batch write) Used at the end of a successful iteration to persist the buffer

func (*Manager) CheckpointManager

func (m *Manager) CheckpointManager() *checkpoint.Manager

CheckpointManager returns the associated checkpoint manager

func (*Manager) Cleanup

func (m *Manager) Cleanup() error

Cleanup removes the context directory

func (*Manager) CommitTurns

func (m *Manager) CommitTurns(message string) error

CommitTurns commits the current turns.jsonl state

func (*Manager) CreateBranch

func (m *Manager) CreateBranch() (string, error)

CreateBranch creates a new branch and switches to it

func (*Manager) CreateCheckpoint

func (m *Manager) CreateCheckpoint() (string, error)

CreateCheckpoint creates a named checkpoint at the current file state

func (*Manager) CurrentBranch

func (m *Manager) CurrentBranch() string

CurrentBranch returns the current branch name

func (*Manager) Enabled

func (m *Manager) Enabled() bool

Enabled returns whether context tools are enabled

func (*Manager) GetDiffSinceCheckpoint

func (m *Manager) GetDiffSinceCheckpoint(checkpointID string) (string, error)

GetDiffSinceCheckpoint gets the diff of file changes since a checkpoint

func (*Manager) GetLastMessage

func (m *Manager) GetLastMessage() *llm.Message

GetLastMessage returns the cached last message (nil if empty)

func (*Manager) GetSystemPrompt

func (m *Manager) GetSystemPrompt() string

GetSystemPrompt returns the stored system prompt

func (*Manager) GetTurnCount

func (m *Manager) GetTurnCount() int

GetTurnCount returns the number of assistant messages (turns)

func (*Manager) HasUnfinishedTask

func (m *Manager) HasUnfinishedTask() bool

HasUnfinishedTask checks if there's an unfinished Tasks.Start in history

func (*Manager) Initialize

func (m *Manager) Initialize() error

Initialize sets up the context tools git repository

func (*Manager) ModifyLastMessage

func (m *Manager) ModifyLastMessage(modifier func(*llm.Message)) error

ModifyLastMessage modifies the last message in turns.jsonl Used for patterns like appending intervention hints to last tool result

func (*Manager) ParentBranch

func (m *Manager) ParentBranch(branchName string) string

ParentBranch returns the parent branch of the given branch

func (*Manager) ReadMessagesForLLM

func (m *Manager) ReadMessagesForLLM() ([]llm.Message, error)

ReadMessagesForLLM reads turns from file and prepends system prompt This is the primary method for getting messages to send to the LLM

func (*Manager) ReadTurnsForLLM

func (m *Manager) ReadTurnsForLLM() ([]llm.Message, error)

ReadTurnsForLLM reads all messages from current branch's turns.jsonl This is the primary way to get message history for LLM calls

func (*Manager) ReplaceLastToolResult

func (m *Manager) ReplaceLastToolResult(content ToolContent) error

ReplaceLastToolResult replaces the content of the last tool result message

func (*Manager) RestoreFileToStart

func (m *Manager) RestoreFileToStart(path string) ([]byte, error)

RestoreFileToStart restores a file to its state at conversation start

func (*Manager) RestoreFileToTaskStart

func (m *Manager) RestoreFileToTaskStart(path string) ([]byte, error)

RestoreFileToTaskStart restores a file to its state at current task start

func (*Manager) RollbackToCheckpoint

func (m *Manager) RollbackToCheckpoint(checkpointID string) error

RollbackToCheckpoint rolls back file system to a checkpoint state

func (*Manager) SetEnabled

func (m *Manager) SetEnabled(enabled bool)

SetEnabled enables or disables context tools

func (*Manager) SetSystemPrompt

func (m *Manager) SetSystemPrompt(prompt string)

SetSystemPrompt stores the system prompt (kept in RAM, prepended when reading)

func (*Manager) SwitchBranch

func (m *Manager) SwitchBranch(branchName string) error

SwitchBranch switches to an existing branch

type Middleware

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

Middleware processes LLM responses before storage

func NewMiddleware

func NewMiddleware(manager *Manager, config RuntimeNoticeConfig) *Middleware

NewMiddleware creates a new context tools middleware

func (*Middleware) InjectTurnNumber

func (m *Middleware) InjectTurnNumber(content string) string

InjectTurnNumber is a simpler version that only injects turn number Use this when you don't need runtime notices

func (*Middleware) ProcessAssistantMessage

func (m *Middleware) ProcessAssistantMessage(content string) string

ProcessAssistantMessage injects turn number and optional runtime notice into an assistant message before it's stored to history

type RuntimeNoticeConfig

type RuntimeNoticeConfig struct {
	TaskWarnTurns       int  // Warn after N turns in task (default: 5)
	TaskCriticalTurns   int  // Critical warning after N turns (default: 10)
	ContextCapacityWarn int  // Warn at N% context capacity (default: 80)
	MaxNestedDepth      int  // Max task nesting depth warning (default: 2)
	NotifyFileChanges   bool // Notify about file changes in task
}

RuntimeNoticeConfig configures when runtime notices are injected

type ToolContent

type ToolContent struct {
	Result   string        `json:"result"`
	Diff     string        `json:"diff,omitempty"`    // for completed task
	Success  *bool         `json:"success,omitempty"` // for completed task (nil = true)
	Internal *InternalMeta `json:"_internal,omitempty"`
}

ToolContent represents the parsed content of a tool result

func ParseToolContent

func ParseToolContent(msg llm.Message) *ToolContent

ParseToolContent parses the content field of a tool message

Jump to

Keyboard shortcuts

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