forked from codinit-dev/codinit-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThinkingProcess.tsx
More file actions
170 lines (150 loc) · 5.83 KB
/
Copy pathThinkingProcess.tsx
File metadata and controls
170 lines (150 loc) · 5.83 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { memo, useState, useEffect } from 'react';
import { TextShimmer } from '~/components/ui/text-shimmer';
interface ThinkingProcessProps {
children: React.ReactNode;
isStreaming?: boolean;
}
export const ThinkingProcess = memo(({ children, isStreaming = false }: ThinkingProcessProps) => {
const [displayedSteps, setDisplayedSteps] = useState<string[]>([]);
const [isComplete, setIsComplete] = useState(false);
const parseSteps = (content: React.ReactNode): string[] => {
if (typeof content !== 'string') {
return [];
}
const lines = content.split('\n').filter((line) => line.trim());
const steps: string[] = [];
lines.forEach((line) => {
const trimmed = line.trim();
const numberedMatch = trimmed.match(/^\d+\.\s*(.+)$/);
if (numberedMatch) {
steps.push(numberedMatch[1]);
return;
}
const bulletMatch = trimmed.match(/^[-*]\s*(.+)$/);
if (bulletMatch) {
steps.push(bulletMatch[1]);
return;
}
if (trimmed.length > 0) {
steps.push(trimmed);
}
});
return steps;
};
const steps = parseSteps(children);
useEffect(() => {
if (steps.length === 0) {
return undefined;
}
// If not streaming, show all steps immediately
if (!isStreaming) {
setDisplayedSteps(steps);
setIsComplete(true);
return undefined;
}
// If streaming, show steps progressively
setDisplayedSteps([]);
setIsComplete(false);
let currentIndex = 0;
const interval = setInterval(() => {
if (currentIndex < steps.length) {
setDisplayedSteps((prev) => [...prev, steps[currentIndex]]);
currentIndex++;
} else {
setIsComplete(true);
clearInterval(interval);
}
}, 300); // Show a new step every 300ms
return () => clearInterval(interval);
}, [children, isStreaming]);
if (steps.length === 0) {
return null;
}
return (
<div className="thinking-process my-4 p-4 thinking-glow rounded-lg shadow-sm border border-codinit-elements-glow-thinking-base transition-all duration-300">
<div className="flex items-center gap-2 mb-3">
<div
className={`i-ph:lightbulb-duotone text-xl ${isComplete ? 'text-codinit-elements-glow-thinking-secondary' : 'text-codinit-elements-glow-thinking-primary animate-pulse'}`}
/>
{isComplete ? (
<span className="text-sm font-semibold text-codinit-elements-glow-thinking-secondary">Reasoning Process</span>
) : (
<TextShimmer
as="span"
className="text-sm font-semibold text-codinit-elements-glow-thinking-secondary"
duration={2}
spread={1.5}
>
Thinking...
</TextShimmer>
)}
{!isComplete && (
<div className="flex gap-1 ml-2">
<span
className="w-1.5 h-1.5 bg-codinit-elements-glow-thinking-primary rounded-full animate-bounce"
style={{ animationDelay: '0ms' }}
/>
<span
className="w-1.5 h-1.5 bg-codinit-elements-glow-thinking-primary rounded-full animate-bounce"
style={{ animationDelay: '150ms' }}
/>
<span
className="w-1.5 h-1.5 bg-codinit-elements-glow-thinking-primary rounded-full animate-bounce"
style={{ animationDelay: '300ms' }}
/>
</div>
)}
</div>
<div className="space-y-2">
{displayedSteps.map((step, index) => (
<div
key={index}
className="flex items-start gap-3 group animate-in fade-in slide-in-from-left-2 duration-300"
style={{ animationDelay: `${index * 50}ms` }}
>
<div
className={`flex-shrink-0 w-6 h-6 rounded-full text-codinit-elements-textPrimary text-xs font-bold flex items-center justify-center mt-0.5 transition-all duration-300 ${
isComplete
? 'bg-codinit-elements-glow-thinking-secondary'
: index === displayedSteps.length - 1
? 'bg-codinit-elements-glow-thinking-primary animate-pulse'
: 'bg-codinit-elements-glow-thinking-secondary'
}`}
>
{isComplete || index < displayedSteps.length - 1 ? (
index + 1
) : (
<div className="i-ph:circle-notch-bold animate-spin text-sm" />
)}
</div>
<div className="flex-1 text-sm text-codinit-elements-textPrimary leading-relaxed pt-0.5">
{!isComplete && index === displayedSteps.length - 1 ? (
<TextShimmer as="span" duration={2.5} spread={2}>
{step}
</TextShimmer>
) : (
step
)}
</div>
</div>
))}
{!isComplete && displayedSteps.length < steps.length && (
<div className="flex items-start gap-3 opacity-40">
<div className="flex-shrink-0 w-6 h-6 rounded-full bg-codinit-elements-bg-depth-3 text-codinit-elements-textTertiary text-xs font-bold flex items-center justify-center mt-0.5">
<div className="w-2 h-2 rounded-full bg-codinit-elements-textTertiary/50 animate-pulse" />
</div>
<div className="flex-1 text-sm text-codinit-elements-textSecondary leading-relaxed pt-0.5 italic">
Processing next step...
</div>
</div>
)}
</div>
{isComplete && (
<div className="mt-3 pt-3 border-t border-codinit-elements-glow-thinking-base flex items-center gap-2 text-xs text-codinit-elements-glow-thinking-secondary animate-in fade-in duration-500">
<div className="i-ph:check-circle-duotone text-base" />
<span>Analysis complete</span>
</div>
)}
</div>
);
});