@cloudbase/agent-react-ui 1.0.1-alpha.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/README.md +123 -0
  2. package/components.json +21 -0
  3. package/dist/index.css +4241 -0
  4. package/dist/index.css.map +1 -0
  5. package/dist/index.d.mts +59 -0
  6. package/dist/index.d.ts +59 -0
  7. package/dist/index.js +2169 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/index.mjs +2182 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/example/.env.sample +2 -0
  12. package/example/App.tsx +368 -0
  13. package/example/app.css +1 -0
  14. package/example/index.html +12 -0
  15. package/example/main.tsx +9 -0
  16. package/example/vite.config.ts +34 -0
  17. package/package.json +75 -0
  18. package/postcss.config.cjs +3 -0
  19. package/src/components/ai-elements/agent.tsx +140 -0
  20. package/src/components/ai-elements/artifact.tsx +147 -0
  21. package/src/components/ai-elements/attachments.tsx +421 -0
  22. package/src/components/ai-elements/audio-player.tsx +228 -0
  23. package/src/components/ai-elements/canvas.tsx +22 -0
  24. package/src/components/ai-elements/chain-of-thought.tsx +228 -0
  25. package/src/components/ai-elements/checkpoint.tsx +71 -0
  26. package/src/components/ai-elements/code-block.tsx +532 -0
  27. package/src/components/ai-elements/commit.tsx +448 -0
  28. package/src/components/ai-elements/confirmation.tsx +176 -0
  29. package/src/components/ai-elements/connection.tsx +28 -0
  30. package/src/components/ai-elements/context.tsx +408 -0
  31. package/src/components/ai-elements/controls.tsx +18 -0
  32. package/src/components/ai-elements/conversation.tsx +100 -0
  33. package/src/components/ai-elements/edge.tsx +140 -0
  34. package/src/components/ai-elements/environment-variables.tsx +295 -0
  35. package/src/components/ai-elements/file-tree.tsx +258 -0
  36. package/src/components/ai-elements/image.tsx +24 -0
  37. package/src/components/ai-elements/inline-citation.tsx +287 -0
  38. package/src/components/ai-elements/message.tsx +336 -0
  39. package/src/components/ai-elements/mic-selector.tsx +370 -0
  40. package/src/components/ai-elements/model-selector.tsx +211 -0
  41. package/src/components/ai-elements/node.tsx +71 -0
  42. package/src/components/ai-elements/open-in-chat.tsx +365 -0
  43. package/src/components/ai-elements/package-info.tsx +233 -0
  44. package/src/components/ai-elements/panel.tsx +15 -0
  45. package/src/components/ai-elements/persona.tsx +270 -0
  46. package/src/components/ai-elements/plan.tsx +142 -0
  47. package/src/components/ai-elements/prompt-input.tsx +1263 -0
  48. package/src/components/ai-elements/queue.tsx +274 -0
  49. package/src/components/ai-elements/reasoning.tsx +193 -0
  50. package/src/components/ai-elements/sandbox.tsx +126 -0
  51. package/src/components/ai-elements/schema-display.tsx +458 -0
  52. package/src/components/ai-elements/shimmer.tsx +64 -0
  53. package/src/components/ai-elements/snippet.tsx +139 -0
  54. package/src/components/ai-elements/sources.tsx +77 -0
  55. package/src/components/ai-elements/speech-input.tsx +301 -0
  56. package/src/components/ai-elements/stack-trace.tsx +482 -0
  57. package/src/components/ai-elements/suggestion.tsx +53 -0
  58. package/src/components/ai-elements/task.tsx +87 -0
  59. package/src/components/ai-elements/terminal.tsx +261 -0
  60. package/src/components/ai-elements/test-results.tsx +485 -0
  61. package/src/components/ai-elements/tool.tsx +174 -0
  62. package/src/components/ai-elements/toolbar.tsx +16 -0
  63. package/src/components/ai-elements/transcription.tsx +124 -0
  64. package/src/components/ai-elements/voice-selector.tsx +479 -0
  65. package/src/components/ai-elements/web-preview.tsx +263 -0
  66. package/src/components/chat/Chat.tsx +178 -0
  67. package/src/components/chat/Input.tsx +98 -0
  68. package/src/components/chat/Message.tsx +276 -0
  69. package/src/components/chat/index.ts +2 -0
  70. package/src/components/index.ts +1 -0
  71. package/src/components/ui/accordion.tsx +64 -0
  72. package/src/components/ui/alert.tsx +66 -0
  73. package/src/components/ui/avatar.tsx +107 -0
  74. package/src/components/ui/badge.tsx +48 -0
  75. package/src/components/ui/button-group.tsx +83 -0
  76. package/src/components/ui/button.tsx +64 -0
  77. package/src/components/ui/card.tsx +92 -0
  78. package/src/components/ui/carousel.tsx +239 -0
  79. package/src/components/ui/collapsible.tsx +31 -0
  80. package/src/components/ui/command.tsx +184 -0
  81. package/src/components/ui/dialog.tsx +158 -0
  82. package/src/components/ui/dropdown-menu.tsx +257 -0
  83. package/src/components/ui/hover-card.tsx +42 -0
  84. package/src/components/ui/input-group.tsx +168 -0
  85. package/src/components/ui/input.tsx +21 -0
  86. package/src/components/ui/popover.tsx +87 -0
  87. package/src/components/ui/progress.tsx +31 -0
  88. package/src/components/ui/scroll-area.tsx +56 -0
  89. package/src/components/ui/select.tsx +190 -0
  90. package/src/components/ui/separator.tsx +28 -0
  91. package/src/components/ui/spinner.tsx +16 -0
  92. package/src/components/ui/switch.tsx +33 -0
  93. package/src/components/ui/tabs.tsx +91 -0
  94. package/src/components/ui/textarea.tsx +18 -0
  95. package/src/components/ui/tooltip.tsx +61 -0
  96. package/src/css/global.css +123 -0
  97. package/src/css/index.css +1 -0
  98. package/src/hooks/index.ts +1 -0
  99. package/src/hooks/use-copy-to-clipboard.ts +31 -0
  100. package/src/index.ts +4 -0
  101. package/src/lib/utils.ts +6 -0
  102. package/src/locales/context.ts +8 -0
  103. package/src/locales/hooks.ts +20 -0
  104. package/src/locales/index.ts +3 -0
  105. package/src/locales/langs/en.ts +17 -0
  106. package/src/locales/langs/index.ts +12 -0
  107. package/src/locales/langs/zh-cn.ts +18 -0
  108. package/tsconfig.json +21 -0
  109. package/tsup.config.ts +21 -0
