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:
@@ -0,0 +1,115 @@
|
||||
// Package skills embeds the bundled Agent Skills into the CLI binary so
|
||||
// `weknora skills install` can write them out without a source checkout
|
||||
// (replacing the manual symlink-from-checkout MVP). The markdown under each
|
||||
// skill dir is the single source of truth — embedding it keeps the shipped
|
||||
// binary and the docs in lockstep (the parity tests guard the command/flag
|
||||
// vocabulary the skills reference).
|
||||
package skills
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//go:embed all:weknora-shared all:weknora-rag-search
|
||||
var fsys embed.FS
|
||||
|
||||
// Skill is one bundled skill: its directory name, the description parsed from
|
||||
// its SKILL.md frontmatter, and the relative file paths it contains.
|
||||
type Skill struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Files []string `json:"files"`
|
||||
}
|
||||
|
||||
// FS exposes the embedded skill tree for callers that want raw access.
|
||||
func FS() fs.FS { return fsys }
|
||||
|
||||
// List returns the bundled skills (top-level dirs that contain a SKILL.md),
|
||||
// sorted by name.
|
||||
func List() ([]Skill, error) {
|
||||
entries, err := fs.ReadDir(fsys, ".")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var skills []Skill
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() {
|
||||
continue
|
||||
}
|
||||
files, err := skillFiles(e.Name())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !hasSkillMD(files) {
|
||||
continue
|
||||
}
|
||||
skills = append(skills, Skill{
|
||||
Name: e.Name(),
|
||||
Description: descriptionOf(e.Name()),
|
||||
Files: files,
|
||||
})
|
||||
}
|
||||
sort.Slice(skills, func(i, j int) bool { return skills[i].Name < skills[j].Name })
|
||||
return skills, nil
|
||||
}
|
||||
|
||||
// skillFiles returns every file path under a skill dir (relative to the FS
|
||||
// root, e.g. "weknora-shared/SKILL.md"), sorted.
|
||||
func skillFiles(dir string) ([]string, error) {
|
||||
var files []string
|
||||
err := fs.WalkDir(fsys, dir, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !d.IsDir() {
|
||||
files = append(files, p)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Strings(files)
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func hasSkillMD(files []string) bool {
|
||||
for _, f := range files {
|
||||
if path.Base(f) == "SKILL.md" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// descriptionOf reads the `description:` line from a skill's SKILL.md YAML
|
||||
// frontmatter. Best-effort: returns "" if absent or unreadable.
|
||||
func descriptionOf(dir string) string {
|
||||
b, err := fs.ReadFile(fsys, path.Join(dir, "SKILL.md"))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
inFrontmatter := false
|
||||
for _, line := range strings.Split(string(b), "\n") {
|
||||
t := strings.TrimSpace(line)
|
||||
if t == "---" {
|
||||
if inFrontmatter {
|
||||
break // end of frontmatter
|
||||
}
|
||||
inFrontmatter = true
|
||||
continue
|
||||
}
|
||||
if inFrontmatter && strings.HasPrefix(t, "description:") {
|
||||
return strings.TrimSpace(strings.TrimPrefix(t, "description:"))
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ReadFile returns the bytes of one embedded file path (as reported in
|
||||
// Skill.Files), for `skills install` to write to disk.
|
||||
func ReadFile(p string) ([]byte, error) { return fsys.ReadFile(p) }
|
||||
@@ -51,7 +51,7 @@ Default output is `--format json`: a single envelope.
|
||||
| `ok` | `true`/`false` — branch on this first (see batch/wait caveat below) |
|
||||
| `data` | success payload (object or array; absent on mutation-only success) |
|
||||
| `meta` | `count` / `has_more` / `dry_run` / `plan` … |
|
||||
| `error` | on failure: `{type, message, hint?, retry_command?, risk?}` |
|
||||
| `error` | on failure: `{type, message, hint?, retry_argv?, retryable?, risk?}` |
|
||||
| `profile` | active profile name |
|
||||
|
||||
- `error.type` is a **stable typed code** (e.g. `local.kb_not_found`,
|
||||
@@ -98,14 +98,14 @@ exits **5** (adjust the value, retry). Branch on the exit code to tell them apar
|
||||
|
||||
## 5. Destructive writes (exit 10) — hard rule
|
||||
|
||||
Destructive commands (`kb/doc/chunk/session/agent delete`, `kb/agent edit`,
|
||||
Destructive commands (`kb/doc/chunk/session/agent delete`, `kb/agent update`,
|
||||
`auth logout`, `doc delete --all`, …) without `-y` exit **10** with
|
||||
`error.type = input.confirmation_required` and `error.risk = {level, action}`:
|
||||
|
||||
```jsonc
|
||||
{"ok":false,"error":{"type":"input.confirmation_required",
|
||||
"message":"delete knowledge base X requires explicit confirmation: re-run with -y",
|
||||
"retry_command":"weknora kb delete X -y","risk":{"level":"destructive","action":"kb.delete"}}}
|
||||
"retry_argv":["weknora","kb","delete","X","-y"],"risk":{"level":"destructive","action":"kb.delete"}}}
|
||||
```
|
||||
|
||||
**Surface this to the user and get explicit approval. Re-run with `-y` ONLY
|
||||
@@ -127,14 +127,31 @@ KB=$(weknora kb create "Docs" --jq '.data.id' --format json | tr -d '"')
|
||||
weknora doc upload ./manual.pdf --kb "$KB"
|
||||
```
|
||||
|
||||
## 7a. Reliability patterns for long-running agent runs
|
||||
|
||||
### Inspecting prior messages
|
||||
Use `weknora message list --session <sess-id>` to review the message history of a session (e.g., after a stream drops) before deciding whether to re-ask or continue. Use `weknora message search "<query>"` to locate a prior Q&A exchange across all sessions — prefer this over re-running an expensive query when the answer may already exist.
|
||||
|
||||
### Tool-approval unlock
|
||||
An agent run pauses mid-stream on a tool-approval event when the server requires human sign-off before executing a tool call. The pattern:
|
||||
|
||||
1. The stream emits a tool-approval event; capture the `pending_id`.
|
||||
2. **Surface the pending tool call to the user** (show tool name + proposed args). Do not auto-approve.
|
||||
3. After explicit user go-ahead: `weknora session tool-approval resolve <pending-id> -y` to approve, or add `--reject --reason "..."` to reject.
|
||||
4. Resume the answer: `weknora session continue-stream <sess-id> --message <msg-id>`.
|
||||
|
||||
`--modified-args '{"key":"val"}'` replaces the tool arguments on approve (non-empty JSON object required). This is an exit-10 interaction — see §5.
|
||||
|
||||
## 8. Resource model & command map
|
||||
|
||||
```
|
||||
kb knowledge bases list/view/create/edit/delete/pin/unpin/status/check
|
||||
doc documents in a KB list/view/create/upload/fetch/download/delete/wait
|
||||
kb knowledge bases list/view/create/update/delete/pin/unpin/status/check
|
||||
doc documents in a KB list/view/create/upload/fetch/download/reparse/update/delete/wait
|
||||
chunk retrieval units list/view/delete (RAG debug; not search)
|
||||
session conversations list/view/delete/ask/stop/continue-stream
|
||||
agent custom agents list/view/create/edit/delete/status/check
|
||||
session conversations list/view/delete/ask/stop/continue-stream/tool-approval resolve
|
||||
message session messages list/search/delete
|
||||
agent custom agents list/view/create/update/delete/status/check
|
||||
model configured models list/view (read-only; find the id for agent create --model)
|
||||
search retrieval chunks / docs / kb / sessions
|
||||
chat one-shot KB RAG Q&A (streaming)
|
||||
api raw HTTP passthrough to any server endpoint (escape hatch)
|
||||
@@ -165,7 +182,7 @@ that to learn a command without scraping the human help table.
|
||||
| Mistake | Fix |
|
||||
|---|---|
|
||||
| `weknora auth login` before any profile exists | `profile add <n> --host <url> --use` first (§1) |
|
||||
| Expecting `auth login --host/--name` flags | Gone — host comes from the profile; use global `--profile` |
|
||||
| Passing `--host` or `--name` to `auth login` | `auth login` takes neither — host comes from the profile; use `profile add <n> --host <url> --use`, then `auth login` |
|
||||
| Auto-adding `-y` to clear an exit-10 | Never; get user approval first (§5) |
|
||||
| Need the raw `chat` event stream | pass `--format ndjson`; use `--format text` for a live projected transcript |
|
||||
| `search chunks "q"` → exit 1 `local.kb_id_required` | Pass `--kb <name-or-id>`, set `WEKNORA_KB_ID`, or `weknora link` the dir |
|
||||
|
||||
Reference in New Issue
Block a user