文件
mynode/packages/lib/services/ai/utils/extractReasoning.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

38 行
1.2 KiB
TypeScript

// Some models emit reasoning inline instead of in a separate field. Gemma's
// pair is asymmetric, so generic special-token filters miss it.
const markerPairs = [
{ open: '<|channel>', close: '<channel|>' },
{ open: '<think>', close: '</think>' },
{ open: '<thinking>', close: '</thinking>' },
];
interface ExtractedReasoning {
text: string;
reasoning: string;
}
// Only a leading marker counts: anything later is the model talking *about*
// these tags, and stripping it would corrupt the answer.
const extractReasoning = (content: string): ExtractedReasoning => {
if (!content) return { text: '', reasoning: '' };
const trimmed = content.trimStart();
const pair = markerPairs.find(candidate => trimmed.startsWith(candidate.open));
if (!pair) return { text: content.trim(), reasoning: '' };
const bodyStart = pair.open.length;
const closeIndex = trimmed.indexOf(pair.close, bodyStart);
// Cut off mid-thought: keep it as reasoning rather than leak it as the reply.
if (closeIndex === -1) {
return { text: '', reasoning: trimmed.slice(bodyStart).trim() };
}
return {
text: trimmed.slice(closeIndex + pair.close.length).trim(),
reasoning: trimmed.slice(bodyStart, closeIndex).trim(),
};
};
export default extractReasoning;