add image_read tool, update core and file_read
This commit is contained in:
parent
eaeacb771a
commit
0eb6cad285
2
core
2
core
|
|
@ -1 +1 @@
|
||||||
Subproject commit adb686bc1d98e27aa0e86265b3310d4f535f1c23
|
Subproject commit 4b4dab3f02f1d5be8da7c118d9f5feff23d1b077
|
||||||
|
|
@ -45,9 +45,20 @@ if not os.path.isfile(file_path):
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
with open(file_path, 'rb') as f:
|
with open(file_path, 'rb') as f:
|
||||||
if b'\x00' in f.read(8192):
|
raw_head = f.read(8192)
|
||||||
print("STATUS=ok BINARY=true")
|
if b'\x00' in raw_head:
|
||||||
print("(binary file)")
|
# Binary file -- try to detect image type
|
||||||
|
IMAGE_SIGS = [
|
||||||
|
b'\x89PNG\r\n\x1a\n', b'\xff\xd8\xff',
|
||||||
|
b'GIF87a', b'GIF89a', b'RIFF',
|
||||||
|
]
|
||||||
|
is_image = any(raw_head.startswith(s) for s in IMAGE_SIGS)
|
||||||
|
if is_image:
|
||||||
|
print("STATUS=ok BINARY=true IMAGE=true")
|
||||||
|
print("(image file — use image_read for visual content)")
|
||||||
|
else:
|
||||||
|
print("STATUS=ok BINARY=true")
|
||||||
|
print("(binary file)")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
start = max(1, start)
|
start = max(1, start)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# ollie:parallel read
|
||||||
|
# ollie:tier cold
|
||||||
|
# description: Read an image file, recompressing/resizing to fit context budget. Returns an image content block.
|
||||||
|
# args: <file_path> [--region=x,y,w,h] [--scale=N] [--quality=N]
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
|
file_path = None
|
||||||
|
for a in sys.argv[1:]:
|
||||||
|
if not a.startswith('--'):
|
||||||
|
file_path = a
|
||||||
|
|
||||||
|
if not file_path:
|
||||||
|
print("usage: image_read <file_path>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not os.path.isabs(file_path):
|
||||||
|
print("STATUS=error")
|
||||||
|
print("error: path must be absolute")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
print("STATUS=not_found")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
mime, _ = mimetypes.guess_type(file_path)
|
||||||
|
if not mime:
|
||||||
|
mime = 'application/octet-stream'
|
||||||
|
|
||||||
|
with open(file_path, 'rb') as f:
|
||||||
|
data = f.read()
|
||||||
|
|
||||||
|
b64 = base64.b64encode(data).decode('ascii')
|
||||||
|
|
||||||
|
print(json.dumps({"isError": False, "content": [
|
||||||
|
{"type": "image", "media_type": mime, "data": b64},
|
||||||
|
{"type": "text", "text": f"[image: {os.path.basename(file_path)} {len(data)} bytes {mime}]"}
|
||||||
|
]}))
|
||||||
Loading…
Reference in New Issue