aisop/tests/SopVideoAgent.Smoke/DirectDraftVerificationChecks.cs

64 lines
4.2 KiB
C#

using System.Net;
using System.Text;
using System.Text.Json;
using SopHelper.Engine.SopAgent;
internal static class DirectDraftVerificationChecks
{
public static async Task RunAsync(SopVideoSample video)
{
var frames = SopVideoSampler.ReadFrames(video, [1], 0, default);
var draft = new SopVideoDraft { Steps = [new() { Name = "当前步骤", Description = "当前手改判据", Region = "work", StartSeconds = 0, EndSeconds = 4, EvidenceFrame = 0 }] };
foreach (var mode in new[] { "pass", "repair", "never", "infrastructure", "cancel" })
{
using var handler = new DraftHttp(draft);
using var http = new HttpClient(handler);
using var cancellation = new CancellationTokenSource();
var attempts = 0;
var prompts = new List<SopAgentTrace>();
var request = new SopVideoAnalysisRequest("实测", "保留用户工艺", video with { Frames = frames }, ["work"],
UserMessage: "按当前草稿回测", CurrentDraft: draft, VerifyCurrentDraftFirst: true,
Conversation: mode == "pass" ? Enumerable.Range(0, 10).Select(i => new SopAgentChatTurn(i % 2 == 0 ? "user" : "assistant", "历史" + i)).ToArray() : null,
Trace: prompts.Add, VerifyDraft: (current, _, token) =>
{
attempts++;
if (mode == "cancel") { cancellation.Cancel(); token.ThrowIfCancellationRequested(); }
if (attempts == 1 && current.Steps[0].Description != "当前手改判据") throw new Exception("首次必须直接验证当前草稿");
var passed = mode == "pass" || mode == "repair" && attempts == 2;
return Task.FromResult(new SopDraftVerification(passed, "真实失败反馈" + attempts,
[new(1, current.Steps[0].Name, passed, passed ? "通过" : "触发过早")],
[new(1, 10, 1, "work", passed ? .99 : .1, passed, "真实送检结果", frames[0].Jpeg)], InfrastructureFailure: mode == "infrastructure"));
});
try
{
var result = await new SopVideoAgentClient(http).ChatAsync(request,
new("https://direct.invalid/v1", "test", "", ContextLength: 32768, MaxTokens: 2048), cancellation.Token);
Check(mode != "cancel" && result.Draft?.Verification?.Passed == (mode is "pass" or "repair"), "完成状态只能来自真实回测");
Check(attempts == (mode == "repair" ? 2 : mode == "never" ? 3 : 1), "直接回测也必须遵守三轮上限");
}
catch (OperationCanceledException) when (mode == "cancel") { }
var expected = mode == "repair" ? 1 : mode == "never" ? 2 : 0;
Check(handler.Bodies.Count == expected && prompts.Count(t => t.Kind == "model_request") == expected,
"通过、服务故障或取消不请求模型,失败后才请求修订且不产生伪模型日志");
if (expected > 0)
{
using var body = JsonDocument.Parse(handler.Bodies[0]);
Check(body.RootElement.GetProperty("messages").EnumerateArray().Any(m => m.GetProperty("content").ToString().Contains("verification_feedback")),
"首个真正的模型请求就必须包含实际失败反馈");
}
}
Console.WriteLine("PASS: 直接回测手改草稿、通过零模型请求、失败后修订、三轮上限、基础设施故障与取消保护");
}
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
private sealed class DraftHttp(SopVideoDraft draft) : HttpMessageHandler
{
public List<string> Bodies { get; } = [];
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
{
Bodies.Add(await request.Content!.ReadAsStringAsync(token));
return new(HttpStatusCode.OK) { Content = new StringContent(JsonSerializer.Serialize(new
{ choices = new[] { new { finish_reason = "stop", message = new { content = JsonSerializer.Serialize(new { reply = "根据反馈修订", draft }) } } } }), Encoding.UTF8, "application/json") };
}
}
}