feat(im): add AI card streaming for DingTalk and improve IM platform robustness

- Add DingTalk AI Card streaming support with create/deliver/update via OpenAPI
- Extract shared think-block rendering into reusable im.TransformThinkBlocks
- Add stream orphan reaper and edit throttling for DingTalk and Telegram
- Apply think-block transform on all reply paths (DingTalk, Telegram, Feishu)
- Add Card Template ID config to frontend IM channel panel with i18n
This commit is contained in:
nullkey
2026-03-24 20:12:40 +08:00
committed by lyingbug
parent a1a384ec67
commit 1bf6603271
11 changed files with 414 additions and 136 deletions
+1 -68
View File
@@ -666,75 +666,8 @@ func (a *Adapter) SendStreamChunk(ctx context.Context, incoming *im.IncomingMess
return a.cardkitUpdateElement(ctx, accessToken, streamID, streamingElementID, fullContent, seq)
}
// transformThinkBlocks converts <think>...</think> blocks into Feishu-compatible
// markdown blockquotes. Handles both complete blocks and in-progress blocks
// (where </think> has not yet arrived during streaming).
//
// Output format (matching the OpenClaw Feishu convention):
//
// > 💭 **思考过程**
// > thinking line 1
// > thinking line 2
//
// ---
//
// answer text
func transformThinkBlocks(content string) string {
const (
openTag = "<think>"
closeTag = "</think>"
)
openIdx := strings.Index(content, openTag)
if openIdx < 0 {
return content
}
before := content[:openIdx]
after := content[openIdx+len(openTag):]
closeIdx := strings.Index(after, closeTag)
thinkClosed := closeIdx >= 0
var thinkContent, rest string
if thinkClosed {
thinkContent = after[:closeIdx]
rest = after[closeIdx+len(closeTag):]
} else {
thinkContent = after
}
thinkContent = strings.TrimSpace(thinkContent)
var result strings.Builder
result.WriteString(before)
if thinkContent == "" {
if !thinkClosed {
result.WriteString("> 💭 **思考中...**\n")
return result.String()
}
result.WriteString(strings.TrimLeft(rest, "\n"))
return result.String()
}
// Render each line as a blockquote
result.WriteString("> 💭 **思考过程**\n")
for _, line := range strings.Split(thinkContent, "\n") {
result.WriteString("> ")
result.WriteString(line)
result.WriteString("\n")
}
if thinkClosed {
rest = strings.TrimLeft(rest, "\n")
if rest != "" {
result.WriteString("\n---\n\n")
result.WriteString(rest)
}
}
return result.String()
return im.TransformThinkBlocks(content, im.MarkdownThinkStyle)
}
// EndStream disables streaming_mode and cleans up state.