summaryrefslogtreecommitdiff
path: root/webapp/components/tool-configuration-dialog.tsx
blob: 7d3b9ea028cb362fb9d2590001782235f968fa4f (plain)
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
import React from "react";
import { Button } from "@/components/ui/button";
import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { toolTemplates } from "@/lib/tool-templates";
import { BackendTag } from "./backend-tag";

interface ToolConfigurationDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  editingIndex: number | null;
  selectedTemplate: string;
  editingSchemaStr: string;
  isJsonValid: boolean;
  onTemplateChange: (val: string) => void;
  onSchemaChange: (val: string) => void;
  onSave: () => void;
  backendTools: any[]; // schemas returned from the server
}

export const ToolConfigurationDialog: React.FC<
  ToolConfigurationDialogProps
> = ({
  open,
  onOpenChange,
  editingIndex,
  selectedTemplate,
  editingSchemaStr,
  isJsonValid,
  onTemplateChange,
  onSchemaChange,
  onSave,
  backendTools,
}) => {
  // Combine local templates and backend templates
  const localTemplateOptions = toolTemplates.map((template) => ({
    ...template,
    source: "local",
  }));

  const backendTemplateOptions = backendTools.map((t: any) => ({
    ...t,
    source: "backend",
  }));

  const allTemplates = [...localTemplateOptions, ...backendTemplateOptions];

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-lg">
        <DialogHeader>
          <DialogTitle>
            {editingIndex === null ? "Add Tool" : "Edit Tool"}
          </DialogTitle>
        </DialogHeader>
        <div className="space-y-4 py-4">
          <Select value={selectedTemplate} onValueChange={onTemplateChange}>
            <SelectTrigger>
              <SelectValue placeholder="Select a template (optional)" />
            </SelectTrigger>
            <SelectContent>
              {allTemplates.map((template) => (
                <SelectItem key={template.name} value={template.name}>
                  <span className="flex items-center">
                    {template.name}
                    {template.source === "backend" && <BackendTag />}
                  </span>
                </SelectItem>
              ))}
            </SelectContent>
          </Select>

          <Textarea
            className="min-h-[200px] font-mono text-sm"
            value={editingSchemaStr}
            onChange={(e) => onSchemaChange(e.target.value)}
            placeholder="Enter tool JSON schema"
          />
        </div>
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            Cancel
          </Button>
          <Button onClick={onSave} disabled={!isJsonValid}>
            Save Changes
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};