File size: 3,723 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
"use client";

import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { TrashIcon, VariableIcon } from "lucide-react";
import {
  HttpValue,
  OutputSchemaSourceKey,
} from "lib/ai/workflow/workflow.interface";
import { VariableSelect } from "./variable-select";
import { useReactFlow } from "@xyflow/react";
import { UINode } from "lib/ai/workflow/workflow.interface";
import { Tooltip, TooltipContent, TooltipTrigger } from "ui/tooltip";
import { VariableMentionItem } from "./variable-mention-item";
import { findAvailableSchemaBySource } from "lib/ai/workflow/shared.workflow";
import { useTranslations } from "next-intl";
import { cn, exclude } from "lib/utils";

interface HttpValueInputProps {
  value: HttpValue | undefined;
  onChange: (value: HttpValue | undefined) => void;
  onDelete?: () => void;
  placeholder?: string;
  currentNodeId: string;
  allowedTypes?: string[];
  className?: string;
}

export function HttpValueInput({
  value,
  onChange,
  placeholder,
  currentNodeId,
  allowedTypes = [],
  onDelete,
  className,
}: HttpValueInputProps) {
  const { getNodes, getEdges } = useReactFlow<UINode>();
  const t = useTranslations("Workflow");

  // Check if current value is a variable reference
  const isVariable = value && typeof value === "object" && "nodeId" in value;

  // Get the node name for display if it's a variable
  const getVariable = (sourceKey: OutputSchemaSourceKey) => {
    const data = findAvailableSchemaBySource({
      nodeId: currentNodeId,
      source: sourceKey,
      nodes: getNodes().map((node) => node.data),
      edges: getEdges(),
    });
    return exclude(data, ["type"]);
  };

  const handleLiteralChange = (inputValue: string) => {
    if (inputValue === "") {
      onChange(undefined);
      return;
    }
    onChange(inputValue);
  };

  const handleVariableSelect = (item: {
    nodeId: string;
    path: string[];
    nodeName: string;
    type: string;
  }) => {
    onChange({
      nodeId: item.nodeId,
      path: item.path,
    });
  };

  return (
    <div className={cn("flex items-center gap-1 min-w-0", className)}>
      {isVariable ? (
        <div className="flex-1 min-w-0">
          <VariableMentionItem
            className="py-[7px] text-sm truncate"
            {...getVariable(value as OutputSchemaSourceKey)}
            onRemove={() => onChange(undefined)}
          />
        </div>
      ) : (
        <Input
          className="flex-1 placeholder:text-xs"
          value={value?.toString() || ""}
          onChange={(e) => handleLiteralChange(e.target.value)}
          placeholder={placeholder}
        />
      )}

      <Tooltip>
        <TooltipTrigger asChild>
          <div>
            <VariableSelect
              currentNodeId={currentNodeId}
              onChange={handleVariableSelect}
              allowedTypes={allowedTypes}
            >
              <Button
                variant={isVariable ? "secondary" : "ghost"}
                onPointerDown={(e) => {
                  if (isVariable) {
                    e.preventDefault();
                    onChange(undefined);
                  }
                }}
                size="icon"
                className="data-[state=open]:bg-secondary"
              >
                <VariableIcon className={isVariable ? "text-blue-500" : ""} />
              </Button>
            </VariableSelect>
          </div>
        </TooltipTrigger>
        <TooltipContent>
          <p>{t("selectVariable")}</p>
        </TooltipContent>
      </Tooltip>
      {onDelete && (
        <Button variant="ghost" size="icon" onClick={onDelete}>
          <TrashIcon />
        </Button>
      )}
    </div>
  );
}