|
| 1 | +import System from "@/models/system"; |
| 2 | +import showToast from "@/utils/toast"; |
| 3 | +import { Plug } from "@phosphor-icons/react"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | +import { sentenceCase } from "text-case"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Converts setup_args to inputs for the form builder |
| 9 | + * @param {object} setupArgs - The setup arguments object |
| 10 | + * @returns {object} - The inputs object |
| 11 | + */ |
| 12 | +function inputsFromArgs(setupArgs) { |
| 13 | + if ( |
| 14 | + !setupArgs || |
| 15 | + setupArgs.constructor?.call?.().toString() !== "[object Object]" |
| 16 | + ) { |
| 17 | + return {}; |
| 18 | + } |
| 19 | + return Object.entries(setupArgs).reduce( |
| 20 | + (acc, [key, props]) => ({ |
| 21 | + ...acc, |
| 22 | + [key]: props.hasOwnProperty("value") |
| 23 | + ? props.value |
| 24 | + : props?.input?.default || "", |
| 25 | + }), |
| 26 | + {} |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Imported skill config component for imported skills only. |
| 32 | + * @returns {JSX.Element} |
| 33 | + */ |
| 34 | +export default function ImportedSkillConfig({ |
| 35 | + selectedSkill, // imported skill config object |
| 36 | + setImportedSkills, // function to set imported skills since config is file-write |
| 37 | +}) { |
| 38 | + const [config, setConfig] = useState(selectedSkill); |
| 39 | + const [hasChanges, setHasChanges] = useState(false); |
| 40 | + const [inputs, setInputs] = useState( |
| 41 | + inputsFromArgs(selectedSkill?.setup_args) |
| 42 | + ); |
| 43 | + |
| 44 | + const hasSetupArgs = |
| 45 | + selectedSkill?.setup_args && |
| 46 | + Object.keys(selectedSkill.setup_args).length > 0; |
| 47 | + |
| 48 | + async function toggleSkill() { |
| 49 | + const updatedConfig = { ...selectedSkill, active: !config.active }; |
| 50 | + await System.experimentalFeatures.agentPlugins.updatePluginConfig( |
| 51 | + config.hubId, |
| 52 | + { active: !config.active } |
| 53 | + ); |
| 54 | + setImportedSkills((prev) => |
| 55 | + prev.map((s) => (s.hubId === config.hubId ? updatedConfig : s)) |
| 56 | + ); |
| 57 | + setConfig(updatedConfig); |
| 58 | + } |
| 59 | + |
| 60 | + async function handleSubmit(e) { |
| 61 | + e.preventDefault(); |
| 62 | + const errors = []; |
| 63 | + const updatedConfig = { ...config }; |
| 64 | + |
| 65 | + for (const [key, value] of Object.entries(inputs)) { |
| 66 | + const settings = config.setup_args[key]; |
| 67 | + if (settings.required && !value) { |
| 68 | + errors.push(`${key} is required to have a value.`); |
| 69 | + continue; |
| 70 | + } |
| 71 | + if (typeof value !== settings.type) { |
| 72 | + errors.push(`${key} must be of type ${settings.type}.`); |
| 73 | + continue; |
| 74 | + } |
| 75 | + updatedConfig.setup_args[key].value = value; |
| 76 | + } |
| 77 | + |
| 78 | + if (errors.length > 0) { |
| 79 | + errors.forEach((error) => showToast(error, "error")); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + await System.experimentalFeatures.agentPlugins.updatePluginConfig( |
| 84 | + config.hubId, |
| 85 | + updatedConfig |
| 86 | + ); |
| 87 | + setConfig(updatedConfig); |
| 88 | + setImportedSkills((prev) => |
| 89 | + prev.map((skill) => |
| 90 | + skill.hubId === config.hubId ? updatedConfig : skill |
| 91 | + ) |
| 92 | + ); |
| 93 | + showToast("Skill config updated successfully.", "success"); |
| 94 | + } |
| 95 | + |
| 96 | + useEffect(() => { |
| 97 | + setHasChanges( |
| 98 | + JSON.stringify(inputs) !== |
| 99 | + JSON.stringify(inputsFromArgs(selectedSkill.setup_args)) |
| 100 | + ); |
| 101 | + }, [inputs]); |
| 102 | + |
| 103 | + return ( |
| 104 | + <> |
| 105 | + <div className="p-2"> |
| 106 | + <div className="flex flex-col gap-y-[18px] max-w-[500px]"> |
| 107 | + <div className="flex items-center gap-x-2"> |
| 108 | + <Plug size={24} color="white" weight="bold" /> |
| 109 | + <label htmlFor="name" className="text-white text-md font-bold"> |
| 110 | + {sentenceCase(config.name)} |
| 111 | + </label> |
| 112 | + <label className="border-none relative inline-flex cursor-pointer items-center ml-auto"> |
| 113 | + <input |
| 114 | + type="checkbox" |
| 115 | + className="peer sr-only" |
| 116 | + checked={config.active} |
| 117 | + onChange={() => toggleSkill()} |
| 118 | + /> |
| 119 | + <div className="pointer-events-none peer h-6 w-11 rounded-full bg-stone-400 after:absolute after:left-[2px] after:top-[2px] after:h-5 after:w-5 after:rounded-full after:shadow-xl after:border after:border-gray-600 after:bg-white after:box-shadow-md after:transition-all after:content-[''] peer-checked:bg-lime-300 peer-checked:after:translate-x-full peer-checked:after:border-white peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-800"></div> |
| 120 | + <span className="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300"></span> |
| 121 | + </label> |
| 122 | + </div> |
| 123 | + <p className="text-white text-opacity-60 text-xs font-medium py-1.5"> |
| 124 | + {config.description} by{" "} |
| 125 | + <a |
| 126 | + href={config.author_url} |
| 127 | + target="_blank" |
| 128 | + rel="noopener noreferrer" |
| 129 | + className="text-white hover:underline" |
| 130 | + > |
| 131 | + {config.author} |
| 132 | + </a> |
| 133 | + </p> |
| 134 | + |
| 135 | + {hasSetupArgs ? ( |
| 136 | + <div className="flex flex-col gap-y-2"> |
| 137 | + {Object.entries(config.setup_args).map(([key, props]) => ( |
| 138 | + <div key={key} className="flex flex-col gap-y-1"> |
| 139 | + <label htmlFor={key} className="text-white text-sm font-bold"> |
| 140 | + {key} |
| 141 | + </label> |
| 142 | + <input |
| 143 | + type={props?.input?.type || "text"} |
| 144 | + required={props?.input?.required} |
| 145 | + defaultValue={ |
| 146 | + props.hasOwnProperty("value") |
| 147 | + ? props.value |
| 148 | + : props?.input?.default || "" |
| 149 | + } |
| 150 | + onChange={(e) => |
| 151 | + setInputs({ ...inputs, [key]: e.target.value }) |
| 152 | + } |
| 153 | + placeholder={props?.input?.placeholder || ""} |
| 154 | + className="bg-transparent border border-white border-opacity-20 rounded-md p-2 text-white text-sm" |
| 155 | + /> |
| 156 | + <p className="text-white text-opacity-60 text-xs font-medium py-1.5"> |
| 157 | + {props?.input?.hint} |
| 158 | + </p> |
| 159 | + </div> |
| 160 | + ))} |
| 161 | + {hasChanges && ( |
| 162 | + <button |
| 163 | + onClick={handleSubmit} |
| 164 | + type="button" |
| 165 | + className="bg-blue-500 text-white rounded-md p-2" |
| 166 | + > |
| 167 | + Save |
| 168 | + </button> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + ) : ( |
| 172 | + <p className="text-white text-opacity-60 text-sm font-medium py-1.5"> |
| 173 | + There are no options to modify for this skill. |
| 174 | + </p> |
| 175 | + )} |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + </> |
| 179 | + ); |
| 180 | +} |
0 commit comments