118 lines
4.0 KiB
Go
118 lines
4.0 KiB
Go
// Package appconfig composes Audita and PromptKit configuration sources.
|
|
package appconfig
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"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"`
|
|
}
|
|
|
|
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, overlay string, overrides ...ck.Override) (*ck.Result[Config], *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
|
|
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: 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)
|
|
}
|
|
return result, report, err
|
|
}
|