From 0ce50783642994bdaf0e0a5269dc41b48b4a41d8 Mon Sep 17 00:00:00 2001 From: Marc McIntosh Date: Thu, 4 Jan 2024 17:48:10 +0100 Subject: [PATCH] refactor: update type definations and fix some. bugs --- src/components/ChatForm/CharForm.test.tsx | 4 ++ src/components/ChatForm/ChatForm.stories.tsx | 2 + src/features/Chat.test.tsx | 2 +- src/hooks/useChatHistory.ts | 13 +++-- src/hooks/useEventBusForChat.ts | 12 ++--- src/services/refact.ts | 52 ++++++++++++++------ 6 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/components/ChatForm/CharForm.test.tsx b/src/components/ChatForm/CharForm.test.tsx index cc9b7647..622b3e64 100644 --- a/src/components/ChatForm/CharForm.test.tsx +++ b/src/components/ChatForm/CharForm.test.tsx @@ -17,6 +17,8 @@ describe("ChatForm", () => { error="" clearError={vi.fn} canChangeModel={false} + handleContextFile={vi.fn} + hasContextFile={false} />, ); @@ -45,6 +47,8 @@ describe("ChatForm", () => { error="" clearError={vi.fn} canChangeModel={false} + handleContextFile={vi.fn} + hasContextFile={false} />, ); const textarea = app.container.querySelector("textarea"); diff --git a/src/components/ChatForm/ChatForm.stories.tsx b/src/components/ChatForm/ChatForm.stories.tsx index e71fd2d4..51389a35 100644 --- a/src/components/ChatForm/ChatForm.stories.tsx +++ b/src/components/ChatForm/ChatForm.stories.tsx @@ -26,6 +26,8 @@ const meta = { error: null, clearError: noop, canChangeModel: true, + hasContextFile: false, + handleContextFile: noop, }, } satisfies Meta; diff --git a/src/features/Chat.test.tsx b/src/features/Chat.test.tsx index 51d51259..81e9d959 100644 --- a/src/features/Chat.test.tsx +++ b/src/features/Chat.test.tsx @@ -127,7 +127,7 @@ describe("Chat", () => { postMessage(restoreChatAction); - const firstMessage = MARS_ROVER_CHAT.messages[0][1]; + const firstMessage = MARS_ROVER_CHAT.messages[0][1] as string; postMessage(restoreChatAction); diff --git a/src/hooks/useChatHistory.ts b/src/hooks/useChatHistory.ts index d681d80b..9ec41b30 100644 --- a/src/hooks/useChatHistory.ts +++ b/src/hooks/useChatHistory.ts @@ -1,5 +1,5 @@ import { useLocalStorage } from "usehooks-ts"; -import { ChatMessages } from "../services/refact"; +import { ChatMessages, isUserMessage } from "../services/refact"; import { ChatThread, EVENT_NAMES_TO_CHAT } from "../events"; export type ChatHistoryItem = { @@ -17,7 +17,6 @@ export function useChatHistory() { [], ); - // TODO: add model function saveChat(chat: ChatThread | ChatHistoryItem) { const maybeChat = history.find((item) => item.id === chat.id); const now = new Date().toISOString(); @@ -31,10 +30,14 @@ export function useChatHistory() { setHistory(chats); } else { - const firstMessage = chat.messages.find( - (message) => message[0] === "user", + const firstUserMessage = chat.messages.find((message) => + isUserMessage(message), ); - const title = firstMessage?.[1].replace(/^\W*/, "") ?? "New Chat"; + let title = "New Chat"; + + if (firstUserMessage && isUserMessage(firstUserMessage)) { + title = firstUserMessage[1].replace(/^\W*/, ""); + } const newChat: ChatHistoryItem = { id: chat.id, diff --git a/src/hooks/useEventBusForChat.ts b/src/hooks/useEventBusForChat.ts index a0a8bcf7..12bf5e04 100644 --- a/src/hooks/useEventBusForChat.ts +++ b/src/hooks/useEventBusForChat.ts @@ -54,9 +54,9 @@ function formatChatResponse( ): ChatMessages { return response.choices.reduce((acc, cur) => { // TBD: chat doesn't seem to respond with a context file - if (cur.delta.role === "context_file") { - return acc.concat([[cur.delta.role, cur.delta.file_content || ""]]); - } + // if (cur.delta.role === "context_file") { + // return acc.concat([[cur.delta.role, cur.delta.file_content || ""]]); + // } if (acc.length === 0) { return acc.concat([[cur.delta.role, cur.delta.content]]); } @@ -65,12 +65,8 @@ function formatChatResponse( if (lastMessage[0] === cur.delta.role) { const last = acc.slice(0, -1); const currentMessage = lastMessage[1]; - const currentContent = - typeof currentMessage === "string" - ? currentMessage - : currentMessage.file_content; return last.concat([ - [cur.delta.role, currentContent + cur.delta.content], + [cur.delta.role, currentMessage + cur.delta.content], ]); } diff --git a/src/services/refact.ts b/src/services/refact.ts index 7d95fd50..38e29347 100644 --- a/src/services/refact.ts +++ b/src/services/refact.ts @@ -3,6 +3,7 @@ const CHAT_URL = `${REFACT_URL}/v1/chat`; const CAPS_URL = `${REFACT_URL}/v1/caps`; export type ChatRole = "user" | "assistant" | "context_file"; + export type ChatContextFile = { file_name: string; file_content: string; @@ -10,16 +11,40 @@ export type ChatContextFile = { line2: number; }; -export type ChatContentFileMessage = ["context_file", ChatContextFile]; +interface BaseMessage extends Array { + 0: ChatRole; + 1: string | ChatContextFile; +} + +export interface ChatContextFileMessage extends BaseMessage { + 0: "context_file"; + 1: ChatContextFile; +} + +export interface UserMessage extends BaseMessage { + 0: "user"; + 1: string; +} + +export interface AssistantMessage extends BaseMessage { + 0: "assistant"; + 1: string; +} + +export function isUserMessage(message: ChatMessage): message is UserMessage { + return message[0] === "user"; +} + export type ChatMessage = - | [Omit, string] - | ChatContentFileMessage; + | UserMessage + | AssistantMessage + | ChatContextFileMessage; export type ChatMessages = ChatMessage[]; export function isChatContextFileMessage( message: ChatMessage, -): message is ChatContentFileMessage { +): message is ChatContextFileMessage { return message[0] === "context_file"; } @@ -33,18 +58,17 @@ interface AssistantDelta extends BaseDelta { } // TODO: confirm UserDelta and ContextFileDelta are sent frm the lsp -interface ChatContextFileDelta extends BaseDelta { - role: "context_file"; - file_content: string; -} +// interface ChatContextFileDelta extends BaseDelta { +// role: "context_file"; +// file_content: string; +// } -interface UserDelta extends BaseDelta { - role: "user"; - content: string; -} +// interface UserDelta extends BaseDelta { +// role: "user"; +// content: string; +// } -type Delta = UserDelta | AssistantDelta | ChatContextFileDelta; -// interface Delta extends UserDelta, AssistantDelta , ChatContextFile {} +type Delta = AssistantDelta; // UserDelta | AssistantDelta | ChatContextFileDelta; export type ChatChoice = { delta: Delta; // TODO: so far I've only seen AssistantDelta come from the lsp