feat(cli): per-resource filter flags on list commands
Adds the filter flags users were reaching for via `--jq` post-filter: - `kb list --pinned` — client-side filter to KBs with `IsPinned`. - `doc list --status <pending|processing|completed|failed>` — server-side query-param filter; `failed` surfaces ingestion errors immediately for triage. - `session list --since <duration>` — client-side filter to sessions updated within the past duration. Accepts time.ParseDuration forms (24h, 1h30m, 30m) plus a `<N>d` suffix for whole days (7d, 0.5d). Server-side filters are forwarded as query params (where the API supports them) to avoid pulling the full list into memory; client-side filters apply after the fetch so they compose with `--limit`.
This commit is contained in:
+23
-3
@@ -30,6 +30,11 @@ var kbListFields = []string{
|
||||
"created_at", "updated_at",
|
||||
}
|
||||
|
||||
// ListOptions captures `kb list` filter flag state.
|
||||
type ListOptions struct {
|
||||
Pinned bool // --pinned: client-side filter to KBs with IsPinned == true
|
||||
}
|
||||
|
||||
// ListService is the narrow SDK surface this command depends on.
|
||||
type ListService interface {
|
||||
ListKnowledgeBases(ctx context.Context) ([]sdk.KnowledgeBase, error)
|
||||
@@ -42,6 +47,7 @@ type listResult struct {
|
||||
|
||||
// NewCmdList builds `weknora kb list`.
|
||||
func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &ListOptions{}
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List knowledge bases visible to the active context",
|
||||
@@ -55,15 +61,16 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return runList(c.Context(), jopts, cli)
|
||||
return runList(c.Context(), opts, jopts, cli)
|
||||
},
|
||||
}
|
||||
cmd.Flags().BoolVar(&opts.Pinned, "pinned", false, "Only show pinned knowledge bases")
|
||||
cmdutil.AddJSONFlags(cmd, kbListFields)
|
||||
agent.SetAgentHelp(cmd, "Lists all knowledge bases. Returns data.items: [{id, name, ...}]; empty array when none. Use `--json` (bare) for the field list, `--json id,name` to project, or `--jq` for arbitrary reshape.")
|
||||
agent.SetAgentHelp(cmd, "Lists all knowledge bases. Returns data.items: [{id, name, ...}]; empty array when none. --pinned restricts to pinned KBs (client-side filter). Use `--json` (bare) for the field list, `--json id,name` to project, or `--jq` for arbitrary reshape.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func runList(ctx context.Context, jopts *cmdutil.JSONOptions, svc ListService) error {
|
||||
func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions, svc ListService) error {
|
||||
items, err := svc.ListKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
return cmdutil.WrapHTTP(err, "list knowledge bases")
|
||||
@@ -71,6 +78,15 @@ func runList(ctx context.Context, jopts *cmdutil.JSONOptions, svc ListService) e
|
||||
if items == nil {
|
||||
items = []sdk.KnowledgeBase{} // ensure JSON [] not null
|
||||
}
|
||||
if opts.Pinned {
|
||||
filtered := items[:0]
|
||||
for _, kb := range items {
|
||||
if kb.IsPinned {
|
||||
filtered = append(filtered, kb)
|
||||
}
|
||||
}
|
||||
items = filtered
|
||||
}
|
||||
// Spec §1.2: default sort by updated_at desc. Server return order is not
|
||||
// guaranteed, so client-side sort makes output deterministic regardless
|
||||
// of backend storage choices.
|
||||
@@ -87,6 +103,10 @@ func runList(ctx context.Context, jopts *cmdutil.JSONOptions, svc ListService) e
|
||||
}
|
||||
|
||||
if len(items) == 0 {
|
||||
if opts.Pinned {
|
||||
fmt.Fprintln(iostreams.IO.Out, "(no pinned knowledge bases)")
|
||||
return nil
|
||||
}
|
||||
fmt.Fprintln(iostreams.IO.Out, "(no knowledge bases)")
|
||||
return nil
|
||||
}
|
||||
|
||||
+38
-5
@@ -23,7 +23,7 @@ func (f *fakeListSvc) ListKnowledgeBases(ctx context.Context) ([]sdk.KnowledgeBa
|
||||
|
||||
func TestList_Empty_Human(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
if err := runList(context.Background(), nil, &fakeListSvc{items: []sdk.KnowledgeBase{}}); err != nil {
|
||||
if err := runList(context.Background(), &ListOptions{}, nil, &fakeListSvc{items: []sdk.KnowledgeBase{}}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
if !strings.Contains(out.String(), "(no knowledge bases)") {
|
||||
@@ -34,7 +34,7 @@ func TestList_Empty_Human(t *testing.T) {
|
||||
func TestList_Empty_JSON(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
jopts := &cmdutil.JSONOptions{}
|
||||
if err := runList(context.Background(), jopts, &fakeListSvc{items: []sdk.KnowledgeBase{}}); err != nil {
|
||||
if err := runList(context.Background(), &ListOptions{}, jopts, &fakeListSvc{items: []sdk.KnowledgeBase{}}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
got := out.String()
|
||||
@@ -53,7 +53,7 @@ func TestList_NonEmpty_Human_RenderColumns(t *testing.T) {
|
||||
{ID: "kb1", Name: "Marketing", KnowledgeCount: 5, UpdatedAt: now.Add(-3 * time.Hour)},
|
||||
{ID: "kb2", Name: "Engineering", KnowledgeCount: 1, UpdatedAt: now.Add(-2 * 24 * time.Hour)},
|
||||
}
|
||||
if err := runList(context.Background(), nil, &fakeListSvc{items: items}); err != nil {
|
||||
if err := runList(context.Background(), &ListOptions{}, nil, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
got := out.String()
|
||||
@@ -71,7 +71,7 @@ func TestList_JSON_FieldFilter(t *testing.T) {
|
||||
{ID: "kb1", Name: "Marketing", Description: "MKT desc", UpdatedAt: now},
|
||||
}
|
||||
jopts := &cmdutil.JSONOptions{Fields: []string{"id", "name"}}
|
||||
if err := runList(context.Background(), jopts, &fakeListSvc{items: items}); err != nil {
|
||||
if err := runList(context.Background(), &ListOptions{}, jopts, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
var env struct {
|
||||
@@ -103,10 +103,43 @@ func TestList_JSON_JQ(t *testing.T) {
|
||||
{ID: "kb2", Name: "Engineering", UpdatedAt: now.Add(-time.Hour)},
|
||||
}
|
||||
jopts := &cmdutil.JSONOptions{JQ: ".data.items | length"}
|
||||
if err := runList(context.Background(), jopts, &fakeListSvc{items: items}); err != nil {
|
||||
if err := runList(context.Background(), &ListOptions{}, jopts, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
if got := strings.TrimSpace(out.String()); got != "2" {
|
||||
t.Errorf("expected '2', got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestList_PinnedFilter(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
now := time.Now()
|
||||
items := []sdk.KnowledgeBase{
|
||||
{ID: "kb1", Name: "Marketing", IsPinned: true, UpdatedAt: now},
|
||||
{ID: "kb2", Name: "Engineering", IsPinned: false, UpdatedAt: now.Add(-time.Hour)},
|
||||
{ID: "kb3", Name: "Finance", IsPinned: true, UpdatedAt: now.Add(-2 * time.Hour)},
|
||||
}
|
||||
if err := runList(context.Background(), &ListOptions{Pinned: true}, nil, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
got := out.String()
|
||||
if !strings.Contains(got, "kb1") || !strings.Contains(got, "kb3") {
|
||||
t.Errorf("expected pinned KBs kb1 and kb3 in output, got:\n%s", got)
|
||||
}
|
||||
if strings.Contains(got, "kb2") {
|
||||
t.Errorf("unpinned kb2 should be filtered out, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestList_PinnedFilter_NoPinned_HumanMessage(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
items := []sdk.KnowledgeBase{
|
||||
{ID: "kb1", Name: "Marketing", IsPinned: false, UpdatedAt: time.Now()},
|
||||
}
|
||||
if err := runList(context.Background(), &ListOptions{Pinned: true}, nil, &fakeListSvc{items: items}); err != nil {
|
||||
t.Fatalf("runList: %v", err)
|
||||
}
|
||||
if !strings.Contains(out.String(), "(no pinned knowledge bases)") {
|
||||
t.Errorf("expected pinned-specific empty message, got: %q", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user