Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

thinking-part.tsx

79 lines2.2 KB

1import { Show, createEffect, createSignal } from 'solid-js'
2​
3export interface ThinkingPartProps {
4 /** The thinking content to render */
5 content: string
6 /** Base class applied to thinking parts */
7 class?: string
8 /** Whether thinking is complete (has text content after) */
9 isComplete?: boolean
11​
12/**
13 * ThinkingPart component - renders thinking/reasoning content
14 *
15 * This component displays the model's internal reasoning process,
16 * typically shown in a collapsed or expandable format to distinguish
17 * it from the final response. It automatically collapses when thinking
18 * is complete.
19 *
20 * @example Standalone usage
21 * ```tsx
22 * <ThinkingPart
23 * content="Let me think about this step by step..."
24 * class="p-4 rounded bg-gray-100"
25 * />
26 * ```
27 *
28 * @example Usage in partRenderers
29 * ```tsx
30 * <ChatMessage
31 * message={message}
32 * partRenderers={{
33 * thinking: ({ content, isComplete }) => (
34 * <ThinkingPart
35 * content={content}
36 * isComplete={isComplete}
37 * class="px-5 py-3 rounded-2xl bg-gray-800/50"
38 * />
39 * )
40 * }}
41 * />
42 * ```
43 */
44export function ThinkingPart(props: ThinkingPartProps) {
45 const [isCollapsed, setIsCollapsed] = createSignal(false)
46​
47 // Auto-collapse when thinking completes
48 createEffect(() => {
49 if (props.isComplete) {
50 setIsCollapsed(true)
51 }
52 })
53​
54 return (
55 <div
56 class={props.class || undefined}
57 data-part-type="thinking"
58 data-part-content
59 >
60 <button
61 onClick={() => setIsCollapsed(!isCollapsed())}
62 class="flex items-center gap-2 text-sm text-gray-400 hover:text-gray-300 transition-colors mb-2"
63 aria-expanded={!isCollapsed()}
64 aria-label={isCollapsed() ? 'Expand thinking' : 'Collapse thinking'}
65 >
66 <span class="text-xs">{isCollapsed() ? '▶' : '▼'}</span>
67 <span class="italic">💭 Thinking...</span>
68 <Show when={props.isComplete}>
69 <span class="text-xs text-gray-500">(complete)</span>
70 </Show>
71 </button>
72 <Show when={!isCollapsed()}>
73 <div class="text-gray-300 whitespace-pre-wrap font-mono text-sm">
74 {props.content}
75 </div>
76 </Show>
77 </div>
78 )