修复ArcGIS Pro工具调用传参类型错误问题
This commit is contained in:
parent
c2c65e5a35
commit
4bbc743ef2
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ArcGIS.Core.Data;
|
using ArcGIS.Core.Data;
|
||||||
using ArcGIS.Core.Data.Raster;
|
using ArcGIS.Core.Data.Raster;
|
||||||
@ -9,6 +10,7 @@ using ArcGIS.Desktop.Framework.Threading.Tasks;
|
|||||||
using LinkToolAddin.server;
|
using LinkToolAddin.server;
|
||||||
using ModelContextProtocol.Server;
|
using ModelContextProtocol.Server;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace LinkToolAddin.client.tool;
|
namespace LinkToolAddin.client.tool;
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,7 @@ using System.Text;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Documents;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using ArcGIS.Desktop.Framework.Dialogs;
|
using ArcGIS.Desktop.Framework.Dialogs;
|
||||||
@ -37,6 +38,13 @@ namespace LinkToolAddin.host;
|
|||||||
public class Gateway
|
public class Gateway
|
||||||
{
|
{
|
||||||
private static ILog log = LogManager.GetLogger(typeof(Gateway));
|
private static ILog log = LogManager.GetLogger(typeof(Gateway));
|
||||||
|
private static bool goOn = true;
|
||||||
|
|
||||||
|
public static void StopConversation()
|
||||||
|
{
|
||||||
|
goOn = false;
|
||||||
|
}
|
||||||
|
|
||||||
public static async void SendMessage(string message, string model, string gdbPath, Action<MessageListItem> callback)
|
public static async void SendMessage(string message, string model, string gdbPath, Action<MessageListItem> callback)
|
||||||
{
|
{
|
||||||
Llm bailian = new Bailian
|
Llm bailian = new Bailian
|
||||||
@ -240,12 +248,19 @@ public class Gateway
|
|||||||
Role = "user",
|
Role = "user",
|
||||||
Content = message
|
Content = message
|
||||||
});
|
});
|
||||||
bool goOn = true;
|
goOn = true;
|
||||||
string toolPattern = "^<tool_use>[\\s\\S]*?<\\/tool_use>$";
|
string toolPattern = "^<tool_use>[\\s\\S]*?<\\/tool_use>$";
|
||||||
string promptPattern = "^<prompt>[\\s\\S]*?<\\/prompt>$";
|
string promptPattern = "^<prompt>[\\s\\S]*?<\\/prompt>$";
|
||||||
McpServerList mcpServerList = new McpServerList();
|
McpServerList mcpServerList = new McpServerList();
|
||||||
|
int loop = 0;
|
||||||
while (goOn)
|
while (goOn)
|
||||||
{
|
{
|
||||||
|
loop++;
|
||||||
|
if (loop > 20)
|
||||||
|
{
|
||||||
|
MessageBox.Show("达到最大循环次数", "退出循环");
|
||||||
|
break;
|
||||||
|
}
|
||||||
LlmJsonContent jsonContent = new LlmJsonContent()
|
LlmJsonContent jsonContent = new LlmJsonContent()
|
||||||
{
|
{
|
||||||
Model = model,
|
Model = model,
|
||||||
@ -266,6 +281,11 @@ public class Gateway
|
|||||||
if (Regex.IsMatch(chunk, toolPattern))
|
if (Regex.IsMatch(chunk, toolPattern))
|
||||||
{
|
{
|
||||||
//返回工具卡片
|
//返回工具卡片
|
||||||
|
messages.Add(new Message
|
||||||
|
{
|
||||||
|
Role = "assistant",
|
||||||
|
Content = chunk
|
||||||
|
});
|
||||||
XElement toolUse = XElement.Parse(chunk);
|
XElement toolUse = XElement.Parse(chunk);
|
||||||
string fullToolName = toolUse.Element("name")?.Value;
|
string fullToolName = toolUse.Element("name")?.Value;
|
||||||
string toolArgs = toolUse.Element("arguments")?.Value;
|
string toolArgs = toolUse.Element("arguments")?.Value;
|
||||||
@ -290,13 +310,14 @@ public class Gateway
|
|||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePromptTemplate
|
// Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePromptTemplate
|
||||||
});
|
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
messages.Add(new Message
|
|
||||||
{
|
|
||||||
Role = "user",
|
|
||||||
Content = JsonConvert.SerializeObject(toolResponse)
|
|
||||||
});
|
});
|
||||||
|
// messages.Add(new Message
|
||||||
|
// {
|
||||||
|
// Role = "user",
|
||||||
|
// Content = JsonConvert.SerializeObject(toolResponse)
|
||||||
|
// });
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (mcpServer is StdioMcpServer)
|
}else if (mcpServer is StdioMcpServer)
|
||||||
{
|
{
|
||||||
@ -315,19 +336,35 @@ public class Gateway
|
|||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePromptTemplate
|
// Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePromptTemplate
|
||||||
});
|
Content = toolResponse.IsError ? SystemPrompt.ErrorPromptTemplate : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
messages.Add(new Message
|
|
||||||
{
|
|
||||||
Role = "user",
|
|
||||||
Content = JsonConvert.SerializeObject(toolResponse)
|
|
||||||
});
|
});
|
||||||
|
// messages.Add(new Message
|
||||||
|
// {
|
||||||
|
// Role = "user",
|
||||||
|
// Content = JsonConvert.SerializeObject(toolResponse)
|
||||||
|
// });
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (mcpServer is InnerMcpServer)
|
}else if (mcpServer is InnerMcpServer)
|
||||||
{
|
{
|
||||||
Type type = Type.GetType("LinkToolAddin.client.tool."+serverName);
|
Type type = Type.GetType("LinkToolAddin.client.tool."+serverName);
|
||||||
MethodInfo method = type.GetMethod(toolName,BindingFlags.Public | BindingFlags.Static);
|
MethodInfo method = type.GetMethod(toolName,BindingFlags.Public | BindingFlags.Static);
|
||||||
var task = method.Invoke(null, toolParams.Values.ToArray()) as Task<JsonRpcResultEntity>;
|
var methodParams = toolParams.Values.ToArray();
|
||||||
|
object[] args = new object[methodParams.Length];
|
||||||
|
for (int i = 0; i < methodParams.Length; i++)
|
||||||
|
{
|
||||||
|
if (methodParams[i].GetType() == typeof(JArray))
|
||||||
|
{
|
||||||
|
List<string> list = new List<string>();
|
||||||
|
list = (methodParams[i] as JArray).Select(token => token.ToString()).ToList();
|
||||||
|
args[i] = list;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
args[i] = methodParams[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var task = method.Invoke(null, args) as Task<JsonRpcResultEntity>;
|
||||||
JsonRpcResultEntity innerResult = await task;
|
JsonRpcResultEntity innerResult = await task;
|
||||||
if (innerResult is JsonRpcErrorEntity)
|
if (innerResult is JsonRpcErrorEntity)
|
||||||
{
|
{
|
||||||
@ -365,13 +402,14 @@ public class Gateway
|
|||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
Content = SystemPrompt.ContinuePromptTemplate
|
// Content = SystemPrompt.ContinuePromptTemplate
|
||||||
});
|
Content = SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(innerResult))
|
||||||
messages.Add(new Message
|
|
||||||
{
|
|
||||||
Role = "user",
|
|
||||||
Content = JsonConvert.SerializeObject(innerResult)
|
|
||||||
});
|
});
|
||||||
|
// messages.Add(new Message
|
||||||
|
// {
|
||||||
|
// Role = "user",
|
||||||
|
// Content = JsonConvert.SerializeObject(innerResult)
|
||||||
|
// });
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -451,16 +489,9 @@ public class Gateway
|
|||||||
|
|
||||||
private static async Task<string> GetToolInfos(McpServerList mcpServerList)
|
private static async Task<string> GetToolInfos(McpServerList mcpServerList)
|
||||||
{
|
{
|
||||||
int loop = 0;
|
|
||||||
StringBuilder toolInfos = new StringBuilder();
|
StringBuilder toolInfos = new StringBuilder();
|
||||||
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
||||||
{
|
{
|
||||||
loop++;
|
|
||||||
if (loop > 3)
|
|
||||||
{
|
|
||||||
MessageBox.Show("达到最大循环次数", "退出循环");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (mcpServer is InnerMcpServer)
|
if (mcpServer is InnerMcpServer)
|
||||||
{
|
{
|
||||||
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
||||||
|
|||||||
@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
|||||||
using ArcGIS.Desktop.Core.Geoprocessing;
|
using ArcGIS.Desktop.Core.Geoprocessing;
|
||||||
using ArcGIS.Desktop.Framework.Dialogs;
|
using ArcGIS.Desktop.Framework.Dialogs;
|
||||||
using ArcGIS.Desktop.Framework.Threading.Tasks;
|
using ArcGIS.Desktop.Framework.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace LinkToolAddin.server;
|
namespace LinkToolAddin.server;
|
||||||
|
|
||||||
@ -22,7 +23,7 @@ public class CallArcGISPro
|
|||||||
Error = new Error()
|
Error = new Error()
|
||||||
{
|
{
|
||||||
Code = results.ErrorCode,
|
Code = results.ErrorCode,
|
||||||
Message = results.ErrorMessages.ToString()
|
Message = JsonConvert.SerializeObject(results.ErrorMessages)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -30,7 +31,7 @@ public class CallArcGISPro
|
|||||||
{
|
{
|
||||||
jsonRpcResultEntity = new JsonRpcSuccessEntity
|
jsonRpcResultEntity = new JsonRpcSuccessEntity
|
||||||
{
|
{
|
||||||
Result = results.Messages.ToString()
|
Result = JsonConvert.SerializeObject(results.Messages)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return jsonRpcResultEntity;
|
return jsonRpcResultEntity;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user