增强异常捕获能力,减少闪退
This commit is contained in:
parent
c61938383b
commit
660ba2ad53
@ -239,7 +239,10 @@ 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 = "";
|
||||||
|
try
|
||||||
|
{
|
||||||
|
toolInfos = await GetToolInfos(new McpServerList());
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "system",
|
Role = "system",
|
||||||
@ -250,6 +253,11 @@ public class Gateway
|
|||||||
Role = "user",
|
Role = "user",
|
||||||
Content = message
|
Content = message
|
||||||
});
|
});
|
||||||
|
}catch (Exception ex)
|
||||||
|
{
|
||||||
|
log.Error(ex);
|
||||||
|
MessageBox.Show(ex.Message,"获取MCP列表失败");
|
||||||
|
}
|
||||||
goOn = true;
|
goOn = true;
|
||||||
string toolPattern = "<tool_use>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/tool_use>";
|
string toolPattern = "<tool_use>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/tool_use>";
|
||||||
string promptPattern = "<prompt>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/prompt>";
|
string promptPattern = "<prompt>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/prompt>";
|
||||||
@ -286,12 +294,15 @@ public class Gateway
|
|||||||
{
|
{
|
||||||
type = MessageType.END_TAG,
|
type = MessageType.END_TAG,
|
||||||
content = "",
|
content = "",
|
||||||
id = (timestamp+3).ToString(),
|
id = (timestamp + 3).ToString(),
|
||||||
role = "assistant"
|
role = "assistant"
|
||||||
};
|
};
|
||||||
callback?.Invoke(endMessageListItem1);
|
callback?.Invoke(endMessageListItem1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await foreach(LlmStreamChat llmStreamChat in bailian.SendChatStreamAsync(jsonContent))
|
await foreach(LlmStreamChat llmStreamChat in bailian.SendChatStreamAsync(jsonContent))
|
||||||
{
|
{
|
||||||
if (!goOn)
|
if (!goOn)
|
||||||
@ -418,6 +429,12 @@ public class Gateway
|
|||||||
log.Error(e.Message);
|
log.Error(e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}catch (Exception e)
|
||||||
|
{
|
||||||
|
log.Error(e.Message);
|
||||||
|
MessageBox.Show(e.Message, "请求大模型出错");
|
||||||
|
}
|
||||||
|
|
||||||
if (messageContent != "")
|
if (messageContent != "")
|
||||||
{
|
{
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
@ -616,7 +633,8 @@ public class Gateway
|
|||||||
StringBuilder toolInfos = new StringBuilder();
|
StringBuilder toolInfos = new StringBuilder();
|
||||||
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
||||||
{
|
{
|
||||||
log.Info($"正在列出{mcpServer.Name}中的工具");
|
try
|
||||||
|
{
|
||||||
if (mcpServer is InnerMcpServer)
|
if (mcpServer is InnerMcpServer)
|
||||||
{
|
{
|
||||||
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
||||||
@ -687,15 +705,26 @@ public class Gateway
|
|||||||
toolInfos.AppendLine();
|
toolInfos.AppendLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}catch (Exception e)
|
||||||
|
{
|
||||||
|
log.Error(e.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<UserPrompt> prompts = DynamicPrompt.GetAllPrompts();
|
List<UserPrompt> prompts = DynamicPrompt.GetAllPrompts();
|
||||||
foreach (UserPrompt userPrompt in prompts)
|
foreach (UserPrompt userPrompt in prompts)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
XNode node = JsonConvert.DeserializeXNode(JsonConvert.SerializeObject(new PromptDefinition(){UserPrompt = userPrompt}));
|
XNode node = JsonConvert.DeserializeXNode(JsonConvert.SerializeObject(new PromptDefinition(){UserPrompt = userPrompt}));
|
||||||
toolInfos.AppendLine(node.ToString());
|
toolInfos.AppendLine(node.ToString());
|
||||||
toolInfos.AppendLine();
|
toolInfos.AppendLine();
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
log.Error(e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
return toolInfos.ToString();
|
return toolInfos.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -35,53 +35,53 @@ public class McpServerList
|
|||||||
Description = "可以调用进行查询知识库,获取相关参考信息。",
|
Description = "可以调用进行查询知识库,获取相关参考信息。",
|
||||||
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 = "npxh",
|
||||||
// Args = new List<string>()
|
Args = new List<string>()
|
||||||
// {
|
{
|
||||||
// "-y",
|
"-y",
|
||||||
// "@modelcontextprotocol/server-filesystem",
|
"@modelcontextprotocol/server-filesystem",
|
||||||
// "D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData"
|
"D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData"
|
||||||
// }
|
}
|
||||||
//});
|
});
|
||||||
//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",
|
||||||
// "D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData",
|
"D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData",
|
||||||
// "--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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user