feat: auto open artifact

This commit is contained in:
Henry Li
2026-01-21 09:45:55 +08:00
parent 6e024d6c8f
commit 4467b1860f
2 changed files with 22 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ export interface ArtifactsContextType {
selectedArtifact: string | null;
open: boolean;
autoOpen: boolean;
setOpen: (open: boolean) => void;
deselect: () => void;
@@ -27,6 +28,7 @@ export function ArtifactsProvider({ children }: ArtifactsProviderProps) {
const [artifacts, setArtifacts] = useState<string[]>([]);
const [selectedArtifact, setSelectedArtifact] = useState<string | null>(null);
const [open, setOpen] = useState(false);
const [autoOpen, setAutoOpen] = useState(true);
const { setOpen: setSidebarOpen } = useSidebar();
const select = (artifact: string) => {
@@ -43,7 +45,13 @@ export function ArtifactsProvider({ children }: ArtifactsProviderProps) {
setArtifacts,
open,
setOpen,
autoOpen,
setOpen: (isOpen: boolean) => {
if (!isOpen && autoOpen) {
setAutoOpen(false);
}
setOpen(isOpen);
},
selectedArtifact,
select,

View File

@@ -181,7 +181,7 @@ function ToolCall({
result?: string | Record<string, unknown>;
}) {
const { t } = useI18n();
const { select, setOpen } = useArtifacts();
const { setOpen, autoOpen, selectedArtifact, select } = useArtifacts();
if (name === "web_search") {
let label: React.ReactNode = t.toolCalls.searchForRelatedInfo;
if (typeof args.query === "string") {
@@ -265,6 +265,18 @@ function ToolCall({
description = t.toolCalls.writeFile;
}
const path: string | undefined = (args as { path: string })?.path;
if (autoOpen && path) {
setTimeout(() => {
const url = new URL(
`write-file:${path}?message_id=${messageId}&tool_call_id=${id}`,
).toString();
if (selectedArtifact === url) {
return;
}
select(url);
setOpen(true);
}, 100);
}
return (
<ChainOfThoughtStep
key={id}