Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

49.5 KB
Raw Blame
// Package feishu implements the Feishu (飞书) IM adapter for WeKnora, and with
// it the Lark adapter: Feishu and Lark are the same product on two isolated
// clouds (open.feishu.cn and open.larksuite.com) sharing one API surface, so a
// single implementation serves both. A Region picks the cloud — see region.go.
//
// Bot flow:
// 1. User sends a message to the bot (direct or @mention in group)
// 2. Feishu/Lark calls our event subscription URL with the message event
// 3. We parse the event, run QA, then call the Open Platform API to send a reply
// 4. For streaming: create a card, then use CardKit streaming update API
//
// Reference: https://open.feishu.cn/document/server-docs/im-v1/message/create
package feishu

import (
	"bytes"
	"context"
	"crypto/aes"
	"crypto/cipher"
	"crypto/sha256"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"mime"
	"mime/multipart"
	"net/http"
	"net/url"
	"regexp"
	"strings"
	"sync"
	"time"
	"unicode/utf8"

	"github.com/Tencent/WeKnora/internal/im"
	"github.com/Tencent/WeKnora/internal/logger"
	"github.com/Tencent/WeKnora/internal/utils"
	"github.com/gin-gonic/gin"
	larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
)

// Compile-time checks for the optional IM capabilities implemented by Adapter.
var (
	_ im.StreamSender             = (*Adapter)(nil)
	_ im.FullOutputProgressSender = (*Adapter)(nil)
	_ im.FileDownloader           = (*Adapter)(nil)
)

var httpClient = utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{
	Timeout:      10 * time.Second,
	MaxRedirects: 5,
})

// Adapter implements im.Adapter for Feishu/Lark.
type Adapter struct {
	region            Region
	appID             string
	appSecret         string
	verificationToken string
	encryptKey        string

	// apiBaseURL overrides the region's Open Platform API origin. Empty defaults
	// to region.OpenBaseURL; set to a reverse-proxy origin for private/internal
	// deployments that cannot reach open.feishu.cn directly.
	apiBaseURL string

	// Token cache
	tokenMu    sync.Mutex
	tokenCache string
	tokenExpAt time.Time

	// botOpenID is this app's own bot open_id. Feishu reports every bot
	// mention with mentioned_type == "bot"; the open_id is what distinguishes
	// this bot from other bots in the same group.
	botOpenIDMu sync.Mutex
	botOpenID   string
}

// NewAdapter creates a new adapter for the given region (RegionFeishu or RegionLark).
// apiBaseURL overrides the Open Platform API origin (empty uses the region
// default); it is validated for scheme and SSRF safety before use.
func NewAdapter(region Region, appID, appSecret, verificationToken, encryptKey, apiBaseURL string) (*Adapter, error) {
	apiBaseURL = strings.TrimRight(strings.TrimSpace(apiBaseURL), "/")
	if err := validateAPIBaseURL(apiBaseURL, region.OpenBaseURL); err != nil {
		return nil, err
	}
	if apiBaseURL == "" {
		apiBaseURL = region.OpenBaseURL
	}
	startStreamReaper()
	return &Adapter{
		region:            region,
		appID:             appID,
		appSecret:         appSecret,
		verificationToken: verificationToken,
		encryptKey:        encryptKey,
		apiBaseURL:        apiBaseURL,
	}, nil
}

