Documentation
¶
Overview ¶
Package joytoken provides a Go client for the JoyToken API.
It supports OpenAI-compatible Chat Completions, Anthropic-compatible Messages, streaming responses, model discovery, and pricing metadata.
Index ¶
- Constants
- Variables
- func IsAPIError(err error) bool
- type APIError
- type CatalogOption
- type ChatCompletionChoice
- type ChatCompletionChunk
- type ChatCompletionChunkChoice
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionStream
- type ChatMessage
- type ChatTool
- type ChatToolFunction
- type Client
- func (c *Client) CreateChatCompletion(ctx context.Context, request ChatCompletionRequest) (*ChatCompletionResponse, error)
- func (c *Client) CreateMessage(ctx context.Context, request MessageRequest) (*MessageResponse, error)
- func (c *Client) CreateResponse(ctx context.Context, request ResponseRequest) (*Response, error)
- func (c *Client) GenerateImage(ctx context.Context, request ImageGenerationRequest) (*ImageGenerationResponse, error)
- func (c *Client) GetModelMeta(ctx context.Context) (*ModelMetadataResponse, error)
- func (c *Client) GetPricing(ctx context.Context) (*PricingResponse, error)
- func (c *Client) ListModels(ctx context.Context) (*ModelListResponse, error)
- func (c *Client) ListModelsWithOptions(ctx context.Context, options ListModelsOptions) (*ModelListResponse, error)
- func (c *Client) StreamChatCompletion(ctx context.Context, request ChatCompletionRequest) (*ChatCompletionStream, error)
- func (c *Client) StreamMessage(ctx context.Context, request MessageRequest) (*MessageStream, error)
- func (c *Client) StreamResponse(ctx context.Context, request ResponseRequest) (*ResponseStream, error)
- type GeneratedImage
- type HTTPClient
- type ImageGenerationRequest
- type ImageGenerationResponse
- type ListModelsOptions
- type MessageContentBlock
- type MessageParam
- type MessageRequest
- type MessageResponse
- type MessageStream
- type MessageStreamEvent
- type MessageTool
- type MessageUsage
- type ModelInfo
- type ModelListData
- type ModelListResponse
- type ModelLocale
- type ModelMetadata
- type ModelMetadataResponse
- type Option
- func WithAPIBaseURL(apiBaseURL string) Option
- func WithAPIKey(apiKey string) Option
- func WithAnthropicBaseURL(anthropicBaseURL string) Option
- func WithAnthropicVersion(anthropicVersion string) Option
- func WithHTTPClient(httpClient HTTPClient) Option
- func WithHeader(key, value string) Option
- func WithOpenAIBaseURL(openAIBaseURL string) Option
- func WithTimeout(timeout time.Duration) Option
- type Pricing
- type PricingResponse
- type PricingSKU
- type PricingTier
- type Response
- type ResponseInputContentPart
- type ResponseInputItem
- type ResponseOutputContent
- type ResponseOutputItem
- type ResponseRequest
- type ResponseStream
- type ResponseStreamEvent
- type ResponseTool
- type ResponseUsage
- type ToolCall
- type ToolFunction
- type Usage
Constants ¶
const ModelAuto = "auto"
ModelAuto is the only model value accepted by JoyToken requests.
Variables ¶
var ErrMissingAPIKey = errors.New("joytoken API key is required; pass WithAPIKey or set JOY_TOKEN_API_KEY")
ErrMissingAPIKey is returned when an authenticated endpoint is called without configuring a JoyToken API key.
Functions ¶
func IsAPIError ¶
IsAPIError reports whether err contains an APIError.
Types ¶
type CatalogOption ¶
CatalogOption is a value-label pair used by model catalog filters.
type ChatCompletionChoice ¶
type ChatCompletionChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason,omitempty"`
Logprobs any `json:"logprobs,omitempty"`
}
ChatCompletionChoice is one generated completion choice.
type ChatCompletionChunk ¶
type ChatCompletionChunk struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Created int64 `json:"created,omitempty"`
Model string `json:"model,omitempty"`
Choices []ChatCompletionChunkChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
ChatCompletionChunk is one streaming completion event.
type ChatCompletionChunkChoice ¶
type ChatCompletionChunkChoice struct {
Index int `json:"index"`
Delta map[string]any `json:"delta"`
FinishReason string `json:"finish_reason,omitempty"`
Logprobs any `json:"logprobs,omitempty"`
}
ChatCompletionChunkChoice is one incremental streaming choice.
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Stream bool `json:"stream,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stop any `json:"stop,omitempty"`
Tools []ChatTool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
Tier string `json:"tier,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
ChatCompletionRequest is an OpenAI-compatible completion request. Model must be ModelAuto.
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Created int64 `json:"created,omitempty"`
Model string `json:"model,omitempty"`
Choices []ChatCompletionChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
ChatCompletionResponse is a non-streaming completion response.
type ChatCompletionStream ¶
type ChatCompletionStream struct {
// contains filtered or unexported fields
}
ChatCompletionStream reads Chat Completions SSE events.
func (*ChatCompletionStream) Close ¶
func (s *ChatCompletionStream) Close() error
Close closes the underlying streaming response body.
func (*ChatCompletionStream) Recv ¶
func (s *ChatCompletionStream) Recv() (*ChatCompletionChunk, error)
Recv returns the next completion chunk or io.EOF when the stream ends.
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"`
Content any `json:"content,omitempty"`
Name string `json:"name,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
ChatMessage is an OpenAI-compatible conversation message.
type ChatTool ¶
type ChatTool struct {
Type string `json:"type"`
Function ChatToolFunction `json:"function"`
}
ChatTool declares a tool available to Chat Completions.
type ChatToolFunction ¶
type ChatToolFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters map[string]any `json:"parameters,omitempty"`
}
ChatToolFunction contains the schema for a callable function.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a reusable, concurrency-safe JoyToken API client.
func (*Client) CreateChatCompletion ¶
func (c *Client) CreateChatCompletion(ctx context.Context, request ChatCompletionRequest) (*ChatCompletionResponse, error)
CreateChatCompletion creates a non-streaming OpenAI-compatible completion.
func (*Client) CreateMessage ¶
func (c *Client) CreateMessage(ctx context.Context, request MessageRequest) (*MessageResponse, error)
CreateMessage creates a non-streaming Anthropic-compatible message.
func (*Client) CreateResponse ¶
CreateResponse creates a non-streaming OpenAI-compatible Responses result.
func (*Client) GenerateImage ¶
func (c *Client) GenerateImage(ctx context.Context, request ImageGenerationRequest) (*ImageGenerationResponse, error)
GenerateImage creates an OpenAI-compatible image generation.
func (*Client) GetModelMeta ¶
func (c *Client) GetModelMeta(ctx context.Context) (*ModelMetadataResponse, error)
GetModelMeta returns filter metadata for the model catalog.
func (*Client) GetPricing ¶
func (c *Client) GetPricing(ctx context.Context) (*PricingResponse, error)
GetPricing returns the current JoyToken pricing catalog.
func (*Client) ListModels ¶
func (c *Client) ListModels(ctx context.Context) (*ModelListResponse, error)
ListModels lists the public JoyToken model catalog.
func (*Client) ListModelsWithOptions ¶
func (c *Client) ListModelsWithOptions(ctx context.Context, options ListModelsOptions) (*ModelListResponse, error)
ListModelsWithOptions lists the public JoyToken model catalog with optional response localization. When Locale is empty, the API defaults to English.
func (*Client) StreamChatCompletion ¶
func (c *Client) StreamChatCompletion(ctx context.Context, request ChatCompletionRequest) (*ChatCompletionStream, error)
StreamChatCompletion starts a streaming OpenAI-compatible completion. The caller must close the returned stream.
func (*Client) StreamMessage ¶
func (c *Client) StreamMessage(ctx context.Context, request MessageRequest) (*MessageStream, error)
StreamMessage starts a streaming Anthropic-compatible message. The caller must close the returned stream.
func (*Client) StreamResponse ¶
func (c *Client) StreamResponse(ctx context.Context, request ResponseRequest) (*ResponseStream, error)
StreamResponse starts an OpenAI-compatible Responses SSE stream. The caller must close the returned stream. The stream ends after response.completed.
type GeneratedImage ¶
type GeneratedImage struct {
URL string `json:"url,omitempty"`
B64JSON string `json:"b64_json,omitempty"`
RevisedPrompt string `json:"revised_prompt,omitempty"`
}
GeneratedImage contains one URL or base64-encoded generated image.
type HTTPClient ¶
HTTPClient is the subset of http.Client used by Client.
type ImageGenerationRequest ¶
type ImageGenerationRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
N *int `json:"n,omitempty"`
Quality string `json:"quality,omitempty"`
ResponseFormat string `json:"response_format,omitempty"`
Size string `json:"size,omitempty"`
Style string `json:"style,omitempty"`
User string `json:"user,omitempty"`
Background string `json:"background,omitempty"`
Moderation string `json:"moderation,omitempty"`
OutputCompression *int `json:"output_compression,omitempty"`
OutputFormat string `json:"output_format,omitempty"`
}
ImageGenerationRequest is an OpenAI-compatible image generation request. Model must be ModelAuto and Prompt is required by the JoyToken gateway; other fields are forwarded to the selected image provider.
type ImageGenerationResponse ¶
type ImageGenerationResponse struct {
Created int64 `json:"created,omitempty"`
Data []GeneratedImage `json:"data"`
Metadata map[string]any `json:"metadata,omitempty"`
}
ImageGenerationResponse is an OpenAI-compatible image generation result. Metadata contains JoyToken routing and billing details when available.
type ListModelsOptions ¶
type ListModelsOptions struct {
// Locale selects zh or en. An empty value leaves the parameter unset, so
// the API returns its default English descriptions.
Locale ModelLocale
}
ListModelsOptions configures a public model catalog request.
type MessageContentBlock ¶
type MessageContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input map[string]any `json:"input,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
Content any `json:"content,omitempty"`
}
MessageContentBlock is an Anthropic-compatible content block.
type MessageParam ¶
MessageParam is an Anthropic-compatible input message.
type MessageRequest ¶
type MessageRequest struct {
Model string `json:"model"`
MaxTokens int `json:"max_tokens"`
Messages []MessageParam `json:"messages"`
System any `json:"system,omitempty"`
Stream bool `json:"stream,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
Tools []MessageTool `json:"tools,omitempty"`
Tier string `json:"tier,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
MessageRequest is an Anthropic-compatible Messages request. Model must be ModelAuto.
type MessageResponse ¶
type MessageResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Content []MessageContentBlock `json:"content"`
Model string `json:"model"`
StopReason *string `json:"stop_reason,omitempty"`
StopSequence *string `json:"stop_sequence,omitempty"`
Usage MessageUsage `json:"usage"`
Metadata map[string]any `json:"metadata,omitempty"`
}
MessageResponse is a non-streaming Anthropic-compatible response.
type MessageStream ¶
type MessageStream struct {
// contains filtered or unexported fields
}
MessageStream reads Anthropic Messages SSE events.
func (*MessageStream) Close ¶
func (s *MessageStream) Close() error
Close closes the underlying streaming response body.
func (*MessageStream) Recv ¶
func (s *MessageStream) Recv() (*MessageStreamEvent, error)
Recv returns the next message event or io.EOF when the stream ends.
type MessageStreamEvent ¶
type MessageStreamEvent struct {
Type string `json:"type"`
Index *int `json:"index,omitempty"`
Message *MessageResponse `json:"message,omitempty"`
ContentBlock *MessageContentBlock `json:"content_block,omitempty"`
Delta map[string]any `json:"delta,omitempty"`
Usage *MessageUsage `json:"usage,omitempty"`
Error map[string]any `json:"error,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
MessageStreamEvent is one Anthropic-compatible streaming event.
type MessageTool ¶
type MessageTool struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema map[string]any `json:"input_schema"`
}
MessageTool declares a tool available to Anthropic Messages.
type MessageUsage ¶
type MessageUsage struct {
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"`
}
MessageUsage reports Anthropic-compatible token usage.
type ModelInfo ¶
type ModelInfo struct {
ModelID string `json:"modelId,omitempty"`
ModelKey string `json:"modelKey,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Alias string `json:"alias,omitempty"`
Tier string `json:"tier,omitempty"`
Tags []string `json:"tags,omitempty"`
Description string `json:"description,omitempty"`
CustomerInputMtok float64 `json:"customerInputMtok,omitempty"`
CustomerOutputMtok float64 `json:"customerOutputMtok,omitempty"`
CustomerCachereadMtok float64 `json:"customerCachereadMtok,omitempty"`
CustomerCachewriteMtok float64 `json:"customerCachewriteMtok,omitempty"`
CustomerImageInputMtok string `json:"customerImageInputMtok,omitempty"`
CustomerImageOutputMtok string `json:"customerImageOutputMtok,omitempty"`
CustomerImageCachedInputMtok string `json:"customerImageCachedInputMtok,omitempty"`
Provider string `json:"provider,omitempty"`
FeatureTags []string `json:"featureTags,omitempty"`
ScenarioTags []string `json:"scenarioTags,omitempty"`
MCIScore float64 `json:"mciScore,omitempty"`
}
ModelInfo is a model summary returned by ListModels.
type ModelListData ¶
type ModelListData struct {
Models []ModelInfo `json:"models"`
}
ModelListData is the data envelope returned by the model catalog.
type ModelListResponse ¶
type ModelListResponse struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Object string `json:"object,omitempty"`
Data ModelListData `json:"data"`
}
ModelListResponse contains the models available to the caller.
type ModelLocale ¶
type ModelLocale string
ModelLocale selects the language used for localized model descriptions.
const ( // ModelLocaleZH requests Chinese model descriptions. ModelLocaleZH ModelLocale = "zh" // ModelLocaleEN requests English model descriptions. ModelLocaleEN ModelLocale = "en" )
type ModelMetadata ¶
type ModelMetadata struct {
Tiers []CatalogOption `json:"tiers"`
SKUs []CatalogOption `json:"skus"`
FeatureTags []CatalogOption `json:"featureTags"`
IndustryPacks []CatalogOption `json:"industryPacks"`
Providers []CatalogOption `json:"providers"`
UpdatedAt string `json:"updatedAt"`
}
ModelMetadata contains available model catalog filter values.
type ModelMetadataResponse ¶
type ModelMetadataResponse struct {
Code int `json:"code"`
Data ModelMetadata `json:"data"`
Message string `json:"message"`
}
ModelMetadataResponse wraps model catalog metadata.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithAPIBaseURL ¶
WithAPIBaseURL configures the base URL for model and pricing endpoints.
func WithAPIKey ¶
WithAPIKey configures the API key used for authenticated requests.
func WithAnthropicBaseURL ¶
WithAnthropicBaseURL configures the Anthropic-compatible API base URL.
func WithAnthropicVersion ¶
WithAnthropicVersion configures the anthropic-version request header.
func WithHTTPClient ¶
func WithHTTPClient(httpClient HTTPClient) Option
WithHTTPClient configures the HTTP transport used by the client.
func WithHeader ¶
WithHeader adds a header to every request. Later calls replace the same key.
func WithOpenAIBaseURL ¶
WithOpenAIBaseURL configures the OpenAI-compatible API base URL.
func WithTimeout ¶
WithTimeout configures the maximum duration for a request, including reading a non-streaming response or consuming a streaming response. A non-positive duration disables the SDK timeout.
type Pricing ¶
type Pricing struct {
Tiers []PricingTier `json:"tiers"`
SKUs []PricingSKU `json:"skus"`
CurrentVersion string `json:"currentVersion"`
UpdatedAt string `json:"updatedAt"`
}
Pricing contains the current tier and SKU catalog.
type PricingResponse ¶
type PricingResponse struct {
Code int `json:"code"`
Data Pricing `json:"data"`
Message string `json:"message"`
}
PricingResponse wraps the current pricing catalog.
type PricingSKU ¶
type PricingSKU struct {
Code string `json:"code"`
Name string `json:"name"`
Description string `json:"description"`
}
PricingSKU describes an available pricing SKU.
type PricingTier ¶
type PricingTier struct {
Code string `json:"code"`
Name string `json:"name"`
Description string `json:"description"`
USDPerCredit string `json:"usdPerCredit"`
CreditsPerUSD string `json:"creditsPerUsd"`
Unit string `json:"unit"`
RateVersion string `json:"rateVersion"`
SortOrder int32 `json:"sortOrder"`
UpdatedAt string `json:"updatedAt"`
}
PricingTier describes a JoyToken credit conversion tier.
type Response ¶
type Response struct {
ID string `json:"id"`
Object string `json:"object"`
Status string `json:"status"`
Model string `json:"model"`
Output []ResponseOutputItem `json:"output,omitempty"`
Usage *ResponseUsage `json:"usage,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
Response is a non-streaming Responses API result or the response envelope included in response.created and response.completed stream events.
func (*Response) OutputText ¶
OutputText returns all output_text parts concatenated in output order.
type ResponseInputContentPart ¶
ResponseInputContentPart is one text part in a Responses API input message.
type ResponseInputItem ¶
type ResponseInputItem struct {
Type string `json:"type,omitempty"`
Role string `json:"role,omitempty"`
Content any `json:"content,omitempty"`
}
ResponseInputItem is one message in a Responses API input array. Content may be a string or a slice of ResponseInputContentPart values.
type ResponseOutputContent ¶
type ResponseOutputContent struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Annotations []any `json:"annotations,omitempty"`
}
ResponseOutputContent is one content part in a Responses API output message.
type ResponseOutputItem ¶
type ResponseOutputItem struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Role string `json:"role,omitempty"`
Status string `json:"status,omitempty"`
Content []ResponseOutputContent `json:"content,omitempty"`
}
ResponseOutputItem is one item returned in a Responses API output array.
type ResponseRequest ¶
type ResponseRequest struct {
Model string `json:"model"`
Input any `json:"input"`
Instructions string `json:"instructions,omitempty"`
Stream bool `json:"stream,omitempty"`
MaxOutputTokens *int `json:"max_output_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Tools []ResponseTool `json:"tools,omitempty"`
}
ResponseRequest is a request to the OpenAI-compatible Responses API. Model must be ModelAuto. Input may be a string or a slice of ResponseInputItem values.
type ResponseStream ¶
type ResponseStream struct {
// contains filtered or unexported fields
}
ResponseStream reads OpenAI-compatible Responses SSE events.
func (*ResponseStream) Close ¶
func (s *ResponseStream) Close() error
Close closes the underlying streaming response body.
func (*ResponseStream) Recv ¶
func (s *ResponseStream) Recv() (*ResponseStreamEvent, error)
Recv returns the next Responses event or io.EOF when the stream ends.
type ResponseStreamEvent ¶
type ResponseStreamEvent struct {
Type string `json:"type"`
SequenceNumber int `json:"sequence_number"`
Response *Response `json:"response,omitempty"`
OutputIndex int `json:"output_index,omitempty"`
ContentIndex int `json:"content_index,omitempty"`
ItemID string `json:"item_id,omitempty"`
Item *ResponseOutputItem `json:"item,omitempty"`
Part *ResponseOutputContent `json:"part,omitempty"`
Delta string `json:"delta,omitempty"`
Text string `json:"text,omitempty"`
}
ResponseStreamEvent is one SSE event returned by the Responses API. Fields are populated according to Type, for example Delta on response.output_text.delta and Response on response.completed.
type ResponseTool ¶
type ResponseTool struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters map[string]any `json:"parameters,omitempty"`
}
ResponseTool declares a function available to the Responses API. JoyToken currently supports function tools; built-in OpenAI tools are not forwarded.
type ResponseUsage ¶
type ResponseUsage struct {
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
}
ResponseUsage reports token usage using Responses API field names.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolFunction `json:"function"`
}
ToolCall describes a model-requested function call.
type ToolFunction ¶
ToolFunction identifies a function and its JSON arguments.
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens,omitempty"`
CompletionTokens int `json:"completion_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
Cost *float64 `json:"cost,omitempty"`
TotalCost *float64 `json:"total_cost,omitempty"`
}
Usage reports token and cost information for a request.