joytoken

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

JoyToken SDK for Go

English | 简体中文

First-party Go client for JoyToken's public developer API.

The module is distributed directly from GitHub and does not require a separate package-registry publication.

go get github.com/jd-opensource/joytoken-sdk-go

The default endpoint is https://api.joytokens.ai. Set JOY_TOKEN_API_BASE_URL (or pass WithAPIBaseURL) to use another environment. Requests time out after 60 seconds by default; pass WithTimeout(0) to disable that limit.

client := joytoken.NewClient(
    joytoken.WithAPIKey(os.Getenv("JOY_TOKEN_API_KEY")),
)

completion, err := client.CreateChatCompletion(ctx, joytoken.ChatCompletionRequest{
    Model: joytoken.ModelAuto,
    Messages: []joytoken.ChatMessage{
        {Role: "user", Content: "Say hello"},
    },
})

OpenAI Responses:

response, err := client.CreateResponse(ctx, joytoken.ResponseRequest{
    Model: joytoken.ModelAuto,
    Input: "Say hello",
})
if err != nil {
    return err
}
fmt.Println(response.OutputText())

OpenAI Images:

image, err := client.GenerateImage(ctx, joytoken.ImageGenerationRequest{
    Model:  joytoken.ModelAuto,
    Prompt: "A neon JoyToken logo on a black background",
    Size:   "1024x1024",
})
if err != nil {
    return err
}
fmt.Println(image.Data[0].URL)

Anthropic Messages:

message, err := client.CreateMessage(ctx, joytoken.MessageRequest{
    Model:     joytoken.ModelAuto,
    MaxTokens: 1024,
    Messages: []joytoken.MessageParam{
        {Role: "user", Content: "Say hello"},
    },
})

The client supports:

  • POST /openai/v1/chat/completions
  • streaming chat completions via SSE
  • POST /openai/v1/responses
  • streaming Responses text events via SSE
  • POST /openai/v1/images/generations
  • POST /anthropic/v1/messages
  • streaming Anthropic Messages via SSE
  • GET /api/v1/models
  • GET /api/v1/models/meta
  • GET /api/v1/pricing

All model requests require joytoken.ModelAuto; concrete model IDs are not accepted.

Model descriptions can be localized with ListModelsWithOptions. Locale accepts joytoken.ModelLocaleZH or joytoken.ModelLocaleEN; when omitted, the API defaults to English.

models, err := client.ListModelsWithOptions(ctx, joytoken.ListModelsOptions{
    Locale: joytoken.ModelLocaleZH,
})

The SDK preserves the API response envelope; catalog entries are available at models.Data.Models.

The agent subpackage provides the same bounded tool-calling loop as the TypeScript Agent SDK:

go get github.com/jd-opensource/joytoken-sdk-go/agent
import (
    "context"
    "os"

    joytoken "github.com/jd-opensource/joytoken-sdk-go"
    "github.com/jd-opensource/joytoken-sdk-go/agent"
)

ctx := context.Background()
client := joytoken.NewClient(joytoken.WithAPIKey(os.Getenv("JOY_TOKEN_API_KEY")))
provider := agent.NewJoyTokenProvider(client)
runner := agent.New(agent.AgentOptions{
    Model: provider,
    Tools: []agent.AgentTool{{
        Name: "lookup",
        Execute: func(ctx context.Context, input any, execution agent.ToolExecutionContext) (any, error) {
            return "record:42", nil
        },
    }},
})
result, err := runner.Run(ctx, "Summarize record 42")

Every run has a hard eight-step limit by default. Use RunWithOptions with MaxSteps: agent.Int(6) or add StepCountIs, MaxToolCalls, and MaxCost conditions.

Streaming

stream, err := client.StreamChatCompletion(ctx, joytoken.ChatCompletionRequest{
    Model: joytoken.ModelAuto,
    Messages: []joytoken.ChatMessage{{Role: "user", Content: "Say hello"}},
})
if err != nil {
    return err
}
defer stream.Close()

for {
    chunk, err := stream.Recv()
    if errors.Is(err, io.EOF) {
        break
    }
    if err != nil {
        return err
    }
    fmt.Print(chunk.Choices[0].Delta["content"])
}

StreamMessage exposes the same iterator pattern for Anthropic Messages. Always close a stream when the consumer stops early.

Errors

Authenticated model calls, model metadata and pricing requests return joytoken.ErrMissingAPIKey before sending a network request when no API key is configured. ListModels remains the unauthenticated catalog call.

HTTP failures are returned as *joytoken.APIError. Use joytoken.IsAPIError(err) or errors.As to inspect the status code, request ID, response headers, and parsed response body. The Agent package returns provider and tool errors to the caller without hiding them.

Validate

go test ./...

cd example
go test ./...

Live example

cd example
export JOY_TOKEN_API_KEY="..."
go run ./live

Contributing

See CONTRIBUTING.md, SECURITY.md, and CODE_OF_CONDUCT.md.

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

View Source
const ModelAuto = "auto"

ModelAuto is the only model value accepted by JoyToken requests.

Variables

View Source
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

func IsAPIError(err error) bool

IsAPIError reports whether err contains an APIError.

Types

type APIError

type APIError struct {
	StatusCode      int
	RequestID       string
	ResponseHeaders http.Header
	Body            any
}

APIError describes a non-successful JoyToken HTTP response.

func (*APIError) Error

func (e *APIError) Error() string

Error returns a readable API failure description.

type CatalogOption

type CatalogOption struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

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

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 NewClient

func NewClient(opts ...Option) *Client

NewClient creates a JoyToken client from environment defaults and options.

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

func (c *Client) CreateResponse(ctx context.Context, request ResponseRequest) (*Response, error)

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

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

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

type MessageParam struct {
	Role    string `json:"role"`
	Content any    `json:"content"`
}

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

func WithAPIBaseURL(apiBaseURL string) Option

WithAPIBaseURL configures the base URL for model and pricing endpoints.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey configures the API key used for authenticated requests.

func WithAnthropicBaseURL

func WithAnthropicBaseURL(anthropicBaseURL string) Option

WithAnthropicBaseURL configures the Anthropic-compatible API base URL.

func WithAnthropicVersion

func WithAnthropicVersion(anthropicVersion string) Option

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

func WithHeader(key, value string) Option

WithHeader adds a header to every request. Later calls replace the same key.

func WithOpenAIBaseURL

func WithOpenAIBaseURL(openAIBaseURL string) Option

WithOpenAIBaseURL configures the OpenAI-compatible API base URL.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

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

func (r *Response) OutputText() string

OutputText returns all output_text parts concatenated in output order.

type ResponseInputContentPart

type ResponseInputContentPart struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

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

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

type ToolFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

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.

Directories

Path Synopsis
Package agent provides tool-calling agent helpers built on the JoyToken client.
Package agent provides tool-calling agent helpers built on the JoyToken client.

Jump to

Keyboard shortcuts

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