// BotOpenID returns the app's own bot open_id, fetching and caching it on the
// first use. Both the WebSocket and webhook event paths use this value to
// distinguish a mention of this bot from mentions of other bots in a group.
func (a *Adapter) BotOpenID(ctx context.Context) (string, error) {
	a.botOpenIDMu.Lock()
	defer a.botOpenIDMu.Unlock()

	if a.botOpenID != "" {
		return a.botOpenID, nil
	}

	accessToken, err := a.getTenantAccessToken(ctx)
	if err != nil {
		return "", fmt.Errorf("get access token: %w", err)
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, a.api("/open-apis/bot/v3/info"), nil)
	if err != nil {
		return "", fmt.Errorf("create bot info request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("request bot info: %w", err)
	}
	defer func() { _ = resp.Body.Close() }()

	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
		Bot  struct {
			OpenID string `json:"open_id"`
		} `json:"bot"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return "", fmt.Errorf("decode bot info: %w", err)
	}
	if result.Code != 0 {
		return "", fmt.Errorf("bot info error: code=%d msg=%s", result.Code, result.Msg)
	}
	if result.Bot.OpenID == "" {
		return "", fmt.Errorf("bot info response missing open_id")
	}

	a.botOpenID = result.Bot.OpenID
	return a.botOpenID, nil
}

// validateAPIBaseURL checks that a custom Feishu/Lark API base URL uses an
// http(s) scheme and passes SSRF validation. Empty or the region default is
// allowed without further checks. Mirrors wecom.validateEndpointURL but allows
// plain http for internal-network reverse proxies that terminate TLS at nginx.
func validateAPIBaseURL(endpoint, defaultEndpoint string) error {
	if endpoint == "" || endpoint == defaultEndpoint {
		return nil
	}
	u, err := url.Parse(endpoint)
	if err != nil {
		return fmt.Errorf("invalid api_base_url: %w", err)
	}
	if u.Scheme != "http" && u.Scheme != "https" {
		return fmt.Errorf("api_base_url must use http(s):// scheme, got %s://", u.Scheme)
	}
	if err := utils.ValidateURLForSSRF(endpoint); err != nil {
		return fmt.Errorf("%w (for private deployments on internal networks, add the hostname to SSRF_WHITELIST)", err)
	}
	return nil
}

// api builds an Open Platform API URL on this adapter's cloud. path is a format
// string beginning with "/open-apis/"; args fill its verbs.
func (a *Adapter) api(path string, args ...any) string {
	return a.apiBaseURL + fmt.Sprintf(path, args...)
}

// 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 now := <-ticker.C:
					reapOrphanStreams(now)
				case <-reaperStopCh:
					return
				}
			}
		}()
	})
}

func reapOrphanStreams(now time.Time) {
	feishuStreamsMu.Lock()
	defer feishuStreamsMu.Unlock()
	for id, state := range feishuStreams {
		if state.ownerDone != nil {
			select {
			case <-state.ownerDone:
			default:
				// Full-output QA can be silent for longer than the orphan TTL.
				// Keep a grace period after cancellation for final delivery too.
				state.lastActivityAt = now
				continue
			}
		}
		if state.lastActivityAt.Before(now.Add(-streamOrphanTTL)) {
			delete(feishuStreams, id)
		}
	}
}

func lookupStream(streamID string) (*feishuStreamState, bool) {
	feishuStreamsMu.Lock()
	defer feishuStreamsMu.Unlock()
	state, ok := feishuStreams[streamID]
	if ok {
		// Attempts count as activity even when the remote API rejects an update.
		state.lastActivityAt = time.Now()
	}
	return state, ok
}

// 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 a.region.Platform
}

// SupportsFullOutputProgress reports that StartStream immediately sends a
// visible, replaceable thinking card suitable for full-output mode.
func (a *Adapter) SupportsFullOutputProgress() bool {
	return true
}

// VerifyCallback verifies the Feishu event callback by checking the verification token.
// If no verification token is configured (e.g., WebSocket mode), skip verification.
func (a *Adapter) VerifyCallback(c *gin.Context) error {
	if a.verificationToken == "" {
		return fmt.Errorf("webhook verification secret is required")
	}

	im.LimitWebhookBody(c)
	bodyBytes, err := io.ReadAll(c.Request.Body)
	if err != nil {
		return fmt.Errorf("read body: %w", err)
	}
	// Always restore body for subsequent reads (ParseCallback)
	defer func() { c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) }()

	var raw []byte

	// Handle encrypted events
	var encryptedBody struct {
		Encrypt string `json:"encrypt"`
	}
	if err := json.Unmarshal(bodyBytes, &encryptedBody); err == nil && encryptedBody.Encrypt != "" {
		decrypted, err := a.decrypt(encryptedBody.Encrypt)
		if err != nil {
			return fmt.Errorf("decrypt event for verification: %w", err)
		}
		raw = decrypted
	} else {
		raw = bodyBytes
	}

	var eventBody struct {
		Header *feishuEventHeader `json:"header"`
	}
	if err := json.Unmarshal(raw, &eventBody); err != nil {
		return fmt.Errorf("unmarshal event header: %w", err)
	}

	if eventBody.Header == nil || eventBody.Header.Token != a.verificationToken {
		return fmt.Errorf("invalid verification token")
	}

	return nil
}

// HandleURLVerification handles the Feishu URL verification challenge.
func (a *Adapter) HandleURLVerification(c *gin.Context) bool {
	im.LimitWebhookBody(c)
	bodyBytes, err := io.ReadAll(c.Request.Body)
	if err != nil {
		return false
	}
	c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))

	// Try to parse as a challenge request
	var body map[string]interface{}

	// If encrypted, try to decrypt first
	var encryptedBody struct {
		Encrypt string `json:"encrypt"`
	}
	if err := json.Unmarshal(bodyBytes, &encryptedBody); err == nil && encryptedBody.Encrypt != "" {
		decrypted, err := a.decrypt(encryptedBody.Encrypt)
		if err != nil {
			logger.Errorf(c.Request.Context(), "[%s] Failed to decrypt: %v", a.region.Label, err)
			return false
		}
		if err := json.Unmarshal(decrypted, &body); err != nil {
			return false
		}
	} else {
		if err := json.Unmarshal(bodyBytes, &body); err != nil {
			return false
		}
	}

	// Check if this is a URL verification challenge
	if challenge, ok := body["challenge"].(string); ok {
		c.JSON(http.StatusOK, gin.H{"challenge": challenge})
		return true
	}

	// Reset body for subsequent reads
	c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
	return false
}

// feishuEventBody is the typed structure of a Feishu event callback.
type feishuEventBody struct {
	Header *feishuEventHeader `json:"header"`
	Event  *feishuEvent       `json:"event"`
}

type feishuEventHeader struct {
	EventType string `json:"event_type"`
	Token     string `json:"token"`
}

type feishuEvent struct {
	Message *feishuMessage `json:"message"`
	Sender  *feishuSender  `json:"sender"`
}

type feishuMessage struct {
	MessageID   string                 `json:"message_id"`
	RootID      string                 `json:"root_id"`
	ParentID    string                 `json:"parent_id"`
	MessageType string                 `json:"message_type"`
	ChatType    string                 `json:"chat_type"`
	ChatID      string                 `json:"chat_id"`
	Content     string                 `json:"content"`
	Mentions    []*larkim.MentionEvent `json:"mentions,omitempty"`
}

type feishuSender struct {
	SenderID *feishuSenderID `json:"sender_id"`
}

type feishuSenderID struct {
	OpenID string `json:"open_id"`
}

// ParseCallback parses a Feishu event callback into a unified IncomingMessage.
func (a *Adapter) ParseCallback(c *gin.Context) (*im.IncomingMessage, error) {
	im.LimitWebhookBody(c)
	bodyBytes, err := io.ReadAll(c.Request.Body)
	if err != nil {
		return nil, fmt.Errorf("read body: %w", err)
	}

	var raw []byte

	// Handle encrypted events
	var encryptedBody struct {
		Encrypt string `json:"encrypt"`
	}
	if err := json.Unmarshal(bodyBytes, &encryptedBody); err == nil && encryptedBody.Encrypt != "" {
		decrypted, err := a.decrypt(encryptedBody.Encrypt)
		if err != nil {
			return nil, fmt.Errorf("decrypt event: %w", err)
		}
		raw = decrypted
	} else {
		raw = bodyBytes
	}

	var eventBody feishuEventBody
	if err := json.Unmarshal(raw, &eventBody); err != nil {
		return nil, fmt.Errorf("unmarshal event: %w", err)
	}

	// Token verification is handled by VerifyCallback; no need to re-check here.

	// Check event type
	if eventBody.Header == nil || eventBody.Header.EventType != "im.message.receive_v1" {
		if eventBody.Header != nil {
			logger.Infof(c.Request.Context(), "[%s] Ignoring event type: %s", a.region.Label, eventBody.Header.EventType)
		}
		return nil, nil
	}

	// Extract message info
	if eventBody.Event == nil || eventBody.Event.Message == nil {
		return nil, nil
	}
	msg := eventBody.Event.Message

	// Keep webhook delivery in sync with the WebSocket path. In group chats
	// Feishu may deliver every message when the app has the "receive all group
	// messages" permission; only continue when this app's own bot is mentioned.
	if msg.ChatType == "group" {
		botOpenID, err := a.BotOpenID(c.Request.Context())
		if err != nil {
			logger.Warnf(c.Request.Context(),
				"[%s] failed to resolve bot open_id; ignoring group message: %v", a.region.Label, err)
			return nil, nil
		}
		if !mentionsBot(botOpenID, msg.Mentions) {
			logger.Infof(c.Request.Context(),
				"[%s] ignoring group message without this bot mention: msg_id=%q", a.region.Label, msg.MessageID)
			return nil, nil
		}
	}

	// Compute thread ID: use root_id for threaded replies, or message_id for top-level messages.
	threadID := msg.RootID
	if threadID == "" {
		threadID = msg.MessageID
	}

	// Determine chat type
	chatType := im.ChatTypeDirect
	chatID := ""
	if msg.ChatType == "group" {
		chatType = im.ChatTypeGroup
		chatID = msg.ChatID
	}

	// Get sender info
	openID := ""
	if eventBody.Event.Sender != nil && eventBody.Event.Sender.SenderID != nil {
		openID = eventBody.Event.Sender.SenderID.OpenID
	}

	switch msg.MessageType {
	case "text":
		// Parse text content
		var textContent struct {
			Text string `json:"text"`
		}
		if err := json.Unmarshal([]byte(msg.Content), &textContent); err != nil {
			return nil, fmt.Errorf("unmarshal text content: %w", err)
		}

		// Strip @bot mention from group messages
		content := textContent.Text
		if chatType == im.ChatTypeGroup {
			for strings.HasPrefix(content, "@_user_") {
				idx := strings.Index(content, " ")
				if idx >= 0 {
					content = content[idx+1:]
				} else {
					break
				}
			}
		}

		return &im.IncomingMessage{
			Platform:    a.region.Platform,
			MessageType: im.MessageTypeText,
			UserID:      openID,
			ChatID:      chatID,
			ChatType:    chatType,
			Content:     strings.TrimSpace(content),
			MessageID:   msg.MessageID,
			ThreadID:    threadID,
		}, nil

	case "file":
		var fileContent struct {
			FileKey  string `json:"file_key"`
			FileName string `json:"file_name"`
		}
		if err := json.Unmarshal([]byte(msg.Content), &fileContent); err != nil {
			return nil, fmt.Errorf("unmarshal file content: %w", err)
		}
		if fileContent.FileKey == "" {
			return nil, nil
		}
		return &im.IncomingMessage{
			Platform:    a.region.Platform,
			MessageType: im.MessageTypeFile,
			UserID:      openID,
			ChatID:      chatID,
			ChatType:    chatType,
			MessageID:   msg.MessageID,
			ThreadID:    threadID,
			FileKey:     fileContent.FileKey,
			FileName:    fileContent.FileName,
		}, nil

	case "image":
		var imageContent struct {
			ImageKey string `json:"image_key"`
		}
		if err := json.Unmarshal([]byte(msg.Content), &imageContent); err != nil {
			return nil, fmt.Errorf("unmarshal image content: %w", err)
		}
		if imageContent.ImageKey == "" {
			return nil, nil
		}
		return &im.IncomingMessage{
			Platform:    a.region.Platform,
			MessageType: im.MessageTypeImage,
			UserID:      openID,
			ChatID:      chatID,
			ChatType:    chatType,
			MessageID:   msg.MessageID,
			ThreadID:    threadID,
			FileKey:     imageContent.ImageKey,
			FileName:    imageContent.ImageKey + ".png",
		}, nil

	case "post":
		// Rich text: extract plain text for QA
		var postContent struct {
			Title   string              `json:"title"`
			Content [][]json.RawMessage `json:"content"`
		}
		if err := json.Unmarshal([]byte(msg.Content), &postContent); err != nil {
			return nil, fmt.Errorf("unmarshal post content: %w", err)
		}

		var textParts []string
		if postContent.Title != "" {
			textParts = append(textParts, postContent.Title)
		}
		for _, line := range postContent.Content {
			var lineText strings.Builder
			for _, elem := range line {
				var tag struct {
					Tag  string `json:"tag"`
					Text string `json:"text"`
				}
				if err := json.Unmarshal(elem, &tag); err != nil {
					continue
				}
				switch tag.Tag {
				case "text", "a":
					lineText.WriteString(tag.Text)
				}
			}
			if t := strings.TrimSpace(lineText.String()); t != "" {
				textParts = append(textParts, t)
			}
		}

		content := strings.Join(textParts, "\n")
		if chatType == im.ChatTypeGroup {
			for strings.HasPrefix(content, "@_user_") {
				idx := strings.Index(content, " ")
				if idx >= 0 {
					content = content[idx+1:]
				} else {
					break
				}
			}
		}
		content = strings.TrimSpace(content)
		if content == "" {
			return nil, nil
		}

		return &im.IncomingMessage{
			Platform:    a.region.Platform,
			MessageType: im.MessageTypeText,
			UserID:      openID,
			ChatID:      chatID,
			ChatType:    chatType,
			Content:     content,
			MessageID:   msg.MessageID,
			ThreadID:    threadID,
		}, nil

	default:
		logger.Infof(c.Request.Context(), "[%s] Ignoring unsupported message type: %s", a.region.Label, msg.MessageType)
		return nil, nil
	}
}

// SendReply sends a reply message via Feishu API.
//
// It uses the "reply message" API (POST /im/v1/messages/:message_id/reply) so the
// reply lands under the original message / thread instead of creating a new
// top-level message (which, in topic-enabled groups, would spawn a new topic).
// Feishu automatically replies in thread form when the replied-to message is
// already a thread message, so we leave reply_in_thread at its default (false).
//
// If the reply API fails with a fallback-eligible code (e.g. 230071 group does
// not support reply-in-thread, 230019 topic gone, 230054 unsupported message
// type), we retry once via the plain "send message" API so the reply still
// reaches the user.
func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error {
	for _, text := range splitTextReply(reply.Content) {
		if err := a.sendTextReply(ctx, incoming, text); err != nil {
			return err
		}
	}
	return nil
}

// Keep even worst-case JSON escaping below 20 KiB, including the surrounding
// payload, so a long final answer can use the plain-message fallback safely.
const textReplyChunkBytes = 2 * 1024

func splitTextReply(text string) []string {
	var parts []string
	for len(text) > textReplyChunkBytes {
		cut := textReplyChunkBytes
		for !utf8.RuneStart(text[cut]) {
			cut--
		}
		if newline := strings.LastIndexByte(text[:cut], '\n'); newline >= cut/2 {
			cut = newline + 1
		}
		parts = append(parts, text[:cut])
		text = text[cut:]
	}
	return append(parts, text)
}

func (a *Adapter) sendTextReply(ctx context.Context, incoming *im.IncomingMessage, text string) error {
	accessToken, err := a.getTenantAccessToken(ctx)
	if err != nil {
		return fmt.Errorf("get access token: %w", err)
	}

	// Build text message content
	content, _ := json.Marshal(map[string]string{"text": text})

	// Reply payload (no receive_id — the path message_id locates the chat)
	replyPayload := map[string]interface{}{
		"msg_type": "text",
		"content":  string(content),
	}

	// Fallback payload (plain send-message API) — needs receive_id
	receiveIDType, receiveID := a.resolveReceiveID(incoming)
	fallbackPayload := map[string]interface{}{
		"receive_id": receiveID,
		"msg_type":   "text",
		"content":    string(content),
	}

	return a.sendWithFallback(ctx, accessToken, incoming, replyPayload, fallbackPayload, receiveIDType)
}

// resolveReceiveID computes the (receive_id_type, receive_id) pair used by the
// plain "send message" API. Group chats target chat_id; direct chats target the
// user's open_id. Used both for the fallback path and for legacy callers.
func (a *Adapter) resolveReceiveID(incoming *im.IncomingMessage) (receiveIDType string, receiveID string) {
	receiveIDType = "open_id"
	receiveID = incoming.UserID
	if incoming.ChatType == im.ChatTypeGroup && incoming.ChatID != "" {
		receiveIDType = "chat_id"
		receiveID = incoming.ChatID
	}
	return
}

// fallbackEligibleErrorCodes are Feishu API error codes for which replying via
// the reply-message API cannot work (e.g. the group does not support threads,
// the topic was deleted, the message type is unsupported). In these cases we
// retry once via the plain send-message API so the reply still reaches the user.
var fallbackEligibleErrorCodes = map[int]bool{
	230019: true, // The topic does NOT exist.
	230054: true, // This operation is not supported for this message type.
	230071: true, // The group to which the message belongs does not support reply in thread.
}

// sendWithFallback sends a message via the reply-message API first
// (POST /im/v1/messages/:message_id/reply), and on a fallback-eligible error
// retries once via the plain send-message API (POST /im/v1/messages).
//
//   - replyPayload   — body for the reply API (no receive_id needed)
//   - fallbackPayload — body for the send API (includes receive_id)
//   - receiveIDType   — receive_id_type query param for the fallback send API
func (a *Adapter) sendWithFallback(
	ctx context.Context,
	accessToken string,
	incoming *im.IncomingMessage,
	replyPayload, fallbackPayload map[string]interface{},
	receiveIDType string,
) error {
	// If we have a usable message_id, try the reply API first so the reply
	// lands under the original message / thread. If message_id is empty (rare:
	// some event payloads omit it), skip straight to the fallback send API —
	// the reply API needs a message_id in the path and can't work without one.
	if incoming.MessageID != "" && feishuSafePathParam(incoming.MessageID) {
		replyURL := a.api("/open-apis/im/v1/messages/%s/reply", incoming.MessageID)
		code, msg, err := a.postFeishuMessage(ctx, accessToken, replyURL, replyPayload)
		switch {
		case err != nil:
			// Network/transport error — try the fallback since send-message is
			// a different endpoint that might succeed.
			logger.Warnf(ctx, "[%s] reply API transport error (will try fallback): %v", a.region.Label, err)
		case code == 0:
			return nil
		case !fallbackEligibleErrorCodes[code]:
			return fmt.Errorf("%s reply api error: code=%d msg=%s", a.region.Label, code, msg)
		default:
			// Fallback-eligible: retry via plain send-message API.
			logger.Warnf(ctx, "[%s] reply API returned code=%d msg=%s, falling back to send-message API",
				a.region.Label, code, msg)
		}
	} else if incoming.MessageID != "" {
		// message_id present but contains unsafe characters — refuse rather
		// than put it in a URL path. Don't fall back either, since a malformed
		// id suggests a tampered payload.
		return fmt.Errorf("invalid message_id for reply API: %q", incoming.MessageID)
	} else {
		logger.Warnf(ctx,
			"[%s] incoming message has no message_id; replying via send-message API (will not attach to thread)",
			a.region.Label)
	}

	// Fallback: plain send-message API.
	fallbackURL := a.api("/open-apis/im/v1/messages?receive_id_type=%s", receiveIDType)
	code, msg, err := a.postFeishuMessage(ctx, accessToken, fallbackURL, fallbackPayload)
	if err != nil {
		return fmt.Errorf("send message (fallback): %w", err)
	}
	if code != 0 {
		return fmt.Errorf("%s send api error: code=%d msg=%s", a.region.Label, code, msg)
	}
	return nil
}

// postFeishuMessage POSTs a JSON payload to a Feishu IM message API and returns
// the (code, msg) from the response body. Shared by reply and send paths.
func (a *Adapter) postFeishuMessage(ctx context.Context, accessToken, url string, payload map[string]interface{}) (code int, msg string, err error) {
	payloadBytes, _ := json.Marshal(payload)
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payloadBytes))
	if err != nil {
		return 0, "", fmt.Errorf("create request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return 0, "", fmt.Errorf("send request: %w", err)
	}
	defer resp.Body.Close()

	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return 0, "", fmt.Errorf("decode response: %w", err)
	}
	return result.Code, result.Msg, nil
}

// ──────────────────────────────────────────────────────────────────────
// File download support via Feishu GetMessageResource API
// ──────────────────────────────────────────────────────────────────────

// feishuSafePathParam checks that a Feishu API path parameter contains only
// safe characters (alphanumeric, hyphen, underscore). This prevents path
// traversal attacks via crafted callback payloads.
func feishuSafePathParam(s string) bool {
	for _, c := range s {
		if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_') {
			return false
		}
	}
	return len(s) > 0
}

// DownloadFile downloads a file or image attachment from a Feishu message.
// Uses the GetMessageResource API: GET /open-apis/im/v1/messages/:message_id/resources/:file_key?type={file|image}
func (a *Adapter) DownloadFile(ctx context.Context, msg *im.IncomingMessage) (io.ReadCloser, string, error) {
	if msg.FileKey == "" || msg.MessageID == "" {
		return nil, "", fmt.Errorf("file_key and message_id are required")
	}

	// SSRF/path-traversal protection: validate path parameters contain only safe characters
	if !feishuSafePathParam(msg.MessageID) || !feishuSafePathParam(msg.FileKey) {
		return nil, "", fmt.Errorf("invalid message_id or file_key format")
	}

	accessToken, err := a.getTenantAccessToken(ctx)
	if err != nil {
		return nil, "", fmt.Errorf("get access token: %w", err)
	}

	// Determine resource type based on message type
	resourceType := "file"
	if msg.MessageType == im.MessageTypeImage {
		resourceType = "image"
	}

	apiURL := a.api("/open-apis/im/v1/messages/%s/resources/%s?type=%s",
		msg.MessageID, msg.FileKey, resourceType)

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
	if err != nil {
		return nil, "", fmt.Errorf("create request: %w", err)
	}
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return nil, "", fmt.Errorf("download file: %w", err)
	}

	if resp.StatusCode != http.StatusOK {
		resp.Body.Close()
		return nil, "", fmt.Errorf("download file failed: status=%d", resp.StatusCode)
	}

	// Use the original file name from the message, or extract from Content-Disposition
	fileName := msg.FileName
	if fileName == "" {
		fileName = contentDispositionFilename(resp.Header.Get("Content-Disposition"))
	}
	if fileName == "" {
		fileName = msg.FileKey
	}

	return resp.Body, fileName, nil
}

// contentDispositionFilename extracts a filename using the standard parser so
// quoted parameters and RFC 5987 filename* are decoded correctly. A fallback
// keeps accepting malformed headers emitted by some proxies.
func contentDispositionFilename(cd string) string {
	if cd == "" {
		return ""
	}
	if _, params, err := mime.ParseMediaType(cd); err == nil {
		if name := params["filename"]; name != "" {
			return name
		}
	}
	idx := strings.Index(cd, "filename=")
	if idx < 0 {
		return ""
	}
	value := strings.TrimSpace(cd[idx+len("filename="):])
	if value == "" {
		return ""
	}
	if value[0] == '"' {
		if end := strings.IndexByte(value[1:], '"'); end >= 0 {
			return value[1 : end+1]
		}
	}
	if semi := strings.IndexByte(value, ';'); semi >= 0 {
		value = value[:semi]
	}
	return strings.Trim(strings.TrimSpace(value), "\"")
}

// ──────────────────────────────────────────────────────────────────────
// Feishu CardKit v1 streaming implementation (official best practice)
//
// Flow:
//  1. POST  /cardkit/v1/cards                                      — create card entity
//  2. POST  /im/v1/messages  content={"type":"card","data":{"card_id":"…"}} — send card
//  3. PUT   /cardkit/v1/cards/{id}/elements/{eid}/content          — stream element content
//  4. PATCH /cardkit/v1/cards/{id}/settings                        — finalize streaming and summary
//
// Reference: https://github.com/larksuite/openclaw-lark (official Lark plugin)
//            https://open.feishu.cn/document/cardkit-v1/streaming-updates-openapi-overview
// ──────────────────────────────────────────────────────────────────────

const (
	// streamingElementID is the element_id used in the card JSON for streaming content.
	streamingElementID = "streaming_content"
)

// feishuStreamState tracks per-stream accumulated content.
type feishuStreamState struct {
	mu         sync.Mutex
	content    strings.Builder
	contentSeq int64 // sequence of the last successfully sent content
	seq        int64 // strictly incrementing sequence for CardKit API
	// Protected by feishuStreamsMu, independently of content/sequence updates.
	lastActivityAt time.Time
	ownerDone      <-chan struct{}
}

const (
	// streamOrphanTTL bounds idle state after its request ends, or when the
	// caller supplies a context without cancellation. It is not a QA deadline.
	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{}

	startReaperOnce sync.Once
	reaperStopCh    = make(chan struct{})
)

func (s *feishuStreamState) nextSeq() int {
	s.seq++
	return int(s.seq)
}

func cardSummaryPreview(content string) string {
	content = feishuMarkdownImageRe.ReplaceAllString(content, "${1}")
	content = feishuMarkdownLinkRe.ReplaceAllString(content, "${1}")
	preview := []rune(strings.Join(strings.Fields(content), " "))
	if len(preview) > 120 {
		preview = preview[:120]
	}
	return string(preview)
}

// buildStreamingCardJSON builds a Card JSON 2.0 with streaming_mode enabled.
// The placeholder text follows the region so Lark users see English rather than
// the Chinese wording Feishu users expect.
func buildStreamingCardJSON(region Region) string {
	card := map[string]interface{}{
		"schema": "2.0",
		"config": map[string]interface{}{
			"streaming_mode": true,
			"summary":        map[string]string{"content": region.ThinkingText},
		},
		"header": map[string]interface{}{
			"template": "blue",
			"title":    map[string]string{"tag": "plain_text", "content": "WeKnora"},
		},
		"body": map[string]interface{}{
			"elements": []map[string]interface{}{
				{
					"tag":        "markdown",
					"content":    "💭 " + region.ThinkingText,
					"text_size":  "normal",
					"element_id": streamingElementID,
				},
			},
		},
	}
	b, _ := json.Marshal(card)
	return string(b)
}

// StartStream creates a CardKit card entity, sends it as a message, and returns the card_id.
func (a *Adapter) StartStream(ctx context.Context, incoming *im.IncomingMessage) (string, error) {
	accessToken, err := a.getTenantAccessToken(ctx)
	if err != nil {
		return "", fmt.Errorf("get access token: %w", err)
	}

	// 1. Create card entity via CardKit API
	cardJSON := buildStreamingCardJSON(a.region)
	cardID, err := a.cardkitCreate(ctx, accessToken, cardJSON)
	if err != nil {
		return "", fmt.Errorf("create card: %w", err)
	}

	// 2. Send the card as a message (content type="card")
	if err := a.sendCardByCardID(ctx, accessToken, incoming, cardID); err != nil {
		return "", fmt.Errorf("send card message: %w", err)
	}

	// 3. Track stream state
	feishuStreamsMu.Lock()
	feishuStreams[cardID] = &feishuStreamState{lastActivityAt: time.Now(), ownerDone: ctx.Done()}
	feishuStreamsMu.Unlock()

	logger.Infof(ctx, "[%s] Streaming started: card_id=%s", a.region.Label, cardID)
	return cardID, nil
}

// UpdateStreamContent replaces the card element with the full visible content so far.
func (a *Adapter) UpdateStreamContent(ctx context.Context, incoming *im.IncomingMessage, streamID string, fullContent string) error {
	if fullContent == "" {
		return nil
	}

	state, ok := lookupStream(streamID)
	if !ok {
		return fmt.Errorf("unknown stream ID: %s", streamID)
	}

	state.mu.Lock()
	seq := state.nextSeq()
	state.mu.Unlock()

	accessToken, err := a.getTenantAccessToken(ctx)
	if err != nil {
		return fmt.Errorf("get access token: %w", err)
	}

	// Feishu card markdown only accepts an uploaded image_key inside ![alt](...),
	// not an external HTTP/COS URL. Convert markdown image URLs to image_keys
	// (uploading on demand) so the card update does not fail with 200570.
	content := a.resolveMarkdownImages(ctx, accessToken, fullContent)

	if err := a.cardkitUpdateElement(ctx, accessToken, streamID, streamingElementID, content, seq); err != nil {
		return err
	}

	state.mu.Lock()
	if int64(seq) > state.contentSeq {
		state.content.Reset()
		state.content.WriteString(fullContent)
		state.contentSeq = int64(seq)
	}
	state.mu.Unlock()
	return nil
}

// FinalizeStream replaces the card with answer-only content.
func (a *Adapter) FinalizeStream(ctx context.Context, incoming *im.IncomingMessage, streamID string, finalContent string) error {
	return a.UpdateStreamContent(ctx, incoming, streamID, finalContent)
}

// SendStreamChunk is an alias for UpdateStreamContent.
func (a *Adapter) SendStreamChunk(ctx context.Context, incoming *im.IncomingMessage, streamID string, content string) error {
	return a.UpdateStreamContent(ctx, incoming, streamID, content)
}

// EndStream disables streaming_mode, replaces the temporary summary, and cleans up state.
func (a *Adapter) EndStream(ctx context.Context, incoming *im.IncomingMessage, streamID string) error {
	state, ok := lookupStream(streamID)
	if !ok {
		return fmt.Errorf("unknown stream ID: %s", streamID)
	}

	accessToken, err := a.getTenantAccessToken(ctx)
	if err != nil {
		return fmt.Errorf("get access token: %w", err)
	}

	var finalSummary *string
	state.mu.Lock()
	seq := state.nextSeq()
	preview := cardSummaryPreview(state.content.String())
	if preview != "" {
		finalSummary = &preview
	}
	state.mu.Unlock()

	// Always close streaming_mode under config (CardKit rejects a top-level
	// streaming_mode field). Attach a summary only when we have delivered text;
	// an empty summary would leave the create-time "thinking" preview in place
	// or blank the chat list unexpectedly.
	if err := a.cardkitSetStreaming(ctx, accessToken, streamID, false, finalSummary, seq); err != nil {
		return fmt.Errorf("disable streaming_mode: %w", err)
	}
	feishuStreamsMu.Lock()
	if feishuStreams[streamID] == state {
		delete(feishuStreams, streamID)
	}
	feishuStreamsMu.Unlock()

	logger.Infof(ctx, "[%s] Streaming ended: card_id=%s", a.region.Label, streamID)
	return nil
}

// ── CardKit v1 API helpers ──

// cardkitCreate creates a card entity and returns the card_id.
// POST /open-apis/cardkit/v1/cards
func (a *Adapter) cardkitCreate(ctx context.Context, accessToken, cardJSON string) (string, error) {
	payload, _ := json.Marshal(map[string]interface{}{
		"type": "card_json",
		"data": cardJSON,
	})

	req, err := http.NewRequestWithContext(ctx, http.MethodPost,
		a.api("/open-apis/cardkit/v1/cards"), bytes.NewReader(payload))
	if err != nil {
		return "", err
	}
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("read response: %w", err)
	}

	var result struct {
		Code int             `json:"code"`
		Msg  string          `json:"msg"`
		Data json.RawMessage `json:"data"`
	}
	if err := json.Unmarshal(respBody, &result); err != nil {
		return "", fmt.Errorf("decode: %w (body: %s)", err, string(respBody))
	}
	if result.Code != 0 {
		return "", fmt.Errorf("code=%d msg=%s", result.Code, result.Msg)
	}

	var data struct {
		CardID string `json:"card_id"`
	}
	if err := json.Unmarshal(result.Data, &data); err != nil {
		return "", fmt.Errorf("parse card_id: %w (raw: %s)", err, string(result.Data))
	}
	return data.CardID, nil
}

// sendCardByCardID sends a card_id as an interactive message.
// POST /open-apis/im/v1/messages  with content={"type":"card","data":{"card_id":"…"}}
// sendCardByCardID sends a card_id as an interactive message via the reply
// message API so it lands under the original message / thread. Falls back to
// the plain send-message API on fallback-eligible errors (e.g. group does not
// support reply-in-thread).
func (a *Adapter) sendCardByCardID(ctx context.Context, accessToken string, incoming *im.IncomingMessage, cardID string) error {
	receiveIDType, receiveID := a.resolveReceiveID(incoming)

	// Key: type must be "card" (not "card_id")
	content, _ := json.Marshal(map[string]interface{}{
		"type": "card",
		"data": map[string]string{"card_id": cardID},
	})

	// Reply payload (no receive_id) and fallback payload (with receive_id)
	// share the same content/msg_type — only receive_id differs.
	replyPayload := map[string]interface{}{
		"msg_type": "interactive",
		"content":  string(content),
	}
	fallbackPayload := map[string]interface{}{
		"receive_id": receiveID,
		"msg_type":   "interactive",
		"content":    string(content),
	}

	return a.sendWithFallback(ctx, accessToken, incoming, replyPayload, fallbackPayload, receiveIDType)
}

// cardkitUpdateElement updates a card element's content for streaming.
// PUT /open-apis/cardkit/v1/cards/:card_id/elements/:element_id/content
func (a *Adapter) cardkitUpdateElement(ctx context.Context, accessToken, cardID, elementID, content string, sequence int) error {
	payload, _ := json.Marshal(map[string]interface{}{
		"content":  content,
		"sequence": sequence,
	})

	apiURL := a.api("/open-apis/cardkit/v1/cards/%s/elements/%s/content", cardID, elementID)
	req, err := http.NewRequestWithContext(ctx, http.MethodPut, apiURL, bytes.NewReader(payload))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return fmt.Errorf("decode: %w", err)
	}
	if result.Code != 0 {
		return fmt.Errorf("update element error: code=%d msg=%s", result.Code, result.Msg)
	}
	return nil
}

// Card markdown image handling.
//
// Feishu card markdown images use the syntax ![hover_text](image_key) where the
// value inside the parentheses MUST be an image_key obtained from the Feishu
// "upload image" API. External HTTP/COS URLs are rejected with
// code=200570 "card contains invalid image keys". WeKnora's content pipeline
// rewrites provider:// storage URLs to signed HTTP URLs, so by the time content
// reaches the card we must download each referenced image and re-upload it to
// Feishu to obtain a usable image_key.

// feishuMarkdownImageRe matches every markdown image. Only http(s) targets can
// be uploaded to an image_key; unresolved internal handles (resource://,
// local://, minio://, …) are not valid Feishu image keys, so
// resolveMarkdownImages degrades them instead of leaving ![]() in the card.
var feishuMarkdownImageRe = regexp.MustCompile(`!\[([^\]]*)\]\(([^)\s]+)\)`)

// feishuMarkdownLinkRe matches a markdown link whose target is an http(s) URL.
// Applied after image stripping so signed image URLs are not left as links.
var feishuMarkdownLinkRe = regexp.MustCompile(`\[([^\]]*)\]\((https?://[^)\s]+)\)`)

// feishuMaxImageBytes caps the download size of an image before uploading to
// Feishu (Feishu's limit is 10MB; keep a small margin).
const feishuMaxImageBytes = 10 << 20

var (
	feishuImageKeyMu    sync.Mutex
	feishuImageKeyCache = make(map[string]string)
	// feishuImageDownloadClient validates resolved IPs before connecting so that
	// downloading a URL embedded in model/knowledge content cannot be abused for
	// SSRF against internal services.
	feishuImageDownloadClient = utils.NewSSRFSafeHTTPClient(utils.DefaultSSRFSafeHTTPClientConfig())
)

// imageCacheKey normalizes a URL for caching by dropping the query string, so a
// COS/MinIO signed URL (whose signature changes every request) maps to a stable
// key across streaming updates of the same object.
func imageCacheKey(rawURL string) string {
	if i := strings.IndexByte(rawURL, '?'); i >= 0 {
		return rawURL[:i]
	}
	return rawURL
}

// resolveMarkdownImages replaces the URL inside every ![alt](httpURL) with a
// Feishu image_key. On failure, or when the storage resolver left an internal
// handle unresolved, it degrades the image so the rest of the card still
// renders instead of failing the whole update. HTTP(S) targets become a
// markdown link (Feishu accepts only http/https hrefs); other schemes become
// plain text, because [text](resource://…) is also rejected as an illegal link.
func (a *Adapter) resolveMarkdownImages(ctx context.Context, accessToken, content string) string {
	if !strings.Contains(content, "![") {
		return content
	}
	fallback := func(alt, rawURL string) string {
		if alt == "" {
			alt = a.region.ImageFallbackLabel
		}
		if isHTTPImageURL(rawURL) {
			return fmt.Sprintf("[%s](%s)", alt, rawURL)
		}
		return alt
	}
	return feishuMarkdownImageRe.ReplaceAllStringFunc(content, func(match string) string {
		sub := feishuMarkdownImageRe.FindStringSubmatch(match)
		if len(sub) < 3 {
			return match
		}
		alt, rawURL := sub[1], sub[2]
		if !isHTTPImageURL(rawURL) {
			return fallback(alt, rawURL)
		}
		imgKey, err := a.imageKeyForURL(ctx, accessToken, rawURL)
		if err != nil || imgKey == "" {
			logger.Warnf(ctx, "[%s] image upload failed, degrading to link: url=%s err=%v", a.region.Label, rawURL, err)
			return fallback(alt, rawURL)
		}
		return fmt.Sprintf("![%s](%s)", alt, imgKey)
	})
}

// isHTTPImageURL reports whether rawURL is an http(s) URL. Scheme match is
// case-insensitive: IM rewrite may emit HTTPS:// from a configured proxy domain.
func isHTTPImageURL(rawURL string) bool {
	lower := strings.ToLower(rawURL)
	return strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://")
}

// imageKeyForURL returns a Feishu image_key for the given URL, uploading it if
// not already cached.
func (a *Adapter) imageKeyForURL(ctx context.Context, accessToken, rawURL string) (string, error) {
	// image_keys are issued per app on a single cloud. Scope the cache by app so
	// a key uploaded by a Feishu app is never handed to a Lark app (or to another
	// tenant's app), which would fail the card update with code=200570.
	key := a.appID + "\x00" + imageCacheKey(rawURL)

	feishuImageKeyMu.Lock()
	if v, ok := feishuImageKeyCache[key]; ok {
		feishuImageKeyMu.Unlock()
		return v, nil
	}
	feishuImageKeyMu.Unlock()

	imgKey, err := a.uploadImageFromURL(ctx, accessToken, rawURL)
	if err != nil {
		return "", err
	}

	feishuImageKeyMu.Lock()
	feishuImageKeyCache[key] = imgKey
	feishuImageKeyMu.Unlock()
	return imgKey, nil
}

// uploadImageFromURL downloads the image at rawURL and uploads it to Feishu,
// returning the resulting image_key.
// POST /open-apis/im/v1/images  (multipart: image_type=message, image=<binary>)
func (a *Adapter) uploadImageFromURL(ctx context.Context, accessToken, rawURL string) (string, error) {
	if err := utils.ValidateURLForSSRF(rawURL); err != nil {
		return "", fmt.Errorf("ssrf validation: %w", err)
	}

	imgReq, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
	if err != nil {
		return "", err
	}
	imgResp, err := feishuImageDownloadClient.Do(imgReq)
	if err != nil {
		return "", fmt.Errorf("download image: %w", err)
	}
	defer imgResp.Body.Close()
	if imgResp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("download image: status=%d", imgResp.StatusCode)
	}

	imgData, err := io.ReadAll(io.LimitReader(imgResp.Body, feishuMaxImageBytes+1))
	if err != nil {
		return "", fmt.Errorf("read image: %w", err)
	}
	if len(imgData) == 0 {
		return "", fmt.Errorf("empty image body")
	}
	if len(imgData) > feishuMaxImageBytes {
		return "", fmt.Errorf("image exceeds %d bytes", feishuMaxImageBytes)
	}

	var body bytes.Buffer
	writer := multipart.NewWriter(&body)
	if err := writer.WriteField("image_type", "message"); err != nil {
		return "", err
	}
	part, err := writer.CreateFormFile("image", "image")
	if err != nil {
		return "", err
	}
	if _, err := part.Write(imgData); err != nil {
		return "", err
	}
	if err := writer.Close(); err != nil {
		return "", err
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost,
		a.api("/open-apis/im/v1/images"), &body)
	if err != nil {
		return "", err
	}
	req.Header.Set("Content-Type", writer.FormDataContentType())
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("upload image: %w", err)
	}
	defer resp.Body.Close()

	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
		Data struct {
			ImageKey string `json:"image_key"`
		} `json:"data"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return "", fmt.Errorf("decode upload response: %w", err)
	}
	if result.Code != 0 {
		return "", fmt.Errorf("upload image error: code=%d msg=%s", result.Code, result.Msg)
	}
	if result.Data.ImageKey == "" {
		return "", fmt.Errorf("upload image: empty image_key")
	}
	return result.Data.ImageKey, nil
}

// cardkitSetStreaming updates streaming_mode and, when available, the final summary.
// PATCH /open-apis/cardkit/v1/cards/:card_id/settings
func (a *Adapter) cardkitSetStreaming(
	ctx context.Context, accessToken, cardID string, streaming bool, finalSummary *string, sequence int,
) error {
	config := map[string]interface{}{
		"streaming_mode": streaming,
	}
	if finalSummary != nil && strings.TrimSpace(*finalSummary) != "" {
		config["summary"] = map[string]string{"content": *finalSummary}
	}
	settings, _ := json.Marshal(map[string]interface{}{"config": config})
	payload, _ := json.Marshal(map[string]interface{}{
		"settings": string(settings),
		"sequence": sequence,
	})

	apiURL := a.api("/open-apis/cardkit/v1/cards/%s/settings", cardID)
	req, err := http.NewRequestWithContext(ctx, http.MethodPatch, apiURL, bytes.NewReader(payload))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	req.Header.Set("Authorization", "Bearer "+accessToken)

	resp, err := httpClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	var result struct {
		Code int    `json:"code"`
		Msg  string `json:"msg"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return fmt.Errorf("decode: %w", err)
	}
	if result.Code != 0 {
		return fmt.Errorf("set streaming error: code=%d msg=%s", result.Code, result.Msg)
	}
	return nil
}

