From 4c9957dc260a69686eb4add2b2c2c1893f2271b5 Mon Sep 17 00:00:00 2001 From: abhi1992002 Date: Mon, 2 Feb 2026 09:37:11 +0530 Subject: [PATCH] arranging messages code --- .../ChatMessagesContainer.tsx | 122 +++++++++++++ .../components/ChatSidebar/ChatSidebar.tsx | 0 .../components/EmptySession/EmptySession.tsx | 34 ++++ .../src/app/(platform)/copilot-2/page.tsx | 160 +++--------------- .../(platform)/copilot-2/store/chat-store.ts | 0 5 files changed, 179 insertions(+), 137 deletions(-) create mode 100644 autogpt_platform/frontend/src/app/(platform)/copilot-2/components/ChatMessagesContainer/ChatMessagesContainer.tsx rename autogpt_platform/frontend/src/app/(platform)/copilot-2/{tools => }/components/ChatSidebar/ChatSidebar.tsx (100%) create mode 100644 autogpt_platform/frontend/src/app/(platform)/copilot-2/components/EmptySession/EmptySession.tsx create mode 100644 autogpt_platform/frontend/src/app/(platform)/copilot-2/store/chat-store.ts diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/ChatMessagesContainer/ChatMessagesContainer.tsx new file mode 100644 index 0000000000..7806dcb9f2 --- /dev/null +++ b/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/ChatMessagesContainer/ChatMessagesContainer.tsx @@ -0,0 +1,122 @@ +import { + Conversation, + ConversationContent, + ConversationEmptyState, + ConversationScrollButton, + } from "@/components/ai-elements/conversation"; + import { + Message, + MessageContent, + MessageResponse, + } from "@/components/ai-elements/message"; +import { MessageSquareIcon } from "lucide-react"; +import { UIMessage, UIDataTypes, UITools } from "ai"; + + +interface ChatMessagesContainerProps { + messages: UIMessage[]; + status: string; + error: Error | undefined; + handleSubmit: (e: React.FormEvent) => void; + input: string; + setInput: (input: string) => void; + } + +export const ChatMessagesContainer = ({messages, status, error, handleSubmit, input, setInput}: ChatMessagesContainerProps) => { + return ( +
+ + + {messages.length === 0 ? ( + } + title="Start a conversation" + description="Type a message below to begin chatting" + /> + ) : ( + messages.map((message) => ( + + + {message.parts.map((part, i) => { + switch (part.type) { + case "text": + return ( + + {part.text} + + ); + case "tool-find_block": + return ( +
+ {part.state === "input-streaming" && ( +

Finding blocks for you

+ )} + {part.state === "input-available" && ( +

+ Searching blocks for{" "} + {(part.input as { query: string }).query} +

+ )} + {part.state === "output-available" && ( +

+ Found{" "} + { + ( + JSON.parse(part.output as string) as { + count: number; + } + ).count + }{" "} + blocks +

+ )} +
+ ); + default: + return null; + } + })} +
+
+ )) + )} + {status === "submitted" && ( + + +

Thinking...

+
+
+ )} + {error && ( +
+ Error: {error.message} +
+ )} +
+ +
+ +
+
+ setInput(e.target.value)} + disabled={status !== "ready"} + placeholder="Say something..." + className="flex-1 rounded-md border border-zinc-300 px-4 py-2 focus:border-zinc-500 focus:outline-none" + /> + +
+
+
+ ); +}; \ No newline at end of file diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot-2/tools/components/ChatSidebar/ChatSidebar.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/ChatSidebar/ChatSidebar.tsx similarity index 100% rename from autogpt_platform/frontend/src/app/(platform)/copilot-2/tools/components/ChatSidebar/ChatSidebar.tsx rename to autogpt_platform/frontend/src/app/(platform)/copilot-2/components/ChatSidebar/ChatSidebar.tsx diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/EmptySession/EmptySession.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/EmptySession/EmptySession.tsx new file mode 100644 index 0000000000..33eafc2110 --- /dev/null +++ b/autogpt_platform/frontend/src/app/(platform)/copilot-2/components/EmptySession/EmptySession.tsx @@ -0,0 +1,34 @@ +import { ChatSidebar } from "../ChatSidebar/ChatSidebar"; + +interface EmptySessionProps { + isCreating: boolean; + handleSubmit: (e: React.FormEvent) => void; + input: string; + setInput: (input: string) => void; +} + +export const EmptySession = ({ isCreating, handleSubmit, input, setInput }: EmptySessionProps) => { + return ( +
+

+ Start a new conversation +

+
+ setInput(e.target.value)} + disabled={isCreating} + placeholder="Type your message to start..." + className="w-full rounded-md border border-zinc-300 px-4 py-2" + /> + +
+
+ ); +}; \ No newline at end of file diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot-2/page.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot-2/page.tsx index 1da4fe075f..1a561aa0ea 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot-2/page.tsx +++ b/autogpt_platform/frontend/src/app/(platform)/copilot-2/page.tsx @@ -5,21 +5,13 @@ import { DefaultChatTransport } from "ai"; import { useState, useMemo } from "react"; import { parseAsString, useQueryState } from "nuqs"; import { MessageSquare } from "lucide-react"; -import { ChatSidebar } from "./tools/components/ChatSidebar/ChatSidebar"; -import { - Conversation, - ConversationContent, - ConversationEmptyState, - ConversationScrollButton, -} from "@/components/ai-elements/conversation"; -import { - Message, - MessageContent, - MessageResponse, -} from "@/components/ai-elements/message"; +import { ChatSidebar } from "./components/ChatSidebar/ChatSidebar"; +import { EmptySession } from "./components/EmptySession/EmptySession"; +import { ChatMessagesContainer } from "./components/ChatMessagesContainer/ChatMessagesContainer"; +import { postV2CreateSession } from "@/app/api/__generated__/endpoints/chat/chat"; export default function Page() { - const [sessionId] = useQueryState("sessionId", parseAsString); + const [sessionId, setSessionId] = useQueryState("sessionId", parseAsString); const [isCreating, setIsCreating] = useState(false); const [input, setInput] = useState(""); @@ -46,143 +38,37 @@ export default function Page() { transport: transport ?? undefined, }); - function handleSubmit(e: React.FormEvent) { + async function handleSubmit(e: React.FormEvent) { e.preventDefault(); + if(!sessionId) { + const newSessionId = await postV2CreateSession({ + body: JSON.stringify({}), + }); + if (newSessionId.status === 200 && newSessionId.data?.id) { + setSessionId(newSessionId.data.id); + } + console.log("newSessionId", newSessionId); + } if (input.trim()) { sendMessage({ text: input }); setInput(""); } } - // Show landing page when no session exists - if (!sessionId) { - return ( -
- - -
-

- Start a new conversation -

-
- setInput(e.target.value)} - disabled={isCreating} - placeholder="Type your message to start..." - className="w-full rounded-md border border-zinc-300 px-4 py-2" - /> - -
-
-
- ); - } return (
-
- - - {messages.length === 0 ? ( - } - title="Start a conversation" - description="Type a message below to begin chatting" - /> - ) : ( - messages.map((message) => ( - - - {message.parts.map((part, i) => { - switch (part.type) { - case "text": - return ( - - {part.text} - - ); - case "tool-find_block": - return ( -
- {part.state === "input-streaming" && ( -

Finding blocks for you

- )} - {part.state === "input-available" && ( -

- Searching blocks for{" "} - {(part.input as { query: string }).query} -

- )} - {part.state === "output-available" && ( -

- Found{" "} - { - ( - JSON.parse(part.output as string) as { - count: number; - } - ).count - }{" "} - blocks -

- )} -
- ); - default: - return null; - } - })} -
-
- )) - )} - {status === "submitted" && ( - - -

Thinking...

-
-
- )} - {error && ( -
- Error: {error.message} -
- )} -
- -
+ { + sessionId ? ( + + ) : ( + + ) + } -
-
- setInput(e.target.value)} - disabled={status !== "ready"} - placeholder="Say something..." - className="flex-1 rounded-md border border-zinc-300 px-4 py-2 focus:border-zinc-500 focus:outline-none" - /> - -
-
-
+
); } diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot-2/store/chat-store.ts b/autogpt_platform/frontend/src/app/(platform)/copilot-2/store/chat-store.ts new file mode 100644 index 0000000000..e69de29bb2