mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
c368b5b359
* feat(feishu): implement SendMedia and add send_file tool Add outbound media support for the Feishu channel so the agent can send images and files to users via the MediaStore pipeline. Feishu channel: - SendMedia dispatches media parts as image or file uploads - sendImage uploads via Image.Create then sends image message - sendFile uploads via File.Create then sends file message - feishuFileType maps extensions to Feishu file_type values send_file tool: - New tool lets the LLM send a local file to the current chat - Validates path, registers file in MediaStore, returns media ref - Agent loop wires tool registration, MediaStore propagation, and context updates Tested on Radxa Cubie A7A (arm64) with Feishu websocket channel. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(agent): publish outbound media regardless of SendResponse flag The SendResponse flag controls whether the agent loop publishes the final text response (callers that publish it themselves set this to false). However, the media publish path was also gated behind this flag, which meant tool-produced media was silently dropped for normal channel messages. Media should be published immediately when a tool returns media refs, independent of how the text response is delivered. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(tools): use magic-bytes MIME detection and add file size limit to send_file - Replace hardcoded extension-to-MIME map with h2non/filetype (magic bytes) + mime.TypeByExtension fallback, consistent with the vision pipeline in resolveMediaRefs - Add configurable max file size check (defaults to config.DefaultMaxMediaSize, 20 MB) to prevent oversized uploads - Add tests for magic-bytes detection, extension fallback, size limit, and default max size Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor(agent): add ForEachTool to AgentRegistry for cross-agent tool lookup Extract the pattern of iterating agents to find a named tool into AgentRegistry.ForEachTool, simplifying SetMediaStore propagation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(agent,tools): adapt send_file to ctx-based channel injection after upstream refactor Replace ContextualTool interface (removed upstream) with direct ctx reading in SendFileTool.Execute, using ToolChannel/ToolChatID helpers. Remove updateToolContexts which is no longer needed since ExecuteWithContext already injects channel/chatID into ctx for all tools. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat(tools): support toggling send_file tool via config Add SendFileConfig with Enabled field to ToolsConfig, defaulting to true. Wrap send_file tool registration in loop.go with the config check, consistent with the pattern used by other tools. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
177 lines
4.9 KiB
Go
177 lines
4.9 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/sipeed/picoclaw/pkg/config"
|
|
"github.com/sipeed/picoclaw/pkg/media"
|
|
)
|
|
|
|
func TestSendFileTool_MissingPath(t *testing.T) {
|
|
store := media.NewFileMediaStore()
|
|
tool := NewSendFileTool("/tmp", false, 0, store)
|
|
tool.SetContext("feishu", "chat123")
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{})
|
|
if !result.IsError {
|
|
t.Fatal("expected error for missing path")
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_NoContext(t *testing.T) {
|
|
store := media.NewFileMediaStore()
|
|
tool := NewSendFileTool("/tmp", false, 0, store)
|
|
// no SetContext call
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{"path": "/tmp/test.txt"})
|
|
if !result.IsError {
|
|
t.Fatal("expected error when no channel context")
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_NoMediaStore(t *testing.T) {
|
|
tool := NewSendFileTool("/tmp", false, 0, nil)
|
|
tool.SetContext("feishu", "chat123")
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{"path": "/tmp/test.txt"})
|
|
if !result.IsError {
|
|
t.Fatal("expected error when no media store")
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_Directory(t *testing.T) {
|
|
store := media.NewFileMediaStore()
|
|
tool := NewSendFileTool("/tmp", false, 0, store)
|
|
tool.SetContext("feishu", "chat123")
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{"path": "/tmp"})
|
|
if !result.IsError {
|
|
t.Fatal("expected error for directory path")
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_FileTooLarge(t *testing.T) {
|
|
dir := t.TempDir()
|
|
testFile := filepath.Join(dir, "big.bin")
|
|
// Create a file larger than the limit
|
|
if err := os.WriteFile(testFile, make([]byte, 1024), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
store := media.NewFileMediaStore()
|
|
tool := NewSendFileTool(dir, false, 512, store) // 512 byte limit
|
|
tool.SetContext("feishu", "chat123")
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{"path": testFile})
|
|
if !result.IsError {
|
|
t.Fatal("expected error for oversized file")
|
|
}
|
|
if !strings.Contains(result.ForLLM, "too large") {
|
|
t.Errorf("expected 'too large' in error, got %q", result.ForLLM)
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_DefaultMaxSize(t *testing.T) {
|
|
tool := NewSendFileTool("/tmp", false, 0, nil)
|
|
if tool.maxFileSize != config.DefaultMaxMediaSize {
|
|
t.Errorf("expected default max size %d, got %d", config.DefaultMaxMediaSize, tool.maxFileSize)
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_Success(t *testing.T) {
|
|
dir := t.TempDir()
|
|
testFile := filepath.Join(dir, "photo.png")
|
|
if err := os.WriteFile(testFile, []byte("fake png"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
store := media.NewFileMediaStore()
|
|
tool := NewSendFileTool(dir, false, 0, store)
|
|
tool.SetContext("feishu", "chat123")
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{"path": testFile})
|
|
if result.IsError {
|
|
t.Fatalf("unexpected error: %s", result.ForLLM)
|
|
}
|
|
if len(result.Media) != 1 {
|
|
t.Fatalf("expected 1 media ref, got %d", len(result.Media))
|
|
}
|
|
if result.Media[0][:8] != "media://" {
|
|
t.Errorf("expected media:// ref, got %q", result.Media[0])
|
|
}
|
|
}
|
|
|
|
func TestSendFileTool_CustomFilename(t *testing.T) {
|
|
dir := t.TempDir()
|
|
testFile := filepath.Join(dir, "img.jpg")
|
|
if err := os.WriteFile(testFile, []byte("fake jpg"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
store := media.NewFileMediaStore()
|
|
tool := NewSendFileTool(dir, false, 0, store)
|
|
tool.SetContext("telegram", "chat456")
|
|
|
|
result := tool.Execute(context.Background(), map[string]any{
|
|
"path": testFile,
|
|
"filename": "my-photo.jpg",
|
|
})
|
|
if result.IsError {
|
|
t.Fatalf("unexpected error: %s", result.ForLLM)
|
|
}
|
|
if len(result.Media) != 1 {
|
|
t.Fatalf("expected 1 media ref, got %d", len(result.Media))
|
|
}
|
|
}
|
|
|
|
func TestDetectMediaType_MagicBytes(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
// Minimal valid PNG header
|
|
pngHeader := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
|
|
pngFile := filepath.Join(dir, "image.dat") // wrong extension, but valid PNG bytes
|
|
if err := os.WriteFile(pngFile, pngHeader, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got := detectMediaType(pngFile)
|
|
if got != "image/png" {
|
|
t.Errorf("expected image/png from magic bytes, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestDetectMediaType_FallbackToExtension(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
// File with unrecognizable content but known extension
|
|
txtFile := filepath.Join(dir, "readme.txt")
|
|
if err := os.WriteFile(txtFile, []byte("hello world"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got := detectMediaType(txtFile)
|
|
// text/plain or similar — just verify it's not application/octet-stream
|
|
if got == "application/octet-stream" {
|
|
t.Errorf("expected extension-based MIME for .txt, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestDetectMediaType_UnknownFallsToOctetStream(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
// File with no extension and random bytes
|
|
unknownFile := filepath.Join(dir, "mystery")
|
|
if err := os.WriteFile(unknownFile, []byte{0x00, 0x01, 0x02}, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got := detectMediaType(unknownFile)
|
|
if got != "application/octet-stream" {
|
|
t.Errorf("expected application/octet-stream, got %q", got)
|
|
}
|
|
}
|