Skip to content

Commit

Permalink
chore: refactor message store to use zustand (better React State Mana…
Browse files Browse the repository at this point in the history
…gemnt) (#1495)

- reduces complexity by adding complexity

Signed-off-by: Ryan Hopper-Lowe <ryan@acorn.io>
  • Loading branch information
ryanhopperlowe authored Jan 29, 2025
1 parent dc55af4 commit c1e6eb2
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
DialogTitle,
} from "~/components/ui/dialog";
import { Link } from "~/components/ui/link";
import { useThreadEvents } from "~/hooks/messages/useThreadEvents";
import { useInitMessageStore } from "~/hooks/messages/useMessageStore";

type AgentAuthenticationDialogProps = {
threadId: Nullish<string>;
Expand All @@ -30,7 +30,7 @@ export function ToolAuthenticationDialog({
}: AgentAuthenticationDialogProps) {
const { icon, label } = useToolReference(tool);

const { messages: _messages } = useThreadEvents(threadId);
const { messages: _messages } = useInitMessageStore(threadId);

type ItemState = {
isLoading?: boolean;
Expand Down
10 changes: 4 additions & 6 deletions ui/admin/app/components/chat/ChatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ import { ReactNode, createContext, useContext } from "react";
import { mutate } from "swr";

import { AgentIcons } from "~/lib/model/agents";
import { Message } from "~/lib/model/messages";
import { InvokeService } from "~/lib/service/api/invokeService";
import { ThreadsService } from "~/lib/service/api/threadsService";
import { MessageStore } from "~/lib/store/chat/message-store";

import { useThreadEvents } from "~/hooks/messages/useThreadEvents";
import { useInitMessageStore } from "~/hooks/messages/useMessageStore";
import { useAsync } from "~/hooks/useAsync";

type Mode = "agent" | "workflow";

interface ChatContextType {
messages: Message[];
interface ChatContextType extends Pick<MessageStore, "messages" | "isRunning"> {
mode: Mode;
processUserMessage: (text: string) => void;
abortRunningThread: () => void;
id: string;
threadId: Nullish<string>;
invoke: (prompt?: string) => void;
readOnly?: boolean;
isRunning: boolean;
isInvoking: boolean;
introductionMessage?: string;
starterMessages?: string[];
Expand Down Expand Up @@ -73,7 +71,7 @@ export function ChatProvider({
},
});

const { messages, isRunning } = useThreadEvents(threadId);
const { messages, isRunning } = useInitMessageStore(threadId);

const abortRunningThread = () => {
if (!threadId || !isRunning) return;
Expand Down
20 changes: 20 additions & 0 deletions ui/admin/app/hooks/messages/useMessageStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect, useState } from "react";
import { useStore } from "zustand";

import { createMessageStore } from "~/lib/store/chat/message-store";

export const useInitMessageStore = (threadId: Nullish<string>) => {
const [storeObj] = useState(() => createMessageStore());
const store = useStore(storeObj);

const { init, reset } = store;
useEffect(() => {
if (!threadId) return;

init(threadId);

return () => reset();
}, [init, reset, threadId]);

return store;
};
170 changes: 0 additions & 170 deletions ui/admin/app/hooks/messages/useThreadEvents.ts

This file was deleted.

Loading

0 comments on commit c1e6eb2

Please sign in to comment.