diff --git a/dotnet/src/dotnetcore/Providers/AI/Model/GXAgent.cs b/dotnet/src/dotnetcore/Providers/AI/Model/GXAgent.cs index a749cfee4..2b42611aa 100644 --- a/dotnet/src/dotnetcore/Providers/AI/Model/GXAgent.cs +++ b/dotnet/src/dotnetcore/Providers/AI/Model/GXAgent.cs @@ -42,8 +42,7 @@ protected string CallAgent(string assistant, GXProperties gxproperties, IList ch { GXLogging.Debug(log, "Calling Agent: ", assistant); - List chatMessagesList = chatMessages!=null ? chatMessages.Cast().ToList() :null; - ChatCompletionResult chatCompletion = AgentService.AgentHandlerInstance.CallAgent(assistant, chatMessagesList, gxproperties, context).GetAwaiter().GetResult(); + ChatCompletionResult chatCompletion = AgentService.AgentHandlerInstance.CallAgent(assistant, chatMessages, gxproperties, context).GetAwaiter().GetResult(); if (chatCompletion != null && chatCompletion.Choices != null) { @@ -54,8 +53,8 @@ protected string CallAgent(string assistant, GXProperties gxproperties, IList ch case ChatCompletionResult.FINISH_REASON_STOP: return choice.Message.Content; case ChatCompletionResult.FINISH_REASON_TOOL_CALLS: - chatMessagesList.Add(choice.Message); - return ProcessChatResponse(choice, stream, assistant, gxproperties, chatMessagesList, result); + chatMessages.Add(choice.Message); + return ProcessChatResponse(choice, stream, assistant, gxproperties, chatMessages, result); } } } diff --git a/dotnet/src/dotnetcore/Providers/AI/Services/AgentService.cs b/dotnet/src/dotnetcore/Providers/AI/Services/AgentService.cs index 2d13ca7b8..31ad65bb4 100644 --- a/dotnet/src/dotnetcore/Providers/AI/Services/AgentService.cs +++ b/dotnet/src/dotnetcore/Providers/AI/Services/AgentService.cs @@ -51,7 +51,7 @@ static string AddChatToUrl(string url) uriBuilder.Path += "chat"; return uriBuilder.Uri.ToString(); } - internal async Task CallAgent(string assistant, List messages, GXProperties properties, IGxContext context) + internal async Task CallAgent(string assistant, IList messages, GXProperties properties, IGxContext context) { try { diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GxCollections.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GxCollections.cs index 0e4ce9e3f..08eeb822d 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GxCollections.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GxCollections.cs @@ -2708,7 +2708,15 @@ public void AddObjectProperty(string name, object prop) } public string ToJSonString() { +#if NETCORE + string json = JSONHelper.DefaultSerialize(ExternalInstance); + if (json!=null) + return json; + else + return new JArray().ToString(); +#else return string.Empty; +#endif } public string ToJSonString(bool includeState) { diff --git a/dotnet/src/dotnetframework/GxClasses/Helpers/JSONHelper.cs b/dotnet/src/dotnetframework/GxClasses/Helpers/JSONHelper.cs index c47766597..9d99a5d4f 100644 --- a/dotnet/src/dotnetframework/GxClasses/Helpers/JSONHelper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Helpers/JSONHelper.cs @@ -128,6 +128,15 @@ internal override string WriteNullableJSON(Dictionary kbObject) }); } + internal override string DefaultSerialize(T value) + { + if (value != null) + { + return JsonSerializer.Serialize(value); + } + return null; + } + internal override bool IsJsonString(string jobject) { try @@ -229,6 +238,9 @@ internal static GXJsonSerializer Instance internal abstract string WriteJSON(T kbObject) where T : class; internal abstract string WriteNullableJSON(Dictionary kbObject); +#if NETCORE + internal abstract string DefaultSerialize(T value) where T : class; +#endif } public class JSONHelper @@ -328,7 +340,20 @@ public static string Serialize(T kbObject) where T : class { return Serialize(kbObject, Encoding.UTF8); } - +#if NETCORE + public static string DefaultSerialize(T value) where T : class + { + try + { + return GXJsonSerializer.Instance.DefaultSerialize(value); + } + catch (Exception ex) + { + GXLogging.Error(log, "DefaultSerialize error ", ex); + return null; + } + } +#endif public static string Serialize(T kbObject, Encoding encoding) where T : class { return Serialize(kbObject, encoding, null); diff --git a/dotnet/test/DotNetCoreUnitTest/Domain/ChatMessageTest.cs b/dotnet/test/DotNetCoreUnitTest/Domain/ChatMessageTest.cs new file mode 100644 index 000000000..da2173362 --- /dev/null +++ b/dotnet/test/DotNetCoreUnitTest/Domain/ChatMessageTest.cs @@ -0,0 +1,329 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Xml.Serialization; +using GeneXus.AI.Chat; +using GeneXus.Application; +using GeneXus.Utils; +using Xunit; + +namespace DotNetCoreUnitTest.Domain +{ + public class ChatMessageTest + { + [Fact] + public void ChatMessageCollectionTest() + { + GxContext gxContext = new GxContext(); + GXExternalCollection chatMessages = new GXExternalCollection(gxContext, "SdtChatMessage", "GeneXus.Programs"); + ChatMessage msg = new ChatMessage(); + msg.Role = "user"; + chatMessages.Add(msg); + Assert.NotEmpty(chatMessages.ToJSonString()); + } + + + } + [Serializable] + public class SdtChatMessage : GxUserType, IGxExternalObject + { + private static Hashtable mapper; + [XmlIgnore] + private static GXTypeInfo _typeProps; + protected ChatMessage GeneXus_ArtificialIntelligence_ChatMessage_externalReference; + + public SdtChatMessage() + { + } + + public SdtChatMessage(IGxContext context) + { + this.context = context; + this.initialize(); + } + + public override string JsonMap(string value) + { + if (SdtChatMessage.mapper == null) + SdtChatMessage.mapper = new Hashtable(); + return (string)SdtChatMessage.mapper[(object)value]; + } + + public string gxTpr_Role + { + get + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + return this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.Role; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.Role = value; + this.SetDirty("Role"); + } + } + + public string gxTpr_Content + { + get + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + return this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.Content; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.Content = value; + this.SetDirty("Content"); + } + } + + public GXExternalCollection gxTpr_Toolcalls + { + get + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + return new GXExternalCollection(this.context, "GeneXus.Core.genexus.artificialintelligence.SdtToolCall", "GeneXus.Core") + { + ExternalInstance = (IList)CollectionUtils.ConvertToInternal(typeof(List), (object)this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.ToolCalls) + }; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.ToolCalls = (List)CollectionUtils.ConvertToExternal(typeof(List), (object)value.ExternalInstance); + this.SetDirty("Toolcalls"); + } + } + + public string gxTpr_Toolcallid + { + get + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + return this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.ToolCallId; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference.ToolCallId = value; + this.SetDirty("Toolcallid"); + } + } + + public object ExternalInstance + { + get + { + if (this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference == null) + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = new ChatMessage(); + return (object)this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference; + } + set + { + this.GeneXus_ArtificialIntelligence_ChatMessage_externalReference = (ChatMessage)value; + } + } + + protected override GXTypeInfo TypeInfo + { + get => SdtChatMessage._typeProps; + set => SdtChatMessage._typeProps = value; + } + + public void initialize() + { + } + } + + [Serializable] + public class SdtToolCall : GxUserType, IGxExternalObject + { + private static Hashtable mapper; + [XmlIgnore] + private static GXTypeInfo _typeProps; + protected ToolCall GeneXus_ArtificialIntelligence_ToolCall_externalReference; + + public SdtToolCall() + { + } + + public SdtToolCall(IGxContext context) + { + this.context = context; + this.initialize(); + } + + public override string JsonMap(string value) + { + if (SdtToolCall.mapper == null) + SdtToolCall.mapper = new Hashtable(); + return (string)SdtToolCall.mapper[(object)value]; + } + + public string gxTpr_Id + { + get + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + return this.GeneXus_ArtificialIntelligence_ToolCall_externalReference.Id; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference.Id = value; + this.SetDirty("Id"); + } + } + + public string gxTpr_Type + { + get + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + return this.GeneXus_ArtificialIntelligence_ToolCall_externalReference.Type; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference.Type = value; + this.SetDirty("Type"); + } + } + + public SdtFunction gxTpr_Function + { + get + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + return new SdtFunction(this.context) + { + ExternalInstance = (object)this.GeneXus_ArtificialIntelligence_ToolCall_externalReference.Function + }; + } + set + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference.Function = (Function)value.ExternalInstance; + this.SetDirty("Function"); + } + } + + public object ExternalInstance + { + get + { + if (this.GeneXus_ArtificialIntelligence_ToolCall_externalReference == null) + this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = new ToolCall(); + return (object)this.GeneXus_ArtificialIntelligence_ToolCall_externalReference; + } + set => this.GeneXus_ArtificialIntelligence_ToolCall_externalReference = (ToolCall)value; + } + + protected override GXTypeInfo TypeInfo + { + get => SdtToolCall._typeProps; + set => SdtToolCall._typeProps = value; + } + + public void initialize() + { + } + } + [Serializable] + public class SdtFunction : GxUserType, IGxExternalObject + { + private static Hashtable mapper; + [XmlIgnore] + private static GXTypeInfo _typeProps; + protected Function GeneXus_ArtificialIntelligence_Function_externalReference; + + public SdtFunction() + { + } + + public SdtFunction(IGxContext context) + { + this.context = context; + this.initialize(); + } + + public override string JsonMap(string value) + { + if (SdtFunction.mapper == null) + SdtFunction.mapper = new Hashtable(); + return (string)SdtFunction.mapper[(object)value]; + } + + public string gxTpr_Name + { + get + { + if (this.GeneXus_ArtificialIntelligence_Function_externalReference == null) + this.GeneXus_ArtificialIntelligence_Function_externalReference = new Function(); + return this.GeneXus_ArtificialIntelligence_Function_externalReference.Name; + } + set + { + if (this.GeneXus_ArtificialIntelligence_Function_externalReference == null) + this.GeneXus_ArtificialIntelligence_Function_externalReference = new Function(); + this.GeneXus_ArtificialIntelligence_Function_externalReference.Name = value; + this.SetDirty("Name"); + } + } + + public string gxTpr_Arguments + { + get + { + if (this.GeneXus_ArtificialIntelligence_Function_externalReference == null) + this.GeneXus_ArtificialIntelligence_Function_externalReference = new Function(); + return this.GeneXus_ArtificialIntelligence_Function_externalReference.Arguments; + } + set + { + if (this.GeneXus_ArtificialIntelligence_Function_externalReference == null) + this.GeneXus_ArtificialIntelligence_Function_externalReference = new Function(); + this.GeneXus_ArtificialIntelligence_Function_externalReference.Arguments = value; + this.SetDirty("Arguments"); + } + } + + public object ExternalInstance + { + get + { + if (this.GeneXus_ArtificialIntelligence_Function_externalReference == null) + this.GeneXus_ArtificialIntelligence_Function_externalReference = new Function(); + return (object)this.GeneXus_ArtificialIntelligence_Function_externalReference; + } + set => this.GeneXus_ArtificialIntelligence_Function_externalReference = (Function)value; + } + + protected override GXTypeInfo TypeInfo + { + get => SdtFunction._typeProps; + set => SdtFunction._typeProps = value; + } + + public void initialize() + { + } + } + +}