api

package
v0.13.11 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError added in v0.13.11

type APIError struct {
	StatusCode int
	ErrorType  string
	Message    string
}

APIError is a non-success response returned by the Claude API.

Callers can use errors.As to inspect the HTTP status and Anthropic error type without parsing the human-readable message.

func (*APIError) Error added in v0.13.11

func (e *APIError) Error() string

type BaseContent

type BaseContent struct {
	Type_ ContentType `json:"type"`
}

func (BaseContent) MarshalZerologObject

func (bc BaseContent) MarshalZerologObject(e *zerolog.Event)

type Client

type Client struct {
	APIVersion string
	BaseURL    string
	// contains filtered or unexported fields
}

Client represents the Claude API client.

func NewClient

func NewClient(apiKey string, baseURL string, apiVersion ...string) *Client

NewClient initializes and returns a new API client.

func (*Client) Complete

func (c *Client) Complete(req *Request) (*SuccessfulResponse, error)

Complete sends a completion request and returns the response.

func (*Client) CountTokens added in v0.10.9

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, req *MessageRequest) (*MessageResponse, error)

SendMessage sends a message request and returns the response.

func (*Client) SetBearerAuthorization added in v0.13.7

func (c *Client) SetBearerAuthorization(value string)

SetBearerAuthorization adds the verified bearer form for gateways which require it in addition to the Anthropic x-api-key header. It is a Go-only runtime option and is never read from inference settings.

func (*Client) SetHTTPClient added in v0.10.9

func (c *Client) SetHTTPClient(httpClient *http.Client)

func (*Client) SetOAuthBearerAuthorization added in v0.13.7

func (c *Client) SetOAuthBearerAuthorization(value string)

SetOAuthBearerAuthorization configures Anthropic subscription OAuth mode: bearer authorization plus the Claude Code identity/beta headers observed in the provider implementation. It deliberately does not send x-api-key.

func (*Client) SetOutboundURLOptions added in v0.13.4

func (c *Client) SetOutboundURLOptions(opts security.OutboundURLOptions)

func (*Client) StreamComplete

func (c *Client) StreamComplete(req *Request) (<-chan Event, error)

func (*Client) StreamMessage

func (c *Client) StreamMessage(ctx context.Context, req *MessageRequest) (<-chan StreamingEvent, error)

StreamMessage sends a message request and returns a channel of Events for streaming responses.

type Content

type Content interface {
	Type() ContentType
}

func NewImageContent

func NewImageContent(mediaType, base64Data string) Content

func NewTextContent

func NewTextContent(text string) Content

func NewThinkingContent added in v0.13.3

func NewThinkingContent(thinking, signature string) Content

func NewToolResultContent

func NewToolResultContent(toolUseID, content string) Content

func NewToolUseContent

func NewToolUseContent(toolID, toolName string, toolInput string) Content

func UnmarshalContent added in v0.4.40

func UnmarshalContent(data []byte) (Content, error)

type ContentBlock

type ContentBlock struct {
	Type      ContentType `json:"type"`
	ID        string      `json:"id,omitempty"`
	Name      string      `json:"name,omitempty"`
	Input     interface{} `json:"input,omitempty"` // Can be string for text or object for tool_use
	Text      string      `json:"text,omitempty"`
	Thinking  string      `json:"thinking,omitempty"`
	Signature string      `json:"signature,omitempty"`
}

func (ContentBlock) MarshalZerologObject

func (cb ContentBlock) MarshalZerologObject(e *zerolog.Event)

type ContentType

type ContentType string
const (
	ContentTypeText       ContentType = "text"
	ContentTypeImage      ContentType = "image"
	ContentTypeToolUse    ContentType = "tool_use"
	ContentTypeToolResult ContentType = "tool_result"
	ContentTypeThinking   ContentType = "thinking"
)

type Delta

type Delta struct {
	Type         StreamingDeltaType `json:"type"`
	Text         string             `json:"text,omitempty"`
	PartialJSON  string             `json:"partial_json"`
	Thinking     string             `json:"thinking,omitempty"`
	Signature    string             `json:"signature,omitempty"`
	StopReason   string             `json:"stop_reason,omitempty"`
	StopSequence string             `json:"stop_sequence,omitempty"`
}

func (Delta) MarshalZerologObject

