Skip to content

Commit c4ea9ad

Browse files
committed
Refactor code
1 parent 8003b71 commit c4ea9ad

2 files changed

Lines changed: 10 additions & 26 deletions

File tree

trpc_agent_sdk/code_executors/_base_code_executor.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
from typing import Optional
1717

1818
from pydantic import BaseModel
19+
from pydantic import Field
1920
from trpc_agent_sdk.context import InvocationContext
2021

2122
from ._base_workspace_runtime import BaseWorkspaceRuntime
2223
from ._types import CodeBlockDelimiter
2324
from ._types import CodeExecutionInput
2425
from ._types import CodeExecutionResult
2526

27+
DEFAULT_CODE_BLOCK_DELIMITERS: tuple[CodeBlockDelimiter, ...] = (
28+
CodeBlockDelimiter(start="```tool_code\n", end="\n```"),
29+
CodeBlockDelimiter(start="```python\n", end="\n```"),
30+
)
31+
2632

2733
class BaseCodeExecutor(BaseModel):
2834
"""Abstract base class for all code executors.
@@ -67,10 +73,7 @@ class BaseCodeExecutor(BaseModel):
6773
auto-execution attempts for that invocation.
6874
"""
6975

70-
code_block_delimiters: list[CodeBlockDelimiter] = [
71-
CodeBlockDelimiter(start="```tool_code\n", end="\n```"),
72-
CodeBlockDelimiter(start="```python\n", end="\n```"),
73-
]
76+
code_block_delimiters: list[CodeBlockDelimiter] = Field(default_factory=lambda: list(DEFAULT_CODE_BLOCK_DELIMITERS))
7477
"""The list of the enclosing delimiters to identify the code blocks.
7578
7679
For example, the delimiter ('```python\\n', '\\n```') can be

trpc_agent_sdk/code_executors/cube/_code_executor.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from trpc_agent_sdk.context import InvocationContext
2222

2323
from .._base_code_executor import BaseCodeExecutor
24+
from .._base_code_executor import DEFAULT_CODE_BLOCK_DELIMITERS
2425
from .._types import CodeBlock
2526
from .._types import CodeBlockDelimiter
2627
from .._types import CodeExecutionInput
@@ -36,21 +37,6 @@
3637
_BASH_DELIMITER = CodeBlockDelimiter(start="```bash\n", end="\n```")
3738

3839

39-
def _cube_default_code_block_delimiters() -> list[CodeBlockDelimiter]:
40-
"""Default delimiters for :class:`CubeCodeExecutor`.
41-
42-
Reuses :attr:`BaseCodeExecutor.code_block_delimiters` so the parent's
43-
defaults (currently ``tool_code`` + ``python``) stay the single
44-
source of truth, then appends a ``bash`` fence so text-path
45-
extraction matches what :meth:`CubeCodeExecutor.execute_code` can
46-
actually run (see :meth:`_select_interpreter`). Returns a fresh list
47-
on every call to keep per-instance defaults independent of one
48-
another.
49-
"""
50-
parent_default = BaseCodeExecutor.model_fields["code_block_delimiters"].default
51-
return [*parent_default, _BASH_DELIMITER]
52-
53-
5440
class CubeCodeExecutor(BaseCodeExecutor):
5541
"""A code executor that runs blocks inside a Cube/E2B remote sandbox.
5642
@@ -84,13 +70,8 @@ class CubeCodeExecutor(BaseCodeExecutor):
8470
stateful: bool = Field(default=False, frozen=True, exclude=True)
8571
optimize_data_file: bool = Field(default=False, frozen=True, exclude=True)
8672

87-
# Extend the inherited default with a ``bash`` fence so text-path
88-
# extraction matches what ``execute_code`` can actually run (see
89-
# ``_select_interpreter``). The factory reads the parent's default
90-
# at call time so the base list stays the single source of truth;
91-
# callers may still override via the ``code_block_delimiters`` field
92-
# at construction time.
93-
code_block_delimiters: list[CodeBlockDelimiter] = Field(default_factory=_cube_default_code_block_delimiters, )
73+
code_block_delimiters: list[CodeBlockDelimiter] = Field(
74+
default_factory=lambda: [*DEFAULT_CODE_BLOCK_DELIMITERS, _BASH_DELIMITER])
9475

9576
# `_client` is `Optional` because :meth:`close` / :meth:`destroy`
9677
# legitimately drop the handle post-construction. `_cfg` has no such

0 commit comments

Comments
 (0)