Skip to content

Commit

Permalink
refactor: update type definations and fix some. bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMcIntosh committed Jan 4, 2024
1 parent 6edaa9e commit 0ce5078
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/components/ChatForm/CharForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ describe("ChatForm", () => {
error=""
clearError={vi.fn}
canChangeModel={false}
handleContextFile={vi.fn}
hasContextFile={false}
/>,
);

Expand Down Expand Up @@ -45,6 +47,8 @@ describe("ChatForm", () => {
error=""
clearError={vi.fn}
canChangeModel={false}
handleContextFile={vi.fn}
hasContextFile={false}
/>,
);
const textarea = app.container.querySelector("textarea");
Expand Down
2 changes: 2 additions & 0 deletions src/components/ChatForm/ChatForm.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const meta = {
error: null,
clearError: noop,
canChangeModel: true,
hasContextFile: false,
handleContextFile: noop,
},
} satisfies Meta<typeof ChatForm>;

Expand Down
2 changes: 1 addition & 1 deletion src/features/Chat.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 8 additions & 5 deletions src/hooks/useChatHistory.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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();
Expand All @@ -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,
Expand Down
12 changes: 4 additions & 8 deletions src/hooks/useEventBusForChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ function formatChatResponse(
): ChatMessages {
return response.choices.reduce<ChatMessages>((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]]);
}
Expand All @@ -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],
]);
}

Expand Down
52 changes: 38 additions & 14 deletions src/services/refact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,48 @@ 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;
line1: number;
line2: number;
};

export type ChatContentFileMessage = ["context_file", ChatContextFile];
interface BaseMessage extends Array<string | ChatContextFile> {
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<ChatRole, "context_file">, 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";
}

Expand All @@ -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
Expand Down

0 comments on commit 0ce5078

Please sign in to comment.