File size: 12,688 Bytes
6a059d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**

 * Thinking Process Component

 * 

 * Displays the agent's reasoning path including:

 * - Step-by-step reasoning (Claude, o1 style)

 * - Tool execution visualization

 * - Re-ranking progress

 * - HyDE query expansion

 * - Knowledge base indicators

 */

"use client"

import { useState } from "react"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Progress } from "@/components/ui/progress"
import { 
  ChevronDown, 
  ChevronRight,
  Brain,
  Check,
  Clock,
  Search,
  Lightbulb,
  AlertCircle,
  TrendingUp,
  Eye,
  Layers,
  Zap,
  Database,
  RefreshCw,
  Filter,
  Sparkles,
  FileText,
  Scale,
  Newspaper
} from "lucide-react"
import { cn } from "@/lib/utils"

// Enhanced Types
interface ToolExecution {
  name: string
  status: 'pending' | 'running' | 'completed' | 'error'
  input?: Record<string, unknown>
  output?: unknown
  duration_ms?: number
}

interface RetrievalStep {
  namespace: string
  docs_retrieved: number
  docs_after_rerank?: number
  used_hyde?: boolean
  query_expanded?: string
}

interface ThoughtStep {
  step: number
  action: string
  observation: string
  reasoning: string
  duration_ms?: number
  confidence?: number
  tool_executions?: ToolExecution[]
  retrieval?: RetrievalStep
}

interface ReasoningPath {
  query: string
  thoughts: ThoughtStep[]
  total_duration_ms: number
  final_conclusion: string
  knowledge_bases_used?: string[]
  used_reranking?: boolean
  used_hyde?: boolean
  model_used?: string
}

interface ThinkingProcessProps {
  reasoning: ReasoningPath | string
  className?: string
  defaultExpanded?: boolean
  showEnhancements?: boolean
}

