feat: unified attachment pipeline with clipboard image and file picker
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
d63af25fc1
commit
761a80004a
7 files changed
+597
-57
No files matched your search
@@ -0,0 +1,153 @@
|
||||
// clipboard.js - what the composer asks the system for: the clipboard, the
|
||||
// file picker, and the old files of the paste folder.
|
||||
//
|
||||
// Pure: no gpui-kit, no gpui-base, no herdr. The scripts are PowerShell text
|
||||
// (run by main.js with `exec("powershell", ["-NoProfile", "-STA", "-Command",
|
||||
// script])` - `-Command`, because running script files is off on this
|
||||
// machine), and the parsers read what the scripts print, so both halves are
|
||||
// tested in plain Node (tests/clipboard.test.mjs).
|
||||
//
|
||||
// The scripts print one fact per line, the fields separated by a tab:
|
||||
// FILES<TAB><path> (one line per file) - files copied in the Explorer
|
||||
// IMAGE<TAB><path> - an image, saved as a PNG
|
||||
// NONE - nothing to paste
|
||||
|
||||
/** How long a pasted image stays in the paste folder. */
|
||||
export const PASTE_MAX_AGE_MS = 7 * 864e5;
|
||||
|
||||
/** The programs that take an image (Global Constraints). */
|
||||
const IMAGE_APPS = new Set(["claude-code", "codex", "gemini", "opencode"]);
|
||||
|
||||
/**
|
||||
* The PowerShell that reads the clipboard: files first, then an image (saved
|
||||
* as `<outDir>\paste-<stamp>.png`), otherwise `NONE`.
|
||||
*
|
||||
* @param {string} outDir - where a pasted image is saved
|
||||
* @param {string} stamp - what makes the file name its own (a moment)
|
||||
* @returns {string}
|
||||
*/
|
||||
export function clipboardScript(outDir, stamp) {
|
||||
const dir = quote(outDir);
|
||||
const name = `paste-${String(stamp).replace(/[^\w-]/g, "")}.png`;
|
||||
|
||||
return [
|
||||
"Add-Type -AssemblyName System.Windows.Forms",
|
||||
"Add-Type -AssemblyName System.Drawing",
|
||||
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
|
||||
"$tab = [char]9",
|
||||
"$files = [System.Windows.Forms.Clipboard]::GetFileDropList()",
|
||||
"if ($files -ne $null -and $files.Count -gt 0) {",
|
||||
' foreach ($f in $files) { Write-Output ("FILES" + $tab + $f) }',
|
||||
" exit 0",
|
||||
"}",
|
||||
"$img = [System.Windows.Forms.Clipboard]::GetImage()",
|
||||
"if ($img -ne $null) {",
|
||||
` $dir = ${dir}`,
|
||||
" New-Item -ItemType Directory -Force -Path $dir | Out-Null",
|
||||
` $path = Join-Path $dir '${name}'`,
|
||||
" $img.Save($path, [System.Drawing.Imaging.ImageFormat]::Png)",
|
||||
' Write-Output ("IMAGE" + $tab + $path)',
|
||||
" exit 0",
|
||||
"}",
|
||||
"Write-Output 'NONE'",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* What the clipboard script printed.
|
||||
*
|
||||
* @param {{code?:number, stdout?:string, stderr?:string}|null} res
|
||||
* @returns {{kind:"image", paths:string[]} | {kind:"files", paths:string[]} | {kind:"none"} | {kind:"error", message:string}}
|
||||
*/
|
||||
export function parseClipboardResult(res) {
|
||||
if (!res || typeof res !== "object") return { kind: "error", message: "Brak wyniku z PowerShell" };
|
||||
|
||||
if (res.code !== 0) {
|
||||
const stderr = String(res.stderr ?? "").trim();
|
||||
return { kind: "error", message: stderr || `PowerShell zakończył się kodem ${res.code}` };
|
||||
}
|
||||
|
||||
const files = [];
|
||||
let image = "";
|
||||
for (const line of lines(res.stdout)) {
|
||||
const tab = line.indexOf("\t");
|
||||
if (tab < 0) continue;
|
||||
const kind = line.slice(0, tab);
|
||||
const path = line.slice(tab + 1).trim();
|
||||
if (path === "") continue;
|
||||
if (kind === "FILES") files.push(path);
|
||||
if (kind === "IMAGE" && image === "") image = path;
|
||||
}
|
||||
|
||||
if (files.length > 0) return { kind: "files", paths: files };
|
||||
if (image !== "") return { kind: "image", paths: [image] };
|
||||
return { kind: "none" };
|
||||
}
|
||||
|
||||
/**
|
||||
* The PowerShell that opens the file dialog (several files may be picked) and
|
||||
* prints one path per line. Cancel prints nothing.
|
||||
*
|
||||
* @returns {string}
|
||||
*/
|
||||
export function pickerScript() {
|
||||
return [
|
||||
"Add-Type -AssemblyName System.Windows.Forms",
|
||||
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
|
||||
"$dialog = New-Object System.Windows.Forms.OpenFileDialog",
|
||||
"$dialog.Multiselect = $true",
|
||||
"$dialog.Title = 'Wybierz pliki'",
|
||||
"if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {",
|
||||
" foreach ($f in $dialog.FileNames) { Write-Output $f }",
|
||||
"}",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* The paths the dialog printed; cancel, or a failed run, is no path at all.
|
||||
*
|
||||
* @param {{code?:number, stdout?:string}|null} res
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function parsePickerResult(res) {
|
||||
if (!res || typeof res !== "object" || res.code !== 0) return [];
|
||||
return lines(res.stdout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Which files of the paste folder are old enough to go.
|
||||
*
|
||||
* @param {{path:string, mtimeMs:number}[]} entries
|
||||
* @param {number} now
|
||||
* @param {number} [maxAgeMs] - 7 days by default
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function staleFiles(entries, now, maxAgeMs = PASTE_MAX_AGE_MS) {
|
||||
if (!Array.isArray(entries)) return [];
|
||||
return entries
|
||||
.filter((entry) => entry && Number.isFinite(entry.mtimeMs) && now - entry.mtimeMs > maxAgeMs)
|
||||
.map((entry) => entry.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the agent's program take an image?
|
||||
*
|
||||
* @param {string|null} app - `AgentInfo.app`
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function supportsImages(app) {
|
||||
return typeof app === "string" && IMAGE_APPS.has(app.trim());
|
||||
}
|
||||
|
||||
/** The non-empty lines of an output. */
|
||||
function lines(text) {
|
||||
return String(text ?? "")
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.replace(/\r$/, ""))
|
||||
.filter((line) => line.trim() !== "");
|
||||
}
|
||||
|
||||
/** A PowerShell single-quoted string. */
|
||||
function quote(text) {
|
||||
return `'${String(text).replace(/'/g, "''")}'`;
|
||||
}
|
||||
Reference in new issue
Block a user