@majapisoftwares/ui 6.6.0 → 6.8.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.
- package/AGENTS.md +2 -1
- package/docs/STANDARDS.md +17 -6
- package/package.json +6 -2
- package/scripts/check-standards.mjs +2 -2
- package/src/components/ui/code-block.tsx +246 -0
- package/src/components/ui/marker.tsx +72 -0
- package/src/components/ui/message-scroller.tsx +1 -1
- package/src/components/ui/snackbar.tsx +22 -70
- package/src/components/ui/sonner.tsx +36 -3
- package/src/components/ui/toast.tsx +250 -0
- package/src/hooks/use-highlighted-code.ts +77 -0
- package/src/lib/highlighter.tsx +124 -0
- package/src/lib/i18n.tsx +81 -0
- package/src/lib/utils.ts +16 -0
- package/src/patterns/chat-checkpoint.tsx +49 -0
- package/src/patterns/chat-citation.tsx +57 -0
- package/src/patterns/chat-context.tsx +82 -0
- package/src/patterns/chat-trace.tsx +739 -9
- package/src/patterns/chat.tsx +119 -4
- package/src/patterns/file-tree.tsx +122 -0
- package/src/patterns/model-selector.tsx +153 -0
- package/src/patterns/preferences.tsx +2 -2
- package/src/patterns/prompt-input.tsx +18 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Toast as ToastPrimitive } from "@base-ui/react/toast";
|
|
5
|
+
import { cn } from "cn";
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
CircleCheckIcon,
|
|
9
|
+
InfoIcon,
|
|
10
|
+
Loader2Icon,
|
|
11
|
+
OctagonXIcon,
|
|
12
|
+
TriangleAlertIcon,
|
|
13
|
+
XIcon,
|
|
14
|
+
} from "lucide-react";
|
|
15
|
+
|
|
16
|
+
import { useUiMessages } from "../../lib/i18n";
|
|
17
|
+
import { Button } from "./button";
|
|
18
|
+
|
|
19
|
+
const toast = ToastPrimitive.createToastManager();
|
|
20
|
+
|
|
21
|
+
function ToastProvider({ ...props }: ToastPrimitive.Provider.Props) {
|
|
22
|
+
return <ToastPrimitive.Provider {...props} />;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function ToastPortal({ ...props }: ToastPrimitive.Portal.Props) {
|
|
26
|
+
return <ToastPrimitive.Portal data-slot="toast-portal" {...props} />;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function ToastViewport({ className, ...props }: ToastPrimitive.Viewport.Props) {
|
|
30
|
+
return (
|
|
31
|
+
<ToastPrimitive.Viewport
|
|
32
|
+
data-slot="toast-viewport"
|
|
33
|
+
className={cn(
|
|
34
|
+
"pointer-events-none fixed inset-x-4 bottom-4 z-[100] mx-auto w-auto max-w-sm outline-none sm:right-4 sm:left-auto sm:mx-0 sm:w-full",
|
|
35
|
+
className,
|
|
36
|
+
)}
|
|
37
|
+
{...props}
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function Toast({ className, ...props }: ToastPrimitive.Root.Props) {
|
|
43
|
+
return (
|
|
44
|
+
<ToastPrimitive.Root
|
|
45
|
+
data-slot="toast"
|
|
46
|
+
className={cn(
|
|
47
|
+
"group/toast pointer-events-auto absolute right-0 bottom-0 z-[calc(1000-var(--toast-index))] w-full origin-bottom rounded-2xl border bg-popover text-popover-foreground shadow-lg will-change-transform outline-none select-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
|
|
48
|
+
"[--gap:0.75rem] [--height:var(--toast-frontmost-height,var(--toast-height))] [--offset-y:calc(var(--toast-offset-y)*-1+calc(var(--toast-index)*var(--gap)*-1)+var(--toast-swipe-movement-y))] [--peek:0.75rem] [--scale:calc(max(0,1-(var(--toast-index)*0.1)))] [--shrink:calc(1-var(--scale))]",
|
|
49
|
+
"h-(--height) [transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--toast-swipe-movement-y)-(var(--toast-index)*var(--peek))-(var(--shrink)*var(--height))))_scale(var(--scale))] [transition:transform_500ms_cubic-bezier(0.22,1,0.36,1),opacity_500ms,height_150ms]",
|
|
50
|
+
"after:absolute after:top-full after:left-0 after:h-[calc(var(--gap)+1px)] after:w-full after:content-['']",
|
|
51
|
+
"data-expanded:h-(--toast-height) data-expanded:[transform:translateX(var(--toast-swipe-movement-x))_translateY(var(--offset-y))]",
|
|
52
|
+
"data-limited:opacity-0 data-starting-style:[transform:translateY(150%)]",
|
|
53
|
+
"[&[data-ending-style]:not([data-limited]):not([data-swipe-direction])]:[transform:translateY(150%)]",
|
|
54
|
+
"data-ending-style:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))]",
|
|
55
|
+
"data-ending-style:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))]",
|
|
56
|
+
"data-ending-style:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))]",
|
|
57
|
+
"data-ending-style:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))]",
|
|
58
|
+
"data-expanded:data-ending-style:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))]",
|
|
59
|
+
"data-expanded:data-ending-style:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))]",
|
|
60
|
+
"data-expanded:data-ending-style:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))]",
|
|
61
|
+
"data-expanded:data-ending-style:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))]",
|
|
62
|
+
className,
|
|
63
|
+
)}
|
|
64
|
+
{...props}
|
|
65
|
+
/>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function ToastContent({ className, ...props }: ToastPrimitive.Content.Props) {
|
|
70
|
+
return (
|
|
71
|
+
<ToastPrimitive.Content
|
|
72
|
+
data-slot="toast-content"
|
|
73
|
+
className={cn(
|
|
74
|
+
"flex h-full items-center gap-3 overflow-hidden p-4 transition-opacity duration-250 ease-[cubic-bezier(0.22,1,0.36,1)] data-behind:opacity-0 data-expanded:opacity-100",
|
|
75
|
+
className,
|
|
76
|
+
)}
|
|
77
|
+
{...props}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function ToastTitle({ className, ...props }: ToastPrimitive.Title.Props) {
|
|
83
|
+
return (
|
|
84
|
+
<ToastPrimitive.Title
|
|
85
|
+
data-slot="toast-title"
|
|
86
|
+
className={cn("text-sm font-medium", className)}
|
|
87
|
+
{...props}
|
|
88
|
+
/>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function ToastDescription({
|
|
93
|
+
className,
|
|
94
|
+
...props
|
|
95
|
+
}: ToastPrimitive.Description.Props) {
|
|
96
|
+
return (
|
|
97
|
+
<ToastPrimitive.Description
|
|
98
|
+
data-slot="toast-description"
|
|
99
|
+
className={cn("text-sm text-muted-foreground", className)}
|
|
100
|
+
{...props}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function ToastAction({
|
|
106
|
+
className,
|
|
107
|
+
render = <Button variant="outline" size="sm" />,
|
|
108
|
+
...props
|
|
109
|
+
}: ToastPrimitive.Action.Props) {
|
|
110
|
+
return (
|
|
111
|
+
<ToastPrimitive.Action
|
|
112
|
+
data-slot="toast-action"
|
|
113
|
+
render={render}
|
|
114
|
+
className={cn("shrink-0", className)}
|
|
115
|
+
{...props}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function ToastClose({
|
|
121
|
+
className,
|
|
122
|
+
children,
|
|
123
|
+
render = <Button variant="ghost" size="icon-sm" />,
|
|
124
|
+
...props
|
|
125
|
+
}: ToastPrimitive.Close.Props) {
|
|
126
|
+
const messages = useUiMessages();
|
|
127
|
+
return (
|
|
128
|
+
<ToastPrimitive.Close
|
|
129
|
+
data-slot="toast-close"
|
|
130
|
+
aria-label={messages.close}
|
|
131
|
+
render={render}
|
|
132
|
+
className={cn(
|
|
133
|
+
"relative shrink-0 text-muted-foreground after:absolute after:-inset-2 after:content-[''] hover:text-foreground",
|
|
134
|
+
className,
|
|
135
|
+
)}
|
|
136
|
+
{...props}
|
|
137
|
+
>
|
|
138
|
+
{children ?? <XIcon aria-hidden="true" />}
|
|
139
|
+
</ToastPrimitive.Close>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function ToastIcon({ type }: { type: string | undefined }) {
|
|
144
|
+
let icon: React.ReactNode = null;
|
|
145
|
+
|
|
146
|
+
if (type === "success") {
|
|
147
|
+
icon = <CircleCheckIcon aria-hidden="true" />;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (type === "info") {
|
|
151
|
+
icon = <InfoIcon aria-hidden="true" />;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (type === "warning") {
|
|
155
|
+
icon = <TriangleAlertIcon aria-hidden="true" />;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (type === "error") {
|
|
159
|
+
icon = <OctagonXIcon className="text-destructive" aria-hidden="true" />;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (type === "loading") {
|
|
163
|
+
icon = <Loader2Icon className="animate-spin" aria-hidden="true" />;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (!icon) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<span
|
|
172
|
+
data-slot="toast-icon"
|
|
173
|
+
className="shrink-0 [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4"
|
|
174
|
+
>
|
|
175
|
+
{icon}
|
|
176
|
+
</span>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function ToastList() {
|
|
181
|
+
const { toasts } = ToastPrimitive.useToastManager();
|
|
182
|
+
|
|
183
|
+
return toasts.map((toastItem) => (
|
|
184
|
+
<Toast key={toastItem.id} toast={toastItem}>
|
|
185
|
+
<ToastContent>
|
|
186
|
+
<ToastIcon type={toastItem.type} />
|
|
187
|
+
<div className="flex min-w-0 flex-1 flex-col gap-1">
|
|
188
|
+
<ToastTitle />
|
|
189
|
+
<ToastDescription />
|
|
190
|
+
</div>
|
|
191
|
+
<ToastAction />
|
|
192
|
+
<ToastClose />
|
|
193
|
+
</ToastContent>
|
|
194
|
+
</Toast>
|
|
195
|
+
));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function Toaster({
|
|
199
|
+
children,
|
|
200
|
+
toastManager = toast,
|
|
201
|
+
...props
|
|
202
|
+
}: ToastPrimitive.Provider.Props) {
|
|
203
|
+
return (
|
|
204
|
+
<ToastProvider toastManager={toastManager} {...props}>
|
|
205
|
+
{children}
|
|
206
|
+
<ToastPortal>
|
|
207
|
+
<ToastViewport>
|
|
208
|
+
<ToastList />
|
|
209
|
+
</ToastViewport>
|
|
210
|
+
</ToastPortal>
|
|
211
|
+
</ToastProvider>
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The message of an error or of anything thrown. */
|
|
216
|
+
function errorMessage(error: unknown): string {
|
|
217
|
+
if (error instanceof Error) return error.message;
|
|
218
|
+
return typeof error === "string" ? error : String(error);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Shows a failure (anything thrown) as an error toast. */
|
|
222
|
+
function toastError(error: unknown, title?: string) {
|
|
223
|
+
return toast.add({
|
|
224
|
+
type: "error",
|
|
225
|
+
priority: "high",
|
|
226
|
+
...(title ? { title } : {}),
|
|
227
|
+
description: errorMessage(error),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const createToastManager = ToastPrimitive.createToastManager;
|
|
232
|
+
const useToastManager = ToastPrimitive.useToastManager;
|
|
233
|
+
|
|
234
|
+
export {
|
|
235
|
+
Toaster,
|
|
236
|
+
Toast,
|
|
237
|
+
ToastAction,
|
|
238
|
+
ToastClose,
|
|
239
|
+
ToastContent,
|
|
240
|
+
ToastDescription,
|
|
241
|
+
ToastPortal,
|
|
242
|
+
ToastProvider,
|
|
243
|
+
ToastTitle,
|
|
244
|
+
ToastViewport,
|
|
245
|
+
createToastManager,
|
|
246
|
+
errorMessage,
|
|
247
|
+
toast,
|
|
248
|
+
toastError,
|
|
249
|
+
useToastManager,
|
|
250
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
import { loadHighlighter } from "../lib/highlighter";
|
|
6
|
+
|
|
7
|
+
export function useHighlightedCode(value: string, language: string) {
|
|
8
|
+
// A trailing newline would render as an extra empty line.
|
|
9
|
+
const code = value.endsWith("\n") ? value.slice(0, -1) : value;
|
|
10
|
+
const [loaded, setLoaded] = React.useState<{
|
|
11
|
+
language: string;
|
|
12
|
+
highlighter: Awaited<ReturnType<typeof loadHighlighter>>;
|
|
13
|
+
} | null>(null);
|
|
14
|
+
const highlighter = loaded?.language === language ? loaded.highlighter : null;
|
|
15
|
+
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
let cancelled = false;
|
|
18
|
+
|
|
19
|
+
loadHighlighter(language)
|
|
20
|
+
.then((next) => {
|
|
21
|
+
if (!cancelled) {
|
|
22
|
+
setLoaded({ language, highlighter: next });
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
.catch(() => {});
|
|
26
|
+
|
|
27
|
+
return () => {
|
|
28
|
+
cancelled = true;
|
|
29
|
+
};
|
|
30
|
+
}, [language]);
|
|
31
|
+
|
|
32
|
+
// The complete lines, highlighted. While the code streams, only the new
|
|
33
|
+
// lines are tokenized, starting from the grammar state of the last one.
|
|
34
|
+
const [done, setDone] = React.useState<{
|
|
35
|
+
highlighter: typeof highlighter;
|
|
36
|
+
text: string;
|
|
37
|
+
lines: React.ReactNode[];
|
|
38
|
+
state?: Parameters<NonNullable<typeof highlighter>["highlight"]>[1];
|
|
39
|
+
}>({ highlighter: null, text: "", lines: [] });
|
|
40
|
+
|
|
41
|
+
let next = done;
|
|
42
|
+
|
|
43
|
+
if (next.highlighter !== highlighter || !code.startsWith(next.text)) {
|
|
44
|
+
next = { highlighter, text: "", lines: [] };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const end = code.lastIndexOf("\n") + 1;
|
|
48
|
+
|
|
49
|
+
if (highlighter && end > next.text.length) {
|
|
50
|
+
const result = highlighter.highlight(
|
|
51
|
+
code.slice(next.text.length, end - 1),
|
|
52
|
+
next.state,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
next = {
|
|
56
|
+
highlighter,
|
|
57
|
+
text: code.slice(0, end),
|
|
58
|
+
lines: [...next.lines, ...result.lines],
|
|
59
|
+
state: result.state,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (next !== done) {
|
|
64
|
+
setDone(next);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The last line is still being written, so it is tokenized on every render.
|
|
68
|
+
const tail = code.slice(end);
|
|
69
|
+
|
|
70
|
+
return React.useMemo(() => {
|
|
71
|
+
if (!highlighter) {
|
|
72
|
+
return code.split("\n");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return [...next.lines, highlighter.highlight(tail, next.state).lines[0]];
|
|
76
|
+
}, [code, highlighter, next, tail]);
|
|
77
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import {
|
|
3
|
+
createHighlighterCore,
|
|
4
|
+
type GrammarState,
|
|
5
|
+
type HighlighterCore,
|
|
6
|
+
} from "shiki/core";
|
|
7
|
+
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
|
|
8
|
+
|
|
9
|
+
// Add the languages you need. Each one loads the first time it is used.
|
|
10
|
+
const languages = {
|
|
11
|
+
bash: () => import("shiki/langs/shellscript.mjs"),
|
|
12
|
+
css: () => import("shiki/langs/css.mjs"),
|
|
13
|
+
diff: () => import("shiki/langs/diff.mjs"),
|
|
14
|
+
html: () => import("shiki/langs/html.mjs"),
|
|
15
|
+
javascript: () => import("shiki/langs/javascript.mjs"),
|
|
16
|
+
json: () => import("shiki/langs/json.mjs"),
|
|
17
|
+
jsx: () => import("shiki/langs/jsx.mjs"),
|
|
18
|
+
markdown: () => import("shiki/langs/markdown.mjs"),
|
|
19
|
+
python: () => import("shiki/langs/python.mjs"),
|
|
20
|
+
sql: () => import("shiki/langs/sql.mjs"),
|
|
21
|
+
tsx: () => import("shiki/langs/tsx.mjs"),
|
|
22
|
+
typescript: () => import("shiki/langs/typescript.mjs"),
|
|
23
|
+
yaml: () => import("shiki/langs/yaml.mjs"),
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const aliases: Record<string, keyof typeof languages> = {
|
|
27
|
+
js: "javascript",
|
|
28
|
+
md: "markdown",
|
|
29
|
+
py: "python",
|
|
30
|
+
sh: "bash",
|
|
31
|
+
shell: "bash",
|
|
32
|
+
shellscript: "bash",
|
|
33
|
+
ts: "typescript",
|
|
34
|
+
yml: "yaml",
|
|
35
|
+
zsh: "bash",
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let highlighter: Promise<HighlighterCore> | undefined;
|
|
39
|
+
|
|
40
|
+
function getHighlighter() {
|
|
41
|
+
highlighter ??= createHighlighterCore({
|
|
42
|
+
themes: [
|
|
43
|
+
import("shiki/themes/github-light.mjs"),
|
|
44
|
+
import("shiki/themes/github-dark.mjs"),
|
|
45
|
+
],
|
|
46
|
+
langs: [],
|
|
47
|
+
engine: createJavaScriptRegexEngine({ forgiving: true }),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return highlighter;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getLanguage(language: string) {
|
|
54
|
+
const name = language.toLowerCase();
|
|
55
|
+
|
|
56
|
+
if (name === "ansi") {
|
|
57
|
+
return name;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (name in languages) {
|
|
61
|
+
return name as keyof typeof languages;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return aliases[name] ?? null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Loads a language and returns a highlighter for it, or null for a language
|
|
68
|
+
// that is not listed.
|
|
69
|
+
export async function loadHighlighter(language: string) {
|
|
70
|
+
const lang = getLanguage(language);
|
|
71
|
+
|
|
72
|
+
if (!lang) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const shiki = await getHighlighter();
|
|
77
|
+
|
|
78
|
+
if (lang !== "ansi" && !shiki.getLoadedLanguages().includes(lang)) {
|
|
79
|
+
await shiki.loadLanguage(languages[lang]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
// Returns one React node per line, and the grammar state after the last
|
|
84
|
+
// line. Pass that state back to continue where the code left off.
|
|
85
|
+
highlight(code: string, state?: GrammarState) {
|
|
86
|
+
const tokenize = (text: string) =>
|
|
87
|
+
shiki.codeToTokens(text, {
|
|
88
|
+
lang,
|
|
89
|
+
themes: { light: "github-light", dark: "github-dark" },
|
|
90
|
+
defaultColor: false,
|
|
91
|
+
grammarState: state,
|
|
92
|
+
}).tokens;
|
|
93
|
+
|
|
94
|
+
// ANSI has no grammar state, so each line is tokenized on its own and
|
|
95
|
+
// gets the same colors whether it arrives in one piece or streams.
|
|
96
|
+
const tokens =
|
|
97
|
+
lang === "ansi"
|
|
98
|
+
? code.split("\n").flatMap((line) => tokenize(line))
|
|
99
|
+
: tokenize(code);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
lines: tokens.map((line) =>
|
|
103
|
+
line.map((token, index) => (
|
|
104
|
+
<span
|
|
105
|
+
key={index}
|
|
106
|
+
style={token.htmlStyle as React.CSSProperties}
|
|
107
|
+
className="text-(--shiki-light) dark:text-(--shiki-dark)"
|
|
108
|
+
>
|
|
109
|
+
{token.content}
|
|
110
|
+
</span>
|
|
111
|
+
)),
|
|
112
|
+
),
|
|
113
|
+
state: shiki.getLastGrammarState(tokens),
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Returns one React node per line, or null for a language that is not listed.
|
|
120
|
+
export async function highlightCode(code: string, language: string) {
|
|
121
|
+
const highlighter = await loadHighlighter(language);
|
|
122
|
+
|
|
123
|
+
return highlighter ? highlighter.highlight(code).lines : null;
|
|
124
|
+
}
|
package/src/lib/i18n.tsx
CHANGED
|
@@ -85,6 +85,8 @@ export const uiMessages = defineMessages({
|
|
|
85
85
|
responding: "Responding",
|
|
86
86
|
copy: "Copy",
|
|
87
87
|
copied: "Copied",
|
|
88
|
+
reply: "Reply",
|
|
89
|
+
cancelReply: "Cancel reply",
|
|
88
90
|
goodResponse: "Good response",
|
|
89
91
|
badResponse: "Bad response",
|
|
90
92
|
thinking: "Thinking",
|
|
@@ -93,6 +95,44 @@ export const uiMessages = defineMessages({
|
|
|
93
95
|
approve: "Approve",
|
|
94
96
|
deny: "Deny",
|
|
95
97
|
suggestions: "Suggestions",
|
|
98
|
+
researching: "Researching",
|
|
99
|
+
researched: (steps: number) =>
|
|
100
|
+
`Researched in ${steps} ${steps === 1 ? "step" : "steps"}`,
|
|
101
|
+
planProgress: (done: number, total: number) => `${done} of ${total} done`,
|
|
102
|
+
toDo: (count: number) => `${count} to do`,
|
|
103
|
+
completedCount: (count: number) => `${count} completed`,
|
|
104
|
+
usedSources: (count: number) =>
|
|
105
|
+
`Used ${count} ${count === 1 ? "source" : "sources"}`,
|
|
106
|
+
opensInNewTab: "(opens in a new tab)",
|
|
107
|
+
runningCommand: (command: string) => `Running ${command}`,
|
|
108
|
+
ranCommand: (command: string) => `Ran ${command}`,
|
|
109
|
+
exitedWith: (command: string, code: number) =>
|
|
110
|
+
`${command} exited with code ${code}`,
|
|
111
|
+
testCounts: (total: number, failed: number, skipped: number) =>
|
|
112
|
+
[
|
|
113
|
+
`${total} ${total === 1 ? "test" : "tests"}`,
|
|
114
|
+
failed > 0 && `${failed} failed`,
|
|
115
|
+
skipped > 0 && `${skipped} skipped`,
|
|
116
|
+
]
|
|
117
|
+
.filter(Boolean)
|
|
118
|
+
.join(", "),
|
|
119
|
+
ranTests: (counts: string) => `Ran ${counts}`,
|
|
120
|
+
skippedTest: (name: string) => `Skipped ${name}`,
|
|
121
|
+
failedWith: (error: string) => `Failed: ${error}`,
|
|
122
|
+
frameworkFrames: (count: number) =>
|
|
123
|
+
`${count} framework ${count === 1 ? "frame" : "frames"}`,
|
|
124
|
+
restore: "Restore",
|
|
125
|
+
restoreTo: (label: string) => `Restore to ${label}`,
|
|
126
|
+
contextPercent: (percent: number) => `${percent}% context`,
|
|
127
|
+
context: "Context",
|
|
128
|
+
contextUsed: "Context used",
|
|
129
|
+
inputTokens: "Input",
|
|
130
|
+
outputTokens: "Output",
|
|
131
|
+
reasoningTokens: "Reasoning",
|
|
132
|
+
model: "Model",
|
|
133
|
+
effort: "Effort",
|
|
134
|
+
modelIs: (name: string) => `Model: ${name}`,
|
|
135
|
+
effortIs: (name: string) => `Effort: ${name}`,
|
|
96
136
|
},
|
|
97
137
|
"pt-BR": {
|
|
98
138
|
sidebar: "Barra lateral",
|
|
@@ -135,6 +175,8 @@ export const uiMessages = defineMessages({
|
|
|
135
175
|
responding: "Respondendo",
|
|
136
176
|
copy: "Copiar",
|
|
137
177
|
copied: "Copiado",
|
|
178
|
+
reply: "Responder",
|
|
179
|
+
cancelReply: "Cancelar resposta",
|
|
138
180
|
goodResponse: "Boa resposta",
|
|
139
181
|
badResponse: "Resposta ruim",
|
|
140
182
|
thinking: "Pensando",
|
|
@@ -143,6 +185,45 @@ export const uiMessages = defineMessages({
|
|
|
143
185
|
approve: "Aprovar",
|
|
144
186
|
deny: "Negar",
|
|
145
187
|
suggestions: "Sugestões",
|
|
188
|
+
researching: "Pesquisando",
|
|
189
|
+
researched: (steps: number) =>
|
|
190
|
+
`Pesquisou em ${steps} ${steps === 1 ? "passo" : "passos"}`,
|
|
191
|
+
planProgress: (done: number, total: number) => `${done} de ${total} feitos`,
|
|
192
|
+
toDo: (count: number) => `${count} a fazer`,
|
|
193
|
+
completedCount: (count: number) =>
|
|
194
|
+
`${count} ${count === 1 ? "concluído" : "concluídos"}`,
|
|
195
|
+
usedSources: (count: number) =>
|
|
196
|
+
`Usou ${count} ${count === 1 ? "fonte" : "fontes"}`,
|
|
197
|
+
opensInNewTab: "(abre em uma nova aba)",
|
|
198
|
+
runningCommand: (command: string) => `Rodando ${command}`,
|
|
199
|
+
ranCommand: (command: string) => `Rodou ${command}`,
|
|
200
|
+
exitedWith: (command: string, code: number) =>
|
|
201
|
+
`${command} saiu com código ${code}`,
|
|
202
|
+
testCounts: (total: number, failed: number, skipped: number) =>
|
|
203
|
+
[
|
|
204
|
+
`${total} ${total === 1 ? "teste" : "testes"}`,
|
|
205
|
+
failed > 0 && `${failed} ${failed === 1 ? "falhou" : "falharam"}`,
|
|
206
|
+
skipped > 0 && `${skipped} ${skipped === 1 ? "pulado" : "pulados"}`,
|
|
207
|
+
]
|
|
208
|
+
.filter(Boolean)
|
|
209
|
+
.join(", "),
|
|
210
|
+
ranTests: (counts: string) => `Rodou ${counts}`,
|
|
211
|
+
skippedTest: (name: string) => `Pulou ${name}`,
|
|
212
|
+
failedWith: (error: string) => `Falhou: ${error}`,
|
|
213
|
+
frameworkFrames: (count: number) =>
|
|
214
|
+
`${count} ${count === 1 ? "quadro" : "quadros"} do framework`,
|
|
215
|
+
restore: "Restaurar",
|
|
216
|
+
restoreTo: (label: string) => `Restaurar até ${label}`,
|
|
217
|
+
contextPercent: (percent: number) => `${percent}% do contexto`,
|
|
218
|
+
context: "Contexto",
|
|
219
|
+
contextUsed: "Contexto usado",
|
|
220
|
+
inputTokens: "Entrada",
|
|
221
|
+
outputTokens: "Saída",
|
|
222
|
+
reasoningTokens: "Raciocínio",
|
|
223
|
+
model: "Modelo",
|
|
224
|
+
effort: "Esforço",
|
|
225
|
+
modelIs: (name: string) => `Modelo: ${name}`,
|
|
226
|
+
effortIs: (name: string) => `Esforço: ${name}`,
|
|
146
227
|
},
|
|
147
228
|
});
|
|
148
229
|
|
package/src/lib/utils.ts
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
1
|
export { cn } from "cn";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Markdown as one line of plain text, for a `ChatQuote` of an agent's reply:
|
|
5
|
+
* no code blocks, emphasis, links, list markers or table pipes.
|
|
6
|
+
*/
|
|
7
|
+
export function quoteText(markdown: string) {
|
|
8
|
+
return markdown
|
|
9
|
+
.replace(/```[\s\S]*?(```|$)/g, " ")
|
|
10
|
+
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
11
|
+
.replace(/^\s{0,3}(#{1,6}|>|[-*+]|\d+[.)])\s+/gm, "")
|
|
12
|
+
.replace(/^[\s|:-]*-{3,}[\s|:-]*$/gm, " ")
|
|
13
|
+
.replace(/\|/g, " ")
|
|
14
|
+
.replace(/\*\*|__|~~|`|\*/g, "")
|
|
15
|
+
.replace(/\s+/g, " ")
|
|
16
|
+
.trim();
|
|
17
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { useUiMessages } from "../lib/i18n";
|
|
5
|
+
import { Button } from "../components/ui/button";
|
|
6
|
+
import { Marker, MarkerContent } from "../components/ui/marker";
|
|
7
|
+
|
|
8
|
+
export interface CheckpointProps {
|
|
9
|
+
/** What the turn above it changed, e.g. "Button renamed". */
|
|
10
|
+
label: ReactNode;
|
|
11
|
+
/** Keeps that turn and drops every later message. Leave it out on the last turn. */
|
|
12
|
+
onRestore?: () => void;
|
|
13
|
+
/** Accessible name of the restore button, e.g. "Restore to Button renamed". */
|
|
14
|
+
restoreLabel?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A point the conversation can go back to (shadcn AI Elements Checkpoint): a
|
|
19
|
+
* separator after a turn, with Restore.
|
|
20
|
+
*/
|
|
21
|
+
export function Checkpoint({
|
|
22
|
+
label,
|
|
23
|
+
onRestore,
|
|
24
|
+
restoreLabel,
|
|
25
|
+
}: CheckpointProps) {
|
|
26
|
+
const messages = useUiMessages();
|
|
27
|
+
return (
|
|
28
|
+
<Marker variant="separator">
|
|
29
|
+
<MarkerContent className="inline-flex items-center gap-2">
|
|
30
|
+
{label}
|
|
31
|
+
{onRestore ? (
|
|
32
|
+
<Button
|
|
33
|
+
size="xs"
|
|
34
|
+
variant="outline"
|
|
35
|
+
aria-label={
|
|
36
|
+
restoreLabel ??
|
|
37
|
+
(typeof label === "string"
|
|
38
|
+
? messages.restoreTo(label)
|
|
39
|
+
: undefined)
|
|
40
|
+
}
|
|
41
|
+
onClick={onRestore}
|
|
42
|
+
>
|
|
43
|
+
{messages.restore}
|
|
44
|
+
</Button>
|
|
45
|
+
) : null}
|
|
46
|
+
</MarkerContent>
|
|
47
|
+
</Marker>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useUiMessages } from "../lib/i18n";
|
|
4
|
+
import { Button } from "../components/ui/button";
|
|
5
|
+
import {
|
|
6
|
+
HoverCard,
|
|
7
|
+
HoverCardContent,
|
|
8
|
+
HoverCardTrigger,
|
|
9
|
+
} from "../components/ui/hover-card";
|
|
10
|
+
import { hostnameOf } from "./chat-trace";
|
|
11
|
+
|
|
12
|
+
export interface InlineCitationProps {
|
|
13
|
+
url: string;
|
|
14
|
+
/** Falls back to the URL's hostname. */
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A source inside a sentence (shadcn AI Elements Inline Citation): its
|
|
21
|
+
* hostname, which shows the title, description and link on hover. For the
|
|
22
|
+
* list of sources after an answer, use Sources.
|
|
23
|
+
*/
|
|
24
|
+
export function InlineCitation({
|
|
25
|
+
url,
|
|
26
|
+
title,
|
|
27
|
+
description,
|
|
28
|
+
}: InlineCitationProps) {
|
|
29
|
+
const messages = useUiMessages();
|
|
30
|
+
const host = hostnameOf(url);
|
|
31
|
+
return (
|
|
32
|
+
<HoverCard>
|
|
33
|
+
<HoverCardTrigger
|
|
34
|
+
delay={10}
|
|
35
|
+
closeDelay={100}
|
|
36
|
+
render={<Button variant="secondary" size="xs" />}
|
|
37
|
+
>
|
|
38
|
+
{host}
|
|
39
|
+
</HoverCardTrigger>
|
|
40
|
+
<HoverCardContent className="flex w-64 flex-col gap-1">
|
|
41
|
+
<div className="font-medium">{title ?? host}</div>
|
|
42
|
+
{description ? (
|
|
43
|
+
<p className="text-muted-foreground">{description}</p>
|
|
44
|
+
) : null}
|
|
45
|
+
<a
|
|
46
|
+
href={url}
|
|
47
|
+
target="_blank"
|
|
48
|
+
rel="noreferrer"
|
|
49
|
+
className="text-xs text-muted-foreground underline-offset-4 hover:underline"
|
|
50
|
+
>
|
|
51
|
+
{host}
|
|
52
|
+
<span className="sr-only"> {messages.opensInNewTab}</span>
|
|
53
|
+
</a>
|
|
54
|
+
</HoverCardContent>
|
|
55
|
+
</HoverCard>
|
|
56
|
+
);
|
|
57
|
+
}
|