Documentation
¶
Overview ¶
Package types defines shared data structures and interfaces.
Package types defines shared data structures and interfaces.
Package types defines shared data structures and interfaces.
Index ¶
- func TextContent(s string) json.RawMessage
- type APIError
- type CacheControl
- type ChatCompletionChunk
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatContentPart
- type ChatMessage
- type Choice
- type ContentBlock
- type ContentBlockDelta
- type Delta
- type ErrorDetails
- type ErrorResponse
- type FunctionCall
- type FunctionDef
- type GeminiCandidate
- type GeminiContent
- type GeminiFunctionDeclaration
- type GeminiGenerationConfig
- type GeminiPart
- type GeminiRequest
- type GeminiResponse
- type GeminiStreamChunk
- type GeminiTool
- type GeminiUsage
- type ImageSource
- type ImageURL
- type Message
- type MessageEvent
- type MessageRequest
- type MessageResponse
- type Metadata
- type ResponsesChunk
- type ResponsesContent
- type ResponsesInput
- type ResponsesOutput
- type ResponsesReasoning
- type ResponsesRequest
- type ResponsesResponse
- type ResponsesTool
- type ResponsesUsage
- type StreamOptions
- type SystemContentBlock
- type Tool
- type ToolCall
- type ToolDef
- type ToolResult
- type Usage
- type UsageInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TextContent ¶
func TextContent(s string) json.RawMessage
TextContent is a helper to create a json.RawMessage for a plain-text content value.
Types ¶
type CacheControl ¶
type CacheControl struct {
Type string `json:"type"`
}
CacheControl represents cache control directives.
type ChatCompletionChunk ¶
type ChatCompletionChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage *UsageInfo `json:"usage,omitempty"`
}
ChatCompletionChunk represents a streaming chunk from the Chat Completions API.
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Stream *bool `json:"stream,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
ReasoningEffort *string `json:"reasoning_effort,omitempty"`
Thinking json.RawMessage `json:"thinking,omitempty"`
Tools []ToolDef `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Stop interface{} `json:"stop,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"`
}
ChatCompletionRequest represents a request to the OpenAI Chat Completions API.
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage UsageInfo `json:"usage"`
}
ChatCompletionResponse represents a response from the OpenAI Chat Completions API.
type ChatContentPart ¶
type ChatContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
}
ChatContentPart represents a single part in a multimodal message content array.
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
ReasoningContent *string `json:"reasoning_content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Name string `json:"name,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}
ChatMessage represents a single message in the conversation. Content can be either a plain text string or an array of ChatContentPart for multimodal messages containing images.
func (ChatMessage) ContentText ¶
func (m ChatMessage) ContentText() string
ContentText extracts the text content from the message, handling both plain text strings and multimodal content arrays (where it concatenates all text-type parts). Returns empty string if content is absent or unparseable. This is intentional — ContentText is a best-effort text extractor for routing and display purposes, not a strict parser. Failures are never fatal; callers should handle empty returns gracefully.
type Choice ¶
type Choice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason,omitempty"`
Delta ChatMessage `json:"delta,omitempty"`
}
Choice represents a single choice in the response.
type ContentBlock ¶
type ContentBlock struct {
Type string `json:"type"`
Text string `json:"text"`
ID string `json:"id,omitempty"` // For tool_use (the tool call ID)
ToolUseID string `json:"tool_use_id,omitempty"` // For tool_result (references the tool_use ID)
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
Output json.RawMessage `json:"output,omitempty"` // Deprecated: use Content
Content json.RawMessage `json:"content,omitempty"` // For tool_result inner content
IsError *bool `json:"is_error,omitempty"` // For tool_result
Thinking string `json:"thinking"` // For thinking blocks
Signature string `json:"signature,omitempty"` // For thinking blocks
Source *ImageSource `json:"source,omitempty"` // For image blocks
}
ContentBlock represents a block of content within a message. The Type field determines which other fields are populated:
- "text": Text is populated
- "tool_use": ID, Name, Input are populated
- "tool_result": ToolUseID, Content, IsError are populated
- "thinking": Thinking, Signature are populated
- "image": Source is populated
func (*ContentBlock) GetToolID ¶
func (b *ContentBlock) GetToolID() string
GetToolID returns the appropriate tool ID for this block type. For tool_use: returns ID. For tool_result: returns ToolUseID.
func (ContentBlock) MarshalJSON ¶
func (b ContentBlock) MarshalJSON() ([]byte, error)
MarshalJSON customizes JSON serialization of ContentBlock to conform strictly to the schema required by the Anthropic Messages API for each specific block type.
func (*ContentBlock) TextContent ¶
func (b *ContentBlock) TextContent() string
TextContent extracts text from a tool_result's content field. The content field can be a string or an array of content blocks.
type ContentBlockDelta ¶
type ContentBlockDelta struct {
Type string `json:"type"`
Index int `json:"index"`
Delta Delta `json:"delta"`
}
ContentBlockDelta represents a streaming delta for a content block.
type Delta ¶
type Delta struct {
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
Thinking string `json:"thinking,omitempty"`
PartialJSON string `json:"partial_json,omitempty"`
StopReason string `json:"stop_reason,omitempty"`
}
Delta represents a partial update in a streaming response.
type ErrorDetails ¶
type ErrorDetails struct {
Type string `json:"type"`
Message string `json:"message"`
Code string `json:"code,omitempty"`
}
ErrorDetails contains the details of an API error.
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorDetails `json:"error"`
}
ErrorResponse represents an error response from the OpenAI API.
type FunctionCall ¶
FunctionCall represents the function invocation details.
type FunctionDef ¶
type FunctionDef struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
}
FunctionDef represents the function definition schema.
type GeminiCandidate ¶
type GeminiCandidate struct {
Content GeminiContent `json:"content"`
FinishReason string `json:"finishReason,omitempty"`
}
GeminiCandidate represents a response candidate.
type GeminiContent ¶
type GeminiContent struct {
Role string `json:"role"`
Parts []GeminiPart `json:"parts"`
}
GeminiContent represents a single content item.
type GeminiFunctionDeclaration ¶
type GeminiFunctionDeclaration struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
}
GeminiFunctionDeclaration represents a function declaration.
type GeminiGenerationConfig ¶
type GeminiGenerationConfig struct {
Temperature float64 `json:"temperature,omitempty"`
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
}
GeminiGenerationConfig controls generation parameters.
type GeminiPart ¶
type GeminiPart struct {
Text string `json:"text,omitempty"`
}
GeminiPart represents a part of a content item.
type GeminiRequest ¶
type GeminiRequest struct {
Contents []GeminiContent `json:"contents"`
GenerationConfig *GeminiGenerationConfig `json:"generationConfig,omitempty"`
Tools []GeminiTool `json:"tools,omitempty"`
Stream bool `json:"stream,omitempty"`
}
GeminiRequest represents a request to the Gemini API.
type GeminiResponse ¶
type GeminiResponse struct {
Candidates []GeminiCandidate `json:"candidates"`
UsageMetadata *GeminiUsage `json:"usageMetadata,omitempty"`
}
GeminiResponse represents a response from the Gemini API.
type GeminiStreamChunk ¶
type GeminiStreamChunk struct {
Candidates []GeminiCandidate `json:"candidates"`
UsageMetadata *GeminiUsage `json:"usageMetadata,omitempty"`
}
GeminiStreamChunk represents a streaming chunk from the Gemini API.
type GeminiTool ¶
type GeminiTool struct {
FunctionDeclarations []GeminiFunctionDeclaration `json:"functionDeclarations,omitempty"`
}
GeminiTool represents a tool definition.
type GeminiUsage ¶
type GeminiUsage struct {
PromptTokenCount int `json:"promptTokenCount"`
CandidatesTokenCount int `json:"candidatesTokenCount"`
TotalTokenCount int `json:"totalTokenCount"`
}
GeminiUsage represents token usage in a Gemini response.
type ImageSource ¶
type ImageSource struct {
Type string `json:"type"`
MediaType string `json:"media_type"`
Data string `json:"data"`
URL string `json:"url,omitempty"`
}
ImageSource represents an image source for content blocks.
type ImageURL ¶
type ImageURL struct {
URL string `json:"url"`
}
ImageURL represents the URL source for an image in a multimodal message.
type Message ¶
type Message struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
}
Message represents a single message in the conversation. Content can be a string or an array of content blocks.
func (*Message) ContentBlocks ¶
func (m *Message) ContentBlocks() []ContentBlock
ContentBlocks parses the message content into content blocks. Handles both string content and array-of-blocks content:
"content": "hello"
"content": [{"type":"text","text":"hello"}]
type MessageEvent ¶
type MessageEvent struct {
Type string `json:"type"`
Message *MessageResponse `json:"message,omitempty"`
Index *int `json:"index,omitempty"`
ContentBlock *ContentBlock `json:"content_block,omitempty"`
Delta *Delta `json:"delta,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Error *APIError `json:"error,omitempty"`
}
MessageEvent represents a Server-Sent Event from the streaming API.
type MessageRequest ¶
type MessageRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
System json.RawMessage `json:"system,omitempty"`
Messages []Message `json:"messages"`
Stream *bool `json:"stream,omitempty"`
Tools []Tool `json:"tools,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
Thinking json.RawMessage `json:"thinking,omitempty"`
}
MessageRequest represents a request to the Anthropic Messages API.
func (*MessageRequest) SystemText ¶
func (r *MessageRequest) SystemText() string
SystemText extracts the system prompt text from the raw system field. Anthropic accepts system as either a string or an array of content blocks:
"system": "You are helpful"
"system": [{"type":"text","text":"You are helpful","cache_control":...}]
func (*MessageRequest) Validate ¶
func (r *MessageRequest) Validate() error
Validate checks that the MessageRequest has required fields.
type MessageResponse ¶
type MessageResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Content []ContentBlock `json:"content"`
Model string `json:"model"`
StopReason string `json:"stop_reason,omitempty"`
StopSequence string `json:"stop_sequence,omitempty"`
Usage Usage `json:"usage"`
}
MessageResponse represents a response from the Anthropic Messages API.
type Metadata ¶
type Metadata struct {
UserID string `json:"user_id,omitempty"`
}
Metadata contains optional metadata for the request.
type ResponsesChunk ¶
type ResponsesChunk struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
Delta string `json:"delta,omitempty"`
Output []ResponsesOutput `json:"output,omitempty"`
Usage *ResponsesUsage `json:"usage,omitempty"`
}
ResponsesChunk represents a streaming chunk from the Responses API.
type ResponsesContent ¶
ResponsesContent represents content within an output item.
type ResponsesInput ¶
type ResponsesInput struct {
Role string `json:"role"`
Content json.RawMessage `json:"content,omitempty"`
}
ResponsesInput represents a single input item in the Responses request.
type ResponsesOutput ¶
type ResponsesOutput struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
Role string `json:"role,omitempty"`
Content []ResponsesContent `json:"content,omitempty"`
CallID string `json:"call_id,omitempty"`
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
ResponsesOutput represents a single output item.
type ResponsesReasoning ¶
type ResponsesReasoning struct {
Effort string `json:"effort,omitempty"`
}
ResponsesReasoning controls reasoning effort.
type ResponsesRequest ¶
type ResponsesRequest struct {
Model string `json:"model"`
Input []ResponsesInput `json:"input"`
Stream bool `json:"stream,omitempty"`
Tools []ResponsesTool `json:"tools,omitempty"`
Reasoning *ResponsesReasoning `json:"reasoning,omitempty"`
}
ResponsesRequest represents a request to the OpenAI Responses API.
type ResponsesResponse ¶
type ResponsesResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Output []ResponsesOutput `json:"output"`
Usage ResponsesUsage `json:"usage"`
}
ResponsesResponse represents a response from the OpenAI Responses API.
type ResponsesTool ¶
type ResponsesTool struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
}
ResponsesTool represents a tool definition for the Responses API.
type ResponsesUsage ¶
type ResponsesUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
ResponsesUsage represents token usage in a Responses response.
type StreamOptions ¶
type StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitempty"`
}
StreamOptions controls streaming response metadata from OpenAI-compatible APIs.
type SystemContentBlock ¶
type SystemContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
CacheControl *CacheControl `json:"cache_control,omitempty"`
}
SystemContentBlock represents a content block in the system array.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema json.RawMessage `json:"input_schema"`
}
Tool represents a tool definition for function calling.
type ToolCall ¶
type ToolCall struct {
Index int `json:"index,omitempty"`
ID string `json:"id"`
Type string `json:"type"`
Function FunctionCall `json:"function"`
}
ToolCall represents a function call made by the model. Index is only present in streaming deltas — it identifies which tool call position this delta belongs to within the tool_calls array.
type ToolDef ¶
type ToolDef struct {
Type string `json:"type"`
Function FunctionDef `json:"function"`
}
ToolDef represents a tool definition for function calling.
type ToolResult ¶
type ToolResult struct {
ToolUseID string `json:"tool_use_id"`
Content string `json:"content"`
IsError bool `json:"is_error,omitempty"`
}
ToolResult represents the result of a tool execution.
type Usage ¶
type Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"`
}
Usage represents token usage information.
type UsageInfo ¶
type UsageInfo struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
PromptCacheHitTokens int `json:"prompt_cache_hit_tokens,omitempty"`
PromptCacheMissTokens int `json:"prompt_cache_miss_tokens,omitempty"`
}
UsageInfo represents token usage information.