Repository navigation
Expand file tree
/
Copy pathnote.py
More file actions
73 lines (63 loc) · 2.31 KB
/
Copy pathnote.py
File metadata and controls
73 lines (63 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""In-memory scratchpad note tool — proves multi-tool stateful use."""
from __future__ import annotations
from typing import Any
from ai_agent.domain.tool import BaseTool, ToolParameter, ToolResult
class NoteTool(BaseTool):
"""
Simple session scratchpad shared across tool calls.
Useful for demos: write a note, then read it back in a later turn.
"""
def __init__(self) -> None:
super().__init__(
name="note",
description=(
"Store or retrieve a short note in an in-memory scratchpad. "
"action=write requires text; action=read returns the stored note."
),
parameters=[
ToolParameter(
name="action",
type="string",
description="Either 'write' or 'read'",
required=True,
),
ToolParameter(
name="text",
type="string",
description="Note content when action=write",
required=False,
),
],
)
self._note: str | None = None
async def execute(self, arguments: dict[str, Any]) -> ToolResult:
action = arguments.get("action")
if action not in {"write", "read"}:
return ToolResult(
tool_name=self.name,
success=False,
output="",
error="action must be 'write' or 'read'",
)
if action == "write":
text = arguments.get("text")
if not isinstance(text, str) or not text.strip():
return ToolResult(
tool_name=self.name,
success=False,
output="",
error="text is required when action=write",
)
self._note = text.strip()
return ToolResult(
tool_name=self.name,
success=True,
output=f"Saved note ({len(self._note)} chars).",
)
if self._note is None:
return ToolResult(
tool_name=self.name,
success=True,
output="(empty)",
)
return ToolResult(tool_name=self.name, success=True, output=self._note)