Add opt-in PromptKit namespaces
This commit is contained in:
69
integrations/ckprompt/namespaces.go
Normal file
69
integrations/ckprompt/namespaces.go
Normal file
@@ -0,0 +1,69 @@
|
||||
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) []commandkit.Flag[I] {
|
||||
flags := make([]commandkit.Flag[I], 0, len(kinds))
|
||||
for _, kind := range kinds {
|
||||
var meta commandkit.FlagMeta
|
||||
var field func(*namespaceInput) *commandkit.Supplied[string]
|
||||
switch kind {
|
||||
case "prompt":
|
||||
meta = namespaceFlagDefinitions.prompt.meta
|
||||
field = func(input *namespaceInput) *commandkit.Supplied[string] { return &input.prompt }
|
||||
case "profile":
|
||||
meta = namespaceFlagDefinitions.profile.meta
|
||||
field = func(input *namespaceInput) *commandkit.Supplied[string] { return &input.profile }
|
||||
case "backend":
|
||||
meta = namespaceFlagDefinitions.backend.meta
|
||||
field = func(input *namespaceInput) *commandkit.Supplied[string] { return &input.backend }
|
||||
}
|
||||
flags = append(flags, commandkit.ValueFlag(meta, (*string)(nil), parseDefinitionString, func(input *I, value commandkit.Supplied[string]) { *field(destination(input)) = value }))
|
||||
}
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user