| 1 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 2 | import StreamErrorDialog from "../../pages/gui/StreamError"; |
| 3 | import { setInactive } from "../slices/sessionSlice"; |
| 4 | import { setDialogMessage, setShowDialog } from "../slices/uiSlice"; |
| 5 | import { ThunkApiType } from "../store"; |
| 6 | import { cancelStream } from "./cancelStream"; |
| 7 | import { saveCurrentSession } from "./session"; |
| 8 | |
| 9 | export const streamThunkWrapper = createAsyncThunk< |
| 10 | void, |
| 11 | () => Promise<void>, |
| 12 | ThunkApiType |
| 13 | >("chat/streamWrapper", async (runStream, { dispatch, extra, getState }) => { |
| 14 | try { |
| 15 | await runStream(); |
| 16 | const state = getState(); |
| 17 | if (state.session.mode === "chat" || state.session.mode === "agent") { |
| 18 | await dispatch( |
| 19 | saveCurrentSession({ |
| 20 | openNewSession: false, |
| 21 | generateTitle: true, |
| 22 | }), |
| 23 | ); |
| 24 | } |
| 25 | dispatch(setInactive()); |
| 26 | } catch (e) { |
| 27 | dispatch(cancelStream()); |
| 28 | dispatch(setDialogMessage(<StreamErrorDialog error={e} />)); |
| 29 | dispatch(setShowDialog(true)); |
| 30 | } |
| 31 | }); |