diff --git a/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs b/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs
index ebac1fe..ff7f6dd 100644
--- a/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs
+++ b/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs
@@ -82,7 +82,7 @@ public sealed class SopVideoFlowTest : IDisposable
public FrameResult? AnalyzeFrame(Mat image, long frameId, double seconds)
{
if (++_seen % _stride != 0) return null;
- var result = new FrameResult { FrameId = frameId, Timestamp = DateTime.Now, Size = (image.Width, image.Height) };
+ var result = new FrameResult { FrameId = frameId, Timestamp = DateTime.Now, SourceSeconds = seconds, Size = (image.Width, image.Height) };
if (_hands.IsReady) result.Hands = _hands.Detect(image);
FrameObserved?.Invoke(result, seconds);
ProcessResult(result, image);
diff --git a/src/SopHelper.CrossPlatform.Engine/Common/Types.cs b/src/SopHelper.CrossPlatform.Engine/Common/Types.cs
index 988d26b..c97796d 100644
--- a/src/SopHelper.CrossPlatform.Engine/Common/Types.cs
+++ b/src/SopHelper.CrossPlatform.Engine/Common/Types.cs
@@ -154,6 +154,8 @@ public class Hand
///
public class FrameResult
{
+ /// 回放的原始视频进度;现场相机可不提供。
+ public double? SourceSeconds { get; set; }
/// 帧号
public long FrameId { get; set; }
diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/D3VisionDecisionClient.cs b/src/SopHelper.CrossPlatform.Engine/Sop/D3VisionDecisionClient.cs
index 0afbd12..20a7e25 100644
--- a/src/SopHelper.CrossPlatform.Engine/Sop/D3VisionDecisionClient.cs
+++ b/src/SopHelper.CrossPlatform.Engine/Sop/D3VisionDecisionClient.cs
@@ -11,6 +11,8 @@ public sealed class D3VisionDecisionClient(HttpClient http) : IVisionDecisionCli
{
public async Task DecideAsync(VisionDecisionRequest request, CancellationToken cancellationToken)
{
+ if (request.EvidenceMode != VisionEvidenceMode.SingleFrame || request.Frames is { Count: > 1 })
+ return new(false, 0, "D3 单帧接口不支持过程时序判定", VisionDecisionStatus.Unsupported);
var endpoint = Environment.GetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT")
?? "http://127.0.0.1:8766/v1/systemone";
if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https"))
diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/LayaVisionDecisionClient.cs b/src/SopHelper.CrossPlatform.Engine/Sop/LayaVisionDecisionClient.cs
index 40d5de0..ad91593 100644
--- a/src/SopHelper.CrossPlatform.Engine/Sop/LayaVisionDecisionClient.cs
+++ b/src/SopHelper.CrossPlatform.Engine/Sop/LayaVisionDecisionClient.cs
@@ -9,6 +9,8 @@ public sealed class LayaVisionDecisionClient(HttpClient http) : IVisionDecisionC
{
public async Task DecideAsync(VisionDecisionRequest request, CancellationToken cancellationToken)
{
+ if (request.EvidenceMode != VisionEvidenceMode.SingleFrame || request.Frames is { Count: > 1 })
+ return new(false, 0, "Laya 单帧接口不支持过程时序判定", VisionDecisionStatus.Unsupported);
var endpoint = Environment.GetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT")
?? "http://127.0.0.1:8000/v1/vision/decide";
if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https"))
@@ -59,10 +61,13 @@ public sealed class LayaVisionDecisionClient(HttpClient http) : IVisionDecisionC
}
/// 每次取图时读取配置,切换服务无需替换引擎实例。
-public sealed class ConfiguredVisionDecisionClient(HttpClient http) : IVisionDecisionClient
+public sealed class ConfiguredVisionDecisionClient(HttpClient http, IVisionDecisionClient? sequenceClient = null) : IVisionDecisionClient
{
+ public bool SupportsSequences => (sequenceClient ?? new SequenceVisionDecisionClient(http)).SupportsSequences;
public Task DecideAsync(VisionDecisionRequest request, CancellationToken cancellationToken)
- => Environment.GetEnvironmentVariable("SOPHELPER_VISION_PROVIDER")?.ToLowerInvariant() switch
+ => request.EvidenceMode == VisionEvidenceMode.Sequence
+ ? (sequenceClient ?? new SequenceVisionDecisionClient(http)).DecideAsync(request, cancellationToken)
+ : Environment.GetEnvironmentVariable("SOPHELPER_VISION_PROVIDER")?.ToLowerInvariant() switch
{
null or "" or "d3" or "d3-lite" => new D3VisionDecisionClient(http).DecideAsync(request, cancellationToken),
"laya" => new LayaVisionDecisionClient(http).DecideAsync(request, cancellationToken),
diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/ObservationEvidenceWindow.cs b/src/SopHelper.CrossPlatform.Engine/Sop/ObservationEvidenceWindow.cs
new file mode 100644
index 0000000..16cabd6
--- /dev/null
+++ b/src/SopHelper.CrossPlatform.Engine/Sop/ObservationEvidenceWindow.cs
@@ -0,0 +1,51 @@
+namespace SopHelper.Engine.Sop;
+
+/// 按相对采集时间抽样;触发只打开窗口,任何超时或帧数都不能判定完成。
+public sealed class ObservationEvidenceWindow(AiConfirmation options)
+{
+ private readonly List _frames = [];
+ private double? _opened, _lastSample;
+ private long _lastId = -1;
+ public int Version { get; private set; }
+ public int LastSentVersion { get; set; }
+ public bool Opened => _opened.HasValue;
+ public bool Expired { get; private set; }
+ public string? Error { get; private set; }
+ public bool HasFreshEvidence => Opened && Version > LastSentVersion && _frames.Count >= 2;
+ public bool Ready { get; private set; }
+ public IReadOnlyList Frames => _frames;
+ public static double Time(VisionEvidenceFrame frame) => frame.SourceSeconds ?? (frame.CapturedAt - DateTime.UnixEpoch).TotalSeconds;
+ public void Open(double seconds)
+ {
+ if (Opened) return;
+ _opened = seconds;
+ _frames.RemoveAll(f => Time(f) < seconds - options.ObservationPreRollMs / 1000d);
+ }
+ public bool WantsFrame(long id, double seconds) => !Expired && Error is null && id != _lastId
+ && (_lastSample is null || seconds - _lastSample >= options.ObservationSampleMs / 1000d);
+ public void Add(VisionEvidenceFrame frame)
+ {
+ var seconds = Time(frame);
+ if (!double.IsFinite(seconds)) { Error = "采集时间无效"; return; }
+ if (_lastSample is { } previous && seconds < previous) { Error = "采集时间倒退,观察证据无效"; return; }
+ if (!WantsFrame(frame.FrameId, seconds)) return;
+ _lastSample = seconds; _lastId = frame.FrameId;
+ _frames.Add(frame); Version++;
+ if (!Opened) _frames.RemoveAll(f => Time(f) < seconds - options.ObservationPreRollMs / 1000d);
+ while (_frames.Count > options.ObservationMaxFrames)
+ {
+ // 保留起始和最新画面,压缩最密集的内部时间段,避免只留下尾部而丢失过程起点。
+ var remove = Enumerable.Range(1, _frames.Count - 2)
+ .MinBy(i => Time(_frames[i + 1]) - Time(_frames[i - 1]));
+ _frames.RemoveAt(remove);
+ }
+ if (_frames.Sum(f => f.Jpeg.LongLength) > 4 * 1024 * 1024) Error = "观察图片超过预算,未接受该段证据";
+ Advance(seconds);
+ }
+ public void Advance(double seconds)
+ {
+ if (_opened is not { } opened) return;
+ Ready = seconds - opened >= options.ObservationMinMs / 1000d && _frames.Count >= 2;
+ Expired |= seconds - opened >= options.ObservationMaxMs / 1000d;
+ }
+}
diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SequenceVisionDecisionClient.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SequenceVisionDecisionClient.cs
new file mode 100644
index 0000000..aabde22
--- /dev/null
+++ b/src/SopHelper.CrossPlatform.Engine/Sop/SequenceVisionDecisionClient.cs
@@ -0,0 +1,75 @@
+using System.Net.Http.Headers;
+using System.Text;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using SopHelper.Engine.SopAgent;
+
+namespace SopHelper.Engine.Sop;
+
+/// 连接信息不进入配方;由应用连接设置或环境配置提供。
+public sealed record SequenceVisionConnection(string Endpoint, string Model, string ApiKey = "", bool? EnableThinking = null);
+
+public sealed class SequenceVisionDecisionClient(HttpClient http, SequenceVisionConnection? connection = null) : IVisionDecisionClient
+{
+ private SequenceVisionConnection Settings => connection ?? new(
+ Environment.GetEnvironmentVariable("SOPHELPER_TEMPORAL_ENDPOINT") ?? "",
+ Environment.GetEnvironmentVariable("SOPHELPER_TEMPORAL_MODEL") ?? "",
+ Environment.GetEnvironmentVariable("SOPHELPER_TEMPORAL_API_KEY") ?? "");
+ public bool SupportsSequences => Uri.TryCreate(Settings.Endpoint, UriKind.Absolute, out var uri)
+ && uri.Scheme is "http" or "https" && !string.IsNullOrWhiteSpace(Settings.Model);
+ public async Task DecideAsync(VisionDecisionRequest request, CancellationToken token)
+ {
+ if (!SupportsSequences) return new(false, 0, "未配置支持多图输入的过程时序判定服务", VisionDecisionStatus.Unsupported);
+ var frames = request.Frames ?? [];
+ if (request.EvidenceMode != VisionEvidenceMode.Sequence || frames.Count is < 3 or > 32)
+ return new(false, 0, "时序证据不足,需要至少三张真实采集画面", VisionDecisionStatus.Insufficient);
+ if (frames.Select(f => f.FrameId).Distinct().Count() != frames.Count || frames.Any(f => f.Jpeg.Length == 0 || !double.IsFinite(ObservationEvidenceWindow.Time(f)))
+ || frames.Sum(f => f.Jpeg.LongLength) > 10 * 1024 * 1024
+ || frames.Zip(frames.Skip(1)).Any(pair => ObservationEvidenceWindow.Time(pair.Second) <= ObservationEvidenceWindow.Time(pair.First)))
+ throw new InvalidDataException("时序证据编号、顺序或图片预算无效");
+ var settings = Settings;
+ var parts = new List