File size: 12,441 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d10fd4
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d10fd4
c2ea5ed
 
b522e95
 
 
 
 
c2ea5ed
 
6d10fd4
c2ea5ed
 
6d10fd4
 
c2ea5ed
6d10fd4
c2ea5ed
 
6d10fd4
 
c2ea5ed
 
 
 
 
 
 
 
 
6d10fd4
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a5132a
 
 
 
 
b522e95
 
c2ea5ed
 
 
 
 
 
 
 
 
 
7edeff9
c2ea5ed
 
 
 
 
 
 
 
 
 
 
5a5132a
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from "react";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Brain, Zap, Star, Cpu, FlaskConical } from "lucide-react";
import { api } from "@/lib/api";

export type MethodType = "production" | string; // Allow for baseline method names

interface MethodOption {
  id: string;
  name: string;
  description: string;
  method_type: "production" | "baseline";
  schema_type: "reference_based" | "direct_based";
  supported_features: string[];
  processing_type: "async_crew" | "direct_call";
  recommended?: boolean;
}

interface MethodSelectionModalProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onConfirm: (methodName: string) => void;
  onBack: () => void;
  isLoading?: boolean;
  selectedSplitter: string;
}

export function MethodSelectionModal({
  open,
  onOpenChange,
  onConfirm,
  onBack,
  isLoading = false,
  selectedSplitter,
}: MethodSelectionModalProps) {
  const [selectedMethod, setSelectedMethod] = useState<string>("openai_structured");
  const [methods, setMethods] = useState<MethodOption[]>([]);
  const [loadingMethods, setLoadingMethods] = useState(false);

  // Fetch available methods when modal opens
  useEffect(() => {
    if (open) {
      fetchMethods();
    }
  }, [open]);

  const fetchMethods = async () => {
    setLoadingMethods(true);
    try {
      const response = await api.methods.getAvailable();
      const methodList = Object.entries(response.methods).map(
        ([id, method]: [string, any]) => ({
          id,
          ...method,
          recommended: id === "openai_structured",
        })
      );
      // HuggingFace UI: hide all baseline methods from selection
      const productionOnly = methodList.filter(
        (m: any) => m.method_type === "production"
      );
      setMethods(productionOnly);
    } catch (error) {
      console.error("Failed to fetch methods:", error);
      // Fallback to default method
      setMethods([
        {
          id: "openai_structured",
          name: "OpenAI Structured Outputs",
          description:
            "Simple OpenAI structured outputs extractor using Pydantic models",
          method_type: "production",
          schema_type: "reference_based",
          supported_features: ["structured_outputs", "direct_extraction"],
          processing_type: "direct_call",
          recommended: true,
        },
      ]);
    } finally {
      setLoadingMethods(false);
    }
  };

  const handleConfirm = () => {
    const finalMethodName = selectedMethod || "openai_structured";
    console.log("MethodSelectionModal: selectedMethod state:", selectedMethod);
    console.log("MethodSelectionModal: finalMethodName:", finalMethodName);
    console.log(
      "MethodSelectionModal: About to call onConfirm with:",
      finalMethodName
    );
    onConfirm(finalMethodName);
  };

  const getMethodIcon = (method: MethodOption) => {
    if (method.method_type === "production") {
      return Brain;
    }
    if (method.supported_features.includes("clustering")) {
      return Cpu;
    }
    if (method.supported_features.includes("llm")) {
      return Zap;
    }
    return FlaskConical;
  };

  const getSplitterDisplayName = (splitterType: string) => {
    switch (splitterType) {
      case "agent_semantic":
        return "Agent Semantic Splitter";
      case "json":
        return "JSON Splitter";
      case "prompt_interaction":
        return "Prompt Interaction Splitter";
      default:
        return splitterType;
    }
  };

  if (loadingMethods) {
    return (
      <Dialog open={open} onOpenChange={onOpenChange}>
        <DialogContent className="sm:max-w-2xl">
          <div className="flex items-center justify-center p-8">
            <div className="w-6 h-6 border-2 border-primary border-t-transparent rounded-full animate-spin mr-3" />
            Loading extraction methods...
          </div>
        </DialogContent>
      </Dialog>
    );
  }

  // Group methods by type
  const productionMethods = methods.filter(
    (m) => m.method_type === "production"
  );
  // Ensure display order: recommended (openai_structured) first
  const sortedProductionMethods = [...productionMethods].sort((a, b) => {
    if (!!a.recommended === !!b.recommended) return 0;
    return a.recommended ? -1 : 1;
  });
  // Hide baseline methods in the HuggingFace version
  const baselineMethods: MethodOption[] = [];

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-3xl max-h-[90vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <Zap className="h-5 w-5" />
            Select Extraction Method
          </DialogTitle>
          <DialogDescription>
            Choose the extraction method for knowledge graph generation:
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-6 py-4">
          {/* Production Methods */}
          {productionMethods.length > 0 && (
            <div>
              <h3 className="font-semibold text-sm text-muted-foreground mb-3 uppercase tracking-wide">
                Production Methods
              </h3>
              <div className="space-y-3">
                {sortedProductionMethods.map((method) => {
                  const Icon = getMethodIcon(method);
                  const isSelected = selectedMethod === method.id;

                  return (
                    <Card
                      key={method.id}
                      className={`cursor-pointer transition-all duration-200 hover:shadow-md ${
                        isSelected
                          ? "ring-2 ring-primary border-primary bg-primary/5"
                          : "border-border hover:border-primary/50"
                      }`}
                      onClick={() => {
                        console.log(
                          "MethodSelectionModal: Clicking baseline method:",
                          method.id,
                          method.name
                        );
                        setSelectedMethod(method.id);
                      }}
                    >
                      <CardContent className="p-4">
                        <div className="flex items-start gap-4">
                          <div
                            className={`p-3 rounded-full ${
                              isSelected
                                ? "bg-primary text-primary-foreground"
                                : "bg-muted text-muted-foreground"
                            }`}
                          >
                            <Icon className="h-5 w-5" />
                          </div>

                          <div className="flex-1 space-y-2">
                            <div className="flex items-center justify-between">
                              <h4 className="font-semibold">{method.name}</h4>
                              {method.recommended && (
                                <Badge className="bg-amber-100 text-amber-800 border-amber-200">
                                  <Star className="h-3 w-3 mr-1" />
                                  Recommended
                                </Badge>
                              )}
                            </div>

                            <p className="text-sm text-muted-foreground leading-relaxed">
                              {method.description}
                            </p>

                            <div className="flex flex-wrap gap-2">
                              {method.supported_features.map((feature) => (
                                <Badge
                                  key={feature}
                                  variant="secondary"
                                  className="text-xs"
                                >
                                  {feature.replace(/_/g, " ")}
                                </Badge>
                              ))}
                            </div>
                          </div>
                        </div>
                      </CardContent>
                    </Card>
                  );
                })}
              </div>
            </div>
          )}

          {/* Baseline Methods */}
          {baselineMethods.length > 0 && (
            <div>
              <h3 className="font-semibold text-sm text-muted-foreground mb-3 uppercase tracking-wide">
                Baseline Methods (For Comparison)
              </h3>
              <div className="space-y-3 max-h-80 overflow-y-auto">
                {baselineMethods.map((method) => {
                  const Icon = getMethodIcon(method);
                  const isSelected = selectedMethod === method.id;

                  return (
                    <Card
                      key={method.id}
                      className={`cursor-pointer transition-all duration-200 hover:shadow-md ${
                        isSelected
                          ? "ring-2 ring-primary border-primary bg-primary/5"
                          : "border-border hover:border-primary/50"
                      }`}
                      onClick={() => {
                        console.log(
                          "MethodSelectionModal: Clicking method:",
                          method.id,
                          method.name
                        );
                        setSelectedMethod(method.id);
                      }}
                    >
                      <CardContent className="p-3">
                        <div className="flex items-center gap-3">
                          <div
                            className={`p-2 rounded-full ${
                              isSelected
                                ? "bg-primary text-primary-foreground"
                                : "bg-muted text-muted-foreground"
                            }`}
                          >
                            <Icon className="h-4 w-4" />
                          </div>

                          <div className="flex-1 min-w-0">
                            <h4 className="font-medium text-sm">
                              {method.name}
                            </h4>
                            <p className="text-xs text-muted-foreground truncate">
                              {method.description}
                            </p>
                          </div>

                          <div className="flex gap-1">
                            {method.supported_features
                              .slice(0, 2)
                              .map((feature) => (
                                <Badge
                                  key={feature}
                                  variant="outline"
                                  className="text-xs"
                                >
                                  {feature.replace(/_/g, " ")}
                                </Badge>
                              ))}
                          </div>
                        </div>
                      </CardContent>
                    </Card>
                  );
                })}
              </div>
            </div>
          )}
        </div>

        <div className="flex justify-between gap-3 pt-4 border-t">
          <Button variant="outline" onClick={onBack} disabled={isLoading}>
            Back
          </Button>
          <div className="flex gap-2">
            <Button
              variant="outline"
              onClick={() => onOpenChange(false)}
              disabled={isLoading}
            >
              Cancel
            </Button>
            <Button onClick={handleConfirm} disabled={isLoading}>
              {isLoading ? (
                <>
                  <div className="w-4 h-4 border-2 border-primary border-t-transparent rounded-full animate-spin mr-2" />
                  Generating...
                </>
              ) : (
                <>
                  <Brain className="h-4 w-4 mr-2" />
                  Generate Agent Graph
                </>
              )}
            </Button>
          </div>
        </div>
      </DialogContent>
    </Dialog>
  );
}