Skip to content

Commit 1fbef48

Browse files
committed
fix artifact code
1 parent 8534f4a commit 1fbef48

File tree

1 file changed

+69
-7
lines changed

1 file changed

+69
-7
lines changed

src/components/thread/index.tsx

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
SquarePen,
2323
Plus,
2424
CircleX,
25+
XIcon,
2526
} from "lucide-react";
2627
import { useQueryState, parseAsBoolean } from "nuqs";
2728
import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
@@ -39,6 +40,12 @@ import {
3940
} from "../ui/tooltip";
4041
import { useFileUpload } from "@/hooks/use-file-upload";
4142
import { ContentBlocksPreview } from "./ContentBlocksPreview";
43+
import {
44+
useArtifactOpen,
45+
ArtifactContent,
46+
ArtifactTitle,
47+
useArtifactContext,
48+
} from "./artifact";
4249

4350
function StickyToBottomContent(props: {
4451
content: ReactNode;
@@ -106,7 +113,10 @@ function OpenGitHubRepo() {
106113
}
107114

108115
export function Thread() {
109-
const [threadId, setThreadId] = useQueryState("threadId");
116+
const [_threadId, _setThreadId] = useQueryState("threadId");
117+
118+
const [artifactContext, setArtifactContext] = useArtifactContext();
119+
const [artifactOpen, closeArtifact] = useArtifactOpen();
110120
const [chatHistoryOpen, setChatHistoryOpen] = useQueryState(
111121
"chatHistoryOpen",
112122
parseAsBoolean.withDefault(false),
@@ -133,6 +143,33 @@ export function Thread() {
133143

134144
const lastError = useRef<string | undefined>(undefined);
135145

146+
useEffect(() => {
147+
if (!stream.error) {
148+
lastError.current = undefined;
149+
return;
150+
}
151+
try {
152+
const message = (stream.error as any).message;
153+
if (!message || lastError.current === message) {
154+
// Message has already been logged. do not modify ref, return early.
155+
return;
156+
}
157+
// Message is defined, and it has not been logged yet. Save it, and send the error
158+
lastError.current = message;
159+
toast.error("An error occurred. Please try again.", {
160+
description: (
161+
<p>
162+
<strong>Error:</strong> <code>{message}</code>
163+
</p>
164+
),
165+
richColors: true,
166+
closeButton: true,
167+
});
168+
} catch {
169+
// no-op
170+
}
171+
}, [stream.error]);
172+
136173
// TODO: this should be part of the useStream hook
137174
const prevMessageLength = useRef(0);
138175
useEffect(() => {
@@ -164,11 +201,15 @@ export function Thread() {
164201

165202
const toolMessages = ensureToolCallsHaveResponses(stream.messages);
166203
stream.submit(
167-
{ messages: [...toolMessages, newHumanMessage] },
204+
{
205+
messages: [...toolMessages, newHumanMessage],
206+
context: artifactContext,
207+
},
168208
{
169209
streamMode: ["values"],
170210
optimisticValues: (prev) => ({
171211
...prev,
212+
context: artifactContext,
172213
messages: [
173214
...(prev.messages ?? []),
174215
...toolMessages,
@@ -182,11 +223,6 @@ export function Thread() {
182223
setContentBlocks([]);
183224
};
184225

185-
const chatStarted = !!threadId || !!messages.length;
186-
const hasNoAIOrToolMessages = !messages.find(
187-
(m) => m.type === "ai" || m.type === "tool",
188-
);
189-
190226
// Restore handleRegenerate
191227
const handleRegenerate = (
192228
parentCheckpoint: Checkpoint | null | undefined,
@@ -200,6 +236,18 @@ export function Thread() {
200236
});
201237
};
202238

239+
const setThreadId = (id: string | null) => {
240+
_setThreadId(id);
241+
// close artifact and reset artifact context
242+
closeArtifact();
243+
setArtifactContext({});
244+
};
245+
const threadId = _threadId;
246+
const chatStarted = !!threadId || !!messages.length;
247+
const hasNoAIOrToolMessages = !messages.find(
248+
(m) => m.type === "ai" || m.type === "tool",
249+
);
250+
203251
return (
204252
<div className="flex h-screen w-full overflow-hidden">
205253
<div className="relative hidden lg:flex">
@@ -476,6 +524,20 @@ export function Thread() {
476524
/>
477525
</StickToBottom>
478526
</motion.div>
527+
<div className="relative flex flex-col border-l">
528+
<div className="absolute inset-0 flex min-w-[30vw] flex-col">
529+
<div className="grid grid-cols-[1fr_auto] border-b p-4">
530+
<ArtifactTitle className="truncate overflow-hidden" />
531+
<button
532+
onClick={closeArtifact}
533+
className="cursor-pointer"
534+
>
535+
<XIcon className="size-5" />
536+
</button>
537+
</div>
538+
<ArtifactContent className="relative flex-grow" />
539+
</div>
540+
</div>
479541
</div>
480542
);
481543
}

0 commit comments

Comments
 (0)