FunctionEditor.svelte 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <script>
  2. import { getContext, createEventDispatcher, onMount } from 'svelte';
  3. import { goto } from '$app/navigation';
  4. const dispatch = createEventDispatcher();
  5. const i18n = getContext('i18n');
  6. import CodeEditor from '$lib/components/common/CodeEditor.svelte';
  7. import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  8. let formElement = null;
  9. let loading = false;
  10. let showConfirm = false;
  11. export let edit = false;
  12. export let clone = false;
  13. export let id = '';
  14. export let name = '';
  15. export let meta = {
  16. description: ''
  17. };
  18. export let content = '';
  19. $: if (name && !edit && !clone) {
  20. id = name.replace(/\s+/g, '_').toLowerCase();
  21. }
  22. let codeEditor;
  23. let boilerplate = `from pydantic import BaseModel
  24. from typing import Optional
  25. class Filter:
  26. class Valves(BaseModel):
  27. max_turns: int = 4
  28. pass
  29. def __init__(self):
  30. # Indicates custom file handling logic. This flag helps disengage default routines in favor of custom
  31. # implementations, informing the WebUI to defer file-related operations to designated methods within this class.
  32. # Alternatively, you can remove the files directly from the body in from the inlet hook
  33. self.file_handler = True
  34. # Initialize 'valves' with specific configurations. Using 'Valves' instance helps encapsulate settings,
  35. # which ensures settings are managed cohesively and not confused with operational flags like 'file_handler'.
  36. self.valves = self.Valves(**{"max_turns": 2})
  37. pass
  38. def inlet(self, body: dict, user: Optional[dict] = None) -> dict:
  39. # Modify the request body or validate it before processing by the chat completion API.
  40. # This function is the pre-processor for the API where various checks on the input can be performed.
  41. # It can also modify the request before sending it to the API.
  42. print(f"inlet:{__name__}")
  43. print(f"inlet:body:{body}")
  44. print(f"inlet:user:{user}")
  45. if user.get("role", "admin") in ["user", "admin"]:
  46. messages = body.get("messages", [])
  47. if len(messages) > self.valves.max_turns:
  48. raise Exception(
  49. f"Conversation turn limit exceeded. Max turns: {self.valves.max_turns}"
  50. )
  51. return body
  52. def outlet(self, body: dict, user: Optional[dict] = None) -> dict:
  53. # Modify or analyze the response body after processing by the API.
  54. # This function is the post-processor for the API, which can be used to modify the response
  55. # or perform additional checks and analytics.
  56. print(f"outlet:{__name__}")
  57. print(f"outlet:body:{body}")
  58. print(f"outlet:user:{user}")
  59. messages = [
  60. {**message, "content": f"{message['content']} - @@Modified from Outlet"}
  61. for message in body.get("messages", [])
  62. ]
  63. return {"messages": messages}
  64. `;
  65. const saveHandler = async () => {
  66. loading = true;
  67. dispatch('save', {
  68. id,
  69. name,
  70. meta,
  71. content
  72. });
  73. };
  74. const submitHandler = async () => {
  75. if (codeEditor) {
  76. const res = await codeEditor.formatPythonCodeHandler();
  77. if (res) {
  78. console.log('Code formatted successfully');
  79. saveHandler();
  80. }
  81. }
  82. };
  83. </script>
  84. <div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
  85. <div class="mx-auto w-full md:px-0 h-full">
  86. <form
  87. bind:this={formElement}
  88. class=" flex flex-col max-h-[100dvh] h-full"
  89. on:submit|preventDefault={() => {
  90. if (edit) {
  91. submitHandler();
  92. } else {
  93. showConfirm = true;
  94. }
  95. }}
  96. >
  97. <div class="mb-2.5">
  98. <button
  99. class="flex space-x-1"
  100. on:click={() => {
  101. goto('/workspace/functions');
  102. }}
  103. type="button"
  104. >
  105. <div class=" self-center">
  106. <svg
  107. xmlns="http://www.w3.org/2000/svg"
  108. viewBox="0 0 20 20"
  109. fill="currentColor"
  110. class="w-4 h-4"
  111. >
  112. <path
  113. fill-rule="evenodd"
  114. d="M17 10a.75.75 0 01-.75.75H5.612l4.158 3.96a.75.75 0 11-1.04 1.08l-5.5-5.25a.75.75 0 010-1.08l5.5-5.25a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
  115. clip-rule="evenodd"
  116. />
  117. </svg>
  118. </div>
  119. <div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
  120. </button>
  121. </div>
  122. <div class="flex flex-col flex-1 overflow-auto h-0 rounded-lg">
  123. <div class="w-full mb-2 flex flex-col gap-1.5">
  124. <div class="flex gap-2 w-full">
  125. <input
  126. class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  127. type="text"
  128. placeholder="Function Name (e.g. My Filter)"
  129. bind:value={name}
  130. required
  131. />
  132. <input
  133. class="w-full px-3 py-2 text-sm font-medium disabled:text-gray-300 dark:disabled:text-gray-700 bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  134. type="text"
  135. placeholder="Function ID (e.g. my_filter)"
  136. bind:value={id}
  137. required
  138. disabled={edit}
  139. />
  140. </div>
  141. <input
  142. class="w-full px-3 py-2 text-sm font-medium bg-gray-50 dark:bg-gray-850 dark:text-gray-200 rounded-lg outline-none"
  143. type="text"
  144. placeholder="Function Description (e.g. A filter to remove profanity from text)"
  145. bind:value={meta.description}
  146. required
  147. />
  148. </div>
  149. <div class="mb-2 flex-1 overflow-auto h-0 rounded-lg">
  150. <CodeEditor
  151. bind:value={content}
  152. bind:this={codeEditor}
  153. {boilerplate}
  154. on:save={() => {
  155. if (formElement) {
  156. formElement.requestSubmit();
  157. }
  158. }}
  159. />
  160. </div>
  161. <div class="pb-3 flex justify-between">
  162. <div class="flex-1 pr-3">
  163. <div class="text-xs text-gray-500 line-clamp-2">
  164. <span class=" font-semibold dark:text-gray-200">Warning:</span> Functions allow
  165. arbitrary code execution <br />—
  166. <span class=" font-medium dark:text-gray-400"
  167. >don't install random functions from sources you don't trust.</span
  168. >
  169. </div>
  170. </div>
  171. <button
  172. class="px-3 py-1.5 text-sm font-medium bg-emerald-600 hover:bg-emerald-700 text-gray-50 transition rounded-lg"
  173. type="submit"
  174. >
  175. {$i18n.t('Save')}
  176. </button>
  177. </div>
  178. </div>
  179. </form>
  180. </div>
  181. </div>
  182. <ConfirmDialog
  183. bind:show={showConfirm}
  184. on:confirm={() => {
  185. submitHandler();
  186. }}
  187. >
  188. <div class="text-sm text-gray-500">
  189. <div class=" bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3">
  190. <div>Please carefully review the following warnings:</div>
  191. <ul class=" mt-1 list-disc pl-4 text-xs">
  192. <li>Functions allow arbitrary code execution.</li>
  193. <li>Do not install functions from sources you do not fully trust.</li>
  194. </ul>
  195. </div>
  196. <div class="my-3">
  197. I acknowledge that I have read and I understand the implications of my action. I am aware of
  198. the risks associated with executing arbitrary code and I have verified the trustworthiness of
  199. the source.
  200. </div>
  201. </div>
  202. </ConfirmDialog>