mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
d3bf6bc90a
* feat(channel): add DingTalk channel adapter - Add DingTalk channel adapter (`internal/channel/adapters/dingtalk/`) using dingtalk-stream-sdk-go, supporting inbound message receiving and outbound text/markdown reply - Register DingTalk adapter in cmd/agent and cmd/memoh - Add go.mod dependency: github.com/memohai/dingtalk-stream-sdk-go - Add Dingtalk and Wecom SVG icons and Vue components to @memohai/icon - Refactor existing icon components to remove redundant inline wrappers - Add `channelTypeDisplayName` util for consistent channel label resolution - Add DingTalk/WeCom i18n entries (en/zh) for types and typesShort - Extend channel-icon, bot-channels, channel-settings-panel to support dingtalk/wecom - Use channelTypeDisplayName in profile page to replace ad-hoc i18n lookup * fix(channel,attachment): channel quality refactor & attachment pipeline fixes Channel module: - Fix RemoveAdapter not cleaning connectionMeta (stale status leak) - Fix preparedAttachmentTypeFromMime misclassifying image/gif - Fix sleepWithContext time.After goroutine/timer leak - Export IsDataURL/IsHTTPURL/IsDataPath, dedup across packages - Cache OutboundPolicy in managerOutboundStream to avoid repeated lookups - Split OutboundAttachmentStore: extract ContainerAttachmentIngester interface - Add ManagerOption funcs (WithInboundQueueSize, WithInboundWorkers, WithRefreshInterval) - Add thread-safety docs on OutboundStream / managerOutboundStream - Add debug logs on successful send/edit paths - Expand outbound_prepare_test.go with 21 new cases - Convert no-receiver adapter helpers to package-level funcs; drop unused params DingTalk adapter: - Implement AttachmentResolver: download inbound media via /v1.0/robot/messageFiles/download - Fix pure-image inbound messages failing due to missing resolver Attachment pipeline: - Fix images invisible to LLM in pipeline (DCP) path: inject InlineImages into last user message when cfg.Query is empty - Fix public_url fallback: skip direct URL-to-LLM when ContentHash is set, always prefer inlined persisted asset - Inject path: carry ImageParts through agent.InjectMessage; inline persisted attachments in resolver inject goroutine so mid-stream images reach the model - Fix ResolveMime for images: prefer content-sniffed MIME over platform-declared MIME (fixes Feishu sending image/png header for actual JPEG content → API 400)
234 lines
7.2 KiB
Go
234 lines
7.2 KiB
Go
package qq
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/memohai/memoh/internal/channel"
|
|
)
|
|
|
|
func preparedQQEvent(event channel.StreamEvent) channel.PreparedStreamEvent {
|
|
prepared := channel.PreparedStreamEvent{
|
|
Type: event.Type,
|
|
Delta: event.Delta,
|
|
Error: event.Error,
|
|
Status: event.Status,
|
|
Phase: event.Phase,
|
|
}
|
|
if len(event.Attachments) > 0 {
|
|
prepared.Attachments = make([]channel.PreparedAttachment, 0, len(event.Attachments))
|
|
for _, att := range event.Attachments {
|
|
prepared.Attachments = append(prepared.Attachments, channel.PreparedAttachment{
|
|
Logical: att,
|
|
Kind: channel.PreparedAttachmentUpload,
|
|
Name: att.Name,
|
|
Mime: att.Mime,
|
|
PublicURL: att.URL,
|
|
Open: func(context.Context) (io.ReadCloser, error) {
|
|
return io.NopCloser(strings.NewReader("test")), nil
|
|
},
|
|
})
|
|
}
|
|
}
|
|
if event.Final != nil {
|
|
prepared.Final = &channel.PreparedStreamFinalizePayload{
|
|
Message: channel.PreparedMessage{Message: event.Final.Message},
|
|
}
|
|
}
|
|
return prepared
|
|
}
|
|
|
|
func TestQQOutboundStreamFlushesBufferedTextOnFinal(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var sent []channel.OutboundMessage
|
|
stream := &qqOutboundStream{
|
|
target: "c2c:user-openid",
|
|
send: func(_ context.Context, msg channel.PreparedOutboundMessage) error {
|
|
sent = append(sent, msg.LogicalMessage())
|
|
return nil
|
|
},
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventStatus, Status: channel.StreamStatusStarted})); err != nil {
|
|
t.Fatalf("push status: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventDelta, Delta: "Hi "})); err != nil {
|
|
t.Fatalf("push delta1: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventDelta, Delta: "there"})); err != nil {
|
|
t.Fatalf("push delta2: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventFinal, Final: &channel.StreamFinalizePayload{}})); err != nil {
|
|
t.Fatalf("push final: %v", err)
|
|
}
|
|
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one send, got %d", len(sent))
|
|
}
|
|
if sent[0].Target != "c2c:user-openid" {
|
|
t.Fatalf("unexpected target: %s", sent[0].Target)
|
|
}
|
|
if sent[0].Message.PlainText() != "Hi there" {
|
|
t.Fatalf("unexpected text: %q", sent[0].Message.PlainText())
|
|
}
|
|
}
|
|
|
|
func TestQQOutboundStreamFinalUsesExplicitMessageAndBufferedAttachments(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var sent []channel.OutboundMessage
|
|
stream := &qqOutboundStream{
|
|
target: "group:group-openid",
|
|
send: func(_ context.Context, msg channel.PreparedOutboundMessage) error {
|
|
sent = append(sent, msg.LogicalMessage())
|
|
return nil
|
|
},
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{
|
|
Type: channel.StreamEventAttachment,
|
|
Attachments: []channel.Attachment{{Type: channel.AttachmentImage, URL: "https://example.com/a.png"}},
|
|
})); err != nil {
|
|
t.Fatalf("push attachment: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{
|
|
Type: channel.StreamEventFinal,
|
|
Final: &channel.StreamFinalizePayload{Message: channel.Message{
|
|
Text: "done",
|
|
}},
|
|
})); err != nil {
|
|
t.Fatalf("push final: %v", err)
|
|
}
|
|
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one send, got %d", len(sent))
|
|
}
|
|
if sent[0].Message.PlainText() != "done" {
|
|
t.Fatalf("unexpected text: %q", sent[0].Message.PlainText())
|
|
}
|
|
if len(sent[0].Message.Attachments) != 1 {
|
|
t.Fatalf("unexpected attachments: %d", len(sent[0].Message.Attachments))
|
|
}
|
|
}
|
|
|
|
func TestQQOutboundStreamFinalPrefersBufferedVisibleText(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var sent []channel.OutboundMessage
|
|
stream := &qqOutboundStream{
|
|
target: "c2c:user-openid",
|
|
send: func(_ context.Context, msg channel.PreparedOutboundMessage) error {
|
|
sent = append(sent, msg.LogicalMessage())
|
|
return nil
|
|
},
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventDelta, Delta: "visible "})); err != nil {
|
|
t.Fatalf("push delta1: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventDelta, Delta: "answer"})); err != nil {
|
|
t.Fatalf("push delta2: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{
|
|
Type: channel.StreamEventFinal,
|
|
Final: &channel.StreamFinalizePayload{Message: channel.Message{
|
|
Text: "internal trace\nvisible answer",
|
|
}},
|
|
})); err != nil {
|
|
t.Fatalf("push final: %v", err)
|
|
}
|
|
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one send, got %d", len(sent))
|
|
}
|
|
if got := sent[0].Message.PlainText(); got != "visible answer" {
|
|
t.Fatalf("unexpected text: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestQQOutboundStreamIgnoresLaterTextOnlyFinalAfterBufferedReply(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var sent []channel.OutboundMessage
|
|
stream := &qqOutboundStream{
|
|
target: "c2c:user-openid",
|
|
send: func(_ context.Context, msg channel.PreparedOutboundMessage) error {
|
|
sent = append(sent, msg.LogicalMessage())
|
|
return nil
|
|
},
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventDelta, Delta: "visible answer"})); err != nil {
|
|
t.Fatalf("push delta: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventFinal, Final: &channel.StreamFinalizePayload{}})); err != nil {
|
|
t.Fatalf("push first final: %v", err)
|
|
}
|
|
if err := stream.Push(ctx, preparedQQEvent(channel.StreamEvent{
|
|
Type: channel.StreamEventFinal,
|
|
Final: &channel.StreamFinalizePayload{Message: channel.Message{
|
|
Text: "我需要按照用户的要求,在工具调用后完整复述。",
|
|
}},
|
|
})); err != nil {
|
|
t.Fatalf("push second final: %v", err)
|
|
}
|
|
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected 1 outbound message, got %d", len(sent))
|
|
}
|
|
if got := sent[0].Message.PlainText(); got != "visible answer" {
|
|
t.Fatalf("unexpected text: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestQQOutboundStreamRejectsAfterClose(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
stream := &qqOutboundStream{}
|
|
if err := stream.Close(context.Background()); err != nil {
|
|
t.Fatalf("close: %v", err)
|
|
}
|
|
if err := stream.Push(context.Background(), preparedQQEvent(channel.StreamEvent{
|
|
Type: channel.StreamEventDelta,
|
|
Delta: "x",
|
|
})); err == nil {
|
|
t.Fatal("expected closed error")
|
|
}
|
|
}
|
|
|
|
func TestQQOutboundStreamErrorRedactsRegisteredTokenFragments(t *testing.T) {
|
|
channel.ResetIMErrorSecretsForTest()
|
|
t.Cleanup(channel.ResetIMErrorSecretsForTest)
|
|
|
|
const token = "qq-token-ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
channel.SetIMErrorSecrets("test", token)
|
|
prefixHalf := token[:len(token)/2]
|
|
|
|
var sent []channel.OutboundMessage
|
|
stream := &qqOutboundStream{
|
|
target: "c2c:user-openid",
|
|
send: func(_ context.Context, msg channel.PreparedOutboundMessage) error {
|
|
sent = append(sent, msg.LogicalMessage())
|
|
return nil
|
|
},
|
|
}
|
|
|
|
err := stream.Push(context.Background(), preparedQQEvent(channel.StreamEvent{Type: channel.StreamEventError, Error: "failed: " + prefixHalf}))
|
|
if err != nil {
|
|
t.Fatalf("push error: %v", err)
|
|
}
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one outbound message, got %d", len(sent))
|
|
}
|
|
if got := sent[0].Message.PlainText(); strings.Contains(got, prefixHalf) {
|
|
t.Fatalf("expected redacted token fragment, got %q", got)
|
|
}
|
|
}
|