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
@@ -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