mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-04-28 20:20:57 +00:00
0887a07367
Gemma 4 likely rejects the flat "traditional" tools schema we were sending
(the docs use OpenAI-wrapped shape for this model) — causing env.AI.run to
throw and users to see the "AI service hiccup" reply every turn.
Switch to the universal approach:
- system prompt asks the model for a one-line JSON {is_guess, answer, hint}
- ai-client.extractText handles both Workers-AI and OpenAI response shapes
- parseJudgementJson walks brace-depth to extract JSON from stray prose /
accidental code fences
- logs twentyq_ai_throw / twentyq_ai_unparseable with preview on failure
so future issues surface in wrangler tail immediately
Tests: 7 new (parser + extractText); 444 total pass.
172 lines
5.9 KiB
JavaScript
172 lines
5.9 KiB
JavaScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
MODEL_ID,
|
|
UpstreamError,
|
|
extractText,
|
|
judge,
|
|
normalizeJudgement,
|
|
parseJudgementJson,
|
|
redactSecret,
|
|
} from "../../../src/modules/twentyq/ai-client.js";
|
|
import { makeFakeAi, mockFailure, mockJudgement } from "../../fakes/fake-ai.js";
|
|
|
|
const baseState = () => ({
|
|
category: "instrument",
|
|
target: "organ",
|
|
initialHint: "uses wind through pipes",
|
|
startedAt: 1,
|
|
solved: false,
|
|
turns: [],
|
|
});
|
|
|
|
describe("twentyq/ai-client", () => {
|
|
describe("extractText", () => {
|
|
it("reads traditional Workers-AI { response } shape", () => {
|
|
expect(extractText({ response: "hello" })).toBe("hello");
|
|
});
|
|
|
|
it("reads OpenAI-compatible choices[0].message.content", () => {
|
|
expect(extractText({ choices: [{ message: { content: "world" } }] })).toBe("world");
|
|
});
|
|
|
|
it("concatenates array content parts", () => {
|
|
expect(
|
|
extractText({
|
|
choices: [{ message: { content: [{ text: "a" }, { text: "b" }] } }],
|
|
}),
|
|
).toBe("ab");
|
|
});
|
|
|
|
it("passes through strings", () => {
|
|
expect(extractText("direct")).toBe("direct");
|
|
});
|
|
|
|
it("empty string on unknown shape", () => {
|
|
expect(extractText(null)).toBe("");
|
|
expect(extractText({})).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("parseJudgementJson", () => {
|
|
it("parses clean one-line JSON", () => {
|
|
const r = parseJudgementJson('{"is_guess":false,"answer":"yes","hint":"big"}');
|
|
expect(r).toEqual({ is_guess: false, answer: "yes", hint: "big" });
|
|
});
|
|
|
|
it("pulls JSON out of surrounding prose", () => {
|
|
const r = parseJudgementJson(
|
|
'Sure, here is my answer: {"is_guess":true,"answer":"no","hint":"x"} — hope that helps!',
|
|
);
|
|
expect(r?.is_guess).toBe(true);
|
|
});
|
|
|
|
it("strips code fences", () => {
|
|
const r = parseJudgementJson('```json\n{"is_guess":false,"answer":"yes","hint":"h"}\n```');
|
|
expect(r?.hint).toBe("h");
|
|
});
|
|
|
|
it("handles nested braces inside strings", () => {
|
|
const r = parseJudgementJson('{"is_guess":false,"answer":"no","hint":"has {braces}"}');
|
|
expect(r?.hint).toBe("has {braces}");
|
|
});
|
|
|
|
it("returns null when no JSON object present", () => {
|
|
expect(parseJudgementJson("no json here")).toBeNull();
|
|
expect(parseJudgementJson("")).toBeNull();
|
|
expect(parseJudgementJson(null)).toBeNull();
|
|
});
|
|
|
|
it("returns null on malformed JSON", () => {
|
|
expect(parseJudgementJson("{not: valid}")).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("normalizeJudgement", () => {
|
|
it("coerces missing fields to defaults", () => {
|
|
const j = normalizeJudgement(null);
|
|
expect(j.is_guess).toBe(false);
|
|
expect(j.answer).toBe("no");
|
|
expect(j.hint).toBeTruthy();
|
|
});
|
|
|
|
it("forces answer into yes/no", () => {
|
|
expect(normalizeJudgement({ answer: "YES" }).answer).toBe("yes");
|
|
expect(normalizeJudgement({ answer: "maybe" }).answer).toBe("no");
|
|
});
|
|
|
|
it("only true is_guess passes through truthy", () => {
|
|
expect(normalizeJudgement({ is_guess: 1 }).is_guess).toBe(false);
|
|
expect(normalizeJudgement({ is_guess: true }).is_guess).toBe(true);
|
|
});
|
|
|
|
it("falls back to default hint when missing or empty", () => {
|
|
expect(normalizeJudgement({ hint: "" }).hint).toMatch(/parse|yes\/no/i);
|
|
expect(normalizeJudgement({ hint: " " }).hint).toMatch(/parse|yes\/no/i);
|
|
});
|
|
});
|
|
|
|
describe("redactSecret", () => {
|
|
it("strips case-insensitive whole-word target", () => {
|
|
expect(redactSecret("the organ is loud", "organ")).toContain("(redacted)");
|
|
expect(redactSecret("ORGAN!", "organ")).toContain("(redacted)");
|
|
});
|
|
|
|
it("does not redact substring matches mid-word", () => {
|
|
expect(redactSecret("organic shapes", "organ")).toBe("organic shapes");
|
|
});
|
|
|
|
it("safe message when entire hint is the secret", () => {
|
|
const r = redactSecret("organ", "organ");
|
|
expect(r).toMatch(/redacted/i);
|
|
});
|
|
});
|
|
|
|
describe("judge (integration with fake AI)", () => {
|
|
it("returns normalized judgement on happy path", async () => {
|
|
const ai = makeFakeAi();
|
|
mockJudgement(ai, { is_guess: false, answer: "yes", hint: "long and tall" });
|
|
const r = await judge({ AI: ai }, baseState(), "is it big?");
|
|
expect(ai.run).toHaveBeenCalledOnce();
|
|
expect(ai.run.mock.calls[0][0]).toBe(MODEL_ID);
|
|
expect(r).toEqual({ is_guess: false, answer: "yes", hint: "long and tall" });
|
|
});
|
|
|
|
it("redacts secret leaking through hint", async () => {
|
|
const ai = makeFakeAi();
|
|
mockJudgement(ai, { is_guess: false, answer: "yes", hint: "it is an organ in a church" });
|
|
const r = await judge({ AI: ai }, baseState(), "is it big?");
|
|
expect(r.hint).not.toContain("organ");
|
|
expect(r.hint).toContain("(redacted)");
|
|
});
|
|
|
|
it("wraps AI exception in UpstreamError", async () => {
|
|
const ai = makeFakeAi();
|
|
mockFailure(ai, new Error("network fail"));
|
|
await expect(judge({ AI: ai }, baseState(), "is it big?")).rejects.toBeInstanceOf(
|
|
UpstreamError,
|
|
);
|
|
});
|
|
|
|
it("throws UpstreamError when env.AI missing", async () => {
|
|
await expect(judge({}, baseState(), "is it big?")).rejects.toBeInstanceOf(UpstreamError);
|
|
});
|
|
|
|
it("uses default fallback when response is empty", async () => {
|
|
const ai = makeFakeAi();
|
|
ai.run.mockResolvedValueOnce({ response: "" });
|
|
const r = await judge({ AI: ai }, baseState(), "is it big?");
|
|
expect(r.is_guess).toBe(false);
|
|
expect(r.answer).toBe("no");
|
|
});
|
|
|
|
it("does NOT send a tools array (drop function calling for Gemma compatibility)", async () => {
|
|
const ai = makeFakeAi();
|
|
mockJudgement(ai, { is_guess: false, answer: "yes", hint: "h" });
|
|
await judge({ AI: ai }, baseState(), "is it big?");
|
|
const [, body] = ai.run.mock.calls[0];
|
|
expect(body.tools).toBeUndefined();
|
|
expect(body.messages).toBeDefined();
|
|
});
|
|
});
|
|
});
|