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)
189 lines
5.0 KiB
Go
189 lines
5.0 KiB
Go
package messaging
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/memohai/memoh/internal/channel"
|
|
"github.com/memohai/memoh/internal/media"
|
|
)
|
|
|
|
type testSender struct {
|
|
called int
|
|
req channel.SendRequest
|
|
}
|
|
|
|
func (s *testSender) Send(_ context.Context, _ string, _ channel.ChannelType, req channel.SendRequest) error {
|
|
s.called++
|
|
s.req = req
|
|
return nil
|
|
}
|
|
|
|
type testResolver struct{}
|
|
|
|
func (testResolver) ParseChannelType(raw string) (channel.ChannelType, error) {
|
|
return channel.ChannelType(raw), nil
|
|
}
|
|
|
|
type testAssetResolver struct {
|
|
ingestCalled int
|
|
lastPath string
|
|
}
|
|
|
|
func (*testAssetResolver) Stat(_ context.Context, _, _ string) (AssetMeta, error) {
|
|
return AssetMeta{}, context.Canceled
|
|
}
|
|
|
|
func (*testAssetResolver) GetByStorageKey(_ context.Context, _, _ string) (AssetMeta, error) {
|
|
return AssetMeta{}, context.Canceled
|
|
}
|
|
|
|
func (*testAssetResolver) Open(_ context.Context, _, _ string) (io.ReadCloser, media.Asset, error) {
|
|
return nil, media.Asset{}, context.Canceled
|
|
}
|
|
|
|
func (*testAssetResolver) Ingest(_ context.Context, _ media.IngestInput) (media.Asset, error) {
|
|
return media.Asset{}, context.Canceled
|
|
}
|
|
|
|
func (*testAssetResolver) AccessPath(asset media.Asset) string {
|
|
return "https://example.com/media/" + asset.ContentHash
|
|
}
|
|
|
|
func (r *testAssetResolver) IngestContainerFile(_ context.Context, _, containerPath string) (AssetMeta, error) {
|
|
r.ingestCalled++
|
|
r.lastPath = containerPath
|
|
return AssetMeta{
|
|
ContentHash: "hash_1",
|
|
Mime: "image/png",
|
|
SizeBytes: 42,
|
|
StorageKey: "media/generated/hash_1",
|
|
}, nil
|
|
}
|
|
|
|
func TestSendDirectSameConversationWithAttachments(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sender := &testSender{}
|
|
exec := &Executor{
|
|
Sender: sender,
|
|
Resolver: testResolver{},
|
|
}
|
|
|
|
session := SessionContext{
|
|
BotID: "bot_1",
|
|
CurrentPlatform: "feishu",
|
|
ReplyTarget: "chat_id:oc_group_1",
|
|
}
|
|
|
|
result, err := exec.SendDirect(context.Background(), session, "", map[string]any{
|
|
"attachments": []any{"screenshot.png"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SendDirect returned error: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("expected non-nil result")
|
|
}
|
|
if sender.called != 1 {
|
|
t.Fatalf("expected sender called once, got %d", sender.called)
|
|
}
|
|
if sender.req.Target != "chat_id:oc_group_1" {
|
|
t.Fatalf("unexpected target: %q", sender.req.Target)
|
|
}
|
|
if len(sender.req.Message.Attachments) != 1 {
|
|
t.Fatalf("expected 1 attachment, got %d", len(sender.req.Message.Attachments))
|
|
}
|
|
att := sender.req.Message.Attachments[0]
|
|
if att.URL != "/data/screenshot.png" {
|
|
t.Fatalf("unexpected attachment url: %q", att.URL)
|
|
}
|
|
if att.Type != channel.AttachmentImage {
|
|
t.Fatalf("unexpected attachment type: %q", att.Type)
|
|
}
|
|
}
|
|
|
|
func TestSendSameConversationWithAttachmentsUsesLocalResult(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sender := &testSender{}
|
|
exec := &Executor{
|
|
Sender: sender,
|
|
Resolver: testResolver{},
|
|
}
|
|
|
|
session := SessionContext{
|
|
BotID: "bot_1",
|
|
CurrentPlatform: "feishu",
|
|
ReplyTarget: "chat_id:oc_group_1",
|
|
}
|
|
|
|
result, err := exec.Send(context.Background(), session, map[string]any{
|
|
"attachments": []any{"screenshot.png"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Send returned error: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("expected non-nil result")
|
|
}
|
|
if !result.Local {
|
|
t.Fatal("expected local result for same-conversation send")
|
|
}
|
|
if sender.called != 0 {
|
|
t.Fatalf("expected sender not called for local result, got %d", sender.called)
|
|
}
|
|
if len(result.LocalAttachments) != 1 {
|
|
t.Fatalf("expected 1 local attachment, got %d", len(result.LocalAttachments))
|
|
}
|
|
att := result.LocalAttachments[0]
|
|
if att.URL != "/data/screenshot.png" {
|
|
t.Fatalf("unexpected local attachment url: %q", att.URL)
|
|
}
|
|
if att.Type != channel.AttachmentImage {
|
|
t.Fatalf("unexpected local attachment type: %q", att.Type)
|
|
}
|
|
}
|
|
|
|
func TestSendDirectPromotesDataPathAttachmentToContentHash(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
sender := &testSender{}
|
|
assets := &testAssetResolver{}
|
|
exec := &Executor{
|
|
Sender: sender,
|
|
Resolver: testResolver{},
|
|
AssetResolver: assets,
|
|
}
|
|
|
|
session := SessionContext{
|
|
BotID: "bot_1",
|
|
CurrentPlatform: "feishu",
|
|
ReplyTarget: "chat_id:oc_group_1",
|
|
}
|
|
|
|
_, err := exec.SendDirect(context.Background(), session, "", map[string]any{
|
|
"attachments": []any{"screenshot.png"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SendDirect returned error: %v", err)
|
|
}
|
|
if sender.called != 1 {
|
|
t.Fatalf("expected sender called once, got %d", sender.called)
|
|
}
|
|
if assets.ingestCalled != 1 || assets.lastPath != "/data/screenshot.png" {
|
|
t.Fatalf("expected ingest called with /data path, got called=%d path=%q", assets.ingestCalled, assets.lastPath)
|
|
}
|
|
if len(sender.req.Message.Attachments) != 1 {
|
|
t.Fatalf("expected one attachment, got %d", len(sender.req.Message.Attachments))
|
|
}
|
|
att := sender.req.Message.Attachments[0]
|
|
if att.ContentHash != "hash_1" {
|
|
t.Fatalf("expected promoted content hash, got %q", att.ContentHash)
|
|
}
|
|
if att.URL != "https://example.com/media/hash_1" {
|
|
t.Fatalf("expected public access URL after promotion, got %q", att.URL)
|
|
}
|
|
}
|