suggestion-input.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Derived from vercel/ai-elements (skills/ai-elements/scripts/suggestion-input.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 type { PromptInputMessage } from "@/components/ai-elements/prompt-input";
  9. import {
  10. PromptInput,
  11. PromptInputButton,
  12. PromptInputFooter,
  13. PromptInputSelect,
  14. PromptInputSelectContent,
  15. PromptInputSelectItem,
  16. PromptInputSelectTrigger,
  17. PromptInputSelectValue,
  18. PromptInputSubmit,
  19. PromptInputTextarea,
  20. PromptInputTools,
  21. } from "@/components/ai-elements/prompt-input";
  22. import { Suggestion, Suggestions } from "@/components/ai-elements/suggestion";
  23. import { GlobeIcon, MicIcon, PlusIcon, SendIcon } from "lucide-react";
  24. import { nanoid } from "nanoid";
  25. import { memo, useCallback, useState } from "react";
  26. const suggestions: { key: string; value: string }[] = [
  27. { key: nanoid(), value: "What are the latest trends in AI?" },
  28. { key: nanoid(), value: "How does machine learning work?" },
  29. { key: nanoid(), value: "Explain quantum computing" },
  30. { key: nanoid(), value: "Best practices for React development" },
  31. { key: nanoid(), value: "Tell me about TypeScript benefits" },
  32. { key: nanoid(), value: "How to optimize database queries?" },
  33. { key: nanoid(), value: "What is the difference between SQL and NoSQL?" },
  34. { key: nanoid(), value: "Explain cloud computing basics" },
  35. ];
  36. const models = [
  37. { id: "gpt-4", name: "GPT-4" },
  38. { id: "gpt-3.5-turbo", name: "GPT-3.5 Turbo" },
  39. { id: "claude-2", name: "Claude 2" },
  40. { id: "claude-instant", name: "Claude Instant" },
  41. { id: "palm-2", name: "PaLM 2" },
  42. { id: "llama-2-70b", name: "Llama 2 70B" },
  43. { id: "llama-2-13b", name: "Llama 2 13B" },
  44. { id: "cohere-command", name: "Command" },
  45. { id: "mistral-7b", name: "Mistral 7B" },
  46. ];
  47. const handleSubmit = (message: PromptInputMessage) => {
  48. const hasText = Boolean(message.text);
  49. const hasAttachments = Boolean(message.files?.length);
  50. if (!(hasText || hasAttachments)) {
  51. return;
  52. }
  53. console.log("Submitted message:", message.text || "Sent with attachments");
  54. console.log("Attached files:", message.files);
  55. };
  56. interface SuggestionItemProps {
  57. suggestion: { key: string; value: string };
  58. onSuggestionClick: (value: string) => void;
  59. }
  60. const SuggestionItem = memo(({ suggestion, onSuggestionClick }: SuggestionItemProps) => {
  61. const handleClick = useCallback(
  62. () => onSuggestionClick(suggestion.value),
  63. [onSuggestionClick, suggestion.value],
  64. );
  65. return <Suggestion key={suggestion.key} onClick={handleClick} suggestion={suggestion.value} />;
  66. });
  67. SuggestionItem.displayName = "SuggestionItem";
  68. const Example = () => {
  69. const [model, setModel] = useState<string>(models[0].id);
  70. const [text, setText] = useState<string>("");
  71. const handleSuggestionClick = useCallback((suggestion: string) => {
  72. setText(suggestion);
  73. }, []);
  74. const handleTextChange = useCallback(
  75. (e: React.ChangeEvent<HTMLTextAreaElement>) => setText(e.target.value),
  76. [],
  77. );
  78. return (
  79. <div className="grid gap-4">
  80. <Suggestions>
  81. {suggestions.map((suggestion) => (
  82. <SuggestionItem
  83. key={suggestion.key}
  84. onSuggestionClick={handleSuggestionClick}
  85. suggestion={suggestion}
  86. />
  87. ))}
  88. </Suggestions>
  89. <PromptInput onSubmit={handleSubmit}>
  90. <PromptInputTextarea
  91. onChange={handleTextChange}
  92. placeholder="Ask me about anything..."
  93. value={text}
  94. />
  95. <PromptInputFooter>
  96. <PromptInputTools>
  97. <PromptInputButton>
  98. <PlusIcon size={16} />
  99. </PromptInputButton>
  100. <PromptInputButton>
  101. <MicIcon size={16} />
  102. </PromptInputButton>
  103. <PromptInputButton>
  104. <GlobeIcon size={16} />
  105. <span>Search</span>
  106. </PromptInputButton>
  107. <PromptInputSelect onValueChange={setModel} value={model}>
  108. <PromptInputSelectTrigger>
  109. <PromptInputSelectValue />
  110. </PromptInputSelectTrigger>
  111. <PromptInputSelectContent>
  112. {models.map((m) => (
  113. <PromptInputSelectItem key={m.id} value={m.id}>
  114. {m.name}
  115. </PromptInputSelectItem>
  116. ))}
  117. </PromptInputSelectContent>
  118. </PromptInputSelect>
  119. </PromptInputTools>
  120. <PromptInputSubmit>
  121. <SendIcon size={16} />
  122. </PromptInputSubmit>
  123. </PromptInputFooter>
  124. </PromptInput>
  125. </div>
  126. );
  127. };
  128. export default Example;