message

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentBlock

type ContentBlock struct {
	Type      ContentBlockType `json:"type"`
	Text      string           `json:"text,omitempty"`
	MediaType string           `json:"media_type,omitempty"`
	Data      string           `json:"data,omitempty"`
	URL       string           `json:"url,omitempty"`
}

ContentBlock represents a single piece of content within a message.

type ContentBlockType

type ContentBlockType string

ContentBlockType discriminates the kind of content in a ContentBlock.

const (
	ContentBlockText     ContentBlockType = "text"
	ContentBlockImage    ContentBlockType = "image"
	ContentBlockDocument ContentBlockType = "document"
)

type History

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

History stores conversation messages purely in memory. It is concurrency safe and does not perform any persistence.

func NewHistory

func NewHistory() *History

NewHistory constructs an empty history.

func (*History) All

func (h *History) All() []Message

All returns a cloned snapshot of the history in order from oldest to newest.

func (*History) Append

func (h *History) Append(msg Message)

Append stores a message at the end of the history. The message is cloned to avoid external mutation after insertion.

func (*History) Last

func (h *History) Last() (Message, bool)

Last returns the newest message when present.

func (*History) Len

func (h *History) Len() int

Len reports the number of stored messages.

func (*History) Replace

func (h *History) Replace(msgs []Message)

Replace swaps the stored history with the provided slice, cloning entries to keep ownership local to the History.

func (*History) Reset

func (h *History) Reset()

Reset clears the history contents.

func (*History) TokenCount

func (h *History) TokenCount() int

TokenCount reports the estimated token cost of all stored messages.

type Message

type Message struct {
	Role             string
	Content          string
	ContentBlocks    []ContentBlock // Multimodal content; takes precedence over Content when non-empty
	ToolCalls        []ToolCall
	ReasoningContent string
}

Message represents a single conversational turn used within the message package. It is purposefully minimal to keep the history layer independent from concrete model providers.

func CloneMessage

func CloneMessage(msg Message) Message

CloneMessage performs a deep clone of a model.Message, duplicating nested maps to avoid mutation leaks between callers.

func CloneMessages

func CloneMessages(msgs []Message) []Message

CloneMessages clones an entire slice of model messages.

type NaiveCounter

type NaiveCounter struct{}

NaiveCounter approximates tokens using byte length. It intentionally errs on the side of overestimation to avoid exceeding upstream context limits.

func (NaiveCounter) Count

func (NaiveCounter) Count(msg Message) int

Count implements TokenCounter.

type TokenCounter

type TokenCounter interface {
	Count(msg Message) int
}

TokenCounter returns an estimated token cost for a message.

type ToolCall

type ToolCall struct {
	ID        string
	Name      string
	Arguments map[string]any
	Result    string
}

ToolCall mirrors the shape of a tool invocation produced by the assistant.

type Trimmer

type Trimmer struct {
	MaxTokens int
	Counter   TokenCounter
}

Trimmer removes the oldest messages when the estimated token budget exceeds MaxTokens. The newest messages are preserved.

func NewTrimmer

func NewTrimmer(limit int, counter TokenCounter) *Trimmer

NewTrimmer constructs a Trimmer with the provided token limit. When counter is nil a NaiveCounter is used.

func (*Trimmer) Trim

func (t *Trimmer) Trim(history []Message) []Message

Trim returns a trimmed copy of messages that fits within the token limit. If the limit is zero or negative an empty slice is returned.

Jump to

Keyboard shortcuts

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