feat: you can summary screen shot directly
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/mmmy/snapgo/internal/domain"
|
||||
"github.com/mmmy/snapgo/internal/infrastructure/clipboard"
|
||||
)
|
||||
|
||||
// VisionSummarizer describes a multimodal model capable of reading a public
|
||||
// image URL and returning a textual summary.
|
||||
type VisionSummarizer interface {
|
||||
SummarizeImage(ctx context.Context, prompt string, imageURL string) (string, error)
|
||||
}
|
||||
|
||||
// CaptureSummaryService wires screenshot bytes -> S3 public URL -> LLM
|
||||
// summary -> clipboard. It intentionally does not notify UI state itself:
|
||||
// the caller emits fine-grained "uploading / recognizing / done" progress.
|
||||
type CaptureSummaryService struct {
|
||||
Provider domain.OSSProvider
|
||||
Summarizer VisionSummarizer
|
||||
Clipboard clipboard.Writer
|
||||
Prompt string
|
||||
PathPrefix string
|
||||
}
|
||||
|
||||
// UploadImage uploads the screenshot and returns the public URL visible to
|
||||
// the multimodal provider.
|
||||
func (s *CaptureSummaryService) UploadImage(ctx context.Context, pngBytes []byte) (*domain.UploadResult, error) {
|
||||
if s.Provider == nil {
|
||||
return nil, fmt.Errorf("s3 is not configured")
|
||||
}
|
||||
if len(pngBytes) == 0 {
|
||||
return nil, fmt.Errorf("empty screenshot")
|
||||
}
|
||||
key := buildObjectKey(s.PathPrefix)
|
||||
start := time.Now()
|
||||
url, err := s.Provider.Upload(ctx, key, pngBytes, "image/png")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 put object: %w", err)
|
||||
}
|
||||
return &domain.UploadResult{
|
||||
URL: url,
|
||||
Key: key,
|
||||
Provider: s.Provider.Name(),
|
||||
Elapsed: time.Since(start),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Summarize asks the configured model to summarize the uploaded image URL.
|
||||
func (s *CaptureSummaryService) Summarize(ctx context.Context, imageURL string) (string, error) {
|
||||
if s.Summarizer == nil {
|
||||
return "", fmt.Errorf("llm is not configured")
|
||||
}
|
||||
return s.Summarizer.SummarizeImage(ctx, s.Prompt, imageURL)
|
||||
}
|
||||
|
||||
// CopySummary writes the final summary text to the clipboard.
|
||||
func (s *CaptureSummaryService) CopySummary(_ context.Context, summary string) error {
|
||||
if summary == "" {
|
||||
return fmt.Errorf("empty summary")
|
||||
}
|
||||
if s.Clipboard == nil {
|
||||
return fmt.Errorf("clipboard is not configured")
|
||||
}
|
||||
if err := s.Clipboard.WriteText(summary); err != nil {
|
||||
return fmt.Errorf("clipboard write failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeOSSProvider struct {
|
||||
key string
|
||||
data []byte
|
||||
contentType string
|
||||
url string
|
||||
}
|
||||
|
||||
func (f *fakeOSSProvider) Upload(_ context.Context, key string, data []byte, contentType string) (string, error) {
|
||||
f.key = key
|
||||
f.data = append([]byte(nil), data...)
|
||||
f.contentType = contentType
|
||||
if f.url == "" {
|
||||
return "https://cdn.example.com/" + key, nil
|
||||
}
|
||||
return f.url, nil
|
||||
}
|
||||
|
||||
func (f *fakeOSSProvider) Name() string { return "fake" }
|
||||
|
||||
type fakeSummarizer struct {
|
||||
prompt string
|
||||
url string
|
||||
text string
|
||||
}
|
||||
|
||||
func (f *fakeSummarizer) SummarizeImage(_ context.Context, prompt string, imageURL string) (string, error) {
|
||||
f.prompt = prompt
|
||||
f.url = imageURL
|
||||
if f.text == "" {
|
||||
return "summary text", nil
|
||||
}
|
||||
return f.text, nil
|
||||
}
|
||||
|
||||
func TestCaptureSummaryServiceUploadsSummarizesAndCopies(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
provider := &fakeOSSProvider{}
|
||||
summarizer := &fakeSummarizer{}
|
||||
clip := &fakeClipboard{}
|
||||
svc := &CaptureSummaryService{
|
||||
Provider: provider,
|
||||
Summarizer: summarizer,
|
||||
Clipboard: clip,
|
||||
Prompt: "describe screenshot",
|
||||
PathPrefix: "snapgo/",
|
||||
}
|
||||
|
||||
uploaded, err := svc.UploadImage(ctx, []byte("png"))
|
||||
if err != nil {
|
||||
t.Fatalf("upload image: %v", err)
|
||||
}
|
||||
if provider.contentType != "image/png" {
|
||||
t.Fatalf("expected image/png content type, got %q", provider.contentType)
|
||||
}
|
||||
if string(provider.data) != "png" {
|
||||
t.Fatalf("expected uploaded png bytes, got %q", string(provider.data))
|
||||
}
|
||||
|
||||
summary, err := svc.Summarize(ctx, uploaded.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("summarize: %v", err)
|
||||
}
|
||||
if summarizer.prompt != "describe screenshot" {
|
||||
t.Fatalf("expected prompt forwarded, got %q", summarizer.prompt)
|
||||
}
|
||||
if summarizer.url != uploaded.URL {
|
||||
t.Fatalf("expected uploaded url forwarded, got %q", summarizer.url)
|
||||
}
|
||||
if summary != "summary text" {
|
||||
t.Fatalf("expected summary text, got %q", summary)
|
||||
}
|
||||
|
||||
if err := svc.CopySummary(ctx, summary); err != nil {
|
||||
t.Fatalf("copy summary: %v", err)
|
||||
}
|
||||
if clip.text != "summary text" {
|
||||
t.Fatalf("expected summary copied, got %q", clip.text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user