Disable code sandbox if no code sandbox configured by admin

Either set the Terrarium sandbox url or the E2B api key to enable code
sandbox
This commit is contained in:
Debanjum
2025-08-31 10:08:15 -07:00
parent 3441783d5b
commit 3c1948e9de
4 changed files with 15 additions and 1 deletions

View File

@@ -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"

View File

@@ -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'

View File

@@ -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:

View File

@@ -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: