Skip to content

Commit

Permalink
fix: vscode may send the context file as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMcIntosh committed Jan 18, 2024
1 parent bcebcdc commit 07299e1
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/hooks/useEventBusForChat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useReducer, useRef } from "react";
import {
ChatContextFile,
ChatMessages,
ChatResponse,
isChatContextFileMessage,
Expand Down Expand Up @@ -111,12 +112,28 @@ function reducer(state: ChatState, action: ActionToChat): ChatState {
}

if (!isThisChat && isRestoreChat(action)) {
const messages: ChatMessages = action.payload.messages.map((message) => {
if (message[0] === "context_file" && typeof message[1] === "string") {
let file: ChatContextFile[] = [];
try {
file = JSON.parse(message[1]) as ChatContextFile[];
} catch {
file = [];
}
return [message[0], file];
}

return message;
});
return {
...state,
waiting_for_response: false,
streaming: false,
error: null,
chat: action.payload,
chat: {
...action.payload,
messages,
},
};
}

Expand Down Expand Up @@ -287,6 +304,7 @@ export const useEventBusForChat = () => {
useEffect(() => {
const listener = (event: MessageEvent) => {
if (isActionToChat(event.data)) {
console.log(event.data);

Check warning on line 307 in src/hooks/useEventBusForChat.ts

View workflow job for this annotation

GitHub Actions / build (lts/*)

Unexpected console statement

Check warning on line 307 in src/hooks/useEventBusForChat.ts

View workflow job for this annotation

GitHub Actions / build (latest)

Unexpected console statement
dispatch(event.data);
}

Expand Down Expand Up @@ -404,6 +422,34 @@ export const useEventBusForChat = () => {
}
}

function backFromChat() {
postMessage({
type: EVENT_NAMES_FROM_CHAT.BACK_FROM_CHAT,
payload: { id: state.chat.id },
});
}

function openChatInNewTab() {
postMessage({
type: EVENT_NAMES_FROM_CHAT.OPEN_IN_CHAT_IN_TAB,
payload: { id: state.chat.id },
});
}

function sendToSideBar() {
postMessage({
type: EVENT_NAMES_FROM_CHAT.SEND_TO_SIDE_BAR,
payload: { id: state.chat.id },
});
}

function sendReadyMessage() {
postMessage({
type: EVENT_NAMES_FROM_CHAT.READY,
payload: { id: state.chat.id },
});
}

return {
state,
askQuestion,
Expand All @@ -413,5 +459,9 @@ export const useEventBusForChat = () => {
stopStreaming,
handleContextFile,
hasContextFile,
backFromChat,
openChatInNewTab,
sendToSideBar,
sendReadyMessage,
};
};

0 comments on commit 07299e1

Please sign in to comment.