Document sequence composition for consumers
This commit is contained in:
53
examples/consumers/prompt_catalog.go
Normal file
53
examples/consumers/prompt_catalog.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package consumers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
)
|
||||
|
||||
// PromptCatalogConfig is an application-owned prompt collection shape.
|
||||
type PromptCatalogConfig struct {
|
||||
Prompts []PromptRecord `yaml:"prompts"`
|
||||
}
|
||||
|
||||
// PromptRecord keeps its identity and message meaning in the consumer.
|
||||
type PromptRecord struct {
|
||||
ID string `yaml:"id"`
|
||||
Name string `yaml:"name"`
|
||||
Messages []string `yaml:"messages"`
|
||||
}
|
||||
|
||||
// LoadPromptCatalog composes explicitly registered prompt records in the main
|
||||
// tier and replaces the lower-priority default collection as a whole.
|
||||
func LoadPromptCatalog(mainPath string) (*configkit.Result[PromptCatalogConfig], error) {
|
||||
return configkit.LoadLayered(configkit.Input{
|
||||
Source: configkit.File(mainPath),
|
||||
Imports: true,
|
||||
}, configkit.LayeredOptions[PromptCatalogConfig]{
|
||||
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{
|
||||
Source: configkit.Bytes("default prompts", []byte("prompts:\n - id: default\n name: Default prompt\n messages: [Default message]\n")),
|
||||
}}}},
|
||||
Definition: configkit.Definition[PromptCatalogConfig]{
|
||||
SequenceCompositions: []configkit.SequenceComposition{{
|
||||
Path: configkit.Keys("prompts"),
|
||||
IdentityField: "id",
|
||||
}},
|
||||
Validate: validatePromptCatalog,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func validatePromptCatalog(configuration *PromptCatalogConfig, validation *configkit.Validation) {
|
||||
for index, prompt := range configuration.Prompts {
|
||||
if strings.TrimSpace(prompt.Name) == "" {
|
||||
validation.Error(configkit.Keys("prompts").Index(index).Key("name"), "must not be empty")
|
||||
}
|
||||
if len(prompt.Messages) == 0 {
|
||||
validation.Error(configkit.Keys("prompts").Index(index).Key("messages"), "must contain at least one message")
|
||||
}
|
||||
}
|
||||
if len(configuration.Prompts) == 0 {
|
||||
validation.Error(configkit.Keys("prompts"), "must contain at least one prompt")
|
||||
}
|
||||
}
|
||||
82
examples/consumers/prompt_catalog_test.go
Normal file
82
examples/consumers/prompt_catalog_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package consumers_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/examples/consumers"
|
||||
)
|
||||
|
||||
func Example_identifiedPromptCatalog() {
|
||||
result, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "config.yml"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
ids := make([]string, 0, len(result.Config.Prompts))
|
||||
for _, prompt := range result.Config.Prompts {
|
||||
ids = append(ids, prompt.ID)
|
||||
}
|
||||
fmt.Println(ids, err)
|
||||
// Output: [main curated weather welcome] <nil>
|
||||
}
|
||||
|
||||
func TestPromptCatalogImportsRegisteredItemsAndReplacesDefaults(t *testing.T) {
|
||||
result, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "config.yml"))
|
||||
if err != nil {
|
||||
var loadError *configkit.LoadError
|
||||
if errors.As(err, &loadError) {
|
||||
t.Fatalf("load prompt catalog: %v (%#v)", err, loadError.Diagnostics())
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Config.Prompts) != 4 {
|
||||
t.Fatalf("prompt records = %#v", result.Config.Prompts)
|
||||
}
|
||||
ids := []string{"main", "curated", "weather", "welcome"}
|
||||
for index, id := range ids {
|
||||
if result.Config.Prompts[index].ID != id {
|
||||
t.Fatalf("prompt %d ID = %q, want %q", index, result.Config.Prompts[index].ID, id)
|
||||
}
|
||||
}
|
||||
for _, prompt := range result.Config.Prompts {
|
||||
if prompt.ID == "default" {
|
||||
t.Fatalf("lower-priority default survived main sequence replacement: %#v", result.Config.Prompts)
|
||||
}
|
||||
}
|
||||
|
||||
var itemImports, helperImports int
|
||||
for _, source := range result.Sources() {
|
||||
switch filepath.Base(source.Path) {
|
||||
case "curated.yml", "generated-weather.yml", "generated-welcome.yml":
|
||||
if source.PlacementMode != configkit.PlacementModeSequenceItem || source.Destination.String() != "/prompts" {
|
||||
t.Fatalf("item source placement = %#v", source)
|
||||
}
|
||||
itemImports++
|
||||
case "curated-id.yml":
|
||||
if source.PlacementMode != configkit.PlacementModeMapping || source.ImportedFrom == nil {
|
||||
t.Fatalf("helper source = %#v", source)
|
||||
}
|
||||
helperImports++
|
||||
}
|
||||
}
|
||||
if itemImports != 3 || helperImports != 1 {
|
||||
t.Fatalf("item imports = %d, helper imports = %d, sources = %#v", itemImports, helperImports, result.Sources())
|
||||
}
|
||||
if result.Config.Prompts[1].ID != "curated" {
|
||||
t.Fatalf("helper did not provide prompt ID: %#v", result.Config.Prompts[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptCatalogLeavesDomainValidationWithConsumer(t *testing.T) {
|
||||
_, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "invalid.yml"))
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) || len(loadError.Diagnostics()) != 1 ||
|
||||
loadError.Diagnostics()[0].Code != configkit.CodeValidateError ||
|
||||
loadError.Diagnostics()[0].Path.String() != "/prompts/0/messages" {
|
||||
t.Fatalf("consumer domain validation error = %v", err)
|
||||
}
|
||||
}
|
||||
12
examples/consumers/testdata/prompt-catalog/config.yml
vendored
Normal file
12
examples/consumers/testdata/prompt-catalog/config.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
configkit:
|
||||
imports:
|
||||
- path: prompts/curated.yml
|
||||
destination: [prompts]
|
||||
placement: sequence-item
|
||||
- glob: prompts/generated-*.yml
|
||||
destination: [prompts]
|
||||
placement: sequence-item
|
||||
prompts:
|
||||
- id: main
|
||||
name: Main authored prompt
|
||||
messages: [Use the main prompt.]
|
||||
4
examples/consumers/testdata/prompt-catalog/invalid.yml
vendored
Normal file
4
examples/consumers/testdata/prompt-catalog/invalid.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
prompts:
|
||||
- id: invalid
|
||||
name: Invalid prompt
|
||||
messages: []
|
||||
1
examples/consumers/testdata/prompt-catalog/prompts/curated-id.yml
vendored
Normal file
1
examples/consumers/testdata/prompt-catalog/prompts/curated-id.yml
vendored
Normal file
@@ -0,0 +1 @@
|
||||
id: curated
|
||||
6
examples/consumers/testdata/prompt-catalog/prompts/curated.yml
vendored
Normal file
6
examples/consumers/testdata/prompt-catalog/prompts/curated.yml
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
configkit:
|
||||
imports:
|
||||
- path: curated-id.yml
|
||||
name: Curated summary
|
||||
messages:
|
||||
- Summarize the supplied content.
|
||||
4
examples/consumers/testdata/prompt-catalog/prompts/generated-weather.yml
vendored
Normal file
4
examples/consumers/testdata/prompt-catalog/prompts/generated-weather.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
id: weather
|
||||
name: Weather forecast
|
||||
messages:
|
||||
- Describe today's forecast.
|
||||
4
examples/consumers/testdata/prompt-catalog/prompts/generated-welcome.yml
vendored
Normal file
4
examples/consumers/testdata/prompt-catalog/prompts/generated-welcome.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
id: welcome
|
||||
name: Welcome
|
||||
messages:
|
||||
- Welcome the user.
|
||||
Reference in New Issue
Block a user