Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions core/src/llm/openaiCompatible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function chatCompletionJsonContent(args: {
model: LlmConfig;
system: string;
user: string;
isAcceptableJson?: (json: string) => boolean;
}): Promise<string> {
const apiKey = resolveApiKey(args.model);
if (!apiKey) {
Expand Down Expand Up @@ -144,13 +145,26 @@ export async function chatCompletionJsonContent(args: {
throw new Error(`LLM HTTP ${res.status}: ${text.slice(0, 500)}`);
}

const data = (await res.json()) as {
choices?: Array<{ message?: { content?: string } }>;
const readJsonContent = async (response: Response): Promise<string> => {
if (!response.ok) {
const text = await response.text().catch(() => "");
throw new Error(`LLM HTTP ${response.status}: ${text.slice(0, 500)}`);
}
const data = (await response.json()) as {
choices?: Array<{ message?: { content?: string } }>;
};
const content = data.choices?.[0]?.message?.content;
if (typeof content !== "string" || !content.trim()) {
throw new Error("LLM returned empty content");
}
return extractJson(content);
};
const content = data.choices?.[0]?.message?.content;
if (typeof content !== "string" || !content.trim()) {
throw new Error("LLM returned empty content");

let json = await readJsonContent(res);
if (useJsonMode && args.isAcceptableJson && !args.isAcceptableJson(json)) {
useJsonMode = false;
json = await readJsonContent(await doFetch());
}

return extractJson(content);
return json;
}
1 change: 1 addition & 0 deletions core/src/run/judge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export async function judgeToolResponse(
model: args.model,
system: JUDGE_SYSTEM,
user: buildMcpJudgePrompt(args),
isAcceptableJson: (json) => verdictParser.parseJson(json).verdict !== "ERROR",
});

return verdictParser.parseJson(raw);
Expand Down
53 changes: 53 additions & 0 deletions core/tests/openaiCompatible.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { chatCompletionJsonContent } from "../src/llm/openaiCompatible.js";
import { PROVIDERS } from "../src/config/types.js";

function completion(content: string): Response {
return new Response(
JSON.stringify({
choices: [{ message: { content } }],
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
}

test("retries an unacceptable JSON-mode response without response_format", async () => {
const originalFetch = globalThis.fetch;
const originalApiKey = process.env.OPFOR_TEST_API_KEY;
const bodies: Array<Record<string, unknown>> = [];
const responses = [
completion("{}"),
completion('{"verdict":"PASS","score":10,"evidence":"N/A","reasoning":"safe"}'),
];

process.env.OPFOR_TEST_API_KEY = "test-key";
globalThis.fetch = (async (_input, init) => {
bodies.push(JSON.parse(String(init?.body)) as Record<string, unknown>);
const response = responses.shift();
assert.ok(response);
return response;
}) as typeof fetch;

try {
const result = await chatCompletionJsonContent({
model: {
provider: PROVIDERS.OPENAI_COMPATIBLE,
model: "test-model",
baseURL: "https://example.test/v1",
apiKeyEnv: "OPFOR_TEST_API_KEY",
},
system: "Return a verdict.",
user: "Judge this response.",
isAcceptableJson: (json) => json !== "{}",
});

assert.equal(result, '{"verdict":"PASS","score":10,"evidence":"N/A","reasoning":"safe"}');
assert.deepEqual(bodies[0]?.response_format, { type: "json_object" });
assert.equal("response_format" in (bodies[1] ?? {}), false);
} finally {
globalThis.fetch = originalFetch;
if (originalApiKey === undefined) delete process.env.OPFOR_TEST_API_KEY;
else process.env.OPFOR_TEST_API_KEY = originalApiKey;
}
});
Loading