nsarrazin HF Staff commited on
Commit
e59e6bd
·
unverified ·
1 Parent(s): f26a7ac

Fix max upload size in huggingchat (#1251)

Browse files
src/lib/utils/messageUpdates.ts CHANGED
@@ -59,18 +59,28 @@ export async function fetchMessageUpdates(
59
  const abortController = new AbortController();
60
  abortSignal.addEventListener("abort", () => abortController.abort());
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  const response = await fetch(`${opts.base}/conversation/${conversationId}`, {
63
  method: "POST",
64
- headers: { "Content-Type": "application/json" },
65
- body: JSON.stringify({
66
- inputs: opts.inputs,
67
- id: opts.messageId,
68
- is_retry: opts.isRetry,
69
- is_continue: opts.isContinue,
70
- web_search: opts.webSearch,
71
- tools: opts.tools,
72
- files: opts.files,
73
- }),
74
  signal: abortController.signal,
75
  });
76
 
 
59
  const abortController = new AbortController();
60
  abortSignal.addEventListener("abort", () => abortController.abort());
61
 
62
+ const form = new FormData();
63
+
64
+ const optsJSON = JSON.stringify({
65
+ inputs: opts.inputs,
66
+ id: opts.messageId,
67
+ is_retry: opts.isRetry,
68
+ is_continue: opts.isContinue,
69
+ web_search: opts.webSearch,
70
+ tools: opts.tools,
71
+ });
72
+
73
+ opts.files?.forEach((file) => {
74
+ const name = file.type + ";" + file.name;
75
+
76
+ form.append("files", new File([file.value], name, { type: file.mime }));
77
+ });
78
+
79
+ form.append("data", optsJSON);
80
+
81
  const response = await fetch(`${opts.base}/conversation/${conversationId}`, {
82
  method: "POST",
83
+ body: form,
 
 
 
 
 
 
 
 
 
84
  signal: abortController.signal,
85
  });
86
 
src/routes/conversation/[id]/+server.ts CHANGED
@@ -125,7 +125,13 @@ export async function POST({ request, locals, params, getClientAddress }) {
125
  }
126
 
127
  // finally parse the content of the request
128
- const json = await request.json();
 
 
 
 
 
 
129
 
130
  const {
131
  inputs: newPrompt,
@@ -134,7 +140,6 @@ export async function POST({ request, locals, params, getClientAddress }) {
134
  is_continue: isContinue,
135
  web_search: webSearch,
136
  tools: toolsPreferences,
137
- files: inputFiles,
138
  } = z
139
  .object({
140
  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 }) {
148
  is_continue: z.optional(z.boolean()),
149
  web_search: z.optional(z.boolean()),
150
  tools: z.record(z.boolean()).optional(),
151
- files: z.optional(
152
- z.array(
153
- z.object({
154
- type: z.literal("base64").or(z.literal("hash")),
155
- name: z.string(),
156
- value: z.string(),
157
- mime: z.string(),
158
- })
159
- )
160
- ),
161
  })
162
- .parse(json);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  if (usageLimits?.messageLength && (newPrompt?.length ?? 0) > usageLimits.messageLength) {
165
  throw error(400, "Message too long.");
 
125
  }
126
 
127
  // finally parse the content of the request
128
+ const form = await request.formData();
129
+
130
+ const json = form.get("data");
131
+
132
+ if (!json || typeof json !== "string") {
133
+ throw error(400, "Invalid request");
134
+ }
135
 
136
  const {
137
  inputs: newPrompt,
 
140
  is_continue: isContinue,
141
  web_search: webSearch,
142
  tools: toolsPreferences,
 
143
  } = z
144
  .object({
145
  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
 
153
  is_continue: z.optional(z.boolean()),
154
  web_search: z.optional(z.boolean()),
155
  tools: z.record(z.boolean()).optional(),
 
 
 
 
 
 
 
 
 
 
156
  })
157
+ .parse(JSON.parse(json));
158
+
159
+ const inputFiles = await Promise.all(
160
+ form
161
+ .getAll("files")
162
+ .filter((entry): entry is File => entry instanceof File && entry.size > 0)
163
+ .map(async (file) => {
164
+ const [type, ...name] = file.name.split(";");
165
+
166
+ return {
167
+ type: z.literal("base64").or(z.literal("hash")).parse(type),
168
+ value: await file.text(),
169
+ mime: file.type,
170
+ name: name.join(";"),
171
+ };
172
+ })
173
+ );
174
 
175
  if (usageLimits?.messageLength && (newPrompt?.length ?? 0) > usageLimits.messageLength) {
176
  throw error(400, "Message too long.");