Skip to content

Commit

Permalink
Fix max upload size in huggingchat (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin authored Jun 6, 2024
1 parent 8716fdb commit 0738cfa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
30 changes: 20 additions & 10 deletions src/lib/utils/messageUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
37 changes: 24 additions & 13 deletions src/routes/conversation/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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.");
Expand Down

0 comments on commit 0738cfa

Please sign in to comment.