chatbot-template/components/chat/create-artifact.tsx

94 lines
2.9 KiB
TypeScript
Raw Normal View History

import type { UseChatHelpers } from "@ai-sdk/react";
import type { DataUIPart } from "ai";
import type { ComponentType, Dispatch, ReactNode, SetStateAction } from "react";
import type { Suggestion } from "@/lib/db/schema";
import type { ChatMessage, CustomUIDataTypes } from "@/lib/types";
import type { UIArtifact } from "./artifact";
2025-01-27 14:19:47 +05:30
export type ArtifactActionContext<M = any> = {
2025-01-27 14:19:47 +05:30
content: string;
handleVersionChange: (type: "next" | "prev" | "toggle" | "latest") => void;
2025-01-27 14:19:47 +05:30
currentVersionIndex: number;
isCurrentVersion: boolean;
mode: "edit" | "diff";
metadata: M;
setMetadata: Dispatch<SetStateAction<M>>;
2025-01-27 14:19:47 +05:30
};
type ArtifactAction<M = any> = {
2025-01-27 14:19:47 +05:30
icon: ReactNode;
label?: string;
description: string;
onClick: (context: ArtifactActionContext<M>) => Promise<void> | void;
isDisabled?: (context: ArtifactActionContext<M>) => boolean;
2025-01-27 14:19:47 +05:30
};
export type ArtifactToolbarContext = {
sendMessage: UseChatHelpers<ChatMessage>["sendMessage"];
2025-01-27 14:19:47 +05:30
};
export type ArtifactToolbarItem = {
2025-01-27 14:19:47 +05:30
description: string;
icon: ReactNode;
onClick: (context: ArtifactToolbarContext) => void;
2025-01-27 14:19:47 +05:30
};
type ArtifactContent<M = any> = {
2025-01-27 14:19:47 +05:30
title: string;
content: string;
mode: "edit" | "diff";
2025-01-27 14:19:47 +05:30
isCurrentVersion: boolean;
currentVersionIndex: number;
status: "streaming" | "idle";
suggestions: Suggestion[];
2025-01-27 14:19:47 +05:30
onSaveContent: (updatedContent: string, debounce: boolean) => void;
isInline: boolean;
getDocumentContentById: (index: number) => string;
isLoading: boolean;
metadata: M;
setMetadata: Dispatch<SetStateAction<M>>;
};
2025-01-27 14:19:47 +05:30
type InitializeParameters<M = any> = {
2025-01-27 14:19:47 +05:30
documentId: string;
setMetadata: Dispatch<SetStateAction<M>>;
};
2025-01-27 14:19:47 +05:30
type ArtifactConfig<T extends string, M = any> = {
2025-01-27 14:19:47 +05:30
kind: T;
description: string;
content: ComponentType<ArtifactContent<M>>;
actions: ArtifactAction<M>[];
toolbar: ArtifactToolbarItem[];
2025-01-27 14:19:47 +05:30
initialize?: (parameters: InitializeParameters<M>) => void;
onStreamPart: (args: {
2025-01-27 14:19:47 +05:30
setMetadata: Dispatch<SetStateAction<M>>;
setArtifact: Dispatch<SetStateAction<UIArtifact>>;
streamPart: DataUIPart<CustomUIDataTypes>;
2025-01-27 14:19:47 +05:30
}) => void;
};
export class Artifact<T extends string, M = any> {
2025-01-27 14:19:47 +05:30
readonly kind: T;
readonly description: string;
readonly content: ComponentType<ArtifactContent<M>>;
readonly actions: ArtifactAction<M>[];
readonly toolbar: ArtifactToolbarItem[];
readonly initialize?: (parameters: InitializeParameters) => void;
readonly onStreamPart: (args: {
2025-01-27 14:19:47 +05:30
setMetadata: Dispatch<SetStateAction<M>>;
setArtifact: Dispatch<SetStateAction<UIArtifact>>;
streamPart: DataUIPart<CustomUIDataTypes>;
2025-01-27 14:19:47 +05:30
}) => void;
constructor(config: ArtifactConfig<T, M>) {
2025-01-27 14:19:47 +05:30
this.kind = config.kind;
this.description = config.description;
this.content = config.content;
this.actions = config.actions || [];
this.toolbar = config.toolbar || [];
this.initialize = config.initialize || (async () => ({}));
this.onStreamPart = config.onStreamPart;
}
}