2025-09-21 11:02:31 -07:00
|
|
|
"use client";
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
|
import { initialArtifactData, useArtifact } from "@/hooks/use-artifact";
|
|
|
|
|
import { artifactDefinitions } from "./artifact";
|
|
|
|
|
import { useDataStream } from "./data-stream-provider";
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
export function DataStreamHandler() {
|
|
|
|
|
const { dataStream } = useDataStream();
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
const { artifact, setArtifact, setMetadata } = useArtifact();
|
2024-12-19 17:05:04 +05:30
|
|
|
const lastProcessedIndex = useRef(-1);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-09-21 11:02:31 -07:00
|
|
|
if (!dataStream?.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
const newDeltas = dataStream.slice(lastProcessedIndex.current + 1);
|
|
|
|
|
lastProcessedIndex.current = dataStream.length - 1;
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
for (const delta of newDeltas) {
|
2025-02-13 08:25:57 -08:00
|
|
|
const artifactDefinition = artifactDefinitions.find(
|
2025-09-21 11:02:31 -07:00
|
|
|
(currentArtifactDefinition) =>
|
|
|
|
|
currentArtifactDefinition.kind === artifact.kind
|
2025-01-27 14:19:47 +05:30
|
|
|
);
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
if (artifactDefinition?.onStreamPart) {
|
|
|
|
|
artifactDefinition.onStreamPart({
|
2025-01-27 14:19:47 +05:30
|
|
|
streamPart: delta,
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact,
|
2025-01-27 14:19:47 +05:30
|
|
|
setMetadata,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact((draftArtifact) => {
|
|
|
|
|
if (!draftArtifact) {
|
2025-09-21 11:02:31 -07:00
|
|
|
return { ...initialArtifactData, status: "streaming" };
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (delta.type) {
|
2025-09-21 11:02:31 -07:00
|
|
|
case "data-id":
|
2024-12-19 17:05:04 +05:30
|
|
|
return {
|
2025-02-13 08:25:57 -08:00
|
|
|
...draftArtifact,
|
2025-07-03 02:26:34 -07:00
|
|
|
documentId: delta.data,
|
2025-09-21 11:02:31 -07:00
|
|
|
status: "streaming",
|
2024-12-19 17:05:04 +05:30
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
case "data-title":
|
2024-12-19 17:05:04 +05:30
|
|
|
return {
|
2025-02-13 08:25:57 -08:00
|
|
|
...draftArtifact,
|
2025-07-03 02:26:34 -07:00
|
|
|
title: delta.data,
|
2025-09-21 11:02:31 -07:00
|
|
|
status: "streaming",
|
2024-12-19 17:05:04 +05:30
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
case "data-kind":
|
2024-12-19 17:05:04 +05:30
|
|
|
return {
|
2025-02-13 08:25:57 -08:00
|
|
|
...draftArtifact,
|
2025-07-03 02:26:34 -07:00
|
|
|
kind: delta.data,
|
2025-09-21 11:02:31 -07:00
|
|
|
status: "streaming",
|
2024-12-19 17:05:04 +05:30
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
case "data-clear":
|
2024-12-19 17:05:04 +05:30
|
|
|
return {
|
2025-02-13 08:25:57 -08:00
|
|
|
...draftArtifact,
|
2025-09-21 11:02:31 -07:00
|
|
|
content: "",
|
|
|
|
|
status: "streaming",
|
2024-12-19 17:05:04 +05:30
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
case "data-finish":
|
2024-12-19 17:05:04 +05:30
|
|
|
return {
|
2025-02-13 08:25:57 -08:00
|
|
|
...draftArtifact,
|
2025-09-21 11:02:31 -07:00
|
|
|
status: "idle",
|
2024-12-19 17:05:04 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
default:
|
2025-02-13 08:25:57 -08:00
|
|
|
return draftArtifact;
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
});
|
2025-09-21 11:02:31 -07:00
|
|
|
}
|
2025-02-13 08:25:57 -08:00
|
|
|
}, [dataStream, setArtifact, setMetadata, artifact]);
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|