Commit 12b1fe30 by liimin

feat(chat): 添加 AI 导师模拟对练功能

- 在 Chat 组件中添加实践按钮,根据练习状态控制显示
- 实现 Practice 组件,用于模拟对练
- 更新 ChatWrapper 和 ChatWithHistory 以支持练习状态
- 添加 PracticeStatus 枚举和相关翻译
1 parent f0e4147f
{ {
"extends":["next/core-web-vitals","eslint:recommended","plugin:react/recommended"] "extends":["next/core-web-vitals","eslint:recommended","plugin:react/recommended", "prettier"]
} }
...@@ -68,6 +68,7 @@ const ChatWrapper = forwardRef<ChatWrapperMethods, Props>( ...@@ -68,6 +68,7 @@ const ChatWrapper = forwardRef<ChatWrapperMethods, Props>(
clearChatList, clearChatList,
setClearChatList, setClearChatList,
setIsResponding, setIsResponding,
isShowPracticeButton
} = useChatWithHistoryContext(); } = useChatWithHistoryContext();
const appConfig = useMemo(() => { const appConfig = useMemo(() => {
...@@ -345,6 +346,7 @@ const ChatWrapper = forwardRef<ChatWrapperMethods, Props>( ...@@ -345,6 +346,7 @@ const ChatWrapper = forwardRef<ChatWrapperMethods, Props>(
inputDisabled={inputDisabled} inputDisabled={inputDisabled}
isMobile={isMobile} isMobile={isMobile}
sidebarCollapseState={sidebarCollapseState} sidebarCollapseState={sidebarCollapseState}
isShowPracticeButton={isShowPracticeButton}
/> />
</div> </div>
); );
......
...@@ -59,6 +59,7 @@ export type ChatWithHistoryContextValue = { ...@@ -59,6 +59,7 @@ export type ChatWithHistoryContextValue = {
setIsResponding: (state: boolean) => void; setIsResponding: (state: boolean) => void;
currentConversationInputs: Record<string, any> | null; currentConversationInputs: Record<string, any> | null;
setCurrentConversationInputs: (v: Record<string, any>) => void; setCurrentConversationInputs: (v: Record<string, any>) => void;
isShowPracticeButton: boolean;
}; };
export const ChatWithHistoryContext = export const ChatWithHistoryContext =
...@@ -93,6 +94,7 @@ export const ChatWithHistoryContext = ...@@ -93,6 +94,7 @@ export const ChatWithHistoryContext =
setIsResponding: noop, setIsResponding: noop,
currentConversationInputs: {}, currentConversationInputs: {},
setCurrentConversationInputs: noop, setCurrentConversationInputs: noop,
isShowPracticeButton: false,
}); });
export const useChatWithHistoryContext = () => export const useChatWithHistoryContext = () =>
useContext(ChatWithHistoryContext); useContext(ChatWithHistoryContext);
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: liimin 17653212@qq.com * @Author: liimin 17653212@qq.com
* @Date: 2025-04-16 18:17:38 * @Date: 2025-04-16 18:17:38
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2025-06-10 18:42:49 * @LastEditTime: 2025-06-11 15:33:21
* @FilePath: \ai-assistant\app\components\base\chat\chat\index.tsx * @FilePath: \ai-assistant\app\components\base\chat\chat\index.tsx
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
...@@ -10,7 +10,7 @@ import type { FC, ReactNode } from 'react' ...@@ -10,7 +10,7 @@ import type { FC, ReactNode } from 'react'
import { memo, useCallback, useEffect, useRef, useState } from 'react' import { memo, useCallback, useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { debounce } from 'lodash-es' import { debounce } from 'lodash-es'
import { useShallow } from 'zustand/react/shallow' // import { useShallow } from 'zustand/react/shallow'
import type { ChatConfig, ChatItem, Feedback, OnRegenerate, OnSend } from '../types' import type { ChatConfig, ChatItem, Feedback, OnRegenerate, OnSend } from '../types'
import type { ThemeBuilder } from '../embedded-chatbot/theme/theme-context' import type { ThemeBuilder } from '../embedded-chatbot/theme/theme-context'
import Question from './question' import Question from './question'
...@@ -25,7 +25,7 @@ import Button from '@/app/components/base/button' ...@@ -25,7 +25,7 @@ import Button from '@/app/components/base/button'
import { StopCircle } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices' import { StopCircle } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
// import AgentLogModal from "@/app/components/base/agent-log-modal"; // import AgentLogModal from "@/app/components/base/agent-log-modal";
// import PromptLogModal from "@/app/components/base/prompt-log-modal"; // import PromptLogModal from "@/app/components/base/prompt-log-modal";
import { useStore as useAppStore } from '@/app/components/app/store' // import { useStore as useAppStore } from '@/app/components/app/store'
import type { AppData } from '@/models/share' import type { AppData } from '@/models/share'
import Practice from './practice' import Practice from './practice'
...@@ -68,20 +68,21 @@ export type ChatProps = { ...@@ -68,20 +68,21 @@ export type ChatProps = {
inputDisabled?: boolean inputDisabled?: boolean
isMobile?: boolean isMobile?: boolean
sidebarCollapseState?: boolean sidebarCollapseState?: boolean
isShowPracticeButton: boolean
} }
const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsForm, onRegenerate, chatList, isResponding, noStopResponding, onStopResponding, noChatInput, chatContainerClassName, chatContainerInnerClassName, chatFooterClassName, chatFooterInnerClassName, suggestedQuestions, showPromptLog, questionIcon, answerIcon, onAnnotationAdded, onAnnotationEdited, onAnnotationRemoved, chatNode, onFeedback, chatAnswerContainerInner, hideProcessDetail, hideLogModal, themeBuilder, switchSibling, showFeatureBar, showFileUpload, onFeatureBarClick, noSpacing, inputDisabled, isMobile, sidebarCollapseState }) => { const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsForm, onRegenerate, chatList, isResponding, noStopResponding, onStopResponding, noChatInput, chatContainerClassName, chatContainerInnerClassName, chatFooterClassName, chatFooterInnerClassName, suggestedQuestions, showPromptLog, questionIcon, answerIcon, onAnnotationAdded, onAnnotationEdited, onAnnotationRemoved, chatNode, onFeedback, chatAnswerContainerInner, hideProcessDetail, hideLogModal, themeBuilder, switchSibling, showFeatureBar, showFileUpload, onFeatureBarClick, noSpacing, inputDisabled, isMobile, sidebarCollapseState,isShowPracticeButton }) => {
const { t } = useTranslation() const { t } = useTranslation()
const { currentLogItem, setCurrentLogItem, showPromptLogModal, setShowPromptLogModal, showAgentLogModal, setShowAgentLogModal } = useAppStore( // const { currentLogItem, setCurrentLogItem, showPromptLogModal, setShowPromptLogModal, showAgentLogModal, setShowAgentLogModal } = useAppStore(
useShallow(state => ({ // useShallow(state => ({
currentLogItem: state.currentLogItem, // currentLogItem: state.currentLogItem,
setCurrentLogItem: state.setCurrentLogItem, // setCurrentLogItem: state.setCurrentLogItem,
showPromptLogModal: state.showPromptLogModal, // showPromptLogModal: state.showPromptLogModal,
setShowPromptLogModal: state.setShowPromptLogModal, // setShowPromptLogModal: state.setShowPromptLogModal,
showAgentLogModal: state.showAgentLogModal, // showAgentLogModal: state.showAgentLogModal,
setShowAgentLogModal: state.setShowAgentLogModal, // setShowAgentLogModal: state.setShowAgentLogModal,
})), // })),
) // )
const [width, setWidth] = useState(0) const [width, setWidth] = useState(0)
const chatContainerRef = useRef<HTMLDivElement>(null) const chatContainerRef = useRef<HTMLDivElement>(null)
const chatContainerInnerRef = useRef<HTMLDivElement>(null) const chatContainerInnerRef = useRef<HTMLDivElement>(null)
...@@ -212,12 +213,12 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF ...@@ -212,12 +213,12 @@ const Chat: FC<ChatProps> = ({ appData, config, onSend, onClear, inputs, inputsF
<div className="mb-2 flex justify-center"> <div className="mb-2 flex justify-center">
<Button onClick={onStopResponding}> <Button onClick={onStopResponding}>
<StopCircle className="mr-[5px] h-3.5 w-3.5 text-gray-500" /> <StopCircle className="mr-[5px] h-3.5 w-3.5 text-gray-500" />
<span className="text-xs font-normal text-gray-500">{t('停止响应')}</span> <span className="text-xs font-normal text-gray-500">{t('appDebug.operation.stopResponding')}</span>
</Button> </Button>
</div> </div>
)} )}
{hasTryToAsk && <TryToAsk suggestedQuestions={suggestedQuestions} onSend={onSend} isMobile={isMobile} />} {hasTryToAsk && <TryToAsk suggestedQuestions={suggestedQuestions} onSend={onSend} isMobile={isMobile} />}
{<Practice question='AI导师 模拟对练' onSend={handleSend} />} {isShowPracticeButton && <Practice question={t('appDebug.feature.practice.text')} onSend={handleSend} />}
{!noChatInput && <ChatInputArea disabled={inputDisabled} showFeatureBar={showFeatureBar} showFileUpload={showFileUpload} featureBarDisabled={isResponding} onFeatureBarClick={onFeatureBarClick} visionConfig={config?.file_upload} speechToTextConfig={config?.speech_to_text} onSend={handleSend} onClear={onClear} inputs={inputs} inputsForm={inputsForm} theme={themeBuilder?.theme} isResponding={isResponding} />} {!noChatInput && <ChatInputArea disabled={inputDisabled} showFeatureBar={showFeatureBar} showFileUpload={showFileUpload} featureBarDisabled={isResponding} onFeatureBarClick={onFeatureBarClick} visionConfig={config?.file_upload} speechToTextConfig={config?.speech_to_text} onSend={handleSend} onClear={onClear} inputs={inputs} inputsForm={inputsForm} theme={themeBuilder?.theme} isResponding={isResponding} />}
</div> </div>
</div> </div>
......
import type { FC } from 'react' import type { FC } from 'react'
import { memo } from 'react' import { memo } from 'react'
import { useTranslation } from 'react-i18next'
import type { OnSend } from '../types' import type { OnSend } from '../types'
import Button from '@/app/components/base/button' import Button from '@/app/components/base/button'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { InputMessageType } from '../types' import { InputMessageType } from '../types'
import { Practice as PracticeIcon } from '@/app/components/base/icons/src/image/icons'
type PracticeProps = { type PracticeProps = {
question: string question: string
onSend: OnSend onSend: OnSend
} }
const Practice: FC<PracticeProps> = ({ question, onSend }) => { const Practice: FC<PracticeProps> = ({ question, onSend }) => {
return ( return (
<div className="[margin-bottom:12px]"> <div className="mb-3">
<div className={cn('flex flex-wrap')}> {
{ <Button
<Button size="small"
size="small" variant="secondary-accent"
variant="secondary-accent" className="mb-1 w-40 h-8 pl-2
className="mb-1 mr-1 last:mr-0 [width:159px] [height:32px] rounded-8
rounded-8 opacity-100 bg-white
shadow-[0_3px_8px_0_#7D94B840]" shadow-[0_3px_8px_0_#7D94B840]"
onClick={() => onClick={() =>
onSend({ onSend({
message: question, message: question,
type: InputMessageType.SELF, type: InputMessageType.SELF,
}) })
}> }>
{question} <PracticeIcon className="size-4 mr-3" />
</Button> {question}
} </Button>
</div> }
</div> </div>
) )
} }
......
import type { TypeWithI18N } from '@/app/components/header/account-setting/model-provider-page/declarations' import type { TypeWithI18N,Annotation, MessageRating } from '@/types/app'
import type { Annotation, MessageRating } from '@/models/log' // import type { Annotation, MessageRating } from '@/models/log'
import type { FileEntity } from '@/app/components/base/file-uploader/types' import type { FileEntity } from '@/app/components/base/file-uploader/types'
import type { InputVarType } from '@/app/components/workflow/types' import type { InputVarType } from '@/app/components/workflow/types'
import type { FileResponse } from '@/types/workflow' import type { FileResponse } from '@/types/workflow'
......
/* /*
* @Author: yangming@didihu.com.cn yangming@didihu.com.cn * @Author: yangming@didihu.com.cn yangming@didihu.com.cn
* @Date: 2025-04-22 09:29:17 * @Date: 2025-04-22 09:29:17
* @LastEditors: yangming@didihu.com.cn yangming@didihu.com.cn * @LastEditors: limin
* @LastEditTime: 2025-05-15 17:09:35 * @LastEditTime: 2025-06-11 09:39:11
* @FilePath: \glsk-ai-assistant\app\components\base\chat\constants.ts * @FilePath: \ai-assistant\app\components\base\chat\constants.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
export const CONVERSATION_ID_INFO = 'conversationIdInfo' export const CONVERSATION_ID_INFO = 'conversationIdInfo'
...@@ -12,7 +12,7 @@ export const UUID_NIL = '00000000-0000-0000-0000-000000000000' ...@@ -12,7 +12,7 @@ export const UUID_NIL = '00000000-0000-0000-0000-000000000000'
export const AVATAR_AI_URL = 'https://img01.glsx.com.cn/weapp/resource/saas/ai/ai-avatar.png' export const AVATAR_AI_URL = 'https://img01.glsx.com.cn/weapp/resource/saas/ai/ai-avatar.png'
export const AVATAR_USER_URL = 'https://img02.glsx.com.cn/weapp/resource/saas/ai/user-avatar.png' export const AVATAR_USER_URL = 'https://img02.glsx.com.cn/weapp/resource/saas/ai/user-avatar.png'
export const MAIN_TITLE = '广联AI集市' export const MAIN_TITLE = '广联AI集市'
export const SELF_INPUT = '0' // export const SELF_INPUT = '0'
export const AUTO_INPUT = '1' // export const AUTO_INPUT = '1'
export const SEND_BUTTON = 'https://img02.glsx.com.cn/weapp/resource/saas/ai/ai-send.png' export const SEND_BUTTON = 'https://img02.glsx.com.cn/weapp/resource/saas/ai/ai-send.png'
export const CLEAR_BUTTON = 'https://img02.glsx.com.cn/weapp/resource/saas/ai/ai-clear.png' export const CLEAR_BUTTON = 'https://img02.glsx.com.cn/weapp/resource/saas/ai/ai-clear.png'
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: yangming@didihu.com.cn yangming@didihu.com.cn * @Author: yangming@didihu.com.cn yangming@didihu.com.cn
* @Date: 2025-04-22 09:29:17 * @Date: 2025-04-22 09:29:17
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2025-06-10 17:57:27 * @LastEditTime: 2025-06-11 14:57:49
* @FilePath: \ai-assistant\app\components\base\chat\types.ts * @FilePath: \ai-assistant\app\components\base\chat\types.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
...@@ -17,10 +17,6 @@ import type { FileEntity } from '@/app/components/base/file-uploader/types' ...@@ -17,10 +17,6 @@ import type { FileEntity } from '@/app/components/base/file-uploader/types'
export type { VisionFile } from '@/types/app' export type { VisionFile } from '@/types/app'
export { TransferMethod } from '@/types/app' export { TransferMethod } from '@/types/app'
export type {
Inputs,
PromptVariable,
} from '@/models/debug'
export type UserInputForm = { export type UserInputForm = {
default: string default: string
...@@ -84,6 +80,12 @@ export enum InputMessageType { ...@@ -84,6 +80,12 @@ export enum InputMessageType {
SELF = '0', SELF = '0',
SYSTEM = '1' SYSTEM = '1'
} }
export enum PracticeStatus {
None = '0',
Practicing = '1',
Passed = '2',
Failed = '3'
}
export type OnSend = { export type OnSend = {
(message: InputMessageProp, files?: FileEntity[]): void (message: InputMessageProp, files?: FileEntity[]): void
(message: InputMessageProp, files: FileEntity[] | undefined, isRegenerate: boolean, lastAnswer?: ChatItem | null): void (message: InputMessageProp, files: FileEntity[] | undefined, isRegenerate: boolean, lastAnswer?: ChatItem | null): void
......
.wrapper {
display: inline-flex;
background: url(~@/app/components/base/icons/assets/image/icons/practice.png) center center no-repeat;
background-size: contain;
}
// GENERATE BY script
// DON NOT EDIT IT MANUALLY
import * as React from 'react'
import cn from '@/utils/classnames'
import s from './Practice.module.css'
const Icon = (
{
ref,
className,
...restProps
}: React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement> & {
ref?: React.RefObject<HTMLSpanElement>;
},
) => <span className={cn(s.wrapper, className)} {...restProps} ref={ref} />
Icon.displayName = 'Practice'
export default Icon
export { default as Practice } from './Practice'
...@@ -129,9 +129,9 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any ...@@ -129,9 +129,9 @@ const CodeBlock: any = memo(({ inline, className, children = '', ...props }: any
const renderCodeContent = useMemo(() => { const renderCodeContent = useMemo(() => {
const content = String(children).replace(/\n$/, '') const content = String(children).replace(/\n$/, '')
switch (language) { switch (language) {
// case 'mermaid': case 'mermaid':
// if (isSVG) return <Flowchart PrimitiveCode={content} /> if (isSVG) return <Flowchart PrimitiveCode={content} />
// break break
case 'echarts': case 'echarts':
return ( return (
<div style={{ minHeight: '350px', minWidth: '100%', overflowX: 'scroll' }}> <div style={{ minHeight: '350px', minWidth: '100%', overflowX: 'scroll' }}>
...@@ -284,7 +284,7 @@ export function Markdown(props: { content: string; className?: string; customDis ...@@ -284,7 +284,7 @@ export function Markdown(props: { content: string; className?: string; customDis
return ( return (
<div className={cn('markdown-body', '!text-text-primary', props.className)}> <div className={cn('markdown-body', '!text-text-primary', props.className)}>
<ReactMarkdown <ReactMarkdown
remarkPlugins={[[RemarkMath, { singleDollarTextMath: false }], RemarkBreaks]} remarkPlugins={[RemarkGfm, [RemarkMath, { singleDollarTextMath: false }], RemarkBreaks]}
rehypePlugins={[ rehypePlugins={[
RehypeKatex, RehypeKatex,
RehypeRaw as any, RehypeRaw as any,
......
...@@ -43,6 +43,9 @@ const translation = { ...@@ -43,6 +43,9 @@ const translation = {
description: 'Use gpt-4, please set API Key.', description: 'Use gpt-4, please set API Key.',
}, },
feature: { feature: {
practice:{
text: 'AI Practice',
},
groupChat: { groupChat: {
title: 'Chat enhance', title: 'Chat enhance',
description: 'Add pre-conversation settings for apps can enhance user experience.', description: 'Add pre-conversation settings for apps can enhance user experience.',
......
...@@ -43,6 +43,9 @@ const translation = { ...@@ -43,6 +43,9 @@ const translation = {
description: '使用 gpt-4,请设置 API Key', description: '使用 gpt-4,请设置 API Key',
}, },
feature: { feature: {
practice:{
text: 'AI导师 模拟对练',
},
groupChat: { groupChat: {
title: '聊天增强', title: '聊天增强',
description: '为聊天型应用添加预对话设置,可以提升用户体验。', description: '为聊天型应用添加预对话设置,可以提升用户体验。',
......
...@@ -15,7 +15,7 @@ const withMDX = require('@next/mdx')({ ...@@ -15,7 +15,7 @@ const withMDX = require('@next/mdx')({
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
webpack: (config, { dev, isServer }) => { webpack: (config, { dev, isServer }) => {
config.plugins.push(codeInspectorPlugin({ bundler: 'webpack' })) // config.plugins.push(codeInspectorPlugin({ bundler: 'webpack' }))
return config return config
}, },
productionBrowserSourceMaps: false, // enable browser source map generation during the production build productionBrowserSourceMaps: false, // enable browser source map generation during the production build
......
...@@ -126,6 +126,7 @@ ...@@ -126,6 +126,7 @@
"jest": "^29.7.0", "jest": "^29.7.0",
"lint-staged": "^15.2.10", "lint-staged": "^15.2.10",
"magicast": "^0.3.5", "magicast": "^0.3.5",
"prettier": "^3.5.3",
"sass": "^1.80.3", "sass": "^1.80.3",
"storybook": "8.5.0", "storybook": "8.5.0",
"stylelint": "^16.19.1", "stylelint": "^16.19.1",
......
...@@ -448,3 +448,29 @@ export type RetrievalConfig = { ...@@ -448,3 +448,29 @@ export type RetrievalConfig = {
} }
} }
} }
export type TypeWithI18N<T = string> = {
en_US: T
zh_Hans: T
[key: string]: T
}
export const MessageRatings = ['like', 'dislike', null] as const
export type MessageRating = typeof MessageRatings[number]
export type LogAnnotation = {
id: string
content: string
account: {
id: string
name: string
email: string
}
created_at: number
}
export type Annotation = {
id: string
authorName: string
logAnnotation?: LogAnnotation
created_at?: number
}
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!