// getTenantAccessToken retrieves the Feishu tenant access token with caching.
// Feishu tokens expire in 2 hours; we cache with a safety margin.
func (a *Adapter) getTenantAccessToken(ctx context.Context) (string, error) {
	a.tokenMu.Lock()
	defer a.tokenMu.Unlock()

	if a.tokenCache != "" && time.Now().Before(a.tokenExpAt) {
		return a.tokenCache, nil
	}

	payload, _ := json.Marshal(map[string]string{
		"app_id":     a.appID,
		"app_secret": a.appSecret,
	})

	req, err := http.NewRequestWithContext(ctx, http.MethodPost,
		a.api("/open-apis/auth/v3/tenant_access_token/internal"),
		bytes.NewReader(payload))
	if err != nil {
		return "", fmt.Errorf("create request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json; charset=utf-8")

	resp, err := httpClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("request token: %w", err)
	}
	defer resp.Body.Close()

	var result struct {
		Code              int    `json:"code"`
		Msg               string `json:"msg"`
		TenantAccessToken string `json:"tenant_access_token"`
		Expire            int    `json:"expire"` // seconds
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return "", fmt.Errorf("decode response: %w", err)
	}
	if result.Code != 0 {
		return "", fmt.Errorf("get token error: code=%d msg=%s", result.Code, result.Msg)
	}

	a.tokenCache = result.TenantAccessToken
	// Cache with 5-minute safety margin
	ttl := time.Duration(result.Expire) * time.Second
	if ttl > 5*time.Minute {
		ttl -= 5 * time.Minute
	}
	a.tokenExpAt = time.Now().Add(ttl)

	return a.tokenCache, nil
}