func (d Delta) MarshalZerologObject(e *zerolog.Event)

type Error

type Error struct {
	Type    string `json:"type"`
	Message string `json:"message"`
}

func (Error) MarshalZerologObject

func (err Error) MarshalZerologObject(e *zerolog.Event)

type ErrorDetail

type ErrorDetail struct {
	Type    string `json:"type"`
	Message string `json:"message"`
}

ErrorDetail contains error details.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse represents the API's error response.

type Event

type Event struct {
	Data  string
	Event string // message_start
	ID    string
	Retry int
}

Event represents a server-sent event.

type ImageContent

type ImageContent struct {
	BaseContent
	Source ImageSource `json:"source"`
}

func (ImageContent) MarshalZerologObject

func (ic ImageContent) MarshalZerologObject(e *zerolog.Event)

func (ImageContent) Type

func (i ImageContent) Type() ContentType

type ImageSource

type ImageSource struct {
	BaseContent
	Type      string `json:"type"`
	MediaType string `json:"media_type"`
	Data      string `json:"data"`
}

func (ImageSource) MarshalZerologObject

func (is ImageSource) MarshalZerologObject(e *zerolog.Event)

type Message

type Message struct {
	Role    string    `json:"role"`
	Content []Content `json:"content"` // Can be a string or an array of content blocks
}

Message represents a single message in the conversation.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

type MessageCountTokensRequest added in v0.10.9

type MessageCountTokensRequest struct {
	Model    string         `json:"model"`
	Messages []Message      `json:"messages"`
	System   string         `json:"system,omitempty"`
	Metadata *Metadata      `json:"metadata,omitempty"`
	Thinking *ThinkingParam `json:"thinking,omitempty"`
	Tools    []Tool         `json:"tools,omitempty"`
}

type MessageCountTokensResponse added in v0.10.9

type MessageCountTokensResponse struct {
	InputTokens int `json:"input_tokens"`
}

type MessageRequest

type MessageRequest struct {
	Model         string         `json:"model"`
	Messages      []Message      `json:"messages"`
	MaxTokens     int            `json:"max_tokens"`
	Metadata      *Metadata      `json:"metadata,omitempty"`
	StopSequences []string       `json:"stop_sequences,omitempty"`
	Stream        bool           `json:"stream"`
	System        string         `json:"system,omitempty"`
	Temperature   *float64       `json:"temperature,omitempty"`
	Thinking      *ThinkingParam `json:"thinking,omitempty"`
	Tools         []Tool         `json:"tools,omitempty"`
	TopK          *int           `json:"top_k,omitempty"`
	TopP          *float64       `json:"top_p,omitempty"`
	OutputFormat  *OutputFormat  `json:"output_format,omitempty"`
}

MessageRequest represents the Messages API request payload.

type MessageResponse

type MessageResponse struct {
	ID           string    `json:"id"`
	Type         string    `json:"type"`
	Role         string    `json:"role"`
	Content      []Content `json:"content"`
	Model        string    `json:"model"`
	StopReason   string    `json:"stop_reason,omitempty"`
	StopSequence string    `json:"stop_sequence,omitempty"`
	Usage        Usage     `json:"usage"`
}

MessageResponse represents the Messages API response payload.

func (MessageResponse) FullText

func (m MessageResponse) FullText() string

FullText is a way to quickly get the entire text of the message response, for our current streaming system which only deals with full strings.

func (MessageResponse) MarshalZerologObject

func (s MessageResponse) MarshalZerologObject(e *zerolog.Event)

func (*MessageResponse) UnmarshalJSON added in v0.4.40

func (m *MessageResponse) UnmarshalJSON(data []byte) error

type Metadata

type Metadata struct {
	UserID string `json:"user_id,omitempty"`
}

Metadata represents the metadata object for Claude API requests.

type OutputFormat added in v0.8.2

type OutputFormat struct {
	Type   string         `json:"type"`
	Name   string         `json:"name,omitempty"`
	Schema map[string]any `json:"schema,omitempty"`
}

type Request

type Request struct {
	Model             string    `json:"model"`
	Prompt            string    `json:"prompt"`
	MaxTokensToSample int       `json:"max_tokens_to_sample"`
	StopSequences     []string  `json:"stop_sequences,omitempty"`
	Temperature       *float64  `json:"temperature,omitempty"`
	TopP              *float64  `json:"top_p,omitempty"`
	TopK              *int      `json:"top_k,omitempty"`
	Metadata          *Metadata `json:"metadata,omitempty"`
	Stream            bool      `json:"stream"`
}

