Initial commit: Go agentic harness extracted from Felix
This commit is contained in:
commit
1f2c471ff9
89 files changed
+19388
No files matched your search
@@ -0,0 +1,80 @@
|
||||
// Package llmtest provides shared test helpers for LLMProvider stubs.
|
||||
//
|
||||
// Two pieces:
|
||||
// - Base: an embeddable struct that supplies default no-op
|
||||
// implementations of every LLMProvider method except ChatStream.
|
||||
// Test stubs that need custom ChatStream behavior should embed Base
|
||||
// so the LLMProvider interface can grow without churning every stub.
|
||||
// - Stub: a fully-configurable LLMProvider for the common case
|
||||
// (canned text response, optional delay, observable requests).
|
||||
package llmtest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sausheong/harness/llm"
|
||||
)
|
||||
|
||||
// Base provides default implementations of every LLMProvider method
|
||||
// except ChatStream. Embed this in test stubs to avoid having to update
|
||||
// every stub when the interface widens.
|
||||
type Base struct{}
|
||||
|
||||
// Models returns an empty slice (non-nil so callers can range safely).
|
||||
func (Base) Models() []llm.ModelInfo { return []llm.ModelInfo{} }
|
||||
|
||||
// NormalizeToolSchema is identity by default — no fields stripped, no
|
||||
// diagnostics emitted. Per-provider implementations strip fields their
|
||||
// API rejects.
|
||||
func (Base) NormalizeToolSchema(tools []llm.ToolDef) ([]llm.ToolDef, []llm.Diagnostic) {
|
||||
return tools, nil
|
||||
}
|
||||
|
||||
// Stub is a configurable LLMProvider for tests.
|
||||
type Stub struct {
|
||||
Base
|
||||
|
||||
// Text is the canned text response emitted on every ChatStream call.
|
||||
Text string
|
||||
// Delay sleeps before emitting the response. Zero means immediate.
|
||||
Delay time.Duration
|
||||
// Started is closed (once) just before the response is delayed/emitted.
|
||||
// Useful for synchronizing concurrent tests.
|
||||
Started chan struct{}
|
||||
// ChatErr, if non-nil, is returned synchronously from ChatStream.
|
||||
ChatErr error
|
||||
// ChatHook, if non-nil, observes every ChatStream request before
|
||||
// emission. Hook executes synchronously inside ChatStream.
|
||||
ChatHook func(req llm.ChatRequest)
|
||||
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (s *Stub) ChatStream(ctx context.Context, req llm.ChatRequest) (<-chan llm.ChatEvent, error) {
|
||||
if s.ChatHook != nil {
|
||||
s.ChatHook(req)
|
||||
}
|
||||
if s.ChatErr != nil {
|
||||
return nil, s.ChatErr
|
||||
}
|
||||
if s.Started != nil {
|
||||
s.once.Do(func() { close(s.Started) })
|
||||
}
|
||||
ch := make(chan llm.ChatEvent, 4)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
if s.Delay > 0 {
|
||||
select {
|
||||
case <-time.After(s.Delay):
|
||||
case <-ctx.Done():
|
||||
ch <- llm.ChatEvent{Type: llm.EventError, Error: ctx.Err()}
|
||||
return
|
||||
}
|
||||
}
|
||||
ch <- llm.ChatEvent{Type: llm.EventTextDelta, Text: s.Text}
|
||||
ch <- llm.ChatEvent{Type: llm.EventDone}
|
||||
}()
|
||||
return ch, nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package llmtest_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/sausheong/harness/llm"
|
||||
"github.com/sausheong/harness/llm/llmtest"
|
||||
)
|
||||
|
||||
func TestBaseDefaults(t *testing.T) {
|
||||
type stub struct {
|
||||
llmtest.Base
|
||||
}
|
||||
s := &stub{}
|
||||
// Models must be non-nil-empty (callers may range over it).
|
||||
assert.NotNil(t, s.Models())
|
||||
}
|
||||
|
||||
func TestStubCannedText(t *testing.T) {
|
||||
s := &llmtest.Stub{Text: "hello"}
|
||||
ch, err := s.ChatStream(context.Background(), llm.ChatRequest{})
|
||||
require.NoError(t, err)
|
||||
var got string
|
||||
for ev := range ch {
|
||||
if ev.Type == llm.EventTextDelta {
|
||||
got += ev.Text
|
||||
}
|
||||
}
|
||||
assert.Equal(t, "hello", got)
|
||||
}
|
||||
|
||||
func TestStubChatHookObservesRequests(t *testing.T) {
|
||||
var seen []llm.ChatRequest
|
||||
s := &llmtest.Stub{
|
||||
Text: "ok",
|
||||
ChatHook: func(req llm.ChatRequest) { seen = append(seen, req) },
|
||||
}
|
||||
_, _ = s.ChatStream(context.Background(), llm.ChatRequest{Model: "m1"})
|
||||
_, _ = s.ChatStream(context.Background(), llm.ChatRequest{Model: "m2"})
|
||||
require.Len(t, seen, 2)
|
||||
assert.Equal(t, "m1", seen[0].Model)
|
||||
assert.Equal(t, "m2", seen[1].Model)
|
||||
}
|
||||
|
||||
func TestStubChatErrShortCircuits(t *testing.T) {
|
||||
s := &llmtest.Stub{ChatErr: assert.AnError}
|
||||
_, err := s.ChatStream(context.Background(), llm.ChatRequest{})
|
||||
assert.Equal(t, assert.AnError, err)
|
||||
}
|
||||
Reference in new issue
Block a user