nsarrazin HF staff commited on
Commit
0aa57de
1 Parent(s): 922b1b2

Add support for websearch retries (#318)

Browse files

* web search retries

* remove test error lol

src/lib/components/OpenWebSearchResults.svelte CHANGED
@@ -13,7 +13,7 @@
13
 
14
  let detailsOpen: boolean;
15
  let error: boolean;
16
- $: error = webSearchMessages.some((message) => message.type === "error");
17
  </script>
18
 
19
  <details
 
13
 
14
  let detailsOpen: boolean;
15
  let error: boolean;
16
+ $: error = webSearchMessages[webSearchMessages.length - 2]?.type === "error";
17
  </script>
18
 
19
  <details
src/routes/conversation/[id]/web-search/+server.ts CHANGED
@@ -58,9 +58,9 @@ export async function GET({ params, locals, url }) {
58
  updatedAt: new Date(),
59
  };
60
 
61
- function appendUpdate(message: string, args?: string[]) {
62
  webSearch.messages.push({
63
- type: "update",
64
  message,
65
  args,
66
  });
@@ -91,12 +91,20 @@ export async function GET({ params, locals, url }) {
91
  text = webSearch.knowledgeGraph;
92
  appendUpdate("Found a Google knowledge page");
93
  } else if (webSearch.results.length > 0) {
94
- // otherwise we use the top result from search
95
- const topUrl = webSearch.results[0];
96
- appendUpdate("Browsing first result", [JSON.stringify(topUrl)]);
97
-
98
- text = await parseWeb(topUrl);
99
- if (!text) throw new Error("text of the webpage is null");
 
 
 
 
 
 
 
 
100
  } else {
101
  throw new Error("No results found for this search query");
102
  }
 
58
  updatedAt: new Date(),
59
  };
60
 
61
+ function appendUpdate(message: string, args?: string[], type?: "error" | "update") {
62
  webSearch.messages.push({
63
+ type: type ?? "update",
64
  message,
65
  args,
66
  });
 
91
  text = webSearch.knowledgeGraph;
92
  appendUpdate("Found a Google knowledge page");
93
  } else if (webSearch.results.length > 0) {
94
+ let tries = 0;
95
+
96
+ while (!text && tries < 3) {
97
+ const searchUrl = webSearch.results[tries];
98
+ appendUpdate("Browsing result", [JSON.stringify(searchUrl)]);
99
+ try {
100
+ text = await parseWeb(searchUrl);
101
+ if (!text) throw new Error("text of the webpage is null");
102
+ } catch (e) {
103
+ appendUpdate("Error parsing webpage", [], "error");
104
+ tries++;
105
+ }
106
+ }
107
+ if (!text) throw new Error("No text found on the first 3 results");
108
  } else {
109
  throw new Error("No results found for this search query");
110
  }