File size: 10,433 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f95be4
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7edeff9
 
 
7e807a3
 
 
 
 
7edeff9
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
import React, { useState } from "react";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
  Brain,
  Code,
  MessageSquare,
  Star,
  Database,
  ArrowRightLeft,
  Settings,
} from "lucide-react";
import { MethodSelectionModal } from "./MethodSelectionModal";
import { useModelPreferences } from "@/hooks/useModelPreferences";

export type SplitterType = "agent_semantic" | "json" | "prompt_interaction";

export interface ChunkingConfig {
  min_chunk_size?: number;
  max_chunk_size?: number;
}

interface SplitterOption {
  id: SplitterType;
  name: string;
  description: string;
  icon: React.ElementType;
  badge?: {
    text: string;
    icon: React.ElementType;
  };
  recommended?: boolean;
}

interface SplitterSelectionModalProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onConfirm: (
    splitterType: SplitterType,
    methodName?: string,
    modelName?: string,
    chunkingConfig?: ChunkingConfig
  ) => void;
  isLoading?: boolean;
}

// HF Spaces variant: only expose the Agent Semantic Splitter
const SPLITTER_OPTIONS: SplitterOption[] = [
  {
    id: "agent_semantic",
    name: "Agent Semantic Splitter",
    description:
      "Intelligently splits content based on semantic meaning and agent interactions. Best for conversational traces and complex content.",
    icon: Brain,
    badge: {
      text: "Recommended",
      icon: Star,
    },
    recommended: true,
  },
];

