diff --git a/src/khoj/processor/tools/run_code.py b/src/khoj/processor/tools/run_code.py index 26edad27..c62c710e 100644 --- a/src/khoj/processor/tools/run_code.py +++ b/src/khoj/processor/tools/run_code.py @@ -39,7 +39,7 @@ from khoj.utils.rawconfig import LocationData logger = logging.getLogger(__name__) -SANDBOX_URL = os.getenv("KHOJ_TERRARIUM_URL", "http://localhost:8080") +SANDBOX_URL = os.getenv("KHOJ_TERRARIUM_URL") DEFAULT_E2B_TEMPLATE = "pmt2o0ghpang8gbiys57" diff --git a/src/khoj/routers/helpers.py b/src/khoj/routers/helpers.py index 3bfbdb4c..4cad0dd9 100644 --- a/src/khoj/routers/helpers.py +++ b/src/khoj/routers/helpers.py @@ -118,6 +118,7 @@ from khoj.utils.helpers import ( ToolDefinition, get_file_type, in_debug_mode, + is_code_sandbox_enabled, is_none_or_empty, is_operator_enabled, is_valid_url, @@ -370,6 +371,8 @@ async def aget_data_sources_and_output_format( continue if source in [ConversationCommand.Online, ConversationCommand.Webpage] and not is_web_search_enabled(): continue + if source == ConversationCommand.Code and not is_code_sandbox_enabled(): + continue source_options[source.value] = description if len(agent_sources) == 0 or source.value in agent_sources: source_options_str += f'- "{source.value}": "{description}"\n' diff --git a/src/khoj/routers/research.py b/src/khoj/routers/research.py index d242c5b9..5f8719ce 100644 --- a/src/khoj/routers/research.py +++ b/src/khoj/routers/research.py @@ -34,6 +34,7 @@ from khoj.utils.helpers import ( ConversationCommand, ToolDefinition, dict_to_tuple, + is_code_sandbox_enabled, is_none_or_empty, is_operator_enabled, is_web_search_enabled, @@ -120,6 +121,10 @@ async def apick_next_tool( # Skip showing web search tool if agent has no access to internet if tool in web_research_tools and not is_web_search_enabled(): continue + # Skip showing code tool if agent has no access to code execution sandbox + if tool == ConversationCommand.PythonCoder and not is_code_sandbox_enabled(): + continue + # Format description with relevant usage limits if tool == ConversationCommand.SemanticSearchFiles: description = tool_data.description.format(max_search_queries=max_document_searches) elif tool == ConversationCommand.ReadWebpage: diff --git a/src/khoj/utils/helpers.py b/src/khoj/utils/helpers.py index a43521d2..c746455f 100644 --- a/src/khoj/utils/helpers.py +++ b/src/khoj/utils/helpers.py @@ -777,6 +777,12 @@ def is_operator_enabled(): return is_env_var_true("KHOJ_OPERATOR_ENABLED") +def is_code_sandbox_enabled(): + """Check if Khoj can run code in sandbox. + Set KHOJ_TERRARIUM_URL or E2B api key via env var to enable it.""" + return not is_none_or_empty(os.getenv("KHOJ_TERRARIUM_URL")) or is_e2b_code_sandbox_enabled() + + def is_valid_url(url: str) -> bool: """Check if a string is a valid URL""" try: