From d26eddb5f954cdc7862e3a8cf9dbdd5727400992 Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Fri, 13 Nov 2026 19:37:32 +0800
Subject: [PATCH] =?UTF-8?q?=E4=B8=A5=E6=A0=BC=E6=A0=A1=E9=AA=8CAI=E7=94=9F?=
=?UTF-8?q?=E6=88=90=E6=AD=A5=E9=AA=A4=E5=B9=B6=E5=8F=8D=E9=A6=88=E5=A4=8D?=
=?UTF-8?q?=E6=9F=A5=E9=98=B2=E6=AD=A2=E4=B8=8D=E5=AE=8C=E6=95=B4=E5=8C=BA?=
=?UTF-8?q?=E5=9F=9F=E8=A7=84=E5=88=99=E8=BF=9B=E5=85=A5=E9=85=8D=E6=96=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
HAND_AI.md | 4 +-
.../VVMs/RecipeEditor/RecipeEditor.axaml | 6 +-
.../RecipeEditor/SopVideoAgentViewModel.cs | 18 ++--
.../SopAgent/SopVideoAgentClient.cs | 95 +++++++++++++------
.../SopAgent/SopVideoDraftClient.cs | 32 ++++++-
tests/Avalonia.Smoke/Program.cs | 7 ++
tests/SopVideoAgent.Smoke/Program.cs | 69 ++++++++++++++
7 files changed, 187 insertions(+), 44 deletions(-)
diff --git a/HAND_AI.md b/HAND_AI.md
index 29ad76d..b32b74e 100644
--- a/HAND_AI.md
+++ b/HAND_AI.md
@@ -20,7 +20,9 @@
生成后逐步显示时间段、参考画面、名称、说明、区域键和提醒,可先修改。右侧对话区支持自然语言调整,例如“把第二步拆成两步”“合并这两步”“保留说明,只改名字”,也可以先讨论或澄清;点击发送或 Ctrl+Enter。每次会附上当前手动编辑后的草稿、已查看的视频画面及最近 40 条成功对话,Agent 必要时可继续调用视频工具。聊天回复与步骤草稿分开,只有返回并通过校验的完整草稿才更新显示;失败、取消或只讨论时保留原草稿。聊天记录和视频证据仅保留在当前编辑器会话中;“清空”只清对话,切换视频或重新生成成功后重置对话。
-点击“添加到配方”才追加步骤,已有步骤和 ID 保留,同一份草稿不能重复添加。已添加后继续聊天,按钮变为“更新配方”,确认后更新本次 Agent 添加的步骤,数量减少时删除多余的本次步骤,不重复追加、不改其他步骤;已有送检模式、稳定帧数等设置保留。完成条件直接使用生成并编辑后的步骤说明;缺少区域时保留空值并提醒标定,不猜测坐标。到“步骤”页核对触发和送检设置,最后点击“保存”写入配方文件。
+生成阶段严格校验可执行规则:`hand_move` 必须同时引用两个不同的有效起终点区域,进入/离开规则必须有主区域;区域名存在但坐标未标定也不放行,证据画面须处于对应步骤时间段。模型同时收到区域坐标和名称。校验失败后把具体错误反馈给 Agent,最多复查两次;不能为了过校验更换用户要求的动作或伪造区域,标定不足应明确返回阻塞原因。聊天生成与初次生成使用同一校验,写入配方前还会按当前区域再次校验。无效结果不会覆盖已有草稿或配方。移动规则草稿同时显示起点和终点,避免只显示终点造成漏填。
+
+点击“添加到配方”才追加步骤,已有步骤和 ID 保留,同一份草稿不能重复添加。已添加后继续聊天,按钮变为“更新配方”,确认后更新本次 Agent 添加的步骤,数量减少时删除多余的本次步骤,不重复追加、不改其他步骤;已有送检模式、稳定帧数等设置保留。完成条件直接使用生成并编辑后的步骤说明。到“步骤”页核对触发和送检设置,最后点击“保存”写入配方文件。
地址、模型名和密钥修改时自动保存到工作区的 `workspace/sop-agent.json`,无须先成功分析或保存配方,下次启动自动恢复;清空密钥也会保存。该文件包含本地明文密钥,不应分享或提交,Linux 下文件权限限制为当前用户读写。也可通过环境变量 `SOPHELPER_AGENT_ENDPOINT`、`SOPHELPER_AGENT_MODEL`、`SOPHELPER_AGENT_API_KEY` 覆盖设置。图片会发送到填写的接口。
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
index f30f8aa..b86a2f4 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml
@@ -514,9 +514,13 @@
-
+
+
+
+
+
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs
index 9fbb911..709bc4b 100644
--- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopVideoAgentViewModel.cs
@@ -219,7 +219,8 @@ public partial class RecipeEditorViewModel
AgentContextLength, AgentMaxTokens, AgentTemperature, AgentTopP, AgentTimeoutSeconds);
try { SopVideoDraftClient.ValidateSettings(connection); }
catch (ArgumentException error) { AgentStatus = error.Message; return; }
- var regions = Recipe.RegionNames.Where(r => !string.IsNullOrWhiteSpace(r)).ToArray();
+ var regionDefinitions = Recipe.ToConfig().Sop.Regions;
+ var regions = regionDefinitions.Keys.ToArray();
var recipeName = Recipe.Name; var instructions = AgentInstructions;
_agentCancellation = new CancellationTokenSource(TimeSpan.FromSeconds(connection.TimeoutSeconds));
var token = _agentCancellation.Token;
@@ -239,24 +240,25 @@ 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()), connection, token).WaitAsync(token);
+ userMessage, _agentConversation.TakeLast(40).ToArray(), CreateAgentDraftSnapshot(), regionDefinitions), connection, token).WaitAsync(token);
token.ThrowIfCancellationRequested();
draft = result.Draft; reply = result.Reply;
sample = sample with { Frames = result.ObservedFrames };
- if (draft is not null) SopVideoDraftClient.Validate(draft, sample, regions);
+ if (draft is not null) SopVideoDraftClient.Validate(draft, sample, regions, regionDefinitions);
_agentConversation.Add(new("user", userMessage));
_agentConversation.Add(new("assistant", reply));
}
else
{
- draft = await AgentClient.AnalyzeAsync(new(recipeName, instructions, sample, regions, maxFrames, progress), connection, token).WaitAsync(token);
+ draft = await AgentClient.AnalyzeAsync(new(recipeName, instructions, sample, regions, maxFrames, progress,
+ RegionDefinitions: regionDefinitions), connection, token).WaitAsync(token);
sample = sample with { Frames = draft.ObservedFrames };
reply = string.IsNullOrWhiteSpace(draft.Summary) ? $"已生成 {draft.Steps.Count} 步。" : draft.Summary;
}
token.ThrowIfCancellationRequested();
if (draft is not null)
{
- SopVideoDraftClient.Validate(draft, sample, regions);
+ SopVideoDraftClient.Validate(draft, sample, regions, regionDefinitions);
ClearAgentDraft(clearConversation: userMessage is null, forgetApplied: userMessage is null);
foreach (var step in draft.Steps) AgentDraftSteps.Add(new(step, sample.Frames[step.EvidenceFrame]));
AgentSummary = draft.Summary + (draft.Warnings.Count == 0 ? "" : "\n需核对:" + string.Join(";", draft.Warnings));
@@ -282,7 +284,8 @@ public partial class RecipeEditorViewModel
{
if (IsAgentBusy || _draftApplied || _agentSample is null || AgentDraftSteps.Count == 0) return;
var draft = new SopVideoDraft { Steps = AgentDraftSteps.Select(row => row.Step).ToList() };
- try { SopVideoDraftClient.Validate(draft, _agentSample, Recipe.RegionNames.Where(r => !string.IsNullOrWhiteSpace(r)).ToArray()); }
+ var definitions = Recipe.ToConfig().Sop.Regions;
+ try { SopVideoDraftClient.Validate(draft, _agentSample, definitions.Keys.ToArray(), definitions); }
catch (FormatException error) { AgentStatus = "草稿未添加:" + error.Message; return; }
var existing = _agentAppliedNodes.Where(Recipe.Steps.Contains).ToArray();
var applied = new List();
@@ -352,6 +355,9 @@ public sealed class AgentDraftRow : BindableBase, IDisposable
public SopDraftStep Step { get; }
public string Name { get => Step.Name; set { Step.Name = value; RaisePropertyChanged(); } }
public string Description { get => Step.Description; set { Step.Description = value; RaisePropertyChanged(); } }
+ public bool IsHandMove => Step.Method == ConditionMethod.HandMove;
+ public string RegionLabel => IsHandMove ? "终点区域" : "触发区域";
+ public string FromRegion { get => Step.FromRegion; set { Step.FromRegion = value; RaisePropertyChanged(); } }
public string Region
{
get => Step.Method == ConditionMethod.HandMove ? Step.ToRegion : Step.Region;
diff --git a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs
index 3598cab..26c07b5 100644
--- a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs
+++ b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs
@@ -29,6 +29,8 @@ public sealed class SopVideoAgentClient(HttpClient http) : ISopVideoDraftClient
long jpegBytes = observed.Sum(frame => frame.Jpeg.LongLength);
var estimatedInput = 2048L + request.Instructions.Length * 2L + request.Recipe.Length * 2L
+ request.Regions.Sum(region => region.Length * 2L) + observed.Count * 1700L;
+ estimatedInput += JsonSerializer.Serialize(request.RegionDefinitions).Length * 2L;
+ var validationRetries = 0;
var messages = new List