@aws/agentcore 0.8.2 → 0.10.0
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.
- package/dist/agent-inspector/favicon.svg +60 -0
- package/dist/agent-inspector/index.css +1 -0
- package/dist/agent-inspector/index.html +14 -0
- package/dist/agent-inspector/index.js +274 -0
- package/dist/assets/README.md +56 -31
- package/dist/assets/__tests__/__snapshots__/assets.snapshot.test.ts.snap +1669 -245
- package/dist/assets/__tests__/__snapshots__/dockerfile-render.test.ts.snap +77 -0
- package/dist/assets/__tests__/dockerfile-render.test.ts +24 -0
- package/dist/assets/agents/AGENTS.md +84 -55
- package/dist/assets/cdk/cdk.json +1 -1
- package/dist/assets/cdk/package.json +1 -1
- package/dist/assets/container/python/Dockerfile +4 -0
- package/dist/assets/evaluators/python-lambda/execution-role-policy.json +1 -1
- package/dist/assets/python/a2a/googleadk/base/main.py +65 -3
- package/dist/assets/python/a2a/langchain_langgraph/base/main.py +64 -1
- package/dist/assets/python/a2a/strands/base/main.py +65 -2
- package/dist/assets/python/agui/googleadk/base/README.md +30 -0
- package/dist/assets/python/agui/googleadk/base/gitignore.template +41 -0
- package/dist/assets/python/agui/googleadk/base/main.py +31 -0
- package/dist/assets/python/agui/googleadk/base/model/__init__.py +1 -0
- package/dist/assets/python/agui/googleadk/base/model/load.py +41 -0
- package/dist/assets/python/agui/googleadk/base/pyproject.toml +24 -0
- package/dist/assets/python/agui/langchain_langgraph/base/README.md +22 -0
- package/dist/assets/python/agui/langchain_langgraph/base/gitignore.template +41 -0
- package/dist/assets/python/agui/langchain_langgraph/base/main.py +74 -0
- package/dist/assets/python/agui/langchain_langgraph/base/model/__init__.py +1 -0
- package/dist/assets/python/agui/langchain_langgraph/base/model/load.py +123 -0
- package/dist/assets/python/agui/langchain_langgraph/base/pyproject.toml +30 -0
- package/dist/assets/python/agui/strands/base/README.md +22 -0
- package/dist/assets/python/agui/strands/base/gitignore.template +41 -0
- package/dist/assets/python/agui/strands/base/main.py +43 -0
- package/dist/assets/python/agui/strands/base/model/__init__.py +1 -0
- package/dist/assets/python/agui/strands/base/model/load.py +123 -0
- package/dist/assets/python/agui/strands/base/pyproject.toml +27 -0
- package/dist/assets/python/agui/strands/capabilities/memory/__init__.py +1 -0
- package/dist/assets/python/agui/strands/capabilities/memory/session.py +38 -0
- package/dist/assets/python/http/autogen/base/main.py +61 -1
- package/dist/assets/python/http/googleadk/base/main.py +62 -2
- package/dist/assets/python/http/langchain_langgraph/base/main.py +61 -1
- package/dist/assets/python/http/openaiagents/base/main.py +70 -4
- package/dist/assets/python/http/strands/base/main.py +64 -6
- package/dist/cli/index.mjs +480 -457
- package/dist/lib/errors/config.d.ts.map +1 -1
- package/dist/lib/errors/config.js +5 -2
- package/dist/lib/errors/config.js.map +1 -1
- package/dist/schema/constants.d.ts +1 -0
- package/dist/schema/constants.d.ts.map +1 -1
- package/dist/schema/constants.js +8 -1
- package/dist/schema/constants.js.map +1 -1
- package/dist/schema/schemas/agent-env.d.ts +20 -0
- package/dist/schema/schemas/agent-env.d.ts.map +1 -1
- package/dist/schema/schemas/agent-env.js +17 -2
- package/dist/schema/schemas/agent-env.js.map +1 -1
- package/dist/schema/schemas/agentcore-project.d.ts +6 -0
- package/dist/schema/schemas/agentcore-project.d.ts.map +1 -1
- package/dist/schema/schemas/aws-targets.d.ts +3 -0
- package/dist/schema/schemas/aws-targets.d.ts.map +1 -1
- package/dist/schema/schemas/aws-targets.js +1 -0
- package/dist/schema/schemas/aws-targets.js.map +1 -1
- package/dist/schema/schemas/primitives/evaluator.d.ts.map +1 -1
- package/dist/schema/schemas/primitives/evaluator.js +0 -1
- package/dist/schema/schemas/primitives/evaluator.js.map +1 -1
- package/package.json +4 -1
- package/scripts/bump-version.ts +7 -80
- package/scripts/copy-assets.mjs +14 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from bedrock_agentcore.memory.integrations.strands.config import AgentCoreMemoryConfig{{#if memoryProviders.[0].strategies.length}}, RetrievalConfig{{/if}}
|
|
5
|
+
from bedrock_agentcore.memory.integrations.strands.session_manager import AgentCoreMemorySessionManager
|
|
6
|
+
|
|
7
|
+
MEMORY_ID = os.getenv("{{memoryProviders.[0].envVarName}}")
|
|
8
|
+
REGION = os.getenv("AWS_REGION")
|
|
9
|
+
|
|
10
|
+
def get_memory_session_manager(session_id: str, actor_id: str) -> Optional[AgentCoreMemorySessionManager]:
|
|
11
|
+
if not MEMORY_ID:
|
|
12
|
+
return None
|
|
13
|
+
|
|
14
|
+
{{#if memoryProviders.[0].strategies.length}}
|
|
15
|
+
retrieval_config = {
|
|
16
|
+
{{#if (includes memoryProviders.[0].strategies "SEMANTIC")}}
|
|
17
|
+
f"/users/{actor_id}/facts": RetrievalConfig(top_k=3, relevance_score=0.5),
|
|
18
|
+
{{/if}}
|
|
19
|
+
{{#if (includes memoryProviders.[0].strategies "USER_PREFERENCE")}}
|
|
20
|
+
f"/users/{actor_id}/preferences": RetrievalConfig(top_k=3, relevance_score=0.5),
|
|
21
|
+
{{/if}}
|
|
22
|
+
{{#if (includes memoryProviders.[0].strategies "SUMMARIZATION")}}
|
|
23
|
+
f"/summaries/{actor_id}/{session_id}": RetrievalConfig(top_k=3, relevance_score=0.5),
|
|
24
|
+
{{/if}}
|
|
25
|
+
}
|
|
26
|
+
{{/if}}
|
|
27
|
+
|
|
28
|
+
return AgentCoreMemorySessionManager(
|
|
29
|
+
AgentCoreMemoryConfig(
|
|
30
|
+
memory_id=MEMORY_ID,
|
|
31
|
+
session_id=session_id,
|
|
32
|
+
actor_id=actor_id,
|
|
33
|
+
{{#if memoryProviders.[0].strategies.length}}
|
|
34
|
+
retrieval_config=retrieval_config,
|
|
35
|
+
{{/if}}
|
|
36
|
+
),
|
|
37
|
+
REGION
|
|
38
|
+
)
|
|
@@ -22,6 +22,66 @@ add_numbers_tool = FunctionTool(
|
|
|
22
22
|
# Define a collection of tools used by the model
|
|
23
23
|
tools = [add_numbers_tool]
|
|
24
24
|
|
|
25
|
+
{{#if sessionStorageMountPath}}
|
|
26
|
+
SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
|
|
27
|
+
|
|
28
|
+
def _safe_resolve(path: str) -> str:
|
|
29
|
+
"""Resolve path safely within the storage boundary."""
|
|
30
|
+
resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
|
|
31
|
+
if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
|
|
32
|
+
raise ValueError(f"Path '{path}' is outside the storage boundary")
|
|
33
|
+
return resolved
|
|
34
|
+
|
|
35
|
+
def file_read(path: str) -> str:
|
|
36
|
+
"""Read a file from persistent storage. The path is relative to the storage root."""
|
|
37
|
+
try:
|
|
38
|
+
full_path = _safe_resolve(path)
|
|
39
|
+
with open(full_path) as f:
|
|
40
|
+
return f.read()
|
|
41
|
+
except ValueError as e:
|
|
42
|
+
return str(e)
|
|
43
|
+
except OSError as e:
|
|
44
|
+
return f"Error reading '{path}': {e.strerror}"
|
|
45
|
+
|
|
46
|
+
def file_write(path: str, content: str) -> str:
|
|
47
|
+
"""Write content to a file in persistent storage. The path is relative to the storage root."""
|
|
48
|
+
try:
|
|
49
|
+
full_path = _safe_resolve(path)
|
|
50
|
+
parent = os.path.dirname(full_path)
|
|
51
|
+
if parent:
|
|
52
|
+
os.makedirs(parent, exist_ok=True)
|
|
53
|
+
with open(full_path, "w") as f:
|
|
54
|
+
f.write(content)
|
|
55
|
+
return f"Written to {path}"
|
|
56
|
+
except ValueError as e:
|
|
57
|
+
return str(e)
|
|
58
|
+
except OSError as e:
|
|
59
|
+
return f"Error writing '{path}': {e.strerror}"
|
|
60
|
+
|
|
61
|
+
def list_files(directory: str = "") -> str:
|
|
62
|
+
"""List files in persistent storage. The directory is relative to the storage root."""
|
|
63
|
+
try:
|
|
64
|
+
target = _safe_resolve(directory)
|
|
65
|
+
entries = os.listdir(target)
|
|
66
|
+
return "\n".join(entries) if entries else "(empty directory)"
|
|
67
|
+
except ValueError as e:
|
|
68
|
+
return str(e)
|
|
69
|
+
except OSError as e:
|
|
70
|
+
return f"Error listing '{directory}': {e.strerror}"
|
|
71
|
+
|
|
72
|
+
tools.extend([
|
|
73
|
+
FunctionTool(file_read, description="Read a file from persistent storage. The path is relative to the storage root."),
|
|
74
|
+
FunctionTool(file_write, description="Write content to a file in persistent storage. The path is relative to the storage root."),
|
|
75
|
+
FunctionTool(list_files, description="List files in persistent storage. The directory is relative to the storage root."),
|
|
76
|
+
])
|
|
77
|
+
{{/if}}
|
|
78
|
+
|
|
79
|
+
SYSTEM_MESSAGE = """
|
|
80
|
+
You are a helpful assistant. Use tools when appropriate.
|
|
81
|
+
{{#if sessionStorageMountPath}}
|
|
82
|
+
You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
|
|
83
|
+
{{/if}}
|
|
84
|
+
"""
|
|
25
85
|
|
|
26
86
|
@app.entrypoint
|
|
27
87
|
async def invoke(payload, context):
|
|
@@ -35,7 +95,7 @@ async def invoke(payload, context):
|
|
|
35
95
|
name="{{ name }}",
|
|
36
96
|
model_client=load_model(),
|
|
37
97
|
tools=tools + mcp_tools,
|
|
38
|
-
system_message=
|
|
98
|
+
system_message=SYSTEM_MESSAGE,
|
|
39
99
|
)
|
|
40
100
|
|
|
41
101
|
# Process the user prompt
|
|
@@ -26,6 +26,66 @@ def add_numbers(a: int, b: int) -> int:
|
|
|
26
26
|
return a + b
|
|
27
27
|
|
|
28
28
|
|
|
29
|
+
# Define a collection of tools used by the model
|
|
30
|
+
tools = [add_numbers]
|
|
31
|
+
|
|
32
|
+
{{#if sessionStorageMountPath}}
|
|
33
|
+
SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
|
|
34
|
+
|
|
35
|
+
def _safe_resolve(path: str) -> str:
|
|
36
|
+
"""Resolve path safely within the storage boundary."""
|
|
37
|
+
resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
|
|
38
|
+
if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
|
|
39
|
+
raise ValueError(f"Path '{path}' is outside the storage boundary")
|
|
40
|
+
return resolved
|
|
41
|
+
|
|
42
|
+
def file_read(path: str) -> str:
|
|
43
|
+
"""Read a file from persistent storage. The path is relative to the storage root."""
|
|
44
|
+
try:
|
|
45
|
+
full_path = _safe_resolve(path)
|
|
46
|
+
with open(full_path) as f:
|
|
47
|
+
return f.read()
|
|
48
|
+
except ValueError as e:
|
|
49
|
+
return str(e)
|
|
50
|
+
except OSError as e:
|
|
51
|
+
return f"Error reading '{path}': {e.strerror}"
|
|
52
|
+
|
|
53
|
+
def file_write(path: str, content: str) -> str:
|
|
54
|
+
"""Write content to a file in persistent storage. The path is relative to the storage root."""
|
|
55
|
+
try:
|
|
56
|
+
full_path = _safe_resolve(path)
|
|
57
|
+
parent = os.path.dirname(full_path)
|
|
58
|
+
if parent:
|
|
59
|
+
os.makedirs(parent, exist_ok=True)
|
|
60
|
+
with open(full_path, "w") as f:
|
|
61
|
+
f.write(content)
|
|
62
|
+
return f"Written to {path}"
|
|
63
|
+
except ValueError as e:
|
|
64
|
+
return str(e)
|
|
65
|
+
except OSError as e:
|
|
66
|
+
return f"Error writing '{path}': {e.strerror}"
|
|
67
|
+
|
|
68
|
+
def list_files(directory: str = "") -> str:
|
|
69
|
+
"""List files in persistent storage. The directory is relative to the storage root."""
|
|
70
|
+
try:
|
|
71
|
+
target = _safe_resolve(directory)
|
|
72
|
+
entries = os.listdir(target)
|
|
73
|
+
return "\n".join(entries) if entries else "(empty directory)"
|
|
74
|
+
except ValueError as e:
|
|
75
|
+
return str(e)
|
|
76
|
+
except OSError as e:
|
|
77
|
+
return f"Error listing '{directory}': {e.strerror}"
|
|
78
|
+
|
|
79
|
+
tools.extend([file_read, file_write, list_files])
|
|
80
|
+
{{/if}}
|
|
81
|
+
|
|
82
|
+
AGENT_INSTRUCTION = """
|
|
83
|
+
I can answer your questions using the knowledge I have!
|
|
84
|
+
{{#if sessionStorageMountPath}}
|
|
85
|
+
You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
|
|
86
|
+
{{/if}}
|
|
87
|
+
"""
|
|
88
|
+
|
|
29
89
|
# Get MCP Toolset
|
|
30
90
|
{{#if hasGateway}}
|
|
31
91
|
mcp_toolset = get_all_gateway_mcp_toolsets()
|
|
@@ -48,8 +108,8 @@ agent = Agent(
|
|
|
48
108
|
model=MODEL_ID,
|
|
49
109
|
name="{{ name }}",
|
|
50
110
|
description="Agent to answer questions",
|
|
51
|
-
instruction=
|
|
52
|
-
tools=mcp_toolset +
|
|
111
|
+
instruction=AGENT_INSTRUCTION,
|
|
112
|
+
tools=mcp_toolset + tools,
|
|
53
113
|
)
|
|
54
114
|
|
|
55
115
|
|
|
@@ -35,6 +35,66 @@ def add_numbers(a: int, b: int) -> int:
|
|
|
35
35
|
# Define a collection of tools used by the model
|
|
36
36
|
tools = [add_numbers]
|
|
37
37
|
|
|
38
|
+
{{#if sessionStorageMountPath}}
|
|
39
|
+
SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
|
|
40
|
+
|
|
41
|
+
def _safe_resolve(path: str) -> str:
|
|
42
|
+
"""Resolve path safely within the storage boundary."""
|
|
43
|
+
resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
|
|
44
|
+
if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
|
|
45
|
+
raise ValueError(f"Path '{path}' is outside the storage boundary")
|
|
46
|
+
return resolved
|
|
47
|
+
|
|
48
|
+
@tool
|
|
49
|
+
def file_read(path: str) -> str:
|
|
50
|
+
"""Read a file from persistent storage. The path is relative to the storage root."""
|
|
51
|
+
try:
|
|
52
|
+
full_path = _safe_resolve(path)
|
|
53
|
+
with open(full_path) as f:
|
|
54
|
+
return f.read()
|
|
55
|
+
except ValueError as e:
|
|
56
|
+
return str(e)
|
|
57
|
+
except OSError as e:
|
|
58
|
+
return f"Error reading '{path}': {e.strerror}"
|
|
59
|
+
|
|
60
|
+
@tool
|
|
61
|
+
def file_write(path: str, content: str) -> str:
|
|
62
|
+
"""Write content to a file in persistent storage. The path is relative to the storage root."""
|
|
63
|
+
try:
|
|
64
|
+
full_path = _safe_resolve(path)
|
|
65
|
+
parent = os.path.dirname(full_path)
|
|
66
|
+
if parent:
|
|
67
|
+
os.makedirs(parent, exist_ok=True)
|
|
68
|
+
with open(full_path, "w") as f:
|
|
69
|
+
f.write(content)
|
|
70
|
+
return f"Written to {path}"
|
|
71
|
+
except ValueError as e:
|
|
72
|
+
return str(e)
|
|
73
|
+
except OSError as e:
|
|
74
|
+
return f"Error writing '{path}': {e.strerror}"
|
|
75
|
+
|
|
76
|
+
@tool
|
|
77
|
+
def list_files(directory: str = "") -> str:
|
|
78
|
+
"""List files in persistent storage. The directory is relative to the storage root."""
|
|
79
|
+
try:
|
|
80
|
+
target = _safe_resolve(directory)
|
|
81
|
+
entries = os.listdir(target)
|
|
82
|
+
return "\n".join(entries) if entries else "(empty directory)"
|
|
83
|
+
except ValueError as e:
|
|
84
|
+
return str(e)
|
|
85
|
+
except OSError as e:
|
|
86
|
+
return f"Error listing '{directory}': {e.strerror}"
|
|
87
|
+
|
|
88
|
+
tools.extend([file_read, file_write, list_files])
|
|
89
|
+
{{/if}}
|
|
90
|
+
|
|
91
|
+
SYSTEM_PROMPT = """
|
|
92
|
+
You are a helpful assistant. Use tools when appropriate.
|
|
93
|
+
{{#if sessionStorageMountPath}}
|
|
94
|
+
You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
|
|
95
|
+
{{/if}}
|
|
96
|
+
"""
|
|
97
|
+
|
|
38
98
|
|
|
39
99
|
@app.entrypoint
|
|
40
100
|
async def invoke(payload, context):
|
|
@@ -53,7 +113,7 @@ async def invoke(payload, context):
|
|
|
53
113
|
mcp_tools = await mcp_client.get_tools()
|
|
54
114
|
|
|
55
115
|
# Define the agent using create_react_agent
|
|
56
|
-
graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools)
|
|
116
|
+
graph = create_react_agent(get_or_create_model(), tools=mcp_tools + tools, prompt=SYSTEM_PROMPT)
|
|
57
117
|
|
|
58
118
|
# Process the user prompt
|
|
59
119
|
prompt = payload.get("prompt", "What can you help me with?")
|
|
@@ -35,6 +35,68 @@ def add_numbers(a: int, b: int) -> int:
|
|
|
35
35
|
return a + b
|
|
36
36
|
|
|
37
37
|
|
|
38
|
+
tools = [add_numbers]
|
|
39
|
+
|
|
40
|
+
{{#if sessionStorageMountPath}}
|
|
41
|
+
SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
|
|
42
|
+
|
|
43
|
+
def _safe_resolve(path: str) -> str:
|
|
44
|
+
"""Resolve path safely within the storage boundary."""
|
|
45
|
+
resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
|
|
46
|
+
if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
|
|
47
|
+
raise ValueError(f"Path '{path}' is outside the storage boundary")
|
|
48
|
+
return resolved
|
|
49
|
+
|
|
50
|
+
@function_tool
|
|
51
|
+
def file_read(path: str) -> str:
|
|
52
|
+
"""Read a file from persistent storage. The path is relative to the storage root."""
|
|
53
|
+
try:
|
|
54
|
+
full_path = _safe_resolve(path)
|
|
55
|
+
with open(full_path) as f:
|
|
56
|
+
return f.read()
|
|
57
|
+
except ValueError as e:
|
|
58
|
+
return str(e)
|
|
59
|
+
except OSError as e:
|
|
60
|
+
return f"Error reading '{path}': {e.strerror}"
|
|
61
|
+
|
|
62
|
+
@function_tool
|
|
63
|
+
def file_write(path: str, content: str) -> str:
|
|
64
|
+
"""Write content to a file in persistent storage. The path is relative to the storage root."""
|
|
65
|
+
try:
|
|
66
|
+
full_path = _safe_resolve(path)
|
|
67
|
+
parent = os.path.dirname(full_path)
|
|
68
|
+
if parent:
|
|
69
|
+
os.makedirs(parent, exist_ok=True)
|
|
70
|
+
with open(full_path, "w") as f:
|
|
71
|
+
f.write(content)
|
|
72
|
+
return f"Written to {path}"
|
|
73
|
+
except ValueError as e:
|
|
74
|
+
return str(e)
|
|
75
|
+
except OSError as e:
|
|
76
|
+
return f"Error writing '{path}': {e.strerror}"
|
|
77
|
+
|
|
78
|
+
@function_tool
|
|
79
|
+
def list_files(directory: str = "") -> str:
|
|
80
|
+
"""List files in persistent storage. The directory is relative to the storage root."""
|
|
81
|
+
try:
|
|
82
|
+
target = _safe_resolve(directory)
|
|
83
|
+
entries = os.listdir(target)
|
|
84
|
+
return "\n".join(entries) if entries else "(empty directory)"
|
|
85
|
+
except ValueError as e:
|
|
86
|
+
return str(e)
|
|
87
|
+
except OSError as e:
|
|
88
|
+
return f"Error listing '{directory}': {e.strerror}"
|
|
89
|
+
|
|
90
|
+
tools.extend([file_read, file_write, list_files])
|
|
91
|
+
{{/if}}
|
|
92
|
+
|
|
93
|
+
INSTRUCTIONS = """
|
|
94
|
+
You are a helpful assistant. Use tools when appropriate.
|
|
95
|
+
{{#if sessionStorageMountPath}}
|
|
96
|
+
You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
|
|
97
|
+
{{/if}}
|
|
98
|
+
"""
|
|
99
|
+
|
|
38
100
|
# Define the agent execution
|
|
39
101
|
async def main(query):
|
|
40
102
|
ensure_credentials_loaded()
|
|
@@ -44,8 +106,9 @@ async def main(query):
|
|
|
44
106
|
agent = Agent(
|
|
45
107
|
name="{{ name }}",
|
|
46
108
|
model="gpt-4.1",
|
|
109
|
+
instructions=INSTRUCTIONS,
|
|
47
110
|
mcp_servers=mcp_servers,
|
|
48
|
-
tools=
|
|
111
|
+
tools=tools
|
|
49
112
|
)
|
|
50
113
|
result = await Runner.run(agent, query)
|
|
51
114
|
return result
|
|
@@ -53,8 +116,9 @@ async def main(query):
|
|
|
53
116
|
agent = Agent(
|
|
54
117
|
name="{{ name }}",
|
|
55
118
|
model="gpt-4.1",
|
|
119
|
+
instructions=INSTRUCTIONS,
|
|
56
120
|
mcp_servers=[],
|
|
57
|
-
tools=
|
|
121
|
+
tools=tools
|
|
58
122
|
)
|
|
59
123
|
result = await Runner.run(agent, query)
|
|
60
124
|
return result
|
|
@@ -65,8 +129,9 @@ async def main(query):
|
|
|
65
129
|
agent = Agent(
|
|
66
130
|
name="{{ name }}",
|
|
67
131
|
model="gpt-4.1",
|
|
132
|
+
instructions=INSTRUCTIONS,
|
|
68
133
|
mcp_servers=active_servers,
|
|
69
|
-
tools=
|
|
134
|
+
tools=tools
|
|
70
135
|
)
|
|
71
136
|
result = await Runner.run(agent, query)
|
|
72
137
|
return result
|
|
@@ -74,8 +139,9 @@ async def main(query):
|
|
|
74
139
|
agent = Agent(
|
|
75
140
|
name="{{ name }}",
|
|
76
141
|
model="gpt-4.1",
|
|
142
|
+
instructions=INSTRUCTIONS,
|
|
77
143
|
mcp_servers=[],
|
|
78
|
-
tools=
|
|
144
|
+
tools=tools
|
|
79
145
|
)
|
|
80
146
|
result = await Runner.run(agent, query)
|
|
81
147
|
return result
|
|
@@ -9,6 +9,9 @@ from mcp_client.client import get_streamable_http_mcp_client
|
|
|
9
9
|
{{#if hasMemory}}
|
|
10
10
|
from memory.session import get_memory_session_manager
|
|
11
11
|
{{/if}}
|
|
12
|
+
{{#if sessionStorageMountPath}}
|
|
13
|
+
import os
|
|
14
|
+
{{/if}}
|
|
12
15
|
|
|
13
16
|
app = BedrockAgentCoreApp()
|
|
14
17
|
log = app.logger
|
|
@@ -30,11 +33,70 @@ def add_numbers(a: int, b: int) -> int:
|
|
|
30
33
|
return a+b
|
|
31
34
|
tools.append(add_numbers)
|
|
32
35
|
|
|
36
|
+
{{#if sessionStorageMountPath}}
|
|
37
|
+
SESSION_STORAGE_PATH = "{{sessionStorageMountPath}}"
|
|
38
|
+
|
|
39
|
+
def _safe_resolve(path: str) -> str:
|
|
40
|
+
"""Resolve path safely within the storage boundary."""
|
|
41
|
+
resolved = os.path.realpath(os.path.join(SESSION_STORAGE_PATH, path.lstrip("/")))
|
|
42
|
+
if not resolved.startswith(os.path.realpath(SESSION_STORAGE_PATH)):
|
|
43
|
+
raise ValueError(f"Path '{path}' is outside the storage boundary")
|
|
44
|
+
return resolved
|
|
45
|
+
|
|
46
|
+
@tool
|
|
47
|
+
def file_read(path: str) -> str:
|
|
48
|
+
"""Read a file from persistent storage. The path is relative to the storage root."""
|
|
49
|
+
try:
|
|
50
|
+
full_path = _safe_resolve(path)
|
|
51
|
+
with open(full_path) as f:
|
|
52
|
+
return f.read()
|
|
53
|
+
except ValueError as e:
|
|
54
|
+
return str(e)
|
|
55
|
+
except OSError as e:
|
|
56
|
+
return f"Error reading '{path}': {e.strerror}"
|
|
57
|
+
|
|
58
|
+
@tool
|
|
59
|
+
def file_write(path: str, content: str) -> str:
|
|
60
|
+
"""Write content to a file in persistent storage. The path is relative to the storage root."""
|
|
61
|
+
try:
|
|
62
|
+
full_path = _safe_resolve(path)
|
|
63
|
+
parent = os.path.dirname(full_path)
|
|
64
|
+
if parent:
|
|
65
|
+
os.makedirs(parent, exist_ok=True)
|
|
66
|
+
with open(full_path, "w") as f:
|
|
67
|
+
f.write(content)
|
|
68
|
+
return f"Written to {path}"
|
|
69
|
+
except ValueError as e:
|
|
70
|
+
return str(e)
|
|
71
|
+
except OSError as e:
|
|
72
|
+
return f"Error writing '{path}': {e.strerror}"
|
|
73
|
+
|
|
74
|
+
@tool
|
|
75
|
+
def list_files(directory: str = "") -> str:
|
|
76
|
+
"""List files in persistent storage. The directory is relative to the storage root."""
|
|
77
|
+
try:
|
|
78
|
+
target = _safe_resolve(directory)
|
|
79
|
+
entries = os.listdir(target)
|
|
80
|
+
return "\n".join(entries) if entries else "(empty directory)"
|
|
81
|
+
except ValueError as e:
|
|
82
|
+
return str(e)
|
|
83
|
+
except OSError as e:
|
|
84
|
+
return f"Error listing '{directory}': {e.strerror}"
|
|
85
|
+
|
|
86
|
+
tools.extend([file_read, file_write, list_files])
|
|
87
|
+
{{/if}}
|
|
88
|
+
|
|
33
89
|
# Add MCP client to tools if available
|
|
34
90
|
for mcp_client in mcp_clients:
|
|
35
91
|
if mcp_client:
|
|
36
92
|
tools.append(mcp_client)
|
|
37
93
|
|
|
94
|
+
SYSTEM_PROMPT = """
|
|
95
|
+
You are a helpful assistant. Use tools when appropriate.
|
|
96
|
+
{{#if sessionStorageMountPath}}
|
|
97
|
+
You have persistent storage at {{sessionStorageMountPath}}. Use file tools to read and write files. Data persists across sessions.
|
|
98
|
+
{{/if}}
|
|
99
|
+
"""
|
|
38
100
|
|
|
39
101
|
{{#if hasMemory}}
|
|
40
102
|
def agent_factory():
|
|
@@ -46,9 +108,7 @@ def agent_factory():
|
|
|
46
108
|
cache[key] = Agent(
|
|
47
109
|
model=load_model(),
|
|
48
110
|
session_manager=get_memory_session_manager(session_id, user_id),
|
|
49
|
-
system_prompt=
|
|
50
|
-
You are a helpful assistant. Use tools when appropriate.
|
|
51
|
-
""",
|
|
111
|
+
system_prompt=SYSTEM_PROMPT,
|
|
52
112
|
tools=tools
|
|
53
113
|
)
|
|
54
114
|
return cache[key]
|
|
@@ -62,9 +122,7 @@ def get_or_create_agent():
|
|
|
62
122
|
if _agent is None:
|
|
63
123
|
_agent = Agent(
|
|
64
124
|
model=load_model(),
|
|
65
|
-
system_prompt=
|
|
66
|
-
You are a helpful assistant. Use tools when appropriate.
|
|
67
|
-
""",
|
|
125
|
+
system_prompt=SYSTEM_PROMPT,
|
|
68
126
|
tools=tools
|
|
69
127
|
)
|
|
70
128
|
return _agent
|