完善警告和错误信息提示逻辑
This commit is contained in:
parent
7dc73e66eb
commit
805293e80f
@ -56,7 +56,7 @@ public class Gateway
|
||||
api_key = "sk-db177155677e438f832860e7f4da6afc"
|
||||
};
|
||||
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));
|
||||
messages.Add(new Message
|
||||
{
|
||||
@ -246,7 +246,7 @@ public class Gateway
|
||||
string toolInfos = "";
|
||||
try
|
||||
{
|
||||
toolInfos = await GetToolInfos(new McpServerList());
|
||||
toolInfos = await GetToolInfos(new McpServerList(),callback);
|
||||
messages.Add(new Message
|
||||
{
|
||||
Role = "system",
|
||||
@ -441,7 +441,16 @@ public class Gateway
|
||||
}catch (Exception e)
|
||||
{
|
||||
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 != "")
|
||||
{
|
||||
@ -697,10 +706,13 @@ public class Gateway
|
||||
};
|
||||
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();
|
||||
int i = 0;
|
||||
List<int> failedMcp = new List<int>();
|
||||
@ -805,7 +817,15 @@ public class Gateway
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@ -36,53 +36,53 @@ public class McpServerList
|
||||
// "有地理信息的相关案例步骤参考以及Arcgis Pro的工具详细信息",
|
||||
IsActive = true
|
||||
});
|
||||
//servers.Add("filesystem", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "filesystem",
|
||||
// Type = "stdio",
|
||||
// Command = "npx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "-y",
|
||||
// "@modelcontextprotocol/server-filesystem",
|
||||
// "F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb"
|
||||
// }
|
||||
//});
|
||||
//servers.Add("fetch", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "fetch",
|
||||
// Type = "stdio",
|
||||
// Command = "uvx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "mcp-server-fetch"
|
||||
// }
|
||||
//});
|
||||
//servers.Add("bing-search", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "bing-search",
|
||||
// Type = "stdio",
|
||||
// Command = "npx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "bing-cn-mcp"
|
||||
// }
|
||||
//});
|
||||
//servers.Add("mcp-python-interpreter", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "mcp-python-interpreter",
|
||||
// Type = "stdio",
|
||||
// Command = "uvx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "--native-tls",
|
||||
// "mcp-python-interpreter",
|
||||
// "--dir",
|
||||
// "F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb",
|
||||
// "--python-path",
|
||||
// "C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\custom\\python.exe"
|
||||
// }
|
||||
//});
|
||||
servers.Add("filesystem", new StdioMcpServer()
|
||||
{
|
||||
Name = "filesystem",
|
||||
Type = "stdio",
|
||||
Command = "npx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"-y",
|
||||
"@modelcontextprotocol/server-filesystem",
|
||||
"F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb"
|
||||
}
|
||||
});
|
||||
servers.Add("fetch", new StdioMcpServer()
|
||||
{
|
||||
Name = "fetch",
|
||||
Type = "stdio",
|
||||
Command = "uvx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"mcp-server-fetch"
|
||||
}
|
||||
});
|
||||
servers.Add("bing-search", new StdioMcpServer()
|
||||
{
|
||||
Name = "bing-search",
|
||||
Type = "stdio",
|
||||
Command = "npx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"bing-cn-mcp"
|
||||
}
|
||||
});
|
||||
servers.Add("mcp-python-interpreter", new StdioMcpServer()
|
||||
{
|
||||
Name = "mcp-python-interpreter",
|
||||
Type = "stdio",
|
||||
Command = "uvx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"--native-tls",
|
||||
"mcp-python-interpreter",
|
||||
"--dir",
|
||||
"F:\\secondsemester\\linktool\\test\\LinkTool0607\\LinkTool0607.gdb",
|
||||
"--python-path",
|
||||
"C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\custom\\python.exe"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public McpServer GetServer(string name)
|
||||
|
||||
@ -100,6 +100,7 @@ namespace LinkToolAddin.ui.dockpane
|
||||
|
||||
private void SendButton_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
StatusTextBlock.Text = "正在读取用户输入和工具列表";
|
||||
string question = QuestionTextbox.Text;
|
||||
string defaultGdbPath = Project.Current.DefaultGeodatabasePath;
|
||||
string gdbPath = @"";
|
||||
@ -223,7 +224,16 @@ namespace LinkToolAddin.ui.dockpane
|
||||
{
|
||||
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 = "正在读取用户输入";
|
||||
}
|
||||
}
|
||||
else
|
||||
else if(msg.role == "assistant")
|
||||
{
|
||||
if (msg.type == MessageType.REASON_MESSAGE)
|
||||
{
|
||||
@ -280,6 +290,22 @@ namespace LinkToolAddin.ui.dockpane
|
||||
textBox.Text = msg.content;
|
||||
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)
|
||||
@ -446,6 +472,66 @@ namespace LinkToolAddin.ui.dockpane
|
||||
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)
|
||||
{
|
||||
Border border = new Border();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user