agentcontext

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package agentcontext builds the model's view of a session: provider conversation, context boundaries and summaries, and token estimates.

Index

Constants

View Source
const ElisionPercent = 60

ElisionPercent of the usable window is where old bulky results get elided.

View Source
const EstimatedImageTokens = 1_500

EstimatedImageTokens is the flat estimate for one image.

View Source
const LargeOutputTokens = 8_000

LargeOutputTokens is the size above which a tool result is kept in a file and the model gets its head and tail.

View Source
const SummaryInstruction = "Summarise the conversation so far for yourself: the older messages will be replaced by this summary, the most recent ones stay visible. Do not call tools. Use these sections:\n" + summarySections + "\nBe concise and factual; leave out raw tool output, secrets and long code blocks."

SummaryInstruction ends the run's own request when it asks for the summary that replaces the older part of the conversation.

View Source
const SummaryPercent = 80

SummaryPercent of the usable window is where older history gets summarised.

View Source
const TailPercent = 25

TailPercent is the share of the usable window the newest turns keep verbatim after a summary.

View Source
const UnknownWindowTokens = 128_000

UnknownWindowTokens stands in for a model whose window is unknown.

Variables

View Source
var ErrAttachmentUnavailable = errors.New("attachment is no longer available")

ErrAttachmentUnavailable identifies an attachment that used to exist but can no longer be read, for example because a temporary upload expired. Provider conversation building can omit the binary data without failing the whole run.

View Source
var ErrNothingToSummarise = errors.New("nothing to summarise")

ErrNothingToSummarise means the input holds no text a summary could keep.

Functions

func BoundaryLabel

func BoundaryLabel(compaction transcript.Compaction) string

BoundaryLabel is the content clients show for a boundary message.

func CapRoundResults added in v0.2.1

func CapRoundResults(messages []transcript.Message, limit int) []transcript.Message

CapRoundResults returns messages with the results of each tool round within roundResultsPercent of a usable window of limit tokens: results over their even share of it keep their head and tail. messages itself is not changed.

func Conversation

func Conversation(ctx context.Context, history []transcript.Message, images *ImageCache, allowImageInput bool, target Identity) ([]providers.Message, error)

Conversation converts history into provider messages, pairing every tool call with its result; signed reasoning is kept only for target. Stored images come from images.

func EffectiveWindow

func EffectiveWindow(windowTokens int, maxOutputTokens int) int

EffectiveWindow is the prompt room of a model: its window without the output limit and a 5% reserve, never less than half the window.

func Elide

func Elide(messages []transcript.Message, elision Elision) []transcript.Message

Elide returns messages with the covered results over ~1k tokens and the covered images replaced by short notes; a result keeps the user guidance steered into it. messages itself is not changed.

func ElisionDue

func ElisionDue(tokens, limit int) bool

ElisionDue reports whether a prompt of tokens has reached the elision threshold of a usable window of limit tokens.

func EstimateMessageTokens

func EstimateMessageTokens(messages []transcript.Message) int

func EstimateRequestTokens

func EstimateRequestTokens(request providers.Request) int

func EstimateTextTokens

func EstimateTextTokens(text string) int

EstimateTextTokens estimates runes/4 for Latin script and runes/2.5 for other scripts such as Cyrillic, which tokenizers split more finely.

func FormatShortNumber

func FormatShortNumber(value int) string

func HeadTail

func HeadTail(text string, maxTokens int) string

HeadTail keeps the head and the tail of text within about maxTokens and says how much was left out between them.

func Kept

func Kept(previous *transcript.Compaction, covered []transcript.Message, runIDs []string) []string

Kept is what a new boundary keeps verbatim: the user messages and steered guidance of runIDs (the run and the runs it continues), after what the boundary it replaces kept for one of them.

func SessionTokens

func SessionTokens(baseTokens int, compaction *transcript.Compaction, messages []transcript.Message) int

SessionTokens estimates a session's context: fixed parts, the boundary's summary and the messages after it.

