Execute native operations and corrections in core
This commit is contained in:
157
internal/framework/pipeline/operation_native.go
Normal file
157
internal/framework/pipeline/operation_native.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type nativeEvaluation[T any] struct {
|
||||
evaluation contracts.Evaluation[T]
|
||||
assessment contracts.CandidateAssessment
|
||||
guidance []string
|
||||
failure error
|
||||
accepted bool
|
||||
}
|
||||
|
||||
func executeNative[T any](ctx context.Context, execution ExecutionContext[T], operation contracts.NativeOperation[T]) (OperationResult[T], error) {
|
||||
var empty OperationResult[T]
|
||||
if execution.Engine == nil || execution.Assess == nil || operation.Interpret == nil {
|
||||
return empty, errors.New("native engine, interpretation, and assessment must be supplied")
|
||||
}
|
||||
if execution.Controls.MaxAdditionalTurns < 0 {
|
||||
return empty, fmt.Errorf("%w: additional turns must not be negative", promptkit.ErrInvalidRequest)
|
||||
}
|
||||
var current nativeEvaluation[T]
|
||||
var fatal error
|
||||
evaluate := func(result *promptkit.RunResult) {
|
||||
current = nativeEvaluation[T]{}
|
||||
if err := CheckNativeResult(result, nil); err != nil {
|
||||
current.failure = err
|
||||
return
|
||||
}
|
||||
evaluation, err := operation.Interpret(ctx, result)
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
fatal = canceled
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
var captureErr *contracts.CaptureError
|
||||
if errors.As(err, &captureErr) {
|
||||
fatal = err
|
||||
} else {
|
||||
current.failure = err
|
||||
}
|
||||
return
|
||||
}
|
||||
if evaluation == nil {
|
||||
current.failure = errors.New("native response has no evaluation")
|
||||
return
|
||||
}
|
||||
current.evaluation = evaluation
|
||||
view := evaluation.View()
|
||||
current.guidance = append([]string(nil), view.Guidance...)
|
||||
if view.Candidate == nil {
|
||||
current.failure = errors.New("native response could not be interpreted as a usable candidate")
|
||||
return
|
||||
}
|
||||
current.assessment, err = execution.Assess(ctx, *view.Candidate)
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
fatal = canceled
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fatal = err
|
||||
return
|
||||
}
|
||||
validatorGuidance, err := current.assessment.Guidance()
|
||||
if err != nil {
|
||||
fatal = err
|
||||
return
|
||||
}
|
||||
current.guidance = append(current.guidance, validatorGuidance...)
|
||||
current.accepted = current.assessment.Accepted() && len(current.guidance) == 0
|
||||
current.failure = current.assessment.Err()
|
||||
if !current.accepted && current.failure == nil {
|
||||
current.failure = errors.New("native candidate requires correction")
|
||||
}
|
||||
}
|
||||
|
||||
request := producerRequest(operation.Initial, execution.Execution)
|
||||
request.CaptureExchange = request.CaptureExchange || (operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0)
|
||||
result, err := execution.Engine.Run(ctx, request)
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return empty, canceled
|
||||
}
|
||||
if err != nil {
|
||||
var captureErr *contracts.CaptureError
|
||||
if errors.As(err, &captureErr) {
|
||||
return empty, err
|
||||
}
|
||||
current.failure = CheckNativeResult(result, err)
|
||||
} else {
|
||||
evaluate(result)
|
||||
}
|
||||
if fatal != nil {
|
||||
return empty, fatal
|
||||
}
|
||||
if !current.accepted && err == nil && len(current.guidance) > 0 && operation.Correction != nil && execution.Controls.MaxAdditionalTurns > 0 {
|
||||
coordination, coordinateErr := execution.Engine.Coordinate(ctx, promptkit.CoordinateRequest{
|
||||
Start: result.Exchange, AdditionalTurns: execution.Controls.MaxAdditionalTurns,
|
||||
Next: func(_ context.Context, turn promptkit.ConversationTurn) (*promptkit.RunRequest, error) {
|
||||
if turn.Result != nil {
|
||||
evaluate(turn.Result)
|
||||
}
|
||||
if fatal != nil {
|
||||
return nil, fatal
|
||||
}
|
||||
if current.accepted || len(current.guidance) == 0 || turn.RemainingTurns == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
guidance := current.guidance
|
||||
// Selecting another execution invalidates the entire previous
|
||||
// evaluation before even attempting to construct its request.
|
||||
current = nativeEvaluation[T]{}
|
||||
next, err := operation.Correction(ctx, guidance)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if operation.Initial.ProfileID != "" {
|
||||
next.ProfileID = operation.Initial.ProfileID
|
||||
}
|
||||
if operation.Initial.SessionID != "" {
|
||||
next.SessionID = operation.Initial.SessionID
|
||||
}
|
||||
next = producerRequest(next, execution.Execution)
|
||||
return &next, nil
|
||||
},
|
||||
})
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return empty, canceled
|
||||
}
|
||||
if coordination.Undelivered != nil {
|
||||
if coordinateErr == nil {
|
||||
coordinateErr = errors.New("native continuation response was not delivered")
|
||||
}
|
||||
return empty, coordinateErr
|
||||
}
|
||||
if fatal != nil {
|
||||
return empty, fatal
|
||||
}
|
||||
if coordinateErr != nil {
|
||||
var captureErr *contracts.CaptureError
|
||||
if errors.As(coordinateErr, &captureErr) {
|
||||
return empty, coordinateErr
|
||||
}
|
||||
current = nativeEvaluation[T]{failure: coordinateErr}
|
||||
}
|
||||
}
|
||||
if current.accepted {
|
||||
outcome := contracts.ProducerOutcome{Disposition: contracts.ProducerAccepted, ReuseEligible: execution.ReuseEligible}
|
||||
return completeOperation(*current.evaluation.View().Candidate, current.evaluation, current.assessment, outcome, operation.Diagnostics)
|
||||
}
|
||||
return finishOperation(ctx, execution.Controls.Mode, current.failure, current.evaluation, current.assessment, operation.Fallback, operation.Diagnostics)
|
||||
}
|
||||
Reference in New Issue
Block a user