okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-int (c157ea373) into f-split-qa1: my new strings renumbered after s3236 (s3237-s3255)e5476b0e1

main
1.9 KiB33 linesraw
1/**
2 * Keeps markup that belongs to the model's reasoning, not to the answer, out of the answer text (QA report F01: a literal "<analysis>" with
3 * reasoning Off, and a "p> " in front of a short answer, also in the Markdown export).
4 *
5 * This only removes the wrapper itself, never the words around it, and only for names the reasoning formats use. The words stay, so a model that
6 * answers inside such a tag still shows its answer. If the raw text of a model really holds these, that is the engine's output and is reported
7 * there: this is not a fix for it, only a guard so the page never shows a wrapper to a person.
8 *
9 * Streaming: the page calls this with everything received so far, plus the next piece, every time, so it is cheap and leaves a tag that has not
10 * arrived completely yet alone (it is removed when the rest arrives).
11 */
12const WRAPPER = '(?:think|thinking|thought|analysis|reasoning)';
13/** <think>, </think>, <analysis ...> : one reasoning wrapper tag, open or close. */
14const TAG = new RegExp(`</?${WRAPPER}(?:\\s[^<>]*)?/?>`, 'gi');
15/**
16 * A tag that lost its "<" at the very start of the answer ("p> CEDAR-742", "/p> done"): only well-known markup names, only at the start.
17 * The text after it (and the single space that follows) is kept.
18 */
19const LOST_LEAD = /^\s*\/?(?:p|br|div|span|li|ul|ol|h[1-6]|b|i|em|strong|code|pre|answer|reply|final|analysis|think|thinking|reasoning)>[ \t]+/i;
20
21/** The answer with reasoning wrapper tags and a stray leading tag fragment removed. Anything else is returned unchanged. */
22export function tidyAnswer(text: string): string {
23  if (!text) return text;
24  let out = text;
25  if (out.indexOf('>') !== -1) {
26    out = out.replace(TAG, '');
27    out = out.replace(LOST_LEAD, '');
28  }
29  return out;
30}
31
32/** True when `tidyAnswer` would change nothing, so a caller can skip a copy. */
33export const isTidy = (text: string) => tidyAnswer(text) === text;