Request represents the completion request payload.

type StreamingDeltaType

type StreamingDeltaType string
const (
	TextDeltaType      StreamingDeltaType = "text_delta"
	InputJSONDeltaType StreamingDeltaType = "input_json_delta"
	ThinkingDeltaType  StreamingDeltaType = "thinking_delta"
	SignatureDeltaType StreamingDeltaType = "signature_delta"
)

type StreamingEvent

type StreamingEvent struct {
	Type         StreamingEventType `json:"type"`
	Message      *MessageResponse   `json:"message,omitempty"`
	Delta        *Delta             `json:"delta,omitempty"`
	Error        *Error             `json:"error,omitempty"`
	Index        int                `json:"index,omitempty"`
	Usage        *Usage             `json:"usage,omitempty"`
	ContentBlock *ContentBlock      `json:"content_block,omitempty"`
}

func (StreamingEvent) MarshalZerologObject

func (s StreamingEvent) MarshalZerologObject(e *zerolog.Event)

type StreamingEventType

type StreamingEventType string
const (
	PingType              StreamingEventType = "ping"
	MessageStartType      StreamingEventType = "message_start"
	ContentBlockStartType StreamingEventType = "content_block_start"
	ContentBlockDeltaType StreamingEventType = "content_block_delta"
	ContentBlockStopType  StreamingEventType = "content_block_stop"
	MessageDeltaType      StreamingEventType = "message_delta"
	MessageStopType       StreamingEventType = "message_stop"
	ErrorType             StreamingEventType = "error"
)

type SuccessfulResponse

type SuccessfulResponse struct {
	Completion string `json:"completion"`
	StopReason string `json:"stop_reason"`
	Model      string `json:"model"`
}

SuccessfulResponse represents the API's successful response.

type TextContent

type TextContent struct {
	BaseContent
	Text string `json:"text"`
}

func (TextContent) MarshalZerologObject

func (tc TextContent) MarshalZerologObject(e *zerolog.Event)

func (TextContent) Type

func (t TextContent) Type() ContentType

type ThinkingContent added in v0.13.3

type ThinkingContent struct {
	BaseContent
	Thinking  string `json:"thinking,omitempty"`
	Signature string `json:"signature,omitempty"`
}

func (ThinkingContent) MarshalZerologObject added in v0.13.3

func (tc ThinkingContent) MarshalZerologObject(e *zerolog.Event)

func (ThinkingContent) Type added in v0.13.3

func (t ThinkingContent) Type() ContentType

type ThinkingParam added in v0.9.0

type ThinkingParam struct {
	Type         string `json:"type"`
	BudgetTokens int    `json:"budget_tokens"`
}

ThinkingParam configures extended thinking for Claude models.

type Tool

type Tool struct {
	Name        string      `json:"name"`
	Description string      `json:"description,omitempty"`
	InputSchema interface{} `json:"input_schema"` // JSON schema for the tool input
}

Tool represents a tool that the model can use.

type ToolResultContent

type ToolResultContent struct {
	BaseContent
	ToolUseID string `json:"tool_use_id"`
	Content   string `json:"content"`
}

func (ToolResultContent) MarshalZerologObject

func (trc ToolResultContent) MarshalZerologObject(e *zerolog.Event)

func (ToolResultContent) Type

func (t ToolResultContent) Type() ContentType

type ToolUseContent

type ToolUseContent struct {
	BaseContent
	ID    string          `json:"id"`
	Name  string          `json:"name"`
	Input json.RawMessage `json:"input"`
}

func (ToolUseContent) MarshalZerologObject

func (tuc ToolUseContent) MarshalZerologObject(e *zerolog.Event)

func (ToolUseContent) Type

func (t ToolUseContent) Type() ContentType

type Usage

type Usage struct {
	InputTokens              int `json:"input_tokens"`
	OutputTokens             int `json:"output_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens"`
}

Usage represents the billing and rate-limit usage information.

func (Usage) MarshalZerologObject

func (u Usage) MarshalZerologObject(e *zerolog.Event)

Jump to

Keyboard shortcuts

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