Initial ForgeLM import
This commit is contained in:
commit
a392b9dc60
4296 files changed
+1212675
No files matched your search
@@ -0,0 +1,59 @@
|
||||
export interface ParsedAssistantResponse {
|
||||
rawContent: string;
|
||||
content: string;
|
||||
reasoning: string;
|
||||
thinkingOpen: boolean;
|
||||
}
|
||||
|
||||
const THINK_OPEN = "<think>";
|
||||
const THINK_CLOSE = "</think>";
|
||||
|
||||
export function buildChatTitle(text: string): string {
|
||||
const normalized = text.replace(/\s+/g, " ").trim();
|
||||
if (!normalized) return "New Chat";
|
||||
return normalized.slice(0, 50) + (normalized.length > 50 ? "..." : "");
|
||||
}
|
||||
|
||||
export function parseAssistantResponse(rawContent: string): ParsedAssistantResponse {
|
||||
let cursor = 0;
|
||||
let thinkingOpen = false;
|
||||
let content = "";
|
||||
let reasoning = "";
|
||||
|
||||
while (cursor < rawContent.length) {
|
||||
if (!thinkingOpen) {
|
||||
const start = rawContent.indexOf(THINK_OPEN, cursor);
|
||||
if (start === -1) {
|
||||
content += rawContent.slice(cursor);
|
||||
break;
|
||||
}
|
||||
|
||||
content += rawContent.slice(cursor, start);
|
||||
cursor = start + THINK_OPEN.length;
|
||||
thinkingOpen = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
const end = rawContent.indexOf(THINK_CLOSE, cursor);
|
||||
if (end === -1) {
|
||||
reasoning += rawContent.slice(cursor);
|
||||
cursor = rawContent.length;
|
||||
break;
|
||||
}
|
||||
|
||||
reasoning += rawContent.slice(cursor, end);
|
||||
cursor = end + THINK_CLOSE.length;
|
||||
thinkingOpen = false;
|
||||
}
|
||||
|
||||
const normalizedContent = reasoning.trim().length > 0
|
||||
? content.replace(/^\s+/, "")
|
||||
: content;
|
||||
|
||||
return {
|
||||
rawContent,
|
||||
content: normalizedContent.trimEnd(),
|
||||
reasoning: reasoning.trim(),
|
||||
thinkingOpen,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user