Add namespace and collection inventories

This commit is contained in:
2026-10-17 06:25:14 +00:00
parent c9b623b374
commit 8669294cf6
9 changed files with 295 additions and 3 deletions

View File

@@ -6,6 +6,12 @@ import (
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
)
// CollectionIdentity is the captured metadata for one final collection entry.
type CollectionIdentity struct {
Namespace string
ReleaseVersion *string
}
func (state *loaded) ListProfiles(ctx context.Context) ([]string, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -28,6 +34,44 @@ func (state *loaded) ListBackends(ctx context.Context) ([]string, error) {
return backends, nil
}
func (state *loaded) ListNamespaces(ctx context.Context, kinds []string) ([]string, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
selected := kinds
if len(selected) == 0 {
selected = []string{"backend", "profile", "prompt"}
}
set := make(map[string]struct{})
for _, kind := range selected {
for _, namespace := range state.namespaces[kind] {
set[namespace] = struct{}{}
}
}
namespaces := sortedKeys(set)
if err := ctx.Err(); err != nil {
return nil, err
}
return namespaces, nil
}
func (state *loaded) ListCollections(ctx context.Context) ([]CollectionIdentity, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
collections := make([]CollectionIdentity, len(state.collections))
for index, collection := range state.collections {
collections[index] = CollectionIdentity{
Namespace: collection.Namespace,
ReleaseVersion: stringPointerValue(collection.ReleaseVersion),
}
}
if err := ctx.Err(); err != nil {
return nil, err
}
return collections, nil
}
func (state *loaded) ListPrompts(ctx context.Context) ([]domain.PromptIdentity, error) {
if err := ctx.Err(); err != nil {
return nil, err

View File

@@ -127,6 +127,8 @@ type loaded struct {
backendIDs []string
profileIDs []string
promptIDs []domain.PromptIdentity
namespaces map[string][]string
collections []CollectionIdentity
}
type resourceBases struct {
@@ -266,9 +268,38 @@ func load(main configkit.Input, underlays []configkit.Underlay) (*loaded, error)
for _, id := range sortedKeys(state.prompts) {
state.promptIDs = append(state.promptIDs, domain.PromptIdentity{ID: id, Version: stringPointerValue(state.prompts[id].definition.Version)})
}
state.namespaces = map[string][]string{
"backend": resourceNamespaces(state.backendIDs),
"profile": resourceNamespaces(state.profileIDs),
"prompt": promptNamespaces(state.promptIDs),
}
for _, namespace := range sortedKeys(result.Config.Collections) {
collection := result.Config.Collections[namespace]
state.collections = append(state.collections, CollectionIdentity{
Namespace: namespace, ReleaseVersion: stringPointerValue(collection.ReleaseVersion),
})
}
return state, nil
}
func resourceNamespaces(ids []string) []string {
namespaces := make(map[string]struct{})
for _, id := range ids {
if namespace, _, qualified := strings.Cut(id, "/"); qualified {
namespaces[namespace] = struct{}{}
}
}
return sortedKeys(namespaces)
}
func promptNamespaces(prompts []domain.PromptIdentity) []string {
ids := make([]string, len(prompts))
for index := range prompts {
ids[index] = prompts[index].ID
}
return resourceNamespaces(ids)
}
func checkConfigurationVersion(version configkit.Version, validation *configkit.Validation) error {
if !version.Present {
return nil

View File

@@ -97,6 +97,8 @@ type identityInventory interface {
ListBackends(context.Context) ([]string, error)
ListProfiles(context.Context) ([]string, error)
ListPrompts(context.Context) ([]domain.PromptIdentity, error)
ListNamespaces(context.Context, []string) ([]string, error)
ListCollections(context.Context) ([]configuration.CollectionIdentity, error)
}
// ListingFilterMode selects namespace membership for one identity listing.
@@ -277,6 +279,21 @@ func (e *Engine) ListPrompts(ctx context.Context, filters ...ListingFilter) ([]d
return filtered, nil
}
func (e *Engine) ListNamespaces(ctx context.Context, kinds []string) ([]string, error) {
for _, kind := range kinds {
switch kind {
case "prompt", "profile", "backend":
default:
return nil, fmt.Errorf("%w: unknown resource kind %q", usecase.ErrInvalidRequest, kind)
}
}
return e.listing.ListNamespaces(ctx, kinds)
}
func (e *Engine) ListCollections(ctx context.Context) ([]configuration.CollectionIdentity, error) {
return e.listing.ListCollections(ctx)
}
func filterIdentityList(
ctx context.Context,
filters []ListingFilter,