feat: add interactive command mode, :help/:usage commands, and token usage tracking#747
feat: add interactive command mode, :help/:usage commands, and token usage tracking#747seagochen wants to merge 11 commits intosipeed:mainfrom
Conversation
Add three interactive modes to the CLI agent: - /cmd: switch to command mode for direct shell command execution - /pico: switch back to AI chat mode - /hipico <msg>: invoke AI assistance from within command mode - /byepico: end AI assistance and return to command mode Command mode supports cd with directory tracking, cross-platform shell execution (sh on Unix, PowerShell on Windows), and contextual AI assistance that knows the current working directory. https://claude.ai/code/session_01PoHBfe7eKmhHvVF12W3xZ2
Adds a printHelp() function that outputs detailed usage information covering all three modes (chat, command, AI-assisted), available commands, mode switching, and examples. /help is intercepted before the mode-specific switch block so it works in any mode. https://claude.ai/code/session_01PoHBfe7eKmhHvVF12W3xZ2
Adds token usage tracking to AgentInstance using atomic counters (TotalPromptTokens, TotalCompletionTokens, TotalRequests) that accumulate after each LLM call. The /usage command displays current model name, max tokens, temperature, and session token statistics. Available in all interactive modes like /help. https://claude.ai/code/session_01PoHBfe7eKmhHvVF12W3xZ2
Resolve function name collision between the top-level CLI help and the interactive mode help by giving each a distinct, descriptive name. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…mode - Add :edit command for file viewing and editing in cmd mode (:edit file, :edit file N text, :edit file +N text, :edit file -N, :edit file -m """...""" for multi-line write with auto-create) - Intercept vim/nano/vi/emacs with helpful :edit redirect message - Replace Makefile with scripts/ (build.sh, install.sh, deploy.sh, setup.sh, check.sh, docker.sh) for build, test, deploy, and environment setup - Simplify :hipico to one-shot mode, remove :byepico and modeHiPico - Switch command prefixes from / to : (:cmd, :pico, :hipico, :help) - Add console-like code block formatting and emoji file type indicators for ls output in cmd mode - Update Dockerfile and CI workflow to use scripts/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When using :hipico from cmd mode after cd'ing to a subdirectory, the AI now sees the current working directory in the system prompt instead of just a weak user-message prefix. This ensures the AI resolves file paths relative to the cmd working directory, not the workspace root. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Revert the build system back to Makefile, removing the shell scripts introduced in d9ae60b. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update CI and Docker build to use `make` instead of removed scripts/. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- handleCdCommand: redirect cd, cd ~, cd /, cd /path to workspace directory instead of $HOME or system root for safety - guardCommand: fix path regex false positive that extracted "/exe" from "./exe.sh" and blocked it as absolute path outside workspace Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
nikolasdehor
left a comment
There was a problem hiding this comment.
This is a 1068-line PR that adds interactive command mode, file editing, token tracking, and several UX features. It is doing too many things at once and would benefit from being split into separate PRs (token tracking, command mode, edit command, ls formatting). Several significant issues:
Security Issues:
-
:editbypasses workspace restriction: TheresolveEditPathfunction allows absolute paths (/etc/passwd) and~/expansion to the real home directory (not the workspace). This means from any channel (WhatsApp, Telegram, Discord), a user can read and write arbitrary files on the host. Thecdcommand correctly restricts absolute paths to the workspace, but:editdoes not. -
:edit -mcreates files withMkdirAll: Combined with the above, a user can create arbitrary directories and files anywhere on the filesystem. -
Shell command execution via command mode:
executeCmdModecallsagent.Tools.ExecuteWithContextwhich should go through the ExecTool safety guard, but the command mode concept itself needs careful thought about access control — especially when exposed over multi-user channels like Telegram groups.
Correctness Issues:
-
Global mutable state
cmdWorkingDir: The package-levelvar cmdWorkingDirwith aninit()function is only used by the CLIexecuteShellCommandbut is global state. If the agent loop is used from the gateway (non-CLI), this is unused. More importantly, the per-session working directory inloop.go(viagetSessionWorkDir/setSessionWorkDir) and the globalcmdWorkingDirare two separate tracking mechanisms for the same concept. -
Duplicated logic: The mode switching logic (
interactiveModeandsimpleInteractiveMode) is nearly identical — 100+ lines of duplicated switch/case blocks. Extract a shared handler function. -
:commands intercepted globally:handleCommandnow intercepts ALL messages starting with:across ALL channels. If a Telegram user sends:)or:thinking:, it will return "Unknown command: :)". This breaks normal chat usage. The:prefix should only be recognized in specific contexts (CLI interactive mode), not in the agent loop's global command handler. -
ensureLsLongsilently modifies user commands: Injecting-lintolscommands changes the output format. This is surprising behavior — if someone typesls *.gothey expect filename-only output, not a long listing. The emoji formatting is a nice touch but should be opt-in.
Missing:
-
No tests: 1068 lines of new code with zero test coverage. At minimum,
editLineOp,ensureLsLong,isPermString,resolveEditPath,handleCdCommand, andshortenHomePathshould have unit tests. -
No documentation: The PR adds significant user-facing features but does not update the README or docs.
📝 Description
This PR adds several new interactive features to the PicoClaw CLI agent:
Interactive Command Mode (
:cmd/:pico): Users can now switch between AI chat mode and a direct shell command execution mode. Command mode supportscdwith working directory tracking, cross-platform shell execution (sh on Unix, PowerShell on Windows), and persists the working directory across commands.AI-Assisted Command Mode (
:hipico): From within command mode, users can invoke one-shot AI assistance. The current working directory is injected into the system prompt so the AI resolves paths relative to the cmd working directory.:helpCommand: AprintInteractiveHelp()function that displays detailed usage for all modes and commands, available in any interactive mode.:usageCommand: Displays current model name, max tokens, temperature, and accumulated session token statistics (prompt tokens, completion tokens, total requests).Token Usage Tracking:
AgentInstancenow tracksTotalPromptTokens,TotalCompletionTokens, andTotalRequestsvia atomic counters, accumulated after each LLM call viaAddUsage().Refactoring: Renamed
printHelp→printCliHelp(top-level CLI help) andprintInteractiveHelp(interactive mode help) to eliminate function name collisions.🗣️ Type of Change
🤖 AI Code Generation
🔗 Related Issue
📚 Technical Context (Skip for Docs)
🧪 Test Environment
📸 Evidence (Optional)
Click to view Logs/Screenshots
☑️ Checklist