@codrstudio/openclaude-chat 0.1.0

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 (171) hide show
  1. package/dist/components/Chat.d.ts +23 -0
  2. package/dist/components/Chat.js +12 -0
  3. package/dist/components/ErrorNote.d.ts +6 -0
  4. package/dist/components/ErrorNote.js +6 -0
  5. package/dist/components/LazyRender.d.ts +8 -0
  6. package/dist/components/LazyRender.js +22 -0
  7. package/dist/components/Markdown.d.ts +5 -0
  8. package/dist/components/Markdown.js +65 -0
  9. package/dist/components/MessageBubble.d.ts +9 -0
  10. package/dist/components/MessageBubble.js +45 -0
  11. package/dist/components/MessageInput.d.ts +19 -0
  12. package/dist/components/MessageInput.js +214 -0
  13. package/dist/components/MessageList.d.ts +13 -0
  14. package/dist/components/MessageList.js +72 -0
  15. package/dist/components/StreamingIndicator.d.ts +1 -0
  16. package/dist/components/StreamingIndicator.js +9 -0
  17. package/dist/display/AlertRenderer.d.ts +2 -0
  18. package/dist/display/AlertRenderer.js +13 -0
  19. package/dist/display/CarouselRenderer.d.ts +2 -0
  20. package/dist/display/CarouselRenderer.js +41 -0
  21. package/dist/display/ChartRenderer.d.ts +2 -0
  22. package/dist/display/ChartRenderer.js +76 -0
  23. package/dist/display/ChoiceButtonsRenderer.d.ts +6 -0
  24. package/dist/display/ChoiceButtonsRenderer.js +23 -0
  25. package/dist/display/CodeBlockRenderer.d.ts +2 -0
  26. package/dist/display/CodeBlockRenderer.js +17 -0
  27. package/dist/display/ComparisonTableRenderer.d.ts +2 -0
  28. package/dist/display/ComparisonTableRenderer.js +26 -0
  29. package/dist/display/DataTableRenderer.d.ts +2 -0
  30. package/dist/display/DataTableRenderer.js +74 -0
  31. package/dist/display/DisplayReactRenderer.d.ts +26 -0
  32. package/dist/display/DisplayReactRenderer.js +192 -0
  33. package/dist/display/FileCardRenderer.d.ts +2 -0
  34. package/dist/display/FileCardRenderer.js +31 -0
  35. package/dist/display/GalleryRenderer.d.ts +2 -0
  36. package/dist/display/GalleryRenderer.js +11 -0
  37. package/dist/display/ImageViewerRenderer.d.ts +2 -0
  38. package/dist/display/ImageViewerRenderer.js +15 -0
  39. package/dist/display/LinkPreviewRenderer.d.ts +2 -0
  40. package/dist/display/LinkPreviewRenderer.js +20 -0
  41. package/dist/display/MapViewRenderer.d.ts +2 -0
  42. package/dist/display/MapViewRenderer.js +20 -0
  43. package/dist/display/MetricCardRenderer.d.ts +2 -0
  44. package/dist/display/MetricCardRenderer.js +12 -0
  45. package/dist/display/PriceHighlightRenderer.d.ts +2 -0
  46. package/dist/display/PriceHighlightRenderer.js +30 -0
  47. package/dist/display/ProductCardRenderer.d.ts +2 -0
  48. package/dist/display/ProductCardRenderer.js +23 -0
  49. package/dist/display/ProgressStepsRenderer.d.ts +2 -0
  50. package/dist/display/ProgressStepsRenderer.js +14 -0
  51. package/dist/display/SourcesListRenderer.d.ts +2 -0
  52. package/dist/display/SourcesListRenderer.js +5 -0
  53. package/dist/display/SpreadsheetRenderer.d.ts +2 -0
  54. package/dist/display/SpreadsheetRenderer.js +32 -0
  55. package/dist/display/StepTimelineRenderer.d.ts +2 -0
  56. package/dist/display/StepTimelineRenderer.js +21 -0
  57. package/dist/display/index.d.ts +21 -0
  58. package/dist/display/index.js +20 -0
  59. package/dist/display/react-sandbox/bootstrap.d.ts +1 -0
  60. package/dist/display/react-sandbox/bootstrap.js +154 -0
  61. package/dist/display/registry.d.ts +5 -0
  62. package/dist/display/registry.js +52 -0
  63. package/dist/display/sdk-types.d.ts +187 -0
  64. package/dist/display/sdk-types.js +4 -0
  65. package/dist/hooks/ChatProvider.d.ts +9 -0
  66. package/dist/hooks/ChatProvider.js +14 -0
  67. package/dist/hooks/useIsMobile.d.ts +1 -0
  68. package/dist/hooks/useIsMobile.js +12 -0
  69. package/dist/hooks/useOpenClaudeChat.d.ts +36 -0
  70. package/dist/hooks/useOpenClaudeChat.js +361 -0
  71. package/dist/index.d.ts +47 -0
  72. package/dist/index.js +42 -0
  73. package/dist/lib/utils.d.ts +2 -0
  74. package/dist/lib/utils.js +5 -0
  75. package/dist/parts/PartErrorBoundary.d.ts +21 -0
  76. package/dist/parts/PartErrorBoundary.js +27 -0
  77. package/dist/parts/PartRenderer.d.ts +8 -0
  78. package/dist/parts/PartRenderer.js +99 -0
  79. package/dist/parts/ReasoningBlock.d.ts +6 -0
  80. package/dist/parts/ReasoningBlock.js +18 -0
  81. package/dist/parts/ToolActivity.d.ts +11 -0
  82. package/dist/parts/ToolActivity.js +52 -0
  83. package/dist/parts/ToolResult.d.ts +7 -0
  84. package/dist/parts/ToolResult.js +38 -0
  85. package/dist/styles.css +2 -0
  86. package/dist/types.d.ts +40 -0
  87. package/dist/types.js +4 -0
  88. package/dist/ui/alert.d.ts +12 -0
  89. package/dist/ui/alert.js +28 -0
  90. package/dist/ui/badge.d.ts +9 -0
  91. package/dist/ui/badge.js +20 -0
  92. package/dist/ui/button.d.ts +11 -0
  93. package/dist/ui/button.js +31 -0
  94. package/dist/ui/card.d.ts +8 -0
  95. package/dist/ui/card.js +21 -0
  96. package/dist/ui/collapsible.d.ts +1 -0
  97. package/dist/ui/collapsible.js +2 -0
  98. package/dist/ui/dialog.d.ts +19 -0
  99. package/dist/ui/dialog.js +23 -0
  100. package/dist/ui/dropdown-menu.d.ts +11 -0
  101. package/dist/ui/dropdown-menu.js +15 -0
  102. package/dist/ui/input.d.ts +3 -0
  103. package/dist/ui/input.js +6 -0
  104. package/dist/ui/progress.d.ts +7 -0
  105. package/dist/ui/progress.js +9 -0
  106. package/dist/ui/scroll-area.d.ts +5 -0
  107. package/dist/ui/scroll-area.js +12 -0
  108. package/dist/ui/separator.d.ts +4 -0
  109. package/dist/ui/separator.js +8 -0
  110. package/dist/ui/skeleton.d.ts +3 -0
  111. package/dist/ui/skeleton.js +6 -0
  112. package/dist/ui/table.d.ts +10 -0
  113. package/dist/ui/table.js +27 -0
  114. package/package.json +61 -0
  115. package/src/components/Chat.tsx +107 -0
  116. package/src/components/ErrorNote.tsx +35 -0
  117. package/src/components/LazyRender.tsx +42 -0
  118. package/src/components/Markdown.tsx +114 -0
  119. package/src/components/MessageBubble.tsx +107 -0
  120. package/src/components/MessageInput.tsx +421 -0
  121. package/src/components/MessageList.tsx +153 -0
  122. package/src/components/StreamingIndicator.tsx +19 -0
  123. package/src/display/AlertRenderer.tsx +23 -0
  124. package/src/display/CarouselRenderer.tsx +141 -0
  125. package/src/display/ChartRenderer.tsx +195 -0
  126. package/src/display/ChoiceButtonsRenderer.tsx +114 -0
  127. package/src/display/CodeBlockRenderer.tsx +49 -0
  128. package/src/display/ComparisonTableRenderer.tsx +132 -0
  129. package/src/display/DataTableRenderer.tsx +144 -0
  130. package/src/display/DisplayReactRenderer.tsx +269 -0
  131. package/src/display/FileCardRenderer.tsx +55 -0
  132. package/src/display/GalleryRenderer.tsx +65 -0
  133. package/src/display/ImageViewerRenderer.tsx +114 -0
  134. package/src/display/LinkPreviewRenderer.tsx +74 -0
  135. package/src/display/MapViewRenderer.tsx +75 -0
  136. package/src/display/MetricCardRenderer.tsx +29 -0
  137. package/src/display/PriceHighlightRenderer.tsx +62 -0
  138. package/src/display/ProductCardRenderer.tsx +112 -0
  139. package/src/display/ProgressStepsRenderer.tsx +59 -0
  140. package/src/display/SourcesListRenderer.tsx +47 -0
  141. package/src/display/SpreadsheetRenderer.tsx +86 -0
  142. package/src/display/StepTimelineRenderer.tsx +75 -0
  143. package/src/display/index.ts +21 -0
  144. package/src/display/react-sandbox/bootstrap.ts +155 -0
  145. package/src/display/registry.ts +84 -0
  146. package/src/display/sdk-types.ts +217 -0
  147. package/src/hooks/ChatProvider.tsx +21 -0
  148. package/src/hooks/useIsMobile.ts +15 -0
  149. package/src/hooks/useOpenClaudeChat.ts +476 -0
  150. package/src/index.ts +76 -0
  151. package/src/lib/utils.ts +6 -0
  152. package/src/parts/PartErrorBoundary.tsx +51 -0
  153. package/src/parts/PartRenderer.tsx +145 -0
  154. package/src/parts/ReasoningBlock.tsx +41 -0
  155. package/src/parts/ToolActivity.tsx +78 -0
  156. package/src/parts/ToolResult.tsx +79 -0
  157. package/src/styles.css +2 -0
  158. package/src/types.ts +41 -0
  159. package/src/ui/alert.tsx +77 -0
  160. package/src/ui/badge.tsx +36 -0
  161. package/src/ui/button.tsx +54 -0
  162. package/src/ui/card.tsx +68 -0
  163. package/src/ui/collapsible.tsx +7 -0
  164. package/src/ui/dialog.tsx +122 -0
  165. package/src/ui/dropdown-menu.tsx +76 -0
  166. package/src/ui/input.tsx +24 -0
  167. package/src/ui/progress.tsx +36 -0
  168. package/src/ui/scroll-area.tsx +48 -0
  169. package/src/ui/separator.tsx +31 -0
  170. package/src/ui/skeleton.tsx +9 -0
  171. package/src/ui/table.tsx +114 -0
