package pipeline import ( "encoding/json" "fmt" "strings" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) const ( DefaultChunkModule = "generic" DefaultMergeModule = "appendorder" DefaultNormalizeModule = "noop" DefaultOutputModule = "json" DefaultLLMProfile = "default" ) type ModuleBinding struct { Mode *contracts.ExecutionMode `json:"mode,omitempty"` MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"` Cache *contracts.CacheMode `json:"cache,omitempty"` Module string `json:"module"` LLMProfile string `json:"llm_profile,omitempty"` Options map[string]any `json:"options,omitempty"` References map[string]ReferenceSource `json:"references,omitempty"` Validators ValidatorOverride `json:"validators,omitempty"` } // ArtifactReference identifies a normalized artifact produced by an earlier // ordered step. It is a selector only; it does not contain artifact bytes. type ArtifactReference struct { Step string `json:"step"` Lane string `json:"lane"` } // ReferenceSource is the discriminated source form used by configured // reference maps. Exactly one of Path and Artifact is set after resolution. type ReferenceSource struct { Path string `json:"path,omitempty"` Artifact *ArtifactReference `json:"artifact,omitempty"` } func ExternalReference(path string) ReferenceSource { return ReferenceSource{Path: strings.TrimSpace(path)} } func ExternalReferenceMap(values map[string]string) map[string]ReferenceSource { out := make(map[string]ReferenceSource, len(values)) for key, value := range values { out[key] = ExternalReference(value) } return out } func GeneratedReference(step, lane string) ReferenceSource { return ReferenceSource{Artifact: &ArtifactReference{Step: strings.TrimSpace(step), Lane: strings.TrimSpace(lane)}} } func (source ReferenceSource) IsGenerated() bool { return source.Artifact != nil } func (source ReferenceSource) MarshalJSON() ([]byte, error) { if source.Path != "" && source.Artifact != nil { return nil, fmt.Errorf("reference source must not contain both an external path and an artifact selector") } if source.Artifact != nil { return json.Marshal(struct { Artifact *ArtifactReference `json:"artifact"` }{Artifact: source.Artifact}) } return json.Marshal(source.Path) } type ValidatorOverride struct { Set bool `json:"set,omitempty"` Validators []ModuleBinding `json:"validators,omitempty"` } func (binding ModuleBinding) MarshalJSON() ([]byte, error) { type moduleBindingJSON struct { Mode *contracts.ExecutionMode `json:"mode,omitempty"` MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"` Cache *contracts.CacheMode `json:"cache,omitempty"` Module string `json:"module"` LLMProfile string `json:"llm_profile,omitempty"` Options map[string]any `json:"options,omitempty"` References map[string]ReferenceSource `json:"references,omitempty"` Validators *[]ModuleBinding `json:"validators,omitempty"` } out := moduleBindingJSON{ Module: binding.Module, Mode: cloneControl(binding.Mode), MaxAdditionalTurns: cloneControl(binding.MaxAdditionalTurns), Cache: cloneControl(binding.Cache), LLMProfile: binding.LLMProfile, Options: binding.Options, References: binding.References, } if binding.Validators.Set { validators := cloneModuleBindings(binding.Validators.Validators) out.Validators = &validators } return json.Marshal(out) } func (binding *ModuleBinding) UnmarshalJSON(data []byte) error { if binding == nil { return fmt.Errorf("module binding must not be nil") } type moduleBindingJSON struct { Mode *contracts.ExecutionMode `json:"mode,omitempty"` MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"` Cache *contracts.CacheMode `json:"cache,omitempty"` Module string `json:"module"` LLMProfile string `json:"llm_profile,omitempty"` Options map[string]any `json:"options,omitempty"` References map[string]ReferenceSource `json:"references,omitempty"` Validators json.RawMessage `json:"validators,omitempty"` } var decoded moduleBindingJSON if err := json.Unmarshal(data, &decoded); err != nil { return err } out := ModuleBinding{ Module: decoded.Module, Mode: cloneControl(decoded.Mode), MaxAdditionalTurns: cloneControl(decoded.MaxAdditionalTurns), Cache: cloneControl(decoded.Cache), LLMProfile: decoded.LLMProfile, Options: decoded.Options, References: decoded.References, } if len(decoded.Validators) > 0 && string(decoded.Validators) != "null" { var validators []ModuleBinding if err := json.Unmarshal(decoded.Validators, &validators); err != nil { return fmt.Errorf("decode module binding validators: %w", err) } out.Validators = ValidatorOverride{Set: true, Validators: validators} } *binding = CloneModuleBinding(out) normalizeBindingNames(binding) return nil } func normalizeBindingNames(binding *ModuleBinding) { binding.Module = strings.TrimSpace(binding.Module) binding.LLMProfile = strings.TrimSpace(binding.LLMProfile) for i := range binding.Validators.Validators { normalizeBindingNames(&binding.Validators.Validators[i]) } } type ArtifactLaneProfile struct { Extract ModuleBinding `json:"extract"` Merge ModuleBinding `json:"merge,omitempty"` Normalize ModuleBinding `json:"normalize,omitempty"` Validators []ModuleBinding `json:"validators,omitempty"` References map[string]ReferenceSource `json:"references,omitempty"` } type PipelineStepProfile struct { ID string `json:"id"` Artifacts map[string]ArtifactLaneProfile `json:"artifacts"` References map[string]ReferenceSource `json:"references,omitempty"` } type PipelineProfile struct { Mode *contracts.ExecutionMode `json:"mode,omitempty"` MaxAdditionalTurns *int `json:"max_additional_turns,omitempty"` Cache *contracts.CacheMode `json:"cache,omitempty"` ID string `json:"id"` LLMProfile string `json:"llm_profile,omitempty"` Input ModuleBinding `json:"input"` Chunk ModuleBinding `json:"chunk,omitempty"` Artifacts map[string]ArtifactLaneProfile `json:"artifacts"` Steps []PipelineStepProfile `json:"steps,omitempty"` Output ModuleBinding `json:"output,omitempty"` References map[string]ReferenceSource `json:"references,omitempty"` }