|
| 1 | +import { Artifact } from "@/lib/artifacts/artifact"; |
| 2 | +import type { ArtifactMetadata } from "@/lib/artifacts/types"; |
| 3 | +import { Play, Copy, Download, RefreshCw } from "lucide-react"; |
| 4 | +import { toast } from "sonner"; |
| 5 | + |
| 6 | +interface CodeArtifactMetadata extends ArtifactMetadata { |
| 7 | + language: string; |
| 8 | + lineCount: number; |
| 9 | + lastExecuted?: Date; |
| 10 | + isExecuting: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export const codeArtifact = new Artifact<"code", CodeArtifactMetadata>({ |
| 14 | + kind: "code", |
| 15 | + description: "A code artifact for writing, editing, and executing code snippets.", |
| 16 | + |
| 17 | + initialize: async ({ documentId, setMetadata }) => { |
| 18 | + setMetadata({ |
| 19 | + language: "javascript", |
| 20 | + lineCount: 0, |
| 21 | + isExecuting: false, |
| 22 | + }); |
| 23 | + }, |
| 24 | + |
| 25 | + onStreamPart: ({ streamPart, setMetadata, setArtifact }) => { |
| 26 | + if (streamPart.type === "language-update") { |
| 27 | + setMetadata((metadata) => ({ |
| 28 | + ...metadata, |
| 29 | + language: streamPart.content as string, |
| 30 | + })); |
| 31 | + } |
| 32 | + |
| 33 | + if (streamPart.type === "content-update") { |
| 34 | + setArtifact((draftArtifact) => { |
| 35 | + const newContent = draftArtifact.content + (streamPart.content as string); |
| 36 | + const lineCount = newContent.split('\n').length; |
| 37 | + |
| 38 | + setMetadata((metadata) => ({ |
| 39 | + ...metadata, |
| 40 | + lineCount, |
| 41 | + })); |
| 42 | + |
| 43 | + return { |
| 44 | + ...draftArtifact, |
| 45 | + content: newContent, |
| 46 | + status: "streaming", |
| 47 | + }; |
| 48 | + }); |
| 49 | + } |
| 50 | + }, |
| 51 | + |
| 52 | + content: ({ |
| 53 | + mode, |
| 54 | + status, |
| 55 | + content, |
| 56 | + isCurrentVersion, |
| 57 | + currentVersionIndex, |
| 58 | + onSaveContent, |
| 59 | + getDocumentContentById, |
| 60 | + isLoading, |
| 61 | + metadata, |
| 62 | + }) => { |
| 63 | + if (isLoading) { |
| 64 | + return ( |
| 65 | + <div className="flex items-center justify-center p-8"> |
| 66 | + <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-green-600"></div> |
| 67 | + <span className="ml-2 text-sm text-gray-600">Loading code artifact...</span> |
| 68 | + </div> |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if (mode === "diff") { |
| 73 | + const oldContent = getDocumentContentById(currentVersionIndex - 1); |
| 74 | + const newContent = getDocumentContentById(currentVersionIndex); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="space-y-4"> |
| 78 | + <h3 className="text-lg font-semibold">Code Comparison</h3> |
| 79 | + <div className="grid grid-cols-2 gap-4"> |
| 80 | + <div> |
| 81 | + <h4 className="text-sm font-medium text-gray-600 mb-2">Previous Version</h4> |
| 82 | + <div className="bg-red-50 border border-red-200 rounded-lg p-4"> |
| 83 | + <pre className="text-sm font-mono overflow-x-auto"> |
| 84 | + <code>{oldContent}</code> |
| 85 | + </pre> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + <div> |
| 89 | + <h4 className="text-sm font-medium text-gray-600 mb-2">Current Version</h4> |
| 90 | + <div className="bg-green-50 border border-green-200 rounded-lg p-4"> |
| 91 | + <pre className="text-sm font-mono overflow-x-auto"> |
| 92 | + <code>{newContent}</code> |
| 93 | + </pre> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const executeCode = async () => { |
| 102 | + // TODO: Implement code execution using E2B or similar service |
| 103 | + toast.info("Code execution coming soon!"); |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <div className="space-y-4"> |
| 108 | + <div className="flex items-center justify-between text-sm text-gray-600"> |
| 109 | + <div className="flex items-center space-x-4"> |
| 110 | + <span>Language: {metadata.language}</span> |
| 111 | + <span>Lines: {metadata.lineCount}</span> |
| 112 | + {metadata.lastExecuted && ( |
| 113 | + <span>Last executed: {metadata.lastExecuted.toLocaleTimeString()}</span> |
| 114 | + )} |
| 115 | + </div> |
| 116 | + |
| 117 | + <div className="flex items-center space-x-2"> |
| 118 | + <button |
| 119 | + onClick={executeCode} |
| 120 | + disabled={!isCurrentVersion || metadata.isExecuting} |
| 121 | + className="flex items-center space-x-1 px-3 py-1 bg-green-600 text-white rounded text-sm hover:bg-green-700 disabled:opacity-50" |
| 122 | + > |
| 123 | + <Play className="w-3 h-3" /> |
| 124 | + <span>{metadata.isExecuting ? "Running..." : "Run"}</span> |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + |
| 129 | + <div className="border rounded-lg bg-gray-900"> |
| 130 | + <div className="flex items-center justify-between px-4 py-2 bg-gray-800 rounded-t-lg"> |
| 131 | + <span className="text-white text-sm">Language: {metadata.language}</span> |
| 132 | + </div> |
| 133 | + |
| 134 | + <textarea |
| 135 | + value={content} |
| 136 | + onChange={(e) => onSaveContent(e.target.value)} |
| 137 | + className="w-full h-96 p-4 bg-gray-900 text-green-400 font-mono text-sm border-0 rounded-b-lg resize-none focus:outline-none focus:ring-2 focus:ring-green-500" |
| 138 | + placeholder="// Write your code here..." |
| 139 | + disabled={!isCurrentVersion} |
| 140 | + spellCheck={false} |
| 141 | + /> |
| 142 | + </div> |
| 143 | + |
| 144 | + {status === "streaming" && ( |
| 145 | + <div className="flex items-center text-sm text-green-600"> |
| 146 | + <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-green-600 mr-2"></div> |
| 147 | + Generating code... |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + ); |
| 152 | + }, |
| 153 | + |
| 154 | + actions: [ |
| 155 | + { |
| 156 | + icon: <RefreshCw className="w-4 h-4" />, |
| 157 | + description: "Regenerate code", |
| 158 | + onClick: ({ appendMessage }) => { |
| 159 | + appendMessage({ |
| 160 | + role: "user", |
| 161 | + content: "Please regenerate and improve this code.", |
| 162 | + }); |
| 163 | + }, |
| 164 | + }, |
| 165 | + ], |
| 166 | + |
| 167 | + toolbar: [ |
| 168 | + { |
| 169 | + icon: <Copy className="w-4 h-4" />, |
| 170 | + description: "Copy code", |
| 171 | + onClick: () => { |
| 172 | + toast.success("Code copied to clipboard!"); |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + icon: <Download className="w-4 h-4" />, |
| 177 | + description: "Download file", |
| 178 | + onClick: () => { |
| 179 | + toast.success("File downloaded!"); |
| 180 | + }, |
| 181 | + }, |
| 182 | + ], |
| 183 | +}); |
0 commit comments