import { Text, Button, HStack, Heading, Icon, Input, Stack } from "@chakra-ui/react"; import { useState } from "react"; import { BsCheck, BsX } from "react-icons/bs"; import { api } from "~/utils/api"; import { useExperiment, useHandledAsyncCallback } from "~/utils/hooks"; export default function EditScenarioVars() { const experiment = useExperiment(); const vars = api.templateVars.list.useQuery({ experimentId: experiment.data?.id ?? "" }).data ?? []; const [newVariable, setNewVariable] = useState(""); const newVarIsValid = newVariable.length > 0 && !vars.map((v) => v.label).includes(newVariable); const utils = api.useContext(); const addVarMutation = api.templateVars.create.useMutation(); const [onAddVar] = useHandledAsyncCallback(async () => { if (!experiment.data?.id) return; if (!newVarIsValid) return; await addVarMutation.mutateAsync({ experimentId: experiment.data.id, label: newVariable, }); await utils.templateVars.list.invalidate(); setNewVariable(""); }, [addVarMutation, experiment.data?.id, newVarIsValid, newVariable]); const deleteMutation = api.templateVars.delete.useMutation(); const [onDeleteVar] = useHandledAsyncCallback(async (id: string) => { await deleteMutation.mutateAsync({ id }); await utils.templateVars.list.invalidate(); }, []); return ( Scenario Variables Scenario variables can be used in your prompt variants as well as evaluations. setNewVariable(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); onAddVar(); } // If the user types a space, replace it with an underscore if (e.key === " ") { e.preventDefault(); setNewVariable((v) => v + "_"); } }} /> {vars.map((variable) => ( {variable.label} ))} ); }