Created
June 13, 2024 16:59
-
-
Save engineersamuel/a9aec29a8b809a141adb1fd252eb5d2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const startStream = useCallback(async (input: unknown, config: unknown) => { | |
const controller = new AbortController(); | |
setController(controller); | |
startRef.current?.({ input }); | |
let innerLatest: RunState | null = null; | |
await fetchEventSource(resolveApiUrl("/stream_log").toString(), { | |
signal: controller.signal, | |
method: "POST", | |
headers: { "Content-Type": "application/json" }, | |
body: JSON.stringify({ input, config }), | |
onmessage(msg) { | |
if (msg.event === "data") { | |
innerLatest = reducer(innerLatest, JSON.parse(msg.data)?.ops); | |
setLatest(innerLatest); | |
chunkRef.current?.(JSON.parse(msg.data), innerLatest); | |
} | |
}, | |
openWhenHidden: true, | |
onclose() { | |
setController(null); | |
successRef.current?.({ input, output: innerLatest?.final_output }); | |
}, | |
onerror(error) { | |
setController(null); | |
errorRef.current?.(error); | |
throw error; | |
}, | |
}); | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment