Files
notarius/internal/framework/contracts/preparation.go

159 lines
6.0 KiB
Go

package contracts
import (
"context"
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
"go.maximumdirect.net/promptkit"
)
// Preparation is invoked by core once for an operation. An ordinary preparation
// failure is returned as an error; only an explicitly supported limit is a
// PreparationLimit. Preparation does not choose execution collaborators.
type Preparation[T any] func(context.Context) (PreparedOperation[T], error)
// PreparedOperation is one of DeterministicOperation, NativeOperation, or
// PreparationLimit. Its inputs and callbacks belong to one invocation. A shared
// module must not keep mutable operation state in its fields.
//
// Preparation transfers its returned maps, slices, values, and closures to the
// operation; callers must not mutate shared input through them. Existing source
// and reference isolation boundaries still apply. Core does not promise a
// general deep copy of domain values or callback captures.
type PreparedOperation[T any] interface {
preparedOperation()
operationType(T)
}
// EvaluationView is the common information core needs to assess a result.
// Candidate may be nil when interpretation supplies actionable guidance before
// a value exists. Diagnostics and optional evidence belong to this evaluation,
// not to an earlier or later candidate. Supplying a candidate is not acceptance.
type EvaluationView[T any] struct {
Candidate *T
Guidance []string
Diagnostics []ProducerDiagnostic
// Evidence is evaluated only when operation evidence is enabled. It may
// capture typed evaluation details, which must remain fixed for its lifetime.
Evidence func() (any, error)
}
// Evaluation allows a domain facility to retain typed information alongside
// the common view without exposing that information to core.
type Evaluation[T any] interface {
View() EvaluationView[T]
}
// CandidateEvaluation is the common evaluation for ordinary integrations.
type CandidateEvaluation[T any] struct{ Result EvaluationView[T] }
func (evaluation CandidateEvaluation[T]) View() EvaluationView[T] { return evaluation.Result }
// Fallback constructs and intrinsically checks supported output. The final
// evaluation is nil when no evaluation remains eligible. Core selects when to
// invoke it; the callback must not assert ordinary validator acceptance.
type Fallback[T any] func(context.Context, Evaluation[T]) (T, error)
// TerminalDiagnostics derives terminal domain diagnostics from the same
// evaluation and disposition as the returned value. It may be nil.
type TerminalDiagnostics[T any] func(Evaluation[T], ProducerOutcome) ([]ProducerDiagnostic, error)
// DeterministicOperation supplies one candidate without a native request.
type DeterministicOperation[T any] struct {
Evaluation Evaluation[T]
Fallback Fallback[T]
Diagnostics TerminalDiagnostics[T]
Evidence func() (any, error)
}
func (DeterministicOperation[T]) preparedOperation() {}
func (DeterministicOperation[T]) operationType(T) {}
// NativeOperation describes interpretation and correction, not execution.
// Initial contains the explicit profile/session selections. Core carries them
// and its resolved native options through both correction forms.
type NativeOperation[T any] struct {
Initial promptkit.RunRequest
Interpret func(context.Context, *promptkit.RunResult) (Evaluation[T], error)
Correction CorrectionBuilder
Fallback Fallback[T]
Diagnostics TerminalDiagnostics[T]
// Reuse checks and, when needed, adapts a decoded accepted value to this
// operation's current domain inputs. An error makes the cache entry unusable.
Reuse func(T) (T, error)
// Evidence is optional, lazy preparation evidence for this operation.
Evidence func() (any, error)
}
func (NativeOperation[T]) preparedOperation() {}
func (NativeOperation[T]) operationType(T) {}
// CorrectionBuilder constructs only a native assignment; core and PromptKit
// coordinate continuation and conversation history.
type CorrectionBuilder func(context.Context, []string) (promptkit.RunRequest, error)
// PromptCorrection binds ordered guidance to an integration-selected native
// input. More specialized corrections can supply a CorrectionBuilder directly.
func PromptCorrection(promptID, inputName string, bind func([]string) promptkit.ArtifactRef) CorrectionBuilder {
return func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
return promptkit.RunRequest{PromptID: promptID, Inputs: map[string]promptkit.ArtifactRef{inputName: bind(guidance)}}, nil
}
}
// PreparationLimit represents an existing explicit limit, not an arbitrary
// preparation error. Core applies terminal policy to Cause and Fallback.
type PreparationLimit[T any] struct {
Cause error
Fallback Fallback[T]
Diagnostics TerminalDiagnostics[T]
Evidence func() (any, error)
}
func (PreparationLimit[T]) preparedOperation() {}
func (PreparationLimit[T]) operationType(T) {}
// ExtractionInput contains domain inputs only. Its source, chunk, references,
// and metadata follow the existing detached request boundaries; a preparation
// that retains mutable data must own the data it may modify.
type ExtractionInput struct {
Source *source.SourceDocument
Chunk *source.Chunk
AmbientContext map[string]any
SourceInput LLMInputMaterial
SessionID string
References ReferenceSet
LLMProfile string
Metadata map[string]any
}
type MergeInput[T any] struct {
Source *source.SourceDocument
LaneID string
ExtractOutputs []ExtractArtifact[T]
SourceInput LLMInputMaterial
SessionID string
References ReferenceSet
LLMProfile string
Metadata map[string]any
}
type NormalizeInput[T any] struct {
Source *source.SourceDocument
LaneID string
MergeOutput MergeArtifact[T]
SourceInput LLMInputMaterial
SessionID string
References ReferenceSet
LLMProfile string
Metadata map[string]any
}
type ChunkInput struct {
Source *source.SourceDocument
SourceInput LLMInputMaterial
SessionID string
References ReferenceSet
LLMProfile string
Metadata map[string]any
}