Documentation
¶
Index ¶
- Constants
- func EstimateTokens(text string) int
- func ExtractAllTextContent(response *mcp.ToolResponse) string
- func ExtractToolResult(response *mcp.ToolResponse) (string, error)
- func GenerateToolCallID(index int) string
- func GetToolNames(toolCalls []ToolCall) []string
- func HasToolCalls(msg Message) bool
- func MustParseToolArguments(arguments map[string]any, target interface{})
- func ParseToolArguments(arguments map[string]any, target interface{}) error
- func WithToolHandler(ctx context.Context, h ToolHandler) context.Context
- type APIError
- func (e *APIError) Error() string
- func (e *APIError) IsAuthentication() bool
- func (e *APIError) IsInvalidRequest() bool
- func (e *APIError) IsNotFound() bool
- func (e *APIError) IsPermission() bool
- func (e *APIError) IsRateLimit() bool
- func (e *APIError) IsRetryable() bool
- func (e *APIError) IsServerError() bool
- func (e *APIError) IsTokenLimit() bool
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatStream
- type Choice
- type Client
- func (c *Client) AddRemoteServer(config RemoteServerConfig)
- func (c *Client) CancelResponse(ctx context.Context, id string) (*ResponseObject, error)
- func (c *Client) ChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)
- func (c *Client) CreateEmbedding(ctx context.Context, req EmbeddingRequest) (*EmbeddingResponse, error)
- func (c *Client) CreateResponse(ctx context.Context, req CreateResponseRequest) (*ResponseObject, error)
- func (c *Client) GetAllTools(ctx context.Context) ([]mcp.MCPTool, error)
- func (c *Client) GetCustomTools() []Tool
- func (c *Client) GetLocalServer() MCPServer
- func (c *Client) GetModels(ctx context.Context) (*ModelsResponse, error)
- func (c *Client) GetResponse(ctx context.Context, id string) (*ResponseObject, error)
- func (c *Client) RemoveRemoteServer(namespace string)
- func (c *Client) SetCustomTools(tools []Tool)
- func (c *Client) StreamChatCompletion(ctx context.Context, req ChatCompletionRequest) *ChatStream
- type CompletionAccumulator
- func (acc *CompletionAccumulator) AddChunk(chunk ChatCompletionResponse)
- func (acc *CompletionAccumulator) Content() string
- func (acc *CompletionAccumulator) FinishReason() string
- func (acc *CompletionAccumulator) FinishedContent() (string, bool)
- func (acc *CompletionAccumulator) FinishedRefusal() (string, bool)
- func (acc *CompletionAccumulator) FinishedToolCall() (*ToolCall, bool)
- func (acc *CompletionAccumulator) FinishedToolCalls() ([]ToolCall, bool)
- func (acc *CompletionAccumulator) IsComplete() bool
- func (acc *CompletionAccumulator) Reset()
- type CompletionTokensDetails
- type Config
- type ContentPart
- type Conversation
- type ConversationDeleteResponse
- type ConversationItem
- type ConversationItemListResponse
- type CreateConversationRequest
- type CreateItemsRequest
- type CreateResponseRequest
- type Delta
- type DeltaFunction
- type DeltaToolCall
- type Embedding
- type EmbeddingRequest
- type EmbeddingResponse
- type ErrorResponse
- type ImageURL
- type ItemIncludeOptions
- type MCPServer
- type MCPServerFuncs
- type MaxToolIterationsError
- type Message
- func BuildAssistantMessage(content string) Message
- func BuildAssistantToolCallMessage(content string, toolCalls []ToolCall) Message
- func BuildMultimodalMessage(parts ...ContentPart) Message
- func BuildSystemMessage(content string) Message
- func BuildToolResultMessage(toolCallID string, result string) Message
- func BuildUserMessage(content string) Message
- func ExecuteToolCall(tc ToolCall, executor ToolExecutor) (Message, error)
- func ExecuteToolCalls(toolCalls []ToolCall, executor ToolExecutor, stopOnError bool) ([]Message, error)
- type Model
- type ModelsResponse
- type NoOpToolHandler
- type PromptTokensDetails
- type RemoteServerConfig
- type ResponseInputItemsResponse
- type ResponseInputTokensResponse
- type ResponseListResponse
- type ResponseObject
- type SSEEventWriter
- type SSEToolHandler
- type SimpleSSEWriter
- type StreamError
- type StreamingToolCallAccumulator
- func (acc *StreamingToolCallAccumulator) Count() int
- func (acc *StreamingToolCallAccumulator) Finalize() []ToolCall
- func (acc *StreamingToolCallAccumulator) GetToolCall(index int) *ToolCall
- func (acc *StreamingToolCallAccumulator) HasToolCalls() bool
- func (acc *StreamingToolCallAccumulator) ProcessDelta(delta Delta) []string
- func (acc *StreamingToolCallAccumulator) ProcessDeltaWithIDCallback(delta Delta, onNewID func(index int, id string)) []string
- func (acc *StreamingToolCallAccumulator) Reset()
- type TokenCounter
- func (tc *TokenCounter) AddCompletionTokensFromDelta(delta *Delta)
- func (tc *TokenCounter) AddCompletionTokensFromMessage(msg *Message)
- func (tc *TokenCounter) AddCompletionTokensFromText(text string)
- func (tc *TokenCounter) AddPromptTokensFromMessages(messages []Message)
- func (tc *TokenCounter) AddPromptTokensFromText(text string)
- func (tc *TokenCounter) GetUsage() Usage
- func (tc *TokenCounter) InjectUsageIfMissing(resp *ChatCompletionResponse)
- func (tc *TokenCounter) Reset()
- type TokenDetail
- type Tool
- type ToolCall
- type ToolCallFunction
- type ToolExecutionError
- type ToolExecutor
- type ToolFilter
- type ToolFunction
- type ToolHandler
- type ToolStatusEvent
- type UpdateConversationRequest
- type Usage
Constants ¶
const ( IncludeWebSearchSources = "web_search_call.action.sources" IncludeCodeInterpreterOutput = "code_interpreter_call.outputs" IncludeComputerCallImage = "computer_call_output.output.image_url" IncludeFileSearchResults = "file_search_call.results" IncludeInputImageURL = "message.input_image.image_url" IncludeOutputLogProbs = "message.output_text.logprobs" IncludeReasoningEncrypted = "reasoning.encrypted_content" )
Common include options
const ( // EventToolStart is sent when a tool execution begins EventToolStart = "tool_start" // EventToolEnd is sent when a tool execution completes EventToolEnd = "tool_end" )
SSE event types for tool status notifications Standard OpenAI clients ignore these; custom clients can use them for UI feedback
const MAX_TOOL_CALL_ITERATIONS = 20
Variables ¶
This section is empty.
Functions ¶
func EstimateTokens ¶ added in v0.5.0
EstimateTokens returns a rough token count for a given input string. This uses a simple heuristic based on word boundaries and punctuation.
func ExtractAllTextContent ¶
func ExtractAllTextContent(response *mcp.ToolResponse) string
ExtractAllTextContent extracts all text content from an MCP ToolResponse, concatenating multiple text parts with newlines.
func ExtractToolResult ¶
func ExtractToolResult(response *mcp.ToolResponse) (string, error)
ExtractToolResult extracts a string result from an MCP ToolResponse for use in OpenAI tool result messages.
Priority order:
- StructuredContent - serialized to JSON
- First text content in Content array
- Default success message
func GenerateToolCallID ¶
GenerateToolCallID creates a unique ID for tool calls. This is useful when LLMs don't provide an ID in streaming responses. The format matches OpenAI's tool call ID format: "call_" followed by random characters.
func GetToolNames ¶ added in v0.4.0
GetToolNames returns the names of all tools in the provided tool calls.
func HasToolCalls ¶ added in v0.4.0
HasToolCalls returns true if the message contains tool calls.
func MustParseToolArguments ¶ added in v0.4.0
MustParseToolArguments is like ParseToolArguments but panics on error. Use only in situations where you're certain the arguments are valid.
func ParseToolArguments ¶ added in v0.4.0
ParseToolArguments parses the tool call arguments map into the provided struct. The target should be a pointer to the struct.
func WithToolHandler ¶
func WithToolHandler(ctx context.Context, h ToolHandler) context.Context
WithToolHandler attaches a ToolHandler to the context. The handler will receive events during tool processing.
Types ¶
type APIError ¶ added in v0.4.0
type APIError struct {
StatusCode int `json:"-"`
Type string `json:"type"`
Message string `json:"message"`
Param string `json:"param,omitempty"`
Code string `json:"code,omitempty"`
}
APIError represents an error returned by the OpenAI API.
func NewAuthenticationError ¶ added in v0.4.0
NewAuthenticationError creates an authentication error.
func NewInvalidRequestError ¶ added in v0.4.0
NewInvalidRequestError creates an invalid request error.
func NewRateLimitError ¶ added in v0.4.0
NewRateLimitError creates a rate limit error.
func NewServerError ¶ added in v0.4.0
NewServerError creates a server error.
func NewTokenLimitError ¶ added in v0.4.0
NewTokenLimitError creates a token limit error.
func (*APIError) IsAuthentication ¶ added in v0.4.0
IsAuthentication returns true if this is an authentication error (401).
func (*APIError) IsInvalidRequest ¶ added in v0.4.0
IsInvalidRequest returns true if this is an invalid request error (400).
func (*APIError) IsNotFound ¶ added in v0.4.0
IsNotFound returns true if this is a not found error (404).
func (*APIError) IsPermission ¶ added in v0.4.0
IsPermission returns true if this is a permission error (403).
func (*APIError) IsRateLimit ¶ added in v0.4.0
IsRateLimit returns true if this is a rate limit error (429).
func (*APIError) IsRetryable ¶ added in v0.4.0
IsRetryable returns true if this error is likely to succeed on retry.
func (*APIError) IsServerError ¶ added in v0.4.0
IsServerError returns true if this is a server error (5xx).
func (*APIError) IsTokenLimit ¶ added in v0.4.0
IsTokenLimit returns true if this is a token limit error.
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Tools []Tool `json:"tools,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
ReasoningEffort string `json:"reasoning_effort,omitempty"`
Stream bool `json:"stream"`
}
ChatCompletionRequest represents an OpenAI chat completion request
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint,omitempty"`
Choices []Choice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
ChatCompletionResponse represents an OpenAI chat completion response
type ChatStream ¶
type ChatStream struct {
// contains filtered or unexported fields
}
ChatStream provides an iterator interface for streaming chat completion responses. It is designed to be used in a for loop pattern:
stream := openai.NewChatStream(ctx, responseChan, errorChan)
for stream.Next() {
chunk := stream.Current()
// process chunk
}
if err := stream.Err(); err != nil {
// handle error
}
func NewChatStream ¶
func NewChatStream(ctx context.Context, responseChan <-chan ChatCompletionResponse, errorChan <-chan error) *ChatStream
NewChatStream creates a new ChatStream from response and error channels.
func (*ChatStream) Current ¶
func (s *ChatStream) Current() ChatCompletionResponse
Current returns the current response chunk. Must be called after Next returns true.
func (*ChatStream) Done ¶
func (s *ChatStream) Done() bool
Done returns true if the stream has completed.
func (*ChatStream) Err ¶
func (s *ChatStream) Err() error
Err returns any error that occurred during streaming. Should be checked after Next returns false.
func (*ChatStream) Next ¶
func (s *ChatStream) Next() bool
Next advances to the next response chunk. Returns true if a chunk is available, false if the stream is done or an error occurred.
type Choice ¶
type Choice struct {
Index int `json:"index"`
Message Message `json:"message,omitempty"`
Delta Delta `json:"delta,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
}
Choice represents a completion choice
type Client ¶ added in v0.8.0
type Client struct {
// contains filtered or unexported fields
}
Client represents an OpenAI API client using the shared HTTP pool
func (*Client) AddRemoteServer ¶ added in v0.8.0
func (c *Client) AddRemoteServer(config RemoteServerConfig)
AddRemoteServer adds a remote MCP server. The namespace is derived from the client's namespace.
func (*Client) CancelResponse ¶ added in v0.8.0
CancelResponse cancels a response by ID using the OpenAI Responses API https://platform.openai.com/docs/api-reference/responses/cancel
func (*Client) ChatCompletion ¶ added in v0.8.0
func (c *Client) ChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)
ChatCompletion performs a non-streaming chat completion with automatic tool processing
func (*Client) CreateEmbedding ¶ added in v0.8.0
func (c *Client) CreateEmbedding(ctx context.Context, req EmbeddingRequest) (*EmbeddingResponse, error)
CreateEmbedding creates an embedding using the OpenAI Embeddings API https://platform.openai.com/docs/api-reference/embeddings/create
func (*Client) CreateResponse ¶ added in v0.8.0
func (c *Client) CreateResponse(ctx context.Context, req CreateResponseRequest) (*ResponseObject, error)
CreateResponse creates a new response using the OpenAI Responses API https://platform.openai.com/docs/api-reference/responses/create
func (*Client) GetAllTools ¶ added in v0.8.0
GetAllTools returns all tools from local and remote servers Local server tools are returned as-is Remote server tools are already namespaced by their client
func (*Client) GetCustomTools ¶ added in v0.8.0
GetCustomTools returns the custom tools.
func (*Client) GetLocalServer ¶ added in v0.8.0
GetLocalServer returns the local MCP server
func (*Client) GetModels ¶ added in v0.8.0
func (c *Client) GetModels(ctx context.Context) (*ModelsResponse, error)
GetModels retrieves the list of available models from OpenAI
func (*Client) GetResponse ¶ added in v0.8.0
GetResponse retrieves a response by ID using the OpenAI Responses API https://platform.openai.com/docs/api-reference/responses/get
func (*Client) RemoveRemoteServer ¶ added in v0.8.0
RemoveRemoteServer removes a remote MCP server by namespace
func (*Client) SetCustomTools ¶ added in v0.8.0
SetCustomTools sets custom tools that will be sent to the AI but not executed by the client. These tools are returned to the caller for manual execution.
func (*Client) StreamChatCompletion ¶ added in v0.8.0
func (c *Client) StreamChatCompletion(ctx context.Context, req ChatCompletionRequest) *ChatStream
StreamChatCompletion performs a streaming chat completion with automatic tool processing Returns a channel of pure OpenAI ChatCompletionResponse chunks
type CompletionAccumulator ¶
type CompletionAccumulator struct {
Choices []accumulatorChoice
}
CompletionAccumulator accumulates streaming chat completion chunks into complete responses. It handles the incremental building of content, tool calls, and refusals.
func (*CompletionAccumulator) AddChunk ¶
func (acc *CompletionAccumulator) AddChunk(chunk ChatCompletionResponse)
AddChunk processes a streaming chunk and accumulates its content.
func (*CompletionAccumulator) Content ¶
func (acc *CompletionAccumulator) Content() string
Content returns the current accumulated content for the first choice.
func (*CompletionAccumulator) FinishReason ¶
func (acc *CompletionAccumulator) FinishReason() string
FinishReason returns the finish reason for the first choice.
func (*CompletionAccumulator) FinishedContent ¶
func (acc *CompletionAccumulator) FinishedContent() (string, bool)
FinishedContent returns the accumulated content for the first choice if complete. Returns the content and true if finish_reason is "stop", otherwise empty string and false.
func (*CompletionAccumulator) FinishedRefusal ¶
func (acc *CompletionAccumulator) FinishedRefusal() (string, bool)
FinishedRefusal returns the accumulated refusal for the first choice if present. Returns the refusal and true if there is refusal content, otherwise empty string and false.
func (*CompletionAccumulator) FinishedToolCall ¶
func (acc *CompletionAccumulator) FinishedToolCall() (*ToolCall, bool)
FinishedToolCall returns the first accumulated tool call for the first choice if complete. Returns the tool call and true if finish_reason is "tool_calls", otherwise nil and false.
func (*CompletionAccumulator) FinishedToolCalls ¶
func (acc *CompletionAccumulator) FinishedToolCalls() ([]ToolCall, bool)
FinishedToolCalls returns all accumulated tool calls for the first choice if complete. Returns the tool calls and true if finish_reason is "tool_calls", otherwise nil and false.
func (*CompletionAccumulator) IsComplete ¶
func (acc *CompletionAccumulator) IsComplete() bool
IsComplete returns true if the first choice has a finish reason.
func (*CompletionAccumulator) Reset ¶
func (acc *CompletionAccumulator) Reset()
Reset clears the accumulator for reuse.
type CompletionTokensDetails ¶
type CompletionTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens"`
AudioTokens int `json:"audio_tokens"`
AcceptedPredictionTokens int `json:"accepted_prediction_tokens"`
RejectedPredictionTokens int `json:"rejected_prediction_tokens"`
}
CompletionTokensDetails represents detailed completion token usage
type Config ¶ added in v0.8.0
type Config struct {
APIKey string
BaseURL string
LocalServer MCPServer // Local MCP server (no namespace)
RemoteServerConfigs []RemoteServerConfig // Remote MCP server configs
}
Config holds configuration for the OpenAI client
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
}
ContentPart represents a multi-modal content part
func ImageBase64ContentPart ¶ added in v0.4.0
func ImageBase64ContentPart(base64Data string, mediaType string, detail string) ContentPart
ImageBase64ContentPart creates a ContentPart with a base64-encoded image. The mediaType should be something like "image/png" or "image/jpeg".
func ImageURLContentPart ¶ added in v0.4.0
func ImageURLContentPart(url string, detail string) ContentPart
ImageURLContentPart creates a ContentPart with an image URL.
func TextContentPart ¶ added in v0.4.0
func TextContentPart(text string) ContentPart
TextContentPart creates a ContentPart with text content.
type Conversation ¶ added in v0.6.11
type Conversation struct {
ID string `json:"id"`
Object string `json:"object"` // "conversation"
CreatedAt int64 `json:"created_at"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
Conversation represents a conversation object https://platform.openai.com/docs/api-reference/conversations/object
type ConversationDeleteResponse ¶ added in v0.6.11
type ConversationDeleteResponse struct {
ID string `json:"id"`
Object string `json:"object"` // "conversation.deleted"
Deleted bool `json:"deleted"`
}
ConversationDeleteResponse represents the response when deleting a conversation
type ConversationItem ¶ added in v0.6.11
type ConversationItem struct {
Type string `json:"type"` // "message", "tool_call", "reasoning", etc.
ID string `json:"id"`
Status string `json:"status,omitempty"` // "completed", "incomplete", "in_progress"
Role string `json:"role,omitempty"` // "user", "assistant", "system"
Content []ContentPart `json:"content,omitempty"`
// Additional fields based on type
ToolCall *ToolCall `json:"tool_call,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
Output interface{} `json:"output,omitempty"`
Reasoning map[string]interface{} `json:"reasoning,omitempty"`
}
ConversationItem represents an item in a conversation Items can be messages, tool calls, reasoning, etc.
type ConversationItemListResponse ¶ added in v0.6.11
type ConversationItemListResponse struct {
Object string `json:"object"` // "list"
Data []ConversationItem `json:"data"`
FirstID string `json:"first_id,omitempty"`
LastID string `json:"last_id,omitempty"`
HasMore bool `json:"has_more"`
}
ConversationItemListResponse represents a list of conversation items
type CreateConversationRequest ¶ added in v0.6.11
type CreateConversationRequest struct {
Items []ConversationItem `json:"items,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
CreateConversationRequest represents a request to create a conversation
type CreateItemsRequest ¶ added in v0.6.11
type CreateItemsRequest struct {
Items []ConversationItem `json:"items"`
}
CreateItemsRequest represents a request to add items to a conversation
type CreateResponseRequest ¶ added in v0.6.9
type CreateResponseRequest struct {
Model string `json:"model"`
Input []any `json:"input,omitempty"`
Modalities []string `json:"modalities,omitempty"`
Instructions string `json:"instructions,omitempty"`
Tools []Tool `json:"tools,omitempty"`
PreviousResponseID string `json:"previous_response_id,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Background bool `json:"background,omitempty"`
MaxOutputTokens *int `json:"max_output_tokens,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
Store *bool `json:"store,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Truncation string `json:"truncation,omitempty"`
}
CreateResponseRequest represents a request to create a response
type Delta ¶
type Delta struct {
ReasoningContent string `json:"reasoning_content,omitempty"`
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
Refusal string `json:"refusal,omitempty"`
ToolCalls []DeltaToolCall `json:"tool_calls,omitempty"`
}
Delta represents a streaming delta
type DeltaFunction ¶
type DeltaFunction struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
DeltaFunction represents a streaming function delta
type DeltaToolCall ¶
type DeltaToolCall struct {
Index int `json:"index"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Function DeltaFunction `json:"function,omitempty"`
}
DeltaToolCall represents a streaming tool call delta
type Embedding ¶ added in v0.6.7
type Embedding struct {
Object string `json:"object"`
Embedding []float64 `json:"embedding"`
Index int `json:"index"`
}
Embedding represents a single embedding
type EmbeddingRequest ¶ added in v0.6.7
type EmbeddingRequest struct {
Model string `json:"model"`
Input interface{} `json:"input"`
EncodingFormat string `json:"encoding_format,omitempty"`
Dimensions int `json:"dimensions,omitempty"`
User string `json:"user,omitempty"`
}
EmbeddingRequest represents an OpenAI embedding request
type EmbeddingResponse ¶ added in v0.6.7
type EmbeddingResponse struct {
Object string `json:"object"`
Data []Embedding `json:"data"`
Model string `json:"model"`
Usage Usage `json:"usage"`
}
EmbeddingResponse represents an OpenAI embedding response
type ErrorResponse ¶ added in v0.4.0
type ErrorResponse struct {
Error *APIError `json:"error"`
}
ErrorResponse represents the error response structure from OpenAI API.
type ItemIncludeOptions ¶ added in v0.6.11
type ItemIncludeOptions []string
ItemIncludeOptions represents the include parameter for listing items
type MCPServer ¶ added in v0.8.0
type MCPServer interface {
ListTools() []mcp.MCPTool
CallTool(ctx context.Context, name string, args map[string]any) (*mcp.ToolResponse, error)
}
MCPServer interface for MCP server operations (local server)
type MCPServerFuncs ¶ added in v0.8.0
type MCPServerFuncs struct {
ListToolsFunc func() []mcp.MCPTool
CallToolFunc func(ctx context.Context, name string, args map[string]any) (*mcp.ToolResponse, error)
}
MCPServerFuncs allows creating a simple MCPServer from functions
func (*MCPServerFuncs) CallTool ¶ added in v0.8.0
func (m *MCPServerFuncs) CallTool(ctx context.Context, name string, args map[string]any) (*mcp.ToolResponse, error)
func (*MCPServerFuncs) ListTools ¶ added in v0.8.0
func (m *MCPServerFuncs) ListTools() []mcp.MCPTool
type MaxToolIterationsError ¶ added in v0.4.0
type MaxToolIterationsError struct {
Iterations int
}
MaxToolIterationsError is returned when the maximum number of tool call iterations is reached without completing the conversation.
func NewMaxToolIterationsError ¶ added in v0.4.0
func NewMaxToolIterationsError(iterations int) *MaxToolIterationsError
NewMaxToolIterationsError creates a new MaxToolIterationsError.
func (*MaxToolIterationsError) Error ¶ added in v0.4.0
func (e *MaxToolIterationsError) Error() string
type Message ¶
type Message struct {
Role string `json:"role,omitempty"`
Content any `json:"content,omitempty"`
Refusal string `json:"refusal,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
Message represents a chat message
func BuildAssistantMessage ¶ added in v0.4.0
BuildAssistantMessage creates an assistant message with the given content.
func BuildAssistantToolCallMessage ¶ added in v0.4.0
BuildAssistantToolCallMessage creates an assistant message with tool calls. This is useful when reconstructing a conversation that included tool calls.
func BuildMultimodalMessage ¶ added in v0.4.0
func BuildMultimodalMessage(parts ...ContentPart) Message
BuildMultimodalMessage creates a user message with multiple content parts.
func BuildSystemMessage ¶ added in v0.4.0
BuildSystemMessage creates a system message with the given content.
func BuildToolResultMessage ¶ added in v0.4.0
BuildToolResultMessage creates a tool result message for the given tool call ID.
func BuildUserMessage ¶ added in v0.4.0
BuildUserMessage creates a user message with the given content.
func ExecuteToolCall ¶ added in v0.4.0
func ExecuteToolCall(tc ToolCall, executor ToolExecutor) (Message, error)
ExecuteToolCall executes a single tool call using the provided executor. Returns a Message with the tool result that can be appended to the conversation.
func ExecuteToolCalls ¶ added in v0.4.0
func ExecuteToolCalls(toolCalls []ToolCall, executor ToolExecutor, stopOnError bool) ([]Message, error)
ExecuteToolCalls executes multiple tool calls using the provided executor and returns messages containing the results. If a tool call fails, the error message is included in the result and the execution continues (unless stopOnError is true).
func (*Message) GetContentAsString ¶
GetContentAsString returns the content as a string, handling both string and array formats
func (*Message) SetContentAsString ¶
SetContentAsString sets the content as a string
type Model ¶
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
Model represents an individual model
type ModelsResponse ¶
ModelsResponse represents the response from the /models endpoint
type NoOpToolHandler ¶
type NoOpToolHandler struct{}
NoOpToolHandler is a ToolHandler that does nothing. Useful as a default or for testing.
func (NoOpToolHandler) OnToolCall ¶
func (NoOpToolHandler) OnToolCall(toolCall ToolCall) error
func (NoOpToolHandler) OnToolResult ¶
func (NoOpToolHandler) OnToolResult(toolCallID, toolName, result string) error
type PromptTokensDetails ¶
type PromptTokensDetails struct {
CachedTokens int `json:"cached_tokens"`
AudioTokens int `json:"audio_tokens"`
}
PromptTokensDetails represents detailed prompt token usage
type RemoteServerConfig ¶ added in v0.8.0
type RemoteServerConfig struct {
BaseURL string
Auth mcp.AuthProvider
Namespace string
}
RemoteServerConfig holds configuration for a remote MCP server
type ResponseInputItemsResponse ¶ added in v0.6.9
type ResponseInputItemsResponse struct {
Object string `json:"object"` // "list"
Data []any `json:"data"`
}
ResponseInputItemsResponse represents a list of input items for a response
type ResponseInputTokensResponse ¶ added in v0.6.9
type ResponseInputTokensResponse struct {
Object string `json:"object"` // "list"
Data []TokenDetail `json:"data"`
}
ResponseInputTokensResponse represents token details for input
type ResponseListResponse ¶ added in v0.6.9
type ResponseListResponse struct {
Object string `json:"object"` // "list"
Data []ResponseObject `json:"data"`
}
ResponseListResponse represents a list of response objects
type ResponseObject ¶ added in v0.6.9
type ResponseObject struct {
ID string `json:"id"`
Object string `json:"object"` // "response"
CreatedAt int64 `json:"created_at"`
Status string `json:"status"` // "completed", "in_progress", "failed", "cancelled", "queued", "incomplete"
Error *APIError `json:"error,omitempty"`
IncompleteDetails map[string]interface{} `json:"incomplete_details,omitempty"`
Instructions string `json:"instructions,omitempty"`
MaxOutputTokens *int `json:"max_output_tokens,omitempty"`
Model string `json:"model"`
Output []interface{} `json:"output,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
PreviousResponseID string `json:"previous_response_id,omitempty"`
Reasoning map[string]interface{} `json:"reasoning,omitempty"`
Store *bool `json:"store,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
Text map[string]interface{} `json:"text,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
Tools []Tool `json:"tools,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Truncation string `json:"truncation,omitempty"`
Usage *Usage `json:"usage,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
ResponseObject represents a complete OpenAI Responses API response object https://platform.openai.com/docs/api-reference/responses/object
type SSEEventWriter ¶ added in v0.5.0
type SSEEventWriter interface {
// WriteEvent writes an SSE comment event (prefixed with ":")
// The event type and data are formatted as ":eventType:jsonData\n\n"
WriteEvent(eventType string, data any) error
}
SSEEventWriter is an interface for writing SSE events. This allows integration with various HTTP frameworks' streaming implementations.
type SSEToolHandler ¶ added in v0.5.0
type SSEToolHandler struct {
// contains filtered or unexported fields
}
SSEToolHandler implements ToolHandler to send tool events via SSE streaming. It wraps an SSEEventWriter and sends tool_start/tool_end events as SSE comments.
Usage pattern:
- Call OnToolCall() BEFORE executing the tool (sends tool_start with "running" status)
- Execute the tool
- Call OnToolResult() AFTER execution completes (sends tool_end with "complete" status and result)
Example SSE output:
:tool_start:{"tool_call_id":"call_abc123","tool_name":"search","status":"running"}
:tool_end:{"tool_call_id":"call_abc123","tool_name":"search","status":"complete","result":"..."}
func NewSSEToolHandler ¶ added in v0.5.0
func NewSSEToolHandler(writer SSEEventWriter, errorLogger func(err error, eventType, toolName string)) *SSEToolHandler
NewSSEToolHandler creates a new SSEToolHandler that sends tool events to the given writer. The optional errorLogger is called when write failures occur (errors are logged but not returned since tool events are just status notifications and shouldn't block tool execution).
func (*SSEToolHandler) OnToolCall ¶ added in v0.5.0
func (h *SSEToolHandler) OnToolCall(toolCall ToolCall) error
OnToolCall sends a tool_start event when a tool execution begins. This should be called BEFORE executing the tool. Write failures are logged but not returned as errors since they're just status notifications.
func (*SSEToolHandler) OnToolResult ¶ added in v0.5.0
func (h *SSEToolHandler) OnToolResult(toolCallID, toolName, result string) error
OnToolResult sends a tool_end event when a tool execution completes. This should be called AFTER the tool has finished executing. The result parameter contains the tool's output, which is included in the event so clients can display tool results in the UI. Write failures are logged but not returned as errors since they're just status notifications.
type SimpleSSEWriter ¶ added in v0.5.0
type SimpleSSEWriter struct {
// contains filtered or unexported fields
}
SimpleSSEWriter is a basic implementation of SSEEventWriter that writes to an io.Writer. For production use, you may want to implement your own SSEEventWriter with proper flushing, error handling, and client disconnect detection.
func NewSimpleSSEWriter ¶ added in v0.5.0
func NewSimpleSSEWriter(w io.Writer, flusher func()) *SimpleSSEWriter
NewSimpleSSEWriter creates a SimpleSSEWriter that writes to the given io.Writer. If the writer implements http.Flusher, pass a flush function to flush after each write.
func (*SimpleSSEWriter) WriteEvent ¶ added in v0.5.0
func (s *SimpleSSEWriter) WriteEvent(eventType string, data any) error
WriteEvent writes an SSE comment event in the format ":eventType:jsonData\n\n"
type StreamError ¶ added in v0.4.0
type StreamError struct {
Err error
}
StreamError is returned when an error occurs during streaming.
func NewStreamError ¶ added in v0.4.0
func NewStreamError(err error) *StreamError
NewStreamError creates a new StreamError.
func (*StreamError) Error ¶ added in v0.4.0
func (e *StreamError) Error() string
func (*StreamError) Unwrap ¶ added in v0.4.0
func (e *StreamError) Unwrap() error
type StreamingToolCallAccumulator ¶ added in v0.4.0
type StreamingToolCallAccumulator struct {
// contains filtered or unexported fields
}
StreamingToolCallAccumulator handles the complex task of accumulating streaming tool call deltas into complete ToolCall objects. It buffers arguments that come in chunks, generates IDs when missing, and parses the final JSON arguments.
Usage:
acc := NewStreamingToolCallAccumulator()
for each streaming chunk {
acc.ProcessDelta(chunk.Choices[0].Delta)
}
toolCalls := acc.Finalize()
func NewStreamingToolCallAccumulator ¶ added in v0.4.0
func NewStreamingToolCallAccumulator() *StreamingToolCallAccumulator
NewStreamingToolCallAccumulator creates a new accumulator for streaming tool calls.
func (*StreamingToolCallAccumulator) Count ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) Count() int
Count returns the number of tool calls being accumulated.
func (*StreamingToolCallAccumulator) Finalize ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) Finalize() []ToolCall
Finalize parses accumulated arguments and returns complete ToolCall objects. Tool calls with empty names are skipped. Returns tool calls sorted by index.
func (*StreamingToolCallAccumulator) GetToolCall ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) GetToolCall(index int) *ToolCall
GetToolCall returns a specific tool call by index without finalizing. Returns nil if the index doesn't exist.
func (*StreamingToolCallAccumulator) HasToolCalls ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) HasToolCalls() bool
HasToolCalls returns true if any tool calls are being accumulated.
func (*StreamingToolCallAccumulator) ProcessDelta ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) ProcessDelta(delta Delta) []string
ProcessDelta processes a streaming delta and accumulates tool call data. Returns the list of tool call IDs that were updated (useful for tracking progress).
func (*StreamingToolCallAccumulator) ProcessDeltaWithIDCallback ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) ProcessDeltaWithIDCallback(delta Delta, onNewID func(index int, id string)) []string
ProcessDeltaWithIDCallback processes a streaming delta and calls the callback with any newly generated tool call IDs. This is useful when you need to update the original delta with generated IDs for forwarding to clients.
func (*StreamingToolCallAccumulator) Reset ¶ added in v0.4.0
func (acc *StreamingToolCallAccumulator) Reset()
Reset clears the accumulator for reuse.
type TokenCounter ¶ added in v0.5.0
type TokenCounter struct {
// contains filtered or unexported fields
}
TokenCounter estimates token usage for OpenAI API requests and responses. It provides a fast, reproducible approximation of token counts when exact counts are not available from the API.
func NewTokenCounter ¶ added in v0.5.0
func NewTokenCounter() *TokenCounter
NewTokenCounter creates a new TokenCounter
func (*TokenCounter) AddCompletionTokensFromDelta ¶ added in v0.5.0
func (tc *TokenCounter) AddCompletionTokensFromDelta(delta *Delta)
AddCompletionTokensFromDelta adds estimated completion tokens from a streaming delta
func (*TokenCounter) AddCompletionTokensFromMessage ¶ added in v0.5.0
func (tc *TokenCounter) AddCompletionTokensFromMessage(msg *Message)
AddCompletionTokensFromMessage adds estimated completion tokens from a chat message
func (*TokenCounter) AddCompletionTokensFromText ¶ added in v0.5.0
func (tc *TokenCounter) AddCompletionTokensFromText(text string)
AddCompletionTokensFromText adds estimated completion tokens from text
func (*TokenCounter) AddPromptTokensFromMessages ¶ added in v0.5.0
func (tc *TokenCounter) AddPromptTokensFromMessages(messages []Message)
AddPromptTokensFromMessages estimates and adds prompt tokens from chat messages
func (*TokenCounter) AddPromptTokensFromText ¶ added in v0.5.0
func (tc *TokenCounter) AddPromptTokensFromText(text string)
AddPromptTokensFromText adds estimated prompt tokens from text
func (*TokenCounter) GetUsage ¶ added in v0.5.0
func (tc *TokenCounter) GetUsage() Usage
GetUsage returns the current usage statistics
func (*TokenCounter) InjectUsageIfMissing ¶ added in v0.5.0
func (tc *TokenCounter) InjectUsageIfMissing(resp *ChatCompletionResponse)
InjectUsageIfMissing injects estimated usage into a chat completion response if it's missing or zero
func (*TokenCounter) Reset ¶ added in v0.5.0
func (tc *TokenCounter) Reset()
Reset resets the token counters to zero
type TokenDetail ¶ added in v0.6.9
type TokenDetail struct {
Text string `json:"text"`
Token int `json:"token"`
Logprob float64 `json:"logprob"`
TopLogprobs []struct {
Token string `json:"token"`
Logprob float64 `json:"logprob"`
} `json:"top_logprobs,omitempty"`
}
TokenDetail represents detailed information about a token
type Tool ¶
type Tool struct {
Type string `json:"type"`
Function ToolFunction `json:"function"`
}
Tool represents an OpenAI tool definition
func MCPToolsToOpenAI ¶
MCPToolsToOpenAI converts MCP tools to OpenAI function calling format
func MCPToolsToOpenAIFiltered ¶
MCPToolsToOpenAIFiltered converts MCP tools to OpenAI format with optional filtering. If filter is nil, all tools are included. Otherwise, only tools where filter(name) returns true are included.
type ToolCall ¶
type ToolCall struct {
Index int `json:"index,omitempty"`
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
ToolCall represents a tool call from the assistant
type ToolCallFunction ¶
type ToolCallFunction struct {
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
}
ToolCallFunction represents the function details of a tool call
func (ToolCallFunction) MarshalJSON ¶
func (tcf ToolCallFunction) MarshalJSON() ([]byte, error)
MarshalJSON implements custom JSON marshaling for ToolCallFunction. OpenAI expects arguments as a JSON string, not an object.
func (*ToolCallFunction) UnmarshalJSON ¶
func (tcf *ToolCallFunction) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom JSON unmarshaling for ToolCallFunction. OpenAI sends arguments as a JSON string, not an object.
type ToolExecutionError ¶ added in v0.4.0
ToolExecutionError is returned when a tool call fails.
func NewToolExecutionError ¶ added in v0.4.0
func NewToolExecutionError(toolName, toolID string, err error) *ToolExecutionError
NewToolExecutionError creates a new ToolExecutionError.
func (*ToolExecutionError) Error ¶ added in v0.4.0
func (e *ToolExecutionError) Error() string
func (*ToolExecutionError) Unwrap ¶ added in v0.4.0
func (e *ToolExecutionError) Unwrap() error
type ToolExecutor ¶ added in v0.4.0
ToolExecutor is a function that executes tool calls and returns the result. The function receives the tool name and arguments (as a map) and returns the result string and any error.
type ToolFilter ¶
ToolFilter is a function type for filtering tools by name
func ExcludeTools ¶
func ExcludeTools(names ...string) ToolFilter
ExcludeTools returns a filter that excludes tools with the specified names
func ToolsByName ¶
func ToolsByName(names ...string) ToolFilter
ToolsByName returns a filter that includes only tools with the specified names
type ToolFunction ¶
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters,omitempty"`
}
ToolFunction represents a function definition for a tool
type ToolHandler ¶
type ToolHandler interface {
// OnToolCall is called when a tool call is about to be executed.
OnToolCall(toolCall ToolCall) error
// OnToolResult is called when a tool call has completed.
OnToolResult(toolCallID, toolName, result string) error
}
ToolHandler receives events during tool processing. Implement this interface to receive notifications when tools are called and when results are received.
func ToolHandlerFromContext ¶
func ToolHandlerFromContext(ctx context.Context) ToolHandler
ToolHandlerFromContext retrieves a ToolHandler from the context. Returns nil if no handler is attached.
type ToolStatusEvent ¶ added in v0.5.0
type ToolStatusEvent struct {
ToolCallID string `json:"tool_call_id"`
ToolName string `json:"tool_name"`
Status string `json:"status"` // "running" or "complete"
Arguments map[string]any `json:"arguments,omitempty"`
Result string `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
ToolStatusEvent represents a tool execution status for SSE streaming. This is sent as an SSE comment (prefixed with ":") so standard SSE clients ignore it, but custom clients can parse it to show tool execution progress.
type UpdateConversationRequest ¶ added in v0.6.11
type UpdateConversationRequest struct {
Metadata map[string]interface{} `json:"metadata"`
}
UpdateConversationRequest represents a request to update a conversation
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
}
Usage represents token usage