export function ThinkingProcess({ 

  reasoning, 

  className, 

  defaultExpanded = false 

}: ThinkingProcessProps) {
  const [isExpanded, setIsExpanded] = useState(defaultExpanded)
  const [expandedSteps, setExpandedSteps] = useState<Set<number>>(new Set())

  // Handle string reasoning (new format)
  if (typeof reasoning === 'string') {
    return (
      <Card className={cn("border-2 border-primary/20", className)}>

        <CardHeader 

          className="cursor-pointer hover:bg-accent/50 transition-colors py-3"

          onClick={() => setIsExpanded(!isExpanded)}

        >

          <div className="flex items-center justify-between">

            <div className="flex items-center gap-2">

              <Brain className="h-5 w-5 text-primary" />

              <CardTitle className="text-base">Thinking Process</CardTitle>

            </div>

            <div className="flex items-center gap-3">

              <Badge variant="secondary" className="font-mono text-xs">

                Analysis

              </Badge>

              {isExpanded ? (

                <ChevronDown className="h-5 w-5 text-muted-foreground" />

              ) : (

                <ChevronRight className="h-5 w-5 text-muted-foreground" />

              )}

            </div>

          </div>

        </CardHeader>



        {isExpanded && (

          <CardContent className="pt-0 pb-4">

            <ScrollArea className="max-h-[400px]">

              <div className="bg-muted/30 p-4 rounded-lg text-sm text-muted-foreground whitespace-pre-wrap font-mono leading-relaxed">

                {reasoning}

              </div>

            </ScrollArea>

          </CardContent>

        )}

      </Card>
    )
  }

  // Handle structured reasoning (legacy/future format)
  const toggleStep = (step: number) => {
    const newExpanded = new Set(expandedSteps)
    if (newExpanded.has(step)) {
      newExpanded.delete(step)
    } else {
      newExpanded.add(step)
    }
    setExpandedSteps(newExpanded)
  }

  const toggleAll = () => {
    if (isExpanded) {
      setIsExpanded(false)
      setExpandedSteps(new Set())
    } else {
      setIsExpanded(true)
      setExpandedSteps(new Set(reasoning.thoughts.map(t => t.step)))
    }
  }

  return (
    <Card className={cn("border-2 border-primary/20", className)}>

      <CardHeader 

        className="cursor-pointer hover:bg-accent/50 transition-colors"

        onClick={toggleAll}

      >

        <div className="flex items-center justify-between">

          <div className="flex items-center gap-2">

            <Brain className="h-5 w-5 text-primary" />

            <CardTitle className="text-lg">Thinking Process</CardTitle>

          </div>

          <div className="flex items-center gap-3">

            <Badge variant="secondary" className="font-mono">

              {reasoning.thoughts.length} steps

            </Badge>

            <Badge variant="outline" className="font-mono">

              {reasoning.total_duration_ms}ms

            </Badge>

            {isExpanded ? (

              <ChevronDown className="h-5 w-5 text-muted-foreground" />

            ) : (

              <ChevronRight className="h-5 w-5 text-muted-foreground" />

            )}

          </div>

        </div>

        <CardDescription className="mt-2">

          Step-by-step reasoning for: <span className="font-medium">{reasoning.query}</span>

        </CardDescription>

      </CardHeader>



      {isExpanded && (

        <CardContent className="pt-0">

          <ScrollArea className="max-h-[600px]">

            <div className="space-y-3 relative">

              {/* Timeline line */}

              <div className="absolute left-[17px] top-4 bottom-4 w-[2px] bg-border" />



              {reasoning.thoughts.map((thought, idx) => (

                <ThoughtStepCard

                  key={thought.step}

                  thought={thought}

                  isExpanded={expandedSteps.has(thought.step)}

                  onToggle={() => toggleStep(thought.step)}

                  isLast={idx === reasoning.thoughts.length - 1}

                />

              ))}



              {/* Final Conclusion */}

              <div className="ml-12 p-4 rounded-lg bg-primary/5 border-2 border-primary/20">

                <div className="flex items-center gap-2 mb-2">

                  <Check className="h-5 w-5 text-green-500" />

                  <h4 className="font-semibold">Conclusion</h4>

                </div>

                <p className="text-sm text-muted-foreground">{reasoning.final_conclusion}</p>

              </div>

            </div>

          </ScrollArea>

        </CardContent>

      )}

    </Card>
  )
}

