Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

droppedFilePaths.ts

38 lines1.6 KB

1/**
2 * Files dropped from the OS that aren't images are handed to the agent by
3 * *path*, not by content: it can open the file itself, so a 40MB recording or
4 * a PDF never has to cross the wire as an attachment. This mirrors dropping a
5 * file into a terminal, where the shell receives the path.
6 *
7 * Paths are only available in the desktop app (Electron's `webUtils`); in a
8 * browser tab the File object carries no filesystem path at all.
9 */
10​
11/**
12 * Quote a path for the prompt when whitespace would make where it ends
13 * ambiguous. This is prompt text, not a shell command — the goal is a clear
14 * boundary for the reader, not shell-injection safety.
15 */
16export function quoteDroppedFilePath(path: string): string {
17 if (!/\s/.test(path)) {
18 return path;
19 }
20 // A double quote is legal in a POSIX filename, so escape any before wrapping —
21 // otherwise the first inner quote reads as the closing delimiter.
22 return `"${path.replace(/"/g, '\\"')}"`;
24​
25/**
26 * The text inserted into the composer for a set of dropped paths. Entries that
27 * are empty or all whitespace are skipped (a bridge that can't resolve a file
28 * returns null, which the caller filters, but be defensive about "" too).
29 * A path that survives is never altered: leading and trailing spaces are legal
30 * in a filename, so trimming one would point the agent at a file that does not
31 * exist. Quoting keeps such a path readable instead.
32 */
33export function formatDroppedFilePaths(paths: ReadonlyArray<string>): string {
34 return paths
35 .filter((path) => path.trim().length > 0)
36 .map(quoteDroppedFilePath)
37 .join(" ");