Documentation
¶
Index ¶
- func NewAnthropic(t testing.TB, handler AnthropicHandler) string
- func NewOpenAI(t testing.TB, handler OpenAIHandler) string
- func OpenAI(t testing.TB) string
- func StringResponseField(values map[string]interface{}, key string) string
- type AnthropicChunk
- func AnthropicReasoningTextChunks(reasoning []AnthropicReasoningBlock, text string) []AnthropicChunk
- func AnthropicTextChunks(deltas ...string) []AnthropicChunk
- func AnthropicTextChunksWithCacheUsage(usage AnthropicUsage, deltas ...string) []AnthropicChunk
- func AnthropicTextChunksWithMessageUsages(messageStartUsage, messageDeltaUsage AnthropicUsage, deltas ...string) []AnthropicChunk
- func AnthropicToolCallChunks(toolName string, inputJSONDeltas ...string) []AnthropicChunk
- type AnthropicChunkMessage
- type AnthropicContentBlock
- type AnthropicDeltaBlock
- type AnthropicHandler
- type AnthropicMessage
- type AnthropicReasoningBlock
- type AnthropicRequest
- type AnthropicRequestMessage
- type AnthropicRequestTool
- type AnthropicResponse
- type AnthropicUsage
- type ErrorResponse
- type FakeModel
- func (m *FakeModel) Generate(ctx context.Context, call fantasy.Call) (*fantasy.Response, error)
- func (m *FakeModel) GenerateObject(ctx context.Context, call fantasy.ObjectCall) (*fantasy.ObjectResponse, error)
- func (m *FakeModel) Model() string
- func (m *FakeModel) Provider() string
- func (m *FakeModel) Stream(ctx context.Context, call fantasy.Call) (fantasy.StreamResponse, error)
- func (m *FakeModel) StreamObject(ctx context.Context, call fantasy.ObjectCall) (fantasy.ObjectStreamResponse, error)
- type MockAIBridgeTransport
- type MockAIBridgeTransportOption
- type OpenAIChunk
- type OpenAIChunkChoice
- type OpenAICompletion
- type OpenAICompletionChoice
- type OpenAICompletionUsage
- type OpenAIHandler
- type OpenAIMessage
- type OpenAIReasoningItem
- type OpenAIRequest
- type OpenAIResponse
- type OpenAITool
- type OpenAIToolCall
- type OpenAIToolCallFunction
- type OpenAIToolFunction
- type OpenAIWebSearchCall
- type RecordedRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewAnthropic ¶
func NewAnthropic(t testing.TB, handler AnthropicHandler) string
NewAnthropic creates a new Anthropic test server with a handler function. The handler is called for each request and should return either a streaming response (via channel) or a non-streaming response. Returns the base URL of the server.
func NewOpenAI ¶
func NewOpenAI(t testing.TB, handler OpenAIHandler) string
NewOpenAI creates a new OpenAI test server with a handler function. The handler is called for each request and should return either a streaming response (via channel) or a non-streaming response. Returns the base URL of the server.
func OpenAI ¶ added in v2.33.0
OpenAI creates a fake OpenAI-compatible test server with a sensible default handler and returns its base URL. It handles both the Responses API (/responses) and the Chat Completions API (/chat/completions).
Non-streaming requests (e.g. structured-output title generation) receive a JSON payload satisfying the generatedTitle schema. Streaming requests (e.g. the main chat loop) receive a single text chunk. Use NewOpenAI when a test needs control over the response.
func StringResponseField ¶ added in v2.33.0
StringResponseField returns the string value for key from a decoded Responses API item, or an empty string when the field is absent or not a string.
Types ¶
type AnthropicChunk ¶
type AnthropicChunk struct {
Type string `json:"type"`
Index int `json:"index,omitempty"`
Message AnthropicChunkMessage `json:"message,omitempty"`
ContentBlock AnthropicContentBlock `json:"content_block,omitempty"`
Delta AnthropicDeltaBlock `json:"delta,omitempty"`
StopReason string `json:"stop_reason,omitempty"`
StopSequence *string `json:"stop_sequence,omitempty"`
Usage AnthropicUsage `json:"usage,omitempty"`
UsageMap map[string]int `json:"-"`
}
AnthropicChunk represents a streaming chunk from Anthropic.
func AnthropicReasoningTextChunks ¶ added in v2.35.0
func AnthropicReasoningTextChunks(reasoning []AnthropicReasoningBlock, text string) []AnthropicChunk
AnthropicReasoningTextChunks creates a streaming response with one or more thinking blocks followed by one text block.
func AnthropicTextChunks ¶
func AnthropicTextChunks(deltas ...string) []AnthropicChunk
AnthropicTextChunks creates a complete streaming response with text deltas. Takes text deltas and creates all required chunks (message_start, content_block_start, content_block_delta for each delta, content_block_stop, message_delta, message_stop).
func AnthropicTextChunksWithCacheUsage ¶ added in v2.33.0
func AnthropicTextChunksWithCacheUsage(usage AnthropicUsage, deltas ...string) []AnthropicChunk
AnthropicTextChunksWithCacheUsage creates a streaming response with text deltas and explicit cache token usage. The message_start event carries the initial input and cache token counts, and the final message_delta carries the output token count.
func AnthropicTextChunksWithMessageUsages ¶ added in v2.37.0
func AnthropicTextChunksWithMessageUsages(messageStartUsage, messageDeltaUsage AnthropicUsage, deltas ...string) []AnthropicChunk
AnthropicTextChunksWithMessageUsages creates a streaming response with independent message_start and message_delta usage.
func AnthropicToolCallChunks ¶
func AnthropicToolCallChunks(toolName string, inputJSONDeltas ...string) []AnthropicChunk
AnthropicToolCallChunks creates a complete streaming response for a tool call. Input JSON can be split across multiple deltas, matching Anthropic's input_json_delta streaming behavior.
type AnthropicChunkMessage ¶
type AnthropicChunkMessage struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Model string `json:"model"`
Usage map[string]int `json:"usage,omitempty"`
}
AnthropicChunkMessage represents message metadata in a chunk.
type AnthropicContentBlock ¶
type AnthropicContentBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Thinking string `json:"thinking,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
Content any `json:"content,omitempty"`
}
AnthropicContentBlock represents a content block in a chunk.
type AnthropicDeltaBlock ¶
type AnthropicDeltaBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Thinking string `json:"thinking,omitempty"`
Signature string `json:"signature,omitempty"`
PartialJSON string `json:"partial_json,omitempty"`
}
AnthropicDeltaBlock represents a delta block in a chunk.
type AnthropicHandler ¶
type AnthropicHandler func(req *AnthropicRequest) AnthropicResponse
AnthropicHandler handles Anthropic API requests and returns a response.
type AnthropicMessage ¶
type AnthropicMessage struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Role string `json:"role"`
Content string `json:"content,omitempty"`
Model string `json:"model,omitempty"`
StopReason string `json:"stop_reason,omitempty"`
Usage AnthropicUsage `json:"usage,omitempty"`
}
AnthropicMessage represents a message in an Anthropic response.
type AnthropicReasoningBlock ¶ added in v2.35.0
AnthropicReasoningBlock describes one Anthropic thinking block for a streaming test response.
type AnthropicRequest ¶
type AnthropicRequest struct {
*http.Request // Embed http.Request
Model string `json:"model"`
System json.RawMessage `json:"system,omitempty"`
Messages []AnthropicRequestMessage `json:"messages"`
Tools []AnthropicRequestTool `json:"tools,omitempty"`
Stream bool `json:"stream,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
OutputConfig json.RawMessage `json:"output_config,omitempty"`
Thinking json.RawMessage `json:"thinking,omitempty"`
}
AnthropicRequest represents an Anthropic messages request.
type AnthropicRequestMessage ¶
type AnthropicRequestMessage struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
}
AnthropicRequestMessage represents a message in an Anthropic request. Content may be either a string or a structured content array.
type AnthropicRequestTool ¶ added in v2.35.0
type AnthropicRequestTool struct {
Name string `json:"name"`
}
AnthropicRequestTool represents a tool in an Anthropic request.
type AnthropicResponse ¶
type AnthropicResponse struct {
StreamingChunks <-chan AnthropicChunk
Response *AnthropicMessage
Error *ErrorResponse // If set, server returns this HTTP error instead of streaming/JSON.
}
AnthropicResponse represents a response to an Anthropic request. Either StreamingChunks or Response should be set, not both.
func AnthropicNonStreamingResponse ¶
func AnthropicNonStreamingResponse(text string) AnthropicResponse
AnthropicNonStreamingResponse creates a non-streaming response with the given text.
func AnthropicStreamingResponse ¶
func AnthropicStreamingResponse(chunks ...AnthropicChunk) AnthropicResponse
AnthropicStreamingResponse creates a streaming response from chunks.
type AnthropicUsage ¶
type AnthropicUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"`
}
AnthropicUsage represents usage information in an Anthropic response.
type ErrorResponse ¶
ErrorResponse describes an HTTP error that a test server should return instead of a normal streaming or JSON response.
func ValidateResponsesAPIInput ¶ added in v2.33.0
func ValidateResponsesAPIInput(items []interface{}) *ErrorResponse
ValidateResponsesAPIInput validates the Responses API item relationships that OpenAI enforces but the fake test server would otherwise miss.
type FakeModel ¶ added in v2.33.0
type FakeModel struct {
ProviderName string
ModelName string
GenerateFn func(context.Context, fantasy.Call) (*fantasy.Response, error)
StreamFn func(context.Context, fantasy.Call) (fantasy.StreamResponse, error)
GenerateObjectFn func(context.Context, fantasy.ObjectCall) (*fantasy.ObjectResponse, error)
StreamObjectFn func(context.Context, fantasy.ObjectCall) (fantasy.ObjectStreamResponse, error)
}
FakeModel is a configurable test double for fantasy.LanguageModel. Calling a method whose function field is nil panics, forcing tests to be explicit about which methods they expect to be invoked.
func (*FakeModel) GenerateObject ¶ added in v2.33.0
func (m *FakeModel) GenerateObject(ctx context.Context, call fantasy.ObjectCall) (*fantasy.ObjectResponse, error)
func (*FakeModel) StreamObject ¶ added in v2.33.0
func (m *FakeModel) StreamObject(ctx context.Context, call fantasy.ObjectCall) (fantasy.ObjectStreamResponse, error)
type MockAIBridgeTransport ¶ added in v2.35.0
type MockAIBridgeTransport struct {
// contains filtered or unexported fields
}
MockAIBridgeTransport is a test aibridge.TransportFactory that redirects requests to a target URL (typically a chattest.NewOpenAI or chattest.NewAnthropic server) and records each request for later inspection.
By default it strips the leading "/v1" path segment before forwarding, matching how the real AI Gateway transport rewrites upstream-shaped requests. Pass WithPreservePath when the target server expects the full original path.
func NewMockAIBridgeTransport ¶ added in v2.35.0
func NewMockAIBridgeTransport(t testing.TB, targetBaseURL string, opts ...MockAIBridgeTransportOption) *MockAIBridgeTransport
NewMockAIBridgeTransport creates a MockAIBridgeTransport that forwards to targetBaseURL.
func (*MockAIBridgeTransport) RequestsSnapshot ¶ added in v2.35.0
func (f *MockAIBridgeTransport) RequestsSnapshot() []RecordedRequest
RequestsSnapshot returns a copy of all recorded requests.
func (*MockAIBridgeTransport) TransportFor ¶ added in v2.35.0
func (f *MockAIBridgeTransport) TransportFor(providerName string, source aibridge.Source) (http.RoundTripper, error)
TransportFor implements aibridge.TransportFactory.
type MockAIBridgeTransportOption ¶ added in v2.35.0
type MockAIBridgeTransportOption func(*MockAIBridgeTransport)
MockAIBridgeTransportOption configures a MockAIBridgeTransport.
func WithPreservePath ¶ added in v2.35.0
func WithPreservePath() MockAIBridgeTransportOption
WithPreservePath disables the default "/v1" path stripping so the target server receives the full original request path.
type OpenAIChunk ¶
type OpenAIChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []OpenAIChunkChoice `json:"choices"`
}
OpenAIChunk represents a streaming chunk from OpenAI.
func OpenAITextChunks ¶
func OpenAITextChunks(deltas ...string) []OpenAIChunk
OpenAITextChunks creates streaming chunks with text deltas. Each delta string becomes a separate chunk with a single choice. Returns a slice of chunks, one per delta, with each choice having its index (0, 1, 2, ...).
func OpenAIToolCallChunk ¶
func OpenAIToolCallChunk(toolName, arguments string) OpenAIChunk
OpenAIToolCallChunk creates a streaming chunk with a tool call. Takes the tool name and arguments JSON string, creates a tool call for choice index 0.
type OpenAIChunkChoice ¶
type OpenAIChunkChoice struct {
Index int `json:"index"`
Delta string `json:"delta,omitempty"`
ReasoningDelta string `json:"reasoning_delta,omitempty"`
ToolCalls []OpenAIToolCall `json:"tool_calls,omitempty"`
FinishReason string `json:"finish_reason,omitempty"`
}
OpenAIChunkChoice represents a choice in a streaming chunk.
type OpenAICompletion ¶
type OpenAICompletion struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []OpenAICompletionChoice `json:"choices"`
Usage OpenAICompletionUsage `json:"usage"`
}
OpenAICompletion represents a non-streaming OpenAI completion response.
type OpenAICompletionChoice ¶
type OpenAICompletionChoice struct {
Index int `json:"index"`
Message OpenAIMessage `json:"message"`
ToolCalls []OpenAIToolCall `json:"tool_calls,omitempty"`
FinishReason string `json:"finish_reason"`
}
OpenAICompletionChoice represents a choice in a completion response.
type OpenAICompletionUsage ¶
type OpenAICompletionUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
OpenAICompletionUsage represents usage information in a completion response.
type OpenAIHandler ¶
type OpenAIHandler func(req *OpenAIRequest) OpenAIResponse
OpenAIHandler handles OpenAI API requests and returns a response.
type OpenAIMessage ¶
OpenAIMessage represents a message in an OpenAI request.
func (*OpenAIMessage) UnmarshalJSON ¶ added in v2.37.0
func (m *OpenAIMessage) UnmarshalJSON(data []byte) error
UnmarshalJSON accepts both string content and the structured content-part array the SDK emits for multi-part messages, concatenating the text items with newlines.
type OpenAIReasoningItem ¶
type OpenAIReasoningItem struct {
ID string `json:"id,omitempty"`
Summary string `json:"summary,omitempty"`
EncryptedContent string `json:"encrypted_content,omitempty"`
}
OpenAIReasoningItem configures a streamed reasoning output item for the Responses API test server.
type OpenAIRequest ¶
type OpenAIRequest struct {
*http.Request
Model string `json:"model"`
Messages []OpenAIMessage `json:"messages"`
Stream bool `json:"stream,omitempty"`
Tools []OpenAITool `json:"tools,omitempty"`
Prompt []interface{} `json:"prompt,omitempty"` // Responses API input or prompt.
Store *bool `json:"store,omitempty"`
// RawBody holds the original request body so callers can inspect
// fields the typed struct does not expose, such as the Responses
// API "input" payload. It is populated before JSON decoding.
RawBody []byte `json:"-"`
}
OpenAIRequest represents an OpenAI chat completion request.
func (*OpenAIRequest) UnmarshalJSON ¶ added in v2.33.0
func (r *OpenAIRequest) UnmarshalJSON(data []byte) error
type OpenAIResponse ¶
type OpenAIResponse struct {
StreamingChunks <-chan OpenAIChunk
Response *OpenAICompletion
Reasoning *OpenAIReasoningItem
WebSearch *OpenAIWebSearchCall
ResponseID string // If set, used as the response ID in streamed events; otherwise auto-generated.
Error *ErrorResponse // If set, server returns this HTTP error instead of streaming/JSON.
}
OpenAIResponse represents a response to an OpenAI request. Either StreamingChunks or Response should be set, not both.
func OpenAIErrorResponse ¶
func OpenAIErrorResponse(statusCode int, errorType, message string) OpenAIResponse
OpenAIErrorResponse returns an OpenAIResponse that causes the test server to respond with the given HTTP status code and error.
func OpenAINonStreamingResponse ¶
func OpenAINonStreamingResponse(text string) OpenAIResponse
OpenAINonStreamingResponse creates a non-streaming response with the given text.
func OpenAIRateLimitResponse ¶
func OpenAIRateLimitResponse() OpenAIResponse
OpenAIRateLimitResponse returns a 429 rate limit error.
func OpenAIStreamingResponse ¶
func OpenAIStreamingResponse(chunks ...OpenAIChunk) OpenAIResponse
OpenAIStreamingResponse creates a streaming response from chunks.
type OpenAITool ¶
type OpenAITool struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
Function OpenAIToolFunction `json:"function"`
}
OpenAITool represents a tool definition in an OpenAI request.
type OpenAIToolCall ¶
type OpenAIToolCall struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Function OpenAIToolCallFunction `json:"function,omitempty"`
Index int `json:"index,omitempty"` // For streaming deltas
}
OpenAIToolCall represents a tool call in a streaming chunk or completion.
type OpenAIToolCallFunction ¶
type OpenAIToolCallFunction struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
OpenAIToolCallFunction represents the function details in a tool call.
type OpenAIToolFunction ¶
type OpenAIToolFunction struct {
Name string `json:"name"`
}
OpenAIToolFunction represents the function definition inside a tool.
type OpenAIWebSearchCall ¶
type OpenAIWebSearchCall struct {
ID string `json:"id,omitempty"`
Query string `json:"query,omitempty"`
}
OpenAIWebSearchCall configures a streamed web_search_call output item for the Responses API test server.
type RecordedRequest ¶ added in v2.35.0
type RecordedRequest struct {
// Request is a clone of the original [http.Request].
Request *http.Request
// ProviderName is the AI provider instance name passed to
// [TransportFactory.TransportFor].
ProviderName string
// Source is the aibridge source passed to TransportFor.
Source aibridge.Source
// APIKeyID is the delegated API key ID attached to request ctx.
APIKeyID string
}
RecordedRequest captures metadata from a single request that passed through the mock transport factory. Fields not already available on http.Request are included here; tests can access headers and path via RecordedRequest.Request.