File size: 1,055 Bytes
00d40c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export interface ParsedMessage {
	thinking: string;
	content: string;
}

/**
 * Parses a message to separate thinking tokens from the main content
 * @param content The raw message content
 * @returns Object with thinking and content separated
 */
export function parseThinkingTokens(content: string): ParsedMessage {
	if (!content) {
		return { thinking: "", content: "" };
	}

	// Match thinking tokens like <think>...</think> or <thinking>...</thinking>
	const thinkingRegex = /<think(?:ing)?>([\s\S]*?)<\/think(?:ing)?>/gi;
	const matches = Array.from(content.matchAll(thinkingRegex));

	if (matches.length === 0) {
		return { thinking: "", content };
	}

	// Extract all thinking content
	const thinking = matches.map(match => match[1]?.trim() ?? "").join("\n\n");

	// Remove thinking tokens from the main content and clean up extra whitespace
	const cleanContent = content
		.replace(thinkingRegex, "")
		.replace(/\n\s*\n\s*\n/g, "\n\n") // Replace multiple newlines with double newlines
		.trim();

	return { thinking, content: cleanContent };
}