Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/api/routes/surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ import {
SHARED_SKILL_TRIGGER_REFUSAL,
} from "../artifact-share.ts";

function contentTypeForFile(mimetype?: string): string {
const contentType = mimetype || "application/octet-stream";
if (/^(?:text\/|application\/(?:json|xml)(?:;|$))/i.test(contentType) && !/;\s*charset\s*=/i.test(contentType)) {
return `${contentType}; charset=utf-8`;
}
return contentType;
}

function isGrant(b: unknown): b is Grant {
return (
isObj(b) &&
Expand Down Expand Up @@ -252,7 +260,7 @@ async function getFileContent(ctx: ApiCtx): Promise<void> {
const opened = await app.openFileForViewer(id, viewer);
if (!opened) return sendJson(res, 404, { error: "not_found" });
res.writeHead(200, {
"content-type": opened.mimetype || "application/octet-stream",
"content-type": contentTypeForFile(opened.mimetype),
"content-length": String(opened.sizeBytes),
"content-disposition": `inline; filename*=UTF-8''${encodeURIComponent(opened.name)}`,
});
Expand Down
21 changes: 21 additions & 0 deletions test/agent-files-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ describe("agent files self-API", async () => {
assert.deepEqual(page.shared, []);
});

it("adds UTF-8 to text file content responses", async () => {
const res = await get(`/v1/files/${mineId}/content`, await capFor("U1"));
assert.equal(res.status, 200);
assert.equal(res.headers.get("content-type"), "text/plain; charset=utf-8");
assert.equal(Buffer.from(await res.arrayBuffer()).toString("utf8"), "mine");

await built.files.put({
id: "parameterized-text",
ownerScopeId: scopeId("personal", "U1"),
createdBy: "U1",
name: "parameterized.txt",
path: "parameterized.txt",
mimetype: "text/plain; charset=iso-8859-1",
data: Buffer.from("bytes"),
direction: "out",
});
const parameterized = await get("/v1/files/parameterized-text/content", await capFor("U1"));
assert.equal(parameterized.status, 200);
assert.equal(parameterized.headers.get("content-type"), "text/plain; charset=iso-8859-1");
});

it("returns 404 for another principal's file content", async () => {
const res = await get(`/v1/files/${theirsId}/content`, await capFor("U1"));
assert.equal(res.status, 404);
Expand Down