Files
ragflow/web/src/components/ui/button.tsx
Zhichang Yu 3fa15c0e2f feat(agent): Go port — canvas engine, 22 components, DSL v2, 13 endpoints (#15952)
Ports the agent canvas subsystem from Python to Go.

## What's included

### Canvas Engine (Phase 0/1)
- State engine, scheduler, variable resolver, Redis checkpoint store,
cancel protocol
- **209 tests** across canvas / component / io packages

### 22 Components (P0–P4)
| Tier | Components |
|---|---|
| P0 T1+T2+T3 | LLM, Agent, ExitLoop, Switch, Categorize, Begin,
Message, Invoke |
| P1 T3 | VariableAggregator, VariableAssigner, StringTransform,
ListOperations, DataOperations |
| P2 T3 | Iteration, IterationItem, Loop, LoopItem |
| P3 T3 | UserFillUp, Fillup |
| P4 T5 | Browser, ExcelProcessor, DocsGenerator |

### DSL v2 Schema (Phase 2.5)
- Typed v2 in-memory model with v1-to-v2 auto-detect converter
- v1 legacy field stripping per plan §2.11.7

### HTTP Endpoints & Bug Fixes (Plans PR1–PR3)
- **DELETE SQL bug fix**: gorm v2 `Where("id = ?", id).Delete(...)`
pattern
- **CreateAgent validation**: title/DSL required, duplicate check, 103
envelope
- **13 new endpoints**: templates, prompts, tags, sessions CRUD,
chat/completions (SSE + non-stream stubs), rerun, test_db_connection,
logs, webhook/logs
- **756 Go unit tests** (745 → 756, +18)
- **17 → 0 Python integration test failures** (test_agents.py +
test_session_management/)

### Tools
21 eino tools: HTTPHelper, search tools, financial/data tools, mandatory
stubs

### Infrastructure
OTel observability, NATS message queue, DeepDoc gRPC client, SSRF
guards, IDOR mitigation
2026-06-12 22:58:28 +08:00

203 lines
6.2 KiB
TypeScript

import { Slot } from '@radix-ui/react-slot';
import { cva, type VariantProps } from 'class-variance-authority';
import * as React from 'react';
import { cn } from '@/lib/utils';
import { LucideLoader2, Plus } from 'lucide-react';
import { Link, LinkProps } from 'react-router';
const buttonVariants = cva(
cn(
'inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm transition-colors outline-0',
'disabled:pointer-events-none disabled:opacity-50 rounded border-0.5 border-transparent',
'[&_svg]:pointer-events-none [&_svg:not([class*="size-"])]:size-4 shrink-0 [&_svg]:shrink-0',
),
{
variants: {
variant: {
// Solid variant series:
// Button has its own background color, may have borders
default:
'bg-text-primary text-bg-base shadow-xs hover:bg-text-primary/90 focus-visible:bg-text-primary/90',
secondary: `
bg-bg-card
hover:text-text-primary hover:bg-border-button
focus-visible:text-text-primary focus-visible:bg-border-button
`,
highlighted: `
bg-text-primary text-bg-base border-b-4 border-b-accent-primary
hover:bg-text-primary/90 focus-visible:bg-text-primary/90
`,
accent: `
bg-accent-primary text-white
hover:bg-accent-primary/90 focus-visible:bg-accent-primary/90
`,
destructive: `
bg-state-error text-white shadow-xs
hover:bg-state-error/90 focus-visible:ring-state-error/20 dark:focus-visible:ring-state-error/40
`,
// Outline variant series
// Button has transparent or greyish background, may have borders
outline: `
text-text-secondary bg-bg-input border-0.5 border-border-button
hover:text-text-primary hover:bg-border-button hover:border-border-default
focus-visible:text-text-primary focus-visible:bg-border-button focus-visible:border-border-button
`, // light: bg=transparent, dark: bg-input
dashed: `
text-text-secondary border-border-button border-dashed
hover:text-text-primary hover:bg-border-button hover:border-border-default
focus-visible:text-text-primary focus-visible:bg-border-button focus-visible:border-border-button
`,
icon: 'bg-transparent text-foreground hover:bg-transparent/80',
transparent: `
text-text-secondary bg-transparent border-0.5 border-border-button
hover:text-text-primary hover:bg-border-button
focus-visible:text-text-primary focus-visible:bg-border-button focus-visible:border-border-button
`,
danger: `
bg-transparent border border-state-error text-state-error
hover:bg-state-error/10 focus-visible:bg-state-error/10
`,
'danger-hover': `
bg-bg-input border-border-button
hover:bg-state-error/10 focus-visible:bg-state-error/10
hover:text-state-error focus-visible:text-state-error
hover:border-state-error focus-visible:border-state-error
`,
// Ghost variant series
// Button has transparent background, without borders
ghost: `
text-text-secondary
hover:bg-border-button focus-visible:bg-border-button
hover:text-text-primary focus-visible:text-text-primary
`,
delete: `
text-text-secondary
hover:bg-state-error-5 hover:text-state-error
focus-visible:text-state-error focus-visible:bg-state-error-5
`,
link: 'text-primary underline-offset-4 hover:underline',
// Static
// Button has no interaction transitions
static:
'text-text-secondary hover:text-text-primary focus-visible:text-text-primary',
},
size: {
auto: '',
xl: 'h-12 rounded-xl px-5 gap-3',
lg: 'h-10 rounded-lg px-4',
default: 'h-8 rounded px-3',
sm: 'h-7 rounded-sm px-2 gap-1',
xs: 'h-6 rounded-xs px-1 gap-0.5',
'icon-xl': 'size-12 rounded-xl',
'icon-lg': 'size-10 rounded-lg',
icon: 'size-8 rounded',
'icon-sm': 'size-7 rounded-sm',
'icon-xs': 'size-6 rounded-xs',
},
},
defaultVariants: {
variant: 'default',
size: 'default',
},
},
);
export type ButtonVariants = VariantProps<typeof buttonVariants>;
export type ButtonProps<IsAnchor extends boolean = false> = {
asChild?: boolean;
asLink?: boolean;
loading?: boolean;
block?: boolean;
disabled?: boolean;
dot?: boolean;
} & ButtonVariants &
(IsAnchor extends true
? LinkProps
: React.ButtonHTMLAttributes<HTMLButtonElement>);
const Button = React.forwardRef(function Button<
IsAnchor extends boolean = false,
>(
{
children,
className,
variant,
size,
dot = false,
asChild = false,
asLink = false,
loading = false,
disabled = false,
block = false,
...props
}: ButtonProps<IsAnchor>,
ref: React.ForwardedRef<
IsAnchor extends true ? HTMLAnchorElement : HTMLButtonElement
>,
) {
const Comp = asChild ? Slot : asLink ? Link : 'button';
return (
<Comp
className={cn(
buttonVariants({ variant, size, className }),
{ 'w-full': block },
{ relative: dot },
)}
// @ts-ignore
ref={ref as React.RefObject<HTMLButtonElement | HTMLAnchorElement>}
disabled={loading || disabled}
{...props}
>
<>
{dot && (
<span className="absolute size-[6px] rounded-full -right-[3px] -top-[3px] bg-state-error animate" />
)}
{loading && <LucideLoader2 className="animate-spin" />}
{children}
</>
</Comp>
);
});
Button.displayName = 'Button';
export const ButtonLoading = Button;
ButtonLoading.displayName = 'ButtonLoading';
export { Button, buttonVariants };
export const BlockButton = React.forwardRef<HTMLButtonElement, ButtonProps>(
function BlockButton({ children, className, ...props }, ref) {
return (
<Button
variant={'outline'}
ref={ref}
className={cn('w-full border-dashed border-input-border', className)}
{...props}
>
<Plus /> {children}
</Button>
);
},
);