| export interface ParsedMessage { |
| thinking: string; |
| content: string; |
| } |
|
|
| |
| |
| |
| |
| |
| export function parseThinkingTokens(content: string): ParsedMessage { |
| if (!content) { |
| return { thinking: "", content: "" }; |
| } |
|
|
| |
| const thinkingRegex = /<think(?:ing)?>([\s\S]*?)<\/think(?:ing)?>/gi; |
| const matches = Array.from(content.matchAll(thinkingRegex)); |
|
|
| if (matches.length === 0) { |
| return { thinking: "", content }; |
| } |
|
|
| |
| const thinking = matches.map(match => match[1]?.trim() ?? "").join("\n\n"); |
|
|
| |
| const cleanContent = content |
| .replace(thinkingRegex, "") |
| .replace(/\n\s*\n\s*\n/g, "\n\n") |
| .trim(); |
|
|
| return { thinking, content: cleanContent }; |
| } |
|
|