From 63f3f494c27ddc3468598d8e30b35f8ed22f3c07 Mon Sep 17 00:00:00 2001 From: Levi Neely Date: Wed, 29 Jul 2026 20:00:26 +0200 Subject: [PATCH] backend: add Noop backend for testing Programmable test backend that satisfies Backend interface without network calls. ChatStreamFunc can be set to control behavior (blocking, custom responses, errors). --- backend/noop.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 backend/noop.go diff --git a/backend/noop.go b/backend/noop.go new file mode 100644 index 0000000..330fdc1 --- /dev/null +++ b/backend/noop.go @@ -0,0 +1,47 @@ +package backend + +import "context" + +// Noop is a programmable test backend that satisfies the Backend interface +// without making any network calls. By default it returns an empty response +// immediately. Set ChatStreamFunc to override behavior. +type Noop struct { + name string + model string + + // ChatStreamFunc, if non-nil, replaces the default ChatStream behavior. + // Use this to simulate blocking, errors, or custom responses. + ChatStreamFunc func(ctx context.Context, messages []Message, tools []Tool, params GenerationParams) (<-chan StreamEvent, error) +} + +// NewNoop creates a Noop backend with the given name and model. +func NewNoop(name, model string) *Noop { + if name == "" { + name = "noop" + } + if model == "" { + model = "noop" + } + return &Noop{name: name, model: model} +} + +func (n *Noop) Name() string { return n.name } +func (n *Noop) DefaultModel() string { return n.model } +func (n *Noop) Model() string { return n.model } +func (n *Noop) SetModel(model string) { n.model = model } + +func (n *Noop) ContextLength(_ context.Context) int { return 128000 } + +func (n *Noop) Models(_ context.Context) []string { + return []string{n.model} +} + +func (n *Noop) ChatStream(ctx context.Context, messages []Message, tools []Tool, params GenerationParams) (<-chan StreamEvent, error) { + if n.ChatStreamFunc != nil { + return n.ChatStreamFunc(ctx, messages, tools, params) + } + ch := make(chan StreamEvent, 1) + ch <- StreamEvent{Done: true, StopReason: "end_turn"} + close(ch) + return ch, nil +}