Skip to content

Commit 72384f1

Browse files
committed
Fix thinking text
1 parent e63fd8c commit 72384f1

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/copilot-message/components/thinking-block.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
'use client'
22

3-
import { memo, useEffect, useRef, useState } from 'react'
3+
import { memo, useEffect, useMemo, useRef, useState } from 'react'
44
import clsx from 'clsx'
55
import { ChevronUp } from 'lucide-react'
66
import CopilotMarkdownRenderer from './markdown-renderer'
77

8+
/**
9+
* Removes thinking tags (raw or escaped) from streamed content.
10+
*/
11+
function stripThinkingTags(text: string): string {
12+
return text
13+
.replace(/<\/?thinking[^>]*>/gi, '')
14+
.replace(/&lt;\/?thinking[^&]*&gt;/gi, '')
15+
.trim()
16+
}
17+
818
/**
919
* Max height for thinking content before internal scrolling kicks in
1020
*/
@@ -187,6 +197,9 @@ export function ThinkingBlock({
187197
label = 'Thought',
188198
hasSpecialTags = false,
189199
}: ThinkingBlockProps) {
200+
// Strip thinking tags from content on render to handle persisted messages
201+
const cleanContent = useMemo(() => stripThinkingTags(content || ''), [content])
202+
190203
const [isExpanded, setIsExpanded] = useState(false)
191204
const [duration, setDuration] = useState(0)
192205
const [userHasScrolledAway, setUserHasScrolledAway] = useState(false)
@@ -209,10 +222,10 @@ export function ThinkingBlock({
209222
return
210223
}
211224

212-
if (!userCollapsedRef.current && content && content.trim().length > 0) {
225+
if (!userCollapsedRef.current && cleanContent && cleanContent.length > 0) {
213226
setIsExpanded(true)
214227
}
215-
}, [isStreaming, content, hasFollowingContent, hasSpecialTags])
228+
}, [isStreaming, cleanContent, hasFollowingContent, hasSpecialTags])
216229

217230
// Reset start time when streaming begins
218231
useEffect(() => {
@@ -298,7 +311,7 @@ export function ThinkingBlock({
298311
return `${seconds}s`
299312
}
300313

301-
const hasContent = content && content.trim().length > 0
314+
const hasContent = cleanContent.length > 0
302315
// Thinking is "done" when streaming ends OR when there's following content (like a tool call) OR when special tags appear
303316
const isThinkingDone = !isStreaming || hasFollowingContent || hasSpecialTags
304317
const durationText = `${label} for ${formatDuration(duration)}`
@@ -374,7 +387,7 @@ export function ThinkingBlock({
374387
isExpanded ? 'mt-1.5 max-h-[150px] opacity-100' : 'max-h-0 opacity-0'
375388
)}
376389
>
377-
<SmoothThinkingText content={content} isStreaming={isStreaming && !hasFollowingContent} />
390+
<SmoothThinkingText content={cleanContent} isStreaming={isStreaming && !hasFollowingContent} />
378391
</div>
379392
</div>
380393
)
@@ -412,7 +425,7 @@ export function ThinkingBlock({
412425
>
413426
{/* Completed thinking text - dimmed with markdown */}
414427
<div className='[&_*]:!text-[var(--text-muted)] [&_*]:!text-[12px] [&_*]:!leading-[1.4] [&_p]:!m-0 [&_p]:!mb-1 [&_h1]:!text-[12px] [&_h1]:!font-semibold [&_h1]:!m-0 [&_h1]:!mb-1 [&_h2]:!text-[12px] [&_h2]:!font-semibold [&_h2]:!m-0 [&_h2]:!mb-1 [&_h3]:!text-[12px] [&_h3]:!font-semibold [&_h3]:!m-0 [&_h3]:!mb-1 [&_code]:!text-[11px] [&_ul]:!pl-5 [&_ul]:!my-1 [&_ol]:!pl-6 [&_ol]:!my-1 [&_li]:!my-0.5 [&_li]:!py-0 font-season text-[12px] text-[var(--text-muted)]'>
415-
<CopilotMarkdownRenderer content={content} />
428+
<CopilotMarkdownRenderer content={cleanContent} />
416429
</div>
417430
</div>
418431
</div>

apps/sim/lib/copilot/tools/client/blocks/get-block-options.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ export class GetBlockOptionsClientTool extends BaseClientTool {
3737
},
3838
},
3939
getDynamicText: (params, state) => {
40-
if (params?.blockId && typeof params.blockId === 'string') {
40+
const blockId =
41+
(params as any)?.blockId ||
42+
(params as any)?.blockType ||
43+
(params as any)?.block_id ||
44+
(params as any)?.block_type
45+
if (typeof blockId === 'string') {
4146
// Look up the block config to get the human-readable name
42-
const blockConfig = getBlock(params.blockId)
43-
const blockName = (blockConfig?.name ?? params.blockId.replace(/_/g, ' ')).toLowerCase()
47+
const blockConfig = getBlock(blockId)
48+
const blockName = (blockConfig?.name ?? blockId.replace(/_/g, ' ')).toLowerCase()
4449

4550
switch (state) {
4651
case ClientToolCallState.success:

apps/sim/stores/panel/copilot/store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,12 @@ function beginThinkingBlock(context: StreamingContext) {
10021002
}
10031003

10041004
/**
1005-
* Removes thinking tags from streamed content.
1005+
* Removes thinking tags (raw or escaped) from streamed content.
10061006
*/
10071007
function stripThinkingTags(text: string): string {
1008-
return text.replace(/<\/?thinking>/g, '')
1008+
return text
1009+
.replace(/<\/?thinking[^>]*>/gi, '')
1010+
.replace(/&lt;\/?thinking[^&]*&gt;/gi, '')
10091011
}
10101012

10111013
function appendThinkingContent(context: StreamingContext, text: string) {

0 commit comments

Comments
 (0)