Select execution observers from context
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -11,6 +12,66 @@ import (
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestContextualObserversSelectIndependentRuns(t *testing.T) {
|
||||
var order []string
|
||||
var events []promptkit.ExecutionEvent
|
||||
observer := func(name string) promptkit.ExecutionObserver {
|
||||
return func(event promptkit.ExecutionEvent) {
|
||||
order = append(order, name)
|
||||
if name == "inherited" {
|
||||
events = append(events, event)
|
||||
}
|
||||
}
|
||||
}
|
||||
base := context.Background()
|
||||
if promptkit.WithExecutionObserver(base, nil) != base {
|
||||
t.Fatal("nil observer changed context")
|
||||
}
|
||||
ctx := promptkit.WithExecutionObserver(base, observer("inherited"))
|
||||
ctx = promptkit.WithExecutionObserver(ctx, observer("new"))
|
||||
ctx = promptkit.WithExecutionObserver(ctx, nil)
|
||||
child := observationEngine(t, observationClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
return &promptkit.GenerateResponse{Content: "child"}, nil
|
||||
}))
|
||||
var childResult *promptkit.RunResult
|
||||
outer := observationEngine(t, observationClientFunc(func(callCtx context.Context, _ promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
var err error
|
||||
childResult, err = child.Run(callCtx, promptkit.RunRequest{PromptID: "prompt", Observer: observer("child explicit")})
|
||||
return &promptkit.GenerateResponse{Content: "outer"}, err
|
||||
}))
|
||||
result, err := outer.Run(ctx, promptkit.RunRequest{PromptID: "prompt", Observer: observer("outer explicit")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"outer explicit", "inherited", "new", "child explicit", "inherited", "new", "child explicit", "inherited", "new", "child explicit", "inherited", "new", "outer explicit", "inherited", "new", "outer explicit", "inherited", "new"}
|
||||
if !reflect.DeepEqual(order, want) {
|
||||
t.Fatalf("observer order = %v", order)
|
||||
}
|
||||
if childResult.RunID == "" || result.RunID == "" || childResult.RunID == result.RunID {
|
||||
t.Fatalf("run IDs: outer=%q child=%q", result.RunID, childResult.RunID)
|
||||
}
|
||||
counts := map[string]int{}
|
||||
for _, event := range events {
|
||||
counts[event.RunID]++
|
||||
}
|
||||
if counts[result.RunID] != 3 || counts[childResult.RunID] != 3 || len(counts) != 2 {
|
||||
t.Fatalf("events = %+v", events)
|
||||
}
|
||||
order = nil
|
||||
if _, err := child.Run(base, promptkit.RunRequest{PromptID: "prompt"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(order) != 0 {
|
||||
t.Fatalf("unrelated context selected observer: %v", order)
|
||||
}
|
||||
if _, err := child.Run(ctx, promptkit.RunRequest{PromptID: "prompt"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(order, []string{"inherited", "new", "inherited", "new", "inherited", "new"}) {
|
||||
t.Fatalf("context-only observer order = %v", order)
|
||||
}
|
||||
}
|
||||
|
||||
type observationClientFunc func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error)
|
||||
|
||||
func (f observationClientFunc) Generate(ctx context.Context, req promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user