enzostvs HF Staff commited on
Commit
e3c353c
·
1 Parent(s): d4c5d1a

update logs

Browse files
Files changed (2) hide show
  1. app/api/ask/route.ts +28 -15
  2. hooks/useAi.ts +36 -7
app/api/ask/route.ts CHANGED
@@ -671,7 +671,8 @@ This project was created with [DeepSite](https://huggingface.co/deepsite).
671
 
672
  let response;
673
  try {
674
- response = await uploadFiles({
 
675
  repo: {
676
  type: "space",
677
  name: repoId,
@@ -680,21 +681,38 @@ This project was created with [DeepSite](https://huggingface.co/deepsite).
680
  commitTitle: prompt,
681
  accessToken: user.token as string,
682
  });
 
 
 
 
 
 
 
 
683
  } catch (uploadError: any) {
684
  console.error("++UPLOAD FILES ERROR++", uploadError);
685
  console.error("++UPLOAD FILES ERROR MESSAGE++", uploadError.message);
686
  console.error("++REPO ID++", repoId);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687
  throw new Error(`Failed to upload files to repository: ${uploadError.message || 'Unknown error'}`);
688
  }
689
-
690
- console.log("++UPLOAD SUCCESS++");
691
- console.log("++RESPONSE STRUCTURE++", JSON.stringify({
692
- hasCommit: !!response?.commit,
693
- commitKeys: response?.commit ? Object.keys(response.commit) : [],
694
- responseKeys: response ? Object.keys(response) : []
695
- }));
696
-
697
- // Safely construct the response
698
  const responseData: any = {
699
  ok: true,
700
  updatedLines,
@@ -702,23 +720,18 @@ This project was created with [DeepSite](https://huggingface.co/deepsite).
702
  repoId,
703
  };
704
 
705
- // Only add commit if it exists and has valid structure
706
  if (response && response.commit) {
707
  responseData.commit = {
708
  ...response.commit,
709
  title: prompt,
710
  };
711
  } else {
712
- console.warn("++NO COMMIT IN RESPONSE++");
713
- // Provide a fallback commit structure
714
  responseData.commit = {
715
  title: prompt,
716
  oid: 'unknown',
717
  };
718
  }
719
 
720
- console.log("++ABOUT TO RETURN JSON++", JSON.stringify(responseData).substring(0, 200));
721
-
722
  return NextResponse.json(responseData);
723
  } else {
724
  return NextResponse.json(
 
671
 
672
  let response;
673
  try {
674
+ // Add a timeout wrapper for the upload
675
+ const uploadPromise = uploadFiles({
676
  repo: {
677
  type: "space",
678
  name: repoId,
 
681
  commitTitle: prompt,
682
  accessToken: user.token as string,
683
  });
684
+
685
+ const uploadTimeout = new Promise<never>((_, reject) => {
686
+ setTimeout(() => {
687
+ reject(new Error('Upload operation timed out'));
688
+ }, 180000); // 3 minutes timeout for upload
689
+ });
690
+
691
+ response = await Promise.race([uploadPromise, uploadTimeout]);
692
  } catch (uploadError: any) {
693
  console.error("++UPLOAD FILES ERROR++", uploadError);
694
  console.error("++UPLOAD FILES ERROR MESSAGE++", uploadError.message);
695
  console.error("++REPO ID++", repoId);
696
+
697
+ // If it's a timeout, files might have been uploaded but we didn't get response
698
+ if (uploadError.message?.includes('timed out') || uploadError.message?.includes('timeout')) {
699
+ console.warn("++UPLOAD TIMEOUT - Files may have been uploaded++");
700
+ // Return a partial success response
701
+ return NextResponse.json({
702
+ ok: true,
703
+ updatedLines,
704
+ pages: updatedPages,
705
+ repoId,
706
+ commit: {
707
+ title: prompt,
708
+ oid: 'timeout',
709
+ timedOut: true,
710
+ }
711
+ });
712
+ }
713
+
714
  throw new Error(`Failed to upload files to repository: ${uploadError.message || 'Unknown error'}`);
715
  }
 
 
 
 
 
 
 
 
 
716
  const responseData: any = {
717
  ok: true,
718
  updatedLines,
 
720
  repoId,
721
  };
722
 
 
723
  if (response && response.commit) {
724
  responseData.commit = {
725
  ...response.commit,
726
  title: prompt,
727
  };
728
  } else {
 
 
729
  responseData.commit = {
730
  title: prompt,
731
  oid: 'unknown',
732
  };
733
  }
734
 
 
 
735
  return NextResponse.json(responseData);
736
  } else {
737
  return NextResponse.json(
hooks/useAi.ts CHANGED
@@ -311,21 +311,50 @@ export const useAi = (onScrollToBottom?: () => void) => {
311
  });
312
 
313
  if (request && request.body) {
314
- // Clone the response so we can read it twice if needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
  const clonedRequest = request.clone();
316
  let res;
317
  try {
318
  res = await request.json();
319
  } catch (jsonError: any) {
320
- console.error("++JSON PARSE ERROR++", jsonError);
321
- // Try to get the actual response text from the clone
322
  try {
323
  const text = await clonedRequest.text();
324
- console.error("++RESPONSE TEXT++", text.substring(0, 1000));
325
- console.error("++RESPONSE STATUS++", clonedRequest.status);
326
- console.error("++RESPONSE HEADERS++", Array.from(clonedRequest.headers.entries()));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  } catch (textError) {
328
- console.error("++UNABLE TO READ RESPONSE TEXT++", textError);
329
  }
330
  setIsAiWorking(false);
331
  toast.error("Server returned invalid response. Check console for details.");
 
311
  });
312
 
313
  if (request && request.body) {
314
+ if (request.status === 504) {
315
+ console.warn("++504 GATEWAY TIMEOUT - Upload likely succeeded but response timed out++");
316
+ console.warn("++REQUEST STATUS++", request.status);
317
+ console.warn("++REQUEST BODY++", request.body);
318
+
319
+ setIsAiWorking(false);
320
+
321
+ if (isNew) {
322
+ toast.error("The request timed out. Your project may have been created. Please check your HuggingFace spaces.");
323
+ return { error: "gateway_timeout", message: "Request timed out after upload" };
324
+ }
325
+
326
+ toast.success("Changes saved! Refreshing page to show updates...", { duration: 3000 });
327
+ setTimeout(() => {
328
+ window.location.reload();
329
+ }, 1000);
330
+ return { success: true, timedOut: true };
331
+ }
332
+
333
  const clonedRequest = request.clone();
334
  let res;
335
  try {
336
  res = await request.json();
337
  } catch (jsonError: any) {
 
 
338
  try {
339
  const text = await clonedRequest.text();
340
+
341
+ // Check if it's a CloudFront/gateway timeout in the HTML
342
+ if (text.includes("504") || text.includes("Gateway Timeout") || text.includes("gateway timeout")) {
343
+ console.warn("++DETECTED 504 IN HTML RESPONSE++");
344
+ setIsAiWorking(false);
345
+
346
+ if (isNew) {
347
+ toast.error("The request timed out. Your project may have been created. Please check your HuggingFace spaces.");
348
+ return { error: "gateway_timeout", message: "Request timed out after upload" };
349
+ }
350
+
351
+ toast.success("Changes saved! Refreshing page to show updates...", { duration: 3000 });
352
+ setTimeout(() => {
353
+ window.location.reload();
354
+ }, 1000);
355
+ return { success: true, timedOut: true };
356
+ }
357
  } catch (textError) {
 
358
  }
359
  setIsAiWorking(false);
360
  toast.error("Server returned invalid response. Check console for details.");