Skip to content

Open source CapCut alternative.

TypeScript

fix(site): tidy fact-check claims at the route

David Lebddc4df450f4
diff --git a/site/src/lib/inference/factCheck.test.ts b/site/src/lib/inference/factCheck.test.ts
index 5d001f5f..0f5281b9 100644
--- a/site/src/lib/inference/factCheck.test.ts
+++ b/site/src/lib/inference/factCheck.test.ts
@@ -191,4 +191,17 @@ describe("factCheckRequestSchema", () => {
expect(factCheckRequestSchema.safeParse({ claims: ["A"], extra: 1 }).success).toBe(false);
expect(factCheckRequestSchema.safeParse({ claims: ["x".repeat(501)] }).success).toBe(false);
});
+
+ test("tidies what a model sends: spacing, blank and repeated claims, a long context", () => {
+ const parsed = factCheckRequestSchema.safeParse({
+ claims: [" The tower is\n300 m ", " ", "The tower is 300 m"],
+ context: " " + "c".repeat(2500),
+ });
+ expect(parsed.success).toBe(true);
+ expect(parsed.data?.claims).toEqual(["The tower is 300 m"]);
+ // The context keeps its first 2000 characters, the contract's cap.
+ expect(parsed.data?.context?.length).toBe(2000);
+ expect(factCheckRequestSchema.safeParse({ claims: [" ", ""] }).success).toBe(false);
+ expect(factCheckRequestSchema.safeParse({ claims: ["A"], context: " " }).data?.context).toBeUndefined();
+ });
});
diff --git a/site/src/lib/inference/factCheck.ts b/site/src/lib/inference/factCheck.ts
index 7144cac0..0d699dc2 100644
--- a/site/src/lib/inference/factCheck.ts
+++ b/site/src/lib/inference/factCheck.ts
@@ -13,10 +13,21 @@ const FACT_CONTEXT_MAX_CHARS = 2000;
/** The most claims a request may carry. The tool schema teaches the same cap. */
export const FACT_CLAIMS_MAX = 12;
+/** Claims as a model writes them, tidied before the cap applies: runs of
+ * whitespace collapse, blank claims drop, and repeats are checked once. */
+const tidyClaims = (claims: string[]) => [...new Set(claims.map((c) => c.replace(/\s+/g, " ").trim()).filter(Boolean))];
+
export const factCheckRequestSchema = z
.object({
- claims: z.array(z.string().trim().min(1).max(FACT_CLAIM_MAX_CHARS)).min(1).max(FACT_CLAIMS_MAX),
- context: z.string().trim().max(FACT_CONTEXT_MAX_CHARS).optional(),
+ claims: z
+ .array(z.string())
+ .transform(tidyClaims)
+ .pipe(z.array(z.string().max(FACT_CLAIM_MAX_CHARS)).min(1).max(FACT_CLAIMS_MAX)),
+ // A long context keeps its opening, and a blank one is left out.
+ context: z
+ .string()
+ .transform((c) => c.trim().slice(0, FACT_CONTEXT_MAX_CHARS) || undefined)
+ .optional(),
})
.strict();