-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindexing.tsx
177 lines (159 loc) · 6.11 KB
/
indexing.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { useEffect, useState } from "react";
import { RepoQueryPages } from "../../ts/types";
import {
REPO_QUERY_COLLECTION_ENDPOINT,
REPO_QUERY_EMBED_ENDPOINT,
} from "../../constants";
export const IndexingPage = ({
ownerName,
repoName,
setCurrentPage,
}: {
ownerName: string;
repoName: string;
setCurrentPage: (page: RepoQueryPages) => void;
}) => {
const [statusMessage, setStatusMessage] = useState(
"We are checking if this repository is indexed. This may take a while.",
);
useEffect(() => {
const processChunk = (chunk: string) => {
const chunkLines = chunk.split("\n");
const [eventLine] = chunkLines;
const event = eventLine.split(": ")[1];
switch (event) {
case "FETCH_REPO":
setStatusMessage("Fetching Repository from GitHub...");
break;
case "EMBED_REPO":
setStatusMessage("Embedding Repository...");
break;
case "SAVE_EMBEDDINGS":
setStatusMessage(
"Saving the embeddings to our database...",
);
break;
case "ERROR":
setStatusMessage(
"There was an error while indexing this repository. Redirecting to the Home Page.",
);
setTimeout(() => {
setCurrentPage(RepoQueryPages.Home);
}, 3000);
break;
case "DONE":
setStatusMessage(
"Indexing Complete. Redirecting to the Chat Dialog.",
);
setTimeout(
() => {
setCurrentPage(RepoQueryPages.Chat);
},
1000,
);
break;
default:
break;
}
};
async function checkIndexingStatus() {
const response = await fetch(
`${REPO_QUERY_COLLECTION_ENDPOINT}?owner=${ownerName}&name=${repoName}&branch=HEAD`,
);
if (response.status !== 200) {
setStatusMessage(
"This repository is not indexed. We are indexing it now. This may take a while.",
);
const embedResponse = await fetch(
`${REPO_QUERY_EMBED_ENDPOINT}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
owner: ownerName,
name: repoName,
branch: "HEAD",
}),
},
);
if (embedResponse.status === 403) {
setStatusMessage(
"This repository's license does not allow us to index it. Redirecting to the Home Page.",
);
setTimeout(() => {
setCurrentPage(RepoQueryPages.Home);
}, 3000);
return;
}
if (embedResponse.status !== 200) {
setStatusMessage(
"There was an error while indexing this repository. Redirecting to the Home Page.",
);
setTimeout(() => {
setCurrentPage(RepoQueryPages.Home);
}, 3000);
return;
}
const reader = embedResponse.body?.getReader();
if (reader) {
const decoder = new TextDecoder("utf-8");
const { done: readerDone } = await reader.read();
// eslint-disable-next-line no-loops/no-loops
while (!readerDone) {
// eslint-disable-next-line no-await-in-loop
const { value, done } = await reader.read();
const chunkString = decoder.decode(value);
if (chunkString.includes("event:")) {
processChunk(chunkString);
}
if (done) {
break;
}
}
}
} else {
setStatusMessage(
"This repository is already indexed! Redirecting to the Chat Dialog.",
);
setTimeout(() => {
setCurrentPage(RepoQueryPages.Chat);
}, 1000);
}
}
void checkIndexingStatus();
}, []);
return (
<div
className="flex flex-col justify-center items-center w-full h-full"
id="chat-dialog-body-loading-spinner"
>
<div className="text-2xl font-bold text-slate-700">RepoQuery</div>
<p className="text-sm text-gray-500 w-5/6 text-center mt-4">
{statusMessage}
</p>
<div className="mt-4">
<svg
className="animate-spin -ml-1 mr-3 h-5 w-5 text-orange"
fill="none"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0
004 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
</div>
</div>
);
};