Skip to content

Commit

Permalink
Update api
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Mar 29, 2024
1 parent c88aed6 commit bc39e08
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
4 changes: 2 additions & 2 deletions backend/app/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def get_thread_state(user_id: str, thread_id: str):
state = await app.aget_state({"configurable": {"thread_id": thread_id}})
return {
"values": state.values,
"resumeable": bool(state.next),
"next": state.next,
}


Expand All @@ -124,7 +124,7 @@ async def get_thread_history(user_id: str, thread_id: str):
return [
{
"values": c.values,
"resumeable": bool(c.next),
"next": c.next,
"config": c.config,
"parent": c.parent_config,
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ function usePrevious<T>(value: T): T | undefined {
}

export function Chat(props: ChatProps) {
const { messages: serverMessages, resumeable } = useChatMessages(
const { messages: serverMessages, next } = useChatMessages(
props.chat.thread_id,
props.stream,
props.stopStream,
);
const { histories } = useHistories(props.chat.thread_id, props.stream);
const displayHistories = [...(histories ?? [])]
.reverse()
.filter((history) => history.resumeable)
.filter((history) => history.next.length)
.filter(
(history, i, self) => !deepEquals(history.values, self[i - 1]?.values),
);
Expand Down Expand Up @@ -136,7 +136,7 @@ export function Chat(props: ChatProps) {
"An error has occurred. Please try again."}
</div>
)}
{resumeable &&
{next.length > 0 &&
activeDisplayedHistoryIndex === displayHistories.length - 1 &&
props.stream?.status !== "inflight" && (
<div
Expand Down
30 changes: 14 additions & 16 deletions frontend/src/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { cn } from "../utils/cn";
import { marked } from "marked";
import DOMPurify from "dompurify";
import {
// ArrowUturnLeftIcon,
ArrowUturnLeftIcon,
CheckCircleIcon,
ChevronDownIcon,
PencilSquareIcon,
Expand Down Expand Up @@ -195,13 +195,15 @@ function initializeFunctionArgsEntries(definition: FunctionDefinition) {
function initializeToolCallFunctionArgsEntries(
toolCalls: { function?: FunctionDefinition }[],
) {
return toolCalls.map((toolCall) => {
if (toolCall.function !== undefined) {
return initializeFunctionArgsEntries(toolCall.function ?? "{}");
} else {
return [];
}
});
return toolCalls
.map((toolCall) => {
if (toolCall.function !== undefined) {
return initializeFunctionArgsEntries(toolCall.function ?? "{}");
} else {
return [];
}
})
.filter((args): args is [string, unknown][] => args !== undefined);
}

export const Message = memo(function Message(
Expand All @@ -211,7 +213,7 @@ export const Message = memo(function Message(
editMode: boolean;
setEditMode: (newValue: boolean) => void;
onUpdate: (newValue: MessageType) => void;
// onRewindPressed: () => void;
onRewindPressed: () => void;
},
) {
const { runId, onUpdate, ...messageProps } = props;
Expand Down Expand Up @@ -485,11 +487,7 @@ export const Message = memo(function Message(
return newMessageData;
});
setToolCallFunctionArgsEntries((prev) => {
let oldValues: any[] = [];
if (prev) {
oldValues = [...prev];
}
return [...oldValues, ["", ""]];
return [...(prev || []), [["", ""]]];
});
}}
>
Expand All @@ -498,13 +496,13 @@ export const Message = memo(function Message(
</div>
)}
</div>
{/* <ArrowUturnLeftIcon
<ArrowUturnLeftIcon
className={cn(
"w-6 h-6 ml-2 opacity-0 group-hover:opacity-50 transition-opacity cursor-pointer",
props.editMode ? " invisible pointer-events-none" : "",
)}
onMouseUp={props.onRewindPressed}
/> */}
/>
{props.editMode ? (
<CheckCircleIcon
className="w-6 h-6 cursor-pointer ml-2 shrink-0 opacity-0 group-hover:opacity-50 transition-opacity duration-200"
Expand Down
22 changes: 11 additions & 11 deletions frontend/src/hooks/useChatMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Message } from "./useChatList";
import { StreamState } from "./useStreamState";

async function getState(threadId: string) {
const { values, resumeable } = await fetch(`/threads/${threadId}/state`, {
const { values, next } = await fetch(`/threads/${threadId}/state`, {
headers: {
Accept: "application/json",
},
}).then((r) => r.json());
return { values, resumeable };
return { values, next };
}

function usePrevious<T>(value: T): T | undefined {
Expand All @@ -26,18 +26,18 @@ export function useChatMessages(
): {
messages: Message[] | null;
setMessages: React.Dispatch<React.SetStateAction<Message[] | null>>;
resumeable: boolean;
next: string[];
} {
const [messages, setMessages] = useState<Message[] | null>(null);
const [resumeable, setResumeable] = useState(false);
const [next, setNext] = useState<string[]>([]);
const prevStreamStatus = usePrevious(stream?.status);

useEffect(() => {
async function fetchMessages() {
if (threadId) {
const { values, resumeable } = await getState(threadId);
const { values, next } = await getState(threadId);
setMessages(values);
setResumeable(resumeable);
setNext(next);
}
}

Expand All @@ -51,15 +51,15 @@ export function useChatMessages(
useEffect(() => {
async function fetchMessages() {
if (threadId) {
const { values, resumeable } = await getState(threadId);
const { values, next } = await getState(threadId);
setMessages(values);
setResumeable(resumeable);
setNext(next);
stopStream?.(true);
}
}

if (prevStreamStatus === "inflight" && stream?.status !== "inflight") {
setResumeable(false);
setNext([]);
fetchMessages();
}

Expand All @@ -71,8 +71,8 @@ export function useChatMessages(
messages: stream?.merge
? [...(messages ?? []), ...(stream.messages ?? [])]
: stream?.messages ?? messages,
resumeable,
next,
};
}, [messages, stream?.merge, stream?.messages, resumeable]);
}, [messages, stream?.merge, stream?.messages, next]);
return { ...memoizedValues, setMessages };
}
10 changes: 3 additions & 7 deletions frontend/src/hooks/useHistories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,29 @@ async function getHistories(threadId: string) {

export interface History {
values: MessageType[];
resumeable: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
config: Record<string, any>;
next: string[];
config: Record<string, unknown>;
}

export function useHistories(
threadId: string | null,
stream: StreamState | null,
): {
histories: History[];
resumeable: boolean;
setHistories: React.Dispatch<React.SetStateAction<History[]>>;
} {
const [histories, setHistories] = useState<History[]>([]);
const [resumeable, setResumeable] = useState(false);

useEffect(() => {
async function fetchHistories() {
if (threadId) {
const histories = await getHistories(threadId);
setHistories(histories);
setResumeable(resumeable);
}
}
fetchHistories();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [threadId, stream?.status]);

return { histories, resumeable, setHistories };
return { histories, setHistories };
}

0 comments on commit bc39e08

Please sign in to comment.