|
| 1 | +import React, { useCallback } from 'react' |
| 2 | + |
| 3 | +import { Button } from '../button' |
| 4 | +import { defineToolComponent } from './types' |
| 5 | +import { useTheme } from '../../hooks/use-theme' |
| 6 | +import { useChatStore } from '../../state/chat-store' |
| 7 | + |
| 8 | +import type { ToolRenderConfig } from './types' |
| 9 | +import type { SuggestedFollowup } from '../../state/chat-store' |
| 10 | + |
| 11 | +interface FollowupCardProps { |
| 12 | + followup: SuggestedFollowup |
| 13 | + index: number |
| 14 | + isClicked: boolean |
| 15 | + onSendFollowup: (prompt: string, index: number) => void |
| 16 | +} |
| 17 | + |
| 18 | +const FollowupCard = ({ |
| 19 | + followup, |
| 20 | + index, |
| 21 | + isClicked, |
| 22 | + onSendFollowup, |
| 23 | +}: FollowupCardProps) => { |
| 24 | + const theme = useTheme() |
| 25 | + |
| 26 | + const handleClick = useCallback(() => { |
| 27 | + onSendFollowup(followup.prompt, index) |
| 28 | + }, [followup.prompt, index, onSendFollowup]) |
| 29 | + |
| 30 | + // Use label if provided, otherwise truncate the prompt |
| 31 | + const displayLabel = followup.label || truncateText(followup.prompt, 40) |
| 32 | + |
| 33 | + return ( |
| 34 | + <Button |
| 35 | + onClick={handleClick} |
| 36 | + style={{ |
| 37 | + paddingLeft: 2, |
| 38 | + paddingRight: 2, |
| 39 | + paddingTop: 0, |
| 40 | + paddingBottom: 0, |
| 41 | + backgroundColor: isClicked ? theme.surface : theme.surfaceHover, |
| 42 | + borderColor: isClicked ? theme.success : theme.border, |
| 43 | + }} |
| 44 | + > |
| 45 | + <text |
| 46 | + style={{ |
| 47 | + fg: isClicked ? theme.muted : theme.foreground, |
| 48 | + }} |
| 49 | + > |
| 50 | + {isClicked && <span fg={theme.success}>✓ </span>} |
| 51 | + <span>{displayLabel}</span> |
| 52 | + </text> |
| 53 | + </Button> |
| 54 | + ) |
| 55 | +} |
| 56 | + |
| 57 | +function truncateText(text: string, maxLength: number): string { |
| 58 | + if (text.length <= maxLength) return text |
| 59 | + return text.slice(0, maxLength - 1) + '…' |
| 60 | +} |
| 61 | + |
| 62 | +interface SuggestFollowupsItemProps { |
| 63 | + toolCallId: string |
| 64 | + followups: SuggestedFollowup[] |
| 65 | + onSendFollowup: (prompt: string, index: number) => void |
| 66 | +} |
| 67 | + |
| 68 | +const SuggestFollowupsItem = ({ |
| 69 | + toolCallId, |
| 70 | + followups, |
| 71 | + onSendFollowup, |
| 72 | +}: SuggestFollowupsItemProps) => { |
| 73 | + const theme = useTheme() |
| 74 | + const suggestedFollowups = useChatStore((state) => state.suggestedFollowups) |
| 75 | + |
| 76 | + // Get clicked indices for this specific tool call |
| 77 | + const clickedIndices = |
| 78 | + suggestedFollowups?.toolCallId === toolCallId |
| 79 | + ? suggestedFollowups.clickedIndices |
| 80 | + : new Set<number>() |
| 81 | + |
| 82 | + return ( |
| 83 | + <box style={{ flexDirection: 'column', gap: 1, width: '100%' }}> |
| 84 | + <text style={{ fg: theme.muted }}>Suggested next steps:</text> |
| 85 | + <box |
| 86 | + style={{ |
| 87 | + flexDirection: 'row', |
| 88 | + gap: 1, |
| 89 | + flexWrap: 'wrap', |
| 90 | + width: '100%', |
| 91 | + }} |
| 92 | + > |
| 93 | + {followups.map((followup, index) => ( |
| 94 | + <FollowupCard |
| 95 | + key={`followup-${index}`} |
| 96 | + followup={followup} |
| 97 | + index={index} |
| 98 | + isClicked={clickedIndices.has(index)} |
| 99 | + onSendFollowup={onSendFollowup} |
| 100 | + /> |
| 101 | + ))} |
| 102 | + </box> |
| 103 | + </box> |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * UI component for suggest_followups tool. |
| 109 | + * Displays clickable cards that send the followup prompt as a user message when clicked. |
| 110 | + */ |
| 111 | +export const SuggestFollowupsComponent = defineToolComponent({ |
| 112 | + toolName: 'suggest_followups', |
| 113 | + |
| 114 | + render(toolBlock): ToolRenderConfig { |
| 115 | + const { input, toolCallId } = toolBlock |
| 116 | + |
| 117 | + // Extract followups from input |
| 118 | + let followups: SuggestedFollowup[] = [] |
| 119 | + |
| 120 | + if (Array.isArray(input?.followups)) { |
| 121 | + followups = input.followups.filter( |
| 122 | + (f: unknown): f is SuggestedFollowup => |
| 123 | + typeof f === 'object' && |
| 124 | + f !== null && |
| 125 | + typeof (f as SuggestedFollowup).prompt === 'string', |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + if (followups.length === 0) { |
| 130 | + return { content: null } |
| 131 | + } |
| 132 | + |
| 133 | + // Store the followups in state for tracking clicks |
| 134 | + // This is done via a ref to avoid re-renders during the render phase |
| 135 | + const store = useChatStore.getState() |
| 136 | + if ( |
| 137 | + !store.suggestedFollowups || |
| 138 | + store.suggestedFollowups.toolCallId !== toolCallId |
| 139 | + ) { |
| 140 | + // Schedule the state update for after render |
| 141 | + setTimeout(() => { |
| 142 | + useChatStore.getState().setSuggestedFollowups({ |
| 143 | + toolCallId, |
| 144 | + followups, |
| 145 | + clickedIndices: new Set(), |
| 146 | + }) |
| 147 | + }, 0) |
| 148 | + } |
| 149 | + |
| 150 | + // The actual click handling is done in chat.tsx via the global handler |
| 151 | + // Here we just pass a placeholder that will be replaced |
| 152 | + const handleSendFollowup = (prompt: string, index: number) => { |
| 153 | + // This gets called from the FollowupCard component |
| 154 | + // The actual logic is handled via the global followup handler |
| 155 | + const event = new CustomEvent('codebuff:send-followup', { |
| 156 | + detail: { prompt, index }, |
| 157 | + }) |
| 158 | + globalThis.dispatchEvent(event) |
| 159 | + } |
| 160 | + |
| 161 | + return { |
| 162 | + content: ( |
| 163 | + <SuggestFollowupsItem |
| 164 | + toolCallId={toolCallId} |
| 165 | + followups={followups} |
| 166 | + onSendFollowup={handleSendFollowup} |
| 167 | + /> |
| 168 | + ), |
| 169 | + } |
| 170 | + }, |
| 171 | +}) |
0 commit comments