feat(cli): agent CRUD + view full config rendering
Adds the three management verbs missing from v0.4's agent subtree (create / edit / delete) and expands v0.4-shipped agent view to render all 34 AgentConfig fields in human output (was 7). Surface: hot-path flags (--model required + 7 optional) + --config-file YAML/JSON tail + --generate-skeleton template emit. Flag > file > server-default precedence for hybrid invocation. - agent create <name> --model <id> [flags] + --from <agent-id> for copy-then-overlay (CopyAgent + UpdateAgent); preserves source config except for fields explicitly overridden - agent edit <id> with --add-kb / --remove-kb idempotent pair, L-2 fetch-then-update, at-least-one-flag validation, --description "" clearing via Flags().Changed(). --config-file fully replaces the AgentConfig baseline (use surgical flags for partial edits; the Long help spells this out + a test pins the contract). - agent delete <id> with ConfirmDestructive + exit-10 protocol; 404 propagates resource.not_found (not idempotent) - agent view: 10 grouped sections (Identity / LLM / KB attachment / Retrieval / Query rewrite / Tools / FAQ / Web search / Multi-turn / Fallback / Templates); --json field discovery includes all config.* keys Shared helper cli/internal/cmdutil/agentconfig.go handles YAML/JSON parsing, flag-overlay-file fusion, and skeleton emission.
This commit is contained in:
+215
-36
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -12,13 +13,16 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// agentViewFields enumerates fields surfaced for `--json` discovery on
|
||||
// `agent view`. Filter applies to the bare Agent object. Config sub-fields
|
||||
// are intentionally omitted - too granular for naked projection; use
|
||||
// `--jq '.config'` to reach them.
|
||||
// agentViewFields enumerates fields surfaced for `--json=` field discovery
|
||||
// on `agent view`. Only top-level Agent keys are listed because the
|
||||
// `--json=foo,bar` field-projection filter matches flat top-level keys
|
||||
// (see internal/format/filter.go). Nested AgentConfig fields are reachable
|
||||
// via `--jq '.config.system_prompt'` or by selecting `config` whole and
|
||||
// post-processing — listing them here would misleadingly advertise a
|
||||
// projection path that does not actually filter to them.
|
||||
var agentViewFields = []string{
|
||||
"id", "name", "description", "avatar",
|
||||
"is_builtin", "tenant_id", "created_by",
|
||||
"is_builtin", "tenant_id", "created_by", "config",
|
||||
"created_at", "updated_at",
|
||||
}
|
||||
|
||||
@@ -32,12 +36,17 @@ func NewCmdView(f *cmdutil.Factory) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "view <agent-id>",
|
||||
Short: "Show a custom agent's configuration",
|
||||
Long: `Renders the agent's metadata (id / name / description / created-by /
|
||||
timestamps) plus a compact config summary (mode, model, allowed tools, KB
|
||||
scope). Pass --json for the full Agent object including the nested config
|
||||
struct - or --jq '.config' to extract just the config.`,
|
||||
Long: `Renders the agent's metadata and full AgentConfig as grouped KV
|
||||
sections (Identity / LLM / KB attachment / Retrieval / Query rewrite /
|
||||
Tools / FAQ / Web search / Multi-turn / Fallback / Templates). Zero-value
|
||||
fields are omitted; sections with no set fields are suppressed entirely.
|
||||
|
||||
Pass --json for the bare SDK Agent object (config nested, not flattened).
|
||||
Field projection (--json=id,name) works on top-level keys only; reach
|
||||
nested config fields via --jq.`,
|
||||
Example: ` weknora agent view ag_abc
|
||||
weknora agent view ag_abc --json | jq '.config.allowed_tools'`,
|
||||
weknora agent view ag_abc --json=id,name,config # top-level projection
|
||||
weknora agent view ag_abc --json --jq '.config.system_prompt'`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
jopts, err := cmdutil.CheckJSONFlags(c)
|
||||
@@ -67,47 +76,217 @@ func runView(ctx context.Context, jopts *cmdutil.JSONOptions, svc ViewService, a
|
||||
return nil
|
||||
}
|
||||
|
||||
// renderAgent prints a single agent in human-readable KV form. Empty
|
||||
// fields are omitted (mirrors doc view / kb view); the Config block is
|
||||
// compacted to its agent-mode-defining keys.
|
||||
// renderAgent prints a single agent in human-readable form, grouped into
|
||||
// 10 presentation sections (spec §2.9). Zero-value fields are omitted;
|
||||
// a section header prints only when at least one of its fields is set.
|
||||
// Group labels and order match the spec exactly so spec drift surfaces
|
||||
// as test failure rather than silent divergence.
|
||||
func renderAgent(w io.Writer, a *sdk.Agent) {
|
||||
fmt.Fprintf(w, "ID: %s\n", a.ID)
|
||||
fmt.Fprintf(w, "Name: %s\n", a.Name)
|
||||
// Identity is always rendered — id/name/created_at/updated_at are
|
||||
// never meaningfully empty for a fetched Agent.
|
||||
fmt.Fprintln(w, "Identity:")
|
||||
fmt.Fprintf(w, " ID: %s\n", a.ID)
|
||||
fmt.Fprintf(w, " Name: %s\n", a.Name)
|
||||
if a.Description != "" {
|
||||
fmt.Fprintf(w, "Description: %s\n", a.Description)
|
||||
fmt.Fprintf(w, " Description: %s\n", a.Description)
|
||||
}
|
||||
if a.IsBuiltin {
|
||||
fmt.Fprintln(w, "Builtin: yes")
|
||||
fmt.Fprintln(w, " Builtin: yes")
|
||||
}
|
||||
if a.CreatedBy != "" {
|
||||
fmt.Fprintf(w, "Created by: %s\n", a.CreatedBy)
|
||||
fmt.Fprintf(w, " Created by: %s\n", a.CreatedBy)
|
||||
}
|
||||
fmt.Fprintf(w, "Created at: %s\n", a.CreatedAt.Format("2006-01-02 15:04:05"))
|
||||
fmt.Fprintf(w, "Updated at: %s\n", a.UpdatedAt.Format("2006-01-02 15:04:05"))
|
||||
if a.TenantID != 0 {
|
||||
fmt.Fprintf(w, " Tenant ID: %d\n", a.TenantID)
|
||||
}
|
||||
fmt.Fprintf(w, " Created at: %s\n", a.CreatedAt.Format("2006-01-02 15:04:05"))
|
||||
fmt.Fprintf(w, " Updated at: %s\n", a.UpdatedAt.Format("2006-01-02 15:04:05"))
|
||||
|
||||
if a.Config == nil {
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintln(w, "Config:")
|
||||
if a.Config.AgentMode != "" {
|
||||
fmt.Fprintf(w, " Mode: %s\n", a.Config.AgentMode)
|
||||
c := a.Config
|
||||
|
||||
// Each group is rendered via a tiny helper: collect its set rows
|
||||
// upfront, suppress the whole section if empty. Avoids the
|
||||
// "header printed but no body" failure mode that plagues naive
|
||||
// conditional rendering.
|
||||
type row struct{ k, v string }
|
||||
emit := func(label string, rows []row) {
|
||||
if len(rows) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintf(w, "%s:\n", label)
|
||||
for _, r := range rows {
|
||||
fmt.Fprintf(w, " %-32s %s\n", r.k+":", r.v)
|
||||
}
|
||||
}
|
||||
if a.Config.ModelID != "" {
|
||||
fmt.Fprintf(w, " Model ID: %s\n", a.Config.ModelID)
|
||||
|
||||
// LLM
|
||||
llm := []row{}
|
||||
if c.ModelID != "" {
|
||||
llm = append(llm, row{"Model ID", c.ModelID})
|
||||
}
|
||||
if a.Config.RerankModelID != "" {
|
||||
fmt.Fprintf(w, " Rerank model ID: %s\n", a.Config.RerankModelID)
|
||||
if c.RerankModelID != "" {
|
||||
llm = append(llm, row{"Rerank model ID", c.RerankModelID})
|
||||
}
|
||||
if a.Config.KBSelectionMode != "" {
|
||||
fmt.Fprintf(w, " KB selection mode: %s\n", a.Config.KBSelectionMode)
|
||||
if c.Temperature != 0 {
|
||||
llm = append(llm, row{"Temperature", fmt.Sprintf("%g", c.Temperature)})
|
||||
}
|
||||
if len(a.Config.KnowledgeBases) > 0 {
|
||||
fmt.Fprintf(w, " Knowledge bases: %v\n", a.Config.KnowledgeBases)
|
||||
if c.MaxCompletionTokens != 0 {
|
||||
llm = append(llm, row{"Max completion tokens", fmt.Sprintf("%d", c.MaxCompletionTokens)})
|
||||
}
|
||||
if len(a.Config.AllowedTools) > 0 {
|
||||
fmt.Fprintf(w, " Allowed tools: %v\n", a.Config.AllowedTools)
|
||||
if c.MaxIterations != 0 {
|
||||
llm = append(llm, row{"Max iterations", fmt.Sprintf("%d", c.MaxIterations)})
|
||||
}
|
||||
if a.Config.WebSearchEnabled {
|
||||
fmt.Fprintln(w, " Web search: enabled")
|
||||
if c.AgentMode != "" {
|
||||
llm = append(llm, row{"Mode", c.AgentMode})
|
||||
}
|
||||
emit("LLM", llm)
|
||||
|
||||
// KB attachment
|
||||
kb := []row{}
|
||||
if c.KBSelectionMode != "" {
|
||||
kb = append(kb, row{"KB selection mode", c.KBSelectionMode})
|
||||
}
|
||||
if len(c.KnowledgeBases) > 0 {
|
||||
kb = append(kb, row{"Knowledge bases", strings.Join(c.KnowledgeBases, ", ")})
|
||||
}
|
||||
emit("KB attachment", kb)
|
||||
|
||||
// Retrieval
|
||||
retr := []row{}
|
||||
if c.EmbeddingTopK != 0 {
|
||||
retr = append(retr, row{"Embedding top K", fmt.Sprintf("%d", c.EmbeddingTopK)})
|
||||
}
|
||||
if c.KeywordThreshold != 0 {
|
||||
retr = append(retr, row{"Keyword threshold", fmt.Sprintf("%g", c.KeywordThreshold)})
|
||||
}
|
||||
if c.VectorThreshold != 0 {
|
||||
retr = append(retr, row{"Vector threshold", fmt.Sprintf("%g", c.VectorThreshold)})
|
||||
}
|
||||
if c.RerankTopK != 0 {
|
||||
retr = append(retr, row{"Rerank top K", fmt.Sprintf("%d", c.RerankTopK)})
|
||||
}
|
||||
if c.RerankThreshold != 0 {
|
||||
retr = append(retr, row{"Rerank threshold", fmt.Sprintf("%g", c.RerankThreshold)})
|
||||
}
|
||||
emit("Retrieval", retr)
|
||||
|
||||
// Query rewrite
|
||||
qr := []row{}
|
||||
if c.EnableQueryExpansion {
|
||||
qr = append(qr, row{"Query expansion", "enabled"})
|
||||
}
|
||||
if c.EnableRewrite {
|
||||
qr = append(qr, row{"Rewrite", "enabled"})
|
||||
}
|
||||
if c.QueryUnderstandModelID != "" {
|
||||
qr = append(qr, row{"Query understand model ID", c.QueryUnderstandModelID})
|
||||
}
|
||||
if c.RewritePromptSystem != "" {
|
||||
qr = append(qr, row{"Rewrite prompt (system)", truncate1Line(c.RewritePromptSystem)})
|
||||
}
|
||||
if c.RewritePromptUser != "" {
|
||||
qr = append(qr, row{"Rewrite prompt (user)", truncate1Line(c.RewritePromptUser)})
|
||||
}
|
||||
emit("Query rewrite", qr)
|
||||
|
||||
// Tools
|
||||
tools := []row{}
|
||||
if len(c.AllowedTools) > 0 {
|
||||
tools = append(tools, row{"Allowed tools", strings.Join(c.AllowedTools, ", ")})
|
||||
}
|
||||
if c.MCPSelectionMode != "" {
|
||||
tools = append(tools, row{"MCP selection mode", c.MCPSelectionMode})
|
||||
}
|
||||
if len(c.MCPServices) > 0 {
|
||||
tools = append(tools, row{"MCP services", strings.Join(c.MCPServices, ", ")})
|
||||
}
|
||||
if len(c.SupportedFileTypes) > 0 {
|
||||
tools = append(tools, row{"Supported file types", strings.Join(c.SupportedFileTypes, ", ")})
|
||||
}
|
||||
emit("Tools", tools)
|
||||
|
||||
// FAQ
|
||||
faq := []row{}
|
||||
if c.FAQPriorityEnabled {
|
||||
faq = append(faq, row{"FAQ priority", "enabled"})
|
||||
}
|
||||
if c.FAQDirectAnswerThreshold != 0 {
|
||||
faq = append(faq, row{"FAQ direct-answer threshold", fmt.Sprintf("%g", c.FAQDirectAnswerThreshold)})
|
||||
}
|
||||
if c.FAQScoreBoost != 0 {
|
||||
faq = append(faq, row{"FAQ score boost", fmt.Sprintf("%g", c.FAQScoreBoost)})
|
||||
}
|
||||
emit("FAQ", faq)
|
||||
|
||||
// Web search
|
||||
web := []row{}
|
||||
if c.WebSearchEnabled {
|
||||
web = append(web, row{"Web search", "enabled"})
|
||||
}
|
||||
if c.WebSearchMaxResults != 0 {
|
||||
web = append(web, row{"Web search max results", fmt.Sprintf("%d", c.WebSearchMaxResults)})
|
||||
}
|
||||
emit("Web search", web)
|
||||
|
||||
// Multi-turn
|
||||
mt := []row{}
|
||||
if c.MultiTurnEnabled {
|
||||
mt = append(mt, row{"Multi-turn", "enabled"})
|
||||
}
|
||||
if c.HistoryTurns != 0 {
|
||||
mt = append(mt, row{"History turns", fmt.Sprintf("%d", c.HistoryTurns)})
|
||||
}
|
||||
emit("Multi-turn", mt)
|
||||
|
||||
// Fallback
|
||||
fb := []row{}
|
||||
if c.FallbackStrategy != "" {
|
||||
fb = append(fb, row{"Strategy", c.FallbackStrategy})
|
||||
}
|
||||
if c.FallbackResponse != "" {
|
||||
fb = append(fb, row{"Response", c.FallbackResponse})
|
||||
}
|
||||
if c.FallbackPrompt != "" {
|
||||
fb = append(fb, row{"Prompt", truncate1Line(c.FallbackPrompt)})
|
||||
}
|
||||
emit("Fallback", fb)
|
||||
|
||||
// Templates — system_prompt and context_template can be multi-line;
|
||||
// render headed blocks rather than KV rows for readability.
|
||||
if c.SystemPrompt != "" || c.ContextTemplate != "" {
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintln(w, "Templates:")
|
||||
if c.SystemPrompt != "" {
|
||||
fmt.Fprintln(w, " System prompt:")
|
||||
writeIndented(w, c.SystemPrompt, " ")
|
||||
}
|
||||
if c.ContextTemplate != "" {
|
||||
fmt.Fprintln(w, " Context template:")
|
||||
writeIndented(w, c.ContextTemplate, " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// truncate1Line collapses newlines and clips long values for inline KV
|
||||
// rows. Templates section gets full multi-line treatment instead.
|
||||
func truncate1Line(s string) string {
|
||||
s = strings.ReplaceAll(s, "\n", " ")
|
||||
s = strings.ReplaceAll(s, "\r", " ")
|
||||
const max = 80
|
||||
if len(s) > max {
|
||||
return s[:max-3] + "..."
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// writeIndented prints s with the given prefix on every line. Trailing
|
||||
// newline always added so the next section starts on its own line.
|
||||
func writeIndented(w io.Writer, s, prefix string) {
|
||||
for _, line := range strings.Split(strings.TrimRight(s, "\n"), "\n") {
|
||||
fmt.Fprintf(w, "%s%s\n", prefix, line)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user