agent功能开发增加MCP后端
This commit is contained in:
@@ -1549,13 +1549,23 @@ async def agent_chat(request: AgentChatRequest):
|
|||||||
logger.info(f"Agent chat: {request.message} (user: {request.user_id})")
|
logger.info(f"Agent chat: {request.message} (user: {request.user_id})")
|
||||||
|
|
||||||
# ==================== 权限检查 ====================
|
# ==================== 权限检查 ====================
|
||||||
# 仅允许 max 用户使用
|
# 仅允许 max 用户使用(支持多种格式:字符串 "max"、数字 1、或 nickname 为 "max")
|
||||||
if request.user_id != "max":
|
is_max_user = (
|
||||||
|
request.user_id == "max" or
|
||||||
|
request.user_id == "1" or
|
||||||
|
request.user_id == 1 or
|
||||||
|
request.user_nickname == "max"
|
||||||
|
)
|
||||||
|
|
||||||
|
if not is_max_user:
|
||||||
|
logger.warning(f"权限检查失败 - user_id: {request.user_id}, nickname: {request.user_nickname}")
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=403,
|
status_code=403,
|
||||||
detail="很抱歉,「价小前投研」功能目前仅对特定用户开放。如需使用,请联系管理员。"
|
detail="很抱歉,「价小前投研」功能目前仅对特定用户开放。如需使用,请联系管理员。"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger.info(f"权限检查通过 - user_id: {request.user_id}, nickname: {request.user_nickname}")
|
||||||
|
|
||||||
# ==================== 会话管理 ====================
|
# ==================== 会话管理 ====================
|
||||||
# 如果没有提供 session_id,创建新会话
|
# 如果没有提供 session_id,创建新会话
|
||||||
session_id = request.session_id or str(uuid.uuid4())
|
session_id = request.session_id or str(uuid.uuid4())
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ const AgentChatV3 = () => {
|
|||||||
setIsLoadingSessions(true);
|
setIsLoadingSessions(true);
|
||||||
try {
|
try {
|
||||||
const response = await axios.get('/mcp/agent/sessions', {
|
const response = await axios.get('/mcp/agent/sessions', {
|
||||||
params: { user_id: user.id, limit: 50 },
|
params: { user_id: String(user.id), limit: 50 },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.data.success) {
|
if (response.data.success) {
|
||||||
@@ -220,8 +220,16 @@ const AgentChatV3 = () => {
|
|||||||
const handleSendMessage = async () => {
|
const handleSendMessage = async () => {
|
||||||
if (!inputValue.trim() || isProcessing) return;
|
if (!inputValue.trim() || isProcessing) return;
|
||||||
|
|
||||||
// 权限检查
|
// 权限检查 - 检查 username 或 id
|
||||||
if (user?.id !== 'max') {
|
const isMaxUser = user?.username === 'max' || user?.id === 'max' || user?.id === 1;
|
||||||
|
|
||||||
|
if (!isMaxUser) {
|
||||||
|
logger.warn('AgentChat', '权限检查失败', {
|
||||||
|
userId: user?.id,
|
||||||
|
username: user?.username,
|
||||||
|
userObject: user
|
||||||
|
});
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: '权限不足',
|
title: '权限不足',
|
||||||
description: '「价小前投研」功能目前仅对特定用户开放。如需使用,请联系管理员。',
|
description: '「价小前投研」功能目前仅对特定用户开放。如需使用,请联系管理员。',
|
||||||
@@ -232,6 +240,8 @@ const AgentChatV3 = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info('AgentChat', '权限检查通过', { userId: user?.id, username: user?.username });
|
||||||
|
|
||||||
const userMessage = {
|
const userMessage = {
|
||||||
type: MessageTypes.USER,
|
type: MessageTypes.USER,
|
||||||
content: inputValue,
|
content: inputValue,
|
||||||
@@ -266,8 +276,8 @@ const AgentChatV3 = () => {
|
|||||||
isUser: m.type === MessageTypes.USER,
|
isUser: m.type === MessageTypes.USER,
|
||||||
content: m.content,
|
content: m.content,
|
||||||
})),
|
})),
|
||||||
user_id: user?.id || 'anonymous',
|
user_id: user?.id ? String(user.id) : 'anonymous',
|
||||||
user_nickname: user?.nickname || '匿名用户',
|
user_nickname: user?.nickname || user?.username || '匿名用户',
|
||||||
user_avatar: user?.avatar || '',
|
user_avatar: user?.avatar || '',
|
||||||
session_id: currentSessionId,
|
session_id: currentSessionId,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user