feat: integrated with artifacts

This commit is contained in:
Henry Li
2026-01-17 00:02:03 +08:00
parent 16a5ed9a73
commit e5050c6c1e
6 changed files with 174 additions and 39 deletions

View File

@@ -1,10 +1,20 @@
"use client"; "use client";
import type { UseStream } from "@langchain/langgraph-sdk/react";
import { useParams, useRouter } from "next/navigation"; import { useParams, useRouter } from "next/navigation";
import { useCallback, useEffect, useMemo, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import { BreadcrumbItem } from "@/components/ui/breadcrumb"; import { BreadcrumbItem } from "@/components/ui/breadcrumb";
import { ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable"; import {
ResizableHandle,
ResizablePanel,
ResizablePanelGroup,
} from "@/components/ui/resizable";
import { ArtifactFileDetail } from "@/components/workspace/artifacts";
import {
ArtifactsProvider,
useArtifacts,
} from "@/components/workspace/artifacts/context";
import { InputBox } from "@/components/workspace/input-box"; import { InputBox } from "@/components/workspace/input-box";
import { MessageList } from "@/components/workspace/message-list/message-list"; import { MessageList } from "@/components/workspace/message-list/message-list";
import { import {
@@ -13,20 +23,18 @@ import {
WorkspaceHeader, WorkspaceHeader,
} from "@/components/workspace/workspace-container"; } from "@/components/workspace/workspace-container";
import { useLocalSettings } from "@/core/settings"; import { useLocalSettings } from "@/core/settings";
import { type AgentThread } from "@/core/threads"; import { type AgentThread, type AgentThreadState } from "@/core/threads";
import { useSubmitThread, useThreadStream } from "@/core/threads/hooks"; import { useSubmitThread, useThreadStream } from "@/core/threads/hooks";
import { pathOfThread, titleOfThread } from "@/core/threads/utils"; import { pathOfThread, titleOfThread } from "@/core/threads/utils";
import { uuid } from "@/core/utils/uuid"; import { uuid } from "@/core/utils/uuid";
export default function ChatPage() { export default function ChatPage() {
const router = useRouter();
const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>(); const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>();
const isNewThread = useMemo( const isNewThread = useMemo(
() => threadIdFromPath === "new", () => threadIdFromPath === "new",
[threadIdFromPath], [threadIdFromPath],
); );
const [threadId, setThreadId] = useState<string | null>(null); const [threadId, setThreadId] = useState<string | null>(null);
const [settings, setSettings] = useLocalSettings();
useEffect(() => { useEffect(() => {
if (threadIdFromPath !== "new") { if (threadIdFromPath !== "new") {
@@ -39,6 +47,40 @@ export default function ChatPage() {
isNewThread, isNewThread,
threadId, threadId,
}); });
return (
<WorkspaceContainer>
<WorkspaceHeader>
<BreadcrumbItem className="hidden md:block">
{isNewThread
? "New"
: titleOfThread(thread as unknown as AgentThread)}
</BreadcrumbItem>
</WorkspaceHeader>
<WorkspaceBody>
<ArtifactsProvider>
<ThreadDetail
threadId={threadId}
thread={thread}
isNewThread={isNewThread}
/>
</ArtifactsProvider>
</WorkspaceBody>
</WorkspaceContainer>
);
}
function ThreadDetail({
threadId,
thread,
isNewThread,
}: {
threadId?: string | null;
thread: UseStream<AgentThreadState>;
isNewThread: boolean;
}) {
const router = useRouter();
const [settings, setSettings] = useLocalSettings();
const { open, selectedArtifact } = useArtifacts();
const handleSubmit = useSubmitThread({ const handleSubmit = useSubmitThread({
isNewThread, isNewThread,
threadId, threadId,
@@ -52,36 +94,33 @@ export default function ChatPage() {
await thread.stop(); await thread.stop();
}, [thread]); }, [thread]);
return ( return (
<WorkspaceContainer> <ResizablePanelGroup orientation="horizontal">
<WorkspaceHeader> <ResizablePanel className="relative" defaultSize={46}>
<BreadcrumbItem className="hidden md:block"> <div className="flex size-full justify-center">
{isNewThread <MessageList className="size-full" thread={thread} />
? "New" </div>
: titleOfThread(thread as unknown as AgentThread)} <div className="absolute right-0 bottom-0 left-0 flex justify-center px-4">
</BreadcrumbItem> <InputBox
</WorkspaceHeader> className="w-full max-w-(--container-width-md)"
<WorkspaceBody> autoFocus={isNewThread}
<ResizablePanelGroup orientation="horizontal"> status={thread.isLoading ? "streaming" : "ready"}
<ResizablePanel className="relative" defaultSize={46}> context={settings.context}
<div className="flex size-full justify-center"> onContextChange={(context) => setSettings("context", context)}
<MessageList className="size-full" thread={thread} /> onSubmit={handleSubmit}
</div> onStop={handleStop}
<div className="absolute right-0 bottom-0 left-0 flex justify-center px-4"> />
<InputBox </div>
className="w-full max-w-(--container-width-md)" </ResizablePanel>
autoFocus={isNewThread} {open && (
status={thread.isLoading ? "streaming" : "ready"} <>
context={settings.context} <ResizableHandle />
onContextChange={(context) => setSettings("context", context)} <ResizablePanel defaultSize={64}>
onSubmit={handleSubmit} {selectedArtifact && (
onStop={handleStop} <ArtifactFileDetail filepath={selectedArtifact} />
/> )}
</div>
</ResizablePanel> </ResizablePanel>
{/* <ResizableHandle /> </>
<ResizablePanel defaultSize={64}></ResizablePanel> */} )}
</ResizablePanelGroup> </ResizablePanelGroup>
</WorkspaceBody>
</WorkspaceContainer>
); );
} }

View File

@@ -0,0 +1,14 @@
import { FileIcon } from "lucide-react";
export function ArtifactFileDetail({ filepath }: { filepath: string }) {
return (
<div className="flex size-full items-center justify-center">
<div className="flex items-center gap-2">
<div>
<FileIcon />
</div>
<div>{filepath}</div>
</div>
</div>
);
}

View File

@@ -1,4 +1,5 @@
import { DownloadIcon } from "lucide-react"; import { DownloadIcon } from "lucide-react";
import { useCallback } from "react";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { import {
@@ -9,12 +10,32 @@ import {
CardTitle, CardTitle,
} from "@/components/ui/card"; } from "@/components/ui/card";
import { getFileExtension, getFileName } from "@/core/utils/files"; import { getFileExtension, getFileName } from "@/core/utils/files";
import { cn } from "@/lib/utils";
export function PresentFileList({ files }: { files: string[] }) { import { useArtifacts } from "./context";
export function ArtifactFileList({
className,
files,
}: {
className?: string;
files: string[];
}) {
const { openArtifact } = useArtifacts();
const handleClick = useCallback(
(filepath: string) => {
openArtifact(filepath);
},
[openArtifact],
);
return ( return (
<ul className="flex w-full flex-col gap-4"> <ul className={cn("flex w-full flex-col gap-4", className)}>
{files.map((file) => ( {files.map((file) => (
<Card key={file}> <Card
className="cursor-pointer"
key={file}
onClick={() => handleClick(file)}
>
<CardHeader> <CardHeader>
<CardTitle>{getFileName(file)}</CardTitle> <CardTitle>{getFileName(file)}</CardTitle>
<CardDescription>{getFileExtension(file)} file</CardDescription> <CardDescription>{getFileExtension(file)} file</CardDescription>

View File

@@ -0,0 +1,58 @@
import { createContext, useContext, useState, type ReactNode } from "react";
export interface ArtifactsContextType {
artifacts: string[];
selectedArtifact: string | null;
open: boolean;
setOpen: (open: boolean) => void;
addArtifacts: (artifacts: string[]) => void;
openArtifact: (artifact: string) => void;
}
const ArtifactsContext = createContext<ArtifactsContextType | undefined>(
undefined,
);
interface ArtifactsProviderProps {
children: ReactNode;
}
export function ArtifactsProvider({ children }: ArtifactsProviderProps) {
const [artifacts, setArtifacts] = useState<string[]>([]);
const [selectedArtifact, setSelectedArtifact] = useState<string | null>(null);
const [open, setOpen] = useState(false);
const addArtifacts = (newArtifacts: string[]) => {
setArtifacts((prev) => [...prev, ...newArtifacts]);
};
const openArtifact = (artifact: string) => {
setSelectedArtifact(artifact);
setOpen(true);
};
const value: ArtifactsContextType = {
artifacts,
selectedArtifact,
open,
setOpen,
addArtifacts,
openArtifact,
};
return (
<ArtifactsContext.Provider value={value}>
{children}
</ArtifactsContext.Provider>
);
}
export function useArtifacts() {
const context = useContext(ArtifactsContext);
if (context === undefined) {
throw new Error("useArtifacts must be used within an ArtifactsProvider");
}
return context;
}

View File

@@ -0,0 +1,3 @@
export * from "./artifact-file-detail";
export * from "./artifact-file-list";
export * from "./context";

View File

@@ -13,11 +13,11 @@ import {
import type { AgentThreadState } from "@/core/threads"; import type { AgentThreadState } from "@/core/threads";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { ArtifactFileList } from "../artifacts/artifact-file-list";
import { StreamingIndicator } from "../streaming-indicator"; import { StreamingIndicator } from "../streaming-indicator";
import { MessageGroup } from "./message-group"; import { MessageGroup } from "./message-group";
import { MessageListItem } from "./message-list-item"; import { MessageListItem } from "./message-list-item";
import { PresentFileList } from "./present-file-list";
import { MessageListSkeleton } from "./skeleton"; import { MessageListSkeleton } from "./skeleton";
export function MessageList({ export function MessageList({
@@ -57,7 +57,7 @@ export function MessageList({
} }
} }
return ( return (
<PresentFileList key={groupedMessages[0].id} files={files} /> <ArtifactFileList key={groupedMessages[0].id} files={files} />
); );
} }
return ( return (