Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/provider/replay"

Package replay is a scripted Provider for tests of the reducer, the driver and hosts’ own code. It answers each call with the next scripted turn: live deltas, then a response, an error before streaming, or a failure mid-stream. It records the requests it was called with, so a test can check what the loop sent, and never touches the network.

It is not the transcript replay of recorded provider traffic: adapters test their wire handling against HTTP transcripts in their own packages.

Example

p, err := replay.New(provider.Caps{ContextWindow: 200_000}, replay.Text("Hello."))
if err != nil {
panic(err)
}
stream, err := p.Stream(context.Background(), &provider.Request{Model: "any"})
if err != nil {
panic(err)
}
for ev, err := range stream {
if err != nil {
panic(err)
}
switch ev.Kind {
case provider.EventTextDelta:
fmt.Println("delta:", ev.Delta)
case provider.EventDone:
fmt.Println("stop:", ev.Response.StopReason)
}
}
fmt.Println("turns left:", p.Remaining())
// Output:
// delta: Hello.
// stop: end_turn
// turns left: 0

Output:

delta: Hello.
stop: end_turn
turns left: 0
type Provider struct {
// contains filtered or unexported fields
}

Provider serves scripted turns in order. It is safe for concurrent use; concurrent calls take turns in the order they arrive.

func New(caps provider.Caps, turns ...Turn) (*Provider, error)

New returns a provider that answers calls with turns, in order, and reports caps for every model. It checks the script and keeps its own copy of it. Its family is that of the scripted items with raw payloads, which must all agree, or history.FamilyOpenAICompat if none has one.

func (p *Provider) Capabilities(ctx context.Context, _ string) (provider.Caps, error)

Capabilities returns the provider’s capabilities for any model.

func (p *Provider) Family() history.Family

Family returns the family of the scripted raw payloads.

func (p *Provider) Remaining() int

Remaining returns how many turns haven’t been served yet.

func (p *Provider) Requests() []*provider.Request

Requests returns copies of the requests the provider was called with, in order, excluding calls that weren’t dispatched.

func (p *Provider) Stream(ctx context.Context, req *provider.Request) (provider.Stream, error)

Stream answers req with the next turn. A call whose ctx is already done is not dispatched and leaves the turn for the next call. The stream can be read once; reading it again yields only an error.

type Turn struct {
// Check, if set, inspects the request. An error fails the call as a
// non-retryable invalid request, so a test sees the mismatch.
Check func(req *provider.Request) error
// Deltas are the live events streamed before the call ends. EventDone
// is not allowed here; Response supplies it.
Deltas []provider.Event
// Response ends the stream with an EventDone carrying it.
Response *provider.Response
// Err fails the call before any stream starts.
Err error
// FailAfter fails the stream after its deltas, with no EventDone.
FailAfter error
}

Turn is one scripted model call. Exactly one of Response, Err and FailAfter says how it ends.

func Reported(status, text string) Turn

Reported returns a turn in which the model answers with text and ends the run with zelk’s ReportOutcome step, reporting status (met, partial, not_met or blocked) with text as its summary: the way a model finishes a session that requires a report. A met report names one requirement, the request, confirmed by a check, since zelk refuses a met without requirements.

func Summary(summary string) Turn

Summary returns a turn in which the model answers a compaction call with summary between the <summary> tags the call asks for.

func Text(text string) Turn

Text returns a turn in which the model answers with text and ends its turn.

func ToolCalls(calls ...history.ToolCall) Turn

ToolCalls returns a turn in which the model calls tools and waits for their results.