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
@@ -180,6 +180,22 @@ func identToErrorCode(name string) (cmdutil.ErrorCode, bool) {
return cmdutil.CodeLocalUnimplemented, true
case "CodeLocalContextNotFound":
return cmdutil.CodeLocalContextNotFound, true
case "CodeKBIDRequired":
return cmdutil.CodeKBIDRequired, true
case "CodeKBNotFound":
return cmdutil.CodeKBNotFound, true
case "CodeProjectAlreadyLinked":
return cmdutil.CodeProjectAlreadyLinked, true
case "CodeProjectLinkCorrupt":
return cmdutil.CodeProjectLinkCorrupt, true
case "CodeUserAborted":
return cmdutil.CodeUserAborted, true
case "CodeUploadFileNotFound":
return cmdutil.CodeUploadFileNotFound, true
case "CodeSSEStreamAborted":
return cmdutil.CodeSSEStreamAborted, true
case "CodeSessionCreateFailed":
return cmdutil.CodeSessionCreateFailed, true
case "CodeMCPReadonlyMode":
return cmdutil.CodeMCPReadonlyMode, true
case "CodeMCPToolNotAllowed":