Documentation
¶
Overview ¶
Package model defines JSON helpers for marshaling and unmarshaling provider message parts. This file focuses on decoding messages and discriminating concrete part types based on the Kind field.
Package model defines the provider-agnostic message and streaming types used by planners, runtimes, and provider adapters. It models messages as typed parts (thinking, text, tool use/results) plus conversation roles.
Index ¶
- Constants
- Variables
- func SetCompletionValidator(request *Request, validate func(*Response, *Completion) error) error
- func ValidateTokenUsage(usage TokenUsage) error
- type CacheCheckpointPart
- type CacheOptions
- type Chunk
- type Citation
- type CitationLocation
- type CitationsPart
- type Client
- type Completion
- type CompletionChunk
- type CompletionDelta
- type CompletionDeltaChunk
- type ConversationRole
- type DocumentCharLocation
- type DocumentChunkLocation
- type DocumentFormat
- type DocumentPageLocation
- type DocumentPart
- type ImageFormat
- type ImagePart
- type Message
- type ModelClass
- type OutputValidationError
- type OutputValidationKind
- type Part
- type Provider
- type ProviderError
- func (e *ProviderError) Code() string
- func (e *ProviderError) Error() string
- func (e *ProviderError) HTTPStatus() int
- func (e *ProviderError) Kind() ProviderErrorKind
- func (e *ProviderError) Message() string
- func (e *ProviderError) Operation() string
- func (e *ProviderError) Provider() string
- func (e *ProviderError) RequestID() string
- func (e *ProviderError) Retryable() bool
- func (e *ProviderError) Unwrap() error
- type ProviderErrorKind
- type Request
- type RequestContract
- type Response
- type ResponseEvidence
- type StopChunk
- type StreamResponseBuilder
- type Streamer
- type StructuredOutput
- type TextChunk
- type TextPart
- type ThinkingChunk
- type ThinkingOptions
- type ThinkingPart
- type TokenCount
- type TokenCounter
- type TokenEstimator
- type TokenUsage
- type ToolCall
- type ToolCallChunk
- type ToolCallDelta
- type ToolCallDeltaChunk
- type ToolChoice
- type ToolChoiceMode
- type ToolDefinition
- type ToolResultPart
- type ToolUsePart
- type UsageChunk
- type ValidatedStreamer
Constants ¶
const ( // ChunkTypeText identifies a chunk carrying assistant text. ChunkTypeText = "text" // ChunkTypeToolCall identifies a chunk carrying a tool invocation. ChunkTypeToolCall = "tool_call" // ChunkTypeToolCallDelta identifies a chunk carrying an incremental tool-call // input JSON fragment. // // Naming note: this is a *delta* because fragments are not guaranteed to be // valid JSON boundaries. It exists solely for progressive UI previews and is // safe to ignore; the canonical tool payload is still emitted as // ChunkTypeToolCall. ChunkTypeToolCallDelta = "tool_call_delta" // ChunkTypeThinking identifies a chunk carrying thinking content. ChunkTypeThinking = "thinking" // ChunkTypeCompletionDelta identifies a chunk carrying an incremental typed // completion JSON fragment. // // Naming note: this is a *delta* because fragments are not guaranteed to be // valid JSON boundaries. It exists solely for progressive previews and is // safe to ignore; the canonical completion payload is emitted as // ChunkTypeCompletion. ChunkTypeCompletionDelta = "completion_delta" // ChunkTypeCompletion identifies a chunk carrying the canonical typed // completion payload for a structured-output stream. ChunkTypeCompletion = "completion" // ChunkTypeUsage identifies a chunk carrying a usage delta. ChunkTypeUsage = "usage" // ChunkTypeStop identifies the terminal chunk carrying a stop reason. ChunkTypeStop = "stop" )
const ( // MaxToolDefinitionsPerRequest is the largest exact tool catalog accepted by // the model contract and durable recovery boundary. MaxToolDefinitionsPerRequest = 256 )
Variables ¶
var ErrRateLimited = errors.New("model: rate limited")
ErrRateLimited indicates the provider rejected the request due to rate limiting after exhausting any configured retries. Callers must not retry in a tight loop and should treat this as a transient infrastructure failure that is safe to surface to higher layers.
var ErrStreamingUnsupported = errors.New("model: streaming not supported")
ErrStreamingUnsupported indicates the provider does not support streaming.
var ErrStructuredOutputUnsupported = errors.New("model: structured output not supported")
ErrStructuredOutputUnsupported indicates the provider does not support native structured output for the requested call shape.
Functions ¶
func SetCompletionValidator ¶
func SetCompletionValidator(request *Request, validate func(*Response, *Completion) error) error
SetCompletionValidator attaches an application-owned decoder to the exact structured-output request. NewClient captures the function before provider work, and providers cannot replace it through exported request fields.
func ValidateTokenUsage ¶
func ValidateTokenUsage(usage TokenUsage) error
ValidateTokenUsage checks that token counts are non-negative and that a non-zero total equals the sum of uncached input, output, and cache tokens.
Types ¶
type CacheCheckpointPart ¶
type CacheCheckpointPart struct{}
CacheCheckpointPart marks a cache boundary in a message. Provider adapters translate this to provider-specific caching directives (e.g., Bedrock cachePoint). Providers that do not support caching ignore this part. It is complementary to CacheOptions/CachePolicy: agents can combine explicit CacheCheckpointPart instances with policy-driven AfterSystem/AfterTools checkpoints to express complex caching layouts.
type CacheOptions ¶
type CacheOptions struct {
// AfterSystem places a checkpoint after all system messages.
AfterSystem bool
// AfterTools places a checkpoint after tool definitions. Not all
// providers support tool-level checkpoints (e.g., Nova does not).
AfterTools bool
}
CacheOptions configures prompt caching behavior for a request. Provider adapters translate these flags to provider-specific caching directives. Providers that do not support caching ignore these options. When Cache is nil on a Request, the runtime may populate it from the agent RunPolicy (CachePolicy) so callers do not need to thread CacheOptions through every call site. Explicit Request.Cache values always take precedence.
type Chunk ¶
type Chunk interface {
// Kind identifies the event variant.
Kind() string
// contains filtered or unexported methods
}
Chunk is one closed model-stream event. Only the value variants declared in this package are accepted by the validated stream boundary.
type Citation ¶
type Citation struct {
// Title is the source document title/identifier when available.
Title string
// Source is a provider-specific source identifier when available.
Source string
// Location identifies where in the source document the cited content can be found.
Location CitationLocation
// SourceContent is the cited excerpt from the source document when provided.
SourceContent []string
}
Citation links generated content back to a specific location in a source document.
type CitationLocation ¶
type CitationLocation struct {
// DocumentChar identifies a character span within a source document.
DocumentChar *DocumentCharLocation
// DocumentChunk identifies a range of chunks within a source document.
DocumentChunk *DocumentChunkLocation
// DocumentPage identifies a page within a source document.
DocumentPage *DocumentPageLocation
}
CitationLocation identifies where cited content can be found within a document.
Exactly one of DocumentChar, DocumentChunk, or DocumentPage should be set when present.
type CitationsPart ¶
type CitationsPart struct {
// Text is the generated content supported by Citations.
Text string
// Citations reference the source documents that informed Text.
Citations []Citation
}
CitationsPart is a generated content block paired with citation metadata.
Providers may emit this part instead of a TextPart when citation generation is enabled.
type Client ¶
type Client interface {
// Complete performs a non-streaming model invocation.
Complete(ctx context.Context, req *Request) (*Response, error)
// Stream performs a validated streaming model invocation when supported.
Stream(ctx context.Context, req *Request) (ValidatedStreamer, error)
}
Client is the validated provider-agnostic model client.
NewClient owns the canonical implementation. Its distinct streaming return type prevents a raw Provider from satisfying Client accidentally.
type Completion ¶
type Completion struct {
// Name is the stable completion identifier associated with this payload.
Name string
// Payload is the canonical JSON value for the streamed completion.
Payload rawjson.Message
}
Completion is the canonical structured payload emitted for a streaming typed completion.
Contract:
- Provider adapters MUST populate Payload as valid JSON.
- Completion is the only canonical structured payload for a typed completion stream; callers must not reconstruct the final value from completion deltas.
type CompletionChunk ¶
type CompletionChunk struct {
Completion Completion
}
CompletionChunk carries one canonical structured completion payload.
func (CompletionChunk) Kind ¶
func (CompletionChunk) Kind() string
Kind returns the completion event discriminator.
type CompletionDelta ¶
type CompletionDelta struct {
// Name is the stable completion identifier associated with this delta.
Name string
// Delta is the raw JSON fragment emitted by the provider.
Delta string
}
CompletionDelta is an incremental structured-output fragment streamed while the provider is still constructing the final completion JSON.
Contract:
- This is a best-effort UX signal. Consumers may ignore it entirely.
- The canonical completion payload remains Completion.Payload in the final ChunkTypeCompletion emitted before stream termination.
- Delta is not guaranteed to be valid JSON on its own; callers must treat it as an opaque fragment suitable only for progressive previews.
type CompletionDeltaChunk ¶
type CompletionDeltaChunk struct {
Delta CompletionDelta
}
CompletionDeltaChunk carries one provisional structured-output fragment.
func (CompletionDeltaChunk) Kind ¶
func (CompletionDeltaChunk) Kind() string
Kind returns the completion-delta event discriminator.
type ConversationRole ¶
type ConversationRole string
ConversationRole is the role for a message in a conversation.
const ( // ConversationRoleSystem is the role for system messages. ConversationRoleSystem ConversationRole = "system" // ConversationRoleUser is the role for user messages. ConversationRoleUser ConversationRole = "user" // ConversationRoleAssistant is the role for assistant messages. ConversationRoleAssistant ConversationRole = "assistant" )
type DocumentCharLocation ¶
DocumentCharLocation identifies a character span within a document.
type DocumentChunkLocation ¶
DocumentChunkLocation identifies a chunk range within a document.
type DocumentFormat ¶
type DocumentFormat string
DocumentFormat identifies the on-wire format (extension) of a document part.
Provider adapters may support only a subset of formats. Callers should normalize uploads to one of the supported formats before constructing a DocumentPart.
const ( // DocumentFormatPDF identifies a PDF document. DocumentFormatPDF DocumentFormat = "pdf" // DocumentFormatCSV identifies a CSV document. DocumentFormatCSV DocumentFormat = "csv" // DocumentFormatDOC identifies a legacy Microsoft Word document. DocumentFormatDOC DocumentFormat = "doc" // DocumentFormatDOCX identifies a Microsoft Word document. DocumentFormatDOCX DocumentFormat = "docx" // DocumentFormatXLS identifies a legacy Microsoft Excel document. DocumentFormatXLS DocumentFormat = "xls" // DocumentFormatXLSX identifies a Microsoft Excel document. DocumentFormatXLSX DocumentFormat = "xlsx" // DocumentFormatHTML identifies an HTML document. DocumentFormatHTML DocumentFormat = "html" // DocumentFormatTXT identifies a plain text document. DocumentFormatTXT DocumentFormat = "txt" // DocumentFormatMD identifies a Markdown document. DocumentFormatMD DocumentFormat = "md" )
type DocumentPageLocation ¶
DocumentPageLocation identifies a page number within a document.
type DocumentPart ¶
type DocumentPart struct {
// Name is a short neutral identifier for the document (for example, "spec").
Name string
// Format identifies the document format/extension (for example, "pdf", "txt", "md").
Format DocumentFormat
// Bytes carries the raw document bytes when the document is provided as an upload.
Bytes []byte
// Text carries the document content when the document is provided as a single text blob.
Text string
// Chunks carries the document content split into logical chunks when citations
// should reference chunk indices rather than character spans.
Chunks []string
// URI locates the document externally when the document should not be
// embedded in the request payload (for example, "s3://bucket/key.pdf").
//
// Provider adapters fail fast when URI schemes are not supported.
URI string
// Context is optional contextual information about how the document should be
// interpreted by the model when generating citations.
Context string
// Cite requests provider-native citations when supported.
Cite bool
}
DocumentPart carries document content attached to a user message.
Documents are intended for models that support document inputs and citation generation. Exactly one of Bytes, Text, Chunks, or URI must be provided.
type ImageFormat ¶
type ImageFormat string
ImageFormat identifies the on-wire format of an image part.
Provider adapters may support only a subset of formats. Callers should normalize uploads to one of the supported formats before constructing an ImagePart.
const ( // ImageFormatPNG identifies a PNG-encoded image. ImageFormatPNG ImageFormat = "png" // ImageFormatJPEG identifies a JPEG-encoded image. ImageFormatJPEG ImageFormat = "jpeg" // ImageFormatGIF identifies a GIF-encoded image. ImageFormatGIF ImageFormat = "gif" // ImageFormatWEBP identifies a WebP-encoded image. ImageFormatWEBP ImageFormat = "webp" )
type ImagePart ¶
type ImagePart struct {
// Format identifies the encoding of Bytes (e.g., "png").
Format ImageFormat
// Bytes contains the raw image bytes for the declared format.
Bytes []byte
}
ImagePart carries image bytes attached to a user message.
Image parts are intended for multimodal models. Provider adapters fail fast when images are used in unsupported roles or for unsupported model families.
type Message ¶
type Message struct {
// Role identifies the speaker for this message.
Role ConversationRole
// Parts are the ordered content blocks for the message.
Parts []Part
// Meta carries optional provider- or application-specific metadata
// attached to the message.
Meta map[string]any
}
Message is a single chat message.
Messages are ordered and grouped into a transcript passed to model clients. Parts preserve structure (text, thinking, tool use, tool result) rather than flattening to plain strings.
func (Message) MarshalJSON ¶
MarshalJSON encodes a Message while preserving the concrete Part types stored in Parts via an explicit Kind discriminator.
This ensures round-trips through JSON do not lose type information when Parts are stored as an interface slice.
func (*Message) UnmarshalJSON ¶
UnmarshalJSON decodes a Message while materializing concrete Part implementations stored in the Parts slice.
type ModelClass ¶
type ModelClass string
ModelClass identifies the model family.
Providers map these classes to concrete model identifiers.
const ( // ModelClassHighReasoning selects a high-reasoning model family. ModelClassHighReasoning ModelClass = "high-reasoning" // ModelClassDefault selects the default model family. ModelClassDefault ModelClass = "default" // ModelClassSmall selects a small/cheap model family. ModelClassSmall ModelClass = "small" )
type OutputValidationError ¶
type OutputValidationError struct {
// contains filtered or unexported fields
}
OutputValidationError reports that provider output failed the immutable request contract. Error deliberately renders a constant public summary; use Kind, Evidence, Usage, and errors.Is/errors.As for structured diagnostics.
func RestoreOutputValidationError ¶
func RestoreOutputValidationError(kind OutputValidationKind, cause error, evidence ResponseEvidence, usage *TokenUsage) (*OutputValidationError, error)
RestoreOutputValidationError reconstructs a terminal output rejection after a trusted transport decodes its bounded fields.
func (*OutputValidationError) Error ¶
func (e *OutputValidationError) Error() string
Error returns a stable summary without rendering rejected model output or provider-authored identifiers.
func (*OutputValidationError) Evidence ¶
func (e *OutputValidationError) Evidence() ResponseEvidence
Evidence returns bounded identity for the rejected response.
func (*OutputValidationError) Kind ¶
func (e *OutputValidationError) Kind() OutputValidationKind
Kind returns the closed category of the first rejecting check.
func (*OutputValidationError) Unwrap ¶
func (e *OutputValidationError) Unwrap() error
Unwrap preserves the private validation cause for trusted in-process error inspection.
func (*OutputValidationError) Usage ¶
func (e *OutputValidationError) Usage() *TokenUsage
Usage returns a detached copy of valid scalar usage retained from the rejected response.
type OutputValidationKind ¶
type OutputValidationKind string
OutputValidationKind identifies the request-contract rule that rejected provider output. Values are safe to persist and aggregate because they do not contain model text, tool names, arguments, identifiers, or schema paths.
const ( // OutputValidationResponseShape means the normalized response or chunk did // not have a valid canonical structure. OutputValidationResponseShape OutputValidationKind = "response_shape" // OutputValidationOutputBounds means provider output exceeded a byte, // nesting, collection, or generation limit. OutputValidationOutputBounds OutputValidationKind = "output_bounds" // OutputValidationToolIdentity means a tool call lacked a usable identity or // named a tool absent from the exact current request. OutputValidationToolIdentity OutputValidationKind = "tool_identity" // OutputValidationToolArguments means tool arguments were not canonical JSON // or did not satisfy the advertised input schema. OutputValidationToolArguments OutputValidationKind = "tool_arguments" // OutputValidationToolChoice means returned calls violated the request's // tool-choice rule. OutputValidationToolChoice OutputValidationKind = "tool_choice" // OutputValidationStructuredOutput means a requested structured completion // was missing, malformed, or failed its schema. OutputValidationStructuredOutput OutputValidationKind = "structured_output" // OutputValidationStreamProtocol means stream events were incomplete, out of // order, or otherwise violated the terminal protocol. OutputValidationStreamProtocol OutputValidationKind = "stream_protocol" // OutputValidationUsage means provider token accounting was malformed. OutputValidationUsage OutputValidationKind = "usage" )
type Part ¶
type Part interface {
// contains filtered or unexported methods
}
Part is a marker interface implemented by all message parts. Concrete implementations capture user-visible text, provider-issued thinking, and tool call/result content in a strongly typed form.
type Provider ¶
type Provider interface {
// Complete performs a non-streaming model invocation.
Complete(ctx context.Context, req *Request) (*Response, error)
// Stream performs a streaming model invocation when supported.
Stream(ctx context.Context, req *Request) (Streamer, error)
}
Provider is the raw provider-agnostic model transport. Provider adapters translate Requests into provider calls and return normalized Responses and Chunks. Callers should construct a validated Client with NewClient instead of consuming a Provider directly.
type ProviderError ¶
type ProviderError struct {
// contains filtered or unexported fields
}
ProviderError describes a failure returned by a model provider (e.g. Bedrock). It is intended to cross package boundaries so runtimes can surface stable, structured information to callers.
func AsProviderError ¶
func AsProviderError(err error) (*ProviderError, bool)
AsProviderError returns the first ProviderError in err's chain, if any.
func NewProviderError ¶
func NewProviderError(provider, operation string, httpStatus int, kind ProviderErrorKind, code, message, requestID string, retryable bool, cause error) *ProviderError
NewProviderError constructs a ProviderError. provider and kind are required. cause may be nil but is recommended to preserve the original error chain.
func (*ProviderError) Code ¶
func (e *ProviderError) Code() string
Code returns the provider-specific error code when available.
func (*ProviderError) Error ¶
func (e *ProviderError) Error() string
func (*ProviderError) HTTPStatus ¶
func (e *ProviderError) HTTPStatus() int
HTTPStatus returns the provider HTTP status code when available, otherwise 0.
func (*ProviderError) Kind ¶
func (e *ProviderError) Kind() ProviderErrorKind
Kind returns the coarse-grained provider error classification.
func (*ProviderError) Message ¶
func (e *ProviderError) Message() string
Message returns the provider error message when available.
func (*ProviderError) Operation ¶
func (e *ProviderError) Operation() string
Operation returns the provider operation name when known (for example, "converse_stream").
func (*ProviderError) Provider ¶
func (e *ProviderError) Provider() string
Provider returns the provider identifier (for example, "bedrock").
func (*ProviderError) RequestID ¶
func (e *ProviderError) RequestID() string
RequestID returns the provider request identifier when available.
func (*ProviderError) Retryable ¶
func (e *ProviderError) Retryable() bool
Retryable reports whether retrying the call may succeed without changing the request.
func (*ProviderError) Unwrap ¶
func (e *ProviderError) Unwrap() error
Unwrap returns the underlying provider error to preserve the original error chain.
type ProviderErrorKind ¶
type ProviderErrorKind string
ProviderErrorKind classifies provider failures into a small set of categories suitable for retry and UX decisions.
const ( // ProviderErrorKindAuth indicates authentication/authorization failures. ProviderErrorKindAuth ProviderErrorKind = "auth" // ProviderErrorKindInvalidRequest indicates the request is invalid and retrying // without changing the request will not succeed. ProviderErrorKindInvalidRequest ProviderErrorKind = "invalid_request" // ProviderErrorKindRateLimited indicates the provider is throttling requests. ProviderErrorKindRateLimited ProviderErrorKind = "rate_limited" // network issues) where a retry may succeed. ProviderErrorKindUnavailable ProviderErrorKind = "unavailable" // ProviderErrorKindUnknown indicates an unclassified provider failure. ProviderErrorKindUnknown ProviderErrorKind = "unknown" )
type Request ¶
type Request struct {
// RunID identifies the logical run for this request when available.
RunID string
// Model is the provider-specific model identifier when specified.
Model string
// ModelClass selects a model family when Model is not specified.
ModelClass ModelClass
// PromptRefs records the exact prompts and versions used to build this
// request. Runtime components and observers use this for provenance and
// auditing; provider adapters must treat it as metadata and must not
// translate it to provider wire payloads.
PromptRefs []prompt.PromptRef
// Messages is the ordered transcript provided to the model.
Messages []*Message
// Temperature controls sampling when supported by the provider.
Temperature float32
// Tools lists the tool definitions available to the model.
Tools []*ToolDefinition
// ToolChoice optionally constrains how the model uses tools.
ToolChoice *ToolChoice
// MaxTokens caps the number of output tokens when supported.
MaxTokens int
// StructuredOutput constrains assistant output to a JSON schema when the
// provider supports native structured-output enforcement.
StructuredOutput *StructuredOutput
// Stream requests streaming responses when true and supported.
Stream bool
// Thinking configures provider-specific reasoning behavior.
Thinking *ThinkingOptions
// Cache configures prompt caching behavior. Nil means no caching.
Cache *CacheOptions
// contains filtered or unexported fields
}
Request captures inputs for a model invocation.
func CloneRequest ¶
CloneRequest returns a bounded, ownership-safe copy of a model request.
type RequestContract ¶
type RequestContract struct {
// contains filtered or unexported fields
}
RequestContract is an immutable snapshot of the request fields that decide whether provider output can be accepted.
func NewRequestContract ¶
func NewRequestContract(req *Request) (*RequestContract, error)
NewRequestContract validates req and snapshots the exact tool and structured output contracts used to accept provider output.
func (*RequestContract) ValidateResponse ¶
func (c *RequestContract) ValidateResponse(resp *Response) (*Response, error)
ValidateResponse validates a complete provider response against the captured request contract and returns a detached response on success.
func (*RequestContract) ValidateStream ¶
func (c *RequestContract) ValidateStream(stream Streamer) (ValidatedStreamer, error)
ValidateStream wraps a raw provider stream with request-scoped chunk and terminal validation, canonical response access, and serialized finalization.
type Response ¶
type Response struct {
// Content is the ordered list of assistant messages produced.
Content []Message
// ToolCalls lists tool invocations requested by the model.
ToolCalls []ToolCall
// Usage reports token consumption for the request.
Usage TokenUsage
// StopReason records why generation stopped (provider-specific).
StopReason string
// OutputLimited reports that the provider stopped because generated
// output reached its configured token or context limit. A validated
// client never exposes such a response as successful completed work.
OutputLimited bool
}
Response is the canonical result of a model invocation.
Content carries assistant messages; ToolCalls holds any tool invocations requested by the model; Usage and StopReason mirror provider metadata.
func CloneResponse ¶
CloneResponse returns a bounded, ownership-safe copy of a model response.
type ResponseEvidence ¶
ResponseEvidence is bounded identity for rejected output. It intentionally contains no response content.
type StreamResponseBuilder ¶
type StreamResponseBuilder struct {
// contains filtered or unexported fields
}
StreamResponseBuilder assembles a provider-owned terminal response from the chunks emitted by one provider stream. A builder is not safe for concurrent use.
func (*StreamResponseBuilder) Add ¶
func (b *StreamResponseBuilder) Add(chunk Chunk) error
Add records one chunk in the provider's terminal response.
func (*StreamResponseBuilder) Response ¶
func (b *StreamResponseBuilder) Response() *Response
Response returns the assembled response after a stop chunk. It returns nil while the stream is incomplete.
type Streamer ¶
type Streamer interface {
// Recv returns the next streaming chunk or an error.
Recv() (Chunk, error)
// Close releases any resources associated with the stream.
Close() error
// Response returns the provider-owned terminal response after Recv has
// returned a literal io.EOF. It returns nil before then or after failure.
Response() *Response
}
Streamer delivers incremental model output.
Raw provider owners must drain the stream until Recv returns io.EOF or another terminal error, then call Close. Consumers receive a ValidatedStreamer and call Finalize instead.
type StructuredOutput ¶
type StructuredOutput struct {
// Name identifies the response format for providers that require one.
Name string
// Schema is the JSON Schema payload sent to the provider.
Schema rawjson.Message
}
StructuredOutput configures provider-enforced JSON output when supported. Schema must contain a JSON Schema object accepted by the target provider.
type TextChunk ¶
type TextChunk struct {
Message Message
}
TextChunk carries one incremental assistant message.
type TextPart ¶
type TextPart struct {
// Text is the human-readable content for this part.
Text string
}
TextPart is a plain text content block in a message.
Text is emitted as-is to the UI or consumer when the message is rendered.
type ThinkingChunk ¶
type ThinkingChunk struct {
Message Message
}
ThinkingChunk carries one incremental assistant thinking message.
func (ThinkingChunk) Kind ¶
func (ThinkingChunk) Kind() string
Kind returns the thinking event discriminator.
type ThinkingOptions ¶
type ThinkingOptions struct {
// Enable turns provider thinking features on when supported.
Enable bool
// Interleaved requests interleaved thinking and assistant content when
// supported.
Interleaved bool
// BudgetTokens caps the number of thinking tokens when supported.
BudgetTokens int
}
ThinkingOptions configures provider thinking behavior.
type ThinkingPart ¶
type ThinkingPart struct {
// Text is the provider-visible reasoning text when available.
Text string
// Signature is the provider-issued signature for Text when present.
Signature string
// Redacted carries provider-issued reasoning content in redacted form
// when plaintext Text is not available.
Redacted []byte
// Index is the position of this block in the reasoning sequence.
Index int
// Final reports whether this reasoning block is the last one for the
// current turn.
Final bool
}
ThinkingPart represents provider-issued reasoning content.
Providers may attach a signature or redacted payload; callers treat this as opaque metadata and surface it according to UI policy.
type TokenCount ¶
type TokenCount struct {
// Model is the provider-resolved model identifier that would receive this
// request. It is empty when an estimator cannot resolve the concrete model.
Model string
// ModelClass is the logical model family requested by the caller.
ModelClass ModelClass
// InputTokens is the number of input tokens counted or estimated.
InputTokens int
// Exact reports whether InputTokens came from provider-native counting.
Exact bool
}
TokenCount reports preflight input-token usage for a model request.
type TokenCounter ¶
type TokenCounter interface {
CountTokens(ctx context.Context, req *Request) (TokenCount, error)
}
TokenCounter is an optional model-client capability for preflight token counting. Provider adapters that can count tokens natively should implement it with Exact=true. Local estimators should report Exact=false so callers can reject approximation when enforcing hard context-window contracts.
Counts measure the durable transcript. Implementations exclude replayed thinking blocks from the counted surface because thinking signatures are provider-specific replay metadata, not portable user-visible input.
type TokenEstimator ¶
type TokenEstimator struct {
// CharactersPerToken is the approximate byte-to-token conversion ratio.
// When zero, the estimator uses three characters per token.
CharactersPerToken int
// OverheadTokens adds a fixed allowance for provider framing and request
// fields that are not represented directly in message text.
// When zero, the estimator uses 500 tokens.
OverheadTokens int
// MinimumTokens is the minimum non-zero estimate returned for tiny
// requests. When zero, the estimator uses 500 tokens.
MinimumTokens int
}
TokenEstimator provides an explicit local fallback for clients that cannot count tokens natively. It measures the provider-visible request surface by byte size and converts it with a conservative character-per-token ratio.
func (TokenEstimator) CountTokens ¶
func (e TokenEstimator) CountTokens(ctx context.Context, req *Request) (TokenCount, error)
CountTokens estimates req's input-token usage with Exact=false. It is intended for explicit fallback paths such as rate limiting or non-native providers, not for provider-specific billing or hard context-window guarantees.
type TokenUsage ¶
type TokenUsage struct {
// Model is the provider-resolved model identifier that produced this
// usage (e.g., "us.anthropic.claude-sonnet-4-20250514-v1:0"). Set by
// the model adapter; empty when the adapter does not report it.
Model string
// ModelClass is the logical model family that was requested (e.g.,
// "default", "high-reasoning", "small"). Set by the model adapter
// from the original request.
ModelClass ModelClass
// InputTokens is the number of uncached input tokens consumed. It does
// not include CacheReadTokens or CacheWriteTokens.
InputTokens int
// OutputTokens is the number of tokens produced by outputs.
OutputTokens int
// TotalTokens is the total number of tokens consumed by the call. When
// non-zero, it equals InputTokens + OutputTokens + CacheReadTokens +
// CacheWriteTokens.
TotalTokens int
// CacheReadTokens is the number of input tokens read from cache. This
// count is disjoint from InputTokens.
CacheReadTokens int
// CacheWriteTokens is the number of input tokens written to cache. This
// count is disjoint from InputTokens.
CacheWriteTokens int
}
TokenUsage tracks token counts and model attribution for a single model invocation. The counts are additive; the identity fields (Model, ModelClass) describe the source of the delta and are not aggregated by addTokenUsage.
type ToolCall ¶
type ToolCall struct {
// Name is the tool identifier requested by the model.
Name tools.Ident
// Payload is the canonical JSON arguments supplied by the model.
//
// Provider adapters MUST populate this as a valid JSON value. Planners and
// runtimes treat it as opaque JSON and rely on codecs for any schema-aware
// decoding. The RawJSON type exists to make workflow-boundary encoding safe
// (it normalizes empty payloads to JSON null).
Payload rawjson.Message
// ID is an optional provider-issued identifier for the tool call.
ID string
}
ToolCall is a requested tool invocation from the model.
Tool calls capture the tool identity, raw arguments, and an optional provider-issued call identifier.
type ToolCallChunk ¶
type ToolCallChunk struct {
ToolCall ToolCall
}
ToolCallChunk carries one complete model tool call.
func (ToolCallChunk) Kind ¶
func (ToolCallChunk) Kind() string
Kind returns the tool-call event discriminator.
type ToolCallDelta ¶
type ToolCallDelta struct {
// Name is the canonical tool identifier for this delta stream.
//
// Provider adapters MUST populate Name for every emitted delta so
// downstream consumers can render tool-specific previews deterministically.
Name tools.Ident
// ID is the provider-issued tool call identifier used to correlate all
// deltas and the final ToolCall payload.
ID string
// Delta is a raw JSON fragment emitted by the provider.
Delta string
}
ToolCallDelta is an incremental tool-call payload fragment streamed by providers while they are still constructing the full tool input JSON.
Contract:
- This is a best-effort UX signal. Consumers may ignore it entirely.
- The canonical tool payload remains ToolCall.Payload in the final ChunkTypeToolCall emitted once the provider closes the tool block.
- Delta is not guaranteed to be valid JSON on its own; callers must treat it as an opaque fragment suitable only for progressive UI previews.
type ToolCallDeltaChunk ¶
type ToolCallDeltaChunk struct {
Delta ToolCallDelta
}
ToolCallDeltaChunk carries one incremental tool-call payload fragment.
func (ToolCallDeltaChunk) Kind ¶
func (ToolCallDeltaChunk) Kind() string
Kind returns the tool-call-delta event discriminator.
type ToolChoice ¶
type ToolChoice struct {
// Mode selects the desired tool behavior for the request.
Mode ToolChoiceMode
// Name identifies the tool to request when Mode is ToolChoiceModeTool.
// It must match the Name of one of the tool definitions in Request.Tools.
Name string
}
ToolChoice configures optional tool-use behavior for a Request.
When ToolChoice is nil, providers use their default tool behavior (typically auto-selection). When non-nil, providers apply the requested mode or fail fast if the mode is not supported.
type ToolChoiceMode ¶
type ToolChoiceMode string
ToolChoiceMode controls how the model uses tools for a request.
Not all providers support all modes. Provider adapters fail fast when a mode is not supported rather than silently degrading behavior.
const ( // ToolChoiceModeAuto lets the provider decide whether to call tools or // respond with text. This is the default when ToolChoice is nil. ToolChoiceModeAuto ToolChoiceMode = "auto" // ToolChoiceModeNone disables tool use for the request when supported by // the provider. ToolChoiceModeNone ToolChoiceMode = "none" // ToolChoiceModeAny forces the model to request at least one tool when // supported by the provider. ToolChoiceModeAny ToolChoiceMode = "any" // ToolChoiceModeTool forces the model to request the specific tool // identified by ToolChoice.Name when supported by the provider. ToolChoiceModeTool ToolChoiceMode = "tool" )
type ToolDefinition ¶
type ToolDefinition struct {
// Name is the tool identifier as seen by the model.
Name string
// Description is a concise summary presented to the model to decide
// when to call the tool.
Description string
// InputSchema is a JSON Schema describing the tool input payload.
InputSchema any
}
ToolDefinition describes a tool exposed to the model.
Definitions are derived from Goa tool specifications and include the name, description, and JSON Schema input.
func CloneToolDefinitions ¶
func CloneToolDefinitions(definitions []*ToolDefinition) ([]*ToolDefinition, error)
CloneToolDefinitions returns a bounded, ownership-safe copy of tool definitions and their input schemas.
type ToolResultPart ¶
type ToolResultPart struct {
// ToolUseID correlates this result to a prior tool use declaration.
ToolUseID string
// Content is the provider-facing tool result payload.
//
// Contract:
// - Successful results carry a decoded JSON-compatible value.
// - Failed results carry plain error text with IsError=true.
// - Runtime/tooling code must not store raw JSON bytes or Go error
// structs here.
Content any
// IsError reports whether Content represents an error from the tool.
IsError bool
}
ToolResultPart carries a tool result provided by the user side.
Tool results are attached to user messages so the model can read them in subsequent turns.
type ToolUsePart ¶
type ToolUsePart struct {
// ID uniquely identifies this tool call within the run.
ID string
// Name is the tool identifier requested by the model.
Name string
// Input is the JSON-compatible arguments object provided by the model.
Input any
}
ToolUsePart declares a tool invocation by the assistant.
The planner/runtime turns these declarations into concrete tool executions and correlates results via ToolResultPart.ToolUseID.
type UsageChunk ¶
type UsageChunk struct {
Usage TokenUsage
}
UsageChunk carries one incremental token-usage record.
func (UsageChunk) Kind ¶
func (UsageChunk) Kind() string
Kind returns the usage event discriminator.
type ValidatedStreamer ¶
ValidatedStreamer is the complete consumer-facing stream contract returned by clients constructed with NewClient. Response is available only after a clean EOF. Finalize combines the caller's terminal result with exactly-once provider cleanup.