e236be1ced
Three defects surfaced during end-to-end RAG verification — the first two
block real chat usage, the third makes Linux CI flaky:
1. KB id detection — `IsKBID` was checking
`strings.HasPrefix(s, "kb_")`, but WeKnora generates KB ids as bare
UUIDs (internal/types/knowledge_base.go: `uuid.New().String()` stored
in a `varchar(36)` column). Real ids therefore fell through to the
name-resolution path:
$ weknora chat ... --kb a32a63ff-fb36-4874-bcaa-30f48570a694
Error: knowledge base not found: a32a63ff-...
Switched the discriminator to a UUID regex
(`^[0-9a-fA-F]{8}-…-[0-9a-fA-F]{12}$`). KB names are arbitrary
user-supplied strings, so the canonical 8-4-4-4-12 form is an
unambiguous signal. Mirrors gcloud `--project`'s id-vs-name detection.
2. SSE terminal-frame — the accumulator's `Append` was gating
finalization on `r.Done`, but the server's KnowledgeQAStream protocol
emits a leading `agent_query` frame with `done=true` to deliver
session + message metadata *before* the answer fragments arrive:
event: message
data: {"response_type":"agent_query","content":"","done":true,…}
event: message
data: {"response_type":"answer","content":"你好","done":false}
…
event: message
data: {"response_type":"complete","content":"","done":true}
The accumulator therefore flipped to `finished=true` on frame #1 and
discarded every subsequent answer fragment — `weknora chat … --json`
returned `answer: ""` even though the LLM reported completion_tokens
> 0. Fixed: terminate only on `response_type == complete`.
References still captured opportunistically (they may arrive on a
dedicated `references` event before the terminator).
3. doctor credential_storage CI isolation — the check probes the real
OS keyring via `secrets.NewBestEffortStore()`: present on macOS dev
machines → StatusOK; absent on Linux CI runners without libsecret /
Gnome-Keyring → StatusWarn ("falling back to file store"). That
host-dependence was leaking into two test classes that assumed
StatusOK:
* cmd/doctor/doctor_test.go: TestDoctor_AllOK and
TestDoctor_NoConfig_StillRunsCredentialStorage already had a
withCredStoreFactory seam but didn't use it. Added the pin.
* acceptance/contract/envelope_test.go: doctor.success_offline
and doctor.error_network golden cases. The contract test runs
through the cobra tree in-process and shares cmd/doctor's
package-level credStoreFactory var — but couldn't reach it
because the existing seam was unexported.
Fix: export `doctor.SetCredStoreFactoryForTest(fn) (restore func())`
for out-of-package tests; acceptance/contract/helpers_test.go adds
a TestMain that pins the factory to a MemStore-returning closure
for the whole suite (MemStore is neither *FileStore nor a real
keyring, so doctor's type-switch hits StatusOK). Production stays
at secrets.NewBestEffortStore — only the test hook is now reachable
from across packages.
Test fixtures and goldens that used the old `kb_xxx` literals or
`Done: true` terminators were rewritten to use real UUIDs and
`ResponseType: ResponseTypeComplete` respectively. Per-command --help
text and Long descriptions / Examples now show a UUID rather than
`kb_…` so users see the correct shape from the start. New
TestAccumulator_IgnoresAgentQueryDone pins the SSE terminator bug so
it can't regress.
Tests: 24 cli packages green on macOS dev + Linux/macOS/Windows CI
matrix. Verified end-to-end against a live WeKnora server: `weknora
chat "..." --kb <UUID> --no-stream --json` returns the full LLM answer
in the envelope, live token streaming in TTY mode works, and the
credential_storage check renders deterministic envelopes across hosts.
168 lines
5.9 KiB
Go
168 lines
5.9 KiB
Go
// Package search implements the top-level `weknora search` command — the
|
|
// chunk hybrid retrieval entry point (ADR-3: only one search command).
|
|
package search
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
|
|
"github.com/Tencent/WeKnora/cli/internal/format"
|
|
"github.com/Tencent/WeKnora/cli/internal/iostreams"
|
|
sdk "github.com/Tencent/WeKnora/client"
|
|
)
|
|
|
|
// Options is the runtime configuration of one search invocation.
|
|
type Options struct {
|
|
Query string
|
|
KB string // raw --kb flag value (UUID or name)
|
|
KBID string // resolved id, populated by RunE before HybridSearch
|
|
TopK int
|
|
VectorThreshold float64
|
|
KeywordThreshold float64
|
|
NoVector bool
|
|
NoKeyword bool
|
|
JSONOut bool
|
|
}
|
|
|
|
// Service is the narrow SDK surface used by runSearch. *sdk.Client satisfies
|
|
// it; tests inject fakes via Factory.Client.
|
|
type Service interface {
|
|
HybridSearch(ctx context.Context, kbID string, params *sdk.SearchParams) ([]*sdk.SearchResult, error)
|
|
}
|
|
|
|
// NewCmdSearch builds `weknora search "<query>" --kb <id-or-name>`.
|
|
//
|
|
// The single `--kb` flag accepts either a KB UUID (passed through) or a
|
|
// name (resolved via ListKnowledgeBases). Mirrors gcloud `--project`'s
|
|
// id-or-name auto-detection — the only mainstream pattern that collapses
|
|
// the two forms onto one flag.
|
|
func NewCmdSearch(f *cmdutil.Factory) *cobra.Command {
|
|
opts := &Options{}
|
|
cmd := &cobra.Command{
|
|
Use: `search "<query>"`,
|
|
Short: "Hybrid (vector + keyword) chunk retrieval against a knowledge base",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(c *cobra.Command, args []string) error {
|
|
opts.Query = strings.TrimSpace(args[0])
|
|
// Validate input shape before touching the SDK so flag/arg misuse
|
|
// surfaces fast (no auth / no client construction). KB resolution
|
|
// happens *after* — name → id needs a live client.
|
|
if err := opts.validate(); err != nil {
|
|
return err
|
|
}
|
|
cli, err := f.Client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if cmdutil.IsKBID(opts.KB) {
|
|
opts.KBID = opts.KB
|
|
} else {
|
|
resolved, rerr := cmdutil.ResolveKBNameToID(c.Context(), cli, opts.KB)
|
|
if rerr != nil {
|
|
return rerr
|
|
}
|
|
opts.KBID = resolved
|
|
}
|
|
return runSearch(c.Context(), opts, cli)
|
|
},
|
|
}
|
|
cmd.Flags().StringVar(&opts.KB, "kb", "", "Knowledge base UUID or name")
|
|
cmd.Flags().IntVar(&opts.TopK, "top-k", 8, "Maximum results to return")
|
|
cmd.Flags().Float64Var(&opts.VectorThreshold, "vector-threshold", 0, "Vector retrieval similarity floor (per-channel, pre-fusion); 0 = no filter")
|
|
cmd.Flags().Float64Var(&opts.KeywordThreshold, "keyword-threshold", 0, "Keyword retrieval score floor (per-channel, pre-fusion); 0 = no filter")
|
|
cmd.Flags().BoolVar(&opts.NoVector, "no-vector", false, "Disable the vector channel")
|
|
cmd.Flags().BoolVar(&opts.NoKeyword, "no-keyword", false, "Disable the keyword channel")
|
|
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
|
_ = cmd.MarkFlagRequired("kb")
|
|
return cmd
|
|
}
|
|
|
|
// validate checks the option set before any SDK call. RunE invokes it before
|
|
// f.Client() so flag misuse fails fast; runSearch re-invokes it as a safety
|
|
// net for direct test calls that bypass RunE.
|
|
func (o *Options) validate() error {
|
|
if o.Query == "" {
|
|
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument, "query argument cannot be empty")
|
|
}
|
|
if o.NoVector && o.NoKeyword {
|
|
return cmdutil.NewError(cmdutil.CodeInputInvalidArgument, "--no-vector and --no-keyword cannot both be set")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runSearch(ctx context.Context, opts *Options, svc Service) error {
|
|
if err := opts.validate(); err != nil {
|
|
return err
|
|
}
|
|
if svc == nil {
|
|
return cmdutil.NewError(cmdutil.CodeServerError, "search: no SDK client available")
|
|
}
|
|
|
|
params := &sdk.SearchParams{
|
|
QueryText: opts.Query,
|
|
MatchCount: opts.TopK,
|
|
VectorThreshold: opts.VectorThreshold,
|
|
KeywordThreshold: opts.KeywordThreshold,
|
|
DisableVectorMatch: opts.NoVector,
|
|
DisableKeywordsMatch: opts.NoKeyword,
|
|
}
|
|
results, err := svc.HybridSearch(ctx, opts.KBID, params)
|
|
if err != nil {
|
|
return cmdutil.Wrapf(cmdutil.ClassifyHTTPError(err), err, "hybrid search")
|
|
}
|
|
// match_count is the server's *primary-match* cap — after that, the
|
|
// service appends parent / nearby / relation chunks as context
|
|
// enrichment, so the wire response can exceed TopK. CLIs like gh /
|
|
// kubectl / aws treat their `--limit`-style flag as a hard return-count
|
|
// cap; honor that contract by trimming on the client. Recall isn't
|
|
// affected because the server's internal retrieval pool is already
|
|
// max(MatchCount*5, 50).
|
|
if opts.TopK > 0 && len(results) > opts.TopK {
|
|
results = results[:opts.TopK]
|
|
}
|
|
|
|
if opts.JSONOut {
|
|
return cmdutil.NewJSONExporter().Write(iostreams.IO.Out, format.Success(results, &format.Meta{
|
|
KBID: opts.KBID,
|
|
}))
|
|
}
|
|
return renderHumanResults(results, opts.KBID)
|
|
}
|
|
|
|
// renderHumanResults prints a compact pretty list to stdout. The inline
|
|
// indent helper is a minimal stopgap so search output is usable in a plain
|
|
// terminal; a richer tabular renderer can replace this later.
|
|
func renderHumanResults(results []*sdk.SearchResult, kbID string) error {
|
|
if len(results) == 0 {
|
|
fmt.Fprintln(iostreams.IO.Out, "(no results)")
|
|
return nil
|
|
}
|
|
fmt.Fprintf(iostreams.IO.Out, "%d result(s) from kb=%s:\n\n", len(results), kbID)
|
|
for i, r := range results {
|
|
fmt.Fprintf(iostreams.IO.Out, "[%d] score=%.3f", i+1, r.Score)
|
|
if r.KnowledgeID != "" {
|
|
fmt.Fprintf(iostreams.IO.Out, " doc=%s", r.KnowledgeID)
|
|
}
|
|
fmt.Fprintln(iostreams.IO.Out)
|
|
fmt.Fprintln(iostreams.IO.Out, indent(strings.TrimSpace(r.Content), " "))
|
|
fmt.Fprintln(iostreams.IO.Out)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// indent prefixes each line of s with the given prefix.
|
|
func indent(s, prefix string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
lines := strings.Split(s, "\n")
|
|
for i, l := range lines {
|
|
lines[i] = prefix + l
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|