Files
notarius/internal/core/config/loaded_config.go

277 lines
8.5 KiB
Go

package config
import (
"fmt"
"io"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
configkit "go.maximumdirect.net/configkit"
)
// LoadedConfig keeps the finalized runtime configuration together with the
// private ConfigKit inspection snapshot that produced it.
type LoadedConfig struct {
Config Config
result *configkit.Result[Document]
}
// Result returns the original application-owned load for native library handoff.
// Its typed values and resource contexts must not be reconstructed from rendered YAML.
func (loaded *LoadedConfig) Result() *configkit.Result[Document] {
if loaded == nil {
return nil
}
return loaded.result
}
// RuntimeConfig returns an independently owned runtime configuration.
func (loaded *LoadedConfig) RuntimeConfig() Config {
if loaded == nil {
return Config{}
}
return cloneConfig(loaded.Config)
}
// WriteEffective renders ConfigKit's frozen, redacted effective document.
func (loaded *LoadedConfig) WriteEffective(output io.Writer) error {
if loaded == nil || loaded.result == nil {
return fmt.Errorf("loaded configuration is unavailable")
}
return loaded.result.WriteEffective(output)
}
// WriteExplanation renders ConfigKit's safe explanation for a JSON-pointer
// path without exposing ConfigKit types at this package boundary.
func (loaded *LoadedConfig) WriteExplanation(output io.Writer, pointer string) error {
if loaded == nil || loaded.result == nil {
return fmt.Errorf("loaded configuration is unavailable")
}
path, err := configkit.ParsePath(pointer)
if err != nil {
return fmt.Errorf("parse configuration explanation path: %w", err)
}
return loaded.result.WriteExplanation(output, path)
}
func loadConfig(path string, options LoadOptions) (*LoadedConfig, error) {
result, err := loadDocument(path, options)
if err != nil {
return nil, err
}
return FromResult(result)
}
// Load reads a version 6 configuration file into runtime configuration and a
// private inspection snapshot.
func Load(path string, options LoadOptions) (*LoadedConfig, error) {
return loadConfig(path, options)
}
// LoadSource loads a selected source, including its imports, into an owned configuration.
func LoadSource(source configkit.Source, options LoadOptions) (*LoadedConfig, error) {
result, err := loadDocumentWithSource(source, options, true)
if err != nil {
return nil, err
}
return FromResult(result)
}
// MainSourcePath returns the selected main file's recorded path, if any.
func (loaded *LoadedConfig) MainSourcePath() string {
if loaded == nil || loaded.result == nil {
return ""
}
for _, source := range loaded.result.Sources() {
if source.Layer == configkit.LayerMain && source.Kind == configkit.SourceKindFile && source.ImportedFrom == nil {
return source.Path
}
}
return ""
}
func loadConfigSource(source configkit.Source, options LoadOptions) (*LoadedConfig, error) {
result, err := loadDocumentSource(source, options)
if err != nil {
return nil, err
}
return FromResult(result)
}
// FromResult derives runtime pipeline settings without loading or serializing
// the application result again.
func FromResult(result *configkit.Result[Document]) (*LoadedConfig, error) {
if result == nil {
return nil, fmt.Errorf("configuration result is unavailable")
}
runtime, err := documentToConfig(result.Config)
if err != nil {
return nil, err
}
return &LoadedConfig{Config: runtime, result: result}, nil
}
func documentToConfig(document Document) (Config, error) {
config := Default()
config.Pipelines = documentPipelinesToRuntime(document.Pipelines)
if document.Concurrency != nil && document.Concurrency.Workers != nil {
config.Concurrency.Workers = *document.Concurrency.Workers
}
if document.Output != nil && document.Output.Directory != nil {
config.Output.Directory = *document.Output.Directory
}
if document.CacheDirectory != nil {
config.CacheDirectory = *document.CacheDirectory
}
if document.Debug != nil && document.Debug.Directory != nil {
config.Debug.Directory = *document.Debug.Directory
}
return config, nil
}
func documentPipelinesToRuntime(documents map[string]PipelineDocument) map[string]pipeline.PipelineProfile {
if documents == nil {
return nil
}
profiles := make(map[string]pipeline.PipelineProfile, len(documents))
for id, document := range documents {
profiles[id] = documentPipelineToRuntime(id, document)
}
return profiles
}
func documentPipelineToRuntime(id string, document PipelineDocument) pipeline.PipelineProfile {
profile := pipeline.PipelineProfile{
Mode: cloneValue(document.Mode),
MaxAdditionalTurns: cloneValue(document.MaxAdditionalTurns),
Cache: cloneValue(document.Cache),
ID: id,
Input: documentBindingToRuntime(document.Input),
Artifacts: documentLanesToRuntime(document.Artifacts),
References: documentReferencesToRuntime(document.References),
}
if document.LLMProfile != nil {
profile.LLMProfile = *document.LLMProfile
}
if document.Chunk != nil {
profile.Chunk = documentBindingToRuntime(*document.Chunk)
}
if document.Output != nil {
profile.Output = documentBindingToRuntime(*document.Output)
}
if document.Steps != nil {
profile.Steps = make([]pipeline.PipelineStepProfile, len(document.Steps))
for index, step := range document.Steps {
profile.Steps[index] = pipeline.PipelineStepProfile{
ID: step.ID,
Artifacts: documentLanesToRuntime(step.Artifacts),
References: documentReferencesToRuntime(step.References),
}
}
}
return profile
}
func documentLanesToRuntime(documents map[string]ArtifactLaneDocument) map[string]pipeline.ArtifactLaneProfile {
if documents == nil {
return nil
}
lanes := make(map[string]pipeline.ArtifactLaneProfile, len(documents))
for id, document := range documents {
lane := pipeline.ArtifactLaneProfile{
Extract: documentBindingToRuntime(document.Extract),
References: documentReferencesToRuntime(document.References),
}
if document.Merge != nil {
lane.Merge = documentBindingToRuntime(*document.Merge)
}
if document.Normalize != nil {
lane.Normalize = documentBindingToRuntime(*document.Normalize)
}
if document.Validators != nil {
lane.Validators = make([]pipeline.ModuleBinding, len(document.Validators))
for index, validator := range document.Validators {
lane.Validators[index] = documentValidatorToRuntime(validator)
}
}
lanes[id] = lane
}
return lanes
}
func documentBindingToRuntime(document BindingDocument) pipeline.ModuleBinding {
binding := pipeline.ModuleBinding{
Mode: cloneValue(document.Mode),
MaxAdditionalTurns: cloneValue(document.MaxAdditionalTurns),
Cache: cloneValue(document.Cache),
Module: document.Module,
Options: documentOptions(document.Options),
References: documentReferencesToRuntime(document.References),
}
if document.LLMProfile != nil {
binding.LLMProfile = *document.LLMProfile
}
if document.Validators != nil {
binding.Validators.Set = true
binding.Validators.Validators = make([]pipeline.ModuleBinding, len(document.Validators))
for index, validator := range document.Validators {
binding.Validators.Validators[index] = documentValidatorToRuntime(validator)
}
}
return binding
}
func documentValidatorToRuntime(document ValidatorDocument) pipeline.ModuleBinding {
binding := pipeline.ModuleBinding{
Module: document.Module,
Options: documentOptions(document.Options),
}
if document.LLMProfile != nil {
binding.LLMProfile = *document.LLMProfile
}
return binding
}
func documentReferencesToRuntime(documents map[string]ReferenceDocument) map[string]pipeline.ReferenceSource {
if documents == nil {
return nil
}
references := make(map[string]pipeline.ReferenceSource, len(documents))
for name, document := range documents {
if document.Artifact != nil {
references[name] = pipeline.GeneratedReference(document.Artifact.Step, document.Artifact.Lane)
continue
}
if document.Path != nil {
references[name] = pipeline.ExternalReference(*document.Path)
}
}
return references
}
func documentOptions(values map[string]any) map[string]any {
if values == nil {
return nil
}
result := make(map[string]any, len(values))
for key, value := range values {
result[key] = cloneDocumentOption(value)
}
return result
}
func cloneDocumentOption(value any) any {
switch typed := value.(type) {
case map[string]any:
return documentOptions(typed)
case []any:
result := make([]any, len(typed))
for index, item := range typed {
result[index] = cloneDocumentOption(item)
}
return result
default:
return typed
}
}