func Summarize

func Summarize(ctx context.Context, generator Generator, in SummaryInput) (string, error)

Summarize summarises the input in chunks of about ChunkTokens and merges the partial summaries, a chunk at a time, until one is left.

func SummaryDue

func SummaryDue(tokens, limit int) bool

SummaryDue reports whether a prompt of tokens has reached the summary threshold of a usable window of limit tokens.

func SummaryMessages

func SummaryMessages(compaction *transcript.Compaction) []providers.Message

SummaryMessages is the boundary as the first message of a request, when it has anything to say.

func SummaryReply

func SummaryReply(response providers.Response) (string, error)

SummaryReply is the text of a summary reply; a cut, filtered or empty one fails.

func SummaryText

func SummaryText(compaction *transcript.Compaction) string

SummaryText is the user text that stands for the history a boundary covers; empty for a /clear or no boundary.

func TailStart

func TailStart(messages []transcript.Message, budget int) int

TailStart is the index where the kept tail of messages begins: the newest whole turns within budget tokens, at least the newest one. A tail never starts at a call or a result, nor while a call before it waits for its result. 0 means nothing before the tail can be summarised.

func TextOnlyConversation

func TextOnlyConversation(history []transcript.Message) []providers.Message

TextOnlyConversation renders history as plain text for models without tool calling.

func TranscriptTail added in v0.2.1

func TranscriptTail(messages []transcript.Message, maxTokens int) string

TranscriptTail renders the newest message groups that fit in about maxTokens as summaries render history, oldest first.

func WithoutSignedReasoning

func WithoutSignedReasoning(messages []transcript.Message, throughSeq int64) []transcript.Message

WithoutSignedReasoning drops the signed or encrypted reasoning of messages up to throughSeq, which was signed over a history since edited; plain reasoning stays. messages itself is not changed.

Types

type AttachmentData

type AttachmentData struct {
	Data     []byte
	MIMEType string
	Name     string
	Size     int64
}

type AttachmentReader

type AttachmentReader interface {
	ReadAttachment(ctx context.Context, path string, temporary bool, maxBytes int64) (AttachmentData, error)
}

type Elision

type Elision struct {
	ResultsThroughSeq int64
	ImagesThroughSeq  int64
}

Elision hides the tool results and images of messages up to these seqs from a request; the transcript keeps them.

func AdvanceElision

func AdvanceElision(messages []transcript.Message, limit int, current Elision, force bool) (Elision, bool)

AdvanceElision moves current up to NextElision of messages and reports whether it moved. Once results (or images) are elided, they move on only when five more tool rounds (or replies) became eligible, or when forced, so the request prefix stays stable; it never moves without hiding something more.

func NextElision

func NextElision(messages []transcript.Message, limit int) Elision

NextElision covers the results before the last five tool rounds, or before fewer of them when five outgrow 30% of the usable window of limit tokens (the newest round always stays), and the images before the last three replies.

type Generator

type Generator interface {
	Generate(ctx context.Context, req providers.Request) (providers.Response, error)
}

Generator produces one model reply; agent.Model and providers.Runtime satisfy it.

type Identity

type Identity struct {
	Provider string
	Model    string
}

Identity is the Provider and Model a runtime stamps on its replies; signed or encrypted reasoning is replayed only to the same pair.

type ImageCache added in v0.2.1

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

ImageCache keeps the images one run's requests sent, read and encoded once instead of for every request; a build forgets the images it no longer sends. A nil cache reads nothing, so only inline images can be sent.

func NewImageCache added in v0.2.1

func NewImageCache(reader AttachmentReader) *ImageCache

NewImageCache returns an empty cache over reader.

type SummaryInput

type SummaryInput struct {
	Previous    string
	Messages    []transcript.Message
	ChunkTokens int
}

SummaryInput is history summarised on its own, outside the conversation's own request; Previous is the summary it continues.

Jump to

Keyboard shortcuts

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