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:
+143
-11
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -13,9 +14,10 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// uploadChannel is the ingestion-channel tag the server records for CLI uploads.
|
||||
// Distinct from "web" (browser UI), "browser_extension" (one-click capture),
|
||||
// and "wechat" (mini-program). The server uses this only for analytics.
|
||||
// uploadChannel is the default ingestion-channel tag the server records for
|
||||
// CLI uploads. Distinct from "web" (browser UI), "browser_extension"
|
||||
// (one-click capture), and "wechat" (mini-program). The server uses this only
|
||||
// for analytics. Users can override via --channel for cross-tool replay.
|
||||
const uploadChannel = "api"
|
||||
|
||||
// docUploadFields enumerates the fields surfaced for `--json` discovery on
|
||||
@@ -34,6 +36,25 @@ type UploadOptions struct {
|
||||
Recursive bool // --recursive: positional arg is a directory; walk + upload each match
|
||||
Glob string // --glob: filename pattern under --recursive (default "*")
|
||||
FromURL string // --from-url: ingest a remote URL via SDK CreateKnowledgeFromURL
|
||||
|
||||
// EnableMultimodel toggles server-side multimodal extraction
|
||||
// (e.g. images-in-PDF → OCR'd text). nil means "server default" -
|
||||
// the flag was not set. true/false explicitly override.
|
||||
EnableMultimodel *bool
|
||||
|
||||
// Metadata is the raw --metadata key=value list. Parsed into a map
|
||||
// at run-time; empty values allowed, duplicate keys last-wins.
|
||||
Metadata []string
|
||||
|
||||
// Channel overrides the ingestion-channel tag recorded server-side.
|
||||
// Empty ⇒ uploadChannel ("api"). Free-form: server validates.
|
||||
Channel string
|
||||
|
||||
// URL-mode only fields. RunE-side validation rejects these if
|
||||
// --from-url is not set (positional file path or --recursive).
|
||||
Title string // --title: display title (URL mode)
|
||||
FileType string // --file-type: extension hint for extension-less URLs
|
||||
TagID string // --tag-id: associate the new knowledge entry with a tag
|
||||
}
|
||||
|
||||
// UploadService is the narrow SDK surface this command depends on.
|
||||
@@ -70,20 +91,47 @@ has a generic name like "report.pdf" but you want to surface it as e.g.
|
||||
|
||||
The three input modes (positional file / --recursive directory walk /
|
||||
--from-url remote ingest) are mutually exclusive - pass exactly one.
|
||||
Use --recursive --glob to upload a directory tree (see Examples).`,
|
||||
Use --recursive --glob to upload a directory tree (see Examples).
|
||||
|
||||
Server-side ingestion knobs apply to all modes:
|
||||
|
||||
--enable-multimodel Toggle multimodal extraction (image-in-PDF → text).
|
||||
Unset ⇒ server default; pass true or false to override.
|
||||
--metadata key=value Attach arbitrary key/value metadata. Repeatable.
|
||||
Empty value allowed; duplicate keys ⇒ last-wins.
|
||||
Malformed values (no '=', empty key) ⇒
|
||||
input.invalid_argument.
|
||||
--channel <name> Override the ingestion-channel tag (default "api").
|
||||
|
||||
URL mode (--from-url) additionally accepts --title, --file-type, and --tag-id.
|
||||
Passing any of those without --from-url is rejected as input.invalid_argument.`,
|
||||
Example: ` weknora doc upload report.pdf
|
||||
weknora doc upload notes.md --kb a32a63ff-fb36-4874-bcaa-30f48570a694
|
||||
weknora doc upload notes.md --kb my-kb
|
||||
weknora doc upload q3.pdf --name "Q3 Marketing Report.pdf"
|
||||
weknora doc upload ./docs --recursive --glob '*.pdf'
|
||||
weknora doc upload report.pdf --enable-multimodel --metadata team=alpha --metadata sprint=Q4
|
||||
weknora doc upload ./docs --recursive --glob '*.pdf' --metadata team=alpha
|
||||
weknora doc upload --from-url https://example.com/whitepaper.pdf
|
||||
weknora doc upload --from-url https://example.com/article.html --name "Q3 Article"`,
|
||||
weknora doc upload --from-url https://example.com/no-ext --file-type pdf --title "Whitepaper"
|
||||
weknora doc upload --from-url https://example.com/article.html --name "Q3 Article" --tag-id tag_abc`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
jopts, err := cmdutil.CheckJSONFlags(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Translate the tri-state --enable-multimodel flag into the
|
||||
// *bool the SDK expects. Cobra's BoolVar can't distinguish
|
||||
// "unset" from "false", so we register a String flag and read
|
||||
// Changed() + the raw value here.
|
||||
if c.Flags().Changed("enable-multimodel") {
|
||||
raw, _ := c.Flags().GetString("enable-multimodel")
|
||||
v, perr := parseTriBool(raw)
|
||||
if perr != nil {
|
||||
return perr
|
||||
}
|
||||
opts.EnableMultimodel = &v
|
||||
}
|
||||
if err := validateUploadFlags(opts, args); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -114,13 +162,71 @@ Use --recursive --glob to upload a directory tree (see Examples).`,
|
||||
cmd.Flags().BoolVar(&opts.Recursive, "recursive", false, "Treat the positional argument as a directory to walk")
|
||||
cmd.Flags().StringVar(&opts.Glob, "glob", "*", "Filename pattern to filter when --recursive (e.g. '*.pdf')")
|
||||
cmd.Flags().StringVar(&opts.FromURL, "from-url", "", "Ingest a remote `URL` (HTTP/HTTPS) instead of a local file")
|
||||
// Tri-state flag: unset ⇒ server default, "true"/"false" override. The
|
||||
// raw string is decoded into opts.EnableMultimodel in RunE.
|
||||
cmd.Flags().String("enable-multimodel", "", "Toggle multimodal extraction (true|false); unset ⇒ server default")
|
||||
cmd.Flags().Lookup("enable-multimodel").NoOptDefVal = "true"
|
||||
cmd.Flags().StringSliceVar(&opts.Metadata, "metadata", nil, "Attach metadata `key=value` (repeatable; empty value allowed, last-wins on duplicate keys)")
|
||||
cmd.Flags().StringVar(&opts.Channel, "channel", "", "Ingestion-channel tag recorded server-side (default \"api\")")
|
||||
cmd.Flags().StringVar(&opts.Title, "title", "", "Display title for the new entry (--from-url only)")
|
||||
cmd.Flags().StringVar(&opts.FileType, "file-type", "", "File-type hint such as \"pdf\" when the URL has no extension (--from-url only)")
|
||||
cmd.Flags().StringVar(&opts.TagID, "tag-id", "", "Tag id to associate with the new entry (--from-url only)")
|
||||
cmdutil.AddJSONFlags(cmd, docUploadFields)
|
||||
return cmd
|
||||
}
|
||||
|
||||
// parseTriBool parses the raw --enable-multimodel string into a bool. Bare
|
||||
// --enable-multimodel (no value) is treated as "true" via NoOptDefVal at
|
||||
// registration time; callers gate on Changed() so an unset flag never gets
|
||||
// here. An explicit empty string (e.g. --enable-multimodel="" from an
|
||||
// uninterpolated shell variable) is rejected as input.invalid_argument
|
||||
// rather than silently coerced.
|
||||
func parseTriBool(raw string) (bool, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(raw)) {
|
||||
case "true", "1", "yes":
|
||||
return true, nil
|
||||
case "false", "0", "no":
|
||||
return false, nil
|
||||
default:
|
||||
return false, cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
fmt.Sprintf("--enable-multimodel expects true|false, got %q", raw))
|
||||
}
|
||||
}
|
||||
|
||||
// parseMetadataKV converts the raw --metadata key=value slice into a map.
|
||||
// Empty values are allowed. Duplicate keys ⇒ last-wins. Returns nil when
|
||||
// the slice is empty so callers pass nil through to the SDK unchanged.
|
||||
func parseMetadataKV(raw []string) (map[string]string, error) {
|
||||
if len(raw) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
out := make(map[string]string, len(raw))
|
||||
for _, kv := range raw {
|
||||
eq := strings.IndexByte(kv, '=')
|
||||
if eq <= 0 {
|
||||
return nil, cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
fmt.Sprintf("--metadata expects key=value (got %q)", kv))
|
||||
}
|
||||
out[kv[:eq]] = kv[eq+1:]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// effectiveChannel returns the channel string to send to the SDK. Empty
|
||||
// opts.Channel falls back to the default "api" so the wire payload is
|
||||
// identical to the pre-flag behavior.
|
||||
func effectiveChannel(opts *UploadOptions) string {
|
||||
if opts.Channel != "" {
|
||||
return opts.Channel
|
||||
}
|
||||
return uploadChannel
|
||||
}
|
||||
|
||||
// validateUploadFlags enforces mutual exclusion between the three input
|
||||
// modes (positional file path / --recursive directory walk / --from-url
|
||||
// remote ingest) and validates the URL when --from-url is set.
|
||||
// remote ingest) and validates the URL when --from-url is set. It also
|
||||
// rejects the URL-mode-only flags (--title, --file-type, --tag-id) when
|
||||
// --from-url isn't set so misuse fails fast with a typed code.
|
||||
func validateUploadFlags(opts *UploadOptions, args []string) error {
|
||||
hasPath := len(args) == 1
|
||||
hasURL := opts.FromURL != ""
|
||||
@@ -139,17 +245,39 @@ func validateUploadFlags(opts *UploadOptions, args []string) error {
|
||||
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
"a file path is required (or pass --from-url)")
|
||||
}
|
||||
// --title / --file-type / --tag-id are URL-mode only. Reject silently
|
||||
// ignoring them in file mode to avoid the "set X, server did nothing"
|
||||
// surprise.
|
||||
if opts.Title != "" {
|
||||
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
"--title is only valid with --from-url")
|
||||
}
|
||||
if opts.FileType != "" {
|
||||
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
"--file-type is only valid with --from-url")
|
||||
}
|
||||
if opts.TagID != "" {
|
||||
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument,
|
||||
"--tag-id is only valid with --from-url")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runUploadFromURL ingests a remote URL via SDK CreateKnowledgeFromURL.
|
||||
// `--name` becomes the FileName hint so the server's "known file extension"
|
||||
// detection upgrades crawl-mode to file-download-mode when appropriate.
|
||||
// Server-side knobs (--enable-multimodel, --metadata via Title/TagID/FileType)
|
||||
// propagate when set; the SDK request struct omits empty fields via
|
||||
// `json:",omitempty"` tags so wire payload stays minimal.
|
||||
func runUploadFromURL(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID string) error {
|
||||
req := sdk.CreateKnowledgeFromURLRequest{
|
||||
URL: opts.FromURL,
|
||||
FileName: opts.Name,
|
||||
Channel: uploadChannel,
|
||||
URL: opts.FromURL,
|
||||
FileName: opts.Name,
|
||||
FileType: opts.FileType,
|
||||
EnableMultimodel: opts.EnableMultimodel,
|
||||
Title: opts.Title,
|
||||
TagID: opts.TagID,
|
||||
Channel: effectiveChannel(opts),
|
||||
}
|
||||
k, err := svc.CreateKnowledgeFromURL(ctx, kbID, req)
|
||||
if err != nil {
|
||||
@@ -209,7 +337,11 @@ func validateUploadPath(path string) error {
|
||||
}
|
||||
|
||||
func runUpload(ctx context.Context, opts *UploadOptions, jopts *cmdutil.JSONOptions, svc UploadService, kbID, path string) error {
|
||||
k, err := svc.CreateKnowledgeFromFile(ctx, kbID, path, nil /*metadata*/, nil /*enableMultimodel*/, opts.Name, uploadChannel)
|
||||
meta, err := parseMetadataKV(opts.Metadata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
k, err := svc.CreateKnowledgeFromFile(ctx, kbID, path, meta, opts.EnableMultimodel, opts.Name, effectiveChannel(opts))
|
||||
if err != nil {
|
||||
return cmdutil.WrapHTTP(err, "upload %s", path)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user