@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,408 @@
1
+ "use client";
2
+
3
+ import { Button } from "@/components/ui/button";
4
+ import {
5
+ HoverCard,
6
+ HoverCardContent,
7
+ HoverCardTrigger,
8
+ } from "@/components/ui/hover-card";
9
+ import { Progress } from "@/components/ui/progress";
10
+ import { cn } from "@/lib/utils";
11
+ import type { LanguageModelUsage } from "ai";
12
+ import { type ComponentProps, createContext, useContext } from "react";
13
+ import { getUsage } from "tokenlens";
14
+
15
+ const PERCENT_MAX = 100;
16
+ const ICON_RADIUS = 10;
17
+ const ICON_VIEWBOX = 24;
18
+ const ICON_CENTER = 12;
19
+ const ICON_STROKE_WIDTH = 2;
20
+
21
+ type ModelId = string;
22
+
23
+ interface ContextSchema {
24
+ usedTokens: number;
25
+ maxTokens: number;
26
+ usage?: LanguageModelUsage;
27
+ modelId?: ModelId;
28
+ }
29
+
30
+ const ContextContext = createContext<ContextSchema | null>(null);
31
+
32
+ const useContextValue = () => {
33
+ const context = useContext(ContextContext);
34
+
35
+ if (!context) {
36
+ throw new Error("Context components must be used within Context");
37
+ }
38
+
39
+ return context;
40
+ };
41
+
42
+ export type ContextProps = ComponentProps<typeof HoverCard> & ContextSchema;
43
+
44
+ export const Context = ({
45
+ usedTokens,
46
+ maxTokens,
47
+ usage,
48
+ modelId,
49
+ ...props
50
+ }: ContextProps) => (
51
+ <ContextContext.Provider
52
+ value={{
53
+ usedTokens,
54
+ maxTokens,
55
+ usage,
56
+ modelId,
57
+ }}
58
+ >
59
+ <HoverCard closeDelay={0} openDelay={0} {...props} />
60
+ </ContextContext.Provider>
61
+ );
62
+
63
+ const ContextIcon = () => {
64
+ const { usedTokens, maxTokens } = useContextValue();
65
+ const circumference = 2 * Math.PI * ICON_RADIUS;
66
+ const usedPercent = usedTokens / maxTokens;
67
+ const dashOffset = circumference * (1 - usedPercent);
68
+
69
+ return (
70
+ <svg
71
+ aria-label="Model context usage"
72
+ height="20"
73
+ role="img"
74
+ style={{ color: "currentcolor" }}
75
+ viewBox={`0 0 ${ICON_VIEWBOX} ${ICON_VIEWBOX}`}
76
+ width="20"
77
+ >
78
+ <circle
79
+ cx={ICON_CENTER}
80
+ cy={ICON_CENTER}
81
+ fill="none"
82
+ opacity="0.25"
83
+ r={ICON_RADIUS}
84
+ stroke="currentColor"
85
+ strokeWidth={ICON_STROKE_WIDTH}
86
+ />
87
+ <circle
88
+ cx={ICON_CENTER}
89
+ cy={ICON_CENTER}
90
+ fill="none"
91
+ opacity="0.7"
92
+ r={ICON_RADIUS}
93
+ stroke="currentColor"
94
+ strokeDasharray={`${circumference} ${circumference}`}
95
+ strokeDashoffset={dashOffset}
96
+ strokeLinecap="round"
97
+ strokeWidth={ICON_STROKE_WIDTH}
98
+ style={{ transformOrigin: "center", transform: "rotate(-90deg)" }}
99
+ />
100
+ </svg>
101
+ );
102
+ };
103
+
104
+ export type ContextTriggerProps = ComponentProps<typeof Button>;
105
+
106
+ export const ContextTrigger = ({ children, ...props }: ContextTriggerProps) => {
107
+ const { usedTokens, maxTokens } = useContextValue();
108
+ const usedPercent = usedTokens / maxTokens;
109
+ const renderedPercent = new Intl.NumberFormat("en-US", {
110
+ style: "percent",
111
+ maximumFractionDigits: 1,
112
+ }).format(usedPercent);
113
+
114
+ return (
115
+ <HoverCardTrigger asChild>
116
+ {children ?? (
117
+ <Button type="button" variant="ghost" {...props}>
118
+ <span className="font-medium text-muted-foreground">
119
+ {renderedPercent}
120
+ </span>
121
+ <ContextIcon />
122
+ </Button>
123
+ )}
124
+ </HoverCardTrigger>
125
+ );
126
+ };
127
+
128
+ export type ContextContentProps = ComponentProps<typeof HoverCardContent>;
129
+
130
+ export const ContextContent = ({
131
+ className,
132
+ ...props
133
+ }: ContextContentProps) => (
134
+ <HoverCardContent
135
+ className={cn("min-w-60 divide-y overflow-hidden p-0", className)}
136
+ {...props}
137
+ />
138
+ );
139
+
140
+ export type ContextContentHeaderProps = ComponentProps<"div">;
141
+
142
+ export const ContextContentHeader = ({
143
+ children,
144
+ className,
145
+ ...props
146
+ }: ContextContentHeaderProps) => {
147
+ const { usedTokens, maxTokens } = useContextValue();
148
+ const usedPercent = usedTokens / maxTokens;
149
+ const displayPct = new Intl.NumberFormat("en-US", {
150
+ style: "percent",
151
+ maximumFractionDigits: 1,
152
+ }).format(usedPercent);
153
+ const used = new Intl.NumberFormat("en-US", {
154
+ notation: "compact",
155
+ }).format(usedTokens);
156
+ const total = new Intl.NumberFormat("en-US", {
157
+ notation: "compact",
158
+ }).format(maxTokens);
159
+
160
+ return (
161
+ <div className={cn("w-full space-y-2 p-3", className)} {...props}>
162
+ {children ?? (
163
+ <>
164
+ <div className="flex items-center justify-between gap-3 text-xs">
165
+ <p>{displayPct}</p>
166
+ <p className="font-mono text-muted-foreground">
167
+ {used} / {total}
168
+ </p>
169
+ </div>
170
+ <div className="space-y-2">
171
+ <Progress className="bg-muted" value={usedPercent * PERCENT_MAX} />
172
+ </div>
173
+ </>
174
+ )}
175
+ </div>
176
+ );
177
+ };
178
+
179
+ export type ContextContentBodyProps = ComponentProps<"div">;
180
+
181
+ export const ContextContentBody = ({
182
+ children,
183
+ className,
184
+ ...props
185
+ }: ContextContentBodyProps) => (
186
+ <div className={cn("w-full p-3", className)} {...props}>
187
+ {children}
188
+ </div>
189
+ );
190
+
191
+ export type ContextContentFooterProps = ComponentProps<"div">;
192
+
193
+ export const ContextContentFooter = ({
194
+ children,
195
+ className,
196
+ ...props
197
+ }: ContextContentFooterProps) => {
198
+ const { modelId, usage } = useContextValue();
199
+ const costUSD = modelId
200
+ ? getUsage({
201
+ modelId,
202
+ usage: {
203
+ input: usage?.inputTokens ?? 0,
204
+ output: usage?.outputTokens ?? 0,
205
+ },
206
+ }).costUSD?.totalUSD
207
+ : undefined;
208
+ const totalCost = new Intl.NumberFormat("en-US", {
209
+ style: "currency",
210
+ currency: "USD",
211
+ }).format(costUSD ?? 0);
212
+
213
+ return (
214
+ <div
215
+ className={cn(
216
+ "flex w-full items-center justify-between gap-3 bg-secondary p-3 text-xs",
217
+ className
218
+ )}
219
+ {...props}
220
+ >
221
+ {children ?? (
222
+ <>
223
+ <span className="text-muted-foreground">Total cost</span>
224
+ <span>{totalCost}</span>
225
+ </>
226
+ )}
227
+ </div>
228
+ );
229
+ };
230
+
231
+ export type ContextInputUsageProps = ComponentProps<"div">;
232
+
233
+ export const ContextInputUsage = ({
234
+ className,
235
+ children,
236
+ ...props
237
+ }: ContextInputUsageProps) => {
238
+ const { usage, modelId } = useContextValue();
239
+ const inputTokens = usage?.inputTokens ?? 0;
240
+
241
+ if (children) {
242
+ return children;
243
+ }
244
+
245
+ if (!inputTokens) {
246
+ return null;
247
+ }
248
+
249
+ const inputCost = modelId
250
+ ? getUsage({
251
+ modelId,
252
+ usage: { input: inputTokens, output: 0 },
253
+ }).costUSD?.totalUSD
254
+ : undefined;
255
+ const inputCostText = new Intl.NumberFormat("en-US", {
256
+ style: "currency",
257
+ currency: "USD",
258
+ }).format(inputCost ?? 0);
259
+
260
+ return (
261
+ <div
262
+ className={cn("flex items-center justify-between text-xs", className)}
263
+ {...props}
264
+ >
265
+ <span className="text-muted-foreground">Input</span>
266
+ <TokensWithCost costText={inputCostText} tokens={inputTokens} />
267
+ </div>
268
+ );
269
+ };
270
+
271
+ export type ContextOutputUsageProps = ComponentProps<"div">;
272
+
273
+ export const ContextOutputUsage = ({
274
+ className,
275
+ children,
276
+ ...props
277
+ }: ContextOutputUsageProps) => {
278
+ const { usage, modelId } = useContextValue();
279
+ const outputTokens = usage?.outputTokens ?? 0;
280
+
281
+ if (children) {
282
+ return children;
283
+ }
284
+
285
+ if (!outputTokens) {
286
+ return null;
287
+ }
288
+
289
+ const outputCost = modelId
290
+ ? getUsage({
291
+ modelId,
292
+ usage: { input: 0, output: outputTokens },
293
+ }).costUSD?.totalUSD
294
+ : undefined;
295
+ const outputCostText = new Intl.NumberFormat("en-US", {
296
+ style: "currency",
297
+ currency: "USD",
298
+ }).format(outputCost ?? 0);
299
+
300
+ return (
301
+ <div
302
+ className={cn("flex items-center justify-between text-xs", className)}
303
+ {...props}
304
+ >
305
+ <span className="text-muted-foreground">Output</span>
306
+ <TokensWithCost costText={outputCostText} tokens={outputTokens} />
307
+ </div>
308
+ );
309
+ };
310
+
311
+ export type ContextReasoningUsageProps = ComponentProps<"div">;
312
+
313
+ export const ContextReasoningUsage = ({
314
+ className,
315
+ children,
316
+ ...props
317
+ }: ContextReasoningUsageProps) => {
318
+ const { usage, modelId } = useContextValue();
319
+ const reasoningTokens = usage?.reasoningTokens ?? 0;
320
+
321
+ if (children) {
322
+ return children;
323
+ }
324
+
325
+ if (!reasoningTokens) {
326
+ return null;
327
+ }
328
+
329
+ const reasoningCost = modelId
330
+ ? getUsage({
331
+ modelId,
332
+ usage: { reasoningTokens },
333
+ }).costUSD?.totalUSD
334
+ : undefined;
335
+ const reasoningCostText = new Intl.NumberFormat("en-US", {
336
+ style: "currency",
337
+ currency: "USD",
338
+ }).format(reasoningCost ?? 0);
339
+
340
+ return (
341
+ <div
342
+ className={cn("flex items-center justify-between text-xs", className)}
343
+ {...props}
344
+ >
345
+ <span className="text-muted-foreground">Reasoning</span>
346
+ <TokensWithCost costText={reasoningCostText} tokens={reasoningTokens} />
347
+ </div>
348
+ );
349
+ };
350
+
351
+ export type ContextCacheUsageProps = ComponentProps<"div">;
352
+
353
+ export const ContextCacheUsage = ({
354
+ className,
355
+ children,
356
+ ...props
357
+ }: ContextCacheUsageProps) => {
358
+ const { usage, modelId } = useContextValue();
359
+ const cacheTokens = usage?.cachedInputTokens ?? 0;
360
+
361
+ if (children) {
362
+ return children;
363
+ }
364
+
365
+ if (!cacheTokens) {
366
+ return null;
367
+ }
368
+
369
+ const cacheCost = modelId
370
+ ? getUsage({
371
+ modelId,
372
+ usage: { cacheReads: cacheTokens, input: 0, output: 0 },
373
+ }).costUSD?.totalUSD
374
+ : undefined;
375
+ const cacheCostText = new Intl.NumberFormat("en-US", {
376
+ style: "currency",
377
+ currency: "USD",
378
+ }).format(cacheCost ?? 0);
379
+
380
+ return (
381
+ <div
382
+ className={cn("flex items-center justify-between text-xs", className)}
383
+ {...props}
384
+ >
385
+ <span className="text-muted-foreground">Cache</span>
386
+ <TokensWithCost costText={cacheCostText} tokens={cacheTokens} />
387
+ </div>
388
+ );
389
+ };
390
+
391
+ const TokensWithCost = ({
392
+ tokens,
393
+ costText,
394
+ }: {
395
+ tokens?: number;
396
+ costText?: string;
397
+ }) => (
398
+ <span>
399
+ {tokens === undefined
400
+ ? "—"
401
+ : new Intl.NumberFormat("en-US", {
402
+ notation: "compact",
403
+ }).format(tokens)}
404
+ {costText ? (
405
+ <span className="ml-2 text-muted-foreground">• {costText}</span>
406
+ ) : null}
407
+ </span>
408
+ );
@@ -0,0 +1,18 @@
1
+ "use client";
2
+
3
+ import { cn } from "@/lib/utils";
4
+ import { Controls as ControlsPrimitive } from "@xyflow/react";
5
+ import type { ComponentProps } from "react";
6
+
7
+ export type ControlsProps = ComponentProps<typeof ControlsPrimitive>;
8
+
9
+ export const Controls = ({ className, ...props }: ControlsProps) => (
10
+ <ControlsPrimitive
11
+ className={cn(
12
+ "gap-px overflow-hidden rounded-md border bg-card p-1 shadow-none!",
13
+ "[&>button]:rounded-md [&>button]:border-none! [&>button]:bg-transparent! [&>button]:hover:bg-secondary!",
14
+ className
15
+ )}
16
+ {...props}
17
+ />
18
+ );
@@ -0,0 +1,100 @@
1
+ "use client";
2
+
3
+ import { Button } from "@/components/ui/button";
4
+ import { cn } from "@/lib/utils";
5
+ import { ArrowDownIcon } from "lucide-react";
6
+ import type { ComponentProps } from "react";
7
+ import { useCallback } from "react";
8
+ import { StickToBottom, useStickToBottomContext } from "use-stick-to-bottom";
9
+
10
+ export type ConversationProps = ComponentProps<typeof StickToBottom>;
11
+
12
+ export const Conversation = ({ className, ...props }: ConversationProps) => (
13
+ <StickToBottom
14
+ className={cn("relative flex-1 overflow-y-hidden", className)}
15
+ initial="smooth"
16
+ resize="smooth"
17
+ role="log"
18
+ {...props}
19
+ />
20
+ );
21
+
22
+ export type ConversationContentProps = ComponentProps<
23
+ typeof StickToBottom.Content
24
+ >;
25
+
26
+ export const ConversationContent = ({
27
+ className,
28
+ ...props
29
+ }: ConversationContentProps) => (
30
+ <StickToBottom.Content
31
+ className={cn("flex flex-col gap-8 p-4", className)}
32
+ {...props}
33
+ />
34
+ );
35
+
36
+ export type ConversationEmptyStateProps = ComponentProps<"div"> & {
37
+ title?: string;
38
+ description?: string;
39
+ icon?: React.ReactNode;
40
+ };
41
+
42
+ export const ConversationEmptyState = ({
43
+ className,
44
+ title = "No messages yet",
45
+ description = "Start a conversation to see messages here",
46
+ icon,
47
+ children,
48
+ ...props
49
+ }: ConversationEmptyStateProps) => (
50
+ <div
51
+ className={cn(
52
+ "flex size-full flex-col items-center justify-center gap-3 p-8 text-center",
53
+ className
54
+ )}
55
+ {...props}
56
+ >
57
+ {children ?? (
58
+ <>
59
+ {icon && <div className="text-muted-foreground">{icon}</div>}
60
+ <div className="space-y-1">
61
+ <h3 className="font-medium text-sm">{title}</h3>
62
+ {description && (
63
+ <p className="text-muted-foreground text-sm">{description}</p>
64
+ )}
65
+ </div>
66
+ </>
67
+ )}
68
+ </div>
69
+ );
70
+
71
+ export type ConversationScrollButtonProps = ComponentProps<typeof Button>;
72
+
73
+ export const ConversationScrollButton = ({
74
+ className,
75
+ ...props
76
+ }: ConversationScrollButtonProps) => {
77
+ const { isAtBottom, scrollToBottom } = useStickToBottomContext();
78
+
79
+ const handleScrollToBottom = useCallback(() => {
80
+ scrollToBottom();
81
+ }, [scrollToBottom]);
82
+
83
+ return (
84
+ !isAtBottom && (
85
+ <Button
86
+ className={cn(
87
+ "absolute bottom-4 left-[50%] translate-x-[-50%] rounded-full dark:bg-background dark:hover:bg-muted",
88
+ className
89
+ )}
90
+ onClick={handleScrollToBottom}
91
+ size="icon"
92
+ type="button"
93
+ variant="outline"
94
+ {...props}
95
+ >
96
+ <ArrowDownIcon className="size-4" />
97
+ </Button>
98
+ )
99
+ );
100
+ };
@@ -0,0 +1,140 @@
1
+ import {
2
+ BaseEdge,
3
+ type EdgeProps,
4
+ getBezierPath,
5
+ getSimpleBezierPath,
6
+ type InternalNode,
7
+ type Node,
8
+ Position,
9
+ useInternalNode,
10
+ } from "@xyflow/react";
11
+
12
+ const Temporary = ({
13
+ id,
14
+ sourceX,
15
+ sourceY,
16
+ targetX,
17
+ targetY,
18
+ sourcePosition,
19
+ targetPosition,
20
+ }: EdgeProps) => {
21
+ const [edgePath] = getSimpleBezierPath({
22
+ sourceX,
23
+ sourceY,
24
+ sourcePosition,
25
+ targetX,
26
+ targetY,
27
+ targetPosition,
28
+ });
29
+
30
+ return (
31
+ <BaseEdge
32
+ className="stroke-1 stroke-ring"
33
+ id={id}
34
+ path={edgePath}
35
+ style={{
36
+ strokeDasharray: "5, 5",
37
+ }}
38
+ />
39
+ );
40
+ };
41
+
42
+ const getHandleCoordsByPosition = (
43
+ node: InternalNode<Node>,
44
+ handlePosition: Position
45
+ ) => {
46
+ // Choose the handle type based on position - Left is for target, Right is for source
47
+ const handleType = handlePosition === Position.Left ? "target" : "source";
48
+
49
+ const handle = node.internals.handleBounds?.[handleType]?.find(
50
+ (h) => h.position === handlePosition
51
+ );
52
+
53
+ if (!handle) {
54
+ return [0, 0] as const;
55
+ }
56
+
57
+ let offsetX = handle.width / 2;
58
+ let offsetY = handle.height / 2;
59
+
60
+ // this is a tiny detail to make the markerEnd of an edge visible.
61
+ // The handle position that gets calculated has the origin top-left, so depending which side we are using, we add a little offset
62
+ // when the handlePosition is Position.Right for example, we need to add an offset as big as the handle itself in order to get the correct position
63
+ switch (handlePosition) {
64
+ case Position.Left:
65
+ offsetX = 0;
66
+ break;
67
+ case Position.Right:
68
+ offsetX = handle.width;
69
+ break;
70
+ case Position.Top:
71
+ offsetY = 0;
72
+ break;
73
+ case Position.Bottom:
74
+ offsetY = handle.height;
75
+ break;
76
+ default:
77
+ throw new Error(`Invalid handle position: ${handlePosition}`);
78
+ }
79
+
80
+ const x = node.internals.positionAbsolute.x + handle.x + offsetX;
81
+ const y = node.internals.positionAbsolute.y + handle.y + offsetY;
82
+
83
+ return [x, y] as const;
84
+ };
85
+
86
+ const getEdgeParams = (
87
+ source: InternalNode<Node>,
88
+ target: InternalNode<Node>
89
+ ) => {
90
+ const sourcePos = Position.Right;
91
+ const [sx, sy] = getHandleCoordsByPosition(source, sourcePos);
92
+ const targetPos = Position.Left;
93
+ const [tx, ty] = getHandleCoordsByPosition(target, targetPos);
94
+
95
+ return {
96
+ sx,
97
+ sy,
98
+ tx,
99
+ ty,
100
+ sourcePos,
101
+ targetPos,
102
+ };
103
+ };
104
+
105
+ const Animated = ({ id, source, target, markerEnd, style }: EdgeProps) => {
106
+ const sourceNode = useInternalNode(source);
107
+ const targetNode = useInternalNode(target);
108
+
109
+ if (!(sourceNode && targetNode)) {
110
+ return null;
111
+ }
112
+
113
+ const { sx, sy, tx, ty, sourcePos, targetPos } = getEdgeParams(
114
+ sourceNode,
115
+ targetNode
116
+ );
117
+
118
+ const [edgePath] = getBezierPath({
119
+ sourceX: sx,
120
+ sourceY: sy,
121
+ sourcePosition: sourcePos,
122
+ targetX: tx,
123
+ targetY: ty,
124
+ targetPosition: targetPos,
125
+ });
126
+
127
+ return (
128
+ <>
129
+ <BaseEdge id={id} markerEnd={markerEnd} path={edgePath} style={style} />
130
+ <circle fill="var(--primary)" r="4">
131
+ <animateMotion dur="2s" path={edgePath} repeatCount="indefinite" />
132
+ </circle>
133
+ </>
134
+ );
135
+ };
136
+
137
+ export const Edge = {
138
+ Temporary,
139
+ Animated,
140
+ };