artifact.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/artifact.tsx).
  3. * Copyright 2023 Vercel, Inc. Licensed under Apache-2.0.
  4. * Modified by ZCode: local integration, formatting and adaptations.
  5. * See THIRD-PARTY-NOTICES.md in the repository root for license and provenance.
  6. */
  7. "use client";
  8. import {
  9. Artifact,
  10. ArtifactAction,
  11. ArtifactActions,
  12. ArtifactContent,
  13. ArtifactDescription,
  14. ArtifactHeader,
  15. ArtifactTitle,
  16. } from "@/components/ai-elements/artifact";
  17. import { CodeBlock } from "@/components/ai-elements/code-block";
  18. import { CopyIcon, DownloadIcon, PlayIcon, RefreshCwIcon, ShareIcon } from "lucide-react";
  19. const handleRun = () => {
  20. console.log("Run");
  21. };
  22. const handleCopy = () => {
  23. console.log("Copy");
  24. };
  25. const handleRegenerate = () => {
  26. console.log("Regenerate");
  27. };
  28. const handleDownload = () => {
  29. console.log("Download");
  30. };
  31. const handleShare = () => {
  32. console.log("Share");
  33. };
  34. const code = `# Dijkstra's Algorithm implementation
  35. import heapq
  36. def dijkstra(graph, start):
  37. distances = {node: float('inf') for node in graph}
  38. distances[start] = 0
  39. heap = [(0, start)]
  40. visited = set()
  41. while heap:
  42. current_distance, current_node = heapq.heappop(heap)
  43. if current_node in visited:
  44. continue
  45. visited.add(current_node)
  46. for neighbor, weight in graph[current_node].items():
  47. distance = current_distance + weight
  48. if distance < distances[neighbor]:
  49. distances[neighbor] = distance
  50. heapq.heappush(heap, (distance, neighbor))
  51. return distances
  52. # Example graph
  53. graph = {
  54. 'A': {'B': 1, 'C': 4},
  55. 'B': {'A': 1, 'C': 2, 'D': 5},
  56. 'C': {'A': 4, 'B': 2, 'D': 1},
  57. 'D': {'B': 5, 'C': 1}
  58. }
  59. print(dijkstra(graph, 'A'))`;
  60. const Example = () => (
  61. <Artifact>
  62. <ArtifactHeader>
  63. <div>
  64. <ArtifactTitle>Dijkstra&apos;s Algorithm Implementation</ArtifactTitle>
  65. <ArtifactDescription>Updated 1 minute ago</ArtifactDescription>
  66. </div>
  67. <div className="flex items-center gap-2">
  68. <ArtifactActions>
  69. <ArtifactAction icon={PlayIcon} label="Run" onClick={handleRun} tooltip="Run code" />
  70. <ArtifactAction
  71. icon={CopyIcon}
  72. label="Copy"
  73. onClick={handleCopy}
  74. tooltip="Copy to clipboard"
  75. />
  76. <ArtifactAction
  77. icon={RefreshCwIcon}
  78. label="Regenerate"
  79. onClick={handleRegenerate}
  80. tooltip="Regenerate content"
  81. />
  82. <ArtifactAction
  83. icon={DownloadIcon}
  84. label="Download"
  85. onClick={handleDownload}
  86. tooltip="Download file"
  87. />
  88. <ArtifactAction
  89. icon={ShareIcon}
  90. label="Share"
  91. onClick={handleShare}
  92. tooltip="Share artifact"
  93. />
  94. </ArtifactActions>
  95. </div>
  96. </ArtifactHeader>
  97. <ArtifactContent className="p-0">
  98. <CodeBlock className="border-none" code={code} language="python" showLineNumbers />
  99. </ArtifactContent>
  100. </Artifact>
  101. );
  102. export default Example;