wirefake

package
v1.3.5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wirefake has fakes of the model APIs Blitz speaks (Anthropic's Messages API, the Gemini API and OpenAI's Responses API) that are as strict as the real ones (spec_056_conversation_adapters CA-150): each refuses, with the real API's status and words, what its documentation says it refuses. They answer from a script, sign their own reasoning as the real APIs do, and refuse a signature they didn't give, so a test that switches a conversation between them shows what a real switch would break.

Index

Constants

View Source
const LivePath = "/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent"

LivePath is the Gemini API's Live socket, as genai dials it.

Variables

This section is empty.

Functions

func ModelOf

func ModelOf(req map[string]any) string

ModelOf is the model a recorded request asked for: in its body (Anthropic, OpenAI) or its path (Gemini).

Types

type Call

type Call struct {
	Name string
	Args map[string]any
}

Call is one scripted tool call.

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

Fake is one API's fake: a server, its script, and what it was sent.

func Anthropic

func Anthropic(t testing.TB) *Fake

Anthropic fakes the Messages API (POST /v1/messages), streamed when asked ("stream": true) as the API streams: each block started, its deltas, stopped, then the message's stop reason and usage.

func Gemini

func Gemini(t testing.TB) *Fake

Gemini fakes the Gemini API's generateContent and streamGenerateContent (POST /v1beta/models/<model>:…; the stream as server-sent events, the whole answer in one chunk). Point the genai client at it with GOOGLE_GEMINI_BASE_URL.

func OpenAI

func OpenAI(t testing.TB) *Fake

OpenAI fakes the Responses API (POST <base>/responses), streamed when asked ("stream": true) as the API streams: the response created, each output item added and done, then the response completed.

func (*Fake) Refusals

func (f *Fake) Refusals() []string

Refusals are why the fake refused requests, in order, in the API's words.

func (*Fake) Remaining

func (f *Fake) Remaining() int

Remaining is how many scripted answers haven't been given yet.

func (*Fake) Requests

func (f *Fake) Requests() []map[string]any

Requests are the bodies the fake was sent, accepted or not.

func (*Fake) Script

func (f *Fake) Script(rs ...Reply)

Script queues answers, given in order to the requests the fake accepts.

func (*Fake) Streamed

func (f *Fake) Streamed() int

Streamed is how many answers the fake gave as streams.

func (*Fake) URL

func (f *Fake) URL() string

URL is the fake's address.

type File

type File struct {
	MIME string
	Data []byte
}

File is a file a model makes.

type Live added in v1.0.0

type Live struct {
	// URL is the client's base URL (ws://host), for genai's HTTPOptions.
	URL string
	// contains filtered or unexported fields
}

Live fakes the Gemini API's Live socket (BidiGenerateContent) as strictly as the API (spec_063_voice VOX-60): the API key in x-goog-api-key, setup first (a model, audio answers, a prebuilt voice) and answered with setupComplete; then only realtime input (16 kHz PCM, activity start and end), client content and tool responses, each tool response answering a call the fake made. Anything else fails the test and closes the socket with 1007, as the API does. The test plays the server: Send pushes a message to the open connection, Next waits for the client's next message; GoAway and Drop end a connection, and a client resuming must name the last handle given (Resume).

func NewLive added in v1.0.0

func NewLive(t testing.TB, key string) *Live

NewLive starts a fake Live API that wants key.

func (*Live) Call added in v1.0.0

func (f *Live) Call(id, name string, args map[string]any, d time.Duration) map[string]any

Call is the model calling a tool, and the response the client sends back, within d.

func (*Live) Close added in v1.0.0

func (f *Live) Close()

Close ends the connection and the server.

func (*Live) Drop added in v1.0.0

func (f *Live) Drop()

Drop ends the connection with no warning (a network that went away).

func (*Live) GoAway added in v1.0.0

func (f *Live) GoAway()

GoAway warns the client the connection ends soon, then ends it, as the API does at its session limit.

func (*Live) Heard added in v1.0.0

func (f *Live) Heard(text string)

Heard is the server's transcript of what the person said.

func (*Live) Next added in v1.0.0

func (f *Live) Next(key string, d time.Duration) map[string]any

Next waits for the client's next message that has key (others are skipped: audio streams constantly), within d.

func (*Live) Opened added in v1.0.0

func (f *Live) Opened(d time.Duration) map[string]any

Opened waits for a connection's setup (VOX-07's resumption opens another) and returns it.

func (*Live) Resume added in v1.0.0

func (f *Live) Resume(handle string)

Resume gives the client a handle to resume with.

func (*Live) Say added in v1.0.0

func (f *Live) Say(text string, pcm []byte)

Say is the server speaking: the transcript of what it says and its audio (24 kHz PCM), then the turn's end.

func (*Live) Send added in v1.0.0

func (f *Live) Send(msg map[string]any)

Send pushes msg to the open connection, as the server sends it.

func (*Live) Setups added in v1.0.0

func (f *Live) Setups() []map[string]any

Setups are the setups received, one a connection.

type Reply

type Reply struct {
	// Thought is the model's reasoning, returned (and signed) only when
	// the request asks for reasoning, as the real APIs do.
	Thought string
	// Text is the answer's text.
	Text string
	// Calls are the tool calls the answer makes.
	Calls []Call
	// Files are files the answer makes (an image model's pictures), which
	// only the Gemini fake returns, as Gemini does: inline data in the
	// model's turn.
	Files []File
	// Usage is what the answer reports; zero gives a small default.
	Usage Usage
	// Status, when set, refuses the request with this HTTP status (a 503:
	// the model is overloaded) instead of answering.
	Status int
	// Search is a web search the model runs with its provider's own tool,
	// before its Text, which cites the results; only when the request
	// offers that tool, as a real model can search only then.
	Search *Search
	// Finish, when set, is why the answer ended, in the API's own words:
	// Anthropic's stop_reason ("refusal"), Gemini's finishReason ("SAFETY",
	// "RECITATION", "MALFORMED_FUNCTION_CALL"), OpenAI's incomplete reason
	// ("content_filter").
	Finish string
	// Extra are parts of the answer Blitz has no form for, as the API
	// sends them: an Anthropic content block (a server_tool_use), an
	// OpenAI output item (a web_search_call).
	Extra []map[string]any
	// TooLong refuses the request as longer than the model's context
	// window, in the API's words.
	TooLong bool
	// Refusal is the refusal's message, in the API's error shape (its
	// status's text when empty): a model refusing what it can't do, in
	// the words its server uses ("… does not support tools").
	Refusal string
}

Reply is one scripted answer, given in each API's own form.

type Search struct {
	Query   string
	Results []Source
}

Search is a provider web search: its query and what it found.

type Source

type Source struct {
	Title, URL string
}

Source is one search result.

type Usage

type Usage struct {
	Input, Cached, Output, Reasoning int
}

Usage is a reply's token counts, in the neutral sense: Output includes Reasoning (each fake reports them as its API does).

Jump to

Keyboard shortcuts

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