@@ -0,0 +1,77 @@
1
+ "use client";
2
+
3
+ import {
4
+ Collapsible,
5
+ CollapsibleContent,
6
+ CollapsibleTrigger,
7
+ } from "@/components/ui/collapsible";
8
+ import { cn } from "@/lib/utils";
9
+ import { BookIcon, ChevronDownIcon } from "lucide-react";
10
+ import type { ComponentProps } from "react";
11
+
12
+ export type SourcesProps = ComponentProps<"div">;
13
+
14
+ export const Sources = ({ className, ...props }: SourcesProps) => (
15
+ <Collapsible
16
+ className={cn("not-prose mb-4 text-primary text-xs", className)}
17
+ {...props}
18
+ />
19
+ );
20
+
21
+ export type SourcesTriggerProps = ComponentProps<typeof CollapsibleTrigger> & {
22
+ count: number;
23
+ };
24
+
25
+ export const SourcesTrigger = ({
26
+ className,
27
+ count,
28
+ children,
29
+ ...props
30
+ }: SourcesTriggerProps) => (
31
+ <CollapsibleTrigger
32
+ className={cn("flex items-center gap-2", className)}
33
+ {...props}
34
+ >
35
+ {children ?? (
36
+ <>
37
+ <p className="font-medium">Used {count} sources</p>
38
+ <ChevronDownIcon className="h-4 w-4" />
39
+ </>
40
+ )}
41
+ </CollapsibleTrigger>
42
+ );
43
+
44
+ export type SourcesContentProps = ComponentProps<typeof CollapsibleContent>;
45
+
46
+ export const SourcesContent = ({
47
+ className,
48
+ ...props
49
+ }: SourcesContentProps) => (
50
+ <CollapsibleContent
51
+ className={cn(
52
+ "mt-3 flex w-fit flex-col gap-2",
53
+ "data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-top-2 data-[state=open]:slide-in-from-top-2 outline-none data-[state=closed]:animate-out data-[state=open]:animate-in",
54
+ className
55
+ )}
56
+ {...props}
57
+ />
58
+ );
59
+
60
+ export type SourceProps = ComponentProps<"a">;
61
+
62
+ export const Source = ({ href, title, children, ...props }: SourceProps) => (
63
+ <a
64
+ className="flex items-center gap-2"
65
+ href={href}
66
+ rel="noreferrer"
67
+ target="_blank"
68
+ {...props}
69
+ >
70
+ {children ?? (
71
+ <>
72
+ <BookIcon className="h-4 w-4" />
73
+ <span className="block font-medium">{title}</span>
74
+ </>
75
+ )}
76
+ </a>
77
+ );
@@ -0,0 +1,301 @@
1
+ "use client";
2
+
3
+ import { Button } from "@/components/ui/button";
4
+ import { Spinner } from "@/components/ui/spinner";
5
+ import { cn } from "@/lib/utils";
6
+ import { MicIcon, SquareIcon } from "lucide-react";
7
+ import {
8
+ type ComponentProps,
9
+ useCallback,
10
+ useEffect,
11
+ useRef,
12
+ useState,
13
+ } from "react";
14
+
15
+ interface SpeechRecognition extends EventTarget {
16
+ continuous: boolean;
17
+ interimResults: boolean;
18
+ lang: string;
19
+ start(): void;
20
+ stop(): void;
21
+ onstart: ((this: SpeechRecognition, ev: Event) => void) | null;
22
+ onend: ((this: SpeechRecognition, ev: Event) => void) | null;
23
+ onresult:
24
+ | ((this: SpeechRecognition, ev: SpeechRecognitionEvent) => void)
25
+ | null;
26
+ onerror:
27
+ | ((this: SpeechRecognition, ev: SpeechRecognitionErrorEvent) => void)
28
+ | null;
29
+ }
30
+
31
+ interface SpeechRecognitionEvent extends Event {
32
+ results: SpeechRecognitionResultList;
33
+ resultIndex: number;
34
+ }
35
+
36
+ interface SpeechRecognitionResultList {
37
+ readonly length: number;
38
+ item(index: number): SpeechRecognitionResult;
39
+ [index: number]: SpeechRecognitionResult;
40
+ }
41
+
42
+ interface SpeechRecognitionResult {
43
+ readonly length: number;
44
+ item(index: number): SpeechRecognitionAlternative;
45
+ [index: number]: SpeechRecognitionAlternative;
46
+ isFinal: boolean;
47
+ }
48
+
49
+ interface SpeechRecognitionAlternative {
50
+ transcript: string;
51
+ confidence: number;
52
+ }
53
+
54
+ interface SpeechRecognitionErrorEvent extends Event {
55
+ error: string;
56
+ }
57
+
58
+ declare global {
59
+ interface Window {
60
+ SpeechRecognition: {
61
+ new (): SpeechRecognition;
62
+ };
63
+ webkitSpeechRecognition: {
64
+ new (): SpeechRecognition;
65
+ };
66
+ }
67
+ }
68
+
69
+ type SpeechInputMode = "speech-recognition" | "media-recorder" | "none";
70
+
71
+ export type SpeechInputProps = ComponentProps<typeof Button> & {
72
+ onTranscriptionChange?: (text: string) => void;
73
+ /**
74
+ * Callback for when audio is recorded using MediaRecorder fallback.
75
+ * This is called in browsers that don't support the Web Speech API (Firefox, Safari).
76
+ * The callback receives an audio Blob that should be sent to a transcription service.
77
+ * Return the transcribed text, which will be passed to onTranscriptionChange.
78
+ */
79
+ onAudioRecorded?: (audioBlob: Blob) => Promise<string>;
80
+ lang?: string;
81
+ };
82
+
83
+ const detectSpeechInputMode = (): SpeechInputMode => {
84
+ if (typeof window === "undefined") {
85
+ return "none";
86
+ }
87
+
88
+ if ("SpeechRecognition" in window || "webkitSpeechRecognition" in window) {
89
+ return "speech-recognition";
90
+ }
91
+
92
+ if ("MediaRecorder" in window && "mediaDevices" in navigator) {
93
+ return "media-recorder";
94
+ }
95
+
96
+ return "none";
97
+ };
98
+
99
+ export const SpeechInput = ({
100
+ className,
101
+ onTranscriptionChange,
102
+ onAudioRecorded,
103
+ lang = "en-US",
104
+ ...props
105
+ }: SpeechInputProps) => {
106
+ const [isListening, setIsListening] = useState(false);
107
+ const [isProcessing, setIsProcessing] = useState(false);
108
+ const [mode, setMode] = useState<SpeechInputMode>("none");
109
+ const [recognition, setRecognition] = useState<SpeechRecognition | null>(
110
+ null
111
+ );
112
+ const recognitionRef = useRef<SpeechRecognition | null>(null);
113
+ const mediaRecorderRef = useRef<MediaRecorder | null>(null);
114
+ const audioChunksRef = useRef<Blob[]>([]);
115
+
116
+ // Detect mode on mount
117
+ useEffect(() => {
118
+ setMode(detectSpeechInputMode());
119
+ }, []);
120
+
121
+ // Initialize Speech Recognition when mode is speech-recognition
122
+ useEffect(() => {
123
+ if (mode !== "speech-recognition") {
124
+ return;
125
+ }
126
+
127
+ const SpeechRecognition =
128
+ window.SpeechRecognition || window.webkitSpeechRecognition;
129
+ const speechRecognition = new SpeechRecognition();
130
+
131
+ speechRecognition.continuous = true;
132
+ speechRecognition.interimResults = true;
133
+ speechRecognition.lang = lang;
134
+
135
+ speechRecognition.onstart = () => {
136
+ setIsListening(true);
137
+ };
138
+
139
+ speechRecognition.onend = () => {
140
+ setIsListening(false);
141
+ };
142
+
143
+ speechRecognition.onresult = (event) => {
144
+ let finalTranscript = "";
145
+
146
+ for (let i = event.resultIndex; i < event.results.length; i++) {
147
+ const result = event.results[i];
148
+ if (result.isFinal) {
149
+ finalTranscript += result[0]?.transcript ?? "";
150
+ }
151
+ }
152
+
153
+ if (finalTranscript) {
154
+ onTranscriptionChange?.(finalTranscript);
155
+ }
156
+ };
157
+
158
+ speechRecognition.onerror = (event) => {
159
+ console.error("Speech recognition error:", event.error);
160
+ setIsListening(false);
161
+ };
162
+
163
+ recognitionRef.current = speechRecognition;
164
+ setRecognition(speechRecognition);
165
+
166
+ return () => {
167
+ if (recognitionRef.current) {
168
+ recognitionRef.current.stop();
169
+ }
170
+ };
171
+ }, [mode, onTranscriptionChange, lang]);
172
+
173
+ // Start MediaRecorder recording
174
+ const startMediaRecorder = useCallback(async () => {
175
+ if (!onAudioRecorded) {
176
+ console.warn(
177
+ "SpeechInput: onAudioRecorded callback is required for MediaRecorder fallback"
178
+ );
179
+ return;
180
+ }
181
+
182
+ try {
183
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
184
+ const mediaRecorder = new MediaRecorder(stream);
185
+ audioChunksRef.current = [];
186
+
187
+ mediaRecorder.ondataavailable = (event) => {
188
+ if (event.data.size > 0) {
189
+ audioChunksRef.current.push(event.data);
190
+ }
191
+ };
192
+
193
+ mediaRecorder.onstop = async () => {
194
+ // Stop all tracks to release the microphone
195
+ for (const track of stream.getTracks()) {
196
+ track.stop();
197
+ }
198
+
199
+ const audioBlob = new Blob(audioChunksRef.current, {
200
+ type: "audio/webm",
201
+ });
202
+
203
+ if (audioBlob.size > 0) {
204
+ setIsProcessing(true);
205
+ try {
206
+ const transcript = await onAudioRecorded(audioBlob);
207
+ if (transcript) {
208
+ onTranscriptionChange?.(transcript);
209
+ }
210
+ } catch (error) {
211
+ console.error("Transcription error:", error);
212
+ } finally {
213
+ setIsProcessing(false);
214
+ }
215
+ }
216
+ };
217
+
218
+ mediaRecorder.onerror = (event) => {
219
+ console.error("MediaRecorder error:", event);
220
+ setIsListening(false);
221
+ // Stop all tracks on error
222
+ for (const track of stream.getTracks()) {
223
+ track.stop();
224
+ }
225
+ };
226
+
227
+ mediaRecorderRef.current = mediaRecorder;
228
+ mediaRecorder.start();
229
+ setIsListening(true);
230
+ } catch (error) {
231
+ console.error("Failed to start MediaRecorder:", error);
232
+ setIsListening(false);
233
+ }
234
+ }, [onAudioRecorded, onTranscriptionChange]);
235
+
236
+ // Stop MediaRecorder recording
237
+ const stopMediaRecorder = useCallback(() => {
238
+ if (mediaRecorderRef.current?.state === "recording") {
239
+ mediaRecorderRef.current.stop();
240
+ }
241
+ setIsListening(false);
242
+ }, []);
243
+
244
+ const toggleListening = useCallback(() => {
245
+ if (mode === "speech-recognition" && recognition) {
246
+ if (isListening) {
247
+ recognition.stop();
248
+ } else {
249
+ recognition.start();
250
+ }
251
+ } else if (mode === "media-recorder") {
252
+ if (isListening) {
253
+ stopMediaRecorder();
254
+ } else {
255
+ startMediaRecorder();
256
+ }
257
+ }
258
+ }, [mode, recognition, isListening, startMediaRecorder, stopMediaRecorder]);
259
+
260
+ // Determine if button should be disabled
261
+ const isDisabled =
262
+ mode === "none" ||
263
+ (mode === "speech-recognition" && !recognition) ||
264
+ (mode === "media-recorder" && !onAudioRecorded) ||
265
+ isProcessing;
266
+
267
+ return (
268
+ <div className="relative inline-flex items-center justify-center">
269
+ {/* Animated pulse rings */}
270
+ {isListening &&
271
+ [0, 1, 2].map((index) => (
272
+ <div
273
+ className="absolute inset-0 animate-ping rounded-full border-2 border-red-400/30"
274
+ key={index}
275
+ style={{
276
+ animationDelay: `${index * 0.3}s`,
277
+ animationDuration: "2s",
278
+ }}
279
+ />
280
+ ))}
281
+
282
+ {/* Main record button */}
283
+ <Button
284
+ className={cn(
285
+ "relative z-10 rounded-full transition-all duration-300",
286
+ isListening
287
+ ? "bg-destructive text-white hover:bg-destructive/80 hover:text-white"
288
+ : "bg-primary text-primary-foreground hover:bg-primary/80 hover:text-primary-foreground",
289
+ className
290
+ )}
291
+ disabled={isDisabled}
292
+ onClick={toggleListening}
293
+ {...props}
294
+ >
295
+ {isProcessing && <Spinner />}
296
+ {!isProcessing && isListening && <SquareIcon className="size-4" />}
297
+ {!(isProcessing || isListening) && <MicIcon className="size-4" />}
298
+ </Button>
299
+ </div>
300
+ );
301
+ };