// Individual Thought Step Card
function ThoughtStepCard({

  thought,

  isExpanded,

  onToggle,

  isLast

}: {

  thought: ThoughtStep

  isExpanded: boolean

  onToggle: () => void

  isLast: boolean

}) {
  const getActionIcon = (action: string) => {
    const actionLower = action.toLowerCase()
    if (actionLower.includes("search") || actionLower.includes("retriev")) {
      return <Search className="h-4 w-4" />
    }
    if (actionLower.includes("analyz") || actionLower.includes("check")) {
      return <Eye className="h-4 w-4" />
    }
    if (actionLower.includes("reason") || actionLower.includes("think")) {
      return <Lightbulb className="h-4 w-4" />
    }
    if (actionLower.includes("synthesiz") || actionLower.includes("combin")) {
      return <Layers className="h-4 w-4" />
    }
    return <TrendingUp className="h-4 w-4" />
  }

  return (
    <div className="relative">

      {/* Step number bubble */}

      <div className="absolute left-0 top-3 w-[34px] h-[34px] rounded-full bg-primary flex items-center justify-center text-white font-bold text-sm z-10">

        {thought.step}

      </div>



      {/* Step card */}

      <div className="ml-12 relative">

        <div 

          className={cn(

            "border rounded-lg transition-all cursor-pointer",

            isExpanded ? "bg-accent/50 border-primary/30" : "bg-card hover:bg-accent/30"

          )}

          onClick={onToggle}

        >

          {/* Header */}

          <div className="p-3 flex items-center justify-between">

            <div className="flex items-center gap-2 flex-1">

              {getActionIcon(thought.action)}

              <span className="font-medium text-sm">{thought.action}</span>

            </div>

            <div className="flex items-center gap-2">

              {thought.duration_ms && (

                <Badge variant="outline" className="text-xs font-mono">

                  <Clock className="h-3 w-3 mr-1" />

                  {thought.duration_ms}ms

                </Badge>

              )}

              {thought.confidence !== undefined && (

                <Badge 

                  variant={thought.confidence >= 0.8 ? "default" : "secondary"}

                  className="text-xs font-mono"

                >

                  {(thought.confidence * 100).toFixed(0)}%

                </Badge>

              )}

              {isExpanded ? (

                <ChevronDown className="h-4 w-4 text-muted-foreground" />

              ) : (

                <ChevronRight className="h-4 w-4 text-muted-foreground" />

              )}

            </div>

          </div>



          {/* Expanded content */}

          {isExpanded && (

            <div className="px-3 pb-3 space-y-3 border-t">

              {/* Observation */}

              <div className="pt-3">

                <div className="flex items-center gap-2 mb-1">

                  <Eye className="h-3.5 w-3.5 text-muted-foreground" />

                  <span className="text-xs font-semibold text-muted-foreground uppercase tracking-wide">

                    Observation

                  </span>

                </div>

                <p className="text-sm ml-5">{thought.observation}</p>

              </div>



              {/* Reasoning */}

              <div>

                <div className="flex items-center gap-2 mb-1">

                  <Lightbulb className="h-3.5 w-3.5 text-muted-foreground" />

                  <span className="text-xs font-semibold text-muted-foreground uppercase tracking-wide">

                    Reasoning

                  </span>

                </div>

                <p className="text-sm ml-5 text-muted-foreground">{thought.reasoning}</p>

              </div>

            </div>

          )}

        </div>

      </div>

    </div>
  )
}

// Compact version for inline display
export function ThinkingProcessCompact({ 

  reasoning 

}: { 

  reasoning: ReasoningPath 

}) {
  const [isExpanded, setIsExpanded] = useState(false)

  return (
    <div className="border-l-2 border-primary/20 pl-4 py-2">

      <button

        onClick={() => setIsExpanded(!isExpanded)}

        className="flex items-center gap-2 text-sm font-medium hover:text-primary transition-colors"

      >

        <Brain className="h-4 w-4" />

        <span>View thinking process ({reasoning.thoughts.length} steps)</span>

        {isExpanded ? (

          <ChevronDown className="h-4 w-4" />

        ) : (

          <ChevronRight className="h-4 w-4" />

        )}

      </button>



      {isExpanded && (

        <div className="mt-3 space-y-2">

          {reasoning.thoughts.map((thought) => (

            <div key={thought.step} className="text-sm">

              <span className="font-semibold">{thought.step}.</span> {thought.action}

              <p className="text-muted-foreground ml-4 text-xs">{thought.observation}</p>

            </div>

          ))}

        </div>

      )}

    </div>
  )
}

// Loading skeleton
export function ThinkingProcessSkeleton() {
  return (
    <Card className="border-2 border-primary/20">

      <CardHeader>

        <div className="flex items-center justify-between">

          <div className="flex items-center gap-2">

            <Brain className="h-5 w-5 text-primary animate-pulse" />

            <CardTitle className="text-lg">Thinking...</CardTitle>

          </div>

          <Badge variant="secondary" className="animate-pulse">

            Processing

          </Badge>

        </div>

      </CardHeader>

      <CardContent>

        <div className="space-y-3">

          {[1, 2, 3].map((i) => (

            <div key={i} className="flex items-center gap-3">

              <div className="w-8 h-8 rounded-full bg-primary/20 animate-pulse" />

              <div className="flex-1 h-12 bg-accent/50 rounded-lg animate-pulse" />

            </div>

          ))}

        </div>

      </CardContent>

    </Card>
  )
}

export default ThinkingProcess