Spaces:
Sleeping
Sleeping
File size: 13,742 Bytes
05c5ed5 | 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 | "use client";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Label } from "@/components/ui/label";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Link, Plus, TriangleAlertIcon, VariableIcon } from "lucide-react";
import {
HttpNodeData,
HttpMethod,
OutputSchemaSourceKey,
} from "lib/ai/workflow/workflow.interface";
import { UINode } from "lib/ai/workflow/workflow.interface";
import { HttpValueInput } from "../http-value-input";
import { useState } from "react";
import { useReactFlow } from "@xyflow/react";
import { capitalizeFirstLetter, cn } from "lib/utils";
import { Textarea } from "ui/textarea";
import { VariableSelect } from "../variable-select";
import { VariableMentionItem } from "../variable-mention-item";
import { findAvailableSchemaBySource } from "lib/ai/workflow/shared.workflow";
const HTTP_METHODS: HttpMethod[] = [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"HEAD",
];
const tabs = ["basic", "headers", "query", "body"] as const;
const headerExample = [
{
key: "Content-Type",
value: "application/json",
},
{
key: "Authorization",
value: "Bearer <token>",
},
{
key: "X-API-Key",
value: "1234567890",
},
];
const queryExample = [
{
key: "page",
value: "1",
},
{
key: "limit",
value: "10",
},
{
key: "sort",
value: "created_at",
},
];
export function HttpNodeConfig({ node }: { node: UINode<any> }) {
const httpNode = node.data as HttpNodeData;
const [activeTab, setActiveTab] = useState<(typeof tabs)[number]>("basic");
const { updateNodeData, getNodes, getEdges } = useReactFlow<UINode>();
const handleUpdateNode = (updates: Partial<HttpNodeData>) => {
updateNodeData(node.id, updates);
};
const addHeader = () => {
const currentHeaders = httpNode.headers || [];
handleUpdateNode({
headers: [...currentHeaders, { key: "", value: undefined }],
});
};
const updateHeader = (index: number, key: string, value?: any) => {
const currentHeaders = httpNode.headers || [];
const newHeaders = [...currentHeaders];
newHeaders[index] = { key, value };
handleUpdateNode({ headers: newHeaders });
};
const removeHeader = (index: number) => {
const currentHeaders = httpNode.headers || [];
const newHeaders = currentHeaders.filter((_, i) => i !== index);
handleUpdateNode({ headers: newHeaders });
};
const addQueryParam = () => {
const currentQuery = httpNode.query || [];
handleUpdateNode({
query: [...currentQuery, { key: "", value: undefined }],
});
};
const updateQueryParam = (index: number, key: string, value?: any) => {
const currentQuery = httpNode.query || [];
const newQuery = [...currentQuery];
newQuery[index] = { key, value };
handleUpdateNode({ query: newQuery });
};
const removeQueryParam = (index: number) => {
const currentQuery = httpNode.query || [];
const newQuery = currentQuery.filter((_, i) => i !== index);
handleUpdateNode({ query: newQuery });
};
const isBodyVariable =
httpNode.body &&
typeof httpNode.body === "object" &&
"nodeId" in httpNode.body;
return (
<div className="space-y-4">
{/* Tab Navigation */}
<div className="flex relative">
<div className="absolute inset-0 w-full border-b pointer-events-none" />
{tabs.map((tab) => (
<Button
key={tab}
variant="ghost"
className={cn(
"rounded-none border-b",
tab === activeTab && "border-primary",
)}
onClick={() => setActiveTab(tab)}
>
{capitalizeFirstLetter(tab)}
</Button>
))}
</div>
<Card className="border-none">
<CardHeader className="sr-only">
<CardTitle>
{activeTab === "basic"
? "HTTP Request Configuration"
: activeTab === "headers"
? "Request Headers"
: activeTab === "query"
? "Query Parameters"
: "Request Body"}
</CardTitle>
</CardHeader>
{activeTab === "basic" && (
<CardContent className="space-y-4">
{/* HTTP Method */}
<div className="space-y-2">
<Label htmlFor="method">Method</Label>
<Select
value={httpNode.method}
onValueChange={(value) =>
handleUpdateNode({
method: value as HttpMethod,
})
}
>
<SelectTrigger>
<SelectValue placeholder="Select HTTP method" />
</SelectTrigger>
<SelectContent>
{HTTP_METHODS.map((method) => (
<SelectItem key={method} value={method}>
{method}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* URL */}
<div className="space-y-2">
<Label htmlFor="url">URL</Label>
<HttpValueInput
allowedTypes={["string"]}
currentNodeId={httpNode.id}
value={httpNode.url}
onChange={(value) => handleUpdateNode({ url: value })}
placeholder="https://api.example.com/endpoint"
/>
</div>
{/* Timeout */}
<div className="space-y-2">
<Label htmlFor="timeout">Timeout (ms)</Label>
<Input
id="timeout"
type="number"
value={httpNode.timeout || 30000}
onChange={(e) =>
handleUpdateNode({
timeout: parseInt(e.target.value) || 30000,
})
}
min={1000}
max={300000}
step={1000}
/>
<p className="text-xs text-gray-500">
Request timeout in milliseconds (1s - 5min)
</p>
</div>
</CardContent>
)}
{/* Headers Configuration */}
{activeTab === "headers" && (
<CardContent className="space-y-4">
{(httpNode.headers || []).map((header, index) => (
<div key={index} className="flex gap-2 items-end">
<div>
<Label className="text-xs mb-1 ml-1">Key</Label>
<Input
value={header.key}
className="w-24 placeholder:text-xs"
onChange={(e) =>
updateHeader(index, e.target.value, header.value)
}
placeholder={headerExample[index]?.key}
/>
</div>
<div className="flex-1">
<Label className="text-xs mb-1 ml-1">Value</Label>
<HttpValueInput
allowedTypes={["string", "number"]}
className="max-w-[230px]"
currentNodeId={httpNode.id}
value={header.value}
onChange={(value) => updateHeader(index, header.key, value)}
placeholder={headerExample[index]?.value}
onDelete={() => removeHeader(index)}
/>
</div>
</div>
))}
<Button onClick={addHeader} variant="outline" className="w-full">
<Plus className="size-3.5" />
Add Header
</Button>
</CardContent>
)}
{/* Query Parameters Configuration */}
{activeTab === "query" && (
<CardContent className="space-y-4">
{(httpNode.query || []).map((param, index) => (
<div key={index} className="flex gap-2 items-end">
<div>
<Label className="text-xs mb-1 ml-1 text-muted-foreground">
Key
</Label>
<Input
className="w-24 placeholder:text-xs"
value={param.key}
onChange={(e) =>
updateQueryParam(index, e.target.value, param.value)
}
placeholder={queryExample[index]?.key}
/>
</div>
<div className="flex-1">
<Label className="text-xs mb-1 ml-1 text-muted-foreground">
Value
</Label>
<HttpValueInput
allowedTypes={["string", "number"]}
currentNodeId={httpNode.id}
value={param.value}
onChange={(value) =>
updateQueryParam(index, param.key, value)
}
placeholder={queryExample[index]?.value}
onDelete={() => removeQueryParam(index)}
/>
</div>
</div>
))}
<Button
onClick={addQueryParam}
variant="outline"
className="w-full"
>
<Plus className="size-3.5" />
Add Query Parameter
</Button>
</CardContent>
)}
{/* Body Configuration */}
{activeTab === "body" && (
<CardContent className="space-y-4">
{["POST", "PUT", "PATCH"].includes(httpNode.method) ? (
<div className="space-y-2">
<div className="flex items-center gap-2">
<Label>Body</Label>
<VariableSelect
currentNodeId={httpNode.id}
onChange={(value) => handleUpdateNode({ body: value })}
>
<Button
variant={isBodyVariable ? "secondary" : "ghost"}
className="ml-auto data-[state=open]:bg-secondary"
size="sm"
>
<VariableIcon className="size-3" />
</Button>
</VariableSelect>
</div>
{!isBodyVariable ? (
<Textarea
className="resize-none h-48"
placeholder={`{
"name": "example",
"value": 123
}`}
value={httpNode.body?.toString() || ""}
onChange={(e) => handleUpdateNode({ body: e.target.value })}
/>
) : (
<VariableMentionItem
className="py-[7px] text-sm truncate"
{...findAvailableSchemaBySource({
nodeId: httpNode.id,
source: httpNode.body as OutputSchemaSourceKey,
nodes: getNodes().map((node) => node.data),
edges: getEdges(),
})}
onRemove={() => handleUpdateNode({ body: undefined })}
/>
)}
<p className="text-xs text-muted-foreground px-4 mt-4">
Request body content. JSON format will be auto-detected and
Content-Type header will be set automatically if not
specified.
</p>
</div>
) : (
<div className="text-center py-8 text-muted-foreground">
<p>
Request body is not available for {httpNode.method} requests.
</p>
<p className="text-xs mt-1">
Only POST, PUT, and PATCH requests can have a body.
</p>
</div>
)}
</CardContent>
)}
</Card>
</div>
);
}
export function HttpNodeDataStack({ data }: { data: HttpNodeData }) {
const { getNodes } = useReactFlow();
const urlDisplay = (() => {
if (!data.url) return "No URL";
if (typeof data.url === "string") {
const isUrl = data.url.startsWith("http");
return (
<div className="w-48 gap-1 flex items-center px-2 py-1 rounded-sm bg-background">
{isUrl ? (
<Link className="size-3 text-blue-500" />
) : (
<TriangleAlertIcon className="size-3 text-destructive" />
)}
<span className="text-foreground truncate flex-1">{data.url}</span>
</div>
);
}
if (typeof data.url === "object" && "nodeId" in data.url) {
const nodes = getNodes() as UINode[];
const urlAsSource = data.url as OutputSchemaSourceKey;
const sourceNode = nodes.find(
(node) => node.data.id === urlAsSource.nodeId,
);
return (
<VariableMentionItem
nodeName={sourceNode?.data.name || "ERROR"}
path={urlAsSource.path}
notFound={!sourceNode}
className="text-[10px] ring-0 w-full"
/>
);
}
return "Unknown source";
})();
return (
<div className="flex flex-col gap-1 px-4 mt-4">
<div className="text-[10px] px-2 py-1 flex items-center gap-2s">
<span className="px-1.5 py-0.5 rounded-lg font-semibold text-muted-foreground">
{data.method}
</span>
<div className="truncate flex-1 font-bold text-muted-foreground flex items-center">
{urlDisplay}
</div>
</div>
</div>
);
}
|