using System.Net; using System.Text; using System.Text.Json; using SopHelper.Engine.SopAgent; internal static class ReferenceToolChecks { public static async Task RunAsync(SopVideoSample video) { var sample = video with { Frames = SopVideoSampler.ReadFrames(video, [.5, 1.5, 2.5, 3.5], 0, default) }; var current = new SopVideoDraft { Steps = Enumerable.Range(0, 4).Select(i => new SopDraftStep { Name = "保留工艺" + i, Description = "主体与可见完成关系", Region = "work", StartSeconds = i, EndSeconds = i + 1, EvidenceFrame = i }).ToList() }; foreach (var mode in new[] { "valid", "renamed", "bad_evidence", "extra_argument", "out_of_focus" }) { var candidate = JsonSerializer.Deserialize(JsonSerializer.Serialize(current))!; if (mode == "renamed") candidate.Steps[0].Name = "另一工艺"; if (mode == "bad_evidence") candidate.Steps[0].EvidenceFrame = 99; using var handler = new ProbeHttp(current, candidate, mode); using var http = new HttpClient(handler); var checks = 0; var replays = 0; var result = await new SopVideoAgentClient(http).ChatAsync(new("通用流程", "保留顺序", sample, ["work"], UserMessage: "回测并修订", CurrentDraft: current, VerifyCurrentDraftFirst: true, VerifyDraft: (_, _, _) => { replays++; return Task.FromResult(new SopDraftVerification(replays > 1, "真实回测", [new(1, "保留工艺", replays > 1, "结果")], [])); }, CheckReference: (_, observed, id, _) => { checks++; Check(observed.Frames.Count == 4 && id == 1, "候选参考检查只能使用实际读取的证据"); return Task.FromResult>([new(1, 0, .5, "work", true, .95, "静态参考通过", sample.Frames[0].Jpeg), new(1, -1, 0, "work", false, .1, "起始对照不满足", [], "previous_state")]); }), new("https://probe.invalid/v1", "model", "", ContextLength: 131702), default); Check(result.Draft?.Verification?.Passed == true && replays == 2 && checks == (mode == "valid" ? 1 : 0), "参考检查不替代真实回放,改工艺、伪造证据或额外路径参数不得进入本地检查"); using var final = JsonDocument.Parse(handler.Bodies[1]); var resultText = final.RootElement.GetProperty("messages").EnumerateArray().Single(m => m.GetProperty("role").GetString() == "tool").GetProperty("content").GetString()!; Check(resultText.Contains(mode == "valid" ? "reference_checks" : "error"), "真实参考结果或具体参数错误须反馈给模型"); } Console.WriteLine("PASS: 候选参考工具、真实回放不可替代、工艺/证据/参数保护与完整结果反馈"); } private static void Check(bool value, string message) { if (!value) throw new Exception(message); } private sealed class ProbeHttp(SopVideoDraft current, SopVideoDraft candidate, string mode) : HttpMessageHandler { public List Bodies { get; } = []; protected override async Task SendAsync(HttpRequestMessage request, CancellationToken token) { Bodies.Add(await request.Content!.ReadAsStringAsync(token)); var arguments = JsonSerializer.Serialize(new { step_id = mode == "out_of_focus" ? 4 : 1, draft = candidate }); if (mode == "extra_argument") arguments = arguments[..^1] + ",\"path\":\"other.avi\"}"; object response = Bodies.Count == 1 ? new { choices = new[] { new { finish_reason = "tool_calls", message = new { role = "assistant", content = (string?)null, tool_calls = new[] { new { id = "probe", type = "function", function = new { name = "check_draft_reference", arguments } } } } } } } : new { choices = new[] { new { finish_reason = "stop", message = new { content = JsonSerializer.Serialize(new { reply = "依据检查修订", draft = current }) } } } }; return new(HttpStatusCode.OK) { Content = new StringContent(JsonSerializer.Serialize(response), Encoding.UTF8, "application/json") }; } } }