aisop/tests/SopVideoAgent.Smoke/ResponseDiagnosticsChecks.cs

67 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Net;
using System.Text;
using System.Text.Json;
using SopHelper.Engine.SopAgent;
internal static class ResponseDiagnosticsChecks
{
public static async Task RunAsync(SopVideoSample video)
{
foreach (var body in new[]
{
"{\"choices\":[{\"finish_reason\":\"stop\",\"message\":{\"content\":\"{\\\"reply\\\":\\\"正常\\\",\\\"draft\\\":null}\",\"tool_calls\":null}}]}",
"{\"error\":{\"message\":\"视觉后端不可用\"}}", "{\"choices\":[]}", "{\"choices\":[{}]}", "[]"
})
{
using var http = new HttpClient(new ReplyHttp(body));
var trace = new List<SopAgentTrace>();
try
{
var result = await new SopVideoAgentClient(http).ChatAsync(new("测试", "", video with { Frames = [] }, ["work"], UserMessage: "讨论", Trace: trace.Add),
new("https://invalid.test/v1", "model", ""), default);
Check(result.Reply == "正常" && body.Contains("tool_calls"), "空工具调用字段应按普通回复处理");
}
catch (InvalidDataException error)
{ Check(!body.Contains("tool_calls") && error.Message.Contains("Agent 接口"), "异常协议应返回可诊断的具体错误"); }
Check(trace.Any(t => t.Kind == "model_response" && t.Text.Contains(body)), "解析失败前须先保存原始服务响应");
}
foreach (var (content, valid) in new[]
{
("分析说明\n```json\n{\"reply\":\"正常\",\"draft\":null}\n```", true),
("```json\n{\"reply\":\"正常\",\"draft\":null}\n```", true),
("```json\n{\"reply\":\"一\",\"draft\":null}\n```\n```json\n{\"reply\":\"二\",\"draft\":null}\n```", false),
("说明\n```json\n{\"reply\":\"残缺\",\"draft\":null}", false),
("说明{\"reply\":\"无明确最终块\",\"draft\":null}", false),
("分析说明\n{\"reply\":\"正常\",\"draft\":null}", true),
("分析说明\n{\"reply\":\"一\",\"draft\":null}\n{\"reply\":\"二\",\"draft\":null}", false),
("分析说明\n{\"reply\":\"正常\",\"draft\":null,\"note\":\"花括号 } 和转义引号 \\\"\"}", true)
})
{
using var http = new HttpClient(new ReplyHttp(JsonSerializer.Serialize(new { choices = new[] { new { finish_reason = "stop", message = new { content } } } })));
var accepted = false;
try
{
var result = await new SopVideoAgentClient(http).ChatAsync(new("兼容", "", video, ["work"], UserMessage: "讨论"), new("https://invalid.test/v1", "model", ""), default);
accepted = true;
if (valid) Check(result.Reply == "正常", "唯一最终JSON对象的内容必须准确保留");
}
catch (Exception error) when (error is JsonException or FormatException) { }
Check(accepted == valid, "只接受明确且唯一的最终完整JSON代码块,不能猜测多个候选或截断输出");
}
var rootDraft = """{"summary":"完整修订","steps":[{"name":"步骤","description":"可见结果","start_seconds":0,"end_seconds":4,"method":"hand_in_region","region":"work","evidence_frame":0}]}""";
using (var http = new HttpClient(new ReplyHttp(JsonSerializer.Serialize(new { choices = new[] { new { finish_reason = "stop", message = new { content = rootDraft } } } }))))
{
var sample = video with { Frames = SopVideoSampler.ReadFrames(video, [1], 0, default) };
var result = await new SopVideoAgentClient(http).ChatAsync(new("兼容", "", sample, ["work"], UserMessage: "修订"), new("https://invalid.test/v1", "model", ""), default);
Check(result.Draft?.Steps.Count == 1 && result.Reply == "完整修订", "聊天返回根节点完整草稿时仍须经过全部证据校验并保留完整草稿");
}
Console.WriteLine("PASS: 原始响应诊断、服务错误、空choices、空工具调用、最终JSON块与完整根草稿兼容");
}
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
private sealed class ReplyHttp(string body) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
=> Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body, Encoding.UTF8, "application/json") });
}
}