138 lines
6.3 KiB
Go
138 lines
6.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/appconfig"
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/llm"
|
|
"gitea.maximumdirect.net/eric/audita/v4/internal/prompts"
|
|
"go.maximumdirect.net/ckconfig"
|
|
"go.maximumdirect.net/ckprompt"
|
|
commandkit "go.maximumdirect.net/commandkit"
|
|
ck "go.maximumdirect.net/configkit"
|
|
pk "go.maximumdirect.net/promptkit"
|
|
"go.maximumdirect.net/releasekit/buildinfo"
|
|
)
|
|
|
|
type invocationState struct {
|
|
engines []engineEntry
|
|
client *http.Client
|
|
}
|
|
type engineEntry struct {
|
|
selection ckconfig.Selection
|
|
input acquisitionInput
|
|
engine *pk.Engine
|
|
}
|
|
|
|
type acquisitionInput struct {
|
|
GlossaryFiles []string
|
|
}
|
|
|
|
func acquireApplication(ctx context.Context, selection ckconfig.Selection, input acquisitionInput) (*appconfig.Acquisition, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
var source ck.Source
|
|
if selection.Config.Set {
|
|
if strings.TrimSpace(selection.Config.Value) == "" {
|
|
return nil, fmt.Errorf("--config requires a nonempty path")
|
|
}
|
|
source = ck.File(selection.Config.Value)
|
|
} else {
|
|
source = ck.SearchConfig("audita", ck.Searches(ck.WorkingDirectory(), ck.UserConfigDirectory(), ck.UnixSystemDirectories()))
|
|
}
|
|
loaded, report, err := appconfig.Load(ctx, ck.Input{Source: source}, appconfig.Options{Overlay: selection.Overlay.Value, Overrides: selection.Overrides, GlossaryFiles: input.GlossaryFiles})
|
|
return loaded, reportBootstrap(ctx, report, err)
|
|
}
|
|
|
|
func loadApplication(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, _ *invocationState) (*ck.Result[appconfig.Config], error) {
|
|
loaded, err := acquireApplication(ctx, selection, input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return loaded.Result, nil
|
|
}
|
|
|
|
func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, state *invocationState) (*pk.Engine, error) {
|
|
loaded, err := loadApplication(ctx, selection, input, state)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var client *http.Client
|
|
if state != nil {
|
|
client = state.client
|
|
}
|
|
return llm.NewEngine(ctx, loaded, client)
|
|
}
|
|
|
|
func capturePromptEngine(ctx context.Context, selection ckconfig.Selection, input acquisitionInput, state *invocationState) (*pk.Engine, error) {
|
|
if state != nil {
|
|
for _, entry := range state.engines {
|
|
if reflect.DeepEqual(entry.selection, selection) && reflect.DeepEqual(entry.input, input) {
|
|
return entry.engine, nil
|
|
}
|
|
}
|
|
}
|
|
engine, err := loadPromptEngine(ctx, selection, input, state)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if state != nil {
|
|
state.engines = append(state.engines, engineEntry{selection: selection, input: acquisitionInput{GlossaryFiles: append([]string(nil), input.GlossaryFiles...)}, engine: engine})
|
|
}
|
|
return engine, nil
|
|
}
|
|
|
|
func shareComparisonGlossaries(scope ckconfig.Scope[acquisitionInput, *invocationState]) ckconfig.Scope[acquisitionInput, *invocationState] {
|
|
compare := scope.Compare
|
|
scope.Compare = func(ctx context.Context, request ckconfig.ComparisonRequest[acquisitionInput], state *invocationState) (commandkit.Acquired[[]ck.Difference], error) {
|
|
request.Right.Input.GlossaryFiles = append([]string(nil), request.Left.Input.GlossaryFiles...)
|
|
return compare(ctx, request, state)
|
|
}
|
|
return scope
|
|
}
|
|
|
|
func buildApplication(extra ...commandkit.Declaration) (*commandkit.App, error) {
|
|
return buildApplicationWithClient(nil, extra...)
|
|
}
|
|
|
|
func buildApplicationWithClient(client *http.Client, extra ...commandkit.Declaration) (*commandkit.App, error) {
|
|
selector := commandkit.RepeatFlag(commandkit.FlagMeta{Name: "glossary-file", Usage: "Runtime glossary file (repeatable)."}, nil, func(s string) (string, error) { return s, nil }, func(in *acquisitionInput, value commandkit.Supplied[[]string]) { in.GlossaryFiles = value.Value })
|
|
appScope := ckconfig.FromConfigKit("app", "Audita application settings.", loadApplication)
|
|
appScope.Features.Overrides = true
|
|
appScope = shareComparisonGlossaries(withBootstrapContext(appScope))
|
|
promptScope := shareComparisonGlossaries(withBootstrapContext(ckprompt.ConfigScope(loadPromptEngine)))
|
|
configCommand := ckconfig.Command(ckconfig.Suite[acquisitionInput, *invocationState]{NewState: func() *invocationState { return &invocationState{client: client} }, Selectors: []ckconfig.Selector[acquisitionInput]{{Binding: selector, Scopes: []string{"app", "promptkit"}}}, Scopes: []ckconfig.Scope[acquisitionInput, *invocationState]{appScope, promptScope}})
|
|
definitions := ckprompt.WithNamespaces(ckprompt.FromEngine(capturePromptEngine), pk.Namespaces{})
|
|
definitions.NewState = func() *invocationState { return &invocationState{client: client} }
|
|
definitions.Selectors = []commandkit.Flag[acquisitionInput]{selector}
|
|
definitions = ckprompt.WithRequestValidation(definitions, capturePromptEngine, func(_ context.Context, seed ckprompt.ValidationRequest, _ acquisitionInput, _ *invocationState) (pk.RunRequest, error) {
|
|
return applicationPromptRequest(seed.Request, seed.Namespaces), nil
|
|
})
|
|
definitions = ckprompt.WithRequestPreparation(definitions, capturePromptEngine, func(_ context.Context, seed ckprompt.PreparationRequest, _ acquisitionInput, _ *invocationState) (pk.RunRequest, error) {
|
|
return applicationPromptRequest(seed.Request, seed.Namespaces), nil
|
|
})
|
|
definitions = ckprompt.WithSmokeTesting(definitions, capturePromptEngine, nil)
|
|
definitions = withPromptBootstrapContext(definitions)
|
|
declarations := []commandkit.Declaration{configCommand, commandkit.Completion("audita"), commandkit.WithInvocationFeature(processCommand(), bootstrapFeature()), commandkit.WithInvocationFeature(preflightCommand(), bootstrapFeature()), commandkit.Alias("run", "process")}
|
|
declarations = append(declarations, ckprompt.Declarations(definitions)...)
|
|
declarations = append(declarations, extra...)
|
|
return commandkit.New(commandkit.Identity{Name: "audita", Version: buildinfo.Version()}, declarations...)
|
|
}
|
|
|
|
// applicationPromptRequest supplies instructions only for Audita's maintained prompts.
|
|
func applicationPromptRequest(request pk.RunRequest, namespaces *pk.Namespaces) pk.RunRequest {
|
|
id := request.PromptID
|
|
if !strings.Contains(id, "/") && namespaces != nil && namespaces.Prompt != "" {
|
|
id = namespaces.Prompt + "/" + id
|
|
}
|
|
if _, ok := prompts.LookupMetadata(id); ok {
|
|
request.Vars = prompts.WithSharedVariables(request.Vars)
|
|
}
|
|
return request
|
|
}
|