Gary Simmons
commited on
Commit
·
a52a5af
1
Parent(s):
2c918e5
refactor analyze_chess_image function to accept both file paths and raw bytes, improve error handling for file reading
Browse files- libs/chess/chess_tools.py +21 -3
libs/chess/chess_tools.py
CHANGED
|
@@ -178,20 +178,38 @@ def analyze_chess_position(position_text: str, max_moves: int = 8) -> str:
|
|
| 178 |
|
| 179 |
|
| 180 |
@tool
|
| 181 |
-
def analyze_chess_image(
|
| 182 |
"""
|
| 183 |
Best-effort: try to extract a FEN string from an image using OCR (if available),
|
| 184 |
and then analyze the position. If OCR is unavailable or fails, save the image to a
|
| 185 |
temporary file and return a helpful message describing how to use the `analyze_chess_position` tool.
|
| 186 |
|
| 187 |
Args:
|
| 188 |
-
|
| 189 |
|
| 190 |
Returns:
|
| 191 |
JSON string. If a FEN was found and parsed, returns the same output as `analyze_chess_position`.
|
| 192 |
Otherwise returns a helpful error/message and a temporary path where the image was saved.
|
| 193 |
"""
|
| 194 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
# Validate input early and return a clear error message if image bytes
|
| 196 |
# are missing. This prevents opaque failures later when attempting
|
| 197 |
# to open or process the bytes.
|
|
@@ -199,7 +217,7 @@ def analyze_chess_image(image_bytes: bytes) -> str:
|
|
| 199 |
return json.dumps(
|
| 200 |
{
|
| 201 |
"status": "error",
|
| 202 |
-
"error": "
|
| 203 |
},
|
| 204 |
indent=2,
|
| 205 |
)
|
|
|
|
| 178 |
|
| 179 |
|
| 180 |
@tool
|
| 181 |
+
def analyze_chess_image(image_input: str) -> str:
|
| 182 |
"""
|
| 183 |
Best-effort: try to extract a FEN string from an image using OCR (if available),
|
| 184 |
and then analyze the position. If OCR is unavailable or fails, save the image to a
|
| 185 |
temporary file and return a helpful message describing how to use the `analyze_chess_position` tool.
|
| 186 |
|
| 187 |
Args:
|
| 188 |
+
image_input: Either a file path (string) to an image file, or raw image bytes
|
| 189 |
|
| 190 |
Returns:
|
| 191 |
JSON string. If a FEN was found and parsed, returns the same output as `analyze_chess_position`.
|
| 192 |
Otherwise returns a helpful error/message and a temporary path where the image was saved.
|
| 193 |
"""
|
| 194 |
try:
|
| 195 |
+
# Handle both file path (string) and bytes input
|
| 196 |
+
if isinstance(image_input, str):
|
| 197 |
+
# It's a file path
|
| 198 |
+
try:
|
| 199 |
+
with open(image_input, "rb") as f:
|
| 200 |
+
image_bytes = f.read()
|
| 201 |
+
except Exception as e:
|
| 202 |
+
return json.dumps(
|
| 203 |
+
{
|
| 204 |
+
"status": "error",
|
| 205 |
+
"error": f"Could not read image file: {str(e)}",
|
| 206 |
+
},
|
| 207 |
+
indent=2,
|
| 208 |
+
)
|
| 209 |
+
else:
|
| 210 |
+
# Assume it's bytes
|
| 211 |
+
image_bytes = image_input
|
| 212 |
+
|
| 213 |
# Validate input early and return a clear error message if image bytes
|
| 214 |
# are missing. This prevents opaque failures later when attempting
|
| 215 |
# to open or process the bytes.
|
|
|
|
| 217 |
return json.dumps(
|
| 218 |
{
|
| 219 |
"status": "error",
|
| 220 |
+
"error": "image_input was not provided or is empty",
|
| 221 |
},
|
| 222 |
indent=2,
|
| 223 |
)
|