Files
Memoh/internal/channel/channeltest/store.go
T
BBQ d3bf6bc90a fix(channel,attachment): channel quality refactor & attachment pipeline fixes (#349)
* 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)
2026-04-09 14:36:11 +08:00

182 lines
5.0 KiB
Go

package channeltest
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"mime"
"path/filepath"
"strings"
"sync"
"github.com/memohai/memoh/internal/media"
)
type storedAsset struct {
asset media.Asset
data []byte
}
type storedContainerFile struct {
botID string
path string
data []byte
mime string
name string
}
// MemoryAttachmentStore is a lightweight in-memory store for attachment preparation tests.
type MemoryAttachmentStore struct {
mu sync.Mutex
assets map[string]storedAsset
storageIndex map[string]string
containerFiles map[string]storedContainerFile
}
func NewMemoryAttachmentStore() *MemoryAttachmentStore {
return &MemoryAttachmentStore{
assets: make(map[string]storedAsset),
storageIndex: make(map[string]string),
containerFiles: make(map[string]storedContainerFile),
}
}
func (s *MemoryAttachmentStore) Stat(_ context.Context, botID, contentHash string) (media.Asset, error) {
s.mu.Lock()
defer s.mu.Unlock()
stored, ok := s.assets[s.assetKey(botID, contentHash)]
if !ok {
return media.Asset{}, media.ErrAssetNotFound
}
return stored.asset, nil
}
func (s *MemoryAttachmentStore) Open(_ context.Context, botID, contentHash string) (io.ReadCloser, media.Asset, error) {
s.mu.Lock()
defer s.mu.Unlock()
stored, ok := s.assets[s.assetKey(botID, contentHash)]
if !ok {
return nil, media.Asset{}, media.ErrAssetNotFound
}
return io.NopCloser(bytes.NewReader(stored.data)), stored.asset, nil
}
func (s *MemoryAttachmentStore) Ingest(_ context.Context, input media.IngestInput) (media.Asset, error) {
if strings.TrimSpace(input.BotID) == "" {
return media.Asset{}, errors.New("bot id is required")
}
if input.Reader == nil {
return media.Asset{}, errors.New("reader is required")
}
maxBytes := input.MaxBytes
if maxBytes <= 0 {
maxBytes = media.MaxAssetBytes
}
data, err := media.ReadAllWithLimit(input.Reader, maxBytes)
if err != nil {
return media.Asset{}, err
}
return s.ingestBytes(input.BotID, data, input.Mime, input.OriginalExt)
}
func (s *MemoryAttachmentStore) GetByStorageKey(_ context.Context, botID, storageKey string) (media.Asset, error) {
s.mu.Lock()
defer s.mu.Unlock()
contentHash, ok := s.storageIndex[s.storageKey(botID, storageKey)]
if !ok {
return media.Asset{}, media.ErrAssetNotFound
}
stored, ok := s.assets[s.assetKey(botID, contentHash)]
if !ok {
return media.Asset{}, media.ErrAssetNotFound
}
return stored.asset, nil
}
func (*MemoryAttachmentStore) AccessPath(asset media.Asset) string {
return "/data/media/" + strings.TrimSpace(asset.StorageKey)
}
func (s *MemoryAttachmentStore) IngestContainerFile(_ context.Context, botID, containerPath string) (media.Asset, error) {
s.mu.Lock()
containerFile, ok := s.containerFiles[s.containerKey(botID, containerPath)]
s.mu.Unlock()
if !ok {
return media.Asset{}, media.ErrAssetNotFound
}
return s.ingestBytes(containerFile.botID, containerFile.data, containerFile.mime, filepath.Ext(containerFile.name))
}
func (s *MemoryAttachmentStore) SeedAsset(botID string, data []byte, mimeType, originalExt string) (media.Asset, error) {
return s.ingestBytes(botID, data, mimeType, originalExt)
}
func (s *MemoryAttachmentStore) SeedContainerFile(botID, containerPath string, data []byte, mimeType, name string) {
s.mu.Lock()
defer s.mu.Unlock()
s.containerFiles[s.containerKey(botID, containerPath)] = storedContainerFile{
botID: botID,
path: containerPath,
data: append([]byte(nil), data...),
mime: strings.TrimSpace(mimeType),
name: strings.TrimSpace(name),
}
}
func (s *MemoryAttachmentStore) ingestBytes(botID string, data []byte, mimeType, originalExt string) (media.Asset, error) {
sum := sha256.Sum256(data)
contentHash := hex.EncodeToString(sum[:])
ext := strings.TrimSpace(originalExt)
if ext == "" {
ext = firstExtension(strings.TrimSpace(mimeType))
}
if ext == "" {
ext = ".bin"
}
mimeType = strings.TrimSpace(mimeType)
if mimeType == "" {
mimeType = "application/octet-stream"
}
asset := media.Asset{
ContentHash: contentHash,
BotID: botID,
Mime: mimeType,
SizeBytes: int64(len(data)),
StorageKey: contentHash[:2] + "/" + contentHash + ext,
}
s.mu.Lock()
defer s.mu.Unlock()
s.assets[s.assetKey(botID, contentHash)] = storedAsset{
asset: asset,
data: append([]byte(nil), data...),
}
s.storageIndex[s.storageKey(botID, asset.StorageKey)] = contentHash
return asset, nil
}
func (*MemoryAttachmentStore) assetKey(botID, contentHash string) string {
return strings.TrimSpace(botID) + ":" + strings.TrimSpace(contentHash)
}
func (*MemoryAttachmentStore) storageKey(botID, storageKey string) string {
return strings.TrimSpace(botID) + ":" + strings.TrimSpace(storageKey)
}
func (*MemoryAttachmentStore) containerKey(botID, containerPath string) string {
return strings.TrimSpace(botID) + ":" + strings.TrimSpace(containerPath)
}
func firstExtension(mimeType string) string {
if mimeType == "" {
return ""
}
exts, err := mime.ExtensionsByType(mimeType)
if err != nil || len(exts) == 0 {
return ""
}
return exts[0]
}