@@ -0,0 +1,12 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area";
4
+ import * as React from "react";
5
+ import { cn } from "../lib/utils";
6
+ const ScrollArea = React.forwardRef(({ className, children, ...props }, ref) => (_jsxs(ScrollAreaPrimitive.Root, { ref: ref, className: cn("relative overflow-hidden", className), ...props, children: [_jsx(ScrollAreaPrimitive.Viewport, { className: "h-full w-full rounded-[inherit]", children: children }), _jsx(ScrollBar, {}), _jsx(ScrollAreaPrimitive.Corner, {})] })));
7
+ ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName;
8
+ const ScrollBar = React.forwardRef(({ className, orientation = "vertical", ...props }, ref) => (_jsx(ScrollAreaPrimitive.ScrollAreaScrollbar, { ref: ref, orientation: orientation, className: cn("flex touch-none select-none transition-colors", orientation === "vertical" &&
9
+ "h-full w-2.5 border-l border-l-transparent p-[1px]", orientation === "horizontal" &&
10
+ "h-2.5 flex-col border-t border-t-transparent p-[1px]", className), ...props, children: _jsx(ScrollAreaPrimitive.ScrollAreaThumb, { className: "relative flex-1 rounded-full bg-border" }) })));
11
+ ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName;
12
+ export { ScrollArea, ScrollBar };
@@ -0,0 +1,4 @@
1
+ import * as SeparatorPrimitive from "@radix-ui/react-separator";
2
+ import * as React from "react";
3
+ declare const Separator: React.ForwardRefExoticComponent<Omit<SeparatorPrimitive.SeparatorProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
4
+ export { Separator };
@@ -0,0 +1,8 @@
1
+ "use client";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import * as SeparatorPrimitive from "@radix-ui/react-separator";
4
+ import * as React from "react";
5
+ import { cn } from "../lib/utils";
6
+ const Separator = React.forwardRef(({ className, orientation = "horizontal", decorative = true, ...props }, ref) => (_jsx(SeparatorPrimitive.Root, { ref: ref, decorative: decorative, orientation: orientation, className: cn("shrink-0 bg-border", orientation === "horizontal" ? "h-[1px] w-full" : "h-full w-[1px]", className), ...props })));
7
+ Separator.displayName = SeparatorPrimitive.Root.displayName;
8
+ export { Separator };
@@ -0,0 +1,3 @@
1
+ import * as React from "react";
2
+ declare function Skeleton({ className, ...props }: React.HTMLAttributes<HTMLDivElement>): import("react/jsx-runtime").JSX.Element;
3
+ export { Skeleton };
@@ -0,0 +1,6 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { cn } from "../lib/utils";
3
+ function Skeleton({ className, ...props }) {
4
+ return _jsx("div", { className: cn("animate-pulse rounded-md bg-primary/10", className), ...props });
5
+ }
6
+ export { Skeleton };
@@ -0,0 +1,10 @@
1
+ import * as React from "react";
2
+ declare function Table({ className, ...props }: React.ComponentProps<"table">): import("react/jsx-runtime").JSX.Element;
3
+ declare function TableHeader({ className, ...props }: React.ComponentProps<"thead">): import("react/jsx-runtime").JSX.Element;
4
+ declare function TableBody({ className, ...props }: React.ComponentProps<"tbody">): import("react/jsx-runtime").JSX.Element;
5
+ declare function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">): import("react/jsx-runtime").JSX.Element;
6
+ declare function TableRow({ className, ...props }: React.ComponentProps<"tr">): import("react/jsx-runtime").JSX.Element;
7
+ declare function TableHead({ className, ...props }: React.ComponentProps<"th">): import("react/jsx-runtime").JSX.Element;
8
+ declare function TableCell({ className, ...props }: React.ComponentProps<"td">): import("react/jsx-runtime").JSX.Element;
9
+ declare function TableCaption({ className, ...props }: React.ComponentProps<"caption">): import("react/jsx-runtime").JSX.Element;
10
+ export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption, };
@@ -0,0 +1,27 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { cn } from "../lib/utils";
3
+ function Table({ className, ...props }) {
4
+ return (_jsx("div", { "data-slot": "table-container", className: "relative w-full overflow-x-auto", children: _jsx("table", { "data-slot": "table", className: cn("w-full caption-bottom text-sm", className), ...props }) }));
5
+ }
6
+ function TableHeader({ className, ...props }) {
7
+ return (_jsx("thead", { "data-slot": "table-header", className: cn("[&_tr]:border-b", className), ...props }));
8
+ }
9
+ function TableBody({ className, ...props }) {
10
+ return (_jsx("tbody", { "data-slot": "table-body", className: cn("[&_tr:last-child]:border-0", className), ...props }));
11
+ }
12
+ function TableFooter({ className, ...props }) {
13
+ return (_jsx("tfoot", { "data-slot": "table-footer", className: cn("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", className), ...props }));
14
+ }
15
+ function TableRow({ className, ...props }) {
16
+ return (_jsx("tr", { "data-slot": "table-row", className: cn("border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted", className), ...props }));
17
+ }
18
+ function TableHead({ className, ...props }) {
19
+ return (_jsx("th", { "data-slot": "table-head", className: cn("h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0", className), ...props }));
20
+ }
21
+ function TableCell({ className, ...props }) {
22
+ return (_jsx("td", { "data-slot": "table-cell", className: cn("p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0", className), ...props }));
23
+ }
24
+ function TableCaption({ className, ...props }) {
25
+ return (_jsx("caption", { "data-slot": "table-caption", className: cn("mt-4 text-sm text-muted-foreground", className), ...props }));
26
+ }
27
+ export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption, };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@codrstudio/openclaude-chat",
3
+ "version": "0.1.0",
4
+ "description": "Rich chat UI component — HTTP+SSE counterpart to @codrstudio/openclaude-sdk. Shadcn-native (consumes host tokens).",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./styles.css": "./dist/styles.css"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsc && npx @tailwindcss/cli -i src/styles.css -o dist/styles.css --minify",
22
+ "build:ts": "tsc",
23
+ "build:css": "npx @tailwindcss/cli -i src/styles.css -o dist/styles.css --minify",
24
+ "typecheck": "tsc --noEmit"
25
+ },
26
+ "peerDependencies": {
27
+ "react": "^18 || ^19",
28
+ "react-dom": "^18 || ^19"
29
+ },
30
+ "dependencies": {
31
+ "@radix-ui/react-collapsible": "^1",
32
+ "@radix-ui/react-dialog": "^1",
33
+ "@radix-ui/react-dropdown-menu": "^2",
34
+ "@radix-ui/react-scroll-area": "^1",
35
+ "@radix-ui/react-separator": "^1",
36
+ "@tanstack/react-virtual": "^3.13.23",
37
+ "class-variance-authority": "^0.7",
38
+ "clsx": "^2",
39
+ "embla-carousel-react": "^8",
40
+ "framer-motion": "^12.38.0",
41
+ "lucide-react": "^0.400.0",
42
+ "react-markdown": "^9",
43
+ "recharts": "^2",
44
+ "rehype-highlight": "^7",
45
+ "remark-gfm": "^4",
46
+ "sucrase": "^3.35.1",
47
+ "tailwind-merge": "^3"
48
+ },
49
+ "devDependencies": {
50
+ "@tailwindcss/cli": "^4",
51
+ "@types/react": "^19",
52
+ "@types/react-dom": "^19",
53
+ "tailwindcss": "^4",
54
+ "typescript": "^5"
55
+ },
56
+ "license": "MIT",
57
+ "repository": {
58
+ "type": "git",
59
+ "url": "git@github.com:codrstudio/openclaude-chat.git"
60
+ }
61
+ }
@@ -0,0 +1,107 @@
1
+ import React from "react";
2
+ import { cn } from "../lib/utils.js";
3
+ import { ChatProvider, useChatContext } from "../hooks/ChatProvider.js";
4
+ import { MessageList } from "./MessageList.js";
5
+ import { MessageInput } from "./MessageInput.js";
6
+ import type { DisplayRendererMap } from "../display/registry.js";
7
+ import type { Message } from "../types.js";
8
+
9
+ export interface ChatProps {
10
+ /** Base URL do servico openclaude (ex: http://localhost:9500/api/v1/ai). */
11
+ endpoint: string;
12
+ token?: string;
13
+ /** Sessao live existente. Se omitida, o hook cria uma via POST /sessions. */
14
+ sessionId?: string;
15
+ initialMessages?: Message[];
16
+ sessionOptions?: Record<string, unknown>;
17
+ turnOptions?: Record<string, unknown>;
18
+ displayRenderers?: DisplayRendererMap;
19
+ placeholder?: string;
20
+ header?: React.ReactNode;
21
+ footer?: React.ReactNode;
22
+ className?: string;
23
+ enableAttachments?: boolean;
24
+ enableVoice?: boolean;
25
+ fetcher?: typeof fetch;
26
+ emptyState?: React.ReactNode;
27
+ }
28
+
29
+ interface ChatContentProps {
30
+ displayRenderers?: DisplayRendererMap;
31
+ placeholder?: string;
32
+ enableAttachments?: boolean;
33
+ enableVoice?: boolean;
34
+ emptyState?: React.ReactNode;
35
+ }
36
+
37
+ function ChatContent({ displayRenderers, placeholder, enableAttachments = true, enableVoice = true, emptyState }: ChatContentProps) {
38
+ const { messages, input, setInput, handleSubmit, isLoading, stop, error, reload } = useChatContext();
39
+
40
+ return (
41
+ <>
42
+ <MessageList
43
+ messages={messages}
44
+ isLoading={isLoading}
45
+ displayRenderers={displayRenderers}
46
+ error={error ?? undefined}
47
+ onRetry={reload}
48
+ emptyState={emptyState}
49
+ />
50
+ <div className="px-4 pb-4">
51
+ <MessageInput
52
+ input={input}
53
+ setInput={setInput}
54
+ handleSubmit={handleSubmit}
55
+ isLoading={isLoading}
56
+ stop={stop}
57
+ placeholder={placeholder}
58
+ enableAttachments={enableAttachments}
59
+ enableVoice={enableVoice}
60
+ />
61
+ </div>
62
+ </>
63
+ );
64
+ }
65
+
66
+ export function Chat({
67
+ endpoint,
68
+ token,
69
+ sessionId,
70
+ initialMessages,
71
+ sessionOptions,
72
+ turnOptions,
73
+ displayRenderers,
74
+ placeholder,
75
+ header,
76
+ footer,
77
+ className,
78
+ enableAttachments,
79
+ enableVoice,
80
+ fetcher,
81
+ emptyState,
82
+ }: ChatProps) {
83
+ return (
84
+ <ChatProvider
85
+ key={sessionId}
86
+ endpoint={endpoint}
87
+ token={token}
88
+ sessionId={sessionId}
89
+ initialMessages={initialMessages}
90
+ sessionOptions={sessionOptions}
91
+ turnOptions={turnOptions}
92
+ fetcher={fetcher}
93
+ >
94
+ <div className={cn("flex flex-col h-full bg-background text-foreground", className)}>
95
+ {header}
96
+ <ChatContent
97
+ displayRenderers={displayRenderers}
98
+ placeholder={placeholder}
99
+ enableAttachments={enableAttachments}
100
+ enableVoice={enableVoice}
101
+ emptyState={emptyState}
102
+ />
103
+ {footer}
104
+ </div>
105
+ </ChatProvider>
106
+ );
107
+ }
@@ -0,0 +1,35 @@
1
+ import { AlertTriangle, RotateCcw } from "lucide-react";
2
+ import { cn } from "../lib/utils.js";
3
+
4
+ export interface ErrorNoteProps {
5
+ message?: string;
6
+ onRetry?: () => void;
7
+ className?: string;
8
+ }
9
+
10
+ export function ErrorNote({ message, onRetry, className }: ErrorNoteProps) {
11
+ return (
12
+ <div
13
+ role="alert"
14
+ className={cn(
15
+ "flex items-start gap-2 rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive",
16
+ className
17
+ )}
18
+ >
19
+ <AlertTriangle className="size-4 shrink-0 mt-0.5" />
20
+ <span className="flex-1 min-w-0 break-words">
21
+ {message ?? "Falha ao processar mensagem"}
22
+ </span>
23
+ {onRetry && (
24
+ <button
25
+ type="button"
26
+ onClick={onRetry}
27
+ className="shrink-0 inline-flex items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-destructive hover:bg-destructive/10 transition-colors"
28
+ >
29
+ <RotateCcw className="size-3.5" />
30
+ Tentar novamente
31
+ </button>
32
+ )}
33
+ </div>
34
+ );
35
+ }
@@ -0,0 +1,42 @@
1
+ import { useRef, useState, useEffect, type ReactNode } from "react";
2
+
3
+ interface LazyRenderProps {
4
+ children: ReactNode;
5
+ minHeight?: number;
6
+ rootMargin?: string;
7
+ }
8
+
9
+ export function LazyRender({ children, minHeight = 120, rootMargin = "200px" }: LazyRenderProps) {
10
+ const ref = useRef<HTMLDivElement>(null);
11
+ const [visible, setVisible] = useState(false);
12
+
13
+ useEffect(() => {
14
+ const el = ref.current;
15
+ if (!el) return;
16
+
17
+ const observer = new IntersectionObserver(
18
+ ([entry]) => {
19
+ if (entry.isIntersecting) {
20
+ setVisible(true);
21
+ observer.disconnect();
22
+ }
23
+ },
24
+ { rootMargin }
25
+ );
26
+
27
+ observer.observe(el);
28
+ return () => observer.disconnect();
29
+ }, [rootMargin]);
30
+
31
+ if (visible) return <>{children}</>;
32
+
33
+ return (
34
+ <div
35
+ ref={ref}
36
+ className="flex items-center justify-center text-muted-foreground text-xs rounded-md bg-muted/20 animate-pulse"
37
+ style={{ minHeight }}
38
+ >
39
+ Carregando...
40
+ </div>
41
+ );
42
+ }
@@ -0,0 +1,114 @@
1
+ import { memo } from "react";
2
+ import ReactMarkdown from "react-markdown";
3
+ import remarkGfm from "remark-gfm";
4
+ import rehypeHighlight from "rehype-highlight";
5
+ import type { Components } from "react-markdown";
6
+ import { cn } from "../lib/utils.js";
7
+
8
+ const REMARK_PLUGINS = [remarkGfm];
9
+ const REHYPE_PLUGINS = [rehypeHighlight];
10
+
11
+ const components: Components = {
12
+ h1({ children }) {
13
+ return <h1 className="text-xl font-semibold" style={{ marginTop: "20px", marginBottom: "8px" }}>{children}</h1>;
14
+ },
15
+ h2({ children }) {
16
+ return <h2 className="text-lg font-semibold" style={{ marginTop: "20px", marginBottom: "8px" }}>{children}</h2>;
17
+ },
18
+ h3({ children }) {
19
+ return <h3 className="text-base font-semibold" style={{ marginTop: "20px", marginBottom: "8px" }}>{children}</h3>;
20
+ },
21
+ h4({ children }) {
22
+ return <h4 className="font-semibold" style={{ marginTop: "20px", marginBottom: "8px" }}>{children}</h4>;
23
+ },
24
+ p({ children }) {
25
+ return <p className="mb-4 last:mb-0">{children}</p>;
26
+ },
27
+ ul({ children }) {
28
+ return <ul style={{ paddingLeft: "24px", marginTop: "8px", marginBottom: "8px", listStyleType: "disc" }}>{children}</ul>;
29
+ },
30
+ ol({ children }) {
31
+ return <ol style={{ paddingLeft: "24px", marginTop: "8px", marginBottom: "8px", listStyleType: "decimal" }}>{children}</ol>;
32
+ },
33
+ li({ children }) {
34
+ return <li style={{ marginTop: "4px", marginBottom: "4px" }}>{children}</li>;
35
+ },
36
+ hr() {
37
+ return <hr className="border-border" style={{ marginTop: "16px", marginBottom: "16px" }} />;
38
+ },
39
+ pre({ children }) {
40
+ return (
41
+ <pre className="bg-muted border border-border rounded-md overflow-hidden" style={{ marginTop: "12px", marginBottom: "12px" }}>
42
+ {children}
43
+ </pre>
44
+ );
45
+ },
46
+ code({ className, children }) {
47
+ const isBlock = className?.startsWith("language-");
48
+ if (isBlock) {
49
+ return (
50
+ <code className={cn("block p-4 overflow-x-auto font-mono text-sm", className)}>
51
+ {children}
52
+ </code>
53
+ );
54
+ }
55
+ return (
56
+ <code className="bg-muted border border-border rounded-sm px-1.5 py-0.5 font-mono text-sm">
57
+ {children}
58
+ </code>
59
+ );
60
+ },
61
+ a({ href, children }) {
62
+ return (
63
+ <a href={href} target="_blank" rel="noopener noreferrer" className="text-primary underline underline-offset-2 hover:opacity-80">
64
+ {children}
65
+ </a>
66
+ );
67
+ },
68
+ blockquote({ children }) {
69
+ return (
70
+ <blockquote className="border-l-[3px] border-border py-1 px-3 text-muted-foreground" style={{ marginTop: "12px", marginBottom: "12px" }}>
71
+ {children}
72
+ </blockquote>
73
+ );
74
+ },
75
+ table({ children }) {
76
+ return (
77
+ <div className="overflow-x-auto">
78
+ <table className="w-full border-collapse text-sm">{children}</table>
79
+ </div>
80
+ );
81
+ },
82
+ th({ children }) {
83
+ return (
84
+ <th className="border border-border px-3 py-1.5 text-left font-semibold bg-muted">
85
+ {children}
86
+ </th>
87
+ );
88
+ },
89
+ td({ children }) {
90
+ return (
91
+ <td className="border border-border px-3 py-1.5 text-left">
92
+ {children}
93
+ </td>
94
+ );
95
+ },
96
+ };
97
+
98
+ interface MarkdownProps {
99
+ children: string;
100
+ }
101
+
102
+ export const Markdown = memo(function Markdown({ children }: MarkdownProps) {
103
+ return (
104
+ <div className="text-foreground text-sm" style={{ lineHeight: "1.625em" }}>
105
+ <ReactMarkdown
106
+ remarkPlugins={REMARK_PLUGINS}
107
+ rehypePlugins={REHYPE_PLUGINS}
108
+ components={components}
109
+ >
110
+ {children}
111
+ </ReactMarkdown>
112
+ </div>
113
+ );
114
+ });
@@ -0,0 +1,107 @@
1
+ import { memo, useState, useCallback } from "react";
2
+ import type { Message } from "../types.js";
3
+ import { cn } from "../lib/utils.js";
4
+ import { Markdown } from "./Markdown.js";
5
+ import { StreamingIndicator } from "./StreamingIndicator.js";
6
+ import { PartRenderer } from "../parts/PartRenderer.js";
7
+ import { PartErrorBoundary } from "../parts/PartErrorBoundary.js";
8
+ import type { DisplayRendererMap } from "../display/registry.js";
9
+ import { Copy, Check } from "lucide-react";
10
+
11
+ export interface MessageBubbleProps {
12
+ message: Message;
13
+ isStreaming?: boolean;
14
+ displayRenderers?: DisplayRendererMap;
15
+ className?: string;
16
+ }
17
+
18
+ function extractText(message: Message): string {
19
+ if (message.content) return message.content;
20
+ if (!Array.isArray(message.parts)) return "";
21
+ return message.parts
22
+ .filter((p): p is { type: "text"; text: string } => (p as { type: string }).type === "text")
23
+ .map((p) => p.text)
24
+ .join("\n");
25
+ }
26
+
27
+ function CopyButton({ text }: { text: string }) {
28
+ const [copied, setCopied] = useState(false);
29
+
30
+ const handleCopy = useCallback(async () => {
31
+ await navigator.clipboard.writeText(text);
32
+ setCopied(true);
33
+ setTimeout(() => setCopied(false), 2000);
34
+ }, [text]);
35
+
36
+ return (
37
+ <button
38
+ type="button"
39
+ onClick={handleCopy}
40
+ className={cn(
41
+ "h-7 w-7 flex items-center justify-center rounded-lg transition-all",
42
+ "text-muted-foreground/50 opacity-0 group-hover/bubble:opacity-100",
43
+ "hover:bg-muted/50 hover:text-muted-foreground",
44
+ )}
45
+ aria-label={copied ? "Copiado" : "Copiar mensagem"}
46
+ >
47
+ {copied ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />}
48
+ </button>
49
+ );
50
+ }
51
+
52
+ export const MessageBubble = memo(function MessageBubble({ message, isStreaming, displayRenderers, className }: MessageBubbleProps) {
53
+ const isUser = message.role === "user";
54
+ const hasParts = Array.isArray(message.parts) && message.parts.length > 0;
55
+ const hasText = typeof message.content === "string" && message.content.length > 0;
56
+ const isEmptyAssistant = !isUser && !hasParts && !hasText && !isStreaming;
57
+
58
+ return (
59
+ <div className={cn("group/bubble", isUser ? "flex flex-col items-end" : "flex flex-col items-start")}>
60
+ {/* Bubble */}
61
+ <div
62
+ className={cn(
63
+ "min-w-0 overflow-hidden",
64
+ isUser
65
+ ? "max-w-[80%] rounded-lg rounded-br-sm bg-muted text-foreground px-4 py-2.5"
66
+ : "w-full text-foreground py-1",
67
+ className
68
+ )}
69
+ >
70
+ {hasParts
71
+ ? <div className="flex flex-col gap-3">
72
+ {(message.parts as { type: string }[]).map((part, i) => (
73
+ <PartErrorBoundary key={i} label={`part:${part.type}`}>
74
+ <PartRenderer
75
+ part={part as Parameters<typeof PartRenderer>[0]["part"]}
76
+ isStreaming={isStreaming}
77
+ displayRenderers={displayRenderers}
78
+ />
79
+ </PartErrorBoundary>
80
+ ))}
81
+ </div>
82
+ : hasText
83
+ ? <Markdown>{message.content}</Markdown>
84
+ : isEmptyAssistant
85
+ ? <span className="text-xs italic text-muted-foreground">(resposta vazia)</span>
86
+ : null
87
+ }
88
+ {isStreaming && !isUser && <StreamingIndicator />}
89
+ </div>
90
+
91
+ {/* Action bar — outside bubble, below */}
92
+ {!isStreaming && (
93
+ <div className={cn(
94
+ "flex items-center gap-0.5 mt-0.5",
95
+ isUser ? "justify-end" : "justify-start"
96
+ )}>
97
+ <CopyButton text={extractText(message)} />
98
+ </div>
99
+ )}
100
+ </div>
101
+ );
102
+ }, (prev, next) =>
103
+ prev.message === next.message
104
+ && prev.isStreaming === next.isStreaming
105
+ && prev.displayRenderers === next.displayRenderers
106
+ && prev.className === next.className
107
+ );