Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions frontend/src/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ function parseThinkingTags(content: string, isComplete: boolean = false): Parsed
return parts;
}

export function stripThinkingTags(content: string): string {
// Handle the edge case: <think> followed by only whitespace and no closing tag
if (/^<think>\s*$/m.test(content) && !content.includes("</think>")) {
return content.replace(/^<think>\s*/m, "");
}
Comment thread
AnthonyRonning marked this conversation as resolved.
Outdated

// Pattern to match <think> tags (complete or incomplete) and remove them entirely
const thinkPattern = /<think>([\s\S]*?)<\/think>|<think>([\s\S]*?)$/g;
Comment thread
AnthonyRonning marked this conversation as resolved.
Outdated

// Replace all thinking tags with empty string and clean up extra whitespace
return content
.replace(thinkPattern, "")
.replace(/\n\s*\n\s*\n/g, "\n\n") // Replace multiple consecutive newlines with double newlines
.trim();
}

export function PreCode(props: JSX.IntrinsicElements["pre"]) {
const ref = useRef<HTMLPreElement>(null);

Expand Down
5 changes: 3 additions & 2 deletions frontend/src/routes/_auth.chat.$chatId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AsteriskIcon, Check, Copy, UserIcon, ChevronDown, SquarePenIcon } from
import ChatBox from "@/components/ChatBox";
import { useOpenAI } from "@/ai/useOpenAi";
import { useLocalState } from "@/state/useLocalState";
import { Markdown } from "@/components/markdown";
import { Markdown, stripThinkingTags } from "@/components/markdown";
import { ChatMessage, Chat, DEFAULT_MODEL_ID } from "@/state/LocalStateContext";
import { AlertDestructive } from "@/components/AlertDestructive";
import { Sidebar, SidebarToggle } from "@/components/Sidebar";
Expand Down Expand Up @@ -46,7 +46,8 @@ function SystemMessage({

const handleCopy = useCallback(async () => {
try {
await navigator.clipboard.writeText(text);
const textWithoutThinking = stripThinkingTags(text);
await navigator.clipboard.writeText(textWithoutThinking);
setIsCopied(true);
setTimeout(() => setIsCopied(false), 2000);
} catch (error) {
Expand Down