Skip to content

Commit

Permalink
cleanup error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gempir committed Mar 4, 2024
1 parent 27872b9 commit a0d2064
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
9 changes: 6 additions & 3 deletions web/src/components/George/George.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export function George() {
const loadRef = useRef<NodeJS.Timeout>();
const [loading, setLoading] = useState<boolean>(false);

const [doReq, abortController] = useGeorge();
const abortController = useRef(new AbortController());

const [doReq] = useGeorge();

const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
clearInterval(loadRef.current);
Expand Down Expand Up @@ -45,7 +47,8 @@ export function George() {
loadRef.current = setInterval(() => {
setResp(prev => prev + ".");
}, 1000);
doReq(req, (text: string) => {
abortController.current = new AbortController();
doReq(req, abortController.current, (text: string) => {
clearInterval(loadRef.current);
if (text === "@DONE") {
setLoading(false);
Expand All @@ -59,7 +62,7 @@ export function George() {

const abort = () => {
try {
abortController.abort();
abortController.current.abort();
} catch (e) {
console.error(e);
}
Expand Down
90 changes: 44 additions & 46 deletions web/src/hooks/useGeorge.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { useRef } from 'react';
import { Method } from '../service/doFetch';
import { useStore } from '../store';

type FetchStreamResponse = {
data: string | null;
error: Error | null;
loading: boolean;
};

interface GeorgeRequest {
channel: string;
username: string;
Expand All @@ -18,59 +11,64 @@ interface GeorgeRequest {
model: string;
}

type RequestFunc = (req: GeorgeRequest, onText: (text: string) => void, onQuery: (text: string) => void) => void;
type RequestFunc = (req: GeorgeRequest, controller: AbortController, onText: (text: string) => void, onQuery: (text: string) => void) => void;

export const useGeorge = (): [RequestFunc, AbortController] => {
export const useGeorge = (): [RequestFunc] => {
const apiBaseUrl = useStore(state => state.apiBaseUrl);
const scToken = useStore(state => state.scToken);

const controller = useRef<AbortController>(new AbortController());
const request = async (req: GeorgeRequest, controller: AbortController, onText: (text: string) => void, onQuery: (text: string) => void) => {
try {
const response = await fetch(apiBaseUrl + "/api/george", {
method: Method.POST,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${scToken}`
},
body: JSON.stringify(req),
signal: controller.signal
});

const request = async (req: GeorgeRequest, onText: (text: string) => void, onQuery: (text: string) => void) => {
const response = await fetch(apiBaseUrl + "/api/george", {
method: Method.POST,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${scToken}`
},
body: JSON.stringify(req),
signal: controller.current.signal
});
const reader = response.body?.getReader();
if (!reader) {
onText("Failed to read response body");
return "";
}

const reader = response.body?.getReader();
if (!reader) {
return "";
}
let result = '';
let queryDone = false;

let result = '';
let queryDone = false;
while (true) {
const { done, value } = await reader.read();
if (done) {
onText("@DONE");
break;
}
const textValue = new TextDecoder().decode(value);
result += textValue;

while (true) {
const { done, value } = await reader.read();
if (done) {
onText("@DONE");
break;
}
const textValue = new TextDecoder().decode(value);
result += textValue;
if (!queryDone && textValue.includes("====QUERYDONE====")) {
onQuery(result.replace("====QUERYDONE====", ""));
result = "";
queryDone = true;
continue;
}

if (!queryDone && textValue.includes("====QUERYDONE====")) {
onQuery(result.replace("====QUERYDONE====", ""));
result = "";
queryDone = true;
continue;
if (queryDone) {
onText(result);
}
}

if (queryDone) {
// something failed
if (!queryDone) {
onText(result);
}
}

// something failed
if (!queryDone) {
onText(result);
} catch (error) {
const err = error as Error;
onText(err?.message);
return;
}
};

return [request, controller.current];
return [request];
};

0 comments on commit a0d2064

Please sign in to comment.