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:
nullkey
2026-05-16 02:38:48 +08:00
committed by lyingbug
parent 7bccd72ba3
commit f89d54362d
17 changed files with 1280 additions and 79 deletions
+34 -7
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"strings"
"time"
mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"
@@ -122,10 +123,16 @@ func addKBView(server *mcpsdk.Server, svc knowledgeBaseService) {
// ---- doc_list ------------------------------------------------------------
type docListInput struct {
KBID string `json:"kb_id" jsonschema:"knowledge base ID"`
Page int `json:"page,omitempty" jsonschema:"1-indexed page number; defaults to 1"`
PageSize int `json:"page_size,omitempty" jsonschema:"items per page (1..1000); defaults to 20"`
Status string `json:"status,omitempty" jsonschema:"filter by parse status: pending | processing | completed | failed"`
KBID string `json:"kb_id" jsonschema:"knowledge base ID"`
Page int `json:"page,omitempty" jsonschema:"1-indexed page number; defaults to 1"`
PageSize int `json:"page_size,omitempty" jsonschema:"items per page (1..1000); defaults to 20"`
Status string `json:"status,omitempty" jsonschema:"filter by parse status: pending | processing | completed | failed"`
Keyword string `json:"keyword,omitempty" jsonschema:"server-side substring filter (case-sensitive LIKE against title / file_name); leave empty to skip"`
FileType string `json:"file_type,omitempty" jsonschema:"filter by file extension (e.g. pdf, md)"`
Source string `json:"source,omitempty" jsonschema:"filter by ingestion source (e.g. api, web)"`
TagID string `json:"tag_id,omitempty" jsonschema:"filter by tag association"`
StartTime string `json:"start_time,omitempty" jsonschema:"include docs with updated_at >= this RFC3339 timestamp (e.g. 2006-01-02T15:04:05Z)"`
EndTime string `json:"end_time,omitempty" jsonschema:"include docs with updated_at <= this RFC3339 timestamp (e.g. 2006-01-02T15:04:05Z)"`
}
type docListOutput struct {
@@ -138,7 +145,7 @@ type docListOutput struct {
func addDocList(server *mcpsdk.Server, svc knowledgeService) {
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "doc_list",
Description: "List documents in a knowledge base, with pagination and optional parse-status filter. Returns items[] with id, file_name, title, parse_status, size, updated_at - plus the page/total metadata.",
Description: "List documents in a knowledge base, with pagination and optional filters (parse-status, keyword, file_type, source, tag_id, start_time/end_time on updated_at). Returns items[] with id, file_name, title, parse_status, size, updated_at - plus the page/total metadata.",
}, func(ctx context.Context, _ *mcpsdk.CallToolRequest, in docListInput) (*mcpsdk.CallToolResult, docListOutput, error) {
if in.KBID == "" {
return nil, docListOutput{}, fmt.Errorf("kb_id is required")
@@ -154,8 +161,28 @@ func addDocList(server *mcpsdk.Server, svc knowledgeService) {
if size > 1000 {
return nil, docListOutput{}, fmt.Errorf("page_size must be in 1..1000")
}
items, total, err := svc.ListKnowledgeWithFilter(ctx, in.KBID, page, size,
sdk.KnowledgeListFilter{ParseStatus: in.Status})
filter := sdk.KnowledgeListFilter{
ParseStatus: in.Status,
Keyword: in.Keyword,
FileType: in.FileType,
Source: in.Source,
TagID: in.TagID,
}
if in.StartTime != "" {
t, err := time.Parse(time.RFC3339, in.StartTime)
if err != nil {
return nil, docListOutput{}, fmt.Errorf("start_time must be RFC3339 (e.g. 2006-01-02T15:04:05Z), got %q", in.StartTime)
}
filter.StartTime = t
}
if in.EndTime != "" {
t, err := time.Parse(time.RFC3339, in.EndTime)
if err != nil {
return nil, docListOutput{}, fmt.Errorf("end_time must be RFC3339 (e.g. 2006-01-02T15:04:05Z), got %q", in.EndTime)
}
filter.EndTime = t
}
items, total, err := svc.ListKnowledgeWithFilter(ctx, in.KBID, page, size, filter)
if err != nil {
return nil, docListOutput{}, fmt.Errorf("list documents: %w", err)
}
+53
View File
@@ -269,6 +269,59 @@ func TestTool_DocList_StatusFilter_Forwarded(t *testing.T) {
}
}
// TestTool_DocList_PassesFilterFields drives every C11 filter field at once
// and asserts they all land on filter struct (AND-combined server-side).
func TestTool_DocList_PassesFilterFields(t *testing.T) {
svc := &fakeSvc{}
c, _ := newTestServer(t, svc)
args := map[string]any{
"kb_id": "kb_x",
"status": "completed",
"keyword": "spec",
"file_type": "pdf",
"source": "api",
"tag_id": "tag_42",
"start_time": "2026-01-01T00:00:00Z",
"end_time": "2026-12-31T23:59:59Z",
}
callTool(t, c, "doc_list", args, nil)
f := svc.calls.docListFilter
assert.Equal(t, "completed", f.ParseStatus)
assert.Equal(t, "spec", f.Keyword)
assert.Equal(t, "pdf", f.FileType)
assert.Equal(t, "api", f.Source)
assert.Equal(t, "tag_42", f.TagID)
assert.False(t, f.StartTime.IsZero(), "start_time RFC3339 must populate filter.StartTime")
assert.False(t, f.EndTime.IsZero(), "end_time RFC3339 must populate filter.EndTime")
}
// TestTool_DocList_InvalidStartTime asserts malformed RFC3339 is rejected
// at the handler boundary (before the SDK is called).
func TestTool_DocList_InvalidStartTime(t *testing.T) {
c, _ := newTestServer(t, &fakeSvc{})
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
res, err := c.CallTool(ctx, &mcpsdk.CallToolParams{
Name: "doc_list",
Arguments: map[string]any{"kb_id": "kb_x", "start_time": "tomorrow"},
})
require.NoError(t, err)
require.True(t, res.IsError, "expected IsError=true on malformed RFC3339 start_time")
}
// TestTool_DocList_InvalidEndTime mirrors the start_time guard for end_time.
func TestTool_DocList_InvalidEndTime(t *testing.T) {
c, _ := newTestServer(t, &fakeSvc{})
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
res, err := c.CallTool(ctx, &mcpsdk.CallToolParams{
Name: "doc_list",
Arguments: map[string]any{"kb_id": "kb_x", "end_time": "2026-05-01"}, // date-only, not RFC3339
})
require.NoError(t, err)
require.True(t, res.IsError, "expected IsError=true on malformed RFC3339 end_time")
}
func TestTool_DocView(t *testing.T) {
svc := &fakeSvc{getDoc: &sdk.Knowledge{ID: "k1", FileName: "a.pdf"}}
c, _ := newTestServer(t, svc)