agent-dreamer-sdk 0.0.3__tar.gz

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.
@@ -0,0 +1,13 @@
1
+ bin/
2
+ dist/
3
+ .env
4
+ .DS_Store
5
+ coverage.out
6
+ __pycache__/
7
+ *.py[cod]
8
+
9
+ .idea/
10
+ progress.md
11
+ findings.md
12
+ sdk/*.dist
13
+ sdk/typescript/node_modules
@@ -0,0 +1,260 @@
1
+ Metadata-Version: 2.4
2
+ Name: agent-dreamer-sdk
3
+ Version: 0.0.3
4
+ Summary: Agent Memory HTTP SDK and lightweight Agent adapter
5
+ Author: Agent Memory Contributors
6
+ License: MIT
7
+ Keywords: agent,memory,sdk
8
+ Requires-Python: >=3.10
9
+ Provides-Extra: dev
10
+ Requires-Dist: pytest>=8.0; extra == 'dev'
11
+ Requires-Dist: ruff>=0.5; extra == 'dev'
12
+ Description-Content-Type: text/markdown
13
+
14
+ # agent-memory-sdk
15
+
16
+ Agent Memory Python SDK 用于让 Python Agent 接入外部记忆服务。当前版本提供同步 Client 和轻量 `AgentMemorySession` 接入层。
17
+
18
+ ## 安装
19
+
20
+ ```bash
21
+ python -m pip install agent-memory-sdk
22
+ ```
23
+
24
+ 本地开发时可以从源码安装:
25
+
26
+ ```bash
27
+ cd sdk/python
28
+ python -m pip install -e .
29
+ ```
30
+
31
+ ## 创建 Client
32
+
33
+ ```python
34
+ import os
35
+
36
+ from agent_memory import AgentMemoryClient
37
+
38
+ client = AgentMemoryClient(
39
+ base_url=os.environ["AGENT_MEMORY_BASE_URL"],
40
+ api_key=os.environ["AGENT_MEMORY_API_KEY"],
41
+ )
42
+ ```
43
+
44
+ 常用环境变量:
45
+
46
+ ```bash
47
+ export AGENT_MEMORY_BASE_URL='http://localhost:8080'
48
+ export AGENT_MEMORY_API_KEY='<workspace api key>'
49
+ export AGENT_MEMORY_AGENT_INSTANCE_ID='<agent_instance_id>'
50
+ ```
51
+
52
+ ## 创建或复用 Session
53
+
54
+ 每个 Agent 会话开始时先创建或复用 session。`external_session_id` 是调用方自己的会话 ID,同一个 Agent 下重复传入会复用同一个服务端 session。
55
+
56
+ ```python
57
+ import os
58
+
59
+ from agent_memory.types import CreateSessionReq
60
+
61
+ session = client.create_session(
62
+ CreateSessionReq(
63
+ agent_instance_id=os.environ["AGENT_MEMORY_AGENT_INSTANCE_ID"],
64
+ external_session_id="python-task-20260616-001",
65
+ metadata={"source": "python-agent"},
66
+ )
67
+ )
68
+
69
+ print("session_id:", session.session_id)
70
+ ```
71
+
72
+ ## 写入 Memory Event
73
+
74
+ Agent 每轮推理和工具执行过程都应该写入 memory event。事件是记忆生成的事实来源。
75
+
76
+ ```python
77
+ from agent_memory.types import RecordEventReq
78
+
79
+ event = client.record_event(
80
+ RecordEventReq(
81
+ session_id=session.session_id,
82
+ external_thread_id="main",
83
+ event_id="evt-user-001",
84
+ event_type="message",
85
+ actor="user",
86
+ status="success",
87
+ summary="用户要求继续 SDK 接入任务。",
88
+ payload={"text": "请继续 SDK 接入任务"},
89
+ )
90
+ )
91
+
92
+ print("thread_id:", event.thread_id)
93
+ ```
94
+
95
+ 常用事件类型:
96
+
97
+ | event_type | 适合场景 |
98
+ | --- | --- |
99
+ | `message` | 用户消息、Agent 回复 |
100
+ | `tool_call` | 调用工具前记录工具名和参数摘要 |
101
+ | `tool_result` | 工具执行完成后记录结果、状态、错误码 |
102
+ | `planner_update` | 计划、待办、阶段性决策变化 |
103
+ | `task_state` | 任务开始、暂停、完成、失败、阻塞 |
104
+ | `artifact_change` | 文件、代码、文档、报告等产物变化 |
105
+ | `correction_signal` | 用户纠错或要求修正此前信息 |
106
+
107
+ 工具失败事件建议填写 `status`、`error_code` 和 `retryable`。
108
+
109
+ ```python
110
+ client.record_event(
111
+ RecordEventReq(
112
+ session_id=session.session_id,
113
+ event_id="evt-tool-result-001",
114
+ event_type="tool_result",
115
+ actor="tool",
116
+ status="error",
117
+ tool_name="pytest",
118
+ error_code="TEST_FAILED",
119
+ retryable=True,
120
+ summary="pytest 执行失败。",
121
+ payload={"command": "pytest", "exit_code": 1},
122
+ )
123
+ )
124
+ ```
125
+
126
+ ## 查询记忆
127
+
128
+ 每轮 Agent 推理前建议调用一次记忆查询,获取少量可注入上下文。
129
+
130
+ ```python
131
+ from agent_memory.adapter import format_memory_context
132
+ from agent_memory.types import MemoryQueryReq
133
+
134
+ response = client.query_memories(
135
+ MemoryQueryReq(
136
+ session_id=session.session_id,
137
+ thread_id=event.thread_id,
138
+ query_text="继续 SDK 接入任务,需要恢复当前进展和注意事项",
139
+ query_intent="execution_help",
140
+ desired_memory_types=[
141
+ "task_whiteboard",
142
+ "tool_digest",
143
+ "failure_digest",
144
+ "procedure",
145
+ ],
146
+ limit=8,
147
+ event_limit=3,
148
+ )
149
+ )
150
+
151
+ prompt_context = format_memory_context(response)
152
+ print(prompt_context)
153
+ ```
154
+
155
+ 常用查询意图:
156
+
157
+ | query_intent | 适合场景 |
158
+ | --- | --- |
159
+ | `execution_help` | 继续任务、写代码、调用工具、排错 |
160
+ | `policy_lookup` | 查询规则、约束、团队规范 |
161
+ | `preference_lookup` | 查询用户偏好、输出风格 |
162
+ | `factual_lookup` | 查询稳定事实、项目背景 |
163
+ | `broad_context` | 不确定意图时综合召回 |
164
+
165
+ ## 触发短记忆生成
166
+
167
+ 短记忆用于整理当前 session/thread 的近期上下文,例如任务白板、工具结果摘要、失败摘要。
168
+
169
+ 适合触发时机:
170
+
171
+ - 用户消息写入后,尤其是用户给出新目标或新约束。
172
+ - 工具调用或工具结果写入后。
173
+ - 一个阶段性步骤完成后。
174
+ - 出现错误、超时、取消、用户纠错后。
175
+
176
+ ```python
177
+ from agent_memory.types import GenerateMemoryReq
178
+
179
+ run = client.generate_short_memory(
180
+ GenerateMemoryReq(
181
+ session_id=session.session_id,
182
+ thread_id=event.thread_id,
183
+ trigger_source="thread_end",
184
+ hints={"reason": "阶段完成后刷新任务白板"},
185
+ )
186
+ )
187
+
188
+ print("short memory queued:", run.run_ids)
189
+ ```
190
+
191
+ `status=queued` 表示任务已入队。需要 worker 消费完成后,新的短记忆才会出现在查询结果中。
192
+
193
+ ## 触发长记忆整理
194
+
195
+ 长记忆用于沉淀跨会话可复用的信息,例如事实、偏好、流程和规则。
196
+
197
+ 适合触发时机:
198
+
199
+ - 会话结束时。
200
+ - 一个完整任务完成后。
201
+ - 用户明确表达稳定偏好时。
202
+ - 形成可复用流程、项目事实、团队规则时。
203
+ - 不建议对临时、未经确认、明显只对当前轮有效的信息触发长期整理。
204
+
205
+ ```python
206
+ run = client.consolidate_long_memory(
207
+ GenerateMemoryReq(
208
+ session_id=session.session_id,
209
+ trigger_source="session_end",
210
+ hints={"reason": "会话结束后沉淀长期记忆"},
211
+ )
212
+ )
213
+
214
+ print("long memory queued:", run.run_ids)
215
+ ```
216
+
217
+ ## 使用 AgentMemorySession 简化接入
218
+
219
+ `AgentMemorySession` 会自动创建 session、写标准事件,并缓存服务端返回的 `thread_id`,后续查询和生成会自动带上线程上下文。
220
+
221
+ ```python
222
+ import os
223
+
224
+ from agent_memory import AgentMemoryClient, AgentMemorySession
225
+
226
+ client = AgentMemoryClient(
227
+ base_url=os.environ["AGENT_MEMORY_BASE_URL"],
228
+ api_key=os.environ["AGENT_MEMORY_API_KEY"],
229
+ )
230
+
231
+ agent_memory = AgentMemorySession(
232
+ client,
233
+ agent_instance_id=os.environ["AGENT_MEMORY_AGENT_INSTANCE_ID"],
234
+ external_session_id="python-task-20260616-001",
235
+ external_thread_id="main",
236
+ )
237
+
238
+ agent_memory.record_user_message("请继续 SDK 接入任务。")
239
+
240
+ short_run = agent_memory.generate_short_memory("thread_end")
241
+ print("short memory queued:", short_run.run_ids)
242
+
243
+ memory_context = agent_memory.query_context(
244
+ query_text="继续 SDK 接入任务",
245
+ query_intent="execution_help",
246
+ desired_memory_types=["task_whiteboard", "tool_digest", "procedure"],
247
+ limit=6,
248
+ event_limit=3,
249
+ )
250
+
251
+ print(memory_context.text)
252
+ ```
253
+
254
+ ## 查看示例
255
+
256
+ ```bash
257
+ cd sdk/python
258
+ python examples/basic.py
259
+ python examples/agent_loop.py
260
+ ```
@@ -0,0 +1,247 @@
1
+ # agent-memory-sdk
2
+
3
+ Agent Memory Python SDK 用于让 Python Agent 接入外部记忆服务。当前版本提供同步 Client 和轻量 `AgentMemorySession` 接入层。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ python -m pip install agent-memory-sdk
9
+ ```
10
+
11
+ 本地开发时可以从源码安装:
12
+
13
+ ```bash
14
+ cd sdk/python
15
+ python -m pip install -e .
16
+ ```
17
+
18
+ ## 创建 Client
19
+
20
+ ```python
21
+ import os
22
+
23
+ from agent_memory import AgentMemoryClient
24
+
25
+ client = AgentMemoryClient(
26
+ base_url=os.environ["AGENT_MEMORY_BASE_URL"],
27
+ api_key=os.environ["AGENT_MEMORY_API_KEY"],
28
+ )
29
+ ```
30
+
31
+ 常用环境变量:
32
+
33
+ ```bash
34
+ export AGENT_MEMORY_BASE_URL='http://localhost:8080'
35
+ export AGENT_MEMORY_API_KEY='<workspace api key>'
36
+ export AGENT_MEMORY_AGENT_INSTANCE_ID='<agent_instance_id>'
37
+ ```
38
+
39
+ ## 创建或复用 Session
40
+
41
+ 每个 Agent 会话开始时先创建或复用 session。`external_session_id` 是调用方自己的会话 ID,同一个 Agent 下重复传入会复用同一个服务端 session。
42
+
43
+ ```python
44
+ import os
45
+
46
+ from agent_memory.types import CreateSessionReq
47
+
48
+ session = client.create_session(
49
+ CreateSessionReq(
50
+ agent_instance_id=os.environ["AGENT_MEMORY_AGENT_INSTANCE_ID"],
51
+ external_session_id="python-task-20260616-001",
52
+ metadata={"source": "python-agent"},
53
+ )
54
+ )
55
+
56
+ print("session_id:", session.session_id)
57
+ ```
58
+
59
+ ## 写入 Memory Event
60
+
61
+ Agent 每轮推理和工具执行过程都应该写入 memory event。事件是记忆生成的事实来源。
62
+
63
+ ```python
64
+ from agent_memory.types import RecordEventReq
65
+
66
+ event = client.record_event(
67
+ RecordEventReq(
68
+ session_id=session.session_id,
69
+ external_thread_id="main",
70
+ event_id="evt-user-001",
71
+ event_type="message",
72
+ actor="user",
73
+ status="success",
74
+ summary="用户要求继续 SDK 接入任务。",
75
+ payload={"text": "请继续 SDK 接入任务"},
76
+ )
77
+ )
78
+
79
+ print("thread_id:", event.thread_id)
80
+ ```
81
+
82
+ 常用事件类型:
83
+
84
+ | event_type | 适合场景 |
85
+ | --- | --- |
86
+ | `message` | 用户消息、Agent 回复 |
87
+ | `tool_call` | 调用工具前记录工具名和参数摘要 |
88
+ | `tool_result` | 工具执行完成后记录结果、状态、错误码 |
89
+ | `planner_update` | 计划、待办、阶段性决策变化 |
90
+ | `task_state` | 任务开始、暂停、完成、失败、阻塞 |
91
+ | `artifact_change` | 文件、代码、文档、报告等产物变化 |
92
+ | `correction_signal` | 用户纠错或要求修正此前信息 |
93
+
94
+ 工具失败事件建议填写 `status`、`error_code` 和 `retryable`。
95
+
96
+ ```python
97
+ client.record_event(
98
+ RecordEventReq(
99
+ session_id=session.session_id,
100
+ event_id="evt-tool-result-001",
101
+ event_type="tool_result",
102
+ actor="tool",
103
+ status="error",
104
+ tool_name="pytest",
105
+ error_code="TEST_FAILED",
106
+ retryable=True,
107
+ summary="pytest 执行失败。",
108
+ payload={"command": "pytest", "exit_code": 1},
109
+ )
110
+ )
111
+ ```
112
+
113
+ ## 查询记忆
114
+
115
+ 每轮 Agent 推理前建议调用一次记忆查询,获取少量可注入上下文。
116
+
117
+ ```python
118
+ from agent_memory.adapter import format_memory_context
119
+ from agent_memory.types import MemoryQueryReq
120
+
121
+ response = client.query_memories(
122
+ MemoryQueryReq(
123
+ session_id=session.session_id,
124
+ thread_id=event.thread_id,
125
+ query_text="继续 SDK 接入任务,需要恢复当前进展和注意事项",
126
+ query_intent="execution_help",
127
+ desired_memory_types=[
128
+ "task_whiteboard",
129
+ "tool_digest",
130
+ "failure_digest",
131
+ "procedure",
132
+ ],
133
+ limit=8,
134
+ event_limit=3,
135
+ )
136
+ )
137
+
138
+ prompt_context = format_memory_context(response)
139
+ print(prompt_context)
140
+ ```
141
+
142
+ 常用查询意图:
143
+
144
+ | query_intent | 适合场景 |
145
+ | --- | --- |
146
+ | `execution_help` | 继续任务、写代码、调用工具、排错 |
147
+ | `policy_lookup` | 查询规则、约束、团队规范 |
148
+ | `preference_lookup` | 查询用户偏好、输出风格 |
149
+ | `factual_lookup` | 查询稳定事实、项目背景 |
150
+ | `broad_context` | 不确定意图时综合召回 |
151
+
152
+ ## 触发短记忆生成
153
+
154
+ 短记忆用于整理当前 session/thread 的近期上下文,例如任务白板、工具结果摘要、失败摘要。
155
+
156
+ 适合触发时机:
157
+
158
+ - 用户消息写入后,尤其是用户给出新目标或新约束。
159
+ - 工具调用或工具结果写入后。
160
+ - 一个阶段性步骤完成后。
161
+ - 出现错误、超时、取消、用户纠错后。
162
+
163
+ ```python
164
+ from agent_memory.types import GenerateMemoryReq
165
+
166
+ run = client.generate_short_memory(
167
+ GenerateMemoryReq(
168
+ session_id=session.session_id,
169
+ thread_id=event.thread_id,
170
+ trigger_source="thread_end",
171
+ hints={"reason": "阶段完成后刷新任务白板"},
172
+ )
173
+ )
174
+
175
+ print("short memory queued:", run.run_ids)
176
+ ```
177
+
178
+ `status=queued` 表示任务已入队。需要 worker 消费完成后,新的短记忆才会出现在查询结果中。
179
+
180
+ ## 触发长记忆整理
181
+
182
+ 长记忆用于沉淀跨会话可复用的信息,例如事实、偏好、流程和规则。
183
+
184
+ 适合触发时机:
185
+
186
+ - 会话结束时。
187
+ - 一个完整任务完成后。
188
+ - 用户明确表达稳定偏好时。
189
+ - 形成可复用流程、项目事实、团队规则时。
190
+ - 不建议对临时、未经确认、明显只对当前轮有效的信息触发长期整理。
191
+
192
+ ```python
193
+ run = client.consolidate_long_memory(
194
+ GenerateMemoryReq(
195
+ session_id=session.session_id,
196
+ trigger_source="session_end",
197
+ hints={"reason": "会话结束后沉淀长期记忆"},
198
+ )
199
+ )
200
+
201
+ print("long memory queued:", run.run_ids)
202
+ ```
203
+
204
+ ## 使用 AgentMemorySession 简化接入
205
+
206
+ `AgentMemorySession` 会自动创建 session、写标准事件,并缓存服务端返回的 `thread_id`,后续查询和生成会自动带上线程上下文。
207
+
208
+ ```python
209
+ import os
210
+
211
+ from agent_memory import AgentMemoryClient, AgentMemorySession
212
+
213
+ client = AgentMemoryClient(
214
+ base_url=os.environ["AGENT_MEMORY_BASE_URL"],
215
+ api_key=os.environ["AGENT_MEMORY_API_KEY"],
216
+ )
217
+
218
+ agent_memory = AgentMemorySession(
219
+ client,
220
+ agent_instance_id=os.environ["AGENT_MEMORY_AGENT_INSTANCE_ID"],
221
+ external_session_id="python-task-20260616-001",
222
+ external_thread_id="main",
223
+ )
224
+
225
+ agent_memory.record_user_message("请继续 SDK 接入任务。")
226
+
227
+ short_run = agent_memory.generate_short_memory("thread_end")
228
+ print("short memory queued:", short_run.run_ids)
229
+
230
+ memory_context = agent_memory.query_context(
231
+ query_text="继续 SDK 接入任务",
232
+ query_intent="execution_help",
233
+ desired_memory_types=["task_whiteboard", "tool_digest", "procedure"],
234
+ limit=6,
235
+ event_limit=3,
236
+ )
237
+
238
+ print(memory_context.text)
239
+ ```
240
+
241
+ ## 查看示例
242
+
243
+ ```bash
244
+ cd sdk/python
245
+ python examples/basic.py
246
+ python examples/agent_loop.py
247
+ ```
@@ -0,0 +1,38 @@
1
+ import os
2
+
3
+ from agent_memory import AgentMemoryClient, AgentMemorySession
4
+
5
+
6
+ def must_env(key: str) -> str:
7
+ value = os.getenv(key)
8
+ if not value:
9
+ raise SystemExit(f"{key} is required")
10
+ return value
11
+
12
+
13
+ client = AgentMemoryClient(
14
+ base_url=must_env("AGENT_MEMORY_BASE_URL"),
15
+ api_key=must_env("AGENT_MEMORY_API_KEY"),
16
+ )
17
+ session = AgentMemorySession(
18
+ client,
19
+ agent_instance_id=must_env("AGENT_MEMORY_AGENT_INSTANCE_ID"),
20
+ external_session_id="sdk-python-agent-loop-demo",
21
+ external_thread_id="main",
22
+ )
23
+ session_id = session.ensure_session()
24
+ print(f"session_id: {session_id}")
25
+ session.record_user_message("请继续当前 SDK 接入任务。")
26
+ short_run = session.generate_short_memory("thread_end")
27
+ print(f"short_memory_generation: status={short_run.status} run_ids={short_run.run_ids}")
28
+ memory_context = session.query_context(
29
+ query_text="继续 SDK 接入任务",
30
+ query_intent="execution_help",
31
+ desired_memory_types=["task_whiteboard", "tool_digest", "procedure"],
32
+ limit=6,
33
+ event_limit=3,
34
+ )
35
+ print(memory_context.text)
36
+ session.record_assistant_message("已根据记忆恢复任务上下文并继续执行。")
37
+ final_run = session.generate_short_memory("thread_end")
38
+ print(f"final_short_memory_generation: status={final_run.status} run_ids={final_run.run_ids}")
@@ -0,0 +1,55 @@
1
+ import os
2
+
3
+ from agent_memory import AgentMemoryClient
4
+ from agent_memory.adapter import format_memory_context
5
+ from agent_memory.types import (
6
+ CreateSessionReq,
7
+ GenerateMemoryReq,
8
+ MemoryQueryReq,
9
+ RecordEventReq,
10
+ )
11
+
12
+
13
+ def must_env(key: str) -> str:
14
+ value = os.getenv(key)
15
+ if not value:
16
+ raise SystemExit(f"{key} is required")
17
+ return value
18
+
19
+
20
+ client = AgentMemoryClient(
21
+ base_url=must_env("AGENT_MEMORY_BASE_URL"),
22
+ api_key=must_env("AGENT_MEMORY_API_KEY"),
23
+ )
24
+ session = client.create_session(
25
+ CreateSessionReq(
26
+ agent_instance_id=must_env("AGENT_MEMORY_AGENT_INSTANCE_ID"),
27
+ external_session_id="sdk-python-basic-demo",
28
+ metadata={"source": "sdk-python-example"},
29
+ )
30
+ )
31
+ client.record_event(
32
+ RecordEventReq(
33
+ session_id=session.session_id,
34
+ event_id="python-basic-user-001",
35
+ event_type="message",
36
+ actor="user",
37
+ status="success",
38
+ summary="Python SDK 示例写入用户消息。",
39
+ payload={"text": "请查询相关记忆"},
40
+ )
41
+ )
42
+ response = client.query_memories(
43
+ MemoryQueryReq(
44
+ session_id=session.session_id,
45
+ query_text="Python SDK 示例查询记忆",
46
+ query_intent="execution_help",
47
+ desired_memory_types=["task_whiteboard", "procedure"],
48
+ limit=5,
49
+ )
50
+ )
51
+ print(format_memory_context(response))
52
+ client.generate_short_memory(GenerateMemoryReq(session_id=session.session_id))
53
+ client.consolidate_long_memory(
54
+ GenerateMemoryReq(session_id=session.session_id, trigger_source="session_end")
55
+ )
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.25"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "agent-dreamer-sdk"
7
+ version = "0.0.3"
8
+ description = "Agent Memory HTTP SDK and lightweight Agent adapter"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = { text = "MIT" }
12
+ authors = [{ name = "Agent Memory Contributors" }]
13
+ keywords = ["agent", "memory", "sdk"]
14
+ dependencies = []
15
+
16
+ [project.optional-dependencies]
17
+ dev = ["pytest>=8.0", "ruff>=0.5"]
18
+
19
+ [tool.hatch.build.targets.wheel]
20
+ packages = ["src/agent_memory"]
21
+
22
+ [tool.pytest.ini_options]
23
+ testpaths = ["tests"]
24
+
25
+ [tool.ruff]
26
+ line-length = 100
27
+ target-version = "py310"
@@ -0,0 +1,9 @@
1
+ from .adapter import AgentMemorySession, MemoryContext
2
+ from .client import AgentMemoryAPIError, AgentMemoryClient
3
+
4
+ __all__ = [
5
+ "AgentMemoryAPIError",
6
+ "AgentMemoryClient",
7
+ "AgentMemorySession",
8
+ "MemoryContext",
9
+ ]