feat(dingtalk): enable secure multimodal image QA

This commit is contained in:
姚章浩
2026-07-16 00:17:16 +08:00
parent 4022040e98
commit c9c4f0aaa5
59 changed files with 3279 additions and 429 deletions
+2
View File
@@ -64,6 +64,8 @@ type IncomingMessage struct {
FileName string
// FileSize is the file size in bytes (for file messages, optional).
FileSize int64
// ImageURLs carries transient model-ready image inputs (usually data URIs). Never log these values.
ImageURLs []string
// ThreadID is the platform-specific thread identifier.
// - Slack: thread_ts (top-level message uses its own timestamp)
// - Mattermost: root_id, or post_id if top-level
+346 -37
View File
@@ -9,7 +9,12 @@ import (
"encoding/json"
"fmt"
"io"
"mime"
"net"
"net/http"
"net/netip"
"net/url"
"path/filepath"
"strconv"
"strings"
"sync"
@@ -20,6 +25,7 @@ import (
"github.com/Tencent/WeKnora/internal/im"
"github.com/Tencent/WeKnora/internal/logger"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// httpClient is a shared HTTP client with a reasonable timeout for DingTalk API calls.
@@ -33,15 +39,18 @@ const dingtalkConvTypeGroup = "2"
// Compile-time checks.
var (
_ im.Adapter = (*Adapter)(nil)
_ im.StreamSender = (*Adapter)(nil)
_ im.Adapter = (*Adapter)(nil)
_ im.StreamSender = (*Adapter)(nil)
_ im.FileDownloader = (*Adapter)(nil)
)
// Adapter implements im.Adapter for DingTalk.
type Adapter struct {
clientID string
clientSecret string
cardTemplateID string // optional: enables AI card streaming when set
clientID string
clientSecret string
cardTemplateID string // optional: enables AI card streaming when set
messageFileDownloadURL string
allowInsecureDownload bool // tests only; production requires HTTPS
// accessToken cache
tokenMu sync.RWMutex
@@ -114,19 +123,20 @@ func (a *Adapter) VerifyCallback(c *gin.Context) error {
// DingTalk callback message structure.
type callbackMessage struct {
ConversationID string `json:"conversationId"`
ConversationType string `json:"conversationType"`
MsgID string `json:"msgId"`
Msgtype string `json:"msgtype"`
Text *textContent `json:"text"`
SenderNick string `json:"senderNick"`
SenderStaffId string `json:"senderStaffId"`
SenderID string `json:"senderId"`
SessionWebhook string `json:"sessionWebhook"`
RobotCode string `json:"robotCode"`
AtUsers []atUser `json:"atUsers"`
IsInAtList bool `json:"isInAtList"`
ChatbotCorpId string `json:"chatbotCorpId"`
ConversationID string `json:"conversationId"`
ConversationType string `json:"conversationType"`
MsgID string `json:"msgId"`
Msgtype string `json:"msgtype"`
Text *textContent `json:"text"`
Content json.RawMessage `json:"content"`
SenderNick string `json:"senderNick"`
SenderStaffId string `json:"senderStaffId"`
SenderID string `json:"senderId"`
SessionWebhook string `json:"sessionWebhook"`
RobotCode string `json:"robotCode"`
AtUsers []atUser `json:"atUsers"`
IsInAtList bool `json:"isInAtList"`
ChatbotCorpId string `json:"chatbotCorpId"`
}
type textContent struct {
@@ -170,6 +180,20 @@ func parseCallbackMessage(msg *callbackMessage) *im.IncomingMessage {
if msg.Text != nil {
content = strings.TrimSpace(msg.Text.Content)
}
messageType := im.MessageTypeText
fileKey := ""
fileName := ""
if strings.EqualFold(msg.Msgtype, "picture") {
messageType = im.MessageTypeImage
fileName = msg.MsgID + ".png"
content = ""
var picture struct {
DownloadCode string `json:"downloadCode"`
}
if json.Unmarshal(msg.Content, &picture) == nil {
fileKey = strings.TrimSpace(picture.DownloadCode)
}
}
return &im.IncomingMessage{
Platform: im.PlatformDingtalk,
@@ -178,14 +202,278 @@ func parseCallbackMessage(msg *callbackMessage) *im.IncomingMessage {
ChatID: chatID,
ChatType: chatType,
MessageID: msg.MsgID,
MessageType: im.MessageTypeText,
MessageType: messageType,
Content: content,
FileKey: fileKey,
FileName: fileName,
Extra: map[string]string{
"session_webhook": msg.SessionWebhook,
},
}
}
const (
dingtalkMessageFileDownloadURL = "https://api.dingtalk.com/v1.0/robot/messageFiles/download"
maxDingTalkAPIResponseBytes = 1 << 20
)
func (a *Adapter) newDingTalkAPIClient(base *http.Client) *http.Client {
client := *base
client.CheckRedirect = func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
}
if !a.allowInsecureDownload {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = nil
dialer := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
transport.DialContext = newDingTalkSafeDialContext(net.DefaultResolver.LookupIPAddr, dialer.DialContext)
client.Transport = transport
}
return &client
}
func readBoundedDingTalkAPIResponse(resp *http.Response) ([]byte, error) {
if resp.ContentLength > maxDingTalkAPIResponseBytes {
return nil, fmt.Errorf("dingtalk API response exceeds size limit")
}
payload, err := io.ReadAll(io.LimitReader(resp.Body, maxDingTalkAPIResponseBytes+1))
if err != nil {
return nil, fmt.Errorf("read dingtalk API response: %w", err)
}
if len(payload) > maxDingTalkAPIResponseBytes {
return nil, fmt.Errorf("dingtalk API response exceeds size limit")
}
return payload, nil
}
func (a *Adapter) DownloadFile(ctx context.Context, msg *im.IncomingMessage) (io.ReadCloser, string, error) {
if msg == nil || strings.TrimSpace(msg.FileKey) == "" {
return nil, "", fmt.Errorf("download code is required")
}
token, err := a.getAccessToken(ctx)
if err != nil {
return nil, "", fmt.Errorf("get access token: %w", err)
}
body, err := json.Marshal(map[string]string{
"downloadCode": msg.FileKey,
"robotCode": a.clientID,
})
if err != nil {
return nil, "", fmt.Errorf("marshal message file request: %w", err)
}
apiURL := a.messageFileDownloadURL
if apiURL == "" {
apiURL = dingtalkMessageFileDownloadURL
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(body))
if err != nil {
return nil, "", fmt.Errorf("create message file request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-acs-dingtalk-access-token", token)
apiClient := a.newDingTalkAPIClient(httpClient)
resp, err := apiClient.Do(req)
if err != nil {
return nil, "", fmt.Errorf("request message file URL: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return nil, "", fmt.Errorf("dingtalk message file API returned %d", resp.StatusCode)
}
payload, err := readBoundedDingTalkAPIResponse(resp)
if err != nil {
return nil, "", err
}
var result struct {
DownloadURL string `json:"downloadUrl"`
}
if err := json.Unmarshal(payload, &result); err != nil {
return nil, "", fmt.Errorf("decode message file response: %w", err)
}
if !a.allowInsecureDownload {
result.DownloadURL, err = normalizeDingTalkInitialDownloadURL(result.DownloadURL)
if err != nil {
return nil, "", err
}
}
if err := validateDingTalkDownloadURL(result.DownloadURL, a.allowInsecureDownload); err != nil {
return nil, "", err
}
downloadReq, err := http.NewRequestWithContext(ctx, http.MethodGet, result.DownloadURL, nil)
if err != nil {
return nil, "", fmt.Errorf("create image download request: %w", err)
}
downloadClient := *httpClient
if !a.allowInsecureDownload {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = nil
dialer := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
transport.DialContext = newDingTalkSafeDialContext(net.DefaultResolver.LookupIPAddr, dialer.DialContext)
downloadClient.Transport = transport
}
downloadClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return validateDingTalkRedirect(req, via, a.allowInsecureDownload)
}
downloadResp, err := downloadClient.Do(downloadReq)
if err != nil {
if ctx.Err() != nil {
return nil, "", ctx.Err()
}
return nil, "", fmt.Errorf("download dingtalk image request failed")
}
if downloadResp.StatusCode < http.StatusOK || downloadResp.StatusCode >= http.StatusMultipleChoices {
downloadResp.Body.Close()
return nil, "", fmt.Errorf("dingtalk image download returned %d", downloadResp.StatusCode)
}
contentType := strings.ToLower(strings.TrimSpace(strings.Split(downloadResp.Header.Get("Content-Type"), ";")[0]))
if msg.MessageType == im.MessageTypeImage && contentType != "" && contentType != "application/octet-stream" && !strings.HasPrefix(contentType, "image/") {
downloadResp.Body.Close()
return nil, "", fmt.Errorf("dingtalk picture response is not an image")
}
return downloadResp.Body, dingtalkDownloadedFileName(downloadResp, msg, contentType), nil
}
var dingtalkBlockedPublicRanges = []netip.Prefix{
netip.MustParsePrefix("100.64.0.0/10"),
netip.MustParsePrefix("192.0.0.0/24"),
netip.MustParsePrefix("192.0.2.0/24"),
netip.MustParsePrefix("198.18.0.0/15"),
netip.MustParsePrefix("198.51.100.0/24"),
netip.MustParsePrefix("203.0.113.0/24"),
netip.MustParsePrefix("2001:db8::/32"),
}
func isSafeDingTalkDownloadIP(ip net.IP) bool {
if ip == nil || !secutils.IsPublicIP(ip) {
return false
}
addr, ok := netip.AddrFromSlice(ip)
if !ok {
return false
}
addr = addr.Unmap()
if !addr.IsGlobalUnicast() || addr.IsPrivate() || addr.IsLoopback() || addr.IsLinkLocalUnicast() || addr.IsUnspecified() || addr.IsMulticast() {
return false
}
for _, prefix := range dingtalkBlockedPublicRanges {
if prefix.Contains(addr) {
return false
}
}
return true
}
func newDingTalkSafeDialContext(
lookup func(context.Context, string) ([]net.IPAddr, error),
dial func(context.Context, string, string) (net.Conn, error),
) func(context.Context, string, string) (net.Conn, error) {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, fmt.Errorf("invalid dingtalk media address")
}
var ips []net.IPAddr
if literal := net.ParseIP(host); literal != nil {
ips = []net.IPAddr{{IP: literal}}
} else {
ips, err = lookup(ctx, host)
if err != nil {
return nil, fmt.Errorf("resolve dingtalk media host failed")
}
}
if len(ips) == 0 {
return nil, fmt.Errorf("dingtalk media host resolved without addresses")
}
for _, resolved := range ips {
if !isSafeDingTalkDownloadIP(resolved.IP) {
return nil, fmt.Errorf("dingtalk media host resolved to a blocked address")
}
}
var lastErr error
for _, resolved := range ips {
conn, dialErr := dial(ctx, network, net.JoinHostPort(resolved.IP.String(), port))
if dialErr == nil {
return conn, nil
}
lastErr = dialErr
}
return nil, fmt.Errorf("connect to validated dingtalk media address: %w", lastErr)
}
}
func validateDingTalkRedirect(req *http.Request, via []*http.Request, allowInsecure bool) error {
if len(via) >= 10 {
return fmt.Errorf("stopped after 10 dingtalk image redirects")
}
// Go may synthesize Referer from the credential-bearing source URL and copy
// headers across redirects. Media redirects need no inherited headers.
req.Header = make(http.Header)
return validateDingTalkDownloadURL(req.URL.String(), allowInsecure)
}
// normalizeDingTalkInitialDownloadURL upgrades the one-time media URL returned
// directly by DingTalk's authenticated message-file API. Some DingTalk media
// backends still return an http scheme even though the same signed resource is
// available over TLS. Only this initial trusted API response is upgraded;
// redirects remain HTTPS-only and are validated independently.
func normalizeDingTalkInitialDownloadURL(rawURL string) (string, error) {
u, err := url.Parse(strings.TrimSpace(rawURL))
if err != nil || u.Hostname() == "" || u.User != nil {
return "", fmt.Errorf("invalid dingtalk download URL")
}
switch u.Scheme {
case "http":
u.Scheme = "https"
case "https":
default:
return "", fmt.Errorf("invalid dingtalk download URL scheme")
}
return u.String(), nil
}
func validateDingTalkDownloadURL(rawURL string, allowInsecure bool) error {
u, err := url.Parse(strings.TrimSpace(rawURL))
if err != nil || u.Hostname() == "" || u.User != nil {
return fmt.Errorf("invalid dingtalk download URL")
}
if allowInsecure {
if u.Scheme != "http" && u.Scheme != "https" {
return fmt.Errorf("invalid dingtalk download URL scheme")
}
return nil
}
if u.Scheme != "https" {
return fmt.Errorf("dingtalk download URL must use HTTPS")
}
if ip := net.ParseIP(u.Hostname()); ip != nil && (ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsUnspecified()) {
return fmt.Errorf("dingtalk download URL points to a private address")
}
if err := secutils.ValidateURLForSSRF(u.String()); err != nil {
return fmt.Errorf("dingtalk download URL failed SSRF validation: %w", err)
}
return nil
}
func dingtalkDownloadedFileName(resp *http.Response, msg *im.IncomingMessage, contentType string) string {
if _, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition")); err == nil {
if name := filepath.Base(strings.TrimSpace(params["filename"])); name != "" && name != "." && name != ".." {
return name
}
}
if msg != nil && strings.TrimSpace(msg.FileName) != "" {
return filepath.Base(msg.FileName)
}
ext := ".bin"
if extensions, _ := mime.ExtensionsByType(contentType); len(extensions) > 0 {
ext = extensions[0]
}
if msg != nil && msg.MessageID != "" {
return msg.MessageID + ext
}
return "dingtalk-file" + ext
}
// ── Send reply ──
func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, reply *im.ReplyMessage) error {
@@ -203,6 +491,9 @@ func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, r
}
func (a *Adapter) replyViaSessionWebhook(ctx context.Context, webhookURL, content string) error {
if err := validateDingTalkDownloadURL(webhookURL, a.allowInsecureDownload); err != nil {
return fmt.Errorf("invalid DingTalk session webhook")
}
body := map[string]interface{}{
"msgtype": "markdown",
"markdown": map[string]string{
@@ -218,19 +509,22 @@ func (a *Adapter) replyViaSessionWebhook(ctx context.Context, webhookURL, conten
req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhookURL, bytes.NewReader(jsonBody))
if err != nil {
return fmt.Errorf("create request: %w", err)
return fmt.Errorf("create DingTalk session webhook request")
}
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
client := a.newDingTalkAPIClient(httpClient)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("send reply: %w", err)
return fmt.Errorf("send DingTalk session webhook failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("dingtalk sessionWebhook returned %d: %s", resp.StatusCode, string(respBody))
return fmt.Errorf("dingtalk session webhook returned %d", resp.StatusCode)
}
if _, err := readBoundedDingTalkAPIResponse(resp); err != nil {
return err
}
return nil
}
@@ -273,15 +567,18 @@ func (a *Adapter) replyViaOpenAPI(ctx context.Context, incoming *im.IncomingMess
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-acs-dingtalk-access-token", token)
resp, err := httpClient.Do(req)
apiClient := a.newDingTalkAPIClient(httpClient)
resp, err := apiClient.Do(req)
if err != nil {
return fmt.Errorf("send reply: %w", err)
return fmt.Errorf("send reply failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("dingtalk OpenAPI returned %d: %s", resp.StatusCode, string(respBody))
return fmt.Errorf("dingtalk OpenAPI returned %d", resp.StatusCode)
}
if _, err := readBoundedDingTalkAPIResponse(resp); err != nil {
return err
}
return nil
}
@@ -317,22 +614,26 @@ func (a *Adapter) getAccessToken(ctx context.Context) (string, error) {
}
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
apiClient := a.newDingTalkAPIClient(httpClient)
resp, err := apiClient.Do(req)
if err != nil {
return "", err
return "", fmt.Errorf("dingtalk access token request failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("dingtalk accessToken returned %d: %s", resp.StatusCode, string(respBody))
return "", fmt.Errorf("dingtalk access token returned %d", resp.StatusCode)
}
payload, err := readBoundedDingTalkAPIResponse(resp)
if err != nil {
return "", err
}
var result struct {
AccessToken string `json:"accessToken"`
ExpireIn int64 `json:"expireIn"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
if err := json.Unmarshal(payload, &result); err != nil {
return "", fmt.Errorf("decode token response: %w", err)
}
if result.AccessToken == "" {
@@ -366,15 +667,19 @@ func (a *Adapter) dingtalkAPI(ctx context.Context, method, path string, body int
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-acs-dingtalk-access-token", token)
resp, err := httpClient.Do(req)
apiClient := a.newDingTalkAPIClient(httpClient)
resp, err := apiClient.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
return nil, fmt.Errorf("dingtalk API request failed")
}
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
respBody, readErr := readBoundedDingTalkAPIResponse(resp)
if readErr != nil {
return nil, readErr
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("dingtalk API %s returned %d: %s", path, resp.StatusCode, string(respBody))
return nil, fmt.Errorf("dingtalk API %s returned %d", path, resp.StatusCode)
}
return respBody, nil
}
@@ -573,6 +878,7 @@ func (a *Adapter) FinalizeStream(ctx context.Context, incoming *im.IncomingMessa
if outTrackID != "" {
if err := a.streamingUpdateCard(ctx, outTrackID, finalContent, false); err != nil {
logger.Warnf(ctx, "[DingTalk] Failed to finalize card stream: %v", err)
return fmt.Errorf("finalize DingTalk card stream: %w", err)
}
}
return nil
@@ -601,14 +907,17 @@ func (a *Adapter) EndStream(ctx context.Context, incoming *im.IncomingMessage, s
if outTrackID != "" {
if err := a.streamingUpdateCard(ctx, outTrackID, fullContent, true); err != nil {
logger.Warnf(ctx, "[DingTalk] Failed to finalize card stream: %v", err)
return fmt.Errorf("end DingTalk card stream: %w", err)
}
} else if sessionWebhook != "" {
if err := a.replyViaSessionWebhook(ctx, sessionWebhook, fullContent); err != nil {
logger.Warnf(ctx, "[DingTalk] Failed to end stream: %v", err)
return fmt.Errorf("end DingTalk session webhook stream: %w", err)
}
} else {
if err := a.replyViaOpenAPI(ctx, incoming, fullContent); err != nil {
logger.Warnf(ctx, "[DingTalk] Failed to end stream via OpenAPI: %v", err)
return fmt.Errorf("end DingTalk OpenAPI stream: %w", err)
}
}
@@ -0,0 +1,45 @@
package dingtalk
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDingTalkRedirectClearsRefererAndInheritedHeaders(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://cdn-two.example/image.png", nil)
req.Header.Set("Referer", "https://cdn-one.example/image.png?signature=TOPSECRET")
req.Header.Set("Authorization", "Bearer TOPSECRET")
if err := validateDingTalkRedirect(req, nil, true); err != nil {
t.Fatalf("validateDingTalkRedirect: %v", err)
}
if len(req.Header) != 0 {
t.Fatalf("redirect headers = %#v, want empty", req.Header)
}
}
func TestReplyViaSessionWebhookRejectsRedirectWithoutLeakingURL(t *testing.T) {
var reached bool
target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached = true }))
defer target.Close()
source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", target.URL)
w.WriteHeader(http.StatusTemporaryRedirect)
}))
defer source.Close()
a := &Adapter{allowInsecureDownload: true}
signedURL := source.URL + "/hook?access_token=TOPSECRET"
err := a.replyViaSessionWebhook(context.Background(), signedURL, "reply")
if err == nil {
t.Fatal("expected webhook redirect rejection")
}
if reached {
t.Fatal("session webhook followed redirect")
}
if strings.Contains(err.Error(), "TOPSECRET") || strings.Contains(err.Error(), signedURL) {
t.Fatalf("credential URL leaked in error: %v", err)
}
}
+276
View File
@@ -0,0 +1,276 @@
package dingtalk
import (
"context"
"errors"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/Tencent/WeKnora/internal/im"
)
func TestParseCallbackMessagePicture(t *testing.T) {
msg := &callbackMessage{
ConversationID: "cid-group",
ConversationType: dingtalkConvTypeGroup,
MsgID: "msg-picture-1",
Msgtype: "picture",
Content: []byte(`{"downloadCode":"download-code-1"}`),
SenderStaffId: "staff-1",
SenderNick: "Alice",
SessionWebhook: "https://example.invalid/session",
}
got := parseCallbackMessage(msg)
if got.MessageType != im.MessageTypeImage {
t.Fatalf("MessageType = %q, want image", got.MessageType)
}
if got.FileKey != "download-code-1" {
t.Fatalf("FileKey = %q, want download code", got.FileKey)
}
if got.FileName != "msg-picture-1.png" {
t.Fatalf("FileName = %q", got.FileName)
}
if got.Content != "" {
t.Fatalf("Content = %q, want empty", got.Content)
}
}
func TestAdapterDownloadFileUsesMessageFilesAPI(t *testing.T) {
var sawToken bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1.0/robot/messageFiles/download":
sawToken = r.Header.Get("x-acs-dingtalk-access-token") == "cached-access-token"
body, _ := io.ReadAll(r.Body)
if !strings.Contains(string(body), `"downloadCode":"download-code-1"`) || !strings.Contains(string(body), `"robotCode":"robot-code-1"`) {
t.Errorf("unexpected request body: %s", body)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"downloadUrl":"` + serverURLForRequest(r) + `/media/picture"}`))
case "/media/picture":
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Disposition", `attachment; filename="question.png"`)
_, _ = w.Write([]byte("PNGDATA"))
default:
http.NotFound(w, r)
}
}))
defer server.Close()
adapter := NewAdapter("robot-code-1", "secret", "")
adapter.token = "cached-access-token"
adapter.tokenExpAt = time.Now().Add(time.Hour)
adapter.messageFileDownloadURL = server.URL + "/v1.0/robot/messageFiles/download"
adapter.allowInsecureDownload = true
reader, fileName, err := adapter.DownloadFile(context.Background(), &im.IncomingMessage{
MessageType: im.MessageTypeImage,
MessageID: "msg-picture-1",
FileKey: "download-code-1",
})
if err != nil {
t.Fatalf("DownloadFile: %v", err)
}
defer reader.Close()
body, _ := io.ReadAll(reader)
if string(body) != "PNGDATA" {
t.Fatalf("body = %q", body)
}
if fileName != "question.png" {
t.Fatalf("fileName = %q", fileName)
}
if !sawToken {
t.Fatal("missing DingTalk access token header")
}
}
func serverURLForRequest(r *http.Request) string {
return "http://" + r.Host
}
func TestNormalizeDingTalkInitialDownloadURLUpgradesHTTPToHTTPS(t *testing.T) {
rawURL := "http://media.example.com/path/image.png?signature=abc%2F123&name=a+b.png"
got, err := normalizeDingTalkInitialDownloadURL(rawURL)
if err != nil {
t.Fatalf("normalizeDingTalkInitialDownloadURL: %v", err)
}
want := "https://media.example.com/path/image.png?signature=abc%2F123&name=a+b.png"
if got != want {
t.Fatalf("normalized URL = %q, want %q", got, want)
}
}
func TestValidateDingTalkDownloadURLRejectsUnsafeTargets(t *testing.T) {
for _, rawURL := range []string{
"http://example.com/image.png",
"https://user:pass@example.com/image.png",
"https://127.0.0.1/image.png",
"https://10.0.0.8/image.png",
} {
t.Run(rawURL, func(t *testing.T) {
if err := validateDingTalkDownloadURL(rawURL, false); err == nil {
t.Fatalf("unsafe URL accepted: %s", rawURL)
}
})
}
}
func TestParseCallbackMessagePictureWithoutDownloadCodeStaysImage(t *testing.T) {
for _, content := range [][]byte{[]byte(`{}`), []byte(`not-json`)} {
msg := parseCallbackMessage(&callbackMessage{MsgID: "bad-picture", Msgtype: "picture", Content: content})
if msg.MessageType != im.MessageTypeImage {
t.Fatalf("MessageType = %q, want image", msg.MessageType)
}
if msg.FileKey != "" {
t.Fatalf("FileKey = %q, want empty", msg.FileKey)
}
}
}
func TestAdapterDownloadFileDoesNotFollowCredentialedAPIRedirect(t *testing.T) {
var redirectedToken string
target := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
redirectedToken = r.Header.Get("x-acs-dingtalk-access-token")
}))
defer target.Close()
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", target.URL)
w.WriteHeader(http.StatusTemporaryRedirect)
}))
defer api.Close()
adapter := NewAdapter("robot-code-1", "secret", "")
adapter.token = "cached-access-token"
adapter.tokenExpAt = time.Now().Add(time.Hour)
adapter.messageFileDownloadURL = api.URL
adapter.allowInsecureDownload = true
_, _, err := adapter.DownloadFile(context.Background(), &im.IncomingMessage{MessageType: im.MessageTypeImage, FileKey: "code"})
if err == nil {
t.Fatal("expected redirect rejection")
}
if redirectedToken != "" {
t.Fatal("access token leaked across redirect")
}
}
func TestAdapterDownloadFileAcceptsGenericMediaContentType(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"downloadUrl":"` + serverURLForRequest(r) + `/media"}`))
return
}
w.Header().Set("Content-Type", "application/octet-stream")
_, _ = w.Write([]byte("PNGDATA"))
}))
defer server.Close()
adapter := NewAdapter("robot", "secret", "")
adapter.token = "token"
adapter.tokenExpAt = time.Now().Add(time.Hour)
adapter.messageFileDownloadURL = server.URL + "/api"
adapter.allowInsecureDownload = true
body, _, err := adapter.DownloadFile(context.Background(), &im.IncomingMessage{MessageType: im.MessageTypeImage, FileKey: "code"})
if err != nil {
t.Fatalf("DownloadFile: %v", err)
}
body.Close()
}
func TestDingTalkAPIClientDisablesProxyAndRedirects(t *testing.T) {
a := &Adapter{}
client := a.newDingTalkAPIClient(httpClient)
transport, ok := client.Transport.(*http.Transport)
if !ok {
t.Fatalf("transport type = %T, want *http.Transport", client.Transport)
}
if transport.Proxy != nil {
t.Fatal("DingTalk API client must ignore environment proxies")
}
req := httptest.NewRequest(http.MethodPost, "https://api.dingtalk.com/redirect", nil)
if err := client.CheckRedirect(req, nil); !errors.Is(err, http.ErrUseLastResponse) {
t.Fatalf("redirect error = %v, want http.ErrUseLastResponse", err)
}
}
func TestDingTalkRedirectLimitFailsClosed(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://example.com/image.png", nil)
via := make([]*http.Request, 10)
if err := validateDingTalkRedirect(req, via, false); err == nil {
t.Fatal("validateDingTalkRedirect should reject more than 10 requests")
}
}
func TestDingTalkSafeDialUsesValidatedConcreteIP(t *testing.T) {
stop := errors.New("stop after address capture")
var dialed string
dial := newDingTalkSafeDialContext(
func(context.Context, string) ([]net.IPAddr, error) {
return []net.IPAddr{{IP: net.ParseIP("1.1.1.1")}}, nil
},
func(_ context.Context, _ string, addr string) (net.Conn, error) {
dialed = addr
return nil, stop
},
)
_, err := dial(context.Background(), "tcp", "cdn.example.test:443")
if !errors.Is(err, stop) {
t.Fatalf("dial error = %v, want sentinel", err)
}
if dialed != "1.1.1.1:443" {
t.Fatalf("dialed address = %q, want validated concrete IP", dialed)
}
}
func TestDingTalkSafeDialRejectsEveryRestrictedResolvedIP(t *testing.T) {
for _, rawIP := range []string{
"0.0.0.1",
"240.0.0.1",
"2001:0000::1",
"2002:7f00:0001::1",
} {
t.Run(rawIP, func(t *testing.T) {
dialCalled := false
dial := newDingTalkSafeDialContext(
func(context.Context, string) ([]net.IPAddr, error) {
return []net.IPAddr{{IP: net.ParseIP(rawIP)}}, nil
},
func(context.Context, string, string) (net.Conn, error) {
dialCalled = true
return nil, errors.New("must not dial")
},
)
if _, err := dial(context.Background(), "tcp", "cdn.example.test:443"); err == nil {
t.Fatal("restricted resolved IP was accepted")
}
if dialCalled {
t.Fatal("restricted resolved IP reached network dial")
}
})
}
}
func TestAdapterDownloadFileRedactsSignedURLFromErrors(t *testing.T) {
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"downloadUrl":"http://127.0.0.1:1/media?X-Amz-Signature=TOPSECRET"}`))
}))
defer api.Close()
adapter := NewAdapter("robot", "secret", "")
adapter.token = "token"
adapter.tokenExpAt = time.Now().Add(time.Hour)
adapter.messageFileDownloadURL = api.URL
adapter.allowInsecureDownload = true
_, _, err := adapter.DownloadFile(context.Background(), &im.IncomingMessage{MessageType: im.MessageTypeImage, FileKey: "code"})
if err == nil {
t.Fatal("expected download error")
}
if strings.Contains(err.Error(), "TOPSECRET") || strings.Contains(err.Error(), "X-Amz-Signature") {
t.Fatalf("signed URL leaked in error: %v", err)
}
}
@@ -0,0 +1,43 @@
package dingtalk
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/Tencent/WeKnora/internal/im"
)
type failingCardRoundTripper struct{}
func (failingCardRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
return nil, errors.New("simulated delivery failure")
}
func TestFinalizeStreamPropagatesCardDeliveryFailure(t *testing.T) {
oldClient := httpClient
httpClient = &http.Client{Transport: failingCardRoundTripper{}, Timeout: time.Second}
t.Cleanup(func() { httpClient = oldClient })
adapter := &Adapter{
clientID: "robot",
token: "cached-token",
tokenExpAt: time.Now().Add(time.Hour),
}
streamID := "dt:fail-closed-test"
streamsMu.Lock()
dStreams[streamID] = &streamState{outTrackID: "track-id", createdAt: time.Now()}
streamsMu.Unlock()
t.Cleanup(func() {
streamsMu.Lock()
delete(dStreams, streamID)
streamsMu.Unlock()
})
err := adapter.FinalizeStream(context.Background(), &im.IncomingMessage{}, streamID, "image recognition failed")
if err == nil {
t.Fatal("expected final card delivery failure to propagate")
}
}
+37 -2
View File
@@ -2,6 +2,7 @@ package dingtalk
import (
"context"
"encoding/json"
"strings"
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
@@ -71,7 +72,11 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
userID = data.SenderId
}
content := strings.TrimSpace(data.Text.Content)
messageType, content, fileKey := parseDingTalkStreamPayload(data.Msgtype, data.Content, data.Text.Content)
fileName := ""
if messageType == im.MessageTypeImage {
fileName = data.MsgId + ".png"
}
incoming := &im.IncomingMessage{
Platform: im.PlatformDingtalk,
@@ -80,8 +85,10 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
ChatID: chatID,
ChatType: chatType,
MessageID: data.MsgId,
MessageType: im.MessageTypeText,
MessageType: messageType,
Content: content,
FileKey: fileKey,
FileName: fileName,
Extra: map[string]string{
"session_webhook": data.SessionWebhook,
},
@@ -93,3 +100,31 @@ func (c *LongConnClient) onChatBotMessage(ctx context.Context, data *chatbot.Bot
return nil, nil
}
func parseDingTalkStreamPayload(msgType string, rawContent interface{}, text string) (im.MessageType, string, string) {
if !strings.EqualFold(strings.TrimSpace(msgType), "picture") {
return im.MessageTypeText, strings.TrimSpace(text), ""
}
var payload []byte
switch value := rawContent.(type) {
case string:
payload = []byte(value)
case []byte:
payload = value
case json.RawMessage:
payload = value
default:
var err error
payload, err = json.Marshal(rawContent)
if err != nil {
return im.MessageTypeImage, "", ""
}
}
var picture struct {
DownloadCode string `json:"downloadCode"`
}
if json.Unmarshal(payload, &picture) != nil {
return im.MessageTypeImage, "", ""
}
return im.MessageTypeImage, "", strings.TrimSpace(picture.DownloadCode)
}
+37
View File
@@ -0,0 +1,37 @@
package dingtalk
import (
"testing"
"github.com/Tencent/WeKnora/internal/im"
)
func TestParseDingTalkStreamPayloadPicture(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("picture", map[string]interface{}{
"downloadCode": "stream-download-code",
}, "ignored text")
if messageType != im.MessageTypeImage {
t.Fatalf("MessageType = %q, want image", messageType)
}
if content != "" {
t.Fatalf("content = %q, want empty", content)
}
if fileKey != "stream-download-code" {
t.Fatalf("fileKey = %q", fileKey)
}
}
func TestParseDingTalkStreamPayloadText(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("text", nil, " hello ")
if messageType != im.MessageTypeText || content != "hello" || fileKey != "" {
t.Fatalf("got type=%q content=%q fileKey=%q", messageType, content, fileKey)
}
}
func TestParseDingTalkStreamPayloadPictureJSONString(t *testing.T) {
messageType, _, fileKey := parseDingTalkStreamPayload("picture", `{"downloadCode":"string-code"}`, "")
if messageType != im.MessageTypeImage || fileKey != "string-code" {
t.Fatalf("got type=%q fileKey=%q", messageType, fileKey)
}
}
+132
View File
@@ -0,0 +1,132 @@
package im
import (
"bytes"
"context"
"errors"
"image"
"image/color"
"image/png"
"io"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
type staticImageDownloader struct {
body string
fileName string
}
func (d staticImageDownloader) DownloadFile(context.Context, *IncomingMessage) (io.ReadCloser, string, error) {
return io.NopCloser(strings.NewReader(d.body)), d.fileName, nil
}
func testPNG(t *testing.T, width, height int) string {
t.Helper()
img := image.NewNRGBA(image.Rect(0, 0, width, height))
img.SetNRGBA(0, 0, color.NRGBA{R: 20, G: 120, B: 240, A: 255})
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("encode png: %v", err)
}
return buf.String()
}
func TestPrepareIMImageForQAAndBuildRequest(t *testing.T) {
png := testPNG(t, 32, 32)
msg := &IncomingMessage{
Platform: PlatformDingtalk,
MessageType: MessageTypeImage,
MessageID: "picture-message-1",
FileKey: "temporary-download-code",
}
if err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: png, fileName: "question.png"}); err != nil {
t.Fatalf("prepareIMImageForQA: %v", err)
}
if len(msg.ImageURLs) != 1 || !strings.HasPrefix(msg.ImageURLs[0], "data:image/png;base64,") {
t.Fatalf("ImageURLs = %#v", msg.ImageURLs)
}
if msg.Content == "" {
t.Fatal("image-only message should receive a default visual question")
}
req := buildIMQARequest(&types.Session{ID: "session-1"}, msg.Content, "assistant-1", "user-1", nil, nil, nil, msg.ImageURLs)
if len(req.ImageURLs) != 1 || req.ImageURLs[0] != msg.ImageURLs[0] {
t.Fatalf("request ImageURLs = %#v", req.ImageURLs)
}
}
func TestPrepareIMImageForQARejectsInvalidOrOversizedPayload(t *testing.T) {
tests := []struct {
name string
body string
want string
}{
{name: "not an image", body: strings.Repeat("plain text", 80), want: "unsupported image content type"},
{name: "oversized", body: strings.Repeat("x", maxIMImageBytes+1), want: "exceeds 10 MiB limit"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := &IncomingMessage{Platform: PlatformDingtalk, MessageType: MessageTypeImage, MessageID: "image-1"}
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: tt.body, fileName: "payload.bin"})
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Fatalf("error = %v, want containing %q", err, tt.want)
}
if len(msg.ImageURLs) != 0 {
t.Fatal("rejected image must not reach the model")
}
})
}
t.Run("dimensions", func(t *testing.T) {
msg := &IncomingMessage{MessageType: MessageTypeImage}
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: testPNG(t, maxIMImageDimension+1, 1), fileName: "wide.png"})
if err == nil || !strings.Contains(err.Error(), "dimensions") {
t.Fatalf("error = %v, want dimensions rejection", err)
}
})
t.Run("animated formats rejected", func(t *testing.T) {
msg := &IncomingMessage{MessageType: MessageTypeImage}
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: "GIF89a" + strings.Repeat("x", 600), fileName: "animated.gif"})
if err == nil || !strings.Contains(err.Error(), "only JPEG and PNG") {
t.Fatalf("error = %v, want JPEG/PNG rejection", err)
}
})
}
func TestIMQAErrorReplyDistinguishesImageFailures(t *testing.T) {
imageReply := imQAErrorReply([]string{"data:image/png;base64,abc"})
if !strings.Contains(imageReply, "图片识别失败") || !strings.Contains(imageReply, "视觉模型") {
t.Fatalf("image error reply = %q", imageReply)
}
if got := imQAErrorReply(nil); got != "抱歉,处理您的问题时出现了异常,请稍后再试。" {
t.Fatalf("text error reply = %q", got)
}
}
func TestImageStreamDoesNotPublishPartialContent(t *testing.T) {
if allowIncrementalIMFlush(&IncomingMessage{ImageURLs: []string{"data:image/png;base64,YWJj"}}) {
t.Fatal("image streams must buffer until successful completion")
}
if !allowIncrementalIMFlush(&IncomingMessage{Content: "hello"}) {
t.Fatal("text streams must preserve incremental updates")
}
}
func TestFailClosedIMImageAnswerDiscardsPartialOutput(t *testing.T) {
partial := "I think the image might show"
err := errors.New("multimodal request rejected")
got, overridden := failClosedIMImageAnswer(partial, err, []string{"data:image/png;base64,abc"})
if !overridden || strings.Contains(got, partial) || !strings.Contains(got, "图片识别失败") {
t.Fatalf("image override = %q overridden=%v", got, overridden)
}
got, overridden = failClosedIMImageAnswer(partial, err, nil)
if overridden || got != partial {
t.Fatalf("text answer changed = %q overridden=%v", got, overridden)
}
}
+165 -29
View File
@@ -3,10 +3,15 @@ package im
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"regexp"
@@ -483,12 +488,17 @@ func buildIMQARequest(
customAgent *types.CustomAgent,
kbIDs []string,
quote *QuotedMessage,
imageURLs ...[]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
quotedContext := formatQuotedContext(quote)
var images []string
if len(imageURLs) > 0 {
images = append([]string(nil), imageURLs[0]...)
}
return &types.QARequest{
Session: session,
Query: query,
@@ -498,9 +508,78 @@ func buildIMQARequest(
UserMessageID: userMessageID,
WebSearchEnabled: webSearchEnabled,
QuotedContext: quotedContext,
ImageURLs: images,
}
}
const (
maxIMImageBytes = 10 << 20
maxIMImageDimension = 16_384
maxIMImagePixels = 40_000_000
)
func prepareIMImageForQA(ctx context.Context, msg *IncomingMessage, downloader FileDownloader) error {
if msg == nil || downloader == nil {
return fmt.Errorf("image message and downloader are required")
}
reader, fileName, err := downloader.DownloadFile(ctx, msg)
if err != nil {
return fmt.Errorf("download image: %w", err)
}
defer reader.Close()
data, err := io.ReadAll(io.LimitReader(reader, maxIMImageBytes+1))
if err != nil {
return fmt.Errorf("read image: %w", err)
}
if len(data) == 0 {
return fmt.Errorf("downloaded image is empty")
}
if len(data) > maxIMImageBytes {
return fmt.Errorf("image exceeds %d MiB limit", maxIMImageBytes>>20)
}
contentType := strings.ToLower(http.DetectContentType(data))
switch contentType {
case "image/jpeg", "image/png":
case "image/gif", "image/webp":
return fmt.Errorf("only JPEG and PNG images are supported")
default:
return fmt.Errorf("unsupported image content type %q", contentType)
}
config, _, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("decode image metadata: %w", err)
}
if config.Width <= 0 || config.Height <= 0 || config.Width > maxIMImageDimension || config.Height > maxIMImageDimension || int64(config.Width)*int64(config.Height) > maxIMImagePixels {
return fmt.Errorf("image dimensions %dx%d exceed safety limits", config.Width, config.Height)
}
msg.FileName = fileName
msg.ImageURLs = []string{"data:" + contentType + ";base64," + base64.StdEncoding.EncodeToString(data)}
if strings.TrimSpace(msg.Content) == "" {
msg.Content = "请识别这张图片的内容,并回答图片中的问题。"
}
return nil
}
func imQAErrorReply(imageURLs []string) string {
if len(imageURLs) > 0 {
return "图片识别失败:当前模型可能不支持视觉输入,请检查智能体的图片上传开关和视觉模型配置。"
}
return "抱歉,处理您的问题时出现了异常,请稍后再试。"
}
func allowIncrementalIMFlush(msg *IncomingMessage) bool {
return msg == nil || len(msg.ImageURLs) == 0
}
func failClosedIMImageAnswer(answer string, err error, imageURLs []string) (string, bool) {
if err != nil && len(imageURLs) > 0 {
return imQAErrorReply(imageURLs), true
}
return answer, false
}
func buildIMLastRequestState(agentID string, customAgent *types.CustomAgent, kbIDs []string) *types.SessionLastRequestState {
state := &types.SessionLastRequestState{
AgentID: agentID,
@@ -1232,21 +1311,17 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
logger.Infof(ctx, "[IM] HandleMessage: channel=%s platform=%s user=%s chat=%s msgtype=%s content_len=%d",
channelID, msg.Platform, msg.UserID, msg.ChatID, msg.MessageType, len(msg.Content))
logger.Debugf(ctx, "[IM] HandleMessage detail: msgid=%s filekey=%s filename=%s",
msg.MessageID, msg.FileKey, msg.FileName)
logger.Debugf(ctx, "[IM] HandleMessage detail: msgid=%s has_filekey=%t filename=%s",
msg.MessageID, strings.TrimSpace(msg.FileKey) != "", msg.FileName)
// ── File/Image message shortcut ──
// If the message is a file or image and the channel has a knowledge_base_id configured,
// handle it separately without entering the QA pipeline.
if (msg.MessageType == MessageTypeFile || msg.MessageType == MessageTypeImage) && channel.KnowledgeBaseID != "" {
// Files keep the legacy knowledge-base ingestion path. Images are routed
// below either to multimodal QA or, when vision is disabled, to this path.
if msg.MessageType == MessageTypeFile && channel.KnowledgeBaseID != "" {
return s.handleFileMessage(ctx, msg, adapter, channel)
}
// ── Non-text message without text content ──
// If the message is an image/file/video but has no text content, the QA pipeline
// cannot do anything useful (no vision support in IM yet). Sending an empty query
// to KB retrieval would return irrelevant results and cause hallucination.
if msg.Content == "" && (msg.MessageType == MessageTypeImage || msg.MessageType == MessageTypeFile) {
// Non-text files without a configured knowledge base still cannot enter QA.
if msg.Content == "" && msg.MessageType == MessageTypeFile {
logger.Infof(ctx, "[IM] Skipping QA for non-text message without content: type=%s", msg.MessageType)
if err := adapter.SendReply(ctx, msg, &ReplyMessage{
Content: "当前渠道未配置文件知识库,无法处理图片/文件消息。请在渠道设置中配置文件知识库后再发送,或直接用文字描述您的问题。",
@@ -1282,6 +1357,24 @@ func (s *Service) HandleMessage(ctx context.Context, msg *IncomingMessage, chann
}
}
if msg.MessageType == MessageTypeImage {
if customAgent == nil || !customAgent.Config.ImageUploadEnabled {
return adapter.SendReply(ctx, msg, &ReplyMessage{
Content: "当前机器人未启用图片识别,请在智能体设置中开启图片上传并配置视觉模型。",
IsFinal: true,
})
}
if strings.TrimSpace(msg.FileKey) == "" {
return adapter.SendReply(ctx, msg, &ReplyMessage{Content: "图片消息缺少有效下载凭证,请重新发送图片。", IsFinal: true})
}
if _, ok := adapter.(FileDownloader); !ok {
return adapter.SendReply(ctx, msg, &ReplyMessage{Content: "当前钉钉渠道暂不支持图片下载。", IsFinal: true})
}
if strings.TrimSpace(msg.Content) == "" {
msg.Content = "请识别这张图片的内容,并回答图片中的问题。"
}
}
// ── Slash-command dispatch ──
// Commands are handled before the QA pipeline so they respond instantly.
if cmd, args, ok := s.cmdRegistry.Parse(msg.Content); ok {
@@ -1415,6 +1508,21 @@ func (s *Service) executeQARequest(req *qaRequest) {
// runQA after the assistant message is created (that's when we have the
// sessionID + messageID needed to poll StreamManager).
// Keep large image bytes out of the bounded queue. Download, validate and
// encode only after this worker owns a concurrency slot.
if req.msg.MessageType == MessageTypeImage && len(req.msg.ImageURLs) == 0 {
downloader, ok := req.adapter.(FileDownloader)
if !ok {
_ = req.adapter.SendReply(ctx, req.msg, &ReplyMessage{Content: "当前钉钉渠道暂不支持图片下载。", IsFinal: true})
return
}
if err := prepareIMImageForQA(ctx, req.msg, downloader); err != nil {
logger.Warnf(ctx, "[IM] Failed to prepare image for QA: %v", err)
_ = req.adapter.SendReply(ctx, req.msg, &ReplyMessage{Content: "图片下载或格式校验失败,请发送10MB以内的 JPG 或 PNG 图片。", IsFinal: true})
return
}
}
// kbIDs is left empty so the QA pipeline resolves them from the agent config.
var kbIDs []string
@@ -1432,10 +1540,10 @@ func (s *Service) executeQARequest(req *qaRequest) {
}
// Non-streaming fallback: collect full answer then send.
answer, err := s.runQA(ctx, req.session, req.msg.Content, req.agent, kbIDs, req.userKey, req.msg.Quote)
answer, err := s.runQA(ctx, req.session, req.msg.Content, req.agent, kbIDs, req.userKey, req.msg.Quote, req.msg.ImageURLs)
if err != nil {
logger.Errorf(ctx, "[IM] QA failed: %v, sending fallback reply", err)
answer = "抱歉,处理您的问题时出现了异常,请稍后再试。"
answer = imQAErrorReply(req.msg.ImageURLs)
}
reply := &ReplyMessage{
@@ -2132,7 +2240,7 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
// Run QA async
go func() {
var err error
req := buildIMQARequest(session, msg.Content, assistantMsg.ID, userMsg.ID, customAgent, kbIDs, msg.Quote)
req := buildIMQARequest(session, msg.Content, assistantMsg.ID, userMsg.ID, customAgent, kbIDs, msg.Quote, msg.ImageURLs)
if req.QuotedContext != "" {
logger.Debugf(qaCtx, "[IM] QuotedContext set: length=%d", len(req.QuotedContext))
}
@@ -2158,6 +2266,9 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
defer ticker.Stop()
flush := func() {
if !allowIncrementalIMFlush(msg) {
return
}
bufMu.Lock()
parts := getStreamParts()
agentRunning := useAgent && !agentDone
@@ -2211,10 +2322,13 @@ loop:
bufMu.Unlock()
finalDisplay := cleanIMContent(ctx, FormatIMFinalFromParts(parts), tenant, s.defaultFileSvc)
if noVisibleContent || finalDisplay == "" {
if replacement, override := failClosedIMImageAnswer(answer, finalErr, msg.ImageURLs); override {
answer = replacement
finalDisplay = replacement
} else if noVisibleContent || finalDisplay == "" {
fallback := "抱歉,我暂时无法回答这个问题。"
if finalErr != nil {
fallback = "抱歉,处理您的问题时出现了异常,请稍后再试。"
fallback = imQAErrorReply(msg.ImageURLs)
}
finalDisplay = fallback
if answer == "" {
@@ -2222,14 +2336,7 @@ loop:
}
}
if err := streamer.FinalizeStream(ctx, msg, streamID, finalDisplay); err != nil {
logger.Warnf(ctx, "[IM] FinalizeStream failed: %v", err)
}
// End the stream
if err := streamer.EndStream(ctx, msg, streamID); err != nil {
logger.Warnf(ctx, "[IM] EndStream failed: %v", err)
}
deliveryErr := finalizeAndEndIMStream(ctx, streamer, msg, streamID, finalDisplay)
if answer == "" {
answer = "抱歉,我暂时无法回答这个问题。"
@@ -2242,28 +2349,49 @@ loop:
}
logger.Infof(ctx, "[IM] Stream reply sent: platform=%s user=%s answer_len=%d", msg.Platform, msg.UserID, len(answer))
return nil
return deliveryErr
}
func finalizeAndEndIMStream(ctx context.Context, streamer StreamSender, msg *IncomingMessage, streamID, finalDisplay string) error {
var deliveryErr error
if err := streamer.FinalizeStream(ctx, msg, streamID, finalDisplay); err != nil {
logger.Warnf(ctx, "[IM] FinalizeStream failed: %v", err)
deliveryErr = fmt.Errorf("finalize IM stream: %w", err)
}
// End the stream even when finalization failed so adapter resources are released.
if err := streamer.EndStream(ctx, msg, streamID); err != nil {
logger.Warnf(ctx, "[IM] EndStream failed: %v", err)
if deliveryErr == nil {
deliveryErr = fmt.Errorf("end IM stream: %w", err)
}
}
return deliveryErr
}
// fallbackNonStream is used when streaming initialization fails.
func (s *Service) fallbackNonStream(ctx context.Context, msg *IncomingMessage, session *types.Session, customAgent *types.CustomAgent, kbIDs []string, adapter Adapter, userKey string, tenant *types.Tenant) error {
answer, err := s.runQA(ctx, session, msg.Content, customAgent, kbIDs, userKey, msg.Quote)
answer, err := s.runQA(ctx, session, msg.Content, customAgent, kbIDs, userKey, msg.Quote, msg.ImageURLs)
if err != nil {
logger.Errorf(ctx, "[IM] QA fallback failed: %v", err)
answer = "抱歉,处理您的问题时出现了异常,请稍后再试。"
answer = imQAErrorReply(msg.ImageURLs)
}
return adapter.SendReply(ctx, msg, &ReplyMessage{Content: formatIMOutboundAnswer(ctx, answer, tenant, s.defaultFileSvc), IsFinal: true})
}
// runQA executes the WeKnora QA pipeline and returns the full answer text.
func (s *Service) runQA(ctx context.Context, session *types.Session, query string, customAgent *types.CustomAgent, kbIDs []string, userKey string, quote *QuotedMessage) (string, error) {
func (s *Service) runQA(ctx context.Context, session *types.Session, query string, customAgent *types.CustomAgent, kbIDs []string, userKey string, quote *QuotedMessage, imageURLs ...[]string) (string, error) {
// Cancellable context (no hard deadline): each agent round has its own
// LLMCallTimeout. The context can still be cancelled by /stop.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
eventBus := event.NewEventBus()
var images []string
if len(imageURLs) > 0 {
images = imageURLs[0]
}
// Thread-safe answer collection
var answerMu sync.Mutex
@@ -2365,7 +2493,7 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin
// Run QA async
go func() {
var err error
req := buildIMQARequest(session, query, assistantMsg.ID, userMsg.ID, customAgent, kbIDs, quote)
req := buildIMQARequest(session, query, assistantMsg.ID, userMsg.ID, customAgent, kbIDs, quote, images)
if req.QuotedContext != "" {
logger.Debugf(ctx, "[IM] QuotedContext set: length=%d", len(req.QuotedContext))
}
@@ -2406,6 +2534,14 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin
qaError := qaErr
answerMu.Unlock()
if replacement, override := failClosedIMImageAnswer(answer, qaError, images); override {
assistantMsg.Content = replacement
assistantMsg.IsCompleted = true
if updateErr := s.messageService.UpdateMessage(ctx, assistantMsg); updateErr != nil {
logger.Warnf(ctx, "[IM] Failed to persist image QA error: %v", updateErr)
}
return "", qaError
}
if answer == "" && qaError != nil {
return "", qaError
}
+42
View File
@@ -0,0 +1,42 @@
package im
import (
"context"
"errors"
"strings"
"testing"
)
type failingFinalStreamSender struct {
finalizeErr error
endErr error
ended bool
}
func (*failingFinalStreamSender) StartStream(context.Context, *IncomingMessage) (string, error) {
return "stream", nil
}
func (*failingFinalStreamSender) UpdateStreamContent(context.Context, *IncomingMessage, string, string) error {
return nil
}
func (s *failingFinalStreamSender) FinalizeStream(context.Context, *IncomingMessage, string, string) error {
return s.finalizeErr
}
func (s *failingFinalStreamSender) EndStream(context.Context, *IncomingMessage, string) error {
s.ended = true
return s.endErr
}
func TestFinalizeAndEndIMStreamPropagatesDeliveryFailure(t *testing.T) {
sender := &failingFinalStreamSender{
finalizeErr: errors.New("simulated finalize failure"),
endErr: errors.New("simulated end failure"),
}
err := finalizeAndEndIMStream(context.Background(), sender, &IncomingMessage{}, "stream", "final")
if err == nil || !strings.Contains(err.Error(), "finalize IM stream") {
t.Fatalf("error = %v; want finalize delivery error", err)
}
if !sender.ended {
t.Fatal("EndStream must run even after FinalizeStream fails")
}
}