Spaces:
Runtime error
Runtime error
File size: 6,867 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 | "use client";
import { OutputNodeData, UINode } from "lib/ai/workflow/workflow.interface";
import { memo, useCallback, useMemo } from "react";
import {
ChevronDownIcon,
PlusIcon,
TrashIcon,
TriangleAlertIcon,
VariableIcon,
} from "lucide-react";
import { VariableSelect } from "../variable-select";
import { useReactFlow } from "@xyflow/react";
import { Input } from "ui/input";
import { Button } from "ui/button";
import { cleanVariableName, generateUniqueKey } from "lib/utils";
import { Label } from "ui/label";
import { findJsonSchemaByPath } from "lib/ai/workflow/shared.workflow";
import { useTranslations } from "next-intl";
export const OutputNodeDataConfig = memo(function ({
data,
}: {
data: OutputNodeData;
}) {
const { getNodes, updateNodeData } = useReactFlow();
const t = useTranslations();
const outputVariables = useMemo(() => {
const nodes = getNodes() as UINode[];
return data.outputData.map(({ key, source }) => {
const targetNode = nodes.find((node) => node.data.id === source?.nodeId);
const schema = targetNode
? findJsonSchemaByPath(targetNode.data.outputSchema, source?.path ?? [])
: undefined;
return {
key,
schema,
path: source?.path ?? [],
nodeName: targetNode?.data.name,
nodeId: targetNode?.data.id,
isNotFound: (source && !targetNode) || (targetNode && !schema),
};
});
}, [data]);
const updateOutputVariable = useCallback(
(
index: number,
item: { key?: string; source?: { nodeId: string; path: string[] } },
) => {
updateNodeData(data.id, (node) => {
const prev = node.data as OutputNodeData;
return {
outputData: prev.outputData.map((v, i) =>
i === index ? { ...v, ...item } : v,
),
};
});
},
[data.id],
);
const deleteOutputVariable = useCallback(
(index: number) => {
updateNodeData(data.id, (node) => {
const prev = node.data as OutputNodeData;
return {
outputData: prev.outputData.filter((_, i) => i !== index),
};
});
},
[data.id],
);
const addOutputVariable = useCallback(
(key: string = "") => {
updateNodeData(data.id, (node) => {
const prev = node.data as OutputNodeData;
const newKey = generateUniqueKey(
key,
prev.outputData.map((v) => v.key),
);
return {
outputData: [...prev.outputData, { key: newKey, source: undefined }],
};
});
},
[data.id],
);
return (
<div className="flex flex-col gap-2 text-sm px-4 ">
<div className="flex items-center justify-between">
<Label className="text-sm">{t("Workflow.outputVariables")}</Label>
</div>
<div className="flex flex-col gap-2">
{outputVariables.map((item, index) => {
return (
<div className="flex items-center gap-1" key={index}>
<Input
value={item.key}
onChange={(e) => {
updateOutputVariable(index, {
key: cleanVariableName(e.target.value),
});
}}
className="w-24"
placeholder="name"
/>
<VariableSelect
currentNodeId={data.id}
onChange={(item) => {
updateOutputVariable(index, {
source: {
nodeId: item.nodeId,
path: item.path,
},
});
}}
>
<div className="flex-1 min-w-0 w-full flex text-[10px] items-center gap-1 p-2.5 border border-input bg-background rounded-lg cursor-pointer">
{item.isNotFound ? (
<TriangleAlertIcon className="size-3 text-destructive" />
) : (
<VariableIcon className="size-3 text-blue-500" />
)}
<span>{item.nodeName}/</span>
<span className="truncate min-w-0 text-blue-500 flex-1">
{item.path.join(".")}
</span>
<span className="text-muted-foreground">
{item.schema?.type}
</span>
<ChevronDownIcon className="size-3 ml-auto" />
</div>
</VariableSelect>
<Button
variant="ghost"
size="icon"
onClick={() => deleteOutputVariable(index)}
>
<TrashIcon />
</Button>
</div>
);
})}
<Button
variant="ghost"
onClick={() => {
addOutputVariable("text");
}}
className="w-full border-dashed border text-muted-foreground"
>
<PlusIcon /> {t("Workflow.addOutputVariable")}
</Button>
</div>
</div>
);
});
OutputNodeDataConfig.displayName = "OutputNodeDataConfig";
export const OutputNodeDataOutputStack = memo(function ({
data,
}: { data: OutputNodeData }) {
const { getNodes } = useReactFlow();
const outputVariables = useMemo(() => {
const nodes = getNodes() as UINode[];
return data.outputData.map(({ key, source }) => {
const targetNode = nodes.find((node) => node.data.id === source?.nodeId);
const schema = targetNode
? findJsonSchemaByPath(targetNode.data.outputSchema, source?.path ?? [])
: undefined;
return {
key,
schema,
path: source?.path ?? [],
nodeName: targetNode?.data.name,
nodeId: targetNode?.data.id,
isNotFound: (source && !targetNode) || (targetNode && !schema),
};
});
}, [data.outputSchema]);
if (!outputVariables.length) return null;
return (
<div className="flex flex-col gap-1 px-4 mt-4">
{outputVariables.map((item, index) => {
return (
<div
className="border bg-input text-[10px] rounded px-2 py-1 flex items-center gap-1"
key={index}
>
<div className="flex-1 min-w-0 w-full flex items-center gap-1">
{item.isNotFound ? (
<TriangleAlertIcon className="size-3 text-destructive" />
) : (
<VariableIcon className="size-3 text-blue-500" />
)}
<span>{item.nodeName}/</span>
<span className="truncate min-w-0 text-blue-500 flex-1">
{item.path.join(".")}
</span>
<span className="text-muted-foreground">{item.schema?.type}</span>
</div>
</div>
);
})}
</div>
);
});
OutputNodeDataOutputStack.displayName = "OutputNodeDataOutputStack";
|