fix: allow more attachments without raising the image payload budget (#12620)
This commit is contained in:
1 parent
1ba471a37c
commit
2b178231eb
9 files changed
+170
-39
No files matched your search
@@ -477,10 +477,13 @@ describe("composer file attachments", () => {
|
||||
it("applies the remaining attachment slots to photos and videos together", async () => {
|
||||
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [image, video] });
|
||||
|
||||
const result = await pickComposerMedia({ existingCount: 7, maxVideoBytes: 50 * 1024 * 1024 });
|
||||
const result = await pickComposerMedia({
|
||||
existingCount: 99,
|
||||
maxVideoBytes: 50 * 1024 * 1024,
|
||||
});
|
||||
|
||||
expect(result.attachments).toEqual([expect.objectContaining({ type: "image" })]);
|
||||
expect(result.error).toBe("You can attach up to 8 attachments per message.");
|
||||
expect(result.error).toBe("You can attach up to 100 attachments per message.");
|
||||
expect(mocks.pickMedia).toHaveBeenCalledWith(expect.objectContaining({ selectionLimit: 1 }));
|
||||
expect(mocks.copy).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -625,9 +628,9 @@ describe("composer file attachments", () => {
|
||||
});
|
||||
|
||||
it("does not open the picker when the draft has no remaining attachment slots", async () => {
|
||||
await expect(pickComposerFiles({ existingCount: 8 })).resolves.toEqual({
|
||||
await expect(pickComposerFiles({ existingCount: 100 })).resolves.toEqual({
|
||||
files: [],
|
||||
error: "You can attach up to 8 files per message.",
|
||||
error: "You can attach up to 100 files per message.",
|
||||
});
|
||||
|
||||
expect(mocks.pickFile).not.toHaveBeenCalled();
|
||||
@@ -838,7 +841,7 @@ describe("composer file attachments", () => {
|
||||
],
|
||||
});
|
||||
|
||||
const result = await pickComposerFiles({ existingCount: 7, maxBytes: 1024 * 1024 });
|
||||
const result = await pickComposerFiles({ existingCount: 99, maxBytes: 1024 * 1024 });
|
||||
|
||||
expect(result.files.map((file) => file.name)).toEqual(["report.pdf"]);
|
||||
});
|
||||
|
||||
@@ -676,7 +676,7 @@ describe("mobile composer drafts", () => {
|
||||
return undefined;
|
||||
});
|
||||
const key = "environment-1:replace-file";
|
||||
const files = Array.from({ length: 8 }, (_, index) => ({
|
||||
const files = Array.from({ length: 100 }, (_, index) => ({
|
||||
id: `file-${index}`,
|
||||
type: "file" as const,
|
||||
name: `notes-${index}.txt`,
|
||||
@@ -687,8 +687,8 @@ describe("mobile composer drafts", () => {
|
||||
appendComposerDraftAttachments(key, files, { appendReference: true });
|
||||
const firstLink = "[notes-0.txt](t3-context://v1/file/file-0)";
|
||||
const insertion = captureComposerDraftInsertion(key, { start: 0, end: firstLink.length });
|
||||
expect(countComposerDraftAttachmentsAfterSelection(key, insertion)).toBe(7);
|
||||
expect(getComposerDraftAfterSelection(key, insertion).context?.records).toHaveLength(7);
|
||||
expect(countComposerDraftAttachmentsAfterSelection(key, insertion)).toBe(99);
|
||||
expect(getComposerDraftAfterSelection(key, insertion).context?.records).toHaveLength(99);
|
||||
const replacement = { ...files[0]!, id: "replacement", fileUri: "file:///replacement.txt" };
|
||||
if (kind === "attachment") {
|
||||
expect(
|
||||
@@ -716,7 +716,7 @@ describe("mobile composer drafts", () => {
|
||||
insertion,
|
||||
),
|
||||
).toBe(true);
|
||||
expect(getComposerDraftSnapshot(key).attachments).toHaveLength(8);
|
||||
expect(getComposerDraftSnapshot(key).attachments).toHaveLength(100);
|
||||
expect(getComposerDraftSnapshot(key).context?.records).toContainEqual(record);
|
||||
expect(getComposerDraftSnapshot(key).text).toBe(
|
||||
`${formatComposerContextReference(record)}${insertion.text.slice(firstLink.length)}`,
|
||||
@@ -730,7 +730,7 @@ describe("mobile composer drafts", () => {
|
||||
}
|
||||
const draft = getComposerDraftSnapshot(key);
|
||||
expect(draft.attachments.map((file) => file.id)).not.toContain("file-0");
|
||||
expect(draft.attachments.slice(0, 7)).toEqual(files.slice(1));
|
||||
expect(draft.attachments.slice(0, 99)).toEqual(files.slice(1));
|
||||
expect(draft.context?.records.some((record) => record.contextId === "file-0")).toBe(false);
|
||||
await cleanup.promise;
|
||||
expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledWith(files[0]!.fileUri);
|
||||
@@ -746,7 +746,7 @@ describe("mobile composer drafts", () => {
|
||||
return undefined;
|
||||
});
|
||||
const key = "environment-1:concurrent-import";
|
||||
const files = Array.from({ length: 8 }, (_, index) => ({
|
||||
const files = Array.from({ length: 100 }, (_, index) => ({
|
||||
id: `existing-${index}`,
|
||||
type: "file" as const,
|
||||
name: `notes-${index}.txt`,
|
||||
@@ -906,7 +906,7 @@ describe("mobile composer drafts", () => {
|
||||
fileUri: `file:///documents/t3-composer-attachments/${id}.mov`,
|
||||
});
|
||||
const draftKey = "new-task:environment-1:project-cap";
|
||||
const existing = Array.from({ length: 7 }, (_, index) => makeAttachment(`held-${index}`));
|
||||
const existing = Array.from({ length: 99 }, (_, index) => makeAttachment(`held-${index}`));
|
||||
appAtomRegistry.set(composerDraftsAtom, {
|
||||
[draftKey]: { text: "send this", attachments: existing },
|
||||
});
|
||||
@@ -918,7 +918,7 @@ describe("mobile composer drafts", () => {
|
||||
|
||||
expect(rejected).toBe(1);
|
||||
const draft = appAtomRegistry.get(composerDraftsAtom)[draftKey];
|
||||
expect(draft?.attachments).toHaveLength(8);
|
||||
expect(draft?.attachments).toHaveLength(100);
|
||||
expect(draft?.attachments.at(-1)?.id).toBe("incoming-1");
|
||||
await cleanup.promise;
|
||||
expect(composerAttachmentCleanupMocks.remove).toHaveBeenCalledExactlyOnceWith(
|
||||
@@ -932,7 +932,7 @@ describe("mobile composer drafts", () => {
|
||||
{ allowOverflow: true },
|
||||
);
|
||||
expect(overflowRejected).toBe(0);
|
||||
expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(9);
|
||||
expect(appAtomRegistry.get(composerDraftsAtom)[draftKey]?.attachments).toHaveLength(101);
|
||||
});
|
||||
|
||||
it("keeps shared attachment files until every draft releases them", async () => {
|
||||
@@ -2270,7 +2270,7 @@ describe("mobile composer drafts", () => {
|
||||
previewUri: "data:image/png;base64,YWJj",
|
||||
});
|
||||
const existingImage = image("existing");
|
||||
const sharedImages = Array.from({ length: 8 }, (_, index) => image(`shared-${index}`));
|
||||
const sharedImages = Array.from({ length: 100 }, (_, index) => image(`shared-${index}`));
|
||||
|
||||
const merged = mergeComposerDraftContentState(
|
||||
{ [draftKey]: { text: "", attachments: [existingImage] } },
|
||||
@@ -2278,9 +2278,9 @@ describe("mobile composer drafts", () => {
|
||||
{ text: "", attachments: sharedImages },
|
||||
);
|
||||
|
||||
expect(merged[draftKey]?.attachments).toHaveLength(8);
|
||||
expect(merged[draftKey]?.attachments).toHaveLength(100);
|
||||
expect(merged[draftKey]?.attachments[0]).toEqual(existingImage);
|
||||
expect(merged[draftKey]?.attachments.at(-1)?.id).toBe("shared-6");
|
||||
expect(merged[draftKey]?.attachments.at(-1)?.id).toBe("shared-98");
|
||||
});
|
||||
|
||||
it("restores the exact draft captured before an interrupted share import", () => {
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
@@ -59,6 +60,53 @@ function turnStartCommand(input: {
|
||||
}
|
||||
|
||||
describe("normalizeDispatchCommand attachments", () => {
|
||||
it.effect("accepts 100 inline images and rejects 101 before writing files", () =>
|
||||
Effect.gen(function* () {
|
||||
const config = yield* ServerConfig.ServerConfig;
|
||||
const attachments = Array.from({ length: 100 }, () => ({
|
||||
dataUrl: "data:image/png;base64,cGl4ZWxz",
|
||||
sizeBytes: 6,
|
||||
}));
|
||||
const rejected = yield* normalizeDispatchCommand(
|
||||
turnStartCommand({ attachments: [...attachments, attachments[0]!] }),
|
||||
).pipe(Effect.flip);
|
||||
expect(rejected.message).toContain("up to 100");
|
||||
expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([]);
|
||||
const accepted = yield* normalizeDispatchCommand(turnStartCommand({ attachments }));
|
||||
if (accepted.type !== "thread.turn.start") throw new Error("Wrong command");
|
||||
expect(accepted.message.attachments).toHaveLength(100);
|
||||
expect(NodeFS.readdirSync(config.attachmentsDir)).toHaveLength(100);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
it.effect("rejects decoded image overflow before writing it and removes earlier files", () =>
|
||||
Effect.gen(function* () {
|
||||
const config = yield* ServerConfig.ServerConfig;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
let writtenBytes = 0;
|
||||
const dataUrl = `data:image/png;base64,${Buffer.alloc(10 * 1024 * 1024).toString("base64")}`;
|
||||
const command = turnStartCommand({
|
||||
attachments: [
|
||||
...Array.from({ length: 8 }, () => ({ dataUrl, sizeBytes: 1 })),
|
||||
{ dataUrl: "data:image/png;base64,YQ==", sizeBytes: 0 },
|
||||
],
|
||||
});
|
||||
const error = yield* normalizeDispatchCommand(command).pipe(
|
||||
Effect.provideService(FileSystem.FileSystem, {
|
||||
...fileSystem,
|
||||
writeFile: (path, data, options) => {
|
||||
writtenBytes += data.byteLength;
|
||||
return fileSystem.writeFile(path, data, options);
|
||||
},
|
||||
}),
|
||||
Effect.flip,
|
||||
);
|
||||
expect(error.message).toContain("80 MiB");
|
||||
expect(writtenBytes).toBe(80 * 1024 * 1024);
|
||||
expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([]);
|
||||
}).pipe(Effect.provide(testLayer)),
|
||||
);
|
||||
|
||||
it.effect("rejects duplicate client ids before persisting attachments", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* normalizeDispatchCommand(
|
||||
@@ -464,25 +512,25 @@ describe("question attachments", () => {
|
||||
answers: { first: "", second: "" },
|
||||
createdAt: "2026-08-01T00:00:00.000Z",
|
||||
attachmentsByQuestionId: {
|
||||
first: Array.from({ length: 4 }, () => attachment),
|
||||
second: Array.from({ length: 5 }, () => attachment),
|
||||
first: Array.from({ length: 50 }, () => attachment),
|
||||
second: Array.from({ length: 51 }, () => attachment),
|
||||
},
|
||||
};
|
||||
const failure = yield* normalizeDispatchCommand(command).pipe(Effect.flip);
|
||||
expect(failure.message).toContain("up to 8");
|
||||
expect(failure.message).toContain("up to 100");
|
||||
expect(NodeFS.readdirSync(config.attachmentsDir)).toEqual([`${id}.txt`]);
|
||||
const accepted = {
|
||||
...command,
|
||||
attachmentsByQuestionId: {
|
||||
...command.attachmentsByQuestionId,
|
||||
second: Array.from({ length: 4 }, () => attachment),
|
||||
second: Array.from({ length: 50 }, () => attachment),
|
||||
},
|
||||
};
|
||||
const normalized = yield* normalizeDispatchCommand(accepted);
|
||||
if (normalized.type !== "thread.user-input.respond") throw new Error("Wrong command");
|
||||
const attachments = Object.values(normalized.attachmentsByQuestionId!).flat();
|
||||
expect(attachments).toHaveLength(8);
|
||||
expect(new Set(attachments.map((item) => item.id)).size).toBe(8);
|
||||
expect(attachments).toHaveLength(100);
|
||||
expect(new Set(attachments.map((item) => item.id)).size).toBe(100);
|
||||
for (const item of attachments) {
|
||||
expect(item.name).toBe(attachment.name);
|
||||
expect(
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as Path from "effect/Path";
|
||||
import {
|
||||
type ClientOrchestrationCommand,
|
||||
type UserInputAttachments,
|
||||
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
|
||||
getProviderAttachmentLimitError,
|
||||
type IsoDateTime,
|
||||
type OrchestrationCommand,
|
||||
OrchestrationDispatchCommandError,
|
||||
@@ -142,13 +142,9 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
|
||||
canonicalCommand.type === "thread.turn.start"
|
||||
? canonicalCommand.message.attachments
|
||||
: Object.values(canonicalCommand.attachmentsByQuestionId ?? {}).flat();
|
||||
if (
|
||||
canonicalCommand.type === "thread.user-input.respond" &&
|
||||
attachments.length > PROVIDER_SEND_TURN_MAX_ATTACHMENTS
|
||||
) {
|
||||
return yield* new OrchestrationDispatchCommandError({
|
||||
message: `You can attach up to ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} files per question response.`,
|
||||
});
|
||||
const attachmentLimitError = getProviderAttachmentLimitError(attachments);
|
||||
if (attachmentLimitError) {
|
||||
return yield* new OrchestrationDispatchCommandError({ message: attachmentLimitError });
|
||||
}
|
||||
if (canonicalCommand.type === "thread.turn.start") {
|
||||
const clientAttachmentIds = new Set<string>();
|
||||
@@ -163,11 +159,12 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
|
||||
}
|
||||
}
|
||||
const claimedAttachmentPaths: string[] = [];
|
||||
const attachmentsWithDecodedSizes = [...attachments];
|
||||
// Context records bind to attachments by the id the client knew; they follow the rename.
|
||||
const finalAttachmentIdByClientId = new Map<string, string>();
|
||||
const normalizedAttachments = yield* Effect.forEach(
|
||||
attachments,
|
||||
(attachment) =>
|
||||
(attachment, index) =>
|
||||
Effect.gen(function* () {
|
||||
if (!("dataUrl" in attachment)) {
|
||||
const claim = planAttachmentClaim({
|
||||
@@ -259,6 +256,11 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
|
||||
sizeBytes: bytes.byteLength,
|
||||
...(attachment.source ? { source: attachment.source } : {}),
|
||||
};
|
||||
attachmentsWithDecodedSizes[index] = persistedAttachment;
|
||||
const decodedLimitError = getProviderAttachmentLimitError(attachmentsWithDecodedSizes);
|
||||
if (decodedLimitError) {
|
||||
return yield* new OrchestrationDispatchCommandError({ message: decodedLimitError });
|
||||
}
|
||||
|
||||
const attachmentPath = resolveAttachmentPath({
|
||||
attachmentsDir: serverConfig.attachmentsDir,
|
||||
@@ -286,6 +288,7 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
|
||||
}),
|
||||
),
|
||||
);
|
||||
claimedAttachmentPaths.push(attachmentPath);
|
||||
if (attachment.id !== undefined) {
|
||||
finalAttachmentIdByClientId.set(attachment.id, attachmentId);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user