[NELO-1102] feat(im): add thread-based session mode for IM channels

Add session_mode ("user" | "thread") to IM channel config. In thread
mode, each message thread gets its own session, enabling multi-user
collaboration within the same thread context.

Backend:
- Add SessionMode type constants and validation
- Add ThreadID field to IncomingMessage and ChannelSession
- Split resolveSession into resolveUserSession/resolveThreadSession
- Update makeUserKey to include threadID only in thread mode
- Extract ThreadID in Slack, Mattermost, Feishu, Telegram adapters
- Add message_thread_id to Telegram SendReply/StartStream for Forums
- DB migration 000028: session_mode column + thread_id column + index

Frontend:
- Add session_mode radio group to IMChannelPanel dialog
- Auto-reset session_mode when switching to non-thread platform
- Show thread badge on channel card for thread-mode channels
- Add i18n keys for en-US, ko-KR, zh-CN, ru-RU
This commit is contained in:
ochan.kwon
2026-03-27 20:49:10 +09:00
committed by lyingbug
parent 930a953f41
commit 2d5782992e
22 changed files with 1015 additions and 22 deletions
+33 -7
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"
@@ -76,12 +77,13 @@ type telegramUpdate struct {
}
type telegramMsg struct {
MessageID int `json:"message_id"`
From *telegramUser `json:"from"`
Chat telegramChat `json:"chat"`
Text string `json:"text"`
Document *telegramDoc `json:"document"`
Photo []telegramPhoto `json:"photo"`
MessageID int `json:"message_id"`
MessageThreadID int `json:"message_thread_id"`
From *telegramUser `json:"from"`
Chat telegramChat `json:"chat"`
Text string `json:"text"`
Document *telegramDoc `json:"document"`
Photo []telegramPhoto `json:"photo"`
}
type telegramUser struct {
@@ -154,6 +156,11 @@ func parseTelegramMessage(msg *telegramMsg) *im.IncomingMessage {
}
}
threadID := ""
if msg.MessageThreadID != 0 {
threadID = fmt.Sprintf("%d", msg.MessageThreadID)
}
incoming := &im.IncomingMessage{
Platform: im.PlatformTelegram,
UserID: userID,
@@ -161,6 +168,7 @@ func parseTelegramMessage(msg *telegramMsg) *im.IncomingMessage {
ChatID: chatID,
ChatType: chatType,
MessageID: fmt.Sprintf("%d", msg.MessageID),
ThreadID: threadID,
MessageType: im.MessageTypeText,
Content: msg.Text,
}
@@ -206,7 +214,20 @@ func resolveChatID(incoming *im.IncomingMessage) string {
// ── Send reply ──
func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error {
return a.sendMessage(ctx, resolveChatID(incoming), transformThinkBlocks(reply.Content), "")
chatID := resolveChatID(incoming)
text := transformThinkBlocks(reply.Content)
body := map[string]interface{}{
"chat_id": chatID,
"text": text,
"parse_mode": "Markdown",
}
if incoming.ThreadID != "" {
if tid, err := strconv.Atoi(incoming.ThreadID); err == nil {
body["message_thread_id"] = tid
}
}
return a.callAPI(ctx, "sendMessage", body)
}
func (a *Adapter) sendMessage(ctx context.Context, chatID, text, replyToMessageID string) error {
@@ -343,6 +364,11 @@ func (a *Adapter) StartStream(ctx context.Context, incoming *im.IncomingMessage)
"chat_id": chatID,
"text": "正在思考...",
}
if incoming.ThreadID != "" {
if tid, err := strconv.Atoi(incoming.ThreadID); err == nil {
body["message_thread_id"] = tid
}
}
var sentMsg struct {
MessageID int `json:"message_id"`
}
+108
View File
@@ -0,0 +1,108 @@
package telegram
import (
"testing"
"github.com/Tencent/WeKnora/internal/im"
)
func TestParseTelegramMessage_ForumTopicThread(t *testing.T) {
msg := &telegramMsg{
MessageID: 100,
MessageThreadID: 42, // Forum topic ID
From: &telegramUser{ID: 1001, FirstName: "Alice"},
Chat: telegramChat{ID: -9999, Type: "supergroup"},
Text: "hello",
}
incoming := parseTelegramMessage(msg)
if incoming == nil {
t.Fatal("expected non-nil message")
}
if incoming.ThreadID != "42" {
t.Errorf("ThreadID = %q, want %q", incoming.ThreadID, "42")
}
}
func TestParseTelegramMessage_NonForumGroup(t *testing.T) {
msg := &telegramMsg{
MessageID: 200,
MessageThreadID: 0, // not a Forum group
From: &telegramUser{ID: 1002, FirstName: "Bob"},
Chat: telegramChat{ID: -8888, Type: "group"},
Text: "hello",
}
incoming := parseTelegramMessage(msg)
if incoming == nil {
t.Fatal("expected non-nil message")
}
// Non-Forum groups: ThreadID should be empty
if incoming.ThreadID != "" {
t.Errorf("ThreadID = %q, want empty for non-Forum group", incoming.ThreadID)
}
}
func TestParseTelegramMessage_DirectMessage(t *testing.T) {
msg := &telegramMsg{
MessageID: 300,
From: &telegramUser{ID: 1003, FirstName: "Carol"},
Chat: telegramChat{ID: 1003, Type: "private"},
Text: "hi bot",
}
incoming := parseTelegramMessage(msg)
if incoming == nil {
t.Fatal("expected non-nil message")
}
if incoming.ThreadID != "" {
t.Errorf("ThreadID = %q, want empty for DM", incoming.ThreadID)
}
if incoming.ChatType != im.ChatTypeDirect {
t.Errorf("ChatType = %q, want %q", incoming.ChatType, im.ChatTypeDirect)
}
}
func TestParseTelegramMessage_NilMessage(t *testing.T) {
incoming := parseTelegramMessage(nil)
if incoming != nil {
t.Error("expected nil for nil message")
}
}
func TestParseTelegramMessage_Document(t *testing.T) {
msg := &telegramMsg{
MessageID: 400,
MessageThreadID: 7, // in a Forum topic
From: &telegramUser{ID: 1004, FirstName: "Dave"},
Chat: telegramChat{ID: -7777, Type: "supergroup"},
Document: &telegramDoc{
FileID: "doc-123",
FileName: "report.pdf",
FileSize: 2048,
},
}
incoming := parseTelegramMessage(msg)
if incoming == nil {
t.Fatal("expected non-nil message")
}
if incoming.ThreadID != "7" {
t.Errorf("ThreadID = %q, want %q", incoming.ThreadID, "7")
}
if incoming.MessageType != im.MessageTypeFile {
t.Errorf("MessageType = %q, want %q", incoming.MessageType, im.MessageTypeFile)
}
}
func TestParseUpdate_NilMessage(t *testing.T) {
update := &telegramUpdate{UpdateID: 1, Message: nil}
incoming := parseUpdate(update)
if incoming != nil {
t.Error("expected nil for nil update.Message")
}
}