Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(cli): per-resource filter flags on list commands

nullkey3b67986863b0
diff --git a/cli/cmd/doc/list.go b/cli/cmd/doc/list.go
index 2541ca059..3f2ab06fe 100644
--- a/cli/cmd/doc/list.go
+++ b/cli/cmd/doc/list.go
@@ -3,7 +3,9 @@ package doc
import (
"context"
"fmt"
+ "slices"
"sort"
+ "strings"
"text/tabwriter"
"time"
@@ -31,12 +33,19 @@ var docListFields = []string{
type ListOptions struct {
Page int
PageSize int
+ Status string // --status: filter by parse_status (server-side query param)
}
+// docListStatusValues mirrors internal/types/knowledge.go ParseStatus*
+// constants — these are the values the server accepts on the
+// ?parse_status= query. Kept in sync manually since the SDK doesn't
+// re-export the enum.
+var docListStatusValues = []string{"pending", "processing", "completed", "failed"}
+
// ListService is the narrow SDK surface this command depends on.
// *sdk.Client satisfies it.
type ListService interface {
- ListKnowledge(ctx context.Context, kbID string, page, pageSize int, tagID string) ([]sdk.Knowledge, int64, error)
+ ListKnowledgeWithFilter(ctx context.Context, kbID string, page, pageSize int, filter sdk.KnowledgeListFilter) ([]sdk.Knowledge, int64, error)
}
// listResult is the typed payload emitted under data on success.
@@ -91,8 +100,9 @@ backend storage order is not guaranteed and varies between deployments.`,
cmd.Flags().String("kb", "", "Knowledge base UUID or name (overrides env / project link)")
cmd.Flags().IntVar(&opts.Page, "page", 1, "Page number (1-based)")
cmd.Flags().IntVar(&opts.PageSize, "page-size", 20, "Items per page (1..1000)")
+ cmd.Flags().StringVar(&opts.Status, "status", "", "Filter by parse status: pending | processing | completed | failed")
cmdutil.AddJSONFlags(cmd, docListFields)
- agent.SetAgentHelp(cmd, "Lists docs in the resolved KB. Returns data: {items, page, page_size, total, kb_id}; pass --kb when not running inside a project.")
+ agent.SetAgentHelp(cmd, "Lists docs in the resolved KB. Returns data: {items, page, page_size, total, kb_id}; pass --kb when not running inside a project. --status (server-side filter) restricts to a parse-pipeline state — `failed` is the practical one for surfacing ingestion errors.")
return cmd
}
@@ -109,7 +119,15 @@ func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions,
Message: fmt.Sprintf("--page-size must be in 1..1000, got %d", opts.PageSize),
}
}
- items, total, err := svc.ListKnowledge(ctx, kbID, opts.Page, opts.PageSize, "")
+ if opts.Status != "" && !validDocListStatus(opts.Status) {
+ return &cmdutil.Error{
+ Code: cmdutil.CodeInputInvalidArgument,
+ Message: fmt.Sprintf("--status must be one of: %s — got %q",
+ strings.Join(docListStatusValues, " | "), opts.Status),
+ }
+ }
+ items, total, err := svc.ListKnowledgeWithFilter(ctx, kbID, opts.Page, opts.PageSize,
+ sdk.KnowledgeListFilter{ParseStatus: opts.Status})
if err != nil {
return cmdutil.WrapHTTP(err, "list documents")
}
@@ -150,6 +168,12 @@ func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions,
return tw.Flush()
}
+// validDocListStatus reports whether s matches one of the server-accepted
+// parse_status enum values surfaced via --status.
+func validDocListStatus(s string) bool {
+ return slices.Contains(docListStatusValues, s)
+}
+
// formatSize renders a byte count as a short human string (KB / MB).
// Kept tiny on purpose — go-humanize would pull a transitive dep just for one
// column. A "-" placeholder hides zero-size entries (URL / text).
diff --git a/cli/cmd/doc/list_test.go b/cli/cmd/doc/list_test.go
index e37470d85..b06cea7e0 100644
--- a/cli/cmd/doc/list_test.go
+++ b/cli/cmd/doc/list_test.go
@@ -26,12 +26,12 @@ type fakeListSvc struct {
kbID string
page int
pageSize int
- tagID string
+ filter sdk.KnowledgeListFilter
}
}
-func (f *fakeListSvc) ListKnowledge(_ context.Context, kbID string, page, pageSize int, tagID string) ([]sdk.Knowledge, int64, error) {
- f.got.kbID, f.got.page, f.got.pageSize, f.got.tagID = kbID, page, pageSize, tagID
+func (f *fakeListSvc) ListKnowledgeWithFilter(_ context.Context, kbID string, page, pageSize int, filter sdk.KnowledgeListFilter) ([]sdk.Knowledge, int64, error) {
+ f.got.kbID, f.got.page, f.got.pageSize, f.got.filter = kbID, page, pageSize, filter
return f.items, f.total, f.err
}
@@ -61,7 +61,7 @@ func TestList_Success_Human(t *testing.T) {
assert.Equal(t, "kb_xxx", svc.got.kbID)
assert.Equal(t, 1, svc.got.page)
assert.Equal(t, 20, svc.got.pageSize)
- assert.Equal(t, "", svc.got.tagID, "doc list does not filter by tag")
+ assert.Equal(t, sdk.KnowledgeListFilter{}, svc.got.filter, "no flags ⇒ empty filter")
got := out.String()
for _, want := range []string{"ID", "NAME", "STATUS", "SIZE", "UPDATED", "doc1", "alpha.pdf", "completed", "2.0KB", "doc2", "beta.md", "pending"} {
@@ -217,3 +217,38 @@ func TestFormatSize(t *testing.T) {
}
}
+func TestList_StatusFilter_ForwardedToSDK(t *testing.T) {
+ chdirIsolated(t)
+ _, _ = iostreams.SetForTest(t)
+ svc := &fakeListSvc{}
+ opts := &ListOptions{Page: 1, PageSize: 20, Status: "failed"}
+ require.NoError(t, runList(context.Background(), opts, nil, svc, "kb_xxx"))
+ assert.Equal(t, "failed", svc.got.filter.ParseStatus,
+ "--status must be forwarded as filter.ParseStatus for server-side filtering")
+}
+
+func TestList_StatusFilter_RejectsUnknownValue(t *testing.T) {
+ chdirIsolated(t)
+ _, _ = iostreams.SetForTest(t)
+ svc := &fakeListSvc{}
+ opts := &ListOptions{Page: 1, PageSize: 20, Status: "bogus"}
+ err := runList(context.Background(), opts, nil, svc, "kb_xxx")
+ require.Error(t, err)
+ var typed *cmdutil.Error
+ require.ErrorAs(t, err, &typed)
+ assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
+ assert.Contains(t, typed.Message, "pending")
+ assert.Contains(t, typed.Message, "failed")
+}
+
+func TestList_StatusFilter_AcceptsAllEnumValues(t *testing.T) {
+ chdirIsolated(t)
+ for _, v := range docListStatusValues {
+ _, _ = iostreams.SetForTest(t)
+ svc := &fakeListSvc{}
+ opts := &ListOptions{Page: 1, PageSize: 20, Status: v}
+ require.NoError(t, runList(context.Background(), opts, nil, svc, "kb_xxx"),
+ "status=%q should be accepted", v)
+ }
+}
+
diff --git a/cli/cmd/kb/list.go b/cli/cmd/kb/list.go
index 818428741..78efe5946 100644
--- a/cli/cmd/kb/list.go
+++ b/cli/cmd/kb/list.go
@@ -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
}
diff --git a/cli/cmd/kb/list_test.go b/cli/cmd/kb/list_test.go
index ca7d5a138..131131e4e 100644
--- a/cli/cmd/kb/list_test.go
+++ b/cli/cmd/kb/list_test.go
@@ -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())
+ }
+}
diff --git a/cli/cmd/session/list.go b/cli/cmd/session/list.go
index 68c6c36be..b0106c0ae 100644
--- a/cli/cmd/session/list.go
+++ b/cli/cmd/session/list.go
@@ -3,6 +3,8 @@ package sessioncmd
import (
"context"
"fmt"
+ "strconv"
+ "strings"
"text/tabwriter"
"time"
@@ -31,6 +33,7 @@ var sessionListFields = []string{
type ListOptions struct {
Page int
PageSize int
+ Since string // --since: filter to sessions updated within the past duration
}
// ListService is the narrow SDK surface this command depends on.
@@ -65,8 +68,9 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
}
cmd.Flags().IntVar(&opts.Page, "page", defaultPage, "Page number (1-indexed)")
cmd.Flags().IntVar(&opts.PageSize, "page-size", defaultPageSize, "Items per page (1..1000)")
+ cmd.Flags().StringVar(&opts.Since, "since", "", "Only show sessions updated within `duration` (e.g. 7d, 24h, 30m)")
cmdutil.AddJSONFlags(cmd, sessionListFields)
- agent.SetAgentHelp(cmd, "Lists chat sessions. _meta.has_more is set when more pages exist; bump --page and retry to walk them.")
+ agent.SetAgentHelp(cmd, "Lists chat sessions. _meta.has_more is set when more pages exist; bump --page and retry to walk them. --since filters *within the returned page* (client-side); widen --page-size when looking far back.")
return cmd
}
@@ -83,6 +87,14 @@ func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions,
Message: fmt.Sprintf("--page-size must be in 1..%d, got %d", maxPageSize, opts.PageSize),
}
}
+ var since time.Duration
+ if opts.Since != "" {
+ d, err := parseSinceDuration(opts.Since)
+ if err != nil {
+ return err
+ }
+ since = d
+ }
items, total, err := svc.GetSessionsByTenant(ctx, opts.Page, opts.PageSize)
if err != nil {
@@ -91,6 +103,20 @@ func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions,
if items == nil {
items = []sdk.Session{} // JSON [] not null
}
+ if since > 0 {
+ threshold := time.Now().Add(-since)
+ filtered := items[:0]
+ for _, s := range items {
+ t, err := time.Parse(time.RFC3339, s.UpdatedAt)
+ if err != nil {
+ continue // skip unparseable timestamps rather than guess
+ }
+ if t.After(threshold) {
+ filtered = append(filtered, s)
+ }
+ }
+ items = filtered
+ }
if jopts.Enabled() {
meta := &format.Meta{HasMore: opts.Page*opts.PageSize < total}
@@ -118,6 +144,39 @@ func runList(ctx context.Context, opts *ListOptions, jopts *cmdutil.JSONOptions,
return tw.Flush()
}
+// parseSinceDuration accepts `time.ParseDuration` forms (1h30m, 24h, 30m)
+// plus a `<N>d` suffix for whole days (e.g. 7d). Returns an
+// input.invalid_argument *cmdutil.Error for unparseable inputs or
+// non-positive durations.
+func parseSinceDuration(s string) (time.Duration, error) {
+ raw := strings.TrimSpace(s)
+ var d time.Duration
+ var err error
+ if rest, ok := strings.CutSuffix(raw, "d"); ok {
+ num, perr := strconv.ParseFloat(rest, 64)
+ if perr != nil {
+ err = perr
+ } else {
+ d = time.Duration(num * float64(24*time.Hour))
+ }
+ } else {
+ d, err = time.ParseDuration(raw)
+ }
+ if err != nil {
+ return 0, &cmdutil.Error{
+ Code: cmdutil.CodeInputInvalidArgument,
+ Message: fmt.Sprintf("--since %q is not a valid duration: %v (try 7d, 24h, 30m, 1h30m)", s, err),
+ }
+ }
+ if d <= 0 {
+ return 0, &cmdutil.Error{
+ Code: cmdutil.CodeInputInvalidArgument,
+ Message: fmt.Sprintf("--since must be positive, got %q", s),
+ }
+ }
+ return d, nil
+}
+
// fuzzyTime renders a server-provided timestamp string in "2d ago" form.
// Returns the raw input if parsing fails — better to surface the unknown
// format than to silently render "-".
diff --git a/cli/cmd/session/list_test.go b/cli/cmd/session/list_test.go
index c33517168..f1c63d313 100644
--- a/cli/cmd/session/list_test.go
+++ b/cli/cmd/session/list_test.go
@@ -6,6 +6,7 @@ import (
"errors"
"strings"
"testing"
+ "time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -138,3 +139,57 @@ func TestList_NonASCIITitle(t *testing.T) {
require.NoError(t, runList(context.Background(), &ListOptions{Page: 1, PageSize: 30}, nil, svc))
assert.Contains(t, out.String(), "s_zh")
}
+
+func TestList_SinceFilter_DropsOldSessions(t *testing.T) {
+ out, _ := iostreams.SetForTest(t)
+ now := time.Now()
+ items := []sdk.Session{
+ {ID: "recent", Title: "today", UpdatedAt: now.Add(-1 * time.Hour).Format(time.RFC3339)},
+ {ID: "old", Title: "last month", UpdatedAt: now.Add(-30 * 24 * time.Hour).Format(time.RFC3339)},
+ {ID: "yesterday", Title: "yday", UpdatedAt: now.Add(-23 * time.Hour).Format(time.RFC3339)},
+ }
+ require.NoError(t, runList(context.Background(),
+ &ListOptions{Page: 1, PageSize: 30, Since: "7d"}, nil,
+ &fakeListService{items: items, total: 3}))
+ got := out.String()
+ assert.Contains(t, got, "recent")
+ assert.Contains(t, got, "yesterday")
+ assert.NotContains(t, got, "old", "30-day-old session should be filtered out by --since 7d")
+}
+
+func TestList_SinceFilter_ParseDuration_Variants(t *testing.T) {
+ cases := []string{"24h", "1h30m", "30m", "7d", "0.5d", "168h"}
+ for _, v := range cases {
+ t.Run(v, func(t *testing.T) {
+ _, _ = iostreams.SetForTest(t)
+ require.NoError(t, runList(context.Background(),
+ &ListOptions{Page: 1, PageSize: 30, Since: v}, nil,
+ &fakeListService{items: []sdk.Session{}, total: 0}),
+ "--since=%q should parse", v)
+ })
+ }
+}
+
+func TestList_SinceFilter_RejectsInvalidDuration(t *testing.T) {
+ _, _ = iostreams.SetForTest(t)
+ err := runList(context.Background(),
+ &ListOptions{Page: 1, PageSize: 30, Since: "bogus"}, nil,
+ &fakeListService{items: []sdk.Session{}, total: 0})
+ require.Error(t, err)
+ var typed *cmdutil.Error
+ require.ErrorAs(t, err, &typed)
+ assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
+}
+
+func TestList_SinceFilter_RejectsNonPositive(t *testing.T) {
+ _, _ = iostreams.SetForTest(t)
+ for _, v := range []string{"0d", "0h", "-1h"} {
+ err := runList(context.Background(),
+ &ListOptions{Page: 1, PageSize: 30, Since: v}, nil,
+ &fakeListService{items: []sdk.Session{}, total: 0})
+ require.Error(t, err, "--since=%q should reject", v)
+ var typed *cmdutil.Error
+ require.ErrorAs(t, err, &typed)
+ assert.Equal(t, cmdutil.CodeInputInvalidArgument, typed.Code)
+ }
+}
diff --git a/cli/internal/cmdutil/jsonflags.go b/cli/internal/cmdutil/jsonflags.go
index 34c93a501..d127154a2 100644
--- a/cli/internal/cmdutil/jsonflags.go
+++ b/cli/internal/cmdutil/jsonflags.go
@@ -46,7 +46,10 @@ const jsonNoOptSentinel = "\x00json-no-value"
// the command's help. Pass nil to skip the help annotation (uncommon).
func AddJSONFlags(cmd *cobra.Command, fields []string) {
f := cmd.Flags()
- f.StringSlice("json", nil, "Output JSON envelope; bare `--json` for full, `--json=id,name` for filtered fields")
+ // Backticks reserved for pflag's UnquoteUsage to extract the varname;
+ // avoid them in the description so the help doesn't render the flag
+ // name twice.
+ f.StringSlice("json", nil, "Output JSON envelope (bare for full; --json=id,name for `fields`)")
f.Lookup("json").NoOptDefVal = jsonNoOptSentinel
f.StringP("jq", "q", "", "Filter JSON output using a jq `expression` (requires --json)")