conversation.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/conversation.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. Conversation,
  10. ConversationContent,
  11. ConversationDownload,
  12. ConversationEmptyState,
  13. ConversationScrollButton,
  14. } from "@/components/ai-elements/conversation";
  15. import { Message, MessageContent } from "@/components/ai-elements/message";
  16. import { MessageSquareIcon } from "lucide-react";
  17. import { nanoid } from "nanoid";
  18. import { useEffect, useState } from "react";
  19. const messages: {
  20. key: string;
  21. content: string;
  22. role: "user" | "assistant";
  23. }[] = [
  24. {
  25. content: "Hello, how are you?",
  26. key: nanoid(),
  27. role: "user",
  28. },
  29. {
  30. content: "I'm good, thank you! How can I assist you today?",
  31. key: nanoid(),
  32. role: "assistant",
  33. },
  34. {
  35. content: "I'm looking for information about your services.",
  36. key: nanoid(),
  37. role: "user",
  38. },
  39. {
  40. content: "Sure! We offer a variety of AI solutions. What are you interested in?",
  41. key: nanoid(),
  42. role: "assistant",
  43. },
  44. {
  45. content: "I'm interested in natural language processing tools.",
  46. key: nanoid(),
  47. role: "user",
  48. },
  49. {
  50. content: "Great choice! We have several NLP APIs. Would you like a demo?",
  51. key: nanoid(),
  52. role: "assistant",
  53. },
  54. {
  55. content: "Yes, a demo would be helpful.",
  56. key: nanoid(),
  57. role: "user",
  58. },
  59. {
  60. content: "Alright, I can show you a sentiment analysis example. Ready?",
  61. key: nanoid(),
  62. role: "assistant",
  63. },
  64. {
  65. content: "Yes, please proceed.",
  66. key: nanoid(),
  67. role: "user",
  68. },
  69. {
  70. content: "Here is a sample: 'I love this product!' → Positive sentiment.",
  71. key: nanoid(),
  72. role: "assistant",
  73. },
  74. {
  75. content: "Impressive! Can it handle multiple languages?",
  76. key: nanoid(),
  77. role: "user",
  78. },
  79. {
  80. content: "Absolutely, our models support over 20 languages.",
  81. key: nanoid(),
  82. role: "assistant",
  83. },
  84. {
  85. content: "How do I get started with the API?",
  86. key: nanoid(),
  87. role: "user",
  88. },
  89. {
  90. content: "You can sign up on our website and get an API key instantly.",
  91. key: nanoid(),
  92. role: "assistant",
  93. },
  94. {
  95. content: "Is there a free trial available?",
  96. key: nanoid(),
  97. role: "user",
  98. },
  99. {
  100. content: "Yes, we offer a 14-day free trial with full access.",
  101. key: nanoid(),
  102. role: "assistant",
  103. },
  104. {
  105. content: "What kind of support do you provide?",
  106. key: nanoid(),
  107. role: "user",
  108. },
  109. {
  110. content: "We provide 24/7 chat and email support for all users.",
  111. key: nanoid(),
  112. role: "assistant",
  113. },
  114. {
  115. content: "Thank you for the information!",
  116. key: nanoid(),
  117. role: "user",
  118. },
  119. {
  120. content: "You're welcome! Let me know if you have any more questions.",
  121. key: nanoid(),
  122. role: "assistant",
  123. },
  124. ];
  125. const Example = () => {
  126. const [visibleMessages, setVisibleMessages] = useState<
  127. {
  128. key: string;
  129. content: string;
  130. role: "user" | "assistant";
  131. }[]
  132. >([]);
  133. useEffect(() => {
  134. let currentIndex = 0;
  135. const interval = setInterval(() => {
  136. if (currentIndex < messages.length && messages[currentIndex]) {
  137. const currentMessage = messages[currentIndex];
  138. setVisibleMessages((prev) => [
  139. ...prev,
  140. {
  141. content: currentMessage.content,
  142. key: currentMessage.key,
  143. role: currentMessage.role,
  144. },
  145. ]);
  146. currentIndex += 1;
  147. } else {
  148. clearInterval(interval);
  149. }
  150. }, 500);
  151. return () => clearInterval(interval);
  152. }, []);
  153. return (
  154. <Conversation className="relative size-full">
  155. <ConversationContent>
  156. {visibleMessages.length === 0 ? (
  157. <ConversationEmptyState
  158. description="Messages will appear here as the conversation progresses."
  159. icon={<MessageSquareIcon className="size-6" />}
  160. title="Start a conversation"
  161. />
  162. ) : (
  163. visibleMessages.map(({ key, content, role }) => (
  164. <Message from={role} key={key}>
  165. <MessageContent>{content}</MessageContent>
  166. </Message>
  167. ))
  168. )}
  169. </ConversationContent>
  170. <ConversationDownload messages={visibleMessages} />
  171. <ConversationScrollButton />
  172. </Conversation>
  173. );
  174. };
  175. export default Example;