feat(cli)!: add agent-first chat and session ask output modes
This commit is contained in:
@@ -1,37 +1,36 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
import sdk "github.com/Tencent/WeKnora/client"
|
||||
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
// ReferenceIndex is the bounded citation pointer exposed by projected JSON,
|
||||
// text, and MCP output. ChunkID is the chunk the model cited; ParentChunkID is
|
||||
// retained when callers prefer to fetch the larger self-contained passage.
|
||||
// Full chunk text and retrieval metadata remain available only in raw NDJSON.
|
||||
type ReferenceIndex struct {
|
||||
KBID string `json:"kb_id,omitempty"`
|
||||
ChunkID string `json:"chunk_id"`
|
||||
ParentChunkID string `json:"parent_chunk_id,omitempty"`
|
||||
}
|
||||
|
||||
// WriteReferences renders the compact references footer used by chat and
|
||||
// agent invoke: a horizontal rule, one numbered line per reference,
|
||||
// best-available title + optional score. Skipped entirely when refs is
|
||||
// empty - agent-friendly silence beats an empty banner.
|
||||
func WriteReferences(w io.Writer, refs []*sdk.SearchResult) {
|
||||
if len(refs) == 0 {
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintln(w, "──── References ────")
|
||||
for i, r := range refs {
|
||||
if r == nil {
|
||||
// IndexReferences projects full SDK search results into stable lookup keys.
|
||||
// It never mutates the SDK events, which keeps the raw NDJSON path lossless.
|
||||
// fallbackKBID is used by `chat`, whose single KB is known by the CLI even
|
||||
// when an older server omits knowledge_base_id from a reference.
|
||||
func IndexReferences(refs []*sdk.SearchResult, fallbackKBID string) []ReferenceIndex {
|
||||
indexes := make([]ReferenceIndex, 0, len(refs))
|
||||
for _, r := range refs {
|
||||
if r == nil || r.ID == "" {
|
||||
continue
|
||||
}
|
||||
title := r.KnowledgeTitle
|
||||
if title == "" {
|
||||
title = r.KnowledgeFilename
|
||||
kbID := r.KnowledgeBaseID
|
||||
if kbID == "" {
|
||||
kbID = fallbackKBID
|
||||
}
|
||||
if title == "" {
|
||||
title = r.KnowledgeID
|
||||
}
|
||||
fmt.Fprintf(w, "[%d] %s", i+1, title)
|
||||
if r.Score > 0 {
|
||||
fmt.Fprintf(w, " score=%.3f", r.Score)
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
indexes = append(indexes, ReferenceIndex{
|
||||
KBID: kbID,
|
||||
ChunkID: r.ID,
|
||||
ParentChunkID: r.ParentChunkID,
|
||||
})
|
||||
}
|
||||
return indexes
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package format
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
func TestIndexReferences_ProjectsLookupFieldsWithoutMutation(t *testing.T) {
|
||||
refs := []*sdk.SearchResult{
|
||||
{ID: "c1", Content: "bulky passage one", KnowledgeBaseID: "kb1", ParentChunkID: "p1", KnowledgeTitle: "Doc One", Score: 0.5},
|
||||
{ID: "c2", Content: "bulky passage two", ParentChunkID: "p2"},
|
||||
nil,
|
||||
}
|
||||
got := IndexReferences(refs, "fallback")
|
||||
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("indexes=%d, want 2", len(got))
|
||||
}
|
||||
if got[0].KBID != "kb1" || got[0].ChunkID != "c1" || got[0].ParentChunkID != "p1" {
|
||||
t.Errorf("first index=%+v", got[0])
|
||||
}
|
||||
if got[1].KBID != "fallback" || got[1].ChunkID != "c2" {
|
||||
t.Errorf("fallback index=%+v", got[1])
|
||||
}
|
||||
if refs[0].Content != "bulky passage one" || refs[1].Content != "bulky passage two" {
|
||||
t.Errorf("source references mutated: %+v", refs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexReferences_NilSafe(t *testing.T) {
|
||||
if got := IndexReferences(nil, ""); len(got) != 0 {
|
||||
t.Errorf("nil indexes=%+v", got)
|
||||
}
|
||||
if got := IndexReferences([]*sdk.SearchResult{nil, nil}, ""); len(got) != 0 {
|
||||
t.Errorf("nil-entry indexes=%+v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user