8000 Moved mermaid related code to a dedicated feature dir. `vercelChat/m… by veerbal1 · Pull Request #640 · recoupable/Recoup-Chat · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Moved mermaid related code to a dedicated feature dir. `vercelChat/m… # 8000 640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions components/VercelChat/ToolComponents.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ImageSkeleton } from "@/components/ui/ImageSkeleton";
import { ImageResult } from "@/components/ui/ImageResult";
import { ImageGenerationResult } from "@/lib/tools/generateImage";
import MermaidDiagram from "../Chat/mermaid/MermaidDiagram";
import MermaidDiagram from "@/components/VercelChat/tools/mermaid/MermaidDiagram";
import { MermaidDiagramSkele 8000 ton } from "@/components/VercelChat/tools/mermaid/MermaidDiagramSkeleton";
import { GenerateMermaidDiagramResult } from "@/lib/tools/generateMermaidDiagram";
import { MermaidDiagramSkeleton } from "../ui/MermaidDiagramSkeleton";
import CreateArtistToolCall from "./tools/CreateArtistToolCall";
import CreateArtistToolResult from "./tools/CreateArtistToolResult";
import { CreateArtistResult } from "@/lib/tools/createArtist";
Expand Down Expand Up @@ -85,7 +85,10 @@ export function getToolCallComponent({ toolName, toolCallId }: ToolInvocation) {

// Default for other tools
return (
<div key={toolCallId} className="flex items-center gap-1 py-1 px-2 bg-primary/5 rounded-sm border w-fit text-xs">
<div
key={toolCallId}
className="flex items-center gap-1 py-1 px-2 bg-primary/5 rounded-sm border w-fit text-xs"
>
<Loader className="h-3 w-3 animate-spin text-primary" />
<span>Using {getDisplayToolName(toolName)}</span>
</div>
Expand Down Expand Up @@ -161,7 +164,15 @@ export function getToolResultComponent({
}

// Default generic result for other tools
return <GenericSuccess name={getDisplayToolName(toolName)} message={(result as { message?: string }).message ?? getToolInfo(toolName).message} />;
return (
<GenericSuccess
name={getDisplayToolName(toolName)}
message={
(result as { message?: string }).message ??
getToolInfo(toolName).message
}
/>
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use client';
"use client";

import React from 'react';
import MermaidErrorFallback from './MermaidErrorFallback'; // Import the fallback component
import { GenerateMermaidDiagramResult } from '@/lib/tools/generateMermaidDiagram';
import { useMermaid } from '@/hooks/useMermaid';
import handleDownload from '@/lib/utils/download-mermaid-chart';
import MessageMediaDownloadButton from '../MessageMediaDownloadButton';
import React from "react";
import MermaidErrorFallback from "./MermaidErrorFallback"; // Import the fallback component
import { GenerateMermaidDiagramResult } from "@/lib/tools/generateMermaidDiagram";
import { useMermaid } from "@/hooks/useMermaid";
import handleDownload from "@/lib/utils/download-mermaid-chart";
import MessageMediaDownloadButton from "@/components/VercelChat/MessageMediaDownloadButton";

// Define a type for the global mermaid object, or use any if types aren't installed
declare global {
Expand All @@ -29,10 +29,13 @@ interface MermaidDiagramProps {
* Renders a Mermaid diagram from a definition string.
* Dynamically imports the Mermaid library on mount.
*/
const MermaidDiagramInternal: React.FC<MermaidDiagramProps> = ({ chart, id }) => {
const MermaidDiagramInternal: React.FC<MermaidDiagramProps> = ({
chart,
id,
}) => {
const { isLibraryLoaded, hasError, containerRef, uniqueId } = useMermaid({
id,
chart
chart,
});

// Conditional rendering: Show fallback on error, otherwise the diagram container
Expand All @@ -48,13 +51,20 @@ const MermaidDiagramInternal: React.FC<MermaidDiagramProps> = ({ chart, id }) =>
id={uniqueId}
className="mermaid"
// Hide until library loaded and rendered successfully
style={{ visibility: (isLibraryLoaded && !hasError) ? 'visible' : 'hidden', backgroundColor: 'white', border: 'none', padding: '0' }}
style={{
visibility: isLibraryLoaded && !hasError ? "visible" : "hidden",
backgroundColor: "white",
border: "none",
padding: "0",
}}
>
{chart}
</pre>
{/* Download Diagram Button */}
{isLibraryLoaded && !hasError && (
<MessageMediaDownloadButton => handleDownload({ containerRef })} />
<MessageMediaDownloadButton
=> handleDownload({ containerRef })}
/>
)}
{/* Top gradient overlay */}
<div className="absolute z-10 transition-opacity duration-300 opacity-0 group-hover:opacity-100 group-focus:opacity-100 focus-within:opacity-100 end-0 top-0 w-full">
Expand All @@ -64,16 +74,22 @@ const MermaidDiagramInternal: React.FC<MermaidDiagramProps> = ({ chart, id }) =>
);
};

const MermaidDiagram = ({ result }: { result: GenerateMermaidDiagramResult }) => {
const MermaidDiagram = ({
result,
}: {
result: GenerateMermaidDiagramResult;
}) => {
const { content, isError } = result;

if (isError) {
return <MermaidErrorFallback />;
}
const chart = content[0].text;
const mermaidDiagramWithoutCodeBlock = chart.replace(/```mermaid\n([\s\S]*?)```/g, '$1').trim();
const mermaidDiagramWithoutCodeBlock = chart
.replace(/```mermaid\n([\s\S]*?)```/g, "$1")
.trim();

return <MermaidDiagramInternal chart={mermaidDiagramWithoutCodeBlock} />
}
return <MermaidDiagramInternal chart={mermaidDiagramWithoutCodeBlock} />;
};

export default MermaidDiagram;
export default MermaidDiagram;
2 changes: 1 addition & 1 deletion components/ui/ImageResult.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Image from "next/image";
import { ImageGenerationResult } from "@/lib/tools/generateImage";
import { useImageDownloader } from "@/hooks/useImageDownloader";
import MessageMediaDownloadButton from "../Chat/MessageMediaDownloadButton";
import MessageMediaDownloadButton from "../VercelChat/MessageMediaDownloadButton";

interface ImageResultProps {
result: ImageGenerationResult;
Expand Down
Loading
0