matt HOFFNER commited on
Commit
3514880
·
1 Parent(s): a4ebd61
Files changed (1) hide show
  1. src/app/search/web/page.jsx +26 -15
src/app/search/web/page.jsx CHANGED
@@ -26,24 +26,35 @@ export default function WebSearchPage({ searchParams }) {
26
  throw new Error(`HTTP error! status: ${response.status}`);
27
  } else {
28
  const reader = response.body.getReader();
29
- let chunks = [];
 
30
 
31
  while (true) {
32
- const { done, value } = await reader.read();
33
 
34
- if (done) break;
35
-
36
- chunks.push(value);
37
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- const text = new TextDecoder().decode(new Uint8Array(chunks.flat()));
40
-
41
- try {
42
- const json = JSON.parse(text);
43
- console.log(json);
44
- setAiResponse(json);
45
- } catch (error) {
46
- console.error("Failed to parse JSON", error);
47
  }
48
  }
49
  }
@@ -53,7 +64,7 @@ export default function WebSearchPage({ searchParams }) {
53
  });
54
 
55
  return () => controller.abort();
56
- }, [searchParams, searchTerm]);
57
 
58
 
59
  console.log(aiResponse);
 
26
  throw new Error(`HTTP error! status: ${response.status}`);
27
  } else {
28
  const reader = response.body.getReader();
29
+ const decoder = new TextDecoder();
30
+ let text = '';
31
 
32
  while (true) {
33
+ const { value, done } = await reader.read();
34
 
35
+ if (value) {
36
+ text += decoder.decode(value, {stream: true});
37
+ let boundary = text.indexOf('\n');
38
+
39
+ while (boundary !== -1) {
40
+ const jsonStr = text.slice(0, boundary);
41
+ text = text.slice(boundary + 1);
42
+ try {
43
+ const json = JSON.parse(jsonStr);
44
+ console.log(json);
45
+ setAiResponse(prevResponse => [...prevResponse, json]);
46
+ } catch (error) {
47
+ console.error("Failed to parse JSON", error);
48
+ }
49
+
50
+ boundary = text.indexOf('\n');
51
+ }
52
+ }
53
 
54
+ if (done) {
55
+ console.log("Stream complete");
56
+ break;
57
+ }
 
 
 
 
58
  }
59
  }
60
  }
 
64
  });
65
 
66
  return () => controller.abort();
67
+ }, [searchParams, searchTerm]);
68
 
69
 
70
  console.log(aiResponse);