Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

feat(chatchannel): stream Feishu, WeCom and DingTalk replies (#22041)

ciel3af179b868e8
diff --git a/internal/channels/bootstrap.go b/internal/channels/bootstrap.go
index e6807b5610..5b8527c8cf 100644
--- a/internal/channels/bootstrap.go
+++ b/internal/channels/bootstrap.go
@@ -35,9 +35,10 @@ import (
)
const (
- reconcileInterval = 10 * time.Second
- initialStartRetryDelay = 10 * time.Second
- maxStartRetryDelay = 5 * time.Minute
+ reconcileInterval = 10 * time.Second
+ initialStartRetryDelay = 10 * time.Second
+ maxStartRetryDelay = 5 * time.Minute
+ channelFinalSendTimeout = 30 * time.Second
)
type runningChannel struct {
@@ -265,6 +266,37 @@ func (r *Runtime) startChannel(ctx context.Context, accountID string, wanted des
channelService = service.NewChatChannelServiceWithAgentService(r.agentService)
}
ch.SetMessageHandler(func(ctx context.Context, msg core.IncomingMessage) error {
+ outgoing := core.OutgoingMessage{ChatID: msg.ChatID, ReplyToMessageID: msg.MessageID}
+
+ // A streaming channel owns its own message lifecycle: cumulative text goes
+ // to SendStream, and the bridge sends the complete answer if streaming fails.
+ var onDelta func(string)
+ var text chan string
+ var sent chan error
+ if streamer, ok := ch.(core.StreamingChannel); ok && streamer.StreamEnabled() {
+ text = make(chan string, 1)
+ sent = make(chan error, 1)
+ go func() {
+ sent <- streamer.SendStream(ctx, outgoing, text)
+ }()
+ onDelta = func(value string) {
+ // Keep only the newest cumulative snapshot; the channel must not
+ // build a backlog while the platform renders older text.
+ select {
+ case text <- value:
+ default:
+ select {
+ case <-text:
+ default:
+ }
+ select {
+ case text <- value:
+ default:
+ }
+ }
+ }
+ }
+
answer, err := channelService.HandleIncomingMessage(ctx, service.ChatChannelIncomingMessage{
Channel: msg.Channel,
AccountID: msg.AccountID,
@@ -273,15 +305,34 @@ func (r *Runtime) startChannel(ctx context.Context, accountID string, wanted des
MessageID: msg.MessageID,
SenderID: msg.SenderID,
Text: msg.Text,
- })
+ }, onDelta)
+
+ if text != nil {
+ // Streaming updates may have been coalesced. Make the final snapshot
+ // available before closing so a successful stream always has the full answer.
+ if answer != "" {
+ onDelta(answer)
+ }
+ close(text)
+ streamErr := <-sent
+ if streamErr == nil {
+ return err
+ }
+ if err == nil && answer == "" {
+ // The model produced nothing, so there is nothing to deliver.
+ return nil
+ }
+ common.Warn("chat channel stream failed, sending the answer as one message", zap.Error(streamErr), zap.String("channel", ch.ChannelID()), zap.String("account_id", accountID))
+ }
if err != nil || answer == "" {
return err
}
- return ch.Send(ctx, core.OutgoingMessage{
- ChatID: msg.ChatID,
- Text: answer,
- ReplyToMessageID: msg.MessageID,
- })
+ // The turn's context may already be past its deadline, so the fallback send gets a
+ // context that cannot be cancelled by it.
+ sendCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), channelFinalSendTimeout)
+ defer cancel()
+ outgoing.Text = answer
+ return ch.Send(sendCtx, outgoing)
})
if err = ch.Start(ctx); err != nil {
return err
diff --git a/internal/channels/core/channel.go b/internal/channels/core/channel.go
index 14a630e620..173d7b9e05 100644
--- a/internal/channels/core/channel.go
+++ b/internal/channels/core/channel.go
@@ -45,3 +45,12 @@ type Channel interface {
Stop(ctx context.Context) error // Stop cancels channel event
Send(ctx context.Context, msg OutgoingMessage) error // Send posts an outgoing RAGFlow answer to the channel
}
+
+// StreamingChannel is a channel that can deliver a reply while it is generated.
+// SendStream receives the cumulative display text on text; closing text finishes
+// the message. An error means streaming did not complete, so the caller sends the
+// complete answer through Send.
+type StreamingChannel interface {
+ StreamEnabled() bool // StreamEnabled reports whether the account asked for streaming replies
+ SendStream(ctx context.Context, msg OutgoingMessage, text <-chan string) error
+}
diff --git a/internal/channels/dingtalk.go b/internal/channels/dingtalk.go
index 7b72a27b75..7bc7f19477 100644
--- a/internal/channels/dingtalk.go
+++ b/internal/channels/dingtalk.go
@@ -45,14 +45,33 @@ const (
dingTalkMessageTTL = time.Hour
dingTalkQueueSize = 64
dingTalkWorkerIdle = 5 * time.Minute
+ dingTalkStreamTick = 100 * time.Millisecond
+ dingTalkTokenMargin = 60 * time.Second
+ dingTalkCardFailTimeout = 10 * time.Second
+ dingTalkStreamAttempts = 3
+ dingTalkStreamBackoff = 300 * time.Millisecond
+ dingTalkStreamCallLimit = 10 * time.Second
)
+// errDingTalkNothingStreamed reports that no text was available for a streamed card, so
+// the caller still has to deliver the answer.
+var errDingTalkNothingStreamed = errors.New("dingtalk: no streamed text to display")
+
type dingTalkAccount struct {
- AccountID string
- ClientID string
- ClientSecret string
- APIBaseURL string
- Timeout time.Duration
+ AccountID string
+ ClientID string
+ ClientSecret string
+ APIBaseURL string
+ Timeout time.Duration
+ Stream bool
+ CardTemplateID string
+ StreamContentKey string
+}
+
+// dingTalkPeer remembers where a conversation lives so an AI card can be delivered to it.
+type dingTalkPeer struct {
+ chatType string
+ senderStaffID string
}
type dingTalkChannel struct {
@@ -65,9 +84,13 @@ type dingTalkChannel struct {
client *http.Client
stream *client.StreamClient
sessionWebhooks map[string]string
+ peers map[string]dingTalkPeer
processed map[string]time.Time
inflight map[string]time.Time
workers map[string]*dingTalkWorker
+ tokenMu sync.Mutex
+ accessToken string
+ tokenExpiry time.Time
}
type dingTalkWorker struct {
@@ -87,14 +110,18 @@ func newDingTalkChannel(account dingTalkAccount) (*dingTalkChannel, error) {
if account.Timeout <= 0 {
account.Timeout = defaultDingTalkTimeout
}
+ if account.StreamContentKey == "" {
+ account.StreamContentKey = "content"
+ }
proxyURL, err := dingTalkSafeProxyURL()
if err != nil {
return nil, err
}
ch := &dingTalkChannel{
account: account,
- client: newSafeChannelHTTPClient(account.Timeout, channelURLPolicy{exactHosts: []string{"oapi.dingtalk.com"}}),
+ client: newSafeChannelHTTPClient(account.Timeout, channelURLPolicy{exactHosts: []string{"oapi.dingtalk.com", "api.dingtalk.com"}}),
sessionWebhooks: map[string]string{},
+ peers: map[string]dingTalkPeer{},
processed: map[string]time.Time{},
inflight: map[string]time.Time{},
workers: map[string]*dingTalkWorker{},
@@ -123,11 +150,14 @@ func newDingTalkChannelFromConfig(accountID string, cfg map[string]any) (*dingTa
}
return newDingTalkChannel(dingTalkAccount{
- AccountID: accountID,
- ClientID: clientID,
- ClientSecret: clientSecret,
- APIBaseURL: firstString(cfg, "api_base_url", "api_url"),
- Timeout: timeout,
+ AccountID: accountID,
+ ClientID: clientID,
+ ClientSecret: clientSecret,
+ APIBaseURL: firstString(cfg, "api_base_url", "api_url"),
+ Timeout: timeout,
+ Stream: cfg["stream"] == true,
+ CardTemplateID: firstString(cfg, "card_template_id"),
+ StreamContentKey: firstString(cfg, "stream_content_key"),
})
}
@@ -171,6 +201,7 @@ func (c *dingTalkChannel) Stop(ctx context.Context) error {
c.cancel = nil
c.ctx = nil
c.sessionWebhooks = map[string]string{}
+ c.peers = map[string]dingTalkPeer{}
c.processed = map[string]time.Time{}
c.inflight = map[string]time.Time{}
c.workers = map[string]*dingTalkWorker{}
@@ -276,6 +307,9 @@ func (c *dingTalkChannel) normalizeBotMessage(data *chatbot.BotCallbackDataModel
if data.SessionWebhook != "" && chatID != "" {
c.saveSessionWebhook(chatID, data.SessionWebhook)
}
+ if chatID != "" {
+ c.savePeer(chatID, data.ConversationType, data.SenderStaffId)
+ }
if text == "" || chatID == "" || senderID == "" {
return core.IncomingMessage{}, "", false
}
@@ -446,6 +480,247 @@ func (c *dingTalkChannel) saveSessionWebhook(chatID, sessionWebhook string) {
c.sessionWebhooks[chatID] = sessionWebhook
}
+// savePeer caches where a conversation lives so streamed cards can be delivered to it.
+func (c *dingTalkChannel) savePeer(chatID, chatType, senderStaffID string) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.peers[chatID] = dingTalkPeer{chatType: chatType, senderStaffID: senderStaffID}
+}
+
+// chatPeer returns the cached delivery target of one conversation.
+func (c *dingTalkChannel) chatPeer(chatID string) dingTalkPeer {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.peers[chatID]
+}
+
+// StreamEnabled reports whether this account asked for streamed card replies. Streaming
+// needs the AI card template that carries the streaming markdown variable.
+func (c *dingTalkChannel) StreamEnabled() bool {
+ return c.account.Stream && c.account.CardTemplateID != ""
+}
+
+// SendStream replies with an AI card: the card is created on the first text, the answer is
+// appended to its streaming variable, and the last update closes the card.
+func (c *dingTalkChannel) SendStream(ctx context.Context, msg core.OutgoingMessage, text <-chan string) error {
+ if strings.TrimSpace(msg.ChatID) == "" {
+ return errors.New("chat_id is required")
+ }
+
+ var first string
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case value, open := <-text:
+ if !open || strings.TrimSpace(value) == "" {
+ return errDingTalkNothingStreamed
+ }
+ first = value
+ }
+
+ outTrackID, err := c.createAICard(ctx, msg.ChatID)
+ if err != nil {
+ return err
+ }
+
+ latest, sent := first, ""
+ // The card is visible from here on, so a broken stream marks it as failed instead of
+ // leaving it in the processing state.
+ fail := func(err error) error {
+ c.markCardFailed(ctx, outTrackID, sent)
+ return err
+ }
+ ticker := time.NewTicker(dingTalkStreamTick)
+ defer ticker.Stop()
+ for {
+ select {
+ case <-ctx.Done():
+ return fail(ctx.Err())
+ case value, open := <-text:
+ if !open {
+ // A closed channel returns immediately forever, so stop selecting on it and
+ // let the ticker drain what is still unsent.
+ text = nil
+ continue
+ }
+ latest = value
+ case <-ticker.C:
+ if sent != latest {
+ delta, replace := dingTalkStreamDelta(sent, latest)
+ if err := c.pushCardDelta(ctx, outTrackID, delta, replace, false); err != nil {
+ return fail(err)
+ }
+ sent = latest
+ continue
+ }
+ if text == nil {
+ if err := c.pushCardDelta(ctx, outTrackID, "", false, true); err != nil {
+ return fail(err)
+ }
+ return nil
+ }
+ }
+ }
+}
+
+// markCardFailed switches the AI card to its failed state. The card keeps whatever it
+// already showed; only the flow status moves to failed, which the platform requires the
+// application to set.
+func (c *dingTalkChannel) markCardFailed(ctx context.Context, outTrackID, content string) {
+ failCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), dingTalkCardFailTimeout)
+ defer cancel()
+ token, err := c.openAPIToken(failCtx)
+ if err != nil {
+ common.Error("dingtalk: failed card could not get an access token", err, zap.String("account_id", c.account.AccountID), zap.String("card_id", outTrackID))
+ return
+ }
+ body := map[string]any{
+ "outTrackId": outTrackID,
+ "cardData": map[string]any{"cardParamMap": map[string]any{
+ "flowStatus": "5",
+ c.account.StreamContentKey: content,
+ }},
+ "cardUpdateOptions": map[string]any{"updateCardDataByKey": true},
+ }
+ if err := c.doOpenAPI(failCtx, http.MethodPut, "/v1.0/card/instances", token, body, nil); err != nil {
+ common.Error("dingtalk: marking the card as failed failed", err, zap.String("account_id", c.account.AccountID), zap.String("card_id", outTrackID))
+ }
+}
+
+// dingTalkStreamDelta returns the text the card still needs and whether it replaces the
+// card content. A rewritten answer has to replace: the streaming API appends whenever
+// isFull is false, which would leave the stale answer in front of the new one.
+func dingTalkStreamDelta(shown, target string) (string, bool) {
+ if strings.HasPrefix(target, shown) {
+ return target[len(shown):], false
+ }
+ return target, true
+}
+
+// createAICard delivers an empty AI card to the conversation and returns its instance id.
+func (c *dingTalkChannel) createAICard(ctx context.Context, chatID string) (string, error) {
+ peer := c.chatPeer(chatID)
+ outTrackID := "ragflow_" + common.GenerateToken()
+ body := map[string]any{
+ "cardTemplateId": c.account.CardTemplateID,
+ "outTrackId": outTrackID,
+ "cardData": map[string]any{"cardParamMap": map[string]any{
+ "flowStatus": "1",
+ c.account.StreamContentKey: "",
+ }},
+ "callbackType": "STREAM",
+ "imGroupOpenSpaceModel": map[string]any{"supportForward": true},
+ "imRobotOpenSpaceModel": map[string]any{"supportForward": true},
+ }
+ if peer.chatType == "2" {
+ body["openSpaceId"] = "dtv1.card//IM_GROUP." + chatID
+ body["imGroupOpenDeliverModel"] = map[string]any{"robotCode": c.account.ClientID}
+ } else {
+ if peer.senderStaffID == "" {
+ return "", errors.New("dingtalk stream card needs the sender staff id of a one-to-one chat")
+ }
+ body["openSpaceId"] = "dtv1.card//IM_ROBOT." + peer.senderStaffID
+ body["imRobotOpenDeliverModel"] = map[string]any{"spaceType": "IM_ROBOT"}
+ }
+ token, err := c.openAPIToken(ctx)
+ if err != nil {
+ return "", err
+ }
+ if err := c.doOpenAPI(ctx, http.MethodPost, "/v1.0/card/instances/createAndDeliver", token, body, nil); err != nil {
+ return "", err
+ }
+ return outTrackID, nil
+}
+
+// pushCardDelta writes content to the card's streaming variable: replace decides between a
+// full rewrite and an append, finalize closes the stream. Every push is one request, so a
+// transient failure is retried with the same idempotency id instead of failing the reply.
+func (c *dingTalkChannel) pushCardDelta(ctx context.Context, outTrackID, content string, replace, finalize bool) error {
+ guid := common.GenerateToken()
+ return retryChannelCall(ctx, dingTalkStreamAttempts, dingTalkStreamCallLimit, dingTalkStreamBackoff, func(callCtx context.Context) error {
+ token, err := c.openAPIToken(callCtx)
+ if err != nil {
+ return err
+ }
+ return c.doOpenAPI(callCtx, http.MethodPut, "/v1.0/card/streaming", token, map[string]any{
+ "outTrackId": outTrackID,
+ "guid": guid,
+ "key": c.account.StreamContentKey,
+ "content": content,
+ "isFull": replace,
+ "isFinalize": finalize,
+ "isError": false,
+ }, nil)
+ })
+}
+
+// openAPIToken returns a cached DingTalk open API access token.
+func (c *dingTalkChannel) openAPIToken(ctx context.Context) (string, error) {
+ c.tokenMu.Lock()
+ defer c.tokenMu.Unlock()
+ if c.accessToken != "" && time.Now().Before(c.tokenExpiry) {
+ return c.accessToken, nil
+ }
+ var response struct {
+ AccessToken string `json:"accessToken"`
+ ExpireIn int64 `json:"expireIn"`
+ }
+ if err := c.doOpenAPI(ctx, http.MethodPost, "/v1.0/oauth2/accessToken", "", map[string]any{
+ "appKey": c.account.ClientID,
+ "appSecret": c.account.ClientSecret,
+ }, &response); err != nil {
+ return "", err
+ }
+ if response.AccessToken == "" {
+ return "", errors.New("dingtalk open api returned no access token")
+ }
+ expiry := time.Duration(response.ExpireIn) * time.Second
+ if expiry <= 0 {
+ expiry = time.Hour
+ }
+ c.accessToken = response.AccessToken
+ c.tokenExpiry = time.Now().Add(expiry - dingTalkTokenMargin)
+ return c.accessToken, nil
+}
+
+// doOpenAPI calls the DingTalk open API and reports the platform error payload.
+func (c *dingTalkChannel) doOpenAPI(ctx context.Context, method, path, token string, body map[string]any, out any) error {
+ payload, err := json.Marshal(body)
+ if err != nil {
+ return err
+ }
+ req, err := http.NewRequestWithContext(ctx, method, strings.TrimRight(c.account.APIBaseURL, "/")+path, bytes.NewReader(payload))
+ if err != nil {
+ return err
+ }
+ req.Header.Set("Content-Type", "application/json")
+ if token != "" {
+ req.Header.Set("x-acs-dingtalk-access-token", token)
+ }
+ resp, err := c.client.Do(req)
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+ data, _ := io.ReadAll(io.LimitReader(resp.Body, 1024*1024))
+ if resp.StatusCode >= 400 {
+ return fmt.Errorf("dingtalk open api %s status: %d, response: %s", path, resp.StatusCode, string(data))
+ }
+ var envelope struct {
+ Success *bool `json:"success"`
+ Code string `json:"code"`
+ Message string `json:"message"`
+ }
+ _ = json.Unmarshal(data, &envelope)
+ if envelope.Success != nil && !*envelope.Success {
+ return fmt.Errorf("dingtalk open api %s failed: code=%s message=%s", path, envelope.Code, envelope.Message)
+ }
+ if out != nil {
+ return json.Unmarshal(data, out)
+ }
+ return nil
+}
+
// postWebhook posts a Markdown reply payload to a DingTalk sessionWebhook.
func (c *dingTalkChannel) postWebhook(ctx context.Context, sessionWebhook string, payload map[string]any) error {
body, _ := json.Marshal(payload)
diff --git a/internal/channels/dingtalk_test.go b/internal/channels/dingtalk_test.go
index 4dc9392950..9754515661 100644
--- a/internal/channels/dingtalk_test.go
+++ b/internal/channels/dingtalk_test.go
@@ -173,3 +173,294 @@ func TestDingTalkEnqueueFailureCancelsInflight(t *testing.T) {
t.Fatal("inflight key remains after enqueue failure")
}
}
+
+func TestDingTalkStreamEnabledNeedsSwitchAndTemplate(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ cfg map[string]any
+ want bool
+ }{
+ {name: "switch and template", cfg: map[string]any{"stream": true, "card_template_id": "tpl-1"}, want: true},
+ {name: "switch without template", cfg: map[string]any{"stream": true}, want: false},
+ {name: "template without switch", cfg: map[string]any{"card_template_id": "tpl-1"}, want: false},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ cfg := map[string]any{"client_id": "client-1", "client_secret": "secret-1"}
+ for key, value := range test.cfg {
+ cfg[key] = value
+ }
+ ch, err := newDingTalkChannelFromConfig("account-1", cfg)
+ if err != nil {
+ t.Fatalf("newDingTalkChannelFromConfig() error = %v", err)
+ }
+ if got := ch.StreamEnabled(); got != test.want {
+ t.Fatalf("StreamEnabled() = %t, want %t", got, test.want)
+ }
+ if ch.account.StreamContentKey != "content" {
+ t.Fatalf("stream content key = %q, want content", ch.account.StreamContentKey)
+ }
+ })
+ }
+}
+
+func TestDingTalkSendStreamCreatesAndFinishesCard(t *testing.T) {
+ type apiCall struct {
+ method string
+ path string
+ body map[string]any
+ }
+ var calls []apiCall
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ body := map[string]any{}
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode body: %v", err)
+ return
+ }
+ calls = append(calls, apiCall{method: r.Method, path: r.URL.Path, body: body})
+ w.Header().Set("Content-Type", "application/json")
+ switch r.URL.Path {
+ case "/v1.0/oauth2/accessToken":
+ _, _ = w.Write([]byte(`{"accessToken":"token-1","expireIn":7200}`))
+ default:
+ _, _ = w.Write([]byte(`{"success":true,"result":{}}`))
+ }
+ }))
+ defer server.Close()
+ allowChannelURLForTest(t, server.URL)
+
+ ch, err := newDingTalkChannel(dingTalkAccount{
+ AccountID: "account-1",
+ ClientID: "client-1",
+ ClientSecret: "secret-1",
+ APIBaseURL: server.URL,
+ Stream: true,
+ CardTemplateID: "tpl-1",
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ ch.peers["chat-1"] = dingTalkPeer{chatType: "2"}
+
+ text := make(chan string, 2)
+ text <- "Hel"
+ text <- "Hello"
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err != nil {
+ t.Fatalf("SendStream() error = %v", err)
+ }
+
+ var createBody map[string]any
+ var streamCalls []map[string]any
+ for _, call := range calls {
+ switch call.path {
+ case "/v1.0/card/instances/createAndDeliver":
+ createBody = call.body
+ case "/v1.0/card/streaming":
+ streamCalls = append(streamCalls, call.body)
+ }
+ }
+ if createBody == nil {
+ t.Fatalf("no card create call, calls = %+v", calls)
+ }
+ if createBody["cardTemplateId"] != "tpl-1" {
+ t.Fatalf("cardTemplateId = %v, want tpl-1", createBody["cardTemplateId"])
+ }
+ if createBody["openSpaceId"] != "dtv1.card//IM_GROUP.chat-1" {
+ t.Fatalf("openSpaceId = %v, want the group space", createBody["openSpaceId"])
+ }
+ cardParamMap := weComMapValue(createBody["cardData"])["cardParamMap"]
+ if weComMapValue(cardParamMap)["flowStatus"] != "1" {
+ t.Fatalf("cardParamMap = %+v, want flowStatus 1", cardParamMap)
+ }
+ if _, ok := weComMapValue(cardParamMap)["content"]; !ok {
+ t.Fatalf("cardParamMap = %+v, want the streaming content key", cardParamMap)
+ }
+
+ if len(streamCalls) == 0 {
+ t.Fatal("no streaming call was made")
+ }
+ outTrackID, _ := createBody["outTrackId"].(string)
+ var content string
+ for index, call := range streamCalls {
+ if call["outTrackId"] != outTrackID {
+ t.Fatalf("streaming call %d outTrackId = %v, want %q", index, call["outTrackId"], outTrackID)
+ }
+ if call["key"] != "content" {
+ t.Fatalf("streaming call %d key = %v, want content", index, call["key"])
+ }
+ if call["isFull"] != false {
+ t.Fatalf("streaming call %d isFull = %v, want an append", index, call["isFull"])
+ }
+ content += weComStringValue(call["content"])
+ wantFinal := index == len(streamCalls)-1
+ if call["isFinalize"] != wantFinal {
+ t.Fatalf("streaming call %d isFinalize = %v, want %t", index, call["isFinalize"], wantFinal)
+ }
+ }
+ if content != "Hello" {
+ t.Fatalf("streamed content = %q, want the complete answer", content)
+ }
+}
+
+func TestDingTalkSendStreamNeedsSenderForDirectChat(t *testing.T) {
+ ch, err := newDingTalkChannel(dingTalkAccount{
+ AccountID: "account-1",
+ ClientID: "client-1",
+ ClientSecret: "secret-1",
+ Stream: true,
+ CardTemplateID: "tpl-1",
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ ch.peers["chat-1"] = dingTalkPeer{chatType: "1"}
+
+ text := make(chan string, 1)
+ text <- "Hello"
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err == nil {
+ t.Fatal("SendStream succeeded without the sender staff id")
+ }
+}
+
+func TestDingTalkSendStreamMarksCardFailed(t *testing.T) {
+ type apiCall struct {
+ method string
+ path string
+ body map[string]any
+ }
+ var calls []apiCall
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ body := map[string]any{}
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode body: %v", err)
+ return
+ }
+ calls = append(calls, apiCall{method: r.Method, path: r.URL.Path, body: body})
+ w.Header().Set("Content-Type", "application/json")
+ switch r.URL.Path {
+ case "/v1.0/oauth2/accessToken":
+ _, _ = w.Write([]byte(`{"accessToken":"token-1","expireIn":7200}`))
+ case "/v1.0/card/streaming":
+ _, _ = w.Write([]byte(`{"success":false,"code":"InvalidParameter","message":"rejected"}`))
+ default:
+ _, _ = w.Write([]byte(`{"success":true,"result":{}}`))
+ }
+ }))
+ defer server.Close()
+ allowChannelURLForTest(t, server.URL)
+
+ ch, err := newDingTalkChannel(dingTalkAccount{
+ AccountID: "account-1",
+ ClientID: "client-1",
+ ClientSecret: "secret-1",
+ APIBaseURL: server.URL,
+ Stream: true,
+ CardTemplateID: "tpl-1",
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ ch.peers["chat-1"] = dingTalkPeer{chatType: "2"}
+
+ text := make(chan string, 2)
+ text <- "Hello"
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err == nil {
+ t.Fatal("SendStream succeeded although the card update was rejected")
+ }
+
+ var createBody, failBody map[string]any
+ for _, call := range calls {
+ switch call.path {
+ case "/v1.0/card/instances/createAndDeliver":
+ createBody = call.body
+ case "/v1.0/card/instances":
+ if call.method == http.MethodPut {
+ failBody = call.body
+ }
+ }
+ }
+ if createBody == nil {
+ t.Fatalf("no card create call, calls = %+v", calls)
+ }
+ if failBody == nil {
+ t.Fatalf("card was not marked failed, calls = %+v", calls)
+ }
+ if failBody["outTrackId"] != createBody["outTrackId"] {
+ t.Fatalf("failed card outTrackId = %v, want %v", failBody["outTrackId"], createBody["outTrackId"])
+ }
+ paramMap := weComMapValue(weComMapValue(failBody["cardData"])["cardParamMap"])
+ if paramMap["flowStatus"] != "5" {
+ t.Fatalf("cardParamMap = %+v, want flowStatus 5", paramMap)
+ }
+ // The first update was rejected, so the card never received text.
+ if paramMap["content"] != "" {
+ t.Fatalf("failed card content = %v, want the text that actually reached the card", paramMap["content"])
+ }
+}
+
+func TestDingTalkPushCardDeltaRetriesWithSameGuid(t *testing.T) {
+ var guids []string
+ failures := 1
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ body := map[string]any{}
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode body: %v", err)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ switch r.URL.Path {
+ case "/v1.0/oauth2/accessToken":
+ _, _ = w.Write([]byte(`{"accessToken":"token-1","expireIn":7200}`))
+ case "/v1.0/card/streaming":
+ guid, _ := body["guid"].(string)
+ guids = append(guids, guid)
+ if failures > 0 {
+ failures--
+ _, _ = w.Write([]byte(`{"success":false,"code":"ServiceUnavailable","message":"try later"}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"success":true,"result":{}}`))
+ default:
+ _, _ = w.Write([]byte(`{"success":true,"result":{}}`))
+ }
+ }))
+ defer server.Close()
+ allowChannelURLForTest(t, server.URL)
+
+ ch, err := newDingTalkChannel(dingTalkAccount{
+ AccountID: "account-1",
+ ClientID: "client-1",
+ ClientSecret: "secret-1",
+ APIBaseURL: server.URL,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if err := ch.pushCardDelta(context.Background(), "track-1", "hello", false, false); err != nil {
+ t.Fatalf("pushCardDelta() error = %v", err)
+ }
+ if len(guids) != 2 {
+ t.Fatalf("push attempts = %d, want the failed attempt and the successful retry", len(guids))
+ }
+ if guids[0] == "" || guids[0] != guids[1] {
+ t.Fatalf("guids = %v, want the retry to reuse the idempotency id", guids)
+ }
+}
+
+func TestDingTalkStreamDeltaReplacesRewrittenAnswer(t *testing.T) {
+ delta, replace := dingTalkStreamDelta("Hello", "Hello world")
+ if delta != " world" || replace {
+ t.Fatalf("delta = %q replace = %t, want an append of the new tail", delta, replace)
+ }
+
+ delta, replace = dingTalkStreamDelta("Half an answer", "Please continue in RAGFlow.")
+ if delta != "Please continue in RAGFlow." || !replace {
+ t.Fatalf("delta = %q replace = %t, want the whole rewritten answer replacing the card", delta, replace)
+ }
+}
diff --git a/internal/channels/feishu.go b/internal/channels/feishu.go
index c521fa6d64..8f8980d731 100644
--- a/internal/channels/feishu.go
+++ b/internal/channels/feishu.go
@@ -30,6 +30,7 @@ import (
lark "github.com/larksuite/oapi-sdk-go/v3"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/event/dispatcher"
+ larkcardkit "github.com/larksuite/oapi-sdk-go/v3/service/cardkit/v1"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
larkws "github.com/larksuite/oapi-sdk-go/v3/ws"
"go.uber.org/zap"
@@ -39,19 +40,37 @@ import (
)
const (
- defaultFeishuDomain = "feishu"
- defaultFeishuTimeout = 30 * time.Second
- feishuMessageTTL = time.Hour
- feishuQueueSize = 64
- feishuWorkerIdle = 5 * time.Minute
+ defaultFeishuDomain = "feishu"
+ defaultFeishuTimeout = 30 * time.Second
+ feishuTurnBudget = 5 * time.Minute
+ feishuMessageTTL = time.Hour
+ feishuQueueSize = 64
+ feishuWorkerIdle = 5 * time.Minute
+ feishuStreamTick = 100 * time.Millisecond
+ feishuStreamStep = 10
+ feishuStreamAttempts = 3
+ feishuStreamBackoff = 300 * time.Millisecond
+ feishuStreamCallLimit = 10 * time.Second
+ feishuCloseTimeout = feishuStreamAttempts*feishuStreamCallLimit + feishuStreamAttempts*feishuStreamBackoff
+ feishuStreamElementID = "ragflow_stream"
)
+// feishuStreamCardJSON is the card 2.0 payload whose markdown element receives the
+// streamed answer. Streaming mode and shared-card mode are both required by the
+// card element content API.
+var feishuStreamCardJSON = `{"schema":"2.0","config":{"streaming_mode":true,"update_multi":true,"summary":{"content":"RAGFlow"}},"body":{"elements":[{"tag":"markdown","element_id":"` + feishuStreamElementID + `","content":""}]}}`
+
+// errFeishuNothingStreamed reports that no text reached the card, so the caller still
+// has to deliver the answer.
+var errFeishuNothingStreamed = errors.New("feishu: no streamed text to display")
+
type feishuAccount struct {
AccountID string
AppID string
AppSecret string
Domain string
Timeout time.Duration
+ Stream bool
}
type feishuChannel struct {
@@ -130,6 +149,7 @@ func newFeishuChannelFromConfig(accountID string, cfg map[string]any) (*feishuCh
AppSecret: appSecret,
Domain: valueOrDefault(firstString(cfg, "domain"), defaultFeishuDomain),
Timeout: timeout,
+ Stream: cfg["stream"] == true,
}), nil
}
@@ -198,15 +218,135 @@ func (c *feishuChannel) Send(ctx context.Context, msg core.OutgoingMessage) erro
}
content, _ := json.Marshal(map[string]string{"text": msg.Text})
- contentText := string(content)
- msgType := "text"
+ return c.sendMessage(ctx, msg, "text", string(content))
+}
+
+// StreamEnabled reports whether this account streams replies.
+func (c *feishuChannel) StreamEnabled() bool {
+ return c.account.Stream
+}
+
+// SendStream delivers the answer as a streaming card: the card is created once the
+// first text arrives, refreshed at a fixed interval, and closed when text is drained.
+// An error means no text reached the card, so the caller still sends the full answer.
+func (c *feishuChannel) SendStream(ctx context.Context, msg core.OutgoingMessage, text <-chan string) error {
+ if strings.TrimSpace(msg.ChatID) == "" {
+ return errors.New("chat_id is required")
+ }
+ var first string
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case value, open := <-text:
+ if !open || strings.TrimSpace(value) == "" {
+ return errFeishuNothingStreamed
+ }
+ first = value
+ }
+
+ cardID, err := c.createStreamCard(ctx)
+ if err != nil {
+ return err
+ }
+ cardMessage, _ := json.Marshal(map[string]any{"type": "card", "data": map[string]string{"card_id": cardID}})
+ if err := c.sendMessage(ctx, msg, "interactive", string(cardMessage)); err != nil {
+ return err
+ }
+
+ sequence := 0
+ latest, sent := first, ""
+ flush := func(value string) error {
+ if value == "" || value == sent {
+ return nil
+ }
+ next := sequence + 1
+ uuid := common.GenerateToken()
+ err := feishuRetry(ctx, func(callCtx context.Context) error {
+ resp, err := c.rest.Cardkit.V1.CardElement.Content(callCtx, larkcardkit.NewContentCardElementReqBuilder().
+ CardId(cardID).
+ ElementId(feishuStreamElementID).
+ Body(larkcardkit.NewContentCardElementReqBodyBuilder().
+ Content(value).
+ Sequence(next).
+ Uuid(uuid).
+ Build()).
+ Build())
+ if err != nil {
+ return err
+ }
+ return feishuResponseError("card content", resp.Code, resp.Msg)
+ })
+ if err != nil {
+ return err
+ }
+ sequence = next
+ sent = value
+ return nil
+ }
+ // fail reports an update that stayed broken after the retries. The bridge then sends
+ // the complete answer as a normal message, including text that was already streamed.
+ fail := func(action string, err error) error {
+ common.Error("feishu: "+action, err, zap.String("account_id", c.account.AccountID), zap.String("card_id", cardID))
+ _ = c.closeStreamCard(ctx, cardID, &sequence)
+ return err
+ }
+ ticker := time.NewTicker(feishuStreamTick)
+ defer ticker.Stop()
+ for {
+ select {
+ case <-ctx.Done():
+ // The turn is over, but the card still has to be closed or it keeps rendering as
+ // in progress.
+ _ = c.closeStreamCard(ctx, cardID, &sequence)
+ return ctx.Err()
+ case value, open := <-text:
+ if !open {
+ // A closed channel returns immediately forever, so stop selecting on it and
+ // let the ticker drain what is still unsent.
+ text = nil
+ continue
+ }
+ latest = value
+ case <-ticker.C:
+ if sent != latest {
+ if err := flush(nextStreamText(sent, latest, feishuStreamStep)); err != nil {
+ return fail("card content failed", err)
+ }
+ continue
+ }
+ if text == nil {
+ return c.closeStreamCard(ctx, cardID, &sequence)
+ }
+ }
+ }
+}
+
+// nextStreamText returns the text to display this tick, advancing by at most step runes.
+// A rewritten target starts from its first rune so it cannot trigger a full block update.
+func nextStreamText(shown, target string, step int) string {
+ if step < 1 {
+ step = 1
+ }
+ runes := []rune(target)
+ at := 0
+ if strings.HasPrefix(target, shown) {
+ at = len([]rune(shown))
+ }
+ if len(runes)-at <= step {
+ return target
+ }
+ return string(runes[:at+step])
+}
+
+// sendMessage posts msgType/content to the chat, as a reply when msg carries a target.
+func (c *feishuChannel) sendMessage(ctx context.Context, msg core.OutgoingMessage, msgType, content string) error {
if strings.TrimSpace(msg.ReplyToMessageID) != "" {
resp, err := c.rest.Im.Message.Reply(ctx, larkim.NewReplyMessageReqBuilder().
MessageId(msg.ReplyToMessageID).
Body(larkim.NewReplyMessageReqBodyBuilder().
MsgType(msgType).
- Content(contentText).
+ Content(content).
Build()).
Build())
if err != nil {
@@ -220,7 +360,7 @@ func (c *feishuChannel) Send(ctx context.Context, msg core.OutgoingMessage) erro
Body(larkim.NewCreateMessageReqBodyBuilder().
ReceiveId(msg.ChatID).
MsgType(msgType).
- Content(contentText).
+ Content(content).
Build()).
Build())
if err != nil {
@@ -229,6 +369,69 @@ func (c *feishuChannel) Send(ctx context.Context, msg core.OutgoingMessage) erro
return feishuResponseError("create", resp.Code, resp.Msg)
}
+// createStreamCard creates the card entity that carries the streamed answer.
+func (c *feishuChannel) createStreamCard(ctx context.Context) (string, error) {
+ resp, err := c.rest.Cardkit.V1.Card.Create(ctx, larkcardkit.NewCreateCardReqBuilder().
+ Body(larkcardkit.NewCreateCardReqBodyBuilder().
+ Type("card_json").
+ Data(feishuStreamCardJSON).
+ Build()).
+ Build())
+ if err != nil {
+ return "", err
+ }
+ if err := feishuResponseError("card create", resp.Code, resp.Msg); err != nil {
+ return "", err
+ }
+ if resp.Data == nil || stringValue(resp.Data.CardId) == "" {
+ return "", errors.New("feishu card create returned no card id")
+ }
+ return stringValue(resp.Data.CardId), nil
+}
+
+// feishuRetry retries a card API call with the Feishu streaming policy.
+func feishuRetry(ctx context.Context, call func(context.Context) error) error {
+ return retryChannelCall(ctx, feishuStreamAttempts, feishuStreamCallLimit, feishuStreamBackoff, call)
+}
+
+// closeStreamCard ends the card's streaming state and reports a failed close. The turn
+// context may already be cancelled when this runs, so the call uses a detached context
+// whose budget covers every retry attempt.
+func (c *feishuChannel) closeStreamCard(ctx context.Context, cardID string, sequence *int) error {
+ closeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), feishuCloseTimeout)
+ defer cancel()
+ err := c.finishStreamCard(closeCtx, cardID, sequence)
+ if err != nil {
+ common.Error("feishu: card settings failed", err, zap.String("account_id", c.account.AccountID), zap.String("card_id", cardID))
+ }
+ return err
+}
+
+// finishStreamCard turns streaming mode off so the card stops rendering as in progress.
+func (c *feishuChannel) finishStreamCard(ctx context.Context, cardID string, sequence *int) error {
+ next := *sequence + 1
+ uuid := common.GenerateToken()
+ err := feishuRetry(ctx, func(callCtx context.Context) error {
+ resp, err := c.rest.Cardkit.V1.Card.Settings(callCtx, larkcardkit.NewSettingsCardReqBuilder().
+ CardId(cardID).
+ Body(larkcardkit.NewSettingsCardReqBodyBuilder().
+ Settings(`{"config":{"streaming_mode":false}}`).
+ Sequence(next).
+ Uuid(uuid).
+ Build()).
+ Build())
+ if err != nil {
+ return err
+ }
+ return feishuResponseError("card settings", resp.Code, resp.Msg)
+ })
+ if err != nil {
+ return err
+ }
+ *sequence = next
+ return nil
+}
+
// run keeps the SDK WebSocket client active until the channel stops.
func (c *feishuChannel) run(ctx context.Context) {
if c.wsClient == nil {
@@ -343,7 +546,7 @@ func (c *feishuChannel) runWorker(ctx context.Context, chatID string, worker *fe
if ctx.Err() != nil {
return
}
- handlerCtx, cancel := context.WithTimeout(ctx, c.account.Timeout)
+ handlerCtx, cancel := context.WithTimeout(ctx, feishuTurnBudget)
c.handleIncoming(handlerCtx, msg)
cancel()
resetTimer(idle, feishuWorkerIdle)
diff --git a/internal/channels/feishu_test.go b/internal/channels/feishu_test.go
index d0c4eec335..4c8216e96e 100644
--- a/internal/channels/feishu_test.go
+++ b/internal/channels/feishu_test.go
@@ -17,9 +17,20 @@
package channels
import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync"
"testing"
"time"
+ lark "github.com/larksuite/oapi-sdk-go/v3"
+
"ragflow/internal/channels/core"
)
@@ -83,3 +94,502 @@ func TestFeishuEnqueueDoesNotMarkDroppedMessageSeen(t *testing.T) {
t.Fatal("dropped message was marked seen")
}
}
+
+func TestNewFeishuChannelFromConfigStreamFlag(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ cfg map[string]any
+ want bool
+ }{
+ {name: "enabled", cfg: map[string]any{"stream": true}, want: true},
+ {name: "absent", cfg: map[string]any{}, want: false},
+ {name: "non boolean value is ignored", cfg: map[string]any{"stream": "true"}, want: false},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ cfg := map[string]any{"app_id": "app-1", "app_secret": "secret-1"}
+ for key, value := range test.cfg {
+ cfg[key] = value
+ }
+ ch, err := newFeishuChannelFromConfig("account-1", cfg)
+ if err != nil {
+ t.Fatalf("newFeishuChannelFromConfig returned error: %v", err)
+ }
+ if ch.StreamEnabled() != test.want {
+ t.Fatalf("StreamEnabled() = %t, want %t", ch.StreamEnabled(), test.want)
+ }
+ })
+ }
+}
+
+type feishuAPICall struct {
+ method string
+ path string
+ body string
+}
+
+// feishuFakeAPI is a scriptable stand-in for the cardkit and IM endpoints the streaming
+// sender uses.
+type feishuFakeAPI struct {
+ cardCreateCode int
+ // contentScript scripts the content updates in order: true answers OK, false
+ // answers an error. Past the end every update succeeds.
+ contentScript []bool
+ settingsFails bool
+
+ mu sync.Mutex
+ calls []feishuAPICall
+ contentAt int
+}
+
+func (f *feishuFakeAPI) recorded() []feishuAPICall {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ return append([]feishuAPICall(nil), f.calls...)
+}
+
+// contentFailsNow consumes one entry of the content script and reports whether this
+// update has to fail.
+func (f *feishuFakeAPI) contentFailsNow() bool {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ index := f.contentAt
+ f.contentAt++
+ if index < len(f.contentScript) {
+ return !f.contentScript[index]
+ }
+ return false
+}
+
+// feishuContentFailures returns a content script that answers the first n updates with an
+// error.
+func feishuContentFailures(n int) []bool {
+ script := make([]bool, n)
+ return script
+}
+
+func newFeishuFakeAPI(t *testing.T, cardCreateCode int) (*httptest.Server, *feishuFakeAPI) {
+ t.Helper()
+ fake := &feishuFakeAPI{cardCreateCode: cardCreateCode}
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ body, _ := io.ReadAll(r.Body)
+ fake.mu.Lock()
+ fake.calls = append(fake.calls, feishuAPICall{method: r.Method, path: r.URL.Path, body: string(body)})
+ fake.mu.Unlock()
+ w.Header().Set("Content-Type", "application/json")
+ switch {
+ case strings.Contains(r.URL.Path, "tenant_access_token"):
+ _, _ = w.Write([]byte(`{"code":0,"msg":"ok","tenant_access_token":"t-1","expire":7200}`))
+ case r.URL.Path == "/open-apis/cardkit/v1/cards":
+ if fake.cardCreateCode != 0 {
+ _, _ = fmt.Fprintf(w, `{"code":%d,"msg":"no permission"}`, fake.cardCreateCode)
+ return
+ }
+ _, _ = w.Write([]byte(`{"code":0,"msg":"ok","data":{"card_id":"card-1"}}`))
+ case r.URL.Path == "/open-apis/im/v1/messages":
+ _, _ = w.Write([]byte(`{"code":0,"msg":"ok","data":{"message_id":"om-1"}}`))
+ case strings.HasSuffix(r.URL.Path, "/content"):
+ if fake.contentFailsNow() {
+ _, _ = w.Write([]byte(`{"code":230020,"msg":"rate limited"}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"code":0,"msg":"ok"}`))
+ case strings.HasSuffix(r.URL.Path, "/settings"):
+ if fake.settingsFails {
+ _, _ = w.Write([]byte(`{"code":230020,"msg":"rate limited"}`))
+ return
+ }
+ _, _ = w.Write([]byte(`{"code":0,"msg":"ok"}`))
+ default:
+ w.WriteHeader(http.StatusNotFound)
+ _, _ = w.Write([]byte(`{"code":404,"msg":"not found"}`))
+ }
+ }))
+ t.Cleanup(server.Close)
+ return server, fake
+}
+
+func newFeishuChannelAgainstFakeAPI(server *httptest.Server) *feishuChannel {
+ ch := newFeishuChannel(feishuAccount{
+ AccountID: "account-1",
+ AppID: "app-1",
+ AppSecret: "secret-1",
+ Stream: true,
+ })
+ ch.rest = lark.NewClient(
+ "app-1",
+ "secret-1",
+ lark.WithOpenBaseUrl(server.URL),
+ lark.WithHttpClient(server.Client()),
+ )
+ return ch
+}
+
+func TestFeishuSendStreamCreatesUpdatesAndFinalizesCard(t *testing.T) {
+ server, fake := newFeishuFakeAPI(t, 0)
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ text := make(chan string, 4)
+ text <- "Hel"
+ text <- "Hello"
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err != nil {
+ t.Fatalf("SendStream returned error: %v", err)
+ }
+
+ var cardCreates, messageCreates, cardSettings int
+ var sequences []int
+ var contents []string
+ lastContentAt, settingsAt := -1, -1
+ var interactiveCardID string
+ for callIndex, call := range fake.recorded() {
+ switch {
+ case call.path == "/open-apis/cardkit/v1/cards":
+ cardCreates++
+ var payload struct {
+ Type string `json:"type"`
+ Data string `json:"data"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil {
+ t.Fatalf("card create body is not json: %v", err)
+ }
+ if payload.Type != "card_json" {
+ t.Fatalf("card type = %q, want card_json", payload.Type)
+ }
+ var card struct {
+ Schema string `json:"schema"`
+ Config struct {
+ StreamingMode bool `json:"streaming_mode"`
+ } `json:"config"`
+ Body struct {
+ Elements []struct {
+ Tag string `json:"tag"`
+ ElementID string `json:"element_id"`
+ } `json:"elements"`
+ } `json:"body"`
+ }
+ if err := json.Unmarshal([]byte(payload.Data), &card); err != nil {
+ t.Fatalf("card data is not json: %v", err)
+ }
+ if card.Schema != "2.0" || !card.Config.StreamingMode {
+ t.Fatalf("card data = %s, want a streaming card 2.0 json", payload.Data)
+ }
+ if len(card.Body.Elements) != 1 || card.Body.Elements[0].Tag != "markdown" || card.Body.Elements[0].ElementID != feishuStreamElementID {
+ t.Fatalf("card elements = %+v, want one markdown element named %q", card.Body.Elements, feishuStreamElementID)
+ }
+ case call.path == "/open-apis/im/v1/messages":
+ messageCreates++
+ var payload struct {
+ MsgType string `json:"msg_type"`
+ Content string `json:"content"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil {
+ t.Fatalf("message body is not json: %v", err)
+ }
+ if payload.MsgType != "interactive" {
+ t.Fatalf("msg_type = %q, want interactive", payload.MsgType)
+ }
+ var content struct {
+ Type string `json:"type"`
+ Data struct {
+ CardID string `json:"card_id"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal([]byte(payload.Content), &content); err != nil {
+ t.Fatalf("message content is not json: %v", err)
+ }
+ interactiveCardID = content.Data.CardID
+ case strings.HasSuffix(call.path, "/content"):
+ var payload struct {
+ Content string `json:"content"`
+ Sequence int `json:"sequence"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil {
+ t.Fatalf("content body is not json: %v", err)
+ }
+ contents = append(contents, payload.Content)
+ lastContentAt = callIndex
+ sequences = append(sequences, payload.Sequence)
+ case strings.HasSuffix(call.path, "/settings"):
+ cardSettings++
+ if !strings.Contains(call.body, `streaming_mode`) || !strings.Contains(call.body, "false") {
+ t.Fatalf("settings body = %s, want streaming_mode false", call.body)
+ }
+ var payload struct {
+ Sequence int `json:"sequence"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil {
+ t.Fatalf("settings body is not json: %v", err)
+ }
+ settingsAt = callIndex
+ sequences = append(sequences, payload.Sequence)
+ }
+ }
+
+ if cardCreates != 1 {
+ t.Fatalf("card creates = %d, want 1", cardCreates)
+ }
+ if messageCreates != 1 {
+ t.Fatalf("message creates = %d, want 1", messageCreates)
+ }
+ if interactiveCardID != "card-1" {
+ t.Fatalf("card message referenced %q, want card-1", interactiveCardID)
+ }
+ if len(sequences) == 0 {
+ t.Fatal("no card update was sent")
+ }
+ for i := 1; i < len(sequences); i++ {
+ if sequences[i] <= sequences[i-1] {
+ t.Fatalf("sequences = %v, want strictly increasing", sequences)
+ }
+ }
+ if cardSettings != 1 {
+ t.Fatalf("card settings calls = %d, want 1", cardSettings)
+ }
+ shown := ""
+ for _, content := range contents {
+ if !strings.HasPrefix(content, shown) {
+ t.Fatalf("content %q does not extend the previous content %q", content, shown)
+ }
+ shown = content
+ }
+ if shown != "Hello" {
+ t.Fatalf("last streamed content = %q, want the complete answer", shown)
+ }
+ if settingsAt < lastContentAt {
+ t.Fatalf("card was finalized at %d before the last content update at %d", settingsAt, lastContentAt)
+ }
+}
+
+func TestNextStreamTextAdvancesByPrefix(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ shown, target string
+ step int
+ want string
+ }{
+ {name: "empty shown advances by one step", shown: "", target: "abcdefgh", step: 2, want: "ab"},
+ {name: "next slice advances by one step", shown: "ab", target: "abcdefgh", step: 2, want: "abcd"},
+ {name: "large backlog still advances by one step", shown: "abcd", target: "abcdefghijklmnopqrst", step: 2, want: "abcdef"},
+ {name: "drained target is returned whole", shown: "abcdefgh", target: "abcdefgh", step: 2, want: "abcdefgh"},
+ {name: "short backlog is returned whole", shown: "abcdefg", target: "abcdefgh", step: 2, want: "abcdefgh"},
+ {name: "non prefix target is returned unchanged", shown: "abcd", target: "ab", step: 2, want: "ab"},
+ {name: "step below one advances a single rune", shown: "", target: "abc", step: 0, want: "a"},
+ {name: "multi byte runes are never split", shown: "你好", target: "你好世界", step: 2, want: "你好世界"},
+ {name: "multi byte runes advance by one step", shown: "你好", target: "你好世界再见", step: 2, want: "你好世界"},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ if got := nextStreamText(test.shown, test.target, test.step); got != test.want {
+ t.Fatalf("nextStreamText(%q, %q, %d) = %q, want %q", test.shown, test.target, test.step, got, test.want)
+ }
+ if !strings.HasPrefix(test.target, test.shown) {
+ return
+ }
+ if got := nextStreamText(test.shown, test.target, test.step); !strings.HasPrefix(got, test.shown) || !strings.HasPrefix(test.target, got) {
+ t.Fatalf("nextStreamText(%q, %q, %d) = %q, want a prefix extension", test.shown, test.target, test.step, got)
+ }
+ })
+ }
+}
+
+func TestFeishuSendStreamFailsWhenCardCreationFails(t *testing.T) {
+ server, _ := newFeishuFakeAPI(t, 99991663)
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ text := make(chan string, 2)
+ text <- "Hello"
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err == nil {
+ t.Fatal("SendStream succeeded although the card could not be created")
+ }
+}
+
+func TestFeishuSendStreamSkipsEmptyText(t *testing.T) {
+ server, fake := newFeishuFakeAPI(t, 0)
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ text := make(chan string)
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); !errors.Is(err, errFeishuNothingStreamed) {
+ t.Fatalf("SendStream error = %v, want the nothing-streamed sentinel", err)
+ }
+ if len(fake.recorded()) != 0 {
+ t.Fatalf("SendStream called the API %d times for an empty answer", len(fake.recorded()))
+ }
+}
+
+// feishuTextMessages returns the plain text messages the fake API received.
+func feishuTextMessages(calls []feishuAPICall) []string {
+ var texts []string
+ for _, call := range calls {
+ if call.path != "/open-apis/im/v1/messages" {
+ continue
+ }
+ var payload struct {
+ MsgType string `json:"msg_type"`
+ Content string `json:"content"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil || payload.MsgType != "text" {
+ continue
+ }
+ var content struct {
+ Text string `json:"text"`
+ }
+ if err := json.Unmarshal([]byte(payload.Content), &content); err != nil {
+ continue
+ }
+ texts = append(texts, content.Text)
+ }
+ return texts
+}
+
+func TestFeishuSendStreamRetriesContentUpdate(t *testing.T) {
+ server, fake := newFeishuFakeAPI(t, 0)
+ fake.contentScript = []bool{false, false, true}
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ text := make(chan string, 2)
+ text <- "Hello"
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err != nil {
+ t.Fatalf("SendStream returned error: %v", err)
+ }
+
+ var sequences []int
+ for _, call := range fake.recorded() {
+ if !strings.HasSuffix(call.path, "/content") {
+ continue
+ }
+ var payload struct {
+ Content string `json:"content"`
+ Sequence int `json:"sequence"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil {
+ t.Fatalf("content body is not json: %v", err)
+ }
+ if payload.Content != "Hello" {
+ t.Fatalf("content = %q, want the complete answer", payload.Content)
+ }
+ sequences = append(sequences, payload.Sequence)
+ }
+ if len(sequences) < 3 {
+ t.Fatalf("content updates = %v, want at least the two failures and the success", sequences)
+ }
+ if sequences[0] != sequences[1] || sequences[1] != sequences[2] {
+ t.Fatalf("sequences = %v, want the retries to reuse one sequence", sequences)
+ }
+ if texts := feishuTextMessages(fake.recorded()); len(texts) != 0 {
+ t.Fatalf("plain text messages = %v, want none when the retry succeeds", texts)
+ }
+}
+
+func TestFeishuSendStreamReturnsErrorWhenUpdateKeepsFailing(t *testing.T) {
+ server, fake := newFeishuFakeAPI(t, 0)
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ answer := strings.Repeat("a", 20)
+ // Let the first update land, then break every later one.
+ fake.contentScript = append([]bool{true}, feishuContentFailures(30)...)
+
+ text := make(chan string, 2)
+ text <- answer
+ close(text)
+
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err == nil {
+ t.Fatal("SendStream succeeded although retries were exhausted")
+ }
+ if texts := feishuTextMessages(fake.recorded()); len(texts) != 0 {
+ t.Fatalf("plain text messages = %v, want the bridge to send the complete answer", texts)
+ }
+}
+
+func TestFeishuSendStreamReportsFailedClose(t *testing.T) {
+ server, fake := newFeishuFakeAPI(t, 0)
+ fake.settingsFails = true
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ text := make(chan string, 2)
+ text <- "Hello"
+ close(text)
+
+ // The close failed, so the caller has to know: it falls back to a one-shot send
+ // instead of leaving the card in the streaming state.
+ if err := ch.SendStream(context.Background(), core.OutgoingMessage{ChatID: "chat-1"}, text); err == nil {
+ t.Fatal("SendStream succeeded although the card was never closed")
+ }
+
+ var last string
+ for _, call := range fake.recorded() {
+ if !strings.HasSuffix(call.path, "/content") {
+ continue
+ }
+ var payload struct {
+ Content string `json:"content"`
+ }
+ if err := json.Unmarshal([]byte(call.body), &payload); err != nil {
+ t.Fatalf("content body is not json: %v", err)
+ }
+ last = payload.Content
+ }
+ if last != "Hello" {
+ t.Fatalf("last content = %q, want the complete answer", last)
+ }
+ if texts := feishuTextMessages(fake.recorded()); len(texts) != 0 {
+ t.Fatalf("plain text messages = %v, want none when only the settings call fails", texts)
+ }
+}
+
+func TestFeishuSendStreamClosesCardWhenCancelled(t *testing.T) {
+ server, fake := newFeishuFakeAPI(t, 0)
+ ch := newFeishuChannelAgainstFakeAPI(server)
+
+ text := make(chan string, 1)
+ text <- "Hello"
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ errCh := make(chan error, 1)
+ go func() {
+ errCh <- ch.SendStream(ctx, core.OutgoingMessage{ChatID: "chat-1"}, text)
+ }()
+
+ // Cancel only after the card received its first update, so the finish path runs on an
+ // already open card.
+ deadline := time.Now().Add(2 * time.Second)
+ for {
+ updated := false
+ for _, call := range fake.recorded() {
+ if strings.HasSuffix(call.path, "/content") {
+ updated = true
+ break
+ }
+ }
+ if updated {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("the card never received an update")
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ cancel()
+
+ select {
+ case err := <-errCh:
+ if !errors.Is(err, context.Canceled) {
+ t.Fatalf("SendStream error = %v, want context.Canceled", err)
+ }
+ case <-time.After(3 * time.Second):
+ t.Fatal("SendStream did not return after the context was cancelled")
+ }
+
+ for _, call := range fake.recorded() {
+ if strings.HasSuffix(call.path, "/settings") {
+ return
+ }
+ }
+ t.Fatalf("card was left in streaming mode, calls = %+v", fake.recorded())
+}
diff --git a/internal/channels/retry.go b/internal/channels/retry.go
new file mode 100644
index 0000000000..95d4f8f188
--- /dev/null
+++ b/internal/channels/retry.go
@@ -0,0 +1,50 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package channels
+
+import (
+ "context"
+ "time"
+)
+
+// retryChannelCall runs call up to attempts times, bounding every attempt with
+// perCallLimit so a stalled request cannot consume the whole reply, and backing off
+// between attempts. A cancelled outer context stops the retries immediately: at that
+// point the turn is over and repeating the call cannot help.
+func retryChannelCall(ctx context.Context, attempts int, perCallLimit, baseBackoff time.Duration, call func(context.Context) error) error {
+ var err error
+ for attempt := 1; attempt <= attempts; attempt++ {
+ attemptCtx, cancel := context.WithTimeout(ctx, perCallLimit)
+ err = call(attemptCtx)
+ cancel()
+ if err == nil {
+ return nil
+ }
+ if ctx.Err() != nil {
+ return ctx.Err()
+ }
+ if attempt == attempts {
+ break
+ }
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-time.After(time.Duration(attempt) * baseBackoff):
+ }
+ }
+ return err
+}
diff --git a/internal/channels/wecom.go b/internal/channels/wecom.go
index 88c1e95c09..7b79b34c0b 100644
--- a/internal/channels/wecom.go
+++ b/internal/channels/wecom.go
@@ -50,12 +50,17 @@ const (
weComReconnectDelay = 3 * time.Second
weComHeartbeatInterval = 25 * time.Second
weComTokenSafetyMargin = 60 * time.Second
+ weComStreamTick = 100 * time.Millisecond
+ weComStreamStep = 5
)
var (
errWeComAuthentication = errors.New("WeCom websocket authentication failed")
errWeComDisconnected = errors.New("WeCom websocket disconnected by server event")
- weComRequestSequence atomic.Uint64
+ // errWeComNothingStreamed reports that no text was available for a stream reply, so the
+ // caller still has to deliver the answer.
+ errWeComNothingStreamed = errors.New("wecom: no streamed text to display")
+ weComRequestSequence atomic.Uint64
)
type wecomAccount struct {
@@ -71,6 +76,7 @@ type wecomAccount struct {
WebhookPort int
APIBaseURL string
WSURL string
+ Stream bool
}
type wecomChannel struct {
@@ -156,6 +162,7 @@ func newWeComChannelFromConfig(accountID string, cfg map[string]any) (*wecomChan
Secret: firstString(cfg, "secret"),
APIBaseURL: firstString(cfg, "api_base_url"),
WSURL: firstString(cfg, "ws_url"),
+ Stream: cfg["stream"] == true,
}
if account.Secret == "" {
return nil, fmt.Errorf("wecom account %q missing required field: secret", accountID)
@@ -507,6 +514,105 @@ func (c *wecomChannel) handleWebSocketMessage(ctx context.Context, raw map[strin
c.handleTextMessage(ctx, chatID, senderID, messageID, text, chatType, raw)
}
+// StreamEnabled reports whether this account asked for streaming replies. Streaming needs
+// the smart-bot long connection, which only the WebSocket mode speaks.
+func (c *wecomChannel) StreamEnabled() bool {
+ return c.account.Stream && c.account.ConnectionType == "websocket"
+}
+
+// SendStream replies with a WeCom stream message: the first push creates it, later pushes
+// refresh it under the same stream id, and finish=true ends it. An error means no text
+// reached the user, so the caller still sends the complete answer through Send.
+func (c *wecomChannel) SendStream(ctx context.Context, msg core.OutgoingMessage, text <-chan string) error {
+ if strings.TrimSpace(msg.ChatID) == "" {
+ return errors.New("chat_id is required")
+ }
+ if strings.TrimSpace(msg.ReplyToMessageID) == "" {
+ return errors.New("WeCom stream reply requires the callback request id")
+ }
+
+ var first string
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case value, open := <-text:
+ if !open || strings.TrimSpace(value) == "" {
+ return errWeComNothingStreamed
+ }
+ first = value
+ }
+
+ streamID := "STREAM_" + common.GenerateToken()
+ push := func(value string, finish bool) error {
+ c.mu.Lock()
+ conn := c.ws
+ c.mu.Unlock()
+ if conn == nil {
+ return errors.New("WeCom websocket is not connected")
+ }
+ return c.writeWebSocketJSON(conn, map[string]any{
+ "cmd": "aibot_respond_msg",
+ "headers": map[string]any{"req_id": msg.ReplyToMessageID},
+ "body": map[string]any{
+ "msgtype": "stream",
+ "stream": map[string]any{
+ "id": streamID,
+ "finish": finish,
+ "content": value,
+ },
+ },
+ })
+ }
+
+ latest, sent := first, ""
+ // finishStream closes the stream message best-effort. Without finish=true WeCom keeps
+ // rendering the message as in progress until the ten minute window ends, so every exit
+ // after the first push tries once, whatever the reason.
+ finishStream := func() {
+ if sent == "" {
+ return
+ }
+ if err := push(sent, true); err != nil {
+ common.Error("wecom: stream finish failed", err, zap.String("account_id", c.account.AccountID))
+ }
+ }
+
+ ticker := time.NewTicker(weComStreamTick)
+ defer ticker.Stop()
+ for {
+ select {
+ case <-ctx.Done():
+ finishStream()
+ return ctx.Err()
+ case value, open := <-text:
+ if !open {
+ // A closed channel returns immediately forever, so stop selecting on it and
+ // let the ticker drain what is still unsent.
+ text = nil
+ continue
+ }
+ latest = value
+ case <-ticker.C:
+ if sent != latest {
+ next := nextStreamText(sent, latest, weComStreamStep)
+ if err := push(next, false); err != nil {
+ finishStream()
+ return err
+ }
+ sent = next
+ continue
+ }
+ if text == nil {
+ err := push(sent, true)
+ if err != nil {
+ finishStream()
+ }
+ return err
+ }
+ }
+ }
+}
+
func (c *wecomChannel) sendWebSocketMessage(msg core.OutgoingMessage) error {
if msg.Text == "" {
return errors.New("WeCom websocket reply text is required")
diff --git a/internal/channels/wecom_test.go b/internal/channels/wecom_test.go
index f0071f419d..f3ebef1fa9 100644
--- a/internal/channels/wecom_test.go
+++ b/internal/channels/wecom_test.go
@@ -27,6 +27,7 @@ import (
"encoding/hex"
"encoding/json"
"encoding/xml"
+ "errors"
"net"
"net/http"
"net/http/httptest"
@@ -602,3 +603,250 @@ func testWeComSignature(token, timestamp, nonce, encrypted string) string {
hash := sha1.Sum([]byte(strings.Join(parts, "")))
return hex.EncodeToString(hash[:])
}
+
+func TestWeComStreamEnabledOnlyForWebSocket(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ connectionType string
+ stream any
+ want bool
+ }{
+ {name: "websocket with the switch on", connectionType: "websocket", stream: true, want: true},
+ {name: "websocket without the switch", connectionType: "websocket", stream: nil, want: false},
+ {name: "webhook ignores the switch", connectionType: "webhook", stream: true, want: false},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ cfg := map[string]any{
+ "connection_type": test.connectionType,
+ "secret": "secret",
+ "bot_id": "bot-id",
+ "corp_id": "corp-id",
+ "token": "token",
+ "aes_key": strings.Repeat("a", 43),
+ "agent_id": 1000001,
+ }
+ if test.stream != nil {
+ cfg["stream"] = test.stream
+ }
+ ch, err := newWeComChannelFromConfig("account-1", cfg)
+ if err != nil {
+ t.Fatalf("newWeComChannelFromConfig() error = %v", err)
+ }
+ if got := ch.StreamEnabled(); got != test.want {
+ t.Fatalf("StreamEnabled() = %t, want %t", got, test.want)
+ }
+ })
+ }
+}
+
+func TestWeComSendStreamPushesAndFinishes(t *testing.T) {
+ upgrader := websocket.Upgrader{CheckOrigin: func(*http.Request) bool { return true }}
+ frames := make(chan map[string]any, 8)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ t.Errorf("upgrade websocket: %v", err)
+ return
+ }
+ defer conn.Close()
+
+ var subscribe map[string]any
+ if err := conn.ReadJSON(&subscribe); err != nil {
+ t.Errorf("read subscribe: %v", err)
+ return
+ }
+ if err := conn.WriteJSON(map[string]any{"cmd": "aibot_subscribe", "errcode": 0, "errmsg": "ok"}); err != nil {
+ t.Errorf("write subscribe response: %v", err)
+ return
+ }
+ if err := conn.WriteJSON(map[string]any{
+ "cmd": "aibot_msg_callback",
+ "headers": map[string]any{"req_id": "request-1"},
+ "body": map[string]any{
+ "msgid": "message-1",
+ "msgtype": "text",
+ "chattype": "group",
+ "chatid": "chat-1",
+ "from": map[string]any{"userid": "user-1"},
+ "text": map[string]any{"content": "question"},
+ },
+ }); err != nil {
+ t.Errorf("write callback: %v", err)
+ return
+ }
+ for {
+ var frame map[string]any
+ if err := conn.ReadJSON(&frame); err != nil {
+ return
+ }
+ frames <- frame
+ stream := weComMapValue(weComMapValue(frame["body"])["stream"])
+ if finished, _ := stream["finish"].(bool); finished {
+ return
+ }
+ }
+ }))
+ defer server.Close()
+
+ wsURL := "ws" + strings.TrimPrefix(server.URL, "http")
+ allowChannelURLForTest(t, wsURL)
+ channel, err := newWeComChannel(wecomAccount{
+ AccountID: "account-1",
+ ConnectionType: "websocket",
+ BotID: "bot-id",
+ Secret: "secret",
+ WSURL: wsURL,
+ Stream: true,
+ })
+ if err != nil {
+ t.Fatalf("newWeComChannel() error = %v", err)
+ }
+ handlerErr := make(chan error, 1)
+ channel.SetMessageHandler(func(ctx context.Context, msg core.IncomingMessage) error {
+ text := make(chan string, 2)
+ text <- "Hel"
+ text <- "Hello"
+ close(text)
+ err := channel.SendStream(ctx, core.OutgoingMessage{ChatID: msg.ChatID, ReplyToMessageID: msg.MessageID}, text)
+ handlerErr <- err
+ return err
+ })
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ if err := channel.Start(ctx); err != nil {
+ t.Fatalf("Start() error = %v", err)
+ }
+
+ var got []map[string]any
+ for len(got) < 2 {
+ select {
+ case frame := <-frames:
+ got = append(got, frame)
+ case <-time.After(3 * time.Second):
+ t.Fatalf("timed out waiting for stream frames, got %d", len(got))
+ }
+ }
+ if err := <-handlerErr; err != nil {
+ t.Fatalf("SendStream() error = %v", err)
+ }
+
+ var streamID string
+ for index, frame := range got {
+ if frame["cmd"] != "aibot_respond_msg" {
+ t.Fatalf("frame %d cmd = %v, want aibot_respond_msg", index, frame["cmd"])
+ }
+ if reqID := weComMapValue(frame["headers"])["req_id"]; reqID != "request-1" {
+ t.Fatalf("frame %d req_id = %v, want the callback request id", index, reqID)
+ }
+ body := weComMapValue(frame["body"])
+ if body["msgtype"] != "stream" {
+ t.Fatalf("frame %d msgtype = %v, want stream", index, body["msgtype"])
+ }
+ stream := weComMapValue(body["stream"])
+ id, _ := stream["id"].(string)
+ if id == "" {
+ t.Fatalf("frame %d is missing the stream id", index)
+ }
+ if streamID == "" {
+ streamID = id
+ } else if id != streamID {
+ t.Fatalf("frame %d stream id = %q, want the same id as the first push", index, id)
+ }
+ if stream["content"] != "Hello" {
+ t.Fatalf("frame %d content = %v, want the complete answer", index, stream["content"])
+ }
+ if finish, _ := stream["finish"].(bool); finish != (index == len(got)-1) {
+ t.Fatalf("frame %d finish = %v, want the last frame to finish the stream", index, finish)
+ }
+ }
+ if err := channel.Stop(context.Background()); err != nil {
+ t.Fatalf("Stop() error = %v", err)
+ }
+}
+
+func TestWeComSendStreamFinishesWhenCancelled(t *testing.T) {
+ upgrader := websocket.Upgrader{CheckOrigin: func(*http.Request) bool { return true }}
+ frames := make(chan map[string]any, 8)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ t.Errorf("upgrade websocket: %v", err)
+ return
+ }
+ defer conn.Close()
+ for {
+ var frame map[string]any
+ if err := conn.ReadJSON(&frame); err != nil {
+ return
+ }
+ frames <- frame
+ }
+ }))
+ defer server.Close()
+
+ wsURL := "ws" + strings.TrimPrefix(server.URL, "http")
+ conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
+ if err != nil {
+ t.Fatalf("dial websocket: %v", err)
+ }
+ defer conn.Close()
+
+ channel, err := newWeComChannel(wecomAccount{
+ AccountID: "account-1",
+ ConnectionType: "websocket",
+ BotID: "bot-id",
+ Secret: "secret",
+ Stream: true,
+ })
+ if err != nil {
+ t.Fatalf("newWeComChannel() error = %v", err)
+ }
+ channel.ws = conn
+
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ text := make(chan string, 1)
+ text <- "Hello"
+ errCh := make(chan error, 1)
+ go func() {
+ errCh <- channel.SendStream(ctx, core.OutgoingMessage{ChatID: "chat-1", ReplyToMessageID: "request-1"}, text)
+ }()
+
+ readFrame := func() map[string]any {
+ select {
+ case frame := <-frames:
+ return frame
+ case err := <-errCh:
+ t.Fatalf("SendStream returned before the expected frame: %v", err)
+ return nil
+ case <-time.After(3 * time.Second):
+ t.Fatal("timed out waiting for a stream frame")
+ return nil
+ }
+ }
+
+ first := readFrame()
+ if stream := weComMapValue(weComMapValue(first["body"])["stream"]); stream["finish"] != false {
+ t.Fatalf("first frame finish = %v, want an unfinished update", stream["finish"])
+ }
+
+ cancel()
+
+ select {
+ case sendErr := <-errCh:
+ if !errors.Is(sendErr, context.Canceled) {
+ t.Fatalf("SendStream error = %v, want context.Canceled", sendErr)
+ }
+ case <-time.After(3 * time.Second):
+ t.Fatal("SendStream did not return after the context was cancelled")
+ }
+
+ last := readFrame()
+ stream := weComMapValue(weComMapValue(last["body"])["stream"])
+ if stream["finish"] != true {
+ t.Fatalf("last frame finish = %v, want the stream to be closed", stream["finish"])
+ }
+ if stream["content"] != "Hello" {
+ t.Fatalf("finish content = %v, want the last shown text", stream["content"])
+ }
+}
diff --git a/internal/service/chat_channel.go b/internal/service/chat_channel.go
index a1d7cbfbb6..1952b3f1cf 100644
--- a/internal/service/chat_channel.go
+++ b/internal/service/chat_channel.go
@@ -16,6 +16,7 @@ package service
import (
"context"
+ "encoding/json"
"errors"
"fmt"
"strings"
@@ -285,7 +286,9 @@ func (s *ChatChannelService) GetChatChannelRuntime(ctx context.Context, channelI
}
// HandleIncomingMessage routes one external channel message through the bound RAGFlow dialog.
-func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg ChatChannelIncomingMessage) (string, error) {
+// When onDelta is non-nil the answer is generated as a stream and onDelta receives the
+// cumulative display text as it grows; the returned string is always the complete answer.
+func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg ChatChannelIncomingMessage, onDelta func(string)) (string, error) {
if strings.TrimSpace(msg.Text) == "" {
return "", nil
}
@@ -307,7 +310,7 @@ func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg Chat
if channel.AgentID != nil {
ctx, cancel := context.WithTimeout(ownerCtx, 5*time.Minute)
defer cancel()
- return s.handleChannelAgentMessage(ctx, channel, msg)
+ return s.handleChannelAgentMessage(ctx, channel, msg, onDelta)
}
if channel.ChatID == nil || strings.TrimSpace(*channel.ChatID) == "" {
return "", nil
@@ -326,6 +329,17 @@ func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg Chat
return "", err
}
+ stream := onDelta != nil
+ var streamChan chan string
+ var streamed chan channelStreamResult
+ if stream {
+ streamChan = make(chan string, 64)
+ streamed = make(chan channelStreamResult, 1)
+ go func() {
+ streamed <- consumeChannelStream(streamChan, onDelta)
+ }()
+ }
+
// Channel chat sessions are owned by the dialog tenant; authorization above
// separately enforces the channel owner's current access to that dialog.
completionCtx := permission.WithIdentity(ctx, dialog.TenantID, dialog.TenantID)
@@ -340,9 +354,21 @@ func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg Chat
nil,
map[string]interface{}{"quote": false},
false,
- false,
- nil,
+ stream,
+ streamChan,
)
+ if stream {
+ close(streamChan)
+ consumed := <-streamed
+ if err != nil {
+ common.Error("chat channel completion failed", err, zap.String("channel_id", channel.ID))
+ return "Sorry, I couldn't process your message right now.", nil
+ }
+ if consumed.err != nil {
+ common.Warn("chat channel stream reported an error", zap.Error(consumed.err), zap.String("channel_id", channel.ID))
+ }
+ return consumed.answer, nil
+ }
if err != nil {
common.Error("chat channel completion failed", err, zap.String("channel_id", channel.ID))
return "Sorry, I couldn't process your message right now.", nil
@@ -354,6 +380,91 @@ func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg Chat
return answer, nil
}
+// channelStreamResult is what consumeChannelStream reports back to its caller.
+type channelStreamResult struct {
+ answer string
+ err error
+}
+
+// consumeChannelStream drains the SSE frames ChatCompletions writes on frames and reports
+// the cumulative display text through onDelta. Reasoning wrapped in <think>...</think> and
+// the markers themselves are dropped, so the streamed text matches what the non-stream
+// answer contains.
+func consumeChannelStream(frames <-chan string, onDelta func(string)) channelStreamResult {
+ var answer strings.Builder
+ inThink := false
+ for frame := range frames {
+ payload, ok := strings.CutPrefix(frame, "data:")
+ if !ok {
+ continue
+ }
+ var wrapper struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Data json.RawMessage `json:"data"`
+ }
+ if err := json.Unmarshal([]byte(strings.TrimSpace(payload)), &wrapper); err != nil {
+ continue
+ }
+ if wrapper.Code != 0 {
+ return channelStreamResult{answer: answer.String(), err: errors.New(wrapper.Message)}
+ }
+ var chunk map[string]interface{}
+ if err := json.Unmarshal(wrapper.Data, &chunk); err != nil {
+ // `data: true` closes the stream.
+ continue
+ }
+ text, _ := chunk["answer"].(string)
+ if final, _ := chunk["final"].(bool); final {
+ // The final frame carries the authoritative answer, so it replaces whatever the
+ // deltas produced even when that rewrites the text; the channel repaints.
+ // Pipelines that persist reasoning keep it inside <think> tags in that answer,
+ // so it goes through the same filter as the deltas instead of reaching the user.
+ finalThink := false
+ text = strings.TrimLeft(stripChannelThink(text, &finalThink), "\r\n")
+ if text != "" {
+ answer.Reset()
+ answer.WriteString(text)
+ onDelta(answer.String())
+ }
+ continue
+ }
+ text = stripChannelThink(text, &inThink)
+ if text == "" {
+ continue
+ }
+ answer.WriteString(text)
+ onDelta(answer.String())
+ }
+ return channelStreamResult{answer: answer.String()}
+}
+
+// stripChannelThink removes the <think>...</think> reasoning block from a delta. The
+// closing marker can share a delta with the answer text that follows it.
+func stripChannelThink(text string, inThink *bool) string {
+ for {
+ if *inThink {
+ idx := strings.Index(text, "</think>")
+ if idx < 0 {
+ return ""
+ }
+ text = text[idx+len("</think>"):]
+ *inThink = false
+ continue
+ }
+ idx := strings.Index(text, "<think>")
+ if idx < 0 {
+ break
+ }
+ text = text[idx+len("<think>"):]
+ *inThink = true
+ }
+ if *inThink {
+ return ""
+ }
+ return text
+}
+
func (s *ChatChannelService) DeleteChatChannel(ctx context.Context, channelID string) (bool, common.ErrorCode, error) {
if err := permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, permission.ResourceRef{Kind: permission.ResourceKindChatChannel, ID: channelID}, permission.OperationDelete); err != nil {
code, permissionErr := permissionresponse.NormalizeHidden(err)
diff --git a/internal/service/chat_channel_agent.go b/internal/service/chat_channel_agent.go
index 8b4059b210..94e9308da0 100644
--- a/internal/service/chat_channel_agent.go
+++ b/internal/service/chat_channel_agent.go
@@ -54,7 +54,7 @@ func validateChannelBinding(ctx context.Context, chatID, agentID *string) error
return permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, ref, operation)
}
-func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, channel *entity.ChatChannel, msg ChatChannelIncomingMessage) (string, error) {
+func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, channel *entity.ChatChannel, msg ChatChannelIncomingMessage, onDelta func(string)) (string, error) {
sessionID := channelSessionID(*channel.AgentID, channel.ID, msg.ChatID)
// RunAgent creates and authorizes the persisted session; this boundary only
// supplies its stable identity for the first message and subsequent turns.
@@ -67,6 +67,13 @@ func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, chan
var answer strings.Builder
var runErr error
thinking := false
+ // Report every visible change so a streaming channel can render the answer as it
+ // grows; thinking blocks are already filtered out above.
+ emit := func() {
+ if onDelta != nil && answer.Len() > 0 {
+ onDelta(answer.String())
+ }
+ }
for {
select {
case <-ctx.Done():
@@ -93,6 +100,7 @@ func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, chan
}
if !thinking {
answer.WriteString(data.Content)
+ emit()
}
case "waiting_for_user":
var data canvas.WaitingForUserEvent
@@ -102,6 +110,7 @@ func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, chan
if len(data.Inputs) > 1 {
answer.Reset()
answer.WriteString("This workflow requires structured input. Please continue in RAGFlow.")
+ emit()
continue
}
if answer.Len() > 0 && data.Tips != "" {
@@ -111,6 +120,7 @@ func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, chan
if answer.Len() == 0 {
answer.WriteString("Please reply to continue.")
}
+ emit()
case "error":
// Provider/internal errors may contain credentials or implementation
// details; adapters receive an error, never that raw event payload.
diff --git a/internal/service/chat_channel_agent_test.go b/internal/service/chat_channel_agent_test.go
index 055ddf1887..73c4c4ebdb 100644
--- a/internal/service/chat_channel_agent_test.go
+++ b/internal/service/chat_channel_agent_test.go
@@ -69,7 +69,7 @@ func TestChatChannelRejectsCrossTenantBindingAndExecution(t *testing.T) {
createServiceTestChannel(t, db, &entity.ChatChannel{
ID: "historical", TenantID: "other", Channel: "telegram", Config: entity.JSONMap{}, ChatID: new("private"), Status: 1,
})
- if _, err := svc.HandleIncomingMessage(t.Context(), ChatChannelIncomingMessage{AccountID: "historical", ChatID: "external", Text: "hello"}); !errors.Is(err, permission.ErrPermissionDenied) {
+ if _, err := svc.HandleIncomingMessage(t.Context(), ChatChannelIncomingMessage{AccountID: "historical", ChatID: "external", Text: "hello"}, nil); !errors.Is(err, permission.ErrPermissionDenied) {
t.Fatalf("historical unauthorized binding: %v", err)
}
}
@@ -93,7 +93,7 @@ func TestChatChannelAgentRevokedAccess(t *testing.T) {
t.Fatal("revoked workflow must not execute")
return nil, nil
})
- if _, err := svc.HandleIncomingMessage(t.Context(), ChatChannelIncomingMessage{AccountID: channel.ID, ChatID: "external", Text: "hello"}); !errors.Is(err, permission.ErrPermissionDenied) {
+ if _, err := svc.HandleIncomingMessage(t.Context(), ChatChannelIncomingMessage{AccountID: channel.ID, ChatID: "external", Text: "hello"}, nil); !errors.Is(err, permission.ErrPermissionDenied) {
t.Fatalf("revoked workflow binding: %v", err)
}
}
@@ -131,7 +131,7 @@ func TestChatChannelAgentRealEngineContinuationAndResume(t *testing.T) {
t.Fatal(err)
}
msg := ChatChannelIncomingMessage{AccountID: channel.ID, ChatID: "external-one", Text: "start"}
- answer, err := svc.HandleIncomingMessage(t.Context(), msg)
+ answer, err := svc.HandleIncomingMessage(t.Context(), msg, nil)
if err != nil || !strings.Contains(answer, "Your answer?") {
t.Fatalf("waiting: %q %v", answer, err)
}
@@ -143,12 +143,12 @@ func TestChatChannelAgentRealEngineContinuationAndResume(t *testing.T) {
t.Fatalf("session name = %v", session.Name)
}
msg.Text = "hello"
- answer, err = svc.HandleIncomingMessage(t.Context(), msg)
+ answer, err = svc.HandleIncomingMessage(t.Context(), msg, nil)
if err != nil || answer != "got: hello" {
t.Fatalf("resume: %q %v", answer, err)
}
msg.ChatID = "external-two"
- answer, err = svc.HandleIncomingMessage(t.Context(), msg)
+ answer, err = svc.HandleIncomingMessage(t.Context(), msg, nil)
if err != nil || !strings.Contains(answer, "Your answer?") {
t.Fatalf("isolated conversation: %q %v", answer, err)
}
@@ -160,7 +160,7 @@ func TestChatChannelAgentRealEngineContinuationAndResume(t *testing.T) {
if err := db.Model(&entity.ChatChannel{}).Where("id = ?", channel.ID).Update("status", 0).Error; err != nil {
t.Fatalf("disable: %v", err)
}
- if answer, err := svc.HandleIncomingMessage(t.Context(), msg); answer != "" || err != nil {
+ if answer, err := svc.HandleIncomingMessage(t.Context(), msg, nil); answer != "" || err != nil {
t.Fatalf("disabled channel: %q %v", answer, err)
}
}
@@ -192,7 +192,7 @@ func TestChatChannelAgentErrorAndThinking(t *testing.T) {
close(events)
return events, nil
})}
- answer, err := svc.handleChannelAgentMessage(t.Context(), &entity.ChatChannel{ID: "channel", AgentID: new("agent")}, ChatChannelIncomingMessage{ChatID: "chat", Text: "hello"})
+ answer, err := svc.handleChannelAgentMessage(t.Context(), &entity.ChatChannel{ID: "channel", AgentID: new("agent")}, ChatChannelIncomingMessage{ChatID: "chat", Text: "hello"}, nil)
if answer != tc.want || (err != nil) != tc.failed {
t.Fatalf("answer=%q err=%v", answer, err)
}
diff --git a/internal/service/chat_channel_test.go b/internal/service/chat_channel_test.go
index ed6347a8f7..9dacf2eb89 100644
--- a/internal/service/chat_channel_test.go
+++ b/internal/service/chat_channel_test.go
@@ -521,3 +521,122 @@ func TestGetOrCreateForChannelFallbackNames(t *testing.T) {
}
})
}
+
+func TestConsumeChannelStreamAccumulatesDeltas(t *testing.T) {
+ frames := make(chan string, 8)
+ frames <- `data:{"code": 0, "message": "success", "data": {"answer": "Hel"}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "lo", "start_to_think": false}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "Hello", "final": true}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": true}` + "\n\n"
+ close(frames)
+
+ var seen []string
+ got := consumeChannelStream(frames, func(text string) { seen = append(seen, text) })
+
+ if got.err != nil {
+ t.Fatalf("consumeChannelStream error = %v", got.err)
+ }
+ if got.answer != "Hello" {
+ t.Fatalf("answer = %q, want Hello", got.answer)
+ }
+ if len(seen) == 0 || seen[len(seen)-1] != "Hello" {
+ t.Fatalf("last delta = %v, want the complete answer", seen)
+ }
+}
+
+func TestConsumeChannelStreamReportsErrorFrames(t *testing.T) {
+ frames := make(chan string, 2)
+ frames <- "data:" + `{"code": 500, "message": "model configuration unavailable", "data": {"answer": "**ERROR**: model configuration unavailable"}}` + "\n\n"
+ close(frames)
+
+ got := consumeChannelStream(frames, func(string) {})
+ if got.err == nil || !strings.Contains(got.err.Error(), "model configuration unavailable") {
+ t.Fatalf("consumeChannelStream error = %v, want the frame message", got.err)
+ }
+}
+
+func TestConsumeChannelStreamDropsThinking(t *testing.T) {
+ frames := make(chan string, 6)
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "<think>", "start_to_think": true}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "secret reasoning"}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "</think>Hello", "end_to_think": true}}` + "\n\n"
+ close(frames)
+
+ var seen []string
+ got := consumeChannelStream(frames, func(text string) { seen = append(seen, text) })
+
+ if got.err != nil {
+ t.Fatalf("consumeChannelStream error = %v", got.err)
+ }
+ if got.answer != "Hello" {
+ t.Fatalf("answer = %q, want Hello", got.answer)
+ }
+ if len(seen) != 1 || seen[0] != "Hello" {
+ t.Fatalf("deltas = %v, want only the visible answer", seen)
+ }
+}
+
+func TestStripChannelThink(t *testing.T) {
+ inThink := false
+ if got := stripChannelThink("<think>", &inThink); got != "" || !inThink {
+ t.Fatalf("opening marker = %q inThink=%t, want an empty drop", got, inThink)
+ }
+ if got := stripChannelThink("still thinking", &inThink); got != "" || !inThink {
+ t.Fatalf("reasoning text = %q, want it dropped", got)
+ }
+ if got := stripChannelThink("</think>Answer", &inThink); got != "Answer" || inThink {
+ t.Fatalf("closing marker = %q inThink=%t, want the trailing answer", got, inThink)
+ }
+ if got := stripChannelThink("plain", &inThink); got != "plain" {
+ t.Fatalf("plain text = %q, want plain", got)
+ }
+}
+
+func TestConsumeChannelStreamStripsThinkFromFinal(t *testing.T) {
+ frames := make(chan string, 6)
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "<think>", "start_to_think": true}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "secret reasoning"}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "</think>Hello", "end_to_think": true}}` + "\n\n"
+ // The agentic pipeline ships its authoritative answer with the thinking segment
+ // wrapped in <think> tags, so the final frame must not reach the channel as is.
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "<think>secret reasoning</think>\n\nHello there", "final": true}}` + "\n\n"
+ close(frames)
+
+ var seen []string
+ got := consumeChannelStream(frames, func(text string) { seen = append(seen, text) })
+
+ if got.err != nil {
+ t.Fatalf("consumeChannelStream error = %v", got.err)
+ }
+ if got.answer != "Hello there" {
+ t.Fatalf("answer = %q, want the visible final answer", got.answer)
+ }
+ if len(seen) == 0 || seen[len(seen)-1] != "Hello there" {
+ t.Fatalf("deltas = %v, want the visible final answer last", seen)
+ }
+ for _, text := range seen {
+ if strings.Contains(text, "think") || strings.Contains(text, "secret reasoning") {
+ t.Fatalf("deltas = %v, want the reasoning dropped", seen)
+ }
+ }
+}
+
+func TestConsumeChannelStreamAdoptsRewrittenFinal(t *testing.T) {
+ frames := make(chan string, 4)
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "Hello"}}` + "\n\n"
+ frames <- "data:" + `{"code": 0, "message": "success", "data": {"answer": "Different answer", "final": true}}` + "\n\n"
+ close(frames)
+
+ var seen []string
+ got := consumeChannelStream(frames, func(text string) { seen = append(seen, text) })
+
+ if got.err != nil {
+ t.Fatalf("consumeChannelStream error = %v", got.err)
+ }
+ if got.answer != "Different answer" {
+ t.Fatalf("answer = %q, want the authoritative final text", got.answer)
+ }
+ if len(seen) != 2 || seen[0] != "Hello" || seen[1] != "Different answer" {
+ t.Fatalf("deltas = %v, want the delta and then the final text", seen)
+ }
+}
diff --git a/web/src/pages/user-setting/chat-channel/constant/index.tsx b/web/src/pages/user-setting/chat-channel/constant/index.tsx
index 87db784d92..5eafdb0cc6 100644
--- a/web/src/pages/user-setting/chat-channel/constant/index.tsx
+++ b/web/src/pages/user-setting/chat-channel/constant/index.tsx
@@ -246,6 +246,39 @@ export const ChatChannelFormFields: Record<ChatChannelKey, FormFieldConfig[]> =
type: FormFieldType.Password,
required: true,
},
+ {
+ label: 'Streaming',
+ name: 'config.credential.stream',
+ type: FormFieldType.Switch,
+ required: false,
+ defaultValue: false,
+ },
+ {
+ label: 'AI Card Template ID',
+ name: 'config.credential.card_template_id',
+ type: FormFieldType.Text,
+ required: true,
+ defaultValue: '',
+ shouldRender: (values: any) =>
+ values?.config?.credential?.stream === true,
+ customValidate: (val: string, values: any) =>
+ values?.config?.credential?.stream === true && !(val ?? '').trim()
+ ? 'AI card template ID is required when streaming is on'
+ : true,
+ },
+ {
+ label: 'Stream Content Key',
+ name: 'config.credential.stream_content_key',
+ type: FormFieldType.Text,
+ required: true,
+ defaultValue: 'content',
+ shouldRender: (values: any) =>
+ values?.config?.credential?.stream === true,
+ customValidate: (val: string, values: any) =>
+ values?.config?.credential?.stream === true && !(val ?? '').trim()
+ ? 'Stream content key is required when streaming is on'
+ : true,
+ },
],
[ChatChannelKey.FEISHU]: [
{
@@ -272,6 +305,13 @@ export const ChatChannelFormFields: Record<ChatChannelKey, FormFieldConfig[]> =
{ label: 'Lark (international)', value: 'lark' },
],
},
+ {
+ label: 'Streaming',
+ name: 'config.credential.stream',
+ type: FormFieldType.Switch,
+ required: false,
+ defaultValue: false,
+ },
],
[ChatChannelKey.GOOGLECHAT]: [
{
@@ -689,6 +729,15 @@ export const ChatChannelFormFields: Record<ChatChannelKey, FormFieldConfig[]> =
shouldRender: (values: any) =>
values?.config?.credential?.connection_type !== 'websocket',
},
+ {
+ label: 'Streaming',
+ name: 'config.credential.stream',
+ type: FormFieldType.Switch,
+ required: false,
+ defaultValue: false,
+ shouldRender: (values: any) =>
+ values?.config?.credential?.connection_type === 'websocket',
+ },
],
[ChatChannelKey.WHATSAPP]: [
{
@@ -775,8 +824,17 @@ ChatChannelFormDefaultValues[
ChatChannelFormDefaultValues[
ChatChannelKey.WECOM
].config.credential.connection_type = 'webhook';
+ChatChannelFormDefaultValues[ChatChannelKey.WECOM].config.credential.stream =
+ false;
+ChatChannelFormDefaultValues[ChatChannelKey.DINGTALK].config.credential.stream =
+ false;
+ChatChannelFormDefaultValues[
+ ChatChannelKey.DINGTALK
+].config.credential.stream_content_key = 'content';
ChatChannelFormDefaultValues[ChatChannelKey.FEISHU].config.credential.domain =
'feishu';
+ChatChannelFormDefaultValues[ChatChannelKey.FEISHU].config.credential.stream =
+ false;
export const getChatChannelFields = (
key?: ChatChannelKey,
): FormFieldConfig[] => {