Documentation
¶
Overview ¶
Package schema wraps bifrost's provider-agnostic chat schema with the helpers context-guru components need: token accounting, deep-clone for fail-open snapshots, tool-result iteration, and byte-preservation for lossless round-trips of provider-specific fields.
Components operate directly on *schemas.BifrostChatRequest (see package components); this package is the small toolbox around that type, not a competing model. Wire<->schema conversion lives in the host adapters — the bifrost proxy gets it for free from bifrost's transport, the AuthBridge plugin uses FromOpenAIBytes/FromAnthropicBytes (added in P3).
Index ¶
- func BlockText(b schemas.ChatContentBlock) string
- func CloneMessages(in []schemas.ChatMessage) []schemas.ChatMessage
- func MessageText(m schemas.ChatMessage) string
- func MessagesTokens(req *schemas.BifrostChatRequest) int
- func Rewritable(m schemas.ChatMessage) bool
- func SetMessageText(m *schemas.ChatMessage, text string)
- func TextTokens(s string) int
- type Provider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BlockText ¶
func BlockText(b schemas.ChatContentBlock) string
BlockText returns the text payload of a content block, or "" if the block carries no text (image/audio/file/refusal).
func CloneMessages ¶
func CloneMessages(in []schemas.ChatMessage) []schemas.ChatMessage
CloneMessages deep-copies a message slice via JSON round-trip. Used to snapshot a request before a component runs so the pipeline can restore it on error/panic or when a component fails the never-worse gate. JSON round-trip is safe because bifrost's content types implement custom (Un)MarshalJSON that preserve cache_control/citations/cachePoint.
func MessageText ¶
func MessageText(m schemas.ChatMessage) string
MessageText returns the concatenated text of a message, handling both the string-content and block-content representations.
func MessagesTokens ¶
func MessagesTokens(req *schemas.BifrostChatRequest) int
MessagesTokens estimates the token cost of a request's messages by counting the message CONTENT text — what the model actually reads — not the JSON envelope. This is the signal the never-worse gate needs: control metadata like cache_control adds envelope bytes but no model-visible tokens, so a cache-injection Reformat must not look "worse" for adding it.
func Rewritable ¶
func Rewritable(m schemas.ChatMessage) bool
Rewritable reports whether m's content can be safely replaced with a plain text string (via SetMessageText) without losing data. It is false when the message carries any non-text content block — image/audio/file/refusal, or a block type bifrost does not model (e.g. Anthropic tool_result, whose payload lives in fields MessageText never reads). Those bytes would vanish on a text-only rewrite and were never stashed, so components must skip such messages. String content and all-text block content are rewritable.
func SetMessageText ¶
func SetMessageText(m *schemas.ChatMessage, text string)
SetMessageText replaces a message's content with a single text string, collapsing any block structure. Components that rewrite a whole message's text (e.g. cmdfilter, offload markers) use this.
func TextTokens ¶
TextTokens counts tokens in a raw string via the shared tokenizer.
Types ¶
type Provider ¶
type Provider = schemas.ModelProvider
Provider identifies the wire dialect a request arrived in. It drives provider-specific behaviour (e.g. cache_control is Anthropic-family) and how the adapter renders bytes back out.