feat(cli): agent-first WeKnora CLI

The `weknora` command-line client for the WeKnora RAG server. Full command
surface: kb / doc / chunk / session / message / agent / model / search / chat /
auth / profile / link / api / mcp / skills / config / doctor / schema /
exit-codes.

Agent-first wire contract: JSON envelope by default, typed error codes mapped to
stable exit codes, error.retry_argv / retryable / partial-failure status,
global --dry-run preview, the exit-10 confirmation protocol for writes, and
control/ANSI/Bidi input hygiene. Stateless env-credential auth
(WEKNORA_API_KEY / WEKNORA_TOKEN + WEKNORA_HOST) for headless/CI/agent use.
A curated read-only MCP tool surface (`mcp serve`) and embedded Agent Skills
(`skills install`). Extensive unit + contract-golden tests and CI.
This commit is contained in:
nullkey
2026-06-26 20:40:59 +08:00
committed by lyingbug
parent 445a4330fa
commit 6f8b825f5f
151 changed files with 7305 additions and 733 deletions
+30 -25
View File
@@ -40,7 +40,7 @@ func TestWriteEnvelope_OmitDataWhenNil(t *testing.T) {
func TestWriteEnvelope_WithMeta(t *testing.T) {
var buf bytes.Buffer
meta := &output.Meta{Count: 2, HasMore: false}
meta := &output.Meta{Count: output.IntPtr(2), HasMore: false}
if err := output.WriteEnvelope(&buf, []string{"a", "b"}, meta, false, ""); err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -51,13 +51,36 @@ func TestWriteEnvelope_WithMeta(t *testing.T) {
}
}
// TestWriteEnvelope_ZeroCountSerializes pins the *int fix: a list command that
// sets Count to 0 (empty result) must still emit "count":0, while a meta that
// leaves Count nil (non-list / dry-run) must omit the key entirely. This is the
// agent-contract guarantee that omitempty on a plain int silently broke.
func TestWriteEnvelope_ZeroCountSerializes(t *testing.T) {
var bufZero bytes.Buffer
zero := &output.Meta{Count: output.IntPtr(0)}
if err := output.WriteEnvelope(&bufZero, []string{}, zero, false, ""); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotZero := bufZero.String(); !strings.Contains(gotZero, `"count":0`) {
t.Errorf("explicit zero count must serialize; got %q", gotZero)
}
var bufNil bytes.Buffer
if err := output.WriteEnvelope(&bufNil, []string{}, &output.Meta{}, false, ""); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotNil := bufNil.String(); strings.Contains(gotNil, "count") {
t.Errorf("nil count must be omitted; got %q", gotNil)
}
}
func TestWriteErrorEnvelope_FullShape(t *testing.T) {
var buf bytes.Buffer
errDetail := &output.ErrDetail{
Type: "input.confirmation_required",
Message: "kb delete kb_x requires confirmation",
Hint: "re-run with -y/--yes",
RetryCommand: "weknora kb delete kb_x -y",
Type: "input.confirmation_required",
Message: "kb delete kb_x requires confirmation",
Hint: "re-run with -y/--yes",
RetryArgv: []string{"weknora", "kb", "delete", "kb_x", "-y"},
Risk: &output.RiskDetail{
Level: "destructive",
Action: "kb.delete",
@@ -73,8 +96,8 @@ func TestWriteErrorEnvelope_FullShape(t *testing.T) {
if !strings.Contains(got, `"type":"input.confirmation_required"`) {
t.Errorf("missing typed code; got %q", got)
}
if !strings.Contains(got, `"retry_command":"weknora kb delete kb_x -y"`) {
t.Errorf("missing retry_command; got %q", got)
if !strings.Contains(got, `"retry_argv":["weknora","kb","delete","kb_x","-y"]`) {
t.Errorf("missing retry_argv; got %q", got)
}
if !strings.Contains(got, `"risk":{"level":"destructive","action":"kb.delete"}`) {
t.Errorf("missing risk; got %q", got)
@@ -91,21 +114,3 @@ func TestWriteEnvelope_IndentedTTYMode(t *testing.T) {
t.Errorf("expected indented multi-line output; got %q", got)
}
}
func TestGetNotice_NilSafe(t *testing.T) {
output.PendingNotice = nil
if got := output.GetNotice(); got != nil {
t.Errorf("GetNotice with nil PendingNotice should return nil; got %v", got)
}
}
func TestGetNotice_WithSetter(t *testing.T) {
output.PendingNotice = func() map[string]any {
return map[string]any{"deprecation": "foo is deprecated"}
}
defer func() { output.PendingNotice = nil }()
got := output.GetNotice()
if got["deprecation"] != "foo is deprecated" {
t.Errorf("notice not populated; got %v", got)
}
}