Documentation
¶
Overview ¶
Package agentcontext builds the model's view of a session: provider conversation, context boundaries and summaries, and token estimates.
Index ¶
- Constants
- Variables
- func BoundaryLabel(compaction transcript.Compaction) string
- func CapRoundResults(messages []transcript.Message, limit int) []transcript.Message
- func Conversation(ctx context.Context, history []transcript.Message, images *ImageCache, ...) ([]providers.Message, error)
- func EffectiveWindow(windowTokens int, maxOutputTokens int) int
- func Elide(messages []transcript.Message, elision Elision) []transcript.Message
- func ElisionDue(tokens, limit int) bool
- func EstimateMessageTokens(messages []transcript.Message) int
- func EstimateRequestTokens(request providers.Request) int
- func EstimateTextTokens(text string) int
- func FormatShortNumber(value int) string
- func HeadTail(text string, maxTokens int) string
- func Kept(previous *transcript.Compaction, covered []transcript.Message, runIDs []string) []string
- func SessionTokens(baseTokens int, compaction *transcript.Compaction, ...) int
- func Summarize(ctx context.Context, generator Generator, in SummaryInput) (string, error)
- func SummaryDue(tokens, limit int) bool
- func SummaryMessages(compaction *transcript.Compaction) []providers.Message
- func SummaryReply(response providers.Response) (string, error)
- func SummaryText(compaction *transcript.Compaction) string
- func TailStart(messages []transcript.Message, budget int) int
- func TextOnlyConversation(history []transcript.Message) []providers.Message
- func TranscriptTail(messages []transcript.Message, maxTokens int) string
- func WithoutSignedReasoning(messages []transcript.Message, throughSeq int64) []transcript.Message
- type AttachmentData
- type AttachmentReader
- type Elision
- type Generator
- type Identity
- type ImageCache
- type SummaryInput
Constants ¶
const ElisionPercent = 60
ElisionPercent of the usable window is where old bulky results get elided.
const EstimatedImageTokens = 1_500
EstimatedImageTokens is the flat estimate for one image.
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.
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.
const SummaryPercent = 80
SummaryPercent of the usable window is where older history gets summarised.
const TailPercent = 25
TailPercent is the share of the usable window the newest turns keep verbatim after a summary.
const UnknownWindowTokens = 128_000
UnknownWindowTokens stands in for a model whose window is unknown.
Variables ¶
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.
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 ¶
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 ¶
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 EstimateTextTokens ¶
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 HeadTail ¶
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 ¶
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 ¶
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 ¶
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 AttachmentReader ¶
type Elision ¶
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 ¶
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.