2025-09-21 11:02:31 -07:00
|
|
|
"use client";
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-11-29 13:02:24 +00:00
|
|
|
import { useEffect } from "react";
|
2025-09-21 11:02:31 -07:00
|
|
|
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() {
|
2025-11-29 13:02:24 +00:00
|
|
|
const { dataStream, setDataStream } = 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
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-09-21 11:02:31 -07:00
|
|
|
if (!dataStream?.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-11-01 03:38:43 +03:30
|
|
|
const newDeltas = dataStream.slice();
|
|
|
|
|
setDataStream([]);
|
2024-12-19 17:05:04 +05:30
|
|
|
|
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-11-29 13:02:24 +00:00
|
|
|
}, [dataStream, setArtifact, setMetadata, artifact, setDataStream]);
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|