-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathPromptsTab.tsx
More file actions
535 lines (500 loc) · 19 KB
/
PromptsTab.tsx
File metadata and controls
535 lines (500 loc) · 19 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import { useCallback, useEffect, useMemo, useState } from "react";
import { Button } from "./ui/button";
import { Input } from "./ui/input";
import { Textarea } from "./ui/textarea";
import { Badge } from "./ui/badge";
import { ScrollArea } from "./ui/scroll-area";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "./ui/select";
import {
MessageSquare,
Play,
RefreshCw,
ChevronRight,
PanelLeftClose,
} from "lucide-react";
import { EmptyState } from "./ui/empty-state";
import { ThreePanelLayout } from "./ui/three-panel-layout";
import { JsonEditor } from "@/components/ui/json-editor";
import { extractDisplayFromValue } from "@/components/chat-v2/shared/tool-result-text";
import type { MCPPrompt, MCPServerConfig } from "@mcpjam/sdk/browser";
import {
getPrompt as getPromptApi,
listPrompts as listPromptsApi,
} from "@/lib/apis/mcp-prompts-api";
import { SelectedToolHeader } from "./ui-playground/SelectedToolHeader";
interface PromptsTabProps {
serverConfig?: MCPServerConfig;
serverName?: string;
}
type PromptArgument = NonNullable<MCPPrompt["arguments"]>[number];
interface FormField {
name: string;
type: string;
description?: string;
required: boolean;
value: string | boolean;
enum?: string[];
minimum?: number;
maximum?: number;
}
export function PromptsTab({ serverConfig, serverName }: PromptsTabProps) {
const [prompts, setPrompts] = useState<MCPPrompt[]>([]);
const [selectedPrompt, setSelectedPrompt] = useState<string>("");
const [formFields, setFormFields] = useState<FormField[]>([]);
const [promptContent, setPromptContent] = useState<any>(null);
const [loading, setLoading] = useState(false);
const [fetchingPrompts, setFetchingPrompts] = useState(false);
const [error, setError] = useState<string>("");
const [isSidebarVisible, setIsSidebarVisible] = useState(true);
const selectedPromptData = useMemo(() => {
return prompts.find((prompt) => prompt.name === selectedPrompt) ?? null;
}, [prompts, selectedPrompt]);
const promptDisplay = extractDisplayFromValue(promptContent);
useEffect(() => {
if (serverConfig && serverName) {
fetchPrompts();
}
}, [serverConfig, serverName]);
useEffect(() => {
if (selectedPromptData?.arguments) {
generateFormFields(selectedPromptData.arguments);
} else {
setFormFields([]);
}
}, [selectedPromptData]);
const fetchPrompts = async () => {
if (!serverName) return;
setFetchingPrompts(true);
setError("");
try {
const serverPrompts = await listPromptsApi(serverName);
setPrompts(serverPrompts);
// Clear selection if the selected prompt no longer exists
if (
selectedPrompt &&
!serverPrompts.some((prompt) => prompt.name === selectedPrompt)
) {
setSelectedPrompt("");
setPromptContent(null);
}
} catch (err) {
const message =
err instanceof Error ? err.message : `Could not fetch prompts: ${err}`;
setError(message);
} finally {
setFetchingPrompts(false);
}
};
const generateFormFields = (args: PromptArgument[]) => {
if (!args || args.length === 0) {
setFormFields([]);
return;
}
const fields: FormField[] = args.map((arg) => ({
name: arg.name,
type: "string",
description: arg.description,
required: Boolean(arg.required),
value: "",
}));
setFormFields(fields);
};
const updateFieldValue = (fieldName: string, value: string | boolean) => {
setFormFields((prev) =>
prev.map((field) =>
field.name === fieldName ? { ...field, value } : field,
),
);
};
const buildParameters = useCallback((): Record<string, string> => {
const params: Record<string, string> = {};
formFields.forEach((field) => {
if (
field.value !== "" &&
field.value !== null &&
field.value !== undefined
) {
let processedValue: string;
if (field.type === "array" || field.type === "object") {
processedValue =
typeof field.value === "string"
? field.value
: JSON.stringify(field.value);
} else if (field.type === "boolean") {
processedValue = field.value ? "true" : "false";
} else if (field.type === "number" || field.type === "integer") {
processedValue = String(field.value);
} else {
processedValue = String(field.value);
}
params[field.name] = processedValue;
}
});
return params;
}, [formFields]);
// Get prompt - can be called with explicit promptName for auto-run on selection
const getPrompt = useCallback(
async (promptName?: string, params?: Record<string, string>) => {
const targetPrompt = promptName ?? selectedPrompt;
if (!targetPrompt || !serverName) return;
setLoading(true);
setError("");
try {
const resolvedParams = params ?? buildParameters();
const data = await getPromptApi(
serverName,
targetPrompt,
resolvedParams,
);
setPromptContent(data.content);
} catch (err) {
const message =
err instanceof Error ? err.message : `Error getting prompt: ${err}`;
setError(message);
} finally {
setLoading(false);
}
},
[selectedPrompt, serverName, buildParameters],
);
const promptNames = prompts.map((prompt) => prompt.name);
// Handle Enter key in input fields
const handleInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === "Enter" && !loading) {
e.preventDefault();
getPrompt();
}
};
// Handle Enter key to get prompt globally
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Enter" && !e.shiftKey && selectedPrompt && !loading) {
const target = e.target as HTMLElement;
const tagName = target.tagName;
const isEditable = target.isContentEditable;
if (tagName === "INPUT" || tagName === "TEXTAREA" || isEditable) {
return;
}
e.preventDefault();
getPrompt();
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [selectedPrompt, loading, getPrompt]);
if (!serverConfig || !serverName) {
return (
<EmptyState
icon={MessageSquare}
title="No Server Selected"
description="Connect to an MCP server to explore and test its available prompts."
/>
);
}
const sidebarContent = (
<div className="h-full flex flex-col border-r border-border bg-background">
{/* App Builder-style Header */}
<div className="border-b border-border flex-shrink-0">
<div className="px-2 py-1.5 flex items-center gap-2">
{/* Title */}
<div className="flex items-center gap-1.5">
<span className="px-3 py-1.5 rounded-md text-xs font-medium bg-primary/10 text-primary">
Prompts
<span className="ml-1 text-[10px] font-mono opacity-70">
{promptNames.length}
</span>
</span>
</div>
{/* Secondary actions */}
<div className="flex items-center gap-0.5 text-muted-foreground/80">
<Button
onClick={fetchPrompts}
variant="ghost"
size="sm"
disabled={fetchingPrompts}
className="h-7 w-7 p-0"
title="Refresh prompts"
>
<RefreshCw
className={`h-3.5 w-3.5 ${fetchingPrompts ? "animate-spin" : ""}`}
/>
</Button>
<Button
onClick={() => setIsSidebarVisible(false)}
variant="ghost"
size="sm"
className="h-7 w-7 p-0"
title="Hide sidebar"
>
<PanelLeftClose className="h-3.5 w-3.5" />
</Button>
</div>
{/* Run button */}
<Button
onClick={() => getPrompt()}
disabled={loading || !selectedPrompt}
size="sm"
className="h-8 px-3 text-xs ml-auto"
>
{loading ? (
<RefreshCw className="h-3 w-3 animate-spin" />
) : (
<Play className="h-3 w-3" />
)}
<span className="ml-1">{loading ? "Loading" : "Run"}</span>
</Button>
</div>
</div>
{/* Content */}
{selectedPrompt ? (
/* Parameters View when prompt is selected */
<div className="flex-1 flex flex-col min-h-0">
<SelectedToolHeader
toolName={selectedPrompt}
description={selectedPromptData?.description}
onExpand={() => setSelectedPrompt("")}
onClear={() => setSelectedPrompt("")}
/>
<ScrollArea className="flex-1">
<div className="px-3 py-3">
{formFields.length === 0 ? (
<p className="text-xs text-muted-foreground text-center py-8">
No parameters required
</p>
) : (
<div className="space-y-3">
{formFields.map((field) => (
<div key={field.name} className="space-y-1.5">
<div className="flex items-center gap-2">
<code className="font-mono text-xs font-medium text-foreground">
{field.name}
</code>
{field.required && (
<Badge
variant="outline"
className="text-[9px] px-1 py-0 border-amber-500/50 text-amber-600 dark:text-amber-400"
>
required
</Badge>
)}
</div>
{field.description && (
<p className="text-[10px] text-muted-foreground leading-relaxed">
{field.description}
</p>
)}
<div className="pt-0.5">
{field.type === "enum" ? (
<Select
value={String(field.value)}
onValueChange={(value) =>
updateFieldValue(field.name, value)
}
>
<SelectTrigger className="w-full h-8 bg-background border-border text-xs">
<SelectValue placeholder="Select an option" />
</SelectTrigger>
<SelectContent>
{field.enum?.map((option) => (
<SelectItem key={option} value={option}>
{option}
</SelectItem>
))}
</SelectContent>
</Select>
) : field.type === "boolean" ? (
<div className="flex items-center gap-2 h-8">
<input
type="checkbox"
checked={field.value === true}
onChange={(e) =>
updateFieldValue(field.name, e.target.checked)
}
className="w-4 h-4 rounded border-border accent-primary"
/>
<span className="text-xs text-foreground">
{field.value === true ? "true" : "false"}
</span>
</div>
) : field.type === "array" ||
field.type === "object" ? (
<Textarea
value={
typeof field.value === "string"
? field.value
: JSON.stringify(field.value, null, 2)
}
onChange={(e) =>
updateFieldValue(field.name, e.target.value)
}
placeholder={`Enter ${field.type} as JSON`}
className="font-mono text-xs min-h-[80px] bg-background border-border resize-y"
/>
) : (
<Input
type={
field.type === "number" ||
field.type === "integer"
? "number"
: "text"
}
value={String(field.value)}
onChange={(e) =>
updateFieldValue(field.name, e.target.value)
}
onKeyDown={handleInputKeyDown}
placeholder={`Enter ${field.name}`}
className="bg-background border-border text-xs h-8"
/>
)}
</div>
</div>
))}
</div>
)}
</div>
</ScrollArea>
</div>
) : (
/* Prompts List when no prompt is selected */
<div className="flex-1 overflow-hidden">
<ScrollArea className="h-full">
<div className="p-2">
{fetchingPrompts ? (
<div className="flex flex-col items-center justify-center py-16 text-center">
<div className="w-8 h-8 bg-muted rounded-full flex items-center justify-center mb-3">
<RefreshCw className="h-4 w-4 text-muted-foreground animate-spin" />
</div>
<p className="text-xs text-muted-foreground font-semibold mb-1">
Loading prompts...
</p>
<p className="text-xs text-muted-foreground/70">
Fetching available prompts from server
</p>
</div>
) : promptNames.length === 0 ? (
<div className="text-center py-8">
<p className="text-sm text-muted-foreground">
No prompts available
</p>
</div>
) : (
<div className="space-y-1">
{prompts.map((prompt) => {
const displayTitle = prompt.title ?? prompt.name;
return (
<div
key={prompt.name}
className="cursor-pointer transition-all duration-200 hover:bg-muted/30 dark:hover:bg-muted/50 p-3 rounded-md mx-2 hover:shadow-sm"
onClick={() => {
setSelectedPrompt(prompt.name);
// Auto-run if no arguments required
if (
!prompt.arguments ||
prompt.arguments.length === 0
) {
getPrompt(prompt.name, {});
}
}}
>
<div className="flex items-start gap-3">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<code className="font-mono text-xs font-medium text-foreground bg-muted px-1.5 py-0.5 rounded border border-border">
{prompt.name}
</code>
{prompt.title && (
<span className="text-xs font-semibold text-foreground">
{displayTitle}
</span>
)}
</div>
{prompt.description && (
<p className="text-xs mt-2 line-clamp-2 leading-relaxed text-muted-foreground">
{prompt.description}
</p>
)}
</div>
<ChevronRight className="h-3 w-3 text-muted-foreground flex-shrink-0 mt-1" />
</div>
</div>
);
})}
</div>
)}
</div>
</ScrollArea>
</div>
)}
</div>
);
const centerContent = (
<div className="h-full flex flex-col bg-background">
<div className="flex-1 min-h-0 flex flex-col">
{error ? (
<div className="p-4">
<div className="p-3 bg-destructive/10 border border-destructive/20 rounded text-destructive text-xs font-medium">
{error}
</div>
</div>
) : promptContent !== null && promptContent !== undefined ? (
<div className="flex-1 min-h-0 p-4 flex flex-col">
{promptDisplay?.kind === "text" ? (
<pre className="flex-1 min-h-0 whitespace-pre-wrap text-xs font-mono bg-muted/30 p-4 rounded-md border border-border overflow-auto">
{promptDisplay.text}
</pre>
) : (
<div className="flex-1 min-h-0">
<JsonEditor
value={
promptDisplay?.kind === "json"
? promptDisplay.value
: promptContent
}
readOnly
showToolbar={false}
height="100%"
/>
</div>
)}
</div>
) : (
<div className="h-full flex items-center justify-center">
<div className="text-center">
<div className="w-12 h-12 bg-muted rounded-full flex items-center justify-center mx-auto mb-3">
<MessageSquare className="h-5 w-5 text-muted-foreground" />
</div>
<p className="text-xs font-semibold text-foreground mb-1">
{selectedPrompt ? "Response" : "No selection"}
</p>
<p className="text-xs text-muted-foreground font-medium">
{selectedPrompt
? formFields.length > 0
? "Fill in parameters and click Run"
: "Loading..."
: "Select a prompt from the sidebar"}
</p>
</div>
</div>
)}
</div>
</div>
);
return (
<ThreePanelLayout
id="prompts"
sidebar={sidebarContent}
content={centerContent}
sidebarVisible={isSidebarVisible}
onSidebarVisibilityChange={setIsSidebarVisible}
sidebarTooltip="Show prompts sidebar"
serverName={serverName}
/>
);
}