feat(cli): add project-link foundation with init and link commands

v0.2 grounds the CLI's resource commands (kb / doc / chat / query) in a
per-project link file (.weknora/project.yaml) so users don't have to pass
--kb-id on every invocation. Mirrors npm/cargo/git: walk up the cwd tree
to find the project root, override via flag or env when needed.

This commit ships the foundation layer:

  - cli/internal/projectlink/  Discover (walk-up, depth=64) / Load / Save
  - cmdutil.Factory.ResolveKB  5-level fallback chain:
      --kb-id flag → --kb name (ListKnowledgeBases lookup) →
      WEKNORA_KB_ID env → walk-up project link → CodeKBIDRequired
  - cmdutil.ResolveKBNameToID   shared name→id helper used by init / link
                                / Factory.ResolveKB (was duplicated 3 ways
                                in early implementation; reuse review #2)
  - cli/cmd/init/               interactive (huh prompt) or flag-driven
                                first-time setup; refuses to overwrite
                                without --force
  - cli/cmd/link/               non-interactive update; --kb-id and --kb
                                are mutually exclusive and one is required

Also registers the v0.2 ErrorCode set (all codes for the eight new
commands) and AST-scan identToErrorCode mapping in one place — keeps the
acceptance/contract suite green across the v0.2 commit chain even before
later commits reference each code.
This commit is contained in:
nullkey
2026-05-11 02:43:55 +08:00
committed by lyingbug
parent fc694d5d21
commit 19afd5eed9
13 changed files with 1141 additions and 17 deletions
+16
View File
@@ -151,6 +151,22 @@ func defaultHint(code ErrorCode) string {
return "remove ~/.config/weknora/config.yaml and re-run `weknora auth login`"
case CodeLocalFileIO:
return "check file permissions under $XDG_CONFIG_HOME/weknora/"
case CodeKBIDRequired:
return "run `weknora init` to link a knowledge base, or pass --kb-id"
case CodeKBNotFound:
return "list available with `weknora kb list`"
case CodeProjectAlreadyLinked:
return "use --force to overwrite, or `weknora link` to update"
case CodeProjectLinkCorrupt:
return "remove .weknora/project.yaml and run `weknora init` again"
case CodeUserAborted:
return "no action taken; pass --force to skip the confirmation prompt"
case CodeUploadFileNotFound:
return "verify the path is correct and readable"
case CodeSSEStreamAborted:
return "the streaming answer was cut off mid-flight; retry, or pass --no-stream to buffer the full response"
case CodeSessionCreateFailed:
return "could not create a chat session; pass --session-id to reuse an existing session"
}
return ""
}