fix: handle message_id with extra wrapping quotes and request strict JSON output without reasoning

This commit is contained in:
MythEclipse
2026-05-19 00:24:13 +07:00
parent a338d6b7e2
commit 520da01be7
2 changed files with 72 additions and 5 deletions
@@ -320,6 +320,26 @@ describe("parseModerationResponse", () => {
expect(result).toHaveLength(1);
expect(result[0].messageId).toBe("m1");
});
it("handles message_id returned with extra wrapping quotes", () => {
const result = parseModerationResponse(
JSON.stringify({
results: [
{
message_id: '"test-msg-1"',
status: "clean",
flags: [],
score: 0.1,
analysis: "OK",
},
],
}),
["test-msg-1"],
);
expect(result).toHaveLength(1);
expect(result[0].messageId).toBe("test-msg-1");
});
});
describe("runModerationAnalysis", () => {
@@ -359,6 +379,45 @@ describe("runModerationAnalysis", () => {
expect(result.raw).toEqual(mockResponse);
});
it("requests strict JSON output without thinking", async () => {
const mockResponse = {
choices: [
{
message: {
content: JSON.stringify({
results: [
{
message_id: "m1",
status: "clean",
flags: [],
score: 0.1,
analysis: "OK",
},
],
}),
},
},
],
};
global.fetch = vi.fn().mockResolvedValue({
ok: true,
text: async () => JSON.stringify(mockResponse),
json: async () => mockResponse,
});
await runModerationAnalysis({
targets: [createMessageRecord()],
contextText: "test context",
});
const requestBody = JSON.parse((global.fetch as any).mock.calls[0][1].body);
expect(requestBody.temperature).toBe(0);
expect(requestBody.response_format).toEqual({ type: "json_object" });
expect(requestBody.reasoning_budget).toBeUndefined();
expect(requestBody.chat_template_kwargs).toEqual({ enable_thinking: false });
});
it("throws on non-ok HTTP response", async () => {
global.fetch = vi.fn().mockResolvedValue({
ok: false,