|
| 1 | +import React, { useCallback, useState } from 'react' |
| 2 | +import { TextAttributes } from '@opentui/core' |
| 3 | + |
| 4 | +import { defineToolComponent } from './types' |
| 5 | +import { useTheme } from '../../hooks/use-theme' |
| 6 | +import { useChatStore } from '../../state/chat-store' |
| 7 | +import { useTerminalDimensions } from '../../hooks/use-terminal-dimensions' |
| 8 | + |
| 9 | +import type { ToolRenderConfig } from './types' |
| 10 | +import type { SuggestedFollowup } from '../../state/chat-store' |
| 11 | + |
| 12 | +interface FollowupLineProps { |
| 13 | + followup: SuggestedFollowup |
| 14 | + index: number |
| 15 | + isClicked: boolean |
| 16 | + onSendFollowup: (prompt: string, index: number) => void |
| 17 | +} |
| 18 | + |
| 19 | +const FollowupLine = ({ |
| 20 | + followup, |
| 21 | + index, |
| 22 | + isClicked, |
| 23 | + onSendFollowup, |
| 24 | +}: FollowupLineProps) => { |
| 25 | + const theme = useTheme() |
| 26 | + const { terminalWidth } = useTerminalDimensions() |
| 27 | + const [isHovered, setIsHovered] = useState(false) |
| 28 | + |
| 29 | + const handleClick = useCallback(() => { |
| 30 | + if (isClicked) return |
| 31 | + onSendFollowup(followup.prompt, index) |
| 32 | + }, [followup.prompt, index, onSendFollowup, isClicked]) |
| 33 | + |
| 34 | + const handleMouseOver = useCallback(() => setIsHovered(true), []) |
| 35 | + const handleMouseOut = useCallback(() => setIsHovered(false), []) |
| 36 | + |
| 37 | + const hasLabel = Boolean(followup.label) |
| 38 | + // "→ " = 2 chars (icon + space), " · " separator = 3 chars, "…" = 1 char |
| 39 | + const iconWidth = 2 |
| 40 | + const separatorWidth = hasLabel ? 3 : 0 |
| 41 | + const ellipsisWidth = 1 |
| 42 | + const maxWidth = terminalWidth - 6 // Extra margin for safety |
| 43 | + |
| 44 | + // Build the display text with label and prompt |
| 45 | + let labelText = followup.label || '' |
| 46 | + let promptText = followup.prompt |
| 47 | + |
| 48 | + // Calculate available space |
| 49 | + const availableForContent = maxWidth - iconWidth |
| 50 | + |
| 51 | + if (hasLabel) { |
| 52 | + // Show: label · prompt (truncated) |
| 53 | + const labelWithSeparator = labelText.length + separatorWidth |
| 54 | + const totalLength = labelWithSeparator + promptText.length |
| 55 | + |
| 56 | + if (totalLength > availableForContent) { |
| 57 | + // Truncate prompt to fit |
| 58 | + const availableForPrompt = availableForContent - labelWithSeparator - ellipsisWidth |
| 59 | + if (availableForPrompt > 0) { |
| 60 | + promptText = promptText.slice(0, availableForPrompt) + '…' |
| 61 | + } else { |
| 62 | + // Not enough space for prompt, just show label truncated |
| 63 | + promptText = '' |
| 64 | + if (labelText.length > availableForContent - ellipsisWidth) { |
| 65 | + labelText = labelText.slice(0, availableForContent - ellipsisWidth) + '…' |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } else { |
| 70 | + // No label, just show prompt (truncated) |
| 71 | + if (promptText.length > availableForContent) { |
| 72 | + promptText = promptText.slice(0, availableForContent - ellipsisWidth) + '…' |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Determine colors based on state |
| 77 | + const iconColor = isClicked |
| 78 | + ? theme.success |
| 79 | + : isHovered |
| 80 | + ? theme.primary |
| 81 | + : theme.muted |
| 82 | + const labelColor = isClicked |
| 83 | + ? theme.muted |
| 84 | + : isHovered |
| 85 | + ? theme.primary |
| 86 | + : theme.foreground |
| 87 | + const promptColor = isClicked |
| 88 | + ? theme.muted |
| 89 | + : isHovered |
| 90 | + ? theme.primary |
| 91 | + : theme.muted |
| 92 | + |
| 93 | + return ( |
| 94 | + <box |
| 95 | + onMouseDown={handleClick} |
| 96 | + onMouseOver={handleMouseOver} |
| 97 | + onMouseOut={handleMouseOut} |
| 98 | + > |
| 99 | + <text selectable={false}> |
| 100 | + <span fg={iconColor}>{isClicked ? '✓' : '→'}</span> |
| 101 | + <span fg={labelColor} attributes={isHovered ? TextAttributes.UNDERLINE : undefined}> |
| 102 | + {' '}{hasLabel ? labelText : promptText} |
| 103 | + </span> |
| 104 | + {hasLabel && promptText && ( |
| 105 | + <span fg={promptColor}> |
| 106 | + {' · '}{promptText} |
| 107 | + </span> |
| 108 | + )} |
| 109 | + </text> |
| 110 | + </box> |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +interface SuggestFollowupsItemProps { |
| 115 | + toolCallId: string |
| 116 | + followups: SuggestedFollowup[] |
| 117 | + onSendFollowup: (prompt: string, index: number) => void |
| 118 | +} |
| 119 | + |
| 120 | +const SuggestFollowupsItem = ({ |
| 121 | + toolCallId, |
| 122 | + followups, |
| 123 | + onSendFollowup, |
| 124 | +}: SuggestFollowupsItemProps) => { |
| 125 | + const theme = useTheme() |
| 126 | + const suggestedFollowups = useChatStore((state) => state.suggestedFollowups) |
| 127 | + |
| 128 | + // Get clicked indices for this specific tool call |
| 129 | + const clickedIndices = |
| 130 | + suggestedFollowups?.toolCallId === toolCallId |
| 131 | + ? suggestedFollowups.clickedIndices |
| 132 | + : new Set<number>() |
| 133 | + |
| 134 | + return ( |
| 135 | + <box style={{ flexDirection: 'column' }}> |
| 136 | + <text style={{ fg: theme.muted }}> |
| 137 | + Next steps: |
| 138 | + </text> |
| 139 | + {followups.map((followup, index) => ( |
| 140 | + <FollowupLine |
| 141 | + key={`followup-${index}`} |
| 142 | + followup={followup} |
| 143 | + index={index} |
| 144 | + isClicked={clickedIndices.has(index)} |
| 145 | + onSendFollowup={onSendFollowup} |
| 146 | + /> |
| 147 | + ))} |
| 148 | + </box> |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * UI component for suggest_followups tool. |
| 154 | + * Displays clickable cards that send the followup prompt as a user message when clicked. |
| 155 | + */ |
| 156 | +export const SuggestFollowupsComponent = defineToolComponent({ |
| 157 | + toolName: 'suggest_followups', |
| 158 | + |
| 159 | + render(toolBlock): ToolRenderConfig { |
| 160 | + const { input, toolCallId } = toolBlock |
| 161 | + |
| 162 | + // Extract followups from input |
| 163 | + let followups: SuggestedFollowup[] = [] |
| 164 | + |
| 165 | + if (Array.isArray(input?.followups)) { |
| 166 | + followups = input.followups.filter( |
| 167 | + (f: unknown): f is SuggestedFollowup => |
| 168 | + typeof f === 'object' && |
| 169 | + f !== null && |
| 170 | + typeof (f as SuggestedFollowup).prompt === 'string', |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + if (followups.length === 0) { |
| 175 | + return { content: null } |
| 176 | + } |
| 177 | + |
| 178 | + // Store the followups in state for tracking clicks |
| 179 | + // This is done via a ref to avoid re-renders during the render phase |
| 180 | + const store = useChatStore.getState() |
| 181 | + if ( |
| 182 | + !store.suggestedFollowups || |
| 183 | + store.suggestedFollowups.toolCallId !== toolCallId |
| 184 | + ) { |
| 185 | + // Schedule the state update for after render |
| 186 | + setTimeout(() => { |
| 187 | + useChatStore.getState().setSuggestedFollowups({ |
| 188 | + toolCallId, |
| 189 | + followups, |
| 190 | + clickedIndices: new Set(), |
| 191 | + }) |
| 192 | + }, 0) |
| 193 | + } |
| 194 | + |
| 195 | + // The actual click handling is done in chat.tsx via the global handler |
| 196 | + // Here we just pass a placeholder that will be replaced |
| 197 | + const handleSendFollowup = (prompt: string, index: number) => { |
| 198 | + // This gets called from the FollowupCard component |
| 199 | + // The actual logic is handled via the global followup handler |
| 200 | + const event = new CustomEvent('codebuff:send-followup', { |
| 201 | + detail: { prompt, index }, |
| 202 | + }) |
| 203 | + globalThis.dispatchEvent(event) |
| 204 | + } |
| 205 | + |
| 206 | + return { |
| 207 | + content: ( |
| 208 | + <SuggestFollowupsItem |
| 209 | + toolCallId={toolCallId} |
| 210 | + followups={followups} |
| 211 | + onSendFollowup={handleSendFollowup} |
| 212 | + /> |
| 213 | + ), |
| 214 | + } |
| 215 | + }, |
| 216 | +}) |
0 commit comments