Skip to content

feat(session): persist message IDs, reply threading, and author identity in session history#1709

Draft
dimonb wants to merge 6 commits intosipeed:mainfrom
dimonb:pr/01-message-history
Draft

feat(session): persist message IDs, reply threading, and author identity in session history#1709
dimonb wants to merge 6 commits intosipeed:mainfrom
dimonb:pr/01-message-history

Conversation

@dimonb
Copy link
Contributor

@dimonb dimonb commented Mar 17, 2026

📝 Description

Persists structured metadata alongside messages in JSONL session history so the LLM can navigate thread structure and address participants by name across restarts.

What this adds:

  • MessageID and ReplyToMessageID fields on Message so session history can preserve thread metadata instead of flattening conversations to plain text
  • User messages keep inbound platform message_id values when they are appended to history
  • Assistant messages are written to history only after confirmed delivery, so supported channels can persist the outbound platform message_id as well
  • Outbound delivery callbacks plus optional message-ID sender support in the channel pipeline; placeholder edits also count as delivered assistant messages
  • MessageSender{Username, FirstName, LastName} struct — stored alongside user messages so the LLM knows who said what in multi-user chats (e.g. Telegram groups)
  • FirstName/LastName extracted from Telegram user and stored in bus.SenderInfo
  • bus.MessageBus.SubscribeOutbound helper (blocks until next outbound message or ctx done) — required by cron_test.go which was already referencing it

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)

🔗 Related Issue

📚 Technical Context (Skip for Docs)

  • Reference URL: https://core.telegram.org/bots/api#message (MessageID / reply_to_message)
  • Reasoning: In multi-user Telegram group chats, session history loses context about who said what and which messages are replies. Without MessageID/ReplyToMessageID in the JSONL store, the LLM cannot reconstruct thread structure after a restart. Without storing assistant delivery IDs, outbound turns still lose their platform identity after restart. Without MessageSender, it cannot address participants by name.

🧪 Test Environment

  • Hardware: PC (x86_64)
  • OS: macOS 15 / Linux (Debian 12)
  • Model/Provider: Claude Sonnet, OpenAI GPT-4o
  • Channels: Telegram, Discord, Slack, QQ

📸 Evidence (Optional)

Click to view Logs/Screenshots

All existing tests pass (go test ./...) and lint passes (make lint).

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly.

@dimonb
Copy link
Contributor Author

dimonb commented Mar 17, 2026

Hi, guys. The idea of this PR is to preserve more information in the session history to give the LLM more context to implement threading, reactions, etc.

My plan is to gradually submit more PRs to the community to support reactions, more complex history compaction, and other cool features that I’ve implemented in my fork. It’s a little messy right now and needs some improvements in loop.go, so I’ll wait.

dimonb added 4 commits March 17, 2026 19:26
Add MessageID and ReplyToMessageID fields to Message struct so they are
persisted in JSONL sessions. Store IDs when saving user messages in the
agent loop. Annotate history messages with [msg:#ID] and
[reply_to:#PARENT] prefixes in BuildMessages so the LLM can navigate
thread structure after restart.
Add MessageSender struct to persist author identity alongside messages
in JSONL session history. This lets the LLM address participants by name
in multi-user conversations (e.g. Telegram group chats).

- Add MessageSender{Username, FirstName, LastName} to protocoltypes
- Add Sender *MessageSender field to Message struct
- Add FirstName/LastName fields to bus.SenderInfo
- Extract FirstName/LastName from Telegram user struct
- Store Sender in user messages saved to session history
SubscribeOutbound blocks until the next outbound message is available
or the context is done. Used by cron_test to assert message delivery.
@dimonb dimonb force-pushed the pr/01-message-history branch from 359fcfa to 273ea9c Compare March 17, 2026 17:27
@dimonb dimonb marked this pull request as draft March 17, 2026 18:14
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.

1 participant