@exulu/backend 5.1.2 → 5.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-37MKZIH2.js → chunk-PU3LGCBR.js} +281 -33
- package/dist/{convert-exulu-tools-to-ai-sdk-tools-BNDHJH7B.js → convert-exulu-tools-to-ai-sdk-tools-77FLVHEA.js} +3 -1
- package/dist/index.cjs +2800 -376
- package/dist/index.js +2412 -285
- package/ee/agentic-retrieval/pipeline/config.test.ts +42 -0
- package/ee/agentic-retrieval/pipeline/config.ts +36 -0
- package/ee/agentic-retrieval/pipeline/index.test.ts +3 -3
- package/ee/agentic-retrieval/pipeline/index.ts +43 -1
- package/ee/agentic-retrieval/pipeline/micro-call.test.ts +58 -0
- package/ee/agentic-retrieval/pipeline/micro-call.ts +20 -3
- package/ee/agentic-retrieval/pipeline/near-duplicates.test.ts +50 -0
- package/ee/agentic-retrieval/pipeline/near-duplicates.ts +74 -0
- package/ee/agentic-retrieval/pipeline/prefilter.test.ts +32 -0
- package/ee/agentic-retrieval/pipeline/prefilter.ts +13 -1
- package/ee/agentic-retrieval/pipeline/rerank.test.ts +79 -0
- package/ee/agentic-retrieval/pipeline/rerank.ts +61 -6
- package/ee/claim-check/agent-config.ts +26 -0
- package/ee/claim-check/concurrency.test.ts +33 -0
- package/ee/claim-check/concurrency.ts +30 -0
- package/ee/claim-check/evidence.test.ts +198 -0
- package/ee/claim-check/evidence.ts +162 -0
- package/ee/claim-check/prompts/decompose.ts +52 -0
- package/ee/claim-check/prompts/disambiguate.ts +58 -0
- package/ee/claim-check/prompts/select-disambiguate.ts +17 -0
- package/ee/claim-check/prompts/select.ts +55 -0
- package/ee/claim-check/prompts/shared.test.ts +193 -0
- package/ee/claim-check/prompts/shared.ts +89 -0
- package/ee/claim-check/prompts/verify.ts +56 -0
- package/ee/claim-check/rate-limit.test.ts +40 -0
- package/ee/claim-check/rate-limit.ts +82 -0
- package/ee/claim-check/routes.test.ts +1188 -0
- package/ee/claim-check/routes.ts +682 -0
- package/ee/claim-check/run.test.ts +1317 -0
- package/ee/claim-check/run.ts +710 -0
- package/ee/claim-check/score.test.ts +108 -0
- package/ee/claim-check/score.ts +75 -0
- package/ee/claim-check/second-chance.test.ts +89 -0
- package/ee/claim-check/second-chance.ts +80 -0
- package/ee/claim-check/split.test.ts +179 -0
- package/ee/claim-check/split.ts +220 -0
- package/ee/claim-check/stages/decompose.test.ts +129 -0
- package/ee/claim-check/stages/decompose.ts +64 -0
- package/ee/claim-check/stages/disambiguate.test.ts +130 -0
- package/ee/claim-check/stages/disambiguate.ts +74 -0
- package/ee/claim-check/stages/select-disambiguate.test.ts +137 -0
- package/ee/claim-check/stages/select-disambiguate.ts +90 -0
- package/ee/claim-check/stages/select.test.ts +155 -0
- package/ee/claim-check/stages/select.ts +94 -0
- package/ee/claim-check/stages/usage.ts +11 -0
- package/ee/claim-check/stages/verify.test.ts +154 -0
- package/ee/claim-check/stages/verify.ts +78 -0
- package/ee/claim-check/types.ts +178 -0
- package/ee/entitlements.ts +3 -0
- package/ee/invoke-skills/create-sandbox.ts +18 -1
- package/ee/python/requirements.txt +3 -0
- package/ee/schemas.ts +71 -0
- package/ee/workers.ts +13 -0
- package/package.json +1 -1
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { scoreClaims, buildSentenceResults } from "./score";
|
|
2
|
+
import type { Claim, Sentence, SentenceState } from "./types";
|
|
3
|
+
|
|
4
|
+
const THRESHOLDS = { warn: 0.1, alert: 0.3 };
|
|
5
|
+
|
|
6
|
+
const claim = (id: string, verdict: Claim["verdict"], sentenceIndex = 0): Claim => ({
|
|
7
|
+
id,
|
|
8
|
+
sentenceIndex,
|
|
9
|
+
text: `claim ${id}`,
|
|
10
|
+
inferred: false,
|
|
11
|
+
verdict,
|
|
12
|
+
evidence: [],
|
|
13
|
+
rationale: "",
|
|
14
|
+
rechecked: false,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe("scoreClaims", () => {
|
|
18
|
+
it("counts the three verdicts separately and derives the combined share", () => {
|
|
19
|
+
const score = scoreClaims(
|
|
20
|
+
[
|
|
21
|
+
claim("c1", "supported"),
|
|
22
|
+
claim("c2", "supported"),
|
|
23
|
+
claim("c3", "contradicted"),
|
|
24
|
+
claim("c4", "unsupported"),
|
|
25
|
+
],
|
|
26
|
+
THRESHOLDS,
|
|
27
|
+
);
|
|
28
|
+
expect(score).toEqual({
|
|
29
|
+
claims: 4,
|
|
30
|
+
supported: 2,
|
|
31
|
+
contradicted: 1,
|
|
32
|
+
unsupported: 1,
|
|
33
|
+
unsupportedShare: 0.5,
|
|
34
|
+
label: "alert",
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("labels on inclusive lower bounds: below warn is ok, warn is warn, alert is alert", () => {
|
|
39
|
+
const supported = (n: number) => Array.from({ length: n }, (_, i) => claim(`s${i}`, "supported"));
|
|
40
|
+
// 0/10 => 0 => ok
|
|
41
|
+
expect(scoreClaims(supported(10), THRESHOLDS).label).toBe("ok");
|
|
42
|
+
// 1/10 => 0.1 => exactly warn
|
|
43
|
+
expect(scoreClaims([...supported(9), claim("x", "unsupported")], THRESHOLDS).label).toBe("warn");
|
|
44
|
+
// 2/10 => 0.2 => still warn
|
|
45
|
+
expect(
|
|
46
|
+
scoreClaims([...supported(8), claim("x", "unsupported"), claim("y", "unsupported")], THRESHOLDS).label,
|
|
47
|
+
).toBe("warn");
|
|
48
|
+
// 3/10 => 0.3 => exactly alert
|
|
49
|
+
expect(
|
|
50
|
+
scoreClaims(
|
|
51
|
+
[...supported(7), claim("x", "unsupported"), claim("y", "unsupported"), claim("z", "contradicted")],
|
|
52
|
+
THRESHOLDS,
|
|
53
|
+
).label,
|
|
54
|
+
).toBe("alert");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Review Focus 4
|
|
58
|
+
it("returns label n/a and share 0 when there are no checkable claims — never 0 as a verdict", () => {
|
|
59
|
+
const score = scoreClaims([], THRESHOLDS);
|
|
60
|
+
expect(score).toEqual({
|
|
61
|
+
claims: 0,
|
|
62
|
+
supported: 0,
|
|
63
|
+
contradicted: 0,
|
|
64
|
+
unsupported: 0,
|
|
65
|
+
unsupportedShare: 0,
|
|
66
|
+
label: "n/a",
|
|
67
|
+
});
|
|
68
|
+
expect(Number.isNaN(score.unsupportedShare)).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("rounds the share to four decimals rather than emitting a repeating float", () => {
|
|
72
|
+
const score = scoreClaims([claim("a", "supported"), claim("b", "supported"), claim("c", "unsupported")], THRESHOLDS);
|
|
73
|
+
expect(score.unsupportedShare).toBe(0.3333);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("respects custom thresholds", () => {
|
|
77
|
+
const strict = scoreClaims([claim("a", "supported"), claim("b", "unsupported")], { warn: 0.6, alert: 0.9 });
|
|
78
|
+
expect(strict.unsupportedShare).toBe(0.5);
|
|
79
|
+
expect(strict.label).toBe("ok");
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("buildSentenceResults", () => {
|
|
84
|
+
const sentences: Sentence[] = [
|
|
85
|
+
{ index: 0, start: 0, end: 10, text: "Satz eins.", headingPath: [] },
|
|
86
|
+
{ index: 1, start: 11, end: 21, text: "Satz zwei.", headingPath: [] },
|
|
87
|
+
{ index: 2, start: 22, end: 32, text: "Satz drei.", headingPath: [] },
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
it("carries offsets, state, reason and the claim ids of each sentence", () => {
|
|
91
|
+
const states = new Map<number, { state: SentenceState; reason?: string }>([
|
|
92
|
+
[0, { state: "checked" }],
|
|
93
|
+
[1, { state: "no_verifiable_content", reason: "opinion" }],
|
|
94
|
+
[2, { state: "cannot_disambiguate", reason: "'dort' unresolvable" }],
|
|
95
|
+
]);
|
|
96
|
+
const results = buildSentenceResults(sentences, states, [claim("c1", "supported", 0), claim("c2", "unsupported", 0)]);
|
|
97
|
+
|
|
98
|
+
expect(results[0]).toEqual({ index: 0, start: 0, end: 10, state: "checked", claimIds: ["c1", "c2"] });
|
|
99
|
+
expect(results[1]).toEqual({ index: 1, start: 11, end: 21, state: "no_verifiable_content", reason: "opinion", claimIds: [] });
|
|
100
|
+
expect(results[2]!.state).toBe("cannot_disambiguate");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("defaults a sentence with no recorded state to not_checked", () => {
|
|
104
|
+
const results = buildSentenceResults(sentences, new Map(), []);
|
|
105
|
+
expect(results.every((r) => r.state === "not_checked")).toBe(true);
|
|
106
|
+
expect(results[0]!.reason).toBe("stage did not report an outcome for this sentence");
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// ee/claim-check/score.ts
|
|
2
|
+
//
|
|
3
|
+
// The honesty rules of the whole feature live here (spec §2.4):
|
|
4
|
+
// 1. only CHECKED claims are in the denominator — dropped sentences never
|
|
5
|
+
// drag the number down;
|
|
6
|
+
// 2. "contradicted" and "unsupported" stay separate, because "the sources say
|
|
7
|
+
// otherwise" and "the sources are silent" are different problems;
|
|
8
|
+
// 3. no checkable claims => "n/a", never 0, which would read as a failure.
|
|
9
|
+
import type {
|
|
10
|
+
Claim,
|
|
11
|
+
ClaimCheckScore,
|
|
12
|
+
ScoreLabel,
|
|
13
|
+
Sentence,
|
|
14
|
+
SentenceResult,
|
|
15
|
+
SentenceState,
|
|
16
|
+
} from "./types";
|
|
17
|
+
|
|
18
|
+
function labelFor(share: number, thresholds: { warn: number; alert: number }): ScoreLabel {
|
|
19
|
+
if (share >= thresholds.alert) return "alert";
|
|
20
|
+
if (share >= thresholds.warn) return "warn";
|
|
21
|
+
return "ok";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function scoreClaims(
|
|
25
|
+
claims: Claim[],
|
|
26
|
+
thresholds: { warn: number; alert: number },
|
|
27
|
+
): ClaimCheckScore {
|
|
28
|
+
const supported = claims.filter((c) => c.verdict === "supported").length;
|
|
29
|
+
const contradicted = claims.filter((c) => c.verdict === "contradicted").length;
|
|
30
|
+
const unsupported = claims.filter((c) => c.verdict === "unsupported").length;
|
|
31
|
+
const total = claims.length;
|
|
32
|
+
|
|
33
|
+
if (total === 0) {
|
|
34
|
+
return { claims: 0, supported: 0, contradicted: 0, unsupported: 0, unsupportedShare: 0, label: "n/a" };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const share = Number(((contradicted + unsupported) / total).toFixed(4));
|
|
38
|
+
return {
|
|
39
|
+
claims: total,
|
|
40
|
+
supported,
|
|
41
|
+
contradicted,
|
|
42
|
+
unsupported,
|
|
43
|
+
unsupportedShare: share,
|
|
44
|
+
label: labelFor(share, thresholds),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function buildSentenceResults(
|
|
49
|
+
sentences: Sentence[],
|
|
50
|
+
states: Map<number, { state: SentenceState; reason?: string }>,
|
|
51
|
+
claims: Claim[],
|
|
52
|
+
): SentenceResult[] {
|
|
53
|
+
const claimIdsBySentence = new Map<number, string[]>();
|
|
54
|
+
for (const claim of claims) {
|
|
55
|
+
const list = claimIdsBySentence.get(claim.sentenceIndex) ?? [];
|
|
56
|
+
list.push(claim.id);
|
|
57
|
+
claimIdsBySentence.set(claim.sentenceIndex, list);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return sentences.map((sentence) => {
|
|
61
|
+
const recorded = states.get(sentence.index) ?? {
|
|
62
|
+
state: "not_checked" as SentenceState,
|
|
63
|
+
reason: "stage did not report an outcome for this sentence",
|
|
64
|
+
};
|
|
65
|
+
const result: SentenceResult = {
|
|
66
|
+
index: sentence.index,
|
|
67
|
+
start: sentence.start,
|
|
68
|
+
end: sentence.end,
|
|
69
|
+
state: recorded.state,
|
|
70
|
+
claimIds: claimIdsBySentence.get(sentence.index) ?? [],
|
|
71
|
+
};
|
|
72
|
+
if (recorded.reason) result.reason = recorded.reason;
|
|
73
|
+
return result;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
jest.mock("@EE/agentic-retrieval/pipeline/search", () => ({ searchContexts: jest.fn() }));
|
|
2
|
+
import { searchContexts } from "@EE/agentic-retrieval/pipeline/search";
|
|
3
|
+
import { makeSecondChance } from "./second-chance";
|
|
4
|
+
|
|
5
|
+
const CONTEXTS = [{ id: "handbuecher", name: "Handbücher" }, { id: "tickets", name: "Tickets" }] as any[];
|
|
6
|
+
|
|
7
|
+
const agentWith = (claimCheck: string, knowledgeBases?: string) => ({
|
|
8
|
+
id: "agent-1",
|
|
9
|
+
tools: [
|
|
10
|
+
{
|
|
11
|
+
id: "agentic_context_search",
|
|
12
|
+
config: [
|
|
13
|
+
{ name: "claim_check", variable: claimCheck, type: "json" },
|
|
14
|
+
...(knowledgeBases ? [{ name: "knowledge_bases", variable: knowledgeBases, type: "json" }] : []),
|
|
15
|
+
],
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
beforeEach(() => (searchContexts as jest.Mock).mockReset());
|
|
21
|
+
|
|
22
|
+
describe("makeSecondChance", () => {
|
|
23
|
+
it("searches the agent's enabled contexts and returns ranked snippets", async () => {
|
|
24
|
+
(searchContexts as jest.Mock).mockResolvedValue({
|
|
25
|
+
chunks: [
|
|
26
|
+
{ chunk_id: "k1", item_id: "i1", item_name: "handbuch.pdf", chunk_content: "Die FST-2XT arbeitet mit 230 Volt." },
|
|
27
|
+
{ chunk_id: "k2", item_id: "i2", item_name: "anderes.pdf", chunk_content: "Unverwandter Inhalt über Türen." },
|
|
28
|
+
],
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const secondChance = makeSecondChance({
|
|
32
|
+
agent: agentWith('{"enabled":true}', '{"handbuecher":{"enabled":true},"tickets":{"enabled":false}}'),
|
|
33
|
+
user: { id: 7 } as any,
|
|
34
|
+
model: {} as any,
|
|
35
|
+
contexts: CONTEXTS,
|
|
36
|
+
cap: 5,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const snippets = await secondChance!("Die FST-2XT arbeitet mit 230 Volt.");
|
|
40
|
+
expect((searchContexts as jest.Mock).mock.calls[0][0].contextIds).toEqual(["handbuecher"]);
|
|
41
|
+
expect(snippets[0]!.text).toContain("230 Volt");
|
|
42
|
+
expect(snippets[0]!.sourceId).toBe("i1");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("returns undefined when the agent has no retrieval tool or no enabled context", () => {
|
|
46
|
+
expect(makeSecondChance({ agent: { id: "a", tools: [] }, model: {} as any, contexts: CONTEXTS, cap: 5 })).toBeUndefined();
|
|
47
|
+
expect(
|
|
48
|
+
makeSecondChance({
|
|
49
|
+
agent: agentWith('{"enabled":true}', '{"handbuecher":{"enabled":false},"tickets":{"enabled":false}}'),
|
|
50
|
+
model: {} as any,
|
|
51
|
+
contexts: CONTEXTS,
|
|
52
|
+
cap: 5,
|
|
53
|
+
}),
|
|
54
|
+
).toBeUndefined();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("stops searching once the cap is spent", async () => {
|
|
58
|
+
(searchContexts as jest.Mock).mockResolvedValue({ chunks: [] });
|
|
59
|
+
const secondChance = makeSecondChance({
|
|
60
|
+
agent: agentWith('{"enabled":true}'),
|
|
61
|
+
model: {} as any,
|
|
62
|
+
contexts: CONTEXTS,
|
|
63
|
+
cap: 2,
|
|
64
|
+
})!;
|
|
65
|
+
await secondChance("eins");
|
|
66
|
+
await secondChance("zwei");
|
|
67
|
+
await secondChance("drei");
|
|
68
|
+
expect((searchContexts as jest.Mock).mock.calls.length).toBe(2);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("swallows a retrieval failure and returns no snippets, leaving the first verdict standing", async () => {
|
|
72
|
+
(searchContexts as jest.Mock).mockRejectedValue(new Error("vector search down"));
|
|
73
|
+
const secondChance = makeSecondChance({
|
|
74
|
+
agent: agentWith('{"enabled":true}'),
|
|
75
|
+
model: {} as any,
|
|
76
|
+
contexts: CONTEXTS,
|
|
77
|
+
cap: 5,
|
|
78
|
+
})!;
|
|
79
|
+
// Expected noise: the implementation warns on a swallowed failure (see
|
|
80
|
+
// second-chance.ts); spied so it doesn't dirty test output, same as
|
|
81
|
+
// routes.test.ts does for its own swallowed-error case.
|
|
82
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
|
|
83
|
+
try {
|
|
84
|
+
await expect(secondChance("eine Aussage")).resolves.toEqual([]);
|
|
85
|
+
} finally {
|
|
86
|
+
warnSpy.mockRestore();
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// ee/claim-check/second-chance.ts
|
|
2
|
+
//
|
|
3
|
+
// One targeted retrieval per unsupported claim (spec decision 2). The point is
|
|
4
|
+
// to tell "the model made this up" apart from "true, but from a passage the
|
|
5
|
+
// answer was never shown" — so this only exists for callers with an agent
|
|
6
|
+
// scope. External callers bring their own sources and have nothing to search.
|
|
7
|
+
import type { LanguageModel } from "ai";
|
|
8
|
+
import type { User } from "@EXULU_TYPES/models/user";
|
|
9
|
+
import type { ExuluContext } from "@SRC/exulu/context";
|
|
10
|
+
import { searchContexts } from "@EE/agentic-retrieval/pipeline/search";
|
|
11
|
+
import { parsePipelineConfig } from "@EE/agentic-retrieval/pipeline/config";
|
|
12
|
+
import { toolConfigOf, hasNoToolConfig } from "./agent-config";
|
|
13
|
+
import { indexSources, selectEvidence } from "./evidence";
|
|
14
|
+
import type { EvidenceSnippet } from "./types";
|
|
15
|
+
|
|
16
|
+
const MAX_SNIPPETS_PER_CLAIM = 4;
|
|
17
|
+
|
|
18
|
+
export function makeSecondChance(args: {
|
|
19
|
+
agent: any;
|
|
20
|
+
user?: User;
|
|
21
|
+
role?: string;
|
|
22
|
+
model: LanguageModel;
|
|
23
|
+
contexts: ExuluContext[];
|
|
24
|
+
cap: number;
|
|
25
|
+
}): ((claimText: string) => Promise<EvidenceSnippet[]>) | undefined {
|
|
26
|
+
const raw = toolConfigOf(args.agent);
|
|
27
|
+
if (hasNoToolConfig(raw)) return undefined;
|
|
28
|
+
|
|
29
|
+
const cfg = parsePipelineConfig(raw);
|
|
30
|
+
const enabled = args.contexts.filter((c) => cfg.knowledgeBases[c.id]?.enabled !== false);
|
|
31
|
+
if (enabled.length === 0) return undefined;
|
|
32
|
+
|
|
33
|
+
const contextsById = new Map(enabled.map((c) => [c.id, c]));
|
|
34
|
+
const contextIds = enabled.map((c) => c.id);
|
|
35
|
+
let spent = 0;
|
|
36
|
+
|
|
37
|
+
return async (claimText: string): Promise<EvidenceSnippet[]> => {
|
|
38
|
+
if (spent >= args.cap) return [];
|
|
39
|
+
spent += 1;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const { chunks } = await searchContexts({
|
|
43
|
+
contextIds,
|
|
44
|
+
contextsById,
|
|
45
|
+
kbProfiles: cfg.knowledgeBases,
|
|
46
|
+
// The claim IS the query: it is already standalone and decontextualized,
|
|
47
|
+
// which is what the extraction stages were for.
|
|
48
|
+
question: claimText,
|
|
49
|
+
keywords: [],
|
|
50
|
+
importantKeyword: "",
|
|
51
|
+
user: args.user,
|
|
52
|
+
role: args.role,
|
|
53
|
+
model: args.model,
|
|
54
|
+
preselectedItems: new Map(),
|
|
55
|
+
identifierPinsByContext: new Map(),
|
|
56
|
+
memoryPinnedItemIdsByContext: new Map(),
|
|
57
|
+
userPinnedItemIdsByContext: new Map(),
|
|
58
|
+
rewrites: cfg.vocabulary.rewrites,
|
|
59
|
+
styleHint: cfg.vocabulary.styleHint,
|
|
60
|
+
maxQueries: 1,
|
|
61
|
+
skipPrefilter: true,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (chunks.length === 0) return [];
|
|
65
|
+
|
|
66
|
+
const index = indexSources(
|
|
67
|
+
chunks.map((c: any) => ({
|
|
68
|
+
id: String(c.item_id ?? c.chunk_id),
|
|
69
|
+
name: c.item_name,
|
|
70
|
+
content: String(c.chunk_content ?? ""),
|
|
71
|
+
})),
|
|
72
|
+
);
|
|
73
|
+
return selectEvidence([claimText], index, MAX_SNIPPETS_PER_CLAIM);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
// A failed second chance must never change a verdict or fail the check.
|
|
76
|
+
console.warn("[EXULU claim-check] second-chance retrieval failed:", err);
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// ee/claim-check/split.test.ts
|
|
2
|
+
import { splitIntoSentences, buildWindows, localeFromText } from "./split";
|
|
3
|
+
|
|
4
|
+
const offsetsAreExact = (answer: string) =>
|
|
5
|
+
splitIntoSentences(answer).every((s) => answer.slice(s.start, s.end) === s.text);
|
|
6
|
+
|
|
7
|
+
describe("splitIntoSentences", () => {
|
|
8
|
+
it("splits plain German prose and keeps offsets exact", () => {
|
|
9
|
+
const answer = "Die FST-2XT ist eine Steuerung. Sie wird im Schaltschrank montiert.";
|
|
10
|
+
const sentences = splitIntoSentences(answer, "de");
|
|
11
|
+
expect(sentences.map((s) => s.text)).toEqual([
|
|
12
|
+
"Die FST-2XT ist eine Steuerung.",
|
|
13
|
+
"Sie wird im Schaltschrank montiert.",
|
|
14
|
+
]);
|
|
15
|
+
expect(offsetsAreExact(answer)).toBe(true);
|
|
16
|
+
expect(sentences[0]!.index).toBe(0);
|
|
17
|
+
expect(sentences[1]!.index).toBe(1);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("does not split on German abbreviations", () => {
|
|
21
|
+
const answer = "Die Ursache ist z. B. ein defekter Schalter. Nr. 7 ist betroffen, ca. 3 Stück.";
|
|
22
|
+
expect(splitIntoSentences(answer, "de").map((s) => s.text)).toEqual([
|
|
23
|
+
"Die Ursache ist z. B. ein defekter Schalter.",
|
|
24
|
+
"Nr. 7 ist betroffen, ca. 3 Stück.",
|
|
25
|
+
]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Regression: a bare single-letter abbreviation entry ("z.") must not glue
|
|
29
|
+
// on ordinary nouns ending in the same letter plus a period (Platz, Satz,
|
|
30
|
+
// Netz, ...) while still gluing real abbreviations.
|
|
31
|
+
it("does not over-glue ordinary sentences ending in a noun that shares an abbreviation's last letter", () => {
|
|
32
|
+
const answer = "Der Aufzug steht am Platz. Er wird repariert.";
|
|
33
|
+
expect(splitIntoSentences(answer, "de").map((s) => s.text)).toEqual([
|
|
34
|
+
"Der Aufzug steht am Platz.",
|
|
35
|
+
"Er wird repariert.",
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Review Focus 1
|
|
40
|
+
it("skips heading lines and fenced code blocks, recording the heading path", () => {
|
|
41
|
+
const answer = [
|
|
42
|
+
"# Fehlerbehebung",
|
|
43
|
+
"",
|
|
44
|
+
"## FERNABSCHALTUNG",
|
|
45
|
+
"Der Kabinenlichtschalter ist eine Ursache.",
|
|
46
|
+
"",
|
|
47
|
+
"```bash",
|
|
48
|
+
"systemctl restart lift. Nicht als Satz zählen.",
|
|
49
|
+
"```",
|
|
50
|
+
"",
|
|
51
|
+
"Weitere Ursachen sind dokumentiert.",
|
|
52
|
+
].join("\n");
|
|
53
|
+
const sentences = splitIntoSentences(answer, "de");
|
|
54
|
+
expect(sentences.map((s) => s.text)).toEqual([
|
|
55
|
+
"Der Kabinenlichtschalter ist eine Ursache.",
|
|
56
|
+
"Weitere Ursachen sind dokumentiert.",
|
|
57
|
+
]);
|
|
58
|
+
expect(sentences[0]!.headingPath).toEqual(["Fehlerbehebung", "FERNABSCHALTUNG"]);
|
|
59
|
+
expect(offsetsAreExact(answer)).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Review Focus 1 / Finding 2: table rows and fenced code stay excluded —
|
|
63
|
+
// code is not a claim, and a table row is a fragment whose claim semantics
|
|
64
|
+
// need their own treatment.
|
|
65
|
+
it("does not emit markdown table rows or fenced code as sentences", () => {
|
|
66
|
+
const answer = [
|
|
67
|
+
"Übersicht:",
|
|
68
|
+
"",
|
|
69
|
+
"| Teil | Nr. |",
|
|
70
|
+
"| --- | --- |",
|
|
71
|
+
"| Schalter | 7 |",
|
|
72
|
+
"",
|
|
73
|
+
"```",
|
|
74
|
+
"rm -rf /tmp. Kein Satz.",
|
|
75
|
+
"```",
|
|
76
|
+
].join("\n");
|
|
77
|
+
expect(splitIntoSentences(answer, "de").map((s) => s.text)).toEqual(["Übersicht:"]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Finding 2: bullets and numbered steps used to produce NO sentence at all —
|
|
81
|
+
// no sentences[] entry, no limits count, no note — so the response reported
|
|
82
|
+
// "ok" for content it had never examined. They are prose now; the marker is
|
|
83
|
+
// stripped and the offsets point at the content after it.
|
|
84
|
+
it("segments list bullets and numbered items as prose, with offsets after the marker", () => {
|
|
85
|
+
const answer = [
|
|
86
|
+
"- Punkt eins ist wahr.",
|
|
87
|
+
"* Punkt zwei ist falsch.",
|
|
88
|
+
"+ Punkt drei fehlt.",
|
|
89
|
+
"1. Erster Schritt ist nötig.",
|
|
90
|
+
"2) Zweiter Schritt folgt.",
|
|
91
|
+
].join("\n");
|
|
92
|
+
const sentences = splitIntoSentences(answer, "de");
|
|
93
|
+
expect(sentences.map((s) => s.text)).toEqual([
|
|
94
|
+
"Punkt eins ist wahr.",
|
|
95
|
+
"Punkt zwei ist falsch.",
|
|
96
|
+
"Punkt drei fehlt.",
|
|
97
|
+
"Erster Schritt ist nötig.",
|
|
98
|
+
"Zweiter Schritt folgt.",
|
|
99
|
+
]);
|
|
100
|
+
expect(offsetsAreExact(answer)).toBe(true);
|
|
101
|
+
// The marker itself is never part of the sentence, and never part of its range.
|
|
102
|
+
expect(answer[sentences[0]!.start - 1]).toBe(" ");
|
|
103
|
+
expect(answer.slice(sentences[3]!.start, sentences[3]!.end)).toBe("Erster Schritt ist nötig.");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("ignores a bare marker with no content and keeps horizontal rules out", () => {
|
|
107
|
+
const answer = ["-", "- ", "---", "***", "- Echter Punkt."].join("\n");
|
|
108
|
+
expect(splitIntoSentences(answer, "de").map((s) => s.text)).toEqual(["Echter Punkt."]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Finding 2: the German fixture this feature's first tenant actually posts —
|
|
112
|
+
// a heading, prose, two bullets, two numbered steps and a table.
|
|
113
|
+
it("checks the bullets and steps of a German maintenance answer but not its table rows", () => {
|
|
114
|
+
const answer = [
|
|
115
|
+
"## FST-2XT Steuerung",
|
|
116
|
+
"",
|
|
117
|
+
"Die Fernabschaltung hat mehrere Ursachen.",
|
|
118
|
+
"",
|
|
119
|
+
"- Der Kabinenlichtschalter steht auf AUS.",
|
|
120
|
+
"- Die Sicherung F3 ist defekt.",
|
|
121
|
+
"",
|
|
122
|
+
"1. Schaltschrank öffnen und Sicherung F3 prüfen.",
|
|
123
|
+
"2. Kabinenlichtschalter auf EIN stellen.",
|
|
124
|
+
"",
|
|
125
|
+
"| Teil | Nr. |",
|
|
126
|
+
"| --- | --- |",
|
|
127
|
+
"| Sicherung | F3 |",
|
|
128
|
+
].join("\n");
|
|
129
|
+
const sentences = splitIntoSentences(answer, "de");
|
|
130
|
+
const texts = sentences.map((s) => s.text);
|
|
131
|
+
|
|
132
|
+
expect(texts).toEqual([
|
|
133
|
+
"Die Fernabschaltung hat mehrere Ursachen.",
|
|
134
|
+
"Der Kabinenlichtschalter steht auf AUS.",
|
|
135
|
+
"Die Sicherung F3 ist defekt.",
|
|
136
|
+
"Schaltschrank öffnen und Sicherung F3 prüfen.",
|
|
137
|
+
"Kabinenlichtschalter auf EIN stellen.",
|
|
138
|
+
]);
|
|
139
|
+
expect(texts.some((t) => t.includes("|"))).toBe(false);
|
|
140
|
+
expect(offsetsAreExact(answer)).toBe(true);
|
|
141
|
+
expect(sentences.every((s) => s.headingPath[0] === "FST-2XT Steuerung")).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Review Focus 2
|
|
145
|
+
it("keeps offsets exact across umlauts, emoji and combining characters", () => {
|
|
146
|
+
const answer = "Grüße aus Köln 🎉. Der Aufzug läuft wieder. Schön! 👍🏽";
|
|
147
|
+
expect(offsetsAreExact(answer)).toBe(true);
|
|
148
|
+
expect(splitIntoSentences(answer, "de").length).toBeGreaterThanOrEqual(2);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("returns an empty array for empty or whitespace-only input", () => {
|
|
152
|
+
expect(splitIntoSentences("")).toEqual([]);
|
|
153
|
+
expect(splitIntoSentences(" \n\n ")).toEqual([]);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("buildWindows", () => {
|
|
158
|
+
it("clamps the window at both ends of the answer", () => {
|
|
159
|
+
const answer = "Eins. Zwei. Drei. Vier.";
|
|
160
|
+
const sentences = splitIntoSentences(answer, "de");
|
|
161
|
+
const windows = buildWindows(sentences, 5, 5);
|
|
162
|
+
expect(windows[0]!.preceding).toEqual([]);
|
|
163
|
+
expect(windows[0]!.following.map((s) => s.text)).toEqual(["Zwei.", "Drei.", "Vier."]);
|
|
164
|
+
expect(windows[3]!.following).toEqual([]);
|
|
165
|
+
expect(windows[3]!.preceding.map((s) => s.text)).toEqual(["Eins.", "Zwei.", "Drei."]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("honours a following window of 0 (the later stages' setting)", () => {
|
|
169
|
+
const sentences = splitIntoSentences("Eins. Zwei. Drei.", "de");
|
|
170
|
+
expect(buildWindows(sentences, 5, 0).every((w) => w.following.length === 0)).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe("localeFromText", () => {
|
|
175
|
+
it("maps detected German to de and unknown input to en", () => {
|
|
176
|
+
expect(localeFromText("Der Aufzug steht still und zeigt eine Fernabschaltung an.")).toBe("de");
|
|
177
|
+
expect(localeFromText("")).toBe("en");
|
|
178
|
+
});
|
|
179
|
+
});
|