readImageFile reads an image file and returns it as base64-encoded image content. The caller must ensure the file exists (e.g. via os.Stat) before calling this method.
(resolvedPath, originalPath string)
| 1034 | // readImageFile reads an image file and returns it as base64-encoded image content. |
| 1035 | // The caller must ensure the file exists (e.g. via os.Stat) before calling this method. |
| 1036 | func (t *ToolSet) readImageFile(resolvedPath, originalPath string) (*tools.ToolCallResult, error) { |
| 1037 | data, err := t.readFile(resolvedPath) |
| 1038 | if err != nil { |
| 1039 | errMsg := err.Error() |
| 1040 | return &tools.ToolCallResult{ |
| 1041 | Output: errMsg, |
| 1042 | IsError: true, |
| 1043 | Meta: ReadFileMeta{ |
| 1044 | Error: errMsg, |
| 1045 | }, |
| 1046 | }, nil |
| 1047 | } |
| 1048 | |
| 1049 | mimeType := chat.DetectMimeType(resolvedPath) |
| 1050 | |
| 1051 | // Resize the image if it exceeds provider limits (max 2000×2000, max 4.5MB). |
| 1052 | resized, err := chat.ResizeImage(data, mimeType) |
| 1053 | if err != nil { |
| 1054 | // Check if the original exceeds limits before falling back |
| 1055 | if len(data) > chat.MaxImageBytes { |
| 1056 | return &tools.ToolCallResult{ |
| 1057 | Output: fmt.Sprintf("Error: Image file too large (%d bytes, max %d bytes)", len(data), chat.MaxImageBytes), |
| 1058 | IsError: true, |
| 1059 | Meta: ReadFileMeta{Path: originalPath, Error: "image too large"}, |
| 1060 | }, nil |
| 1061 | } |
| 1062 | // Original is within limits, proceed with fallback |
| 1063 | slog.Warn("Image resize failed, sending original (within limits)", "path", originalPath, "error", err) |
| 1064 | encoded := base64.StdEncoding.EncodeToString(data) |
| 1065 | return &tools.ToolCallResult{ |
| 1066 | Output: fmt.Sprintf("Read image file %s [%s] (%d bytes)", originalPath, mimeType, len(data)), |
| 1067 | Images: []tools.ImageContent{{Data: encoded, MimeType: mimeType}}, |
| 1068 | Meta: ReadFileMeta{Path: originalPath}, |
| 1069 | }, nil |
| 1070 | } |
| 1071 | |
| 1072 | encoded := base64.StdEncoding.EncodeToString(resized.Data) |
| 1073 | output := fmt.Sprintf("Read image file %s [%s] (%d bytes)", originalPath, resized.MimeType, len(resized.Data)) |
| 1074 | if note := chat.FormatDimensionNote(resized); note != "" { |
| 1075 | output += "\n" + note |
| 1076 | } |
| 1077 | |
| 1078 | return &tools.ToolCallResult{ |
| 1079 | Output: output, |
| 1080 | Images: []tools.ImageContent{{Data: encoded, MimeType: resized.MimeType}}, |
| 1081 | Meta: ReadFileMeta{Path: originalPath}, |
| 1082 | }, nil |
| 1083 | } |
| 1084 | |
| 1085 | func (t *ToolSet) handleReadMultipleFiles(ctx context.Context, args ReadMultipleFilesArgs) (*tools.ToolCallResult, error) { |
| 1086 | annotateFilesystemSpan(ctx, "read_multiple_files", "") |
no test coverage detected