fix artifact code
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
|||||||
SquarePen,
|
SquarePen,
|
||||||
Plus,
|
Plus,
|
||||||
CircleX,
|
CircleX,
|
||||||
|
XIcon,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useQueryState, parseAsBoolean } from "nuqs";
|
import { useQueryState, parseAsBoolean } from "nuqs";
|
||||||
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
|
||||||
@@ -39,6 +40,12 @@ import {
|
|||||||
} from "../ui/tooltip";
|
} from "../ui/tooltip";
|
||||||
import { useFileUpload } from "@/hooks/use-file-upload";
|
import { useFileUpload } from "@/hooks/use-file-upload";
|
||||||
import { ContentBlocksPreview } from "./ContentBlocksPreview";
|
import { ContentBlocksPreview } from "./ContentBlocksPreview";
|
||||||
|
import {
|
||||||
|
useArtifactOpen,
|
||||||
|
ArtifactContent,
|
||||||
|
ArtifactTitle,
|
||||||
|
useArtifactContext,
|
||||||
|
} from "./artifact";
|
||||||
|
|
||||||
function StickyToBottomContent(props: {
|
function StickyToBottomContent(props: {
|
||||||
content: ReactNode;
|
content: ReactNode;
|
||||||
@@ -106,7 +113,10 @@ function OpenGitHubRepo() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Thread() {
|
export function Thread() {
|
||||||
const [threadId, setThreadId] = useQueryState("threadId");
|
const [_threadId, _setThreadId] = useQueryState("threadId");
|
||||||
|
|
||||||
|
const [artifactContext, setArtifactContext] = useArtifactContext();
|
||||||
|
const [artifactOpen, closeArtifact] = useArtifactOpen();
|
||||||
const [chatHistoryOpen, setChatHistoryOpen] = useQueryState(
|
const [chatHistoryOpen, setChatHistoryOpen] = useQueryState(
|
||||||
"chatHistoryOpen",
|
"chatHistoryOpen",
|
||||||
parseAsBoolean.withDefault(false),
|
parseAsBoolean.withDefault(false),
|
||||||
@@ -133,6 +143,33 @@ export function Thread() {
|
|||||||
|
|
||||||
const lastError = useRef<string | undefined>(undefined);
|
const lastError = useRef<string | undefined>(undefined);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!stream.error) {
|
||||||
|
lastError.current = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const message = (stream.error as any).message;
|
||||||
|
if (!message || lastError.current === message) {
|
||||||
|
// Message has already been logged. do not modify ref, return early.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Message is defined, and it has not been logged yet. Save it, and send the error
|
||||||
|
lastError.current = message;
|
||||||
|
toast.error("An error occurred. Please try again.", {
|
||||||
|
description: (
|
||||||
|
<p>
|
||||||
|
<strong>Error:</strong> <code>{message}</code>
|
||||||
|
</p>
|
||||||
|
),
|
||||||
|
richColors: true,
|
||||||
|
closeButton: true,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
}, [stream.error]);
|
||||||
|
|
||||||
// TODO: this should be part of the useStream hook
|
// TODO: this should be part of the useStream hook
|
||||||
const prevMessageLength = useRef(0);
|
const prevMessageLength = useRef(0);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -164,11 +201,15 @@ export function Thread() {
|
|||||||
|
|
||||||
const toolMessages = ensureToolCallsHaveResponses(stream.messages);
|
const toolMessages = ensureToolCallsHaveResponses(stream.messages);
|
||||||
stream.submit(
|
stream.submit(
|
||||||
{ messages: [...toolMessages, newHumanMessage] },
|
{
|
||||||
|
messages: [...toolMessages, newHumanMessage],
|
||||||
|
context: artifactContext,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
streamMode: ["values"],
|
streamMode: ["values"],
|
||||||
optimisticValues: (prev) => ({
|
optimisticValues: (prev) => ({
|
||||||
...prev,
|
...prev,
|
||||||
|
context: artifactContext,
|
||||||
messages: [
|
messages: [
|
||||||
...(prev.messages ?? []),
|
...(prev.messages ?? []),
|
||||||
...toolMessages,
|
...toolMessages,
|
||||||
@@ -182,11 +223,6 @@ export function Thread() {
|
|||||||
setContentBlocks([]);
|
setContentBlocks([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const chatStarted = !!threadId || !!messages.length;
|
|
||||||
const hasNoAIOrToolMessages = !messages.find(
|
|
||||||
(m) => m.type === "ai" || m.type === "tool",
|
|
||||||
);
|
|
||||||
|
|
||||||
// Restore handleRegenerate
|
// Restore handleRegenerate
|
||||||
const handleRegenerate = (
|
const handleRegenerate = (
|
||||||
parentCheckpoint: Checkpoint | null | undefined,
|
parentCheckpoint: Checkpoint | null | undefined,
|
||||||
@@ -200,6 +236,18 @@ export function Thread() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setThreadId = (id: string | null) => {
|
||||||
|
_setThreadId(id);
|
||||||
|
// close artifact and reset artifact context
|
||||||
|
closeArtifact();
|
||||||
|
setArtifactContext({});
|
||||||
|
};
|
||||||
|
const threadId = _threadId;
|
||||||
|
const chatStarted = !!threadId || !!messages.length;
|
||||||
|
const hasNoAIOrToolMessages = !messages.find(
|
||||||
|
(m) => m.type === "ai" || m.type === "tool",
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-screen w-full overflow-hidden">
|
<div className="flex h-screen w-full overflow-hidden">
|
||||||
<div className="relative hidden lg:flex">
|
<div className="relative hidden lg:flex">
|
||||||
@@ -476,6 +524,20 @@ export function Thread() {
|
|||||||
/>
|
/>
|
||||||
</StickToBottom>
|
</StickToBottom>
|
||||||
</motion.div>
|
</motion.div>
|
||||||
|
<div className="relative flex flex-col border-l">
|
||||||
|
<div className="absolute inset-0 flex min-w-[30vw] flex-col">
|
||||||
|
<div className="grid grid-cols-[1fr_auto] border-b p-4">
|
||||||
|
<ArtifactTitle className="truncate overflow-hidden" />
|
||||||
|
<button
|
||||||
|
onClick={closeArtifact}
|
||||||
|
className="cursor-pointer"
|
||||||
|
>
|
||||||
|
<XIcon className="size-5" />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<ArtifactContent className="relative flex-grow" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user