* fix(feishu): fix image download with API fallback and post image support
- Add Image.Get API fallback when MessageResource.Get fails (different
permission scope: im:resource vs im:message:readonly)
- Extract and download images from post (rich text) messages
- Extract images from interactive card messages
- Deduplicate post image keys across locales
- Add comprehensive tests for new helpers
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat(media): add image path tags alongside base64 for LLM file access
Images are still base64-encoded into msg.Media for multimodal LLMs,
but now also get [image:path] tags injected into message content so
the LLM knows the local file path for save/forward operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor(media): only auto-inject images for tool results, not user messages
Channel-received images (role=user) now get path tags only, letting
the LLM decide whether to view via load_image or just operate on
the file. Tool result images (role=tool, e.g. load_image) are
base64-encoded into a synthetic user message appended after the tool
message, since many LLM APIs don't support image_url in tool messages.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix(media): preserve tool-message ordering for multi-tool-call scenarios
Move synthetic user message (carrying base64 tool images) to after the
entire contiguous tool-message block instead of immediately after each
tool message. This preserves the assistant→tool→tool ordering required
by OpenAI-compatible APIs.
Also fix load_image to use generic [image: photo] placeholder so
injectPathTags can properly replace it with the actual path.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix(test): update load_image test for [image: photo] placeholder
The test was checking ForLLM for the media:// ref, but load_image now
emits the generic [image: photo] placeholder instead.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix(media): match all channel image placeholders in injectPathTags
Different channels emit different placeholder formats — Telegram/Feishu
use [image: photo], WeCom/WeChat/Line use bare [image], QQ/Discord use
[image: <filename>]. The previous string-match code only handled
[image: photo], so for the other channels the path tag was appended as
a duplicate, producing content like "[image] [image:/path]".
Switch to per-type regex that matches all generic placeholder shapes
while leaving path tags ([image:/path]) untouched. Also fixes the same
issue for [audio], [video], [file] tags. Added test coverage for the
various placeholder shapes.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix(media): skip path tag append for JSON content (Feishu cards/posts)
When content is structured JSON (interactive cards, post messages),
injectPathTags now skips the fallback append — only placeholder
replacement is attempted. This prevents corrupting JSON payloads
like {"schema":"2.0",...} with appended [image:/path] tags.
Adds looksLikeJSON() helper and three test cases covering JSON
objects, arrays, and an end-to-end resolveMediaRefs scenario with
Feishu card content.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(media): prepend path tags for JSON content, narrow looksLikeJSON
Two fixes from code review:
1. looksLikeJSON now only checks for '{' prefix (not '['), avoiding
false positives on regular text like "[update] see attached".
2. For JSON content (Feishu cards/posts), path tags are prepended
before the JSON instead of being silently dropped. This ensures
the LLM can discover attached images via the path tag while the
JSON payload stays valid for downstream parsing.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
399 lines
10 KiB
Go
399 lines
10 KiB
Go
//go:build amd64 || arm64 || riscv64 || mips64 || ppc64
|
|
|
|
package feishu
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/channels"
|
|
)
|
|
|
|
func TestExtractContent(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
messageType string
|
|
rawContent string
|
|
want string
|
|
}{
|
|
{
|
|
name: "text message",
|
|
messageType: "text",
|
|
rawContent: `{"text": "hello world"}`,
|
|
want: "hello world",
|
|
},
|
|
{
|
|
name: "text message invalid JSON",
|
|
messageType: "text",
|
|
rawContent: `not json`,
|
|
want: "not json",
|
|
},
|
|
{
|
|
name: "post message returns raw JSON",
|
|
messageType: "post",
|
|
rawContent: `{"title": "test post"}`,
|
|
want: `{"title": "test post"}`,
|
|
},
|
|
{
|
|
name: "image message returns empty",
|
|
messageType: "image",
|
|
rawContent: `{"image_key": "img_xxx"}`,
|
|
want: "",
|
|
},
|
|
{
|
|
name: "file message with filename",
|
|
messageType: "file",
|
|
rawContent: `{"file_key": "file_xxx", "file_name": "report.pdf"}`,
|
|
want: "report.pdf",
|
|
},
|
|
{
|
|
name: "file message without filename",
|
|
messageType: "file",
|
|
rawContent: `{"file_key": "file_xxx"}`,
|
|
want: "",
|
|
},
|
|
{
|
|
name: "audio message with filename",
|
|
messageType: "audio",
|
|
rawContent: `{"file_key": "file_xxx", "file_name": "recording.ogg"}`,
|
|
want: "recording.ogg",
|
|
},
|
|
{
|
|
name: "media message with filename",
|
|
messageType: "media",
|
|
rawContent: `{"file_key": "file_xxx", "file_name": "video.mp4"}`,
|
|
want: "video.mp4",
|
|
},
|
|
{
|
|
name: "unknown message type returns raw",
|
|
messageType: "sticker",
|
|
rawContent: `{"sticker_id": "sticker_xxx"}`,
|
|
want: `{"sticker_id": "sticker_xxx"}`,
|
|
},
|
|
{
|
|
name: "empty raw content",
|
|
messageType: "text",
|
|
rawContent: "",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "interactive card returns raw JSON",
|
|
messageType: "interactive",
|
|
rawContent: `{"schema":"2.0","body":{"elements":[{"tag":"markdown","content":"Hello from card"}]}}`,
|
|
want: `{"schema":"2.0","body":{"elements":[{"tag":"markdown","content":"Hello from card"}]}}`,
|
|
},
|
|
{
|
|
name: "interactive card with complex structure returns raw JSON",
|
|
messageType: "interactive",
|
|
rawContent: `{"header":{"title":{"tag":"plain_text","content":"Title"}},"elements":[{"tag":"div","text":{"tag":"lark_md","content":"Card content"}}]}`,
|
|
want: `{"header":{"title":{"tag":"plain_text","content":"Title"}},"elements":[{"tag":"div","text":{"tag":"lark_md","content":"Card content"}}]}`,
|
|
},
|
|
{
|
|
name: "interactive card invalid JSON returns as-is",
|
|
messageType: "interactive",
|
|
rawContent: `not valid json`,
|
|
want: `not valid json`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := extractContent(tt.messageType, tt.rawContent)
|
|
if got != tt.want {
|
|
t.Errorf("extractContent(%q, %q) = %q, want %q", tt.messageType, tt.rawContent, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAppendMediaTags(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
content string
|
|
messageType string
|
|
mediaRefs []string
|
|
want string
|
|
}{
|
|
{
|
|
name: "no refs returns content unchanged",
|
|
content: "hello",
|
|
messageType: "image",
|
|
mediaRefs: nil,
|
|
want: "hello",
|
|
},
|
|
{
|
|
name: "empty refs returns content unchanged",
|
|
content: "hello",
|
|
messageType: "image",
|
|
mediaRefs: []string{},
|
|
want: "hello",
|
|
},
|
|
{
|
|
name: "image with content",
|
|
content: "check this",
|
|
messageType: "image",
|
|
mediaRefs: []string{"ref1"},
|
|
want: "check this [image: photo]",
|
|
},
|
|
{
|
|
name: "image empty content",
|
|
content: "",
|
|
messageType: "image",
|
|
mediaRefs: []string{"ref1"},
|
|
want: "[image: photo]",
|
|
},
|
|
{
|
|
name: "audio",
|
|
content: "listen",
|
|
messageType: "audio",
|
|
mediaRefs: []string{"ref1"},
|
|
want: "listen [audio]",
|
|
},
|
|
{
|
|
name: "media/video",
|
|
content: "watch",
|
|
messageType: "media",
|
|
mediaRefs: []string{"ref1"},
|
|
want: "watch [video]",
|
|
},
|
|
{
|
|
name: "file",
|
|
content: "report.pdf",
|
|
messageType: "file",
|
|
mediaRefs: []string{"ref1"},
|
|
want: "report.pdf [file]",
|
|
},
|
|
{
|
|
name: "unknown type",
|
|
content: "something",
|
|
messageType: "sticker",
|
|
mediaRefs: []string{"ref1"},
|
|
want: "something [attachment]",
|
|
},
|
|
{
|
|
name: "interactive card with images returns content unchanged",
|
|
content: `{"schema":"2.0","body":{"elements":[{"tag":"img","img_key":"img_123"}]}}`,
|
|
messageType: "interactive",
|
|
mediaRefs: []string{"ref1"},
|
|
want: `{"schema":"2.0","body":{"elements":[{"tag":"img","img_key":"img_123"}]}}`,
|
|
},
|
|
{
|
|
name: "post message with images returns content unchanged",
|
|
content: `{"zh_cn":{"title":"","content":[[{"tag":"img","image_key":"img_001"}]]}}`,
|
|
messageType: "post",
|
|
mediaRefs: []string{"ref1"},
|
|
want: `{"zh_cn":{"title":"","content":[[{"tag":"img","image_key":"img_001"}]]}}`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := appendMediaTags(tt.content, tt.messageType, tt.mediaRefs)
|
|
if got != tt.want {
|
|
t.Errorf(
|
|
"appendMediaTags(%q, %q, %v) = %q, want %q",
|
|
tt.content,
|
|
tt.messageType,
|
|
tt.mediaRefs,
|
|
got,
|
|
tt.want,
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractFeishuSenderID(t *testing.T) {
|
|
strPtr := func(s string) *string { return &s }
|
|
|
|
tests := []struct {
|
|
name string
|
|
sender *larkim.EventSender
|
|
want string
|
|
}{
|
|
{
|
|
name: "nil sender",
|
|
sender: nil,
|
|
want: "",
|
|
},
|
|
{
|
|
name: "nil sender ID",
|
|
sender: &larkim.EventSender{SenderId: nil},
|
|
want: "",
|
|
},
|
|
{
|
|
name: "userId preferred",
|
|
sender: &larkim.EventSender{
|
|
SenderId: &larkim.UserId{
|
|
UserId: strPtr("u_abc123"),
|
|
OpenId: strPtr("ou_def456"),
|
|
UnionId: strPtr("on_ghi789"),
|
|
},
|
|
},
|
|
want: "u_abc123",
|
|
},
|
|
{
|
|
name: "openId fallback",
|
|
sender: &larkim.EventSender{
|
|
SenderId: &larkim.UserId{
|
|
UserId: strPtr(""),
|
|
OpenId: strPtr("ou_def456"),
|
|
UnionId: strPtr("on_ghi789"),
|
|
},
|
|
},
|
|
want: "ou_def456",
|
|
},
|
|
{
|
|
name: "unionId fallback",
|
|
sender: &larkim.EventSender{
|
|
SenderId: &larkim.UserId{
|
|
UserId: strPtr(""),
|
|
OpenId: strPtr(""),
|
|
UnionId: strPtr("on_ghi789"),
|
|
},
|
|
},
|
|
want: "on_ghi789",
|
|
},
|
|
{
|
|
name: "all empty strings",
|
|
sender: &larkim.EventSender{
|
|
SenderId: &larkim.UserId{
|
|
UserId: strPtr(""),
|
|
OpenId: strPtr(""),
|
|
UnionId: strPtr(""),
|
|
},
|
|
},
|
|
want: "",
|
|
},
|
|
{
|
|
name: "nil userId pointer falls through",
|
|
sender: &larkim.EventSender{
|
|
SenderId: &larkim.UserId{
|
|
UserId: nil,
|
|
OpenId: strPtr("ou_def456"),
|
|
UnionId: nil,
|
|
},
|
|
},
|
|
want: "ou_def456",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := extractFeishuSenderID(tt.sender)
|
|
if got != tt.want {
|
|
t.Errorf("extractFeishuSenderID() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFinalizeTrackedToolFeedbackMessage_ClearAfterSuccessfulEdit(t *testing.T) {
|
|
ch := &FeishuChannel{
|
|
progress: channels.NewToolFeedbackAnimator(nil),
|
|
}
|
|
ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`")
|
|
|
|
msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage(
|
|
context.Background(),
|
|
"chat-1",
|
|
"final reply",
|
|
func(_ context.Context, chatID, messageID, content string) error {
|
|
if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" {
|
|
t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content)
|
|
}
|
|
return nil
|
|
},
|
|
)
|
|
if !handled {
|
|
t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message")
|
|
}
|
|
if len(msgIDs) != 1 || msgIDs[0] != "msg-1" {
|
|
t.Fatalf("unexpected msgIDs: %v", msgIDs)
|
|
}
|
|
if _, ok := ch.currentToolFeedbackMessage("chat-1"); ok {
|
|
t.Fatal("expected tracked tool feedback to be cleared after successful edit")
|
|
}
|
|
}
|
|
|
|
func TestFinalizeTrackedToolFeedbackMessage_StopsTrackingBeforeEdit(t *testing.T) {
|
|
ch := &FeishuChannel{
|
|
progress: channels.NewToolFeedbackAnimator(nil),
|
|
}
|
|
ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`")
|
|
|
|
msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage(
|
|
context.Background(),
|
|
"chat-1",
|
|
"final reply",
|
|
func(_ context.Context, chatID, messageID, content string) error {
|
|
if _, ok := ch.currentToolFeedbackMessage(chatID); ok {
|
|
t.Fatal("expected tracked tool feedback to be stopped before edit")
|
|
}
|
|
if chatID != "chat-1" || messageID != "msg-1" || content != "final reply" {
|
|
t.Fatalf("unexpected edit args: %s %s %s", chatID, messageID, content)
|
|
}
|
|
return nil
|
|
},
|
|
)
|
|
if !handled {
|
|
t.Fatal("expected finalizeTrackedToolFeedbackMessage to handle tracked message")
|
|
}
|
|
if len(msgIDs) != 1 || msgIDs[0] != "msg-1" {
|
|
t.Fatalf("unexpected msgIDs: %v", msgIDs)
|
|
}
|
|
}
|
|
|
|
func TestFinalizeTrackedToolFeedbackMessage_EditFailureKeepsTrackedMessage(t *testing.T) {
|
|
ch := &FeishuChannel{
|
|
progress: channels.NewToolFeedbackAnimator(nil),
|
|
}
|
|
ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`")
|
|
|
|
msgIDs, handled := ch.finalizeTrackedToolFeedbackMessage(
|
|
context.Background(),
|
|
"chat-1",
|
|
"final reply",
|
|
func(context.Context, string, string, string) error {
|
|
return errors.New("edit failed")
|
|
},
|
|
)
|
|
if handled {
|
|
t.Fatal("expected finalizeTrackedToolFeedbackMessage to report unhandled on edit failure")
|
|
}
|
|
if len(msgIDs) != 0 {
|
|
t.Fatalf("unexpected msgIDs: %v", msgIDs)
|
|
}
|
|
if msgID, ok := ch.currentToolFeedbackMessage("chat-1"); !ok || msgID != "msg-1" {
|
|
t.Fatalf("expected tracked tool feedback to remain after failed edit, got (%q, %v)", msgID, ok)
|
|
}
|
|
}
|
|
|
|
func TestResetTrackedToolFeedbackAfterEditFailure_DismissesTrackedMessage(t *testing.T) {
|
|
var (
|
|
deletedChatID string
|
|
deletedMsgID string
|
|
)
|
|
|
|
ch := &FeishuChannel{
|
|
progress: channels.NewToolFeedbackAnimator(nil),
|
|
deleteMessageFn: func(_ context.Context, chatID, messageID string) error {
|
|
deletedChatID = chatID
|
|
deletedMsgID = messageID
|
|
return nil
|
|
},
|
|
}
|
|
ch.RecordToolFeedbackMessage("chat-1", "msg-1", "🔧 `read_file`")
|
|
|
|
ch.resetTrackedToolFeedbackAfterEditFailure(context.Background(), "chat-1")
|
|
|
|
if deletedChatID != "chat-1" || deletedMsgID != "msg-1" {
|
|
t.Fatalf("unexpected delete target: chat=%q msg=%q", deletedChatID, deletedMsgID)
|
|
}
|
|
if _, ok := ch.currentToolFeedbackMessage("chat-1"); ok {
|
|
t.Fatal("expected tracked tool feedback to be cleared after edit failure reset")
|
|
}
|
|
}
|