Skip to content

Commit 797c8be

Browse files
Suppress assistant NO_REPLY chat directives
1 parent 87dc3dc commit 797c8be

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

‎src/OpenClaw.Shared/Models.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,12 @@ internal static string FormatLargeNumber(long n)
17671767
/// </summary>
17681768
public class ChatMessageInfo
17691769
{
1770+
public const string SilentAssistantDirective = "NO_REPLY";
1771+
1772+
public static bool IsSilentAssistantDirective(string? role, string? text) =>
1773+
string.Equals(role, "assistant", StringComparison.OrdinalIgnoreCase) &&
1774+
string.Equals(text?.Trim(), SilentAssistantDirective, StringComparison.Ordinal);
1775+
17701776
/// <summary>Session this message belongs to (e.g. "main").</summary>
17711777
public string SessionKey { get; set; } = "";
17721778

‎src/OpenClaw.Shared/OpenClawGatewayClient.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ private static ChatHistoryInfo ParseChatHistory(JsonElement payload, string sess
547547
string text = ExtractMessageText(m);
548548
if (string.IsNullOrEmpty(text)) continue;
549549
if (string.IsNullOrEmpty(role)) continue;
550+
if (ChatMessageInfo.IsSilentAssistantDirective(role, text)) continue;
550551

551552
var (inputTokens, outputTokens, responseTokens, contextPercent) = ExtractChatUsage(m);
552553
list.Add(new ChatMessageInfo
@@ -3068,6 +3069,7 @@ private void HandleChatEvent(JsonElement root, int rawMessageLength)
30683069

30693070
var text = ExtractMessageText(message);
30703071
if (string.IsNullOrEmpty(text)) return;
3072+
if (ChatMessageInfo.IsSilentAssistantDirective(role, text)) return;
30713073

30723074
EmitChatMessageReceived(sessionKey, role, text, state, tsMs, inTok, outTok, respTok, ctxPct);
30733075

@@ -3089,6 +3091,8 @@ private void HandleChatEvent(JsonElement root, int rawMessageLength)
30893091

30903092
if (!string.IsNullOrEmpty(text))
30913093
{
3094+
if (ChatMessageInfo.IsSilentAssistantDirective(role, text)) return;
3095+
30923096
EmitChatMessageReceived(sessionKey, role, text, state, tsMs, inTok, outTok, respTok, ctxPct);
30933097

30943098
if (role == "assistant")
@@ -3154,6 +3158,9 @@ JsonValueKind.Number when v.TryGetInt32(out var i) => i,
31543158
private void EmitChatMessageReceived(string sessionKey, string role, string text, string? state, long tsMs,
31553159
int? inputTokens = null, int? outputTokens = null, int? responseTokens = null, int? contextPct = null)
31563160
{
3161+
if (ChatMessageInfo.IsSilentAssistantDirective(role, text))
3162+
return;
3163+
31573164
try
31583165
{
31593166
ChatMessageReceived?.Invoke(this, new ChatMessageInfo

‎src/OpenClaw.Tray.WinUI/Chat/OpenClawChatDataProvider.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ ChatTimelineState ApplyAndCaptureMeta(ChatTimelineState s, ChatEvent e, ChatEntr
646646
break;
647647

648648
case "assistant":
649+
if (ChatMessageInfo.IsSilentAssistantDirective(roleLower, text))
650+
{
651+
Logger.Debug("[ChatHistory] → routed: SILENT assistant directive");
652+
break;
653+
}
654+
649655
// If this assistant response was aborted, show a placeholder
650656
// instead of the actual (partial) content.
651657
if (shouldMarkAborted)
@@ -1692,6 +1698,8 @@ private void OnChatMessageReceived(object? sender, ChatMessageInfo message)
16921698

16931699
if (roleLower != "assistant")
16941700
return;
1701+
if (ChatMessageInfo.IsSilentAssistantDirective(roleLower, message.Text))
1702+
return;
16951703
if (string.IsNullOrEmpty(message.Text))
16961704
return;
16971705

‎tests/OpenClaw.Shared.Tests/OpenClawGatewayClientTests.cs‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public void ProcessRawMessage(string json)
124124
method!.Invoke(_client, new object[] { json });
125125
}
126126

127+
public ChatHistoryInfo ParseChatHistoryPayload(string payloadJson, string sessionKey = "main")
128+
{
129+
using var document = JsonDocument.Parse(payloadJson);
130+
var method = typeof(OpenClawGatewayClient).GetMethod(
131+
"ParseChatHistory",
132+
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
133+
return (ChatHistoryInfo)method!.Invoke(null, new object[] { document.RootElement.Clone(), sessionKey })!;
134+
}
135+
127136
public long ExtractChatTimestampMs(string payloadJson)
128137
{
129138
using var document = JsonDocument.Parse(payloadJson);
@@ -845,6 +854,81 @@ public void ProcessRawMessage_SessionMessageWithContentBlocks_EmitsChatMessage()
845854
Assert.Equal(1781631280633, received.Ts);
846855
}
847856

857+
[Fact]
858+
public void ProcessRawMessage_SessionMessageAssistantNoReply_DropsFrame()
859+
{
860+
var helper = new GatewayClientTestHelper();
861+
ChatMessageInfo? received = null;
862+
OpenClawNotification? notification = null;
863+
helper.Client.ChatMessageReceived += (_, message) => received = message;
864+
helper.Client.NotificationReceived += (_, value) => notification = value;
865+
866+
helper.ProcessRawMessage("""
867+
{
868+
"type": "event",
869+
"event": "session.message",
870+
"payload": {
871+
"sessionKey": "main",
872+
"message": {
873+
"role": "assistant",
874+
"content": " NO_REPLY\n",
875+
"timestamp": 1781631280633
876+
},
877+
"state": "final"
878+
}
879+
}
880+
""");
881+
882+
Assert.Null(received);
883+
Assert.Null(notification);
884+
}
885+
886+
[Fact]
887+
public void ProcessRawMessage_SessionMessageUserNoReply_IsNotDropped()
888+
{
889+
var helper = new GatewayClientTestHelper();
890+
ChatMessageInfo? received = null;
891+
helper.Client.ChatMessageReceived += (_, message) => received = message;
892+
893+
helper.ProcessRawMessage("""
894+
{
895+
"type": "event",
896+
"event": "session.message",
897+
"payload": {
898+
"sessionKey": "main",
899+
"message": {
900+
"role": "user",
901+
"content": "NO_REPLY",
902+
"timestamp": 1781631280633
903+
},
904+
"state": "final"
905+
}
906+
}
907+
""");
908+
909+
Assert.NotNull(received);
910+
Assert.Equal("user", received!.Role);
911+
Assert.Equal("NO_REPLY", received.Text);
912+
}
913+
914+
[Fact]
915+
public void ParseChatHistoryPayload_AssistantNoReply_DropsTranscriptEntry()
916+
{
917+
var helper = new GatewayClientTestHelper();
918+
919+
var history = helper.ParseChatHistoryPayload("""
920+
{
921+
"messages": [
922+
{ "role": "user", "content": "before", "timestamp": 1 },
923+
{ "role": "assistant", "content": "NO_REPLY", "timestamp": 2 },
924+
{ "role": "assistant", "content": "visible reply", "timestamp": 3 }
925+
]
926+
}
927+
""");
928+
929+
Assert.Equal(["before", "visible reply"], history.Messages.Select(m => m.Text).ToArray());
930+
}
931+
848932
[Fact]
849933
public void ProcessRawMessage_SessionMessageWithMalformedMessage_DropsFrame()
850934
{

‎tests/OpenClaw.Tray.Tests/OpenClawChatDataProviderTests.cs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,28 @@ public async Task ChatMessageReceived_FinalAssistant_AppendsAssistantEntry()
237237
Assert.Contains(notifications, n => n.Kind == ChatProviderNotificationKind.TurnComplete);
238238
}
239239

240+
[Fact]
241+
public async Task ChatMessageReceived_AssistantNoReply_IsSuppressed()
242+
{
243+
var (bridge, provider, snapshots, notifications) = CreateProvider(new[] { MainSession() });
244+
await provider.LoadAsync();
245+
snapshots.Clear();
246+
notifications.Clear();
247+
248+
bridge.RaiseChat(new ChatMessageInfo
249+
{
250+
SessionKey = "main",
251+
Role = "assistant",
252+
Text = "NO_REPLY",
253+
State = "final"
254+
});
255+
256+
var timeline = (await provider.LoadAsync()).Timelines["main"];
257+
Assert.Empty(snapshots);
258+
Assert.Empty(timeline.Entries);
259+
Assert.DoesNotContain(notifications, n => n.Kind == ChatProviderNotificationKind.TurnComplete);
260+
}
261+
240262
[Fact]
241263
public async Task ChatMessageReceived_DeltaAssistant_AppendsAssistantWithoutEndingTurn()
242264
{
@@ -1491,6 +1513,29 @@ public async Task LoadHistoryAsync_FoldsTranscriptIntoTimeline()
14911513
Assert.False(timeline.TurnActive);
14921514
}
14931515

1516+
[Fact]
1517+
public async Task LoadHistoryAsync_AssistantNoReply_IsSuppressed()
1518+
{
1519+
var (bridge, provider, snapshots, _) = CreateProvider(new[] { MainSession() });
1520+
bridge.HistoryBehavior = _ => Task.FromResult(new ChatHistoryInfo
1521+
{
1522+
SessionKey = "main",
1523+
Messages = new[]
1524+
{
1525+
new ChatMessageInfo { Role = "user", Text = "Hi", State = "final" },
1526+
new ChatMessageInfo { Role = "assistant", Text = "NO_REPLY", State = "final" },
1527+
new ChatMessageInfo { Role = "assistant", Text = "Visible", State = "final" }
1528+
}
1529+
});
1530+
await provider.LoadAsync();
1531+
snapshots.Clear();
1532+
1533+
await provider.LoadHistoryAsync("main");
1534+
1535+
var timeline = snapshots[^1].Timelines["main"];
1536+
Assert.Equal(["Hi", "Visible"], timeline.Entries.Select(e => e.Text).ToArray());
1537+
}
1538+
14941539
[Fact]
14951540
public async Task LoadHistoryAsync_MultipleAssistantTurns_PreservesEachAsSeparateEntry()
14961541
{

0 commit comments

Comments
 (0)