|
| 1 | +import { TextAttributes } from '@opentui/core' |
| 2 | +import { useCallback, useState } from 'react' |
| 3 | + |
| 4 | +import { defineToolComponent } from './types' |
| 5 | +import { useTheme } from '../../hooks/use-theme' |
| 6 | +import { safeOpen } from '../../utils/open-url' |
| 7 | +import { Button } from '../button' |
| 8 | + |
| 9 | +import type { ChatTheme } from '../../types/theme-system' |
| 10 | +import type { ToolRenderConfig } from './types' |
| 11 | +import type { RenderUIButtonWidget } from '@codebuff/common/tools/params/tool/render-ui' |
| 12 | + |
| 13 | +type RenderUIButtonVariant = NonNullable<RenderUIButtonWidget['variant']> |
| 14 | + |
| 15 | +const isRenderUIButtonWidget = ( |
| 16 | + widget: unknown, |
| 17 | +): widget is RenderUIButtonWidget => { |
| 18 | + if (widget === null || typeof widget !== 'object') { |
| 19 | + return false |
| 20 | + } |
| 21 | + |
| 22 | + const candidate = widget as Partial<RenderUIButtonWidget> |
| 23 | + return ( |
| 24 | + candidate.type === 'button' && |
| 25 | + typeof candidate.text === 'string' && |
| 26 | + candidate.text.trim().length > 0 && |
| 27 | + typeof candidate.link === 'string' && |
| 28 | + candidate.link.trim().length > 0 && |
| 29 | + (candidate.variant === undefined || |
| 30 | + candidate.variant === 'primary' || |
| 31 | + candidate.variant === 'secondary') |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +const getButtonColors = ( |
| 36 | + theme: ChatTheme, |
| 37 | + variant: RenderUIButtonVariant, |
| 38 | + isHovered: boolean, |
| 39 | + status: 'idle' | 'opened' | 'failed', |
| 40 | +) => { |
| 41 | + if (status === 'failed') { |
| 42 | + return { |
| 43 | + backgroundColor: theme.surface, |
| 44 | + foregroundColor: theme.error, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (status === 'opened') { |
| 49 | + return { |
| 50 | + backgroundColor: theme.surface, |
| 51 | + foregroundColor: theme.success, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (variant === 'secondary') { |
| 56 | + return { |
| 57 | + backgroundColor: isHovered ? theme.surfaceHover : theme.surface, |
| 58 | + foregroundColor: theme.foreground, |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + backgroundColor: theme.primary, |
| 64 | + foregroundColor: theme.name === 'dark' ? '#111827' : '#ffffff', |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const RenderUIButton = ({ widget }: { widget: RenderUIButtonWidget }) => { |
| 69 | + const theme = useTheme() |
| 70 | + const [isHovered, setIsHovered] = useState(false) |
| 71 | + const [status, setStatus] = useState<'idle' | 'opened' | 'failed'>('idle') |
| 72 | + const variant = widget.variant ?? 'primary' |
| 73 | + const { backgroundColor, foregroundColor } = getButtonColors( |
| 74 | + theme, |
| 75 | + variant, |
| 76 | + isHovered, |
| 77 | + status, |
| 78 | + ) |
| 79 | + |
| 80 | + const handleClick = useCallback(async () => { |
| 81 | + const opened = await safeOpen(widget.link) |
| 82 | + setStatus(opened ? 'opened' : 'failed') |
| 83 | + }, [widget.link]) |
| 84 | + |
| 85 | + const statusText = |
| 86 | + status === 'opened' |
| 87 | + ? 'Opened' |
| 88 | + : status === 'failed' |
| 89 | + ? `Could not open: ${widget.link}` |
| 90 | + : '' |
| 91 | + |
| 92 | + return ( |
| 93 | + <box |
| 94 | + style={{ |
| 95 | + flexDirection: 'row', |
| 96 | + alignItems: 'center', |
| 97 | + gap: statusText ? 1 : 0, |
| 98 | + }} |
| 99 | + > |
| 100 | + <Button |
| 101 | + onClick={handleClick} |
| 102 | + onMouseOver={() => setIsHovered(true)} |
| 103 | + onMouseOut={() => setIsHovered(false)} |
| 104 | + style={{ |
| 105 | + backgroundColor, |
| 106 | + paddingLeft: 1, |
| 107 | + paddingRight: 1, |
| 108 | + }} |
| 109 | + > |
| 110 | + <text> |
| 111 | + <span |
| 112 | + fg={foregroundColor} |
| 113 | + attributes={isHovered ? TextAttributes.BOLD : undefined} |
| 114 | + > |
| 115 | + {widget.text} |
| 116 | + </span> |
| 117 | + </text> |
| 118 | + </Button> |
| 119 | + <text style={{ wrapMode: 'word' }}> |
| 120 | + <span fg={status === 'failed' ? theme.error : theme.muted}> |
| 121 | + {statusText} |
| 122 | + </span> |
| 123 | + </text> |
| 124 | + </box> |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +export const RenderUIComponent = defineToolComponent({ |
| 129 | + toolName: 'render_ui', |
| 130 | + |
| 131 | + render(toolBlock): ToolRenderConfig { |
| 132 | + const widget = toolBlock.input?.widget |
| 133 | + |
| 134 | + if (!isRenderUIButtonWidget(widget)) { |
| 135 | + return { content: null } |
| 136 | + } |
| 137 | + |
| 138 | + return { |
| 139 | + content: <RenderUIButton widget={widget} />, |
| 140 | + collapsedPreview: `${widget.text} -> ${widget.link}`, |
| 141 | + } |
| 142 | + }, |
| 143 | +}) |
0 commit comments