Share application loading in composed example
This commit is contained in:
@@ -249,7 +249,16 @@ The composed example is completed in Stage 5.
|
|||||||
|
|
||||||
## Stage 5: Demonstrate shared loading and the subtree adapter
|
## Stage 5: Demonstrate shared loading and the subtree adapter
|
||||||
|
|
||||||
Status: pending.
|
Status: complete.
|
||||||
|
|
||||||
|
Evidence: The composed example loads application defaults, bundled and local
|
||||||
|
resources, campaign-derived settings, and storage in one ConfigKit result;
|
||||||
|
the engine consumes that result and the storage scope uses the subtree adapter.
|
||||||
|
Updated fixtures and tests exercise native paths, shared inspection and reuse,
|
||||||
|
storage show/explain/diff and overrides, whole-load failure evidence, and the
|
||||||
|
existing campaign, comparison, catalog, and lifecycle cases. With Go 1.27.4,
|
||||||
|
`GOTOOLCHAIN=local go test ./integrations/ckprompt/examples/composed` passed in
|
||||||
|
the aligned workspace.
|
||||||
|
|
||||||
Update the maintained [composed example](../../integrations/ckprompt/examples/composed/main.go),
|
Update the maintained [composed example](../../integrations/ckprompt/examples/composed/main.go),
|
||||||
its fixtures, and its existing tests. A separate consumer example is unnecessary.
|
its fixtures, and its existing tests. A separate consumer example is unnecessary.
|
||||||
|
|||||||
@@ -8,3 +8,6 @@ campaigns:
|
|||||||
prompt_id: briefing
|
prompt_id: briefing
|
||||||
profile_id: autumn
|
profile_id: autumn
|
||||||
model: autumn-model
|
model: autumn-model
|
||||||
|
storage:
|
||||||
|
driver: local
|
||||||
|
location: ./after
|
||||||
|
|||||||
@@ -8,3 +8,6 @@ campaigns:
|
|||||||
prompt_id: briefing
|
prompt_id: briefing
|
||||||
profile_id: autumn
|
profile_id: autumn
|
||||||
model: autumn-model
|
model: autumn-model
|
||||||
|
storage:
|
||||||
|
driver: local
|
||||||
|
location: ./before
|
||||||
|
|||||||
@@ -22,9 +22,16 @@ type campaign struct {
|
|||||||
Model string `yaml:"model"`
|
Model string `yaml:"model"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type storageConfiguration struct {
|
||||||
|
Driver string `yaml:"driver"`
|
||||||
|
Location string `yaml:"location"`
|
||||||
|
}
|
||||||
|
|
||||||
type appConfiguration struct {
|
type appConfiguration struct {
|
||||||
DefaultCampaign string `yaml:"default_campaign"`
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||||
Campaigns map[string]campaign `yaml:"campaigns"`
|
Storage storageConfiguration `yaml:"storage"`
|
||||||
|
DefaultCampaign string `yaml:"default_campaign"`
|
||||||
|
Campaigns map[string]campaign `yaml:"campaigns"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type inspectionInput struct {
|
type inspectionInput struct {
|
||||||
@@ -95,26 +102,75 @@ func loadApplicationConfig(ctx context.Context, selection ckconfig.Selection, in
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
source := configkit.Mapping("composed application settings", map[string]any{
|
definition := configkit.Definition[appConfiguration]{
|
||||||
"default_campaign": "spring",
|
Defaults: func() appConfiguration {
|
||||||
"campaigns": map[string]any{
|
return appConfiguration{DefaultCampaign: "spring", Storage: storageConfiguration{Driver: "local", Location: "./data"}, Campaigns: map[string]campaign{
|
||||||
"spring": map[string]any{"prompt_id": "briefing", "profile_id": "spring", "model": "spring-model"},
|
"spring": {PromptID: "briefing", ProfileID: "spring", Model: "spring-model"},
|
||||||
"autumn": map[string]any{"prompt_id": "briefing", "profile_id": "autumn", "model": "autumn-model"},
|
"autumn": {PromptID: "briefing", ProfileID: "autumn", Model: "autumn-model"},
|
||||||
|
}}
|
||||||
},
|
},
|
||||||
})
|
Resolve: func(value *appConfiguration, resolution *configkit.Resolution) error {
|
||||||
|
name, chosen, err := selectedCampaign(*value, commandkit.Supplied[string]{})
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
deps := []configkit.Path{configkit.Keys("default_campaign"), configkit.Keys("campaigns", name)}
|
||||||
|
profilePath := configkit.Keys("promptkit", "profiles")
|
||||||
|
profileFound := false
|
||||||
|
for _, profile := range value.PromptKit.Profiles {
|
||||||
|
if profile.ID == chosen.ProfileID {
|
||||||
|
profileFound = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !profileFound {
|
||||||
|
value.PromptKit.Profiles = append(value.PromptKit.Profiles, promptkit.ProfileSettings{ID: chosen.ProfileID, Backend: new("local"), Endpoint: new("https://example.invalid/v1"), Model: new(chosen.Model), ExtraParams: map[string]any{"campaign": name}})
|
||||||
|
resolution.RecordNormalization(profilePath, "Selected campaign profile", deps...)
|
||||||
|
}
|
||||||
|
promptPath := configkit.Keys("promptkit", "prompts")
|
||||||
|
for index := range value.PromptKit.Prompts {
|
||||||
|
prompt := &value.PromptKit.Prompts[index]
|
||||||
|
if prompt.ID != "briefing" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if chosen.PromptID != prompt.ID {
|
||||||
|
prompt.ID = chosen.PromptID
|
||||||
|
resolution.RecordNormalization(promptPath.Index(index).Key("id"), "Selected campaign prompt", deps...)
|
||||||
|
}
|
||||||
|
if prompt.DefaultProfile == nil {
|
||||||
|
prompt.DefaultProfile = new(chosen.ProfileID)
|
||||||
|
resolution.RecordDerivedDefault(promptPath.Index(index).Key("default_profile"), "Selected campaign profile", deps...)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
definition = promptkit.Configure(definition, func(value *appConfiguration) *promptkit.Settings { return &value.PromptKit })
|
||||||
|
resources := promptkit.SettingsInput(configkit.Mapping("example resources", map[string]any{
|
||||||
|
"collections": map[string]any{"field-notes": map[string]any{
|
||||||
|
"release_version": "1",
|
||||||
|
"backends": []any{map[string]any{"id": "demo", "endpoint": "https://example.invalid/v1"}},
|
||||||
|
"profiles": []any{map[string]any{"id": "assistant", "backend": "field-notes/demo", "model": "example-model"}},
|
||||||
|
"prompts": []any{map[string]any{"id": "briefing", "default_profile": "field-notes/assistant", "inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}}, "messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}."}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}},
|
||||||
|
}},
|
||||||
|
"prompts": []any{map[string]any{"id": "briefing", "inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}}, "messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}. {{if .mode}}{{input \"notes\"}}{{end}}"}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}},
|
||||||
|
"backends": []any{map[string]any{"id": "local", "endpoint": "https://example.invalid/v1"}},
|
||||||
|
}))
|
||||||
|
source := configkit.NoSource()
|
||||||
if selection.Config.Set {
|
if selection.Config.Set {
|
||||||
source = configkit.File(selection.Config.Value)
|
source = configkit.File(selection.Config.Value)
|
||||||
}
|
}
|
||||||
options := configkit.Options[appConfiguration]{}
|
options := configkit.LayeredOptions[appConfiguration]{Definition: definition, Underlays: []configkit.Underlay{promptkit.BundledOpenRouterUnderlay(), {Inputs: []configkit.Input{resources}}}}
|
||||||
if selection.Overlay.Set {
|
if selection.Overlay.Set {
|
||||||
options.Overlay = selection.Overlay.Value
|
options.Overlay = selection.Overlay.Value
|
||||||
options.Filesystem = &configkit.FilesystemContext{Root: ".", BaseDir: "."}
|
options.OverlayFilesystem = &configkit.FilesystemContext{Root: ".", BaseDir: "."}
|
||||||
}
|
}
|
||||||
if campaignInput.Set {
|
if campaignInput.Set {
|
||||||
options.Overrides = []configkit.Override{{Path: configkit.Keys("default_campaign"), Value: campaignInput.Value, Flag: "--campaign"}}
|
options.Overrides = []configkit.Override{{Path: configkit.Keys("default_campaign"), Value: campaignInput.Value, Flag: "--campaign"}}
|
||||||
}
|
}
|
||||||
options.Overrides = append(options.Overrides, selection.Overrides...)
|
options.Overrides = append(options.Overrides, selection.Overrides...)
|
||||||
result, err := configkit.Load(source, options)
|
result, err := configkit.LoadLayered(configkit.Input{Source: source}, options)
|
||||||
if err == nil && result != nil && cache != nil {
|
if err == nil && result != nil && cache != nil {
|
||||||
cache.applications = append(cache.applications, applicationCacheEntry{selection: selection, campaign: campaignInput, result: result})
|
cache.applications = append(cache.applications, applicationCacheEntry{selection: selection, campaign: campaignInput, result: result})
|
||||||
}
|
}
|
||||||
@@ -126,48 +182,14 @@ func loadPromptEngine(ctx context.Context, selection ckconfig.Selection, input *
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
name, chosen, err := selectedCampaign(application.Config, input.Campaign)
|
if _, _, err := selectedCampaign(application.Config, commandkit.Supplied[string]{}); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
engineConfig := configkit.Mapping("campaign "+name, map[string]any{
|
|
||||||
"collections": map[string]any{
|
|
||||||
"field-notes": map[string]any{
|
|
||||||
"release_version": "1",
|
|
||||||
"backends": []any{map[string]any{"id": "demo", "endpoint": "https://example.invalid/v1"}},
|
|
||||||
"profiles": []any{map[string]any{"id": "assistant", "backend": "field-notes/demo", "model": "example-model"}},
|
|
||||||
"prompts": []any{map[string]any{
|
|
||||||
"id": "briefing",
|
|
||||||
"default_profile": "field-notes/assistant",
|
|
||||||
"inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}},
|
|
||||||
"messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}."}},
|
|
||||||
"output": map[string]any{"format": "text", "validation_mode": "none"},
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"backends": []any{map[string]any{"id": "local", "endpoint": "https://example.invalid/v1"}},
|
|
||||||
"profiles": []any{map[string]any{
|
|
||||||
"id": chosen.ProfileID,
|
|
||||||
"backend": "local",
|
|
||||||
"endpoint": "https://example.invalid/v1",
|
|
||||||
"model": chosen.Model,
|
|
||||||
"extra_params": map[string]any{
|
|
||||||
"campaign": name,
|
|
||||||
},
|
|
||||||
}},
|
|
||||||
"prompts": []any{map[string]any{
|
|
||||||
"id": chosen.PromptID,
|
|
||||||
"default_profile": chosen.ProfileID,
|
|
||||||
"inputs": []any{map[string]any{"name": "topic", "required": true, "content_type": "text/plain", "description": "Topic to brief"}},
|
|
||||||
"messages": []any{map[string]any{"role": "user", "content": "Write a concise briefing about {{.topic}}. {{if .mode}}{{input \"notes\"}}{{end}}"}},
|
|
||||||
"output": map[string]any{"format": "text", "validation_mode": "none"},
|
|
||||||
}},
|
|
||||||
})
|
|
||||||
var client promptkit.LLMClient = &exampleLLMClient{}
|
var client promptkit.LLMClient = &exampleLLMClient{}
|
||||||
if cache != nil && cache.llmClient != nil {
|
if cache != nil && cache.llmClient != nil {
|
||||||
client = cache.llmClient
|
client = cache.llmClient
|
||||||
}
|
}
|
||||||
return promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{Source: engineConfig}}}, promptkit.WithLLMClient(client))
|
return promptkit.NewFromConfig(application, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client))
|
||||||
}
|
}
|
||||||
|
|
||||||
func selectedCampaign(configuration appConfiguration, supplied commandkit.Supplied[string]) (string, campaign, error) {
|
func selectedCampaign(configuration appConfiguration, supplied commandkit.Supplied[string]) (string, campaign, error) {
|
||||||
@@ -231,6 +253,8 @@ func newAppWithEngineLoader(
|
|||||||
|
|
||||||
appScope := ckconfig.FromConfigKit("app", "Application settings.", loadApplicationConfig)
|
appScope := ckconfig.FromConfigKit("app", "Application settings.", loadApplicationConfig)
|
||||||
appScope.Features.Overrides = true
|
appScope.Features.Overrides = true
|
||||||
|
storageScope := ckconfig.FromConfigKitSubtree("storage", "Storage settings.", configkit.Keys("storage"), loadApplicationConfig)
|
||||||
|
storageScope.Features.Overrides = true
|
||||||
engineScope := ckprompt.ConfigScope(engineLoader)
|
engineScope := ckprompt.ConfigScope(engineLoader)
|
||||||
configCommand := ckconfig.Command(ckconfig.Suite[*inspectionInput, *runCache]{
|
configCommand := ckconfig.Command(ckconfig.Suite[*inspectionInput, *runCache]{
|
||||||
NewInput: inputFactory,
|
NewInput: inputFactory,
|
||||||
@@ -242,10 +266,10 @@ func newAppWithEngineLoader(
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Selectors: []ckconfig.Selector[*inspectionInput]{
|
Selectors: []ckconfig.Selector[*inspectionInput]{
|
||||||
{Binding: campaignSelector(), AgainstName: "against-campaign", Scopes: []string{"app", "promptkit"}},
|
{Binding: campaignSelector(), AgainstName: "against-campaign", Scopes: []string{"app", "storage", "promptkit"}},
|
||||||
{Binding: limitFlag, AgainstName: "against-limit", Scopes: []string{"app"}},
|
{Binding: limitFlag, AgainstName: "against-limit", Scopes: []string{"app"}},
|
||||||
},
|
},
|
||||||
Scopes: []ckconfig.Scope[*inspectionInput, *runCache]{appScope, engineScope},
|
Scopes: []ckconfig.Scope[*inspectionInput, *runCache]{appScope, storageScope, engineScope},
|
||||||
})
|
})
|
||||||
|
|
||||||
definitions := ckprompt.FromEngine(engineLoader)
|
definitions := ckprompt.FromEngine(engineLoader)
|
||||||
|
|||||||
@@ -294,7 +294,7 @@ func TestComposedPromptKitAndDomainCommandsRemainAvailable(t *testing.T) {
|
|||||||
{"config", "validate", "--scope", "all", "--campaign", "spring", "--limit=0"},
|
{"config", "validate", "--scope", "all", "--campaign", "spring", "--limit=0"},
|
||||||
{"config", "show", "--scope", "promptkit", "--campaign", "spring"},
|
{"config", "show", "--scope", "promptkit", "--campaign", "spring"},
|
||||||
{"config", "sources", "--scope", "promptkit", "--campaign", "spring"},
|
{"config", "sources", "--scope", "promptkit", "--campaign", "spring"},
|
||||||
{"config", "explain", "--scope", "promptkit", "--path", fmt.Sprintf("/profiles/%d/model", index), "--campaign", "spring"},
|
{"config", "explain", "--scope", "promptkit", "--path", fmt.Sprintf("/promptkit/profiles/%d/model", index), "--campaign", "spring"},
|
||||||
{"promptkit", "show", "prompt", "briefing", "--campaign", "spring", "--format", "yaml"},
|
{"promptkit", "show", "prompt", "briefing", "--campaign", "spring", "--format", "yaml"},
|
||||||
{"promptkit", "show", "profile", "spring", "--campaign", "spring"},
|
{"promptkit", "show", "profile", "spring", "--campaign", "spring"},
|
||||||
{"session", "restore", "session-42", "--campaign", "spring"},
|
{"session", "restore", "session-42", "--campaign", "spring"},
|
||||||
@@ -461,17 +461,19 @@ func TestPromptKitScopeDiffAndCatalogsUseSelectedCampaign(t *testing.T) {
|
|||||||
}
|
}
|
||||||
var diff struct {
|
var diff struct {
|
||||||
Changes struct {
|
Changes struct {
|
||||||
Profiles []struct {
|
PromptKit struct {
|
||||||
ID string `yaml:"id"`
|
Profiles []struct {
|
||||||
Model string `yaml:"model"`
|
ID string `yaml:"id"`
|
||||||
} `yaml:"profiles"`
|
Model string `yaml:"model"`
|
||||||
|
} `yaml:"profiles"`
|
||||||
|
} `yaml:"promptkit"`
|
||||||
} `yaml:"changes"`
|
} `yaml:"changes"`
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(output.Bytes(), &diff); err != nil {
|
if err := yaml.Unmarshal(output.Bytes(), &diff); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
foundChange := false
|
foundChange := false
|
||||||
for _, profile := range diff.Changes.Profiles {
|
for _, profile := range diff.Changes.PromptKit.Profiles {
|
||||||
if profile.ID == "spring" && profile.Model == "spring-model-after" {
|
if profile.ID == "spring" && profile.Model == "spring-model-after" {
|
||||||
foundChange = true
|
foundChange = true
|
||||||
}
|
}
|
||||||
@@ -740,3 +742,64 @@ func composedField(mapping *yaml.Node, name string) *yaml.Node {
|
|||||||
func ckconfigSelection(path string) ckconfig.Selection {
|
func ckconfigSelection(path string) ckconfig.Selection {
|
||||||
return ckconfig.Selection{Config: commandkit.Supplied[string]{Value: path, Set: true}}
|
return ckconfig.Selection{Config: commandkit.Supplied[string]{Value: path, Set: true}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestComposedStorageScopeAndSharedResult(t *testing.T) {
|
||||||
|
cache := &runCache{}
|
||||||
|
selection := ckconfigSelection("fixtures/before.yml")
|
||||||
|
input := &inspectionInput{Campaign: commandkit.Supplied[string]{Value: "spring", Set: true}}
|
||||||
|
result, err := loadApplicationConfig(context.Background(), selection, input, cache)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
engine, err := loadPromptEngine(context.Background(), selection, input, cache)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(cache.applications) != 1 || cache.applications[0].result != result {
|
||||||
|
t.Fatal("engine did not use the selected application result")
|
||||||
|
}
|
||||||
|
inspection, err := engine.InspectConfiguration()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
appView, ok := result.InspectSubtree(configkit.Keys("promptkit"))
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("missing application inspection")
|
||||||
|
}
|
||||||
|
var appYAML, promptYAML bytes.Buffer
|
||||||
|
if err := appView.WriteEffective(&appYAML); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := inspection.WriteEffective(&promptYAML); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if appYAML.String() != promptYAML.String() {
|
||||||
|
t.Fatal("engine inspection differs from shared application result")
|
||||||
|
}
|
||||||
|
app, err := newApp()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
args []string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{[]string{"config", "show", "--scope=storage", "--config=fixtures/before.yml"}, "./before"},
|
||||||
|
{[]string{"config", "explain", "--scope=storage", "--config=fixtures/before.yml", "--path=/storage/location"}, "/storage/location"},
|
||||||
|
{[]string{"config", "diff", "--scope=storage", "--config=fixtures/before.yml", "--against-config=fixtures/after.yml"}, "./after"},
|
||||||
|
{[]string{"config", "show", "--scope=storage", `--set=/storage/location="custom"`, "--campaign=spring"}, "custom"},
|
||||||
|
{[]string{"config", "show", "--scope=app"}, "promptkit:"},
|
||||||
|
{[]string{"config", "validate", "--scope=all"}, "storage"},
|
||||||
|
} {
|
||||||
|
var output, diagnostics bytes.Buffer
|
||||||
|
outcome := app.Execute(context.Background(), tc.args, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||||
|
if outcome.Kind != commandkit.OutcomeSuccess || !strings.Contains(output.String(), tc.want) {
|
||||||
|
t.Errorf("%v: %#v output=%q diagnostics=%q", tc.args, outcome, output.String(), diagnostics.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var diagnostics bytes.Buffer
|
||||||
|
outcome := app.Execute(context.Background(), []string{"config", "show", "--scope=storage", "--config=fixtures/missing.yml"}, commandkit.Streams{Out: io.Discard, Err: &diagnostics})
|
||||||
|
if outcome.Kind != commandkit.OutcomeOperational || !strings.Contains(diagnostics.String(), "missing.yml") {
|
||||||
|
t.Fatalf("failed load: %#v %q", outcome, diagnostics.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user