@saadnvd1/agent-os 0.3.815 → 0.3.817
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/api/files/upload-temp/route.test.ts +55 -3
- package/app/api/files/upload-temp/route.ts +15 -10
- package/components/Chat/FileChip.tsx +8 -4
- package/components/Chat/composer/paste.ts +5 -2
- package/components/Chat/composer/useAttachFiles.ts +8 -5
- package/content/whats-new.json +8 -0
- package/lib/attachments/check.ts +2 -5
- package/lib/attachments/har.ts +122 -0
- package/lib/attachments/message.ts +24 -2
- package/lib/attachments/policy.ts +104 -9
- package/lib/attachments/prepare.ts +100 -0
- package/lib/attachments/store.ts +7 -1
- package/lib/config/client-config-types.ts +2 -0
- package/package.json +1 -1
|
@@ -68,7 +68,7 @@ describe("POST /api/files/upload-temp", () => {
|
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
it("refuses types it doesn't take, saying which", async () => {
|
|
71
|
-
for (const name of ["a.zip", "
|
|
71
|
+
for (const name of ["a.zip", "tool.exe", "noext", "clip.mov"]) {
|
|
72
72
|
const res = await upload(name, "x", { sessionId: SESSION });
|
|
73
73
|
expect(res.status).toBe(415);
|
|
74
74
|
expect((await res.json()).error).toContain(`${name}: this file type`);
|
|
@@ -105,11 +105,61 @@ describe("POST /api/files/upload-temp", () => {
|
|
|
105
105
|
|
|
106
106
|
it("refuses a file over the limit, and says what the limit is", async () => {
|
|
107
107
|
const big = Buffer.alloc(ATTACHMENT_LIMITS.maxBytes + 1, 97);
|
|
108
|
-
const res = await upload("big.
|
|
108
|
+
const res = await upload("big.csv", big, { sessionId: SESSION });
|
|
109
109
|
expect(res.status).toBe(413);
|
|
110
110
|
expect((await res.json()).error).toContain("the limit is 25.0 MB");
|
|
111
111
|
});
|
|
112
112
|
|
|
113
|
+
it("takes shell scripts, config and extensionless names as text", async () => {
|
|
114
|
+
for (const name of ["run.sh", "Dockerfile", ".env.example", "main.tf"]) {
|
|
115
|
+
const res = await upload(name, "echo hi\n", { sessionId: SESSION });
|
|
116
|
+
expect(res.status, name).toBe(200);
|
|
117
|
+
expect((await res.json()).kind).toBe("text");
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("names what is accepted when it refuses a type", async () => {
|
|
122
|
+
const res = await upload("a.zip", "x", { sessionId: SESSION });
|
|
123
|
+
expect((await res.json()).error).toMatch(/HAR.*HEIC|HEIC.*HAR/);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("redacts a HAR on the way in and keeps a summary beside it", async () => {
|
|
127
|
+
const secret = ["s", "3", "cr", "et"].join("") + Date.now();
|
|
128
|
+
const har = JSON.stringify({
|
|
129
|
+
log: {
|
|
130
|
+
entries: [
|
|
131
|
+
{
|
|
132
|
+
request: {
|
|
133
|
+
method: "GET",
|
|
134
|
+
url: "https://api.example.com/me",
|
|
135
|
+
headers: [{ name: "Authorization", value: `Bearer ${secret}` }],
|
|
136
|
+
cookies: [{ name: "sid", value: secret }],
|
|
137
|
+
},
|
|
138
|
+
response: { status: 500, headers: [], cookies: [] },
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
const res = await upload("trace.har", har, { sessionId: SESSION });
|
|
144
|
+
expect(res.status).toBe(200);
|
|
145
|
+
const body = await res.json();
|
|
146
|
+
expect(body.mediaType).toBe("application/json");
|
|
147
|
+
const saved = fs.readFileSync(body.path, "utf8");
|
|
148
|
+
expect(saved).not.toContain(secret);
|
|
149
|
+
expect(saved).toContain("api.example.com/me");
|
|
150
|
+
expect(fs.readFileSync(`${body.path}.summary.txt`, "utf8")).toContain(
|
|
151
|
+
"1 requests"
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("refuses a .har that isn't a HAR", async () => {
|
|
156
|
+
for (const data of ["not json", '{"log":{}}']) {
|
|
157
|
+
const res = await upload("x.har", data, { sessionId: SESSION });
|
|
158
|
+
expect(res.status).toBe(400);
|
|
159
|
+
expect((await res.json()).error).toContain("real HAR");
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
113
163
|
it("refuses an image over the image limit", async () => {
|
|
114
164
|
const big = Buffer.concat([
|
|
115
165
|
PNG,
|
|
@@ -122,7 +172,9 @@ describe("POST /api/files/upload-temp", () => {
|
|
|
122
172
|
const res = await POST(
|
|
123
173
|
new Request("http://localhost/x", {
|
|
124
174
|
method: "POST",
|
|
125
|
-
headers: {
|
|
175
|
+
headers: {
|
|
176
|
+
"content-length": String(ATTACHMENT_LIMITS.maxTextBytes * 2),
|
|
177
|
+
},
|
|
126
178
|
body: "{}",
|
|
127
179
|
})
|
|
128
180
|
);
|
|
@@ -4,11 +4,13 @@ import path from "path";
|
|
|
4
4
|
import os from "os";
|
|
5
5
|
import { checkUpload } from "@/lib/attachments/check";
|
|
6
6
|
import { ATTACHMENT_LIMITS, tooBigMessage } from "@/lib/attachments/policy";
|
|
7
|
+
import { prepareUpload } from "@/lib/attachments/prepare";
|
|
7
8
|
import { saveAttachment } from "@/lib/attachments/store";
|
|
8
9
|
import { peerChatLink } from "@/lib/chat/peer-relay";
|
|
9
10
|
|
|
10
11
|
// Base64 is a third bigger than the bytes, and the JSON around it is small.
|
|
11
|
-
const MAX_BODY =
|
|
12
|
+
const MAX_BODY =
|
|
13
|
+
Math.ceil((ATTACHMENT_LIMITS.maxTextBytes * 4) / 3) + 64 * 1024;
|
|
12
14
|
|
|
13
15
|
const fail = (error: string, status: number) =>
|
|
14
16
|
NextResponse.json({ error }, { status });
|
|
@@ -34,6 +36,8 @@ export async function POST(request: Request) {
|
|
|
34
36
|
const buffer = Buffer.from(base64, "base64");
|
|
35
37
|
const checked = checkUpload(name, buffer);
|
|
36
38
|
if (!checked.ok) return fail(checked.error, checked.status);
|
|
39
|
+
const prepared = await prepareUpload(checked.type, name, buffer);
|
|
40
|
+
if (!prepared.ok) return fail(prepared.error, prepared.status);
|
|
37
41
|
|
|
38
42
|
if (typeof sessionId === "string" && sessionId) {
|
|
39
43
|
if (sessionId !== "draft" && peerChatLink(sessionId))
|
|
@@ -43,25 +47,26 @@ export async function POST(request: Request) {
|
|
|
43
47
|
);
|
|
44
48
|
const file = saveAttachment(
|
|
45
49
|
sessionId,
|
|
46
|
-
name,
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
prepared.name,
|
|
51
|
+
prepared.data,
|
|
52
|
+
prepared.mediaType,
|
|
53
|
+
prepared.summary
|
|
49
54
|
);
|
|
50
|
-
return NextResponse.json({ ...file, kind:
|
|
55
|
+
return NextResponse.json({ ...file, kind: prepared.kind });
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
const tempDir = path.join(os.tmpdir(), "agent-os-screenshots");
|
|
54
59
|
await mkdir(tempDir, { recursive: true });
|
|
55
|
-
const safeName = name.replace(/[^a-zA-Z0-9.-]/g, "_");
|
|
60
|
+
const safeName = prepared.name.replace(/[^a-zA-Z0-9.-]/g, "_");
|
|
56
61
|
const filePath = path.join(tempDir, `${Date.now()}-${safeName}`);
|
|
57
|
-
await writeFile(filePath,
|
|
62
|
+
await writeFile(filePath, prepared.data);
|
|
58
63
|
|
|
59
64
|
return NextResponse.json({
|
|
60
65
|
path: filePath,
|
|
61
66
|
name: safeName,
|
|
62
|
-
size:
|
|
63
|
-
mediaType:
|
|
64
|
-
kind:
|
|
67
|
+
size: prepared.data.length,
|
|
68
|
+
mediaType: prepared.mediaType,
|
|
69
|
+
kind: prepared.kind,
|
|
65
70
|
});
|
|
66
71
|
} catch (error) {
|
|
67
72
|
console.error("Upload error:", error);
|
|
@@ -32,14 +32,18 @@ export function FileChip({
|
|
|
32
32
|
uploading?: boolean;
|
|
33
33
|
onRemove?: () => void;
|
|
34
34
|
}) {
|
|
35
|
-
const
|
|
35
|
+
const type = typeFor(file.name);
|
|
36
|
+
const Icon = ICONS[type?.kind ?? "text"];
|
|
37
|
+
const detail = uploading
|
|
38
|
+
? "Uploading…"
|
|
39
|
+
: [formatBytes(file.size), type?.note].filter(Boolean).join(" · ");
|
|
36
40
|
return (
|
|
37
41
|
<div
|
|
38
42
|
className={cn(
|
|
39
43
|
"bg-foreground/[0.05] flex h-14 max-w-56 min-w-0 items-center gap-2 rounded-lg pl-3",
|
|
40
44
|
onRemove ? "pr-1" : "pr-3"
|
|
41
45
|
)}
|
|
42
|
-
title={file.name}
|
|
46
|
+
title={type?.note ? `${file.name} (${type.note})` : file.name}
|
|
43
47
|
>
|
|
44
48
|
{uploading ? (
|
|
45
49
|
<Loader2 className="text-muted-foreground h-4 w-4 shrink-0 animate-spin" />
|
|
@@ -48,8 +52,8 @@ export function FileChip({
|
|
|
48
52
|
)}
|
|
49
53
|
<span className="min-w-0">
|
|
50
54
|
<span className="block truncate text-sm">{file.name}</span>
|
|
51
|
-
<span className="text-muted-foreground block text-xs">
|
|
52
|
-
{
|
|
55
|
+
<span className="text-muted-foreground block truncate text-xs">
|
|
56
|
+
{detail}
|
|
53
57
|
</span>
|
|
54
58
|
</span>
|
|
55
59
|
{onRemove && (
|
|
@@ -14,9 +14,12 @@ export interface PasteHandlers {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
// Dragged files sometimes arrive without a type; go by the name then.
|
|
17
|
+
// Only the formats a model takes; HEIC photos and SVG go up as files, where
|
|
18
|
+
// the server converts or keeps them.
|
|
19
|
+
const INLINE_IMAGE = /^image\/(png|jpe?g|gif|webp)$/;
|
|
17
20
|
export const isImage = (f: File) =>
|
|
18
|
-
f.type
|
|
19
|
-
(!f.type && /\.(png|jpe?g|gif|webp
|
|
21
|
+
INLINE_IMAGE.test(f.type) ||
|
|
22
|
+
(!f.type && /\.(png|jpe?g|gif|webp)$/i.test(f.name));
|
|
20
23
|
|
|
21
24
|
function editorMode(data: DataTransfer): string | undefined {
|
|
22
25
|
try {
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from "@/data/client-config/queries";
|
|
10
10
|
import {
|
|
11
11
|
acceptAttr,
|
|
12
|
+
limitFor,
|
|
12
13
|
tooBigMessage,
|
|
13
14
|
typeFor,
|
|
14
15
|
unsupportedMessage,
|
|
@@ -83,11 +84,13 @@ export function useAttachFiles({
|
|
|
83
84
|
const shrunk = await shrinkImage(f, limits.maxImageBytes);
|
|
84
85
|
if (shrunk.ok) images.push(shrunk.file);
|
|
85
86
|
else error = tooBigMessage(f.name, f.size, limits.maxImageBytes);
|
|
86
|
-
} else
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
} else {
|
|
88
|
+
const type = typeFor(f.name, limits.types);
|
|
89
|
+
if (!type) error = unsupportedMessage(f.name);
|
|
90
|
+
else if (f.size > limitFor(type, limits))
|
|
91
|
+
error = tooBigMessage(f.name, f.size, limitFor(type, limits));
|
|
92
|
+
else others.push(f);
|
|
93
|
+
}
|
|
91
94
|
if (error) toast.error(error);
|
|
92
95
|
}
|
|
93
96
|
if (images.length) onImages(await Promise.all(images.map(readImage)));
|
package/content/whats-new.json
CHANGED
|
@@ -1897,6 +1897,14 @@
|
|
|
1897
1897
|
"area": "AgentOS",
|
|
1898
1898
|
"text": "The reachability check now tells a stuck AgentOS apart from another program on its port, and setup keeps a copy of a startup file you edited before replacing it.",
|
|
1899
1899
|
"url": "https://github.com/saadnvd1/agent-os/pull/547"
|
|
1900
|
+
},
|
|
1901
|
+
{
|
|
1902
|
+
"id": 548,
|
|
1903
|
+
"date": "2026-11-01",
|
|
1904
|
+
"kind": "new",
|
|
1905
|
+
"area": "Chat",
|
|
1906
|
+
"text": "Chats now accept HAR network captures (with logins and cookies hidden), iPhone photos and many more file types.",
|
|
1907
|
+
"url": "https://github.com/saadnvd1/agent-os/pull/548"
|
|
1900
1908
|
}
|
|
1901
1909
|
]
|
|
1902
1910
|
}
|
package/lib/attachments/check.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isText, sniff } from "../intake/sniff";
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
limitFor,
|
|
4
4
|
tooBigMessage,
|
|
5
5
|
typeFor,
|
|
6
6
|
unsupportedMessage,
|
|
@@ -28,10 +28,7 @@ const bad = (name: string): UploadCheck => ({
|
|
|
28
28
|
export function checkUpload(name: string, data: Buffer): UploadCheck {
|
|
29
29
|
const type = typeFor(name);
|
|
30
30
|
if (!type) return { ok: false, status: 415, error: unsupportedMessage(name) };
|
|
31
|
-
const limit =
|
|
32
|
-
type.kind === "image"
|
|
33
|
-
? ATTACHMENT_LIMITS.maxImageBytes
|
|
34
|
-
: ATTACHMENT_LIMITS.maxBytes;
|
|
31
|
+
const limit = limitFor(type);
|
|
35
32
|
if (data.length > limit)
|
|
36
33
|
return {
|
|
37
34
|
ok: false,
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// A HAR is a browser's network log: it carries live cookies and bearer
|
|
2
|
+
// tokens. On the way in the values of the credential headers and every
|
|
3
|
+
// cookie are replaced; names, URLs, timings, bodies and statuses stay.
|
|
4
|
+
|
|
5
|
+
export const REDACTED = "[redacted]";
|
|
6
|
+
|
|
7
|
+
const SECRET_HEADER =
|
|
8
|
+
/^(cookie|set-cookie|authorization|proxy-authorization)$|api[-_]?key|token|secret|^x-(csrf|xsrf)/i;
|
|
9
|
+
|
|
10
|
+
interface Pair {
|
|
11
|
+
name?: unknown;
|
|
12
|
+
value?: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Message {
|
|
16
|
+
headers?: Pair[];
|
|
17
|
+
cookies?: Pair[];
|
|
18
|
+
status?: number;
|
|
19
|
+
bodySize?: number;
|
|
20
|
+
content?: { size?: number };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Entry {
|
|
24
|
+
request?: Message & { method?: string; url?: string };
|
|
25
|
+
response?: Message;
|
|
26
|
+
time?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface HarResult {
|
|
30
|
+
json: string;
|
|
31
|
+
redacted: number;
|
|
32
|
+
summary: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function scrub(list: Pair[] | undefined, always: boolean): number {
|
|
36
|
+
let n = 0;
|
|
37
|
+
if (!Array.isArray(list)) return n;
|
|
38
|
+
for (const p of list) {
|
|
39
|
+
if (!p || typeof p !== "object" || p.value === REDACTED) continue;
|
|
40
|
+
if (always || (typeof p.name === "string" && SECRET_HEADER.test(p.name))) {
|
|
41
|
+
p.value = REDACTED;
|
|
42
|
+
n++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return n;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const hostOf = (url: string) => {
|
|
49
|
+
try {
|
|
50
|
+
return new URL(url).host;
|
|
51
|
+
} catch {
|
|
52
|
+
return "(unparsed)";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const top = (counts: Map<string, number>, n: number) =>
|
|
57
|
+
[...counts].sort((a, b) => b[1] - a[1]).slice(0, n);
|
|
58
|
+
|
|
59
|
+
function summarize(entries: Entry[]): string {
|
|
60
|
+
const hosts = new Map<string, number>();
|
|
61
|
+
const statuses = new Map<string, number>();
|
|
62
|
+
const rows = entries.map((e) => {
|
|
63
|
+
const url = e.request?.url ?? "";
|
|
64
|
+
const status = e.response?.status ?? 0;
|
|
65
|
+
const host = hostOf(url);
|
|
66
|
+
hosts.set(host, (hosts.get(host) ?? 0) + 1);
|
|
67
|
+
const klass = status >= 100 ? `${Math.floor(status / 100)}xx` : "failed";
|
|
68
|
+
statuses.set(klass, (statuses.get(klass) ?? 0) + 1);
|
|
69
|
+
const size = Math.max(
|
|
70
|
+
e.response?.content?.size ?? 0,
|
|
71
|
+
e.response?.bodySize ?? 0
|
|
72
|
+
);
|
|
73
|
+
const line = `${e.request?.method ?? "?"} ${url.slice(0, 160)} -> ${status || "no response"}`;
|
|
74
|
+
return { line, size, status, time: e.time ?? 0 };
|
|
75
|
+
});
|
|
76
|
+
const failing = rows.filter((r) => r.status === 0 || r.status >= 400);
|
|
77
|
+
const largest = [...rows].sort((a, b) => b.size - a.size).slice(0, 5);
|
|
78
|
+
const lines = [
|
|
79
|
+
`HAR summary: ${entries.length} requests, ${hosts.size} hosts.`,
|
|
80
|
+
`Hosts: ${top(hosts, 8)
|
|
81
|
+
.map(([h, c]) => `${h} (${c})`)
|
|
82
|
+
.join(", ")}`,
|
|
83
|
+
`Status: ${[...statuses]
|
|
84
|
+
.sort()
|
|
85
|
+
.map(([k, c]) => `${k} ${c}`)
|
|
86
|
+
.join(", ")}`,
|
|
87
|
+
`Largest responses: ${largest.map((r) => `${r.line} [${r.size} B]`).join("; ")}`,
|
|
88
|
+
failing.length
|
|
89
|
+
? `Failing (${failing.length}${failing.length > 8 ? ", first 8" : ""}): ${failing
|
|
90
|
+
.slice(0, 8)
|
|
91
|
+
.map((r) => r.line)
|
|
92
|
+
.join("; ")}`
|
|
93
|
+
: "Failing: none",
|
|
94
|
+
"Cookie and auth header values were replaced with [redacted].",
|
|
95
|
+
];
|
|
96
|
+
return lines.join("\n");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The HAR with its secrets replaced and a short summary, or null when the text isn't a HAR. */
|
|
100
|
+
export function redactHar(text: string): HarResult | null {
|
|
101
|
+
let har: { log?: { entries?: Entry[] } };
|
|
102
|
+
try {
|
|
103
|
+
har = JSON.parse(text);
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const entries = har?.log?.entries;
|
|
108
|
+
if (!Array.isArray(entries)) return null;
|
|
109
|
+
let redacted = 0;
|
|
110
|
+
for (const e of entries) {
|
|
111
|
+
for (const m of [e?.request, e?.response]) {
|
|
112
|
+
if (!m || typeof m !== "object") continue;
|
|
113
|
+
redacted += scrub(m.headers, false) + scrub(m.cookies, true);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Indented, so the agent can grep and read it by line.
|
|
117
|
+
return {
|
|
118
|
+
json: JSON.stringify(har, null, 1),
|
|
119
|
+
redacted,
|
|
120
|
+
summary: summarize(entries.filter((e) => e && typeof e === "object")),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import { ATTACHMENT_LIMITS, formatBytes } from "./policy";
|
|
3
|
+
import { summaryPath } from "./store";
|
|
3
4
|
import type { ChatFile } from "./types";
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -8,14 +9,35 @@ import type { ChatFile } from "./types";
|
|
|
8
9
|
* spreadsheet through a script, a docx through pandoc). The chat keeps the
|
|
9
10
|
* plain text and shows the files as chips.
|
|
10
11
|
*/
|
|
12
|
+
// Over this, the agent is told to read in parts.
|
|
13
|
+
const LARGE_BYTES = 1_000_000;
|
|
14
|
+
const SUMMARY_CHARS = 3000;
|
|
15
|
+
|
|
16
|
+
function summaryOf(file: ChatFile): string {
|
|
17
|
+
try {
|
|
18
|
+
return fs
|
|
19
|
+
.readFileSync(summaryPath(file.path), "utf8")
|
|
20
|
+
.slice(0, SUMMARY_CHARS);
|
|
21
|
+
} catch {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
11
26
|
export function agentText(text: string, files?: ChatFile[]): string {
|
|
12
27
|
if (!files?.length) return text;
|
|
13
28
|
const list = files
|
|
14
|
-
.map((f) =>
|
|
29
|
+
.map((f) => {
|
|
30
|
+
const summary = summaryOf(f);
|
|
31
|
+
const large = f.size > LARGE_BYTES ? ", large: read it in parts" : "";
|
|
32
|
+
return (
|
|
33
|
+
`- ${f.path} (${f.mediaType}, ${formatBytes(f.size)}${large})` +
|
|
34
|
+
(summary ? `\n${summary.replace(/^/gm, " ")}` : "")
|
|
35
|
+
);
|
|
36
|
+
})
|
|
15
37
|
.join("\n");
|
|
16
38
|
return [
|
|
17
39
|
text,
|
|
18
|
-
`Attached files, saved on this machine.
|
|
40
|
+
`Attached files, saved on this machine. They are never pasted into this message; open them with your own tools (grep or head a big one rather than reading it whole):\n${list}`,
|
|
19
41
|
]
|
|
20
42
|
.filter(Boolean)
|
|
21
43
|
.join("\n\n");
|
|
@@ -8,6 +8,8 @@ export interface AttachmentType {
|
|
|
8
8
|
ext: string;
|
|
9
9
|
mediaType: string;
|
|
10
10
|
kind: AttachmentKind;
|
|
11
|
+
// Shown on the chip: what the server does to this type on the way in.
|
|
12
|
+
note?: string;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
const t = (
|
|
@@ -34,9 +36,22 @@ export const ATTACHMENT_TYPES: AttachmentType[] = [
|
|
|
34
36
|
...t("doc", `${OOXML}.presentationml.presentation`, "pptx"),
|
|
35
37
|
...t("doc", "application/vnd.oasis.opendocument.text", "odt"),
|
|
36
38
|
...t("doc", "application/rtf", "rtf"),
|
|
37
|
-
...t("
|
|
39
|
+
...t("image", "image/heic", "heic", "heif"),
|
|
40
|
+
...t("text", "text/plain", "txt", "log", "out", "err", "env"),
|
|
38
41
|
...t("text", "text/markdown", "md", "markdown"),
|
|
39
|
-
...t("text", "application/json", "json", "jsonl", "ipynb"),
|
|
42
|
+
...t("text", "application/json", "json", "jsonl", "ndjson", "ipynb"),
|
|
43
|
+
{
|
|
44
|
+
ext: "har",
|
|
45
|
+
mediaType: "application/json",
|
|
46
|
+
kind: "text",
|
|
47
|
+
note: "auth headers redacted",
|
|
48
|
+
},
|
|
49
|
+
...t("text", "text/calendar", "ics"),
|
|
50
|
+
...t("text", "message/rfc822", "eml"),
|
|
51
|
+
...t("text", "text/vtt", "vtt"),
|
|
52
|
+
...t("text", "application/x-subrip", "srt"),
|
|
53
|
+
// Svg is kept as text so nothing ever renders it.
|
|
54
|
+
...t("text", "text/plain", "svg"),
|
|
40
55
|
...t(
|
|
41
56
|
"text",
|
|
42
57
|
"text/plain",
|
|
@@ -67,15 +82,67 @@ export const ATTACHMENT_TYPES: AttachmentType[] = [
|
|
|
67
82
|
"cs",
|
|
68
83
|
"php",
|
|
69
84
|
"diff",
|
|
70
|
-
"patch"
|
|
85
|
+
"patch",
|
|
86
|
+
"sh",
|
|
87
|
+
"bash",
|
|
88
|
+
"zsh",
|
|
89
|
+
"fish",
|
|
90
|
+
"ini",
|
|
91
|
+
"conf",
|
|
92
|
+
"cfg",
|
|
93
|
+
"properties",
|
|
94
|
+
"tf",
|
|
95
|
+
"tfvars",
|
|
96
|
+
"hcl",
|
|
97
|
+
"graphql",
|
|
98
|
+
"gql",
|
|
99
|
+
"proto",
|
|
100
|
+
"vue",
|
|
101
|
+
"svelte",
|
|
102
|
+
"astro",
|
|
103
|
+
"dart",
|
|
104
|
+
"lua",
|
|
105
|
+
"r",
|
|
106
|
+
"scala",
|
|
107
|
+
"ex",
|
|
108
|
+
"exs",
|
|
109
|
+
"erl",
|
|
110
|
+
"zig",
|
|
111
|
+
"nix",
|
|
112
|
+
"lock",
|
|
113
|
+
"sum",
|
|
114
|
+
"mod",
|
|
115
|
+
"gitignore",
|
|
116
|
+
"gitattributes",
|
|
117
|
+
"dockerignore",
|
|
118
|
+
"editorconfig"
|
|
119
|
+
),
|
|
120
|
+
// Files that are known by their whole name, matched without a dot.
|
|
121
|
+
...t(
|
|
122
|
+
"text",
|
|
123
|
+
"text/plain",
|
|
124
|
+
"dockerfile",
|
|
125
|
+
"makefile",
|
|
126
|
+
"gnumakefile",
|
|
127
|
+
"procfile",
|
|
128
|
+
"gemfile",
|
|
129
|
+
"rakefile",
|
|
130
|
+
"brewfile",
|
|
131
|
+
"justfile",
|
|
132
|
+
"env.example",
|
|
133
|
+
"env.sample",
|
|
134
|
+
"env.template"
|
|
71
135
|
),
|
|
72
136
|
];
|
|
73
137
|
|
|
74
138
|
export const ATTACHMENT_LIMITS = {
|
|
75
139
|
// Files and images on one message.
|
|
76
140
|
max: 8,
|
|
77
|
-
// One
|
|
141
|
+
// One document, sheet or PDF.
|
|
78
142
|
maxBytes: 25_000_000,
|
|
143
|
+
// One text file (logs, HAR captures, code). It is saved to disk and opened
|
|
144
|
+
// by the agent from its path, never put into the prompt, so it can be big.
|
|
145
|
+
maxTextBytes: 100_000_000,
|
|
79
146
|
// One image, under the 5 MB a model accepts once base64'd.
|
|
80
147
|
maxImageBytes: 3_750_000,
|
|
81
148
|
// A PDF up to this goes to Claude as a document block; bigger ones, and
|
|
@@ -91,13 +158,41 @@ const BY_EXT = new Map(ATTACHMENT_TYPES.map((x) => [x.ext, x]));
|
|
|
91
158
|
export const extOf = (name: string): string =>
|
|
92
159
|
/\.([A-Za-z0-9]+)$/.exec(name)?.[1].toLowerCase() ?? "";
|
|
93
160
|
|
|
161
|
+
// "Dockerfile" and ".env.example" have no extension worth the name; they are
|
|
162
|
+
// matched whole, lower-cased and without a leading dot.
|
|
163
|
+
const wholeName = (name: string): string =>
|
|
164
|
+
(name.split(/[\\/]/).pop() ?? "").toLowerCase().replace(/^\./, "");
|
|
165
|
+
|
|
94
166
|
export const typeFor = (
|
|
95
167
|
name: string,
|
|
96
168
|
types: AttachmentType[] = ATTACHMENT_TYPES
|
|
97
|
-
): AttachmentType | null =>
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
169
|
+
): AttachmentType | null => {
|
|
170
|
+
const ext = extOf(name);
|
|
171
|
+
const whole = wholeName(name);
|
|
172
|
+
const find = (key: string) =>
|
|
173
|
+
types === ATTACHMENT_TYPES
|
|
174
|
+
? BY_EXT.get(key)
|
|
175
|
+
: types.find((x) => x.ext === key);
|
|
176
|
+
return (ext && find(ext)) || find(whole) || null;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// iPhone photos: no browser or model reads them, so they are converted on
|
|
180
|
+
// intake and kept as files instead of going inline with the message.
|
|
181
|
+
export const isHeic = (name: string) => /^heic|^heif/.test(extOf(name));
|
|
182
|
+
|
|
183
|
+
/** The byte limit for one file of this type. */
|
|
184
|
+
export const limitFor = (
|
|
185
|
+
type: AttachmentType,
|
|
186
|
+
limits: Pick<
|
|
187
|
+
typeof ATTACHMENT_LIMITS,
|
|
188
|
+
"maxBytes" | "maxTextBytes" | "maxImageBytes"
|
|
189
|
+
> = ATTACHMENT_LIMITS
|
|
190
|
+
): number => {
|
|
191
|
+
if (type.kind === "text") return limits.maxTextBytes;
|
|
192
|
+
if (type.kind === "image" && !isHeic(`x.${type.ext}`))
|
|
193
|
+
return limits.maxImageBytes;
|
|
194
|
+
return limits.maxBytes;
|
|
195
|
+
};
|
|
101
196
|
|
|
102
197
|
// For <input accept>: every extension, so the picker greys out the rest.
|
|
103
198
|
export const acceptAttr = (types: AttachmentType[] = ATTACHMENT_TYPES) =>
|
|
@@ -110,7 +205,7 @@ export const formatBytes = (n: number): string =>
|
|
|
110
205
|
|
|
111
206
|
// What a person is told when a file can't be attached.
|
|
112
207
|
export const unsupportedMessage = (name: string): string =>
|
|
113
|
-
`${name}: this file type can't be attached. Supported: PDF, spreadsheets (xlsx, xls, csv), Word
|
|
208
|
+
`${name}: this file type can't be attached. Supported: images (including HEIC), PDF, spreadsheets (xlsx, xls, csv), Word and PowerPoint, HAR captures, SVG, and text files: logs, markdown, JSON, config, shell scripts, Dockerfile, Makefile and source code.`;
|
|
114
209
|
|
|
115
210
|
export const tooBigMessage = (name: string, size: number, limit: number) =>
|
|
116
211
|
`${name} is ${formatBytes(size)}; the limit is ${formatBytes(limit)}.`;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { execFile } from "child_process";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { promisify } from "util";
|
|
6
|
+
import { redactHar } from "./har";
|
|
7
|
+
import {
|
|
8
|
+
extOf,
|
|
9
|
+
isHeic,
|
|
10
|
+
type AttachmentKind,
|
|
11
|
+
type AttachmentType,
|
|
12
|
+
} from "./policy";
|
|
13
|
+
|
|
14
|
+
const run = promisify(execFile);
|
|
15
|
+
|
|
16
|
+
export type Prepared =
|
|
17
|
+
| {
|
|
18
|
+
ok: true;
|
|
19
|
+
data: Buffer;
|
|
20
|
+
name: string;
|
|
21
|
+
mediaType: string;
|
|
22
|
+
kind: AttachmentKind;
|
|
23
|
+
// Saved beside the file and given to the agent with its path.
|
|
24
|
+
summary?: string;
|
|
25
|
+
}
|
|
26
|
+
| { ok: false; status: 400; error: string };
|
|
27
|
+
|
|
28
|
+
export type Converter = (cmd: string, args: string[]) => Promise<unknown>;
|
|
29
|
+
|
|
30
|
+
const realConverter: Converter = (cmd, args) =>
|
|
31
|
+
run(cmd, args, { timeout: 30_000 });
|
|
32
|
+
|
|
33
|
+
// sips ships with macOS; ImageMagick and libheif cover Linux.
|
|
34
|
+
const TOOLS = (src: string, out: string): [string, string[]][] => [
|
|
35
|
+
["sips", ["-s", "format", "jpeg", src, "--out", out]],
|
|
36
|
+
["magick", [src, out]],
|
|
37
|
+
["heif-convert", [src, out]],
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
/** A JPEG of an iPhone photo, or null when this machine has no converter. */
|
|
41
|
+
export async function heicToJpeg(
|
|
42
|
+
data: Buffer,
|
|
43
|
+
convert: Converter = realConverter
|
|
44
|
+
): Promise<Buffer | null> {
|
|
45
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "aos-heic-"));
|
|
46
|
+
try {
|
|
47
|
+
const src = path.join(dir, "in.heic");
|
|
48
|
+
const out = path.join(dir, "out.jpg");
|
|
49
|
+
fs.writeFileSync(src, data);
|
|
50
|
+
for (const [cmd, args] of TOOLS(src, out)) {
|
|
51
|
+
try {
|
|
52
|
+
await convert(cmd, args);
|
|
53
|
+
if (fs.existsSync(out)) return fs.readFileSync(out);
|
|
54
|
+
} catch {
|
|
55
|
+
// Not installed or failed: the next tool.
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
} finally {
|
|
60
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What is stored for a checked upload: secrets removed, photos converted. */
|
|
65
|
+
export async function prepareUpload(
|
|
66
|
+
type: AttachmentType,
|
|
67
|
+
name: string,
|
|
68
|
+
data: Buffer,
|
|
69
|
+
convert?: Converter
|
|
70
|
+
): Promise<Prepared> {
|
|
71
|
+
const keep: Prepared = {
|
|
72
|
+
ok: true,
|
|
73
|
+
data,
|
|
74
|
+
name,
|
|
75
|
+
mediaType: type.mediaType,
|
|
76
|
+
kind: type.kind,
|
|
77
|
+
};
|
|
78
|
+
if (type.ext === "har") {
|
|
79
|
+
const har = redactHar(data.toString("utf8"));
|
|
80
|
+
if (!har)
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
status: 400,
|
|
84
|
+
error: `${name} doesn't look like a real HAR file.`,
|
|
85
|
+
};
|
|
86
|
+
return { ...keep, data: Buffer.from(har.json), summary: har.summary };
|
|
87
|
+
}
|
|
88
|
+
if (isHeic(name)) {
|
|
89
|
+
const jpeg = await heicToJpeg(data, convert);
|
|
90
|
+
if (!jpeg) return keep;
|
|
91
|
+
const stem = name.slice(0, name.length - extOf(name).length - 1);
|
|
92
|
+
return {
|
|
93
|
+
...keep,
|
|
94
|
+
data: jpeg,
|
|
95
|
+
name: `${stem}.jpg`,
|
|
96
|
+
mediaType: "image/jpeg",
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return keep;
|
|
100
|
+
}
|
package/lib/attachments/store.ts
CHANGED
|
@@ -28,6 +28,10 @@ export function safeName(name: string, taken: Set<string>, n: number): string {
|
|
|
28
28
|
return out;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
// A short description kept beside a big file (a HAR's hosts and failures),
|
|
32
|
+
// which the agent is given with the path.
|
|
33
|
+
export const summaryPath = (file: string) => `${file}.summary.txt`;
|
|
34
|
+
|
|
31
35
|
export function scopeDir(scope: string): string {
|
|
32
36
|
return path.join(attachmentsRoot(), segment(scope) || "new");
|
|
33
37
|
}
|
|
@@ -36,7 +40,8 @@ export function saveAttachment(
|
|
|
36
40
|
scope: string,
|
|
37
41
|
name: string,
|
|
38
42
|
data: Buffer,
|
|
39
|
-
mediaType: string
|
|
43
|
+
mediaType: string,
|
|
44
|
+
summary?: string
|
|
40
45
|
): ChatFile {
|
|
41
46
|
const dir = scopeDir(scope);
|
|
42
47
|
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
@@ -44,6 +49,7 @@ export function saveAttachment(
|
|
|
44
49
|
const file = safeName(name, taken, taken.size + 1);
|
|
45
50
|
const full = path.join(dir, file);
|
|
46
51
|
fs.writeFileSync(full, data, { mode: 0o600 });
|
|
52
|
+
if (summary) fs.writeFileSync(summaryPath(full), summary, { mode: 0o600 });
|
|
47
53
|
return { path: full, name: file, size: data.length, mediaType };
|
|
48
54
|
}
|
|
49
55
|
|
|
@@ -36,12 +36,14 @@ export interface ClientConfig {
|
|
|
36
36
|
export interface AttachmentConfig {
|
|
37
37
|
max: number;
|
|
38
38
|
maxBytes: number;
|
|
39
|
+
maxTextBytes: number;
|
|
39
40
|
maxImageBytes: number;
|
|
40
41
|
pdfInlineBytes: number;
|
|
41
42
|
types: {
|
|
42
43
|
ext: string;
|
|
43
44
|
mediaType: string;
|
|
44
45
|
kind: "image" | "pdf" | "sheet" | "doc" | "text";
|
|
46
|
+
note?: string;
|
|
45
47
|
}[];
|
|
46
48
|
}
|
|
47
49
|
|