Skip to content

Implement session-based conversation history for PrettyGeminiBot#20

Merged
hiyouga merged 7 commits intomainfrom
copilot/update-gemini-bot-dialogue-logic
Jan 1, 2026
Merged

Implement session-based conversation history for PrettyGeminiBot#20
hiyouga merged 7 commits intomainfrom
copilot/update-gemini-bot-dialogue-logic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 1, 2026

Modify Pretty Gemini Bot Conversation Logic ✅

  • Understand the current implementation and Gemini API chat history
  • Add environment variable for conversation history length
  • Add conversation history array to PrettyGeminiBot class
  • Modify get_response to create new chat session with history for each message
  • Update clear method to clear conversation history
  • Address all code review feedback
  • Add constant for messages per round
  • Improve message validation with explicit None checks
  • Simplify code by removing unnecessary exception handling and checks
  • Test the changes with mock API calls
  • Run code style checks

Summary

Successfully implemented session-based conversation history management for PrettyGeminiBot:

  • ✅ Each user message creates a new chat session with recent conversation history
  • ✅ History length is configurable via PRETTY_GEMINI_BOT_HISTORY_LENGTH environment variable (default: 10 rounds)
  • ✅ History is properly validated and limited to prevent excessive token usage
  • ✅ Non-negative validation ensures history_length >= 0
  • ✅ Messages are validated with explicit None checks before appending
  • ✅ Clear method properly resets conversation history
  • ✅ Code simplified based on review feedback
  • ✅ All tests pass and code meets quality standards

Implementation Details

The bot now maintains conversation context through:

  1. A conversation_history array that stores recent N rounds (configurable)
  2. Creating a new chat session for each message with the recent history
  3. Proper extraction and validation of new messages from the chat history
  4. Using MESSAGES_PER_ROUND constant for maintainability
  5. Simple and clean history slicing without unnecessary complexity
Original prompt

修改Pretty gemini bot的对话创建逻辑:每次用户发来新的对话都看成是一个新的session,需要通过gemini chats创建一个新的对话组,而不是在bot级维护。但是我们仍然需要有对话上下文,在bot的属性里面添加一个维护对话历史长度的数组,gemini chats.create支持一个history参数,这个history参数必须与是client.get_history拿到的数组类型一致。请通过一个环境变量来控制对话历史长度,并且在每次用户发来新的对话时候把最近的N轮历史加进去。在clear方法时候清空所有历史。


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Jan 1, 2026
Copilot AI and others added 5 commits January 1, 2026 04:40
- Add conversation_history array to store recent conversation rounds
- Add PRETTY_GEMINI_BOT_HISTORY_LENGTH environment variable to control history length
- Create new chat session for each message with recent history
- Update clear() method to clear conversation history
- Update .env.example with new environment variable

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
- Validate history_length to be non-negative using max(0, int(...))
- Fix history extraction to use correct indices based on history_before_length
- Ensure new messages are appended correctly regardless of initial history size

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
- Define MESSAGES_PER_ROUND constant to make code more maintainable
- Add validation to ensure both user and assistant messages exist before appending
- Extract messages to variables for better readability and safety

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
- Remove redundant conditional check for empty list (slice notation handles it)
- Use explicit None check instead of truthiness check for message validation
- This ensures valid messages with falsy content are not incorrectly rejected

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
- Add validation to trim history at complete round boundaries
- Handle edge cases where history might have incomplete rounds
- Ensures conversation context integrity is maintained

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
Copilot AI changed the title [WIP] Update dialogue creation logic for Pretty gemini bot Implement session-based conversation history for PrettyGeminiBot Jan 1, 2026
Copilot AI requested a review from hiyouga January 1, 2026 04:51
- Remove unnecessary ValueError exception handling in history_length initialization
- Remove unnecessary else block for incomplete conversation rounds check
- Since conversation_history is always updated in pairs, incomplete rounds are impossible

Co-authored-by: hiyouga <16256802+hiyouga@users.noreply.github.com>
@hiyouga hiyouga marked this pull request as ready for review January 1, 2026 05:35
Copilot AI review requested due to automatic review settings January 1, 2026 05:35
Copilot AI requested a review from hiyouga January 1, 2026 05:35
@hiyouga hiyouga merged commit 647d643 into main Jan 1, 2026
5 checks passed
@hiyouga hiyouga deleted the copilot/update-gemini-bot-dialogue-logic branch January 1, 2026 05:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements session-based conversation history management for PrettyGeminiBot, enabling the bot to maintain context across multiple user interactions while creating a new chat session for each message.

Key changes:

  • Added configurable conversation history tracking via PRETTY_GEMINI_BOT_HISTORY_LENGTH environment variable
  • Refactored from persistent chat session to session-per-message approach with history
  • Implemented automatic history management with recent N rounds preserved

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
mini_ema/bot/pretty_gemini_bot.py Adds conversation history array, implements session-based chat creation with history parameter, updates clear() to reset history, and adds logic to extract and store new messages
.env.example Documents the new PRETTY_GEMINI_BOT_HISTORY_LENGTH environment variable with default value of 10 rounds

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.

3 participants