Discover selected pipeline model targets
This commit is contained in:
156
internal/preflight/targets.go
Normal file
156
internal/preflight/targets.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package preflight
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// BindingScope identifies selected work associated with a native target.
|
||||
type BindingScope struct {
|
||||
Step string `json:"step,omitempty" yaml:"step,omitempty"`
|
||||
Lane string `json:"lane,omitempty" yaml:"lane,omitempty"`
|
||||
Role pipeline.ModuleStage `json:"role" yaml:"role"`
|
||||
Module string `json:"module" yaml:"module"`
|
||||
Validator string `json:"validator,omitempty" yaml:"validator,omitempty"`
|
||||
}
|
||||
|
||||
type ProfileTarget struct {
|
||||
ID string
|
||||
Bindings []BindingScope
|
||||
}
|
||||
|
||||
type AccessTarget struct {
|
||||
BackendID string
|
||||
ProfileID string
|
||||
Bindings []BindingScope
|
||||
}
|
||||
|
||||
type Targets struct {
|
||||
Profiles []ProfileTarget
|
||||
Access []AccessTarget
|
||||
}
|
||||
|
||||
// DiscoverTargets identifies initial request targets without invoking providers.
|
||||
func DiscoverTargets(ctx context.Context, resolved pipeline.ResolvedPipeline, catalog pipeline.ModuleCatalog, engine *promptkit.Engine) (Targets, error) {
|
||||
var targets Targets
|
||||
if ctx == nil {
|
||||
return targets, errors.New("preflight context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return targets, err
|
||||
}
|
||||
profiles := make(map[string]int)
|
||||
access := make(map[string]int)
|
||||
add := func(scope BindingScope, class contracts.ExecutionClass, profile string, prompts []string) error {
|
||||
if class != contracts.ExecutionClassLLMBacked {
|
||||
return nil
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if engine == nil {
|
||||
return fmt.Errorf("%v: %w: native engine is required", scope, promptkit.ErrInvalidConfig)
|
||||
}
|
||||
ids := prompts
|
||||
if len(ids) == 0 {
|
||||
ids = []string{""}
|
||||
}
|
||||
for _, promptID := range ids {
|
||||
selected := profile
|
||||
if selected == "" && promptID != "" {
|
||||
prompt, err := engine.InspectPrompt(ctx, promptID)
|
||||
if err != nil {
|
||||
return resourceFinding(fmt.Errorf("%v prompt %q: %w", scope, promptID, err))
|
||||
}
|
||||
if prompt.DefaultProfileID != nil {
|
||||
selected = *prompt.DefaultProfileID
|
||||
}
|
||||
}
|
||||
if selected == "" {
|
||||
return fmt.Errorf("%v: no statically selected profile for initial prompt %q", scope, promptID)
|
||||
}
|
||||
p, err := engine.InspectProfile(ctx, selected)
|
||||
if err != nil {
|
||||
return resourceFinding(fmt.Errorf("%v profile %q: %w", scope, selected, err))
|
||||
}
|
||||
pi, ok := profiles[selected]
|
||||
if !ok {
|
||||
pi = len(targets.Profiles)
|
||||
profiles[selected] = pi
|
||||
targets.Profiles = append(targets.Profiles, ProfileTarget{ID: selected})
|
||||
}
|
||||
targets.Profiles[pi].Bindings = appendScope(targets.Profiles[pi].Bindings, scope)
|
||||
key := "profile:" + selected
|
||||
backend := ""
|
||||
if p.Backend != nil && *p.Backend != "" {
|
||||
backend = *p.Backend
|
||||
key = "backend:" + backend
|
||||
}
|
||||
ai, ok := access[key]
|
||||
if !ok {
|
||||
ai = len(targets.Access)
|
||||
access[key] = ai
|
||||
targets.Access = append(targets.Access, AccessTarget{BackendID: backend, ProfileID: selected})
|
||||
}
|
||||
targets.Access[ai].Bindings = appendScope(targets.Access[ai].Bindings, scope)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
input, _ := catalog.Inputs.Spec(resolved.Input.Module)
|
||||
if err := add(BindingScope{Role: pipeline.StageInput, Module: resolved.Input.Module}, resolved.InputExecutionClass, resolved.Input.LLMProfile, input.InitialPrompts); err != nil {
|
||||
return targets, err
|
||||
}
|
||||
chunk, _ := catalog.Chunkers.Spec(resolved.Chunk.Module)
|
||||
if err := add(BindingScope{Role: pipeline.StageChunk, Module: resolved.Chunk.Module}, resolved.ChunkExecutionClass, resolved.Chunk.LLMProfile, chunk.InitialPrompts); err != nil {
|
||||
return targets, err
|
||||
}
|
||||
steps := make(map[string]string)
|
||||
for _, step := range resolved.Steps {
|
||||
for _, lane := range step.ArtifactLanes {
|
||||
steps[lane.ID] = step.ID
|
||||
extract, _ := catalog.Extractors.Spec(lane.Extract.Module)
|
||||
merge, _ := catalog.Mergers.Spec(lane.Merge.Module)
|
||||
normalize, _ := catalog.Normalizers.Spec(lane.Normalize.Module)
|
||||
for _, item := range []struct {
|
||||
role pipeline.ModuleStage
|
||||
binding pipeline.ModuleBinding
|
||||
class contracts.ExecutionClass
|
||||
prompts []string
|
||||
}{
|
||||
{pipeline.StageExtract, lane.Extract, lane.ExtractExecutionClass, extract.InitialPrompts},
|
||||
{pipeline.StageMerge, lane.Merge, lane.MergeExecutionClass, merge.InitialPrompts},
|
||||
{pipeline.StageNormalize, lane.Normalize, lane.NormalizeExecutionClass, normalize.InitialPrompts},
|
||||
} {
|
||||
if err := add(BindingScope{Step: step.ID, Lane: lane.ID, Role: item.role, Module: item.binding.Module}, item.class, item.binding.LLMProfile, item.prompts); err != nil {
|
||||
return targets, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, chain := range resolved.ValidatorChains {
|
||||
for _, validator := range chain.Validators {
|
||||
if err := add(BindingScope{Step: steps[chain.LaneID], Lane: chain.LaneID, Role: chain.Stage, Module: chain.ModuleKey, Validator: validator.Binding.Module}, validator.ExecutionClass, validator.Binding.LLMProfile, validator.Prompts); err != nil {
|
||||
return targets, err
|
||||
}
|
||||
}
|
||||
}
|
||||
output, _ := catalog.Outputs.Spec(resolved.Output.Module)
|
||||
if err := add(BindingScope{Role: pipeline.StageOutput, Module: resolved.Output.Module}, resolved.OutputExecutionClass, resolved.Output.LLMProfile, output.InitialPrompts); err != nil {
|
||||
return targets, err
|
||||
}
|
||||
return targets, nil
|
||||
}
|
||||
|
||||
func appendScope(scopes []BindingScope, scope BindingScope) []BindingScope {
|
||||
for _, existing := range scopes {
|
||||
if existing == scope {
|
||||
return scopes
|
||||
}
|
||||
}
|
||||
return append(scopes, scope)
|
||||
}
|
||||
Reference in New Issue
Block a user