完善警告和错误信息提示逻辑

This commit is contained in:
PeterZhong 2025-06-16 00:14:56 +08:00
parent 7dc73e66eb
commit 805293e80f
3 changed files with 160 additions and 54 deletions

View File

@ -56,7 +56,7 @@ public class Gateway
api_key = "sk-db177155677e438f832860e7f4da6afc" api_key = "sk-db177155677e438f832860e7f4da6afc"
}; };
List<Message> messages = new List<Message>(); List<Message> messages = new List<Message>();
string toolInfos = await GetToolInfos(new McpServerList()); string toolInfos = await GetToolInfos(new McpServerList(),callback);
log.Info(SystemPrompt.SysPrompt(gdbPath, toolInfos)); log.Info(SystemPrompt.SysPrompt(gdbPath, toolInfos));
messages.Add(new Message messages.Add(new Message
{ {
@ -246,7 +246,7 @@ public class Gateway
string toolInfos = ""; string toolInfos = "";
try try
{ {
toolInfos = await GetToolInfos(new McpServerList()); toolInfos = await GetToolInfos(new McpServerList(),callback);
messages.Add(new Message messages.Add(new Message
{ {
Role = "system", Role = "system",
@ -441,7 +441,16 @@ public class Gateway
}catch (Exception e) }catch (Exception e)
{ {
log.Error(e.Message); log.Error(e.Message);
MessageBox.Show(e.Message, "请求大模型出错"); MessageListItem endMessageListItem2 = new ChatMessageItem
{
type = MessageType.ERROR,
content = $"请求大模型出错:{e.Message}",
id = timestamp.ToString(),
role = "system"
};
callback?.Invoke(endMessageListItem2);
// MessageBox.Show(e.Message, "请求大模型出错");
break;
} }
if (messageContent != "") if (messageContent != "")
{ {
@ -697,10 +706,13 @@ public class Gateway
}; };
callback?.Invoke(endMessageListItem); callback?.Invoke(endMessageListItem);
} }
MessageBox.Show("本轮回复完成", "结束提示");
} }
private static async Task<string> GetToolInfos(McpServerList mcpServerList) private static async Task<string> GetToolInfos(McpServerList mcpServerList,Action<MessageListItem> callback)
{ {
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
StringBuilder toolInfos = new StringBuilder(); StringBuilder toolInfos = new StringBuilder();
int i = 0; int i = 0;
List<int> failedMcp = new List<int>(); List<int> failedMcp = new List<int>();
@ -805,7 +817,15 @@ public class Gateway
if (!failedMcp.IsNullOrEmpty()) if (!failedMcp.IsNullOrEmpty())
{ {
MessageBox.Show($"读取失败的MCP服务有{JsonConvert.SerializeObject(failedMcpString)}","MCP读取错误"); MessageListItem endMessageListItem2 = new ChatMessageItem
{
type = MessageType.WARNING,
content = $"读取失败的MCP服务有{JsonConvert.SerializeObject(failedMcpString)}",
id = timestamp.ToString(),
role = "system"
};
callback?.Invoke(endMessageListItem2);
// MessageBox.Show($"读取失败的MCP服务有{JsonConvert.SerializeObject(failedMcpString)}","MCP读取错误");
} }
return toolInfos.ToString(); return toolInfos.ToString();
} }

View File

@ -36,53 +36,53 @@ public class McpServerList
// "有地理信息的相关案例步骤参考以及Arcgis Pro的工具详细信息", // "有地理信息的相关案例步骤参考以及Arcgis Pro的工具详细信息",
IsActive = true IsActive = true
}); });
//servers.Add("filesystem", new StdioMcpServer() servers.Add("filesystem", new StdioMcpServer()
//{ {
// Name = "filesystem", Name = "filesystem",
// Type = "stdio", Type = "stdio",
// Command = "npx", Command = "npx",
// Args = new List<string>() Args = new List<string>()
// { {
// "-y", "-y",
// "@modelcontextprotocol/server-filesystem", "@modelcontextprotocol/server-filesystem",
// "F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb" "F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb"
// } }
//}); });
//servers.Add("fetch", new StdioMcpServer() servers.Add("fetch", new StdioMcpServer()
//{ {
// Name = "fetch", Name = "fetch",
// Type = "stdio", Type = "stdio",
// Command = "uvx", Command = "uvx",
// Args = new List<string>() Args = new List<string>()
// { {
// "mcp-server-fetch" "mcp-server-fetch"
// } }
//}); });
//servers.Add("bing-search", new StdioMcpServer() servers.Add("bing-search", new StdioMcpServer()
//{ {
// Name = "bing-search", Name = "bing-search",
// Type = "stdio", Type = "stdio",
// Command = "npx", Command = "npx",
// Args = new List<string>() Args = new List<string>()
// { {
// "bing-cn-mcp" "bing-cn-mcp"
// } }
//}); });
//servers.Add("mcp-python-interpreter", new StdioMcpServer() servers.Add("mcp-python-interpreter", new StdioMcpServer()
//{ {
// Name = "mcp-python-interpreter", Name = "mcp-python-interpreter",
// Type = "stdio", Type = "stdio",
// Command = "uvx", Command = "uvx",
// Args = new List<string>() Args = new List<string>()
// { {
// "--native-tls", "--native-tls",
// "mcp-python-interpreter", "mcp-python-interpreter",
// "--dir", "--dir",
// "F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb", "F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb",
// "--python-path", "--python-path",
// "C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\custom\\python.exe" "C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\custom\\python.exe"
// } }
//}); });
} }
public McpServer GetServer(string name) public McpServer GetServer(string name)

