Skip to content

Commit 1f09f9e

Browse files
committed
feat: Add follow-up question trigger functionality in Chat components, allowing users to initiate immediate follow-up questions based on AI responses, enhancing interaction and engagement
1 parent e6b50fa commit 1f09f9e

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

src/renderer/src/components/pages/chat/ChatInput.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
SettingOutlined,
77
QuestionCircleOutlined,
88
BulbOutlined,
9-
DownOutlined
9+
DownOutlined,
10+
PlaySquareOutlined
1011
} from '@ant-design/icons'
1112
import { LLMConfig } from '../../../types/type'
1213
import { useSettingsStore } from '../../../stores/settingsStore'
@@ -166,6 +167,7 @@ interface ChatInputProps {
166167
autoQuestionMode?: 'ai' | 'preset'
167168
autoQuestionListId?: string
168169
onAutoQuestionChange?: (enabled: boolean, mode: 'ai' | 'preset', listId?: string) => void
170+
onTriggerFollowUpQuestion?: () => Promise<void>
169171
}
170172

171173
export interface ChatInputRef {
@@ -189,7 +191,8 @@ const ChatInput = forwardRef<ChatInputRef, ChatInputProps>(
189191
autoQuestionEnabled = false,
190192
autoQuestionMode = 'ai',
191193
autoQuestionListId,
192-
onAutoQuestionChange
194+
onAutoQuestionChange,
195+
onTriggerFollowUpQuestion
193196
},
194197
ref
195198
) => {
@@ -295,10 +298,32 @@ const ChatInput = forwardRef<ChatInputRef, ChatInputProps>(
295298
mode={autoQuestionMode}
296299
selectedListId={autoQuestionListId}
297300
promptLists={settings.promptLists || []}
298-
disabled={disabled}
301+
disabled={false}
299302
onChange={onAutoQuestionChange}
300303
/>
301304
</div>
305+
306+
{/* 立即追问按钮 */}
307+
{autoQuestionEnabled && (
308+
<Tooltip title={`立即触发一次${autoQuestionMode === 'ai' ? 'AI' : '预设列表'}追问`}>
309+
<Button
310+
type="text"
311+
size="small"
312+
icon={<PlaySquareOutlined />}
313+
onClick={async () => {
314+
try {
315+
await onTriggerFollowUpQuestion?.()
316+
} catch (error) {
317+
console.error('立即追问失败:', error)
318+
}
319+
}}
320+
disabled={disabled || loading}
321+
style={{ fontSize: '11px', color: '#1890ff' }}
322+
>
323+
立即追问
324+
</Button>
325+
</Tooltip>
326+
)}
302327
</Space>
303328

304329
{/* 发送/停止按钮 */}

src/renderer/src/components/pages/chat/ChatLogic.tsx

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface ChatLogicProps {
2222
onToggleFavorite: (messageId: string) => void
2323
onModelChangeForMessage: (messageId: string, newModelId: string) => Promise<void>
2424
onDeleteMessage: (messageId: string) => Promise<void>
25+
onTriggerFollowUpQuestion: () => Promise<void>
2526
}) => React.ReactNode
2627
}
2728

@@ -77,8 +78,7 @@ export default function ChatLogic({
7778
taskType,
7879
taskContext,
7980
async (messageId: string) => {
80-
// 只在普通聊天任务完成后触发自动提问
81-
if (autoQuestionEnabled && taskType === 'chat') {
81+
if (autoQuestionEnabled) {
8282
try {
8383
setTimeout(async () => {
8484
await autoQuestion.generateAndSendFollowUpQuestion(messageId)
@@ -112,6 +112,54 @@ export default function ChatLogic({
112112
sendMessageRef.current = messageOperations.handleSendMessage
113113
}, [messageOperations.handleSendMessage])
114114

115+
// 获取最新的AI消息ID
116+
const getLatestAIMessageId = useCallback(() => {
117+
if (!chat?.messages) return null
118+
119+
// 获取当前路径的消息
120+
const currentPath = chat.currentPath || []
121+
let messages: any[] = []
122+
123+
if (currentPath.length > 0) {
124+
// 使用当前路径
125+
messages = currentPath
126+
.map((id: string) => chat.messages.find((msg: any) => msg.id === id))
127+
.filter(Boolean)
128+
} else {
129+
// 使用消息树获取默认路径
130+
messages = messageTree.getCurrentPathMessages()
131+
}
132+
133+
// 从后往前找最新的AI消息
134+
for (let i = messages.length - 1; i >= 0; i--) {
135+
if (messages[i].role === 'assistant') {
136+
return messages[i].id
137+
}
138+
}
139+
140+
return null
141+
}, [chat, messageTree])
142+
143+
// 立即触发追问
144+
const handleTriggerFollowUpQuestion = useCallback(async () => {
145+
if (!autoQuestionEnabled) {
146+
console.warn('自动追问功能未开启')
147+
return
148+
}
149+
150+
const latestAIMessageId = getLatestAIMessageId()
151+
if (!latestAIMessageId) {
152+
console.warn('未找到AI消息,无法触发追问')
153+
return
154+
}
155+
156+
try {
157+
await autoQuestion.generateAndSendFollowUpQuestion(latestAIMessageId)
158+
} catch (error) {
159+
console.error('立即触发追问失败:', error)
160+
}
161+
}, [autoQuestionEnabled, getLatestAIMessageId, autoQuestion])
162+
115163
const handleModelChange = (modelId: string) => {
116164
setSelectedModel(modelId)
117165
}
@@ -133,6 +181,7 @@ export default function ChatLogic({
133181
onEditAndResendMessage: messageOperations.handleEditAndResendMessage,
134182
onToggleFavorite: messageOperations.handleToggleFavorite,
135183
onModelChangeForMessage: messageOperations.handleModelChangeForMessage,
136-
onDeleteMessage: messageOperations.handleDeleteMessage
184+
onDeleteMessage: messageOperations.handleDeleteMessage,
185+
onTriggerFollowUpQuestion: handleTriggerFollowUpQuestion
137186
})
138187
}

src/renderer/src/components/pages/chat/ChatWindow.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
180180
onEditAndResendMessage,
181181
onToggleFavorite,
182182
onModelChangeForMessage,
183-
onDeleteMessage
183+
onDeleteMessage,
184+
onTriggerFollowUpQuestion
184185
}) => (
185186
<>
186187
{/* 消息树侧边栏 */}
@@ -196,7 +197,7 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
196197
/>
197198

198199
{/* 聊天主内容区 */}
199-
<div className="chat-main-content">
200+
<div className="chat-content" style={{ flex: 1, display: 'flex', flexDirection: 'column' }}>
200201
<ChatHeader
201202
chatId={chatId}
202203
chatTitle={chat.title}
@@ -228,7 +229,6 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
228229
onToggleMessageCollapse={handleToggleMessageCollapse}
229230
// 设置相关props
230231
onOpenSettings={handleOpenSettings}
231-
onDeleteMessage={onDeleteMessage}
232232
/>
233233
<ChatInput
234234
ref={chatInputRef}
@@ -253,6 +253,7 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
253253
autoQuestionMode={autoQuestionMode}
254254
autoQuestionListId={autoQuestionListId}
255255
onAutoQuestionChange={handleAutoQuestionChange}
256+
onTriggerFollowUpQuestion={onTriggerFollowUpQuestion}
256257
/>
257258
</div>
258259
</>

0 commit comments

Comments
 (0)