Skip to content

Commit

Permalink
Get it working
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Apr 16, 2024
1 parent b875f90 commit 1f64249
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 34 deletions.
7 changes: 4 additions & 3 deletions frontend/src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ function CommitEdits(props: {

export function Chat(props: ChatProps) {
const { chatId } = useParams();
const { messages, next } = useChatMessages(
const { messages, next, refreshMessages } = useChatMessages(
chatId ?? null,
props.stream,
props.stopStream,
);
const { currentChat, assistantConfig, isLoading } = useThreadAndAssistant();
const { editing, recordEdits, commitEdits, abandonEdits } = useMessageEditing(
currentChat?.thread_id,
chatId,
refreshMessages,
);

const prevMessages = usePrevious(messages);
Expand All @@ -94,7 +95,7 @@ export function Chat(props: ChatProps) {
editing[msg.id] ? (
<MessageEditor
key={msg.id}
message={msg}
message={editing[msg.id]}
onUpdate={recordEdits}
abandonEdits={() => abandonEdits(msg)}
/>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/MessageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
PlusCircleIcon,
TrashIcon,
} from "@heroicons/react/24/outline";
import { AutosizeTextarea } from "./StringEditor";
import { StringEditor } from "./StringEditor";
import { JsonEditor } from "./JsonEditor";

// TODO adapt (and use) or remove
Expand Down Expand Up @@ -99,7 +99,7 @@ function Function(props: {
)}
>
<div className="flex items-center">
<AutosizeTextarea
<StringEditor
className="py-0 px-0 prose text-sm leading-normal bg-white"
value={str(value)?.toString()}
onChange={(newValue) => {
Expand Down Expand Up @@ -184,7 +184,7 @@ export function MessageContentEditor(props: {
return null;
}
return (
<AutosizeTextarea
<StringEditor
onChange={(newValue) =>
props.onUpdate({
...props.message,
Expand Down
13 changes: 1 addition & 12 deletions frontend/src/components/StringEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { Ref } from "react";
import { cn } from "../utils/cn";

const COMMON_CLS = cn(
"text-sm col-[1] row-[1] m-0 resize-none overflow-hidden whitespace-pre-wrap break-words bg-transparent px-2 py-1 rounded shadow-none",
);

export function AutosizeTextarea(props: {
id?: string;
inputRef?: Ref<HTMLTextAreaElement>;
export function StringEditor(props: {
value?: string | null | undefined;
placeholder?: string;
className?: string;
onChange?: (e: string) => void;
onFocus?: () => void;
onBlur?: () => void;
onKeyDown?: (e: React.KeyboardEvent<HTMLTextAreaElement>) => void;
autoFocus?: boolean;
readOnly?: boolean;
cursorPointer?: boolean;
Expand All @@ -29,8 +23,6 @@ export function AutosizeTextarea(props: {
}
>
<textarea
ref={props.inputRef}
id={props.id}
className={cn(
COMMON_CLS,
"text-transparent caret-black rounded focus:outline-0 focus:ring-0",
Expand All @@ -42,12 +34,9 @@ export function AutosizeTextarea(props: {
const target = e.target as HTMLTextAreaElement;
props.onChange?.(target.value);
}}
onFocus={props.onFocus}
onBlur={props.onBlur}
placeholder={props.placeholder}
readOnly={props.readOnly}
autoFocus={props.autoFocus && !props.readOnly}
onKeyDown={props.onKeyDown}
/>
<div
aria-hidden
Expand Down
26 changes: 13 additions & 13 deletions frontend/src/hooks/useChatMessages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useRef, useState } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Message } from "../types";
import { StreamState, mergeMessagesById } from "./useStreamState";

Expand All @@ -23,26 +23,25 @@ export function useChatMessages(
threadId: string | null,
stream: StreamState | null,
stopStream?: (clear?: boolean) => void,
): { messages: Message[] | null; next: string[] } {
) {
const [messages, setMessages] = useState<Message[] | null>(null);
const [next, setNext] = useState<string[]>([]);
const prevStreamStatus = usePrevious(stream?.status);

useEffect(() => {
async function fetchMessages() {
if (threadId) {
const { values, next } = await getState(threadId);
setMessages(values);
setNext(next);
}
const refreshMessages = useCallback(async () => {
if (threadId) {
const { values, next } = await getState(threadId);
setMessages(values);
setNext(next);
}
}, [threadId]);

fetchMessages();

useEffect(() => {
refreshMessages();
return () => {
setMessages(null);
};
}, [threadId]);
}, [threadId, refreshMessages]);

useEffect(() => {
async function fetchMessages() {
Expand All @@ -64,9 +63,10 @@ export function useChatMessages(

return useMemo(
() => ({
refreshMessages,
messages: mergeMessagesById(messages, stream?.messages),
next,
}),
[messages, stream?.messages, next],
[messages, stream?.messages, next, refreshMessages],
);
}
24 changes: 21 additions & 3 deletions frontend/src/hooks/useMessageEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@ import { useCallback, useState } from "react";
import { Message } from "../types";
import { omit } from "lodash";

export function useMessageEditing(threadId?: string) {
export function useMessageEditing(
threadId: string | undefined,
onSuccess: () => void,
) {
const [editing, setEditing] = useState<Record<string, Message>>({});

const recordEdits = useCallback((msg: Message) => {
setEditing((current) => ({ ...current, [msg.id]: msg }));
}, []);
const commitEdits = useCallback(async () => {
if (!threadId) return;
// TODO
}, [threadId]);
fetch(`/threads/${threadId}/state`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ values: Object.values(editing) }),
})
.then((res) => {
if (res.ok) {
setEditing({});
onSuccess();
} else {
return Promise.reject(res.statusText);
}
})
.catch((e) => {
console.error(e);
});
}, [threadId, editing, onSuccess]);
const abandonEdits = useCallback((msg?: Message) => {
if (msg) {
setEditing((current) => {
Expand Down

0 comments on commit 1f64249

Please sign in to comment.