letta-nightly 0.5.4.dev20241202104311__py3-none-any.whl → 0.5.4.dev20241203104336__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of letta-nightly might be problematic. Click here for more details.

@@ -1,11 +1,8 @@
1
- import importlib
2
1
  import inspect
3
- import os
4
2
  from textwrap import dedent # remove indentation
5
3
  from types import ModuleType
6
4
  from typing import Dict, List, Optional
7
5
 
8
- from letta.constants import CLI_WARNING_PREFIX
9
6
  from letta.errors import LettaToolCreateError
10
7
  from letta.functions.schema_generator import generate_schema
11
8
 
@@ -90,46 +87,3 @@ def load_function_set(module: ModuleType) -> dict:
90
87
  if len(function_dict) == 0:
91
88
  raise ValueError(f"No functions found in module {module}")
92
89
  return function_dict
93
-
94
-
95
- def validate_function(module_name, module_full_path):
96
- try:
97
- file = os.path.basename(module_full_path)
98
- spec = importlib.util.spec_from_file_location(module_name, module_full_path)
99
- module = importlib.util.module_from_spec(spec)
100
- spec.loader.exec_module(module)
101
- except ModuleNotFoundError as e:
102
- # Handle missing module imports
103
- missing_package = str(e).split("'")[1] # Extract the name of the missing package
104
- print(f"{CLI_WARNING_PREFIX}skipped loading python file '{module_full_path}'!")
105
- return (
106
- False,
107
- f"'{file}' imports '{missing_package}', but '{missing_package}' is not installed locally - install python package '{missing_package}' to link functions from '{file}' to Letta.",
108
- )
109
- except SyntaxError as e:
110
- # Handle syntax errors in the module
111
- return False, f"{CLI_WARNING_PREFIX}skipped loading python file '{file}' due to a syntax error: {e}"
112
- except Exception as e:
113
- # Handle other general exceptions
114
- return False, f"{CLI_WARNING_PREFIX}skipped loading python file '{file}': {e}"
115
-
116
- return True, None
117
-
118
-
119
- def load_function_file(filepath: str) -> dict:
120
- file = os.path.basename(filepath)
121
- module_name = file[:-3] # Remove '.py' from filename
122
- try:
123
- spec = importlib.util.spec_from_file_location(module_name, filepath)
124
- module = importlib.util.module_from_spec(spec)
125
- spec.loader.exec_module(module)
126
- except ModuleNotFoundError as e:
127
- # Handle missing module imports
128
- missing_package = str(e).split("'")[1] # Extract the name of the missing package
129
- print(f"{CLI_WARNING_PREFIX}skipped loading python file '{filepath}'!")
130
- print(
131
- f"'{file}' imports '{missing_package}', but '{missing_package}' is not installed locally - install python package '{missing_package}' to link functions from '{file}' to Letta."
132
- )
133
- # load all functions in the module
134
- function_dict = load_function_set(module)
135
- return function_dict
letta/schemas/tool.py CHANGED
@@ -201,3 +201,15 @@ class ToolUpdate(LettaBase):
201
201
  class Config:
202
202
  extra = "ignore" # Allows extra fields without validation errors
203
203
  # TODO: Remove this, and clean usage of ToolUpdate everywhere else
