Documentation
¶
Overview ¶
Package chat provides a client for the stateless Chat API.
The Chat API is a pure function: f(messages, context) → stream of blocks. It doesn't read or write messages to any database. The client sends the full conversation history on every request and receives a streamed response.
Message persistence is handled separately by the caller.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UserFacingStreamError ¶ added in v1.22.1
UserFacingStreamError returns concise error copy suitable for toast UI.
Types ¶
type Client ¶
type Client interface {
// StreamSnapshots sends the conversation to the Chat API and streams normalized snapshots.
// Each snapshot includes scoped progress metadata (conversation/turn/seq/status).
StreamSnapshots(ctx context.Context, req Request, onSnapshot func(StreamSnapshot)) (*StreamResult, error)
// Stream sends the conversation to the Chat API and streams the response.
// The onMessage callback is called each time the message is updated with new content.
// The returned StreamResult contains the final message and any metadata.
Stream(ctx context.Context, req Request, onMessage func(*domain.Message)) (*StreamResult, error)
// SetAccountID sets the account ID for requests.
SetAccountID(accountID domain.AccountID)
// WithAccountID returns a new client scoped to accountID.
WithAccountID(accountID domain.AccountID) Client
}
Client sends messages to the Chat API and streams responses.
type EventType ¶
type EventType string
EventType identifies the kind of SSE event from the Chat API.
const ( EventTypeMessageStart EventType = "message_start" EventTypeTextDelta EventType = "text_delta" EventTypeThinkingDelta EventType = "thinking_delta" EventTypeToolUse EventType = "tool_use" EventTypeToolInputDelta EventType = "tool_input_delta" EventTypeContentBlockStop EventType = "content_block_stop" EventTypeMessageStop EventType = "message_stop" EventTypeMetadataUpdate EventType = "metadata_update" )
type Property ¶
type Property struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
Items *Items `json:"items,omitempty"`
}
Property defines a single property in a JSON Schema.
type Request ¶
type Request struct {
ConversationID string `json:"conversation_id"`
Messages []domain.Message `json:"messages"`
Context []domain.ContextEntity `json:"context,omitempty"`
Tools []Tool `json:"tools"`
}
Request is the input to the Chat API. The client sends the full conversation history on every request.
type Schema ¶
type Schema struct {
Type string `json:"type"`
Properties map[string]Property `json:"properties"` // Always required by Anthropic API
Required []string `json:"required,omitempty"`
}
Schema defines the JSON Schema for tool input.
func NewObjectSchema ¶
NewObjectSchema creates an object schema with the given properties.
func (Schema) MarshalJSON ¶
MarshalJSON ensures Properties is never null (Anthropic API requires it).
type StreamErrorClass ¶ added in v1.22.1
type StreamErrorClass string
StreamErrorClass is a normalized category for stream failures.
const ( StreamErrorClassCancelled StreamErrorClass = "cancelled" StreamErrorClassTimeout StreamErrorClass = "timeout" StreamErrorClassProtocol StreamErrorClass = "protocol_error" StreamErrorClassServer StreamErrorClass = "server_error" StreamErrorClassUnknown StreamErrorClass = "unknown" )
func ClassifyStreamError ¶ added in v1.22.1
func ClassifyStreamError(err error) StreamErrorClass
ClassifyStreamError maps raw stream errors into stable operational buckets.
type StreamMetadata ¶
type StreamMetadata struct {
Title string // AI-generated conversation title, set after first exchange
ContextWindow int // Model's max token capacity (from message_start)
InputTokens int // Tokens consumed by input this turn (from message_stop)
OutputTokens int // Tokens generated by output this turn (from message_stop)
}
StreamMetadata contains post-stream metadata from the Chat API.
type StreamResult ¶
type StreamResult struct {
Message *domain.Message
Metadata *StreamMetadata // nil if no metadata_update event was received
}
StreamResult captures everything the stream produced.
type StreamSnapshot ¶ added in v1.22.1
type StreamSnapshot struct {
ConversationID string
TurnID string
Seq int
Status StreamStatus
AbortReason string
Done bool
Message *domain.Message
Metadata *StreamMetadata
}
type StreamStatus ¶ added in v1.22.1
type StreamStatus string
const ( StreamStatusStreaming StreamStatus = "streaming" StreamStatusCompleted StreamStatus = "completed" StreamStatusToolUse StreamStatus = "tool_use" StreamStatusAborted StreamStatus = "aborted" )