From 65c011490fabe99fc0311fbf3a85ed9d7679b020 Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Wed, 18 Nov 2026 13:17:50 +0800
Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BAAgent=E5=AE=9E=E9=99=85?=
=?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=AF=8D=E5=B9=B6=E8=87=AA=E5=8A=A8=E5=8E=8B?=
=?UTF-8?q?=E7=BC=A9=E8=A7=86=E9=A2=91=E4=B8=8E=E8=81=8A=E5=A4=A9=E4=B8=8A?=
=?UTF-8?q?=E4=B8=8B=E6=96=87?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
聊天按轮次展示系统规则、输入、工具结果和真实回测反馈,隐藏图片Base64及鉴权信息。接近预算时释放历史图片并替换已过期草稿,长对话分批摘要并保留最近原文、当前草稿及最新要求,取消最近40条的静默截断。摘要失败、截断和取消时停止请求并保留原记录。
验证:SopVideoAgent.Smoke通过,覆盖上下文压缩、摘要成功/失败/取消、日志脱敏及原有生成回测闭环;VideoFlowTest.Smoke通过,覆盖提示词折叠显示和生成后日志保留。使用隔离构建目录,未重启用户程序。
---
.../VVMs/RecipeEditor/RecipeEditor.axaml | 8 +-
.../RecipeEditor/SopVideoAgentViewModel.cs | 32 ++++---
.../SopAgent/SopAgentContext.cs | 87 +++++++++++++++++++
.../SopAgent/SopVideoAgentClient.cs | 78 ++++++++++++++++-
.../SopAgent/SopVideoDraftClient.cs | 4 +-
.../ContextCompressionChecks.cs | 80 +++++++++++++++++
tests/SopVideoAgent.Smoke/Program.cs | 1 +
.../AgentDraftVerificationChecks.cs | 4 +
8 files changed, 279 insertions(+), 15 deletions(-)
create mode 100644 src/SopHelper.CrossPlatform.Engine/SopAgent/SopAgentContext.cs
create mode 100644 tests/SopVideoAgent.Smoke/ContextCompressionChecks.cs
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
index e4075fc..87f4521 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
@@ -571,7 +571,13 @@
-
+
+
+
+
+
+
+
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs
index 8b7cb26..dbd1e8e 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs
@@ -17,6 +17,7 @@ public partial class RecipeEditorViewModel
private static readonly HttpClient AgentHttp = new() { Timeout = Timeout.InfiniteTimeSpan, MaxResponseContentBufferSize = 1024 * 1024 };
public ISopVideoDraftClient AgentClient { get; set; } = new SopVideoAgentClient(AgentHttp);
private CancellationTokenSource? _agentCancellation;
+ private object? _agentPromptSession;
private Task? _agentTask;
private SopVideoSample? _agentSample;
private bool _isAgentBusy, _draftApplied;
@@ -231,6 +232,14 @@ public partial class RecipeEditorViewModel
IsAgentBusy = true;
AgentStatus = "正在读取标准 SOP 视频…";
var activeAnalysis = _agentCancellation;
+ var promptSession = _agentPromptSession = new object();
+ var promptLog = new Progress(prompt =>
+ {
+ if (_agentClosed || !ReferenceEquals(_agentPromptSession, promptSession)) return;
+ AgentChatMessages.Add(new("发送给 Agent 的提示词", prompt.Text, true,
+ (prompt.Round == 0 ? "历史摘要请求" : $"第 {prompt.Round} 轮") + $" · 估算输入 {prompt.EstimatedTokens:N0} / 上下文 {prompt.ContextLength:N0} tokens"
+ + (string.IsNullOrWhiteSpace(prompt.Compression) ? "" : "\n" + prompt.Compression)));
+ });
var progress = new Progress(value => { if (IsAgentBusy && ReferenceEquals(_agentCancellation, activeAnalysis)) AgentStatus = value; });
var verificationTemplate = CreateAgentVerificationTemplate();
Func>? verify = AgentAutoVerify
@@ -248,8 +257,8 @@ public partial class RecipeEditorViewModel
AgentChatInput = "";
AgentChatMessages.Add(new("我", userMessage));
var result = await AgentClient.ChatAsync(new(recipeName, instructions, sample, regions, maxFrames, progress,
- userMessage, _agentConversation.TakeLast(40).ToArray(), CreateAgentDraftSnapshot(), regionDefinitions,
- CreateAgentImageScopes(), VerifyDraft: verify), connection, token).WaitAsync(token);
+ userMessage, _agentConversation.ToArray(), CreateAgentDraftSnapshot(), regionDefinitions,
+ CreateAgentImageScopes(), VerifyDraft: verify, PromptSent: promptLog), connection, token).WaitAsync(token);
token.ThrowIfCancellationRequested();
draft = result.Draft; reply = result.Reply;
sample = sample with { Frames = result.ObservedFrames };
@@ -260,7 +269,7 @@ public partial class RecipeEditorViewModel
else
{
draft = await AgentClient.AnalyzeAsync(new(recipeName, instructions, sample, regions, maxFrames, progress,
- RegionDefinitions: regionDefinitions, VerifyDraft: verify), connection, token).WaitAsync(token);
+ RegionDefinitions: regionDefinitions, VerifyDraft: verify, PromptSent: promptLog), connection, token).WaitAsync(token);
sample = sample with { Frames = draft.ObservedFrames };
reply = string.IsNullOrWhiteSpace(draft.Summary) ? $"已生成 {draft.Steps.Count} 步。" : draft.Summary;
}
@@ -271,7 +280,7 @@ public partial class RecipeEditorViewModel
if (verify is not null && draft.Verification is null) draft.Verification = await verify(draft, sample, token);
token.ThrowIfCancellationRequested();
token.ThrowIfCancellationRequested();
- ClearAgentDraft(clearConversation: userMessage is null, forgetApplied: userMessage is null);
+ ClearAgentDraft(clearConversation: false, forgetApplied: userMessage is null);
for (var i = 0; i < draft.Steps.Count; i++)
{
var step = draft.Steps[i];
@@ -287,18 +296,18 @@ public partial class RecipeEditorViewModel
}
else AgentStatus = "";
_agentSample = sample;
- if (userMessage is null) _agentConversation.Add(new("assistant", reply));
+ if (userMessage is null) { _agentConversation.Clear(); _agentConversation.Add(new("assistant", reply)); }
AgentChatMessages.Add(new("Agent", reply));
}
catch (OperationCanceledException)
{
await WaitForAgentVerificationCleanupAsync();
AgentStatus = "分析已取消或超时,当前配方未修改。";
- if (userMessage is not null) AgentChatMessages.Add(new("系统", "已取消或超时。"));
+ AgentChatMessages.Add(new("系统", "已取消或超时。"));
}
catch (Exception error) when (error is ArgumentException or InvalidDataException or FileNotFoundException or FormatException)
- { AgentStatus = "分析未完成:" + error.Message; if (userMessage is not null) AgentChatMessages.Add(new("系统", AgentStatus)); }
- catch (Exception) { AgentStatus = "分析失败,请检查视频、接口、模型名和密钥;当前配方未修改。"; if (userMessage is not null) AgentChatMessages.Add(new("系统", AgentStatus)); }
+ { AgentStatus = "分析未完成:" + error.Message; AgentChatMessages.Add(new("系统", AgentStatus)); }
+ catch (Exception) { AgentStatus = "分析失败,请检查视频、接口、模型名和密钥;当前配方未修改。"; AgentChatMessages.Add(new("系统", AgentStatus)); }
finally
{
_agentCancellation.Dispose(); _agentCancellation = null;
@@ -380,7 +389,7 @@ public partial class RecipeEditorViewModel
existing?.AiHandPadding ?? 160, existing?.AiHandMinSize ?? 320, existing?.AiHandMemoryMs ?? 3000);
}).ToArray();
- private void ClearAgentConversation() { _agentConversation.Clear(); AgentChatMessages.Clear(); }
+ private void ClearAgentConversation() { _agentPromptSession = null; _agentConversation.Clear(); AgentChatMessages.Clear(); }
private void ClearAgentDraft(bool clearConversation = true, bool forgetApplied = true)
{
@@ -399,7 +408,10 @@ public partial class RecipeEditorViewModel
}
}
-public sealed record AgentChatLine(string Speaker, string Text);
+public sealed record AgentChatLine(string Speaker, string Text, bool IsPrompt = false, string Summary = "")
+{
+ public bool IsRegular => !IsPrompt;
+}
public sealed class AgentDraftRow : BindableBase, IDisposable
{
diff --git a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopAgentContext.cs b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopAgentContext.cs
new file mode 100644
index 0000000..e6dad06
--- /dev/null
+++ b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopAgentContext.cs
@@ -0,0 +1,87 @@
+using System.Text.Json;
+using System.Text.Json.Nodes;
+
+namespace SopHelper.Engine.SopAgent;
+
+/// 压缩传输上下文;原始画面仍保留在本地,工具调用与结果始终成对保留。
+public static class SopAgentContext
+{
+ public static long Estimate(IReadOnlyList