feat(im): integrate command dispatch, QA queue, rate limiting, and streaming improvements
- Wire slash-command registry into Service and HandleMessage dispatch - Add bounded QA worker pool (qaQueue) with per-user backpressure - Add sliding-window rate limiter to prevent message abuse - Support /stop cancellation for queued and in-flight QA requests - Fix WeCom group chat replies via appchat API with user fallback - Fix WeCom longconn nil map panic and stream buffer memory leak - Add Feishu stream reaper to clean orphaned streams (memory leak fix) - Show "正在思考..." placeholder during Feishu streaming - Handle empty-stream fallback when no visible content is produced - Derive WebSearchEnabled from agent config for IM channels
This commit is contained in:
@@ -50,6 +50,7 @@ type Adapter struct {
|
||||
|
||||
// NewAdapter creates a new Feishu adapter.
|
||||
func NewAdapter(appID, appSecret, verificationToken, encryptKey string) *Adapter {
|
||||
startStreamReaper()
|
||||
return &Adapter{
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
@@ -58,6 +59,44 @@ func NewAdapter(appID, appSecret, verificationToken, encryptKey string) *Adapter
|
||||
}
|
||||
}
|
||||
|
||||
// startStreamReaper starts a background goroutine (once) that periodically
|
||||
// removes orphaned stream entries from feishuStreams. This prevents memory
|
||||
// leaks when EndStream is never called due to panics or pipeline errors.
|
||||
func startStreamReaper() {
|
||||
startReaperOnce.Do(func() {
|
||||
go func() {
|
||||
ticker := time.NewTicker(streamReaperInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
cutoff := time.Now().Add(-streamOrphanTTL)
|
||||
feishuStreamsMu.Lock()
|
||||
for id, state := range feishuStreams {
|
||||
if state.createdAt.Before(cutoff) {
|
||||
delete(feishuStreams, id)
|
||||
}
|
||||
}
|
||||
feishuStreamsMu.Unlock()
|
||||
case <-reaperStopCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
// StopStreamReaper stops the background stream reaper goroutine.
|
||||
// Should be called during application shutdown.
|
||||
func StopStreamReaper() {
|
||||
select {
|
||||
case <-reaperStopCh:
|
||||
// already closed
|
||||
default:
|
||||
close(reaperStopCh)
|
||||
}
|
||||
}
|
||||
|
||||
// Platform returns the platform identifier.
|
||||
func (a *Adapter) Platform() im.Platform {
|
||||
return im.PlatformFeishu
|
||||
@@ -510,14 +549,27 @@ const (
|
||||
|
||||
// feishuStreamState tracks per-stream accumulated content.
|
||||
type feishuStreamState struct {
|
||||
mu sync.Mutex
|
||||
content strings.Builder
|
||||
seq int64 // strictly incrementing sequence for CardKit API
|
||||
mu sync.Mutex
|
||||
content strings.Builder
|
||||
seq int64 // strictly incrementing sequence for CardKit API
|
||||
createdAt time.Time // for orphan stream detection
|
||||
firstChunk bool // true after the first real content chunk clears the placeholder
|
||||
}
|
||||
|
||||
const (
|
||||
// streamOrphanTTL is the maximum lifetime of a stream entry before it's
|
||||
// considered orphaned (e.g., EndStream was never called due to an error).
|
||||
streamOrphanTTL = 5 * time.Minute
|
||||
// streamReaperInterval is how often the reaper scans for orphaned streams.
|
||||
streamReaperInterval = 1 * time.Minute
|
||||
)
|
||||
|
||||
var (
|
||||
feishuStreamsMu sync.Mutex
|
||||
feishuStreams = map[string]*feishuStreamState{}
|
||||
feishuStreams = map[string]*feishuStreamState{}
|
||||
|
||||
startReaperOnce sync.Once
|
||||
reaperStopCh = make(chan struct{})
|
||||
)
|
||||
|
||||
func (s *feishuStreamState) nextSeq() int {
|
||||
@@ -541,7 +593,7 @@ func buildStreamingCardJSON() string {
|
||||
"elements": []map[string]interface{}{
|
||||
{
|
||||
"tag": "markdown",
|
||||
"content": "",
|
||||
"content": "💭 正在思考...",
|
||||
"text_size": "normal",
|
||||
"element_id": streamingElementID,
|
||||
},
|
||||
@@ -573,7 +625,7 @@ func (a *Adapter) StartStream(ctx context.Context, incoming *im.IncomingMessage)
|
||||
|
||||
// 3. Track stream state
|
||||
feishuStreamsMu.Lock()
|
||||
feishuStreams[cardID] = &feishuStreamState{}
|
||||
feishuStreams[cardID] = &feishuStreamState{createdAt: time.Now()}
|
||||
feishuStreamsMu.Unlock()
|
||||
|
||||
logger.Infof(ctx, "[Feishu] Streaming started: card_id=%s", cardID)
|
||||
@@ -596,6 +648,11 @@ func (a *Adapter) SendStreamChunk(ctx context.Context, incoming *im.IncomingMess
|
||||
}
|
||||
|
||||
state.mu.Lock()
|
||||
if !state.firstChunk {
|
||||
// Clear the "💭 正在思考..." placeholder on first real content
|
||||
state.content.Reset()
|
||||
state.firstChunk = true
|
||||
}
|
||||
state.content.WriteString(content)
|
||||
fullContent := transformThinkBlocks(state.content.String())
|
||||
seq := state.nextSeq()
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
package im
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultMaxQueueSize is the maximum number of pending QA requests in the queue.
|
||||
defaultMaxQueueSize = 50
|
||||
// defaultMaxPerUser limits how many requests a single user can have queued.
|
||||
defaultMaxPerUser = 3
|
||||
// defaultWorkers is the default number of concurrent QA workers.
|
||||
defaultWorkers = 5
|
||||
// queueTimeout is how long a request can wait in the queue before being discarded.
|
||||
queueTimeout = 60 * time.Second
|
||||
)
|
||||
|
||||
// qaRequest represents a QA request waiting in the queue.
|
||||
type qaRequest struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
msg *IncomingMessage
|
||||
session *types.Session
|
||||
agent *types.CustomAgent
|
||||
adapter Adapter
|
||||
channel *IMChannel
|
||||
channelID string
|
||||
|
||||
// userKey is "channelID:userID:chatID", used for per-user limits and /stop.
|
||||
userKey string
|
||||
enqueuedAt time.Time
|
||||
}
|
||||
|
||||
// QueueMetrics exposes observable queue state.
|
||||
type QueueMetrics struct {
|
||||
// Depth is the current number of requests waiting in the queue.
|
||||
Depth int
|
||||
// ActiveWorkers is the number of workers currently executing a QA request.
|
||||
ActiveWorkers int64
|
||||
// TotalEnqueued is the cumulative number of requests enqueued.
|
||||
TotalEnqueued int64
|
||||
// TotalProcessed is the cumulative number of requests dequeued and executed.
|
||||
TotalProcessed int64
|
||||
// TotalRejected is the cumulative number of requests rejected (queue full / per-user limit).
|
||||
TotalRejected int64
|
||||
// TotalTimeout is the cumulative number of requests discarded due to queue timeout.
|
||||
TotalTimeout int64
|
||||
}
|
||||
|
||||
// qaQueue is a bounded, per-user-limited request queue with a fixed worker pool.
|
||||
type qaQueue struct {
|
||||
mu sync.Mutex
|
||||
cond *sync.Cond
|
||||
queue []*qaRequest
|
||||
maxSize int
|
||||
maxPerUser int
|
||||
workers int
|
||||
perUser map[string]int // userKey → queued count
|
||||
closed bool
|
||||
|
||||
// metrics
|
||||
activeWorkers atomic.Int64
|
||||
totalEnqueued atomic.Int64
|
||||
totalProcessed atomic.Int64
|
||||
totalRejected atomic.Int64
|
||||
totalTimeout atomic.Int64
|
||||
|
||||
// handler is called by workers to execute the QA request.
|
||||
handler func(req *qaRequest)
|
||||
}
|
||||
|
||||
// newQAQueue creates a new bounded queue with the given worker count.
|
||||
func newQAQueue(workers, maxSize, maxPerUser int, handler func(req *qaRequest)) *qaQueue {
|
||||
q := &qaQueue{
|
||||
queue: make([]*qaRequest, 0, maxSize),
|
||||
maxSize: maxSize,
|
||||
maxPerUser: maxPerUser,
|
||||
workers: workers,
|
||||
perUser: make(map[string]int),
|
||||
handler: handler,
|
||||
}
|
||||
q.cond = sync.NewCond(&q.mu)
|
||||
return q
|
||||
}
|
||||
|
||||
// Start launches the worker goroutines and the metrics reporter. Call Stop to shut down.
|
||||
func (q *qaQueue) Start(stopCh <-chan struct{}) {
|
||||
for i := 0; i < q.workers; i++ {
|
||||
go q.runWorker(i)
|
||||
}
|
||||
go q.metricsLoop(stopCh)
|
||||
}
|
||||
|
||||
// Stop signals all workers to exit after draining.
|
||||
func (q *qaQueue) Stop() {
|
||||
q.mu.Lock()
|
||||
q.closed = true
|
||||
q.mu.Unlock()
|
||||
q.cond.Broadcast()
|
||||
}
|
||||
|
||||
// Enqueue adds a request to the queue. Returns the queue position (0-based)
|
||||
// or an error if the queue is full or per-user limit is reached.
|
||||
func (q *qaQueue) Enqueue(req *qaRequest) (position int, err error) {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
if q.closed {
|
||||
return 0, fmt.Errorf("queue is closed")
|
||||
}
|
||||
|
||||
if len(q.queue) >= q.maxSize {
|
||||
q.totalRejected.Add(1)
|
||||
return 0, fmt.Errorf("queue full (%d/%d)", len(q.queue), q.maxSize)
|
||||
}
|
||||
|
||||
if q.perUser[req.userKey] >= q.maxPerUser {
|
||||
q.totalRejected.Add(1)
|
||||
return 0, fmt.Errorf("per-user queue limit reached (%d/%d)", q.perUser[req.userKey], q.maxPerUser)
|
||||
}
|
||||
|
||||
req.enqueuedAt = time.Now()
|
||||
q.queue = append(q.queue, req)
|
||||
q.perUser[req.userKey]++
|
||||
q.totalEnqueued.Add(1)
|
||||
pos := len(q.queue) - 1
|
||||
|
||||
q.cond.Signal()
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
// Remove cancels and removes a queued request by userKey.
|
||||
// Returns true if a request was found and removed.
|
||||
func (q *qaQueue) Remove(userKey string) bool {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
for i, req := range q.queue {
|
||||
if req.userKey == userKey {
|
||||
req.cancel()
|
||||
q.queue = append(q.queue[:i], q.queue[i+1:]...)
|
||||
q.perUser[userKey]--
|
||||
if q.perUser[userKey] <= 0 {
|
||||
delete(q.perUser, userKey)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Metrics returns a snapshot of the queue's observable state.
|
||||
func (q *qaQueue) Metrics() QueueMetrics {
|
||||
q.mu.Lock()
|
||||
depth := len(q.queue)
|
||||
q.mu.Unlock()
|
||||
|
||||
return QueueMetrics{
|
||||
Depth: depth,
|
||||
ActiveWorkers: q.activeWorkers.Load(),
|
||||
TotalEnqueued: q.totalEnqueued.Load(),
|
||||
TotalProcessed: q.totalProcessed.Load(),
|
||||
TotalRejected: q.totalRejected.Load(),
|
||||
TotalTimeout: q.totalTimeout.Load(),
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qaQueue) runWorker(id int) {
|
||||
for {
|
||||
req := q.dequeue()
|
||||
if req == nil {
|
||||
return // queue closed
|
||||
}
|
||||
|
||||
// Skip requests that have been cancelled or timed out while queued.
|
||||
if req.ctx.Err() != nil {
|
||||
q.totalTimeout.Add(1)
|
||||
continue
|
||||
}
|
||||
|
||||
waitDuration := time.Since(req.enqueuedAt)
|
||||
if waitDuration > queueTimeout {
|
||||
q.totalTimeout.Add(1)
|
||||
logger.Warnf(req.ctx, "[IM] Queue timeout: user=%s waited=%s, discarding", req.msg.UserID, waitDuration)
|
||||
_ = req.adapter.SendReply(req.ctx, req.msg, &ReplyMessage{
|
||||
Content: "您的消息等待超时,请重新发送。",
|
||||
IsFinal: true,
|
||||
})
|
||||
req.cancel()
|
||||
continue
|
||||
}
|
||||
|
||||
logger.Infof(req.ctx, "[IM] Dequeued: worker=%d user=%s waited=%s depth=%d",
|
||||
id, req.msg.UserID, waitDuration, q.Metrics().Depth)
|
||||
|
||||
q.activeWorkers.Add(1)
|
||||
q.handler(req)
|
||||
q.activeWorkers.Add(-1)
|
||||
q.totalProcessed.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (q *qaQueue) dequeue() *qaRequest {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
for len(q.queue) == 0 && !q.closed {
|
||||
q.cond.Wait()
|
||||
}
|
||||
|
||||
if q.closed && len(q.queue) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
req := q.queue[0]
|
||||
q.queue = q.queue[1:]
|
||||
q.perUser[req.userKey]--
|
||||
if q.perUser[req.userKey] <= 0 {
|
||||
delete(q.perUser, req.userKey)
|
||||
}
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
const metricsLogInterval = 30 * time.Second
|
||||
|
||||
// metricsLoop periodically logs queue metrics for operational visibility.
|
||||
func (q *qaQueue) metricsLoop(stopCh <-chan struct{}) {
|
||||
ticker := time.NewTicker(metricsLogInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
m := q.Metrics()
|
||||
// Only log when there is activity to avoid noise.
|
||||
if m.Depth > 0 || m.ActiveWorkers > 0 {
|
||||
logger.Infof(context.Background(),
|
||||
"[IM] Queue metrics: depth=%d active_workers=%d enqueued=%d processed=%d rejected=%d timeout=%d",
|
||||
m.Depth, m.ActiveWorkers, m.TotalEnqueued, m.TotalProcessed, m.TotalRejected, m.TotalTimeout)
|
||||
}
|
||||
case <-stopCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package im
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// rateLimitWindow is the sliding window duration for rate limiting.
|
||||
rateLimitWindow = 60 * time.Second
|
||||
// rateLimitMaxRequests is the maximum number of requests allowed per window per key.
|
||||
rateLimitMaxRequests = 10
|
||||
// rateLimitCleanupInterval is how often stale entries are purged.
|
||||
rateLimitCleanupInterval = 1 * time.Minute
|
||||
)
|
||||
|
||||
// rateLimitEntry holds the request timestamps for a single key.
|
||||
type rateLimitEntry struct {
|
||||
mu sync.Mutex
|
||||
timestamps []time.Time
|
||||
deleted bool // marked true when removed from the map by cleanupLoop
|
||||
}
|
||||
|
||||
// slidingWindowLimiter implements per-key sliding window rate limiting.
|
||||
type slidingWindowLimiter struct {
|
||||
window time.Duration
|
||||
maxRequests int
|
||||
entries sync.Map // key -> *rateLimitEntry
|
||||
}
|
||||
|
||||
func newSlidingWindowLimiter(window time.Duration, maxRequests int) *slidingWindowLimiter {
|
||||
return &slidingWindowLimiter{
|
||||
window: window,
|
||||
maxRequests: maxRequests,
|
||||
}
|
||||
}
|
||||
|
||||
// Allow checks if the request for the given key is within the rate limit.
|
||||
// Returns true if allowed, false if rate limited.
|
||||
func (l *slidingWindowLimiter) Allow(key string) bool {
|
||||
now := time.Now()
|
||||
cutoff := now.Add(-l.window)
|
||||
|
||||
for {
|
||||
val, _ := l.entries.LoadOrStore(key, &rateLimitEntry{})
|
||||
entry := val.(*rateLimitEntry)
|
||||
|
||||
entry.mu.Lock()
|
||||
// If the entry was concurrently deleted by cleanupLoop, retry with a fresh one.
|
||||
if entry.deleted {
|
||||
entry.mu.Unlock()
|
||||
l.entries.Delete(key) // ensure stale entry is gone
|
||||
continue
|
||||
}
|
||||
|
||||
// Remove expired timestamps
|
||||
valid := entry.timestamps[:0]
|
||||
for _, t := range entry.timestamps {
|
||||
if t.After(cutoff) {
|
||||
valid = append(valid, t)
|
||||
}
|
||||
}
|
||||
entry.timestamps = valid
|
||||
|
||||
if len(entry.timestamps) >= l.maxRequests {
|
||||
entry.mu.Unlock()
|
||||
return false
|
||||
}
|
||||
|
||||
entry.timestamps = append(entry.timestamps, now)
|
||||
entry.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// cleanupLoop periodically removes stale entries from the limiter.
|
||||
func (l *slidingWindowLimiter) cleanupLoop(stopCh <-chan struct{}) {
|
||||
ticker := time.NewTicker(rateLimitCleanupInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
cutoff := time.Now().Add(-l.window)
|
||||
l.entries.Range(func(key, val interface{}) bool {
|
||||
entry := val.(*rateLimitEntry)
|
||||
entry.mu.Lock()
|
||||
allExpired := true
|
||||
for _, t := range entry.timestamps {
|
||||
if t.After(cutoff) {
|
||||
allExpired = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if allExpired {
|
||||
entry.deleted = true
|
||||
l.entries.Delete(key)
|
||||
}
|
||||
entry.mu.Unlock()
|
||||
return true
|
||||
})
|
||||
case <-stopCh:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
+291
-46
@@ -48,8 +48,9 @@ type AdapterFactory func(ctx context.Context, channel *IMChannel, msgHandler fun
|
||||
// Service orchestrates IM message handling:
|
||||
// 1. Receives a unified IncomingMessage from an Adapter
|
||||
// 2. Resolves or creates a WeKnora session for the IM channel
|
||||
// 3. Calls the WeKnora QA pipeline
|
||||
// 4. Collects the streaming answer and sends it back via the Adapter
|
||||
// 3. Dispatches slash-commands (/help, /kb, /clear, etc.) without entering QA
|
||||
// 4. Calls the WeKnora QA pipeline for normal messages
|
||||
// 5. Collects the streaming answer and sends it back via the Adapter
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
sessionService interfaces.SessionService
|
||||
@@ -60,9 +61,15 @@ type Service struct {
|
||||
// knowledgeService is used for saving IM file messages to knowledge bases.
|
||||
knowledgeService interfaces.KnowledgeService
|
||||
|
||||
// kbService is used by slash-commands (/info) to list and inspect knowledge bases.
|
||||
kbService interfaces.KnowledgeBaseService
|
||||
|
||||
// modelService is used to obtain the chat model for generating smart notification replies.
|
||||
modelService interfaces.ModelService
|
||||
|
||||
// cmdRegistry holds all registered slash-commands.
|
||||
cmdRegistry *CommandRegistry
|
||||
|
||||
// channels maps channel ID -> running channel state
|
||||
channels map[string]*channelState
|
||||
mu sync.RWMutex
|
||||
@@ -73,9 +80,26 @@ type Service struct {
|
||||
// processedMsgs tracks recently processed message IDs to prevent duplicate handling.
|
||||
processedMsgs sync.Map
|
||||
|
||||
// rateLimiter enforces per-user sliding window rate limiting.
|
||||
rateLimiter *slidingWindowLimiter
|
||||
|
||||
// inflight tracks cancel functions for in-progress QA requests, keyed by
|
||||
// "channelID:userID:chatID". This allows /stop to abort a running request.
|
||||
inflight sync.Map // key -> context.CancelFunc
|
||||
|
||||
// qaQueue manages bounded queuing and worker-pool execution of QA requests,
|
||||
// providing backpressure to protect downstream LLM resources.
|
||||
qaQueue *qaQueue
|
||||
|
||||
stopCh chan struct{}
|
||||
}
|
||||
|
||||
// makeUserKey builds the canonical key used to identify a user's request
|
||||
// across the queue, inflight map, and /stop command.
|
||||
func makeUserKey(channelID, userID, chatID string) string {
|
||||
return fmt.Sprintf("%s:%s:%s", channelID, userID, chatID)
|
||||
}
|
||||
|
||||
func buildIMQARequest(
|
||||
session *types.Session,
|
||||
query string,
|
||||
@@ -84,6 +108,10 @@ func buildIMQARequest(
|
||||
customAgent *types.CustomAgent,
|
||||
kbIDs []string,
|
||||
) *types.QARequest {
|
||||
// WebSearchEnabled: the web handler passes this per-request from the
|
||||
// frontend toggle; for IM channels the user has no per-message toggle,
|
||||
// so we derive it from the agent config (the single source of truth).
|
||||
webSearchEnabled := customAgent != nil && customAgent.Config.WebSearchEnabled
|
||||
return &types.QARequest{
|
||||
Session: session,
|
||||
Query: query,
|
||||
@@ -91,6 +119,7 @@ func buildIMQARequest(
|
||||
CustomAgent: customAgent,
|
||||
KnowledgeBaseIDs: kbIDs,
|
||||
UserMessageID: userMessageID,
|
||||
WebSearchEnabled: webSearchEnabled,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,8 +131,17 @@ func NewService(
|
||||
tenantService interfaces.TenantService,
|
||||
agentService interfaces.CustomAgentService,
|
||||
knowledgeService interfaces.KnowledgeService,
|
||||
kbService interfaces.KnowledgeBaseService,
|
||||
modelService interfaces.ModelService,
|
||||
) *Service {
|
||||
// Build command registry.
|
||||
registry := NewCommandRegistry()
|
||||
registry.Register(newHelpCommand(registry))
|
||||
registry.Register(newInfoCommand(kbService))
|
||||
registry.Register(newSearchCommand(sessionService, kbService))
|
||||
registry.Register(newStopCommand())
|
||||
registry.Register(newClearCommand())
|
||||
|
||||
s := &Service{
|
||||
db: db,
|
||||
sessionService: sessionService,
|
||||
@@ -111,14 +149,22 @@ func NewService(
|
||||
tenantService: tenantService,
|
||||
agentService: agentService,
|
||||
knowledgeService: knowledgeService,
|
||||
kbService: kbService,
|
||||
modelService: modelService,
|
||||
cmdRegistry: registry,
|
||||
channels: make(map[string]*channelState),
|
||||
adapterFactories: make(map[string]AdapterFactory),
|
||||
rateLimiter: newSlidingWindowLimiter(rateLimitWindow, rateLimitMaxRequests),
|
||||
stopCh: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Initialize the QA worker pool and bounded queue.
|
||||
s.qaQueue = newQAQueue(defaultWorkers, defaultMaxQueueSize, defaultMaxPerUser, s.executeQARequest)
|
||||
s.qaQueue.Start(s.stopCh)
|
||||
|
||||
// Start periodic dedup cleanup instead of per-message goroutines
|
||||
go s.dedupCleanupLoop()
|
||||
go s.rateLimiter.cleanupLoop(s.stopCh)
|
||||
|
||||
return s
|
||||
}
|
||||
@@ -133,6 +179,7 @@ func (s *Service) RegisterAdapterFactory(platform string, factory AdapterFactory
|
||||
// Stop gracefully shuts down the service, stopping all channels and background goroutines.
|
||||
func (s *Service) Stop() {
|
||||
close(s.stopCh)
|
||||
s.qaQueue.Stop()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for id, cs := range s.channels {
|
||||
@@ -283,7 +330,7 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
msg.Content = string(contentRunes[:maxContentLength])
|
||||
}
|
||||
|
||||
// Get channel config
|
||||
// Get channel config (moved before rate limit so we can reply to the user)
|
||||
adapter, channel, ok := s.GetChannelAdapter(channelID)
|
||||
if !ok {
|
||||
// Try loading from DB (channel might have been created after service start)
|
||||
@@ -301,6 +348,22 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
}
|
||||
}
|
||||
|
||||
// Rate limit: enforce per-user sliding window to prevent abuse.
|
||||
// Slash-commands (/stop, /clear, etc.) bypass rate limiting so the user
|
||||
// always retains control over the bot even under heavy messaging.
|
||||
isCommand := s.cmdRegistry.IsRegistered(msg.Content)
|
||||
if !isCommand {
|
||||
rateLimitKey := makeUserKey(channelID, msg.UserID, msg.ChatID)
|
||||
if !s.rateLimiter.Allow(rateLimitKey) {
|
||||
logger.Warnf(ctx, "[IM] Rate limited: channel=%s user=%s chat=%s", channelID, msg.UserID, msg.ChatID)
|
||||
_ = adapter.SendReply(ctx, msg, &ReplyMessage{
|
||||
Content: "您的消息发送过于频繁,请稍后再试。",
|
||||
IsFinal: true,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
tenantID := channel.TenantID
|
||||
agentID := channel.AgentID
|
||||
|
||||
@@ -330,13 +393,7 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
return fmt.Errorf("resolve session: %w", err)
|
||||
}
|
||||
|
||||
// 3. Get the WeKnora session
|
||||
session, err := s.sessionService.GetSession(sessionCtx, channelSession.SessionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get session: %w", err)
|
||||
}
|
||||
|
||||
// 4. Resolve custom agent (optional)
|
||||
// 3. Resolve custom agent (optional)
|
||||
var customAgent *types.CustomAgent
|
||||
if agentID != "" {
|
||||
agent, err := s.agentService.GetAgentByID(sessionCtx, agentID)
|
||||
@@ -347,22 +404,95 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Resolve knowledge base IDs from agent config
|
||||
var kbIDs []string
|
||||
if customAgent != nil {
|
||||
kbIDs = customAgent.Config.KnowledgeBases
|
||||
// ── Slash-command dispatch ──
|
||||
// Commands are handled before the QA pipeline so they respond instantly.
|
||||
if cmd, args, ok := s.cmdRegistry.Parse(msg.Content); ok {
|
||||
return s.handleCommand(sessionCtx, cmd, args, msg, adapter, channel, channelSession, customAgent)
|
||||
}
|
||||
// Unrecognised slash-word: show help hint instead of sending to QA.
|
||||
if LooksLikeCommand(msg.Content) {
|
||||
_ = adapter.SendReply(ctx, msg, &ReplyMessage{
|
||||
Content: "未知指令,发送 `/help` 查看所有可用指令。",
|
||||
IsFinal: true,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// 6. If the adapter supports streaming and output_mode is not "full", use streaming
|
||||
streamDisabled := channel.OutputMode == "full"
|
||||
// 4. Get the WeKnora session
|
||||
session, err := s.sessionService.GetSession(sessionCtx, channelSession.SessionID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get session: %w", err)
|
||||
}
|
||||
|
||||
// 5. Enqueue the QA request into the bounded worker pool.
|
||||
// The worker pool controls LLM concurrency and provides backpressure.
|
||||
qaCtx, qaCancel := context.WithCancel(sessionCtx)
|
||||
userKey := makeUserKey(channelID, msg.UserID, msg.ChatID)
|
||||
|
||||
req := &qaRequest{
|
||||
ctx: qaCtx,
|
||||
cancel: qaCancel,
|
||||
msg: msg,
|
||||
session: session,
|
||||
agent: customAgent,
|
||||
adapter: adapter,
|
||||
channel: channel,
|
||||
channelID: channelID,
|
||||
userKey: userKey,
|
||||
}
|
||||
|
||||
pos, enqueueErr := s.qaQueue.Enqueue(req)
|
||||
if enqueueErr != nil {
|
||||
qaCancel()
|
||||
logger.Warnf(ctx, "[IM] Queue rejected: user=%s reason=%v", msg.UserID, enqueueErr)
|
||||
_ = adapter.SendReply(ctx, msg, &ReplyMessage{
|
||||
Content: "当前排队人数较多,请稍后再试。",
|
||||
IsFinal: true,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
if pos > 0 {
|
||||
logger.Infof(ctx, "[IM] Enqueued: user=%s pos=%d depth=%d", msg.UserID, pos, s.qaQueue.Metrics().Depth)
|
||||
_ = adapter.SendReply(ctx, msg, &ReplyMessage{
|
||||
Content: fmt.Sprintf("收到,前面还有 %d 条消息在处理,请稍候 ⏳", pos),
|
||||
IsFinal: true,
|
||||
})
|
||||
} else {
|
||||
logger.Infof(ctx, "[IM] Enqueued: user=%s pos=0 (immediate)", msg.UserID)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// executeQARequest is the worker handler that runs the QA pipeline for a queued request.
|
||||
// It is called by qaQueue workers and must not block indefinitely.
|
||||
func (s *Service) executeQARequest(req *qaRequest) {
|
||||
ctx := req.ctx
|
||||
defer req.cancel()
|
||||
|
||||
// Track in-flight request so /stop can cancel it.
|
||||
s.inflight.Store(req.userKey, req.cancel)
|
||||
defer s.inflight.Delete(req.userKey)
|
||||
|
||||
// kbIDs is left empty so the QA pipeline resolves them from the agent config.
|
||||
var kbIDs []string
|
||||
|
||||
// Determine output mode from channel config.
|
||||
streamDisabled := req.channel.OutputMode == "full"
|
||||
|
||||
// If the adapter supports streaming and output is not "full", use streaming.
|
||||
if !streamDisabled {
|
||||
if streamer, ok := adapter.(StreamSender); ok {
|
||||
return s.handleMessageStream(sessionCtx, msg, session, customAgent, kbIDs, streamer, adapter)
|
||||
if streamer, ok := req.adapter.(StreamSender); ok {
|
||||
if err := s.handleMessageStream(ctx, req.msg, req.session, req.agent, kbIDs, streamer, req.adapter); err != nil {
|
||||
logger.Errorf(ctx, "[IM] Stream QA failed: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Non-streaming fallback: collect full answer then send
|
||||
answer, err := s.runQA(sessionCtx, session, msg.Content, customAgent, kbIDs)
|
||||
// Non-streaming fallback: collect full answer then send.
|
||||
answer, err := s.runQA(ctx, req.session, req.msg.Content, req.agent, kbIDs)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "[IM] QA failed: %v, sending fallback reply", err)
|
||||
answer = "抱歉,处理您的问题时出现了异常,请稍后再试。"
|
||||
@@ -372,12 +502,112 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
|
||||
Content: answer,
|
||||
IsFinal: true,
|
||||
}
|
||||
if err := adapter.SendReply(ctx, msg, reply); err != nil {
|
||||
return fmt.Errorf("send reply: %w", err)
|
||||
if err := req.adapter.SendReply(ctx, req.msg, reply); err != nil {
|
||||
logger.Errorf(ctx, "[IM] Send reply failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[IM] Reply sent: channel=%s platform=%s user=%s answer_len=%d",
|
||||
channelID, msg.Platform, msg.UserID, len(answer))
|
||||
req.channelID, req.msg.Platform, req.msg.UserID, len(answer))
|
||||
}
|
||||
|
||||
// handleCommand executes a slash-command and sends the result back to the user.
|
||||
// It also handles side effects (ActionClear, ActionStop).
|
||||
func (s *Service) handleCommand(
|
||||
ctx context.Context,
|
||||
cmd Command,
|
||||
args []string,
|
||||
msg *IncomingMessage,
|
||||
adapter Adapter,
|
||||
channel *IMChannel,
|
||||
channelSession *ChannelSession,
|
||||
customAgent *types.CustomAgent,
|
||||
) error {
|
||||
agentName := ""
|
||||
if customAgent != nil {
|
||||
agentName = customAgent.Name
|
||||
}
|
||||
|
||||
cmdCtx := &CommandContext{
|
||||
Incoming: msg,
|
||||
Session: channelSession,
|
||||
TenantID: channel.TenantID,
|
||||
AgentName: agentName,
|
||||
CustomAgent: customAgent,
|
||||
ChannelOutputMode: channel.OutputMode,
|
||||
}
|
||||
|
||||
result, err := cmd.Execute(ctx, cmdCtx, args)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "[IM] Command /%s error: %v", cmd.Name(), err)
|
||||
_ = adapter.SendReply(ctx, msg, &ReplyMessage{
|
||||
Content: "抱歉,执行指令时出现了异常,请稍后再试。",
|
||||
IsFinal: true,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// Handle service-level side effects.
|
||||
switch result.Action {
|
||||
case ActionClear:
|
||||
// Soft-delete the current ChannelSession and clear the LLM context
|
||||
// so the next message creates a completely fresh conversation.
|
||||
if err := s.db.Model(&ChannelSession{}).
|
||||
Where("id = ?", channelSession.ID).
|
||||
Update("deleted_at", time.Now()).Error; err != nil {
|
||||
logger.Warnf(ctx, "[IM] Failed to soft-delete channel session: %v", err)
|
||||
}
|
||||
if err := s.sessionService.ClearContext(ctx, channelSession.SessionID); err != nil {
|
||||
logger.Warnf(ctx, "[IM] Failed to clear session context: %v", err)
|
||||
}
|
||||
case ActionStop:
|
||||
// Cancel the request — first check if it's queued, then check if it's in-flight.
|
||||
inflightKey := makeUserKey(channel.ID, msg.UserID, msg.ChatID)
|
||||
if s.qaQueue.Remove(inflightKey) {
|
||||
logger.Infof(ctx, "[IM] Cancelled queued QA: key=%s", inflightKey)
|
||||
} else if cancelFn, loaded := s.inflight.LoadAndDelete(inflightKey); loaded {
|
||||
cancelFn.(context.CancelFunc)()
|
||||
logger.Infof(ctx, "[IM] Cancelled in-flight QA: key=%s", inflightKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Send the command reply, respecting the configured output mode.
|
||||
sent := false
|
||||
if channel.OutputMode != "full" {
|
||||
if streamer, ok := adapter.(StreamSender); ok {
|
||||
if err := s.sendStreamReply(ctx, msg, streamer, result.Content); err != nil {
|
||||
logger.Warnf(ctx, "[IM] Stream reply for command /%s failed, falling back: %v", cmd.Name(), err)
|
||||
} else {
|
||||
sent = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !sent {
|
||||
_ = adapter.SendReply(ctx, msg, &ReplyMessage{
|
||||
Content: result.Content,
|
||||
IsFinal: true,
|
||||
})
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[IM] Command /%s executed: channel=%s user=%s action=%d",
|
||||
cmd.Name(), channel.ID, msg.UserID, result.Action)
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendStreamReply sends a complete content string via the streaming interface
|
||||
// (StartStream → SendStreamChunk → EndStream). This is used for command replies
|
||||
// when the output mode is set to "stream", so they visually match QA responses.
|
||||
func (s *Service) sendStreamReply(ctx context.Context, msg *IncomingMessage, streamer StreamSender, content string) error {
|
||||
streamID, err := streamer.StartStream(ctx, msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("start stream: %w", err)
|
||||
}
|
||||
if err := streamer.SendStreamChunk(ctx, msg, streamID, content); err != nil {
|
||||
return fmt.Errorf("send stream chunk: %w", err)
|
||||
}
|
||||
if err := streamer.EndStream(ctx, msg, streamID); err != nil {
|
||||
return fmt.Errorf("end stream: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -576,6 +806,7 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
|
||||
// (across flush boundaries) was '\n'. This lets ensureNewlineBefore
|
||||
// work correctly even after buf has been Reset by a flush.
|
||||
lastCharNewline = true
|
||||
streamedAny bool // whether any user-visible content was written to buf
|
||||
)
|
||||
closeDone := func() { closeOnce.Do(func() { close(done) }) }
|
||||
|
||||
@@ -625,6 +856,7 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
|
||||
}
|
||||
|
||||
bufWrite(data.Content)
|
||||
streamedAny = true
|
||||
bufMu.Unlock()
|
||||
|
||||
if data.Done {
|
||||
@@ -774,20 +1006,33 @@ loop:
|
||||
// Final flush of any remaining content
|
||||
flush()
|
||||
|
||||
// If no user-visible content was streamed (e.g., the entire response was
|
||||
// in <think> blocks, or the QA pipeline errored), send a fallback message
|
||||
// as the last chunk so the Feishu card doesn't end up empty.
|
||||
bufMu.Lock()
|
||||
answer := answerBuilder.String()
|
||||
finalErr := qaErr
|
||||
noVisibleContent := !streamedAny
|
||||
bufMu.Unlock()
|
||||
|
||||
if noVisibleContent {
|
||||
fallback := "抱歉,我暂时无法回答这个问题。"
|
||||
if finalErr != nil {
|
||||
fallback = "抱歉,处理您的问题时出现了异常,请稍后再试。"
|
||||
}
|
||||
if err := streamer.SendStreamChunk(ctx, msg, streamID, fallback); err != nil {
|
||||
logger.Warnf(ctx, "[IM] SendStreamChunk fallback failed: %v", err)
|
||||
}
|
||||
if answer == "" {
|
||||
answer = fallback
|
||||
}
|
||||
}
|
||||
|
||||
// End the stream
|
||||
if err := streamer.EndStream(ctx, msg, streamID); err != nil {
|
||||
logger.Warnf(ctx, "[IM] EndStream failed: %v", err)
|
||||
}
|
||||
|
||||
// Persist the full answer
|
||||
bufMu.Lock()
|
||||
answer := answerBuilder.String()
|
||||
finalErr := qaErr
|
||||
bufMu.Unlock()
|
||||
|
||||
if answer == "" && finalErr != nil {
|
||||
answer = "抱歉,处理您的问题时出现了异常,请稍后再试。"
|
||||
}
|
||||
if answer == "" {
|
||||
answer = "抱歉,我暂时无法回答这个问题。"
|
||||
}
|
||||
@@ -1434,20 +1679,20 @@ func (s *Service) watchAndSendSummary(
|
||||
case types.ParseStatusCompleted:
|
||||
// Parsing done. If summary generation is in progress, wait for it.
|
||||
switch knowledge.SummaryStatus {
|
||||
case types.SummaryStatusNone, "":
|
||||
// No summary task configured. For image files the VLM caption
|
||||
// is stored in Description by finalizeImageKnowledge, so we
|
||||
// still show it if present.
|
||||
if knowledge.Description != "" && knowledge.Description != fileName {
|
||||
_ = s.sendSmartReply(ctx, adapter, msg, channel,
|
||||
fmt.Sprintf("用户之前上传的文件「%s」已解析完成。以下是文件的完整摘要内容:\n%s\n\n请生成一条通知消息,包含:1) 告知文件已解析完成;2) 用 Markdown 格式(标题、列表、加粗等)结构化展示上述摘要内容,不要删减或概括;3) 提示用户可以针对该文件提问。", fileName, knowledge.Description),
|
||||
fmt.Sprintf("📄 文件「%s」已解析完成。\n\n**摘要:**\n\n%s\n\n---\n可以针对该文件进行提问。", fileName, knowledge.Description))
|
||||
} else {
|
||||
_ = s.sendSmartReply(ctx, adapter, msg, channel,
|
||||
fmt.Sprintf("用户之前上传的文件「%s」已解析完成,现在可以开始针对该文件进行提问了。", fileName),
|
||||
fmt.Sprintf("📄 文件「%s」已解析完成,可以开始提问了!", fileName))
|
||||
}
|
||||
return
|
||||
case types.SummaryStatusNone, "":
|
||||
// No summary task configured. For image files the VLM caption
|
||||
// is stored in Description by finalizeImageKnowledge, so we
|
||||
// still show it if present.
|
||||
if knowledge.Description != "" && knowledge.Description != fileName {
|
||||
_ = s.sendSmartReply(ctx, adapter, msg, channel,
|
||||
fmt.Sprintf("用户之前上传的文件「%s」已解析完成。以下是文件的完整摘要内容:\n%s\n\n请生成一条通知消息,包含:1) 告知文件已解析完成;2) 用 Markdown 格式(标题、列表、加粗等)结构化展示上述摘要内容,不要删减或概括;3) 提示用户可以针对该文件提问。", fileName, knowledge.Description),
|
||||
fmt.Sprintf("📄 文件「%s」已解析完成。\n\n**摘要:**\n\n%s\n\n---\n可以针对该文件进行提问。", fileName, knowledge.Description))
|
||||
} else {
|
||||
_ = s.sendSmartReply(ctx, adapter, msg, channel,
|
||||
fmt.Sprintf("用户之前上传的文件「%s」已解析完成,现在可以开始针对该文件进行提问了。", fileName),
|
||||
fmt.Sprintf("📄 文件「%s」已解析完成,可以开始提问了!", fileName))
|
||||
}
|
||||
return
|
||||
|
||||
case types.SummaryStatusCompleted:
|
||||
// Summary is ready — send it
|
||||
|
||||
@@ -202,8 +202,11 @@ func (c *LongConnClient) Stop() {
|
||||
// SendReply sends a text reply through the WebSocket connection.
|
||||
// This is used by the IM service to reply to messages in long connection mode.
|
||||
func (c *LongConnClient) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error {
|
||||
reqID, ok := incoming.Extra["req_id"]
|
||||
if !ok || reqID == "" {
|
||||
var reqID string
|
||||
if incoming.Extra != nil {
|
||||
reqID = incoming.Extra["req_id"]
|
||||
}
|
||||
if reqID == "" {
|
||||
return fmt.Errorf("missing req_id in incoming message extra")
|
||||
}
|
||||
|
||||
@@ -236,7 +239,7 @@ func (c *LongConnClient) SendReply(ctx context.Context, incoming *im.IncomingMes
|
||||
// StartStream begins a streaming reply session.
|
||||
// Returns a stream ID that must be used in subsequent chunk/end calls.
|
||||
func (c *LongConnClient) StartStream(ctx context.Context, incoming *im.IncomingMessage) (string, error) {
|
||||
if _, ok := incoming.Extra["req_id"]; !ok {
|
||||
if incoming.Extra == nil || incoming.Extra["req_id"] == "" {
|
||||
return "", fmt.Errorf("missing req_id in incoming message extra")
|
||||
}
|
||||
streamID := fmt.Sprintf("stream_%d", c.reqSeq.Add(1))
|
||||
@@ -288,7 +291,13 @@ func (c *LongConnClient) EndStream(ctx context.Context, incoming *im.IncomingMes
|
||||
}
|
||||
|
||||
func (c *LongConnClient) sendStreamFrame(incoming *im.IncomingMessage, streamID, content string, finish bool) error {
|
||||
reqID := incoming.Extra["req_id"]
|
||||
var reqID string
|
||||
if incoming.Extra != nil {
|
||||
reqID = incoming.Extra["req_id"]
|
||||
}
|
||||
if reqID == "" {
|
||||
return fmt.Errorf("missing req_id in incoming message extra")
|
||||
}
|
||||
|
||||
body := streamReplyBody{MsgType: "stream"}
|
||||
body.Stream.ID = streamID
|
||||
@@ -324,6 +333,12 @@ func (c *LongConnClient) connectAndRun(ctx context.Context) error {
|
||||
c.conn = nil
|
||||
c.mu.Unlock()
|
||||
_ = conn.Close()
|
||||
|
||||
// Clear in-flight stream buffers to prevent memory leaks on reconnect.
|
||||
// Streams interrupted by a connection drop cannot be resumed.
|
||||
c.streamBufsMu.Lock()
|
||||
c.streamBufs = nil
|
||||
c.streamBufsMu.Unlock()
|
||||
}()
|
||||
|
||||
// Authenticate
|
||||
|
||||
@@ -212,21 +212,75 @@ func (a *WebhookAdapter) ParseCallback(c *gin.Context) (*im.IncomingMessage, err
|
||||
}
|
||||
|
||||
// SendReply sends a reply message via WeCom API.
|
||||
// For simplicity, this uses the "应用消息" API to proactively send messages.
|
||||
// A production implementation would integrate with the streaming callback model.
|
||||
// For group chats, it tries the appchat API first to reply in the group,
|
||||
// then falls back to sending a direct message to the user.
|
||||
func (a *WebhookAdapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error {
|
||||
// Get access token
|
||||
accessToken, err := a.getAccessToken(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get access token: %w", err)
|
||||
}
|
||||
|
||||
// Build message payload.
|
||||
// Note: /cgi-bin/message/send only supports touser/toparty/totag — it does NOT
|
||||
// support regular group chat IDs (chatid is only for appchat-created groups).
|
||||
// So for both direct and group messages we reply to the user directly.
|
||||
// For group chats, try sending to the group via appchat API first.
|
||||
// This works for groups created via /cgi-bin/appchat/create.
|
||||
if incoming.ChatType == im.ChatTypeGroup && incoming.ChatID != "" {
|
||||
if err := a.sendToAppChat(ctx, accessToken, incoming.ChatID, reply); err == nil {
|
||||
return nil
|
||||
}
|
||||
logger.Debugf(ctx, "[WeCom] appchat/send failed for chat=%s, falling back to touser: %v", incoming.ChatID, err)
|
||||
}
|
||||
|
||||
// Fallback (or direct message): send to the user directly.
|
||||
return a.sendToUser(ctx, accessToken, incoming.UserID, reply)
|
||||
}
|
||||
|
||||
// sendToAppChat sends a message to a WeCom group chat via the appchat API.
|
||||
// Reference: https://developer.work.weixin.qq.com/document/path/90248
|
||||
func (a *WebhookAdapter) sendToAppChat(ctx context.Context, accessToken, chatID string, reply *im.ReplyMessage) error {
|
||||
payload := map[string]interface{}{
|
||||
"touser": incoming.UserID,
|
||||
"chatid": chatID,
|
||||
"msgtype": "markdown",
|
||||
"markdown": map[string]string{
|
||||
"content": reply.Content,
|
||||
},
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal payload: %w", err)
|
||||
}
|
||||
|
||||
sendURL := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=%s", accessToken)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, sendURL, bytes.NewReader(payloadBytes))
|
||||
if err != nil {
|
||||
return fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("send appchat message: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return fmt.Errorf("decode response: %w", err)
|
||||
}
|
||||
if result.ErrCode != 0 {
|
||||
return fmt.Errorf("appchat api error: code=%d msg=%s", result.ErrCode, result.ErrMsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendToUser sends a message directly to a user via the application message API.
|
||||
// Reference: https://developer.work.weixin.qq.com/document/path/90236
|
||||
func (a *WebhookAdapter) sendToUser(ctx context.Context, accessToken, userID string, reply *im.ReplyMessage) error {
|
||||
payload := map[string]interface{}{
|
||||
"touser": userID,
|
||||
"msgtype": "markdown",
|
||||
"agentid": a.corpAgentID,
|
||||
"markdown": map[string]string{
|
||||
|
||||
Reference in New Issue
Block a user