matt HOFFNER commited on
Commit
7a6cf6a
·
1 Parent(s): ecb5f4b
Files changed (1) hide show
  1. src/app/search/web/page.jsx +25 -15
src/app/search/web/page.jsx CHANGED
@@ -21,23 +21,31 @@ export default function WebSearchPage({ searchParams }) {
21
  body: JSON.stringify({ question: searchTerm || "Seattle activities this weekend" }),
22
  signal,
23
  });
24
-
25
  if (!response.ok) {
26
  throw new Error(`HTTP error! status: ${response.status}`);
27
  } else {
28
- // Consume the stream as text
29
- let responseText = await response.text();
30
-
31
- // Try to parse the JSON
32
- let json;
33
- try {
34
- json = JSON.parse(responseText);
35
- } catch (error) {
36
- console.error("Failed to parse JSON", error);
37
- }
38
-
39
- console.log(json);
40
- setAiResponse(json);
 
 
 
 
 
 
 
 
41
  }
42
  }
43
 
@@ -46,7 +54,9 @@ export default function WebSearchPage({ searchParams }) {
46
  });
47
 
48
  return () => controller.abort();
49
- }, [searchParams, searchTerm]);
 
 
50
 
51
  console.log(aiResponse);
52
 
 
21
  body: JSON.stringify({ question: searchTerm || "Seattle activities this weekend" }),
22
  signal,
23
  });
24
+
25
  if (!response.ok) {
26
  throw new Error(`HTTP error! status: ${response.status}`);
27
  } else {
28
+ const reader = response.body.getReader();
29
+ const decoder = new TextDecoder();
30
+ let chunks = '';
31
+
32
+ return reader.read().then(function processText({ done, value }) {
33
+ if (done) {
34
+ let json;
35
+ try {
36
+ json = JSON.parse(chunks);
37
+ } catch (error) {
38
+ console.error("Failed to parse JSON", error);
39
+ }
40
+
41
+ console.log(json);
42
+ setAiResponse(json);
43
+ console.log("Stream complete");
44
+ return;
45
+ }
46
+ chunks += decoder.decode(value, {stream: true});
47
+ return reader.read().then(processText);
48
+ });
49
  }
50
  }
51
 
 
54
  });
55
 
56
  return () => controller.abort();
57
+ }, [searchParams, searchTerm]);
58
+
59
+
60
 
61
  console.log(aiResponse);
62