341b7e3990
DINGTALK_CARD_PARTIAL_UPDATES selects how much of an answer is written into
the card while it is still being produced:
partial (default, unset) - placeholder card plus throttled partial answer
and progress frames, then the final update
final - placeholder card, no partial frames at all; the
finished answer lands in the single final update
direct - no placeholder card; one card with the answer
Both non-default modes drop the 500ms redraw pump for the turn, so the card
only ever shows content that is actually final. handleNonStreaming and the
two new modes share one delivery helper, and the markdown fallback keeps the
existing behaviour when the card cannot be delivered.
970 lines
34 KiB
Go
970 lines
34 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/directory"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundtext"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
|
)
|
|
|
|
const (
|
|
msgTypeText = "text"
|
|
msgTypePicture = "picture"
|
|
conversationTypeDirect = "1"
|
|
conversationTypeGroup = "2"
|
|
replyTitle = "DobotAI"
|
|
|
|
unsupportedMessageReply = "当前版本暂仅支持文字消息。"
|
|
qaErrorReply = "抱歉,当前暂时无法完成回答,请稍后重试。"
|
|
imageErrorReply = "图片识别暂时不可用,请稍后重试或发送文字。"
|
|
imageQuery = "请描述并分析这张图片。"
|
|
streamingPlaceholder = "正在理解问题…"
|
|
)
|
|
|
|
type TenantConfig struct {
|
|
TenantKey string
|
|
Channel string
|
|
}
|
|
|
|
type BotCallback struct {
|
|
ConversationType string
|
|
ConversationID string
|
|
ConversationTitle string
|
|
Msgtype string
|
|
MsgID string
|
|
SenderNick string
|
|
SenderStaffID string
|
|
SenderID string
|
|
IsInAtList bool
|
|
SessionWebhook string
|
|
Text string
|
|
DownloadCode string
|
|
DownloadCodes []string
|
|
}
|
|
|
|
type QAAsker interface {
|
|
Ask(ctx context.Context, req core.QARequest) (core.QAResult, error)
|
|
}
|
|
|
|
type DeliveryRecorder interface {
|
|
RecordDelivery(ctx context.Context, update core.DeliveryUpdate) error
|
|
}
|
|
|
|
// StreamingQAAsker asks the QA backend with streaming observer support.
|
|
type StreamingQAAsker interface {
|
|
QAAsker
|
|
AskStream(ctx context.Context, req core.QARequest, observer weknora.AnswerObserver) (core.QAResult, error)
|
|
}
|
|
|
|
// EventQAAsker exposes safe stage metadata without thinking/tool payloads.
|
|
type EventQAAsker interface {
|
|
QAAsker
|
|
AskEvents(ctx context.Context, req core.QARequest, observer weknora.StreamObserver) (core.QAResult, error)
|
|
}
|
|
|
|
type MarkdownSender interface {
|
|
SendMarkdown(ctx context.Context, webhookURL, title, text string) error
|
|
}
|
|
|
|
type ImageDownloader interface {
|
|
Download(ctx context.Context, downloadCode string) (core.ImageInput, error)
|
|
}
|
|
|
|
// ImagePublisher publishes a downloaded image to object storage and returns a
|
|
// stable HTTPS URL. Implementation satisfies outboundimage.Publisher.
|
|
type ImagePublisher interface {
|
|
Publish(ctx context.Context, data []byte, contentType string) (string, error)
|
|
}
|
|
|
|
type UserDirectory interface {
|
|
ResolveUser(ctx context.Context, tenantKey, staffID, fallbackName string) (directory.Profile, error)
|
|
}
|
|
|
|
type OutboundContentRewriter interface {
|
|
Rewrite(ctx context.Context, content string) string
|
|
}
|
|
|
|
type OutboundPartialSanitizer interface {
|
|
SanitizePartial(content string) string
|
|
}
|
|
|
|
type SettingsMessage struct {
|
|
TenantKey string
|
|
Channel string
|
|
StaffID string
|
|
UnionID string
|
|
DisplayName string
|
|
Department string
|
|
Text string
|
|
ConversationType string
|
|
ConversationID string
|
|
ConversationTitle string
|
|
}
|
|
|
|
type SettingsRouter interface {
|
|
Handle(context.Context, SettingsMessage) (handled bool, reply string, err error)
|
|
}
|
|
|
|
type ReasoningMessage struct {
|
|
TenantKey string
|
|
Channel string
|
|
StaffID string
|
|
Text string
|
|
ConversationID string
|
|
}
|
|
|
|
type ReasoningDecision struct {
|
|
Handled bool
|
|
Reply string
|
|
Query string
|
|
AgentID string
|
|
Execution reasoningmode.ExecutionMode
|
|
Expired bool
|
|
}
|
|
|
|
type ReasoningRouter interface {
|
|
Prepare(context.Context, ReasoningMessage) (ReasoningDecision, error)
|
|
}
|
|
|
|
type LanguagePreferenceResolver interface {
|
|
GetPreference(context.Context, string, string, string) (string, error)
|
|
}
|
|
|
|
// FinalCardSender sends an AI card as the final reply. Implementations must
|
|
// not log err, outTrackID, or content at any level.
|
|
type FinalCardSender interface {
|
|
SendFinalCard(context.Context, CardRequest) (string, error)
|
|
}
|
|
|
|
// StreamingCardSender supports streaming card lifecycle: start, update, and finalize.
|
|
type StreamingCardSender interface {
|
|
FinalCardSender
|
|
StartCard(ctx context.Context, req CardRequest) (string, error)
|
|
UpdateCard(ctx context.Context, id, content string, isFinalize, isError bool) error
|
|
}
|
|
|
|
type HandlerOptions struct {
|
|
ImageDownloader ImageDownloader
|
|
ImagePublisher ImagePublisher
|
|
CardSender FinalCardSender
|
|
Directory UserDirectory
|
|
OutboundRewriter OutboundContentRewriter
|
|
// CardImageMedia rewrites our own published image URLs in card bodies into
|
|
// DingTalk media ids; nil keeps external URLs as-is.
|
|
CardImageMedia CardImageMediaRewriter
|
|
SettingsRouter SettingsRouter
|
|
ReasoningRouter ReasoningRouter
|
|
LanguageResolver LanguagePreferenceResolver
|
|
// OutboundRewriteTimeout bounds the outbound image rewrite stage (fetch +
|
|
// publish). Answers carrying many large screenshots need more than the
|
|
// delivery timeout, otherwise the remaining images are dropped. Zero keeps
|
|
// finalDeliveryTimeout.
|
|
OutboundRewriteTimeout time.Duration
|
|
// WideTableGrouping selects how answer tables are adapted, per frame: ""
|
|
// or "on" groups over-wide tables on every frame (historical), "off" never
|
|
// groups, "stream" groups only while the answer is still streaming and
|
|
// keeps the real table on the final frame, "smooth" also groups every
|
|
// table on streaming frames so a forming table never freezes the card.
|
|
// See tableGroupingForMode.
|
|
WideTableGrouping string
|
|
// PartialUpdates selects whether the answer is streamed into the card.
|
|
// Empty (or an unknown value) keeps the historical partial streaming; see
|
|
// CardPartialUpdatesMode.
|
|
PartialUpdates CardPartialUpdatesMode
|
|
}
|
|
|
|
type Handler struct {
|
|
cfg TenantConfig
|
|
qa QAAsker
|
|
reply MarkdownSender
|
|
downloader ImageDownloader
|
|
imagePublisher ImagePublisher
|
|
card FinalCardSender
|
|
directory UserDirectory
|
|
outboundRewriter OutboundContentRewriter
|
|
cardImageMedia CardImageMediaRewriter
|
|
settingsRouter SettingsRouter
|
|
reasoningRouter ReasoningRouter
|
|
languageResolver LanguagePreferenceResolver
|
|
now func() time.Time
|
|
cardUpdateInterval time.Duration
|
|
finalDeliveryTimeout time.Duration
|
|
// outboundRewriteTimeout applies to the image rewrite stage only; zero
|
|
// falls back to finalDeliveryTimeout.
|
|
outboundRewriteTimeout time.Duration
|
|
// groupTablesPartial / groupTablesFinal decide per frame whether an
|
|
// over-wide table becomes a grouped list. Grouped lists survive the
|
|
// streaming frames where a table is still incomplete; the final frame can
|
|
// keep the real table, which the PC wide-screen card lays out itself.
|
|
groupTablesPartial bool
|
|
groupTablesFinal bool
|
|
// groupAllTablesPartial groups every table on streaming frames (mode
|
|
// "smooth"): the card keeps growing while a table is still forming.
|
|
groupAllTablesPartial bool
|
|
// partialUpdates decides whether the answer is streamed into the card at
|
|
// all; see CardPartialUpdatesMode.
|
|
partialUpdates CardPartialUpdatesMode
|
|
}
|
|
|
|
// NewHandler accepts at most one image downloader. A nil/omitted downloader
|
|
// keeps text ingress available while picture messages fail closed.
|
|
func NewHandler(cfg TenantConfig, qa QAAsker, reply MarkdownSender, downloaders ...ImageDownloader) (*Handler, error) {
|
|
var downloader ImageDownloader
|
|
if len(downloaders) > 1 {
|
|
return nil, errors.New("dingtalk handler: at most one image downloader is allowed")
|
|
}
|
|
if len(downloaders) == 1 {
|
|
downloader = downloaders[0]
|
|
}
|
|
return NewHandlerWithOptions(cfg, qa, reply, HandlerOptions{ImageDownloader: downloader})
|
|
}
|
|
|
|
// NewHandlerWithOptions creates a Handler with an optional image downloader
|
|
// and optional card sender. Passing nil for either option is equivalent to
|
|
// omitting the feature.
|
|
func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, opts HandlerOptions) (*Handler, error) {
|
|
if strings.TrimSpace(cfg.TenantKey) == "" {
|
|
return nil, errors.New("dingtalk handler: tenant key is required")
|
|
}
|
|
if strings.TrimSpace(cfg.Channel) == "" {
|
|
return nil, errors.New("dingtalk handler: channel is required")
|
|
}
|
|
if qa == nil {
|
|
return nil, errors.New("dingtalk handler: QA asker is required")
|
|
}
|
|
if reply == nil {
|
|
return nil, errors.New("dingtalk handler: markdown sender is required")
|
|
}
|
|
groupPartial, groupFinal, groupAllPartial := tableGroupingForMode(opts.WideTableGrouping)
|
|
|
|
return &Handler{
|
|
cfg: cfg,
|
|
qa: qa,
|
|
reply: reply,
|
|
downloader: opts.ImageDownloader,
|
|
imagePublisher: opts.ImagePublisher,
|
|
card: opts.CardSender,
|
|
directory: opts.Directory,
|
|
outboundRewriter: opts.OutboundRewriter,
|
|
cardImageMedia: opts.CardImageMedia,
|
|
settingsRouter: opts.SettingsRouter,
|
|
reasoningRouter: opts.ReasoningRouter,
|
|
languageResolver: opts.LanguageResolver,
|
|
now: time.Now,
|
|
cardUpdateInterval: 500 * time.Millisecond,
|
|
finalDeliveryTimeout: defaultHTTPTimeout,
|
|
|
|
outboundRewriteTimeout: opts.OutboundRewriteTimeout,
|
|
groupTablesPartial: groupPartial,
|
|
groupTablesFinal: groupFinal,
|
|
groupAllTablesPartial: groupAllPartial,
|
|
partialUpdates: opts.PartialUpdates,
|
|
}, nil
|
|
}
|
|
|
|
func (h *Handler) Handle(ctx context.Context, cb BotCallback) error {
|
|
// Admission comes first: a group message without @ must have zero side effects.
|
|
if cb.ConversationType == conversationTypeGroup && !cb.IsInAtList {
|
|
return nil
|
|
}
|
|
|
|
// Validate the only reply destination before downloading media or spending a
|
|
// model call. Webhook errors never include its query string.
|
|
webhookURL := strings.TrimSpace(cb.SessionWebhook)
|
|
if webhookURL == "" {
|
|
return errors.New("dingtalk handler: sessionWebhook is required to reply")
|
|
}
|
|
if err := ValidateWebhookURL(webhookURL); err != nil {
|
|
return fmt.Errorf("dingtalk handler: invalid sessionWebhook: %w", err)
|
|
}
|
|
if strings.TrimSpace(cb.MsgID) == "" {
|
|
return errors.New("dingtalk handler: msgId is required")
|
|
}
|
|
|
|
externalUserID := strings.TrimSpace(cb.SenderStaffID)
|
|
if externalUserID == "" {
|
|
externalUserID = strings.TrimSpace(cb.SenderID)
|
|
}
|
|
if externalUserID == "" {
|
|
return errors.New("dingtalk handler: sender staffId/senderId is required")
|
|
}
|
|
|
|
triggerPerson := strings.TrimSpace(cb.SenderNick)
|
|
senderName := triggerPerson
|
|
department := ""
|
|
region := ""
|
|
actorUnionID := ""
|
|
if h.directory != nil {
|
|
profile, _ := h.directory.ResolveUser(ctx, h.cfg.TenantKey, externalUserID, senderName)
|
|
if name := strings.TrimSpace(profile.Name); name != "" {
|
|
senderName = name
|
|
department = strings.TrimSpace(profile.DepartmentName)
|
|
region = strings.TrimSpace(profile.Region)
|
|
}
|
|
actorUnionID = strings.TrimSpace(profile.UnionID)
|
|
}
|
|
// Trigger person snapshot falls back to official name when callback nick is blank.
|
|
if triggerPerson == "" {
|
|
triggerPerson = senderName
|
|
}
|
|
|
|
if h.settingsRouter != nil && strings.EqualFold(strings.TrimSpace(cb.Msgtype), msgTypeText) {
|
|
handled, settingsReply, err := h.settingsRouter.Handle(ctx, SettingsMessage{
|
|
TenantKey: h.cfg.TenantKey, Channel: h.cfg.Channel, StaffID: externalUserID,
|
|
UnionID: actorUnionID, DisplayName: senderName, Department: department, Text: strings.TrimSpace(cb.Text),
|
|
ConversationType: mapDingTalkConversationType(cb.ConversationType),
|
|
ConversationID: strings.TrimSpace(cb.ConversationID), ConversationTitle: strings.TrimSpace(cb.ConversationTitle),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("dingtalk settings: %w", err)
|
|
}
|
|
if handled {
|
|
if strings.TrimSpace(settingsReply) == "" {
|
|
return nil
|
|
}
|
|
return h.sendReply(ctx, webhookURL, settingsReply)
|
|
}
|
|
}
|
|
|
|
req := core.QARequest{
|
|
TenantKey: h.cfg.TenantKey,
|
|
Channel: h.cfg.Channel,
|
|
ExternalUserID: externalUserID,
|
|
ExternalMessageID: strings.TrimSpace(cb.MsgID),
|
|
SenderName: senderName,
|
|
TriggerPersonSnapshot: triggerPerson,
|
|
ActorUnionIDSnapshot: actorUnionID,
|
|
Department: department,
|
|
Region: region,
|
|
MessageType: strings.ToLower(strings.TrimSpace(cb.Msgtype)),
|
|
ConversationTitle: strings.TrimSpace(cb.ConversationTitle),
|
|
}
|
|
if h.languageResolver != nil {
|
|
if locale, err := h.languageResolver.GetPreference(ctx, h.cfg.TenantKey, h.cfg.Channel, externalUserID); err == nil {
|
|
req.ResponseLocale = strings.TrimSpace(locale)
|
|
}
|
|
}
|
|
switch cb.ConversationType {
|
|
case conversationTypeGroup:
|
|
if strings.TrimSpace(cb.ConversationID) == "" {
|
|
return errors.New("dingtalk handler: conversationId is required for group messages")
|
|
}
|
|
req.ConversationType = core.ConversationGroup
|
|
req.ExternalChatID = strings.TrimSpace(cb.ConversationID)
|
|
default:
|
|
req.ConversationType = core.ConversationDirect
|
|
req.ConversationTitle = ""
|
|
}
|
|
|
|
switch strings.ToLower(strings.TrimSpace(cb.Msgtype)) {
|
|
case msgTypeText:
|
|
req.Query = strings.TrimSpace(cb.Text)
|
|
if req.Query == "" {
|
|
return errors.New("dingtalk handler: text message content is empty")
|
|
}
|
|
case msgTypePicture:
|
|
codes := effectiveDownloadCodes(cb)
|
|
if len(codes) == 0 {
|
|
log.Printf("DingTalk image preparation failed: stage=missing_download_code")
|
|
return h.sendReply(ctx, webhookURL, imageErrorReply)
|
|
}
|
|
if h.downloader == nil {
|
|
log.Printf("DingTalk image preparation failed: stage=downloader_unavailable")
|
|
return h.sendReply(ctx, webhookURL, imageErrorReply)
|
|
}
|
|
images := make([]core.ImageInput, 0, len(codes))
|
|
for _, code := range codes {
|
|
image, err := h.downloader.Download(ctx, code)
|
|
if err != nil {
|
|
log.Printf("DingTalk image preparation failed: stage=%s", imageFailureStage(err))
|
|
return h.sendReply(ctx, webhookURL, imageErrorReply)
|
|
}
|
|
if h.imagePublisher != nil {
|
|
if url := h.publishQuestionImage(ctx, image); url != "" {
|
|
image.PublishedURL = url
|
|
}
|
|
}
|
|
images = append(images, image)
|
|
}
|
|
req.Query = strings.TrimSpace(cb.Text)
|
|
if req.Query == "" {
|
|
req.Query = imageQuery
|
|
}
|
|
req.Images = images
|
|
default:
|
|
return h.sendReply(ctx, webhookURL, unsupportedMessageReply)
|
|
}
|
|
|
|
if h.reasoningRouter != nil {
|
|
decision, err := h.reasoningRouter.Prepare(ctx, ReasoningMessage{
|
|
TenantKey: h.cfg.TenantKey, Channel: h.cfg.Channel, StaffID: externalUserID,
|
|
Text: req.Query, ConversationID: req.ExternalChatID,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("dingtalk reasoning mode: %w", err)
|
|
}
|
|
if decision.Handled {
|
|
if strings.TrimSpace(decision.Reply) == "" {
|
|
return nil
|
|
}
|
|
return h.sendReply(ctx, webhookURL, decision.Reply)
|
|
}
|
|
req.Query = strings.TrimSpace(decision.Query)
|
|
req.AgentIDOverride = strings.TrimSpace(decision.AgentID)
|
|
req.ReasoningExecution = string(decision.Execution)
|
|
req.ReasoningExpired = decision.Expired
|
|
}
|
|
|
|
if eventCard, eventQA := h.eventStreamingCapable(); eventCard != nil && eventQA != nil {
|
|
return h.handleEventStreaming(ctx, webhookURL, req, eventCard, eventQA)
|
|
}
|
|
// Backward-compatible A3.2 answer-only streaming capability.
|
|
streamingCard, streamingQA := h.streamingCapable()
|
|
if streamingCard != nil && streamingQA != nil {
|
|
return h.handleStreaming(ctx, webhookURL, req, streamingCard, streamingQA)
|
|
}
|
|
return h.handleNonStreaming(ctx, webhookURL, req)
|
|
}
|
|
|
|
func mapDingTalkConversationType(value string) string {
|
|
if value == conversationTypeGroup {
|
|
return "group"
|
|
}
|
|
return "direct"
|
|
}
|
|
|
|
func (h *Handler) eventStreamingCapable() (StreamingCardSender, EventQAAsker) {
|
|
sc, ok := h.card.(StreamingCardSender)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
eq, ok := h.qa.(EventQAAsker)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
return sc, eq
|
|
}
|
|
|
|
func (h *Handler) streamingCapable() (StreamingCardSender, StreamingQAAsker) {
|
|
sc, ok := h.card.(StreamingCardSender)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
sq, ok := h.qa.(StreamingQAAsker)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
return sc, sq
|
|
}
|
|
|
|
func (h *Handler) handleNonStreaming(ctx context.Context, webhookURL string, req core.QARequest) error {
|
|
result, err := h.qa.Ask(ctx, req)
|
|
if err != nil {
|
|
logQAFailure("non_streaming", err)
|
|
if replyErr := h.sendReply(ctx, webhookURL, qaErrorReply); replyErr != nil {
|
|
return replyErr
|
|
}
|
|
return nil
|
|
}
|
|
if result.Replayed && result.DeliveryStatus == core.DeliverySent {
|
|
return nil
|
|
}
|
|
return h.deliverAnswerCardDirect(ctx, webhookURL, req, result)
|
|
}
|
|
|
|
// deliverAnswerCardDirect delivers a finished answer as a single card built from
|
|
// the final content (no partial frames), falling back to the markdown reply when
|
|
// the card cannot be delivered. The delivery gets a fresh deadline so a long
|
|
// answer whose QA context already expired still reaches the user.
|
|
func (h *Handler) deliverAnswerCardDirect(ctx context.Context, webhookURL string, req core.QARequest, result core.QAResult) error {
|
|
timeout := h.finalDeliveryTimeout
|
|
if timeout <= 0 {
|
|
timeout = defaultHTTPTimeout
|
|
}
|
|
deliverCtx, cancelDeliver := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
|
defer cancelDeliver()
|
|
|
|
content := h.rewriteOutboundContent(deliverCtx, result.Answer)
|
|
|
|
if h.card != nil {
|
|
cardReq := CardRequest{
|
|
ConversationType: req.ConversationType,
|
|
ExternalUserID: req.ExternalUserID,
|
|
ExternalChatID: req.ExternalChatID,
|
|
Content: h.rewriteCardImages(deliverCtx, content, true),
|
|
}
|
|
if outTrackID, cardErr := h.card.SendFinalCard(deliverCtx, cardReq); cardErr == nil {
|
|
h.recordDelivery(ctx, result, core.DeliverySent, core.DeliveryMethodCard, outTrackID, "")
|
|
return nil
|
|
}
|
|
log.Printf("DingTalk card delivery failed, falling back to markdown")
|
|
}
|
|
if err := h.sendReply(deliverCtx, webhookURL, content); err != nil {
|
|
h.recordDelivery(ctx, result, core.DeliveryFailed, "", "", core.DeliveryFailureChannelDelivery)
|
|
return err
|
|
}
|
|
h.recordDelivery(ctx, result, core.DeliverySent, core.DeliveryMethodMarkdown, "", "")
|
|
return nil
|
|
}
|
|
|
|
func (h *Handler) handleStreaming(ctx context.Context, webhookURL string, req core.QARequest, streamingCard StreamingCardSender, streamingQA StreamingQAAsker) error {
|
|
if !h.partialUpdates.placeholderCardRequested() {
|
|
// Mode "direct": no placeholder card and no partial frames. The answer
|
|
// is asked for first and delivered as one finished card.
|
|
result, err := streamingQA.AskStream(ctx, req, func(string) {})
|
|
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
|
logQAFailure("stream_direct", errOrEmptyAnswer(err, result.Answer))
|
|
return h.sendReply(ctx, webhookURL, qaErrorReply)
|
|
}
|
|
if result.Replayed && result.DeliveryStatus == core.DeliverySent {
|
|
return nil
|
|
}
|
|
return h.deliverAnswerCardDirect(ctx, webhookURL, req, result)
|
|
}
|
|
|
|
cardReq := CardRequest{
|
|
ConversationType: req.ConversationType,
|
|
ExternalUserID: req.ExternalUserID,
|
|
ExternalChatID: req.ExternalChatID,
|
|
Content: streamingPlaceholder,
|
|
}
|
|
outTrackID, err := streamingCard.StartCard(ctx, cardReq)
|
|
if err != nil {
|
|
log.Printf("DingTalk streaming card start failed, falling back to non-streaming")
|
|
// Fallback: Ask → Markdown only (no SendFinalCard, no AskStream)
|
|
result, askErr := h.qa.Ask(ctx, req)
|
|
if askErr != nil {
|
|
logQAFailure("streaming_fallback", askErr)
|
|
if replyErr := h.sendReply(ctx, webhookURL, qaErrorReply); replyErr != nil {
|
|
return replyErr
|
|
}
|
|
return nil
|
|
}
|
|
if result.Replayed && result.DeliveryStatus == core.DeliverySent {
|
|
return nil
|
|
}
|
|
content := h.rewriteOutboundContent(ctx, result.Answer)
|
|
if replyErr := h.sendReply(ctx, webhookURL, content); replyErr != nil {
|
|
h.recordDelivery(ctx, result, core.DeliveryFailed, "", "", core.DeliveryFailureChannelDelivery)
|
|
return replyErr
|
|
}
|
|
h.recordDelivery(ctx, result, core.DeliverySent, core.DeliveryMethodMarkdown, "", "")
|
|
return nil
|
|
}
|
|
|
|
// Streaming observer with the configured update throttle (500ms by default).
|
|
var lastUpdate time.Time
|
|
var lastPartialContent string
|
|
feed := reasoningmode.NewPartialFeed(req.ReasoningExecution)
|
|
observer := func(fullContent string) {
|
|
if !h.partialUpdates.streamsPartialFrames() {
|
|
// Mode "final": the finished answer is the only card content, so the
|
|
// throttle and the frame itself are skipped.
|
|
return
|
|
}
|
|
visible := feed.OnAnswer(fullContent)
|
|
if strings.TrimSpace(visible) == "" {
|
|
return
|
|
}
|
|
fullContent = decorateReasoningPartial(visible, req.ReasoningExecution)
|
|
now := h.now()
|
|
if lastUpdate.IsZero() || now.Sub(lastUpdate) >= h.cardUpdateInterval {
|
|
partialContent := h.sanitizeOutboundPartial(ctx, fullContent)
|
|
if strings.TrimSpace(partialContent) == "" || partialContent == lastPartialContent {
|
|
return
|
|
}
|
|
// Advance the throttle window before I/O so a failing DingTalk API
|
|
// cannot be hammered once per model chunk. The final update below is
|
|
// always attempted regardless of this timestamp.
|
|
lastUpdate = now
|
|
if updateErr := streamingCard.UpdateCard(ctx, outTrackID, partialContent, false, false); updateErr != nil {
|
|
log.Printf("DingTalk streaming card update failed, continuing")
|
|
return
|
|
}
|
|
lastPartialContent = partialContent
|
|
}
|
|
}
|
|
|
|
result, err := streamingQA.AskStream(ctx, req, observer)
|
|
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
|
logQAFailure("streaming", errOrEmptyAnswer(err, result.Answer))
|
|
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, core.QAResult{}, qaErrorReply, true)
|
|
}
|
|
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, result, result.Answer, false)
|
|
}
|
|
|
|
func (h *Handler) handleEventStreaming(ctx context.Context, webhookURL string, req core.QARequest, streamingCard StreamingCardSender, eventQA EventQAAsker) error {
|
|
if !h.partialUpdates.placeholderCardRequested() {
|
|
// Mode "direct": no placeholder card and no partial frames. The answer
|
|
// is asked for first and delivered as one finished card.
|
|
result, err := eventQA.AskEvents(ctx, req, func(weknora.StreamEvent) {})
|
|
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
|
logQAFailure("event_direct", errOrEmptyAnswer(err, result.Answer))
|
|
return h.sendReply(ctx, webhookURL, qaErrorReply)
|
|
}
|
|
if result.Replayed && result.DeliveryStatus == core.DeliverySent {
|
|
return nil
|
|
}
|
|
return h.deliverAnswerCardDirect(ctx, webhookURL, req, result)
|
|
}
|
|
|
|
cardReq := CardRequest{
|
|
ConversationType: req.ConversationType,
|
|
ExternalUserID: req.ExternalUserID,
|
|
ExternalChatID: req.ExternalChatID,
|
|
Content: streamingPlaceholder,
|
|
}
|
|
outTrackID, err := streamingCard.StartCard(ctx, cardReq)
|
|
if err != nil {
|
|
log.Printf("DingTalk event card start failed, falling back to markdown")
|
|
result, askErr := h.qa.Ask(ctx, req)
|
|
if askErr != nil {
|
|
logQAFailure("event_fallback", askErr)
|
|
return h.sendReply(ctx, webhookURL, qaErrorReply)
|
|
}
|
|
if result.Replayed && result.DeliveryStatus == core.DeliverySent {
|
|
return nil
|
|
}
|
|
content := h.rewriteOutboundContent(ctx, result.Answer)
|
|
if replyErr := h.sendReply(ctx, webhookURL, content); replyErr != nil {
|
|
h.recordDelivery(ctx, result, core.DeliveryFailed, "", "", core.DeliveryFailureChannelDelivery)
|
|
return replyErr
|
|
}
|
|
h.recordDelivery(ctx, result, core.DeliverySent, core.DeliveryMethodMarkdown, "", "")
|
|
return nil
|
|
}
|
|
|
|
var pump *cardUpdatePump
|
|
if h.partialUpdates.streamsPartialFrames() {
|
|
pump = newCardUpdatePump(ctx, h.cardUpdateInterval, func(updateCtx context.Context, content string) error {
|
|
return streamingCard.UpdateCard(updateCtx, outTrackID, content, false, false)
|
|
})
|
|
}
|
|
feed := reasoningmode.NewPartialFeed(req.ReasoningExecution)
|
|
result, err := eventQA.AskEvents(ctx, req, func(event weknora.StreamEvent) {
|
|
if pump == nil {
|
|
// Mode "final": the finished answer is the only card content, so the
|
|
// partial frames and the progress lines are dropped instead of being
|
|
// drawn on every model chunk.
|
|
return
|
|
}
|
|
if event.ResponseType == "answer" {
|
|
visible := feed.OnAnswer(event.FullAnswer)
|
|
if strings.TrimSpace(visible) == "" {
|
|
return
|
|
}
|
|
visibleAnswer := decorateReasoningPartial(visible, req.ReasoningExecution)
|
|
if partialContent := h.sanitizeOutboundPartial(ctx, visibleAnswer); strings.TrimSpace(partialContent) != "" {
|
|
pump.Offer(partialContent)
|
|
}
|
|
return
|
|
}
|
|
switch event.ResponseType {
|
|
case "tool_call", "tool_result", "thinking", "reflection":
|
|
// A reasoning step ended: the next narrative segment starts a new
|
|
// line so the card shows the reasoning process line by line.
|
|
feed.StepBoundary()
|
|
}
|
|
if feed.Started() {
|
|
return
|
|
}
|
|
if content := renderProgressEvent(event); content != "" {
|
|
pump.Offer(content)
|
|
}
|
|
})
|
|
// Stop and drain the asynchronous worker before the synchronous final update
|
|
// so no stale progress/partial answer can overwrite the terminal card state.
|
|
if pump != nil {
|
|
pump.Stop()
|
|
}
|
|
|
|
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
|
logQAFailure("event_streaming", errOrEmptyAnswer(err, result.Answer))
|
|
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, core.QAResult{}, qaErrorReply, true)
|
|
}
|
|
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, result, result.Answer, false)
|
|
}
|
|
|
|
func decorateReasoningPartial(content, execution string) string {
|
|
switch reasoningmode.ExecutionMode(strings.TrimSpace(execution)) {
|
|
case reasoningmode.ExecutionReasoning, reasoningmode.ExecutionReasoningSingle:
|
|
if !strings.HasPrefix(content, "【智能推理模式】") {
|
|
return "【智能推理模式】\n\n" + content
|
|
}
|
|
}
|
|
return content
|
|
}
|
|
|
|
func (h *Handler) finalizeCardOrMarkdown(ctx context.Context, webhookURL string, streamingCard StreamingCardSender, outTrackID string, result core.QAResult, content string, isError bool) error {
|
|
timeout := h.finalDeliveryTimeout
|
|
if timeout <= 0 {
|
|
timeout = defaultHTTPTimeout
|
|
}
|
|
|
|
if !isError {
|
|
content = h.rewriteOutboundContent(ctx, content)
|
|
}
|
|
|
|
// Final card convergence must still be attempted when the QA context was
|
|
// canceled or timed out; otherwise the card can remain "processing".
|
|
finalCtx, cancelFinal := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
|
cardContent := content
|
|
if !isError {
|
|
// Card bodies render DingTalk media ids only, so the images are
|
|
// converted here while the markdown fallback keeps the external URLs.
|
|
cardContent = h.rewriteCardImages(finalCtx, content, true)
|
|
}
|
|
updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, cardContent, true, isError)
|
|
cancelFinal()
|
|
if updateErr == nil {
|
|
if !isError {
|
|
h.recordDelivery(ctx, result, core.DeliverySent, core.DeliveryMethodCard, outTrackID, "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// The card attempt may have consumed its entire deadline. Markdown must get
|
|
// a fresh delivery window rather than reusing the expired finalCtx.
|
|
fallbackCtx, cancelFallback := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
|
defer cancelFallback()
|
|
if err := h.sendReply(fallbackCtx, webhookURL, content); err != nil {
|
|
if !isError {
|
|
h.recordDelivery(ctx, result, core.DeliveryFailed, "", "", core.DeliveryFailureChannelDelivery)
|
|
}
|
|
return err
|
|
}
|
|
if !isError {
|
|
h.recordDelivery(ctx, result, core.DeliverySent, core.DeliveryMethodMarkdown, "", "")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) string {
|
|
content = outboundtext.NormalizeMath(content)
|
|
options := tableNormalizeOptions{groupWideTables: h.groupTablesFinal}
|
|
if h.outboundRewriter == nil {
|
|
return normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
|
|
}
|
|
timeout := h.outboundRewriteTimeout
|
|
if timeout <= 0 {
|
|
timeout = h.finalDeliveryTimeout
|
|
}
|
|
if timeout <= 0 {
|
|
timeout = defaultHTTPTimeout
|
|
}
|
|
rewriteCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
|
defer cancel()
|
|
return normalizeMarkdownTables(sanitizeDisplayAnswer(h.outboundRewriter.Rewrite(rewriteCtx, content)), options)
|
|
}
|
|
|
|
func (h *Handler) sanitizeOutboundPartial(ctx context.Context, content string) string {
|
|
content = outboundtext.NormalizeMath(content)
|
|
sanitizer, ok := h.outboundRewriter.(OutboundPartialSanitizer)
|
|
if ok {
|
|
content = sanitizer.SanitizePartial(content)
|
|
}
|
|
options := tableNormalizeOptions{groupWideTables: h.groupTablesPartial, groupAllTables: h.groupAllTablesPartial}
|
|
content = normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
|
|
return h.rewriteCardImages(ctx, stableMarkdownPrefix(content), false)
|
|
}
|
|
|
|
// rewriteCardImages converts image references in card content into DingTalk
|
|
// media ids. It is a no-op when the feature is not configured and never fails
|
|
// the caller: unuploadable images keep their original external reference.
|
|
func (h *Handler) rewriteCardImages(ctx context.Context, content string, wait bool) string {
|
|
if h.cardImageMedia == nil || strings.TrimSpace(content) == "" {
|
|
return content
|
|
}
|
|
rewritten := h.cardImageMedia.RewriteForCard(ctx, content, wait)
|
|
if strings.TrimSpace(rewritten) == "" {
|
|
return content
|
|
}
|
|
return rewritten
|
|
}
|
|
|
|
func (h *Handler) recordDelivery(ctx context.Context, result core.QAResult, status, method, providerMessageID, failureCode string) {
|
|
if strings.TrimSpace(result.TurnID) == "" || strings.TrimSpace(result.AnswerMessageID) == "" {
|
|
return
|
|
}
|
|
recorder, ok := h.qa.(DeliveryRecorder)
|
|
if !ok {
|
|
return
|
|
}
|
|
timeout := h.finalDeliveryTimeout
|
|
if timeout <= 0 {
|
|
timeout = defaultHTTPTimeout
|
|
}
|
|
auditCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
|
defer cancel()
|
|
err := recorder.RecordDelivery(auditCtx, core.DeliveryUpdate{
|
|
TenantKey: h.cfg.TenantKey,
|
|
Channel: h.cfg.Channel,
|
|
AnswerMessageID: result.AnswerMessageID,
|
|
CorrelationID: result.TurnID,
|
|
Status: status,
|
|
DeliveryMethod: method,
|
|
ProviderMessageID: providerMessageID,
|
|
FailureCode: failureCode,
|
|
})
|
|
if err != nil {
|
|
log.Printf("DingTalk delivery audit update failed: status=%s method=%s", status, method)
|
|
}
|
|
}
|
|
|
|
func renderProgressEvent(event weknora.StreamEvent) string {
|
|
switch event.ResponseType {
|
|
case "tool_call":
|
|
switch strings.TrimSpace(event.ToolName) {
|
|
case "query_understand":
|
|
return "正在理解问题…"
|
|
case "knowledge_search":
|
|
return "正在检索知识库…"
|
|
}
|
|
case "tool_result":
|
|
switch strings.TrimSpace(event.ToolName) {
|
|
case "query_understand":
|
|
return "问题理解完成\n\n正在检索知识库…"
|
|
case "knowledge_search":
|
|
return "知识库检索完成\n\n正在生成答案…"
|
|
}
|
|
case "references":
|
|
return fmt.Sprintf("检索到 %d 条相关内容\n\n正在生成答案…", event.ReferenceCount)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func errOrEmptyAnswer(err error, answer string) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(answer) == "" {
|
|
return weknora.ErrEmptyAnswer
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func logQAFailure(mode string, err error) {
|
|
log.Printf("DingTalk QA failed: mode=%s stage=%s", mode, qaFailureStage(err))
|
|
}
|
|
|
|
func qaFailureStage(err error) string {
|
|
switch {
|
|
case err == nil:
|
|
return "unknown"
|
|
case errors.Is(err, context.Canceled):
|
|
return "context_canceled"
|
|
case errors.Is(err, context.DeadlineExceeded):
|
|
return "context_deadline"
|
|
case errors.Is(err, core.ErrTurnInProgress):
|
|
return "duplicate_or_in_progress"
|
|
case errors.Is(err, weknora.ErrEmptyAnswer):
|
|
return "empty_answer"
|
|
}
|
|
message := strings.ToLower(err.Error())
|
|
switch {
|
|
case strings.Contains(message, "begin turn"):
|
|
return "begin_turn"
|
|
case strings.Contains(message, "agent") && strings.Contains(message, "binding"):
|
|
return "agent_binding"
|
|
case strings.Contains(message, "session"):
|
|
return "session"
|
|
case strings.Contains(message, "weknora agent chat"):
|
|
return "agent_chat"
|
|
case strings.Contains(message, "required") || strings.Contains(message, "unsupported"):
|
|
return "validation"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
func imageFailureStage(err error) string {
|
|
message := strings.ToLower(err.Error())
|
|
switch {
|
|
case strings.Contains(message, "oauth"):
|
|
return "oauth"
|
|
case strings.Contains(message, "api"):
|
|
return "file_api"
|
|
case strings.Contains(message, "unsafe") || strings.Contains(message, "destination"):
|
|
return "unsafe_url"
|
|
case strings.Contains(message, "format") || strings.Contains(message, "image data") || strings.Contains(message, "dimensions") || strings.Contains(message, "size limit"):
|
|
return "image_validation"
|
|
case strings.Contains(message, "request") || strings.Contains(message, "http") || strings.Contains(message, "read"):
|
|
return "image_fetch"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
func (h *Handler) sendReply(ctx context.Context, webhookURL, text string) error {
|
|
if strings.TrimSpace(webhookURL) == "" {
|
|
return errors.New("dingtalk handler: sessionWebhook is required to reply")
|
|
}
|
|
if err := h.reply.SendMarkdown(ctx, webhookURL, replyTitle, text); err != nil {
|
|
return fmt.Errorf("dingtalk handler: send reply: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// publishQuestionImage best-effort publishes a downloaded image's bytes to
|
|
// object storage for traceability. It returns the stable HTTPS URL, or an
|
|
// empty string when publishing is unavailable or fails, so that an image
|
|
// question is never blocked by storage.
|
|
func (h *Handler) publishQuestionImage(ctx context.Context, image core.ImageInput) string {
|
|
if h.imagePublisher == nil {
|
|
return ""
|
|
}
|
|
data, err := decodeImageDataURI(image.DataURI)
|
|
if err != nil || len(data) == 0 {
|
|
log.Printf("DingTalk question image publish failed: stage=decode")
|
|
return ""
|
|
}
|
|
url, err := h.imagePublisher.Publish(ctx, data, image.MIMEType)
|
|
if err != nil || strings.TrimSpace(url) == "" {
|
|
log.Printf("DingTalk question image publish failed: stage=publish")
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(url)
|
|
}
|
|
|
|
// decodeImageDataURI strips the "data:<mime>;base64," prefix and decodes the
|
|
// payload into raw bytes. It is used only to publish inbound images and never
|
|
// writes the data URI to storage.
|
|
func decodeImageDataURI(dataURI string) ([]byte, error) {
|
|
const prefix = "base64,"
|
|
index := strings.Index(dataURI, prefix)
|
|
if index < 0 {
|
|
return nil, errors.New("dingtalk image: data URI missing base64 payload")
|
|
}
|
|
return base64.StdEncoding.DecodeString(dataURI[index+len(prefix):])
|
|
}
|
|
|
|
// effectiveDownloadCodes returns the ordered, de-duplicated list of image
|
|
// download codes for a picture callback. DownloadCode equals the first code
|
|
// and DownloadCodes holds every code (richText with several pictures), so the
|
|
// single value is used to canonicalize the order without duplication.
|
|
func effectiveDownloadCodes(cb BotCallback) []string {
|
|
var codes []string
|
|
seen := make(map[string]struct{})
|
|
for _, code := range append([]string{cb.DownloadCode}, cb.DownloadCodes...) {
|
|
code = strings.TrimSpace(code)
|
|
if code == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[code]; ok {
|
|
continue
|
|
}
|
|
seen[code] = struct{}{}
|
|
codes = append(codes, code)
|
|
}
|
|
return codes
|
|
}
|