2024-06-11 02:30:48 +00:00
< script >
2024-10-06 03:16:46 +00:00
import { getContext , createEventDispatcher , onMount , tick } from 'svelte';
2024-06-11 02:30:48 +00:00
const i18n = getContext('i18n');
2024-06-17 20:38:07 +00:00
import CodeEditor from '$lib/components/common/CodeEditor.svelte';
2024-06-11 02:30:48 +00:00
import { goto } from '$app/navigation';
2024-06-12 08:17:46 +00:00
import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
2024-10-21 01:24:50 +00:00
import Badge from '$lib/components/common/Badge.svelte';
2024-10-21 01:31:24 +00:00
import ChevronLeft from '$lib/components/icons/ChevronLeft.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
2024-06-11 02:30:48 +00:00
2024-06-11 03:58:47 +00:00
const dispatch = createEventDispatcher();
2024-06-11 04:33:46 +00:00
let formElement = null;
2024-06-11 02:30:48 +00:00
let loading = false;
2024-06-12 08:17:46 +00:00
let showConfirm = false;
2024-06-11 02:30:48 +00:00
2024-06-11 04:33:46 +00:00
export let edit = false;
2024-06-11 05:16:49 +00:00
export let clone = false;
2024-06-11 04:33:46 +00:00
export let id = '';
export let name = '';
export let meta = {
2024-06-11 02:30:48 +00:00
description: ''
};
2024-06-11 04:33:46 +00:00
export let content = '';
2024-10-05 19:04:36 +00:00
let _content = '';
$: if (content) {
updateContent();
}
const updateContent = () => {
_content = content;
};
2024-06-11 02:30:48 +00:00
2024-06-11 05:16:49 +00:00
$: if (name && !edit && !clone) {
2024-06-11 02:30:48 +00:00
id = name.replace(/\s+/g, '_').toLowerCase();
}
let codeEditor;
2024-06-17 20:38:07 +00:00
let boilerplate = `import os
import requests
from datetime import datetime
class Tools:
def __init__(self):
pass
# Add your custom tools using pure Python code here, make sure to add type hints
# Use Sphinx-style docstrings to document your tools, they will be used for generating tools specifications
# Please refer to function_calling_filter_pipeline.py file from pipelines project for an example
def get_user_name_and_email_and_id(self, __user__: dict = {} ) -> str:
"""
Get the user name, Email and ID from the user object.
"""
# Do not include :param for __user__ in the docstring as it should not be shown in the tool's specification
# The session user object will be passed as a parameter when the function is called
print(__user__)
result = ""
if "name" in __user__:
result += f"User: { __user__ [ 'name' ]} "
if "id" in __user__:
result += f" (ID: { __user__ [ 'id' ]} )"
if "email" in __user__:
result += f" (Email: { __user__ [ 'email' ]} )"
if result == "":
result = "User: Unknown"
return result
def get_current_time(self) -> str:
"""
Get the current time in a more human-readable format.
:return: The current time.
"""
now = datetime.now()
current_time = now.strftime("%I:%M:%S %p") # Using 12-hour format with AM/PM
current_date = now.strftime(
"%A, %B %d, %Y"
) # Full weekday, month name, day, and year
return f"Current Date and Time = { current_date } , { current_time } "
def calculator(self, equation: str) -> str:
"""
Calculate the result of an equation.
:param equation: The equation to calculate.
"""
# Avoid using eval in production code
# https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
try:
result = eval(equation)
return f"{ equation } = { result } "
except Exception as e:
print(e)
return "Invalid equation"
def get_current_weather(self, city: str) -> str:
"""
Get the current weather for a given city.
:param city: The name of the city to get the weather for.
:return: The current weather information or an error message.
"""
api_key = os.getenv("OPENWEATHER_API_KEY")
if not api_key:
return (
"API key is not set in the environment variable 'OPENWEATHER_API_KEY'."
)
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": api_key,
"units": "metric", # Optional: Use 'imperial' for Fahrenheit
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status() # Raise HTTPError for bad responses (4xx and 5xx)
data = response.json()
if data.get("cod") != 200:
return f"Error fetching weather data: { data . get ( 'message' )} "
weather_description = data["weather"][0]["description"]
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]
wind_speed = data["wind"]["speed"]
return f"Weather in { city } : { temperature } °C"
except requests.RequestException as e:
return f"Error fetching weather data: { str ( e )} "
`;
2024-06-11 02:30:48 +00:00
const saveHandler = async () => {
loading = true;
2024-06-11 03:58:47 +00:00
dispatch('save', {
id,
name,
meta,
content
});
2024-06-11 02:30:48 +00:00
};
const submitHandler = async () => {
if (codeEditor) {
2024-10-05 23:16:51 +00:00
content = _content;
await tick();
2024-06-17 20:38:07 +00:00
const res = await codeEditor.formatPythonCodeHandler();
2024-10-06 19:07:45 +00:00
await tick();
content = _content;
await tick();
2024-06-11 02:30:48 +00:00
if (res) {
console.log('Code formatted successfully');
2024-10-05 23:16:51 +00:00
2024-06-11 02:30:48 +00:00
saveHandler();
}
}
};
< / script >
< div class = " flex flex-col justify-between w-full overflow-y-auto h-full" >
< div class = "mx-auto w-full md:px-0 h-full" >
2024-06-11 03:58:47 +00:00
< form
2024-06-11 04:33:46 +00:00
bind:this={ formElement }
2024-06-11 03:58:47 +00:00
class=" flex flex-col max-h-[100dvh] h-full"
on:submit| preventDefault={() => {
2024-06-12 08:22:52 +00:00
if (edit) {
submitHandler();
} else {
showConfirm = true;
}
2024-06-11 03:58:47 +00:00
}}
>
2024-10-21 01:24:50 +00:00
< div class = "flex flex-col flex-1 overflow-auto h-0" >
< div class = "w-full mb-2 flex flex-col gap-0.5" >
2024-10-21 01:31:24 +00:00
< div class = "flex w-full items-center" >
< div class = " flex-shrink-0 mr-2" >
< Tooltip content = { $i18n . t ( 'Back' )} >
< button
2024-10-21 01:35:19 +00:00
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"
2024-10-21 01:31:24 +00:00
on:click={() => {
goto('/workspace/tools');
}}
type="button"
>
< ChevronLeft strokeWidth = "2.5" / >
< / button >
< / Tooltip >
< / div >
2024-10-21 01:24:50 +00:00
< div class = "flex-1" >
2024-11-16 02:21:41 +00:00
< Tooltip content = { $i18n . t ( 'e.g. My Tools' )} placement="top-start" >
2024-11-12 23:46:44 +00:00
< input
class="w-full text-2xl font-medium bg-transparent outline-none"
type="text"
2024-11-16 02:21:41 +00:00
placeholder={ $i18n . t ( 'Tool Name' )}
2024-11-12 23:46:44 +00:00
bind:value={ name }
required
/>
< / Tooltip >
2024-10-21 01:24:50 +00:00
< / div >
< div >
< Badge type = "muted" content = { $i18n . t ( 'Tool' )} / >
< / div >
< / div >
2024-11-12 23:46:44 +00:00
< div class = " flex gap-2 px-1 items-center" >
2024-10-21 01:24:50 +00:00
{ #if edit }
< div class = "text-sm text-gray-500 flex-shrink-0" >
{ id }
< / div >
{ : else }
2024-11-16 02:21:41 +00:00
< Tooltip className = "w-full" content = { $i18n . t ( 'e.g. my_tools' )} placement="top-start" >
2024-11-12 23:46:44 +00:00
< input
class="w-full text-sm disabled:text-gray-500 bg-transparent outline-none"
type="text"
2024-11-16 02:21:41 +00:00
placeholder={ $i18n . t ( 'Tool ID' )}
2024-11-12 23:46:44 +00:00
bind:value={ id }
required
disabled={ edit }
/>
< / Tooltip >
{ /if }
< Tooltip
className="w-full self-center items-center flex"
2024-11-16 02:21:41 +00:00
content={ $i18n . t ( 'e.g. Tools for performing various operations' )}
2024-11-12 23:46:44 +00:00
placement="top-start"
>
2024-10-21 01:24:50 +00:00
< input
2024-11-12 23:46:44 +00:00
class="w-full text-sm bg-transparent outline-none"
2024-10-21 01:24:50 +00:00
type="text"
2024-11-16 02:21:41 +00:00
placeholder={ $i18n . t ( 'Tool Description' )}
2024-11-12 23:46:44 +00:00
bind:value={ meta . description }
2024-10-21 01:24:50 +00:00
required
/>
2024-11-12 23:46:44 +00:00
< / Tooltip >
2024-06-11 02:30:48 +00:00
< / div >
< / div >
< div class = "mb-2 flex-1 overflow-auto h-0 rounded-lg" >
2024-06-11 03:58:47 +00:00
< CodeEditor
bind:this={ codeEditor }
2024-10-05 19:04:36 +00:00
value={ content }
2024-06-17 20:38:07 +00:00
{ boilerplate }
2024-10-05 19:38:44 +00:00
lang="python"
2024-10-05 19:04:36 +00:00
on:change={( e ) => {
_content = e.detail.value;
}}
2024-06-11 03:58:47 +00:00
on:save={() => {
2024-06-11 04:33:46 +00:00
if (formElement) {
formElement.requestSubmit();
}
2024-06-11 03:58:47 +00:00
}}
/>
2024-06-11 02:30:48 +00:00
< / div >
2024-06-11 17:47:46 +00:00
< div class = "pb-3 flex justify-between" >
< div class = "flex-1 pr-3" >
< div class = "text-xs text-gray-500 line-clamp-2" >
2024-07-15 12:54:20 +00:00
< span class = " font-semibold dark:text-gray-200" > { $i18n . t ( 'Warning:' )} </ span >
{ $i18n . t ( 'Tools are a function calling system with arbitrary code execution' )} < br /> —
2024-06-11 17:47:46 +00:00
< span class = " font-medium dark:text-gray-400"
2024-07-15 13:01:02 +00:00
>{ $i18n . t ( `don't install random tools from sources you don't trust.` )} < /span
2024-06-11 17:47:46 +00:00
>
< / div >
< / div >
2024-06-12 08:17:46 +00:00
2024-06-11 02:30:48 +00:00
< button
2024-10-21 07:05:27 +00:00
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"
2024-06-12 08:17:46 +00:00
type="submit"
2024-06-11 02:30:48 +00:00
>
{ $i18n . t ( 'Save' )}
< / button >
< / div >
< / div >
2024-06-11 03:58:47 +00:00
< / form >
2024-06-11 02:30:48 +00:00
< / div >
< / div >
2024-06-12 08:17:46 +00:00
< ConfirmDialog
bind:show={ showConfirm }
on:confirm={() => {
submitHandler();
}}
>
< div class = "text-sm text-gray-500" >
< div class = " bg-yellow-500/20 text-yellow-700 dark:text-yellow-200 rounded-lg px-4 py-3" >
2024-07-15 12:54:20 +00:00
< div > { $i18n . t ( 'Please carefully review the following warnings:' )} </ div >
2024-06-12 08:17:46 +00:00
< ul class = " mt-1 list-disc pl-4 text-xs" >
2024-07-15 13:14:43 +00:00
< li >
2024-07-15 13:16:49 +00:00
{ $i18n . t ( 'Tools have a function calling system that allows arbitrary code execution.' )}
2024-07-15 13:14:43 +00:00
< / li >
2024-07-15 12:54:20 +00:00
< li > { $i18n . t ( 'Do not install tools from sources you do not fully trust.' )} </ li >
2024-06-12 08:17:46 +00:00
< / ul >
< / div >
< div class = "my-3" >
2024-07-15 12:54:20 +00:00
{ $i18n . t (
'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.'
)}
2024-06-12 08:17:46 +00:00
< / div >
< / div >
< / ConfirmDialog >