204
+
205
+
206
+ class ToolRun(LettaBase):
207
+ id: str = Field(..., description="The ID of the tool to run.")
208
+ args: str = Field(..., description="The arguments to pass to the tool (as stringified JSON).")
209
+
210
+
211
+ class ToolRunFromSource(LettaBase):
212
+ args: str = Field(..., description="The arguments to pass to the tool (as stringified JSON).")
213
+ name: Optional[str] = Field(..., description="The name of the tool to run.")
214
+ source_code: str = Field(None, description="The source code of the function.")
215
+ source_type: Optional[str] = Field(None, description="The type of the source code.")
@@ -448,21 +448,18 @@ async def send_message(
448
448
  This endpoint accepts a message from a user and processes it through the agent.
449
449
  """
450
450
  actor = server.get_user_or_default(user_id=user_id)
451
-
452
- agent_lock = server.per_agent_lock_manager.get_lock(agent_id)
453
- async with agent_lock:
454
- result = await send_message_to_agent(
455
- server=server,
456
- agent_id=agent_id,
457
- user_id=actor.id,
458
- messages=request.messages,
459
- stream_steps=False,
460
- stream_tokens=False,
461
- # Support for AssistantMessage
462
- assistant_message_tool_name=request.assistant_message_tool_name,
463
- assistant_message_tool_kwarg=request.assistant_message_tool_kwarg,
464
- )
465
- return result
451
+ result = await send_message_to_agent(
452
+ server=server,
453
+ agent_id=agent_id,
454
+ user_id=actor.id,
455
+ messages=request.messages,
456
+ stream_steps=False,
457
+ stream_tokens=False,
458
+ # Support for AssistantMessage
459
+ assistant_message_tool_name=request.assistant_message_tool_name,
460
+ assistant_message_tool_kwarg=request.assistant_message_tool_kwarg,
461
+ )
462
+ return result
466
463
 
467
464
 
468
465
  @router.post(
@@ -490,21 +487,18 @@ async def send_message_streaming(
490
487
  It will stream the steps of the response always, and stream the tokens if 'stream_tokens' is set to True.
491
488
  """
492
489
  actor = server.get_user_or_default(user_id=user_id)
493
-
494
- agent_lock = server.per_agent_lock_manager.get_lock(agent_id)
495
- async with agent_lock:
496
- result = await send_message_to_agent(
497
- server=server,
498
- agent_id=agent_id,
499
- user_id=actor.id,
500
- messages=request.messages,
501
- stream_steps=True,
502
- stream_tokens=request.stream_tokens,
503
- # Support for AssistantMessage
504
- assistant_message_tool_name=request.assistant_message_tool_name,
505
- assistant_message_tool_kwarg=request.assistant_message_tool_kwarg,
506
- )
507
- return result
490
+ result = await send_message_to_agent(
491
+ server=server,
492
+ agent_id=agent_id,
493
+ user_id=actor.id,
494
+ messages=request.messages,
495
+ stream_steps=True,
496
+ stream_tokens=request.stream_tokens,
497
+ # Support for AssistantMessage
498
+ assistant_message_tool_name=request.assistant_message_tool_name,
499
+ assistant_message_tool_kwarg=request.assistant_message_tool_kwarg,
500
+ )
501
+ return result
508
502
 
509
503
 
510
504
  # TODO: move this into server.py?
@@ -8,6 +8,7 @@ from letta.schemas.sandbox_config import SandboxEnvironmentVariable as PydanticE
8
8
  from letta.schemas.sandbox_config import (
9
9
  SandboxEnvironmentVariableCreate,
10
10
  SandboxEnvironmentVariableUpdate,
11
+ SandboxType,
11
12
  )
12
13
  from letta.server.rest_api.utils import get_letta_server, get_user_id
13
14
  from letta.server.server import SyncServer
@@ -29,6 +30,24 @@ def create_sandbox_config(
29
30
  return server.sandbox_config_manager.create_or_update_sandbox_config(config_create, actor)
30
31
 
31
32
 
33
+ @router.post("/e2b/default", response_model=PydanticSandboxConfig)
34
+ def create_default_e2b_sandbox_config(
35
+ server: SyncServer = Depends(get_letta_server),
36
+ user_id: str = Depends(get_user_id),
37
+ ):
38
+ actor = server.get_user_or_default(user_id=user_id)
39
+ return server.sandbox_config_manager.get_or_create_default_sandbox_config(sandbox_type=SandboxType.E2B, actor=actor)
40
+
41
+
42
+ @router.post("/local/default", response_model=PydanticSandboxConfig)
43
+ def create_default_local_sandbox_config(
44
+ server: SyncServer = Depends(get_letta_server),
45
+ user_id: str = Depends(get_user_id),
46
+ ):
47
+ actor = server.get_user_or_default(user_id=user_id)
48
+ return server.sandbox_config_manager.get_or_create_default_sandbox_config(sandbox_type=SandboxType.LOCAL, actor=actor)
49
+
50
+
32
51
  @router.patch("/{sandbox_config_id}", response_model=PydanticSandboxConfig)
33
52
  def update_sandbox_config(
34
53
  sandbox_config_id: str,
@@ -1,10 +1,12 @@
1
1
  from typing import List, Optional
2
2
 
3
+ from composio.client.collections import ActionModel, AppModel
3
4
  from fastapi import APIRouter, Body, Depends, Header, HTTPException
4
5
 
5
6
  from letta.errors import LettaToolCreateError
6
7
  from letta.orm.errors import UniqueConstraintViolationError
7
- from letta.schemas.tool import Tool, ToolCreate, ToolUpdate
8
+ from letta.schemas.letta_message import FunctionReturn
9
+ from letta.schemas.tool import Tool, ToolCreate, ToolRunFromSource, ToolUpdate
8
10
  from letta.server.rest_api.utils import get_letta_server
9
11
  from letta.server.server import SyncServer
10
12
 
@@ -156,3 +158,74 @@ def add_base_tools(
156
158
  """
157
159
  actor = server.get_user_or_default(user_id=user_id)
158
160
  return server.tool_manager.add_base_tools(actor=actor)
161
+
162
+
163
+ # NOTE: can re-enable if needed
164
+ # @router.post("/{tool_id}/run", response_model=FunctionReturn, operation_id="run_tool")
165
+ # def run_tool(
166
+ # server: SyncServer = Depends(get_letta_server),
167
+ # request: ToolRun = Body(...),
168
+ # user_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
169
+ # ):
170
+ # """
171
+ # Run an existing tool on provided arguments
172
+ # """
173
+ # actor = server.get_user_or_default(user_id=user_id)
174
+
175
+ # return server.run_tool(tool_id=request.tool_id, tool_args=request.tool_args, user_id=actor.id)
176
+
177
+
178
+ @router.post("/run", response_model=FunctionReturn, operation_id="run_tool_from_source")
179
+ def run_tool_from_source(
180
+ server: SyncServer = Depends(get_letta_server),
181
+ request: ToolRunFromSource = Body(...),
182
+ user_id: Optional[str] = Header(None, alias="user_id"), # Extract user_id from header, default to None if not present
183
+ ):
184
+ """
185
+ Attempt to build a tool from source, then run it on the provided arguments
186
+ """
187
+ actor = server.get_user_or_default(user_id=user_id)
188
+
189
+ return server.run_tool_from_source(
190
+ tool_source=request.source_code,
191
+ tool_source_type=request.source_type,
192
+ tool_args=request.args,
193
+ tool_name=request.name,
194
+ user_id=actor.id,
195
+ )
196
+
197
+
198
+ # Specific routes for Composio
199
+
200
+
201
+ @router.get("/composio/apps", response_model=List[AppModel], operation_id="list_composio_apps")
202
+ def list_composio_apps(server: SyncServer = Depends(get_letta_server)):
203
+ """
204
+ Get a list of all Composio apps
205
+ """
206
+ return server.get_composio_apps()
207
+
208
+
209
+ @router.get("/composio/apps/{composio_app_name}/actions", response_model=List[ActionModel], operation_id="list_composio_actions_by_app")
210
+ def list_composio_actions_by_app(
211
+ composio_app_name: str,
212
+ server: SyncServer = Depends(get_letta_server),
213
+ ):
214
+ """
215
+ Get a list of all Composio actions for a specific app
216
+ """
217
+ return server.get_composio_actions_from_app_name(composio_app_name=composio_app_name)
218
+
219
+
220
+ @router.post("/composio/{composio_action_name}", response_model=Tool, operation_id="add_composio_tool")
221
+ def add_composio_tool(
222
+ composio_action_name: str,
223
+ server: SyncServer = Depends(get_letta_server),
224
+ user_id: Optional[str] = Header(None, alias="user_id"),
225
+ ):
226
+ """
227
+ Add a new Composio tool by action name (Composio refers to each tool as an `Action`)
228
+ """
229
+ actor = server.get_user_or_default(user_id=user_id)
230
+ tool_create = ToolCreate.from_composio(action=composio_action_name)
231
+ return server.tool_manager.create_or_update_tool(pydantic_tool=Tool(**tool_create.model_dump()), actor=actor)
letta/server/server.py CHANGED
@@ -1,4 +1,5 @@
1
1
  # inspecting tools
2
+ import json
2
3
  import os
3
4
  import traceback
4
5
  import warnings
@@ -7,6 +8,8 @@ from asyncio import Lock
7
8
  from datetime import datetime
8
9
  from typing import Callable, Dict, List, Optional, Tuple, Union
9
10
 
11
+ from composio.client import Composio
12
+ from composio.client.collections import ActionModel, AppModel
10
13
  from fastapi import HTTPException
11
14
 
12
15
  import letta.constants as constants
@@ -54,7 +57,7 @@ from letta.schemas.embedding_config import EmbeddingConfig
54
57
  # openai schemas
55
58
  from letta.schemas.enums import JobStatus
56
59
  from letta.schemas.job import Job
57
- from letta.schemas.letta_message import LettaMessage
60
+ from letta.schemas.letta_message import FunctionReturn, LettaMessage
58
61
  from letta.schemas.llm_config import LLMConfig
59
62
  from letta.schemas.memory import (
60
63
  ArchivalMemorySummary,
@@ -76,9 +79,10 @@ from letta.services.organization_manager import OrganizationManager
76
79
  from letta.services.per_agent_lock_manager import PerAgentLockManager
77
80
  from letta.services.sandbox_config_manager import SandboxConfigManager
78
81
  from letta.services.source_manager import SourceManager
82
+ from letta.services.tool_execution_sandbox import ToolExecutionSandbox
79
83
  from letta.services.tool_manager import ToolManager
80
84
  from letta.services.user_manager import UserManager
81
- from letta.utils import create_random_username, json_dumps, json_loads
85
+ from letta.utils import create_random_username, get_utc_time, json_dumps, json_loads
82
86
 
83
87
  logger = get_logger(__name__)
84
88
 
@@ -227,6 +231,11 @@ class SyncServer(Server):
227
231
  # Locks
228
232
  self.send_message_lock = Lock()
229
233
 
234
+ # Composio
235
+ self.composio_client = None
236
+ if tool_settings.composio_api_key:
237
+ self.composio_client = Composio(api_key=tool_settings.composio_api_key)
238
+
230
239
  # Initialize the metadata store
231
240
  config = LettaConfig.load()
232
241
  if settings.letta_pg_uri_no_default:
@@ -365,14 +374,20 @@ class SyncServer(Server):
365
374
 
366
375
  def load_agent(self, agent_id: str, interface: Union[AgentInterface, None] = None) -> Agent:
367
376
  """Updated method to load agents from persisted storage"""
368
- agent_state = self.get_agent(agent_id=agent_id)
369
- actor = self.user_manager.get_user_by_id(user_id=agent_state.user_id)
377
+ agent_lock = self.per_agent_lock_manager.get_lock(agent_id)
378
+ with agent_lock:
379
+ agent_state = self.get_agent(agent_id=agent_id)
380
+ actor = self.user_manager.get_user_by_id(user_id=agent_state.user_id)
381
+
382
+ interface = interface or self.default_interface_factory()
383
+ if agent_state.agent_type == AgentType.memgpt_agent:
384
+ agent = Agent(agent_state=agent_state, interface=interface, user=actor)
385
+ else:
386
+ agent = O1Agent(agent_state=agent_state, interface=interface, user=actor)
370
387
 
371
- interface = interface or self.default_interface_factory()
372
- if agent_state.agent_type == AgentType.memgpt_agent:
373
- return Agent(agent_state=agent_state, interface=interface, user=actor)
374
- else:
375
- return O1Agent(agent_state=agent_state, interface=interface, user=actor)
388
+ # Persist to agent
389
+ save_agent(agent, self.ms)
390
+ return agent
376
391
 
377
392
  def _step(
378
393
  self,
@@ -1715,7 +1730,7 @@ class SyncServer(Server):
1715
1730
  self.blocks_agents_manager.add_block_to_agent(agent_id, block_id, block_label=block.label)
1716
1731
 
1717
1732
  # get agent memory
1718
- memory = self.load_agent(agent_id=agent_id).agent_state.memory
1733
+ memory = self.get_agent(agent_id=agent_id).memory
1719
1734
  return memory
1720
1735
 
1721
1736
  def unlink_block_from_agent_memory(self, user_id: str, agent_id: str, block_label: str, delete_if_no_ref: bool = True) -> Memory:
@@ -1723,7 +1738,7 @@ class SyncServer(Server):
1723
1738
  self.blocks_agents_manager.remove_block_with_label_from_agent(agent_id=agent_id, block_label=block_label)
1724
1739
 
1725
1740
  # get agent memory
1726
- memory = self.load_agent(agent_id=agent_id).agent_state.memory
1741
+ memory = self.get_agent(agent_id=agent_id).memory
1727
1742
  return memory
1728
1743
 
1729
1744
  def update_agent_memory_limit(self, user_id: str, agent_id: str, block_label: str, limit: int) -> Memory:
@@ -1733,7 +1748,7 @@ class SyncServer(Server):
1733
1748
  block_id=block.id, block_update=BlockUpdate(limit=limit), actor=self.user_manager.get_user_by_id(user_id=user_id)
1734
1749
  )
1735
1750
  # get agent memory
1736
- memory = self.load_agent(agent_id=agent_id).agent_state.memory
1751
+ memory = self.get_agent(agent_id=agent_id).memory
1737
1752
  return memory
1738
1753
 
1739
1754
  def upate_block(self, user_id: str, block_id: str, block_update: BlockUpdate) -> Block:
@@ -1750,3 +1765,122 @@ class SyncServer(Server):
1750
1765
  if block.label == label:
1751
1766
  return block
1752
1767
  return None
1768
+
1769
+ # def run_tool(self, tool_id: str, tool_args: str, user_id: str) -> FunctionReturn:
1770
+ # """Run a tool using the sandbox and return the result"""
1771
+
1772
+ # try:
1773
+ # tool_args_dict = json.loads(tool_args)
1774
+ # except json.JSONDecodeError:
1775
+ # raise ValueError("Invalid JSON string for tool_args")
1776
+
1777
+ # # Get the tool by ID
1778
+ # user = self.user_manager.get_user_by_id(user_id=user_id)
1779
+ # tool = self.tool_manager.get_tool_by_id(tool_id=tool_id, actor=user)
1780
+ # if tool.name is None:
1781
+ # raise ValueError(f"Tool with id {tool_id} does not have a name")
1782
+
1783
+ # # TODO eventually allow using agent state in tools
1784
+ # agent_state = None
1785
+
1786
+ # try:
1787
+ # sandbox_run_result = ToolExecutionSandbox(tool.name, tool_args_dict, user_id).run(agent_state=agent_state)
1788
+ # if sandbox_run_result is None:
1789
+ # raise ValueError(f"Tool with id {tool_id} returned execution with None")
1790
+ # function_response = str(sandbox_run_result.func_return)
1791
+
1792
+ # return FunctionReturn(
1793
+ # id="null",
1794
+ # function_call_id="null",
1795
+ # date=get_utc_time(),
1796
+ # status="success",
1797
+ # function_return=function_response,
1798
+ # )
1799
+ # except Exception as e:
1800
+ # # same as agent.py
1801
+ # from letta.constants import MAX_ERROR_MESSAGE_CHAR_LIMIT
1802
+
1803
+ # error_msg = f"Error executing tool {tool.name}: {e}"
1804
+ # if len(error_msg) > MAX_ERROR_MESSAGE_CHAR_LIMIT:
1805
+ # error_msg = error_msg[:MAX_ERROR_MESSAGE_CHAR_LIMIT]
1806
+
1807
+ # return FunctionReturn(
1808
+ # id="null",
1809
+ # function_call_id="null",
1810
+ # date=get_utc_time(),
1811
+ # status="error",
1812
+ # function_return=error_msg,
1813
+ # )
1814
+
1815
+ def run_tool_from_source(
1816
+ self,
1817
+ user_id: str,
1818
+ tool_args: str,
1819
+ tool_source: str,
1820
+ tool_source_type: Optional[str] = None,
1821
+ tool_name: Optional[str] = None,
1822
+ ) -> FunctionReturn:
1823
+ """Run a tool from source code"""
1824
+
1825
+ try:
1826
+ tool_args_dict = json.loads(tool_args)
1827
+ except json.JSONDecodeError:
1828
+ raise ValueError("Invalid JSON string for tool_args")
1829
+
1830
+ if tool_source_type is not None and tool_source_type != "python":
1831
+ raise ValueError("Only Python source code is supported at this time")
1832
+
1833
+ # NOTE: we're creating a floating Tool object and NOT persiting to DB
1834
+ tool = Tool(
1835
+ name=tool_name,
1836
+ source_code=tool_source,
1837
+ )
1838
+ assert tool.name is not None, "Failed to create tool object"
1839
+
1840
+ # TODO eventually allow using agent state in tools
1841
+ agent_state = None
1842
+
1843
+ # Next, attempt to run the tool with the sandbox
1844
+ try:
1845
+ sandbox_run_result = ToolExecutionSandbox(tool.name, tool_args_dict, user_id, tool_object=tool).run(agent_state=agent_state)
1846
+ if sandbox_run_result is None:
1847
+ raise ValueError(f"Tool with id {tool.id} returned execution with None")
1848
+ function_response = str(sandbox_run_result.func_return)
1849
+
1850
+ return FunctionReturn(
1851
+ id="null",
1852
+ function_call_id="null",
1853
+ date=get_utc_time(),
1854
+ status="success",
1855
+ function_return=function_response,
1856
+ )
1857
+ except Exception as e:
1858
+ # same as agent.py
1859
+ from letta.constants import MAX_ERROR_MESSAGE_CHAR_LIMIT
1860
+
1861
+ error_msg = f"Error executing tool {tool.name}: {e}"
1862
+ if len(error_msg) > MAX_ERROR_MESSAGE_CHAR_LIMIT:
1863
+ error_msg = error_msg[:MAX_ERROR_MESSAGE_CHAR_LIMIT]
1864
+
1865
+ return FunctionReturn(
1866
+ id="null",
1867
+ function_call_id="null",
1868
+ date=get_utc_time(),
1869
+ status="error",
1870
+ function_return=error_msg,
1871
+ )
1872
+
1873
+ # Composio wrappers
1874
+ def get_composio_apps(self) -> List["AppModel"]:
1875
+ """Get a list of all Composio apps with actions"""
1876
+ apps = self.composio_client.apps.get()
1877
+ apps_with_actions = []
1878
+ for app in apps:
1879
+ if app.meta["actionsCount"] > 0:
1880
+ apps_with_actions.append(app)
1881
+
1882
+ return apps_with_actions
1883
+
1884
+ def get_composio_actions_from_app_name(self, composio_app_name: str) -> List["ActionModel"]:
1885
+ actions = self.composio_client.actions.get(apps=[composio_app_name])
1886
+ return actions
@@ -1,4 +1,4 @@
1
- import asyncio
1
+ import threading
2
2
  from collections import defaultdict
3
3
 
4
4
 
@@ -6,9 +6,9 @@ class PerAgentLockManager:
6
6
  """Manages per-agent locks."""
7
7
 
8
8
  def __init__(self):
9
- self.locks = defaultdict(asyncio.Lock)
9
+ self.locks = defaultdict(threading.Lock)
10
10
 
11
- def get_lock(self, agent_id: str) -> asyncio.Lock:
11
+ def get_lock(self, agent_id: str) -> threading.Lock:
12
12
  """Retrieve the lock for a specific agent_id."""
13
13
  return self.locks[agent_id]
14
14
 
@@ -11,6 +11,7 @@ from typing import Any, Optional
11
11
  from letta.log import get_logger
12
12
  from letta.schemas.agent import AgentState
13
13
  from letta.schemas.sandbox_config import SandboxConfig, SandboxRunResult, SandboxType
14
+ from letta.schemas.tool import Tool
14
15
  from letta.services.sandbox_config_manager import SandboxConfigManager
15
16
  from letta.services.tool_manager import ToolManager
16
17
  from letta.services.user_manager import UserManager
@@ -27,7 +28,7 @@ class ToolExecutionSandbox:
27
28
  # We make this a long random string to avoid collisions with any variables in the user's code
28
29
  LOCAL_SANDBOX_RESULT_VAR_NAME = "result_ZQqiequkcFwRwwGQMqkt"
29
30
 
30
- def __init__(self, tool_name: str, args: dict, user_id: str, force_recreate=False):
31
+ def __init__(self, tool_name: str, args: dict, user_id: str, force_recreate=False, tool_object: Optional[Tool] = None):
31
32
  self.tool_name = tool_name
32
33
  self.args = args
33
34
 
@@ -36,14 +37,18 @@ class ToolExecutionSandbox:
36
37
  # agent_state is the state of the agent that invoked this run
37
38
  self.user = UserManager().get_user_by_id(user_id=user_id)
38
39
 
39
- # Get the tool
40
- # TODO: So in theory, it's possible this retrieves a tool not provisioned to the agent
41
- # TODO: That would probably imply that agent_state is incorrectly configured
42
- self.tool = ToolManager().get_tool_by_name(tool_name=tool_name, actor=self.user)
43
- if not self.tool:
44
- raise ValueError(
45
- f"Agent attempted to invoke tool {self.tool_name} that does not exist for organization {self.user.organization_id}"
46
- )
40
+ # If a tool object is provided, we use it directly, otherwise pull via name
41
+ if tool_object is not None:
42
+ self.tool = tool_object
43
+ else:
44
+ # Get the tool via name
45
+ # TODO: So in theory, it's possible this retrieves a tool not provisioned to the agent
46
+ # TODO: That would probably imply that agent_state is incorrectly configured
47
+ self.tool = ToolManager().get_tool_by_name(tool_name=tool_name, actor=self.user)
48
+ if not self.tool:
49
+ raise ValueError(
50
+ f"Agent attempted to invoke tool {self.tool_name} that does not exist for organization {self.user.organization_id}"
51
+ )
47
52
 
48
53
  self.sandbox_config_manager = SandboxConfigManager(tool_settings)
49
54
  self.force_recreate = force_recreate
@@ -132,7 +137,8 @@ class ToolExecutionSandbox:
132
137
  sandbox_config_fingerprint=sbx_config.fingerprint(),
133
138
  )
134
139
  except Exception as e:
135
- raise RuntimeError(f"Executing tool {self.tool_name} has an unexpected error: {e}")
140
+ logger.error(f"Executing tool {self.tool_name} has an unexpected error: {e}")
141
+ raise e
136
142
  finally:
137
143
  # Clean up the temp file and restore stdout
138
144
  sys.stdout = old_stdout
@@ -154,7 +160,9 @@ class ToolExecutionSandbox:
154
160
  env_vars = self.sandbox_config_manager.get_sandbox_env_vars_as_dict(sandbox_config_id=sbx_config.id, actor=self.user, limit=100)
155
161
  execution = sbx.run_code(code, envs=env_vars)
156
162
  if execution.error is not None:
157
- raise Exception(f"Executing tool {self.tool_name} failed with {execution.error}")
163
+ logger.error(f"Executing tool {self.tool_name} failed with {execution.error}")
164
+ # Raise a concise exception as this gets returned to the LLM
165
+ raise self.parse_exception_from_e2b_execution(execution)
158
166
  elif len(execution.results) == 0:
159
167
  return None
160
168
  else:
@@ -166,6 +174,12 @@ class ToolExecutionSandbox:
166
174
  sandbox_config_fingerprint=sbx_config.fingerprint(),
167
175
  )
168
176
 
177
+ def parse_exception_from_e2b_execution(self, e2b_execution: "Execution") -> Exception:
178
+ builtins_dict = __builtins__ if isinstance(__builtins__, dict) else vars(__builtins__)
179
+ # Dynamically fetch the exception class from builtins, defaulting to Exception if not found
180
+ exception_class = builtins_dict.get(e2b_execution.error.name, Exception)
181
+ return exception_class(e2b_execution.error.value)
182
+
169
183
  def get_running_e2b_sandbox_with_same_state(self, sandbox_config: SandboxConfig) -> Optional["Sandbox"]:
170
184
  from e2b_code_interpreter import Sandbox
171
185
 
letta/utils.py CHANGED
@@ -1015,13 +1015,6 @@ def get_persona_text(name: str, enforce_limit=True):
1015
1015
  raise ValueError(f"Persona {name}.txt not found")
1016
1016
 
1017
1017
 
1018
- def get_human_text(name: str):
1019
- for file_path in list_human_files():
1020
- file = os.path.basename(file_path)
1021
- if f"{name}.txt" == file or name == file:
1022
- return open(file_path, "r", encoding="utf-8").read().strip()
1023
-
1024
-
1025
1018
  def get_schema_diff(schema_a, schema_b):
1026
1019
  # Assuming f_schema and linked_function['json_schema'] are your JSON schemas
1027
1020
  f_schema_json = json_dumps(schema_a)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: letta-nightly
3
- Version: 0.5.4.dev20241202104311
3
+ Version: 0.5.4.dev20241203104336
4
4
  Summary: Create LLM agents with long-term memory and custom tools
5
5
  License: Apache License
6
6
  Author: Letta Team
@@ -129,24 +129,24 @@ The two main ways to install Letta are through **pypi** (`pip`) or via **Docker*
129
129
 
130
130
  ### Step 1 - Install Letta using `pip`
131
131
  ```sh
132
- $ pip install -U letta
132
+ pip install -U letta
133
133
  ```
134
134
 
135
135
  ### Step 2 - Set your environment variables for your chosen LLM / embedding providers
136
136
  ```sh
137
- $ export OPENAI_API_KEY=sk-...
137
+ export OPENAI_API_KEY=sk-...
138
138
  ```
139
139
 
140
140
  For Ollama (see our full [documentation](https://docs.letta.com/install) for examples of how to set up various providers):
141
141
  ```sh
142
- $ export OLLAMA_BASE_URL=http://localhost:11434
142
+ export OLLAMA_BASE_URL=http://localhost:11434
143
143
  ```
144
144
 
145
145
  ### Step 3 - Run the Letta CLI
146
146
 
147
147
  You can create agents and chat with them via the Letta CLI tool (`letta run`):
148
148
  ```sh
149
- $ letta run
149
+ letta run
150
150
  ```
151
151
  ```
152
152
  🧬 Creating new agent...
@@ -179,7 +179,7 @@ Hit enter to begin (will request first Letta message)
179
179
 
180
180
  You can start the Letta API server with `letta server` (see the full API reference [here](https://docs.letta.com/api-reference)):
181
181
  ```sh
182
- $ letta server
182
+ letta server
183
183
  ```
184
184
  ```
185
185
  Initializing database...
@@ -26,7 +26,7 @@ letta/errors.py,sha256=mFeTpZP37otDMr68s9hyGOnafJPrWeblQOI79cgP4nQ,3209
26
26
  letta/functions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
27
  letta/functions/function_sets/base.py,sha256=9Rs8SNrtUgqYtlmztE1gVO6FEn864u8t-X1qik24nps,8096
28
28
  letta/functions/function_sets/extras.py,sha256=Jik3UiDqYTm4Lam1XPTvuVjvgUHwIAhopsnbmVhGMBg,4732
29
- letta/functions/functions.py,sha256=qCoU9w51uXC8NODHr6dj_tz69NB4924I9PToN2yh2NA,5418
29
+ letta/functions/functions.py,sha256=evH6GKnIJwVVre1Xre2gaSIqREv4eNM4DiWOhn8PMqg,3299
30
30
  letta/functions/helpers.py,sha256=K84kqAN1RXZIhjb7-btS0C2p-SInYNv6FvSfo-16Y6g,8578
31
31
  letta/functions/schema_generator.py,sha256=Y0rQjJBI8Z5fSKmT71EGXtHpIvNb3dMM5X00TP89tlY,19330
32
32
  letta/helpers/__init__.py,sha256=p0luQ1Oe3Skc6sH4O58aHHA3Qbkyjifpuq0DZ1GAY0U,59
@@ -156,7 +156,7 @@ letta/schemas/organization.py,sha256=d2oN3IK2HeruEHKXwIzCbJ3Fxdi_BEe9JZ8J9aDbHwQ
156
156
  letta/schemas/passage.py,sha256=eYQMxD_XjHAi72jmqcGBU4wM4VZtSU0XK8uhQxxN3Ug,3563
157
157
  letta/schemas/sandbox_config.py,sha256=LC0hnB3TbFJmY7lXqVsseJkqTbxry0xmBB0bwI8Y7Rc,4769
158
158
  letta/schemas/source.py,sha256=B1VbaDJV-EGPv1nQXwCx_RAzeAJd50UqP_1m1cIRT8c,2854
159
- letta/schemas/tool.py,sha256=DolG1PZDacREGQco7gYFVJNZFLjfBa1jDRmuCPKNiuM,9131
159
+ letta/schemas/tool.py,sha256=V8eTe2I1P9khanOJQYICmT0Fo_rMY599neGVW4Wn2Cw,9715
160
160
  letta/schemas/tool_rule.py,sha256=pLt-BzgFSrlVO6ipY4kygyvfoM0BWA-XdqhGxso9aKs,1192
161
161
  letta/schemas/usage.py,sha256=lvn1ooHwLEdv6gwQpw5PBUbcwn_gwdT6HA-fCiix6sY,817
162
162
  letta/schemas/user.py,sha256=V32Tgl6oqB3KznkxUz12y7agkQicjzW7VocSpj78i6Q,1526
@@ -178,19 +178,19 @@ letta/server/rest_api/routers/openai/assistants/threads.py,sha256=g8iu98__tQEMY9
178
178
  letta/server/rest_api/routers/openai/chat_completions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
179
179
  letta/server/rest_api/routers/openai/chat_completions/chat_completions.py,sha256=qFMpxfYIlJ-PW08IQt09RW44u6hwkssdsUT-h_GuOvE,4836
180
180
  letta/server/rest_api/routers/v1/__init__.py,sha256=RZc0fIHNN4BGretjU6_TGK7q49RyV4jfYNudoiK_sUo,762
181
- letta/server/rest_api/routers/v1/agents.py,sha256=SHWsfcSB86WS23wiGqmZFeY0ns5s1VpzJ2mQZYpo60M,25168
181
+ letta/server/rest_api/routers/v1/agents.py,sha256=Tj7QyHjem_tOgzDzTyEJREDH2rzDgpE6S5azBDrhY_o,24884
182
182
  letta/server/rest_api/routers/v1/blocks.py,sha256=UCVfMbb8hzOXI6a8OYWKuXyOropIxw6PYKZkwwAh1v0,4880
183
183
  letta/server/rest_api/routers/v1/health.py,sha256=pKCuVESlVOhGIb4VC4K-H82eZqfghmT6kvj2iOkkKuc,401
184
184
  letta/server/rest_api/routers/v1/jobs.py,sha256=a-j0v-5A0un0pVCOHpfeWnzpOWkVDQO6ti42k_qAlZY,2272
185
185
  letta/server/rest_api/routers/v1/llms.py,sha256=TcyvSx6MEM3je5F4DysL7ligmssL_pFlJaaO4uL95VY,877
186
186
  letta/server/rest_api/routers/v1/organizations.py,sha256=tyqVzXTpMtk3sKxI3Iz4aS6RhbGEbXDzFBB_CpW18v4,2080
187
- letta/server/rest_api/routers/v1/sandbox_configs.py,sha256=j-q9coHFhrsRwyswGyrVPUjawI0Iy6eYaG4iKd6ZKMA,4219
187
+ letta/server/rest_api/routers/v1/sandbox_configs.py,sha256=4tkTH8z9vpuBiGzxrS_wxkFdznnWZx-U-9F08czHMP8,5004
188
188
  letta/server/rest_api/routers/v1/sources.py,sha256=5Cs2YTSooh_WNT2C18PsdKzkyr4ZvaHt5Xjubyz0yJw,9196
189
- letta/server/rest_api/routers/v1/tools.py,sha256=eDzRv7ETP8Tu0kPxFutLC1Lz4J411sHOqbHF9nD5MLs,6204
189
+ letta/server/rest_api/routers/v1/tools.py,sha256=jxw4DEM2OycGDOH4rVWMNQqtBqBJhXQruOpIYsVVyEk,8977
190
190
  letta/server/rest_api/routers/v1/users.py,sha256=M1wEr2IyHzuRwINYxLXTkrbAH3osLe_cWjzrWrzR1aw,3729
191
191
  letta/server/rest_api/static_files.py,sha256=NG8sN4Z5EJ8JVQdj19tkFa9iQ1kBPTab9f_CUxd_u4Q,3143
192
192
  letta/server/rest_api/utils.py,sha256=6c5a_-ZFTlwZ1IuzpRQtqxSG1eD56nNhKhWlrdgBYWk,3103
193
- letta/server/server.py,sha256=Ki5MrO5y0L9iCTO1SoNqRtCLKdWQ5uxoRDebAzPi8NM,73777
193
+ letta/server/server.py,sha256=jgi0QipurP1xZKUEdUDk-ZD_6MbB8rDw82fd0RB60wc,78943
194
194
  letta/server/startup.sh,sha256=wTOQOJJZw_Iec57WIu0UW0AVflk0ZMWYZWg8D3T_gSQ,698
195
195
  letta/server/static_files/assets/index-3ab03d5b.css,sha256=OrA9W4iKJ5h2Wlr7GwdAT4wow0CM8hVit1yOxEL49Qw,54295
196
196
  letta/server/static_files/assets/index-9fa459a2.js,sha256=wtfkyHnEIMACHKL3UgN_jZNOKWEcOFjmWoeRHLngPwk,1815584
@@ -208,10 +208,10 @@ letta/services/agents_tags_manager.py,sha256=zNqeXDpaf4dQ77jrRHiQfITdk4FawBzcND-
208
208
  letta/services/block_manager.py,sha256=TrbStwHAREwnybA6jZSkNPe-EYUa5rdiuliPR2PTV-M,5426
209
209
  letta/services/blocks_agents_manager.py,sha256=mfO3EMW9os_E1_r4SRlC2wmBFFLpt8p-yhdOH_Iotaw,5627
210
210
  letta/services/organization_manager.py,sha256=OfE2_NMmhqXURX4sg7hCOiFQVQpV5ZiPu7J3sboCSYc,3555
211
- letta/services/per_agent_lock_manager.py,sha256=02iw5e-xoLiKGqqn2KdJdk-QlrDHPz5oMuEs1ibwXHA,540
211
+ letta/services/per_agent_lock_manager.py,sha256=porM0cKKANQ1FvcGXOO_qM7ARk5Fgi1HVEAhXsAg9-4,546
212
212
  letta/services/sandbox_config_manager.py,sha256=9BCu59nHR4nIMFXgFyEMOY2UTmZvBMS3GlDBWWCHB4I,12648
213
213
  letta/services/source_manager.py,sha256=StX5Wfd7XSCKJet8qExIu3GMoI-eMIbEarAeTv2gq0s,6555
214
- letta/services/tool_execution_sandbox.py,sha256=LrFzANZ75hNtAtfJiSOu2geu18d-9ms0IgN7SEVhh9I,13462
214
+ letta/services/tool_execution_sandbox.py,sha256=VaULtNz8n0lUbJDzahvyYZNujn8nnmeQeQ311kJe_uU,14324
215
215
  letta/services/tool_manager.py,sha256=FVCB9R3NFahh-KE5jROzf6J9WEgqhqGoDk5RpWjlgjg,7835
216
216
  letta/services/tool_sandbox_env/.gitkeep,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
217
217
  letta/services/user_manager.py,sha256=UJa0hqCjz0yXtvrCR8OVBqlSR5lC_Ejn-uG__58zLds,4398
@@ -219,9 +219,9 @@ letta/settings.py,sha256=ZcUcwvl7hStawZ0JOA0133jNk3j5qBd7qlFAAaIPsU8,3608
219
219
  letta/streaming_interface.py,sha256=_FPUWy58j50evHcpXyd7zB1wWqeCc71NCFeWh_TBvnw,15736
220
220
  letta/streaming_utils.py,sha256=329fsvj1ZN0r0LpQtmMPZ2vSxkDBIUUwvGHZFkjm2I8,11745
221
221
  letta/system.py,sha256=buKYPqG5n2x41hVmWpu6JUpyd7vTWED9Km2_M7dLrvk,6960
222
- letta/utils.py,sha256=COwQLAt02eEM9tjp6p5kN8YeTqGXr714l5BvffLVCLU,32376
223
- letta_nightly-0.5.4.dev20241202104311.dist-info/LICENSE,sha256=mExtuZ_GYJgDEI38GWdiEYZizZS4KkVt2SF1g_GPNhI,10759
224
- letta_nightly-0.5.4.dev20241202104311.dist-info/METADATA,sha256=X5kCjVS10z2psCmtDwSwrD9gZCsa8W5Ljgbh-X6N4nY,11515
225
- letta_nightly-0.5.4.dev20241202104311.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
226
- letta_nightly-0.5.4.dev20241202104311.dist-info/entry_points.txt,sha256=2zdiyGNEZGV5oYBuS-y2nAAgjDgcC9yM_mHJBFSRt5U,40
227
- letta_nightly-0.5.4.dev20241202104311.dist-info/RECORD,,
222
+ letta/utils.py,sha256=iELiiJhSnijGDmwyk_T4NBJIqFUnEw_Flv9ZpSBUPFA,32136
223
+ letta_nightly-0.5.4.dev20241203104336.dist-info/LICENSE,sha256=mExtuZ_GYJgDEI38GWdiEYZizZS4KkVt2SF1g_GPNhI,10759
224
+ letta_nightly-0.5.4.dev20241203104336.dist-info/METADATA,sha256=TLAqq1J7qA4wMmkrsK-u1P1ryMPcgolT0ho-je8B7UQ,11505
225
+ letta_nightly-0.5.4.dev20241203104336.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
226
+ letta_nightly-0.5.4.dev20241203104336.dist-info/entry_points.txt,sha256=2zdiyGNEZGV5oYBuS-y2nAAgjDgcC9yM_mHJBFSRt5U,40
227
+ letta_nightly-0.5.4.dev20241203104336.dist-info/RECORD,,