73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package kb
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/agent"
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/format"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
"github.com/Tencent/WeKnora/cli/internal/text"
|
|
sdk "github.com/Tencent/WeKnora/client"
|
|
)
|
|
|
|
// GetOptions captures `weknora kb get` flags.
|
|
type GetOptions struct {
|
|
JSONOut bool
|
|
}
|
|
|
|
// GetService is the narrow SDK surface this command depends on.
|
|
type GetService interface {
|
|
GetKnowledgeBase(ctx context.Context, id string) (*sdk.KnowledgeBase, error)
|
|
}
|
|
|
|
// NewCmdGet builds `weknora kb get <id>`.
|
|
func NewCmdGet(f *cmdutil.Factory) *cobra.Command {
|
|
opts := &GetOptions{}
|
|
cmd := &cobra.Command{
|
|
Use: "get <id>",
|
|
Short: "Show a knowledge base by ID",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
cli, err := f.Client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return runGet(c.Context(), opts, cli, args[0])
|
|
},
|
|
}
|
|
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
|
agent.SetAgentHelp(cmd, "Returns details of one knowledge base by ID (config + counts).")
|
|
return cmd
|
|
}
|
|
|
|
func runGet(ctx context.Context, opts *GetOptions, svc GetService, id string) error {
|
|
kb, err := svc.GetKnowledgeBase(ctx, id)
|
|
if err != nil {
|
|
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "get knowledge base %q", id)
|
|
}
|
|
if opts.JSONOut {
|
|
return format.WriteEnvelope(iostreams.IO.Out, format.Success(kb, nil))
|
|
}
|
|
// Human: KEY: VALUE
|
|
w := iostreams.IO.Out
|
|
fmt.Fprintf(w, "ID: %s\n", kb.ID)
|
|
fmt.Fprintf(w, "NAME: %s\n", kb.Name)
|
|
if kb.Description != "" {
|
|
fmt.Fprintf(w, "DESC: %s\n", kb.Description)
|
|
}
|
|
fmt.Fprintf(w, "DOCS: %s\n", text.Pluralize(int(kb.KnowledgeCount), "doc"))
|
|
fmt.Fprintf(w, "CHUNKS: %s\n", text.Pluralize(int(kb.ChunkCount), "chunk"))
|
|
if kb.EmbeddingModelID != "" {
|
|
fmt.Fprintf(w, "EMBEDDING: %s\n", kb.EmbeddingModelID)
|
|
}
|
|
if !kb.UpdatedAt.IsZero() {
|
|
// Detail page favors absolute time; FuzzyAgo is reserved for list views.
|
|
fmt.Fprintf(w, "UPDATED: %s\n", kb.UpdatedAt.Format("2006-01-02 15:04:05"))
|
|
}
|
|
return nil
|
|
}
|