Files
audita/examples/library-adoption/main.go

90 lines
2.8 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
ck "gitea.maximumdirect.net/eric/ConfigKit"
)
func main() {
// Only the synthetic offline demonstration sets this value. Consumers resolve
// their configured credential references through ConfigKit.
if err := os.Setenv("AUDITA_PROTOTYPE_KEY", "synthetic-offline-key"); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := run(os.Args[1:], os.Stdout, demonstrationClient()); err != nil {
if errors.Is(err, flag.ErrHelp) {
return
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(args []string, output io.Writer, client *http.Client) error {
flags := flag.NewFlagSet("library-adoption", flag.ContinueOnError)
flags.SetOutput(os.Stderr)
overlay := flags.String("overlay", "", "ConfigKit overlay")
proposal := flags.String("proposal-profile", "", "proposal profile")
validation := flags.String("validation-profile", "", "validation profile")
if err := flags.Parse(args); err != nil {
return err
}
if flags.NArg() != 0 {
return fmt.Errorf("unexpected positional arguments: %v", flags.Args())
}
var overrides []ck.Override
flags.Visit(func(f *flag.Flag) {
switch f.Name {
case "proposal-profile":
overrides = append(overrides, ck.Override{Path: ck.Keys("llm", "proposal_profile"), Value: *proposal, Flag: "--proposal-profile"})
case "validation-profile":
overrides = append(overrides, ck.Override{Path: ck.Keys("llm", "validation_profile"), Value: *validation, Flag: "--validation-profile"})
}
})
loaded, err := loadConfig(ck.File("config.yaml"), *overlay, overrides...)
if err != nil {
return err
}
runtime, err := newRuntime(loaded.Config, ".", client)
if err != nil {
return err
}
recorder := &Recorder{}
result, err := runtime.workflow(context.Background(), recorder.observer)
if err != nil {
return fmt.Errorf("offline workflow failed: %w", err)
}
attempts, retries, repairs := recorder.totals()
type selection struct {
Profile string `json:"profile"`
Backend string `json:"backend"`
Model string `json:"model"`
}
selected := func(role string) selection {
p := runtime.Profiles[role]
return selection{p.ProfileID, p.EffectiveModelParams.BackendID, p.EffectiveModelParams.Model}
}
summary := struct {
Overlay string `json:"overlay"`
Proposal selection `json:"proposal"`
Assessment selection `json:"assessment"`
Result WorkflowResult `json:"result"`
Attempts uint64 `json:"attempts"`
Retries uint64 `json:"transient_retries"`
Repairs uint64 `json:"structural_repairs"`
}{*overlay, selected("proposal"), selected("assessment"), result, attempts, retries, repairs}
if err := json.NewEncoder(output).Encode(summary); err != nil {
return err
}
return nil
}