Skip to content

The Open Standard for Generative UI

thread list manager

abhishek330e326a5628
diff --git a/frontend-sdk/package.json b/frontend-sdk/package.json
index 5df87af53..e23f5aa03 100644
--- a/frontend-sdk/package.json
+++ b/frontend-sdk/package.json
@@ -8,5 +8,6 @@
},
"keywords": [],
"author": "",
- "license": "ISC"
+ "license": "ISC",
+ "packageManager": "[email protected]+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
}
diff --git a/frontend-sdk/packages/react-core/src/index.ts b/frontend-sdk/packages/react-core/src/index.ts
index 4095fd9a0..4255d4fe4 100644
--- a/frontend-sdk/packages/react-core/src/index.ts
+++ b/frontend-sdk/packages/react-core/src/index.ts
@@ -4,6 +4,6 @@ export { useThreadActions } from "./hooks/useThreadActions";
export { useThreadListActions } from "./hooks/useThreadListActions";
export { useThreadListState } from "./hooks/useThreadListState";
export { useThreadState } from "./hooks/useThreadState";
-export * from "./types";
export { useThreadListManager } from "./useThreadListManager";
-export { useVercelChatManager } from "./vercel-sdk/useVercelChatManager";
+
+export * from "./types";
diff --git a/frontend-sdk/packages/react-core/src/internal/useThreadListManagerStore.ts b/frontend-sdk/packages/react-core/src/internal/useThreadListManagerStore.ts
index 73fedb67e..2a9a46009 100644
--- a/frontend-sdk/packages/react-core/src/internal/useThreadListManagerStore.ts
+++ b/frontend-sdk/packages/react-core/src/internal/useThreadListManagerStore.ts
@@ -13,8 +13,9 @@ export const useThreadListManagerStore = (inputThreadListManager: ThreadListMana
threads: inputThreadListManager.threads,
isLoading: inputThreadListManager.isLoading,
error: inputThreadListManager.error,
+ selectedThreadId: inputThreadListManager.selectedThreadId,
- switchToNew: () => inputThreadListManagerRef.current.switchToNew(),
+ switchToNewThread: () => inputThreadListManagerRef.current.switchToNewThread(),
load: () => inputThreadListManagerRef.current.load(),
updateThread: (...props) => inputThreadListManagerRef.current.updateThread(...props),
deleteThread: (...props) => inputThreadListManagerRef.current.deleteThread(...props),
diff --git a/frontend-sdk/packages/react-core/src/types.ts b/frontend-sdk/packages/react-core/src/types.ts
deleted file mode 100644
index 06c455191..000000000
--- a/frontend-sdk/packages/react-core/src/types.ts
+++ /dev/null
@@ -1,110 +0,0 @@
-import { Message as VercelMessage } from "ai";
-import { z } from "zod";
-
-/** Represents a chat message, extending Vercel's Message type */
-export type Message = VercelMessage;
-
-/**
- * Represents a message being created, with an optional ID
- * @extends Omit<Message, "id">
- */
-export type CreateMessage = Omit<Message, "id"> & {
- id?: Message["id"];
-};
-
-/**
- * Function type for converting between message types
- * @template T - The source message type
- */
-export type MessageConverter<T = Message> = (message: T) => Message;
-
-export interface GenUITool<T extends z.ZodSchema = z.ZodSchema> {
- name: string;
- description: string;
- parameters: T;
- Component: React.ComponentType<
- z.infer<T> & {
- toolCallId: string;
- addToolResult: (args: { toolCallId: string; result: unknown }) => void;
- }
- >;
-}
-
-/**
- * Represents a chat thread
- */
-export type Thread = {
- /** Unique identifier for the thread */
- threadId: string;
- /** Title of the thread */
- title: string;
- /** Creation timestamp */
- createdAt: Date;
- /** Indicates if the thread is currently processing */
- isRunning: boolean;
-};
-
-/**
- * Actions available for managing a thread
- * @template T - The message type used in the thread
- */
-export type ThreadActions<T = Message> = {
- onNew: (message: CreateMessage) => void;
- onReload?: (messageId: string) => undefined;
- onCancel?: () => void;
- onAddToolResult?:
- | ((props: { toolCallId: string; result: any }) => Promise<void> | void)
- | undefined;
- convertMessage: T extends Message ? MessageConverter<T> | undefined : MessageConverter<T>;
-};
-
-/**
- * Represents the state of a thread
- * @template T - The message type used in the thread
- */
-export type ThreadState<T = Message> = {
- isDisabled?: boolean | undefined;
- isRunning?: boolean | undefined;
- messages: T[];
- error: Error | null | undefined;
- tools: { [id: string]: GenUITool };
-};
-
-/**
- * Combines thread state and actions
- * @template T - The message type used in the thread
- */
-export type ThreadManager<T = Message> = ThreadState<T> & ThreadActions<T>;
-
-/**
- * Represents the state of the thread list
- */
-export type ThreadListState = {
- threads: Thread[];
- isLoading: boolean;
- error: Error | null | undefined;
-};
-
-/**
- * Actions available for managing the thread list
- */
-export type ThreadListActions = {
- load: () => void;
- switchToNew: () => void;
- selectThread: (threadId: string) => void;
- updateThread: (thread: Thread) => void;
- deleteThread: (threadId: string) => void;
-};
-
-/**
- * Combines thread list state and actions
- */
-export type ThreadListManager = ThreadListState & ThreadListActions;
-
-/**
- * Main chat manager combining thread and thread list management
- */
-export type ChatManager = {
- threadListManager: ThreadListManager;
- threadManager: ThreadManager;
-};
diff --git a/frontend-sdk/packages/react-core/src/types/chatManager.ts b/frontend-sdk/packages/react-core/src/types/chatManager.ts
new file mode 100644
index 000000000..9cd46bfd9
--- /dev/null
+++ b/frontend-sdk/packages/react-core/src/types/chatManager.ts
@@ -0,0 +1,77 @@
+import { CreateMessage, Message } from "./message";
+
+/**
+ * Represents a chat thread
+ */
+export type Thread = {
+ /** Unique identifier for the thread */
+ threadId: string;
+ /** Title of the thread */
+ title: string;
+ /** Creation timestamp */
+ createdAt: Date;
+ /** Indicates if the thread is currently processing */
+ isRunning?: boolean;
+};
+
+/**
+ * Actions available for managing a thread
+ * @template T - The message type used in the thread
+ */
+export type ThreadActions = {
+ addMessages: (...messages: CreateMessage[]) => Promise<void>;
+ updateMessage: (message: Message) => void;
+ onCancel: () => void;
+};
+
+/**
+ * Represents the state of a thread
+ */
+export type ThreadState = {
+ isDisabled?: boolean | undefined;
+ /** Indicates if the thread is currently processing and controls should be disabled */
+ isRunning?: boolean | undefined;
+ messages: Message[];
+ error: Error | null | undefined;
+};
+
+/**
+ * Combines thread state and actions
+ * @template T - The message type used in the thread
+ */
+export type ThreadManager = ThreadState & ThreadActions;
+
+/**
+ * Represents the state of the thread list
+ */
+export type ThreadListState = {
+ threads: Thread[];
+ isLoading: boolean;
+ error: Error | null | undefined;
+ selectedThreadId: string | null;
+};
+
+/**
+ * Actions available for managing the thread list
+ */
+export type ThreadListActions = {
+ load: () => void;
+ loadMore: () => void;
+ switchToNewThread: () => void;
+ selectThread: (threadId: string) => void;
+ updateThread: (thread: Thread) => void;
+ deleteThread: (threadId: string) => void;
+};
+
+/**
+ * Combines thread list state and actions
+ */
+export type ThreadListManager = ThreadListState & ThreadListActions;
+
+/**
+ * Main chat manager combining thread and thread list management
+ */
+export type ChatManager = {
+ threadListManager?: ThreadListManager;
+ threadManager: ThreadManager;
+};
diff --git a/frontend-sdk/packages/react-core/src/types/index.ts b/frontend-sdk/packages/react-core/src/types/index.ts
new file mode 100644
index 000000000..0dabe9ccd
--- /dev/null
+++ b/frontend-sdk/packages/react-core/src/types/index.ts
@@ -0,0 +1,3 @@
+export * from "./chatManager";
+export * from "./message";
+export * from "./responseTemplate";
diff --git a/frontend-sdk/packages/react-core/src/types/message.ts b/frontend-sdk/packages/react-core/src/types/message.ts
new file mode 100644
index 000000000..d9dabcc9c
--- /dev/null
+++ b/frontend-sdk/packages/react-core/src/types/message.ts
@@ -0,0 +1,41 @@
+type common = {
+ id: string;
+ isVisuallyHidden?: boolean;
+};
+
+export type UserMessage = common & {
+ role: "user";
+} & (
+ | {
+ type: "prompt";
+ message?: string;
+ }
+ | {
+ type: "action";
+ message?: string;
+ context?: {
+ formState?: Record<string, any>;
+ };
+ actionDetails?: string;
+ }
+ );
+
+export type AssistantMessage = common & {
+ role: "assistant";
+ context?: {
+ uiState?: Record<string, any>;
+ };
+ message?: string;
+ responseTemplate?: {
+ name: string;
+ templateProps: any;
+ };
+};
+
+/**
+ * Represents a message being created, with an optional ID
+ * @extends Omit<Message, "id">
+ */
+export type Message = UserMessage | AssistantMessage;
+
+export type CreateMessage = Omit<Message, "id">;
diff --git a/frontend-sdk/packages/react-core/src/types/responseTemplate.ts b/frontend-sdk/packages/react-core/src/types/responseTemplate.ts
new file mode 100644
index 000000000..2de986f54
--- /dev/null
+++ b/frontend-sdk/packages/react-core/src/types/responseTemplate.ts
@@ -0,0 +1,4 @@
+export interface ResponseTemplate {
+ name: string;
+ Component: React.ComponentType;
+}
diff --git a/frontend-sdk/packages/react-core/src/useThreadListManager.ts b/frontend-sdk/packages/react-core/src/useThreadListManager.ts
index 2abead7fe..d79b2b669 100644
--- a/frontend-sdk/packages/react-core/src/useThreadListManager.ts
+++ b/frontend-sdk/packages/react-core/src/useThreadListManager.ts
@@ -36,10 +36,16 @@ export const useThreadListManager = (props: Props): DefaultManager => {
};
return {
+ selectedThreadId: null,
threads: [] as Thread[],
error: null,
isLoading: true,
- selectThread: (threadId) => propsRef.current.onSelectThread(threadId),
+ selectThread: (threadId) => {
+ set({ selectedThreadId: threadId });
+ propsRef.current.onSelectThread(threadId);
+ },
+ // todo: implement
+ loadMore: () => {},
switchToNew: () => propsRef.current.onSwitchToNew(),
load: () => {
set({ isLoading: true });
@@ -80,6 +86,7 @@ export const useThreadListManager = (props: Props): DefaultManager => {
});
},
addNew: (thread: Thread) => {},
+ switchToNewThread: () => propsRef.current.onSwitchToNew(),
};
});
}, []);
diff --git a/frontend-sdk/packages/react-core/src/useThreadManager.ts b/frontend-sdk/packages/react-core/src/useThreadManager.ts
new file mode 100644
index 000000000..c246e311d
--- /dev/null
+++ b/frontend-sdk/packages/react-core/src/useThreadManager.ts
@@ -0,0 +1,97 @@
+import { useEffect, useMemo, useRef } from "react";
+import { createStore } from "zustand";
+import { CreateMessage, Message, ThreadManager } from "./types";
+
+type Props = {
+ threadId: string | null;
+ loadThread: (threadId: string) => Promise<Message[]>;
+ onAddMessages: (props: {
+ messages: CreateMessage[];
+ threadManager: ThreadManager;
+ abortController: AbortController;
+ }) => Promise<Message[]>;
+};
+
+export const useThreadManager = (props: Props): ThreadManager => {
+ const propsRef = useRef(props);
+ propsRef.current = props;
+
+ const store = useMemo(() => {
+ return createStore<
+ ThreadManager & {
+ abortController: AbortController | null;
+ }
+ >((set) => {
+ return {
+ messages: [] as Message[],
+ error: null,
+ abortController: null,
+ isRunning: false,
+ addMessages: async (...messages: CreateMessage[]) => {
+ const abortController = new AbortController();
+ const threadManager = store.getState();
+
+ if (threadManager.isRunning) {
+ return;
+ }
+
+ set({ abortController, isRunning: true });
+ abortController.signal.addEventListener("abort", () => {
+ set({ abortController: null, isRunning: false });
+ });
+
+ try {
+ await propsRef.current.onAddMessages({
+ messages,
+ threadManager: store.getState(),
+ abortController: new AbortController(),
+ });
+ } finally {
+ set({ abortController: null, isRunning: false });
+ }
+ },
+ updateMessage: (message: Message) => {
+ const messages = store.getState().messages.map((m) => {
+ if (m.id === message.id) {
+ return message;
+ }
+ return m;
+ });
+ set({ messages });
+ },
+ onCancel: () => {
+ const abortController = store.getState().abortController;
+ if (abortController) {
+ abortController.abort();
+ }
+ },
+ };
+ });
+ }, [propsRef]);
+
+ useEffect(() => {
+ const threadId = props.threadId;
+ // cancel any previous requests
+ store.getState().onCancel();
+
+ store.setState({ messages: [] });
+
+ if (threadId) {
+ const abortController = new AbortController();
+ store.setState({ abortController, isRunning: true });
+ props
+ .loadThread(threadId)
+ .then((messages) => {
+ store.setState({ messages });
+ })
+ .catch((error) => {
+ store.setState({ error });
+ })
+ .finally(() => {
+ store.setState({ abortController: null, isRunning: false });
+ });
+ }
+ }, [props.threadId]);
+
+ return store.getState();
+};
diff --git a/frontend-sdk/packages/react-core/src/vercel-sdk/useVercelChatManager.ts b/frontend-sdk/packages/react-core/src/vercel-sdk/useVercelChatManager.ts
deleted file mode 100644
index c9fe09b55..000000000
--- a/frontend-sdk/packages/react-core/src/vercel-sdk/useVercelChatManager.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import { useChat } from "ai/react";
-import { GenUITool, ThreadManager } from "../types";
-
-type UseChatParams = Parameters<typeof useChat>[0] & { id?: never };
-type UseChatReturn = ReturnType<typeof useChat>;
-
-export const useVercelChatManager = (
- params: UseChatParams,
- tools: GenUITool[],
-): ThreadManager & { useChatState: UseChatReturn } => {
- const toolsMap = tools.reduce(
- (acc, tool) => {
- acc[tool.name] = tool;
- return acc;
- },
- {} as Record<string, GenUITool>,
- );
-
- const vercelChatReturn = useChat({
- ...params,
- body: { ...params.body, tools },
- });
- const { messages, isLoading, addToolResult, append, reload, stop, error } = vercelChatReturn;
-
- return {
- messages,
- isRunning: isLoading,
- error,
- onNew: append,
- onReload: () => {
- reload();
- },
- onCancel: stop,
- onAddToolResult: addToolResult,
- convertMessage: undefined,
- useChatState: vercelChatReturn,
- tools: toolsMap,
- };
-};