Documentation
¶
Overview ¶
Package replay provides Model implementations that need no provider: a scripted model for unit tests, and a recorder and replayer pair so that a real model's responses can be captured once and served deterministically afterwards. Tests and CI never call a provider.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotRecorded = errors.New("replay: no recorded response for this request")
ErrNotRecorded means the replayer has no response for the request, or the request has been made more times than it was recorded.
Functions ¶
func Key ¶
func Key(model string, req agentrt.ModelRequest) (string, error)
Key is the replay key of a request: the SHA-256 of its canonical JSON with the model name. Numbers keep their literal text, so two requests that differ only in an integer beyond float64 precision have different keys; for every number float64 represents exactly, which is every number in the recordings written before this, the key is unchanged.
Types ¶
type Recorder ¶
Recorder wraps a model and stores every successful exchange in Dir.
Exchanges with identical requests are stored in sequence: the first at <key>.json, the second at <key>.2.json, and so on, so a replay serves them in the order they were recorded rather than the last one for every call. The first exchange for a key in a Recorder's lifetime replaces whatever an earlier session recorded under that key, including any higher-numbered files, so re-recording a run leaves nothing stale.
func (*Recorder) Generate ¶
func (r *Recorder) Generate(ctx context.Context, req agentrt.ModelRequest) (agentrt.ModelResponse, error)
Generate implements agentrt.Model.
type Replayer ¶
Replayer serves recorded responses and fails on anything unrecorded. Identical requests are served in the order they were recorded; a request made more times than it was recorded is unrecorded, because the replayed session has diverged from the recorded one.
func (*Replayer) Generate ¶
func (p *Replayer) Generate(_ context.Context, req agentrt.ModelRequest) (agentrt.ModelResponse, error)
Generate implements agentrt.Model.
type Scripted ¶
type Scripted struct {
ModelName string
Responses []agentrt.ModelResponse
// Errors, when set, is consulted first: a non-nil entry is returned
// instead of the response at that index.
Errors []error
// Requests records every request received.
Requests []agentrt.ModelRequest
// contains filtered or unexported fields
}
Scripted returns canned responses in order.
func (*Scripted) Generate ¶
func (s *Scripted) Generate(_ context.Context, req agentrt.ModelRequest) (agentrt.ModelResponse, error)
Generate implements agentrt.Model.