SetAgentHelp attaches agent-targeted help metadata to a command. Routing: - WEKNORA_AGENT_HELP=1: emit the AgentHelp JSON blob to stdout and return — agents get clean parseable JSON, no trailing prose. - Otherwise (human help path): if cmd has risk annotations from SetRisk, prepend "Risk:
(cmd *cobra.Command, ah AgentHelp)
| 40 | // prepend "Risk: <action> (<level>)" line; then render the normal human |
| 41 | // help via origHelp; then append the "AI agents:" Warnings block. |
| 42 | func SetAgentHelp(cmd *cobra.Command, ah AgentHelp) { |
| 43 | origHelp := cmd.HelpFunc() |
| 44 | cmd.SetHelpFunc(func(c *cobra.Command, args []string) { |
| 45 | if os.Getenv("WEKNORA_AGENT_HELP") == "1" { |
| 46 | emitAgentHelp(c.OutOrStdout(), ah) |
| 47 | return |
| 48 | } |
| 49 | // Prepend Risk: line in the default (human) branch only; the |
| 50 | // JSON branch above already carries warnings[]. Skip silently if |
| 51 | // the command has no risk annotations. |
| 52 | if level, action, ok := GetRisk(c); ok { |
| 53 | fmt.Fprintf(c.OutOrStdout(), "Risk: %s (%s)\n\n", action, level) |
| 54 | } |
| 55 | origHelp(c, args) |
| 56 | if len(ah.Warnings) > 0 { |
| 57 | w := c.OutOrStdout() |
| 58 | fmt.Fprintln(w) |
| 59 | fmt.Fprintln(w, "AI agents:") |
| 60 | for _, msg := range ah.Warnings { |
| 61 | fmt.Fprintln(w, "- "+msg) |
| 62 | } |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | func emitAgentHelp(w io.Writer, ah AgentHelp) { |
| 68 | enc := json.NewEncoder(w) |