feat(cli): doc/kb resource expansion — upload flags + list filters + view fields
Closes deep-tuning gaps in v0.4-shipped doc / kb / session commands. Each command had multiple SDK fields the CLI silently hardcoded or omitted; this commit threads them through. doc upload: - --enable-multimodel (tri-state via NoOptDefVal): toggle multimodal extraction (PDF/DOCX image-to-text) per upload. Empty explicit value (e.g. --enable-multimodel="" from uninterpolated shell var) rejected as input.invalid_argument rather than silently coerced. - --metadata key=value (repeatable): attach arbitrary metadata - --channel <name> (default api, override for browser / wechat ingests) - URL mode gains --title / --file-type / --tag-id; URL-only flags rejected with input.invalid_argument when used without --from-url doc list filter flags: - --keyword (server-side LIKE — case-sensitive per PG) - --file-type / --source / --tag-id - --start-time / --end-time (RFC3339) search docs switches from client-side substring to server-side Keyword via ListKnowledgeWithFilter — smaller wire payload, but case-sensitivity shifts (documented in CHANGELOG + help text + Long). MCP doc_list schema gains the same 6 filter fields (parity with CLI). session view --full + --limit: loads chat history via LoadMessages SDK method. kb view human KV expanded: type / pinned / temporary / processing state + count / summary model / created timestamp. All omit-empty. doc view human KV expanded: title (when distinct from filename) / description / source / channel / summary_status / enable_status / tag_id / storage (human bytes) / file_hash (12-char prefix).
This commit is contained in:
+29
-28
@@ -16,9 +16,9 @@ import (
|
||||
)
|
||||
|
||||
// docsPageSize is the default --page-size on `search docs`: how many
|
||||
// entries to pull per ListKnowledge round-trip when paging through a KB
|
||||
// to filter client-side. Server caps page_size at 1000 (per the doc/list
|
||||
// bound this branch already added). Tunable via --page-size in 1..1000.
|
||||
// entries to pull per ListKnowledgeWithFilter round-trip. The server
|
||||
// applies the keyword filter pre-pagination, so most KBs return in a
|
||||
// single page even at conservative sizes. Server caps page_size at 1000.
|
||||
const docsPageSize = 200
|
||||
|
||||
// docsMaxPageSize bounds the --page-size flag, matching session/doc list canon.
|
||||
@@ -39,10 +39,10 @@ type DocsSearchOptions struct {
|
||||
KB string // raw --kb (UUID or name)
|
||||
KBID string // resolved id; populated before listing
|
||||
Limit int
|
||||
// PageSize is the server batch size per ListKnowledge call (1..1000,
|
||||
// default 200). Tunable so a caller searching a small KB can fetch
|
||||
// everything in one round-trip, or a caller on flaky network can
|
||||
// shorten the batch.
|
||||
// PageSize is the server batch size per ListKnowledgeWithFilter call
|
||||
// (1..1000, default 200). Tunable so a caller searching a small KB
|
||||
// can fetch everything in one round-trip, or a caller on flaky
|
||||
// network can shorten the batch.
|
||||
PageSize int
|
||||
// AllPages walks server pages internally until total exhausted or
|
||||
// --limit accumulated. Default true preserves v0.4 behavior; setting
|
||||
@@ -51,25 +51,32 @@ type DocsSearchOptions struct {
|
||||
}
|
||||
|
||||
// DocsSearchService is the narrow SDK surface this command depends on.
|
||||
// Server has no fuzzy-document-name endpoint, so the CLI pages through
|
||||
// ListKnowledge and filters by Title / FileName client-side.
|
||||
// The server applies the keyword filter pre-pagination via the
|
||||
// ?keyword= query param, so the CLI just forwards opts.Query as
|
||||
// filter.Keyword and accumulates the (already-filtered) pages.
|
||||
type DocsSearchService 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)
|
||||
}
|
||||
|
||||
// NewCmdDocs builds `weknora search docs "<query>" --kb <id-or-name>`.
|
||||
// Pages through the KB's documents and surfaces every entry whose title
|
||||
// or filename contains the query (case-insensitive). Useful for finding
|
||||
// a specific upload to download or delete.
|
||||
// or file_name contains the query as a server-side case-sensitive LIKE
|
||||
// match. Useful for finding a specific upload to download or delete.
|
||||
func NewCmdDocs(f *cmdutil.Factory) *cobra.Command {
|
||||
opts := &DocsSearchOptions{}
|
||||
cmd := &cobra.Command{
|
||||
Use: `docs "<query>"`,
|
||||
Short: "Find documents in a knowledge base by name (client-side substring match)",
|
||||
Long: `Pages through the KB's documents and surfaces every entry whose title or
|
||||
filename contains the query (case-insensitive). Useful for finding a
|
||||
Short: "Find documents in a knowledge base by keyword (server-side filter)",
|
||||
Long: `Pages through the KB's documents, forwarding the query as the server-side
|
||||
keyword filter (matched against title / file_name). Useful for finding a
|
||||
specific upload to download or delete by id.
|
||||
|
||||
The query is a case-sensitive server-side LIKE filter (the server runs
|
||||
` + "`LIKE %keyword%`" + ` against title and file_name). For case-insensitive
|
||||
matching, lower-case the query yourself, e.g.
|
||||
` + "`weknora search docs \"$(printf %s YOUR_QUERY | tr 'A-Z' 'a-z')\"`" + `, or
|
||||
fall back to ` + "`weknora api`" + ` with a custom filter.
|
||||
|
||||
By default, --all-pages=true walks every server page until --limit is
|
||||
reached or the KB is exhausted (matching v0.4 behavior). Pass
|
||||
--all-pages=false to stop after one page.`,
|
||||
@@ -115,24 +122,24 @@ func runDocsSearch(ctx context.Context, opts *DocsSearchOptions, jopts *cmdutil.
|
||||
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
fmt.Sprintf("--page-size must be in 1..%d, got %d", docsMaxPageSize, opts.PageSize))
|
||||
}
|
||||
needle := strings.ToLower(opts.Query)
|
||||
filter := sdk.KnowledgeListFilter{Keyword: opts.Query}
|
||||
var matches []sdk.Knowledge
|
||||
|
||||
// Page through the KB until limit matches found or pagination exhausted.
|
||||
// The server applies the keyword filter pre-pagination, so every item
|
||||
// returned is already a match - no client-side filter needed.
|
||||
// --all-pages=true (default) walks every server page; --all-pages=false
|
||||
// stops after the first page. The server returns total; stop when
|
||||
// page*pageSize >= total.
|
||||
for page := 1; ; page++ {
|
||||
items, total, err := svc.ListKnowledge(ctx, opts.KBID, page, opts.PageSize, "")
|
||||
items, total, err := svc.ListKnowledgeWithFilter(ctx, opts.KBID, page, opts.PageSize, filter)
|
||||
if err != nil {
|
||||
return cmdutil.WrapHTTP(err, "list documents")
|
||||
}
|
||||
for _, k := range items {
|
||||
if matchKnowledge(k, needle) {
|
||||
matches = append(matches, k)
|
||||
if opts.Limit > 0 && len(matches) >= opts.Limit {
|
||||
goto done
|
||||
}
|
||||
matches = append(matches, k)
|
||||
if opts.Limit > 0 && len(matches) >= opts.Limit {
|
||||
goto done
|
||||
}
|
||||
}
|
||||
if !opts.AllPages {
|
||||
@@ -164,12 +171,6 @@ done:
|
||||
return tw.Flush()
|
||||
}
|
||||
|
||||
// matchKnowledge reports whether title or filename contains needle (already
|
||||
// lowercased by caller).
|
||||
func matchKnowledge(k sdk.Knowledge, needle string) bool {
|
||||
return text.ContainsFold(needle, k.Title, k.FileName)
|
||||
}
|
||||
|
||||
// sortKnowledgeByRecency sorts in place by UpdatedAt desc.
|
||||
func sortKnowledgeByRecency(items []sdk.Knowledge) {
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
|
||||
+38
-13
@@ -15,17 +15,20 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// fakeDocsSearchSvc scripts paginated ListKnowledge responses. Pages are
|
||||
// indexed 1-based; items keyed by page.
|
||||
// fakeDocsSearchSvc scripts paginated ListKnowledgeWithFilter responses.
|
||||
// Pages are indexed 1-based; items keyed by page. The last-received filter
|
||||
// is captured so tests can assert opts.Query was threaded as filter.Keyword.
|
||||
type fakeDocsSearchSvc struct {
|
||||
pages map[int][]sdk.Knowledge
|
||||
total int64
|
||||
err error
|
||||
calls []int // page numbers requested, for assertions
|
||||
pages map[int][]sdk.Knowledge
|
||||
total int64
|
||||
err error
|
||||
calls []int // page numbers requested, for assertions
|
||||
lastFilter sdk.KnowledgeListFilter
|
||||
}
|
||||
|
||||
func (f *fakeDocsSearchSvc) ListKnowledge(_ context.Context, kbID string, page, pageSize int, tagID string) ([]sdk.Knowledge, int64, error) {
|
||||
func (f *fakeDocsSearchSvc) ListKnowledgeWithFilter(_ context.Context, kbID string, page, pageSize int, filter sdk.KnowledgeListFilter) ([]sdk.Knowledge, int64, error) {
|
||||
f.calls = append(f.calls, page)
|
||||
f.lastFilter = filter
|
||||
if f.err != nil {
|
||||
return nil, 0, f.err
|
||||
}
|
||||
@@ -34,21 +37,23 @@ func (f *fakeDocsSearchSvc) ListKnowledge(_ context.Context, kbID string, page,
|
||||
|
||||
func TestDocsSearch_Substring(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
// Server applies the keyword filter pre-pagination; the fake simulates
|
||||
// that by only returning the matching items (d1/d3, not d2).
|
||||
svc := &fakeDocsSearchSvc{
|
||||
pages: map[int][]sdk.Knowledge{
|
||||
1: {
|
||||
{ID: "d1", Title: "Q3 Forecast", FileName: "q3.pdf", UpdatedAt: mustTime(t, "2026-05-10T00:00:00Z")},
|
||||
{ID: "d2", Title: "Random Notes", FileName: "notes.md", UpdatedAt: mustTime(t, "2026-05-12T00:00:00Z")},
|
||||
{ID: "d3", Title: "Q3 retro", FileName: "retro.pdf", UpdatedAt: mustTime(t, "2026-05-11T00:00:00Z")},
|
||||
},
|
||||
},
|
||||
total: 3,
|
||||
total: 2,
|
||||
}
|
||||
require.NoError(t, runDocsSearch(context.Background(), &DocsSearchOptions{Query: "q3", KBID: "kb1", Limit: 20, PageSize: docsPageSize, AllPages: true}, nil, svc))
|
||||
assert.Equal(t, "q3", svc.lastFilter.Keyword, "query must be threaded as filter.Keyword")
|
||||
got := out.String()
|
||||
assert.Contains(t, got, "d1")
|
||||
assert.Contains(t, got, "d3")
|
||||
assert.NotContains(t, got, "d2") // "Random Notes" doesn't contain q3
|
||||
assert.NotContains(t, got, "d2")
|
||||
}
|
||||
|
||||
func TestDocsSearch_MatchesFileName(t *testing.T) {
|
||||
@@ -61,20 +66,24 @@ func TestDocsSearch_MatchesFileName(t *testing.T) {
|
||||
assert.Contains(t, out.String(), "d1")
|
||||
}
|
||||
|
||||
// TestDocsSearch_PaginatesUntilTotal walks server-paginated results.
|
||||
// Server-side filter has already been applied, so every returned item
|
||||
// is in the result set; the runner just walks pages until total exhausted
|
||||
// or --limit hit. With limit > total matches, we expect 2 pages.
|
||||
func TestDocsSearch_PaginatesUntilTotal(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
page1 := make([]sdk.Knowledge, docsPageSize)
|
||||
for i := range page1 {
|
||||
page1[i] = sdk.Knowledge{ID: "p1", Title: "no match"}
|
||||
page1[i] = sdk.Knowledge{ID: "p1", Title: "needle"}
|
||||
}
|
||||
page2 := []sdk.Knowledge{{ID: "found", Title: "needle here"}}
|
||||
svc := &fakeDocsSearchSvc{
|
||||
pages: map[int][]sdk.Knowledge{1: page1, 2: page2},
|
||||
total: int64(docsPageSize) + 1,
|
||||
}
|
||||
require.NoError(t, runDocsSearch(context.Background(), &DocsSearchOptions{Query: "needle", KBID: "kb1", Limit: 20, PageSize: docsPageSize, AllPages: true}, nil, svc))
|
||||
require.NoError(t, runDocsSearch(context.Background(), &DocsSearchOptions{Query: "needle", KBID: "kb1", Limit: docsPageSize + 1, PageSize: docsPageSize, AllPages: true}, nil, svc))
|
||||
assert.Contains(t, out.String(), "found")
|
||||
assert.Equal(t, []int{1, 2}, svc.calls, "must page past the first batch when no match on page 1")
|
||||
assert.Equal(t, []int{1, 2}, svc.calls, "must page past the first batch when more items reported")
|
||||
}
|
||||
|
||||
func TestDocsSearch_StopsAtLimit(t *testing.T) {
|
||||
@@ -145,6 +154,22 @@ func TestSearchDocs_AllPagesFalse_StopsAtFirstPage(t *testing.T) {
|
||||
assert.Len(t, svc.calls, 1, "must stop at first page when --all-pages=false")
|
||||
}
|
||||
|
||||
// TestSearchDocs_KeywordPassedToFilter pins the v0.5 switch from client-side
|
||||
// substring filtering to server-side ?keyword= via ListKnowledgeWithFilter.
|
||||
// The query argument must arrive on the filter struct (not a discarded
|
||||
// client-side variable).
|
||||
func TestSearchDocs_KeywordPassedToFilter(t *testing.T) {
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
svc := &fakeDocsSearchSvc{pages: map[int][]sdk.Knowledge{1: {{ID: "d1"}}}, total: 1}
|
||||
require.NoError(t, runDocsSearch(context.Background(), &DocsSearchOptions{Query: "my-query", KBID: "kb1", Limit: 20, PageSize: docsPageSize, AllPages: true}, nil, svc))
|
||||
assert.Equal(t, "my-query", svc.lastFilter.Keyword, "Query must be threaded as filter.Keyword on ListKnowledgeWithFilter")
|
||||
// Other filter fields must be empty - search docs only forwards the keyword.
|
||||
assert.Empty(t, svc.lastFilter.ParseStatus)
|
||||
assert.Empty(t, svc.lastFilter.FileType)
|
||||
assert.Empty(t, svc.lastFilter.Source)
|
||||
assert.Empty(t, svc.lastFilter.TagID)
|
||||
}
|
||||
|
||||
// TestSearchDocs_PageSizeBound asserts the 1..1000 range guard mirrors the
|
||||
// session/doc list canon. Out-of-range values must produce
|
||||
// input.invalid_argument and never reach the SDK.
|
||||
|
||||
Reference in New Issue
Block a user