CodeEditor.svelte 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <script lang="ts">
  2. import CodeEditor from '$lib/components/common/CodeEditor.svelte';
  3. export let saveHandler: Function;
  4. export let value = '';
  5. let codeEditor;
  6. let boilerplate = `# Tip: Use Ctrl/Cmd + S to format the code
  7. from datetime import datetime
  8. import requests
  9. class Tools:
  10. def __init__(self):
  11. pass
  12. # Add your custom tools using pure Python code here, make sure to add type hints
  13. # Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
  14. # Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
  15. def get_current_time(self) -> str:
  16. """
  17. Get the current time.
  18. :return: The current time.
  19. """
  20. now = datetime.now()
  21. current_time = now.strftime("%H:%M:%S")
  22. return f"Current Time = {current_time}"
  23. def calculator(self, equation: str) -> str:
  24. """
  25. Calculate the result of an equation.
  26. :param equation: The equation to calculate.
  27. """
  28. # Avoid using eval in production code
  29. # https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
  30. try:
  31. result = eval(equation)
  32. return f"{equation} = {result}"
  33. except Exception as e:
  34. print(e)
  35. return "Invalid equation"
  36. `;
  37. export const formatHandler = async () => {
  38. if (codeEditor) {
  39. return await codeEditor.formatPythonCodeHandler();
  40. }
  41. return false;
  42. };
  43. </script>
  44. <CodeEditor
  45. bind:value
  46. {boilerplate}
  47. bind:this={codeEditor}
  48. on:save={() => {
  49. saveHandler();
  50. }}
  51. />