Files
commandkit/integrations/ckprompt/namespaces.go

70 lines
2.7 KiB
Go

package ckprompt
import (
commandkit "go.maximumdirect.net/commandkit"
promptkit "go.maximumdirect.net/promptkit"
)
// WithNamespaces enables standard resource namespace selection with explicit
// defaults. Supplied CLI namespace flags replace only the corresponding default.
// Loaders used with this option should leave namespace selection to the adapter.
func WithNamespaces[I, S any](suite Suite[I, S], defaults promptkit.Namespaces) Suite[I, S] {
suite.namespaces = &defaults
return suite
}
type namespaceInput struct {
prompt, profile, backend commandkit.Supplied[string]
}
func namespaceFlags[I any](destination func(*I) *namespaceInput, kinds ...string) []requestBinding[I] {
flags := make([]requestBinding[I], 0, len(kinds))
for _, kind := range kinds {
var definition requestFlagDefinition
var field func(*namespaceInput) *commandkit.Supplied[string]
switch kind {
case "prompt":
definition = namespaceFlagDefinitions.prompt
field = func(input *namespaceInput) *commandkit.Supplied[string] { return &input.prompt }
case "profile":
definition = namespaceFlagDefinitions.profile
field = func(input *namespaceInput) *commandkit.Supplied[string] { return &input.profile }
case "backend":
definition = namespaceFlagDefinitions.backend
field = func(input *namespaceInput) *commandkit.Supplied[string] { return &input.backend }
}
flags = append(flags, requestBinding[I]{commandkit.ValueFlag(definition.meta, (*string)(nil), parseDefinitionString, func(input *I, value commandkit.Supplied[string]) { *field(destination(input)) = value }), definition})
}
return flags
}
var namespaceFlagDefinitions = struct{ prompt, profile, backend requestFlagDefinition }{
prompt: requestFlagDefinition{commandkit.FlagMeta{Name: "llm-prompt-namespace", Usage: "Prompt resource namespace."}, []string{"prompt-namespace"}},
profile: requestFlagDefinition{commandkit.FlagMeta{Name: "llm-profile-namespace", Usage: "Profile resource namespace."}, []string{"profile-namespace"}},
backend: requestFlagDefinition{commandkit.FlagMeta{Name: "llm-backend-namespace", Usage: "Backend resource namespace."}, []string{"backend-namespace"}},
}
func selectedNamespaces(defaults *promptkit.Namespaces, selected namespaceInput) *promptkit.Namespaces {
if defaults == nil {
return nil
}
value := *defaults
if selected.prompt.Set {
value.Prompt = selected.prompt.Value
}
if selected.profile.Set {
value.Profile = selected.profile.Value
}
if selected.backend.Set {
value.Backend = selected.backend.Value
}
return &value
}
func namespacedEngine(engine *promptkit.Engine, namespaces *promptkit.Namespaces) (*promptkit.Engine, error) {
if namespaces == nil {
return engine, nil
}
return engine.WithNamespaces(*namespaces)
}