export function SplitterSelectionModal({
  open,
  onOpenChange,
  onConfirm,
  isLoading = false,
}: SplitterSelectionModalProps) {
  const [selectedSplitter, setSelectedSplitter] =
    useState<SplitterType>("agent_semantic");
  const [showMethodSelection, setShowMethodSelection] = useState(false);
  const [chunkingConfig, setChunkingConfig] = useState<ChunkingConfig>({
    min_chunk_size: 100000, // 100K chars default
    max_chunk_size: 300000, // 300K chars default
  });
  const { selectedModel } = useModelPreferences();

  const handleSplitterConfirm = () => {
    setShowMethodSelection(true);
  };

  const handleMethodConfirm = (methodName: string) => {
    const finalMethodName = methodName || "production";
    console.log("SplitterSelectionModal: Final method name:", finalMethodName);
    console.log("SplitterSelectionModal: Selected model:", selectedModel);
    console.log("SplitterSelectionModal: Chunking config:", chunkingConfig);

    // Close the modal immediately when user clicks Generate Agent Graph
    setShowMethodSelection(false);
    onOpenChange(false);

    // Then trigger the generation process with model parameter and chunking config
    onConfirm(selectedSplitter, finalMethodName, selectedModel, chunkingConfig);
  };

  const handleMethodBack = () => {
    setShowMethodSelection(false);
  };

  const handleCancel = () => {
    setShowMethodSelection(false);
    onOpenChange(false);
  };

  // Reset state when modal closes
  const handleOpenChange = (open: boolean) => {
    if (!open) {
      setShowMethodSelection(false);
    }
    onOpenChange(open);
  };

  return (
    <>
      <Dialog
        open={open && !showMethodSelection}
        onOpenChange={handleOpenChange}
      >
        <DialogContent className="sm:max-w-2xl">
          <DialogHeader>
            <DialogTitle className="flex items-center gap-2">
              <Brain className="h-5 w-5" />
              Select Chunking Strategy
            </DialogTitle>
            <DialogDescription>
              Choose how you want to split your trace content for knowledge
              graph generation:
            </DialogDescription>
          </DialogHeader>

          <div className="space-y-4 py-4">
            {SPLITTER_OPTIONS.map((option) => {
              const Icon = option.icon;
              const BadgeIcon = option.badge?.icon;
              const isSelected = selectedSplitter === option.id;

              return (
                <Card
                  key={option.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={() => setSelectedSplitter(option.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">{option.name}</h4>
                          {option.badge && (
                            <Badge
                              variant={
                                option.recommended ? "default" : "secondary"
                              }
                              className={`${
                                option.recommended
                                  ? "bg-amber-100 text-amber-800 border-amber-200"
                                  : ""
                              }`}
                            >
                              {BadgeIcon && (
                                <BadgeIcon className="h-3 w-3 mr-1" />
                              )}
                              {option.badge.text}
                            </Badge>
                          )}
                        </div>

                        <p className="text-sm text-muted-foreground leading-relaxed">
                          {option.description}
                        </p>
                      </div>
                    </div>
                  </CardContent>
                </Card>
              );
            })}
          </div>

          {/* Configuration for Agent Semantic Splitter */}
          {selectedSplitter === "agent_semantic" && (
            <div className="space-y-4 py-4 border-t">
              <div className="flex items-center gap-2 mb-3">
                <Settings className="h-4 w-4 text-muted-foreground" />
                <h4 className="font-medium">Configuration</h4>
              </div>

              <div className="grid grid-cols-2 gap-4">
                <div className="space-y-2">
                  <Label htmlFor="min-chunk-size">
                    Minimum Characters
                    <span className="text-xs text-muted-foreground ml-1">
                      (≈25K tokens)
                    </span>
                  </Label>
                  <Input
                    id="min-chunk-size"
                    type="number"
                    min="10000"
                    max="1000000"
                    step="10000"
                    value={chunkingConfig.min_chunk_size || ""}
                    onChange={(e) =>
                      setChunkingConfig((prev) => ({
                        ...prev,
                        min_chunk_size: parseInt(e.target.value) || 100000,
                      }))
                    }
                    placeholder="100000"
                    className="text-sm"
                  />
                  <p className="text-xs text-muted-foreground">
                    Minimum chunk size in characters
                  </p>
                </div>

                <div className="space-y-2">
                  <Label htmlFor="max-chunk-size">
                    Maximum Characters
                    <span className="text-xs text-muted-foreground ml-1">
                      (≈75K tokens)
                    </span>
                  </Label>
                  <Input
                    id="max-chunk-size"
                    type="number"
                    min="50000"
                    max="2000000"
                    step="10000"
                    value={chunkingConfig.max_chunk_size || ""}
                    onChange={(e) =>
                      setChunkingConfig((prev) => ({
                        ...prev,
                        max_chunk_size: parseInt(e.target.value) || 300000,
                      }))
                    }
                    placeholder="300000"
                    className="text-sm"
                  />
                  <p className="text-xs text-muted-foreground">
                    Maximum chunk size in characters
                  </p>
                </div>
              </div>

              <div className="bg-muted/30 rounded-lg p-3">
                <div className="flex items-center gap-2">
                  <Brain className="h-4 w-4 text-blue-500" />
                  <p className="text-xs text-muted-foreground">
                    <span className="font-medium text-foreground">
                      Smart Chunking:
                    </span>{" "}
                    Balance context preservation with processing speed -
                    defaults optimized for most traces.
                  </p>
                </div>
              </div>
            </div>
          )}

          <div className="flex justify-end gap-3 pt-4 border-t">
            <Button
              variant="outline"
              onClick={handleCancel}
              disabled={isLoading}
            >
              Cancel
            </Button>
            <Button onClick={handleSplitterConfirm} disabled={isLoading}>
              <Brain className="h-4 w-4 mr-2" />
              Next: Select Method
            </Button>
          </div>
        </DialogContent>
      </Dialog>

      <MethodSelectionModal
        open={showMethodSelection}
        onOpenChange={handleCancel}
        onConfirm={handleMethodConfirm}
        onBack={handleMethodBack}
        isLoading={isLoading}
        selectedSplitter={selectedSplitter}
      />
    </>
  );
}