Skip to content

Commit 40e3ba1

Browse files
committed
Recommended fixes
1 parent 3e197f3 commit 40e3ba1

1 file changed

Lines changed: 15 additions & 44 deletions

File tree

python/beeai_framework/tools/scratchpad/scratchpad.py

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
import asyncio
15-
import logging
15+
from beeai_framework.logger import Logger
1616
import re
1717
from typing import ClassVar
1818

@@ -27,7 +27,7 @@
2727
ToolRunOptions,
2828
)
2929

30-
logger = logging.getLogger(__name__)
30+
logger = Logger(__name__)
3131

3232

3333
class ScratchpadInput(BaseModel):
@@ -90,55 +90,24 @@ def _ensure_session(cls, session_id: str) -> None:
9090
if session_id not in cls._scratchpads:
9191
cls._scratchpads[session_id] = []
9292

93-
def _get_session_id(self, context: RunContext | None = None) -> str:
93+
def _get_session_id(self) -> str:
9494
"""Extract session ID from context.
9595
9696
Caches the session ID on first call to ensure the same session
9797
is used across all tool calls for this tool instance.
9898
99-
Args:
100-
context: Run context to extract session identifier from.
101-
10299
Returns:
103100
Session ID string for data isolation.
104101
105102
Raises:
106-
ValueError: If no valid session ID can be extracted from context.
103+
ToolInputValidationError: If no valid session ID can be extracted from context.
107104
"""
108105
# Return cached session ID if we already determined it
109106
if self._cached_session_id:
110107
return self._cached_session_id
111108

112-
if not context:
113-
raise ToolInputValidationError(
114-
"Scratchpad requires RunContext with a valid session identifier. "
115-
"No context provided."
116-
)
117-
118-
# Try different context attributes in order of preference
119-
session_id = None
120-
121-
# run_id: Should persist across tool calls in the same agent run
122-
if hasattr(context, "run_id") and context.run_id:
123-
session_id = str(context.run_id)
124-
logger.debug(f"Using run_id as session: {session_id}")
125-
126-
# conversation_id: If available, persists across the conversation
127-
elif hasattr(context, "conversation_id") and context.conversation_id:
128-
session_id = str(context.conversation_id)
129-
logger.debug(f"Using conversation_id as session: {session_id}")
130-
131-
# agent_id: If available, unique per agent instance
132-
elif hasattr(context, "agent_id") and context.agent_id:
133-
session_id = str(context.agent_id)
134-
logger.debug(f"Using agent_id as session: {session_id}")
135-
136-
# No valid session ID found - raise error
137-
if not session_id:
138-
raise ToolInputValidationError(
139-
"Scratchpad requires RunContext with a valid session identifier "
140-
"(run_id, conversation_id, or agent_id). None found in context."
141-
)
109+
# Get run_id from RunContext as session identifier
110+
session_id = RunContext.get().run_id
142111

143112
# Cache the session ID for future calls
144113
self._cached_session_id = session_id
@@ -162,13 +131,17 @@ def description(self) -> str:
162131
)
163132

164133
@property
165-
def input_schema(self) -> type[BaseModel]:
134+
def input_schema(self) -> type[ScratchpadInput]:
166135
"""Input schema for the tool."""
167136
return ScratchpadInput
168137

169-
def _create_emitter(self) -> Emitter:
170-
"""Create emitter for the tool."""
171-
return Emitter()
138+
@property
139+
def emitter(self) -> Emitter:
140+
"""Emitter for the tool."""
141+
return Emitter.root.child(
142+
namespace=["tool", "scratchpad"],
143+
creator=self,
144+
)
172145

173146
def _get_entries(self, session_id: str) -> list[str]:
174147
"""Get scratchpad entries for a session.
@@ -381,19 +354,17 @@ async def _run(
381354
self,
382355
input: ScratchpadInput,
383356
options: ToolRunOptions | None = None,
384-
context: RunContext | None = None,
385357
) -> StringToolOutput:
386358
"""Execute scratchpad operation.
387359
388360
Args:
389361
input: ScratchpadInput model instance.
390362
options: Optional tool run options.
391-
context: Optional run context.
392363
393364
Returns:
394365
StringToolOutput with the result of the operation.
395366
"""
396-
session_id = self._get_session_id(context)
367+
session_id = self._get_session_id()
397368
operation = input.operation.lower().strip()
398369
content = input.content
399370

0 commit comments

Comments
 (0)