View File

@ -100,6 +100,7 @@ namespace LinkToolAddin.ui.dockpane
private void SendButton_OnClick(object sender, RoutedEventArgs e) private void SendButton_OnClick(object sender, RoutedEventArgs e)
{ {
StatusTextBlock.Text = "正在读取用户输入和工具列表";
string question = QuestionTextbox.Text; string question = QuestionTextbox.Text;
string defaultGdbPath = Project.Current.DefaultGeodatabasePath; string defaultGdbPath = Project.Current.DefaultGeodatabasePath;
string gdbPath = @""; string gdbPath = @"";
@ -223,7 +224,16 @@ namespace LinkToolAddin.ui.dockpane
{ {
if (msg.type == MessageType.WARNING) if (msg.type == MessageType.WARNING)
{ {
Border border = GetWarningChatBorder(msg);
borderItemsDict[msgId] = border;
ChatHistoryStackPanel.Children.Add(border);
StatusTextBlock.Text = "出现警告信息";
}else if (msg.type == MessageType.ERROR)
{
Border border = GetWarningChatBorder(msg);
borderItemsDict[msgId] = border;
ChatHistoryStackPanel.Children.Add(border);
StatusTextBlock.Text = "出现错误信息";
} }
} }
} }
@ -263,7 +273,7 @@ namespace LinkToolAddin.ui.dockpane
StatusTextBlock.Text = "正在读取用户输入"; StatusTextBlock.Text = "正在读取用户输入";
} }
} }
else else if(msg.role == "assistant")
{ {
if (msg.type == MessageType.REASON_MESSAGE) if (msg.type == MessageType.REASON_MESSAGE)
{ {
@ -280,6 +290,22 @@ namespace LinkToolAddin.ui.dockpane
textBox.Text = msg.content; textBox.Text = msg.content;
StatusTextBlock.Text = "回答生成中"; StatusTextBlock.Text = "回答生成中";
} }
}else if (msg.role == "system")
{
if (msg.type == MessageType.WARNING)
{
Border borderItem = borderItemsDict[msgId];
Grid grid = borderItem.Child as Grid;
TextBox textBox = grid.Children[1] as TextBox;
textBox.Text = msg.content;
StatusTextBlock.Text = "出现警告信息";
}else if (msg.type == MessageType.ERROR)
{
Border borderItem = borderItemsDict[msgId];
Grid grid = borderItem.Child as Grid;
TextBox textBox = grid.Children[1] as TextBox;
textBox.Text = msg.content;
}
} }
} }
if (Math.Abs(verticalOffset + viewportHeight - contentHeight) < tolerance) if (Math.Abs(verticalOffset + viewportHeight - contentHeight) < tolerance)
@ -446,6 +472,66 @@ namespace LinkToolAddin.ui.dockpane
return border; return border;
} }
private Border GetWarningChatBorder(MessageListItem msg)
{
Border border = new Border();
border.Margin = new Thickness(24);
border.Padding = new Thickness(8);
border.BorderThickness = new Thickness(1);
border.BorderBrush = Brushes.DarkGoldenrod;
border.CornerRadius = new CornerRadius(3);
border.Background = Brushes.Moccasin;
Grid grid = new Grid();
// Image icon = new Image()
// {
// Source = LocalResource.ReadImageByResource("LinkToolAddin.resource.img.tool.png"),
// Stretch = Stretch.Fill,
// HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center
// };
// icon.Margin = new Thickness(0, 0, 8, 0);
// grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(24, GridUnitType.Pixel)});
grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)});
TextBlock textBlock = new TextBlock();
textBlock.Text = (msg as ChatMessageItem).content;
textBlock.TextWrapping = TextWrapping.Wrap;
// grid.Children.Add(icon);
grid.Children.Add(textBlock);
// Grid.SetColumn(icon, 0);
Grid.SetColumn(textBlock, 1);
border.Child = grid;
return border;
}
private Border GetErrorChatBorder(MessageListItem msg)
{
Border border = new Border();
border.Margin = new Thickness(24);
border.Padding = new Thickness(8);
border.BorderThickness = new Thickness(1);
border.BorderBrush = Brushes.Crimson;
border.CornerRadius = new CornerRadius(3);
border.Background = Brushes.LightPink;
Grid grid = new Grid();
// Image icon = new Image()
// {
// Source = LocalResource.ReadImageByResource("LinkToolAddin.resource.img.tool.png"),
// Stretch = Stretch.Fill,
// HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center
// };
// icon.Margin = new Thickness(0, 0, 8, 0);
grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(24, GridUnitType.Pixel)});
grid.ColumnDefinitions.Add(new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)});
TextBlock textBlock = new TextBlock();
textBlock.Text = (msg as ChatMessageItem).content;
textBlock.TextWrapping = TextWrapping.Wrap;
// grid.Children.Add(icon);
grid.Children.Add(textBlock);
// Grid.SetColumn(icon, 0);
Grid.SetColumn(textBlock, 1);
border.Child = grid;
return border;
}
private Border GetToolChatBorder(MessageListItem msg) private Border GetToolChatBorder(MessageListItem msg)
{ {
Border border = new Border(); Border border = new Border();