feat: 集成混合路由快速路径和前端 SSE 事件支持
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m11s
Some checks failed
构建并部署 AI Agent 服务 / deploy (push) Failing after 6m11s
This commit is contained in:
@@ -216,6 +216,44 @@ export const HumanReviewCard: React.FC<HumanReviewCardProps> = ({ review, onActi
|
||||
);
|
||||
};
|
||||
|
||||
// 新增:路径指示器组件
|
||||
interface PathIndicatorProps {
|
||||
path?: string;
|
||||
intent?: string;
|
||||
confidence?: number;
|
||||
}
|
||||
|
||||
export const PathIndicator: React.FC<PathIndicatorProps> = ({
|
||||
path,
|
||||
intent,
|
||||
confidence
|
||||
}) => {
|
||||
if (!path) return null;
|
||||
|
||||
const getPathIcon = () => path === 'fast' ? '⚡' : '🔄';
|
||||
const getPathText = () => path === 'fast' ? '快速路径' : 'React 循环';
|
||||
const getPathColor = () => path === 'fast' ? 'text-green-600' : 'text-blue-600';
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2 text-xs text-gray-500 mb-2 flex-wrap">
|
||||
<span>{getPathIcon()}</span>
|
||||
<span className={getPathColor()}>{getPathText()}</span>
|
||||
{intent && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span>意图: {intent}</span>
|
||||
</>
|
||||
)}
|
||||
{confidence && (
|
||||
<>
|
||||
<span>•</span>
|
||||
<span>置信度: {(confidence * 100).toFixed(0)}%</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
interface AssistantMessageProps {
|
||||
message: Message;
|
||||
onReviewAction?: (review: HumanReview, action: 'approve' | 'reject' | 'modify', comment?: string, modifiedContent?: string) => void;
|
||||
@@ -228,8 +266,17 @@ export const AssistantMessage: React.FC<AssistantMessageProps> = ({ message, onR
|
||||
AI
|
||||
</div>
|
||||
<div className="flex-1">
|
||||
{/* 新增:路径指示器 */}
|
||||
{message.metadata && (
|
||||
<PathIndicator
|
||||
path={message.metadata.path}
|
||||
intent={message.metadata.intent}
|
||||
confidence={message.metadata.confidence}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ReasoningSection content={message.reasoning} />
|
||||
|
||||
|
||||
{message.toolCalls.map(toolCall => (
|
||||
<ToolCallCard key={toolCall.id} toolCall={toolCall} />
|
||||
))}
|
||||
@@ -329,7 +376,7 @@ interface ChatContainerProps {
|
||||
|
||||
export const ChatContainer: React.FC<ChatContainerProps> = ({ model = 'zhipu', threadId: propThreadId }) => {
|
||||
const [threadId] = useState(() => propThreadId || Date.now().toString());
|
||||
const { messages, isLoading, sendMessage, handleReviewAction } = useChat();
|
||||
const { messages, isLoading, sendMessage, handleReviewAction, lastIntent } = useChat();
|
||||
|
||||
const handleSend = (text: string) => {
|
||||
sendMessage(text, threadId, model);
|
||||
@@ -376,4 +423,4 @@ export const ChatContainer: React.FC<ChatContainerProps> = ({ model = 'zhipu', t
|
||||
);
|
||||
};
|
||||
|
||||
export default ChatContainer;
|
||||
export default ChatContainer
|
||||
@@ -5,6 +5,21 @@ export interface SSEEvent {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// 新增:意图分类事件
|
||||
export interface IntentClassifiedEvent extends SSEEvent {
|
||||
type: 'intent_classified';
|
||||
intent: string;
|
||||
confidence: number;
|
||||
reasoning: string;
|
||||
}
|
||||
|
||||
// 新增:路径决策事件
|
||||
export interface PathDecisionEvent extends SSEEvent {
|
||||
type: 'path_decision';
|
||||
path: 'fast' | 'react_loop';
|
||||
intent: string;
|
||||
}
|
||||
|
||||
export interface ToolCall {
|
||||
id: string;
|
||||
tool: string;
|
||||
@@ -30,6 +45,12 @@ export interface Message {
|
||||
humanReview?: HumanReview;
|
||||
isLoading: boolean;
|
||||
timestamp: Date;
|
||||
// 新增:元数据
|
||||
metadata?: {
|
||||
intent?: string;
|
||||
confidence?: number;
|
||||
path?: 'fast' | 'react_loop';
|
||||
};
|
||||
}
|
||||
|
||||
const API_BASE = 'http://localhost:8079';
|
||||
@@ -74,7 +95,7 @@ export class ApiClient {
|
||||
if (line.startsWith('data: ')) {
|
||||
const data = line.slice(6);
|
||||
if (data === '[DONE]') return;
|
||||
|
||||
|
||||
try {
|
||||
const event = JSON.parse(data);
|
||||
yield event;
|
||||
@@ -117,6 +138,8 @@ export class ApiClient {
|
||||
export function useChat() {
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
// 新增:最后一次意图识别结果
|
||||
const [lastIntent, setLastIntent] = useState<{ intent: string; confidence: number } | null>(null);
|
||||
const [apiClient] = useState(() => new ApiClient());
|
||||
const currentMessageRef = useRef<Message | null>(null);
|
||||
|
||||
@@ -129,6 +152,8 @@ export function useChat() {
|
||||
toolCalls: [],
|
||||
isLoading: role === 'assistant',
|
||||
timestamp: new Date(),
|
||||
// 新增:初始化元数据
|
||||
metadata: undefined,
|
||||
};
|
||||
setMessages(prev => [...prev, message]);
|
||||
currentMessageRef.current = message;
|
||||
@@ -137,24 +162,51 @@ export function useChat() {
|
||||
|
||||
const updateCurrentMessage = useCallback((updates: Partial<Message>) => {
|
||||
if (!currentMessageRef.current) return;
|
||||
|
||||
setMessages(prev => prev.map(msg =>
|
||||
msg.id === currentMessageRef.current!.id
|
||||
|
||||
setMessages(prev => prev.map(msg =>
|
||||
msg.id === currentMessageRef.current!.id
|
||||
? { ...msg, ...updates }
|
||||
: msg
|
||||
));
|
||||
|
||||
|
||||
currentMessageRef.current = { ...currentMessageRef.current, ...updates };
|
||||
}, []);
|
||||
|
||||
const sendMessage = useCallback(async (text: string, threadId: string, model: string = 'zhipu') => {
|
||||
setIsLoading(true);
|
||||
setLastIntent(null); // 重置
|
||||
addMessage('user', text);
|
||||
addMessage('assistant', '');
|
||||
|
||||
try {
|
||||
for await (const event of apiClient.chatStream(text, threadId, model)) {
|
||||
switch (event.type) {
|
||||
// 新增:处理意图分类事件
|
||||
case 'intent_classified':
|
||||
console.log(`🧠 意图识别: ${event.intent} (置信度: ${event.confidence})`);
|
||||
setLastIntent({
|
||||
intent: event.intent,
|
||||
confidence: event.confidence,
|
||||
});
|
||||
updateCurrentMessage({
|
||||
metadata: {
|
||||
intent: event.intent,
|
||||
confidence: event.confidence,
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
||||
// 新增:处理路径决策事件
|
||||
case 'path_decision':
|
||||
console.log(`🧭 路径决策: ${event.path}`);
|
||||
updateCurrentMessage({
|
||||
metadata: {
|
||||
...currentMessageRef.current?.metadata,
|
||||
path: event.path,
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
||||
case 'node_start':
|
||||
console.log('Node started:', event.node);
|
||||
break;
|
||||
@@ -268,5 +320,6 @@ export function useChat() {
|
||||
isLoading,
|
||||
sendMessage,
|
||||
handleReviewAction,
|
||||
lastIntent, // 新增:导出最后意图
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user