Skip to content

implemented command history#613

Merged
dgageot merged 4 commits intodocker:mainfrom
riturajFi:feat/command_history_in_tui
Oct 29, 2025
Merged

implemented command history#613
dgageot merged 4 commits intodocker:mainfrom
riturajFi:feat/command_history_in_tui

Conversation

@riturajFi
Copy link
Copy Markdown
Contributor

@riturajFi riturajFi commented Oct 28, 2025

✨ Summary

This PR implements command/message history support for the TUI chat editor.
Users can now recall and reuse previously sent messages using the ↑ / ↓ arrow keys, similar to terminal shells or chat clients like Slack.

This addition enhances usability and continuity by allowing users to:

  • Navigate through previously sent messages.
  • Edit or resend older messages easily.
  • Retain command history across application restarts.

Fixes: #606


🚀 Features Added

1. 🔁 Persistent Message History

  • Integrated the shared history.History module (pkg/history) into the TUI editor.
  • All sent messages are now stored in a persistent history file (e.g., ~/.config/cagent/history.json).
  • Message history is automatically reloaded between sessions, ensuring continuity across app restarts.
  • Deduplication logic prevents storing consecutive identical messages.

2. ⬆️⬇️ Keyboard Navigation

  • Implemented Up/Down arrow navigation inside the editor:

    • – Loads the previous message.
    • – Moves forward through history or restores the current draft when reaching the latest point.
  • Navigation behavior:

    • Works only for single-line inputs to avoid interrupting multi-line editing.
    • Automatically exits browsing mode when typing resumes.
    • Keeps the cursor at the end of the recalled message for immediate editing.

3. 🧠 Draft Preservation

  • When users start navigating through history, their current unfinished input (draft) is temporarily saved.
  • If they navigate forward to the end of history, their draft is restored — preventing accidental data loss.

4. ⚙️ Chat Page Integration

  • The chatPage now initializes and shares a single History instance with the Editor.
  • On each message send (SendMsg), the message content is appended to history before being processed by the runtime.
  • Ensures both components operate with a single source of truth for command history.

5. 💬 UX Improvements & Reliability

  • Smooth no-op behavior when history is empty or unavailable.
  • Editor remains fully functional even if history initialization fails.
  • Maintains all previous keyboard behavior (Enter → send message, Ctrl+C → quit).

🔒 Error Handling & Safety

  • Gracefully handles missing or corrupted history files.
  • Avoids panics and fatal dependencies.
  • Operates in degraded mode (no history) if initialization fails.

Result:
Users get a familiar and efficient workflow for recalling and reusing past commands — improving productivity and chat experience within the TUI.

@riturajFi riturajFi requested a review from a team as a code owner October 28, 2025 04:50
@riturajFi
Copy link
Copy Markdown
Contributor Author

@dgageot kindly review

@dgageot
Copy link
Copy Markdown
Member

dgageot commented Oct 29, 2025

@riturajFi I'm not sure I understand the code. It seems to me that it could be simpler. Also, it seems to store the same messages twice in the history. I think it's better if we can avoid that.

@riturajFi
Copy link
Copy Markdown
Contributor Author

Thanks @dgageot , doing changes right aw3

@riturajFi riturajFi force-pushed the feat/command_history_in_tui branch from c0a67b1 to 50b9206 Compare October 29, 2025 16:23
@riturajFi
Copy link
Copy Markdown
Contributor Author

riturajFi commented Oct 29, 2025

Hi @dgageot,
I’ve added comments throughout my solution to improve readability and clarity.
I’ve kept the code intentionally simple, but I’d appreciate your feedback on whether the structure makes sense architecturally.

A few notes:

  1. The add-to-history logic currently resides in the chat page, though it could be moved into the editor for better encapsulation if you think that’s cleaner.
  2. I verified that there’s no duplicate message saving issue — checked the history.json file to confirm:
    riturajtripathy@riturajtripathy-IdeaPad-Slim-3-15ABR8-1: cat /home/riturajtripathy/.cagent/history.json
     {"messages":["hi","ji"]}
    

Let me know your thoughts or if you’d prefer a different architectural direction.

@dgageot dgageot merged commit 2678580 into docker:main Oct 29, 2025
5 checks passed
@rumpl
Copy link
Copy Markdown
Member

rumpl commented Oct 29, 2025

Just coming back to say: Thank you!

I spent the whole evening using this all the time, such a great little feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement command history in the TUI

3 participants