Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Accept Telegram/Qt clipboard images without a PNG/JPEG suffix

justrachf75471ffd1eb
diff --git a/src/clipboard_edge_tests.zig b/src/clipboard_edge_tests.zig
index 25e84722..326b9864 100644
--- a/src/clipboard_edge_tests.zig
+++ b/src/clipboard_edge_tests.zig
@@ -26,7 +26,13 @@ const setup =
\\ else if(kind==='unsupported') put(['org.example.clipboard-edge-unsupported'],[png]);
\\ else if(kind==='unsupported-text') put(['public.html','public.utf8-plain-text'],['<b>fixture</b>','fixture']);
\\ else if(kind==='remote') put(['public.file-url'],['https://example.invalid/image.png']);
- \\ else {
+ \\ else if(kind==='filenames'||kind==='filenames-alternate') {
+ \\ if(!png.writeToFileAtomically(argv[2],true)) throw Error('fixture file write failed');
+ \\ const declared=kind==='filenames-alternate'?['public.tiff','NSFilenamesPboardType']:['NSFilenamesPboardType'];
+ \\ pb.declareTypesOwner($(declared),null);
+ \\ if(kind==='filenames-alternate' && !pb.setDataForType(broken,'public.tiff')) throw Error('fixture flavor write failed');
+ \\ if(!pb.setPropertyListForType([argv[2]],'NSFilenamesPboardType')) throw Error('fixture flavor write failed');
+ \\ } else {
\\ if(kind!=='missing') {
\\ const data=kind==='nonimage'||kind==='nonimage-png'?broken:png;
\\ if(!data.writeToFileAtomically(argv[2],true)) throw Error('fixture file write failed');
@@ -68,10 +74,13 @@ test "#883 named pasteboard edge cases mixed flavors alternate images file URLs
.{ .kind = "corrupt-text", .expected = .convert },
.{ .kind = "file", .ext = "space # percent% quote' double\" café 雪.PNG", .expected = .furl, .source_png = true },
.{ .kind = "file-alternate", .expected = .furl, .source_png = true },
+ .{ .kind = "file", .ext = "cache", .expected = .furl, .source_png = true },
+ .{ .kind = "filenames", .expected = .furl, .source_png = true },
+ .{ .kind = "filenames-alternate", .expected = .furl, .source_png = true },
.{ .kind = "missing", .expected = .convert },
.{ .kind = "nonimage", .ext = "txt", .expected = .empty },
.{ .kind = "nonimage-png", .expected = .convert },
- .{ .kind = "unsupported-file", .ext = "unsupported", .expected = .empty, .source_png = true },
+ .{ .kind = "unsupported-file", .ext = "unsupported", .expected = .furl, .source_png = true },
.{ .kind = "remote", .expected = .empty },
.{ .kind = "unsupported", .expected = .empty },
.{ .kind = "unsupported-text", .expected = .empty },
@@ -95,7 +104,16 @@ test "#883 named pasteboard edge cases mixed flavors alternate images file URLs
.png => got == .ok and got.ok.flavor == .png,
.furl => got == .ok and got.ok.flavor == .furl,
};
- if (!matches) std.debug.print("clipboard edge case {s}: expected {s}, got {s}\n", .{ case.kind, @tagName(case.expected), @tagName(got) });
+ if (!matches) std.debug.print("clipboard edge case {s}: expected {s}, got {s} {s}\n", .{
+ case.kind,
+ @tagName(case.expected),
+ @tagName(got),
+ switch (got) {
+ .failed => |k| @tagName(k),
+ .ok => |g| g.flavor.name(),
+ else => "",
+ },
+ });
try std.testing.expect(matches);
if (got == .ok) {
try std.testing.expect(got.ok.owned);
diff --git a/src/clipboard_export.js b/src/clipboard_export.js
index e20275d8..fff095d2 100644
--- a/src/clipboard_export.js
+++ b/src/clipboard_export.js
@@ -1,41 +1,98 @@
// Runs under the system JavaScript for Automation runtime; no application
// activation, Apple Events to another app, or shell interpolation is needed.
ObjC.import('AppKit');
+
+function jsString(value) {
+ if (!value) return '';
+ if (typeof value === 'string') return value;
+ if (value.js) return String(value.js);
+ const unwrapped = ObjC.unwrap(value);
+ return typeof unwrapped === 'string' ? unwrapped : '';
+}
+
+function isTrue(flag) {
+ if (flag === true || flag === 1) return true;
+ if (!flag) return false;
+ const unwrapped = ObjC.unwrap(flag);
+ return unwrapped === true || unwrapped === 1;
+}
+
+function asFileURL(value) {
+ const raw = jsString(value);
+ if (!raw) return null;
+ // Remote http(s) file-url flavors must stay empty, not a network fetch.
+ if (/^[a-z][a-z0-9+.-]*:/i.test(raw) && !/^file:/i.test(raw)) return null;
+ const url = /^file:/i.test(raw) ? $.NSURL.URLWithString(raw) : $.NSURL.fileURLWithPath(raw);
+ if (!url || !isTrue(url.isFileURL)) return null;
+ if (isTrue(url.isFileReferenceURL) && url.filePathURL) return url.filePathURL;
+ return url;
+}
+
+// Telegram/Qt cache files often have no extension. Skip only obvious
+// non-images so a copied .txt stays empty instead of convert.
+function skipPath(url) {
+ const ext = (ObjC.unwrap(url.pathExtension) || '').toLowerCase();
+ return /^(txt|html?|json|xml|csv|zip|tar|gz|mp4|mov|m4[av]|mkv|mp3|wav|aiff|docx?|xlsx?|pptx?|ics|vcf|webloc)$/.test(ext);
+}
+
+function pngFromData(data) {
+ if (!data || !data.length) return null;
+ const img = $.NSImage.alloc.initWithData(data);
+ if (!img || !img.isValid) return null;
+ const bitmap = $.NSBitmapImageRep.imageRepWithData(img.TIFFRepresentation);
+ if (!bitmap || !bitmap.pixelsWide || !bitmap.pixelsHigh) return null;
+ const png = bitmap.representationUsingTypeProperties($.NSBitmapImageFileTypePNG, $({}));
+ return png && png.length ? png : null;
+}
+
function run(argv) {
const pb = argv[1] ? $.NSPasteboard.pasteboardWithName(argv[1]) : $.NSPasteboard.generalPasteboard;
const revision = pb.changeCount;
const types = ObjC.deepUnwrap(pb.types) || [];
- const raster = ['public.png', 'public.tiff', 'public.jpeg', 'public.heic', 'com.compuserve.gif', 'public.webp'];
let sawImage = false;
- for (const type of raster.concat(['com.adobe.pdf', 'public.file-url'])) {
- if (types.indexOf(type) < 0) continue;
- let data;
- let flavor = type === 'com.adobe.pdf' ? 'pdf' : 'png';
- if (type === 'public.file-url') {
- const value = pb.stringForType(type);
- if (!value || !value.js) continue;
- const url = $.NSURL.URLWithString(value);
- if (!url.isFileURL) continue;
- const ext = (ObjC.unwrap(url.pathExtension) || '').toLowerCase();
- if (!/^(png|jpe?g|gif|webp|tiff?|heic|bmp|pdf)$/.test(ext)) continue;
- sawImage = true;
- flavor = 'furl';
- data = $.NSData.dataWithContentsOfURL(url);
- } else {
- sawImage = true;
- data = pb.dataForType(type);
- }
- if (!data || !data.length) continue;
- const img = $.NSImage.alloc.initWithData(data);
- if (!img || !img.isValid) continue;
- const bitmap = $.NSBitmapImageRep.imageRepWithData(img.TIFFRepresentation);
- if (!bitmap || !bitmap.pixelsWide || !bitmap.pixelsHigh) continue;
- const png = bitmap.representationUsingTypeProperties($.NSBitmapImageFileTypePNG, $({}));
- if (!png || !png.length) continue;
+
+ function finish(png, flavor) {
if (pb.changeCount !== revision) return 'changed';
if (!png.writeToFileAtomically(argv[0], true)) return 'extract';
return 'ok:' + flavor;
}
+
+ // Files first: Telegram/Slack/Finder copy a real path (sometimes without
+ // an image suffix) and may also advertise a dummy TIFF/icon.
+ const seen = {};
+ const files = [];
+ function addFile(url, mustExist) {
+ if (!url || skipPath(url)) return;
+ const path = jsString(url.path);
+ if (!path || seen[path]) return;
+ if (mustExist && !$.NSFileManager.defaultManager.fileExistsAtPath(path)) return;
+ seen[path] = true;
+ files.push(url);
+ }
+ if (types.indexOf('public.file-url') >= 0) {
+ const value = pb.stringForType('public.file-url');
+ const raw = value && value.js ? String(value.js) : jsString(value);
+ if (/^file:/i.test(raw)) addFile(asFileURL(value), false);
+ }
+ if (types.indexOf('NSFilenamesPboardType') >= 0) {
+ const names = ObjC.deepUnwrap(pb.propertyListForType('NSFilenamesPboardType')) || [];
+ if (Array.isArray(names) && names.length && !/^[a-z][a-z0-9+.-]*:/i.test(String(names[0])))
+ addFile($.NSURL.fileURLWithPath(String(names[0])), true);
+ }
+
+ for (const url of files) {
+ sawImage = true;
+ const png = pngFromData($.NSData.dataWithContentsOfURL(url));
+ if (png) return finish(png, 'furl');
+ }
+
+ const raster = ['public.png', 'public.tiff', 'public.jpeg', 'public.heic', 'com.compuserve.gif', 'public.webp', 'com.adobe.pdf'];
+ for (const type of raster) {
+ if (types.indexOf(type) < 0) continue;
+ sawImage = true;
+ const png = pngFromData(pb.dataForType(type));
+ if (png) return finish(png, type === 'com.adobe.pdf' ? 'pdf' : 'png');
+ }
if (pb.changeCount !== revision) return 'changed';
return sawImage ? 'convert' : 'empty';
}
diff --git a/src/clipboard_native.zig b/src/clipboard_native.zig
index f99f9abf..53cff11c 100644
--- a/src/clipboard_native.zig
+++ b/src/clipboard_native.zig
@@ -1,4 +1,5 @@
//! Direct pasteboard flavors avoid AppleScript's lossy PNGf coercion (#843).
+//! Telegram/Qt copies a cache path (`NSFilenamesPboardType`), often without an image suffix.
const std = @import("std");
const clip = @import("vision_clipboard.zig");
const runner = @import("process_runner.zig");
diff --git a/src/vision_clipboard.zig b/src/vision_clipboard.zig
index 51cae866..de1a3a58 100644
--- a/src/vision_clipboard.zig
+++ b/src/vision_clipboard.zig
@@ -38,8 +38,9 @@ pub const Flavor = enum {
/// (TIFF, JPEG, HEIC, live data-provider promises), so it covers almost
/// every real screenshot/copy-image clipboard.
png,
- /// `«class furl»` — a file the user copied in Finder, Telegram, Slack…
- /// The pasteboard carries only the URL, never pixels.
+ /// `«class furl»` / `NSFilenamesPboardType` — a file the user copied in
+ /// Finder, Telegram, Slack… The pasteboard carries a path, never pixels.
+ /// Telegram cache files often have no image extension.
furl,
/// `«class PDF »` — Preview, Illustrator, Keynote and friends put up a
/// vector-only clipboard with no raster flavor at all.