// decrypt decrypts a Feishu encrypted event body.
// Feishu uses AES-256-CBC with SHA-256 of the encrypt key as the AES key.
func (a *Adapter) decrypt(encrypted string) ([]byte, error) {
	if a.encryptKey == "" {
		return nil, fmt.Errorf("encrypt_key not configured")
	}

	ciphertext, err := base64.StdEncoding.DecodeString(encrypted)
	if err != nil {
		return nil, fmt.Errorf("base64 decode: %w", err)
	}

	// SHA-256 of encrypt key as AES key
	keyHash := sha256.Sum256([]byte(a.encryptKey))
	block, err := aes.NewCipher(keyHash[:])
	if err != nil {
		return nil, fmt.Errorf("new cipher: %w", err)
	}

	if len(ciphertext) < aes.BlockSize {
		return nil, fmt.Errorf("ciphertext too short")
	}

	iv := ciphertext[:aes.BlockSize]
	ciphertext = ciphertext[aes.BlockSize:]

	mode := cipher.NewCBCDecrypter(block, iv)
	mode.CryptBlocks(ciphertext, ciphertext)

	// Remove and verify PKCS#7 padding
	if len(ciphertext) == 0 {
		return nil, fmt.Errorf("empty plaintext")
	}
	padLen := int(ciphertext[len(ciphertext)-1])
	if padLen > aes.BlockSize || padLen == 0 || padLen > len(ciphertext) {
		return nil, fmt.Errorf("invalid padding")
	}
	for i := 0; i < padLen; i++ {
		if ciphertext[len(ciphertext)-1-i] != byte(padLen) {
			return nil, fmt.Errorf("invalid padding")
		}
	}

	return ciphertext[:len(ciphertext)-padLen], nil
}