Skip to content

.NET: [Bug]: The MessageInjectingChatClient does not store the historical conversations of injected messages. #6056

@naruto1227

Description

@naruto1227

Description

The MessageInjectingChatClient failed to add the injected messages to the conversation history.

Code Sample

public class MessageInjectingTest
{
    public static async Task Exec()
    {
        var chatClient = OpenApiClientFactory.CreateClient();
       var agent= chatClient.AsAIAgent(new ChatClientAgentOptions()
        {
            EnableMessageInjection = true,
            ChatOptions = new ChatOptions()
            {
                Instructions = "You are a weather forecast specialist, skilled in analyzing weather conditions.",
                Tools = [AIFunctionFactory.Create(GetWeather)]
            }
        });
        var session=await agent.CreateSessionAsync(); 
        await foreach (var item in agent.RunStreamingAsync("获取北京的天气",session))
        {
            Console.Write(item.Text);
        }
    }

    [Description("获取天气预报的信息,输入城市,返回天气的详情")]
    private static string GetWeather([Description("城市的名称:例如上海、北京、杭州")]string city)
    {
        var currentRunContext = AIAgent.CurrentRunContext!;
        if (city=="北京")
        {
            var messageInjectingChatClient = currentRunContext.Agent.GetService<MessageInjectingChatClient>();
            messageInjectingChatClient!.EnqueueMessages(currentRunContext.Session!, [
               new ChatMessage(role:ChatRole.User, content: "我说错了,我想要查询上海的天气")
            ]);
        }

        return $"当前城市:{city}\n 天气:{Random.Shared.Next(20,30)}度 \n 气温:晴天";
    }
}

Error Messages / Stack Traces

request 1:

2026-05-24 15:13:32.878 +08:00 [DBG] Request [e37c03f9-9a49-4547-a81f-c1618b7030b7] content: {"messages":[{"role":"system","content":"您是一名天气预报专员,擅长分析天气情况"},{"role":"user","content":"获取北京的天气"},{"role":"assistant","content":"我来为您获取北京的天气预报信息。","tool_calls":[{"id":"tool-e5847c37a8f04159b8dae3ce0a65ed87","type":"function","function":{"name":"GetWeather","arguments":"{\n  \"city\": \"北京\"\n}"}}]},{"role":"tool","tool_call_id":"tool-e5847c37a8f04159b8dae3ce0a65ed87","content":"\"当前城市:北京\\n 天气:26度 \\n 气温:晴天\""},{"role":"user","content":"我说错了,我想要查询上海的天气"}],"model":"glm-4.7","stream":true,"stream_options":{"include_usage":true},"tools":[{"type":"function","function":{"description":"获取天气预报的信息,输入城市,返回天气的详情","name":"GetWeather","parameters":{
  "type": "object",
  "required": [
    "city"
  ],
  "properties": {
    "city": {
      "description": "\u57CE\u5E02\u7684\u540D\u79F0\uFF1A\u4F8B\u5982\u4E0A\u6D77\u3001\u5317\u4EAC\u3001\u676D\u5DDE",
      "type": "string"
    }
  },
  "additionalProperties": false
}}}],"tool_choice":"auto"}


request 2:

2026-05-24 15:13:35.110 +08:00 [DBG] Request [240d6b90-b928-450f-895c-3418ae707629] content: {"messages":[{"role":"system","content":"您是一名天气预报专员,擅长分析天气情况"},{"role":"user","content":"获取北京的天气"},{"role":"assistant","content":"我来为您获取北京的天气预报信息。","tool_calls":[{"id":"tool-e5847c37a8f04159b8dae3ce0a65ed87","type":"function","function":{"name":"GetWeather","arguments":"{\n  \"city\": \"北京\"\n}"}}]},{"role":"tool","tool_call_id":"tool-e5847c37a8f04159b8dae3ce0a65ed87","content":"\"当前城市:北京\\n 天气:26度 \\n 气温:晴天\""},{"role":"assistant","content":"","tool_calls":[{"id":"tool-72a59fa2f8cc4687a9b17efbc87d2231","type":"function","function":{"name":"GetWeather","arguments":"{\n  \"city\": \"上海\"\n}"}}]},{"role":"tool","tool_call_id":"tool-72a59fa2f8cc4687a9b17efbc87d2231","content":"\"当前城市:上海\\n 天气:22度 \\n 气温:晴天\""}],"model":"glm-4.7","stream":true,"stream_options":{"include_usage":true},"tools":[{"type":"function","function":{"description":"获取天气预报的信息,输入城市,返回天气的详情","name":"GetWeather","parameters":{
  "type": "object",
  "required": [
    "city"
  ],
  "properties": {
    "city": {
      "description": "\u57CE\u5E02\u7684\u540D\u79F0\uFF1A\u4F8B\u5982\u4E0A\u6D77\u3001\u5317\u4EAC\u3001\u676D\u5DDE",
      "type": "string"
    }
  },
  "additionalProperties": false
}}}],"tool_choice":"auto"}

Package Versions

1.0.6.1

.NET Version

.Net 10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions