Documentation
¶
Index ¶
- Constants
- type CompressionCompleteCallback
- type CompressionProgressCallback
- type Manager
- func (m *Manager) AddMessage(_ context.Context, message *schema.Message)
- func (m *Manager) Clear()
- func (m *Manager) GetChatModel() model.ToolCallingChatModel
- func (m *Manager) GetFullMessages() []*schema.Message
- func (m *Manager) GetLastUserMessage() string
- func (m *Manager) GetMessageCount() int
- func (m *Manager) GetMessages() []*schema.Message
- func (m *Manager) GetSummary() string
- func (m *Manager) IncRound(ctx context.Context)
- func (m *Manager) RemoveLastRound()
- func (m *Manager) SetChatModel(chatmodel model.ToolCallingChatModel)
- func (m *Manager) SetCompressionCompleteCallback(cb CompressionCompleteCallback)
- func (m *Manager) SetCompressionProgressCallback(cb CompressionProgressCallback)
- func (m *Manager) SetLoading(loading bool)
- func (m *Manager) SetPersistenceCallback(cb PersistenceCallback)
- func (m *Manager) SetSystemPrompt(prompt string, renderer func(string) (string, error))
- type PersistenceCallback
Constants ¶
const ( // ContextModeCompress is the default mode: when the window exceeds // maxMessageRounds, the oldest rounds are summarized by the chatmodel and // atomically replaced by a single summary round (a blocking model call per // compression event). ContextModeCompress = "compress" // ContextModeTruncate keeps the window within maxMessageRounds by dropping // the oldest round(s) once the limit is reached. No model call is // involved. The cap is enforced at snapshot time (GetMessages), so the // history sent to the model can never exceed maxMessageRound rounds. ContextModeTruncate = "truncate" )
Context overflow modes (chats.<name>.contextMode)
const (
DefaultMaxMessageRound int = 10
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompressionCompleteCallback ¶
CompressionCompleteCallback is a callback function that is called after compression completes This allows the caller to persist the modified messages (full overwrite mode)
type CompressionProgressCallback ¶ added in v1.6.0
CompressionProgressCallback is invoked right before the blocking summary model call so the caller can notify the user (e.g. print a hint on the CLI or push a message over WebSocket) that the request will pause while the history is compressed.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages conversation context.
Append-only principle: once a message has been added (and therefore may have been sent to the model), it is never modified afterwards. The history only grows by appending at the tail. The only operations that rewrite the head are rare atomic events, both triggered when a new round starts:
- compress mode (default): the oldest rounds are atomically replaced by a single summary round;
- truncate mode: the oldest round(s) are dropped at snapshot time so the history sent to the model stays within maxMessageRound.
In compress mode this keeps the longest common prefix of consecutive requests stable, which is what provider prompt caches (OpenAI/DeepSeek/Ark/ Anthropic) match on: the head is rewritten only once per compression event instead of per model call. Truncate mode trades this away for simplicity.
func NewManager ¶
NewManager creates a new Manager instance.
contextMode selects the overflow strategy (ContextModeCompress, default, or ContextModeTruncate); any unrecognized value falls back to compress.
func (*Manager) AddMessage ¶
AddMessage adds a message to the context.
Messages are only appended: compression is never triggered here, because the current round is still in progress. It is triggered from IncRound, when a new round starts and every existing round is complete.
func (*Manager) Clear ¶
func (m *Manager) Clear()
Clear clears the context (preserves system messages)
func (*Manager) GetChatModel ¶
func (m *Manager) GetChatModel() model.ToolCallingChatModel
func (*Manager) GetFullMessages ¶
GetFullMessages retrieves all full messages in the current context This includes all original messages without any simplification. The returned messages are guaranteed to have proper tool_call / tool_result pairing.
func (*Manager) GetLastUserMessage ¶
GetLastUserMessage returns the content of the last user message in the conversation. Returns empty string if no user message is found.
func (*Manager) GetMessageCount ¶
GetMessageCount returns the total number of messages in the context
func (*Manager) GetMessages ¶
GetMessages retrieves the messages in the current context. All rounds are returned in full (append-only: no simplification of older rounds), so the sequence of consecutive requests shares a stable, growing prefix that provider prompt caches can match. The returned messages are guaranteed to have proper tool_call / tool_result pairing.
func (*Manager) GetSummary ¶
GetSummary generates a summary of the conversation
func (*Manager) IncRound ¶
IncRound starts a new round.
This is the only place where compression is triggered: the just-finished round is complete and the new round is empty, so any summary model call here does not interfere with in-flight messages of the current round. Compression is skipped while restoring from persistence.
func (*Manager) RemoveLastRound ¶
func (m *Manager) RemoveLastRound()
RemoveLastRound removes the last round of messages from the context. This is used for regenerating a response - the last assistant response is removed so the user message can be re-processed.
func (*Manager) SetChatModel ¶
func (m *Manager) SetChatModel(chatmodel model.ToolCallingChatModel)
SetChatModel sets the chat model for message compression
func (*Manager) SetCompressionCompleteCallback ¶
func (m *Manager) SetCompressionCompleteCallback(cb CompressionCompleteCallback)
SetCompressionCompleteCallback sets the callback that is called after compression completes
func (*Manager) SetCompressionProgressCallback ¶ added in v1.6.0
func (m *Manager) SetCompressionProgressCallback(cb CompressionProgressCallback)
SetCompressionProgressCallback sets the callback that is invoked before the blocking summary model call (to notify the user that compression is starting)
func (*Manager) SetLoading ¶ added in v1.6.0
SetLoading toggles the loading (restore-from-persistence) state. While loading, IncRound skips compression.
func (*Manager) SetPersistenceCallback ¶
func (m *Manager) SetPersistenceCallback(cb PersistenceCallback)
SetPersistenceCallback sets the callback for auto-saving messages
func (*Manager) SetSystemPrompt ¶ added in v1.6.1
SetSystemPrompt shares the runner's system prompt with the manager.
prompt is the same template passed to the agent's Instruction and renderer the same function the agent's GenModelInput uses to expand it, so the compression summary request leads with the exact same system message the runner sends — keeping its prefix prompt-cache friendly. An empty prompt or a nil renderer is a no-op: the summary call is then sent without a system message (as before).
type PersistenceCallback ¶
PersistenceCallback is a callback function for single message persistence Each time a new message is added, this callback is invoked with that single message