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).
This commit is contained in:
parent
1b35862f1a
commit
63f3f494c2
|
|
@ -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
|
||||
}
|
||||
Reference in New Issue