返回 DeepSeek-Reasonix
viewimage.go
根目录 / internal / tool / builtin / viewimage.go
1 package builtin
2
3 import (
4 "context"
5 "encoding/base64"
6 "encoding/json"
7 "fmt"
8 "io"
9 "os"
10 "strings"
11
12 "reasonix/internal/attachment"
13 "reasonix/internal/tool"
14 )
15
16 // Bound encoded payload size consistently with MCP image results.
17 const viewImageMaxBytes = 3 << 20
18
19 type viewImage struct {
20 workDir string
21 paths *PathResolver
22 forbidRoots []string
23 }
24
25 func init() { tool.RegisterBuiltin(viewImage{}) }
26 func (viewImage) Name() string { return "view_image" }
27 func (viewImage) Description() string {
28 return "Read a local PNG, JPEG, GIF, or WebP image by path and return visual content through native vision or the configured image-understanding model. Use this for image paths instead of read_file. Maximum file size: 3 MiB; maximum dimensions: 40 million pixels."
29 }
30 func (viewImage) Schema() json.RawMessage {
31 return json.RawMessage(`{"type":"object","properties":{"path":{"type":"string","description":"Image file path"}},"required":["path"]}`)
32 }
33 func (viewImage) ReadOnly() bool { return true }
34 func (v viewImage) Execute(ctx context.Context, args json.RawMessage) (string, error) {
35 text, _, err := v.ExecuteWithImages(ctx, args)
36 if err == nil {
37 text += "\nVisual content requires a structured image channel and an image-capable model."
38 }
39 return text, err
40 }
41 func (v viewImage) ExecuteWithImages(ctx context.Context, args json.RawMessage) (string, []string, error) {
42 var p struct {
43 Path string `json:"path"`
44 }
45 if err := json.Unmarshal(args, &p); err != nil {
46 return "", nil, fmt.Errorf("invalid args: %w", err)
47 }
48 if strings.TrimSpace(p.Path) == "" {
49 return "", nil, fmt.Errorf("path is required")
50 }
51 if err := ctx.Err(); err != nil {
52 return "", nil, err
53 }
54 rp := resolveReadablePath(v.workDir, p.Path, v.paths)
55 if confineRead(v.forbidRoots, rp.Path) {
56 return "", nil, fmt.Errorf("read %s: file not found", rp.DisplayPath)
57 }
58 info, err := os.Stat(rp.Path)
59 if err != nil {
60 return "", nil, fmt.Errorf("read %s: %s", rp.DisplayPath, rp.ErrorText(err))
61 }
62 if !info.Mode().IsRegular() {
63 return "", nil, fmt.Errorf("%s is not a regular image file", rp.DisplayPath)
64 }
65 if info.Size() > viewImageMaxBytes {
66 return "", nil, fmt.Errorf("image exceeds 3 MiB limit")
67 }
68 f, err := os.Open(rp.Path)
69 if err != nil {
70 return "", nil, fmt.Errorf("read %s: %s", rp.DisplayPath, rp.ErrorText(err))
71 }
72 defer f.Close()
73 data, err := io.ReadAll(io.LimitReader(f, viewImageMaxBytes+1))
74 if err != nil {
75 return "", nil, fmt.Errorf("read %s: %s", rp.DisplayPath, rp.ErrorText(err))
76 }
77 if len(data) > viewImageMaxBytes {
78 return "", nil, fmt.Errorf("image exceeds 3 MiB limit")
79 }
80 if err := ctx.Err(); err != nil {
81 return "", nil, err
82 }
83 mime, width, height, err := attachment.ValidateImage(data, "", attachment.ViewImagePolicy())
84 if err != nil {
85 return "", nil, fmt.Errorf("invalid or unsupported image: %w", err)
86 }
87 return fmt.Sprintf("[image: %s, %dx%d] %s", mime, width, height, rp.DisplayPath), []string{"data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data)}, nil
88 }
89
89 lines GO