ToolkitEditor.svelte 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <script>
  2. import { getContext, createEventDispatcher, onMount, tick } from 'svelte';
  3. const i18n = getContext('i18n');
  4. import CodeEditor from '$lib/components/common/CodeEditor.svelte';
  5. import { goto } from '$app/navigation';
  6. import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  7. import Badge from '$lib/components/common/Badge.svelte';
  8. import ChevronLeft from '$lib/components/icons/ChevronLeft.svelte';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import LockClosed from '$lib/components/icons/LockClosed.svelte';
  11. import AccessControlModal from '../common/AccessControlModal.svelte';
  12. const dispatch = createEventDispatcher();
  13. let formElement = null;
  14. let loading = false;
  15. let showConfirm = false;
  16. let showAccessControlModal = false;
  17. export let edit = false;
  18. export let clone = false;
  19. export let id = '';
  20. export let name = '';
  21. export let meta = {
  22. description: ''
  23. };
  24. export let content = '';
  25. export let accessControl = null;
  26. let _content = '';
  27. $: if (content) {
  28. updateContent();
  29. }
  30. const updateContent = () => {
  31. _content = content;
  32. };
  33. $: if (name && !edit && !clone) {
  34. id = name.replace(/\s+/g, '_').toLowerCase();
  35. }
  36. let codeEditor;
  37. let boilerplate = `import os
  38. import requests
  39. from datetime import datetime
  40. class Tools:
  41. def __init__(self):
  42. pass
  43. # Add your custom tools using pure Python code here, make sure to add type hints
  44. # Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
  45. # Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
  46. def get_user_name_and_email_and_id(self, __user__: dict = {}) -> str:
  47. """
  48. Get the user name, Email and ID from the user object.
  49. """
  50. # Do not include :param for __user__ in the docstring as it should not be shown in the tool's specification
  51. # The session user object will be passed as a parameter when the function is called
  52. print(__user__)
  53. result = ""
  54. if "name" in __user__:
  55. result += f"User: {__user__['name']}"
  56. if "id" in __user__:
  57. result += f" (ID: {__user__['id']})"
  58. if "email" in __user__:
  59. result += f" (Email: {__user__['email']})"
  60. if result == "":
  61. result = "User: Unknown"
  62. return result
  63. def get_current_time(self) -> str:
  64. """
  65. Get the current time in a more human-readable format.
  66. :return: The current time.
  67. """
  68. now = datetime.now()
  69. current_time = now.strftime("%I:%M:%S %p") # Using 12-hour format with AM/PM
  70. current_date = now.strftime(
  71. "%A, %B %d, %Y"
  72. ) # Full weekday, month name, day, and year
  73. return f"Current Date and Time = {current_date}, {current_time}"
  74. def calculator(self, equation: str) -> str:
  75. """
  76. Calculate the result of an equation.
  77. :param equation: The equation to calculate.
  78. """
  79. # Avoid using eval in production code
  80. # https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
  81. try:
  82. result = eval(equation)
  83. return f"{equation} = {result}"
  84. except Exception as e:
  85. print(e)
  86. return "Invalid equation"
  87. def get_current_weather(self, city: str) -> str:
  88. """
  89. Get the current weather for a given city.
  90. :param city: The name of the city to get the weather for.
  91. :return: The current weather information or an error message.
  92. """
  93. api_key = os.getenv("OPENWEATHER_API_KEY")
  94. if not api_key:
  95. return (
  96. "API key is not set in the environment variable 'OPENWEATHER_API_KEY'."
  97. )
  98. base_url = "http://api.openweathermap.org/data/2.5/weather"
  99. params = {
  100. "q": city,
  101. "appid": api_key,
  102. "units": "metric", # Optional: Use 'imperial' for Fahrenheit
  103. }
  104. try:
  105. response = requests.get(base_url, params=params)
  106. response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx)
  107. data = response.json()
  108. if data.get("cod") != 200:
  109. return f"Error fetching weather data: {data.get('message')}"
  110. weather_description = data["weather"][0]["description"]
  111. temperature = data["main"]["temp"]
  112. humidity = data["main"]["humidity"]
  113. wind_speed = data["wind"]["speed"]
  114. return f"Weather in {city}: {temperature}°C"
  115. except requests.RequestException as e:
  116. return f"Error fetching weather data: {str(e)}"
  117. `;
  118. const saveHandler = async () => {
  119. loading = true;
  120. dispatch('save', {
  121. id,
  122. name,
  123. meta,
  124. content,
  125. access_control: accessControl
  126. });
  127. };
  128. const submitHandler = async () => {
  129. if (codeEditor) {
  130. content = _content;
  131. await tick();
  132. const res = await codeEditor.formatPythonCodeHandler();
  133. await tick();
  134. content = _content;
  135. await tick();
  136. if (res) {
  137. console.log('Code formatted successfully');
  138. saveHandler();
  139. }
  140. }
  141. };
  142. </script>
  143. <AccessControlModal bind:show={showAccessControlModal} bind:accessControl />
  144. <div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
  145. <div class="mx-auto w-full md:px-0 h-full">
  146. <form
  147. bind:this={formElement}
  148. class=" flex flex-col max-h-[100dvh] h-full"
  149. on:submit|preventDefault={() => {
  150. if (edit) {
  151. submitHandler();
  152. } else {
  153. showConfirm = true;
  154. }
  155. }}
  156. >
  157. <div class="flex flex-col flex-1 overflow-auto h-0">
  158. <div class="w-full mb-2 flex flex-col gap-0.5">
  159. <div class="flex w-full items-center">
  160. <div class=" flex-shrink-0 mr-2">
  161. <Tooltip content={$i18n.t('Back')}>
  162. <button
  163. class="w-full text-left text-sm py-1.5 px-1 rounded-lg dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-gray-850"
  164. on:click={() => {
  165. goto('/workspace/tools');
  166. }}
  167. type="button"
  168. >
  169. <ChevronLeft strokeWidth="2.5" />
  170. </button>
  171. </Tooltip>
  172. </div>
  173. <div class="flex-1">
  174. <Tooltip content={$i18n.t('e.g. My Tools')} placement="top-start">
  175. <input
  176. class="w-full text-2xl font-semibold bg-transparent outline-none"
  177. type="text"
  178. placeholder={$i18n.t('Tool Name')}
  179. bind:value={name}
  180. required
  181. />
  182. </Tooltip>
  183. </div>
  184. <div class="self-center flex-shrink-0">
  185. <button
  186. class="bg-gray-50 hover:bg-gray-100 text-black dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-white transition px-2 py-1 rounded-full flex gap-1 items-center"
  187. type="button"
  188. on:click={() => {
  189. showAccessControlModal = true;
  190. }}
  191. >
  192. <LockClosed strokeWidth="2.5" className="size-3.5" />
  193. <div class="text-sm font-medium flex-shrink-0">
  194. {$i18n.t('Access')}
  195. </div>
  196. </button>
  197. </div>
  198. </div>
  199. <div class=" flex gap-2 px-1 items-center">
  200. {#if edit}
  201. <div class="text-sm text-gray-500 flex-shrink-0">
  202. {id}
  203. </div>
  204. {:else}
  205. <Tooltip className="w-full" content={$i18n.t('e.g. my_tools')} placement="top-start">
  206. <input
  207. class="w-full text-sm disabled:text-gray-500 bg-transparent outline-none"
  208. type="text"
  209. placeholder={$i18n.t('Tool ID')}
  210. bind:value={id}
  211. required
  212. disabled={edit}
  213. />
  214. </Tooltip>
  215. {/if}
  216. <Tooltip
  217. className="w-full self-center items-center flex"
  218. content={$i18n.t('e.g. Tools for performing various operations')}
  219. placement="top-start"
  220. >
  221. <input
  222. class="w-full text-sm bg-transparent outline-none"
  223. type="text"
  224. placeholder={$i18n.t('Tool Description')}
  225. bind:value={meta.description}
  226. required
  227. />
  228. </Tooltip>
  229. </div>
  230. </div>
  231. <div class="mb-2 flex-1 overflow-auto h-0 rounded-lg">
  232. <CodeEditor
  233. bind:this={codeEditor}
  234. value={content}
  235. {boilerplate}
  236. lang="python"
  237. on:change={(e) => {
  238. _content = e.detail.value;
  239. }}
  240. on:save={() => {
  241. if (formElement) {
  242. formElement.requestSubmit();
  243. }
  244. }}
  245. />
  246. </div>
  247. <div class="pb-3 flex justify-between">
  248. <div class="flex-1 pr-3">
  249. <div class="text-xs text-gray-500 line-clamp-2">
  250. <span class=" font-semibold dark:text-gray-200">{$i18n.t('Warning:')}</span>
  251. {$i18n.t('Tools are a function calling system with arbitrary code execution')} <br />—
  252. <span class=" font-medium dark:text-gray-400"
  253. >{$i18n.t(`don't install random tools from sources you don't trust.`)}</span
  254. >
  255. </div>
  256. </div>
  257. <button
  258. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
  259. type="submit"
  260. >
  261. {$i18n.t('Save')}
  262. </button>
  263. </div>
  264. </div>
  265. </form>
  266. </div>
  267. </div>
  268. <ConfirmDialog
  269. bind:show={showConfirm}
  270. on:confirm={() => {
  271. submitHandler();
  272. }}
  273. >
  274. <div class="text-sm text-gray-500">
  275. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  276. <div>{$i18n.t('Please carefully review the following warnings:')}</div>
  277. <ul class=" mt-1 list-disc pl-4 text-xs">
  278. <li>
  279. {$i18n.t('Tools have a function calling system that allows arbitrary code execution.')}
  280. </li>
  281. <li>{$i18n.t('Do not install tools from sources you do not fully trust.')}</li>
  282. </ul>
  283. </div>
  284. <div class="my-3">
  285. {$i18n.t(
  286. 'I acknowledge that I have read and I understand the implications of my action. I am aware of the risks associated with executing arbitrary code and I have verified the trustworthiness of the source.'
  287. )}
  288. </div>
  289. </div>
  290. </ConfirmDialog>