171 lines
5.8 KiB
Go
171 lines
5.8 KiB
Go
// Package appconfig composes Audita and PromptKit configuration sources.
|
|
package appconfig
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/responseschema"
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
|
ck "go.maximumdirect.net/configkit"
|
|
pk "go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
// AssetError reports a failure to access an embedded application resource.
|
|
type AssetError struct{ Err error }
|
|
|
|
func (e *AssetError) Error() string { return e.Err.Error() }
|
|
func (e *AssetError) Unwrap() error { return e.Err }
|
|
|
|
func IsAssetError(err error) bool {
|
|
var target *AssetError
|
|
return errors.As(err, &target)
|
|
}
|
|
|
|
// Config combines Audita's root settings with native PromptKit settings.
|
|
type Config struct {
|
|
Application config.Config `yaml:",inline"`
|
|
PromptKit pk.Settings `yaml:"promptkit"`
|
|
}
|
|
|
|
type Options struct {
|
|
Overlay string
|
|
Overrides []ck.Override
|
|
GlossaryFiles []string
|
|
}
|
|
|
|
type Acquisition struct {
|
|
*ck.Result[Config]
|
|
GlossarySources []string
|
|
}
|
|
|
|
// GlossaryInputError identifies failures in the separate runtime input load.
|
|
type GlossaryInputError struct{ Err error }
|
|
|
|
func (e *GlossaryInputError) Error() string { return e.Err.Error() }
|
|
func (e *GlossaryInputError) Unwrap() error { return e.Err }
|
|
|
|
const glossaryImports = `config:
|
|
imports:
|
|
- path: glossary.yml
|
|
required: false
|
|
destination: [glossary_collections]
|
|
- dir: glossary
|
|
required: false
|
|
recursive: true
|
|
pattern: ["*.yml", "*.yaml"]
|
|
destination: [glossary_collections]
|
|
`
|
|
|
|
const operatorImports = `config:
|
|
imports:
|
|
- path: prompts.yml
|
|
required: false
|
|
- dir: prompts
|
|
required: false
|
|
recursive: true
|
|
pattern: ["*.yml", "*.yaml"]
|
|
`
|
|
|
|
// Definition combines application rules with PromptKit's native rules.
|
|
func Definition() ck.Definition[Config] {
|
|
domain := config.Definition()
|
|
definition := ck.Definition[Config]{
|
|
Defaults: func() Config { return Config{Application: domain.Defaults()} },
|
|
Version: domain.Version,
|
|
SequenceIdentities: domain.SequenceIdentities,
|
|
Sensitive: domain.Sensitive,
|
|
Resolve: func(value *Config, resolution *ck.Resolution) error {
|
|
return domain.Resolve(&value.Application, resolution)
|
|
},
|
|
Validate: func(value *Config, validation *ck.Validation) {
|
|
domain.Validate(&value.Application, validation)
|
|
},
|
|
}
|
|
return pk.Configure(definition, func(value *Config) *pk.Settings { return &value.PromptKit })
|
|
}
|
|
|
|
// Load composes built-in resources and optional companions with the selected input.
|
|
func Load(ctx context.Context, input ck.Input, options Options) (*Acquisition, *ck.SecretsReport, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
report, prepared, err := ck.LoadSecretsIntoEnvironmentFromConfig(input.Source, ck.SecretsFromConfigOptions{Filesystem: input.Filesystem})
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
return nil, report, errors.Join(err, canceled)
|
|
}
|
|
if err != nil {
|
|
return nil, report, err
|
|
}
|
|
input.Source = prepared
|
|
var glossarySources []string
|
|
overrides := append([]ck.Override(nil), options.Overrides...)
|
|
if len(options.GlossaryFiles) > 0 {
|
|
inputs := make([]ck.Input, len(options.GlossaryFiles))
|
|
for i, path := range options.GlossaryFiles {
|
|
absolute, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return nil, report, &GlossaryInputError{Err: err}
|
|
}
|
|
inputs[i] = ck.Input{Source: ck.File(absolute), Imports: true}
|
|
}
|
|
underlays := make([]ck.Underlay, 0, len(inputs)-1)
|
|
for _, item := range inputs[:len(inputs)-1] {
|
|
underlays = append(underlays, ck.Underlay{Inputs: []ck.Input{item}})
|
|
}
|
|
glossaries, err := ck.LoadLayered(inputs[len(inputs)-1], ck.LayeredOptions[map[string]any]{
|
|
Definition: ck.Definition[map[string]any]{SequenceIdentities: []ck.SequenceIdentity{config.GlossaryEntryIdentity(nil)}},
|
|
Underlays: underlays,
|
|
})
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
return nil, report, errors.Join(err, canceled)
|
|
}
|
|
if err != nil {
|
|
return nil, report, &GlossaryInputError{Err: err}
|
|
}
|
|
for _, source := range glossaries.Sources() {
|
|
if source.Kind == ck.SourceKindFile {
|
|
glossarySources = append(glossarySources, source.Path)
|
|
}
|
|
}
|
|
overrides = append(overrides, ck.Override{Path: ck.Keys("glossary_collections"), Value: glossaries.Config, Flag: "--glossary-file"})
|
|
}
|
|
embedded, err := pk.FilesystemInputs("Audita embedded prompts", prompts.Filesystem(), prompts.AssetRoot)
|
|
if err != nil {
|
|
return nil, report, &AssetError{Err: fmt.Errorf("discover embedded prompts: %w", err)}
|
|
}
|
|
for i := range embedded {
|
|
embedded[i].Destination = ck.Keys("promptkit")
|
|
}
|
|
schemas := ck.NewFilesystem("Audita embedded schemas", responseschema.Filesystem())
|
|
embedded = append(embedded, ck.Input{
|
|
Source: pk.SchemaRootSource("Audita embedded schemas", responseschema.AssetRoot),
|
|
Destination: ck.Keys("promptkit"),
|
|
Filesystem: &ck.FilesystemContext{FS: &schemas, Root: ".", BaseDir: "."},
|
|
})
|
|
input.Imports = true
|
|
result, err := ck.LoadLayered(input, ck.LayeredOptions[Config]{
|
|
Definition: Definition(),
|
|
Overlay: options.Overlay,
|
|
Overrides: overrides,
|
|
Underlays: []ck.Underlay{
|
|
pk.BundledOpenRouterUnderlay(),
|
|
{Inputs: embedded},
|
|
{Inputs: []ck.Input{
|
|
{Source: ck.Bytes("Audita glossary imports", []byte(glossaryImports)), Imports: true, MainRelative: ck.MainRelative()},
|
|
{Source: ck.Bytes("Audita automatic prompt imports", []byte(operatorImports)), Destination: ck.Keys("promptkit"), Imports: true, MainRelative: ck.MainRelative()},
|
|
}},
|
|
},
|
|
})
|
|
if canceled := ctx.Err(); canceled != nil {
|
|
return nil, report, errors.Join(err, canceled)
|
|
}
|
|
if err != nil {
|
|
return nil, report, err
|
|
}
|
|
return &Acquisition{Result: result, GlossarySources: glossarySources}, report, nil
|
|
}
|