From 0738cfaace605d125743c00b02a1d3e5082f7077 Mon Sep 17 00:00:00 2001 From: Nathan Sarrazin Date: Thu, 6 Jun 2024 14:26:46 +0200 Subject: [PATCH] Fix max upload size in huggingchat (#1251) --- src/lib/utils/messageUpdates.ts | 30 +++++++++++++------- src/routes/conversation/[id]/+server.ts | 37 ++++++++++++++++--------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/lib/utils/messageUpdates.ts b/src/lib/utils/messageUpdates.ts index a7af983cd34..fda6799588d 100644 --- a/src/lib/utils/messageUpdates.ts +++ b/src/lib/utils/messageUpdates.ts @@ -59,18 +59,28 @@ export async function fetchMessageUpdates( const abortController = new AbortController(); abortSignal.addEventListener("abort", () => abortController.abort()); + const form = new FormData(); + + const optsJSON = JSON.stringify({ + inputs: opts.inputs, + id: opts.messageId, + is_retry: opts.isRetry, + is_continue: opts.isContinue, + web_search: opts.webSearch, + tools: opts.tools, + }); + + opts.files?.forEach((file) => { + const name = file.type + ";" + file.name; + + form.append("files", new File([file.value], name, { type: file.mime })); + }); + + form.append("data", optsJSON); + const response = await fetch(`${opts.base}/conversation/${conversationId}`, { method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - inputs: opts.inputs, - id: opts.messageId, - is_retry: opts.isRetry, - is_continue: opts.isContinue, - web_search: opts.webSearch, - tools: opts.tools, - files: opts.files, - }), + body: form, signal: abortController.signal, }); diff --git a/src/routes/conversation/[id]/+server.ts b/src/routes/conversation/[id]/+server.ts index f615f0de3f6..562ef6508a6 100644 --- a/src/routes/conversation/[id]/+server.ts +++ b/src/routes/conversation/[id]/+server.ts @@ -125,7 +125,13 @@ export async function POST({ request, locals, params, getClientAddress }) { } // finally parse the content of the request - const json = await request.json(); + const form = await request.formData(); + + const json = form.get("data"); + + if (!json || typeof json !== "string") { + throw error(400, "Invalid request"); + } const { inputs: newPrompt, @@ -134,7 +140,6 @@ export async function POST({ request, locals, params, getClientAddress }) { is_continue: isContinue, web_search: webSearch, tools: toolsPreferences, - files: inputFiles, } = z .object({ id: z.string().uuid().refine(isMessageId).optional(), // parent message id to append to for a normal message, or the message id for a retry/continue @@ -148,18 +153,24 @@ export async function POST({ request, locals, params, getClientAddress }) { is_continue: z.optional(z.boolean()), web_search: z.optional(z.boolean()), tools: z.record(z.boolean()).optional(), - files: z.optional( - z.array( - z.object({ - type: z.literal("base64").or(z.literal("hash")), - name: z.string(), - value: z.string(), - mime: z.string(), - }) - ) - ), }) - .parse(json); + .parse(JSON.parse(json)); + + const inputFiles = await Promise.all( + form + .getAll("files") + .filter((entry): entry is File => entry instanceof File && entry.size > 0) + .map(async (file) => { + const [type, ...name] = file.name.split(";"); + + return { + type: z.literal("base64").or(z.literal("hash")).parse(type), + value: await file.text(), + mime: file.type, + name: name.join(";"), + }; + }) + ); if (usageLimits?.messageLength && (newPrompt?.length ?? 0) > usageLimits.messageLength) { throw error(400, "Message too long.");