ToolkitEditor.svelte 8.7 KB

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